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