raw
keccak                  1  -- S.MG implementation of Keccak-f permutations
keccak 2
keccak 3 -- (Based on The Keccak Reference, Version 3.0, January 14, 2011, by
keccak 4 -- Guido Bertoni, Joan Daemen, Michael Peeters and Gilles Van Assche)
keccak 5
keccak 6 -- S.MG, 2018
keccak 7
vtools_vpatch 8 with Bits; use Bits;
keccak 9 package SMG_Keccak is
keccak 10 pragma Pure(SMG_Keccak); --stateless, no side effects -> can cache calls
keccak 11
keccak 12 --knobs (can change as per keccak design but fixed here for S.MG purposes)--
keccak 13 Keccak_L: constant := 6; --gives keccak z (word) dimension of 2^6=64 and
keccak 14 --therefore keccak function 1600 with current
keccak 15 --constants (5*5*2^6)
keccak 16
keccak 17 Default_Bitrate: constant := 1344; --max bits the sponge can eat/spit without
keccak 18 --needing to scramble the state
keccak 19
keccak 20 --constants: dimensions of keccak state and number of rounds
keccak 21 XY_Length: constant := 5;
keccak 22 Z_Length: constant := 2**Keccak_L;
keccak 23 Width: constant := XY_Length * XY_Length * Z_Length;
keccak 24 N_Rounds: constant := 12 + 2*Keccak_L;
keccak 25
keccak 26 --types
keccak 27 type XYCoord is mod XY_Length;
keccak 28 type ZCoord is mod Z_Length;
keccak 29 type Round_Index is mod N_Rounds;
keccak 30
keccak 31 type ZWord is mod 2**Z_Length; --"lane" in keccak ref
keccak 32 type Plane is array(XYCoord) of ZWord; --a "horizontal slice" of keccak state
keccak 33 type State is array(XYCoord, XYCoord) of ZWord; --the full keccak state
keccak 34
keccak 35 type Round_Constants is array(Round_Index) of ZWord; --magic keccak constants
keccak 36
keccak 37 -- rate can be chosen by caller at each call, between 1 and width of state
keccak 38 -- higher rate means sponge "eats" more bits at a time but has fewer bits in
keccak 39 -- the "secret" part of the state (i.e. lower capacity)
keccak 40 subtype Keccak_Rate is Positive range 1..Width; -- capacity = width - rate
keccak 41
keccak 42 subtype Bitword is Bitstream( 0..Z_Length - 1 ); -- bits of one state "word"
keccak 43
keccak 44 -- type conversions
keccak 45 function BitsToWord( BWord : in Bitword ) return ZWord;
keccak 46 function WordToBits( Word : in ZWord ) return Bitword;
keccak 47
keccak 48 -- flip input octets (i.e. groups of 8 bits)
keccak 49 function FlipOctets( BWord : in Bitword ) return Bitword;
keccak 50
keccak 51 -- public function, the sponge itself
keccak 52 -- Keccak sponge structure using Keccak_Function, Pad and a given bitrate;
keccak 53 -- Input - the stream of bits to hash (the message)
keccak 54 -- Output - a bitstream of desired size for holding output
keccak 55 -- Block_Len - the bitrate to use; this is effectively the block length
keccak 56 -- for splitting Input AND squeezing output between scrambles
keccak 57 procedure Sponge(Input : in Bitstream;
keccak 58 Output : out Bitstream;
keccak 59 Block_Len : in Keccak_Rate := Default_Bitrate );
keccak 60
vdiff_keccak 61 -- state based Sponge
vdiff_keccak 62 type Keccak_Context (Block_Len: Keccak_Rate := Default_Bitrate) is
vdiff_keccak 63 record
vdiff_keccak 64 Internal: State := (others => (others => 0));
vdiff_keccak 65 Block: Bitstream(1..Block_Len) := (others => 0);
vdiff_keccak 66 Pos: Natural;
vdiff_keccak 67 end record;
vdiff_keccak 68
vdiff_keccak 69 procedure KeccakBegin(Ctx: in out Keccak_Context);
vdiff_keccak 70 procedure KeccakHash(Ctx: in out Keccak_Context;
vdiff_keccak 71 Input: Bitstream);
vdiff_keccak 72 procedure KeccakEnd(Ctx: in out Keccak_Context;
vdiff_keccak 73 Output: out Bitstream);
vdiff_keccak 74
keccak 75 private
keccak 76 -- these are internals of the keccak implementation, not meant to be directly
keccak 77 -- accessed/used
keccak 78
keccak 79 -- this will squeeze Block'Length bits out of state S
keccak 80 -- NO scramble of state in here!
keccak 81 -- NB: make SURE that Block'Length is the correct bitrate for this sponge
keccak 82 -- in particular, Block'Length should be a correct bitrate aka LESS than Width
keccak 83 procedure SqueezeBlock( Block: out Bitstream; S: in State);
keccak 84
keccak 85 -- This absorbs into sponge the given block, modifying the state accordingly
keccak 86 -- NO scramble of state in here so make sure the whole Block fits in state!
keccak 87 -- NB: make SURE that Block'Length is *the correct bitrate* for this sponge
keccak 88 -- in particular, Block'Length should be a correct bitrate aka LESS than Width
keccak 89 procedure AbsorbBlock( Block: in Bitstream; S: in out State );
keccak 90
keccak 91 --Keccak magic numbers
keccak 92 RC : constant Round_Constants :=
keccak 93 (
keccak 94 16#0000_0000_0000_0001#,
keccak 95 16#0000_0000_0000_8082#,
keccak 96 16#8000_0000_0000_808A#,
keccak 97 16#8000_0000_8000_8000#,
keccak 98 16#0000_0000_0000_808B#,
keccak 99 16#0000_0000_8000_0001#,
keccak 100 16#8000_0000_8000_8081#,
keccak 101 16#8000_0000_0000_8009#,
keccak 102 16#0000_0000_0000_008A#,
keccak 103 16#0000_0000_0000_0088#,
keccak 104 16#0000_0000_8000_8009#,
keccak 105 16#0000_0000_8000_000A#,
keccak 106 16#0000_0000_8000_808B#,
keccak 107 16#8000_0000_0000_008B#,
keccak 108 16#8000_0000_0000_8089#,
keccak 109 16#8000_0000_0000_8003#,
keccak 110 16#8000_0000_0000_8002#,
keccak 111 16#8000_0000_0000_0080#,
keccak 112 16#0000_0000_0000_800A#,
keccak 113 16#8000_0000_8000_000A#,
keccak 114 16#8000_0000_8000_8081#,
keccak 115 16#8000_0000_0000_8080#,
keccak 116 16#0000_0000_8000_0001#,
keccak 117 16#8000_0000_8000_8008#
keccak 118 );
keccak 119
keccak 120 --gnat-specific methods to have bit-ops for modular types
keccak 121 function Rotate_Left( Value : ZWord;
keccak 122 Amount : Natural)
keccak 123 return ZWord;
keccak 124 pragma Import(Intrinsic, Rotate_Left);
keccak 125
keccak 126 function Shift_Right( Value : ZWord;
keccak 127 Amount : Natural)
keccak 128 return ZWord;
keccak 129 pragma Import(Intrinsic, Shift_Right);
keccak 130
keccak 131 function Shift_Left( Value : ZWord;
keccak 132 Amount : Natural)
keccak 133 return ZWord;
keccak 134 pragma Import(Intrinsic, Shift_Left);
keccak 135
keccak 136 --Keccak transformations of the internal state
keccak 137 function Theta ( Input : in State) return State;
keccak 138 function Rho ( Input : in State) return State;
keccak 139 function Pi ( Input : in State) return State;
keccak 140 function Chi ( Input : in State) return State;
keccak 141 function Iota ( Round_Const : in ZWord; Input : in State) return State;
keccak 142
keccak 143 --Keccak function with block width currently 1600 (Width constant above)
keccak 144 --this simply applies *all* keccak transformations in the correct order, using
keccak 145 -- the keccak magic numbers (round constants) as per keccak reference
keccak 146 function Keccak_Function(Input: in State) return State;
keccak 147
keccak 148 end SMG_Keccak;