]> git.proxmox.com Git - mirror_qemu.git/blob - crypto/hash.c
Merge remote-tracking branch 'mreitz/tags/pull-block-2017-10-06' into queue-block
[mirror_qemu.git] / crypto / hash.c
1 /*
2 * QEMU Crypto hash algorithms
3 *
4 * Copyright (c) 2015 Red Hat, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "crypto/hash.h"
24 #include "hashpriv.h"
25
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,
34 };
35
36 size_t qcrypto_hash_digest_len(QCryptoHashAlgorithm alg)
37 {
38 assert(alg < G_N_ELEMENTS(qcrypto_hash_alg_size));
39 return qcrypto_hash_alg_size[alg];
40 }
41
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)
48 {
49 #ifdef CONFIG_AF_ALG
50 int ret;
51
52 ret = qcrypto_hash_afalg_driver.hash_bytesv(alg, iov, niov,
53 result, resultlen,
54 errp);
55 if (ret == 0) {
56 return ret;
57 }
58
59 /*
60 * TODO:
61 * Maybe we should treat some afalg errors as fatal
62 */
63 error_free(*errp);
64 #endif
65
66 return qcrypto_hash_lib_driver.hash_bytesv(alg, iov, niov,
67 result, resultlen,
68 errp);
69 }
70
71
72 int qcrypto_hash_bytes(QCryptoHashAlgorithm alg,
73 const char *buf,
74 size_t len,
75 uint8_t **result,
76 size_t *resultlen,
77 Error **errp)
78 {
79 struct iovec iov = { .iov_base = (char *)buf,
80 .iov_len = len };
81 return qcrypto_hash_bytesv(alg, &iov, 1, result, resultlen, errp);
82 }
83
84 static const char hex[] = "0123456789abcdef";
85
86 int qcrypto_hash_digestv(QCryptoHashAlgorithm alg,
87 const struct iovec *iov,
88 size_t niov,
89 char **digest,
90 Error **errp)
91 {
92 uint8_t *result = NULL;
93 size_t resultlen = 0;
94 size_t i;
95
96 if (qcrypto_hash_bytesv(alg, iov, niov, &result, &resultlen, errp) < 0) {
97 return -1;
98 }
99
100 *digest = g_new0(char, (resultlen * 2) + 1);
101 for (i = 0 ; i < resultlen ; i++) {
102 (*digest)[(i * 2)] = hex[(result[i] >> 4) & 0xf];
103 (*digest)[(i * 2) + 1] = hex[result[i] & 0xf];
104 }
105 (*digest)[resultlen * 2] = '\0';
106 g_free(result);
107 return 0;
108 }
109
110 int qcrypto_hash_digest(QCryptoHashAlgorithm alg,
111 const char *buf,
112 size_t len,
113 char **digest,
114 Error **errp)
115 {
116 struct iovec iov = { .iov_base = (char *)buf, .iov_len = len };
117
118 return qcrypto_hash_digestv(alg, &iov, 1, digest, errp);
119 }
120
121 int qcrypto_hash_base64v(QCryptoHashAlgorithm alg,
122 const struct iovec *iov,
123 size_t niov,
124 char **base64,
125 Error **errp)
126 {
127 uint8_t *result = NULL;
128 size_t resultlen = 0;
129
130 if (qcrypto_hash_bytesv(alg, iov, niov, &result, &resultlen, errp) < 0) {
131 return -1;
132 }
133
134 *base64 = g_base64_encode(result, resultlen);
135 g_free(result);
136 return 0;
137 }
138
139 int qcrypto_hash_base64(QCryptoHashAlgorithm alg,
140 const char *buf,
141 size_t len,
142 char **base64,
143 Error **errp)
144 {
145 struct iovec iov = { .iov_base = (char *)buf, .iov_len = len };
146
147 return qcrypto_hash_base64v(alg, &iov, 1, base64, errp);
148 }