raw
eucrypt_ch10_oaep...    1 -- Implementation of TMSR's OAEP with Keccak as hash function
eucrypt_ch10_oaep... 2 --
eucrypt_ch10_oaep... 3 -- S.MG, 2018
eucrypt_ch10_oaep... 4
eucrypt_ch10_oaep... 5 with SMG_Keccak; use SMG_Keccak; -- Keccak is used as hash function
eucrypt_ch10_oaep... 6 with Interfaces; use Interfaces; -- for Unsigned_8 type and bit-level ops
eucrypt_ch10_oaep... 7 with Interfaces.C; use Interfaces.C; -- for interop with C
eucrypt_ch10_oaep... 8
eucrypt_ch10_oaep... 9 package SMG_OAEP is
eucrypt_ch10_oaep... 10 pragma Pure( SMG_OAEP ); -- stateless, no side effects -> can cache calls
eucrypt_ch10_oaep... 11
eucrypt_ch10_oaep... 12 -- fixed length of OAEP block in bits and in octets
eucrypt_ch10_oaep... 13 OAEP_LENGTH_BITS : constant := 4096;
eucrypt_ch10_oaep... 14 OAEP_LENGTH_OCTETS : constant := 512;
eucrypt_ch10_oaep... 15 OAEP_HALF_OCTETS : constant := OAEP_LENGTH_OCTETS / 2;
eucrypt_oaep_fix_... 16 TMSR : constant String := "TMSR-RSA";
eucrypt_ch12_wrap... 17 MAX_LEN_MSG : constant Natural := OAEP_HALF_OCTETS - TMSR'Length - 3;
eucrypt_ch12_wrap... 18 pragma Export( C, MAX_LEN_MSG, "max_len_msg"); -- to be accessed from rsa.c
eucrypt_ch10_oaep... 19
eucrypt_ch10_oaep... 20 -- subtypes used by the OAEP encrypt/decrypt
eucrypt_ch10_oaep... 21 subtype OAEP_Block is String( 1 .. OAEP_LENGTH_OCTETS );
eucrypt_ch10_oaep... 22 subtype OAEP_HALF is String( 1 .. OAEP_HALF_OCTETS );
eucrypt_ch10_oaep... 23
eucrypt_ch12_wrap... 24 -- copy from Ada String to C char array and back, octet by octet
eucrypt_ch12_wrap... 25
eucrypt_ch12_wrap... 26 -- This copies first Len characters from A to the first Len positions in S
eucrypt_ch12_wrap... 27 -- NB: this does NOT allocate /check memory!
eucrypt_ch12_wrap... 28 -- Caller has to ensure that:
eucrypt_ch12_wrap... 29 -- S has space for at least Len characters
eucrypt_ch12_wrap... 30 -- A has at least Len characters
eucrypt_ch12_wrap... 31 procedure Char_Array_To_String( A : in Interfaces.C.char_array;
eucrypt_ch12_wrap... 32 Len : in Natural;
eucrypt_ch12_wrap... 33 S : out String);
eucrypt_ch12_wrap... 34
eucrypt_ch12_wrap... 35 -- This copies first Len characters from S to the first Len positions in A
eucrypt_ch12_wrap... 36 -- NB: there are NO checks or memory allocations here!
eucrypt_ch12_wrap... 37 -- Caller has to make sure that:
eucrypt_ch12_wrap... 38 -- S'Length >= Len
eucrypt_ch12_wrap... 39 -- A has allocated space for at least Len characters
eucrypt_ch12_wrap... 40 procedure String_To_Char_Array( S : in String;
eucrypt_ch12_wrap... 41 Len : in Natural;
eucrypt_ch12_wrap... 42 A : out Interfaces.C.char_array);
eucrypt_ch12_wrap... 43
eucrypt_ch10_oaep... 44 -- padding & formatting of maximum 1960 bits of the given String
eucrypt_ch10_oaep... 45 -- uses TMSR's OAEP schema:
eucrypt_ch10_oaep... 46 -- 1.format M00 as: [random octet][sz1][sz2]"TMSR-RSA"[random]*Message
eucrypt_ch10_oaep... 47 -- where sz1 and sz2 store the length of the message in bits
eucrypt_ch10_oaep... 48 -- the random octets before message are padding to make OAEP_LENGTH_OCTETS
eucrypt_ch10_oaep... 49 -- 2. R = OAEP_HALF_OCTETS random bits
eucrypt_ch10_oaep... 50 -- 3. X = M00 xor hash(R)
eucrypt_ch10_oaep... 51 -- 4. Y = R xor hash(X)
eucrypt_ch10_oaep... 52 -- 5. Result is X || Y
eucrypt_ch10_oaep... 53 -- NB: the Entropy parameter should be random octets from which this method
eucrypt_ch10_oaep... 54 -- will use as many as required for the OAEP encryption of given Msg
eucrypt_oaep_fix_... 55 -- NB: at MOST MAX_LEN_MSG octets of Msg! (Msg at most 1960 bits)
eucrypt_ch12_wrap... 56 procedure OAEP_Encrypt( Msg : in String;
eucrypt_ch12_wrap... 57 Entropy : in OAEP_Block;
eucrypt_ch12_wrap... 58 Output : out OAEP_Block);
eucrypt_ch12_wrap... 59
eucrypt_ch12_wrap... 60
eucrypt_ch12_wrap... 61 -- wrapper of oaep_encrypt for direct use from C
eucrypt_ch12_wrap... 62 -- NB: caller HAS TO provide the length of the Message (parameter LenMsg)
eucrypt_ch12_wrap... 63 -- NB: caller HAS TO provide the length of the Entropy (parameter LenEnt)
eucrypt_ch12_wrap... 64 -- NB: caller HAS TO provide the allocated space for result (LenEncr)
eucrypt_ch12_wrap... 65 -- NB: LenEncr HAS TO be at least OAEP_LENGTH_OCTETS!
eucrypt_ch12_wrap... 66 -- NB: LenEnt HAS TO be at least OAEP_LENGTH_OCTETS or this will FAIL!
eucrypt_ch12_wrap... 67 procedure OAEP_Encrypt_C( Msg : in Interfaces.C.char_array;
eucrypt_ch12_wrap... 68 MsgLen : in Interfaces.C.size_t;
eucrypt_ch12_wrap... 69 Entropy : in Interfaces.C.char_array;
eucrypt_ch12_wrap... 70 EntLen : in Interfaces.C.size_t;
eucrypt_ch12_wrap... 71 Encr : out Interfaces.C.char_array;
eucrypt_ch12_wrap... 72 EncrLen : in Interfaces.C.size_t;
eucrypt_ch12_wrap... 73 Success : out Interfaces.C.Int);
eucrypt_ch12_wrap... 74 pragma Export( C, OAEP_Encrypt_C, "oaep_encrypt_c" );
eucrypt_ch10_oaep... 75
eucrypt_ch10_oaep... 76 -- This is the opposite of OAEP_Encrypt above.
eucrypt_ch10_oaep... 77 -- @param Encr - an OAEP block previously obtained from OAEP_Encrypt
eucrypt_ch10_oaep... 78 -- @param Len - this will hold the length of the obtained message (in bits!)
eucrypt_ch10_oaep... 79 -- @param Output - the first Len octets of this are the recovered message
eucrypt_ch10_oaep... 80 -- @param Success - set to TRUE if message was recovered, false otherwise
eucrypt_ch10_oaep... 81 -- NB: when Success is FALSE, both Len and Output have undefined values
eucrypt_ch10_oaep... 82 procedure OAEP_Decrypt( Encr : in OAEP_Block;
eucrypt_ch10_oaep... 83 Len : out Natural;
eucrypt_ch10_oaep... 84 Output : out OAEP_HALF;
eucrypt_ch10_oaep... 85 Success : out Boolean);
eucrypt_ch10_oaep... 86
eucrypt_ch12_wrap... 87 -- wrapper for use from C
eucrypt_ch12_wrap... 88 procedure oaep_decrypt_c( Encr : in Interfaces.C.Char_Array;
eucrypt_ch12_wrap... 89 EncrLen : in Interfaces.C.Int;
eucrypt_ch12_wrap... 90 Decr : out Interfaces.C.Char_Array;
eucrypt_ch12_wrap... 91 DecrLen : in out Interfaces.C.Int;
eucrypt_ch12_wrap... 92 Success : out Interfaces.C.Int);
eucrypt_ch12_wrap... 93 pragma Export( C, oaep_decrypt_c, "oaep_decrypt_c");
eucrypt_ch12_wrap... 94
eucrypt_ch10_oaep... 95 -- helper method, xor on strings
eucrypt_ch10_oaep... 96 -- NB: only Output'Length bits will be considered from S1 and S2
eucrypt_ch10_oaep... 97 -- NB: caller is responsible for S1 and S2 being long enough!
eucrypt_ch10_oaep... 98 procedure XOR_Strings( S1: in String; S2: in String; Output: out String );
eucrypt_ch10_oaep... 99
eucrypt_ch10_oaep... 100 -- gnat-specific methods for bit-level operations
eucrypt_ch10_oaep... 101 function Shift_Right( Value : Unsigned_8;
eucrypt_ch10_oaep... 102 Amount : Natural )
eucrypt_ch10_oaep... 103 return Unsigned_8;
eucrypt_ch10_oaep... 104 pragma Import(Intrinsic, Shift_Right);
eucrypt_ch10_oaep... 105
eucrypt_ch10_oaep... 106 function Shift_Left( Value : Unsigned_8;
eucrypt_ch10_oaep... 107 Amount : Natural )
eucrypt_ch10_oaep... 108 return Unsigned_8;
eucrypt_ch10_oaep... 109 pragma Import(Intrinsic, Shift_Left);
eucrypt_ch10_oaep... 110
eucrypt_ch16_byte... 111 -- conversions between bytestream and string
eucrypt_ch10_oaep... 112 -- NB: caller has to ensure correct size of output parameter! no checks here.
eucrypt_ch16_byte... 113 procedure ToString( B: in Bytestream; S: out String );
eucrypt_ch16_byte... 114 procedure ToBytestream( S: in String; B: out Bytestream );
eucrypt_ch10_oaep... 115
eucrypt_ch10_oaep... 116 -- public wrapper for Sponge to use String for input/output
eucrypt_ch10_oaep... 117 procedure HashKeccak( Input : in String;
eucrypt_ch10_oaep... 118 Output : out String;
eucrypt_ch16_byte... 119 Block_Len : in Keccak_Rate := Default_Byterate);
eucrypt_ch10_oaep... 120
eucrypt_ch16_byte... 121 -- wrapper for calling Keccak hashing from C, with DEFAULT byterate
eucrypt_ch10_oaep... 122 -- @param Input the input string, as array of characters (C style)
eucrypt_ch12_wrap... 123 -- @param LenIn the length of the input string (as number of OCTETS)
eucrypt_ch12_wrap... 124 -- @param LenOut the desired number of OCTETS to be returned as output
eucrypt_ch12_wrap... 125 -- @param Output array of at least LenOut characters; will contain the hash
eucrypt_ch10_oaep... 126 -- NB: caller HAS TO provide the length of the Input (parameter LenIn)
eucrypt_ch10_oaep... 127 -- NB: caller HAS TO provide the length of the Output (parameter LenOut)
eucrypt_ch12_wrap... 128 procedure Hash( Input : in Interfaces.C.Char_Array;
eucrypt_ch12_wrap... 129 LenIn : in Interfaces.C.size_t;
eucrypt_ch12_wrap... 130 LenOut : in Interfaces.C.size_t;
eucrypt_ch12_wrap... 131 Output : out Interfaces.C.Char_Array);
eucrypt_ch10_oaep... 132 pragma Export( C, Hash, "hash" );
eucrypt_ch10_oaep... 133
eucrypt_ch10_oaep... 134 end SMG_OAEP;