VirtualBox

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

Last change on this file was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use