vdiff_keccak 1 package body Keccak_C is
vdiff_keccak 2
vdiff_keccak 3 procedure ToBitstream(S: in String; B: out Bitstream ) is
vdiff_keccak 4 V : Unsigned_8;
vdiff_keccak 5 Pos : Natural;
vdiff_keccak 6 begin
vdiff_keccak 7 Pos := B'First;
vdiff_keccak 8 for C of S loop
vdiff_keccak 9 V := Character'Pos( C );
vdiff_keccak 10 B( Pos ) := Bit( V and 1 );
vdiff_keccak 11 B( Pos + 1 ) := Bit( Shift_Right( V, 1 ) and 1 );
vdiff_keccak 12 B( Pos + 2 ) := Bit( Shift_Right( V, 2 ) and 1 );
vdiff_keccak 13 B( Pos + 3 ) := Bit( Shift_Right( V, 3 ) and 1 );
vdiff_keccak 14 B( Pos + 4 ) := Bit( Shift_Right( V, 4 ) and 1 );
vdiff_keccak 15 B( Pos + 5 ) := Bit( Shift_Right( V, 5 ) and 1 );
vdiff_keccak 16 B( Pos + 6 ) := Bit( Shift_Right( V, 6 ) and 1 );
vdiff_keccak 17 B( Pos + 7 ) := Bit( Shift_Right( V, 7 ) and 1 );
vdiff_keccak 18 Pos := Pos + 8;
vdiff_keccak 19 end loop;
vdiff_keccak 20 end ToBitstream;
vdiff_keccak 21
vdiff_keccak 22 procedure ToString(B: in Bitstream; S: out String ) is
vdiff_keccak 23 N : Natural;
vdiff_keccak 24 Pos : Natural;
vdiff_keccak 25 begin
vdiff_keccak 26 Pos := B'First;
vdiff_keccak 27 for I in S'Range loop
vdiff_keccak 28 N := Natural( B( Pos ) ) +
vdiff_keccak 29 Natural( B( Pos + 1 ) ) * 2 +
vdiff_keccak 30 Natural( B( Pos + 2 ) ) * 4 +
vdiff_keccak 31 Natural( B( Pos + 3 ) ) * 8 +
vdiff_keccak 32 Natural( B( Pos + 4 ) ) * 16 +
vdiff_keccak 33 Natural( B( Pos + 5 ) ) * 32 +
vdiff_keccak 34 Natural( B( Pos + 6 ) ) * 64 +
vdiff_keccak 35 Natural( B( Pos + 7 ) ) * 128;
vdiff_keccak 36 Pos := Pos + 8;
vdiff_keccak 37 S( I ) := Character'Val( N );
vdiff_keccak 38 end loop;
vdiff_keccak 39 end ToString;
vdiff_keccak 40
vdiff_keccak 41
vdiff_keccak 42
vdiff_keccak 43 procedure C_Get_Size(Size: out Interfaces.C.size_t) is
vdiff_keccak 44 begin
vdiff_keccak 45 Size := C_Context'Size / 8;
vdiff_keccak 46 end C_Get_Size;
vdiff_keccak 47
vdiff_keccak 48 function C_Begin return C_Context_Access is
vdiff_keccak 49 Result : C_Context_Access;
vdiff_keccak 50 begin
vdiff_keccak 51 Result := new C_Context;
vdiff_keccak 52 KeccakBegin(Result.all);
vdiff_keccak 53 return Result;
vdiff_keccak 54 end C_Begin;
vdiff_keccak 55
vdiff_keccak 56 procedure C_Hash(Ctx: in C_Context_Access;
vdiff_keccak 57 Input: Interfaces.C.Char_Array;
vdiff_keccak 58 Len: Interfaces.C.Size_T) is
vdiff_keccak 59 L: Natural := Natural(Len);
vdiff_keccak 60 S: String(1..L);
vdiff_keccak 61 B: Bitstream(1..S'Length*8);
vdiff_keccak 62 begin
vdiff_keccak 63 Interfaces.C.To_Ada(Input, S, L, Trim_Nul => False);
vdiff_keccak 64 ToBitstream(S, B);
vdiff_keccak 65 KeccakHash(Ctx.all, B);
vdiff_keccak 66 end C_Hash;
vdiff_keccak 67
vdiff_keccak 68 procedure C_End(Ctx: C_Context_Access;
vdiff_keccak 69 Output: out Interfaces.C.Char_Array;
vdiff_keccak 70 Len: Interfaces.C.Size_T) is
vdiff_keccak 71 L: Natural := Natural(Len);
vdiff_keccak 72 S: String(1..L);
vdiff_keccak 73 B: Bitstream(1..S'Length*8);
vdiff_keccak 74 Count: Interfaces.C.Size_T;
vdiff_keccak 75 begin
vdiff_keccak 76 KeccakEnd(Ctx.all, B);
vdiff_keccak 77 ToString(B, S);
vdiff_keccak 78 Interfaces.C.To_C(S, Output(0..Len), Count, Append_Nul => False);
vdiff_keccak 79
vdiff_keccak 80 end C_End;
vdiff_keccak 81
vdiff_keccak 82 procedure C_Context_Deallocate is new Ada.Unchecked_Deallocation
vdiff_keccak 83 (C_Context, C_Context_Access);
vdiff_keccak 84
vdiff_keccak 85 procedure C_Deallocate(Ctx: in out C_Context_Access) is
vdiff_keccak 86 begin
vdiff_keccak 87 C_Context_Deallocate(Ctx);
vdiff_keccak 88 end C_Deallocate;
vdiff_keccak 89 end Keccak_C;