- 9650F68C8BFBA99299B059E943008CE5C682B3991E60EA7EE51C22F23373B56A9C1BD1C6459F5D7F8A5553244A4656C357035CF54CAECAC4CBCAB25041B2C011
+ 35A028DE1F46C6B81DB6159697DF6A2CD170ACADF60CBB5CA01880206BF59B34D6F9FE8199B77120FC680D882BCC2DEC0D21EE552FE76AF5686CBC02A418397C
smg_comms/src/packing.adb
(74 . 4)(74 . 86)
55 return Msg;
56 end Unpack;
57
58 -- Packing a RSA message into RSA packet, using the given key
59 function Pack( Msg : in Raw_Types.RSA_Msg;
60 K : in RSA_OAEP.RSA_pkey)
61 return Raw_Types.RSA_Pkt is
62
63 -- a chunk that can be processed via rsa+oaep at any given time
64 Chunk: Raw_Types.Octets(1..Raw_Types.OAEP_MAX_LEN) := (others => 0);
65
66 -- number of chunks in the message to process
67 -- NO incomplete chunks will be processed!
68 -- NB: values are set so that there are no incomplete blocks here
69 N : constant Natural := Msg'Length / Chunk'Length;
70
71 -- intermediate result, as obtained from rsa_oaep
72 Encr: Raw_Types.RSA_len := (others => 0);
73
74 -- final resulting RSA Packet
75 Pkt : Raw_Types.RSA_Pkt := (others => 0);
76 begin
77 -- there should ALWAYS be precisely N chunks in Msg to feed to rsa_oaep
78 -- process chunks of Msg one at a time
79 for I in 1..N loop
80 -- get current chunk
81 Chunk := Msg(Msg'First + (I-1) * Chunk'Length ..
82 Msg'First + I * Chunk'Length - 1 );
83 -- call rsa oaep encrypt on current chunk
84 RSA_OAEP.Encrypt( Chunk, K, Encr );
85 -- copy result to its place in final packet
86 Pkt( Pkt'First + (I-1) * Encr'Length ..
87 Pkt'First + I * Encr'Length - 1 ) := Encr;
88 end loop;
89 -- return final result
90 return Pkt;
91 end Pack;
92
93 -- Unpacking a RSA packet into contained message, using the given key
94 function Unpack( Pkt : in Raw_Types.RSA_Pkt;
95 K : in RSA_OAEP.RSA_skey;
96 Success : out Boolean)
97 return Raw_Types.RSA_Msg is
98 -- a chunk - basically input for RSA_OAEP.Decrypt
99 Chunk : Raw_Types.RSA_len := (others => 0);
100
101 -- intermediate result of rsa_oaep decrypt
102 Decr : Raw_Types.Octets( 1..Raw_Types.OAEP_MAX_LEN ) := (others => 0);
103 Len : Natural;
104 Flag : Boolean;
105
106 -- number of chunks in the packet
107 -- NB: there should be only FULL chunks! otherwise -> fail
108 N : constant Natural := Pkt'Length / Chunk'Length;
109
110 -- final resulting message content of the given RSA packet
111 Msg : Raw_Types.RSA_Msg := (others => 0);
112 begin
113 -- initialize Success flag
114 Success := True;
115
116 -- process given packet, chunk by chunk
117 for I in 1..N loop
118 -- get current chunk
119 Chunk := Pkt( Pkt'First + (I-1) * Chunk'Length ..
120 Pkt'First + I * Chunk'Length - 1 );
121 -- decrypt it via rsa+oaep
122 RSA_OAEP.Decrypt( Chunk, K, Decr, Len, Flag );
123 -- check result and if ok then copy it to final result at its place
124 -- NB: if returned length is EVER less than OAEP_MAX_LEN then -> fail!
125 -- the reason for above: there will be undefined bits in the output!
126 if Len /= Raw_Types.OAEP_MAX_LEN or (not Flag) then
127 Success := False;
128 return Msg;
129 else
130 Msg( Msg'First + (I-1) * Decr'Length ..
131 Msg'First + I * Decr'Length - 1 ) := Decr;
132 end if;
133 end loop;
134
135 -- return obtained message
136 return Msg;
137 end Unpack;
138
139
140 end Packing;