hmac-gcrypt.c (822d15ded8887742ea7ea4ddbfcfebb443813dd3) hmac-gcrypt.c (14a5a2aef47e27b6afd1f8855eb41c75b69440dc)
1/*
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"
1/*
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"
18#include "hmacpriv.h"
18#include <gcrypt.h>
19
20static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX] = {
21 [QCRYPTO_HASH_ALG_MD5] = GCRY_MAC_HMAC_MD5,
22 [QCRYPTO_HASH_ALG_SHA1] = GCRY_MAC_HMAC_SHA1,
23 [QCRYPTO_HASH_ALG_SHA224] = GCRY_MAC_HMAC_SHA224,
24 [QCRYPTO_HASH_ALG_SHA256] = GCRY_MAC_HMAC_SHA256,
25 [QCRYPTO_HASH_ALG_SHA384] = GCRY_MAC_HMAC_SHA384,

--- 11 unchanged lines hidden (view full) ---

37 if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) &&
38 qcrypto_hmac_alg_map[alg] != GCRY_MAC_NONE) {
39 return true;
40 }
41
42 return false;
43}
44
19#include <gcrypt.h>
20
21static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX] = {
22 [QCRYPTO_HASH_ALG_MD5] = GCRY_MAC_HMAC_MD5,
23 [QCRYPTO_HASH_ALG_SHA1] = GCRY_MAC_HMAC_SHA1,
24 [QCRYPTO_HASH_ALG_SHA224] = GCRY_MAC_HMAC_SHA224,
25 [QCRYPTO_HASH_ALG_SHA256] = GCRY_MAC_HMAC_SHA256,
26 [QCRYPTO_HASH_ALG_SHA384] = GCRY_MAC_HMAC_SHA384,

--- 11 unchanged lines hidden (view full) ---

38 if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) &&
39 qcrypto_hmac_alg_map[alg] != GCRY_MAC_NONE) {
40 return true;
41 }
42
43 return false;
44}
45
45static QCryptoHmacGcrypt *
46qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
47 const uint8_t *key, size_t nkey,
48 Error **errp)
46void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
47 const uint8_t *key, size_t nkey,
48 Error **errp)
49{
50 QCryptoHmacGcrypt *ctx;
51 gcry_error_t err;
52
53 if (!qcrypto_hmac_supports(alg)) {
54 error_setg(errp, "Unsupported hmac algorithm %s",
55 QCryptoHashAlgorithm_lookup[alg]);
56 return NULL;

--- 19 unchanged lines hidden (view full) ---

76
77 return ctx;
78
79error:
80 g_free(ctx);
81 return NULL;
82}
83
49{
50 QCryptoHmacGcrypt *ctx;
51 gcry_error_t err;
52
53 if (!qcrypto_hmac_supports(alg)) {
54 error_setg(errp, "Unsupported hmac algorithm %s",
55 QCryptoHashAlgorithm_lookup[alg]);
56 return NULL;

--- 19 unchanged lines hidden (view full) ---

76
77 return ctx;
78
79error:
80 g_free(ctx);
81 return NULL;
82}
83
84void qcrypto_hmac_free(QCryptoHmac *hmac)
84static void
85qcrypto_gcrypt_hmac_ctx_free(QCryptoHmac *hmac)
85{
86 QCryptoHmacGcrypt *ctx;
87
86{
87 QCryptoHmacGcrypt *ctx;
88
88 if (!hmac) {
89 return;
90 }
91
92 ctx = hmac->opaque;
93 gcry_mac_close(ctx->handle);
94
95 g_free(ctx);
89 ctx = hmac->opaque;
90 gcry_mac_close(ctx->handle);
91
92 g_free(ctx);
96 g_free(hmac);
97}
98
93}
94
99int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
100 const struct iovec *iov,
101 size_t niov,
102 uint8_t **result,
103 size_t *resultlen,
104 Error **errp)
95static 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)
105{
106 QCryptoHmacGcrypt *ctx;
107 gcry_error_t err;
108 uint32_t ret;
109 int i;
110
111 ctx = hmac->opaque;
112

--- 29 unchanged lines hidden (view full) ---

142 error_setg(errp, "Cannot reset hmac context: %s",
143 gcry_strerror(err));
144 return -1;
145 }
146
147 return 0;
148}
149
102{
103 QCryptoHmacGcrypt *ctx;
104 gcry_error_t err;
105 uint32_t ret;
106 int i;
107
108 ctx = hmac->opaque;
109

--- 29 unchanged lines hidden (view full) ---

139 error_setg(errp, "Cannot reset hmac context: %s",
140 gcry_strerror(err));
141 return -1;
142 }
143
144 return 0;
145}
146
150QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg,
151 const uint8_t *key, size_t nkey,
152 Error **errp)
153{
154 QCryptoHmac *hmac;
155 QCryptoHmacGcrypt *ctx;
156
157 ctx = qcrypto_hmac_ctx_new(alg, key, nkey, errp);
158 if (!ctx) {
159 return NULL;
160 }
161
162 hmac = g_new0(QCryptoHmac, 1);
163 hmac->alg = alg;
164 hmac->opaque = ctx;
165
166 return hmac;
167}
147QCryptoHmacDriver qcrypto_hmac_lib_driver = {
148 .hmac_bytesv = qcrypto_gcrypt_hmac_bytesv,
149 .hmac_free = qcrypto_gcrypt_hmac_ctx_free,
150};