]> git.proxmox.com Git - mirror_qemu.git/blame - crypto/hmac.c
s390x/pci: let pci devices start in configured mode
[mirror_qemu.git] / crypto / hmac.c
CommitLineData
12a4f216
LM
1/*
2 * QEMU Crypto hmac algorithms
3 *
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
5 *
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
8 * top-level directory.
9 *
10 */
11
12#include "qemu/osdep.h"
13#include "qapi/error.h"
14#include "crypto/hmac.h"
14a5a2ae 15#include "hmacpriv.h"
12a4f216
LM
16
17static const char hex[] = "0123456789abcdef";
18
14a5a2ae
LM
19int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
20 const struct iovec *iov,
21 size_t niov,
22 uint8_t **result,
23 size_t *resultlen,
24 Error **errp)
25{
26 QCryptoHmacDriver *drv = hmac->driver;
27
28 return drv->hmac_bytesv(hmac, iov, niov, result, resultlen, errp);
29}
30
12a4f216
LM
31int qcrypto_hmac_bytes(QCryptoHmac *hmac,
32 const char *buf,
33 size_t len,
34 uint8_t **result,
35 size_t *resultlen,
36 Error **errp)
37{
38 struct iovec iov = {
39 .iov_base = (char *)buf,
40 .iov_len = len
41 };
42
43 return qcrypto_hmac_bytesv(hmac, &iov, 1, result, resultlen, errp);
44}
45
46int qcrypto_hmac_digestv(QCryptoHmac *hmac,
47 const struct iovec *iov,
48 size_t niov,
49 char **digest,
50 Error **errp)
51{
52 uint8_t *result = NULL;
53 size_t resultlen = 0;
54 size_t i;
55
56 if (qcrypto_hmac_bytesv(hmac, iov, niov, &result, &resultlen, errp) < 0) {
57 return -1;
58 }
59
60 *digest = g_new0(char, (resultlen * 2) + 1);
61
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];
65 }
66
67 (*digest)[resultlen * 2] = '\0';
68
69 g_free(result);
70 return 0;
71}
72
73int qcrypto_hmac_digest(QCryptoHmac *hmac,
74 const char *buf,
75 size_t len,
76 char **digest,
77 Error **errp)
78{
79 struct iovec iov = {
80 .iov_base = (char *)buf,
81 .iov_len = len
82 };
83
84 return qcrypto_hmac_digestv(hmac, &iov, 1, digest, errp);
85}
14a5a2ae
LM
86
87QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg,
88 const uint8_t *key, size_t nkey,
89 Error **errp)
90{
91 QCryptoHmac *hmac;
42e7e15f
LM
92 void *ctx = NULL;
93 Error *err2 = NULL;
94 QCryptoHmacDriver *drv = NULL;
95
96#ifdef CONFIG_AF_ALG
97 ctx = qcrypto_afalg_hmac_ctx_new(alg, key, nkey, &err2);
98 if (ctx) {
99 drv = &qcrypto_hmac_afalg_driver;
100 }
101#endif
14a5a2ae 102
14a5a2ae 103 if (!ctx) {
42e7e15f
LM
104 ctx = qcrypto_hmac_ctx_new(alg, key, nkey, errp);
105 if (!ctx) {
106 return NULL;
107 }
108
109 drv = &qcrypto_hmac_lib_driver;
110 error_free(err2);
14a5a2ae
LM
111 }
112
113 hmac = g_new0(QCryptoHmac, 1);
114 hmac->alg = alg;
115 hmac->opaque = ctx;
42e7e15f 116 hmac->driver = (void *)drv;
14a5a2ae
LM
117
118 return hmac;
119}
120
121void qcrypto_hmac_free(QCryptoHmac *hmac)
122{
123 QCryptoHmacDriver *drv;
124
125 if (hmac) {
126 drv = hmac->driver;
127 drv->hmac_free(hmac);
128 g_free(hmac);
129 }
130}