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 Put_Line("OUTPUT: " & O);
eucrypt_ch10_oaep... 358 ToBitstream( O, B );
eucrypt_ch10_oaep... 359 if B /= Exp then
eucrypt_ch10_oaep... 360 Put_Line("FAILED: testing hash keccak on string");
eucrypt_ch10_oaep... 361 Put_Line("Output:");
eucrypt_ch10_oaep... 362 for I of B loop
eucrypt_ch10_oaep... 363 Put( Bit'Image( I ) );
eucrypt_ch10_oaep... 364 end loop;
eucrypt_ch10_oaep... 365 new_line(1);
eucrypt_ch10_oaep... 366 Put_Line("Expected:");
eucrypt_ch10_oaep... 367 for I of Exp loop
eucrypt_ch10_oaep... 368 Put( Bit'Image( I ) );
eucrypt_ch10_oaep... 369 end loop;
eucrypt_ch10_oaep... 370 else
eucrypt_ch10_oaep... 371 Put_Line("PASSED: testing hash keccak on string");
eucrypt_ch10_oaep... 372 end if;
eucrypt_ch10_oaep... 373 new_line(1);
eucrypt_ch10_oaep... 374 end test_hash_keccak;
eucrypt_ch10_oaep... 375
eucrypt_ch10_oaep... 376 procedure test_xor_strings is
eucrypt_ch10_oaep... 377 S1 : String := "ABC";
eucrypt_ch10_oaep... 378 S2 : String := "CBA";
eucrypt_ch10_oaep... 379 Exp : String := "...";
eucrypt_ch10_oaep... 380 Result : String := "...";
eucrypt_ch10_oaep... 381 begin
eucrypt_ch10_oaep... 382 Exp( Exp'First ) := Character'Val( 2 );
eucrypt_ch10_oaep... 383 Exp( Exp'First + 1 ) := Character'Val( 0 );
eucrypt_ch10_oaep... 384 Exp( Exp'First + 2 ) := Character'Val( 2 );
eucrypt_ch10_oaep... 385
eucrypt_ch10_oaep... 386 Put_Line("----Testing xor on strings---");
eucrypt_ch10_oaep... 387 XOR_Strings( S1, S2, Result);
eucrypt_ch10_oaep... 388 Put_Line("S1 is " & S1);
eucrypt_ch10_oaep... 389 Put_Line("S2 is " & S2);
eucrypt_ch10_oaep... 390 Put_Line("S1 xor S2 is " & Result);
eucrypt_ch10_oaep... 391 Put_Line("Result is: ");
eucrypt_ch10_oaep... 392 for C of Result loop
eucrypt_ch10_oaep... 393 Put( Natural'Image( Character'Pos( C ) ) );
eucrypt_ch10_oaep... 394 end loop;
eucrypt_ch10_oaep... 395 new_line(1);
eucrypt_ch10_oaep... 396
eucrypt_ch10_oaep... 397 if Result /= Exp then
eucrypt_ch10_oaep... 398 Put_Line("FAILED: xor on strings");
eucrypt_ch10_oaep... 399 else
eucrypt_ch10_oaep... 400 Put_Line("PASSED: xor on strings");
eucrypt_ch10_oaep... 401 end if;
eucrypt_ch10_oaep... 402 end test_xor_strings;
eucrypt_ch10_oaep... 403
eucrypt_ch10_oaep... 404 procedure test_oaep is
eucrypt_ch10_oaep... 405 Msg : String := "abcdefghij jihgfedcba123456789";
eucrypt_ch10_oaep... 406 Encr : OAEP_Block := ( others => ' ' );
eucrypt_ch10_oaep... 407 Decr : OAEP_HALF := ( others => ' ' );
eucrypt_ch10_oaep... 408 Entropy : OAEP_Block := ( others => 'e' );
eucrypt_ch10_oaep... 409 Len : Natural;
eucrypt_ch10_oaep... 410 Flag : Boolean;
eucrypt_ch10_oaep... 411 begin
eucrypt_ch10_oaep... 412 Put_Line("----Testing OAEP Encrypt----");
eucrypt_ch10_oaep... 413 OAEP_Encrypt( Msg, Entropy, Encr );
eucrypt_ch10_oaep... 414
eucrypt_ch10_oaep... 415 Put_Line("----Testing OAEP Decrypt----");
eucrypt_ch10_oaep... 416 OAEP_Decrypt( Encr, Len, Decr, Flag );
eucrypt_ch10_oaep... 417
eucrypt_ch10_oaep... 418 Put_Line("Msg is: " & Msg);
eucrypt_ch10_oaep... 419 Put_Line("Encr is: " & Encr);
eucrypt_ch10_oaep... 420 Put_Line("Decr is: " & Decr);
eucrypt_ch10_oaep... 421 Put_Line("Flag is: " & Boolean'Image( Flag ) );
eucrypt_ch10_oaep... 422 Put_Line("Len is: " & Natural'Image( Len ) );
eucrypt_ch10_oaep... 423
eucrypt_ch10_oaep... 424 if Flag = False or
eucrypt_ch10_oaep... 425 Len /= Msg'Length * 8 or
eucrypt_ch10_oaep... 426 Decr( Decr'First .. Decr'First + Msg'Length - 1 ) /= Msg
eucrypt_ch10_oaep... 427 then
eucrypt_ch10_oaep... 428 Put_Line("FAILED: oaep test");
eucrypt_ch10_oaep... 429 else
eucrypt_ch10_oaep... 430 Put_Line("PASSED: oaep test");
eucrypt_ch10_oaep... 431 end if;
eucrypt_ch10_oaep... 432
eucrypt_ch10_oaep... 433 end test_oaep;
eucrypt_ch10_oaep... 434
eucrypt_ch6_kecca... 435 -- end of helper methods
eucrypt_ch6_kecca... 436
eucrypt_ch6_kecca... 437 --variables
eucrypt_ch6_kecca... 438 T: Test_Round;
eucrypt_ch6_kecca... 439 begin
eucrypt_ch6_kecca... 440 Put_Line("-----Testing with zero state as input------");
eucrypt_ch6_kecca... 441 if (not read_from_file("testvectorszero.txt", T)) then
eucrypt_ch6_kecca... 442 return;
eucrypt_ch6_kecca... 443 end if;
eucrypt_ch6_kecca... 444
eucrypt_ch6_kecca... 445 for I in Round_Index loop
eucrypt_ch6_kecca... 446 Put_Line("---round " & Round_Index'Image(I) & "---");
eucrypt_ch6_kecca... 447 test_one_round(T(I), I);
eucrypt_ch6_kecca... 448 end loop;
eucrypt_ch6_kecca... 449
eucrypt_ch7_kecca... 450 -- test also Keccak_Function as a whole --
eucrypt_ch7_kecca... 451 test_keccak_function(T);
eucrypt_ch7_kecca... 452
eucrypt_ch6_kecca... 453 Put_Line("-----Testing with non-zero state as input------");
eucrypt_ch6_kecca... 454 if (not read_from_file("testvectorsnonzero.txt", T)) then
eucrypt_ch6_kecca... 455 return;
eucrypt_ch6_kecca... 456 end if;
eucrypt_ch6_kecca... 457
eucrypt_ch6_kecca... 458 for I in Round_Index loop
eucrypt_ch6_kecca... 459 Put_Line("---round " & Round_Index'Image(I) & "---");
eucrypt_ch6_kecca... 460 test_one_round(T(I), I);
eucrypt_ch6_kecca... 461 end loop;
eucrypt_ch6_kecca... 462
eucrypt_ch7_kecca... 463 -- test also Keccak_Function as a whole --
eucrypt_ch7_kecca... 464 test_keccak_function(T);
eucrypt_ch7_kecca... 465
eucrypt_ch7_kecca... 466 -- test BitsToWord and WordToBits
eucrypt_ch7_kecca... 467 test_bits_to_word_conversion;
eucrypt_ch7_kecca... 468
eucrypt_ch7_kecca... 469 -- test Sponge construction
eucrypt_ch7_kecca... 470 test_sponge;
eucrypt_ch7_kecca... 471
eucrypt_ch9_kecca... 472 -- test flipping octets
eucrypt_ch9_kecca... 473 test_flip;
eucrypt_ch9_kecca... 474
eucrypt_ch10_oaep... 475 -- test bitstream conversion
eucrypt_ch10_oaep... 476 test_bitstream_conversion;
eucrypt_ch10_oaep... 477
eucrypt_ch10_oaep... 478 -- test hash keccak (strings version)
eucrypt_ch10_oaep... 479 test_hash_keccak;
eucrypt_ch10_oaep... 480
eucrypt_ch10_oaep... 481 -- test oaep encrypt + decrypt
eucrypt_ch10_oaep... 482 test_oaep;
eucrypt_ch10_oaep... 483
eucrypt_ch10_oaep... 484 -- test xor on strings
eucrypt_ch10_oaep... 485 test_xor_strings;
eucrypt_ch10_oaep... 486
eucrypt_ch6_kecca... 487 end SMG_Keccak.Test;