- D8FB2B62B07A71DE0B2269325308207B9B2CFA850195D64339F7D35B33DE572BCC01121CFE6EEC977A2B12097EA34C39F12E27BC651E26E47F5A7DAD74AE27F1
+ 6D2A795035290C00E290240A4D93260ACDC2D7253CE4742BEE41515920FDFB6EE78C4154753EF5F0FFF15DED3D28FC69CA0AA90EF417FAC0E914F3E4D4EB4A10
smg_comms/tests/test_rsa_oaep.adb
(3 . 12)(3 . 12)
200 with Interfaces; use Interfaces;
201 with Interfaces.C; use Interfaces.C;
202 with Ada.Text_IO; use Ada.Text_IO;
203 with Ada.Sequential_IO;
204 with RSA_OAEP; use RSA_OAEP;
205 with OAEP; use OAEP;
206 with Raw_Types; use Raw_Types;
207 with RNG; use RNG;
208 with Keccak; use Keccak;
209 with IO_RSA;
210
211 package body Test_RSA_OAEP is
212
(104 . 7)(104 . 7)
214 skey: RSA_skey;
215 begin
216 -- initialize with RSA pair previously generated
217 ReadRSAKey( "keys_rsa.txt", skey );
218 IO_RSA.ReadRSAKey( "keys_rsa.txt", skey );
219
220 -- copy n and e for public key
221 pkey.n := skey.n;
(140 . 7)(140 . 7)
223 Len : Natural;
224 begin
225 -- initialize with RSA pair previously generated
226 ReadRSAKey( "keys_rsa.txt", skey );
227 IO_RSA.ReadRSAKey( "keys_rsa.txt", skey );
228 -- copy n and e for public key
229 pkey.n := skey.n;
230 pkey.e := skey.e;
(180 . 19)(180 . 6)
232 end test_rsa_oaep;
233
234 -- helper methods
235 procedure Hex2Octets( Hex: in String; O: out Raw_Types.Octets ) is
236 S : String := "16#AA#";
237 -- to make sure that input String has EVEN number of chars (ie full octets)
238 H : String(1..Hex'Length+Hex'Length mod 2) := (others=>'0');
239 begin
240 -- first char is 0 if needed to cover full octet...
241 H(H'Length-Hex'Length+1..H'Length) := Hex;
242 O := (others => 0);
243 for I in 0 .. H'Length/2-1 loop
244 S := "16#" & H(H'First + I*2 .. H'First + I*2 + 1) & "#";
245 O(O'Last - H'Length/2 + 1 + I) := Unsigned_8'Value(S);
246 end loop;
247 end Hex2Octets;
248
249 procedure PrintOctets( O: in Raw_Types.Octets; Title: in String ) is
250 begin
(203 . 71)(190 . 4)
252 New_Line;
253 end PrintOctets;
254
255 procedure ReadRSAKey( Filename: in String; Key: out RSA_OAEP.RSA_skey ) is
256 package Char_IO is new Ada.Sequential_IO(Character);
257 use Char_IO;
258 Full : String(1..RSA_len'Length*2) := (others => '0');
259 Half : String(1..RSA_half'Length*2) := (others => '0');
260 F : Char_IO.File_Type;
261 C : Character;
262 begin
263 Open( File => F, Mode => In_File, Name => Filename );
264
265 -- read n
266 for I in Full'Range loop
267 Read(F, Full(I));
268 end loop;
269 -- read new line character and convert to hex
270 Read(F, C);
271 Hex2Octets(Full, Key.n);
272
273 -- read e
274 for I in Half'Range loop
275 Read(F, Half(I));
276 end loop;
277 -- read new line character and convert to hex
278 Read(F, C);
279 Hex2Octets(Half, Key.e);
280
281 -- read d
282 for I in Full'Range loop
283 Read(F, Full(I));
284 end loop;
285 -- read new line character and convert to hex
286 Read(F, C);
287 Hex2Octets(Full, Key.d);
288
289 -- read p
290 for I in Half'Range loop
291 Read(F, Half(I));
292 end loop;
293 -- read new line character and convert to hex
294 Read(F, C);
295 Hex2Octets(Half, Key.p);
296
297 -- read q
298 for I in Half'Range loop
299 Read(F, Half(I));
300 end loop;
301 -- read new line character and convert to hex
302 Read(F, C);
303 Hex2Octets(Half, Key.q);
304
305 -- read u
306 for I in Half'Range loop
307 Read(F, Half(I));
308 end loop;
309 Hex2Octets(Half, Key.u);
310
311 -- Close file
312 Close( F );
313
314 exception
315 when Char_IO.End_Error =>
316 Put_Line("ReadRSAKey ERROR: Unexpected end of file in " & Filename);
317 when others =>
318 Put_Line("ReadRSAKey ERROR: can not open file " & Filename);
319
320 end ReadRSAKey;
321
322 end Test_RSA_OAEP;