1 | /* $Id: digest-builtin.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Crypto - Cryptographic Hash / Message Digest API, Built-in providers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "internal/iprt.h"
|
---|
32 | #include <iprt/crypto/digest.h>
|
---|
33 |
|
---|
34 | #include <iprt/err.h>
|
---|
35 | #include <iprt/mem.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/md2.h>
|
---|
38 | #include <iprt/md5.h>
|
---|
39 | #include <iprt/sha.h>
|
---|
40 | #include <iprt/crypto/pkix.h>
|
---|
41 |
|
---|
42 | #ifdef IPRT_WITH_OPENSSL
|
---|
43 | # include "internal/iprt-openssl.h"
|
---|
44 | # include <openssl/evp.h>
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * MD2
|
---|
49 | */
|
---|
50 |
|
---|
51 | /** @impl_interface_method{RTCRDIGESTDESC::pfnUpdate} */
|
---|
52 | static DECLCALLBACK(void) rtCrDigestMd2_Update(void *pvState, const void *pvData, size_t cbData)
|
---|
53 | {
|
---|
54 | RTMd2Update((PRTMD2CONTEXT)pvState, pvData, cbData);
|
---|
55 | }
|
---|
56 |
|
---|
57 | /** @impl_interface_method{RTCRDIGESTDESC::pfnFinal} */
|
---|
58 | static DECLCALLBACK(void) rtCrDigestMd2_Final(void *pvState, uint8_t *pbHash)
|
---|
59 | {
|
---|
60 | RTMd2Final((PRTMD2CONTEXT)pvState, pbHash);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /** @impl_interface_method{RTCRDIGESTDESC::pfnInit} */
|
---|
64 | static DECLCALLBACK(int) rtCrDigestMd2_Init(void *pvState, void *pvOpaque, bool fReInit)
|
---|
65 | {
|
---|
66 | AssertReturn(pvOpaque == NULL, VERR_INVALID_PARAMETER);
|
---|
67 | RTMd2Init((PRTMD2CONTEXT)pvState);
|
---|
68 | return VINF_SUCCESS;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /** MD2 alias ODIs. */
|
---|
72 | static const char * const g_apszMd2Aliases[] =
|
---|
73 | {
|
---|
74 | RTCR_PKCS1_MD2_WITH_RSA_OID,
|
---|
75 | "1.3.14.3.2.24" /* OIW md2WithRSASignature */,
|
---|
76 | NULL
|
---|
77 | };
|
---|
78 |
|
---|
79 | /** MD2 descriptor. */
|
---|
80 | static RTCRDIGESTDESC const g_rtCrDigestMd2Desc =
|
---|
81 | {
|
---|
82 | "md2",
|
---|
83 | "1.2.840.113549.2.2",
|
---|
84 | g_apszMd2Aliases,
|
---|
85 | RTDIGESTTYPE_MD2,
|
---|
86 | RTMD2_HASH_SIZE,
|
---|
87 | sizeof(RTMD2CONTEXT),
|
---|
88 | 0,
|
---|
89 | rtCrDigestMd2_Update,
|
---|
90 | rtCrDigestMd2_Final,
|
---|
91 | rtCrDigestMd2_Init,
|
---|
92 | NULL,
|
---|
93 | NULL,
|
---|
94 | NULL,
|
---|
95 | NULL,
|
---|
96 | };
|
---|
97 |
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * MD5
|
---|
101 | */
|
---|
102 |
|
---|
103 | /** @impl_interface_method{RTCRDIGESTDESC::pfnUpdate} */
|
---|
104 | static DECLCALLBACK(void) rtCrDigestMd5_Update(void *pvState, const void *pvData, size_t cbData)
|
---|
105 | {
|
---|
106 | RTMd5Update((PRTMD5CONTEXT)pvState, pvData, cbData);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** @impl_interface_method{RTCRDIGESTDESC::pfnFinal} */
|
---|
110 | static DECLCALLBACK(void) rtCrDigestMd5_Final(void *pvState, uint8_t *pbHash)
|
---|
111 | {
|
---|
112 | RTMd5Final(pbHash, (PRTMD5CONTEXT)pvState);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /** @impl_interface_method{RTCRDIGESTDESC::pfnInit} */
|
---|
116 | static DECLCALLBACK(int) rtCrDigestMd5_Init(void *pvState, void *pvOpaque, bool fReInit)
|
---|
117 | {
|
---|
118 | AssertReturn(pvOpaque == NULL, VERR_INVALID_PARAMETER);
|
---|
119 | RTMd5Init((PRTMD5CONTEXT)pvState);
|
---|
120 | return VINF_SUCCESS;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /** MD5 alias ODIs. */
|
---|
124 | static const char * const g_apszMd5Aliases[] =
|
---|
125 | {
|
---|
126 | RTCR_PKCS1_MD5_WITH_RSA_OID,
|
---|
127 | "1.3.14.3.2.25" /* OIW md5WithRSASignature */,
|
---|
128 | NULL
|
---|
129 | };
|
---|
130 |
|
---|
131 | /** MD5 descriptor. */
|
---|
132 | static RTCRDIGESTDESC const g_rtCrDigestMd5Desc =
|
---|
133 | {
|
---|
134 | "md5",
|
---|
135 | "1.2.840.113549.2.5",
|
---|
136 | g_apszMd5Aliases,
|
---|
137 | RTDIGESTTYPE_MD5,
|
---|
138 | RTMD5_HASH_SIZE,
|
---|
139 | sizeof(RTMD5CONTEXT),
|
---|
140 | 0,
|
---|
141 | rtCrDigestMd5_Update,
|
---|
142 | rtCrDigestMd5_Final,
|
---|
143 | rtCrDigestMd5_Init,
|
---|
144 | NULL,
|
---|
145 | NULL,
|
---|
146 | NULL,
|
---|
147 | NULL,
|
---|
148 | };
|
---|
149 |
|
---|
150 |
|
---|
151 | /*
|
---|
152 | * SHA-1
|
---|
153 | */
|
---|
154 |
|
---|
155 | /** @impl_interface_method{RTCRDIGESTDESC::pfnUpdate} */
|
---|
156 | static DECLCALLBACK(void) rtCrDigestSha1_Update(void *pvState, const void *pvData, size_t cbData)
|
---|
157 | {
|
---|
158 | RTSha1Update((PRTSHA1CONTEXT)pvState, pvData, cbData);
|
---|
159 | }
|
---|
160 |
|
---|
161 | /** @impl_interface_method{RTCRDIGESTDESC::pfnFinal} */
|
---|
162 | static DECLCALLBACK(void) rtCrDigestSha1_Final(void *pvState, uint8_t *pbHash)
|
---|
163 | {
|
---|
164 | RTSha1Final((PRTSHA1CONTEXT)pvState, pbHash);
|
---|
165 | }
|
---|
166 |
|
---|
167 | /** @impl_interface_method{RTCRDIGESTDESC::pfnInit} */
|
---|
168 | static DECLCALLBACK(int) rtCrDigestSha1_Init(void *pvState, void *pvOpaque, bool fReInit)
|
---|
169 | {
|
---|
170 | AssertReturn(pvOpaque == NULL, VERR_INVALID_PARAMETER);
|
---|
171 | RTSha1Init((PRTSHA1CONTEXT)pvState);
|
---|
172 | return VINF_SUCCESS;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /** SHA-1 alias ODIs. */
|
---|
176 | static const char * const g_apszSha1Aliases[] =
|
---|
177 | {
|
---|
178 | RTCR_PKCS1_SHA1_WITH_RSA_OID,
|
---|
179 | "1.3.14.3.2.29" /* OIW sha1WithRSASignature */,
|
---|
180 | NULL
|
---|
181 | };
|
---|
182 |
|
---|
183 | /** SHA-1 descriptor. */
|
---|
184 | static RTCRDIGESTDESC const g_rtCrDigestSha1Desc =
|
---|
185 | {
|
---|
186 | "sha-1",
|
---|
187 | "1.3.14.3.2.26",
|
---|
188 | g_apszSha1Aliases,
|
---|
189 | RTDIGESTTYPE_SHA1,
|
---|
190 | RTSHA1_HASH_SIZE,
|
---|
191 | sizeof(RTSHA1CONTEXT),
|
---|
192 | 0,
|
---|
193 | rtCrDigestSha1_Update,
|
---|
194 | rtCrDigestSha1_Final,
|
---|
195 | rtCrDigestSha1_Init,
|
---|
196 | NULL,
|
---|
197 | NULL,
|
---|
198 | NULL,
|
---|
199 | NULL,
|
---|
200 | };
|
---|
201 |
|
---|
202 |
|
---|
203 | /*
|
---|
204 | * SHA-256
|
---|
205 | */
|
---|
206 |
|
---|
207 | /** @impl_interface_method{RTCRDIGESTDESC::pfnUpdate} */
|
---|
208 | static DECLCALLBACK(void) rtCrDigestSha256_Update(void *pvState, const void *pvData, size_t cbData)
|
---|
209 | {
|
---|
210 | RTSha256Update((PRTSHA256CONTEXT)pvState, pvData, cbData);
|
---|
211 | }
|
---|
212 |
|
---|
213 | /** @impl_interface_method{RTCRDIGESTDESC::pfnFinal} */
|
---|
214 | static DECLCALLBACK(void) rtCrDigestSha256_Final(void *pvState, uint8_t *pbHash)
|
---|
215 | {
|
---|
216 | RTSha256Final((PRTSHA256CONTEXT)pvState, pbHash);
|
---|
217 | }
|
---|
218 |
|
---|
219 | /** @impl_interface_method{RTCRDIGESTDESC::pfnInit} */
|
---|
220 | static DECLCALLBACK(int) rtCrDigestSha256_Init(void *pvState, void *pvOpaque, bool fReInit)
|
---|
221 | {
|
---|
222 | AssertReturn(pvOpaque == NULL, VERR_INVALID_PARAMETER);
|
---|
223 | RTSha256Init((PRTSHA256CONTEXT)pvState);
|
---|
224 | return VINF_SUCCESS;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /** SHA-256 alias ODIs. */
|
---|
228 | static const char * const g_apszSha256Aliases[] =
|
---|
229 | {
|
---|
230 | RTCR_PKCS1_SHA256_WITH_RSA_OID,
|
---|
231 | NULL
|
---|
232 | };
|
---|
233 |
|
---|
234 | /** SHA-256 descriptor. */
|
---|
235 | static RTCRDIGESTDESC const g_rtCrDigestSha256Desc =
|
---|
236 | {
|
---|
237 | "sha-256",
|
---|
238 | "2.16.840.1.101.3.4.2.1",
|
---|
239 | g_apszSha256Aliases,
|
---|
240 | RTDIGESTTYPE_SHA256,
|
---|
241 | RTSHA256_HASH_SIZE,
|
---|
242 | sizeof(RTSHA256CONTEXT),
|
---|
243 | 0,
|
---|
244 | rtCrDigestSha256_Update,
|
---|
245 | rtCrDigestSha256_Final,
|
---|
246 | rtCrDigestSha256_Init,
|
---|
247 | NULL,
|
---|
248 | NULL,
|
---|
249 | NULL,
|
---|
250 | NULL,
|
---|
251 | };
|
---|
252 |
|
---|
253 |
|
---|
254 | /*
|
---|
255 | * SHA-512
|
---|
256 | */
|
---|
257 |
|
---|
258 | /** @impl_interface_method{RTCRDIGESTDESC::pfnUpdate} */
|
---|
259 | static DECLCALLBACK(void) rtCrDigestSha512_Update(void *pvState, const void *pvData, size_t cbData)
|
---|
260 | {
|
---|
261 | RTSha512Update((PRTSHA512CONTEXT)pvState, pvData, cbData);
|
---|
262 | }
|
---|
263 |
|
---|
264 | /** @impl_interface_method{RTCRDIGESTDESC::pfnFinal} */
|
---|
265 | static DECLCALLBACK(void) rtCrDigestSha512_Final(void *pvState, uint8_t *pbHash)
|
---|
266 | {
|
---|
267 | RTSha512Final((PRTSHA512CONTEXT)pvState, pbHash);
|
---|
268 | }
|
---|
269 |
|
---|
270 | /** @impl_interface_method{RTCRDIGESTDESC::pfnInit} */
|
---|
271 | static DECLCALLBACK(int) rtCrDigestSha512_Init(void *pvState, void *pvOpaque, bool fReInit)
|
---|
272 | {
|
---|
273 | AssertReturn(pvOpaque == NULL, VERR_INVALID_PARAMETER);
|
---|
274 | RTSha512Init((PRTSHA512CONTEXT)pvState);
|
---|
275 | return VINF_SUCCESS;
|
---|
276 | }
|
---|
277 |
|
---|
278 | /** SHA-512 alias ODIs. */
|
---|
279 | static const char * const g_apszSha512Aliases[] =
|
---|
280 | {
|
---|
281 | RTCR_PKCS1_SHA512_WITH_RSA_OID,
|
---|
282 | NULL
|
---|
283 | };
|
---|
284 |
|
---|
285 | /** SHA-512 descriptor. */
|
---|
286 | static RTCRDIGESTDESC const g_rtCrDigestSha512Desc =
|
---|
287 | {
|
---|
288 | "sha-512",
|
---|
289 | "2.16.840.1.101.3.4.2.3",
|
---|
290 | g_apszSha512Aliases,
|
---|
291 | RTDIGESTTYPE_SHA512,
|
---|
292 | RTSHA512_HASH_SIZE,
|
---|
293 | sizeof(RTSHA512CONTEXT),
|
---|
294 | 0,
|
---|
295 | rtCrDigestSha512_Update,
|
---|
296 | rtCrDigestSha512_Final,
|
---|
297 | rtCrDigestSha512_Init,
|
---|
298 | NULL,
|
---|
299 | NULL,
|
---|
300 | NULL,
|
---|
301 | NULL,
|
---|
302 | };
|
---|
303 |
|
---|
304 |
|
---|
305 | /*
|
---|
306 | * SHA-224
|
---|
307 | */
|
---|
308 |
|
---|
309 | /** @impl_interface_method{RTCRDIGESTDESC::pfnUpdate} */
|
---|
310 | static DECLCALLBACK(void) rtCrDigestSha224_Update(void *pvState, const void *pvData, size_t cbData)
|
---|
311 | {
|
---|
312 | RTSha224Update((PRTSHA224CONTEXT)pvState, pvData, cbData);
|
---|
313 | }
|
---|
314 |
|
---|
315 | /** @impl_interface_method{RTCRDIGESTDESC::pfnFinal} */
|
---|
316 | static DECLCALLBACK(void) rtCrDigestSha224_Final(void *pvState, uint8_t *pbHash)
|
---|
317 | {
|
---|
318 | RTSha224Final((PRTSHA224CONTEXT)pvState, pbHash);
|
---|
319 | }
|
---|
320 |
|
---|
321 | /** @impl_interface_method{RTCRDIGESTDESC::pfnInit} */
|
---|
322 | static DECLCALLBACK(int) rtCrDigestSha224_Init(void *pvState, void *pvOpaque, bool fReInit)
|
---|
323 | {
|
---|
324 | AssertReturn(pvOpaque == NULL, VERR_INVALID_PARAMETER);
|
---|
325 | RTSha224Init((PRTSHA224CONTEXT)pvState);
|
---|
326 | return VINF_SUCCESS;
|
---|
327 | }
|
---|
328 |
|
---|
329 | /** SHA-224 alias ODIs. */
|
---|
330 | static const char * const g_apszSha224Aliases[] =
|
---|
331 | {
|
---|
332 | RTCR_PKCS1_SHA224_WITH_RSA_OID,
|
---|
333 | NULL
|
---|
334 | };
|
---|
335 |
|
---|
336 | /** SHA-224 descriptor. */
|
---|
337 | static RTCRDIGESTDESC const g_rtCrDigestSha224Desc =
|
---|
338 | {
|
---|
339 | "sha-224",
|
---|
340 | "2.16.840.1.101.3.4.2.4",
|
---|
341 | g_apszSha224Aliases,
|
---|
342 | RTDIGESTTYPE_SHA224,
|
---|
343 | RTSHA224_HASH_SIZE,
|
---|
344 | sizeof(RTSHA224CONTEXT),
|
---|
345 | 0,
|
---|
346 | rtCrDigestSha224_Update,
|
---|
347 | rtCrDigestSha224_Final,
|
---|
348 | rtCrDigestSha224_Init,
|
---|
349 | NULL,
|
---|
350 | NULL,
|
---|
351 | NULL,
|
---|
352 | NULL,
|
---|
353 | };
|
---|
354 |
|
---|
355 |
|
---|
356 | /*
|
---|
357 | * SHA-384
|
---|
358 | */
|
---|
359 |
|
---|
360 | /** @impl_interface_method{RTCRDIGESTDESC::pfnUpdate} */
|
---|
361 | static DECLCALLBACK(void) rtCrDigestSha384_Update(void *pvState, const void *pvData, size_t cbData)
|
---|
362 | {
|
---|
363 | RTSha384Update((PRTSHA384CONTEXT)pvState, pvData, cbData);
|
---|
364 | }
|
---|
365 |
|
---|
366 | /** @impl_interface_method{RTCRDIGESTDESC::pfnFinal} */
|
---|
367 | static DECLCALLBACK(void) rtCrDigestSha384_Final(void *pvState, uint8_t *pbHash)
|
---|
368 | {
|
---|
369 | RTSha384Final((PRTSHA384CONTEXT)pvState, pbHash);
|
---|
370 | }
|
---|
371 |
|
---|
372 | /** @impl_interface_method{RTCRDIGESTDESC::pfnInit} */
|
---|
373 | static DECLCALLBACK(int) rtCrDigestSha384_Init(void *pvState, void *pvOpaque, bool fReInit)
|
---|
374 | {
|
---|
375 | AssertReturn(pvOpaque == NULL, VERR_INVALID_PARAMETER);
|
---|
376 | RTSha384Init((PRTSHA384CONTEXT)pvState);
|
---|
377 | return VINF_SUCCESS;
|
---|
378 | }
|
---|
379 |
|
---|
380 | /** SHA-384 alias ODIs. */
|
---|
381 | static const char * const g_apszSha384Aliases[] =
|
---|
382 | {
|
---|
383 | RTCR_PKCS1_SHA384_WITH_RSA_OID,
|
---|
384 | NULL
|
---|
385 | };
|
---|
386 |
|
---|
387 | /** SHA-384 descriptor. */
|
---|
388 | static RTCRDIGESTDESC const g_rtCrDigestSha384Desc =
|
---|
389 | {
|
---|
390 | "sha-384",
|
---|
391 | "2.16.840.1.101.3.4.2.2",
|
---|
392 | g_apszSha384Aliases,
|
---|
393 | RTDIGESTTYPE_SHA384,
|
---|
394 | RTSHA384_HASH_SIZE,
|
---|
395 | sizeof(RTSHA384CONTEXT),
|
---|
396 | 0,
|
---|
397 | rtCrDigestSha384_Update,
|
---|
398 | rtCrDigestSha384_Final,
|
---|
399 | rtCrDigestSha384_Init,
|
---|
400 | NULL,
|
---|
401 | NULL,
|
---|
402 | NULL,
|
---|
403 | NULL,
|
---|
404 | };
|
---|
405 |
|
---|
406 |
|
---|
407 | #ifndef IPRT_WITHOUT_SHA512T224
|
---|
408 | /*
|
---|
409 | * SHA-512/224
|
---|
410 | */
|
---|
411 |
|
---|
412 | /** @impl_interface_method{RTCRDIGESTDESC::pfnUpdate} */
|
---|
413 | static DECLCALLBACK(void) rtCrDigestSha512t224_Update(void *pvState, const void *pvData, size_t cbData)
|
---|
414 | {
|
---|
415 | RTSha512t224Update((PRTSHA512T224CONTEXT)pvState, pvData, cbData);
|
---|
416 | }
|
---|
417 |
|
---|
418 | /** @impl_interface_method{RTCRDIGESTDESC::pfnFinal} */
|
---|
419 | static DECLCALLBACK(void) rtCrDigestSha512t224_Final(void *pvState, uint8_t *pbHash)
|
---|
420 | {
|
---|
421 | RTSha512t224Final((PRTSHA512T224CONTEXT)pvState, pbHash);
|
---|
422 | }
|
---|
423 |
|
---|
424 | /** @impl_interface_method{RTCRDIGESTDESC::pfnInit} */
|
---|
425 | static DECLCALLBACK(int) rtCrDigestSha512t224_Init(void *pvState, void *pvOpaque, bool fReInit)
|
---|
426 | {
|
---|
427 | AssertReturn(pvOpaque == NULL, VERR_INVALID_PARAMETER);
|
---|
428 | RTSha512t224Init((PRTSHA512T224CONTEXT)pvState);
|
---|
429 | return VINF_SUCCESS;
|
---|
430 | }
|
---|
431 |
|
---|
432 | /** SHA-512/224 alias ODIs. */
|
---|
433 | static const char * const g_apszSha512t224Aliases[] =
|
---|
434 | {
|
---|
435 | NULL
|
---|
436 | };
|
---|
437 |
|
---|
438 | /** SHA-512/224 descriptor. */
|
---|
439 | static RTCRDIGESTDESC const g_rtCrDigestSha512t224Desc =
|
---|
440 | {
|
---|
441 | "sha-512/224",
|
---|
442 | "2.16.840.1.101.3.4.2.5",
|
---|
443 | g_apszSha512t224Aliases,
|
---|
444 | RTDIGESTTYPE_SHA512T224,
|
---|
445 | RTSHA512T224_HASH_SIZE,
|
---|
446 | sizeof(RTSHA512T224CONTEXT),
|
---|
447 | 0,
|
---|
448 | rtCrDigestSha512t224_Update,
|
---|
449 | rtCrDigestSha512t224_Final,
|
---|
450 | rtCrDigestSha512t224_Init,
|
---|
451 | NULL,
|
---|
452 | NULL,
|
---|
453 | NULL,
|
---|
454 | NULL,
|
---|
455 | };
|
---|
456 | #endif /* !IPRT_WITHOUT_SHA512T224 */
|
---|
457 |
|
---|
458 |
|
---|
459 | #ifndef IPRT_WITHOUT_SHA512T256
|
---|
460 | /*
|
---|
461 | * SHA-512/256
|
---|
462 | */
|
---|
463 |
|
---|
464 | /** @impl_interface_method{RTCRDIGESTDESC::pfnUpdate} */
|
---|
465 | static DECLCALLBACK(void) rtCrDigestSha512t256_Update(void *pvState, const void *pvData, size_t cbData)
|
---|
466 | {
|
---|
467 | RTSha512t256Update((PRTSHA512T256CONTEXT)pvState, pvData, cbData);
|
---|
468 | }
|
---|
469 |
|
---|
470 | /** @impl_interface_method{RTCRDIGESTDESC::pfnFinal} */
|
---|
471 | static DECLCALLBACK(void) rtCrDigestSha512t256_Final(void *pvState, uint8_t *pbHash)
|
---|
472 | {
|
---|
473 | RTSha512t256Final((PRTSHA512T256CONTEXT)pvState, pbHash);
|
---|
474 | }
|
---|
475 |
|
---|
476 | /** @impl_interface_method{RTCRDIGESTDESC::pfnInit} */
|
---|
477 | static DECLCALLBACK(int) rtCrDigestSha512t256_Init(void *pvState, void *pvOpaque, bool fReInit)
|
---|
478 | {
|
---|
479 | AssertReturn(pvOpaque == NULL, VERR_INVALID_PARAMETER);
|
---|
480 | RTSha512t256Init((PRTSHA512T256CONTEXT)pvState);
|
---|
481 | return VINF_SUCCESS;
|
---|
482 | }
|
---|
483 |
|
---|
484 | /** SHA-512/256 alias ODIs. */
|
---|
485 | static const char * const g_apszSha512t256Aliases[] =
|
---|
486 | {
|
---|
487 | NULL
|
---|
488 | };
|
---|
489 |
|
---|
490 | /** SHA-512/256 descriptor. */
|
---|
491 | static RTCRDIGESTDESC const g_rtCrDigestSha512t256Desc =
|
---|
492 | {
|
---|
493 | "sha-512/256",
|
---|
494 | "2.16.840.1.101.3.4.2.6",
|
---|
495 | g_apszSha512t256Aliases,
|
---|
496 | RTDIGESTTYPE_SHA512T256,
|
---|
497 | RTSHA512T256_HASH_SIZE,
|
---|
498 | sizeof(RTSHA512T256CONTEXT),
|
---|
499 | 0,
|
---|
500 | rtCrDigestSha512t256_Update,
|
---|
501 | rtCrDigestSha512t256_Final,
|
---|
502 | rtCrDigestSha512t256_Init,
|
---|
503 | NULL,
|
---|
504 | NULL,
|
---|
505 | NULL,
|
---|
506 | NULL,
|
---|
507 | };
|
---|
508 | #endif /* !IPRT_WITHOUT_SHA512T256 */
|
---|
509 |
|
---|
510 |
|
---|
511 | /**
|
---|
512 | * Array of built in message digest vtables.
|
---|
513 | */
|
---|
514 | static PCRTCRDIGESTDESC const g_apDigestOps[] =
|
---|
515 | {
|
---|
516 | &g_rtCrDigestMd2Desc,
|
---|
517 | &g_rtCrDigestMd5Desc,
|
---|
518 | &g_rtCrDigestSha1Desc,
|
---|
519 | &g_rtCrDigestSha256Desc,
|
---|
520 | &g_rtCrDigestSha512Desc,
|
---|
521 | &g_rtCrDigestSha224Desc,
|
---|
522 | &g_rtCrDigestSha384Desc,
|
---|
523 | #ifndef IPRT_WITHOUT_SHA512T224
|
---|
524 | &g_rtCrDigestSha512t224Desc,
|
---|
525 | #endif
|
---|
526 | #ifndef IPRT_WITHOUT_SHA512T256
|
---|
527 | &g_rtCrDigestSha512t256Desc,
|
---|
528 | #endif
|
---|
529 | };
|
---|
530 |
|
---|
531 |
|
---|
532 | #ifdef IPRT_WITH_OPENSSL
|
---|
533 | /*
|
---|
534 | * OpenSSL EVP.
|
---|
535 | */
|
---|
536 |
|
---|
537 | /** @impl_interface_method{RTCRDIGESTDESC::pfnUpdate} */
|
---|
538 | static DECLCALLBACK(void) rtCrDigestOsslEvp_Update(void *pvState, const void *pvData, size_t cbData)
|
---|
539 | {
|
---|
540 | EVP_DigestUpdate((EVP_MD_CTX *)pvState, pvData, cbData);
|
---|
541 | }
|
---|
542 |
|
---|
543 |
|
---|
544 | /** @impl_interface_method{RTCRDIGESTDESC::pfnFinal} */
|
---|
545 | static DECLCALLBACK(void) rtCrDigestOsslEvp_Final(void *pvState, uint8_t *pbHash)
|
---|
546 | {
|
---|
547 | unsigned int cbHash = EVP_MAX_MD_SIZE;
|
---|
548 | EVP_DigestFinal((EVP_MD_CTX *)pvState, (unsigned char *)pbHash, &cbHash);
|
---|
549 | }
|
---|
550 |
|
---|
551 | /** @impl_interface_method{RTCRDIGESTDESC::pfnInit} */
|
---|
552 | static DECLCALLBACK(int) rtCrDigestOsslEvp_Init(void *pvState, void *pvOpaque, bool fReInit)
|
---|
553 | {
|
---|
554 | EVP_MD_CTX *pThis = (EVP_MD_CTX *)pvState;
|
---|
555 | EVP_MD const *pEvpType = (EVP_MD const *)pvOpaque;
|
---|
556 |
|
---|
557 | if (fReInit)
|
---|
558 | {
|
---|
559 | pEvpType = EVP_MD_CTX_md(pThis);
|
---|
560 | EVP_MD_CTX_cleanup(pThis);
|
---|
561 | }
|
---|
562 |
|
---|
563 | AssertPtrReturn(pEvpType, VERR_INVALID_PARAMETER);
|
---|
564 | Assert(pEvpType->md_size);
|
---|
565 | if (EVP_DigestInit(pThis, pEvpType))
|
---|
566 | return VINF_SUCCESS;
|
---|
567 | return VERR_CR_DIGEST_OSSL_DIGEST_INIT_ERROR;
|
---|
568 | }
|
---|
569 |
|
---|
570 |
|
---|
571 | /** @impl_interface_method{RTCRDIGESTDESC::pfn} */
|
---|
572 | static DECLCALLBACK(void) rtCrDigestOsslEvp_Delete(void *pvState)
|
---|
573 | {
|
---|
574 | EVP_MD_CTX *pThis = (EVP_MD_CTX *)pvState;
|
---|
575 | EVP_MD_CTX_cleanup(pThis);
|
---|
576 | }
|
---|
577 |
|
---|
578 |
|
---|
579 | /** @impl_interface_method{RTCRDIGESTDESC::pfnClone} */
|
---|
580 | static DECLCALLBACK(int) rtCrDigestOsslEvp_Clone(void *pvState, void const *pvSrcState)
|
---|
581 | {
|
---|
582 | EVP_MD_CTX *pThis = (EVP_MD_CTX *)pvState;
|
---|
583 | EVP_MD_CTX const *pSrc = (EVP_MD_CTX const *)pvSrcState;
|
---|
584 |
|
---|
585 | if (EVP_MD_CTX_copy(pThis, pSrc))
|
---|
586 | return VINF_SUCCESS;
|
---|
587 | return VERR_CR_DIGEST_OSSL_DIGEST_CTX_COPY_ERROR;
|
---|
588 | }
|
---|
589 |
|
---|
590 |
|
---|
591 | /** @impl_interface_method{RTCRDIGESTDESC::pfnGetHashSize} */
|
---|
592 | static DECLCALLBACK(uint32_t) rtCrDigestOsslEvp_GetHashSize(void *pvState)
|
---|
593 | {
|
---|
594 | EVP_MD_CTX *pThis = (EVP_MD_CTX *)pvState;
|
---|
595 | return EVP_MD_size(EVP_MD_CTX_md(pThis));
|
---|
596 | }
|
---|
597 |
|
---|
598 |
|
---|
599 | /** @impl_interface_method{RTCRDIGESTDESC::pfnGetHashSize} */
|
---|
600 | static DECLCALLBACK(RTDIGESTTYPE) rtCrDigestOsslEvp_GetDigestType(void *pvState)
|
---|
601 | {
|
---|
602 | EVP_MD_CTX *pThis = (EVP_MD_CTX *)pvState;
|
---|
603 | /** @todo figure which digest algorithm it is! */
|
---|
604 | return RTDIGESTTYPE_UNKNOWN;
|
---|
605 | }
|
---|
606 |
|
---|
607 |
|
---|
608 | /** Descriptor for the OpenSSL EVP base message digest provider. */
|
---|
609 | static RTCRDIGESTDESC const g_rtCrDigestOpenSslDesc =
|
---|
610 | {
|
---|
611 | "OpenSSL EVP",
|
---|
612 | NULL,
|
---|
613 | NULL,
|
---|
614 | RTDIGESTTYPE_UNKNOWN,
|
---|
615 | EVP_MAX_MD_SIZE,
|
---|
616 | sizeof(EVP_MD_CTX),
|
---|
617 | 0,
|
---|
618 | rtCrDigestOsslEvp_Update,
|
---|
619 | rtCrDigestOsslEvp_Final,
|
---|
620 | rtCrDigestOsslEvp_Init,
|
---|
621 | rtCrDigestOsslEvp_Delete,
|
---|
622 | rtCrDigestOsslEvp_Clone,
|
---|
623 | rtCrDigestOsslEvp_GetHashSize,
|
---|
624 | rtCrDigestOsslEvp_GetDigestType
|
---|
625 | };
|
---|
626 |
|
---|
627 | #endif /* IPRT_WITH_OPENSSL */
|
---|
628 |
|
---|
629 |
|
---|
630 | RTDECL(PCRTCRDIGESTDESC) RTCrDigestFindByObjIdString(const char *pszObjId, void **ppvOpaque)
|
---|
631 | {
|
---|
632 | if (ppvOpaque)
|
---|
633 | *ppvOpaque = NULL;
|
---|
634 |
|
---|
635 | /*
|
---|
636 | * Primary OIDs.
|
---|
637 | */
|
---|
638 | uint32_t i = RT_ELEMENTS(g_apDigestOps);
|
---|
639 | while (i-- > 0)
|
---|
640 | if (strcmp(g_apDigestOps[i]->pszObjId, pszObjId) == 0)
|
---|
641 | return g_apDigestOps[i];
|
---|
642 |
|
---|
643 | /*
|
---|
644 | * Alias OIDs.
|
---|
645 | */
|
---|
646 | i = RT_ELEMENTS(g_apDigestOps);
|
---|
647 | while (i-- > 0)
|
---|
648 | {
|
---|
649 | const char * const *ppszAliases = g_apDigestOps[i]->papszObjIdAliases;
|
---|
650 | if (ppszAliases)
|
---|
651 | for (; *ppszAliases; ppszAliases++)
|
---|
652 | if (strcmp(*ppszAliases, pszObjId) == 0)
|
---|
653 | return g_apDigestOps[i];
|
---|
654 | }
|
---|
655 |
|
---|
656 | #ifdef IPRT_WITH_OPENSSL
|
---|
657 | /*
|
---|
658 | * Try EVP and see if it knows the algorithm.
|
---|
659 | */
|
---|
660 | if (ppvOpaque)
|
---|
661 | {
|
---|
662 | rtCrOpenSslInit();
|
---|
663 | int iAlgoNid = OBJ_txt2nid(pszObjId);
|
---|
664 | if (iAlgoNid != NID_undef)
|
---|
665 | {
|
---|
666 | const char *pszAlogSn = OBJ_nid2sn(iAlgoNid);
|
---|
667 | const EVP_MD *pEvpMdType = EVP_get_digestbyname(pszAlogSn);
|
---|
668 | if (pEvpMdType)
|
---|
669 | {
|
---|
670 | /*
|
---|
671 | * Return the OpenSSL provider descriptor and the EVP_MD address.
|
---|
672 | */
|
---|
673 | Assert(pEvpMdType->md_size);
|
---|
674 | *ppvOpaque = (void *)pEvpMdType;
|
---|
675 | return &g_rtCrDigestOpenSslDesc;
|
---|
676 | }
|
---|
677 | }
|
---|
678 | }
|
---|
679 | #endif
|
---|
680 | return NULL;
|
---|
681 | }
|
---|
682 |
|
---|
683 |
|
---|
684 | RTDECL(PCRTCRDIGESTDESC) RTCrDigestFindByObjId(PCRTASN1OBJID pObjId, void **ppvOpaque)
|
---|
685 | {
|
---|
686 | return RTCrDigestFindByObjIdString(pObjId->szObjId, ppvOpaque);
|
---|
687 | }
|
---|
688 |
|
---|
689 |
|
---|
690 | RTDECL(int) RTCrDigestCreateByObjIdString(PRTCRDIGEST phDigest, const char *pszObjId)
|
---|
691 | {
|
---|
692 | void *pvOpaque;
|
---|
693 | PCRTCRDIGESTDESC pDesc = RTCrDigestFindByObjIdString(pszObjId, &pvOpaque);
|
---|
694 | if (pDesc)
|
---|
695 | return RTCrDigestCreate(phDigest, pDesc, pvOpaque);
|
---|
696 | return VERR_NOT_FOUND;
|
---|
697 | }
|
---|
698 |
|
---|
699 |
|
---|
700 | RTDECL(int) RTCrDigestCreateByObjId(PRTCRDIGEST phDigest, PCRTASN1OBJID pObjId)
|
---|
701 | {
|
---|
702 | void *pvOpaque;
|
---|
703 | PCRTCRDIGESTDESC pDesc = RTCrDigestFindByObjId(pObjId, &pvOpaque);
|
---|
704 | if (pDesc)
|
---|
705 | return RTCrDigestCreate(phDigest, pDesc, pvOpaque);
|
---|
706 | return VERR_NOT_FOUND;
|
---|
707 | }
|
---|
708 |
|
---|
709 |
|
---|
710 | RTDECL(PCRTCRDIGESTDESC) RTCrDigestFindByType(RTDIGESTTYPE enmDigestType)
|
---|
711 | {
|
---|
712 | AssertReturn(enmDigestType > RTDIGESTTYPE_INVALID && enmDigestType <= RTDIGESTTYPE_END, NULL);
|
---|
713 |
|
---|
714 | uint32_t i = RT_ELEMENTS(g_apDigestOps);
|
---|
715 | while (i-- > 0)
|
---|
716 | if (g_apDigestOps[i]->enmType == enmDigestType)
|
---|
717 | return g_apDigestOps[i];
|
---|
718 | return NULL;
|
---|
719 | }
|
---|
720 |
|
---|
721 |
|
---|
722 | RTDECL(int) RTCrDigestCreateByType(PRTCRDIGEST phDigest, RTDIGESTTYPE enmDigestType)
|
---|
723 | {
|
---|
724 | PCRTCRDIGESTDESC pDesc = RTCrDigestFindByType(enmDigestType);
|
---|
725 | if (pDesc)
|
---|
726 | return RTCrDigestCreate(phDigest, pDesc, NULL);
|
---|
727 | return VERR_NOT_FOUND;
|
---|
728 | }
|
---|
729 |
|
---|