smg_comms_io_rsa_... 1
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_io_rsa_... 10 procedure ReadRSAKey( Filename: in String; Key: out RSA_OAEP.RSA_skey ) is
smg_comms_io_rsa_... 11 package Char_IO is new Ada.Sequential_IO(Character);
smg_comms_io_rsa_... 12 use Char_IO;
smg_comms_io_rsa_... 13 Full : String(1..RSA_len'Length*2) := (others => '0');
smg_comms_io_rsa_... 14 Half : String(1..RSA_half'Length*2) := (others => '0');
smg_comms_io_rsa_... 15 F : Char_IO.File_Type;
smg_comms_io_rsa_... 16 C : Character;
smg_comms_io_rsa_... 17 begin
smg_comms_io_rsa_... 18 Open( File => F, Mode => In_File, Name => Filename );
smg_comms_io_rsa_... 19
smg_comms_io_rsa_... 20
smg_comms_io_rsa_... 21 for I in Full'Range loop
smg_comms_io_rsa_... 22 Read(F, Full(I));
smg_comms_io_rsa_... 23 end loop;
smg_comms_io_rsa_... 24
smg_comms_io_rsa_... 25 Read(F, C);
smg_comms_io_rsa_... 26 Hex2Octets(Full, Key.n);
smg_comms_io_rsa_... 27
smg_comms_io_rsa_... 28
smg_comms_io_rsa_... 29 for I in Half'Range loop
smg_comms_io_rsa_... 30 Read(F, Half(I));
smg_comms_io_rsa_... 31 end loop;
smg_comms_io_rsa_... 32
smg_comms_io_rsa_... 33 Read(F, C);
smg_comms_io_rsa_... 34 Hex2Octets(Half, Key.e);
smg_comms_io_rsa_... 35
smg_comms_io_rsa_... 36
smg_comms_io_rsa_... 37 for I in Full'Range loop
smg_comms_io_rsa_... 38 Read(F, Full(I));
smg_comms_io_rsa_... 39 end loop;
smg_comms_io_rsa_... 40
smg_comms_io_rsa_... 41 Read(F, C);
smg_comms_io_rsa_... 42 Hex2Octets(Full, Key.d);
smg_comms_io_rsa_... 43
smg_comms_io_rsa_... 44
smg_comms_io_rsa_... 45 for I in Half'Range loop
smg_comms_io_rsa_... 46 Read(F, Half(I));
smg_comms_io_rsa_... 47 end loop;
smg_comms_io_rsa_... 48
smg_comms_io_rsa_... 49 Read(F, C);
smg_comms_io_rsa_... 50 Hex2Octets(Half, Key.p);
smg_comms_io_rsa_... 51
smg_comms_io_rsa_... 52
smg_comms_io_rsa_... 53 for I in Half'Range loop
smg_comms_io_rsa_... 54 Read(F, Half(I));
smg_comms_io_rsa_... 55 end loop;
smg_comms_io_rsa_... 56
smg_comms_io_rsa_... 57 Read(F, C);
smg_comms_io_rsa_... 58 Hex2Octets(Half, Key.q);
smg_comms_io_rsa_... 59
smg_comms_io_rsa_... 60
smg_comms_io_rsa_... 61 for I in Half'Range loop
smg_comms_io_rsa_... 62 Read(F, Half(I));
smg_comms_io_rsa_... 63 end loop;
smg_comms_io_rsa_... 64 Hex2Octets(Half, Key.u);
smg_comms_io_rsa_... 65
smg_comms_io_rsa_... 66
smg_comms_io_rsa_... 67 Close( F );
smg_comms_io_rsa_... 68
smg_comms_io_rsa_... 69 exception
smg_comms_io_rsa_... 70 when Char_IO.End_Error =>
smg_comms_io_rsa_... 71 Put_Line("ReadRSAKey ERROR: Unexpected end of file in " & Filename);
smg_comms_io_rsa_... 72 when others =>
smg_comms_io_rsa_... 73 Put_Line("ReadRSAKey ERROR: can not open file " & Filename);
smg_comms_io_rsa_... 74
smg_comms_io_rsa_... 75 end ReadRSAKey;
smg_comms_io_rsa_... 76
smg_comms_io_rsa_... 77 procedure Hex2Octets( Hex: in String; O: out Raw_Types.Octets ) is
smg_comms_io_rsa_... 78 S : String := "16#AA#";
smg_comms_io_rsa_... 79
smg_comms_io_rsa_... 80 H : String(1..Hex'Length+Hex'Length mod 2) := (others=>'0');
smg_comms_io_rsa_... 81 begin
smg_comms_io_rsa_... 82
smg_comms_io_rsa_... 83 H(H'Length-Hex'Length+1..H'Length) := Hex;
smg_comms_io_rsa_... 84 O := (others => 0);
smg_comms_io_rsa_... 85 for I in 0 .. H'Length/2-1 loop
smg_comms_io_rsa_... 86 S := "16#" & H(H'First + I*2 .. H'First + I*2 + 1) & "#";
smg_comms_io_rsa_... 87 O(O'Last - H'Length/2 + 1 + I) := Unsigned_8'Value(S);
smg_comms_io_rsa_... 88 end loop;
smg_comms_io_rsa_... 89 end Hex2Octets;
smg_comms_io_rsa_... 90
smg_comms_io_rsa_... 91 end IO_RSA;