1 | /** @file
|
---|
2 | * IPRT - Crypto - Cryptographic Hash / Message Digest.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2014-2019 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef IPRT_INCLUDED_crypto_digest_h
|
---|
27 | #define IPRT_INCLUDED_crypto_digest_h
|
---|
28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
29 | # pragma once
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <iprt/asn1.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | RT_C_DECLS_BEGIN
|
---|
36 |
|
---|
37 | /** @defgroup grp_rt_crdigest RTCrDigest - Crypographic Hash / Message Digest API.
|
---|
38 | * @ingroup grp_rt
|
---|
39 | * @{
|
---|
40 | */
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Cryptographic hash / message digest provider descriptor.
|
---|
44 | *
|
---|
45 | * This gives the basic details and identifiers of the algorithm as well as
|
---|
46 | * function pointers to the implementation.
|
---|
47 | */
|
---|
48 | typedef struct RTCRDIGESTDESC
|
---|
49 | {
|
---|
50 | /** The message digest provider name. */
|
---|
51 | const char *pszName;
|
---|
52 | /** The object ID string. */
|
---|
53 | const char *pszObjId;
|
---|
54 | /** Pointer to a NULL terminated table of alias object IDs (optional). */
|
---|
55 | const char * const *papszObjIdAliases;
|
---|
56 | /** The IPRT digest type. */
|
---|
57 | RTDIGESTTYPE enmType;
|
---|
58 | /** The max size of the final hash (binary). */
|
---|
59 | uint32_t cbHash;
|
---|
60 | /** The size of the state. */
|
---|
61 | uint32_t cbState;
|
---|
62 | /** Flags, RTCRDIGESTDESC_F_XXX. */
|
---|
63 | uint32_t fFlags;
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Allocates the digest data.
|
---|
67 | */
|
---|
68 | DECLCALLBACKMEMBER(void *, pfnNew)(void);
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Frees the digest data.
|
---|
72 | *
|
---|
73 | * @param pvState The opaque message digest state.
|
---|
74 | */
|
---|
75 | DECLCALLBACKMEMBER(void, pfnFree)(void *pvState);
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Updates the digest with more data.
|
---|
79 | *
|
---|
80 | * @param pvState The opaque message digest state.
|
---|
81 | * @param pvData The data to add to the digest.
|
---|
82 | * @param cbData The amount of data to add to the digest.
|
---|
83 | */
|
---|
84 | DECLCALLBACKMEMBER(void, pfnUpdate)(void *pvState, const void *pvData, size_t cbData);
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Finalizes the digest calculation.
|
---|
88 | *
|
---|
89 | * @param pvState The opaque message digest state.
|
---|
90 | * @param pbHash Where to store the output digest. This buffer is at
|
---|
91 | * least RTCRDIGESTDESC::cbHash bytes large.
|
---|
92 | */
|
---|
93 | DECLCALLBACKMEMBER(void, pfnFinal)(void *pvState, uint8_t *pbHash);
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * (Re-)Initializes the digest. Optional.
|
---|
97 | *
|
---|
98 | * Optional, RT_BZERO will be used if NULL.
|
---|
99 | *
|
---|
100 | * @returns IPRT status code.
|
---|
101 | * @param pvState The opaque message digest state.
|
---|
102 | * @param pvOpaque Opaque algortihm specific parameter.
|
---|
103 | * @param fReInit Set if this is a re-init call.
|
---|
104 | */
|
---|
105 | DECLCALLBACKMEMBER(int, pfnInit)(void *pvState, void *pvOpaque, bool fReInit);
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Deletes the message digest state.
|
---|
109 | *
|
---|
110 | * Optional, memset will be used if NULL.
|
---|
111 | *
|
---|
112 | * @param pvState The opaque message digest state.
|
---|
113 | */
|
---|
114 | DECLCALLBACKMEMBER(void, pfnDelete)(void *pvState);
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Clones the message digest state.
|
---|
118 | *
|
---|
119 | * Optional, memcpy will be used if NULL.
|
---|
120 | *
|
---|
121 | * @returns IPRT status code.
|
---|
122 | * @param pvState The opaque message digest state (destination).
|
---|
123 | * @param pvSrcState The opaque message digest state to clone (source).
|
---|
124 | */
|
---|
125 | DECLCALLBACKMEMBER(int, pfnClone)(void *pvState, void const *pvSrcState);
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Gets the hash size.
|
---|
129 | *
|
---|
130 | * Optional, if not provided RTCRDIGESTDESC::cbHash will be returned. If
|
---|
131 | * provided though, RTCRDIGESTDESC::cbHash must be set to the largest possible
|
---|
132 | * hash size.
|
---|
133 | *
|
---|
134 | * @returns The hash size.
|
---|
135 | * @param pvState The opaque message digest state.
|
---|
136 | */
|
---|
137 | DECLCALLBACKMEMBER(uint32_t, pfnGetHashSize)(void *pvState);
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Gets the digest type (when enmType is RTDIGESTTYPE_UNKNOWN).
|
---|
141 | *
|
---|
142 | * @returns The hash size.
|
---|
143 | * @param pvState The opaque message digest state.
|
---|
144 | */
|
---|
145 | DECLCALLBACKMEMBER(RTDIGESTTYPE, pfnGetDigestType)(void *pvState);
|
---|
146 | } RTCRDIGESTDESC;
|
---|
147 | /** Pointer to const message digest details and vtable. */
|
---|
148 | typedef RTCRDIGESTDESC const *PCRTCRDIGESTDESC;
|
---|
149 |
|
---|
150 | /** @name RTCRDIGESTDESC_F_XXX
|
---|
151 | * @{ */
|
---|
152 | /** Digest is deprecated. */
|
---|
153 | #define RTCRDIGESTDESC_F_DEPRECATED RT_BIT_32(0)
|
---|
154 | /** Digest is compromised. */
|
---|
155 | #define RTCRDIGESTDESC_F_COMPROMISED RT_BIT_32(1)
|
---|
156 | /** Digest is severely compromised. */
|
---|
157 | #define RTCRDIGESTDESC_F_SERVERELY_COMPROMISED RT_BIT_32(2)
|
---|
158 | /** @} */
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Finds a cryptographic hash / message digest descriptor by object identifier
|
---|
162 | * string.
|
---|
163 | *
|
---|
164 | * @returns Pointer to the message digest details & vtable if found. NULL if
|
---|
165 | * not found.
|
---|
166 | * @param pszObjId The dotted object identifier string of the message
|
---|
167 | * digest algorithm.
|
---|
168 | * @param ppvOpaque Where to return an opaque implementation specfici
|
---|
169 | * sub-type indicator that can be passed to
|
---|
170 | * RTCrDigestCreate. This is optional, fewer
|
---|
171 | * algortihms are available if not specified.
|
---|
172 | */
|
---|
173 | RTDECL(PCRTCRDIGESTDESC) RTCrDigestFindByObjIdString(const char *pszObjId, void **ppvOpaque);
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Finds a cryptographic hash / message digest descriptor by object identifier
|
---|
177 | * ASN.1 object.
|
---|
178 | *
|
---|
179 | * @returns Pointer to the message digest details & vtable if found. NULL if
|
---|
180 | * not found.
|
---|
181 | * @param pObjId The ASN.1 object ID of the message digest algorithm.
|
---|
182 | * @param ppvOpaque Where to return an opaque implementation specfici
|
---|
183 | * sub-type indicator that can be passed to
|
---|
184 | * RTCrDigestCreate. This is optional, fewer
|
---|
185 | * algortihms are available if not specified.
|
---|
186 | */
|
---|
187 | RTDECL(PCRTCRDIGESTDESC) RTCrDigestFindByObjId(PCRTASN1OBJID pObjId, void **ppvOpaque);
|
---|
188 |
|
---|
189 | RTDECL(PCRTCRDIGESTDESC) RTCrDigestFindByType(RTDIGESTTYPE enmDigestType);
|
---|
190 | RTDECL(int) RTCrDigestCreateByObjIdString(PRTCRDIGEST phDigest, const char *pszObjId);
|
---|
191 | RTDECL(int) RTCrDigestCreateByObjId(PRTCRDIGEST phDigest, PCRTASN1OBJID pObjId);
|
---|
192 | RTDECL(int) RTCrDigestCreateByType(PRTCRDIGEST phDigest, RTDIGESTTYPE enmDigestType);
|
---|
193 |
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * @returns IPRT status code.
|
---|
197 | * @retval VINF_SUCCESS on success.
|
---|
198 | * @retval VINF_CR_DIGEST_DEPRECATED on success from a deprecated hash algorithm.
|
---|
199 | * @retval VINF_CR_DIGEST_COMPROMISED on success from a compromised hash algorithm.
|
---|
200 | * @retval VINF_CR_DIGEST_SEVERELY_COMPROMISED on success from a severely compromised hash algorithm.
|
---|
201 | */
|
---|
202 | RTDECL(int) RTCrDigestCreate(PRTCRDIGEST phDigest, PCRTCRDIGESTDESC pDesc, void *pvOpaque);
|
---|
203 | /**
|
---|
204 | * @returns IPRT status code.
|
---|
205 | * @retval VINF_SUCCESS on success.
|
---|
206 | * @retval VINF_CR_DIGEST_DEPRECATED on success from a deprecated hash algorithm.
|
---|
207 | * @retval VINF_CR_DIGEST_COMPROMISED on success from a compromised hash algorithm.
|
---|
208 | * @retval VINF_CR_DIGEST_SEVERELY_COMPROMISED on success from a severely compromised hash algorithm.
|
---|
209 | */
|
---|
210 | RTDECL(int) RTCrDigestClone(PRTCRDIGEST phDigest, RTCRDIGEST hSrc);
|
---|
211 | /**
|
---|
212 | * Resets the digest to start calculating a new digest.
|
---|
213 | */
|
---|
214 | RTDECL(int) RTCrDigestReset(RTCRDIGEST hDigest);
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Retains a references to the digest.
|
---|
218 | *
|
---|
219 | * @returns New reference count. UINT32_MAX if invalid handle.
|
---|
220 | * @param hDigest Handle to the digest.
|
---|
221 | */
|
---|
222 | RTDECL(uint32_t) RTCrDigestRetain(RTCRDIGEST hDigest);
|
---|
223 | /**
|
---|
224 | * Releases a references to the digest.
|
---|
225 | *
|
---|
226 | * @returns New reference count. UINT32_MAX if invalid handle.
|
---|
227 | * @param hDigest Handle to the digest. NIL is ignored (returns 0).
|
---|
228 | */
|
---|
229 | RTDECL(uint32_t) RTCrDigestRelease(RTCRDIGEST hDigest);
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Updates the digest with more message data.
|
---|
233 | *
|
---|
234 | * @returns IPRT status code.
|
---|
235 | * @param hDigest Handle to the digest.
|
---|
236 | * @param pvData Pointer to the message data.
|
---|
237 | * @param cbData The number of bytes of data @a pvData points to.
|
---|
238 | */
|
---|
239 | RTDECL(int) RTCrDigestUpdate(RTCRDIGEST hDigest, void const *pvData, size_t cbData);
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Updates the digest with more message data from the given VFS file handle.
|
---|
243 | *
|
---|
244 | * @returns IPRT status code.
|
---|
245 | * @param hDigest Handle to the digest.
|
---|
246 | * @param hVfsFile Handle to the VFS file.
|
---|
247 | * @param fRewindFile Rewind to the start of the file if @a true, start
|
---|
248 | * consumption at the current file position if @a false.
|
---|
249 | */
|
---|
250 | RTDECL(int) RTCrDigestUpdateFromVfsFile(RTCRDIGEST hDigest, RTVFSFILE hVfsFile, bool fRewindFile);
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Finalizes the hash calculation, copying out the resulting hash value.
|
---|
254 | *
|
---|
255 | * This can be called more than once and will always return the same result.
|
---|
256 | *
|
---|
257 | * @returns IPRT status code.
|
---|
258 | * @retval VINF_SUCCESS on success.
|
---|
259 | * @retval VINF_CR_DIGEST_DEPRECATED on success from a deprecated hash algorithm.
|
---|
260 | * @retval VINF_CR_DIGEST_COMPROMISED on success from a compromised hash algorithm.
|
---|
261 | * @retval VINF_CR_DIGEST_SEVERELY_COMPROMISED on success from a severely compromised hash algorithm.
|
---|
262 | * @retval VINF_BUFFER_UNDERFLOW if the supplied buffer is too big.
|
---|
263 | * @retval VERR_BUFFER_OVERFLOW if the supplied buffer is too small.
|
---|
264 | * @retval VERR_INVALID_STATE if there is nothing to finalize.
|
---|
265 | *
|
---|
266 | * @param hDigest The digest handle.
|
---|
267 | * @param pvHash Where to return the hash. Optional.
|
---|
268 | * @param cbHash The hash size. Optional.
|
---|
269 | */
|
---|
270 | RTDECL(int) RTCrDigestFinal(RTCRDIGEST hDigest, void *pvHash, size_t cbHash);
|
---|
271 |
|
---|
272 | RTDECL(bool) RTCrDigestMatch(RTCRDIGEST hDigest, void const *pvHash, size_t cbHash);
|
---|
273 | RTDECL(uint8_t const *) RTCrDigestGetHash(RTCRDIGEST hDigest);
|
---|
274 | RTDECL(uint32_t) RTCrDigestGetHashSize(RTCRDIGEST hDigest);
|
---|
275 | RTDECL(uint64_t) RTCrDigestGetConsumedSize(RTCRDIGEST hDigest);
|
---|
276 | RTDECL(bool) RTCrDigestIsFinalized(RTCRDIGEST hDigest);
|
---|
277 | RTDECL(RTDIGESTTYPE) RTCrDigestGetType(RTCRDIGEST hDigest);
|
---|
278 | RTDECL(const char *) RTCrDigestGetAlgorithmOid(RTCRDIGEST hDigest);
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Gets the flags for the algorithm.
|
---|
282 | *
|
---|
283 | * @returns RTCRDIGESTDESC_F_XXX, UINT32_MAX on invalid handle.
|
---|
284 | * @param hDigest The digest handle.
|
---|
285 | */
|
---|
286 | RTDECL(uint32_t) RTCrDigestGetFlags(RTCRDIGEST hDigest);
|
---|
287 |
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Translates an IPRT digest type value to an OID.
|
---|
291 | *
|
---|
292 | * @returns Dotted OID string on success, NULL if not translatable.
|
---|
293 | * @param enmDigestType The IPRT digest type value to convert.
|
---|
294 | */
|
---|
295 | RTDECL(const char *) RTCrDigestTypeToAlgorithmOid(RTDIGESTTYPE enmDigestType);
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Translates an IPRT digest type value to a name/descriptive string.
|
---|
299 | *
|
---|
300 | * The purpose here is for human readable output rather than machine readable
|
---|
301 | * output, i.e. the names aren't set in stone.
|
---|
302 | *
|
---|
303 | * @returns Pointer to read-only string, NULL if unknown type.
|
---|
304 | * @param enmDigestType The IPRT digest type value to convert.
|
---|
305 | */
|
---|
306 | RTDECL(const char *) RTCrDigestTypeToName(RTDIGESTTYPE enmDigestType);
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Translates an IPRT digest type value to a hash size.
|
---|
310 | *
|
---|
311 | * @returns Hash size (in bytes).
|
---|
312 | * @param enmDigestType The IPRT digest type value to convert.
|
---|
313 | */
|
---|
314 | RTDECL(uint32_t) RTCrDigestTypeToHashSize(RTDIGESTTYPE enmDigestType);
|
---|
315 |
|
---|
316 | /** @} */
|
---|
317 |
|
---|
318 | RT_C_DECLS_END
|
---|
319 |
|
---|
320 | #endif /* !IPRT_INCLUDED_crypto_digest_h */
|
---|
321 |
|
---|