xref: /openbmc/qemu/crypto/hmac-nettle.c (revision 731d58b545ef66072d38b428fe0dcd1d691e364c)
112a4f216SLongpeng(Mike) /*
212a4f216SLongpeng(Mike)  * QEMU Crypto hmac algorithms (based on nettle)
312a4f216SLongpeng(Mike)  *
412a4f216SLongpeng(Mike)  * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
512a4f216SLongpeng(Mike)  *
612a4f216SLongpeng(Mike)  * Authors:
712a4f216SLongpeng(Mike)  *    Longpeng(Mike) <longpeng2@huawei.com>
812a4f216SLongpeng(Mike)  *
912a4f216SLongpeng(Mike)  * This work is licensed under the terms of the GNU GPL, version 2 or
1012a4f216SLongpeng(Mike)  * (at your option) any later version.  See the COPYING file in the
1112a4f216SLongpeng(Mike)  * top-level directory.
1212a4f216SLongpeng(Mike)  *
1312a4f216SLongpeng(Mike)  */
1412a4f216SLongpeng(Mike) 
1512a4f216SLongpeng(Mike) #include "qemu/osdep.h"
1612a4f216SLongpeng(Mike) #include "qapi/error.h"
1712a4f216SLongpeng(Mike) #include "crypto/hmac.h"
1814a5a2aeSLongpeng(Mike) #include "hmacpriv.h"
1912a4f216SLongpeng(Mike) #include <nettle/hmac.h>
2012a4f216SLongpeng(Mike) 
21f4d76747SLongpeng(Mike) typedef void (*qcrypto_nettle_hmac_setkey)(void *ctx,
22115e4b70SDaniel P. Berrangé                                            size_t key_length,
23f8878490SDaniel P. Berrangé                                            const uint8_t *key);
24f4d76747SLongpeng(Mike) 
25f4d76747SLongpeng(Mike) typedef void (*qcrypto_nettle_hmac_update)(void *ctx,
26115e4b70SDaniel P. Berrangé                                            size_t length,
27f8878490SDaniel P. Berrangé                                            const uint8_t *data);
28f4d76747SLongpeng(Mike) 
29f4d76747SLongpeng(Mike) typedef void (*qcrypto_nettle_hmac_digest)(void *ctx,
30115e4b70SDaniel P. Berrangé                                            size_t length,
31f8878490SDaniel P. Berrangé                                            uint8_t *digest);
32f4d76747SLongpeng(Mike) 
33f4d76747SLongpeng(Mike) typedef struct QCryptoHmacNettle QCryptoHmacNettle;
34f4d76747SLongpeng(Mike) struct QCryptoHmacNettle {
35f4d76747SLongpeng(Mike)     union qcrypto_nettle_hmac_ctx {
36f4d76747SLongpeng(Mike)         struct hmac_md5_ctx md5_ctx;
37f4d76747SLongpeng(Mike)         struct hmac_sha1_ctx sha1_ctx;
38f4d76747SLongpeng(Mike)         struct hmac_sha256_ctx sha256_ctx; /* equals hmac_sha224_ctx */
39f4d76747SLongpeng(Mike)         struct hmac_sha512_ctx sha512_ctx; /* equals hmac_sha384_ctx */
40f4d76747SLongpeng(Mike)         struct hmac_ripemd160_ctx ripemd160_ctx;
41*d078da86Sliequan che #ifdef CONFIG_CRYPTO_SM3
42*d078da86Sliequan che  struct hmac_sm3_ctx ctx;
43*d078da86Sliequan che #endif
44f4d76747SLongpeng(Mike)     } u;
45f4d76747SLongpeng(Mike) };
46f4d76747SLongpeng(Mike) 
47f4d76747SLongpeng(Mike) struct qcrypto_nettle_hmac_alg {
48f4d76747SLongpeng(Mike)     qcrypto_nettle_hmac_setkey setkey;
49f4d76747SLongpeng(Mike)     qcrypto_nettle_hmac_update update;
50f4d76747SLongpeng(Mike)     qcrypto_nettle_hmac_digest digest;
51f4d76747SLongpeng(Mike)     size_t len;
52ef834aa2SMarkus Armbruster } qcrypto_hmac_alg_map[QCRYPTO_HASH_ALGO__MAX] = {
53ef834aa2SMarkus Armbruster     [QCRYPTO_HASH_ALGO_MD5] = {
54f4d76747SLongpeng(Mike)         .setkey = (qcrypto_nettle_hmac_setkey)hmac_md5_set_key,
55f4d76747SLongpeng(Mike)         .update = (qcrypto_nettle_hmac_update)hmac_md5_update,
56f4d76747SLongpeng(Mike)         .digest = (qcrypto_nettle_hmac_digest)hmac_md5_digest,
57f4d76747SLongpeng(Mike)         .len = MD5_DIGEST_SIZE,
58f4d76747SLongpeng(Mike)     },
59ef834aa2SMarkus Armbruster     [QCRYPTO_HASH_ALGO_SHA1] = {
60f4d76747SLongpeng(Mike)         .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha1_set_key,
61f4d76747SLongpeng(Mike)         .update = (qcrypto_nettle_hmac_update)hmac_sha1_update,
62f4d76747SLongpeng(Mike)         .digest = (qcrypto_nettle_hmac_digest)hmac_sha1_digest,
63f4d76747SLongpeng(Mike)         .len = SHA1_DIGEST_SIZE,
64f4d76747SLongpeng(Mike)     },
65ef834aa2SMarkus Armbruster     [QCRYPTO_HASH_ALGO_SHA224] = {
66f4d76747SLongpeng(Mike)         .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha224_set_key,
67f4d76747SLongpeng(Mike)         .update = (qcrypto_nettle_hmac_update)hmac_sha224_update,
68f4d76747SLongpeng(Mike)         .digest = (qcrypto_nettle_hmac_digest)hmac_sha224_digest,
69f4d76747SLongpeng(Mike)         .len = SHA224_DIGEST_SIZE,
70f4d76747SLongpeng(Mike)     },
71ef834aa2SMarkus Armbruster     [QCRYPTO_HASH_ALGO_SHA256] = {
72f4d76747SLongpeng(Mike)         .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha256_set_key,
73f4d76747SLongpeng(Mike)         .update = (qcrypto_nettle_hmac_update)hmac_sha256_update,
74f4d76747SLongpeng(Mike)         .digest = (qcrypto_nettle_hmac_digest)hmac_sha256_digest,
75f4d76747SLongpeng(Mike)         .len = SHA256_DIGEST_SIZE,
76f4d76747SLongpeng(Mike)     },
77ef834aa2SMarkus Armbruster     [QCRYPTO_HASH_ALGO_SHA384] = {
78f4d76747SLongpeng(Mike)         .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha384_set_key,
79f4d76747SLongpeng(Mike)         .update = (qcrypto_nettle_hmac_update)hmac_sha384_update,
80f4d76747SLongpeng(Mike)         .digest = (qcrypto_nettle_hmac_digest)hmac_sha384_digest,
81f4d76747SLongpeng(Mike)         .len = SHA384_DIGEST_SIZE,
82f4d76747SLongpeng(Mike)     },
83ef834aa2SMarkus Armbruster     [QCRYPTO_HASH_ALGO_SHA512] = {
84f4d76747SLongpeng(Mike)         .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha512_set_key,
85f4d76747SLongpeng(Mike)         .update = (qcrypto_nettle_hmac_update)hmac_sha512_update,
86f4d76747SLongpeng(Mike)         .digest = (qcrypto_nettle_hmac_digest)hmac_sha512_digest,
87f4d76747SLongpeng(Mike)         .len = SHA512_DIGEST_SIZE,
88f4d76747SLongpeng(Mike)     },
89ef834aa2SMarkus Armbruster     [QCRYPTO_HASH_ALGO_RIPEMD160] = {
90f4d76747SLongpeng(Mike)         .setkey = (qcrypto_nettle_hmac_setkey)hmac_ripemd160_set_key,
91f4d76747SLongpeng(Mike)         .update = (qcrypto_nettle_hmac_update)hmac_ripemd160_update,
92f4d76747SLongpeng(Mike)         .digest = (qcrypto_nettle_hmac_digest)hmac_ripemd160_digest,
93f4d76747SLongpeng(Mike)         .len = RIPEMD160_DIGEST_SIZE,
94f4d76747SLongpeng(Mike)     },
95*d078da86Sliequan che #ifdef CONFIG_CRYPTO_SM3
96*d078da86Sliequan che     [QCRYPTO_HASH_ALGO_SM3] = {
97*d078da86Sliequan che         .setkey = (qcrypto_nettle_hmac_setkey)hmac_sm3_set_key,
98*d078da86Sliequan che         .update = (qcrypto_nettle_hmac_update)hmac_sm3_update,
99*d078da86Sliequan che         .digest = (qcrypto_nettle_hmac_digest)hmac_sm3_digest,
100*d078da86Sliequan che         .len = SM3_DIGEST_SIZE,
101*d078da86Sliequan che     },
102*d078da86Sliequan che #endif
103f4d76747SLongpeng(Mike) };
104f4d76747SLongpeng(Mike) 
qcrypto_hmac_supports(QCryptoHashAlgo alg)105ef834aa2SMarkus Armbruster bool qcrypto_hmac_supports(QCryptoHashAlgo alg)
10612a4f216SLongpeng(Mike) {
107f4d76747SLongpeng(Mike)     if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) &&
108f4d76747SLongpeng(Mike)         qcrypto_hmac_alg_map[alg].setkey != NULL) {
109f4d76747SLongpeng(Mike)         return true;
110f4d76747SLongpeng(Mike)     }
111f4d76747SLongpeng(Mike) 
11212a4f216SLongpeng(Mike)     return false;
11312a4f216SLongpeng(Mike) }
11412a4f216SLongpeng(Mike) 
qcrypto_hmac_ctx_new(QCryptoHashAlgo alg,const uint8_t * key,size_t nkey,Error ** errp)115ef834aa2SMarkus Armbruster void *qcrypto_hmac_ctx_new(QCryptoHashAlgo alg,
11612a4f216SLongpeng(Mike)                            const uint8_t *key, size_t nkey,
11712a4f216SLongpeng(Mike)                            Error **errp)
11812a4f216SLongpeng(Mike) {
119f4d76747SLongpeng(Mike)     QCryptoHmacNettle *ctx;
120f4d76747SLongpeng(Mike) 
121f4d76747SLongpeng(Mike)     if (!qcrypto_hmac_supports(alg)) {
122f4d76747SLongpeng(Mike)         error_setg(errp, "Unsupported hmac algorithm %s",
123ef834aa2SMarkus Armbruster                    QCryptoHashAlgo_str(alg));
12412a4f216SLongpeng(Mike)         return NULL;
12512a4f216SLongpeng(Mike)     }
12612a4f216SLongpeng(Mike) 
127f4d76747SLongpeng(Mike)     ctx = g_new0(QCryptoHmacNettle, 1);
128f4d76747SLongpeng(Mike) 
129f4d76747SLongpeng(Mike)     qcrypto_hmac_alg_map[alg].setkey(&ctx->u, nkey, key);
130f4d76747SLongpeng(Mike) 
1318c2776d8SLongpeng(Mike)     return ctx;
132f4d76747SLongpeng(Mike) }
133f4d76747SLongpeng(Mike) 
13414a5a2aeSLongpeng(Mike) static void
qcrypto_nettle_hmac_ctx_free(QCryptoHmac * hmac)13514a5a2aeSLongpeng(Mike) qcrypto_nettle_hmac_ctx_free(QCryptoHmac *hmac)
13612a4f216SLongpeng(Mike) {
137f4d76747SLongpeng(Mike)     QCryptoHmacNettle *ctx;
138f4d76747SLongpeng(Mike) 
139f4d76747SLongpeng(Mike)     ctx = hmac->opaque;
140f4d76747SLongpeng(Mike)     g_free(ctx);
141f4d76747SLongpeng(Mike) }
142f4d76747SLongpeng(Mike) 
14314a5a2aeSLongpeng(Mike) static int
qcrypto_nettle_hmac_bytesv(QCryptoHmac * hmac,const struct iovec * iov,size_t niov,uint8_t ** result,size_t * resultlen,Error ** errp)14414a5a2aeSLongpeng(Mike) qcrypto_nettle_hmac_bytesv(QCryptoHmac *hmac,
14512a4f216SLongpeng(Mike)                            const struct iovec *iov,
14612a4f216SLongpeng(Mike)                            size_t niov,
14712a4f216SLongpeng(Mike)                            uint8_t **result,
14812a4f216SLongpeng(Mike)                            size_t *resultlen,
14912a4f216SLongpeng(Mike)                            Error **errp)
15012a4f216SLongpeng(Mike) {
151f4d76747SLongpeng(Mike)     QCryptoHmacNettle *ctx;
152f8878490SDaniel P. Berrangé     size_t i;
153f4d76747SLongpeng(Mike) 
154f4d76747SLongpeng(Mike)     ctx = (QCryptoHmacNettle *)hmac->opaque;
155f4d76747SLongpeng(Mike) 
156f4d76747SLongpeng(Mike)     for (i = 0; i < niov; ++i) {
157f4d76747SLongpeng(Mike)         size_t len = iov[i].iov_len;
158f4d76747SLongpeng(Mike)         uint8_t *base = iov[i].iov_base;
159f4d76747SLongpeng(Mike)         while (len) {
160f4d76747SLongpeng(Mike)             size_t shortlen = MIN(len, UINT_MAX);
161f4d76747SLongpeng(Mike)             qcrypto_hmac_alg_map[hmac->alg].update(&ctx->u, len, base);
162f4d76747SLongpeng(Mike)             len -= shortlen;
163f4d76747SLongpeng(Mike)             base += len;
164f4d76747SLongpeng(Mike)         }
165f4d76747SLongpeng(Mike)     }
166f4d76747SLongpeng(Mike) 
167f4d76747SLongpeng(Mike)     if (*resultlen == 0) {
168f4d76747SLongpeng(Mike)         *resultlen = qcrypto_hmac_alg_map[hmac->alg].len;
169f4d76747SLongpeng(Mike)         *result = g_new0(uint8_t, *resultlen);
170f4d76747SLongpeng(Mike)     } else if (*resultlen != qcrypto_hmac_alg_map[hmac->alg].len) {
171f4d76747SLongpeng(Mike)         error_setg(errp,
172f4d76747SLongpeng(Mike)                    "Result buffer size %zu is smaller than hash %zu",
173f4d76747SLongpeng(Mike)                    *resultlen, qcrypto_hmac_alg_map[hmac->alg].len);
17412a4f216SLongpeng(Mike)         return -1;
17512a4f216SLongpeng(Mike)     }
176f4d76747SLongpeng(Mike) 
177f4d76747SLongpeng(Mike)     qcrypto_hmac_alg_map[hmac->alg].digest(&ctx->u, *resultlen, *result);
178f4d76747SLongpeng(Mike) 
179f4d76747SLongpeng(Mike)     return 0;
180f4d76747SLongpeng(Mike) }
1818c2776d8SLongpeng(Mike) 
18214a5a2aeSLongpeng(Mike) QCryptoHmacDriver qcrypto_hmac_lib_driver = {
18314a5a2aeSLongpeng(Mike)     .hmac_bytesv = qcrypto_nettle_hmac_bytesv,
18414a5a2aeSLongpeng(Mike)     .hmac_free = qcrypto_nettle_hmac_ctx_free,
18514a5a2aeSLongpeng(Mike) };
186