]> git.proxmox.com Git - mirror_qemu.git/blob - crypto/hash-gcrypt.c
crypto: implement sha224, sha384, sha512 and ripemd160 hashes
[mirror_qemu.git] / crypto / hash-gcrypt.c
1 /*
2 * QEMU Crypto hash algorithms
3 *
4 * Copyright (c) 2016 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 "gcrypt.h"
25
26
27 static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALG__MAX] = {
28 [QCRYPTO_HASH_ALG_MD5] = GCRY_MD_MD5,
29 [QCRYPTO_HASH_ALG_SHA1] = GCRY_MD_SHA1,
30 [QCRYPTO_HASH_ALG_SHA224] = GCRY_MD_SHA224,
31 [QCRYPTO_HASH_ALG_SHA256] = GCRY_MD_SHA256,
32 [QCRYPTO_HASH_ALG_SHA384] = GCRY_MD_SHA384,
33 [QCRYPTO_HASH_ALG_SHA512] = GCRY_MD_SHA512,
34 [QCRYPTO_HASH_ALG_RIPEMD160] = GCRY_MD_RMD160,
35 };
36
37 gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg)
38 {
39 if (alg < G_N_ELEMENTS(qcrypto_hash_alg_map) &&
40 qcrypto_hash_alg_map[alg] != GCRY_MD_NONE) {
41 return true;
42 }
43 return false;
44 }
45
46
47 int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
48 const struct iovec *iov,
49 size_t niov,
50 uint8_t **result,
51 size_t *resultlen,
52 Error **errp)
53 {
54 int i, ret;
55 gcry_md_hd_t md;
56 unsigned char *digest;
57
58 if (alg >= G_N_ELEMENTS(qcrypto_hash_alg_map) ||
59 qcrypto_hash_alg_map[alg] == GCRY_MD_NONE) {
60 error_setg(errp,
61 "Unknown hash algorithm %d",
62 alg);
63 return -1;
64 }
65
66 ret = gcry_md_open(&md, qcrypto_hash_alg_map[alg], 0);
67
68 if (ret < 0) {
69 error_setg(errp,
70 "Unable to initialize hash algorithm: %s",
71 gcry_strerror(ret));
72 return -1;
73 }
74
75 for (i = 0; i < niov; i++) {
76 gcry_md_write(md, iov[i].iov_base, iov[i].iov_len);
77 }
78
79 ret = gcry_md_get_algo_dlen(qcrypto_hash_alg_map[alg]);
80 if (ret <= 0) {
81 error_setg(errp,
82 "Unable to get hash length: %s",
83 gcry_strerror(ret));
84 goto error;
85 }
86 if (*resultlen == 0) {
87 *resultlen = ret;
88 *result = g_new0(uint8_t, *resultlen);
89 } else if (*resultlen != ret) {
90 error_setg(errp,
91 "Result buffer size %zu is smaller than hash %d",
92 *resultlen, ret);
93 goto error;
94 }
95
96 digest = gcry_md_read(md, 0);
97 if (!digest) {
98 error_setg(errp,
99 "No digest produced");
100 goto error;
101 }
102 memcpy(*result, digest, *resultlen);
103
104 gcry_md_close(md);
105 return 0;
106
107 error:
108 gcry_md_close(md);
109 return -1;
110 }