raw
eucrypt_ch8_bit_k...    1 with SMG_Bit_Keccak; use SMG_Bit_Keccak;
eucrypt_ch8_bit_k... 2 with Ada.Exceptions; use Ada.Exceptions;
eucrypt_ch8_bit_k... 3 with Ada.Text_IO; use Ada.Text_IO;
eucrypt_ch8_bit_k... 4 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
eucrypt_ch8_bit_k... 5 with Interfaces; use Interfaces;
eucrypt_ch8_bit_k... 6
eucrypt_ch8_bit_k... 7 procedure SMG_Bit_Keccak.Test is
eucrypt_ch8_bit_k... 8 --types
eucrypt_ch8_bit_k... 9 type Keccak_Perms is ( None, Theta, Rho, Pi, Chi, Iota );
eucrypt_ch8_bit_k... 10 type Test_Vector is array( Keccak_Perms ) of State;
eucrypt_ch8_bit_k... 11 type Test_Round is array( Round_Index ) of Test_Vector;
eucrypt_ch8_bit_k... 12 subtype Hexstring is String( 1 .. Z_Length / 4 ); --word as hex string
eucrypt_ch8_bit_k... 13 subtype Bitstring is String( 1 .. Z_Length ); -- word as binary string
eucrypt_ch8_bit_k... 14 type Bithex is array( 0 .. 3 ) of Bit;
eucrypt_ch8_bit_k... 15
eucrypt_ch8_bit_k... 16 -- helper methods
eucrypt_ch8_bit_k... 17 procedure HexCharToBit( H : in Character; B: out Bithex) is
eucrypt_ch8_bit_k... 18 begin
eucrypt_ch8_bit_k... 19 case H is
eucrypt_ch8_bit_k... 20 when '0' => B := (0, 0, 0, 0);
eucrypt_ch8_bit_k... 21 when '1' => B := (0, 0, 0, 1);
eucrypt_ch8_bit_k... 22 when '2' => B := (0, 0, 1, 0);
eucrypt_ch8_bit_k... 23 when '3' => B := (0, 0, 1, 1);
eucrypt_ch8_bit_k... 24 when '4' => B := (0, 1, 0, 0);
eucrypt_ch8_bit_k... 25 when '5' => B := (0, 1, 0, 1);
eucrypt_ch8_bit_k... 26 when '6' => B := (0, 1, 1, 0);
eucrypt_ch8_bit_k... 27 when '7' => B := (0, 1, 1, 1);
eucrypt_ch8_bit_k... 28 when '8' => B := (1, 0, 0, 0);
eucrypt_ch8_bit_k... 29 when '9' => B := (1, 0, 0, 1);
eucrypt_ch8_bit_k... 30 when 'A' => B := (1, 0, 1, 0);
eucrypt_ch8_bit_k... 31 when 'B' => B := (1, 0, 1, 1);
eucrypt_ch8_bit_k... 32 when 'C' => B := (1, 1, 0, 0);
eucrypt_ch8_bit_k... 33 when 'D' => B := (1, 1, 0, 1);
eucrypt_ch8_bit_k... 34 when 'E' => B := (1, 1, 1, 0);
eucrypt_ch8_bit_k... 35 when 'F' => B := (1, 1, 1, 1);
eucrypt_ch8_bit_k... 36 when others => null;
eucrypt_ch8_bit_k... 37 end case;
eucrypt_ch8_bit_k... 38 end HexCharToBit;
eucrypt_ch8_bit_k... 39
eucrypt_ch8_bit_k... 40 function HexToBitword( H: in Hexstring ) return Bitword is
eucrypt_ch8_bit_k... 41 BW : Bitword;
eucrypt_ch8_bit_k... 42 B1, B2 : Bithex;
eucrypt_ch8_bit_k... 43 PosH, PosB : Natural;
eucrypt_ch8_bit_k... 44 begin
eucrypt_ch8_bit_k... 45 -- read the hexstring octet by octet
eucrypt_ch8_bit_k... 46 for I in 1 .. Z_Length / 8 loop
eucrypt_ch8_bit_k... 47 PosH := Integer(H'First) + (I - 1) * 2;
eucrypt_ch8_bit_k... 48 HexCharToBit( H(PosH), B1 );
eucrypt_ch8_bit_k... 49 HexCharToBit( H(PosH + 1), B2 );
eucrypt_ch8_bit_k... 50
eucrypt_ch8_bit_k... 51 PosB := Integer(BW'First) + (I - 1) * 8;
eucrypt_ch8_bit_k... 52 for J in 0 .. 3 loop
eucrypt_ch8_bit_k... 53 BW ( ZCoord(PosB + J) ) := B1(J);
eucrypt_ch8_bit_k... 54 BW ( ZCoord(PosB + 4 + J) ) := B2(J);
eucrypt_ch8_bit_k... 55 end loop;
eucrypt_ch8_bit_k... 56 end loop;
eucrypt_ch8_bit_k... 57 return BW;
eucrypt_ch8_bit_k... 58 end HexToBitword;
eucrypt_ch8_bit_k... 59
eucrypt_ch8_bit_k... 60 -- prints one bitword as an array of bits
eucrypt_ch8_bit_k... 61 procedure print_bitword( B: in Bitword ) is
eucrypt_ch8_bit_k... 62 bstr: Bitstring;
eucrypt_ch8_bit_k... 63 begin
eucrypt_ch8_bit_k... 64 for I in ZCoord loop
eucrypt_ch8_bit_k... 65 if B( I ) > 0 then
eucrypt_ch8_bit_k... 66 bstr( Bitstring'First + Integer(I) ) := '1';
eucrypt_ch8_bit_k... 67 else
eucrypt_ch8_bit_k... 68 bstr( Bitstring'First + Integer(I) ) := '0';
eucrypt_ch8_bit_k... 69 end if;
eucrypt_ch8_bit_k... 70 end loop;
eucrypt_ch8_bit_k... 71 Put(bstr);
eucrypt_ch8_bit_k... 72 end print_bitword;
eucrypt_ch8_bit_k... 73
eucrypt_ch8_bit_k... 74 -- prints a keccak state, bitword by bitword
eucrypt_ch8_bit_k... 75 procedure print_state( S: in State; Title: in String) is
eucrypt_ch8_bit_k... 76 begin
eucrypt_ch8_bit_k... 77 Put_Line("---------" & Title & "---------");
eucrypt_ch8_bit_k... 78 for Y in XYCoord loop
eucrypt_ch8_bit_k... 79 for X in XYCoord loop
eucrypt_ch8_bit_k... 80 Put( "S(" & XYCoord'Image(X) & ", " & XYCoord'Image(Y) & ")= ");
eucrypt_ch8_bit_k... 81 print_bitword( S( X, Y ) );
eucrypt_ch8_bit_k... 82 new_line(1);
eucrypt_ch8_bit_k... 83 end loop;
eucrypt_ch8_bit_k... 84 end loop;
eucrypt_ch8_bit_k... 85 end print_state;
eucrypt_ch8_bit_k... 86
eucrypt_ch8_bit_k... 87 function read_state(File: in FILE_TYPE; Oct: Positive :=8) return State is
eucrypt_ch8_bit_k... 88 S: State;
eucrypt_ch8_bit_k... 89 Line1: String := "0000000000000000 " &
eucrypt_ch8_bit_k... 90 "0000000000000000 " &
eucrypt_ch8_bit_k... 91 "0000000000000000 " &
eucrypt_ch8_bit_k... 92 "0000000000000000 " &
eucrypt_ch8_bit_k... 93 "0000000000000000";
eucrypt_ch8_bit_k... 94 StartPos, EndPos: Positive;
eucrypt_ch8_bit_k... 95 Len: Positive := Oct*2;
eucrypt_ch8_bit_k... 96 HStr: Hexstring;
eucrypt_ch8_bit_k... 97 begin
eucrypt_ch8_bit_k... 98 for Y in XYCoord loop
eucrypt_ch8_bit_k... 99 Line1 := Get_Line(File);
eucrypt_ch8_bit_k... 100 StartPos := Line1'First;
eucrypt_ch8_bit_k... 101 EndPos := StartPos + Len-1;
eucrypt_ch8_bit_k... 102
eucrypt_ch8_bit_k... 103 for X in XYCoord loop
eucrypt_ch8_bit_k... 104 HStr := Line1( StartPos .. EndPos );
eucrypt_ch8_bit_k... 105 S( X, Y ) := HexToBitword(HStr);
eucrypt_ch8_bit_k... 106 StartPos := EndPos + 2; --one space to skip
eucrypt_ch8_bit_k... 107 EndPos := StartPos + Len - 1;
eucrypt_ch8_bit_k... 108 end loop;
eucrypt_ch8_bit_k... 109 end loop;
eucrypt_ch8_bit_k... 110 return S;
eucrypt_ch8_bit_k... 111 end read_state;
eucrypt_ch8_bit_k... 112
eucrypt_ch8_bit_k... 113 --reads a full test round from specified file (pre-defined format)
eucrypt_ch8_bit_k... 114 function read_from_file (filename : in String;
eucrypt_ch8_bit_k... 115 T : out Test_Round)
eucrypt_ch8_bit_k... 116 return Boolean is
eucrypt_ch8_bit_k... 117 file: FILE_TYPE;
eucrypt_ch8_bit_k... 118 InputMarker: String := "lanes as 64-bit words:";
eucrypt_ch8_bit_k... 119 octets: Positive := 8;
eucrypt_ch8_bit_k... 120 RoundNo: Round_Index;
eucrypt_ch8_bit_k... 121 begin
eucrypt_ch8_bit_k... 122 -- try to open the input file
eucrypt_ch8_bit_k... 123 begin
eucrypt_ch8_bit_k... 124 open(file, In_File, filename);
eucrypt_ch8_bit_k... 125 exception
eucrypt_ch8_bit_k... 126 when others =>
eucrypt_ch8_bit_k... 127 Put_Line(Standard_Error,
eucrypt_ch8_bit_k... 128 "Can not open the file '" & filename & "'. Does it exist?");
eucrypt_ch8_bit_k... 129 return False;
eucrypt_ch8_bit_k... 130 end;
eucrypt_ch8_bit_k... 131
eucrypt_ch8_bit_k... 132 -- find & read input state first
eucrypt_ch8_bit_k... 133 RoundNo := -1;
eucrypt_ch8_bit_k... 134 loop
eucrypt_ch8_bit_k... 135 declare
eucrypt_ch8_bit_k... 136 Line: String := Get_Line(file);
eucrypt_ch8_bit_k... 137 begin
eucrypt_ch8_bit_k... 138 --check if this is test data of any known kind
eucrypt_ch8_bit_k... 139 if index(Line, InputMarker, 1) > 0 then
eucrypt_ch8_bit_k... 140 T(0)(None) := read_state(file, octets);
eucrypt_ch8_bit_k... 141 print_state(T(0)(None), "Read Input State");
eucrypt_ch8_bit_k... 142 elsif index(Line, "Round ", 1) > 0 then
eucrypt_ch8_bit_k... 143 RoundNo := RoundNo +1;
eucrypt_ch8_bit_k... 144 elsif index(Line, "theta", 1) > 0 then
eucrypt_ch8_bit_k... 145 T(RoundNo)(Theta) := read_state(file, octets);
eucrypt_ch8_bit_k... 146 if (RoundNo > 0) then
eucrypt_ch8_bit_k... 147 T(RoundNo)(None) := T(RoundNo-1)(Iota); -- previous state as input
eucrypt_ch8_bit_k... 148 end if;
eucrypt_ch8_bit_k... 149 elsif index(Line, "rho", 1) > 0 then
eucrypt_ch8_bit_k... 150 T(RoundNo)(Rho) := read_state(file, octets);
eucrypt_ch8_bit_k... 151 elsif index(Line, "pi", 1) > 0 then
eucrypt_ch8_bit_k... 152 T(RoundNo)(Pi) := read_state(file, octets);
eucrypt_ch8_bit_k... 153 elsif index(Line, "chi", 1) > 0 then
eucrypt_ch8_bit_k... 154 T(RoundNo)(Chi) := read_state(file, octets);
eucrypt_ch8_bit_k... 155 elsif index(Line, "iota", 1) > 0 then
eucrypt_ch8_bit_k... 156 T(RoundNo)(Iota) := read_state(file, octets);
eucrypt_ch8_bit_k... 157 end if;
eucrypt_ch8_bit_k... 158 exit when End_Of_File(file);
eucrypt_ch8_bit_k... 159 end;
eucrypt_ch8_bit_k... 160 end loop;
eucrypt_ch8_bit_k... 161 Close(file);
eucrypt_ch8_bit_k... 162 return True;
eucrypt_ch8_bit_k... 163 end read_from_file;
eucrypt_ch8_bit_k... 164
eucrypt_ch8_bit_k... 165
eucrypt_ch8_bit_k... 166 -- performs one single round of Keccak, step by step
eucrypt_ch8_bit_k... 167 -- each permutation is tested separately
eucrypt_ch8_bit_k... 168 -- test fails with exception raised at first output not matching expected
eucrypt_ch8_bit_k... 169 procedure test_one_round(T: Test_Vector; Round: Round_Index) is
eucrypt_ch8_bit_k... 170 Input: State;
eucrypt_ch8_bit_k... 171 Expected: State;
eucrypt_ch8_bit_k... 172 Output: State;
eucrypt_ch8_bit_k... 173 Test_One_Round_Fail: Exception;
eucrypt_ch8_bit_k... 174 begin
eucrypt_ch8_bit_k... 175 Input := T(None);
eucrypt_ch8_bit_k... 176 for I in Keccak_Perms range Theta..Iota loop
eucrypt_ch8_bit_k... 177 Expected := T(I);
eucrypt_ch8_bit_k... 178 case I is
eucrypt_ch8_bit_k... 179 when Theta => Output := SMG_Bit_Keccak.Theta(Input);
eucrypt_ch8_bit_k... 180 when Rho => Output := SMG_Bit_Keccak.Rho(Input);
eucrypt_ch8_bit_k... 181 when Pi => Output := SMG_Bit_Keccak.Pi(Input);
eucrypt_ch8_bit_k... 182 when Chi => Output := SMG_Bit_Keccak.Chi(Input);
eucrypt_ch8_bit_k... 183 when Iota => Output := SMG_Bit_Keccak.Iota(RC(Round), Input);
eucrypt_ch8_bit_k... 184 when others => null;
eucrypt_ch8_bit_k... 185 end case;
eucrypt_ch8_bit_k... 186
eucrypt_ch8_bit_k... 187 if (Output /= Expected) then
eucrypt_ch8_bit_k... 188 print_state(Output, "----------real output-------");
eucrypt_ch8_bit_k... 189 print_state(Expected, "----------expected output--------");
eucrypt_ch8_bit_k... 190 raise Test_One_Round_Fail;
eucrypt_ch8_bit_k... 191 else
eucrypt_ch8_bit_k... 192 Put_Line("PASSED: " & Keccak_Perms'Image(I));
eucrypt_ch8_bit_k... 193 end if;
eucrypt_ch8_bit_k... 194 -- get ready for next permutation
eucrypt_ch8_bit_k... 195 Input := Expected;
eucrypt_ch8_bit_k... 196 end loop;
eucrypt_ch8_bit_k... 197 end test_one_round;
eucrypt_ch8_bit_k... 198
eucrypt_ch8_bit_k... 199 procedure test_bwrotate_left( Input : in Bitword;
eucrypt_ch8_bit_k... 200 N : Positive;
eucrypt_ch8_bit_k... 201 Expected : in Bitword) is
eucrypt_ch8_bit_k... 202 Output: Bitword;
eucrypt_ch8_bit_k... 203 begin
eucrypt_ch8_bit_k... 204 Output := BWRotate_Left( Input, N );
eucrypt_ch8_bit_k... 205 if Output /= Expected then
eucrypt_ch8_bit_k... 206 Put_Line("FAIL: test bitword rotate left");
eucrypt_ch8_bit_k... 207 Put_Line("Output:");
eucrypt_ch8_bit_k... 208 print_bitword( Output );
eucrypt_ch8_bit_k... 209 Put_Line("Expected:");
eucrypt_ch8_bit_k... 210 print_bitword( Expected );
eucrypt_ch8_bit_k... 211 else
eucrypt_ch8_bit_k... 212 Put_Line("PASS: test bitword rotate left");
eucrypt_ch8_bit_k... 213 end if;
eucrypt_ch8_bit_k... 214 end test_bwrotate_left;
eucrypt_ch8_bit_k... 215
eucrypt_ch8_bit_k... 216 procedure test_keccak_function(T: in Test_Round) is
eucrypt_ch8_bit_k... 217 S: State;
eucrypt_ch8_bit_k... 218 begin
eucrypt_ch8_bit_k... 219 Put_Line("---Full Keccak Function test---");
eucrypt_ch8_bit_k... 220 S := Keccak_Function(T(Round_Index'First)(None));
eucrypt_ch8_bit_k... 221 if S /= T(Round_Index'Last)(Iota) then
eucrypt_ch8_bit_k... 222 Put_Line("FAILED: full keccak function test");
eucrypt_ch8_bit_k... 223 else
eucrypt_ch8_bit_k... 224 Put_Line("PASSED: full keccak function test");
eucrypt_ch8_bit_k... 225 end if;
eucrypt_ch8_bit_k... 226 end test_keccak_function;
eucrypt_ch8_bit_k... 227
eucrypt_ch8_bit_k... 228 procedure test_sponge is
eucrypt_ch8_bit_k... 229 Bitrate : constant Keccak_Rate := 1344;
eucrypt_ch8_bit_k... 230 Input1 : Bitstream( 1 .. 5 ) := (1, 1, 0, 0, 1);
eucrypt_ch8_bit_k... 231 Input2 : Bitstream( 1 .. 30) := (1, 1, 0, 0,
eucrypt_ch8_bit_k... 232 1, 0, 1, 0,
eucrypt_ch8_bit_k... 233 0, 0, 0, 1,
eucrypt_ch8_bit_k... 234 1, 0, 1, 0,
eucrypt_ch8_bit_k... 235 1, 1, 0, 1,
eucrypt_ch8_bit_k... 236 1, 1, 1, 0,
eucrypt_ch8_bit_k... 237 1, 0, 0, 1,
eucrypt_ch8_bit_k... 238 1, 0);
eucrypt_ch8_bit_k... 239 Hex : array(0..15) of Character := ("0123456789ABCDEF");
eucrypt_ch8_bit_k... 240 C : Natural;
eucrypt_ch8_bit_k... 241 ExpHex1 : constant String :=
eucrypt_ch8_bit_k... 242 "CB7FFB7CE7572A06C537858A0090FC2888C3C6BA9A3ADAB4"&
eucrypt_ch8_bit_k... 243 "FE7C9AB4EFE7A1E619B834C843A5A79E23F3F7E314AA597D"&
eucrypt_ch8_bit_k... 244 "9DAD376E8413A005984D00CF954F62F59EF30B050C99EA64"&
eucrypt_ch8_bit_k... 245 "E958335DAE684195D439B6E6DFD0E402518B5E7A227C48CF"&
eucrypt_ch8_bit_k... 246 "239CEA1C391241D7605733A9F4B8F3FFBE74EE45A40730ED"&
eucrypt_ch8_bit_k... 247 "1E2FDEFCCA941F518708CBB5B6D5A69C30263267B97D7B29"&
eucrypt_ch8_bit_k... 248 "AC87043880AE43033B1017EFB75C33248E2962892CE69DA8"&
eucrypt_ch8_bit_k... 249 "BAF1DF4C0902B16C64A1ADD42FF458C94C4D3B0B32711BBA"&
eucrypt_ch8_bit_k... 250 "22104989982543D1EF1661AFAF2573687D588C81113ED7FA"&
eucrypt_ch8_bit_k... 251 "F7DDF912021FC03D0E98ACC0200A9F7A0E9629DBA33BA0A3"&
eucrypt_ch8_bit_k... 252 "C03CCA5A7D3560A6DB589422AC64882EF14A62AD9807B353"&
eucrypt_ch8_bit_k... 253 "8DEE1548194DBD456F92B568CE76827F41E0FB3C7F25F3A4"&
eucrypt_ch8_bit_k... 254 "C707AD825B289730FEBDFD22A3E742C6FB7125DE0E38B130"&
eucrypt_ch8_bit_k... 255 "F3059450CA6185156A7EEE2AB7C8E4709956DC6D5E9F99D5"&
eucrypt_ch8_bit_k... 256 "0A19473EA7D737AC934815D68C0710235483DB8551FD8756"&
eucrypt_ch8_bit_k... 257 "45692B4E5E16BB9B1142AE300F5F69F43F0091D534F372E1"&
eucrypt_ch8_bit_k... 258 "FFC2E522E71003E4D27EF6ACCD36B2756FB5FF02DBF0C96B"&
eucrypt_ch8_bit_k... 259 "CAE68E7D6427810582F87051590F6FB65D7B948A9C9D6C93"&
eucrypt_ch8_bit_k... 260 "AF4562367A0AD79109D6F3087C775FE6D60D66B74F8D29FB"&
eucrypt_ch8_bit_k... 261 "4BA80D0168693A748812EA0CD3CA23854CC84D4E716F4C1A"&
eucrypt_ch8_bit_k... 262 "A3B340B1DED2F304DFDBACC1D792C8AC9A1426913E3F67DB"&
eucrypt_ch8_bit_k... 263 "790FD5CFB77DAA29";
eucrypt_ch8_bit_k... 264 ExpHex2 : constant String :=
eucrypt_ch8_bit_k... 265 "35F4FBA9D29E833B1DB17CA2077C11B3348C8AF2A29344AE"&
eucrypt_ch8_bit_k... 266 "6AAA1F63FC4536CE795C54F0359953B97CEA27491691E93E"&
eucrypt_ch8_bit_k... 267 "E4829EAB388211E6E8BD3EDA74366D0947DFA3D65D127593"&
eucrypt_ch8_bit_k... 268 "0AFC42884B7324717DCB003D7B3B5C2E92B84F478CC8DBB5"&
eucrypt_ch8_bit_k... 269 "174EB4BAC6207BD22E56FCC6E5FB11BC598FDBE6208913CE"&
eucrypt_ch8_bit_k... 270 "34BC03837FDBFCDFF9407D948531B5FC7FFE7029F30E7EDC"&
eucrypt_ch8_bit_k... 271 "F9282F0A630FA99839776F5EEA485449F62E421552AF9571";
eucrypt_ch8_bit_k... 272 HexStr1 : String( 1 .. ExpHex1'Length );
eucrypt_ch8_bit_k... 273 Output1 : Bitstream( 1 .. ExpHex1'Length * 4 );
eucrypt_ch8_bit_k... 274 HexStr2 : String( 1 .. ExpHex2'Length );
eucrypt_ch8_bit_k... 275 Output2 : Bitstream( 1 .. ExpHex2'Length * 4 );
eucrypt_ch8_bit_k... 276 Error : Natural;
eucrypt_ch8_bit_k... 277 Pos : Natural;
eucrypt_ch8_bit_k... 278 HexPos : Natural;
eucrypt_ch8_bit_k... 279 begin
eucrypt_ch8_bit_k... 280
eucrypt_ch8_bit_k... 281 -- test 1
eucrypt_ch8_bit_k... 282 Put_Line("---sponge test 1---");
eucrypt_ch8_bit_k... 283 Sponge(Input1, Bitrate, Output1);
eucrypt_ch8_bit_k... 284 Put_Line("Input is:");
eucrypt_ch8_bit_k... 285 for I of Input1 loop
eucrypt_ch8_bit_k... 286 Put(Bit'Image(I));
eucrypt_ch8_bit_k... 287 end loop;
eucrypt_ch8_bit_k... 288 new_line(1);
eucrypt_ch8_bit_k... 289
eucrypt_ch8_bit_k... 290 Put_Line("Output is:");
eucrypt_ch8_bit_k... 291 for I of Output1 loop
eucrypt_ch8_bit_k... 292 Put(Bit'Image(I));
eucrypt_ch8_bit_k... 293 end loop;
eucrypt_ch8_bit_k... 294 new_line(1);
eucrypt_ch8_bit_k... 295
eucrypt_ch8_bit_k... 296 Error := 0;
eucrypt_ch8_bit_k... 297 for I in 1..Output1'Length/4 loop
eucrypt_ch8_bit_k... 298 Pos := Output1'First + (I-1)*4;
eucrypt_ch8_bit_k... 299 C := Natural( Output1( Pos ) ) +
eucrypt_ch8_bit_k... 300 Natural( Output1( Pos + 1 ) ) * 2 +
eucrypt_ch8_bit_k... 301 Natural( Output1( Pos + 2 ) ) * 4 +
eucrypt_ch8_bit_k... 302 Natural( Output1( Pos + 3 ) ) * 8;
eucrypt_ch8_bit_k... 303 HexPos := I + 2 * ( I mod 2 ) - 1;
eucrypt_ch8_bit_k... 304 Hexstr1( HexPos ) := Hex(C);
eucrypt_ch8_bit_k... 305 if Hexstr1( HexPos ) /= ExpHex1( HexPos ) then
eucrypt_ch8_bit_k... 306 Error := Error + 1;
eucrypt_ch8_bit_k... 307 end if;
eucrypt_ch8_bit_k... 308 end loop;
eucrypt_ch8_bit_k... 309 Put_Line("Expected: ");
eucrypt_ch8_bit_k... 310 Put_Line(ExpHex1);
eucrypt_ch8_bit_k... 311 Put_Line("Obtained: ");
eucrypt_ch8_bit_k... 312 Put_Line(Hexstr1);
eucrypt_ch8_bit_k... 313 Put_Line("Errors found: " & Natural'Image(Error));
eucrypt_ch8_bit_k... 314
eucrypt_ch8_bit_k... 315 -- test 2
eucrypt_ch8_bit_k... 316 Put_Line("---sponge test 2---");
eucrypt_ch8_bit_k... 317 Sponge(Input2, Bitrate, Output2);
eucrypt_ch8_bit_k... 318 Put_Line("Input is:");
eucrypt_ch8_bit_k... 319 for I of Input2 loop
eucrypt_ch8_bit_k... 320 Put(Bit'Image(I));
eucrypt_ch8_bit_k... 321 end loop;
eucrypt_ch8_bit_k... 322 new_line(1);
eucrypt_ch8_bit_k... 323
eucrypt_ch8_bit_k... 324 Put_Line("Output is:");
eucrypt_ch8_bit_k... 325 for I of Output2 loop
eucrypt_ch8_bit_k... 326 Put(Bit'Image(I));
eucrypt_ch8_bit_k... 327 end loop;
eucrypt_ch8_bit_k... 328 new_line(1);
eucrypt_ch8_bit_k... 329
eucrypt_ch8_bit_k... 330 Error := 0;
eucrypt_ch8_bit_k... 331 for I in 1..Output2'Length/4 loop
eucrypt_ch8_bit_k... 332 Pos := Output2'First + (I-1)*4;
eucrypt_ch8_bit_k... 333 C := Natural( Output2( Pos ) ) +
eucrypt_ch8_bit_k... 334 Natural( Output2( Pos + 1 ) ) * 2 +
eucrypt_ch8_bit_k... 335 Natural( Output2( Pos + 2 ) ) * 4 +
eucrypt_ch8_bit_k... 336 Natural( Output2( Pos + 3 ) ) * 8;
eucrypt_ch8_bit_k... 337 HexPos := I + 2 * ( I mod 2 ) - 1;
eucrypt_ch8_bit_k... 338 Hexstr2( HexPos ) := Hex(C);
eucrypt_ch8_bit_k... 339 if Hexstr2( HexPos ) /= ExpHex2( HexPos ) then
eucrypt_ch8_bit_k... 340 Error := Error + 1;
eucrypt_ch8_bit_k... 341 end if;
eucrypt_ch8_bit_k... 342 end loop;
eucrypt_ch8_bit_k... 343 Put_Line("Expected: ");
eucrypt_ch8_bit_k... 344 Put_Line(ExpHex2);
eucrypt_ch8_bit_k... 345 Put_Line("Obtained: ");
eucrypt_ch8_bit_k... 346 Put_Line(Hexstr2);
eucrypt_ch8_bit_k... 347 Put_Line("Errors found: " & Natural'Image(Error));
eucrypt_ch8_bit_k... 348
eucrypt_ch8_bit_k... 349 end test_sponge;
eucrypt_ch8_bit_k... 350
eucrypt_ch8_bit_k... 351 -- end of helper methods
eucrypt_ch8_bit_k... 352
eucrypt_ch8_bit_k... 353 --variables
eucrypt_ch8_bit_k... 354 T : Test_Round;
eucrypt_ch8_bit_k... 355 BW, E : Bitword;
eucrypt_ch8_bit_k... 356 begin
eucrypt_ch8_bit_k... 357 BW:=(0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
eucrypt_ch8_bit_k... 358 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,1,0);
eucrypt_ch8_bit_k... 359 E:=(0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
eucrypt_ch8_bit_k... 360 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 1,0,0,0);
eucrypt_ch8_bit_k... 361 test_bwrotate_left(BW, 2, E);
eucrypt_ch8_bit_k... 362
eucrypt_ch8_bit_k... 363 Put_Line("-----Testing with zero state as input------");
eucrypt_ch8_bit_k... 364 if (not read_from_file("testvectorszero.txt", T)) then
eucrypt_ch8_bit_k... 365 return;
eucrypt_ch8_bit_k... 366 end if;
eucrypt_ch8_bit_k... 367
eucrypt_ch8_bit_k... 368 for I in Round_Index loop
eucrypt_ch8_bit_k... 369 Put_Line("---round " & Round_Index'Image(I) & "---");
eucrypt_ch8_bit_k... 370 test_one_round(T(I), I);
eucrypt_ch8_bit_k... 371 end loop;
eucrypt_ch8_bit_k... 372
eucrypt_ch8_bit_k... 373 -- test also Keccak_Function as a whole --
eucrypt_ch8_bit_k... 374 test_keccak_function(T);
eucrypt_ch8_bit_k... 375
eucrypt_ch8_bit_k... 376 Put_Line("-----Testing with non-zero state as input------");
eucrypt_ch8_bit_k... 377 if (not read_from_file("testvectorsnonzero.txt", T)) then
eucrypt_ch8_bit_k... 378 return;
eucrypt_ch8_bit_k... 379 end if;
eucrypt_ch8_bit_k... 380
eucrypt_ch8_bit_k... 381 for I in Round_Index loop
eucrypt_ch8_bit_k... 382 Put_Line("---round " & Round_Index'Image(I) & "---");
eucrypt_ch8_bit_k... 383 test_one_round(T(I), I);
eucrypt_ch8_bit_k... 384 end loop;
eucrypt_ch8_bit_k... 385
eucrypt_ch8_bit_k... 386 -- test also Keccak_Function as a whole --
eucrypt_ch8_bit_k... 387 test_keccak_function(T);
eucrypt_ch8_bit_k... 388
eucrypt_ch8_bit_k... 389 -- test Sponge construction
eucrypt_ch8_bit_k... 390 test_sponge;
eucrypt_ch8_bit_k... 391
eucrypt_ch8_bit_k... 392 end SMG_Bit_Keccak.Test;
eucrypt_ch8_bit_k... 393