-
Notifications
You must be signed in to change notification settings - Fork 970
Expand file tree
/
Copy pathssl_crypto.c
More file actions
3531 lines (3121 loc) · 109 KB
/
ssl_crypto.c
File metadata and controls
3531 lines (3121 loc) · 109 KB
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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ssl_crypto.c
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#ifndef WOLFSSL_SSL_CRYPTO_INCLUDED
#ifndef WOLFSSL_IGNORE_FILE_WARN
#warning ssl_crypto.c does not need to be compiled separately from ssl.c
#endif
#else
/*******************************************************************************
* START OF Digest APIs
******************************************************************************/
#ifdef OPENSSL_EXTRA
#ifndef NO_MD4
/* Initialize MD4 hash operation.
*
* @param [in, out] md4 MD4 context object.
*/
void wolfSSL_MD4_Init(WOLFSSL_MD4_CTX* md4)
{
/* Ensure WOLFSSL_MD4_CTX is big enough for wolfCrypt Md4. */
WOLFSSL_ASSERT_SIZEOF_GE(md4->buffer, wc_Md4);
WOLFSSL_ENTER("MD4_Init");
/* Initialize wolfCrypt MD4 object. */
wc_InitMd4((wc_Md4*)md4);
}
/* Update MD4 hash with data.
*
* @param [in, out] md4 MD4 context object.
* @param [in] data Data to be hashed.
* @param [in] len Length of data in bytes.
*/
void wolfSSL_MD4_Update(WOLFSSL_MD4_CTX* md4, const void* data,
unsigned long len)
{
WOLFSSL_ENTER("MD4_Update");
/* Update wolfCrypt MD4 object with data. */
wc_Md4Update((wc_Md4*)md4, (const byte*)data, (word32)len);
}
/* Finalize MD4 hash and return output.
*
* @param [out] digest Hash output.
* Must be able to hold MD4_DIGEST_SIZE bytes.
* @param [in, out] md4 MD4 context object.
*/
void wolfSSL_MD4_Final(unsigned char* digest, WOLFSSL_MD4_CTX* md4)
{
WOLFSSL_ENTER("MD4_Final");
/* Finalize wolfCrypt MD4 hash into digest. */
wc_Md4Final((wc_Md4*)md4, digest);
}
#endif /* NO_MD4 */
#endif /* OPENSSL_EXTRA */
#if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
#ifndef NO_MD5
/* Initialize MD5 hash operation.
*
* @param [in, out] md5 MD5 context object.
* @return 1 on success.
* @return 0 when md5 is NULL.
*/
int wolfSSL_MD5_Init(WOLFSSL_MD5_CTX* md5)
{
/* Ensure WOLFSSL_MD5_CTX is big enough for wolfCrypt wc_Md5. */
WOLFSSL_ASSERT_SIZEOF_GE(WOLFSSL_MD5_CTX, wc_Md5);
WOLFSSL_ENTER("MD5_Init");
/* Initialize wolfCrypt MD5 object. */
return wc_InitMd5((wc_Md5*)md5) == 0;
}
/* Update MD5 hash with data.
*
* @param [in, out] md5 MD5 context object.
* @param [in] input Data to be hashed.
* @param [in] sz Length of data in bytes.
* @return 1 on success.
* @return 0 when md5 is NULL.
*/
int wolfSSL_MD5_Update(WOLFSSL_MD5_CTX* md5, const void* input,
unsigned long sz)
{
WOLFSSL_ENTER("MD5_Update");
/* Update wolfCrypt MD5 object with data. */
return wc_Md5Update((wc_Md5*)md5, (const byte*)input, (word32)sz) == 0;
}
/* Finalize MD5 hash and return output.
*
* @param [out] digest Hash output.
* Must be able to hold MD5_DIGEST_SIZE bytes.
* @param [in, out] md5 MD5 context object.
* @return 1 on success.
* @return 0 when md5 or output is NULL.
*/
int wolfSSL_MD5_Final(byte* output, WOLFSSL_MD5_CTX* md5)
{
int ret;
WOLFSSL_ENTER("MD5_Final");
/* Finalize wolfCrypt MD5 hash into output. */
ret = (wc_Md5Final((wc_Md5*)md5, output) == 0);
/* Free resources here, as OpenSSL API doesn't include MD5_Free(). */
wc_Md5Free((wc_Md5*)md5);
return ret;
}
/* Apply MD5 transformation to the data.
*
* 'data' has words reversed in this function when big endian.
*
* @param [in, out] md5 MD5 context object.
* @param [in, out] data One block of data to be hashed.
* @return 1 on success.
* @return 0 when md5 or data is NULL.
*/
int wolfSSL_MD5_Transform(WOLFSSL_MD5_CTX* md5, const unsigned char* data)
{
WOLFSSL_ENTER("MD5_Transform");
#if defined(BIG_ENDIAN_ORDER)
/* Byte reversal done outside transform. */
if ((md5 != NULL) && (data != NULL)) {
ByteReverseWords((word32*)data, (word32*)data, WC_MD5_BLOCK_SIZE);
}
#endif
/* Transform block of data with wolfCrypt MD5 object. */
return wc_Md5Transform((wc_Md5*)md5, data) == 0;
}
/* One shot MD5 hash of data.
*
* When hash is null, a static buffer of MD5_DIGEST_SIZE is used.
* When the static buffer is used this function is not thread safe.
*
* @param [in] data Data to be hashed.
* @param [in] len Length of data in bytes.
* @param [out] hash Buffer to hold digest. May be NULL.
* Must be able to hold MD5_DIGEST_SIZE bytes.
* @return Buffer holding hash on success.
* @return NULL when hashing fails.
*/
unsigned char* wolfSSL_MD5(const unsigned char* data, size_t len,
unsigned char* hash)
{
/* Buffer to use when hash is NULL. */
static unsigned char dgst[WC_MD5_DIGEST_SIZE];
WOLFSSL_ENTER("wolfSSL_MD5");
/* Ensure buffer available for digest result. */
if (hash == NULL) {
hash = dgst;
}
/* One shot MD5 hash with wolfCrypt. */
if (wc_Md5Hash(data, (word32)len, hash) != 0) {
WOLFSSL_MSG("wc_Md5Hash error");
hash = NULL;
}
return hash;
}
#endif /* !NO_MD5 */
#ifndef NO_SHA
/* Initialize SHA hash operation.
*
* @param [in, out] sha SHA context object.
* @return 1 on success.
* @return 0 when sha is NULL.
*/
int wolfSSL_SHA_Init(WOLFSSL_SHA_CTX* sha)
{
/* Ensure WOLFSSL_SHA_CTX is big enough for wolfCrypt wc_Sha. */
WOLFSSL_ASSERT_SIZEOF_GE(WOLFSSL_SHA_CTX, wc_Sha);
WOLFSSL_ENTER("SHA_Init");
/* Initialize wolfCrypt SHA object. */
return wc_InitSha((wc_Sha*)sha) == 0;
}
/* Update SHA hash with data.
*
* @param [in, out] sha SHA context object.
* @param [in] input Data to be hashed.
* @param [in] sz Length of data in bytes.
* @return 1 on success.
* @return 0 when md5 is NULL.
*/
int wolfSSL_SHA_Update(WOLFSSL_SHA_CTX* sha, const void* input,
unsigned long sz)
{
WOLFSSL_ENTER("SHA_Update");
/* Update wolfCrypt SHA object with data. */
return wc_ShaUpdate((wc_Sha*)sha, (const byte*)input, (word32)sz) == 0;
}
/* Finalize SHA hash and return output.
*
* @param [out] output Hash output.
* Must be able to hold SHA_DIGEST_SIZE bytes.
* @param [in, out] sha SHA context object.
* @return 1 on success.
* @return 0 when sha or output is NULL.
*/
int wolfSSL_SHA_Final(byte* output, WOLFSSL_SHA_CTX* sha)
{
int ret;
WOLFSSL_ENTER("SHA_Final");
/* Finalize wolfCrypt SHA hash into output. */
ret = (wc_ShaFinal((wc_Sha*)sha, output) == 0);
/* Free resources here, as OpenSSL API doesn't include SHA_Free(). */
wc_ShaFree((wc_Sha*)sha);
return ret;
}
#if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
/* Apply SHA transformation to the data.
*
* 'data' has words reversed in this function when little endian.
*
* @param [in, out] sha SHA context object.
* @param [in, out] data One block of data to be hashed.
* @return 1 on success.
* @return 0 when sha or data is NULL.
*/
int wolfSSL_SHA_Transform(WOLFSSL_SHA_CTX* sha, const unsigned char* data)
{
WOLFSSL_ENTER("SHA_Transform");
#if defined(LITTLE_ENDIAN_ORDER)
/* Byte reversal done outside transform. */
if ((sha != NULL) && (data != NULL)) {
ByteReverseWords((word32*)data, (word32*)data, WC_SHA_BLOCK_SIZE);
}
#endif
/* Transform block of data with wolfCrypt SHA object. */
return wc_ShaTransform((wc_Sha*)sha, data) == 0;
}
#endif
/* Initialize SHA-1 hash operation.
*
* @param [in, out] sha SHA context object.
* @return 1 on success.
* @return 0 when sha is NULL.
*/
int wolfSSL_SHA1_Init(WOLFSSL_SHA_CTX* sha)
{
WOLFSSL_ENTER("SHA1_Init");
return wolfSSL_SHA_Init(sha);
}
/* Update SHA-1 hash with data.
*
* @param [in, out] sha SHA context object.
* @param [in] input Data to be hashed.
* @param [in] sz Length of data in bytes.
* @return 1 on success.
* @return 0 when sha is NULL.
*/
int wolfSSL_SHA1_Update(WOLFSSL_SHA_CTX* sha, const void* input,
unsigned long sz)
{
WOLFSSL_ENTER("SHA1_Update");
return wolfSSL_SHA_Update(sha, input, sz);
}
/* Finalize SHA-1 hash and return output.
*
* @param [out] output Hash output.
* Must be able to hold SHA_DIGEST_SIZE bytes.
* @param [in, out] sha SHA context object.
* @return 1 on success.
* @return 0 when sha or output is NULL.
*/
int wolfSSL_SHA1_Final(byte* output, WOLFSSL_SHA_CTX* sha)
{
WOLFSSL_ENTER("SHA1_Final");
return wolfSSL_SHA_Final(output, sha);
}
#if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
/* Apply SHA-1 transformation to the data.
*
* 'data' has words reversed in this function when little endian.
*
* @param [in, out] sha SHA context object.
* @param [in, out] data One block of data to be hashed.
* @return 1 on success.
* @return 0 when sha or data is NULL.
*/
int wolfSSL_SHA1_Transform(WOLFSSL_SHA_CTX* sha, const unsigned char* data)
{
WOLFSSL_ENTER("SHA1_Transform");
return wolfSSL_SHA_Transform(sha, data);
}
#endif
#endif /* !NO_SHA */
#ifndef NO_SHA256
#ifdef WOLFSSL_SHA224
/* Initialize SHA-224 hash operation.
*
* @param [in, out] sha224 SHA-224 context object.
* @return 1 on success.
* @return 0 when sha224 is NULL.
*/
int wolfSSL_SHA224_Init(WOLFSSL_SHA224_CTX* sha224)
{
/* Ensure WOLFSSL_SHA224_CTX is big enough for wolfCrypt wc_Sha224. */
WOLFSSL_ASSERT_SIZEOF_GE(WOLFSSL_SHA224_CTX, wc_Sha224);
WOLFSSL_ENTER("SHA224_Init");
/* Initialize wolfCrypt SHA-224 object. */
return wc_InitSha224((wc_Sha224*)sha224) == 0;
}
/* Update SHA-224 hash with data.
*
* @param [in, out] sha224 SHA-224 context object.
* @param [in] input Data to be hashed.
* @param [in] sz Length of data in bytes.
* @return 1 on success.
* @return 0 when sha224 is NULL.
*/
int wolfSSL_SHA224_Update(WOLFSSL_SHA224_CTX* sha224, const void* input,
unsigned long sz)
{
WOLFSSL_ENTER("SHA224_Update");
/* Update wolfCrypt SHA-224 object with data. */
return wc_Sha224Update((wc_Sha224*)sha224, (const byte*)input, (word32)sz)
== 0;
}
/* Finalize SHA-224 hash and return output.
*
* @param [out] output Hash output.
* Must be able to hold SHA224_DIGEST_SIZE bytes.
* @param [in, out] sha224 SHA-224 context object.
* @return 1 on success.
* @return 0 when sha224 or output is NULL.
*/
int wolfSSL_SHA224_Final(byte* output, WOLFSSL_SHA224_CTX* sha224)
{
int ret;
WOLFSSL_ENTER("SHA224_Final");
/* Finalize wolfCrypt SHA-224 hash into output. */
ret = (wc_Sha224Final((wc_Sha224*)sha224, output) == 0);
/* Free resources here, as OpenSSL API doesn't include SHA224_Free(). */
wc_Sha224Free((wc_Sha224*)sha224);
return ret;
}
#endif /* WOLFSSL_SHA224 */
/* Initialize SHA-256 hash operation.
*
* @param [in, out] sha256 SHA-256 context object.
* @return 1 on success.
* @return 0 when sha256 is NULL.
*/
int wolfSSL_SHA256_Init(WOLFSSL_SHA256_CTX* sha256)
{
/* Ensure WOLFSSL_SHA256_CTX is big enough for wolfCrypt wc_Sha256. */
WOLFSSL_ASSERT_SIZEOF_GE(WOLFSSL_SHA256_CTX, wc_Sha256);
WOLFSSL_ENTER("SHA256_Init");
/* Initialize wolfCrypt SHA-256 object. */
return wc_InitSha256((wc_Sha256*)sha256) == 0;
}
/* Update SHA-256 hash with data.
*
* @param [in, out] sha256 SHA-256 context object.
* @param [in] input Data to be hashed.
* @param [in] sz Length of data in bytes.
* @return 1 on success.
* @return 0 when sha256 is NULL.
*/
int wolfSSL_SHA256_Update(WOLFSSL_SHA256_CTX* sha256, const void* input,
unsigned long sz)
{
WOLFSSL_ENTER("SHA256_Update");
/* Update wolfCrypt SHA-256 object with data. */
return wc_Sha256Update((wc_Sha256*)sha256, (const byte*)input, (word32)sz)
== 0;
}
/* Finalize SHA-256 hash and return output.
*
* @param [out] output Hash output.
* Must be able to hold SHA256_DIGEST_SIZE bytes.
* @param [in, out] sha256 SHA-256 context object.
* @return 1 on success.
* @return 0 when sha256 or output is NULL.
*/
int wolfSSL_SHA256_Final(byte* output, WOLFSSL_SHA256_CTX* sha256)
{
int ret;
WOLFSSL_ENTER("SHA256_Final");
/* Finalize wolfCrypt SHA-256 hash into output. */
ret = (wc_Sha256Final((wc_Sha256*)sha256, output) == 0);
/* Free resources here, as OpenSSL API doesn't include SHA256_Free(). */
wc_Sha256Free((wc_Sha256*)sha256);
return ret;
}
#if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
!defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
!defined(WOLFSSL_KCAPI_HASH) /* doesn't support direct transform */
/* Apply SHA-256 transformation to the data.
*
* 'data' has words reversed in this function when little endian.
*
* @param [in, out] sha256 SHA256 context object.
* @param [in, out] data One block of data to be hashed.
* @return 1 on success.
* @return 0 when sha256 or data is NULL.
*/
int wolfSSL_SHA256_Transform(WOLFSSL_SHA256_CTX* sha256,
const unsigned char* data)
{
WOLFSSL_ENTER("SHA256_Transform");
#if defined(LITTLE_ENDIAN_ORDER)
/* Byte reversal done outside transform. */
if ((sha256 != NULL) && (data != NULL)) {
ByteReverseWords((word32*)data, (word32*)data, WC_SHA256_BLOCK_SIZE);
}
#endif
/* Transform block of data with wolfCrypt SHA-256 object. */
return wc_Sha256Transform((wc_Sha256*)sha256, data) == 0;
}
#endif
#endif /* !NO_SHA256 */
#ifdef WOLFSSL_SHA384
/* Initialize SHA-384 hash operation.
*
* @param [in, out] sha384 SHA-384 context object.
* @return 1 on success.
* @return 0 when sha384 is NULL.
*/
int wolfSSL_SHA384_Init(WOLFSSL_SHA384_CTX* sha384)
{
/* Ensure WOLFSSL_SHA384_CTX is big enough for wolfCrypt wc_Sha384. */
WOLFSSL_ASSERT_SIZEOF_GE(WOLFSSL_SHA384_CTX, wc_Sha384);
WOLFSSL_ENTER("SHA384_Init");
/* Initialize wolfCrypt SHA-384 object. */
return wc_InitSha384((wc_Sha384*)sha384) == 0;
}
/* Update SHA-384 hash with data.
*
* @param [in, out] sha384 SHA-384 context object.
* @param [in] input Data to be hashed.
* @param [in] sz Length of data in bytes.
* @return 1 on success.
* @return 0 when sha384 is NULL.
*/
int wolfSSL_SHA384_Update(WOLFSSL_SHA384_CTX* sha384, const void* input,
unsigned long sz)
{
WOLFSSL_ENTER("SHA384_Update");
/* Update wolfCrypt SHA-384 object with data. */
return wc_Sha384Update((wc_Sha384*)sha384, (const byte*)input, (word32)sz)
== 0;
}
/* Finalize SHA-384 hash and return output.
*
* @param [out] output Hash output.
* Must be able to hold SHA384_DIGEST_SIZE bytes.
* @param [in, out] sha384 SHA-384 context object.
* @return 1 on success.
* @return 0 when sha384 or output is NULL.
*/
int wolfSSL_SHA384_Final(byte* output, WOLFSSL_SHA384_CTX* sha384)
{
int ret;
WOLFSSL_ENTER("SHA384_Final");
/* Finalize wolfCrypt SHA-384 hash into output. */
ret = (wc_Sha384Final((wc_Sha384*)sha384, output) == 0);
/* Free resources here, as OpenSSL API doesn't include SHA384_Free(). */
wc_Sha384Free((wc_Sha384*)sha384);
return ret;
}
#endif /* WOLFSSL_SHA384 */
#ifdef WOLFSSL_SHA512
/* Initialize SHA-512 hash operation.
*
* @param [in, out] sha512 SHA-512 context object.
* @return 1 on success.
* @return 0 when sha512 is NULL.
*/
int wolfSSL_SHA512_Init(WOLFSSL_SHA512_CTX* sha512)
{
/* Ensure WOLFSSL_SHA512_CTX is big enough for wolfCrypt wc_Sha512. */
WOLFSSL_ASSERT_SIZEOF_GE(WOLFSSL_SHA512_CTX, wc_Sha512);
WOLFSSL_ENTER("SHA512_Init");
/* Initialize wolfCrypt SHA-512 object. */
return wc_InitSha512((wc_Sha512*)sha512) == 0;
}
/* Update SHA-512 hash with data.
*
* @param [in, out] sha512 SHA-512 context object.
* @param [in] input Data to be hashed.
* @param [in] sz Length of data in bytes.
* @return 1 on success.
* @return 0 when sha512 is NULL.
*/
int wolfSSL_SHA512_Update(WOLFSSL_SHA512_CTX* sha512, const void* input,
unsigned long sz)
{
WOLFSSL_ENTER("SHA512_Update");
/* Update wolfCrypt SHA-512 object with data. */
return wc_Sha512Update((wc_Sha512*)sha512, (const byte*)input, (word32)sz)
== 0;
}
/* Finalize SHA-512 hash and return output.
*
* @param [out] output Hash output.
* Must be able to hold SHA512_DIGEST_SIZE bytes.
* @param [in, out] sha512 SHA-512 context object.
* @return 1 on success.
* @return 0 when sha512 or output is NULL.
*/
int wolfSSL_SHA512_Final(byte* output, WOLFSSL_SHA512_CTX* sha512)
{
int ret;
WOLFSSL_ENTER("SHA512_Final");
/* Finalize wolfCrypt SHA-512 hash into output. */
ret = (wc_Sha512Final((wc_Sha512*)sha512, output) == 0);
/* Free resources here, as OpenSSL API doesn't include SHA512_Free(). */
wc_Sha512Free((wc_Sha512*)sha512);
return ret;
}
#if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
!defined(WOLFSSL_KCAPI_HASH) /* doesn't support direct transform */
/* Apply SHA-512 transformation to the data.
*
* @param [in, out] sha512 SHA512 context object.
* @param [in] data One block of data to be hashed.
* @return 1 on success.
* @return 0 when sha512 or data is NULL.
*/
int wolfSSL_SHA512_Transform(WOLFSSL_SHA512_CTX* sha512,
const unsigned char* data)
{
WOLFSSL_ENTER("SHA512_Transform");
/* Transform block of data with wolfCrypt SHA-512 object. */
return wc_Sha512Transform((wc_Sha512*)sha512, data) == 0;
}
#endif /* !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
(HAVE_FIPS_VERSION > 2)) && !WOLFSSL_KCAPI_HASH */
#if !defined(WOLFSSL_NOSHA512_224) && \
(!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
/* Initialize SHA-512-224 hash operation.
*
* @param [in, out] sha512 SHA-512-224 context object.
* @return 1 on success.
* @return 0 when sha512 is NULL.
*/
int wolfSSL_SHA512_224_Init(WOLFSSL_SHA512_224_CTX* sha512)
{
WOLFSSL_ENTER("SHA512_224_Init");
/* Initialize wolfCrypt SHA-512-224 object. */
return wc_InitSha512_224((wc_Sha512*)sha512) == 0;
}
/* Update SHA-512-224 hash with data.
*
* @param [in, out] sha512 SHA-512-224 context object.
* @param [in] input Data to be hashed.
* @param [in] sz Length of data in bytes.
* @return 1 on success.
* @return 0 when sha512 is NULL.
*/
int wolfSSL_SHA512_224_Update(WOLFSSL_SHA512_224_CTX* sha512, const void* input,
unsigned long sz)
{
WOLFSSL_ENTER("SHA512_224_Update");
/* Update wolfCrypt SHA-512-224 object with data. */
return wc_Sha512_224Update((wc_Sha512*)sha512, (const byte*)input,
(word32)sz) == 0;
}
/* Finalize SHA-512-224 hash and return output.
*
* @param [out] output Hash output.
* Must be able to hold SHA224_DIGEST_SIZE bytes.
* @param [in, out] sha512 SHA-512-224 context object.
* @return 1 on success.
* @return 0 when sha512 or output is NULL.
*/
int wolfSSL_SHA512_224_Final(byte* output, WOLFSSL_SHA512_224_CTX* sha512)
{
int ret;
WOLFSSL_ENTER("SHA512_224_Final");
/* Finalize wolfCrypt SHA-512-224 hash into output. */
ret = (wc_Sha512_224Final((wc_Sha512*)sha512, output) == 0);
/* Free resources here, as OpenSSL API doesn't include SHA512_224_Free(). */
wc_Sha512_224Free((wc_Sha512*)sha512);
return ret;
}
#if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
/* Apply SHA-512-224 transformation to the data.
*
* @param [in, out] sha512 SHA512 context object.
* @param [in] data One block of data to be hashed.
* @return 1 on success.
* @return 0 when sha512 or data is NULL.
*/
int wolfSSL_SHA512_224_Transform(WOLFSSL_SHA512_CTX* sha512,
const unsigned char* data)
{
WOLFSSL_ENTER("SHA512_224_Transform");
/* Transform block of data with wolfCrypt SHA-512-224 object. */
return wc_Sha512_224Transform((wc_Sha512*)sha512, data) == 0;
}
#endif /* !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
(HAVE_FIPS_VERSION > 2)) */
#endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
#if !defined(WOLFSSL_NOSHA512_256) && \
(!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
/* Initialize SHA-512-256 hash operation.
*
* @param [in, out] sha512 SHA-512-256 context object.
* @return 1 on success.
* @return 0 when sha512 is NULL.
*/
int wolfSSL_SHA512_256_Init(WOLFSSL_SHA512_256_CTX* sha)
{
WOLFSSL_ENTER("SHA512_256_Init");
/* Initialize wolfCrypt SHA-512-256 object. */
return wc_InitSha512_256((wc_Sha512*)sha) == 0;
}
/* Update SHA-512-256 hash with data.
*
* @param [in, out] sha512 SHA-512-256 context object.
* @param [in] input Data to be hashed.
* @param [in] sz Length of data in bytes.
* @return 1 on success.
* @return 0 when sha512 is NULL.
*/
int wolfSSL_SHA512_256_Update(WOLFSSL_SHA512_256_CTX* sha512, const void* input,
unsigned long sz)
{
WOLFSSL_ENTER("SHA512_256_Update");
/* Update wolfCrypt SHA-512-256 object with data. */
return wc_Sha512_256Update((wc_Sha512*)sha512, (const byte*)input,
(word32)sz) == 0;
}
/* Finalize SHA-512-256 hash and return output.
*
* @param [out] output Hash output.
* Must be able to hold SHA256_DIGEST_SIZE bytes.
* @param [in, out] sha512 SHA-512-256 context object.
* @return 1 on success.
* @return 0 when sha512 or output is NULL.
*/
int wolfSSL_SHA512_256_Final(byte* output, WOLFSSL_SHA512_256_CTX* sha512)
{
int ret;
WOLFSSL_ENTER("SHA512_256_Final");
/* Finalize wolfCrypt SHA-512-256 hash into output. */
ret = (wc_Sha512_256Final((wc_Sha512*)sha512, output) == 0);
/* Free resources here, as OpenSSL API doesn't include SHA512_256_Free(). */
wc_Sha512_224Free((wc_Sha512*)sha512);
return ret;
}
#if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
/* Apply SHA-512-256 transformation to the data.
*
* @param [in, out] sha512 SHA512 context object.
* @param [in] data One block of data to be hashed.
* @return 1 on success.
* @return 0 when sha512 or data is NULL.
*/
int wolfSSL_SHA512_256_Transform(WOLFSSL_SHA512_CTX* sha512,
const unsigned char* data)
{
WOLFSSL_ENTER("SHA512_256_Transform");
/* Transform block of data with wolfCrypt SHA-512-256 object. */
return wc_Sha512_256Transform((wc_Sha512*)sha512, data) == 0;
}
#endif /* !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
(HAVE_FIPS_VERSION > 2)) */
#endif /* !WOLFSSL_NOSHA512_256 && !FIPS ... */
#endif /* WOLFSSL_SHA512 */
#ifdef WOLFSSL_SHA3
#ifndef WOLFSSL_NOSHA3_224
/* Initialize SHA3-224 hash operation.
*
* @param [in, out] sha3_224 SHA3-224 context object.
* @return 1 on success.
* @return 0 when sha3_224 is NULL.
*/
int wolfSSL_SHA3_224_Init(WOLFSSL_SHA3_224_CTX* sha3_224)
{
/* Ensure WOLFSSL_SHA3_224_CTX is big enough for wolfCrypt wc_Sha3. */
WOLFSSL_ASSERT_SIZEOF_GE(WOLFSSL_SHA3_224_CTX, wc_Sha3);
WOLFSSL_ENTER("SHA3_224_Init");
/* Initialize wolfCrypt SHA3-224 object. */
return wc_InitSha3_224((wc_Sha3*)sha3_224, NULL, INVALID_DEVID) == 0;
}
/* Update SHA3-224 hash with data.
*
* @param [in, out] sha3 SHA3-224 context object.
* @param [in] input Data to be hashed.
* @param [in] sz Length of data in bytes.
* @return 1 on success.
* @return 0 when sha3 is NULL.
*/
int wolfSSL_SHA3_224_Update(WOLFSSL_SHA3_224_CTX* sha3, const void* input,
unsigned long sz)
{
WOLFSSL_ENTER("SHA3_224_Update");
/* Update wolfCrypt SHA3-224 object with data. */
return wc_Sha3_224_Update((wc_Sha3*)sha3, (const byte*)input, (word32)sz)
== 0;
}
/* Finalize SHA3-224 hash and return output.
*
* @param [out] output Hash output.
* Must be able to hold SHA3_224_DIGEST_SIZE bytes.
* @param [in, out] sha3 SHA3-224 context object.
* @return 1 on success.
* @return 0 when sha3 or output is NULL.
*/
int wolfSSL_SHA3_224_Final(byte* output, WOLFSSL_SHA3_224_CTX* sha3)
{
int ret;
WOLFSSL_ENTER("SHA3_224_Final");
/* Finalize wolfCrypt SHA3-224 hash into output. */
ret = (wc_Sha3_224_Final((wc_Sha3*)sha3, output) == 0);
/* Free resources here, as OpenSSL API doesn't include SHA3_224_Free(). */
wc_Sha3_224_Free((wc_Sha3*)sha3);
return ret;
}
#endif /* WOLFSSL_NOSHA3_224 */
#ifndef WOLFSSL_NOSHA3_256
/* Initialize SHA3-256 hash operation.
*
* @param [in, out] sha3_256 SHA3-256 context object.
* @return 1 on success.
* @return 0 when sha3_256 is NULL.
*/
int wolfSSL_SHA3_256_Init(WOLFSSL_SHA3_256_CTX* sha3_256)
{
/* Ensure WOLFSSL_SHA3_256_CTX is big enough for wolfCrypt wc_Sha3. */
WOLFSSL_ASSERT_SIZEOF_GE(WOLFSSL_SHA3_256_CTX, wc_Sha3);
WOLFSSL_ENTER("SHA3_256_Init");
/* Initialize wolfCrypt SHA3-256 object. */
return wc_InitSha3_256((wc_Sha3*)sha3_256, NULL, INVALID_DEVID) == 0;
}
/* Update SHA3-256 hash with data.
*
* @param [in, out] sha3 SHA3-256 context object.
* @param [in] input Data to be hashed.
* @param [in] sz Length of data in bytes.
* @return 1 on success.
* @return 0 when sha3 is NULL.
*/
int wolfSSL_SHA3_256_Update(WOLFSSL_SHA3_256_CTX* sha3, const void* input,
unsigned long sz)
{
WOLFSSL_ENTER("SHA3_256_Update");
/* Update wolfCrypt SHA3-256 object with data. */
return wc_Sha3_256_Update((wc_Sha3*)sha3, (const byte*)input, (word32)sz)
== 0;
}
/* Finalize SHA3-256 hash and return output.
*
* @param [out] output Hash output.
* Must be able to hold SHA3_256_DIGEST_SIZE bytes.
* @param [in, out] sha3 SHA3-256 context object.
* @return 1 on success.
* @return 0 when sha3 or output is NULL.
*/
int wolfSSL_SHA3_256_Final(byte* output, WOLFSSL_SHA3_256_CTX* sha3)
{
int ret;
WOLFSSL_ENTER("SHA3_256_Final");
/* Finalize wolfCrypt SHA3-256 hash into output. */
ret = (wc_Sha3_256_Final((wc_Sha3*)sha3, output) == 0);
/* Free resources here, as OpenSSL API doesn't include SHA3_256_Free(). */
wc_Sha3_256_Free((wc_Sha3*)sha3);
return ret;
}
#endif /* WOLFSSL_NOSHA3_256 */
#ifndef WOLFSSL_NOSHA3_384
/* Initialize SHA3-384 hash operation.
*
* @param [in, out] sha3_384 SHA3-384 context object.
* @return 1 on success.
* @return 0 when sha3_384 is NULL.
*/
int wolfSSL_SHA3_384_Init(WOLFSSL_SHA3_384_CTX* sha3_384)
{
/* Ensure WOLFSSL_SHA3_384_CTX is big enough for wolfCrypt wc_Sha3. */
WOLFSSL_ASSERT_SIZEOF_GE(WOLFSSL_SHA3_384_CTX, wc_Sha3);
WOLFSSL_ENTER("SHA3_384_Init");
/* Initialize wolfCrypt SHA3-384 object. */
return wc_InitSha3_384((wc_Sha3*)sha3_384, NULL, INVALID_DEVID) == 0;
}
/* Update SHA3-384 hash with data.
*
* @param [in, out] sha3 SHA3-384 context object.
* @param [in] input Data to be hashed.
* @param [in] sz Length of data in bytes.
* @return 1 on success.
* @return 0 when sha3 is NULL.
*/
int wolfSSL_SHA3_384_Update(WOLFSSL_SHA3_384_CTX* sha3, const void* input,
unsigned long sz)
{
WOLFSSL_ENTER("SHA3_384_Update");
/* Update wolfCrypt SHA3-384 object with data. */
return wc_Sha3_384_Update((wc_Sha3*)sha3, (const byte*)input, (word32)sz)
== 0;
}
/* Finalize SHA3-384 hash and return output.
*
* @param [out] output Hash output.
* Must be able to hold SHA3_384_DIGEST_SIZE bytes.
* @param [in, out] sha3 SHA3-384 context object.
* @return 1 on success.
* @return 0 when sha3 or output is NULL.
*/
int wolfSSL_SHA3_384_Final(byte* output, WOLFSSL_SHA3_384_CTX* sha3)
{
int ret;
WOLFSSL_ENTER("SHA3_384_Final");
/* Finalize wolfCrypt SHA3-384 hash into output. */
ret = (wc_Sha3_384_Final((wc_Sha3*)sha3, output) == 0);
/* Free resources here, as OpenSSL API doesn't include SHA3_384_Free(). */
wc_Sha3_384_Free((wc_Sha3*)sha3);
return ret;
}
#endif /* WOLFSSL_NOSHA3_384 */
#ifndef WOLFSSL_NOSHA3_512
/* Initialize SHA3-512 hash operation.
*
* @param [in, out] sha3_512 SHA3-512 context object.
* @return 1 on success.
* @return 0 when sha3_512 is NULL.
*/
int wolfSSL_SHA3_512_Init(WOLFSSL_SHA3_512_CTX* sha3_512)
{
/* Ensure WOLFSSL_SHA3_512_CTX is big enough for wolfCrypt wc_Sha3. */
WOLFSSL_ASSERT_SIZEOF_GE(WOLFSSL_SHA3_512_CTX, wc_Sha3);
WOLFSSL_ENTER("SHA3_512_Init");
/* Initialize wolfCrypt SHA3-512 object. */
return wc_InitSha3_512((wc_Sha3*)sha3_512, NULL, INVALID_DEVID) == 0;
}
/* Update SHA3-512 hash with data.
*
* @param [in, out] sha3 SHA3-512 context object.
* @param [in] input Data to be hashed.
* @param [in] sz Length of data in bytes.
* @return 1 on success.
* @return 0 when sha3 is NULL.
*/
int wolfSSL_SHA3_512_Update(WOLFSSL_SHA3_512_CTX* sha3, const void* input,
unsigned long sz)
{
WOLFSSL_ENTER("SHA3_512_Update");
/* Update wolfCrypt SHA3-512 object with data. */
return wc_Sha3_512_Update((wc_Sha3*)sha3, (const byte*)input, (word32)sz)
== 0;