VirtualBox

source: vbox/trunk/include/iprt/crypto/digest.h@ 73705

Last change on this file since 73705 was 73705, checked in by vboxsync, 7 years ago

IPRT: Better fix for missing md4 failure; adding information status codes for indicating deprecated and compromised digests when used.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.6 KB
Line 
1/** @file
2 * IPRT - Crypto - Cryptographic Hash / Message Digest.
3 */
4
5/*
6 * Copyright (C) 2014-2017 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_crypto_digest_h
27#define ___iprt_crypto_digest_h
28
29#include <iprt/asn1.h>
30
31
32RT_C_DECLS_BEGIN
33
34/** @defgroup grp_rt_crdigest RTCrDigest - Crypographic Hash / Message Digest API.
35 * @ingroup grp_rt
36 * @{
37 */
38
39/**
40 * Cryptographic hash / message digest provider descriptor.
41 *
42 * This gives the basic details and identifiers of the algorithm as well as
43 * function pointers to the implementation.
44 */
45typedef struct RTCRDIGESTDESC
46{
47 /** The message digest provider name. */
48 const char *pszName;
49 /** The object ID string. */
50 const char *pszObjId;
51 /** Pointer to a NULL terminated table of alias object IDs (optional). */
52 const char * const *papszObjIdAliases;
53 /** The IPRT digest type. */
54 RTDIGESTTYPE enmType;
55 /** The max size of the final hash (binary). */
56 uint32_t cbHash;
57 /** The size of the state. */
58 uint32_t cbState;
59 /** Flags, RTCRDIGESTDESC_F_XXX. */
60 uint32_t fFlags;
61
62 /**
63 * Allocates the digest data.
64 */
65 DECLCALLBACKMEMBER(void *, pfnNew)(void);
66
67 /**
68 * Frees the digest data.
69 *
70 * @param pvState The opaque message digest state.
71 */
72 DECLCALLBACKMEMBER(void, pfnFree)(void *pvState);
73
74 /**
75 * Updates the digest with more data.
76 *
77 * @param pvState The opaque message digest state.
78 * @param pvData The data to add to the digest.
79 * @param cbData The amount of data to add to the digest.
80 */
81 DECLCALLBACKMEMBER(void, pfnUpdate)(void *pvState, const void *pvData, size_t cbData);
82
83 /**
84 * Finalizes the digest calculation.
85 *
86 * @param pvState The opaque message digest state.
87 * @param pbHash Where to store the output digest. This buffer is at
88 * least RTCRDIGESTDESC::cbHash bytes large.
89 */
90 DECLCALLBACKMEMBER(void, pfnFinal)(void *pvState, uint8_t *pbHash);
91
92 /**
93 * (Re-)Initializes the digest. Optional.
94 *
95 * Optional, RT_BZERO will be used if NULL.
96 *
97 * @returns IPRT status code.
98 * @param pvState The opaque message digest state.
99 * @param pvOpaque Opaque algortihm specific parameter.
100 * @param fReInit Set if this is a re-init call.
101 */
102 DECLCALLBACKMEMBER(int, pfnInit)(void *pvState, void *pvOpaque, bool fReInit);
103
104 /**
105 * Deletes the message digest state.
106 *
107 * Optional, memset will be used if NULL.
108 *
109 * @param pvState The opaque message digest state.
110 */
111 DECLCALLBACKMEMBER(void, pfnDelete)(void *pvState);
112
113 /**
114 * Clones the message digest state.
115 *
116 * Optional, memcpy will be used if NULL.
117 *
118 * @returns IPRT status code.
119 * @param pvState The opaque message digest state (destination).
120 * @param pvSrcState The opaque message digest state to clone (source).
121 */
122 DECLCALLBACKMEMBER(int, pfnClone)(void *pvState, void const *pvSrcState);
123
124 /**
125 * Gets the hash size.
126 *
127 * Optional, if not provided RTCRDIGESTDESC::cbHash will be returned. If
128 * provided though, RTCRDIGESTDESC::cbHash must be set to the largest possible
129 * hash size.
130 *
131 * @returns The hash size.
132 * @param pvState The opaque message digest state.
133 */
134 DECLCALLBACKMEMBER(uint32_t, pfnGetHashSize)(void *pvState);
135
136 /**
137 * Gets the digest type (when enmType is RTDIGESTTYPE_UNKNOWN).
138 *
139 * @returns The hash size.
140 * @param pvState The opaque message digest state.
141 */
142 DECLCALLBACKMEMBER(RTDIGESTTYPE, pfnGetDigestType)(void *pvState);
143} RTCRDIGESTDESC;
144/** Pointer to const message digest details and vtable. */
145typedef RTCRDIGESTDESC const *PCRTCRDIGESTDESC;
146
147/** @name RTCRDIGESTDESC_F_XXX
148 * @{ */
149/** Digest is deprecated. */
150#define RTCRDIGESTDESC_F_DEPRECATED RT_BIT_32(0)
151/** Digest is compromised. */
152#define RTCRDIGESTDESC_F_COMPROMISED RT_BIT_32(1)
153/** Digest is severely compromised. */
154#define RTCRDIGESTDESC_F_SERVERELY_COMPROMISED RT_BIT_32(2)
155/** @} */
156
157/**
158 * Finds a cryptographic hash / message digest descriptor by object identifier
159 * string.
160 *
161 * @returns Pointer to the message digest details & vtable if found. NULL if
162 * not found.
163 * @param pszObjId The dotted object identifier string of the message
164 * digest algorithm.
165 * @param ppvOpaque Where to return an opaque implementation specfici
166 * sub-type indicator that can be passed to
167 * RTCrDigestCreate. This is optional, fewer
168 * algortihms are available if not specified.
169 */
170RTDECL(PCRTCRDIGESTDESC) RTCrDigestFindByObjIdString(const char *pszObjId, void **ppvOpaque);
171
172/**
173 * Finds a cryptographic hash / message digest descriptor by object identifier
174 * ASN.1 object.
175 *
176 * @returns Pointer to the message digest details & vtable if found. NULL if
177 * not found.
178 * @param pObjId The ASN.1 object ID of the message digest algorithm.
179 * @param ppvOpaque Where to return an opaque implementation specfici
180 * sub-type indicator that can be passed to
181 * RTCrDigestCreate. This is optional, fewer
182 * algortihms are available if not specified.
183 */
184RTDECL(PCRTCRDIGESTDESC) RTCrDigestFindByObjId(PCRTASN1OBJID pObjId, void **ppvOpaque);
185
186RTDECL(PCRTCRDIGESTDESC) RTCrDigestFindByType(RTDIGESTTYPE enmDigestType);
187RTDECL(int) RTCrDigestCreateByObjIdString(PRTCRDIGEST phDigest, const char *pszObjId);
188RTDECL(int) RTCrDigestCreateByObjId(PRTCRDIGEST phDigest, PCRTASN1OBJID pObjId);
189RTDECL(int) RTCrDigestCreateByType(PRTCRDIGEST phDigest, RTDIGESTTYPE enmDigestType);
190
191
192/**
193 * @returns IPRT status code.
194 * @retval VINF_SUCCESS on success.
195 * @retval VINF_CR_DIGEST_DEPRECATED on success from a deprecated hash algorithm.
196 * @retval VINF_CR_DIGEST_COMPROMISED on success from a compromised hash algorithm.
197 * @retval VINF_CR_DIGEST_SEVERELY_COMPROMISED on success from a severely compromised hash algorithm.
198 */
199RTDECL(int) RTCrDigestCreate(PRTCRDIGEST phDigest, PCRTCRDIGESTDESC pDesc, void *pvOpaque);
200/**
201 * @returns IPRT status code.
202 * @retval VINF_SUCCESS on success.
203 * @retval VINF_CR_DIGEST_DEPRECATED on success from a deprecated hash algorithm.
204 * @retval VINF_CR_DIGEST_COMPROMISED on success from a compromised hash algorithm.
205 * @retval VINF_CR_DIGEST_SEVERELY_COMPROMISED on success from a severely compromised hash algorithm.
206 */
207RTDECL(int) RTCrDigestClone(PRTCRDIGEST phDigest, RTCRDIGEST hSrc);
208RTDECL(int) RTCrDigestReset(RTCRDIGEST hDigest);
209RTDECL(uint32_t) RTCrDigestRetain(RTCRDIGEST hDigest);
210RTDECL(uint32_t) RTCrDigestRelease(RTCRDIGEST hDigest);
211RTDECL(int) RTCrDigestUpdate(RTCRDIGEST hDigest, void const *pvData, size_t cbData);
212RTDECL(int) RTCrDigestUpdateFromVfsFile(RTCRDIGEST hDigest, RTVFSFILE hVfsFile, bool fRewindFile);
213
214/**
215 * Finalizes the hash calculation, copying out the resulting hash value.
216 *
217 * This can be called more than once and will always return the same result.
218 *
219 * @returns IPRT status code.
220 * @retval VINF_SUCCESS on success.
221 * @retval VINF_CR_DIGEST_DEPRECATED on success from a deprecated hash algorithm.
222 * @retval VINF_CR_DIGEST_COMPROMISED on success from a compromised hash algorithm.
223 * @retval VINF_CR_DIGEST_SEVERELY_COMPROMISED on success from a severely compromised hash algorithm.
224 * @retval VINF_BUFFER_UNDERFLOW if the supplied buffer is too big.
225 * @retval VERR_BUFFER_OVERFLOW if the supplied buffer is too small.
226 * @retval VERR_INVALID_STATE if there is nothing to finalize.
227 *
228 * @param hDigest The digest handle.
229 * @param pvHash Where to return the hash. Optional.
230 * @param cbHash The hash size. Optional.
231 */
232RTDECL(int) RTCrDigestFinal(RTCRDIGEST hDigest, void *pvHash, size_t cbHash);
233
234RTDECL(bool) RTCrDigestMatch(RTCRDIGEST hDigest, void const *pvHash, size_t cbHash);
235RTDECL(uint8_t const *) RTCrDigestGetHash(RTCRDIGEST hDigest);
236RTDECL(uint32_t) RTCrDigestGetHashSize(RTCRDIGEST hDigest);
237RTDECL(uint64_t) RTCrDigestGetConsumedSize(RTCRDIGEST hDigest);
238RTDECL(bool) RTCrDigestIsFinalized(RTCRDIGEST hDigest);
239RTDECL(RTDIGESTTYPE) RTCrDigestGetType(RTCRDIGEST hDigest);
240RTDECL(const char *) RTCrDigestGetAlgorithmOid(RTCRDIGEST hDigest);
241
242/**
243 * Gets the flags for the algorithm.
244 *
245 * @returns RTCRDIGESTDESC_F_XXX, UINT32_MAX on invalid handle.
246 * @param hDigest The digest handle.
247 */
248RTDECL(uint32_t) RTCrDigestGetFlags(RTCRDIGEST hDigest);
249
250
251/**
252 * Translates an IPRT digest type value to an OID.
253 *
254 * @returns Dotted OID string on success, NULL if not translatable.
255 * @param enmDigestType The IPRT digest type value to convert.
256 */
257RTDECL(const char *) RTCrDigestTypeToAlgorithmOid(RTDIGESTTYPE enmDigestType);
258
259/**
260 * Translates an IPRT digest type value to a name/descriptive string.
261 *
262 * The purpose here is for human readable output rather than machine readable
263 * output, i.e. the names aren't set in stone.
264 *
265 * @returns Pointer to read-only string, NULL if unknown type.
266 * @param enmDigestType The IPRT digest type value to convert.
267 */
268RTDECL(const char *) RTCrDigestTypeToName(RTDIGESTTYPE enmDigestType);
269
270/**
271 * Translates an IPRT digest type value to a hash size.
272 *
273 * @returns Hash size (in bytes).
274 * @param enmDigestType The IPRT digest type value to convert.
275 */
276RTDECL(uint32_t) RTCrDigestTypeToHashSize(RTDIGESTTYPE enmDigestType);
277
278/** @} */
279
280RT_C_DECLS_END
281
282#endif
283
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette