-- Packing/unpacking for Eulora's communication protocol: -- Serpent Message to/from Serpent Packet -- RSA Message to/from RSA Packet -- S.MG, 2018 package body Packing is -- Packing a Serpent message into Serpent package, using the given key function Pack( Msg : in Raw_Types.Serpent_Msg; K : in Serpent.Key ) return Raw_Types.Serpent_Pkt is -- single Serpent blocks containing plain / encrypted data Plain : Serpent.Block; Encr : Serpent.Block; -- Serpent Key Schedule - needed for direct encr/decr calls KS : Serpent.Key_Schedule; -- final resulting Serpent package Pkt : Raw_Types.Serpent_Pkt := (others => 0); begin -- prepare the Serpent key schedule based on given key Serpent.Prepare_Key( K, KS ); -- encrypt message block by block and copy result in packet for I in 1 .. S_Blocks loop -- get current block to encrypt Plain := Msg( Msg'First + (I-1) * Block_Len .. Msg'First + I * Block_Len - 1 ); -- encrypt with Serpent Serpent.Encrypt( KS, Plain, Encr ); -- copy result to output packet Pkt( Pkt'First + (I-1) * Block_Len .. Pkt'First + I * Block_Len - 1 ) := Encr; end loop; -- return result return Pkt; end Pack; -- Unpacking a Serpent packet into contained message, using the given key function Unpack( Pkt : in Raw_Types.Serpent_Pkt; K : in Serpent.Key) return Raw_Types.Serpent_Msg is -- single Serpent blocks containing plain / encrypted data Plain : Serpent.Block; Encr : Serpent.Block; -- Serpent Key Schedule - needed for direct encr/decr calls KS : Serpent.Key_Schedule; -- the message extracted from the given packet Msg : Raw_Types.Serpent_Msg := (others => 0); begin -- prepare the Serpent key for use Serpent.Prepare_Key( K, KS ); -- decrypt the Serpent packet block by block for I in 1 .. S_Blocks loop -- get current block from input and decrypt Encr := Pkt( Pkt'First + (I-1) * Block_Len .. Pkt'First + I * Block_Len - 1 ); Serpent.Decrypt( KS, Encr, Plain ); -- copy result to its correct position in final output Msg( Msg'First + (I-1) * Block_Len .. Msg'First + I * Block_Len - 1 ) := Plain; end loop; -- return the result - the message content of given package return Msg; end Unpack; end Packing;