blob: 8b08a9c6753147be54c6bc624c65bed241959505 [file] [log] [blame]
Daniel P. Berrange0c16c052016-03-11 18:09:22 +00001/*
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
Thomas Huthb7cbb872019-02-13 16:54:59 +01009 * version 2.1 of the License, or (at your option) any later version.
Daniel P. Berrange0c16c052016-03-11 18:09:22 +000010 *
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"
Longpeng(Mike)aa8efad2017-07-14 14:03:59 -040024#include "hashpriv.h"
Daniel P. Berrange0c16c052016-03-11 18:09:22 +000025#include <nettle/md5.h>
26#include <nettle/sha.h>
Daniel P. Berrange9164b892016-03-11 18:33:08 +000027#include <nettle/ripemd160.h>
Daniel P. Berrange0c16c052016-03-11 18:09:22 +000028
29typedef void (*qcrypto_nettle_init)(void *ctx);
30typedef void (*qcrypto_nettle_write)(void *ctx,
Daniel P. Berrangé115e4b72021-05-14 13:04:07 +010031 size_t len,
Daniel P. Berrange0c16c052016-03-11 18:09:22 +000032 const uint8_t *buf);
33typedef void (*qcrypto_nettle_result)(void *ctx,
Daniel P. Berrangé115e4b72021-05-14 13:04:07 +010034 size_t len,
Daniel P. Berrange0c16c052016-03-11 18:09:22 +000035 uint8_t *buf);
36
37union qcrypto_hash_ctx {
38 struct md5_ctx md5;
39 struct sha1_ctx sha1;
Daniel P. Berrange9164b892016-03-11 18:33:08 +000040 struct sha224_ctx sha224;
Daniel P. Berrange0c16c052016-03-11 18:09:22 +000041 struct sha256_ctx sha256;
Daniel P. Berrange9164b892016-03-11 18:33:08 +000042 struct sha384_ctx sha384;
43 struct sha512_ctx sha512;
44 struct ripemd160_ctx ripemd160;
Daniel P. Berrange0c16c052016-03-11 18:09:22 +000045};
46
47struct qcrypto_hash_alg {
48 qcrypto_nettle_init init;
49 qcrypto_nettle_write write;
50 qcrypto_nettle_result result;
51 size_t len;
52} qcrypto_hash_alg_map[] = {
Markus Armbrusteref834aa2024-09-04 13:18:28 +020053 [QCRYPTO_HASH_ALGO_MD5] = {
Daniel P. Berrange0c16c052016-03-11 18:09:22 +000054 .init = (qcrypto_nettle_init)md5_init,
55 .write = (qcrypto_nettle_write)md5_update,
56 .result = (qcrypto_nettle_result)md5_digest,
57 .len = MD5_DIGEST_SIZE,
58 },
Markus Armbrusteref834aa2024-09-04 13:18:28 +020059 [QCRYPTO_HASH_ALGO_SHA1] = {
Daniel P. Berrange0c16c052016-03-11 18:09:22 +000060 .init = (qcrypto_nettle_init)sha1_init,
61 .write = (qcrypto_nettle_write)sha1_update,
62 .result = (qcrypto_nettle_result)sha1_digest,
63 .len = SHA1_DIGEST_SIZE,
64 },
Markus Armbrusteref834aa2024-09-04 13:18:28 +020065 [QCRYPTO_HASH_ALGO_SHA224] = {
Daniel P. Berrange9164b892016-03-11 18:33:08 +000066 .init = (qcrypto_nettle_init)sha224_init,
67 .write = (qcrypto_nettle_write)sha224_update,
68 .result = (qcrypto_nettle_result)sha224_digest,
69 .len = SHA224_DIGEST_SIZE,
70 },
Markus Armbrusteref834aa2024-09-04 13:18:28 +020071 [QCRYPTO_HASH_ALGO_SHA256] = {
Daniel P. Berrange0c16c052016-03-11 18:09:22 +000072 .init = (qcrypto_nettle_init)sha256_init,
73 .write = (qcrypto_nettle_write)sha256_update,
74 .result = (qcrypto_nettle_result)sha256_digest,
75 .len = SHA256_DIGEST_SIZE,
76 },
Markus Armbrusteref834aa2024-09-04 13:18:28 +020077 [QCRYPTO_HASH_ALGO_SHA384] = {
Daniel P. Berrange9164b892016-03-11 18:33:08 +000078 .init = (qcrypto_nettle_init)sha384_init,
79 .write = (qcrypto_nettle_write)sha384_update,
80 .result = (qcrypto_nettle_result)sha384_digest,
81 .len = SHA384_DIGEST_SIZE,
82 },
Markus Armbrusteref834aa2024-09-04 13:18:28 +020083 [QCRYPTO_HASH_ALGO_SHA512] = {
Daniel P. Berrange9164b892016-03-11 18:33:08 +000084 .init = (qcrypto_nettle_init)sha512_init,
85 .write = (qcrypto_nettle_write)sha512_update,
86 .result = (qcrypto_nettle_result)sha512_digest,
87 .len = SHA512_DIGEST_SIZE,
88 },
Markus Armbrusteref834aa2024-09-04 13:18:28 +020089 [QCRYPTO_HASH_ALGO_RIPEMD160] = {
Daniel P. Berrange9164b892016-03-11 18:33:08 +000090 .init = (qcrypto_nettle_init)ripemd160_init,
91 .write = (qcrypto_nettle_write)ripemd160_update,
92 .result = (qcrypto_nettle_result)ripemd160_digest,
93 .len = RIPEMD160_DIGEST_SIZE,
94 },
Daniel P. Berrange0c16c052016-03-11 18:09:22 +000095};
96
Markus Armbrusteref834aa2024-09-04 13:18:28 +020097gboolean qcrypto_hash_supports(QCryptoHashAlgo alg)
Daniel P. Berrange0c16c052016-03-11 18:09:22 +000098{
99 if (alg < G_N_ELEMENTS(qcrypto_hash_alg_map) &&
100 qcrypto_hash_alg_map[alg].init != NULL) {
101 return true;
102 }
103 return false;
104}
105
106
Longpeng(Mike)aa8efad2017-07-14 14:03:59 -0400107static int
Markus Armbrusteref834aa2024-09-04 13:18:28 +0200108qcrypto_nettle_hash_bytesv(QCryptoHashAlgo alg,
Longpeng(Mike)aa8efad2017-07-14 14:03:59 -0400109 const struct iovec *iov,
110 size_t niov,
111 uint8_t **result,
112 size_t *resultlen,
113 Error **errp)
Daniel P. Berrange0c16c052016-03-11 18:09:22 +0000114{
Daniel P. Berrangéf8878492019-07-12 11:14:19 +0100115 size_t i;
Daniel P. Berrange0c16c052016-03-11 18:09:22 +0000116 union qcrypto_hash_ctx ctx;
117
Daniel P. Berrange76032892016-07-05 17:41:45 +0100118 if (!qcrypto_hash_supports(alg)) {
Daniel P. Berrange0c16c052016-03-11 18:09:22 +0000119 error_setg(errp,
120 "Unknown hash algorithm %d",
121 alg);
122 return -1;
123 }
124
125 qcrypto_hash_alg_map[alg].init(&ctx);
126
127 for (i = 0; i < niov; i++) {
128 /* Some versions of nettle have functions
129 * declared with 'int' instead of 'size_t'
130 * so to be safe avoid writing more than
131 * UINT_MAX bytes at a time
132 */
133 size_t len = iov[i].iov_len;
134 uint8_t *base = iov[i].iov_base;
135 while (len) {
136 size_t shortlen = MIN(len, UINT_MAX);
137 qcrypto_hash_alg_map[alg].write(&ctx, len, base);
138 len -= shortlen;
139 base += len;
140 }
141 }
142
143 if (*resultlen == 0) {
144 *resultlen = qcrypto_hash_alg_map[alg].len;
145 *result = g_new0(uint8_t, *resultlen);
146 } else if (*resultlen != qcrypto_hash_alg_map[alg].len) {
147 error_setg(errp,
148 "Result buffer size %zu is smaller than hash %zu",
149 *resultlen, qcrypto_hash_alg_map[alg].len);
150 return -1;
151 }
152
153 qcrypto_hash_alg_map[alg].result(&ctx, *resultlen, *result);
154
155 return 0;
156}
Longpeng(Mike)aa8efad2017-07-14 14:03:59 -0400157
158
159QCryptoHashDriver qcrypto_hash_lib_driver = {
160 .hash_bytesv = qcrypto_nettle_hash_bytesv,
161};