raw
smg_comms_skeys_s...    1   -- Tests for the serialization of data structures in SMG Protocol
smg_comms_skeys_s... 2 -- S.MG, 2018
smg_comms_skeys_s... 3
smg_comms_skeys_s... 4 with RNG;
smg_comms_skeys_s... 5 with Data_Structs; use Data_Structs;
smg_comms_skeys_s... 6 with Messages; use Messages;
smg_comms_skeys_s... 7 with Interfaces; use Interfaces;
smg_comms_skeys_s... 8 with System;
smg_comms_skeys_s... 9 with System.Storage_Elements; use System.Storage_Elements;
smg_comms_skeys_s... 10 with Ada.Text_IO; use Ada.Text_IO;
smg_comms_skeys_s... 11
smg_comms_skeys_s... 12 package body Test_Serializing is
smg_comms_skeys_s... 13
smg_comms_skeys_s... 14 procedure Serialize_Keyset_SS is
smg_comms_keymgm 15 Msg : Serpent_Msg;
smg_comms_keymgm 16 RMsg : RSA_Msg;
smg_comms_keymgm 17 KSet : Serpent_Keyset(5);
smg_comms_keymgm 18 LSB : Interfaces.Unsigned_8 := 16#01#;
smg_comms_keymgm 19 MSB : Interfaces.Unsigned_8 := 16#80#;
smg_comms_keymgm 20 LMSB : Interfaces.Unsigned_8 := 16#81#;
smg_comms_keymgm 21 Counter : Interfaces.Unsigned_16 := 101;
smg_comms_keymgm 22 NewSetS : Serpent_Keyset;
smg_comms_keymgm 23 NewSetR : Serpent_Keyset;
smg_comms_keymgm 24 NewCounterR: Interfaces.Unsigned_16:=0;
smg_comms_keymgm 25 NewCounterS: Interfaces.Unsigned_16:=0;
smg_comms_skeys_s... 26 begin
smg_comms_skeys_s... 27 Put_Line("Generating the Serpent Keys...");
smg_comms_skeys_s... 28 -- fill a set of Serpent Keys
smg_comms_skeys_s... 29 for I in 1..KSet.N loop
smg_comms_skeys_s... 30 RNG.Get_Octets(KSet.Keys(Interfaces.Unsigned_8(I)));
smg_comms_skeys_s... 31 end loop;
smg_comms_skeys_s... 32 KSet.Flag := LSB;
smg_comms_skeys_s... 33
smg_comms_keymgm 34 Put_Line("Writing the keys to messages...");
smg_comms_keymgm 35 -- write keyset to serpent & rsa messages
smg_comms_skeys_s... 36 Write_SKeys_SMsg( KSet, Counter, Msg );
smg_comms_keymgm 37 Write_SKeys_RMsg( KSet, Counter, RMsg );
smg_comms_skeys_s... 38
smg_comms_keymgm 39 Put_Line("Reading keys back from messages...");
smg_comms_keymgm 40 -- read keyset from serpent and rsa messages
smg_comms_keymgm 41 Read_SKeys_SMsg( Msg, NewCounterS, NewSetS );
smg_comms_keymgm 42 Read_SKeys_RMsg( RMsg, NewCounterR, NewSetR );
smg_comms_skeys_s... 43
smg_comms_skeys_s... 44 Put_Line("Comparing the keysets...");
smg_comms_skeys_s... 45 -- compare the two keysets
smg_comms_keymgm 46 if NewCounterS /= Counter or NewCounterS /= Counter or
smg_comms_keymgm 47 NewSetS /= KSet or NewSetR /= KSet then
smg_comms_skeys_s... 48 Put_Line("FAIL: keysets are different!");
smg_comms_skeys_s... 49 else
smg_comms_skeys_s... 50 Put_Line("PASS: keysets are the same!");
smg_comms_skeys_s... 51 end if;
smg_comms_skeys_s... 52
smg_comms_skeys_s... 53 Put_Line("Attempting to read from mangled message");
smg_comms_skeys_s... 54 begin
smg_comms_skeys_s... 55 Msg(Msg'First) := Msg(Msg'First)+25;
smg_comms_keymgm 56 Read_SKeys_SMsg( Msg, Counter, NewSetS);
smg_comms_skeys_s... 57 Put_Line("FAIL: read failed to raise invalid message exception!");
smg_comms_skeys_s... 58 exception
smg_comms_skeys_s... 59 when Invalid_Msg =>
smg_comms_skeys_s... 60 Put_Line("PASS: exception correctly raised for invalid message");
smg_comms_skeys_s... 61 end;
smg_comms_skeys_s... 62 end Serialize_Keyset_SS;
smg_comms_skeys_s... 63
smg_comms_keymgm 64 procedure Serialize_Keys_Mgm is
smg_comms_keymgm 65 N_Burnt : Counter_8bits;
smg_comms_keymgm 66 Counter : Interfaces.Unsigned_16 := 16#EDA9#;
smg_comms_keymgm 67 Mgm_S : Keys_Mgm;
smg_comms_keymgm 68 Mgm_R : Keys_Mgm;
smg_comms_keymgm 69 Cnt_S : Interfaces.Unsigned_16:=0;
smg_comms_keymgm 70 Cnt_R : Interfaces.Unsigned_16:=0;
smg_comms_keymgm 71 O1 : Octets_1;
smg_comms_keymgm 72 SMsg : Serpent_Msg;
smg_comms_keymgm 73 RMsg : RSA_Msg;
smg_comms_keymgm 74 begin
smg_comms_keymgm 75 -- fill the struct with random stuff
smg_comms_keymgm 76 RNG.Get_Octets( O1 );
smg_comms_keymgm 77 N_Burnt := Cast(O1);
smg_comms_keymgm 78 declare
smg_comms_keymgm 79 Mgm: Keys_Mgm(N_Burnt);
smg_comms_keymgm 80 begin
smg_comms_keymgm 81 RNG.Get_Octets( O1 );
smg_comms_keymgm 82 Mgm.N_Server := O1(O1'First);
smg_comms_keymgm 83 RNG.Get_Octets( O1 );
smg_comms_keymgm 84 Mgm.N_Client := O1(O1'First);
smg_comms_keymgm 85 RNG.Get_Octets( O1 );
smg_comms_keymgm 86 Mgm.Key_ID := O1(O1'First);
smg_comms_keymgm 87 if N_Burnt > 0 then
smg_comms_keymgm 88 RNG.Get_Octets( Mgm.Burnt );
smg_comms_keymgm 89 end if;
smg_comms_keymgm 90 -- write it to Serpent and RSA messages
smg_comms_keymgm 91 Write_KMgm_SMsg(Mgm, Counter, SMsg);
smg_comms_keymgm 92 Write_KMgm_RMsg(Mgm, Counter, RMsg);
smg_comms_keymgm 93
smg_comms_keymgm 94 -- read it back from Serpent and RSA messages
smg_comms_keymgm 95 Read_KMgm_SMsg( SMsg, Cnt_S, Mgm_S );
smg_comms_keymgm 96 Read_KMgm_RMsg( RMsg, Cnt_R, Mgm_R );
smg_comms_keymgm 97
smg_comms_keymgm 98 -- check results
smg_comms_keymgm 99 if Cnt_S /= Counter or
smg_comms_keymgm 100 Mgm_S.N_Burnt /= Mgm.N_Burnt or
smg_comms_keymgm 101 Mgm_S /= Mgm then
smg_comms_keymgm 102 Put_Line("FAIL: read/write key management struct to S msg.");
smg_comms_keymgm 103 else
smg_comms_keymgm 104 Put_Line("PASS: read/write key management struct to S msg.");
smg_comms_keymgm 105 end if;
smg_comms_keymgm 106
smg_comms_keymgm 107 if Cnt_R /= Counter or
smg_comms_keymgm 108 Mgm_R.N_Burnt /= Mgm.N_Burnt or
smg_comms_keymgm 109 Mgm_R /= Mgm then
smg_comms_keymgm 110 Put_Line("FAIL: read/write key management struct to R msg.");
smg_comms_keymgm 111 Put_Line("Cnt_R is " & Unsigned_16'Image(Cnt_R));
smg_comms_keymgm 112 Put_Line("Counter is " & Unsigned_16'Image(Counter));
smg_comms_keymgm 113 Put_Line("Mgm_R.N_Burnt is " & Counter_8bits'Image(Mgm_R.N_Burnt));
smg_comms_keymgm 114 Put_Line("Mgm.N_Burnt is " & Counter_8bits'Image(Mgm.N_Burnt));
smg_comms_keymgm 115 else
smg_comms_keymgm 116 Put_Line("PASS: read/write key management struct to R msg.");
smg_comms_keymgm 117 end if;
smg_comms_keymgm 118
smg_comms_keymgm 119 -- attempt mangled call - should raise exception
smg_comms_keymgm 120 begin
smg_comms_keymgm 121 SMsg(SMsg'First) := SMsg(SMsg'First) + 1;
smg_comms_keymgm 122 Read_KMgm_SMsg( SMsg, Cnt_S, Mgm_S);
smg_comms_keymgm 123 Put_Line("FAIL: Read_KMgm_SMsg failed to raise exception!");
smg_comms_keymgm 124 exception
smg_comms_keymgm 125 when Invalid_Msg =>
smg_comms_keymgm 126 Put_Line("PASS: Read_KMgm_SMsg correctly raised exception.");
smg_comms_keymgm 127 end;
smg_comms_keymgm 128
smg_comms_keymgm 129 end;
smg_comms_keymgm 130
smg_comms_keymgm 131 end Serialize_Keys_Mgm;
smg_comms_keymgm 132
smg_comms_skeys_s... 133 end Test_Serializing;
smg_comms_skeys_s... 134