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