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 "crypto/hmac.h"
16 static const char hex
[] = "0123456789abcdef";
18 int qcrypto_hmac_bytesv(QCryptoHmac
*hmac
,
19 const struct iovec
*iov
,
25 QCryptoHmacDriver
*drv
= hmac
->driver
;
27 return drv
->hmac_bytesv(hmac
, iov
, niov
, result
, resultlen
, errp
);
30 int qcrypto_hmac_bytes(QCryptoHmac
*hmac
,
38 .iov_base
= (char *)buf
,
42 return qcrypto_hmac_bytesv(hmac
, &iov
, 1, result
, resultlen
, errp
);
45 int qcrypto_hmac_digestv(QCryptoHmac
*hmac
,
46 const struct iovec
*iov
,
51 uint8_t *result
= NULL
;
55 if (qcrypto_hmac_bytesv(hmac
, iov
, niov
, &result
, &resultlen
, errp
) < 0) {
59 *digest
= g_new0(char, (resultlen
* 2) + 1);
61 for (i
= 0 ; i
< resultlen
; i
++) {
62 (*digest
)[(i
* 2)] = hex
[(result
[i
] >> 4) & 0xf];
63 (*digest
)[(i
* 2) + 1] = hex
[result
[i
] & 0xf];
66 (*digest
)[resultlen
* 2] = '\0';
72 int qcrypto_hmac_digest(QCryptoHmac
*hmac
,
79 .iov_base
= (char *)buf
,
83 return qcrypto_hmac_digestv(hmac
, &iov
, 1, digest
, errp
);
86 QCryptoHmac
*qcrypto_hmac_new(QCryptoHashAlgorithm alg
,
87 const uint8_t *key
, size_t nkey
,
92 QCryptoHmacDriver
*drv
= NULL
;
95 ctx
= qcrypto_afalg_hmac_ctx_new(alg
, key
, nkey
, NULL
);
97 drv
= &qcrypto_hmac_afalg_driver
;
102 ctx
= qcrypto_hmac_ctx_new(alg
, key
, nkey
, errp
);
107 drv
= &qcrypto_hmac_lib_driver
;
110 hmac
= g_new0(QCryptoHmac
, 1);
113 hmac
->driver
= (void *)drv
;
118 void qcrypto_hmac_free(QCryptoHmac
*hmac
)
120 QCryptoHmacDriver
*drv
;
124 drv
->hmac_free(hmac
);