xref: /openbmc/qemu/crypto/hmac-gnutls.c (revision b8eada54b2ad8a7d98d93d5ab4d3e888c5880097)
1678307b6SDaniel P. Berrangé /*
2678307b6SDaniel P. Berrangé  * QEMU Crypto hmac algorithms
3678307b6SDaniel P. Berrangé  *
4678307b6SDaniel P. Berrangé  * Copyright (c) 2021 Red Hat, Inc.
5678307b6SDaniel P. Berrangé  *
6678307b6SDaniel P. Berrangé  * Derived from hmac-gcrypt.c:
7678307b6SDaniel P. Berrangé  *
8678307b6SDaniel P. Berrangé  *   Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
9678307b6SDaniel P. Berrangé  *
10678307b6SDaniel P. Berrangé  * This work is licensed under the terms of the GNU GPL, version 2 or
11678307b6SDaniel P. Berrangé  * (at your option) any later version.  See the COPYING file in the
12678307b6SDaniel P. Berrangé  * top-level directory.
13678307b6SDaniel P. Berrangé  *
14678307b6SDaniel P. Berrangé  */
15678307b6SDaniel P. Berrangé 
16678307b6SDaniel P. Berrangé #include "qemu/osdep.h"
17678307b6SDaniel P. Berrangé #include <gnutls/crypto.h>
18678307b6SDaniel P. Berrangé 
19678307b6SDaniel P. Berrangé #include "qapi/error.h"
20678307b6SDaniel P. Berrangé #include "crypto/hmac.h"
21678307b6SDaniel P. Berrangé #include "hmacpriv.h"
22678307b6SDaniel P. Berrangé 
23*ef834aa2SMarkus Armbruster static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALGO__MAX] = {
24*ef834aa2SMarkus Armbruster     [QCRYPTO_HASH_ALGO_MD5] = GNUTLS_MAC_MD5,
25*ef834aa2SMarkus Armbruster     [QCRYPTO_HASH_ALGO_SHA1] = GNUTLS_MAC_SHA1,
26*ef834aa2SMarkus Armbruster     [QCRYPTO_HASH_ALGO_SHA224] = GNUTLS_MAC_SHA224,
27*ef834aa2SMarkus Armbruster     [QCRYPTO_HASH_ALGO_SHA256] = GNUTLS_MAC_SHA256,
28*ef834aa2SMarkus Armbruster     [QCRYPTO_HASH_ALGO_SHA384] = GNUTLS_MAC_SHA384,
29*ef834aa2SMarkus Armbruster     [QCRYPTO_HASH_ALGO_SHA512] = GNUTLS_MAC_SHA512,
30*ef834aa2SMarkus Armbruster     [QCRYPTO_HASH_ALGO_RIPEMD160] = GNUTLS_MAC_RMD160,
31678307b6SDaniel P. Berrangé };
32678307b6SDaniel P. Berrangé 
33678307b6SDaniel P. Berrangé typedef struct QCryptoHmacGnutls QCryptoHmacGnutls;
34678307b6SDaniel P. Berrangé struct QCryptoHmacGnutls {
35678307b6SDaniel P. Berrangé     gnutls_hmac_hd_t handle;
36678307b6SDaniel P. Berrangé };
37678307b6SDaniel P. Berrangé 
qcrypto_hmac_supports(QCryptoHashAlgo alg)38*ef834aa2SMarkus Armbruster bool qcrypto_hmac_supports(QCryptoHashAlgo alg)
39678307b6SDaniel P. Berrangé {
40678307b6SDaniel P. Berrangé     size_t i;
41678307b6SDaniel P. Berrangé     const gnutls_digest_algorithm_t *algs;
42678307b6SDaniel P. Berrangé     if (alg >= G_N_ELEMENTS(qcrypto_hmac_alg_map) ||
43678307b6SDaniel P. Berrangé         qcrypto_hmac_alg_map[alg] == GNUTLS_DIG_UNKNOWN) {
44678307b6SDaniel P. Berrangé         return false;
45678307b6SDaniel P. Berrangé     }
46678307b6SDaniel P. Berrangé     algs = gnutls_digest_list();
47678307b6SDaniel P. Berrangé     for (i = 0; algs[i] != GNUTLS_DIG_UNKNOWN; i++) {
48678307b6SDaniel P. Berrangé         if (algs[i] == qcrypto_hmac_alg_map[alg]) {
49678307b6SDaniel P. Berrangé             return true;
50678307b6SDaniel P. Berrangé         }
51678307b6SDaniel P. Berrangé     }
52678307b6SDaniel P. Berrangé     return false;
53678307b6SDaniel P. Berrangé }
54678307b6SDaniel P. Berrangé 
qcrypto_hmac_ctx_new(QCryptoHashAlgo alg,const uint8_t * key,size_t nkey,Error ** errp)55*ef834aa2SMarkus Armbruster void *qcrypto_hmac_ctx_new(QCryptoHashAlgo alg,
56678307b6SDaniel P. Berrangé                            const uint8_t *key, size_t nkey,
57678307b6SDaniel P. Berrangé                            Error **errp)
58678307b6SDaniel P. Berrangé {
59678307b6SDaniel P. Berrangé     QCryptoHmacGnutls *ctx;
60678307b6SDaniel P. Berrangé     int err;
61678307b6SDaniel P. Berrangé 
62678307b6SDaniel P. Berrangé     if (!qcrypto_hmac_supports(alg)) {
63678307b6SDaniel P. Berrangé         error_setg(errp, "Unsupported hmac algorithm %s",
64*ef834aa2SMarkus Armbruster                    QCryptoHashAlgo_str(alg));
65678307b6SDaniel P. Berrangé         return NULL;
66678307b6SDaniel P. Berrangé     }
67678307b6SDaniel P. Berrangé 
68678307b6SDaniel P. Berrangé     ctx = g_new0(QCryptoHmacGnutls, 1);
69678307b6SDaniel P. Berrangé 
70678307b6SDaniel P. Berrangé     err = gnutls_hmac_init(&ctx->handle,
71678307b6SDaniel P. Berrangé                            qcrypto_hmac_alg_map[alg],
72678307b6SDaniel P. Berrangé                            (const void *)key, nkey);
73678307b6SDaniel P. Berrangé     if (err != 0) {
74678307b6SDaniel P. Berrangé         error_setg(errp, "Cannot initialize hmac: %s",
75678307b6SDaniel P. Berrangé                    gnutls_strerror(err));
76678307b6SDaniel P. Berrangé         goto error;
77678307b6SDaniel P. Berrangé     }
78678307b6SDaniel P. Berrangé 
79678307b6SDaniel P. Berrangé     return ctx;
80678307b6SDaniel P. Berrangé 
81678307b6SDaniel P. Berrangé error:
82678307b6SDaniel P. Berrangé     g_free(ctx);
83678307b6SDaniel P. Berrangé     return NULL;
84678307b6SDaniel P. Berrangé }
85678307b6SDaniel P. Berrangé 
86678307b6SDaniel P. Berrangé static void
qcrypto_gnutls_hmac_ctx_free(QCryptoHmac * hmac)87678307b6SDaniel P. Berrangé qcrypto_gnutls_hmac_ctx_free(QCryptoHmac *hmac)
88678307b6SDaniel P. Berrangé {
89678307b6SDaniel P. Berrangé     QCryptoHmacGnutls *ctx;
90678307b6SDaniel P. Berrangé 
91678307b6SDaniel P. Berrangé     ctx = hmac->opaque;
92678307b6SDaniel P. Berrangé     gnutls_hmac_deinit(ctx->handle, NULL);
93678307b6SDaniel P. Berrangé 
94678307b6SDaniel P. Berrangé     g_free(ctx);
95678307b6SDaniel P. Berrangé }
96678307b6SDaniel P. Berrangé 
97678307b6SDaniel P. Berrangé static int
qcrypto_gnutls_hmac_bytesv(QCryptoHmac * hmac,const struct iovec * iov,size_t niov,uint8_t ** result,size_t * resultlen,Error ** errp)98678307b6SDaniel P. Berrangé qcrypto_gnutls_hmac_bytesv(QCryptoHmac *hmac,
99678307b6SDaniel P. Berrangé                            const struct iovec *iov,
100678307b6SDaniel P. Berrangé                            size_t niov,
101678307b6SDaniel P. Berrangé                            uint8_t **result,
102678307b6SDaniel P. Berrangé                            size_t *resultlen,
103678307b6SDaniel P. Berrangé                            Error **errp)
104678307b6SDaniel P. Berrangé {
105678307b6SDaniel P. Berrangé     QCryptoHmacGnutls *ctx;
106678307b6SDaniel P. Berrangé     uint32_t ret;
107678307b6SDaniel P. Berrangé     int i;
108678307b6SDaniel P. Berrangé 
109678307b6SDaniel P. Berrangé     ctx = hmac->opaque;
110678307b6SDaniel P. Berrangé 
111678307b6SDaniel P. Berrangé     for (i = 0; i < niov; i++) {
112678307b6SDaniel P. Berrangé         gnutls_hmac(ctx->handle, iov[i].iov_base, iov[i].iov_len);
113678307b6SDaniel P. Berrangé     }
114678307b6SDaniel P. Berrangé 
115678307b6SDaniel P. Berrangé     ret = gnutls_hmac_get_len(qcrypto_hmac_alg_map[hmac->alg]);
116678307b6SDaniel P. Berrangé     if (ret <= 0) {
117678307b6SDaniel P. Berrangé         error_setg(errp, "Unable to get hmac length: %s",
118678307b6SDaniel P. Berrangé                    gnutls_strerror(ret));
119678307b6SDaniel P. Berrangé         return -1;
120678307b6SDaniel P. Berrangé     }
121678307b6SDaniel P. Berrangé 
122678307b6SDaniel P. Berrangé     if (*resultlen == 0) {
123678307b6SDaniel P. Berrangé         *resultlen = ret;
124678307b6SDaniel P. Berrangé         *result = g_new0(uint8_t, *resultlen);
125678307b6SDaniel P. Berrangé     } else if (*resultlen != ret) {
126678307b6SDaniel P. Berrangé         error_setg(errp, "Result buffer size %zu is smaller than hmac %d",
127678307b6SDaniel P. Berrangé                    *resultlen, ret);
128678307b6SDaniel P. Berrangé         return -1;
129678307b6SDaniel P. Berrangé     }
130678307b6SDaniel P. Berrangé 
131678307b6SDaniel P. Berrangé     gnutls_hmac_output(ctx->handle, *result);
132678307b6SDaniel P. Berrangé 
133678307b6SDaniel P. Berrangé     return 0;
134678307b6SDaniel P. Berrangé }
135678307b6SDaniel P. Berrangé 
136678307b6SDaniel P. Berrangé QCryptoHmacDriver qcrypto_hmac_lib_driver = {
137678307b6SDaniel P. Berrangé     .hmac_bytesv = qcrypto_gnutls_hmac_bytesv,
138678307b6SDaniel P. Berrangé     .hmac_free = qcrypto_gnutls_hmac_ctx_free,
139678307b6SDaniel P. Berrangé };
140