- 6F76C1A956F79131D310EC0AC023F10FEBD27376C54D01332EE47B623904816F6469E483B575B86A8F0B568EE224E9812A8FF3D665D891512D2C8BFA3124BAC4
+ FA02CB033AD63404D8E721C3EEABCF7775E42B1553CFD3E07D6F91FC67E307AC2DD754FB4009F56CE7C38B62AA32564EC309DFD69C4ECF09CF8DB46742390C00
eucrypt/smg_keccak/smg_oaep.adb
(2 . 6)(2 . 37)
215
216 package body SMG_OAEP is
217
218 -- This copies first Len characters from A to the first Len positions in S
219 -- NB: this does NOT allocate /check memory!
220 -- Caller has to ensure that:
221 -- S has space for at least Len characters
222 -- A has at least Len characters
223 procedure Char_Array_To_String( A : in Interfaces.C.char_array;
224 Len : in Natural;
225 S : out String) is
226 begin
227 for Index in 0 .. Len - 1 loop
228 S( S'First + Index ) := Character( A( Interfaces.C.size_t( Index )));
229 end loop;
230 end Char_Array_To_String;
231
232 -- This copies first Len characters from S to the first Len positions in A
233 -- NB: there are NO checks or memory allocations here!
234 -- Caller has to make sure that:
235 -- S'Length >= Len
236 -- A has allocated space for at least Len characters
237 procedure String_To_Char_Array( S : in String;
238 Len : in Natural;
239 A : out Interfaces.C.char_array) is
240 C : Character;
241 begin
242 for Index in 0 .. Len - 1 loop
243 C := S( S'First + Index );
244 A( Interfaces.C.size_t( Index )) := Interfaces.C.Char( C );
245 end loop;
246 end String_To_Char_Array;
247
248
249 procedure HashKeccak( Input : in String;
250 Output : out String;
251 Block_Len : in Keccak_Rate := Default_Bitrate) is
(13 . 23)(44 . 21)
253 ToString( BOut, Output );
254 end HashKeccak;
255
256 function Hash( Input : Interfaces.C.Char_Array;
257 LenIn : Interfaces.C.size_t;
258 LenOut : Interfaces.C.size_t;
259 Block_Len : Interfaces.C.int := Default_Bitrate)
260 return Interfaces.C.Char_Array is
261 AdaLenIn : Natural := Natural(LenIn);
262 AdaLenOut : Natural := Natural(LenOut);
263 InStr : String( 0 .. AdaLenIn-1 ) := (others => '0');
264 OutStr : String( 0 .. AdaLenOut-1 ) := (others => '0');
265 COut : Interfaces.C.Char_Array( 0 .. LenOut-1 );
266 Count : Natural := AdaLenOut;
267 CCount : Interfaces.C.size_t := LenOut;
268 begin
269 Interfaces.C.To_Ada( Input, InStr, AdaLenIn );
270 HashKeccak( InStr, OutStr, Keccak_Rate(Block_Len) );
271 Interfaces.C.To_C( OutStr, COut, CCount );
272 return COut;
273 procedure Hash( Input : in Interfaces.C.Char_Array;
274 LenIn : in Interfaces.C.size_t;
275 LenOut : in Interfaces.C.size_t;
276 Output : out Interfaces.C.Char_Array) is
277 AdaLenIn : Natural := Natural( LenIn );
278 AdaLenOut : Natural := Natural( LenOut );
279 InStr : String( 1 .. AdaLenIn ) := (others => '0');
280 OutStr : String( 1 .. AdaLenOut ) := (others => '0');
281 Block_Len : Keccak_Rate := Default_Bitrate;
282 begin
283 -- Interfaces.C.To_Ada( Input, InStr, AdaLenIn );
284 Char_Array_To_String( Input, AdaLenIn, InStr );
285 HashKeccak( InStr, OutStr, Block_Len );
286 String_To_Char_Array( OutStr, AdaLenOut, Output );
287 -- Interfaces.C.To_C( OutStr, COut, CCount );
288 end Hash;
289
290 -- conversion between types
(146 . 6)(175 . 78)
292
293 end OAEP_Encrypt;
294
295 procedure OAEP_Encrypt_C( Msg : in Interfaces.C.char_array;
296 MsgLen : in Interfaces.C.size_t;
297 Entropy : in Interfaces.C.char_array;
298 EntLen : in Interfaces.C.size_t;
299 Encr : out Interfaces.C.char_array;
300 EncrLen : in Interfaces.C.size_t;
301 Success : out Interfaces.C.Int) is
302 AdaMsgLen : Natural := Natural( MsgLen );
303 AdaEntLen : Natural := Natural( EntLen );
304 AdaEncrLen : Natural := Natural( EncrLen );
305 AdaMsg : String( 1 .. AdaMsgLen );
306 AdaEntBlock: OAEP_Block;
307 AdaResult : OAEP_Block := ( others => '0' );
308 begin
309 Success := 0;
310 -- check there is enough entropy and enoug output space, fail otherwise
311 if AdaEntLen /= AdaEntBlock'Length or AdaEncrLen < AdaResult'Length then
312 return;
313 end if;
314 -- translate to Ada
315 --Interfaces.C.To_Ada( Msg, AdaMsg, AdaMsgLen );
316 Char_Array_To_String( Msg, AdaMsgLen, AdaMsg );
317 --Interfaces.C.To_Ada( Entropy, AdaEntropy, AdaEntLen );
318 Char_Array_To_String( Entropy, AdaEntLen, AdaEntBlock );
319
320 -- call the actual oaep encrypt
321 OAEP_Encrypt( AdaMsg, AdaEntBlock, AdaResult );
322
323 -- translate back to C, set success flag and return
324 --Interfaces.C.To_C( AdaResult, CEncr, CEncrLen, False );
325 -- EncrLen has already been tested to be at least AdaResult'Length
326 String_To_Char_Array( AdaResult, AdaEncrLen, Encr );
327 Success := 1;
328
329 end OAEP_Encrypt_C;
330
331 procedure oaep_decrypt_c( Encr : in Interfaces.C.Char_Array;
332 EncrLen : in Interfaces.C.Int;
333 Decr : out Interfaces.C.Char_Array;
334 DecrLen : in out Interfaces.C.Int;
335 Success : out Interfaces.C.Int) is
336 AdaDecr : OAEP_HALF := ( others => '0' );
337 AdaEncr : OAEP_Block:= ( others => '0' );
338 AdaEncrLen : Natural := Natural( EncrLen );
339 AdaDecrLen : Natural := 0;
340 AdaFlag : Boolean;
341 begin
342 -- check and set success flag/exit if needed
343 Success := 0;
344 if EncrLen /= OAEP_Block'Length then
345 return;
346 end if;
347
348 -- translate to Ada: copy octet by octet as C.To_Ada is problematic
349 -- Interfaces.C.To_Ada( Encr, AdaEncr, AdaEncrLen, False );
350 Char_Array_To_String( Encr, AdaEncrLen, AdaEncr );
351
352 -- actual decrypt
353 OAEP_Decrypt( AdaEncr, AdaDecrLen, AdaDecr, AdaFlag );
354
355 -- translate back to C
356 AdaDecrLen := AdaDecrLen / 8; -- from bits to octets
357 if AdaFlag and
358 Natural( DecrLen ) >= AdaDecrLen and
359 AdaDecr'Length >= AdaDecrLen then
360 Success := 1;
361 DecrLen := Interfaces.C.Int( AdaDecrLen );
362 -- Interfaces.C.To_C( AdaDecr, Decr, AdaDecrLen );
363 String_To_Char_Array( AdaDecr, AdaDecrLen, Decr );
364 end if;
365 end oaep_decrypt_c;
366
367 procedure OAEP_Decrypt( Encr : in OAEP_Block;
368 Len : out Natural;
369 Output : out OAEP_HALF;