raw
eucrypt_ch10_oaep...    1 with SMG_OAEP; use SMG_OAEP;
eucrypt_ch6_kecca... 2 with SMG_Keccak; use SMG_Keccak;
eucrypt_ch6_kecca... 3 with Ada.Exceptions; use Ada.Exceptions;
eucrypt_ch6_kecca... 4 with Ada.Text_IO; use Ada.Text_IO;
eucrypt_ch6_kecca... 5 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
eucrypt_ch6_kecca... 6 with Interfaces; use Interfaces;
eucrypt_ch6_kecca... 7
eucrypt_ch6_kecca... 8 procedure SMG_Keccak.Test is
eucrypt_ch6_kecca... 9 --types
eucrypt_ch6_kecca... 10 type Keccak_Perms is (None, Theta, Rho, Pi, Chi, Iota);
eucrypt_ch6_kecca... 11 type Test_Vector is array(Keccak_Perms) of State;
eucrypt_ch6_kecca... 12 type Test_Round is array(Round_Index) of Test_Vector;
eucrypt_ch6_kecca... 13
eucrypt_ch6_kecca... 14 --helper methods
eucrypt_ch6_kecca... 15 procedure print_state(S: in State; Title: in String) is
eucrypt_ch6_kecca... 16 Hex: array(0..15) of Character := ("0123456789ABCDEF");
eucrypt_ch6_kecca... 17 Len: constant Natural := Z_Length / 4;
eucrypt_ch6_kecca... 18 HexString: String(1..Len);
eucrypt_ch6_kecca... 19 W: ZWord;
eucrypt_ch6_kecca... 20 begin
eucrypt_ch6_kecca... 21 Put_Line("---------" & Title & "---------");
eucrypt_ch6_kecca... 22 for Y in XYCoord loop
eucrypt_ch6_kecca... 23 for X in XYCoord loop
eucrypt_ch6_kecca... 24 W := S(X,Y);
eucrypt_ch6_kecca... 25 for Z in 0..Len-1 loop
eucrypt_ch6_kecca... 26 HexString(Natural(Len-Z)) := Hex(Natural(W mod 16));
eucrypt_ch6_kecca... 27 W := W / 16;
eucrypt_ch6_kecca... 28 end loop;
eucrypt_ch6_kecca... 29 Put(HexString & " ");
eucrypt_ch6_kecca... 30 end loop;
eucrypt_ch6_kecca... 31 Put_Line("");
eucrypt_ch6_kecca... 32 end loop;
eucrypt_ch6_kecca... 33 end;
eucrypt_ch6_kecca... 34
eucrypt_ch7_kecca... 35 procedure print_bitstream(B: in Bitstream; Title: in String) is
eucrypt_ch7_kecca... 36 Hex : array(0..15) of Character := ("0123456789ABCDEF");
eucrypt_ch7_kecca... 37 HexString : String(1..B'Length/4);
eucrypt_ch7_kecca... 38 C : Natural;
eucrypt_ch7_kecca... 39 Pos : Natural;
eucrypt_ch7_kecca... 40 begin
eucrypt_ch7_kecca... 41 for I in 1..B'Length/4 loop
eucrypt_ch7_kecca... 42 Pos := (I-1)*4 + B'First;
eucrypt_ch7_kecca... 43 C := Natural( B(Pos) ) * 8 +
eucrypt_ch7_kecca... 44 Natural( B(Pos + 1) ) * 4 +
eucrypt_ch7_kecca... 45 Natural( B(Pos + 2) ) * 2 +
eucrypt_ch7_kecca... 46 Natural( B(Pos + 3) );
eucrypt_ch7_kecca... 47 HexString(I) := Hex(C);
eucrypt_ch7_kecca... 48 end loop;
eucrypt_ch7_kecca... 49 Put_Line("---" & Title & "---");
eucrypt_ch7_kecca... 50 Put_Line(HexString);
eucrypt_ch7_kecca... 51 end print_bitstream;
eucrypt_ch7_kecca... 52
eucrypt_ch6_kecca... 53 function read_state(File: in FILE_TYPE; Oct: Positive :=8) return State is
eucrypt_ch6_kecca... 54 S: State;
eucrypt_ch6_kecca... 55 Line1: String := "0000000000000000 " &
eucrypt_ch6_kecca... 56 "0000000000000000 " &
eucrypt_ch6_kecca... 57 "0000000000000000 " &
eucrypt_ch6_kecca... 58 "0000000000000000 " &
eucrypt_ch6_kecca... 59 "0000000000000000";
eucrypt_ch6_kecca... 60 StartPos, EndPos: Positive;
eucrypt_ch6_kecca... 61 Len: Positive := Oct*2;
eucrypt_ch6_kecca... 62 begin
eucrypt_ch6_kecca... 63 for Y in XYCoord loop
eucrypt_ch6_kecca... 64 Line1 := Get_Line(File);
eucrypt_ch6_kecca... 65 StartPos := Line1'First;
eucrypt_ch6_kecca... 66 EndPos := StartPos + Len-1;
eucrypt_ch6_kecca... 67
eucrypt_ch6_kecca... 68 for X in XYCoord loop
eucrypt_ch6_kecca... 69 S(X,Y) := ZWord'value("16#" & Line1(StartPos..EndPos) & "#");
eucrypt_ch6_kecca... 70 StartPos := EndPos + 2; --one space to skip
eucrypt_ch6_kecca... 71 EndPos := StartPos + Len - 1;
eucrypt_ch6_kecca... 72 end loop;
eucrypt_ch6_kecca... 73 end loop;
eucrypt_ch6_kecca... 74 return S;
eucrypt_ch6_kecca... 75 end read_state;
eucrypt_ch6_kecca... 76
eucrypt_ch6_kecca... 77 --reads a full test round from specified file (pre-defined format)
eucrypt_ch6_kecca... 78 function read_from_file (filename : in String;
eucrypt_ch6_kecca... 79 T : out Test_Round)
eucrypt_ch6_kecca... 80 return Boolean is
eucrypt_ch6_kecca... 81 file: FILE_TYPE;
eucrypt_ch6_kecca... 82 InputMarker: String := "lanes as 64-bit words:";
eucrypt_ch6_kecca... 83 octets: Positive := 8;
eucrypt_ch6_kecca... 84 RoundNo: Round_Index;
eucrypt_ch6_kecca... 85 begin
eucrypt_ch6_kecca... 86 -- try to open the input file
eucrypt_ch6_kecca... 87 begin
eucrypt_ch6_kecca... 88 open(file, In_File, filename);
eucrypt_ch6_kecca... 89 exception
eucrypt_ch6_kecca... 90 when others =>
eucrypt_ch6_kecca... 91 Put_Line(Standard_Error,
eucrypt_ch6_kecca... 92 "Can not open the file '" & filename & "'. Does it exist?");
eucrypt_ch6_kecca... 93 return False;
eucrypt_ch6_kecca... 94 end;
eucrypt_ch6_kecca... 95
eucrypt_ch6_kecca... 96 -- find & read input state first
eucrypt_ch6_kecca... 97 RoundNo := -1;
eucrypt_ch6_kecca... 98 loop
eucrypt_ch6_kecca... 99 declare
eucrypt_ch6_kecca... 100 Line: String := Get_Line(file);
eucrypt_ch6_kecca... 101 begin
eucrypt_ch6_kecca... 102 --check if this is test data of any known kind
eucrypt_ch6_kecca... 103 if index(Line, InputMarker, 1) > 0 then
eucrypt_ch6_kecca... 104 T(0)(None) := read_state(file, octets);
eucrypt_ch6_kecca... 105 print_state(T(0)(None), "Read Input State");
eucrypt_ch6_kecca... 106 elsif index(Line, "Round ", 1) > 0 then
eucrypt_ch6_kecca... 107 RoundNo := RoundNo +1;
eucrypt_ch6_kecca... 108 elsif index(Line, "theta", 1) > 0 then
eucrypt_ch6_kecca... 109 T(RoundNo)(Theta) := read_state(file, octets);
eucrypt_ch6_kecca... 110 if (RoundNo > 0) then
eucrypt_ch6_kecca... 111 T(RoundNo)(None) := T(RoundNo-1)(Iota); -- previous state as input
eucrypt_ch6_kecca... 112 end if;
eucrypt_ch6_kecca... 113 elsif index(Line, "rho", 1) > 0 then
eucrypt_ch6_kecca... 114 T(RoundNo)(Rho) := read_state(file, octets);
eucrypt_ch6_kecca... 115 elsif index(Line, "pi", 1) > 0 then
eucrypt_ch6_kecca... 116 T(RoundNo)(Pi) := read_state(file, octets);
eucrypt_ch6_kecca... 117 elsif index(Line, "chi", 1) > 0 then
eucrypt_ch6_kecca... 118 T(RoundNo)(Chi) := read_state(file, octets);
eucrypt_ch6_kecca... 119 elsif index(Line, "iota", 1) > 0 then
eucrypt_ch6_kecca... 120 T(RoundNo)(Iota) := read_state(file, octets);
eucrypt_ch6_kecca... 121 end if;
eucrypt_ch6_kecca... 122 exit when End_Of_File(file);
eucrypt_ch6_kecca... 123 end;
eucrypt_ch6_kecca... 124 end loop;
eucrypt_ch6_kecca... 125 Close(file);
eucrypt_ch6_kecca... 126 return True;
eucrypt_ch6_kecca... 127 end read_from_file;
eucrypt_ch6_kecca... 128
eucrypt_ch6_kecca... 129 -- performs one single round of Keccak, step by step
eucrypt_ch6_kecca... 130 -- each permutation is tested separately
eucrypt_ch6_kecca... 131 -- test fails with exception raised at first output not matching expected
eucrypt_ch6_kecca... 132 procedure test_one_round(T: Test_Vector; Round: Round_Index) is
eucrypt_ch6_kecca... 133 Input: State;
eucrypt_ch6_kecca... 134 Expected: State;
eucrypt_ch6_kecca... 135 Output: State;
eucrypt_ch6_kecca... 136 Test_One_Round_Fail: Exception;
eucrypt_ch6_kecca... 137 begin
eucrypt_ch6_kecca... 138 Input := T(None);
eucrypt_ch6_kecca... 139 for I in Keccak_Perms range Theta..Iota loop
eucrypt_ch6_kecca... 140 Expected := T(I);
eucrypt_ch6_kecca... 141 case I is
eucrypt_ch6_kecca... 142 when Theta => Output := SMG_Keccak.Theta(Input);
eucrypt_ch6_kecca... 143 when Rho => Output := SMG_Keccak.Rho(Input);
eucrypt_ch6_kecca... 144 when Pi => Output := SMG_Keccak.Pi(Input);
eucrypt_ch6_kecca... 145 when Chi => Output := SMG_Keccak.Chi(Input);
eucrypt_ch6_kecca... 146 when Iota => Output := SMG_Keccak.Iota(RC(Round), Input);
eucrypt_ch6_kecca... 147 when others => null;
eucrypt_ch6_kecca... 148 end case;
eucrypt_ch6_kecca... 149
eucrypt_ch6_kecca... 150 if (Output /= Expected) then
eucrypt_ch6_kecca... 151 print_state(Output, "----------real output-------");
eucrypt_ch6_kecca... 152 print_state(Expected, "----------expected output--------");
eucrypt_ch6_kecca... 153 raise Test_One_Round_Fail;
eucrypt_ch6_kecca... 154 else
eucrypt_ch6_kecca... 155 Put_Line("PASSED: " & Keccak_Perms'Image(I));
eucrypt_ch6_kecca... 156 end if;
eucrypt_ch6_kecca... 157 -- get ready for next permutation
eucrypt_ch6_kecca... 158 Input := Expected;
eucrypt_ch6_kecca... 159 end loop;
eucrypt_ch6_kecca... 160 end test_one_round;
eucrypt_ch7_kecca... 161
eucrypt_ch7_kecca... 162 procedure test_bits_to_word_conversion is
eucrypt_ch7_kecca... 163 bits: Bitword := (others => 0);
eucrypt_ch7_kecca... 164 obtained_bits: Bitword := (others => 0);
eucrypt_ch7_kecca... 165 expected: ZWord;
eucrypt_ch7_kecca... 166 obtained: ZWord;
eucrypt_ch7_kecca... 167 begin
eucrypt_ch9_kecca... 168 expected := 16#8FA4F19E0287BBE7#;
eucrypt_ch7_kecca... 169 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,
eucrypt_ch7_kecca... 170 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,
eucrypt_ch7_kecca... 171 1,1,1,1, 0,0,0,1);
eucrypt_ch7_kecca... 172 obtained := BitsToWord(bits);
eucrypt_ch7_kecca... 173 obtained_bits := WordToBits(expected);
eucrypt_ch7_kecca... 174
eucrypt_ch7_kecca... 175 if obtained /= expected then
eucrypt_ch7_kecca... 176 Put_Line("FAIL: bits to word");
eucrypt_ch7_kecca... 177 Put_Line("Expected: " & ZWord'Image(expected));
eucrypt_ch7_kecca... 178 Put_Line("Obtained: " & ZWord'Image(obtained));
eucrypt_ch7_kecca... 179 else
eucrypt_ch7_kecca... 180 Put_Line("PASSED: bits to word");
eucrypt_ch7_kecca... 181 end if;
eucrypt_ch7_kecca... 182
eucrypt_ch7_kecca... 183 if obtained_bits /= bits then
eucrypt_ch7_kecca... 184 Put_Line("FAIL: word to bits");
eucrypt_ch7_kecca... 185 Put("Expected: ");
eucrypt_ch7_kecca... 186 for I in Bitword'Range loop
eucrypt_ch7_kecca... 187 Put(Bit'Image(bits(I)));
eucrypt_ch7_kecca... 188 end loop;
eucrypt_ch7_kecca... 189 Put_Line("");
eucrypt_ch7_kecca... 190 Put_Line("Obtained: ");
eucrypt_ch7_kecca... 191 for I in Bitword'Range loop
eucrypt_ch7_kecca... 192 Put(Bit'Image(obtained_bits(I)));
eucrypt_ch7_kecca... 193 end loop;
eucrypt_ch7_kecca... 194 Put_Line("");
eucrypt_ch7_kecca... 195 else
eucrypt_ch7_kecca... 196 Put_Line("PASSED: word to bits");
eucrypt_ch7_kecca... 197 end if;
eucrypt_ch7_kecca... 198 end test_bits_to_word_conversion;
eucrypt_ch7_kecca... 199
eucrypt_ch9_kecca... 200 procedure test_flip is
eucrypt_ch9_kecca... 201 B: constant Bitword := (1, 0, 1, 1, 1, 1, 0, 0,
eucrypt_ch9_kecca... 202 1, 1, 1, 0, 0, 0, 0, 1,
eucrypt_ch9_kecca... 203 0, 1, 1, 0, 0, 0, 1, 0,
eucrypt_ch9_kecca... 204 1, 1, 1, 1, 1, 1, 1, 1,
eucrypt_ch9_kecca... 205 1, 1, 0, 1, 1, 0, 0, 1,
eucrypt_ch9_kecca... 206 0, 0, 0, 0, 0, 0, 0, 0,
eucrypt_ch9_kecca... 207 0, 0, 1, 1, 0, 0, 0, 1,
eucrypt_ch9_kecca... 208 0, 0, 0, 1, 1, 0, 0, 0);
eucrypt_ch9_kecca... 209 Expected: Bitword := (0, 0, 0, 1, 1, 0, 0, 0,
eucrypt_ch9_kecca... 210 0, 0, 1, 1, 0, 0, 0, 1,
eucrypt_ch9_kecca... 211 0, 0, 0, 0, 0, 0, 0, 0,
eucrypt_ch9_kecca... 212 1, 1, 0, 1, 1, 0, 0, 1,
eucrypt_ch9_kecca... 213 1, 1, 1, 1, 1, 1, 1, 1,
eucrypt_ch9_kecca... 214 0, 1, 1, 0, 0, 0, 1, 0,
eucrypt_ch9_kecca... 215 1, 1, 1, 0, 0, 0, 0, 1,
eucrypt_ch9_kecca... 216 1, 0, 1, 1, 1, 1, 0, 0);
eucrypt_ch9_kecca... 217 Output : Bitword;
eucrypt_ch9_kecca... 218 begin
eucrypt_ch9_kecca... 219 Output := FlipOctets( B );
eucrypt_ch9_kecca... 220 if Output /= Expected then
eucrypt_ch9_kecca... 221 Put_Line( "FAILED: flip octets" );
eucrypt_ch9_kecca... 222 Put_Line( "Expected: " );
eucrypt_ch9_kecca... 223 for I of Expected loop
eucrypt_ch9_kecca... 224 Put(Bit'Image(I));
eucrypt_ch9_kecca... 225 end loop;
eucrypt_ch9_kecca... 226 new_line(1);
eucrypt_ch9_kecca... 227 Put_Line( "Output: " );
eucrypt_ch9_kecca... 228 for I of Output loop
eucrypt_ch9_kecca... 229 Put(Bit'Image(I));
eucrypt_ch9_kecca... 230 end loop;
eucrypt_ch9_kecca... 231 new_line(1);
eucrypt_ch9_kecca... 232 else
eucrypt_ch9_kecca... 233 Put_Line( "PASSED: flip octets" );
eucrypt_ch9_kecca... 234 end if;
eucrypt_ch9_kecca... 235 end test_flip;
eucrypt_ch9_kecca... 236
eucrypt_ch7_kecca... 237 procedure test_sponge is
eucrypt_ch7_kecca... 238 Bitrate : constant Keccak_Rate := 1344;
eucrypt_ch7_kecca... 239 Input : Bitstream(1..5) := (1, 1, 0, 0, 1);
eucrypt_ch7_kecca... 240 Hex : array(0..15) of Character := ("0123456789ABCDEF");
eucrypt_ch7_kecca... 241 C : Natural;
eucrypt_ch9_kecca... 242 HexPos : Natural;
eucrypt_ch7_kecca... 243 Error : Natural;
eucrypt_ch7_kecca... 244 Pos : Natural;
eucrypt_ch9_kecca... 245 ExpHex : constant String :=
eucrypt_ch9_kecca... 246 "CB7FFB7CE7572A06C537858A0090FC2888C3C6BA9A3ADAB4"&
eucrypt_ch9_kecca... 247 "FE7C9AB4EFE7A1E619B834C843A5A79E23F3F7E314AA597D"&
eucrypt_ch9_kecca... 248 "9DAD376E8413A005984D00CF954F62F59EF30B050C99EA64"&
eucrypt_ch9_kecca... 249 "E958335DAE684195D439B6E6DFD0E402518B5E7A227C48CF"&
eucrypt_ch9_kecca... 250 "239CEA1C391241D7605733A9F4B8F3FFBE74EE45A40730ED"&
eucrypt_ch9_kecca... 251 "1E2FDEFCCA941F518708CBB5B6D5A69C30263267B97D7B29"&
eucrypt_ch9_kecca... 252 "AC87043880AE43033B1017EFB75C33248E2962892CE69DA8"&
eucrypt_ch9_kecca... 253 "BAF1DF4C0902B16C64A1ADD42FF458C94C4D3B0B32711BBA"&
eucrypt_ch9_kecca... 254 "22104989982543D1EF1661AFAF2573687D588C81113ED7FA"&
eucrypt_ch9_kecca... 255 "F7DDF912021FC03D0E98ACC0200A9F7A0E9629DBA33BA0A3"&
eucrypt_ch9_kecca... 256 "C03CCA5A7D3560A6DB589422AC64882EF14A62AD9807B353"&
eucrypt_ch9_kecca... 257 "8DEE1548194DBD456F92B568CE76827F41E0FB3C7F25F3A4"&
eucrypt_ch9_kecca... 258 "C707AD825B289730FEBDFD22A3E742C6FB7125DE0E38B130"&
eucrypt_ch9_kecca... 259 "F3059450CA6185156A7EEE2AB7C8E4709956DC6D5E9F99D5"&
eucrypt_ch9_kecca... 260 "0A19473EA7D737AC934815D68C0710235483DB8551FD8756"&
eucrypt_ch9_kecca... 261 "45692B4E5E16BB9B1142AE300F5F69F43F0091D534F372E1"&
eucrypt_ch9_kecca... 262 "FFC2E522E71003E4D27EF6ACCD36B2756FB5FF02DBF0C96B"&
eucrypt_ch9_kecca... 263 "CAE68E7D6427810582F87051590F6FB65D7B948A9C9D6C93"&
eucrypt_ch9_kecca... 264 "AF4562367A0AD79109D6F3087C775FE6D60D66B74F8D29FB"&
eucrypt_ch9_kecca... 265 "4BA80D0168693A748812EA0CD3CA23854CC84D4E716F4C1A"&
eucrypt_ch9_kecca... 266 "A3B340B1DED2F304DFDBACC1D792C8AC9A1426913E3F67DB"&
eucrypt_ch9_kecca... 267 "790FD5CFB77DAA29";
eucrypt_ch9_kecca... 268 Output : Bitstream( 1 .. ExpHex'Length * 4 );
eucrypt_ch9_kecca... 269 HexString : String( 1 .. ExpHex'Length );
eucrypt_ch7_kecca... 270 begin
eucrypt_ch7_kecca... 271 Put_Line("---sponge test---");
eucrypt_ch10_oaep... 272 Sponge(Input, Output, Bitrate);
eucrypt_ch7_kecca... 273 Put_Line("Input is:");
eucrypt_ch7_kecca... 274 for I of Input loop
eucrypt_ch7_kecca... 275 Put(Bit'Image(I));
eucrypt_ch7_kecca... 276 end loop;
eucrypt_ch7_kecca... 277 new_line(1);
eucrypt_ch7_kecca... 278
eucrypt_ch7_kecca... 279 Put_Line("Output is:");
eucrypt_ch7_kecca... 280 for I of Output loop
eucrypt_ch7_kecca... 281 Put(Bit'Image(I));
eucrypt_ch7_kecca... 282 end loop;
eucrypt_ch7_kecca... 283 new_line(1);
eucrypt_ch7_kecca... 284
eucrypt_ch7_kecca... 285 Error := 0;
eucrypt_ch7_kecca... 286 for I in 1..Output'Length/4 loop
eucrypt_ch7_kecca... 287 Pos := Output'First + (I-1)*4;
eucrypt_ch9_kecca... 288 C := Natural( Output( Pos ) ) +
eucrypt_ch9_kecca... 289 Natural( Output( Pos + 1 ) ) * 2 +
eucrypt_ch9_kecca... 290 Natural( Output( Pos + 2 ) ) * 4 +
eucrypt_ch9_kecca... 291 Natural( Output( Pos + 3 ) ) * 8;
eucrypt_ch9_kecca... 292 HexPos := I + 2 * ( I mod 2 ) - 1;
eucrypt_ch9_kecca... 293 Hexstring(HexPos) := Hex( C );
eucrypt_ch9_kecca... 294 if Hexstring(HexPos) /= ExpHex(HexPos) then
eucrypt_ch7_kecca... 295 Error := Error + 1;
eucrypt_ch7_kecca... 296 end if;
eucrypt_ch7_kecca... 297 end loop;
eucrypt_ch7_kecca... 298 Put_Line("Expected: ");
eucrypt_ch7_kecca... 299 Put_Line(ExpHex);
eucrypt_ch7_kecca... 300 Put_Line("Obtained: ");
eucrypt_ch7_kecca... 301 Put_Line(Hexstring);
eucrypt_ch7_kecca... 302 Put_Line("Errors found: " & Natural'Image(Error));
eucrypt_ch7_kecca... 303
eucrypt_ch7_kecca... 304 end test_sponge;
eucrypt_ch7_kecca... 305
eucrypt_ch7_kecca... 306 procedure test_keccak_function(T: in Test_Round) is
eucrypt_ch7_kecca... 307 S: State;
eucrypt_ch7_kecca... 308 begin
eucrypt_ch7_kecca... 309 Put_Line("---Full Keccak Function test---");
eucrypt_ch7_kecca... 310 S := Keccak_Function(T(Round_Index'First)(None));
eucrypt_ch7_kecca... 311 if S /= T(Round_Index'Last)(Iota) then
eucrypt_ch7_kecca... 312 Put_Line("FAILED: full keccak function test");
eucrypt_ch7_kecca... 313 else
eucrypt_ch7_kecca... 314 Put_Line("PASSED: full keccak function test");
eucrypt_ch7_kecca... 315 end if;
eucrypt_ch7_kecca... 316 end test_keccak_function;
eucrypt_ch7_kecca... 317
eucrypt_ch10_oaep... 318 procedure test_bitstream_conversion is
eucrypt_ch10_oaep... 319 S: String := "Aa*/";
eucrypt_ch10_oaep... 320 E: Bitstream( 0 .. 31 ) := (1, 0, 0, 0, 0, 0, 1, 0,
eucrypt_ch10_oaep... 321 1, 0, 0, 0, 0, 1, 1, 0,
eucrypt_ch10_oaep... 322 0, 1, 0, 1, 0, 1, 0, 0,
eucrypt_ch10_oaep... 323 1, 1, 1, 1, 0, 1, 0, 0);
eucrypt_ch10_oaep... 324 B: Bitstream( 0 .. 31 );
eucrypt_ch10_oaep... 325 SS: String := " t ";
eucrypt_ch10_oaep... 326 begin
eucrypt_ch10_oaep... 327 Put_Line("---Testing string to bitstream conversion---");
eucrypt_ch10_oaep... 328 ToBitstream( S, B );
eucrypt_ch10_oaep... 329 if E /= B then
eucrypt_ch10_oaep... 330 Put_Line("FAILED: string to bitstream conversion.");
eucrypt_ch10_oaep... 331 else
eucrypt_ch10_oaep... 332 Put_Line("PASSED: string to bitstream conversion.");
eucrypt_ch10_oaep... 333 end if;
eucrypt_ch10_oaep... 334
eucrypt_ch10_oaep... 335 Put_Line("---Testing bitstream to string conversion---");
eucrypt_ch10_oaep... 336 ToString( B, SS );
eucrypt_ch10_oaep... 337 if SS /= S then
eucrypt_ch10_oaep... 338 Put_Line("FAILED: bitstream to string conversion");
eucrypt_ch10_oaep... 339 Put_Line("EXPECTED: " & S);
eucrypt_ch10_oaep... 340 Put_Line("OUTPUT: " & SS);
eucrypt_ch10_oaep... 341 else
eucrypt_ch10_oaep... 342 Put_Line("PASSED: bitstream to string conversion");
eucrypt_ch10_oaep... 343 end if;
eucrypt_ch10_oaep... 344 end test_bitstream_conversion;
eucrypt_ch10_oaep... 345
eucrypt_ch10_oaep... 346 procedure test_hash_keccak is
eucrypt_ch10_oaep... 347 S: String := "X";
eucrypt_ch10_oaep... 348 O: String := "abc";
eucrypt_ch10_oaep... 349 B: Bitstream( 0 .. 23 );
eucrypt_ch10_oaep... 350 BB: Bitstream( 1.. 8):= (0, 0, 0, 1, 1, 0, 1, 0);
eucrypt_ch10_oaep... 351 Exp: Bitstream( 0 .. 23 ) := (1, 1, 1, 0, 0, 0, 0, 1,
eucrypt_ch10_oaep... 352 0, 1, 1, 0, 0, 0, 1, 0,
eucrypt_ch10_oaep... 353 1, 1, 1, 0, 0, 0, 1, 1);
eucrypt_ch10_oaep... 354 begin
eucrypt_ch10_oaep... 355 Put_Line("----Testing hash keccak on string " & S & "----");
eucrypt_ch10_oaep... 356 HashKeccak(S, O);
eucrypt_ch10_oaep... 357 ToBitstream( O, B );
eucrypt_ch10_oaep... 358 if B /= Exp then
eucrypt_ch10_oaep... 359 Put_Line("FAILED: testing hash keccak on string");
eucrypt_ch10_oaep... 360 Put_Line("Output:");
eucrypt_ch10_oaep... 361 for I of B loop
eucrypt_ch10_oaep... 362 Put( Bit'Image( I ) );
eucrypt_ch10_oaep... 363 end loop;
eucrypt_ch10_oaep... 364 new_line(1);
eucrypt_ch10_oaep... 365 Put_Line("Expected:");
eucrypt_ch10_oaep... 366 for I of Exp loop
eucrypt_ch10_oaep... 367 Put( Bit'Image( I ) );
eucrypt_ch10_oaep... 368 end loop;
eucrypt_ch10_oaep... 369 else
eucrypt_ch10_oaep... 370 Put_Line("PASSED: testing hash keccak on string");
eucrypt_ch10_oaep... 371 end if;
eucrypt_ch10_oaep... 372 new_line(1);
eucrypt_ch10_oaep... 373 end test_hash_keccak;
eucrypt_ch10_oaep... 374
eucrypt_ch10_oaep... 375 procedure test_xor_strings is
eucrypt_ch10_oaep... 376 S1 : String := "ABC";
eucrypt_ch10_oaep... 377 S2 : String := "CBA";
eucrypt_ch10_oaep... 378 Exp : String := "...";
eucrypt_ch10_oaep... 379 Result : String := "...";
eucrypt_ch10_oaep... 380 begin
eucrypt_ch10_oaep... 381 Exp( Exp'First ) := Character'Val( 2 );
eucrypt_ch10_oaep... 382 Exp( Exp'First + 1 ) := Character'Val( 0 );
eucrypt_ch10_oaep... 383 Exp( Exp'First + 2 ) := Character'Val( 2 );
eucrypt_ch10_oaep... 384
eucrypt_ch10_oaep... 385 Put_Line("----Testing xor on strings---");
eucrypt_ch10_oaep... 386 XOR_Strings( S1, S2, Result);
eucrypt_ch10_oaep... 387 Put_Line("S1 is " & S1);
eucrypt_ch10_oaep... 388 Put_Line("S2 is " & S2);
eucrypt_ch10_oaep... 389 Put_Line("Result is: ");
eucrypt_ch10_oaep... 390 for C of Result loop
eucrypt_ch10_oaep... 391 Put( Natural'Image( Character'Pos( C ) ) );
eucrypt_ch10_oaep... 392 end loop;
eucrypt_ch10_oaep... 393 new_line(1);
eucrypt_ch10_oaep... 394
eucrypt_ch10_oaep... 395 if Result /= Exp then
eucrypt_ch10_oaep... 396 Put_Line("FAILED: xor on strings");
eucrypt_ch10_oaep... 397 else
eucrypt_ch10_oaep... 398 Put_Line("PASSED: xor on strings");
eucrypt_ch10_oaep... 399 end if;
eucrypt_ch10_oaep... 400 end test_xor_strings;
eucrypt_ch10_oaep... 401
eucrypt_ch10_oaep... 402 procedure test_oaep is
eucrypt_ch10_oaep... 403 Msg : String := "abcdefghij jihgfedcba123456789";
eucrypt_oaep_fix_... 404 LongMsg : String( 1..1000 ) := ( others => 'T' );
eucrypt_ch10_oaep... 405 Encr : OAEP_Block := ( others => ' ' );
eucrypt_ch10_oaep... 406 Decr : OAEP_HALF := ( others => ' ' );
eucrypt_ch10_oaep... 407 Entropy : OAEP_Block := ( others => 'e' );
eucrypt_ch10_oaep... 408 Len : Natural;
eucrypt_ch10_oaep... 409 Flag : Boolean;
eucrypt_oaep_fix_... 410 C : Character;
eucrypt_oaep_fix_... 411 MaxLen : constant := 245;
eucrypt_ch10_oaep... 412 begin
eucrypt_ch10_oaep... 413 Put_Line("----Testing OAEP Encrypt----");
eucrypt_ch10_oaep... 414 OAEP_Encrypt( Msg, Entropy, Encr );
eucrypt_ch10_oaep... 415
eucrypt_ch10_oaep... 416 Put_Line("----Testing OAEP Decrypt----");
eucrypt_ch10_oaep... 417 OAEP_Decrypt( Encr, Len, Decr, Flag );
eucrypt_ch10_oaep... 418
eucrypt_ch10_oaep... 419 if Flag = False or
eucrypt_ch10_oaep... 420 Len /= Msg'Length * 8 or
eucrypt_ch10_oaep... 421 Decr( Decr'First .. Decr'First + Msg'Length - 1 ) /= Msg
eucrypt_ch10_oaep... 422 then
eucrypt_ch10_oaep... 423 Put_Line("FAILED: oaep test");
eucrypt_oaep_fix_... 424 Put_Line("Msg is: " & Msg);
eucrypt_oaep_fix_... 425 Put_Line("Decr is: " & Decr);
eucrypt_oaep_fix_... 426 Put_Line("Flag is: " & Boolean'Image( Flag ) );
eucrypt_oaep_fix_... 427 Put_Line("Len is: " & Natural'Image( Len ) );
eucrypt_ch10_oaep... 428 else
eucrypt_ch10_oaep... 429 Put_Line("PASSED: oaep test");
eucrypt_ch10_oaep... 430 end if;
eucrypt_ch10_oaep... 431
eucrypt_oaep_fix_... 432 -- test decrypt on invalid (non-OAEP) string
eucrypt_oaep_fix_... 433 Flag := True;
eucrypt_oaep_fix_... 434 C := Encr( Encr'First );
eucrypt_oaep_fix_... 435 Encr( Encr'First ) := Character'Val( Character'Pos( C ) / 2 );
eucrypt_oaep_fix_... 436 Decr := ( others => ' ' );
eucrypt_oaep_fix_... 437 OAEP_Decrypt( Encr, Len, Decr, Flag );
eucrypt_oaep_fix_... 438
eucrypt_oaep_fix_... 439 if Flag = True then
eucrypt_oaep_fix_... 440 Put_Line("FAILED: oaep test with invalid package");
eucrypt_oaep_fix_... 441 else
eucrypt_oaep_fix_... 442 Put_Line("PASSED: oaep test with invalid package");
eucrypt_oaep_fix_... 443 end if;
eucrypt_oaep_fix_... 444
eucrypt_oaep_fix_... 445 -- test encrypt on message longer than maximum payload (1096 bits)
eucrypt_oaep_fix_... 446 Flag := False;
eucrypt_oaep_fix_... 447 Len := 0;
eucrypt_oaep_fix_... 448 LongMsg( 1..Msg'Length ) := Msg;
eucrypt_oaep_fix_... 449 Encr := ( others => '.' );
eucrypt_oaep_fix_... 450 OAEP_Encrypt( LongMsg, Entropy, Encr);
eucrypt_oaep_fix_... 451 OAEP_Decrypt( Encr, Len, Decr, Flag);
eucrypt_oaep_fix_... 452
eucrypt_oaep_fix_... 453 if Flag = False or
eucrypt_oaep_fix_... 454 Len /= MaxLen * 8 or
eucrypt_oaep_fix_... 455 Decr( Decr'First .. Decr'First + Len / 8 - 1 ) /=
eucrypt_oaep_fix_... 456 LongMsg( LongMsg'First..LongMsg'First + MaxLen - 1 )
eucrypt_oaep_fix_... 457 then
eucrypt_oaep_fix_... 458 Put_Line("FAILED: oaep test with too long message");
eucrypt_oaep_fix_... 459 Put_Line("Msg is: " & LongMsg);
eucrypt_oaep_fix_... 460 Put_Line("Decr is: " & Decr);
eucrypt_oaep_fix_... 461 Put_Line("Flag is: " & Boolean'Image( Flag ) );
eucrypt_oaep_fix_... 462 Put_Line("Len is: " & Natural'Image( Len ) );
eucrypt_oaep_fix_... 463 else
eucrypt_oaep_fix_... 464 Put_Line("PASSED: oaep test with too long message");
eucrypt_oaep_fix_... 465 end if;
eucrypt_oaep_fix_... 466
eucrypt_ch10_oaep... 467 end test_oaep;
eucrypt_ch10_oaep... 468
eucrypt_keccak_bi... 469 procedure test_all_bitrates is
eucrypt_keccak_bi... 470 Input : constant String := "hello, world";
eucrypt_keccak_bi... 471 Bin : Bitstream( 0 .. Input'Length * 8 - 1 ) := ( others => 0 );
eucrypt_keccak_bi... 472 Bout : Bitstream( 0 .. 100 ) := ( others => 0 );
eucrypt_keccak_bi... 473 begin
eucrypt_keccak_bi... 474 ToBitstream( Input, Bin );
eucrypt_keccak_bi... 475 Put_Line("Testing all bitrates:");
eucrypt_keccak_bi... 476 for Bitrate in Keccak_Rate'Range loop
eucrypt_keccak_bi... 477 Sponge(Bin, Bout, Bitrate);
eucrypt_keccak_bi... 478 Put_Line("PASSED: keccak with bitrate " & Integer'Image(Bitrate));
eucrypt_keccak_bi... 479 end loop;
eucrypt_keccak_bi... 480 end test_all_bitrates;
eucrypt_keccak_bi... 481
eucrypt_ch6_kecca... 482 -- end of helper methods
eucrypt_ch6_kecca... 483
eucrypt_ch6_kecca... 484 --variables
eucrypt_ch6_kecca... 485 T: Test_Round;
eucrypt_ch6_kecca... 486 begin
eucrypt_ch6_kecca... 487 Put_Line("-----Testing with zero state as input------");
eucrypt_ch6_kecca... 488 if (not read_from_file("testvectorszero.txt", T)) then
eucrypt_ch6_kecca... 489 return;
eucrypt_ch6_kecca... 490 end if;
eucrypt_ch6_kecca... 491
eucrypt_ch6_kecca... 492 for I in Round_Index loop
eucrypt_ch6_kecca... 493 Put_Line("---round " & Round_Index'Image(I) & "---");
eucrypt_ch6_kecca... 494 test_one_round(T(I), I);
eucrypt_ch6_kecca... 495 end loop;
eucrypt_ch6_kecca... 496
eucrypt_ch7_kecca... 497 -- test also Keccak_Function as a whole --
eucrypt_ch7_kecca... 498 test_keccak_function(T);
eucrypt_ch7_kecca... 499
eucrypt_ch6_kecca... 500 Put_Line("-----Testing with non-zero state as input------");
eucrypt_ch6_kecca... 501 if (not read_from_file("testvectorsnonzero.txt", T)) then
eucrypt_ch6_kecca... 502 return;
eucrypt_ch6_kecca... 503 end if;
eucrypt_ch6_kecca... 504
eucrypt_ch6_kecca... 505 for I in Round_Index loop
eucrypt_ch6_kecca... 506 Put_Line("---round " & Round_Index'Image(I) & "---");
eucrypt_ch6_kecca... 507 test_one_round(T(I), I);
eucrypt_ch6_kecca... 508 end loop;
eucrypt_ch6_kecca... 509
eucrypt_ch7_kecca... 510 -- test also Keccak_Function as a whole --
eucrypt_ch7_kecca... 511 test_keccak_function(T);
eucrypt_ch7_kecca... 512
eucrypt_ch7_kecca... 513 -- test BitsToWord and WordToBits
eucrypt_ch7_kecca... 514 test_bits_to_word_conversion;
eucrypt_ch7_kecca... 515
eucrypt_ch7_kecca... 516 -- test Sponge construction
eucrypt_ch7_kecca... 517 test_sponge;
eucrypt_ch7_kecca... 518
eucrypt_ch9_kecca... 519 -- test flipping octets
eucrypt_ch9_kecca... 520 test_flip;
eucrypt_ch9_kecca... 521
eucrypt_ch10_oaep... 522 -- test bitstream conversion
eucrypt_ch10_oaep... 523 test_bitstream_conversion;
eucrypt_ch10_oaep... 524
eucrypt_ch10_oaep... 525 -- test hash keccak (strings version)
eucrypt_ch10_oaep... 526 test_hash_keccak;
eucrypt_ch10_oaep... 527
eucrypt_ch10_oaep... 528 -- test oaep encrypt + decrypt
eucrypt_ch10_oaep... 529 test_oaep;
eucrypt_ch10_oaep... 530
eucrypt_ch10_oaep... 531 -- test xor on strings
eucrypt_ch10_oaep... 532 test_xor_strings;
eucrypt_ch10_oaep... 533
eucrypt_keccak_bi... 534 -- test ALL bitrates of the Keccak sponge
eucrypt_keccak_bi... 535 test_all_bitrates;
eucrypt_keccak_bi... 536
eucrypt_ch6_kecca... 537 end SMG_Keccak.Test;