blob: 1ad6c4f253069223b6a479bca25b25e858030918 [file] [log] [blame]
Longpeng(Mike)12a4f212016-12-13 18:42:56 +08001/*
2 * QEMU Crypto hmac algorithms (based on nettle)
3 *
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
5 *
6 * Authors:
7 * Longpeng(Mike) <longpeng2@huawei.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or
10 * (at your option) any later version. See the COPYING file in the
11 * top-level directory.
12 *
13 */
14
15#include "qemu/osdep.h"
16#include "qapi/error.h"
17#include "crypto/hmac.h"
Longpeng(Mike)14a5a2a2017-07-14 14:04:04 -040018#include "hmacpriv.h"
Longpeng(Mike)12a4f212016-12-13 18:42:56 +080019#include <nettle/hmac.h>
20
Longpeng(Mike)f4d76742016-12-13 18:42:59 +080021typedef void (*qcrypto_nettle_hmac_setkey)(void *ctx,
Daniel P. Berrangé115e4b72021-05-14 13:04:07 +010022 size_t key_length,
Daniel P. Berrangéf8878492019-07-12 11:14:19 +010023 const uint8_t *key);
Longpeng(Mike)f4d76742016-12-13 18:42:59 +080024
25typedef void (*qcrypto_nettle_hmac_update)(void *ctx,
Daniel P. Berrangé115e4b72021-05-14 13:04:07 +010026 size_t length,
Daniel P. Berrangéf8878492019-07-12 11:14:19 +010027 const uint8_t *data);
Longpeng(Mike)f4d76742016-12-13 18:42:59 +080028
29typedef void (*qcrypto_nettle_hmac_digest)(void *ctx,
Daniel P. Berrangé115e4b72021-05-14 13:04:07 +010030 size_t length,
Daniel P. Berrangéf8878492019-07-12 11:14:19 +010031 uint8_t *digest);
Longpeng(Mike)f4d76742016-12-13 18:42:59 +080032
33typedef struct QCryptoHmacNettle QCryptoHmacNettle;
34struct QCryptoHmacNettle {
35 union qcrypto_nettle_hmac_ctx {
36 struct hmac_md5_ctx md5_ctx;
37 struct hmac_sha1_ctx sha1_ctx;
38 struct hmac_sha256_ctx sha256_ctx; /* equals hmac_sha224_ctx */
39 struct hmac_sha512_ctx sha512_ctx; /* equals hmac_sha384_ctx */
40 struct hmac_ripemd160_ctx ripemd160_ctx;
41 } u;
42};
43
44struct qcrypto_nettle_hmac_alg {
45 qcrypto_nettle_hmac_setkey setkey;
46 qcrypto_nettle_hmac_update update;
47 qcrypto_nettle_hmac_digest digest;
48 size_t len;
49} qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX] = {
50 [QCRYPTO_HASH_ALG_MD5] = {
51 .setkey = (qcrypto_nettle_hmac_setkey)hmac_md5_set_key,
52 .update = (qcrypto_nettle_hmac_update)hmac_md5_update,
53 .digest = (qcrypto_nettle_hmac_digest)hmac_md5_digest,
54 .len = MD5_DIGEST_SIZE,
55 },
56 [QCRYPTO_HASH_ALG_SHA1] = {
57 .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha1_set_key,
58 .update = (qcrypto_nettle_hmac_update)hmac_sha1_update,
59 .digest = (qcrypto_nettle_hmac_digest)hmac_sha1_digest,
60 .len = SHA1_DIGEST_SIZE,
61 },
62 [QCRYPTO_HASH_ALG_SHA224] = {
63 .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha224_set_key,
64 .update = (qcrypto_nettle_hmac_update)hmac_sha224_update,
65 .digest = (qcrypto_nettle_hmac_digest)hmac_sha224_digest,
66 .len = SHA224_DIGEST_SIZE,
67 },
68 [QCRYPTO_HASH_ALG_SHA256] = {
69 .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha256_set_key,
70 .update = (qcrypto_nettle_hmac_update)hmac_sha256_update,
71 .digest = (qcrypto_nettle_hmac_digest)hmac_sha256_digest,
72 .len = SHA256_DIGEST_SIZE,
73 },
74 [QCRYPTO_HASH_ALG_SHA384] = {
75 .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha384_set_key,
76 .update = (qcrypto_nettle_hmac_update)hmac_sha384_update,
77 .digest = (qcrypto_nettle_hmac_digest)hmac_sha384_digest,
78 .len = SHA384_DIGEST_SIZE,
79 },
80 [QCRYPTO_HASH_ALG_SHA512] = {
81 .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha512_set_key,
82 .update = (qcrypto_nettle_hmac_update)hmac_sha512_update,
83 .digest = (qcrypto_nettle_hmac_digest)hmac_sha512_digest,
84 .len = SHA512_DIGEST_SIZE,
85 },
86 [QCRYPTO_HASH_ALG_RIPEMD160] = {
87 .setkey = (qcrypto_nettle_hmac_setkey)hmac_ripemd160_set_key,
88 .update = (qcrypto_nettle_hmac_update)hmac_ripemd160_update,
89 .digest = (qcrypto_nettle_hmac_digest)hmac_ripemd160_digest,
90 .len = RIPEMD160_DIGEST_SIZE,
91 },
92};
93
Longpeng(Mike)12a4f212016-12-13 18:42:56 +080094bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg)
95{
Longpeng(Mike)f4d76742016-12-13 18:42:59 +080096 if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) &&
97 qcrypto_hmac_alg_map[alg].setkey != NULL) {
98 return true;
99 }
100
Longpeng(Mike)12a4f212016-12-13 18:42:56 +0800101 return false;
102}
103
Longpeng(Mike)14a5a2a2017-07-14 14:04:04 -0400104void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
105 const uint8_t *key, size_t nkey,
106 Error **errp)
Longpeng(Mike)12a4f212016-12-13 18:42:56 +0800107{
Longpeng(Mike)f4d76742016-12-13 18:42:59 +0800108 QCryptoHmacNettle *ctx;
109
110 if (!qcrypto_hmac_supports(alg)) {
111 error_setg(errp, "Unsupported hmac algorithm %s",
Markus Armbruster977c7362017-08-24 10:46:08 +0200112 QCryptoHashAlgorithm_str(alg));
Longpeng(Mike)f4d76742016-12-13 18:42:59 +0800113 return NULL;
114 }
115
Longpeng(Mike)f4d76742016-12-13 18:42:59 +0800116 ctx = g_new0(QCryptoHmacNettle, 1);
117
118 qcrypto_hmac_alg_map[alg].setkey(&ctx->u, nkey, key);
119
Longpeng(Mike)8c2776d2017-07-14 14:04:02 -0400120 return ctx;
Longpeng(Mike)12a4f212016-12-13 18:42:56 +0800121}
122
Longpeng(Mike)14a5a2a2017-07-14 14:04:04 -0400123static void
124qcrypto_nettle_hmac_ctx_free(QCryptoHmac *hmac)
Longpeng(Mike)12a4f212016-12-13 18:42:56 +0800125{
Longpeng(Mike)f4d76742016-12-13 18:42:59 +0800126 QCryptoHmacNettle *ctx;
127
Longpeng(Mike)f4d76742016-12-13 18:42:59 +0800128 ctx = hmac->opaque;
Longpeng(Mike)f4d76742016-12-13 18:42:59 +0800129 g_free(ctx);
Longpeng(Mike)12a4f212016-12-13 18:42:56 +0800130}
131
Longpeng(Mike)14a5a2a2017-07-14 14:04:04 -0400132static int
133qcrypto_nettle_hmac_bytesv(QCryptoHmac *hmac,
134 const struct iovec *iov,
135 size_t niov,
136 uint8_t **result,
137 size_t *resultlen,
138 Error **errp)
Longpeng(Mike)12a4f212016-12-13 18:42:56 +0800139{
Longpeng(Mike)f4d76742016-12-13 18:42:59 +0800140 QCryptoHmacNettle *ctx;
Daniel P. Berrangéf8878492019-07-12 11:14:19 +0100141 size_t i;
Longpeng(Mike)f4d76742016-12-13 18:42:59 +0800142
143 ctx = (QCryptoHmacNettle *)hmac->opaque;
144
145 for (i = 0; i < niov; ++i) {
146 size_t len = iov[i].iov_len;
147 uint8_t *base = iov[i].iov_base;
148 while (len) {
149 size_t shortlen = MIN(len, UINT_MAX);
150 qcrypto_hmac_alg_map[hmac->alg].update(&ctx->u, len, base);
151 len -= shortlen;
152 base += len;
153 }
154 }
155
156 if (*resultlen == 0) {
157 *resultlen = qcrypto_hmac_alg_map[hmac->alg].len;
158 *result = g_new0(uint8_t, *resultlen);
159 } else if (*resultlen != qcrypto_hmac_alg_map[hmac->alg].len) {
160 error_setg(errp,
161 "Result buffer size %zu is smaller than hash %zu",
162 *resultlen, qcrypto_hmac_alg_map[hmac->alg].len);
163 return -1;
164 }
165
166 qcrypto_hmac_alg_map[hmac->alg].digest(&ctx->u, *resultlen, *result);
167
168 return 0;
Longpeng(Mike)12a4f212016-12-13 18:42:56 +0800169}
Longpeng(Mike)8c2776d2017-07-14 14:04:02 -0400170
Longpeng(Mike)14a5a2a2017-07-14 14:04:04 -0400171QCryptoHmacDriver qcrypto_hmac_lib_driver = {
172 .hmac_bytesv = qcrypto_nettle_hmac_bytesv,
173 .hmac_free = qcrypto_nettle_hmac_ctx_free,
174};