xref: /qemu/crypto/hmac-nettle.c (revision 115e4b70)
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,
22*115e4b70SDaniel 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,
26*115e4b70SDaniel P. Berrangé                                            size_t length,
27f8878490SDaniel P. Berrangé                                            const uint8_t *data);
28f4d76747SLongpeng(Mike) 
29f4d76747SLongpeng(Mike) typedef void (*qcrypto_nettle_hmac_digest)(void *ctx,
30*115e4b70SDaniel 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;
41f4d76747SLongpeng(Mike)     } u;
42f4d76747SLongpeng(Mike) };
43f4d76747SLongpeng(Mike) 
44f4d76747SLongpeng(Mike) struct qcrypto_nettle_hmac_alg {
45f4d76747SLongpeng(Mike)     qcrypto_nettle_hmac_setkey setkey;
46f4d76747SLongpeng(Mike)     qcrypto_nettle_hmac_update update;
47f4d76747SLongpeng(Mike)     qcrypto_nettle_hmac_digest digest;
48f4d76747SLongpeng(Mike)     size_t len;
49f4d76747SLongpeng(Mike) } qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX] = {
50f4d76747SLongpeng(Mike)     [QCRYPTO_HASH_ALG_MD5] = {
51f4d76747SLongpeng(Mike)         .setkey = (qcrypto_nettle_hmac_setkey)hmac_md5_set_key,
52f4d76747SLongpeng(Mike)         .update = (qcrypto_nettle_hmac_update)hmac_md5_update,
53f4d76747SLongpeng(Mike)         .digest = (qcrypto_nettle_hmac_digest)hmac_md5_digest,
54f4d76747SLongpeng(Mike)         .len = MD5_DIGEST_SIZE,
55f4d76747SLongpeng(Mike)     },
56f4d76747SLongpeng(Mike)     [QCRYPTO_HASH_ALG_SHA1] = {
57f4d76747SLongpeng(Mike)         .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha1_set_key,
58f4d76747SLongpeng(Mike)         .update = (qcrypto_nettle_hmac_update)hmac_sha1_update,
59f4d76747SLongpeng(Mike)         .digest = (qcrypto_nettle_hmac_digest)hmac_sha1_digest,
60f4d76747SLongpeng(Mike)         .len = SHA1_DIGEST_SIZE,
61f4d76747SLongpeng(Mike)     },
62f4d76747SLongpeng(Mike)     [QCRYPTO_HASH_ALG_SHA224] = {
63f4d76747SLongpeng(Mike)         .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha224_set_key,
64f4d76747SLongpeng(Mike)         .update = (qcrypto_nettle_hmac_update)hmac_sha224_update,
65f4d76747SLongpeng(Mike)         .digest = (qcrypto_nettle_hmac_digest)hmac_sha224_digest,
66f4d76747SLongpeng(Mike)         .len = SHA224_DIGEST_SIZE,
67f4d76747SLongpeng(Mike)     },
68f4d76747SLongpeng(Mike)     [QCRYPTO_HASH_ALG_SHA256] = {
69f4d76747SLongpeng(Mike)         .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha256_set_key,
70f4d76747SLongpeng(Mike)         .update = (qcrypto_nettle_hmac_update)hmac_sha256_update,
71f4d76747SLongpeng(Mike)         .digest = (qcrypto_nettle_hmac_digest)hmac_sha256_digest,
72f4d76747SLongpeng(Mike)         .len = SHA256_DIGEST_SIZE,
73f4d76747SLongpeng(Mike)     },
74f4d76747SLongpeng(Mike)     [QCRYPTO_HASH_ALG_SHA384] = {
75f4d76747SLongpeng(Mike)         .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha384_set_key,
76f4d76747SLongpeng(Mike)         .update = (qcrypto_nettle_hmac_update)hmac_sha384_update,
77f4d76747SLongpeng(Mike)         .digest = (qcrypto_nettle_hmac_digest)hmac_sha384_digest,
78f4d76747SLongpeng(Mike)         .len = SHA384_DIGEST_SIZE,
79f4d76747SLongpeng(Mike)     },
80f4d76747SLongpeng(Mike)     [QCRYPTO_HASH_ALG_SHA512] = {
81f4d76747SLongpeng(Mike)         .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha512_set_key,
82f4d76747SLongpeng(Mike)         .update = (qcrypto_nettle_hmac_update)hmac_sha512_update,
83f4d76747SLongpeng(Mike)         .digest = (qcrypto_nettle_hmac_digest)hmac_sha512_digest,
84f4d76747SLongpeng(Mike)         .len = SHA512_DIGEST_SIZE,
85f4d76747SLongpeng(Mike)     },
86f4d76747SLongpeng(Mike)     [QCRYPTO_HASH_ALG_RIPEMD160] = {
87f4d76747SLongpeng(Mike)         .setkey = (qcrypto_nettle_hmac_setkey)hmac_ripemd160_set_key,
88f4d76747SLongpeng(Mike)         .update = (qcrypto_nettle_hmac_update)hmac_ripemd160_update,
89f4d76747SLongpeng(Mike)         .digest = (qcrypto_nettle_hmac_digest)hmac_ripemd160_digest,
90f4d76747SLongpeng(Mike)         .len = RIPEMD160_DIGEST_SIZE,
91f4d76747SLongpeng(Mike)     },
92f4d76747SLongpeng(Mike) };
93f4d76747SLongpeng(Mike) 
qcrypto_hmac_supports(QCryptoHashAlgorithm alg)9412a4f216SLongpeng(Mike) bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg)
9512a4f216SLongpeng(Mike) {
96f4d76747SLongpeng(Mike)     if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) &&
97f4d76747SLongpeng(Mike)         qcrypto_hmac_alg_map[alg].setkey != NULL) {
98f4d76747SLongpeng(Mike)         return true;
99f4d76747SLongpeng(Mike)     }
100f4d76747SLongpeng(Mike) 
10112a4f216SLongpeng(Mike)     return false;
10212a4f216SLongpeng(Mike) }
10312a4f216SLongpeng(Mike) 
qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,const uint8_t * key,size_t nkey,Error ** errp)10414a5a2aeSLongpeng(Mike) void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
10512a4f216SLongpeng(Mike)                            const uint8_t *key, size_t nkey,
10612a4f216SLongpeng(Mike)                            Error **errp)
10712a4f216SLongpeng(Mike) {
108f4d76747SLongpeng(Mike)     QCryptoHmacNettle *ctx;
109f4d76747SLongpeng(Mike) 
110f4d76747SLongpeng(Mike)     if (!qcrypto_hmac_supports(alg)) {
111f4d76747SLongpeng(Mike)         error_setg(errp, "Unsupported hmac algorithm %s",
112977c736fSMarkus Armbruster                    QCryptoHashAlgorithm_str(alg));
11312a4f216SLongpeng(Mike)         return NULL;
11412a4f216SLongpeng(Mike)     }
11512a4f216SLongpeng(Mike) 
116f4d76747SLongpeng(Mike)     ctx = g_new0(QCryptoHmacNettle, 1);
117f4d76747SLongpeng(Mike) 
118f4d76747SLongpeng(Mike)     qcrypto_hmac_alg_map[alg].setkey(&ctx->u, nkey, key);
119f4d76747SLongpeng(Mike) 
1208c2776d8SLongpeng(Mike)     return ctx;
121f4d76747SLongpeng(Mike) }
122f4d76747SLongpeng(Mike) 
12314a5a2aeSLongpeng(Mike) static void
qcrypto_nettle_hmac_ctx_free(QCryptoHmac * hmac)12414a5a2aeSLongpeng(Mike) qcrypto_nettle_hmac_ctx_free(QCryptoHmac *hmac)
12512a4f216SLongpeng(Mike) {
126f4d76747SLongpeng(Mike)     QCryptoHmacNettle *ctx;
127f4d76747SLongpeng(Mike) 
128f4d76747SLongpeng(Mike)     ctx = hmac->opaque;
129f4d76747SLongpeng(Mike)     g_free(ctx);
130f4d76747SLongpeng(Mike) }
131f4d76747SLongpeng(Mike) 
13214a5a2aeSLongpeng(Mike) static int
qcrypto_nettle_hmac_bytesv(QCryptoHmac * hmac,const struct iovec * iov,size_t niov,uint8_t ** result,size_t * resultlen,Error ** errp)13314a5a2aeSLongpeng(Mike) qcrypto_nettle_hmac_bytesv(QCryptoHmac *hmac,
13412a4f216SLongpeng(Mike)                            const struct iovec *iov,
13512a4f216SLongpeng(Mike)                            size_t niov,
13612a4f216SLongpeng(Mike)                            uint8_t **result,
13712a4f216SLongpeng(Mike)                            size_t *resultlen,
13812a4f216SLongpeng(Mike)                            Error **errp)
13912a4f216SLongpeng(Mike) {
140f4d76747SLongpeng(Mike)     QCryptoHmacNettle *ctx;
141f8878490SDaniel P. Berrangé     size_t i;
142f4d76747SLongpeng(Mike) 
143f4d76747SLongpeng(Mike)     ctx = (QCryptoHmacNettle *)hmac->opaque;
144f4d76747SLongpeng(Mike) 
145f4d76747SLongpeng(Mike)     for (i = 0; i < niov; ++i) {
146f4d76747SLongpeng(Mike)         size_t len = iov[i].iov_len;
147f4d76747SLongpeng(Mike)         uint8_t *base = iov[i].iov_base;
148f4d76747SLongpeng(Mike)         while (len) {
149f4d76747SLongpeng(Mike)             size_t shortlen = MIN(len, UINT_MAX);
150f4d76747SLongpeng(Mike)             qcrypto_hmac_alg_map[hmac->alg].update(&ctx->u, len, base);
151f4d76747SLongpeng(Mike)             len -= shortlen;
152f4d76747SLongpeng(Mike)             base += len;
153f4d76747SLongpeng(Mike)         }
154f4d76747SLongpeng(Mike)     }
155f4d76747SLongpeng(Mike) 
156f4d76747SLongpeng(Mike)     if (*resultlen == 0) {
157f4d76747SLongpeng(Mike)         *resultlen = qcrypto_hmac_alg_map[hmac->alg].len;
158f4d76747SLongpeng(Mike)         *result = g_new0(uint8_t, *resultlen);
159f4d76747SLongpeng(Mike)     } else if (*resultlen != qcrypto_hmac_alg_map[hmac->alg].len) {
160f4d76747SLongpeng(Mike)         error_setg(errp,
161f4d76747SLongpeng(Mike)                    "Result buffer size %zu is smaller than hash %zu",
162f4d76747SLongpeng(Mike)                    *resultlen, qcrypto_hmac_alg_map[hmac->alg].len);
16312a4f216SLongpeng(Mike)         return -1;
16412a4f216SLongpeng(Mike)     }
165f4d76747SLongpeng(Mike) 
166f4d76747SLongpeng(Mike)     qcrypto_hmac_alg_map[hmac->alg].digest(&ctx->u, *resultlen, *result);
167f4d76747SLongpeng(Mike) 
168f4d76747SLongpeng(Mike)     return 0;
169f4d76747SLongpeng(Mike) }
1708c2776d8SLongpeng(Mike) 
17114a5a2aeSLongpeng(Mike) QCryptoHmacDriver qcrypto_hmac_lib_driver = {
17214a5a2aeSLongpeng(Mike)     .hmac_bytesv = qcrypto_nettle_hmac_bytesv,
17314a5a2aeSLongpeng(Mike)     .hmac_free = qcrypto_nettle_hmac_ctx_free,
17414a5a2aeSLongpeng(Mike) };
175