- 87241E219925CCE47CD73892DC728607F14BD01324BA0B08D04DB6F2D7F2469958D4C65A5F3C26F2DB1857841857DCD3204FDC78238CAE3126905BB5A3CECC99
+ 589991A191E8D120F9338FD279017CAD6C04AAACE3045C9FCF337AC156C7DF9AAE8D8D353FA32C08439D005A8CB30D309E957EB26D5A1CB8BEA2D4FADD44C8C6
eucrypt/smg_keccak/smg_keccak.adb
(1 . 4)(1 . 5)
5 -- S.MG, 2018
6 with System; use System; -- for Bit_Order
7
8 package body SMG_Keccak is
9
(64 . 34)(65 . 58)
11 end Sponge;
12
13 -- convert from a bitstream of ZWord size to an actual ZWord number
14 -- first bit of bitstream will be most significant bit of ZWord
15 function BitsToWord( Bits: in Bitword ) return ZWord is
16 W: ZWord;
17 P: Natural;
18 function BitsToWord( BWord: in Bitword ) return ZWord is
19 W : ZWord;
20 Bits: Bitword;
21 begin
22 -- just copy octets if machine is little endian
23 -- flip octets if machine is big endian
24 if Default_Bit_Order = Low_Order_First then
25 Bits := BWord;
26 else
27 Bits := FlipOctets( BWord );
28 end if;
29 -- actual bits to word conversion
30 W := 0;
31 P := 0;
32 -- LSB bit order (inside octet) as per Keccak spec
33 for I in reverse Bitword'Range loop
34 W := W + ZWord( Bits(I) ) * ( 2**P );
35 P := P + 1;
36 W := Shift_Left( W, 1 ) + ZWord( Bits( I ) );
37 end loop;
38 return W;
39 end BitsToWord;
40
41 -- convert from a ZWord (lane of state) to a bitstream of ZWord size
42 -- most significant bit of ZWord will be left most bit of bitstream
43 function WordToBits( Word: in ZWord ) return Bitword is
44 Bits: Bitword := (others => 0);
45 W: ZWord;
46 begin
47 W := Word;
48 for I in reverse Bitword'Range loop
49 for I in Bitword'Range loop
50 Bits( I ) := Bit( W mod 2 );
51 W := W / 2;
52 W := Shift_Right( W, 1 );
53 end loop;
54
55 -- flip octets if machine is big endian
56 if Default_Bit_Order = High_Order_First then
57 Bits := FlipOctets( Bits );
58 end if;
59
60 return Bits;
61 end WordToBits;
62
63 -- flip given octets (i.e. groups of 8 bits)
64 function FlipOctets( BWord : in Bitword ) return Bitword is
65 Bits : Bitword;
66 begin
67 -- copy groups of 8 octets changing their order in the array
68 -- i.e. 1st octet in BWord becomes last octet in Bits and so on
69 for I in 0 .. ( Bitword'Length / 8 - 1 ) loop
70 Bits ( Bits'First + I * 8 .. Bits'First + I * 8 + 7 ) :=
71 BWord( BWord'Last - I * 8 - 7 .. BWord'Last - I * 8);
72 end loop;
73 return Bits;
74 end FlipOctets;
75
76 -- helper procedures for sponge absorb/squeeze
77
78 -- NO scramble here, this will absorb ALL given block, make sure it fits!