xref: /openbmc/qemu/include/crypto/hash.h (revision e53c136f)
1 /*
2  * QEMU Crypto hash algorithms
3  *
4  * Copyright (c) 2015 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef QCRYPTO_HASH_H
22 #define QCRYPTO_HASH_H
23 
24 #include "qapi/qapi-types-crypto.h"
25 
26 #define QCRYPTO_HASH_DIGEST_LEN_MD5       16
27 #define QCRYPTO_HASH_DIGEST_LEN_SHA1      20
28 #define QCRYPTO_HASH_DIGEST_LEN_SHA224    28
29 #define QCRYPTO_HASH_DIGEST_LEN_SHA256    32
30 #define QCRYPTO_HASH_DIGEST_LEN_SHA384    48
31 #define QCRYPTO_HASH_DIGEST_LEN_SHA512    64
32 #define QCRYPTO_HASH_DIGEST_LEN_RIPEMD160 20
33 
34 /* See also "QCryptoHashAlgo" defined in qapi/crypto.json */
35 
36 /**
37  * qcrypto_hash_supports:
38  * @alg: the hash algorithm
39  *
40  * Determine if @alg hash algorithm is supported by the
41  * current configured build.
42  *
43  * Returns: true if the algorithm is supported, false otherwise
44  */
45 gboolean qcrypto_hash_supports(QCryptoHashAlgo alg);
46 
47 
48 /**
49  * qcrypto_hash_digest_len:
50  * @alg: the hash algorithm
51  *
52  * Determine the size of the hash digest in bytes
53  *
54  * Returns: the digest length in bytes
55  */
56 size_t qcrypto_hash_digest_len(QCryptoHashAlgo alg);
57 
58 /**
59  * qcrypto_hash_bytesv:
60  * @alg: the hash algorithm
61  * @iov: the array of memory regions to hash
62  * @niov: the length of @iov
63  * @result: pointer to hold output hash
64  * @resultlen: pointer to hold length of @result
65  * @errp: pointer to a NULL-initialized error object
66  *
67  * Computes the hash across all the memory regions
68  * present in @iov. The @result pointer will be
69  * filled with raw bytes representing the computed
70  * hash, which will have length @resultlen. The
71  * memory pointer in @result must be released
72  * with a call to g_free() when no longer required.
73  *
74  * Returns: 0 on success, -1 on error
75  */
76 int qcrypto_hash_bytesv(QCryptoHashAlgo alg,
77                         const struct iovec *iov,
78                         size_t niov,
79                         uint8_t **result,
80                         size_t *resultlen,
81                         Error **errp);
82 
83 /**
84  * qcrypto_hash_bytes:
85  * @alg: the hash algorithm
86  * @buf: the memory region to hash
87  * @len: the length of @buf
88  * @result: pointer to hold output hash
89  * @resultlen: pointer to hold length of @result
90  * @errp: pointer to a NULL-initialized error object
91  *
92  * Computes the hash across all the memory region
93  * @buf of length @len. The @result pointer will be
94  * filled with raw bytes representing the computed
95  * hash, which will have length @resultlen. The
96  * memory pointer in @result must be released
97  * with a call to g_free() when no longer required.
98  *
99  * Returns: 0 on success, -1 on error
100  */
101 int qcrypto_hash_bytes(QCryptoHashAlgo alg,
102                        const char *buf,
103                        size_t len,
104                        uint8_t **result,
105                        size_t *resultlen,
106                        Error **errp);
107 
108 /**
109  * qcrypto_hash_digestv:
110  * @alg: the hash algorithm
111  * @iov: the array of memory regions to hash
112  * @niov: the length of @iov
113  * @digest: pointer to hold output hash
114  * @errp: pointer to a NULL-initialized error object
115  *
116  * Computes the hash across all the memory regions
117  * present in @iov. The @digest pointer will be
118  * filled with the printable hex digest of the computed
119  * hash, which will be terminated by '\0'. The
120  * memory pointer in @digest must be released
121  * with a call to g_free() when no longer required.
122  *
123  * Returns: 0 on success, -1 on error
124  */
125 int qcrypto_hash_digestv(QCryptoHashAlgo alg,
126                          const struct iovec *iov,
127                          size_t niov,
128                          char **digest,
129                          Error **errp);
130 
131 /**
132  * qcrypto_hash_digest:
133  * @alg: the hash algorithm
134  * @buf: the memory region to hash
135  * @len: the length of @buf
136  * @digest: pointer to hold output hash
137  * @errp: pointer to a NULL-initialized error object
138  *
139  * Computes the hash across all the memory region
140  * @buf of length @len. The @digest pointer will be
141  * filled with the printable hex digest of the computed
142  * hash, which will be terminated by '\0'. The
143  * memory pointer in @digest must be released
144  * with a call to g_free() when no longer required.
145  *
146  * Returns: 0 on success, -1 on error
147  */
148 int qcrypto_hash_digest(QCryptoHashAlgo alg,
149                         const char *buf,
150                         size_t len,
151                         char **digest,
152                         Error **errp);
153 
154 /**
155  * qcrypto_hash_base64v:
156  * @alg: the hash algorithm
157  * @iov: the array of memory regions to hash
158  * @niov: the length of @iov
159  * @base64: pointer to hold output hash
160  * @errp: pointer to a NULL-initialized error object
161  *
162  * Computes the hash across all the memory regions
163  * present in @iov. The @base64 pointer will be
164  * filled with the base64 encoding of the computed
165  * hash, which will be terminated by '\0'. The
166  * memory pointer in @base64 must be released
167  * with a call to g_free() when no longer required.
168  *
169  * Returns: 0 on success, -1 on error
170  */
171 int qcrypto_hash_base64v(QCryptoHashAlgo alg,
172                          const struct iovec *iov,
173                          size_t niov,
174                          char **base64,
175                          Error **errp);
176 
177 /**
178  * qcrypto_hash_base64:
179  * @alg: the hash algorithm
180  * @buf: the memory region to hash
181  * @len: the length of @buf
182  * @base64: pointer to hold output hash
183  * @errp: pointer to a NULL-initialized error object
184  *
185  * Computes the hash across all the memory region
186  * @buf of length @len. The @base64 pointer will be
187  * filled with the base64 encoding of the computed
188  * hash, which will be terminated by '\0'. The
189  * memory pointer in @base64 must be released
190  * with a call to g_free() when no longer required.
191  *
192  * Returns: 0 on success, -1 on error
193  */
194 int qcrypto_hash_base64(QCryptoHashAlgo alg,
195                         const char *buf,
196                         size_t len,
197                         char **base64,
198                         Error **errp);
199 
200 #endif /* QCRYPTO_HASH_H */
201