raw
smg_comms_packing...    1   -- Packing/unpacking for Eulora's communication protocol:
smg_comms_packing... 2 -- Serpent Message to/from Serpent Packet
smg_comms_packing... 3 -- RSA Message to/from RSA Packet
smg_comms_packing... 4 -- S.MG, 2018
smg_comms_packing... 5
smg_comms_packing... 6 package body Packing is
smg_comms_packing... 7
smg_comms_packing... 8 -- Packing a Serpent message into Serpent package, using the given key
smg_comms_packing... 9 function Pack( Msg : in Raw_Types.Serpent_Msg;
smg_comms_packing... 10 K : in Serpent.Key )
smg_comms_packing... 11 return Raw_Types.Serpent_Pkt is
smg_comms_packing... 12
smg_comms_packing... 13 -- single Serpent blocks containing plain / encrypted data
smg_comms_packing... 14 Plain : Serpent.Block;
smg_comms_packing... 15 Encr : Serpent.Block;
smg_comms_packing... 16
smg_comms_packing... 17 -- Serpent Key Schedule - needed for direct encr/decr calls
smg_comms_packing... 18 KS : Serpent.Key_Schedule;
smg_comms_packing... 19
smg_comms_packing... 20 -- final resulting Serpent package
smg_comms_packing... 21 Pkt : Raw_Types.Serpent_Pkt := (others => 0);
smg_comms_packing... 22 begin
smg_comms_packing... 23 -- prepare the Serpent key schedule based on given key
smg_comms_packing... 24 Serpent.Prepare_Key( K, KS );
smg_comms_packing... 25
smg_comms_packing... 26 -- encrypt message block by block and copy result in packet
smg_comms_packing... 27 for I in 1 .. S_Blocks loop
smg_comms_packing... 28 -- get current block to encrypt
smg_comms_packing... 29 Plain := Msg( Msg'First + (I-1) * Block_Len ..
smg_comms_packing... 30 Msg'First + I * Block_Len - 1 );
smg_comms_packing... 31 -- encrypt with Serpent
smg_comms_packing... 32 Serpent.Encrypt( KS, Plain, Encr );
smg_comms_packing... 33 -- copy result to output packet
smg_comms_packing... 34 Pkt( Pkt'First + (I-1) * Block_Len ..
smg_comms_packing... 35 Pkt'First + I * Block_Len - 1 )
smg_comms_packing... 36 := Encr;
smg_comms_packing... 37 end loop;
smg_comms_packing... 38
smg_comms_packing... 39 -- return result
smg_comms_packing... 40 return Pkt;
smg_comms_packing... 41 end Pack;
smg_comms_packing... 42
smg_comms_packing... 43 -- Unpacking a Serpent packet into contained message, using the given key
smg_comms_packing... 44 function Unpack( Pkt : in Raw_Types.Serpent_Pkt;
smg_comms_packing... 45 K : in Serpent.Key)
smg_comms_packing... 46 return Raw_Types.Serpent_Msg is
smg_comms_packing... 47 -- single Serpent blocks containing plain / encrypted data
smg_comms_packing... 48 Plain : Serpent.Block;
smg_comms_packing... 49 Encr : Serpent.Block;
smg_comms_packing... 50
smg_comms_packing... 51 -- Serpent Key Schedule - needed for direct encr/decr calls
smg_comms_packing... 52 KS : Serpent.Key_Schedule;
smg_comms_packing... 53
smg_comms_packing... 54 -- the message extracted from the given packet
smg_comms_packing... 55 Msg : Raw_Types.Serpent_Msg := (others => 0);
smg_comms_packing... 56 begin
smg_comms_packing... 57 -- prepare the Serpent key for use
smg_comms_packing... 58 Serpent.Prepare_Key( K, KS );
smg_comms_packing... 59
smg_comms_packing... 60 -- decrypt the Serpent packet block by block
smg_comms_packing... 61 for I in 1 .. S_Blocks loop
smg_comms_packing... 62 -- get current block from input and decrypt
smg_comms_packing... 63 Encr := Pkt( Pkt'First + (I-1) * Block_Len ..
smg_comms_packing... 64 Pkt'First + I * Block_Len - 1 );
smg_comms_packing... 65 Serpent.Decrypt( KS, Encr, Plain );
smg_comms_packing... 66
smg_comms_packing... 67 -- copy result to its correct position in final output
smg_comms_packing... 68 Msg( Msg'First + (I-1) * Block_Len ..
smg_comms_packing... 69 Msg'First + I * Block_Len - 1 )
smg_comms_packing... 70 := Plain;
smg_comms_packing... 71 end loop;
smg_comms_packing... 72
smg_comms_packing... 73 -- return the result - the message content of given package
smg_comms_packing... 74 return Msg;
smg_comms_packing... 75 end Unpack;
smg_comms_packing... 76
smg_comms_packing... 77 end Packing;