1use alloc::vec::Vec;
4
5use digest::{
6 CustomizedInit,
7 Digest,
8 ExtendableOutput,
9 ExtendableOutputReset,
10 Update,
11 XofReader,
12};
13use lib_q_core::{
14 Error,
15 Hash,
16 Result,
17};
18
19use crate::utils::MAX_SP800185_FIXED_OUTPUT_BYTES;
20use crate::{
21 CShake128,
22 CShake256,
23 Keccak224,
24 Keccak256,
25 Keccak384,
26 Keccak512,
27 Kmac128,
28 Kmac256,
29 Kt128,
30 Kt256,
31 ParallelHash128,
32 ParallelHash256,
33 Sha3_224,
34 Sha3_256,
35 Sha3_384,
36 Sha3_512,
37 Shake128,
38 Shake256,
39 TupleHash128,
40 TupleHash256,
41 TurboShake128,
42 TurboShake256,
43};
44
45#[derive(Debug, Clone)]
47pub struct CShake128Hash(CShake128);
48
49#[derive(Debug, Clone)]
51pub struct CShake256Hash(CShake256);
52
53#[derive(Debug, Clone)]
55pub struct Shake128Hash(Shake128);
56
57#[derive(Debug, Clone)]
59pub struct Shake256Hash(Shake256);
60
61#[derive(Debug, Clone)]
63pub struct Sha3_224Hash(Sha3_224);
64
65#[derive(Debug, Clone)]
67pub struct Sha3_256Hash(Sha3_256);
68
69#[derive(Debug, Clone)]
71pub struct Sha3_384Hash(Sha3_384);
72
73#[derive(Debug, Clone)]
75pub struct Sha3_512Hash(Sha3_512);
76
77#[derive(Debug, Clone)]
79pub struct Kt128Hash(Kt128<'static>);
80
81#[derive(Debug, Clone)]
83pub struct Kt256Hash(Kt256<'static>);
84
85#[derive(Debug, Clone)]
87pub struct Keccak224Hash(Keccak224);
88
89#[derive(Debug, Clone)]
91pub struct Keccak256Hash(Keccak256);
92
93#[derive(Debug, Clone)]
95pub struct Keccak384Hash(Keccak384);
96
97#[derive(Debug, Clone)]
99pub struct Keccak512Hash(Keccak512);
100
101#[derive(Debug, Clone)]
103pub struct Kmac128Hash(Kmac128);
104
105#[derive(Debug, Clone)]
107pub struct Kmac256Hash(Kmac256);
108
109#[derive(Debug, Clone)]
111pub struct TupleHash128Hash(TupleHash128);
112
113#[derive(Debug, Clone)]
115pub struct TupleHash256Hash(TupleHash256);
116
117#[derive(Debug, Clone)]
119pub struct ParallelHash128Hash(ParallelHash128);
120
121#[derive(Debug, Clone)]
123pub struct ParallelHash256Hash(ParallelHash256);
124
125#[derive(Debug, Clone)]
127pub struct TurboShake128Hash(TurboShake128<0x1F>);
128
129#[derive(Debug, Clone)]
131pub struct TurboShake256Hash(TurboShake256<0x1F>);
132
133impl CShake128Hash {
135 pub fn new() -> Self {
137 Self(CShake128::default())
138 }
139
140 pub fn new_customized(customization: &[u8]) -> Self {
142 Self(CShake128::new_customized(customization))
143 }
144
145 pub fn new_with_function_name(function_name: &[u8], customization: &[u8]) -> Self {
147 Self(CShake128::new_with_function_name(
148 function_name,
149 customization,
150 ))
151 }
152}
153
154impl CShake256Hash {
155 pub fn new() -> Self {
157 Self(CShake256::default())
158 }
159
160 pub fn new_customized(customization: &[u8]) -> Self {
162 Self(CShake256::new_customized(customization))
163 }
164
165 pub fn new_with_function_name(function_name: &[u8], customization: &[u8]) -> Self {
167 Self(CShake256::new_with_function_name(
168 function_name,
169 customization,
170 ))
171 }
172}
173
174impl Shake128Hash {
175 pub fn new() -> Self {
177 Self(Shake128::default())
178 }
179}
180
181impl Shake256Hash {
182 pub fn new() -> Self {
184 Self(Shake256::default())
185 }
186}
187
188impl Sha3_224Hash {
190 pub fn new() -> Self {
192 Self(Sha3_224::default())
193 }
194}
195
196impl Sha3_256Hash {
197 pub fn new() -> Self {
199 Self(Sha3_256::default())
200 }
201}
202
203impl Sha3_384Hash {
204 pub fn new() -> Self {
206 Self(Sha3_384::default())
207 }
208}
209
210impl Sha3_512Hash {
211 pub fn new() -> Self {
213 Self(Sha3_512::default())
214 }
215}
216
217impl Kt128Hash {
219 pub fn new() -> Self {
221 Self(Kt128::new(b""))
222 }
223
224 pub fn new_customized(customization: &'static [u8]) -> Self {
226 Self(Kt128::new(customization))
227 }
228}
229
230impl Kt256Hash {
231 pub fn new() -> Self {
233 Self(Kt256::new(b""))
234 }
235
236 pub fn new_customized(customization: &'static [u8]) -> Self {
238 Self(Kt256::new(customization))
239 }
240}
241
242impl Keccak224Hash {
243 pub fn new() -> Self {
245 Self(Keccak224::default())
246 }
247}
248
249impl Keccak256Hash {
250 pub fn new() -> Self {
252 Self(Keccak256::default())
253 }
254}
255
256impl Keccak384Hash {
257 pub fn new() -> Self {
259 Self(Keccak384::default())
260 }
261}
262
263impl Keccak512Hash {
264 pub fn new() -> Self {
266 Self(Keccak512::default())
267 }
268}
269
270impl TurboShake128Hash {
271 pub fn new() -> Self {
273 Self(TurboShake128::<0x1F>::default())
274 }
275}
276
277impl TurboShake256Hash {
278 pub fn new() -> Self {
280 Self(TurboShake256::<0x1F>::default())
281 }
282}
283
284impl Hash for CShake128Hash {
286 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
287 let mut hasher = self.0.clone();
288 Update::update(&mut hasher, data);
289 let mut output = [0u8; 16];
290 hasher.finalize_xof_reset_into(&mut output);
291 Ok(output.to_vec())
292 }
293
294 fn output_size(&self) -> usize {
295 16
296 }
297}
298
299impl Hash for CShake256Hash {
300 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
301 let mut hasher = self.0.clone();
302 Update::update(&mut hasher, data);
303 let mut output = [0u8; 32];
304 hasher.finalize_xof_reset_into(&mut output);
305 Ok(output.to_vec())
306 }
307
308 fn output_size(&self) -> usize {
309 32
310 }
311}
312
313impl Hash for Shake128Hash {
315 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
316 let mut hasher = self.0.clone();
317 Update::update(&mut hasher, data);
318 let mut output = [0u8; 16];
319 hasher.finalize_xof_reset_into(&mut output);
320 Ok(output.to_vec())
321 }
322
323 fn output_size(&self) -> usize {
324 16
325 }
326}
327
328impl Hash for Shake256Hash {
329 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
330 let mut hasher = self.0.clone();
331 Update::update(&mut hasher, data);
332 let mut output = [0u8; 32];
333 hasher.finalize_xof_reset_into(&mut output);
334 Ok(output.to_vec())
335 }
336
337 fn output_size(&self) -> usize {
338 32
339 }
340}
341
342impl Hash for Sha3_224Hash {
344 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
345 let mut hasher = self.0.clone();
346 Update::update(&mut hasher, data);
347 let result = hasher.finalize();
348 Ok(result.to_vec())
349 }
350
351 fn output_size(&self) -> usize {
352 28
353 }
354}
355
356impl Hash for Sha3_256Hash {
357 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
358 let mut hasher = self.0.clone();
359 Update::update(&mut hasher, data);
360 let result = hasher.finalize();
361 Ok(result.to_vec())
362 }
363
364 fn output_size(&self) -> usize {
365 32
366 }
367}
368
369impl Hash for Sha3_384Hash {
370 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
371 let mut hasher = self.0.clone();
372 Update::update(&mut hasher, data);
373 let result = hasher.finalize();
374 Ok(result.to_vec())
375 }
376
377 fn output_size(&self) -> usize {
378 48
379 }
380}
381
382impl Hash for Sha3_512Hash {
383 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
384 let mut hasher = self.0.clone();
385 Update::update(&mut hasher, data);
386 let result = hasher.finalize();
387 Ok(result.to_vec())
388 }
389
390 fn output_size(&self) -> usize {
391 64
392 }
393}
394
395impl Hash for Kt128Hash {
397 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
398 let mut hasher = self.0.clone();
399 Update::update(&mut hasher, data);
400 let mut output = [0u8; 32];
401 let mut reader = hasher.finalize_xof();
402 reader.read(&mut output);
403 Ok(output.to_vec())
404 }
405
406 fn output_size(&self) -> usize {
407 32
408 }
409}
410
411impl Hash for Kt256Hash {
412 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
413 let mut hasher = self.0.clone();
414 Update::update(&mut hasher, data);
415 let mut output = [0u8; 64];
416 let mut reader = hasher.finalize_xof();
417 reader.read(&mut output);
418 Ok(output.to_vec())
419 }
420
421 fn output_size(&self) -> usize {
422 64
423 }
424}
425
426impl Hash for Keccak224Hash {
427 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
428 let mut hasher = self.0.clone();
429 Update::update(&mut hasher, data);
430 let result = hasher.finalize();
431 Ok(result.to_vec())
432 }
433
434 fn output_size(&self) -> usize {
435 28
436 }
437}
438
439impl Hash for Keccak256Hash {
440 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
441 let mut hasher = self.0.clone();
442 Update::update(&mut hasher, data);
443 let result = hasher.finalize();
444 Ok(result.to_vec())
445 }
446
447 fn output_size(&self) -> usize {
448 32
449 }
450}
451
452impl Hash for Keccak384Hash {
453 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
454 let mut hasher = self.0.clone();
455 Update::update(&mut hasher, data);
456 let result = hasher.finalize();
457 Ok(result.to_vec())
458 }
459
460 fn output_size(&self) -> usize {
461 48
462 }
463}
464
465impl Hash for Keccak512Hash {
466 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
467 let mut hasher = self.0.clone();
468 Update::update(&mut hasher, data);
469 let result = hasher.finalize();
470 Ok(result.to_vec())
471 }
472
473 fn output_size(&self) -> usize {
474 64
475 }
476}
477
478impl Default for CShake128Hash {
480 fn default() -> Self {
481 Self::new()
482 }
483}
484
485impl Default for CShake256Hash {
486 fn default() -> Self {
487 Self::new()
488 }
489}
490
491impl Default for Shake128Hash {
492 fn default() -> Self {
493 Self::new()
494 }
495}
496
497impl Default for Shake256Hash {
498 fn default() -> Self {
499 Self::new()
500 }
501}
502
503impl Default for Sha3_224Hash {
505 fn default() -> Self {
506 Self::new()
507 }
508}
509
510impl Default for Sha3_256Hash {
511 fn default() -> Self {
512 Self::new()
513 }
514}
515
516impl Default for Sha3_384Hash {
517 fn default() -> Self {
518 Self::new()
519 }
520}
521
522impl Default for Sha3_512Hash {
523 fn default() -> Self {
524 Self::new()
525 }
526}
527
528impl Default for Kt128Hash {
530 fn default() -> Self {
531 Self::new()
532 }
533}
534
535impl Default for Kt256Hash {
536 fn default() -> Self {
537 Self::new()
538 }
539}
540
541impl Default for Keccak224Hash {
542 fn default() -> Self {
543 Self::new()
544 }
545}
546
547impl Default for Keccak256Hash {
548 fn default() -> Self {
549 Self::new()
550 }
551}
552
553impl Default for Keccak384Hash {
554 fn default() -> Self {
555 Self::new()
556 }
557}
558
559impl Default for Keccak512Hash {
560 fn default() -> Self {
561 Self::new()
562 }
563}
564
565impl Kmac128Hash {
567 pub fn new() -> Self {
569 Self(Kmac128::new(b"", b""))
570 }
571
572 pub fn new_with_key_and_custom(key: &[u8], custom: &[u8]) -> Self {
574 Self(Kmac128::new(key, custom))
575 }
576}
577
578impl Kmac256Hash {
579 pub fn new() -> Self {
581 Self(Kmac256::new(b"", b""))
582 }
583
584 pub fn new_with_key_and_custom(key: &[u8], custom: &[u8]) -> Self {
586 Self(Kmac256::new(key, custom))
587 }
588}
589
590impl TupleHash128Hash {
591 pub fn new() -> Self {
593 Self(TupleHash128::new(b""))
594 }
595
596 pub fn new_with_custom(custom: &[u8]) -> Self {
598 Self(TupleHash128::new(custom))
599 }
600}
601
602impl TupleHash256Hash {
603 pub fn new() -> Self {
605 Self(TupleHash256::new(b""))
606 }
607
608 pub fn new_with_custom(custom: &[u8]) -> Self {
610 Self(TupleHash256::new(custom))
611 }
612}
613
614impl ParallelHash128Hash {
615 pub fn new() -> Self {
617 Self(ParallelHash128::new(b"", 8192))
618 }
619
620 pub fn new_with_custom_and_block_size(custom: &[u8], block_size: usize) -> Self {
622 Self(ParallelHash128::new(custom, block_size))
623 }
624}
625
626impl ParallelHash256Hash {
627 pub fn new() -> Self {
629 Self(ParallelHash256::new(b"", 8192))
630 }
631
632 pub fn new_with_custom_and_block_size(custom: &[u8], block_size: usize) -> Self {
634 Self(ParallelHash256::new(custom, block_size))
635 }
636}
637
638impl Hash for Kmac128Hash {
640 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
641 let mut hasher = self.0.clone();
642 hasher.update(data);
643 let result = hasher
644 .finalize_with_length(16)
645 .ok_or(Error::InvalidMessageSize {
646 max: MAX_SP800185_FIXED_OUTPUT_BYTES,
647 actual: 16,
648 })?;
649 Ok(result)
650 }
651
652 fn output_size(&self) -> usize {
653 16
654 }
655}
656
657impl Hash for Kmac256Hash {
658 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
659 let mut hasher = self.0.clone();
660 hasher.update(data);
661 let result = hasher
662 .finalize_with_length(32)
663 .ok_or(Error::InvalidMessageSize {
664 max: MAX_SP800185_FIXED_OUTPUT_BYTES,
665 actual: 32,
666 })?;
667 Ok(result)
668 }
669
670 fn output_size(&self) -> usize {
671 32
672 }
673}
674
675impl Hash for TupleHash128Hash {
676 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
677 let mut hasher = self.0.clone();
678 let tuple = alloc::vec![data];
680 hasher.update_tuple(&tuple);
681 let result = hasher
682 .finalize_with_length(16)
683 .ok_or(Error::InvalidMessageSize {
684 max: MAX_SP800185_FIXED_OUTPUT_BYTES,
685 actual: 16,
686 })?;
687 Ok(result)
688 }
689
690 fn output_size(&self) -> usize {
691 16
692 }
693}
694
695impl Hash for TupleHash256Hash {
696 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
697 let mut hasher = self.0.clone();
698 let tuple = alloc::vec![data];
700 hasher.update_tuple(&tuple);
701 let result = hasher
702 .finalize_with_length(32)
703 .ok_or(Error::InvalidMessageSize {
704 max: MAX_SP800185_FIXED_OUTPUT_BYTES,
705 actual: 32,
706 })?;
707 Ok(result)
708 }
709
710 fn output_size(&self) -> usize {
711 32
712 }
713}
714
715impl Hash for ParallelHash128Hash {
716 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
717 let mut hasher = self.0.clone();
718 hasher.update(data);
719 let result = hasher
720 .finalize_with_length(16)
721 .ok_or(Error::InvalidMessageSize {
722 max: MAX_SP800185_FIXED_OUTPUT_BYTES,
723 actual: 16,
724 })?;
725 Ok(result)
726 }
727
728 fn output_size(&self) -> usize {
729 16
730 }
731}
732
733impl Hash for ParallelHash256Hash {
734 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
735 let mut hasher = self.0.clone();
736 hasher.update(data);
737 let result = hasher
738 .finalize_with_length(32)
739 .ok_or(Error::InvalidMessageSize {
740 max: MAX_SP800185_FIXED_OUTPUT_BYTES,
741 actual: 32,
742 })?;
743 Ok(result)
744 }
745
746 fn output_size(&self) -> usize {
747 32
748 }
749}
750
751impl Hash for TurboShake128Hash {
752 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
753 let mut hasher = self.0.clone();
754 Update::update(&mut hasher, data);
755 let mut output = [0u8; 16]; hasher.finalize_xof_reset_into(&mut output);
757 Ok(output.to_vec())
758 }
759
760 fn output_size(&self) -> usize {
761 16
762 }
763}
764
765impl Hash for TurboShake256Hash {
766 fn hash(&self, data: &[u8]) -> Result<Vec<u8>> {
767 let mut hasher = self.0.clone();
768 Update::update(&mut hasher, data);
769 let mut output = [0u8; 32]; hasher.finalize_xof_reset_into(&mut output);
771 Ok(output.to_vec())
772 }
773
774 fn output_size(&self) -> usize {
775 32
776 }
777}
778
779impl Default for Kmac128Hash {
781 fn default() -> Self {
782 Self::new()
783 }
784}
785
786impl Default for Kmac256Hash {
787 fn default() -> Self {
788 Self::new()
789 }
790}
791
792impl Default for TupleHash128Hash {
793 fn default() -> Self {
794 Self::new()
795 }
796}
797
798impl Default for TupleHash256Hash {
799 fn default() -> Self {
800 Self::new()
801 }
802}
803
804impl Default for ParallelHash128Hash {
805 fn default() -> Self {
806 Self::new()
807 }
808}
809
810impl Default for ParallelHash256Hash {
811 fn default() -> Self {
812 Self::new()
813 }
814}
815
816impl Default for TurboShake128Hash {
817 fn default() -> Self {
818 Self::new()
819 }
820}
821
822impl Default for TurboShake256Hash {
823 fn default() -> Self {
824 Self::new()
825 }
826}
827
828#[cfg(test)]
829mod hash_type_tests {
830 use super::*;
831 use crate::Hash;
832
833 fn assert_hash<H: Hash>(h: &H, data: &[u8]) {
834 let n = h.output_size();
835 let out = h.hash(data).expect("hash");
836 assert_eq!(out.len(), n);
837 }
838
839 #[test]
840 fn wrapper_constructors_hash_and_default() {
841 let data = b"lib-q-hash hash_types coverage";
842
843 assert_hash(&CShake128Hash::new(), data);
844 assert_hash(&CShake128Hash::new_customized(b"app"), data);
845 assert_hash(
846 &CShake128Hash::new_with_function_name(b"fn", b"custom"),
847 data,
848 );
849 assert_hash(&CShake128Hash::default(), data);
850
851 assert_hash(&CShake256Hash::new(), data);
852 assert_hash(&CShake256Hash::new_customized(b"app"), data);
853 assert_hash(
854 &CShake256Hash::new_with_function_name(b"fn", b"custom"),
855 data,
856 );
857 assert_hash(&CShake256Hash::default(), data);
858
859 assert_hash(&Shake128Hash::new(), data);
860 assert_hash(&Shake128Hash::default(), data);
861 assert_hash(&Shake256Hash::new(), data);
862 assert_hash(&Shake256Hash::default(), data);
863
864 assert_hash(&Sha3_224Hash::new(), data);
865 assert_hash(&Sha3_224Hash::default(), data);
866 assert_hash(&Sha3_256Hash::new(), data);
867 assert_hash(&Sha3_256Hash::default(), data);
868 assert_hash(&Sha3_384Hash::new(), data);
869 assert_hash(&Sha3_384Hash::default(), data);
870 assert_hash(&Sha3_512Hash::new(), data);
871 assert_hash(&Sha3_512Hash::default(), data);
872
873 assert_hash(&Kt128Hash::new(), data);
874 assert_hash(&Kt128Hash::new_customized(b"custom"), data);
875 assert_hash(&Kt128Hash::default(), data);
876 assert_hash(&Kt256Hash::new(), data);
877 assert_hash(&Kt256Hash::new_customized(b"custom"), data);
878 assert_hash(&Kt256Hash::default(), data);
879
880 assert_hash(&Keccak224Hash::new(), data);
881 assert_hash(&Keccak224Hash::default(), data);
882 assert_hash(&Keccak256Hash::new(), data);
883 assert_hash(&Keccak256Hash::default(), data);
884 assert_hash(&Keccak384Hash::new(), data);
885 assert_hash(&Keccak384Hash::default(), data);
886 assert_hash(&Keccak512Hash::new(), data);
887 assert_hash(&Keccak512Hash::default(), data);
888
889 assert_hash(&TurboShake128Hash::new(), data);
890 assert_hash(&TurboShake128Hash::default(), data);
891 assert_hash(&TurboShake256Hash::new(), data);
892 assert_hash(&TurboShake256Hash::default(), data);
893
894 assert_hash(&Kmac128Hash::new(), data);
895 assert_hash(&Kmac128Hash::new_with_key_and_custom(b"key", b"cust"), data);
896 assert_hash(&Kmac128Hash::default(), data);
897 assert_hash(&Kmac256Hash::new(), data);
898 assert_hash(&Kmac256Hash::new_with_key_and_custom(b"key", b"cust"), data);
899 assert_hash(&Kmac256Hash::default(), data);
900
901 assert_hash(&TupleHash128Hash::new(), data);
902 assert_hash(&TupleHash128Hash::new_with_custom(b"c"), data);
903 assert_hash(&TupleHash128Hash::default(), data);
904 assert_hash(&TupleHash256Hash::new(), data);
905 assert_hash(&TupleHash256Hash::new_with_custom(b"c"), data);
906 assert_hash(&TupleHash256Hash::default(), data);
907
908 assert_hash(&ParallelHash128Hash::new(), data);
909 assert_hash(
910 &ParallelHash128Hash::new_with_custom_and_block_size(b"c", 1024),
911 data,
912 );
913 assert_hash(&ParallelHash128Hash::default(), data);
914 assert_hash(&ParallelHash256Hash::new(), data);
915 assert_hash(
916 &ParallelHash256Hash::new_with_custom_and_block_size(b"c", 1024),
917 data,
918 );
919 assert_hash(&ParallelHash256Hash::default(), data);
920 }
921}