smg_comms_packing... 1
smg_comms_packing... 2
smg_comms_packing... 3
smg_comms_packing... 4 with Packing; use Packing;
smg_comms_packing... 5 with Raw_Types; use Raw_Types;
smg_comms_packing... 6 with RSA_OAEP; use RSA_OAEP;
smg_comms_packing... 7 with Serpent; use Serpent;
smg_comms_packing... 8 with RNG; use RNG;
smg_comms_io_rsa_... 9 with IO_RSA;
smg_comms_packing... 10
smg_comms_packing... 11 with Interfaces; use Interfaces;
smg_comms_packing... 12 with Ada.Text_IO; use Ada.Text_IO;
smg_comms_packing... 13
smg_comms_packing... 14 package body Test_Packing is
smg_comms_packing... 15
smg_comms_packing... 16 procedure Print(Data: in Raw_Types.Octets; Msg: in String) is
smg_comms_packing... 17 begin
smg_comms_packing... 18 Put_Line(Msg);
smg_comms_packing... 19 for I of Data loop
smg_comms_packing... 20 Put(Unsigned_8'Image(I));
smg_comms_packing... 21 end loop;
smg_comms_packing... 22 New_Line;
smg_comms_packing... 23 end Print;
smg_comms_packing... 24
smg_comms_packing... 25 procedure Test_Pack_Unpack_Serpent is
smg_comms_packing... 26 InMsg : Serpent_Msg := (others => 0);
smg_comms_packing... 27 OutMsg : Serpent_Msg := (others => 0);
smg_comms_packing... 28
smg_comms_packing... 29 InPkt : Serpent_Pkt := (others => 0);
smg_comms_packing... 30 OutPkt : Serpent_Pkt := (others => 0);
smg_comms_packing... 31
smg_comms_packing... 32 K : Key := (others => 0);
smg_comms_packing... 33 KS : Key_Schedule;
smg_comms_packing... 34 Plain : Block := (others => 0);
smg_comms_packing... 35 Encr : Block := (others => 0);
smg_comms_packing... 36 Len : constant Natural :=
smg_comms_packing... 37 Raw_Types.SERPENT_OCTETS / Serpent.Block'Length;
smg_comms_packing... 38 begin
smg_comms_packing... 39 for I in 1 .. 128 loop
smg_comms_packing... 40 OutPkt := Pack(InMsg, K);
smg_comms_packing... 41 if OutPkt = InMsg then
smg_comms_packing... 42 raise Test_Error;
smg_comms_packing... 43 end if;
smg_comms_packing... 44
smg_comms_packing... 45 OutMsg := Unpack(OutPkt, K);
smg_comms_packing... 46 if OutMsg /= InMsg then
smg_comms_packing... 47 raise Test_Error;
smg_comms_packing... 48 end if;
smg_comms_packing... 49
smg_comms_packing... 50
smg_comms_packing... 51 Prepare_Key(K, KS);
smg_comms_packing... 52 for J in 1 .. Len loop
smg_comms_packing... 53 Plain := InMsg((J-1)*Block'Length + 1 .. J*Block'Length);
smg_comms_packing... 54 Serpent.Encrypt(KS, Plain, Encr);
smg_comms_packing... 55 if Encr /= OutPkt((J-1)*Block'Length + 1 .. J*Block'Length) then
smg_comms_packing... 56 Put_Line("FAIL: pack/unpack with Serpent.");
smg_comms_packing... 57 raise Test_Error;
smg_comms_packing... 58 end if;
smg_comms_packing... 59 end loop;
smg_comms_packing... 60
smg_comms_packing... 61
smg_comms_packing... 62 InMsg := OutPkt;
smg_comms_packing... 63 end loop;
smg_comms_packing... 64
smg_comms_packing... 65 Put_Line("PASS: test pack/unpack with Serpent.");
smg_comms_packing... 66 end Test_Pack_Unpack_Serpent;
smg_comms_packing... 67
smg_comms_packing... 68 procedure Test_Pack_Unpack_RSA is
smg_comms_packing... 69 Msg : RSA_Msg;
smg_comms_packing... 70 Decr_Msg : RSA_Msg;
smg_comms_packing... 71 PKey : RSA_pkey;
smg_comms_packing... 72 SKey : RSA_skey;
smg_comms_packing... 73 Success : Boolean;
smg_comms_packing... 74 Pkt : RSA_Pkt;
smg_comms_packing... 75 begin
smg_comms_io_rsa_... 76
smg_comms_io_rsa_... 77 IO_RSA.ReadRSAKey( "keys_rsa.txt", SKey );
smg_comms_io_rsa_... 78
smg_comms_packing... 79
smg_comms_packing... 80 PKey.n := SKey.n;
smg_comms_packing... 81 PKey.e := SKey.e;
smg_comms_packing... 82
smg_comms_packing... 83 RNG.Get_Octets(Msg);
smg_comms_packing... 84
smg_comms_packing... 85
smg_comms_packing... 86 Pkt := Packing.Pack( Msg, PKey );
smg_comms_packing... 87
smg_comms_packing... 88 Decr_Msg := Packing.Unpack( Pkt, SKey, Success );
smg_comms_packing... 89 if (not Success) or (Decr_Msg /= Msg) then
smg_comms_packing... 90 Put_Line("FAIL: pack/unpack with RSA.");
smg_comms_packing... 91 else
smg_comms_packing... 92 Put_Line("PASS: pack/unpack with RSA.");
smg_comms_packing... 93 end if;
smg_comms_packing... 94
smg_comms_packing... 95
smg_comms_packing... 96 Pkt(Pkt'First + 3) := Pkt(Pkt'First + 3) and 16#AB#;
smg_comms_packing... 97 Decr_Msg := (others => 0);
smg_comms_packing... 98 Decr_Msg := Packing.Unpack( Pkt, SKey, Success );
smg_comms_packing... 99 if Success then
smg_comms_packing... 100 Put_Line("FAIL: pack/unpack with RSA on mangled package.");
smg_comms_packing... 101 else
smg_comms_packing... 102 Put_Line("PASS: pack/unpack with RSA on mangled package.");
smg_comms_packing... 103 end if;
smg_comms_packing... 104
smg_comms_packing... 105 end Test_Pack_Unpack_RSA;
smg_comms_packing... 106
smg_comms_packing... 107 end Test_Packing;