raw
smg_comms_rsa_oaep      1  -- Ada implementation of RSA with OAEP according to TMSR and Eulora spec
smg_comms_rsa_oaep 2 -- Uses:
smg_comms_rsa_oaep 3 -- - Eulora's raw types (Ada, raw_types.ads)
smg_comms_rsa_oaep 4 -- - Keccak hashes (Ada, keccak.ads/adb)
smg_comms_rsa_oaep 5 -- - OAEP schema (Ada, oaep.ads/adb)
smg_comms_rsa_oaep 6 -- - RNG (Ada, rng.ads/adb) for true random padding
smg_comms_rsa_oaep 7 -- - C wrappers lib (C, c_wrappers/) for:
smg_comms_rsa_oaep 8 -- - MPI (C, mpi/)
smg_comms_rsa_oaep 9 -- - RSA (C, rsa/rsa.c)
smg_comms_rsa_oaep 10 --
smg_comms_rsa_oaep 11 -- S.MG, 2018
smg_comms_rsa_oaep 12
smg_comms_rsa_oaep 13 with Raw_Types;
smg_comms_rsa_oaep 14 with Interfaces.C; use Interfaces.C;
smg_comms_rsa_oaep 15
smg_comms_rsa_oaep 16 package RSA_OAEP is
smg_comms_rsa_oaep 17 -- exception for mismatched lengths when converting octets <-> char arrays
smg_comms_rsa_oaep 18 Mismatched_Lengths_Error: exception;
smg_comms_rsa_oaep 19
smg_comms_rsa_oaep 20 -- public RSA key with n,e stored as raw octets
smg_comms_rsa_oaep 21 type RSA_pkey is
smg_comms_rsa_oaep 22 record
smg_comms_rsa_oaep 23 n : Raw_Types.RSA_len; --public modulus
smg_comms_rsa_oaep 24 e : Raw_Types.RSA_half; --public exponent
smg_comms_rsa_oaep 25 end record; --RSA_pkey
smg_comms_rsa_oaep 26
smg_comms_rsa_oaep 27 -- private (secret) RSA key with components stored as raw octets
smg_comms_rsa_oaep 28 type RSA_skey is
smg_comms_rsa_oaep 29 record
smg_comms_rsa_oaep 30 n : Raw_Types.RSA_len; --public modulus
smg_comms_rsa_oaep 31 e : Raw_Types.RSA_half; --public exponent
smg_comms_rsa_oaep 32 d : Raw_Types.RSA_len; --secret exponent e*d=1 mod phi; phi=(p-1)*(q-1)
smg_comms_rsa_oaep 33 p : Raw_Types.RSA_half; --prime p
smg_comms_rsa_oaep 34 q : Raw_Types.RSA_half; --prime q
smg_comms_rsa_oaep 35 u : Raw_Types.RSA_half; --inverse of p mod q; for faster calculations
smg_comms_rsa_oaep 36 end record; --RSA_skey
smg_comms_rsa_oaep 37
smg_comms_rsa_oaep 38 -- Encryption RSA+OAEP (i.e. using public key)
smg_comms_rsa_oaep 39 -- NB: at most OAEP.MAX_LEN_MSG octets from Plain will be encrypted!
smg_comms_rsa_oaep 40 -- Relies directly on:
smg_comms_rsa_oaep 41 -- oaep.adb/ads
smg_comms_rsa_oaep 42 -- c_wrappers.c ( RSA )
smg_comms_rsa_oaep 43 -- rng.adb/ads ( random padding )
smg_comms_rsa_oaep 44 procedure Encrypt( Plain: in Raw_Types.Octets;
smg_comms_rsa_oaep 45 Key : in RSA_pkey;
smg_comms_rsa_oaep 46 Encr : out Raw_Types.RSA_len );
smg_comms_rsa_oaep 47
smg_comms_rsa_oaep 48 -- Decryption RSA+OAEP (i.e. using private/secret key)
smg_comms_rsa_oaep 49 -- The opposite of Encrypt above.
smg_comms_rsa_oaep 50 -- NB: Plain has to have ENOUGH space for result!
smg_comms_rsa_oaep 51 -- Result can be at most OAEP.MAX_LEN_MSG octets.
smg_comms_rsa_oaep 52 -- Relies directly on:
smg_comms_rsa_oaep 53 -- oaep.adb/ads
smg_comms_rsa_oaep 54 -- c_wrappers.c (RSA)
smg_comms_rsa_oaep 55 -- Plain_Len gives the length in OCTETS of the decrypted message.
smg_comms_rsa_oaep 56 procedure Decrypt( Encr : in Raw_Types.RSA_len;
smg_comms_rsa_oaep 57 Key : in RSA_skey;
smg_comms_rsa_oaep 58 Plain : out Raw_Types.Octets;
smg_comms_rsa_oaep 59 Plain_Len : out Natural;
smg_comms_rsa_oaep 60 Success : out Boolean);
smg_comms_rsa_oaep 61
smg_comms_rsa_oaep 62 --helper methods:
smg_comms_rsa_oaep 63 -- mainly conversions to/from C's char* and imports from C_wrappers
smg_comms_rsa_oaep 64 -- encrypt with public RSA key given as struct, Ada style
smg_comms_rsa_oaep 65 -- NB: result is potentially 0-led (i.e. at end of Encr not at start!)
smg_comms_rsa_oaep 66 procedure Public_RSA( Plain: in Raw_Types.Octets;
smg_comms_rsa_oaep 67 Key : in RSA_pkey;
smg_comms_rsa_oaep 68 Encr : out Raw_Types.RSA_len);
smg_comms_rsa_oaep 69
smg_comms_rsa_oaep 70 -- encrypt with public RSA key given as char arrays, via C_Wrappers
smg_comms_rsa_oaep 71 -- this returns the length of result because C functions trim leading 0s!
smg_comms_rsa_oaep 72 function Public_RSA_C( Encr : out Interfaces.C.char_array;
smg_comms_rsa_oaep 73 Encr_Len : in Integer;
smg_comms_rsa_oaep 74 Plain : in Interfaces.C.char_array;
smg_comms_rsa_oaep 75 Plain_Len : in Integer;
smg_comms_rsa_oaep 76 RSA_N : in Interfaces.C.char_array;
smg_comms_rsa_oaep 77 N_Len : in Integer;
smg_comms_rsa_oaep 78 RSA_E : in Interfaces.C.char_array;
smg_comms_rsa_oaep 79 E_Len : in Integer)
smg_comms_rsa_oaep 80 return Integer;
smg_comms_rsa_oaep 81 pragma Import(C, Public_RSA_C, "public_rsa_octets");
smg_comms_rsa_oaep 82
smg_comms_rsa_oaep 83 -- decrypt with private RSA key given as struct, Ada style
smg_comms_rsa_oaep 84 -- NB: Plain has to have ENOUGH space!
smg_comms_rsa_oaep 85 -- NB: Result is potentially 0-led (i.e. at the end of Plain, not at start!)
smg_comms_rsa_oaep 86 -- @return actual length of result
smg_comms_rsa_oaep 87 procedure Private_RSA( Encr : in Raw_Types.RSA_len;
smg_comms_rsa_oaep 88 Key : in RSA_skey;
smg_comms_rsa_oaep 89 Plain : out Raw_Types.Octets);
smg_comms_rsa_oaep 90
smg_comms_rsa_oaep 91 -- encrypt with private/secret RSA key given as char arrays (via C_wrappers)
smg_comms_rsa_oaep 92 -- this returns length because C methods trim leading 0s
smg_comms_rsa_oaep 93 function Private_RSA_C( Plain : out Interfaces.C.char_array;
smg_comms_rsa_oaep 94 Plain_Len : in Integer;
smg_comms_rsa_oaep 95 Encr : in Interfaces.C.char_array;
smg_comms_rsa_oaep 96 Encr_Len : in Integer;
smg_comms_rsa_oaep 97 RSA_N : in Interfaces.C.char_array;
smg_comms_rsa_oaep 98 N_Len : in Integer;
smg_comms_rsa_oaep 99 RSA_E : in Interfaces.C.char_array;
smg_comms_rsa_oaep 100 E_Len : in Integer;
smg_comms_rsa_oaep 101 RSA_D : in Interfaces.C.char_array;
smg_comms_rsa_oaep 102 D_Len : in Integer;
smg_comms_rsa_oaep 103 RSA_P : in Interfaces.C.char_array;
smg_comms_rsa_oaep 104 P_Len : in Integer;
smg_comms_rsa_oaep 105 RSA_Q : in Interfaces.C.char_array;
smg_comms_rsa_oaep 106 Q_Len : in Integer;
smg_comms_rsa_oaep 107 RSA_U : in Interfaces.C.char_array;
smg_comms_rsa_oaep 108 U_Len : in Integer)
smg_comms_rsa_oaep 109 return Integer;
smg_comms_rsa_oaep 110 pragma Import( C, Private_RSA_C, "private_rsa_octets" );
smg_comms_rsa_oaep 111
smg_comms_rsa_oaep 112 -- convert from Ada's Octets (array of octets) to C's char* (char_array)
smg_comms_rsa_oaep 113
smg_comms_rsa_oaep 114 -- This copies the octets from O to the beginning of A
smg_comms_rsa_oaep 115 -- NB: there are NO checks or memory allocations here!
smg_comms_rsa_oaep 116 -- Caller has to make sure that:
smg_comms_rsa_oaep 117 -- A has allocated space for at least O'Length octets!
smg_comms_rsa_oaep 118 procedure Octets_To_Char_Array( O : in Raw_Types.Octets;
smg_comms_rsa_oaep 119 A : out Interfaces.C.char_array);
smg_comms_rsa_oaep 120
smg_comms_rsa_oaep 121
smg_comms_rsa_oaep 122 -- This copies first O'Length characters from A to O
smg_comms_rsa_oaep 123 -- NB: this does NOT allocate /check memory!
smg_comms_rsa_oaep 124 -- Caller has to ensure that:
smg_comms_rsa_oaep 125 -- A has space at least O'Length characters
smg_comms_rsa_oaep 126 procedure Char_Array_To_Octets( A : in Interfaces.C.char_array;
smg_comms_rsa_oaep 127 O : out Raw_Types.Octets);
smg_comms_rsa_oaep 128
smg_comms_rsa_oaep 129 end RSA_OAEP;