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 |
|
---|
32 | RT_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 | */
|
---|
45 | typedef 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 | /** Reserved. */
|
---|
60 | uint32_t uReserved;
|
---|
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. */
|
---|
145 | typedef RTCRDIGESTDESC const *PCRTCRDIGESTDESC;
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Finds a cryptographic hash / message digest descriptor by object identifier
|
---|
149 | * string.
|
---|
150 | *
|
---|
151 | * @returns Pointer to the message digest details & vtable if found. NULL if
|
---|
152 | * not found.
|
---|
153 | * @param pszObjId The dotted object identifier string of the message
|
---|
154 | * digest algorithm.
|
---|
155 | * @param ppvOpaque Where to return an opaque implementation specfici
|
---|
156 | * sub-type indicator that can be passed to
|
---|
157 | * RTCrDigestCreate. This is optional, fewer
|
---|
158 | * algortihms are available if not specified.
|
---|
159 | */
|
---|
160 | RTDECL(PCRTCRDIGESTDESC) RTCrDigestFindByObjIdString(const char *pszObjId, void **ppvOpaque);
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Finds a cryptographic hash / message digest descriptor by object identifier
|
---|
164 | * ASN.1 object.
|
---|
165 | *
|
---|
166 | * @returns Pointer to the message digest details & vtable if found. NULL if
|
---|
167 | * not found.
|
---|
168 | * @param pObjId The ASN.1 object ID of the message digest algorithm.
|
---|
169 | * @param ppvOpaque Where to return an opaque implementation specfici
|
---|
170 | * sub-type indicator that can be passed to
|
---|
171 | * RTCrDigestCreate. This is optional, fewer
|
---|
172 | * algortihms are available if not specified.
|
---|
173 | */
|
---|
174 | RTDECL(PCRTCRDIGESTDESC) RTCrDigestFindByObjId(PCRTASN1OBJID pObjId, void **ppvOpaque);
|
---|
175 |
|
---|
176 | RTDECL(PCRTCRDIGESTDESC) RTCrDigestFindByType(RTDIGESTTYPE enmDigestType);
|
---|
177 | RTDECL(int) RTCrDigestCreateByObjIdString(PRTCRDIGEST phDigest, const char *pszObjId);
|
---|
178 | RTDECL(int) RTCrDigestCreateByObjId(PRTCRDIGEST phDigest, PCRTASN1OBJID pObjId);
|
---|
179 | RTDECL(int) RTCrDigestCreateByType(PRTCRDIGEST phDigest, RTDIGESTTYPE enmDigestType);
|
---|
180 |
|
---|
181 |
|
---|
182 | RTDECL(int) RTCrDigestCreate(PRTCRDIGEST phDigest, PCRTCRDIGESTDESC pDesc, void *pvOpaque);
|
---|
183 | RTDECL(int) RTCrDigestClone(PRTCRDIGEST phDigest, RTCRDIGEST hSrc);
|
---|
184 | RTDECL(int) RTCrDigestReset(RTCRDIGEST hDigest);
|
---|
185 | RTDECL(uint32_t) RTCrDigestRetain(RTCRDIGEST hDigest);
|
---|
186 | RTDECL(uint32_t) RTCrDigestRelease(RTCRDIGEST hDigest);
|
---|
187 | RTDECL(int) RTCrDigestUpdate(RTCRDIGEST hDigest, void const *pvData, size_t cbData);
|
---|
188 | RTDECL(int) RTCrDigestUpdateFromVfsFile(RTCRDIGEST hDigest, RTVFSFILE hVfsFile, bool fRewindFile);
|
---|
189 | RTDECL(int) RTCrDigestFinal(RTCRDIGEST hDigest, void *pvHash, size_t cbHash);
|
---|
190 | RTDECL(bool) RTCrDigestMatch(RTCRDIGEST hDigest, void const *pvHash, size_t cbHash);
|
---|
191 | RTDECL(uint8_t const *) RTCrDigestGetHash(RTCRDIGEST hDigest);
|
---|
192 | RTDECL(uint32_t) RTCrDigestGetHashSize(RTCRDIGEST hDigest);
|
---|
193 | RTDECL(uint64_t) RTCrDigestGetConsumedSize(RTCRDIGEST hDigest);
|
---|
194 | RTDECL(bool) RTCrDigestIsFinalized(RTCRDIGEST hDigest);
|
---|
195 | RTDECL(RTDIGESTTYPE) RTCrDigestGetType(RTCRDIGEST hDigest);
|
---|
196 | RTDECL(const char *) RTCrDigestGetAlgorithmOid(RTCRDIGEST hDigest);
|
---|
197 |
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Translates an IPRT digest type value to an OID.
|
---|
201 | *
|
---|
202 | * @returns Dotted OID string on success, NULL if not translatable.
|
---|
203 | * @param enmDigestType The IPRT digest type value to convert.
|
---|
204 | */
|
---|
205 | RTDECL(const char *) RTCrDigestTypeToAlgorithmOid(RTDIGESTTYPE enmDigestType);
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Translates an IPRT digest type value to a name/descriptive string.
|
---|
209 | *
|
---|
210 | * The purpose here is for human readable output rather than machine readable
|
---|
211 | * output, i.e. the names aren't set in stone.
|
---|
212 | *
|
---|
213 | * @returns Pointer to read-only string, NULL if unknown type.
|
---|
214 | * @param enmDigestType The IPRT digest type value to convert.
|
---|
215 | */
|
---|
216 | RTDECL(const char *) RTCrDigestTypeToName(RTDIGESTTYPE enmDigestType);
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Translates an IPRT digest type value to a hash size.
|
---|
220 | *
|
---|
221 | * @returns Hash size (in bytes).
|
---|
222 | * @param enmDigestType The IPRT digest type value to convert.
|
---|
223 | */
|
---|
224 | RTDECL(uint32_t) RTCrDigestTypeToHashSize(RTDIGESTTYPE enmDigestType);
|
---|
225 |
|
---|
226 | /** @} */
|
---|
227 |
|
---|
228 | RT_C_DECLS_END
|
---|
229 |
|
---|
230 | #endif
|
---|
231 |
|
---|