- 3735CBB1DEE9B07A59BBB461BA507613D83D6A67B9CDF5298613EF27074E6962C926C96A491501A315907C01C3E16CC6FD56AACF58106306D56D67D9835753C5
+ B539D3E2EF6593193D55F9009B76427557D080B98995A92F2B4EA4867CEAFB1A7C840F5EC94744F8B595F534372C9124D5AD1E3A2129B588937985FE74FA4BEE
eucrypt/smg_keccak/tests/smg_keccak-test.adb
(1 . 11)(1 . 14)
503 with Interfaces; use Interfaces;
504 with SMG_OAEP; use SMG_OAEP;
505 with SMG_Keccak; use SMG_Keccak;
506 with Ada.Exceptions; use Ada.Exceptions;
507 with Ada.Text_IO; use Ada.Text_IO;
508 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
509 with Interfaces; use Interfaces;
510 with System;
511
512 procedure SMG_Keccak.Test is
513 package Octet_IO is new Ada.Text_IO.Modular_IO(Interfaces.Unsigned_8);
514
515 --types
516 type Keccak_Perms is (None, Theta, Rho, Pi, Chi, Iota);
517 type Test_Vector is array(Keccak_Perms) of State;
(32 . 23)(35 . 14)
519 end loop;
520 end;
521
522 procedure print_bitstream(B: in Bitstream; Title: in String) is
523 Hex : array(0..15) of Character := ("0123456789ABCDEF");
524 HexString : String(1..B'Length/4);
525 C : Natural;
526 Pos : Natural;
527 begin
528 for I in 1..B'Length/4 loop
529 Pos := (I-1)*4 + B'First;
530 C := Natural( B(Pos) ) * 8 +
531 Natural( B(Pos + 1) ) * 4 +
532 Natural( B(Pos + 2) ) * 2 +
533 Natural( B(Pos + 3) );
534 HexString(I) := Hex(C);
535 end loop;
536 procedure print_bytestream(B: in Bytestream; Title: in String) is
537 begin
538 Put_Line("---" & Title & "---");
539 Put_Line(HexString);
540 end print_bitstream;
541 for Pos in B'First..B'Last loop
542 Octet_IO.Put(Item => B(Pos), Width => 2, Base => 16);
543 end loop;
544 New_Line;
545 end print_bytestream;
546
547 function read_state(File: in FILE_TYPE; Oct: Positive :=8) return State is
548 S: State;
(159 . 74)(153 . 61)
550 end loop;
551 end test_one_round;
552
553 procedure test_bits_to_word_conversion is
554 bits: Bitword := (others => 0);
555 obtained_bits: Bitword := (others => 0);
556 procedure test_bytes_to_word_conversion is
557 bytes: Byteword := (others => 0);
558 obtained_bytes: Byteword := (others => 0);
559 expected: ZWord;
560 msbexpected: ZWord;
561 obtained: ZWord;
562 begin
563 expected := 16#8FA4F19E0287BBE7#;
564 bits := (1,1,1,0, 0,1,1,1, 1,1,0,1, 1,1,0,1, 1,1,1,0, 0,0,0,1, 0,1,0,0,
565 0,0,0,0, 0,1,1,1, 1,0,0,1, 1,0,0,0, 1,1,1,1, 0,0,1,0, 0,1,0,1,
566 1,1,1,1, 0,0,0,1);
567 obtained := BitsToWord(bits);
568 obtained_bits := WordToBits(expected);
569 msbexpected := 16#F1258F7940E1DDE7#;
570 bytes := (231, 187, 135, 2, 158, 241, 164, 143); --LSByte order!
571
572 obtained := BytesToWordLE(bytes);
573 obtained_bytes := WordToBytesBE(msbexpected);
574
575 if obtained /= expected then
576 Put_Line("FAIL: bits to word");
577 Put_Line("FAIL: bytes to word");
578 Put_Line("Expected: " & ZWord'Image(expected));
579 Put_Line("Obtained: " & ZWord'Image(obtained));
580 else
581 Put_Line("PASSED: bits to word");
582 Put_Line("PASSED: bytes to word");
583 end if;
584
585 if obtained_bits /= bits then
586 Put_Line("FAIL: word to bits");
587 if obtained_bytes /= bytes then
588 Put_Line("FAIL: word to bytes");
589 Put("Expected: ");
590 for I in Bitword'Range loop
591 Put(Bit'Image(bits(I)));
592 for I in Byteword'Range loop
593 Put(Interfaces.Unsigned_8'Image(bytes(I)));
594 end loop;
595 Put_Line("");
596 Put_Line("Obtained: ");
597 for I in Bitword'Range loop
598 Put(Bit'Image(obtained_bits(I)));
599 for I in Byteword'Range loop
600 Put(Interfaces.Unsigned_8'Image(obtained_bytes(I)));
601 end loop;
602 Put_Line("");
603 else
604 Put_Line("PASSED: word to bits");
605 Put_Line("PASSED: word to bytes");
606 end if;
607 end test_bits_to_word_conversion;
608 end test_bytes_to_word_conversion;
609
610 procedure test_flip is
611 B: constant Bitword := (1, 0, 1, 1, 1, 1, 0, 0,
612 1, 1, 1, 0, 0, 0, 0, 1,
613 0, 1, 1, 0, 0, 0, 1, 0,
614 1, 1, 1, 1, 1, 1, 1, 1,
615 1, 1, 0, 1, 1, 0, 0, 1,
616 0, 0, 0, 0, 0, 0, 0, 0,
617 0, 0, 1, 1, 0, 0, 0, 1,
618 0, 0, 0, 1, 1, 0, 0, 0);
619 Expected: Bitword := (0, 0, 0, 1, 1, 0, 0, 0,
620 0, 0, 1, 1, 0, 0, 0, 1,
621 0, 0, 0, 0, 0, 0, 0, 0,
622 1, 1, 0, 1, 1, 0, 0, 1,
623 1, 1, 1, 1, 1, 1, 1, 1,
624 0, 1, 1, 0, 0, 0, 1, 0,
625 1, 1, 1, 0, 0, 0, 0, 1,
626 1, 0, 1, 1, 1, 1, 0, 0);
627 Output : Bitword;
628 B : Byteword := (231, 187, 135, 2, 158, 241, 164, 143);
629 Expected: Byteword := (143, 164, 241, 158, 2, 135, 187, 231);
630 Output : Byteword;
631 begin
632 Output := FlipOctets( B );
633 if Output /= Expected then
634 Put_Line( "FAILED: flip octets" );
635 Put_Line( "Expected: " );
636 for I of Expected loop
637 Put(Bit'Image(I));
638 Put(Interfaces.Unsigned_8'Image(I));
639 end loop;
640 new_line(1);
641 Put_Line( "Output: " );
642 for I of Output loop
643 Put(Bit'Image(I));
644 Put(Interfaces.Unsigned_8'Image(I));
645 end loop;
646 new_line(1);
647 else
(235 . 72)(216 . 97)
649 end test_flip;
650
651 procedure test_sponge is
652 Bitrate : constant Keccak_Rate := 1344;
653 Input : Bitstream(1..5) := (1, 1, 0, 0, 1);
654 Bitrate : constant Keccak_Rate := 1344 / 8;
655 Input : Bytestream(1..1);
656 Hex : array(0..15) of Character := ("0123456789ABCDEF");
657 C : Natural;
658 HexPos : Natural;
659 Error : Natural;
660 Pos : Natural;
661 ExpHex : constant String :=
662 "CB7FFB7CE7572A06C537858A0090FC2888C3C6BA9A3ADAB4"&
663 "FE7C9AB4EFE7A1E619B834C843A5A79E23F3F7E314AA597D"&
664 "9DAD376E8413A005984D00CF954F62F59EF30B050C99EA64"&
665 "E958335DAE684195D439B6E6DFD0E402518B5E7A227C48CF"&
666 "239CEA1C391241D7605733A9F4B8F3FFBE74EE45A40730ED"&
667 "1E2FDEFCCA941F518708CBB5B6D5A69C30263267B97D7B29"&
668 "AC87043880AE43033B1017EFB75C33248E2962892CE69DA8"&
669 "BAF1DF4C0902B16C64A1ADD42FF458C94C4D3B0B32711BBA"&
670 "22104989982543D1EF1661AFAF2573687D588C81113ED7FA"&
671 "F7DDF912021FC03D0E98ACC0200A9F7A0E9629DBA33BA0A3"&
672 "C03CCA5A7D3560A6DB589422AC64882EF14A62AD9807B353"&
673 "8DEE1548194DBD456F92B568CE76827F41E0FB3C7F25F3A4"&
674 "C707AD825B289730FEBDFD22A3E742C6FB7125DE0E38B130"&
675 "F3059450CA6185156A7EEE2AB7C8E4709956DC6D5E9F99D5"&
676 "0A19473EA7D737AC934815D68C0710235483DB8551FD8756"&
677 "45692B4E5E16BB9B1142AE300F5F69F43F0091D534F372E1"&
678 "FFC2E522E71003E4D27EF6ACCD36B2756FB5FF02DBF0C96B"&
679 "CAE68E7D6427810582F87051590F6FB65D7B948A9C9D6C93"&
680 "AF4562367A0AD79109D6F3087C775FE6D60D66B74F8D29FB"&
681 "4BA80D0168693A748812EA0CD3CA23854CC84D4E716F4C1A"&
682 "A3B340B1DED2F304DFDBACC1D792C8AC9A1426913E3F67DB"&
683 "790FD5CFB77DAA29";
684 Output : Bitstream( 1 .. ExpHex'Length * 4 );
685 HexString : String( 1 .. ExpHex'Length );
686 begin
687 ExpHex : constant String :=
688 --for 129 aka 1000 0001
689 -- "4BAB86C1E1C067A913F62EFD0D65AF58A9268A80A1D8A606F8"&
690 -- "72F535CDE3F1D66704093C78E6F1A8AFA7D9C6D1C016BA88ED"&
691 -- "3577450B2B339745ACF467E6DE357A14EC1B1E005E75156FF2"&
692 -- "B131F224CB2851F9D70D88ACEE2A7C05B9EF0724C1C8653E01"&
693 -- "A55B0A51145C73F23D6C31E0087D2C414B08939F1E67480867"&
694 -- "923B3FC7706675678A50260A62DBBFC38A144D75179DFBCA7B"&
695 -- "DA3CD87C7B8FFD0F8F12149EE6EAF0322BD48A079B3039A62C"&
696 -- "E1A8A5E9BCB03CE38C61ACF3AA9B9FBB159D7EB212054F7DBA"&
697 -- "D7ACE8EF0F70B0863E9E6E018F6AE23D74B01707ED59452B73"&
698 -- "862579B07C3B20DB9E5AA2479E98C0DBE6FD290FCE7F55BDA7"&
699 -- "8C1ED4A6C6A61E373F0A9D154AFF2DC86673658AF2494AAD09"&
700 -- "B8409C9B48D217FF354797BFED51807272B3C3AFE52F80FE4A"&
701 -- "4180ABE14296FF09A5024AED31F310F870288E91CB58569EE1"&
702 -- "FAA2D558E404D2ADFDD96480AE51CBF7FE6D19E8F0FEBC21DA"&
703 -- "88D597CA0C2998B95DDA72EF4C749F965688A1E133698E9E71"&
704 -- "15F3AEC61ADB7CCB279504FA716E7A059564A8DC5E20535A33"&
705 -- "E781C116B5B72B803E204BB25D91192756A575C1D9513282E1"&
706 -- "D8204AABE5DAA0A7774A296F2E4B9A87A87F72CD16A00639E7"&
707 -- "9EF280227975E05CED112346D3AE1DCE1E12EBA9A5673B29C7"&
708 -- "12AE3546D9CD3E09BCA2F2AAD46FFFCC2418E08604BFE30650"&
709 -- "549CF10FE6339D769D628828";
710
711 --for 19 aka 00010011
712 "18F5F5E68207EA4A2F5A992AA4415DDC5DA7EBE12D74CF9E"&
713 "0F33205D4E998E09883471A929959B146733C0D1FAD57919"&
714 "C6F5B5AA64130266AB5D36666AA896FC69D14E821873138E"&
715 "1C53B92FF6106428E5E407AC9E7084DEC1E8819CFB09DD54"&
716 "0DE980C631F67BF0CE55F1D6274E5E4274C1D966551B4A6F"&
717 "DFD780710872F4A8D4DF380F530EFF443355AAC86CA3AC50"&
718 "F65C0339B107B479411B8EFB8860066A2016513E8B3B9E23"&
719 "9A5EEF664EA5B50143F9152E45890078D174A75C837633A8"&
720 "76E9842316CBDACAFACB87D48E57C6A6948CE71F4A934AB0"&
721 "E73E8107F28B3E4CE2CA9A5828F3097126D7141FCF8E6DA4"&
722 "FDA017805782BD761663B43AB607914E6FC853443A240E93"&
723 "F13C228394C036D0E51CD926D033825951279D4A658F6F81"&
724 "5406A8695685C255237CDC6CDD16EC743A42D32BBDDF50A3"&
725 "6033E3121B19049AA127138C58411C7B9E7DF4A130C5A3A1"&
726 "D7253EC804379A75BF443F54575E96DF71757FEFB8EF4634"&
727 "861B2816BAC62B54A88D520D373E7AAAB6F564DEDECC7140"&
728 "0B7113BFF9B88AA62EFEC914401FD54B394C54E4B14E7423"&
729 "99847A7D1E3EEAC173202B275F7A79E4E6721AA29A9EB974"&
730 "D30F72D666A9F7BD7C3FD0EA39239289EB8BA56E3EB4DF94"&
731 "D516ADBEE177CE1462379F8A22C82A315D7A4C7BDAE1D0A5"&
732 "4A4878C81F56011CDD53D49D2366CCF2D9BBCA0494BD72B4"&
733 "17E0C6987118B8F6";
734 Output : Bytestream( 1 .. ExpHex'Length / 2 );
735 begin
736 -- Input(Input'First) := 129; --1000 0001--
737 Input(Input'First) := 19; --00010011 MSB--
738 -- Input(Input'First) := 200; --11001000 MSB--
739 Put_Line("---sponge test---");
740 Sponge(Input, Output, Bitrate);
741 Put_Line("Input is:");
742 for I of Input loop
743 Put(Bit'Image(I));
744 Put(Interfaces.Unsigned_8'Image(I));
745 end loop;
746 new_line(1);
747
748 Put_Line("Output is:");
749 for I of Output loop
750 Put(Bit'Image(I));
751 Put(Interfaces.Unsigned_8'Image(I));
752 end loop;
753 new_line(1);
754
755 Error := 0;
756 for I in 1..Output'Length/4 loop
757 Pos := Output'First + (I-1)*4;
758 C := Natural( Output( Pos ) ) +
759 Natural( Output( Pos + 1 ) ) * 2 +
760 Natural( Output( Pos + 2 ) ) * 4 +
761 Natural( Output( Pos + 3 ) ) * 8;
762 HexPos := I + 2 * ( I mod 2 ) - 1;
763 Hexstring(HexPos) := Hex( C );
764 if Hexstring(HexPos) /= ExpHex(HexPos) then
765 Error := Error + 1;
766 -- check output
767 declare
768 PO, PE: Natural;
769 Pass: Boolean := True;
770 begin
771
772 for I in 0..Output'Length-1 loop
773 PO := Output'First + I;
774 PE := ExpHex'First + I*2;
775 if Hex(Natural(Output(PO) mod 16)) /= ExpHex(PE+1) or
776 Hex(Natural(Output(PO) / 16)) /= ExpHex(PE) then
777 Put_Line("FAIL: test_sponge");
778 Octet_IO.Put(Output(Output'First+I), Width=>2, Base=>16);
779 Put_Line(" vs exp: " & ExpHex(ExpHex'First+I*2..ExpHex'First+I*2+1));
780 Pass := False;
781 exit;
782 end if;
783 end loop;
784 if Pass then
785 Put_Line("PASS: test_sponge");
786 end if;
787 end loop;
788 Put_Line("Expected: ");
789 Put_Line(ExpHex);
790 Put_Line("Obtained: ");
791 Put_Line(Hexstring);
792 Put_Line("Errors found: " & Natural'Image(Error));
793
794 end;
795 end test_sponge;
796
797 procedure test_keccak_function(T: in Test_Round) is
(315 . 56)(321 . 56)
799 end if;
800 end test_keccak_function;
801
802 procedure test_bitstream_conversion is
803 procedure test_bytestream_conversion is
804 S: String := "Aa*/";
805 E: Bitstream( 0 .. 31 ) := (1, 0, 0, 0, 0, 0, 1, 0,
806 1, 0, 0, 0, 0, 1, 1, 0,
807 0, 1, 0, 1, 0, 1, 0, 0,
808 1, 1, 1, 1, 0, 1, 0, 0);
809 B: Bitstream( 0 .. 31 );
810 E: Bytestream( 0..3 ) := (65, 97, 42, 47);
811 B: Bytestream( 0..3 );
812 SS: String := " t ";
813 begin
814 Put_Line("---Testing string to bitstream conversion---");
815 ToBitstream( S, B );
816 Put_Line("---Testing string to bytestream conversion---");
817 ToBytestream( S, B );
818 if E /= B then
819 Put_Line("FAILED: string to bitstream conversion.");
820 Put_Line("FAILED: string to bytestream conversion.");
821 for I in 0..3 loop
822 Put_Line("Got " & Interfaces.Unsigned_8'Image(B(I)) & " vs " &
823 Interfaces.Unsigned_8'Image(E(I)));
824 end loop;
825 else
826 Put_Line("PASSED: string to bitstream conversion.");
827 Put_Line("PASSED: string to bytestream conversion.");
828 end if;
829
830 Put_Line("---Testing bitstream to string conversion---");
831 Put_Line("---Testing bytestream to string conversion---");
832 ToString( B, SS );
833 if SS /= S then
834 Put_Line("FAILED: bitstream to string conversion");
835 Put_Line("FAILED: bytestream to string conversion");
836 Put_Line("EXPECTED: " & S);
837 Put_Line("OUTPUT: " & SS);
838 else
839 Put_Line("PASSED: bitstream to string conversion");
840 Put_Line("PASSED: bytestream to string conversion");
841 end if;
842 end test_bitstream_conversion;
843 end test_bytestream_conversion;
844
845 procedure test_hash_keccak is
846 S: String := "X";
847 O: String := "abc";
848 B: Bitstream( 0 .. 23 );
849 BB: Bitstream( 1.. 8):= (0, 0, 0, 1, 1, 0, 1, 0);
850 Exp: Bitstream( 0 .. 23 ) := (1, 1, 1, 0, 0, 0, 0, 1,
851 0, 1, 1, 0, 0, 0, 1, 0,
852 1, 1, 1, 0, 0, 0, 1, 1);
853 B: Bytestream( 0 .. 2 );
854 BB: Bytestream( 1.. 3);
855 Exp: Bytestream( 1 .. 3 ) := (225, 98, 227);
856 begin
857 BB(BB'First) := 26;
858 Put_Line("----Testing hash keccak on string " & S & "----");
859 HashKeccak(S, O);
860 ToBitstream( O, B );
861 ToBytestream( O, B );
862 if B /= Exp then
863 Put_Line("FAILED: testing hash keccak on string");
864 Put_Line("Output:");
865 for I of B loop
866 Put( Bit'Image( I ) );
867 Put( Interfaces.Unsigned_8'Image( I ) );
868 end loop;
869 new_line(1);
870 Put_Line("Expected:");
871 for I of Exp loop
872 Put( Bit'Image( I ) );
873 Put( Interfaces.Unsigned_8'Image( I ) );
874 end loop;
875 else
876 Put_Line("PASSED: testing hash keccak on string");
(466 . 18)(472 . 46)
878
879 end test_oaep;
880
881 procedure test_all_bitrates is
882 procedure test_all_byterates is
883 Input : constant String := "hello, world";
884 Bin : Bitstream( 0 .. Input'Length * 8 - 1 ) := ( others => 0 );
885 Bout : Bitstream( 0 .. 100 ) := ( others => 0 );
886 Bin : Bytestream( 0 .. Input'Length - 1 ) := ( others => 0 );
887 Bout : Bytestream( 0 .. 100 ) := ( others => 0 );
888 begin
889 ToBitstream( Input, Bin );
890 ToBytestream( Input, Bin );
891 Put_Line("Testing all bitrates:");
892 for Bitrate in Keccak_Rate'Range loop
893 Sponge(Bin, Bout, Bitrate);
894 Put_Line("PASSED: keccak with bitrate " & Integer'Image(Bitrate));
895 for Byterate in Keccak_Rate'Range loop
896 Sponge(Bin, Bout, Byterate);
897 Put_Line("PASSED: keccak with byterate " & Integer'Image(Byterate));
898 end loop;
899 end test_all_byterates;
900
901 procedure test_bitreverse is
902 A: Interfaces.Unsigned_8;
903 B: Interfaces.Unsigned_8;
904 Pass: Boolean := True;
905 begin
906 for I in 0..255 loop
907 -- reverse the bits
908 A := Interfaces.Unsigned_8(I);
909 B := 0;
910 for J in 0..7 loop
911 B := B*2 + A mod 2;
912 A := A / 2;
913 end loop;
914 end test_all_bitrates;
915 -- compare with tabled value
916 if B /= Reverse_Table(I) then
917 Pass := False;
918 Put_Line("FAIL: reverse table value is wrong Table(" &
919 Integer'Image(I) & ")= " &
920 Interfaces.Unsigned_8'Image(Reverse_Table(I)) &
921 " but should be " &
922 Interfaces.Unsigned_8'Image(B));
923 end if;
924 end loop;
925 if Pass then
926 Put_Line("PASS: bitreverse table is correct.");
927 end if;
928 end test_bitreverse;
929
930 -- end of helper methods
931
(510 . 8)(544 . 8)
933 -- test also Keccak_Function as a whole --
934 test_keccak_function(T);
935
936 -- test BitsToWord and WordToBits
937 test_bits_to_word_conversion;
938 -- test BytesToWord and WordToBytes
939 test_bytes_to_word_conversion;
940
941 -- test Sponge construction
942 test_sponge;
(520 . 9)(554 . 11)
944 test_flip;
945
946 -- test bitstream conversion
947 test_bitstream_conversion;
948 test_bytestream_conversion;
949
950 -- test hash keccak (strings version)
951 Put_Line("Sytem's BIT order is " &
952 System.Bit_Order'Image(System.Default_Bit_Order));
953 test_hash_keccak;
954
955 -- test oaep encrypt + decrypt
(531 . 7)(567 . 10)
957 -- test xor on strings
958 test_xor_strings;
959
960 -- test ALL bitrates of the Keccak sponge
961 test_all_bitrates;
962 -- test ALL byterates of the Keccak sponge
963 test_all_byterates;
964
965 -- test the values in the lookup table for bit reversing
966 test_bitreverse;
967
968 end SMG_Keccak.Test;