xics,xics_kvm: Handle CPU unplug correctly
[qemu.git] / crypto / hash.c
blob2907bffd2e74212654688c7b67f5697e9fe9514c
1 /*
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 "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "crypto/hash.h"
25 #ifdef CONFIG_GNUTLS_HASH
26 #include <gnutls/gnutls.h>
27 #include <gnutls/crypto.h>
28 #endif
31 static size_t qcrypto_hash_alg_size[QCRYPTO_HASH_ALG__MAX] = {
32 [QCRYPTO_HASH_ALG_MD5] = 16,
33 [QCRYPTO_HASH_ALG_SHA1] = 20,
34 [QCRYPTO_HASH_ALG_SHA256] = 32,
37 size_t qcrypto_hash_digest_len(QCryptoHashAlgorithm alg)
39 assert(alg < G_N_ELEMENTS(qcrypto_hash_alg_size));
40 return qcrypto_hash_alg_size[alg];
44 #ifdef CONFIG_GNUTLS_HASH
45 static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALG__MAX] = {
46 [QCRYPTO_HASH_ALG_MD5] = GNUTLS_DIG_MD5,
47 [QCRYPTO_HASH_ALG_SHA1] = GNUTLS_DIG_SHA1,
48 [QCRYPTO_HASH_ALG_SHA256] = GNUTLS_DIG_SHA256,
51 gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg)
53 if (alg < G_N_ELEMENTS(qcrypto_hash_alg_map)) {
54 return true;
56 return false;
60 int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
61 const struct iovec *iov,
62 size_t niov,
63 uint8_t **result,
64 size_t *resultlen,
65 Error **errp)
67 int i, ret;
68 gnutls_hash_hd_t dig;
70 if (alg >= G_N_ELEMENTS(qcrypto_hash_alg_map)) {
71 error_setg(errp,
72 "Unknown hash algorithm %d",
73 alg);
74 return -1;
77 ret = gnutls_hash_init(&dig, qcrypto_hash_alg_map[alg]);
79 if (ret < 0) {
80 error_setg(errp,
81 "Unable to initialize hash algorithm: %s",
82 gnutls_strerror(ret));
83 return -1;
86 for (i = 0; i < niov; i++) {
87 ret = gnutls_hash(dig, iov[i].iov_base, iov[i].iov_len);
88 if (ret < 0) {
89 error_setg(errp,
90 "Unable process hash data: %s",
91 gnutls_strerror(ret));
92 goto error;
96 ret = gnutls_hash_get_len(qcrypto_hash_alg_map[alg]);
97 if (ret <= 0) {
98 error_setg(errp,
99 "Unable to get hash length: %s",
100 gnutls_strerror(ret));
101 goto error;
103 if (*resultlen == 0) {
104 *resultlen = ret;
105 *result = g_new0(uint8_t, *resultlen);
106 } else if (*resultlen != ret) {
107 error_setg(errp,
108 "Result buffer size %zu is smaller than hash %d",
109 *resultlen, ret);
110 goto error;
113 gnutls_hash_deinit(dig, *result);
114 return 0;
116 error:
117 gnutls_hash_deinit(dig, NULL);
118 return -1;
121 #else /* ! CONFIG_GNUTLS_HASH */
123 gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg G_GNUC_UNUSED)
125 return false;
128 int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
129 const struct iovec *iov G_GNUC_UNUSED,
130 size_t niov G_GNUC_UNUSED,
131 uint8_t **result G_GNUC_UNUSED,
132 size_t *resultlen G_GNUC_UNUSED,
133 Error **errp)
135 error_setg(errp,
136 "Hash algorithm %d not supported without GNUTLS",
137 alg);
138 return -1;
141 #endif /* ! CONFIG_GNUTLS_HASH */
143 int qcrypto_hash_bytes(QCryptoHashAlgorithm alg,
144 const char *buf,
145 size_t len,
146 uint8_t **result,
147 size_t *resultlen,
148 Error **errp)
150 struct iovec iov = { .iov_base = (char *)buf,
151 .iov_len = len };
152 return qcrypto_hash_bytesv(alg, &iov, 1, result, resultlen, errp);
155 static const char hex[] = "0123456789abcdef";
157 int qcrypto_hash_digestv(QCryptoHashAlgorithm alg,
158 const struct iovec *iov,
159 size_t niov,
160 char **digest,
161 Error **errp)
163 uint8_t *result = NULL;
164 size_t resultlen = 0;
165 size_t i;
167 if (qcrypto_hash_bytesv(alg, iov, niov, &result, &resultlen, errp) < 0) {
168 return -1;
171 *digest = g_new0(char, (resultlen * 2) + 1);
172 for (i = 0 ; i < resultlen ; i++) {
173 (*digest)[(i * 2)] = hex[(result[i] >> 4) & 0xf];
174 (*digest)[(i * 2) + 1] = hex[result[i] & 0xf];
176 (*digest)[resultlen * 2] = '\0';
177 g_free(result);
178 return 0;
181 int qcrypto_hash_digest(QCryptoHashAlgorithm alg,
182 const char *buf,
183 size_t len,
184 char **digest,
185 Error **errp)
187 struct iovec iov = { .iov_base = (char *)buf, .iov_len = len };
189 return qcrypto_hash_digestv(alg, &iov, 1, digest, errp);
192 int qcrypto_hash_base64v(QCryptoHashAlgorithm alg,
193 const struct iovec *iov,
194 size_t niov,
195 char **base64,
196 Error **errp)
198 uint8_t *result = NULL;
199 size_t resultlen = 0;
201 if (qcrypto_hash_bytesv(alg, iov, niov, &result, &resultlen, errp) < 0) {
202 return -1;
205 *base64 = g_base64_encode(result, resultlen);
206 g_free(result);
207 return 0;
210 int qcrypto_hash_base64(QCryptoHashAlgorithm alg,
211 const char *buf,
212 size_t len,
213 char **base64,
214 Error **errp)
216 struct iovec iov = { .iov_base = (char *)buf, .iov_len = len };
218 return qcrypto_hash_base64v(alg, &iov, 1, base64, errp);