2 * QEMU Crypto hmac algorithms
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
6 * This work is licensed under the terms of the GNU GPL, version 2 or
7 * (at your option) any later version. See the COPYING file in the
12 #include "qemu/osdep.h"
13 #include "qapi/error.h"
14 #include "crypto/hmac.h"
17 static const char hex[] = "0123456789abcdef";
19 int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
20 const struct iovec *iov,
26 QCryptoHmacDriver *drv = hmac->driver;
28 return drv->hmac_bytesv(hmac, iov, niov, result, resultlen, errp);
31 int qcrypto_hmac_bytes(QCryptoHmac *hmac,
39 .iov_base = (char *)buf,
43 return qcrypto_hmac_bytesv(hmac, &iov, 1, result, resultlen, errp);
46 int qcrypto_hmac_digestv(QCryptoHmac *hmac,
47 const struct iovec *iov,
52 uint8_t *result = NULL;
56 if (qcrypto_hmac_bytesv(hmac, iov, niov, &result, &resultlen, errp) < 0) {
60 *digest = g_new0(char, (resultlen * 2) + 1);
62 for (i = 0 ; i < resultlen ; i++) {
63 (*digest)[(i * 2)] = hex[(result[i] >> 4) & 0xf];
64 (*digest)[(i * 2) + 1] = hex[result[i] & 0xf];
67 (*digest)[resultlen * 2] = '\0';
73 int qcrypto_hmac_digest(QCryptoHmac *hmac,
80 .iov_base = (char *)buf,
84 return qcrypto_hmac_digestv(hmac, &iov, 1, digest, errp);
87 QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg,
88 const uint8_t *key, size_t nkey,
93 QCryptoHmacDriver *drv = NULL;
96 ctx = qcrypto_afalg_hmac_ctx_new(alg, key, nkey, NULL);
98 drv = &qcrypto_hmac_afalg_driver;
103 ctx = qcrypto_hmac_ctx_new(alg, key, nkey, errp);
108 drv = &qcrypto_hmac_lib_driver;
111 hmac = g_new0(QCryptoHmac, 1);
114 hmac->driver = (void *)drv;
119 void qcrypto_hmac_free(QCryptoHmac *hmac)
121 QCryptoHmacDriver *drv;
125 drv->hmac_free(hmac);