target/hppa: Fix comment
[qemu/kevin.git] / crypto / hash.c
blob8dab25d9ea20ae88f953aada027a049a536043fa
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"
24 #include "hashpriv.h"
26 static size_t qcrypto_hash_alg_size[QCRYPTO_HASH_ALG__MAX] = {
27 [QCRYPTO_HASH_ALG_MD5] = 16,
28 [QCRYPTO_HASH_ALG_SHA1] = 20,
29 [QCRYPTO_HASH_ALG_SHA224] = 28,
30 [QCRYPTO_HASH_ALG_SHA256] = 32,
31 [QCRYPTO_HASH_ALG_SHA384] = 48,
32 [QCRYPTO_HASH_ALG_SHA512] = 64,
33 [QCRYPTO_HASH_ALG_RIPEMD160] = 20,
36 size_t qcrypto_hash_digest_len(QCryptoHashAlgorithm alg)
38 assert(alg < G_N_ELEMENTS(qcrypto_hash_alg_size));
39 return qcrypto_hash_alg_size[alg];
42 int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
43 const struct iovec *iov,
44 size_t niov,
45 uint8_t **result,
46 size_t *resultlen,
47 Error **errp)
49 #ifdef CONFIG_AF_ALG
50 int ret;
52 * TODO:
53 * Maybe we should treat some afalg errors as fatal
55 ret = qcrypto_hash_afalg_driver.hash_bytesv(alg, iov, niov,
56 result, resultlen,
57 NULL);
58 if (ret == 0) {
59 return ret;
61 #endif
63 return qcrypto_hash_lib_driver.hash_bytesv(alg, iov, niov,
64 result, resultlen,
65 errp);
69 int qcrypto_hash_bytes(QCryptoHashAlgorithm alg,
70 const char *buf,
71 size_t len,
72 uint8_t **result,
73 size_t *resultlen,
74 Error **errp)
76 struct iovec iov = { .iov_base = (char *)buf,
77 .iov_len = len };
78 return qcrypto_hash_bytesv(alg, &iov, 1, result, resultlen, errp);
81 static const char hex[] = "0123456789abcdef";
83 int qcrypto_hash_digestv(QCryptoHashAlgorithm alg,
84 const struct iovec *iov,
85 size_t niov,
86 char **digest,
87 Error **errp)
89 uint8_t *result = NULL;
90 size_t resultlen = 0;
91 size_t i;
93 if (qcrypto_hash_bytesv(alg, iov, niov, &result, &resultlen, errp) < 0) {
94 return -1;
97 *digest = g_new0(char, (resultlen * 2) + 1);
98 for (i = 0 ; i < resultlen ; i++) {
99 (*digest)[(i * 2)] = hex[(result[i] >> 4) & 0xf];
100 (*digest)[(i * 2) + 1] = hex[result[i] & 0xf];
102 (*digest)[resultlen * 2] = '\0';
103 g_free(result);
104 return 0;
107 int qcrypto_hash_digest(QCryptoHashAlgorithm alg,
108 const char *buf,
109 size_t len,
110 char **digest,
111 Error **errp)
113 struct iovec iov = { .iov_base = (char *)buf, .iov_len = len };
115 return qcrypto_hash_digestv(alg, &iov, 1, digest, errp);
118 int qcrypto_hash_base64v(QCryptoHashAlgorithm alg,
119 const struct iovec *iov,
120 size_t niov,
121 char **base64,
122 Error **errp)
124 uint8_t *result = NULL;
125 size_t resultlen = 0;
127 if (qcrypto_hash_bytesv(alg, iov, niov, &result, &resultlen, errp) < 0) {
128 return -1;
131 *base64 = g_base64_encode(result, resultlen);
132 g_free(result);
133 return 0;
136 int qcrypto_hash_base64(QCryptoHashAlgorithm alg,
137 const char *buf,
138 size_t len,
139 char **base64,
140 Error **errp)
142 struct iovec iov = { .iov_base = (char *)buf, .iov_len = len };
144 return qcrypto_hash_base64v(alg, &iov, 1, base64, errp);