raw
smg_comms_packing...    1 -- Tests for SMG_Comms.Packing
smg_comms_packing... 2 -- S.MG, 2018
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 -- check result of pack
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 -- iterate, re-packing as "message" the previous package
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 -- initialize with RSA pair previously generated
smg_comms_shorter_e 77 IO_RSA.ReadRSAKey( "keys_rsa.txt", Raw_Types.RSA_KEY_OCTETS,
smg_comms_shorter_e 78 Raw_Types.RSA_KEY_OCTETS*2, SKey );
smg_comms_io_rsa_... 79
smg_comms_packing... 80 -- copy n and e for public key
smg_comms_packing... 81 PKey.n := SKey.n;
smg_comms_packing... 82 PKey.e := SKey.e;
smg_comms_packing... 83 -- get random data for "message"
smg_comms_packing... 84 RNG.Get_Octets(Msg);
smg_comms_packing... 85
smg_comms_packing... 86 -- pack the message into corresponding packet
smg_comms_packing... 87 Pkt := Packing.Pack( Msg, PKey );
smg_comms_packing... 88 -- unpack and check the result
smg_comms_packing... 89 Decr_Msg := Packing.Unpack( Pkt, SKey, Success );
smg_comms_packing... 90 if (not Success) or (Decr_Msg /= Msg) then
smg_comms_packing... 91 Put_Line("FAIL: pack/unpack with RSA.");
smg_comms_packing... 92 else
smg_comms_packing... 93 Put_Line("PASS: pack/unpack with RSA.");
smg_comms_packing... 94 end if;
smg_comms_packing... 95
smg_comms_packing... 96 -- try to unpack a mangled package
smg_comms_files 97 Pkt(Pkt'First + 3) := Pkt(Pkt'First + 3) + 1;
smg_comms_packing... 98 Decr_Msg := (others => 0);
smg_comms_packing... 99 Decr_Msg := Packing.Unpack( Pkt, SKey, Success );
smg_comms_packing... 100 if Success then
smg_comms_packing... 101 Put_Line("FAIL: pack/unpack with RSA on mangled package.");
smg_comms_packing... 102 else
smg_comms_packing... 103 Put_Line("PASS: pack/unpack with RSA on mangled package.");
smg_comms_packing... 104 end if;
smg_comms_packing... 105
smg_comms_packing... 106 end Test_Pack_Unpack_RSA;
smg_comms_packing... 107
smg_comms_packing... 108 end Test_Packing;