xref: /qemu/crypto/hmac-nettle.c (revision 33848cee)
1 /*
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"
18 #include <nettle/hmac.h>
19 
20 typedef void (*qcrypto_nettle_hmac_setkey)(void *ctx,
21               size_t key_length, const uint8_t *key);
22 
23 typedef void (*qcrypto_nettle_hmac_update)(void *ctx,
24               size_t length, const uint8_t *data);
25 
26 typedef void (*qcrypto_nettle_hmac_digest)(void *ctx,
27               size_t length, uint8_t *digest);
28 
29 typedef struct QCryptoHmacNettle QCryptoHmacNettle;
30 struct QCryptoHmacNettle {
31     union qcrypto_nettle_hmac_ctx {
32         struct hmac_md5_ctx md5_ctx;
33         struct hmac_sha1_ctx sha1_ctx;
34         struct hmac_sha256_ctx sha256_ctx; /* equals hmac_sha224_ctx */
35         struct hmac_sha512_ctx sha512_ctx; /* equals hmac_sha384_ctx */
36         struct hmac_ripemd160_ctx ripemd160_ctx;
37     } u;
38 };
39 
40 struct qcrypto_nettle_hmac_alg {
41     qcrypto_nettle_hmac_setkey setkey;
42     qcrypto_nettle_hmac_update update;
43     qcrypto_nettle_hmac_digest digest;
44     size_t len;
45 } qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX] = {
46     [QCRYPTO_HASH_ALG_MD5] = {
47         .setkey = (qcrypto_nettle_hmac_setkey)hmac_md5_set_key,
48         .update = (qcrypto_nettle_hmac_update)hmac_md5_update,
49         .digest = (qcrypto_nettle_hmac_digest)hmac_md5_digest,
50         .len = MD5_DIGEST_SIZE,
51     },
52     [QCRYPTO_HASH_ALG_SHA1] = {
53         .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha1_set_key,
54         .update = (qcrypto_nettle_hmac_update)hmac_sha1_update,
55         .digest = (qcrypto_nettle_hmac_digest)hmac_sha1_digest,
56         .len = SHA1_DIGEST_SIZE,
57     },
58     [QCRYPTO_HASH_ALG_SHA224] = {
59         .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha224_set_key,
60         .update = (qcrypto_nettle_hmac_update)hmac_sha224_update,
61         .digest = (qcrypto_nettle_hmac_digest)hmac_sha224_digest,
62         .len = SHA224_DIGEST_SIZE,
63     },
64     [QCRYPTO_HASH_ALG_SHA256] = {
65         .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha256_set_key,
66         .update = (qcrypto_nettle_hmac_update)hmac_sha256_update,
67         .digest = (qcrypto_nettle_hmac_digest)hmac_sha256_digest,
68         .len = SHA256_DIGEST_SIZE,
69     },
70     [QCRYPTO_HASH_ALG_SHA384] = {
71         .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha384_set_key,
72         .update = (qcrypto_nettle_hmac_update)hmac_sha384_update,
73         .digest = (qcrypto_nettle_hmac_digest)hmac_sha384_digest,
74         .len = SHA384_DIGEST_SIZE,
75     },
76     [QCRYPTO_HASH_ALG_SHA512] = {
77         .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha512_set_key,
78         .update = (qcrypto_nettle_hmac_update)hmac_sha512_update,
79         .digest = (qcrypto_nettle_hmac_digest)hmac_sha512_digest,
80         .len = SHA512_DIGEST_SIZE,
81     },
82     [QCRYPTO_HASH_ALG_RIPEMD160] = {
83         .setkey = (qcrypto_nettle_hmac_setkey)hmac_ripemd160_set_key,
84         .update = (qcrypto_nettle_hmac_update)hmac_ripemd160_update,
85         .digest = (qcrypto_nettle_hmac_digest)hmac_ripemd160_digest,
86         .len = RIPEMD160_DIGEST_SIZE,
87     },
88 };
89 
90 bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg)
91 {
92     if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) &&
93         qcrypto_hmac_alg_map[alg].setkey != NULL) {
94         return true;
95     }
96 
97     return false;
98 }
99 
100 QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg,
101                               const uint8_t *key, size_t nkey,
102                               Error **errp)
103 {
104     QCryptoHmac *hmac;
105     QCryptoHmacNettle *ctx;
106 
107     if (!qcrypto_hmac_supports(alg)) {
108         error_setg(errp, "Unsupported hmac algorithm %s",
109                    QCryptoHashAlgorithm_lookup[alg]);
110         return NULL;
111     }
112 
113     hmac = g_new0(QCryptoHmac, 1);
114     hmac->alg = alg;
115 
116     ctx = g_new0(QCryptoHmacNettle, 1);
117 
118     qcrypto_hmac_alg_map[alg].setkey(&ctx->u, nkey, key);
119 
120     hmac->opaque = ctx;
121 
122     return hmac;
123 }
124 
125 void qcrypto_hmac_free(QCryptoHmac *hmac)
126 {
127     QCryptoHmacNettle *ctx;
128 
129     if (!hmac) {
130         return;
131     }
132 
133     ctx = hmac->opaque;
134 
135     g_free(ctx);
136     g_free(hmac);
137 }
138 
139 int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
140                         const struct iovec *iov,
141                         size_t niov,
142                         uint8_t **result,
143                         size_t *resultlen,
144                         Error **errp)
145 {
146     QCryptoHmacNettle *ctx;
147     int i;
148 
149     ctx = (QCryptoHmacNettle *)hmac->opaque;
150 
151     for (i = 0; i < niov; ++i) {
152         size_t len = iov[i].iov_len;
153         uint8_t *base = iov[i].iov_base;
154         while (len) {
155             size_t shortlen = MIN(len, UINT_MAX);
156             qcrypto_hmac_alg_map[hmac->alg].update(&ctx->u, len, base);
157             len -= shortlen;
158             base += len;
159         }
160     }
161 
162     if (*resultlen == 0) {
163         *resultlen = qcrypto_hmac_alg_map[hmac->alg].len;
164         *result = g_new0(uint8_t, *resultlen);
165     } else if (*resultlen != qcrypto_hmac_alg_map[hmac->alg].len) {
166         error_setg(errp,
167                    "Result buffer size %zu is smaller than hash %zu",
168                    *resultlen, qcrypto_hmac_alg_map[hmac->alg].len);
169         return -1;
170     }
171 
172     qcrypto_hmac_alg_map[hmac->alg].digest(&ctx->u, *resultlen, *result);
173 
174     return 0;
175 }
176