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