2 * QEMU Crypto hash algorithms
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "crypto/hash.h"
23 #ifdef CONFIG_GNUTLS_HASH
24 #include <gnutls/gnutls.h>
25 #include <gnutls/crypto.h>
27 static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALG_LAST] = {
28 [QCRYPTO_HASH_ALG_MD5] = GNUTLS_DIG_MD5,
29 [QCRYPTO_HASH_ALG_SHA1] = GNUTLS_DIG_SHA1,
30 [QCRYPTO_HASH_ALG_SHA256] = GNUTLS_DIG_SHA256,
33 gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg)
35 if (alg < G_N_ELEMENTS(qcrypto_hash_alg_map)) {
41 int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
42 const struct iovec *iov,
51 if (alg >= G_N_ELEMENTS(qcrypto_hash_alg_map)) {
53 "Unknown hash algorithm %d",
58 ret = gnutls_hash_init(&dig, qcrypto_hash_alg_map[alg]);
62 "Unable to initialize hash algorithm: %s",
63 gnutls_strerror(ret));
67 for (i = 0; i < niov; i++) {
68 ret = gnutls_hash(dig, iov[i].iov_base, iov[i].iov_len);
71 "Unable process hash data: %s",
72 gnutls_strerror(ret));
77 ret = gnutls_hash_get_len(qcrypto_hash_alg_map[alg]);
80 "Unable to get hash length: %s",
81 gnutls_strerror(ret));
84 if (*resultlen == 0) {
86 *result = g_new0(uint8_t, *resultlen);
87 } else if (*resultlen != ret) {
89 "Result buffer size %zu is smaller than hash %d",
94 gnutls_hash_deinit(dig, *result);
98 gnutls_hash_deinit(dig, NULL);
102 #else /* ! CONFIG_GNUTLS_HASH */
104 gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg G_GNUC_UNUSED)
109 int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
110 const struct iovec *iov G_GNUC_UNUSED,
111 size_t niov G_GNUC_UNUSED,
112 uint8_t **result G_GNUC_UNUSED,
113 size_t *resultlen G_GNUC_UNUSED,
117 "Hash algorithm %d not supported without GNUTLS",
122 #endif /* ! CONFIG_GNUTLS_HASH */
124 int qcrypto_hash_bytes(QCryptoHashAlgorithm alg,
131 struct iovec iov = { .iov_base = (char *)buf,
133 return qcrypto_hash_bytesv(alg, &iov, 1, result, resultlen, errp);
136 static const char hex[] = "0123456789abcdef";
138 int qcrypto_hash_digestv(QCryptoHashAlgorithm alg,
139 const struct iovec *iov,
144 uint8_t *result = NULL;
145 size_t resultlen = 0;
148 if (qcrypto_hash_bytesv(alg, iov, niov, &result, &resultlen, errp) < 0) {
152 *digest = g_new0(char, (resultlen * 2) + 1);
153 for (i = 0 ; i < resultlen ; i++) {
154 (*digest)[(i * 2)] = hex[(result[i] >> 4) & 0xf];
155 (*digest)[(i * 2) + 1] = hex[result[i] & 0xf];
157 (*digest)[resultlen * 2] = '\0';
162 int qcrypto_hash_digest(QCryptoHashAlgorithm alg,
168 struct iovec iov = { .iov_base = (char *)buf, .iov_len = len };
170 return qcrypto_hash_digestv(alg, &iov, 1, digest, errp);
173 int qcrypto_hash_base64v(QCryptoHashAlgorithm alg,
174 const struct iovec *iov,
179 uint8_t *result = NULL;
180 size_t resultlen = 0;
182 if (qcrypto_hash_bytesv(alg, iov, niov, &result, &resultlen, errp) < 0) {
186 *base64 = g_base64_encode(result, resultlen);
191 int qcrypto_hash_base64(QCryptoHashAlgorithm alg,
197 struct iovec iov = { .iov_base = (char *)buf, .iov_len = len };
199 return qcrypto_hash_base64v(alg, &iov, 1, base64, errp);