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