raw
eucrypt_ch6_kecca...    1  -- S.MG implementation of Keccak-f permutations
eucrypt_ch6_kecca... 2
eucrypt_ch6_kecca... 3 -- (Based on The Keccak Reference, Version 3.0, January 14, 2011, by
eucrypt_ch6_kecca... 4 -- Guido Bertoni, Joan Daemen, Michael Peeters and Gilles Van Assche)
eucrypt_ch6_kecca... 5
eucrypt_ch6_kecca... 6 -- S.MG, 2018
eucrypt_ch6_kecca... 7
eucrypt_ch6_kecca... 8 package SMG_Keccak is
eucrypt_ch6_kecca... 9 pragma Pure(SMG_Keccak); --stateless, no side effects -> can cache calls
eucrypt_ch6_kecca... 10
eucrypt_ch6_kecca... 11 --knobs (can change as per keccak design but fixed here for S.MG purposes)--
eucrypt_ch6_kecca... 12 Keccak_L: constant := 6; --gives keccak z (word) dimension of 2^6=64 and
eucrypt_ch6_kecca... 13 --therefore keccak function 1600 with current
eucrypt_ch6_kecca... 14 --constants (5*5*2^6)
eucrypt_ch6_kecca... 15
eucrypt_ch6_kecca... 16 --constants: dimensions of keccak state and number of rounds
eucrypt_ch6_kecca... 17 XY_Length: constant := 5;
eucrypt_ch6_kecca... 18 Z_Length: constant := 2**Keccak_L;
eucrypt_ch6_kecca... 19 Width: constant := XY_Length * XY_Length * Z_Length;
eucrypt_ch6_kecca... 20 N_Rounds: constant := 12 + 2*Keccak_L;
eucrypt_ch6_kecca... 21
eucrypt_ch6_kecca... 22 --types
eucrypt_ch6_kecca... 23 type XYCoord is mod XY_Length;
eucrypt_ch6_kecca... 24 type ZCoord is mod Z_Length;
eucrypt_ch6_kecca... 25 type Round_Index is mod N_Rounds;
eucrypt_ch6_kecca... 26
eucrypt_ch6_kecca... 27 type ZWord is mod 2**Z_Length; --"lane" in keccak ref
eucrypt_ch6_kecca... 28 type Plane is array(XYCoord) of ZWord; --a "horizontal slice" of keccak state
eucrypt_ch6_kecca... 29 type State is array(XYCoord, XYCoord) of ZWord; --the full keccak state
eucrypt_ch6_kecca... 30
eucrypt_ch6_kecca... 31 type Round_Constants is array(Round_Index) of ZWord; --magic keccak constants
eucrypt_ch6_kecca... 32
eucrypt_ch6_kecca... 33 private
eucrypt_ch6_kecca... 34 -- these are internals of the keccak implementation, not meant to be directly
eucrypt_ch6_kecca... 35 -- accessed/used
eucrypt_ch6_kecca... 36
eucrypt_ch6_kecca... 37 --Keccak magic numbers
eucrypt_ch6_kecca... 38 RC : constant Round_Constants :=
eucrypt_ch6_kecca... 39 (
eucrypt_ch6_kecca... 40 16#0000_0000_0000_0001#,
eucrypt_ch6_kecca... 41 16#0000_0000_0000_8082#,
eucrypt_ch6_kecca... 42 16#8000_0000_0000_808A#,
eucrypt_ch6_kecca... 43 16#8000_0000_8000_8000#,
eucrypt_ch6_kecca... 44 16#0000_0000_0000_808B#,
eucrypt_ch6_kecca... 45 16#0000_0000_8000_0001#,
eucrypt_ch6_kecca... 46 16#8000_0000_8000_8081#,
eucrypt_ch6_kecca... 47 16#8000_0000_0000_8009#,
eucrypt_ch6_kecca... 48 16#0000_0000_0000_008A#,
eucrypt_ch6_kecca... 49 16#0000_0000_0000_0088#,
eucrypt_ch6_kecca... 50 16#0000_0000_8000_8009#,
eucrypt_ch6_kecca... 51 16#0000_0000_8000_000A#,
eucrypt_ch6_kecca... 52 16#0000_0000_8000_808B#,
eucrypt_ch6_kecca... 53 16#8000_0000_0000_008B#,
eucrypt_ch6_kecca... 54 16#8000_0000_0000_8089#,
eucrypt_ch6_kecca... 55 16#8000_0000_0000_8003#,
eucrypt_ch6_kecca... 56 16#8000_0000_0000_8002#,
eucrypt_ch6_kecca... 57 16#8000_0000_0000_0080#,
eucrypt_ch6_kecca... 58 16#0000_0000_0000_800A#,
eucrypt_ch6_kecca... 59 16#8000_0000_8000_000A#,
eucrypt_ch6_kecca... 60 16#8000_0000_8000_8081#,
eucrypt_ch6_kecca... 61 16#8000_0000_0000_8080#,
eucrypt_ch6_kecca... 62 16#0000_0000_8000_0001#,
eucrypt_ch6_kecca... 63 16#8000_0000_8000_8008#
eucrypt_ch6_kecca... 64 );
eucrypt_ch6_kecca... 65
eucrypt_ch6_kecca... 66 --gnat-specific methods to have bit-ops for modular types
eucrypt_ch6_kecca... 67 function Rotate_Left( Value : ZWord;
eucrypt_ch6_kecca... 68 Amount : Natural)
eucrypt_ch6_kecca... 69 return ZWord;
eucrypt_ch6_kecca... 70 pragma Import(Intrinsic, Rotate_Left);
eucrypt_ch6_kecca... 71
eucrypt_ch6_kecca... 72 function Shift_Right( Value : ZWord;
eucrypt_ch6_kecca... 73 Amount : Natural)
eucrypt_ch6_kecca... 74 return ZWord;
eucrypt_ch6_kecca... 75 pragma Import(Intrinsic, Shift_Right);
eucrypt_ch6_kecca... 76
eucrypt_ch6_kecca... 77 --Keccak permutations
eucrypt_ch6_kecca... 78 function Theta ( Input : in State) return State;
eucrypt_ch6_kecca... 79 function Rho ( Input : in State) return State;
eucrypt_ch6_kecca... 80 function Pi ( Input : in State) return State;
eucrypt_ch6_kecca... 81 function Chi ( Input : in State) return State;
eucrypt_ch6_kecca... 82 function Iota ( Round_Const : in ZWord; Input : in State) return State;
eucrypt_ch6_kecca... 83
eucrypt_ch6_kecca... 84 --Keccak full function with block width currently 1600 (Width constant above)
eucrypt_ch6_kecca... 85 --this simply applies *all* keccak permutations in the correct order and using
eucrypt_ch6_kecca... 86 -- the keccak magic numbers (round constants) as per keccak reference
eucrypt_ch6_kecca... 87 function Keccak_Function(Input: in State) return State;
eucrypt_ch6_kecca... 88
eucrypt_ch6_kecca... 89 end SMG_Keccak;