hmac-nettle.c (8c2776d86ca5a9f34bb393b35ec7bd9faef3ff31) hmac-nettle.c (14a5a2aef47e27b6afd1f8855eb41c75b69440dc)
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"
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"
18#include <nettle/hmac.h>
19
20typedef void (*qcrypto_nettle_hmac_setkey)(void *ctx,
21 size_t key_length, const uint8_t *key);
22
23typedef void (*qcrypto_nettle_hmac_update)(void *ctx,
24 size_t length, const uint8_t *data);
25

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

92 if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) &&
93 qcrypto_hmac_alg_map[alg].setkey != NULL) {
94 return true;
95 }
96
97 return false;
98}
99
19#include <nettle/hmac.h>
20
21typedef void (*qcrypto_nettle_hmac_setkey)(void *ctx,
22 size_t key_length, const uint8_t *key);
23
24typedef void (*qcrypto_nettle_hmac_update)(void *ctx,
25 size_t length, const uint8_t *data);
26

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

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
100static QCryptoHmacNettle *
101qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
102 const uint8_t *key, size_t nkey,
103 Error **errp)
101void *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_lookup[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
104{
105 QCryptoHmacNettle *ctx;
106
107 if (!qcrypto_hmac_supports(alg)) {
108 error_setg(errp, "Unsupported hmac algorithm %s",
109 QCryptoHashAlgorithm_lookup[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
120void qcrypto_hmac_free(QCryptoHmac *hmac)
120static void
121qcrypto_nettle_hmac_ctx_free(QCryptoHmac *hmac)
121{
122 QCryptoHmacNettle *ctx;
123
122{
123 QCryptoHmacNettle *ctx;
124
124 if (!hmac) {
125 return;
126 }
127
128 ctx = hmac->opaque;
125 ctx = hmac->opaque;
129
130 g_free(ctx);
126 g_free(ctx);
131 g_free(hmac);
132}
133
127}
128
134int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
135 const struct iovec *iov,
136 size_t niov,
137 uint8_t **result,
138 size_t *resultlen,
139 Error **errp)
129static int
130qcrypto_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)
140{
141 QCryptoHmacNettle *ctx;
142 int i;
143
144 ctx = (QCryptoHmacNettle *)hmac->opaque;
145
146 for (i = 0; i < niov; ++i) {
147 size_t len = iov[i].iov_len;

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

164 return -1;
165 }
166
167 qcrypto_hmac_alg_map[hmac->alg].digest(&ctx->u, *resultlen, *result);
168
169 return 0;
170}
171
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;

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

160 return -1;
161 }
162
163 qcrypto_hmac_alg_map[hmac->alg].digest(&ctx->u, *resultlen, *result);
164
165 return 0;
166}
167
172QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg,
173 const uint8_t *key, size_t nkey,
174 Error **errp)
175{
176 QCryptoHmac *hmac;
177 QCryptoHmacNettle *ctx;
178
179 ctx = qcrypto_hmac_ctx_new(alg, key, nkey, errp);
180 if (!ctx) {
181 return NULL;
182 }
183
184 hmac = g_new0(QCryptoHmac, 1);
185 hmac->alg = alg;
186 hmac->opaque = ctx;
187
188 return hmac;
189}
168QCryptoHmacDriver qcrypto_hmac_lib_driver = {
169 .hmac_bytesv = qcrypto_nettle_hmac_bytesv,
170 .hmac_free = qcrypto_nettle_hmac_ctx_free,
171};