-
+ 85D8EE9E1B9365BC2288699F301BF3D7109BDDBDB804F930EE855AAE7674806A57261DD216B816C2B5ECB6C8E8D62111065F6AB4AD9CD95AC8FA419F5FCCBC40
smg_comms/tests/io_rsa.adb
(0 . 0)(1 . 91)
13 -- S.MG, 2018
14
15 with Ada.Sequential_IO;
16 with Raw_Types; use Raw_Types;
17 with Ada.Text_IO; use Ada.Text_IO;
18 with Interfaces; use Interfaces;
19
20 package body IO_RSA is
21
22 procedure ReadRSAKey( Filename: in String; Key: out RSA_OAEP.RSA_skey ) is
23 package Char_IO is new Ada.Sequential_IO(Character);
24 use Char_IO;
25 Full : String(1..RSA_len'Length*2) := (others => '0');
26 Half : String(1..RSA_half'Length*2) := (others => '0');
27 F : Char_IO.File_Type;
28 C : Character;
29 begin
30 Open( File => F, Mode => In_File, Name => Filename );
31
32 -- read n
33 for I in Full'Range loop
34 Read(F, Full(I));
35 end loop;
36 -- read new line character and convert to hex
37 Read(F, C);
38 Hex2Octets(Full, Key.n);
39
40 -- read e
41 for I in Half'Range loop
42 Read(F, Half(I));
43 end loop;
44 -- read new line character and convert to hex
45 Read(F, C);
46 Hex2Octets(Half, Key.e);
47
48 -- read d
49 for I in Full'Range loop
50 Read(F, Full(I));
51 end loop;
52 -- read new line character and convert to hex
53 Read(F, C);
54 Hex2Octets(Full, Key.d);
55
56 -- read p
57 for I in Half'Range loop
58 Read(F, Half(I));
59 end loop;
60 -- read new line character and convert to hex
61 Read(F, C);
62 Hex2Octets(Half, Key.p);
63
64 -- read q
65 for I in Half'Range loop
66 Read(F, Half(I));
67 end loop;
68 -- read new line character and convert to hex
69 Read(F, C);
70 Hex2Octets(Half, Key.q);
71
72 -- read u
73 for I in Half'Range loop
74 Read(F, Half(I));
75 end loop;
76 Hex2Octets(Half, Key.u);
77
78 -- Close file
79 Close( F );
80
81 exception
82 when Char_IO.End_Error =>
83 Put_Line("ReadRSAKey ERROR: Unexpected end of file in " & Filename);
84 when others =>
85 Put_Line("ReadRSAKey ERROR: can not open file " & Filename);
86
87 end ReadRSAKey;
88
89 procedure Hex2Octets( Hex: in String; O: out Raw_Types.Octets ) is
90 S : String := "16#AA#";
91 -- to make sure that input String has EVEN number of chars (ie full octets)
92 H : String(1..Hex'Length+Hex'Length mod 2) := (others=>'0');
93 begin
94 -- first char is 0 if needed to cover full octet...
95 H(H'Length-Hex'Length+1..H'Length) := Hex;
96 O := (others => 0);
97 for I in 0 .. H'Length/2-1 loop
98 S := "16#" & H(H'First + I*2 .. H'First + I*2 + 1) & "#";
99 O(O'Last - H'Length/2 + 1 + I) := Unsigned_8'Value(S);
100 end loop;
101 end Hex2Octets;
102
103 end IO_RSA;