master
h 195 lines 6.1 KB
Raw
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 #ifndef QCRYPTO_HMAC_H
13 #define QCRYPTO_HMAC_H
14
15 #include "qapi/qapi-types-crypto.h"
16
17 typedef struct QCryptoHmac QCryptoHmac;
18 struct QCryptoHmac {
19 QCryptoHashAlgo alg;
20 void *opaque;
21 void *driver;
22 };
23
24 /**
25 * qcrypto_hmac_supports:
26 * @alg: the hmac algorithm
27 *
28 * Determine if @alg hmac algorithm is supported by
29 * the current configured build
30 *
31 * Returns:
32 * true if the algorithm is supported, false otherwise
33 */
34 bool qcrypto_hmac_supports(QCryptoHashAlgo alg);
35
36 /**
37 * qcrypto_hmac_new:
38 * @alg: the hmac algorithm
39 * @key: the key bytes
40 * @nkey: the length of @key
41 * @errp: pointer to a NULL-initialized error object
42 *
43 * Creates a new hmac object with the algorithm @alg
44 *
45 * The @key parameter provides the bytes representing
46 * the secret key to use. The @nkey parameter specifies
47 * the length of @key in bytes
48 *
49 * Note: must use qcrypto_hmac_free() to release the
50 * returned hmac object when no longer required
51 *
52 * Returns:
53 * a new hmac object, or NULL on error
54 */
55 QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgo alg,
56 const uint8_t *key, size_t nkey,
57 Error **errp);
58
59 /**
60 * qcrypto_hmac_free:
61 * @hmac: the hmac object
62 *
63 * Release the memory associated with @hmac that was
64 * previously allocated by qcrypto_hmac_new()
65 */
66 void qcrypto_hmac_free(QCryptoHmac *hmac);
67
68 G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoHmac, qcrypto_hmac_free)
69
70 /**
71 * qcrypto_hmac_bytesv:
72 * @hmac: the hmac object
73 * @iov: the array of memory regions to hmac
74 * @niov: the length of @iov
75 * @result: pointer to hold output hmac
76 * @resultlen: pointer to hold length of @result
77 * @errp: pointer to a NULL-initialized error object
78 *
79 * Computes the hmac across all the memory regions
80 * present in @iov.
81 *
82 * If @result_len is set to a non-zero value by the caller, then
83 * @result must hold a pointer that is @result_len in size, and
84 * @result_len match the size of the hash output. The digest will
85 * be written into @result.
86 *
87 * If @result_len is set to zero, then this function will allocate
88 * a buffer to hold the hash output digest, storing a pointer to
89 * the buffer in @result, and setting @result_len to its size.
90 * The memory referenced in @result must be released with a call
91 * to g_free() when no longer required by the caller.
92 *
93 * If @result_len is set to a NULL pointer, no result will be returned, and
94 * the hmac object can be used for further invocations of qcrypto_hmac_bytes()
95 * or qcrypto_hmac_bytesv() until a non-NULL pointer is provided. This allows
96 * to build the hmac across memory regions that are not available at the same
97 * time.
98 *
99 * Returns:
100 * 0 on success, -1 on error
101 */
102 int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
103 const struct iovec *iov,
104 size_t niov,
105 uint8_t **result,
106 size_t *resultlen,
107 Error **errp);
108
109 /**
110 * qcrypto_hmac_bytes:
111 * @hmac: the hmac object
112 * @buf: the memory region to hmac
113 * @len: the length of @buf
114 * @result: pointer to hold output hmac
115 * @resultlen: pointer to hold length of @result
116 * @errp: pointer to a NULL-initialized error object
117 *
118 * Computes the hmac across all the memory region
119 * @buf of length @len.
120 *
121 * If @result_len is set to a non-zero value by the caller, then
122 * @result must hold a pointer that is @result_len in size, and
123 * @result_len match the size of the hash output. The digest will
124 * be written into @result.
125 *
126 * If @result_len is set to zero, then this function will allocate
127 * a buffer to hold the hash output digest, storing a pointer to
128 * the buffer in @result, and setting @result_len to its size.
129 * The memory referenced in @result must be released with a call
130 * to g_free() when no longer required by the caller.
131 *
132 * If @result_len is set to a NULL pointer, no result will be returned, and
133 * the hmac object can be used for further invocations of qcrypto_hmac_bytes()
134 * or qcrypto_hmac_bytesv() until a non-NULL pointer is provided. This allows
135 * to build the hmac across memory regions that are not available at the same
136 * time.
137 *
138 * Returns:
139 * 0 on success, -1 on error
140 */
141 int qcrypto_hmac_bytes(QCryptoHmac *hmac,
142 const void *buf,
143 size_t len,
144 uint8_t **result,
145 size_t *resultlen,
146 Error **errp);
147
148 /**
149 * qcrypto_hmac_digestv:
150 * @hmac: the hmac object
151 * @iov: the array of memory regions to hmac
152 * @niov: the length of @iov
153 * @digest: pointer to hold output hmac
154 * @errp: pointer to a NULL-initialized error object
155 *
156 * Computes the hmac across all the memory regions
157 * present in @iov. The @digest pointer will be
158 * filled with the printable hex digest of the computed
159 * hmac, which will be terminated by '\0'. The
160 * memory pointer in @digest must be released
161 * with a call to g_free() when no longer required.
162 *
163 * Returns:
164 * 0 on success, -1 on error
165 */
166 int qcrypto_hmac_digestv(QCryptoHmac *hmac,
167 const struct iovec *iov,
168 size_t niov,
169 char **digest,
170 Error **errp);
171
172 /**
173 * qcrypto_hmac_digest:
174 * @hmac: the hmac object
175 * @buf: the memory region to hmac
176 * @len: the length of @buf
177 * @digest: pointer to hold output hmac
178 * @errp: pointer to a NULL-initialized error object
179 *
180 * Computes the hmac across all the memory region
181 * @buf of length @len. The @digest pointer will be
182 * filled with the printable hex digest of the computed
183 * hmac, which will be terminated by '\0'. The
184 * memory pointer in @digest must be released
185 * with a call to g_free() when no longer required.
186 *
187 * Returns: 0 on success, -1 on error
188 */
189 int qcrypto_hmac_digest(QCryptoHmac *hmac,
190 const void *buf,
191 size_t len,
192 char **digest,
193 Error **errp);
194
195 #endif