blob: 19990cb6ede9848e1884d3bde239913e6bf32234 [file] [log] [blame]
Longpeng(Mike)12a4f212016-12-13 18:42:56 +08001/*
2 * QEMU Crypto hmac algorithms (based on libgcrypt)
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"
Longpeng(Mike)14a5a2a2017-07-14 14:04:04 -040018#include "hmacpriv.h"
Longpeng(Mike)12a4f212016-12-13 18:42:56 +080019#include <gcrypt.h>
20
Markus Armbrusteref834aa2024-09-04 13:18:28 +020021static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALGO__MAX] = {
22 [QCRYPTO_HASH_ALGO_MD5] = GCRY_MAC_HMAC_MD5,
23 [QCRYPTO_HASH_ALGO_SHA1] = GCRY_MAC_HMAC_SHA1,
24 [QCRYPTO_HASH_ALGO_SHA224] = GCRY_MAC_HMAC_SHA224,
25 [QCRYPTO_HASH_ALGO_SHA256] = GCRY_MAC_HMAC_SHA256,
26 [QCRYPTO_HASH_ALGO_SHA384] = GCRY_MAC_HMAC_SHA384,
27 [QCRYPTO_HASH_ALGO_SHA512] = GCRY_MAC_HMAC_SHA512,
28 [QCRYPTO_HASH_ALGO_RIPEMD160] = GCRY_MAC_HMAC_RMD160,
Longpeng(Mike)5ce9cfe2016-12-13 18:42:57 +080029};
30
31typedef struct QCryptoHmacGcrypt QCryptoHmacGcrypt;
32struct QCryptoHmacGcrypt {
33 gcry_mac_hd_t handle;
34};
35
Markus Armbrusteref834aa2024-09-04 13:18:28 +020036bool qcrypto_hmac_supports(QCryptoHashAlgo alg)
Longpeng(Mike)12a4f212016-12-13 18:42:56 +080037{
Longpeng(Mike)5ce9cfe2016-12-13 18:42:57 +080038 if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) &&
39 qcrypto_hmac_alg_map[alg] != GCRY_MAC_NONE) {
40 return true;
41 }
42
Longpeng(Mike)12a4f212016-12-13 18:42:56 +080043 return false;
44}
45
Markus Armbrusteref834aa2024-09-04 13:18:28 +020046void *qcrypto_hmac_ctx_new(QCryptoHashAlgo alg,
Longpeng(Mike)14a5a2a2017-07-14 14:04:04 -040047 const uint8_t *key, size_t nkey,
48 Error **errp)
Longpeng(Mike)12a4f212016-12-13 18:42:56 +080049{
Longpeng(Mike)5ce9cfe2016-12-13 18:42:57 +080050 QCryptoHmacGcrypt *ctx;
51 gcry_error_t err;
52
53 if (!qcrypto_hmac_supports(alg)) {
54 error_setg(errp, "Unsupported hmac algorithm %s",
Markus Armbrusteref834aa2024-09-04 13:18:28 +020055 QCryptoHashAlgo_str(alg));
Longpeng(Mike)5ce9cfe2016-12-13 18:42:57 +080056 return NULL;
57 }
58
Longpeng(Mike)5ce9cfe2016-12-13 18:42:57 +080059 ctx = g_new0(QCryptoHmacGcrypt, 1);
60
61 err = gcry_mac_open(&ctx->handle, qcrypto_hmac_alg_map[alg],
62 GCRY_MAC_FLAG_SECURE, NULL);
63 if (err != 0) {
64 error_setg(errp, "Cannot initialize hmac: %s",
65 gcry_strerror(err));
66 goto error;
67 }
68
69 err = gcry_mac_setkey(ctx->handle, (const void *)key, nkey);
70 if (err != 0) {
71 error_setg(errp, "Cannot set key: %s",
72 gcry_strerror(err));
Longpeng(Mike)822d15d2017-07-14 14:04:01 -040073 gcry_mac_close(ctx->handle);
Longpeng(Mike)5ce9cfe2016-12-13 18:42:57 +080074 goto error;
75 }
76
Longpeng(Mike)822d15d2017-07-14 14:04:01 -040077 return ctx;
Longpeng(Mike)5ce9cfe2016-12-13 18:42:57 +080078
79error:
80 g_free(ctx);
Longpeng(Mike)12a4f212016-12-13 18:42:56 +080081 return NULL;
82}
83
Longpeng(Mike)14a5a2a2017-07-14 14:04:04 -040084static void
85qcrypto_gcrypt_hmac_ctx_free(QCryptoHmac *hmac)
Longpeng(Mike)12a4f212016-12-13 18:42:56 +080086{
Longpeng(Mike)5ce9cfe2016-12-13 18:42:57 +080087 QCryptoHmacGcrypt *ctx;
88
Longpeng(Mike)5ce9cfe2016-12-13 18:42:57 +080089 ctx = hmac->opaque;
90 gcry_mac_close(ctx->handle);
91
92 g_free(ctx);
Longpeng(Mike)12a4f212016-12-13 18:42:56 +080093}
94
Longpeng(Mike)14a5a2a2017-07-14 14:04:04 -040095static int
96qcrypto_gcrypt_hmac_bytesv(QCryptoHmac *hmac,
97 const struct iovec *iov,
98 size_t niov,
99 uint8_t **result,
100 size_t *resultlen,
101 Error **errp)
Longpeng(Mike)12a4f212016-12-13 18:42:56 +0800102{
Longpeng(Mike)5ce9cfe2016-12-13 18:42:57 +0800103 QCryptoHmacGcrypt *ctx;
104 gcry_error_t err;
105 uint32_t ret;
106 int i;
107
108 ctx = hmac->opaque;
109
110 for (i = 0; i < niov; i++) {
111 gcry_mac_write(ctx->handle, iov[i].iov_base, iov[i].iov_len);
112 }
113
114 ret = gcry_mac_get_algo_maclen(qcrypto_hmac_alg_map[hmac->alg]);
115 if (ret <= 0) {
116 error_setg(errp, "Unable to get hmac length: %s",
117 gcry_strerror(ret));
118 return -1;
119 }
120
121 if (*resultlen == 0) {
122 *resultlen = ret;
123 *result = g_new0(uint8_t, *resultlen);
124 } else if (*resultlen != ret) {
125 error_setg(errp, "Result buffer size %zu is smaller than hmac %d",
126 *resultlen, ret);
127 return -1;
128 }
129
130 err = gcry_mac_read(ctx->handle, *result, resultlen);
131 if (err != 0) {
132 error_setg(errp, "Cannot get result: %s",
133 gcry_strerror(err));
134 return -1;
135 }
136
137 err = gcry_mac_reset(ctx->handle);
138 if (err != 0) {
139 error_setg(errp, "Cannot reset hmac context: %s",
140 gcry_strerror(err));
141 return -1;
142 }
143
144 return 0;
Longpeng(Mike)12a4f212016-12-13 18:42:56 +0800145}
Longpeng(Mike)822d15d2017-07-14 14:04:01 -0400146
Longpeng(Mike)14a5a2a2017-07-14 14:04:04 -0400147QCryptoHmacDriver qcrypto_hmac_lib_driver = {
148 .hmac_bytesv = qcrypto_gcrypt_hmac_bytesv,
149 .hmac_free = qcrypto_gcrypt_hmac_ctx_free,
150};