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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183

macro_rules! copy_impl {
    (
        name: $name:ident,
        wc: $wc:ty,
        copy: $copy:ident,
        finalize_func: $ff:ident
        $(,)?
    ) => {
        impl Clone for $name {
            #[doc = concat!(
                "Copy the state of the hash (calls the `", stringify!($copy), "` function)"
            )]
            #[doc = ""]
            #[doc = "# Returns"]
            #[doc = ""]
            #[doc = concat!(
                "A distinct new instance of `", stringify!($name), "`, with the same state as the ",
                "hasher that was cloned."
            )]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = ""]
            #[doc = "assert!(hasher.update_sized(b\"hello world\").is_ok());"]
            #[doc = ""]
            #[doc = "let mut cloned = hasher.clone();"]
            #[doc = "assert_eq!("]
            #[doc = concat!("    cloned.", stringify!($ff), "().unwrap(),")]
            #[doc = concat!("    hasher.", stringify!($ff), "().unwrap(),")]
            #[doc = "    \"The two hashers should have the same output\""]
            #[doc = ");"]
            #[doc = "```"]
            fn clone(&self) -> Self {
                // Fairly opaque in docs for return type of the copy function. Here's the
                // source, and the only place in which this returns a non-zero status.

                // static int wc_Sha3Copy(wc_Sha3* src, wc_Sha3* dst)
                // {
                //     int ret = 0;
                //
                //     if (src == NULL || dst == NULL)
                //         return BAD_FUNC_ARG;
                //
                //     XMEMCPY(dst, src, sizeof(wc_Sha3));
                //     /* ... */
                //     return ret;
                // }

                // This is the same general implementation for all of the Copy implementations, see:
                // -- https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/src/sha256.c#L2527
                // -- https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/src/sha.c#L1117
                // -- https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/src/sha512.c#L1870
                // -- https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/src/md5.c#L541

                // So, the only way that this fails is if src is null or dst is null. This is
                // significant as it plays into deciding if this needs to be feature gated by
                // `panic_api`.

                // So, we know src is non-null, and we know the dst is non-null. This is due to
                // rust's memory safety guarantees. For as long as our $name instance is scope,
                // the associated memory is not prematurely freed. So, this should be infallible,
                // and thus fine to include in the API without the feature gate.

                // ---------------------- Non-const pointer considerations ---------------------
                // Another dangerous part of this API is that the pointer for src is not declared
                // as const. This could have been simply been due to a slight oversight or
                // with the goal consistency, perhaps even future proofing to ensure if they do
                // eventually mutate it (which would not make much sense in a copy impl) they do
                // not violate semantic versioning.
                //
                // I have reviewed all Copy implementations, including all possible paths (even
                // those not included with the current wolf-crypto-sys build). There is absolutely
                // no mutation of the source pointer. This is in our update checklist, whenever
                // we update wolfcrypt this assumption is audited as a pre-requisite for release.
                //
                // The update checklist is at the root of this repository under the name:
                //   wolfcrypt-update-checklist.org

                let mut inner = ::core::mem::MaybeUninit::<$wc>::uninit();

                // we assert that the result is zero, just to validate our previous claims
                // in testing (even though most measures applied, ASAN, etc, would catch this).

                unsafe {
                    // See above commentary as to why this is infallible.
                    assert_eq!(
                        $copy(
                            // See above commentary as to why this is safe.
                            ::core::ptr::addr_of!(self.inner).cast_mut(),
                            inner.as_mut_ptr()
                        ),
                        0,
                        concat!(
                            "Assumption not met, this is a bug, please report this as soon as ",
                            "possible. The `", stringify!($copy), "` function should have been ",
                            "infallible under all possible circumstances. But, it has just failed. ",
                            "This error arises from the `", stringify!($name), "::clone` ",
                            "implementation."
                        )
                    );

                    // See above commentary as to why the $copy operation is infallible, thus
                    // implying `inner` must be initialized at this point.
                    Self { inner: inner.assume_init() }
                }
            }
        }

        #[cfg(test)]
        mod copy_tests {
            use super::*;
            use std::thread;

            #[test]
            fn copied_finalize_equivalent_to_src() {
                let mut hasher = $name::new().unwrap();

                let input = b"hello world";

                assert!(hasher.try_update(input.as_slice()).is_ok());

                let mut c_hasher = hasher.clone();

                let out = hasher.$ff().unwrap();
                let c_out = c_hasher.$ff().unwrap();

                assert_eq!(out, c_out);
            }

            #[test]
            fn multiple_copies_equivalent_finalize() {
                let mut hasher = $name::new().unwrap();
                assert!(hasher.try_update(b"hello world").is_ok());

                let hashers: Vec<$name> = (0..10).map(|_| hasher.clone()).collect();
                let reference = hasher.$ff().unwrap();

                for mut h in hashers.into_iter() {
                    assert_eq!(h.$ff().unwrap(), reference);
                }
            }

            #[test]
            fn cross_thread_boundary() {
                let mut hasher = $name::new().unwrap();
                assert!(hasher.try_update(b"hello").is_ok());

                let cloned = hasher.clone();
                let handle = thread::spawn(move || {
                    let mut threaded_hasher = cloned;
                    assert!(threaded_hasher.try_update(b" world").is_ok());
                    threaded_hasher.$ff().unwrap()
                });

                assert!(hasher.try_update(b" world").is_ok());
                let original_result = hasher.$ff().unwrap();
                let threaded_result = handle.join().unwrap();

                assert_eq!(
                    original_result,
                    threaded_result,
                    "Hash results should be the same across threads"
                );
            }

            #[test]
            fn deep_clone_with_multiple_updates() {
                let mut original = $name::new().unwrap();
                assert!(original.try_update(b"hello").is_ok());

                let mut cloned = original.clone();

                assert!(original.try_update(b" world").is_ok());
                assert!(cloned.try_update(b" universe").is_ok());

                let original_result = original.$ff().unwrap();
                let cloned_result = cloned.$ff().unwrap();

                assert_ne!(
                    original_result, cloned_result,
                    "Cloned hasher should have independent state"
                );
            }

            #[test]
            fn partial_update_clone() {
                let mut original = $name::new().unwrap();
                assert!(original.try_update(b"hel").is_ok());

                let mut cloned = original.clone();

                assert!(original.try_update(b"lo world").is_ok());
                assert!(cloned.try_update(b"lo world").is_ok());

                let original_result = original.$ff().unwrap();
                let cloned_result = cloned.$ff().unwrap();

                assert_eq!(
                    original_result,
                    cloned_result,
                    "Partial updates should result in the same hash"
                );
            }

            #[test]
            fn clone_after_finalize() {
                let mut original = $name::new().unwrap();
                assert!(original.try_update(b"hello world").is_ok());

                let original_result = original.$ff().unwrap();

                // Clone after finalize (which resets the state)
                let mut cloned = original.clone();

                // The cloned hasher should now be in a fresh state
                assert!(cloned.try_update(b"new input").is_ok());
                let cloned_result = cloned.$ff().unwrap();

                assert_ne!(
                    original_result, cloned_result,
                    "Cloned hasher after finalize should be in a fresh state"
                );
            }

            #[test]
            fn multiple_clones_and_updates() {
                let mut original = $name::new().unwrap();
                assert!(original.try_update(b"start").is_ok());

                let mut clone1 = original.clone();
                let mut clone2 = original.clone();

                assert!(original.try_update(b" original").is_ok());
                assert!(clone1.try_update(b" clone1").is_ok());
                assert!(clone2.try_update(b" clone2").is_ok());

                let original_result = original.$ff().unwrap();
                let clone1_result = clone1.$ff().unwrap();
                let clone2_result = clone2.$ff().unwrap();

                assert_ne!(original_result, clone1_result);
                assert_ne!(original_result, clone2_result);
                assert_ne!(clone1_result, clone2_result);
            }
        }
    };
}

/// Create an API for a hashing function
macro_rules! make_api {
    (
        $(sec_warning: $($warning:literal),*,)?
        $(anecdote: $anecdote:literal,)?
        name: $name:ident,
        wc: $wc:ty,
        bs: $bs:literal,
        init: $(= $i_void:ident)? $init:ident $(, heap: $heap:expr, devid: $devId:expr)?,
        update: $(= $u_void:ident)? $update:ident,
        finalize: $(= $f_void:ident)? $finalize:ident
        $(, free: $free:ident)?
        $(, needs-reset: $nr:ident)?
        $(, copy: $copy:ident)? $(,)?
    ) => {
        #[doc = concat!("The `", stringify!($name), $($anecdote, )? "` hasher.")]
        #[doc = ""]
        $(
            #[doc = "# Security Warning"]
            #[doc = ""]
            $(
                #[doc = $warning]
            )*
            #[doc = ""]
        )?
        #[doc = "# Example"]
        #[doc = ""]
        #[doc = "```"]
        #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
        #[doc = ""]
        #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
        #[doc = ""]
        #[doc = "let input = b\"hello world\";"]
        #[doc = "assert!(hasher.try_update(input.as_slice()).is_ok());"]
        #[doc = ""]
        #[doc = "let finalized = hasher.try_finalize().unwrap();"]
        #[doc = "assert_ne!(finalized.as_slice(), input.as_slice());"]
        #[doc = concat!("assert_eq!(finalized.len(), ", stringify!($bs), ");")]
        #[doc = "```"]
        #[repr(transparent)]
        pub struct $name {
            inner: $wc
        }

        #[allow(unused_mut)] // only for md4 and other very weak hashing algos.
        impl $name {
            $(
                #[doc = "# Security Warning"]
                #[doc = ""]
                $(
                    #[doc = $warning]
                )*
                #[doc = ""]
            )?
            #[doc = concat!("Create a new `", stringify!($name), "` instance.")]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = ""]
            #[doc = concat!(
                "If the underlying initialization function fails (`", stringify!($init), "`)"
            )]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = ""]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = ""]
            #[doc = "let input = b\"hello world\";"]
            #[doc = "assert!(hasher.try_update(input.as_slice()).is_ok());"]
            #[doc = ""]
            #[doc = "let finalized = hasher.try_finalize().unwrap();"]
            #[doc = "assert_ne!(finalized.as_slice(), input.as_slice());"]
            #[doc = concat!("assert_eq!(finalized.len(), ", stringify!($bs), ");")]
            #[doc = "```"]
            pub fn new() -> Result<Self, $crate::error::Unspecified> {
                unsafe {
                    let mut res = $crate::opaque_res::Res::new();
                    let mut inner = ::core::mem::MaybeUninit::<$wc>::uninit();

                    make_api!(
                        @check res,
                        $init(inner.as_mut_ptr() $(, $heap, $devId)?)
                        $(, $i_void)?
                    );

                    res.unit_err_with(|| Self { inner: inner.assume_init() })
                }
            }

            #[doc = concat!(
                "Update the underlying `", stringify!($wc), "` instance, without performing any ",
                "safety checks."
            )]
            #[doc = ""]
            #[doc = "# Safety"]
            #[doc = ""]
            #[doc = "The length of data is casted to a 32 bit unsigned integer without checking "]
            #[doc = "for overflows. While it is incredibly unlikely that this overflow will ever"]
            #[doc = "take place, it is not impossible. Thus this function is marked unsafe."]
            #[doc = ""]
            #[doc = "# Arguments"]
            #[doc = ""]
            #[doc = "* `data` - The slice to update the underlying hasher state with."]
            #[doc = ""]
            #[doc = "# Returns"]
            #[doc = ""]
            #[doc = "This function returns the result of the operation."]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = ""]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = ""]
            #[doc = "let input = b\"hello world\";"]
            #[doc = "// SAFETY: The length of `hello world` is 11, which"]
            #[doc = "// cannot overflow even an 8 bit integer."]
            #[doc = "let res = unsafe {"]
            #[doc = "    hasher.update_unchecked(input.as_slice())"]
            #[doc = "};"]
            #[doc = "assert!(res.is_ok());"]
            #[doc = ""]
            #[doc = "let finalized = hasher.try_finalize().unwrap();"]
            #[doc = "assert_ne!(finalized.as_slice(), input.as_slice());"]
            #[doc = concat!("assert_eq!(finalized.len(), ", stringify!($bs), ");")]
            #[doc = "```"]
            #[inline]
            pub unsafe fn update_unchecked(&mut self, data: &[u8]) -> $crate::opaque_res::Res {
                let mut res = $crate::opaque_res::Res::new();

                make_api!(
                    @check res,
                    $update(
                        ::core::ptr::addr_of_mut!(self.inner),
                        data.as_ptr(),
                        data.len() as u32
                    )
                    $(, $u_void)?
                );

                res
            }

            #[doc = concat!("Update the underlying `", stringify!($wc), "` instance.")]
            #[doc = ""]
            #[doc = "# Arguments"]
            #[doc = ""]
            #[doc = "* `data` - The slice to update the underlying hasher state with."]
            #[doc = ""]
            #[doc = "# Returns"]
            #[doc = ""]
            #[doc = "This function returns the result of the operation."]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = ""]
            #[doc = "- If the length of `data` cannot be safely casted to a `u32`."]
            #[doc = concat!("- If the underlying `", stringify!($update), "` function fails.")]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = ""]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = ""]
            #[doc = "let input = b\"hello world\";"]
            #[doc = "assert!(hasher.try_update(input.as_slice()).is_ok());"]
            #[doc = ""]
            #[doc = "let finalized = hasher.try_finalize().unwrap();"]
            #[doc = "assert_ne!(finalized.as_slice(), input.as_slice());"]
            #[doc = concat!("assert_eq!(finalized.len(), ", stringify!($bs), ");")]
            #[doc = "```"]
            #[doc = ""]
            #[doc = "**Note**: if the size of the `data` is known at compile time, see "]
            #[doc = "[`update_sized`] for a slight optimization as the safety checks are done at "]
            #[doc = "compilation time."]
            #[doc = ""]
            #[doc = "[`update_sized`]: Self::update_sized"]
            #[inline]
            pub fn try_update(&mut self, data: &[u8]) -> $crate::opaque_res::Res {
                if !$crate::can_cast_u32(data.len()) {
                    return $crate::opaque_res::Res::ERR;
                }

                unsafe { self.update_unchecked(data) }
            }

            #[doc = concat!(
                "Update the underlying `", stringify!($wc), "` instance, with the safety checks ",
                "performed at compilation time."
            )]
            #[doc = ""]
            #[doc = "# Arguments"]
            #[doc = ""]
            #[doc = "* `data` - The slice to update the underlying hasher state with."]
            #[doc = ""]
            #[doc = "# Returns"]
            #[doc = ""]
            #[doc = "This function returns the result of the operation."]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = ""]
            #[doc = "- If the length of `data` cannot be safely casted to a `u32`."]
            #[doc = concat!("- If the underlying `", stringify!($update), "` function fails.")]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = ""]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = ""]
            #[doc = "let input = b\"hello world\";"]
            #[doc = "assert!(hasher.update_sized(input).is_ok());"]
            #[doc = ""]
            #[doc = "let finalized = hasher.try_finalize().unwrap();"]
            #[doc = "assert_ne!(finalized.as_slice(), input.as_slice());"]
            #[doc = concat!("assert_eq!(finalized.len(), ", stringify!($bs), ");")]
            #[doc = "```"]
            #[doc = ""]
            #[doc = "**Note**: if the size of the `data` is not known at compile time, see "]
            #[doc = "[`try_update`] for more flexibility."]
            #[doc = ""]
            #[doc = "[`try_update`]: Self::try_update"]
            #[inline]
            pub fn update_sized<const C: usize>(&mut self, data: &[u8; C]) -> $crate::opaque_res::Res {
                if !$crate::const_can_cast_u32::<{ C }>() {
                    return $crate::opaque_res::Res::ERR;
                }

                unsafe { self.update_unchecked(data) }
            }

            #[doc = concat!(
                "Update the underlying `", stringify!($wc), "`, panicking under any failure."
            )]
            #[doc = ""]
            #[doc = "# Arguments"]
            #[doc = ""]
            #[doc = "* `data` - The slice to update the underlying hasher state with."]
            #[doc = ""]
            #[doc = "# Panics"]
            #[doc = ""]
            #[doc = "- If the length of `data` cannot be safely casted to a `u32`."]
            #[doc = concat!("- If the underlying `", stringify!($update), "` function fails.")]
            #[doc = ""]
            #[doc = "If a `panic` under any failure is not acceptable for your use case, which "]
            #[doc = "generally is true, please consider using [`try_update`]."]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = ""]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = ""]
            #[doc = "let input = b\"hello world\";"]
            #[doc = "hasher.update(input.as_slice());"]
            #[doc = ""]
            #[doc = "let finalized = hasher.try_finalize().unwrap();"]
            #[doc = "assert_ne!(finalized.as_slice(), input.as_slice());"]
            #[doc = concat!("assert_eq!(finalized.len(), ", stringify!($bs), ");")]
            #[doc = "```"]
            #[doc = ""]
            #[doc = "[`try_update`]: Self::try_update"]
            #[cfg(feature = "panic-api")]
            #[track_caller]
            pub fn update(&mut self, data: &[u8]) {
                self.try_update(data).unit_err(())
                    .expect(concat!("Failed to update hash in `", stringify!($name), "`"))
            }

            #[doc = concat!(
                "Calls the `", stringify!($finalize), "` function, finalizing the hashing of data ",
                "and resetting the underlying `", stringify!($wc), "` instance's state, without ",
                "performing any safety checks."
            )]
            #[doc = ""]
            #[doc = "# Safety"]
            #[doc = ""]
            #[doc = concat!(
                "The size of the `output` argument must have a size of at least `", stringify!($bs),
                "` (the size of the digest)."
            )]
            #[doc = ""]
            #[doc = "# Arguments"]
            #[doc = ""]
            #[doc = "* `output` - The buffer to store the output digest in."]
            #[doc = ""]
            #[doc = "# Returns"]
            #[doc = ""]
            #[doc = "This function returns the result of the operation."]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = "# let input = b\"hello world\";"]
            #[doc = "# assert!(hasher.update_sized(input).is_ok());"]
            #[doc = ""]
            #[doc = "// Use the hasher ..."]
            #[doc = ""]
            #[doc = concat!("let mut output = [0u8; ", stringify!($bs), "];")]
            #[doc = "// SAFETY: The size of the output is exactly "]
            #[doc = "// the size of the digest."]
            #[doc = "let res = unsafe {"]
            #[doc = "    hasher.finalize_unchecked(output.as_mut_slice())"]
            #[doc = "};"]
            #[doc = "assert!(res.is_ok());"]
            #[doc = "```"]
            #[inline]
            pub unsafe fn finalize_unchecked(&mut self, output: &mut [u8]) -> $crate::opaque_res::Res {
                let mut res = $crate::opaque_res::Res::new();

                make_api!(
                    @check res,
                    $finalize(::core::ptr::addr_of_mut!(self.inner), output.as_mut_ptr())
                    $(, $f_void)?
                );

                // it just so happens that whenever we do not have a free api, we do not get reset
                // on finalize. So to make the api consistent we reset manually in this case.
                make_api! { @needs-reset self, $init, res $(, $nr)? $(, = $i_void)? }

                res
            }

            #[inline]
            #[must_use]
            const fn finalize_predicate(len: usize) -> bool {
                len >= $bs
            }

            #[inline]
            #[must_use]
            const fn const_finalize_predicate<const S: usize>() -> bool {
                S >= $bs
            }

            #[doc = concat!(
                "Calls the `", stringify!($finalize), "` function, finalizing the hashing of data ",
                "and resetting the underlying `", stringify!($wc), "` instance's state."
            )]
            #[doc = ""]
            #[doc = "# Arguments"]
            #[doc = ""]
            #[doc = "* `output` - The buffer to store the output digest in."]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = ""]
            #[doc = concat!(
                "- If the size of `output` is less than the digest size (`",
                stringify!($bs), "`)."
            )]
            #[doc = "- If the underlying finalize function fails."]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = "# let input = b\"hello world\";"]
            #[doc = "# assert!(hasher.update_sized(input).is_ok());"]
            #[doc = ""]
            #[doc = "// Use the hasher ..."]
            #[doc = ""]
            #[doc = concat!("let mut output = [0u8; ", stringify!($bs), "];")]
            #[doc = "let res = hasher.finalize_into(output.as_mut_slice());"]
            #[doc = "assert!(res.is_ok());"]
            #[doc = "```"]
            #[doc = ""]
            #[doc = "**Note**: if the size of the `output` slice is known at compile time, see"]
            #[doc = "[`finalize_into_sized`] for a slight optimization as the safety checks are "]
            #[doc = "done at compilation time. There is also [`finalize_into_exact`] if this size "]
            #[doc = concat!("is exactly `", stringify!($bs), "` bytes, moving all checks to the ")]
            #[doc = "type system."]
            #[doc = ""]
            #[doc = "[`finalize_into_sized`]: Self::finalize_into_sized"]
            #[doc = "[`finalize_into_exact`]: Self::finalize_into_exact"]
            #[inline]
            pub fn finalize_into(&mut self, output: &mut [u8]) -> $crate::opaque_res::Res {
                if !Self::finalize_predicate(output.len()) { return $crate::opaque_res::Res::ERR }
                unsafe { self.finalize_unchecked(output) }
            }

            #[doc = concat!(
                "Calls the `", stringify!($finalize), "` function, finalizing the hashing of data ",
                "and resetting the underlying `", stringify!($wc), "` instance's state, with the ",
                "safety checks performed at compilation time."
            )]
            #[doc = ""]
            #[doc = "# Arguments"]
            #[doc = ""]
            #[doc = "* `output` - The buffer to store the output digest in."]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = ""]
            #[doc = concat!(
                "- If the size of `output` is less than the digest size (`",
                stringify!($bs), "`)."
            )]
            #[doc = "- If the underlying finalize function fails."]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = "# let input = b\"hello world\";"]
            #[doc = "# assert!(hasher.update_sized(input).is_ok());"]
            #[doc = ""]
            #[doc = "// Use the hasher ..."]
            #[doc = ""]
            #[doc = concat!("let mut output = [0u8; ", stringify!($bs), "];")]
            #[doc = "let res = hasher.finalize_into_sized(&mut output);"]
            #[doc = "assert!(res.is_ok());"]
            #[doc = "```"]
            #[doc = ""]
            #[doc = "**Note**: If the size of the output buffer is not known at compilation time "]
            #[doc = "see [`finalize_into`] for greater flexibility. There is also "]
            #[doc = "[`finalize_into_exact`] if this size "]
            #[doc = concat!("is exactly `", stringify!($bs), "` bytes, moving all checks to the ")]
            #[doc = "type system."]
            #[doc = ""]
            #[doc = "[`finalize_into`]: Self::finalize_into"]
            #[doc = "[`finalize_into_exact`]: Self::finalize_into_exact"]
            #[inline]
            pub fn finalize_into_sized<const C: usize>(&mut self, output: &mut [u8; C]) -> $crate::opaque_res::Res {
                if !Self::const_finalize_predicate::<{ C }>() { return $crate::opaque_res::Res::ERR }
                unsafe { self.finalize_unchecked(output) }
            }

            #[doc = concat!(
                "Calls the `", stringify!($finalize), "` function, finalizing the hashing of data ",
                "and resetting the underlying `", stringify!($wc), "` instance's state, with the ",
                "safety checks moved to the type system."
            )]
            #[doc = ""]
            #[doc = "# Arguments"]
            #[doc = ""]
            #[doc = "* `output` - The buffer to store the output digest in."]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = ""]
            #[doc = "If the underlying finalize function fails."]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = "# let input = b\"hello world\";"]
            #[doc = "# assert!(hasher.update_sized(input).is_ok());"]
            #[doc = ""]
            #[doc = "// Use the hasher ..."]
            #[doc = ""]
            #[doc = concat!("let mut output = [0u8; ", stringify!($bs), "];")]
            #[doc = "let res = hasher.finalize_into_exact(&mut output);"]
            #[doc = "assert!(res.is_ok());"]
            #[doc = "```"]
            #[doc = ""]
            #[doc = "**Note**: If the size of the output buffer is not known at compilation time "]
            #[doc = "see [`finalize_into`] for greater flexibility. If the size is known at "]
            #[doc = concat!("compilation time, but not exactly `", stringify!($bs), "` bytes, ")]
            #[doc = "see [`finalize_into_sized`]."]
            #[doc = ""]
            #[doc = "[`finalize_into`]: Self::finalize_into"]
            #[doc = "[`finalize_into_sized`]: Self::finalize_into_sized"]
            #[inline]
            pub fn finalize_into_exact(&mut self, output: &mut [u8; $bs]) -> $crate::opaque_res::Res {
                unsafe { self.finalize_unchecked(output) }
            }

            #[doc = concat!(
                "Calls the `", stringify!($finalize), "` function, finalizing the hashing of ",
                "data and resetting the underlying `", stringify!($wc), "` instance's state."
            )]
            #[doc = ""]
            #[doc = "# Returns"]
            #[doc = ""]
            #[doc = "On success, this returns the output digest."]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = ""]
            #[doc = "If the underlying finalize function fails."]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = "# let input = b\"hello world\";"]
            #[doc = "# assert!(hasher.update_sized(input).is_ok());"]
            #[doc = ""]
            #[doc = "// Use the hasher ..."]
            #[doc = ""]
            #[doc = "let res = hasher.try_finalize().unwrap();"]
            #[doc = "assert_ne!(res.as_slice(), input.as_slice());"]
            #[doc = "```"]
            #[inline]
            pub fn try_finalize(&mut self) -> Result<[u8; $bs], $crate::error::Unspecified> {
                let mut buf = [0u8; $bs];
                self.finalize_into_exact(&mut buf).unit_err(buf)
            }

            #[doc = concat!(
                "Calls the `", stringify!($finalize), "` function, finalizing the hashing of ",
                "data and resetting the underlying `", stringify!($wc), "` instance's state."
            )]
            #[doc = ""]
            #[doc = "# Returns"]
            #[doc = ""]
            #[doc = "On success, this returns the output digest."]
            #[doc = ""]
            #[doc = "# Panics"]
            #[doc = ""]
            #[doc = "If the underlying finalize function fails. If panicking is not acceptable "]
            #[doc = "for your use case, which generally is true, see [`try_finalize`]."]
            #[doc = ""]
            #[doc = "# Example"]
            #[doc = ""]
            #[doc = "```"]
            #[doc = concat!("use wolf_crypto::hash::", stringify!($name), ";")]
            #[doc = concat!("let mut hasher = ", stringify!($name), "::new().unwrap();")]
            #[doc = "# let input = b\"hello world\";"]
            #[doc = "# assert!(hasher.update_sized(input).is_ok());"]
            #[doc = ""]
            #[doc = "// Use the hasher ..."]
            #[doc = ""]
            #[doc = "let res = hasher.try_finalize().unwrap();"]
            #[doc = "assert_ne!(res.as_slice(), input.as_slice());"]
            #[doc = "```"]
            #[doc = ""]
            #[doc = "[`try_finalize`]: Self::try_finalize"]
            #[cfg(feature = "panic-api")]
            #[track_caller]
            pub fn finalize(&mut self) -> [u8; $bs] {
                self.try_finalize().expect(concat!(
                    "Failed to finalize in `", stringify!($name), "`"
                ))
            }
        }

        // SAFETY:
        // All methods which mutate the underlying state require a mutable reference,
        // the only way to obtain a mutable reference across thread boundaries is via
        // synchronization or unsafe in Rust (which then would be the user's responsibility).
        unsafe impl Send for $name {}

        // SAFETY:
        // There is no providing of interior mutability, all methods which mutate the underlying
        // state require a mutable reference, thus making this safe to mark `Sync`.
        unsafe impl Sync for $name {}

        // only implement if free was provided, as some hashing functions do not have this API.
        $(
        impl Drop for $name {
            #[doc = concat!(
                "Calls the `", stringify!($free), "` function, cleaning up after itself."
            )]
            #[inline]
            fn drop(&mut self) {
                unsafe { $free(::core::ptr::addr_of_mut!(self.inner)) }
            }
        }
        )?

        $(copy_impl! {
            name: $name,
            wc: $wc,
            copy: $copy,
            finalize_func: try_finalize
        })?

        #[cfg(test)]
        mod unit_tests {
            use super::*;
            use digest::Digest;

            #[test]
            fn reset_behavior() {
                let mut hasher = $name::new().unwrap();

                let input = b"hello world";
                assert!(hasher.try_update(input.as_slice()).is_ok());

                let _ = hasher.try_finalize().unwrap();

                // hasher2 and hasher when hashing more data should have the same digest due to the
                // implicit reset behavior of all finalize functions in wolfcrypt.
                let mut hasher2 = $name::new().unwrap();

                let sec_inp = b"goodbye world";
                assert!(hasher.try_update(sec_inp.as_slice()).is_ok());
                assert!(hasher2.try_update(sec_inp.as_slice()).is_ok());

                let r_out = hasher.try_finalize().unwrap();
                let out = hasher2.try_finalize().unwrap();

                assert_eq!(r_out.as_slice(), out.as_slice());
            }

            #[test]
            fn rust_crypto_equivalence() {
                let mut wolf = $name::new().unwrap();
                let mut rc = <make_api!(@rc $name $(, $heap, $devId)?)>::new();

                let inp = b"hello world";

                assert!(wolf.try_update(inp.as_slice()).is_ok());
                rc.update(inp.as_slice());

                let rc_out = rc.finalize();
                let wolf_out = wolf.try_finalize().unwrap();

                assert_eq!(rc_out.as_slice(), wolf_out.as_slice());
            }

            #[test]
            fn rust_crypto_reset_equivalence() {
                let mut wolf = $name::new().unwrap();
                let mut rc = <make_api!(@rc $name $(, $heap, $devId)?)>::new();

                let inp = b"hello world";

                assert!(wolf.try_update(inp.as_slice()).is_ok());
                rc.update(inp.as_slice());

                let f_rc_out = rc.finalize_reset();
                let f_wolf_out = wolf.try_finalize().unwrap();

                assert_eq!(f_rc_out.as_slice(), f_wolf_out.as_slice());

                let s_inp = b"goodbye world";

                assert!(wolf.try_update(s_inp.as_slice()).is_ok());
                rc.update(s_inp.as_slice());

                let s_rc_out = rc.finalize();
                let s_wolf_out = wolf.try_finalize().unwrap();

                assert_eq!(s_rc_out.as_slice(), s_wolf_out.as_slice());
            }

            #[test]
            fn hash_finalize_hash_finalize() {
                let mut hasher = $name::new().unwrap();
                let inp = b"hello world";

                assert!(hasher.try_update(inp.as_slice()).is_ok());

                let f_out = hasher.try_finalize().unwrap();

                assert!(hasher.try_update(inp.as_slice()).is_ok());

                let s_out = hasher.try_finalize().unwrap();
                assert_eq!(s_out, f_out);
            }

            #[test]
            fn just_finalize() {
                let mut hasher = $name::new().unwrap();
                assert!(hasher.try_finalize().is_ok());
            }

            #[test]
            fn rust_crypto_just_finalize_equivalence() {
                let mut wolf = $name::new().unwrap();
                let rc = <make_api!(@rc $name $(, $heap, $devId)?)>::new();

                let w_out = wolf.try_finalize().unwrap();
                let rc_out = rc.finalize();

                assert_eq!(w_out.as_slice(), rc_out.as_slice());
            }

            #[test]
            fn hash_empty() {
                let input = [0u8; 0];
                let mut hasher = $name::new().unwrap();

                assert!(hasher.try_update(input.as_slice()).is_ok());
                assert!(hasher.try_finalize().is_ok());
            }

            #[test]
            fn rust_crypto_hash_empty_equivalence() {
                let mut wolf = $name::new().unwrap();
                let mut rc = <make_api!(@rc $name $(, $heap, $devId)?)>::new();

                let input = [0u8; 0];

                assert!(wolf.try_update(input.as_slice()).is_ok());
                rc.update(input.as_slice());

                let w_out = wolf.try_finalize().unwrap();
                let rc_out = rc.finalize();

                assert_eq!(w_out.as_slice(), rc_out.as_slice());
            }

            #[test]
            fn finalize_into_predicate() {
                let mut hasher = $name::new().unwrap();
                let mut small_out = [0u8; $bs - 4];

                assert!(hasher.finalize_into(small_out.as_mut_slice()).is_err());
            }

            #[test]
            fn finalize_into_sized_predicate() {
                let mut hasher = $name::new().unwrap();
                let mut small_out = [0u8; $bs - 4];

                assert!(hasher.finalize_into_sized(&mut small_out).is_err());
            }

            #[test]
            fn hash_large() {
                let input = [7u8; 32_768];
                let mut hasher = $name::new().unwrap();

                assert!(hasher.try_update(input.as_slice()).is_ok());
                assert!(hasher.try_finalize().is_ok());
            }

            #[test]
            fn rust_crypto_hash_large_equivalence() {
                let input = [7u8; 32_768];
                let mut wolf = $name::new().unwrap();
                let mut rc = <make_api!(@rc $name $(, $heap, $devId)?)>::new();

                assert!(wolf.try_update(input.as_slice()).is_ok());
                rc.update(input.as_slice());

                let w_out = wolf.try_finalize().unwrap();
                let rc_out = rc.finalize();

                assert_eq!(w_out.as_slice(), rc_out.as_slice());
            }

            #[test]
            fn hash_massive() {
                let input = [7u8; 131_072];
                let mut hasher = $name::new().unwrap();

                assert!(hasher.try_update(input.as_slice()).is_ok());
                assert!(hasher.try_finalize().is_ok());
            }

            #[test]
            fn rust_crypto_hash_massive_equivalence() {
                let input = [7u8; 131_072];
                let mut wolf = $name::new().unwrap();
                let mut rc = <make_api!(@rc $name $(, $heap, $devId)?)>::new();

                assert!(wolf.try_update(input.as_slice()).is_ok());
                rc.update(input.as_slice());

                let w_out = wolf.try_finalize().unwrap();
                let rc_out = rc.finalize();

                assert_eq!(w_out.as_slice(), rc_out.as_slice());
            }

            #[test]
            fn hash_one_mb() {
                // probably do not want to put this on the stack
                let input = vec![7u8; 1_048_576];

                let mut hasher = $name::new().unwrap();

                assert!(hasher.try_update(input.as_slice()).is_ok());
                assert!(hasher.try_finalize().is_ok());
            }

            #[test]
            fn rust_crypto_hash_mb_equivalence() {
                let input = vec![7u8; 1_048_576];
                let mut wolf = $name::new().unwrap();
                let mut rc = <make_api!(@rc $name $(, $heap, $devId)?)>::new();

                assert!(wolf.try_update(input.as_slice()).is_ok());
                rc.update(input.as_slice());

                let w_out = wolf.try_finalize().unwrap();
                let rc_out = rc.finalize();

                assert_eq!(w_out.as_slice(), rc_out.as_slice());
            }
        }

        #[cfg(test)]
        mod property_tests {
            use super::*;
            use digest::Digest;
            use crate::aes::test_utils::{BoundList, AnyList};
            use proptest::prelude::*;

            proptest! {
                #![proptest_config(ProptestConfig::with_cases(100_000 / $bs))]

                #[test]
                fn rust_crypto_eq_wolf_single_update(
                    input in any::<BoundList<1024>>()
                ) {
                    let mut wolf = $name::new().unwrap();
                    let mut rc = <make_api!(@rc $name $(, $heap, $devId)?)>::new();

                    prop_assert!(wolf.try_update(input.as_slice()).is_ok());
                    rc.update(input.as_slice());

                    let finalized = wolf.try_finalize().unwrap();
                    let rc_finalized = rc.finalize();

                    prop_assert_eq!(finalized.as_slice(), rc_finalized.as_slice());
                }
            }

            proptest! {
                #![proptest_config(ProptestConfig::with_cases(50_000 / $bs))]

                #[test]
                fn rust_crypto_eq_wolf_arb_updates(
                    inputs in any::<AnyList<32, BoundList<512>>>()
                ) {
                    let mut wolf = $name::new().unwrap();
                    let mut rc = <make_api!(@rc $name $(, $heap, $devId)?)>::new();

                    for input in inputs.as_slice().iter() {
                        prop_assert!(wolf.try_update(input.as_slice()).is_ok());
                        rc.update(input.as_slice());
                    }

                    let finalized = wolf.try_finalize().unwrap();
                    let rc_finalized = rc.finalize();

                    prop_assert_eq!(finalized.as_slice(), rc_finalized.as_slice());
                }

                #[test]
                fn rust_crypto_arb_reset_equivalence(
                    inputs in any::<AnyList<32, BoundList<512>>>()
                ) {
                    let mut wolf = $name::new().unwrap();
                    let mut rc = <make_api!(@rc $name $(, $heap, $devId)?)>::new();

                    for input in inputs.as_slice().iter() {
                        prop_assert!(wolf.try_update(input.as_slice()).is_ok());
                        rc.update(input.as_slice());

                        let w_out = wolf.try_finalize().unwrap();
                        let rc_out = rc.finalize_reset();

                        prop_assert_eq!(w_out.as_slice(), rc_out.as_slice());
                    }

                    let finalized = wolf.try_finalize().unwrap();
                    let rc_finalized = rc.finalize();

                    prop_assert_eq!(finalized.as_slice(), rc_finalized.as_slice());
                }
            }

            proptest! {
                #[test]
                fn arb_massive(
                    input in proptest::collection::vec(any::<u8>(), 65536..=524_288)
                ) {
                    let mut wolf = $name::new().unwrap();

                    prop_assert!(wolf.try_update(input.as_slice()).is_ok());
                    prop_assert!(wolf.try_finalize().is_ok());
                }

                #[test]
                fn rust_crypto_arb_massive_equivalence(
                    input in proptest::collection::vec(any::<u8>(), 65536..=524_288)
                ) {
                    let mut wolf = $name::new().unwrap();
                    let mut rc = <make_api!(@rc $name $(, $heap, $devId)?)>::new();

                    prop_assert!(wolf.try_update(input.as_slice()).is_ok());
                    rc.update(input.as_slice());

                    let finalized = wolf.try_finalize().unwrap();
                    let rc_finalized = rc.finalize();

                    prop_assert_eq!(finalized.as_slice(), rc_finalized.as_slice());
                }
            }
        }
    };

    (@check $res:ident, $expr:expr, void) => {
        $expr
    };
    (@check $res:ident, $expr:expr) => {
        $res.ensure_0($expr)
    };

    (@needs-reset $this:ident, $init:ident, $res:ident $(, = $void:ident)?) => {};
    (@needs-reset $this:ident, $init:ident, $res:ident, true $(, = $void:ident)?) => {
        make_api! (@check $res, $init(::core::ptr::addr_of_mut!($this.inner)) $(, $void)? )
    };

    // rust-crypto to test against, convenient that I used the same naming convention by accident.
    (@rc $name:ident, $heap:expr, $devId:expr) => {
        sha3::$name
    };
    (@rc Md5) => {
        md5::Md5
    };
    (@rc Md4) => {
        md4::Md4
    };
    (@rc RipeMd) => {
        ripemd::Ripemd160
    };
    (@rc Sha) => {
        sha1::Sha1
    };
    (@rc $name:ident) => {
        sha2::$name
    };
}