ace_pkg.vhd 7.23 KB
Newer Older
lwc-tester committed
1 2 3 4 5 6 7 8 9 10
-- This work is licensed under a Creative Commons
-- Attribution-NonCommercial-ShareAlike 4.0 International License.
-- http://creativecommons.org/licenses/by-nc-sa/4.0

-- Mark D. Aagaard
-- Riham AlTawy
-- Guang Gong
-- Kalikinkar Mandal
-- Raghvendra Rohit
-- Marat Sattarov
lwc-tester committed
11 12
-- Nusa Zidaric
-- http://uwaterloo.ca/communications-security-lab/lwc/ace
lwc-tester committed
13 14 15 16 17


-- This is a human-readable summary of (and not a substitute for) the license. 
-- You are free to:

lwc-tester committed
18 19
--     Share:  copy and redistribute the material in any medium or format
--     Adapt:  remix, transform, and build upon the material
lwc-tester committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

--     The licensor cannot revoke these freedoms as long as you follow
--     the license terms.

-- Under the following terms:

--     Attribution — You must give appropriate credit, provide a link to
--     the license, and indicate if changes were made. You may do so in
--     any reasonable manner, but not in any way that suggests the
--     licensor endorses you or your use.

--     NonCommercial — You may not use the material for commercial
--     purposes.

--     ShareAlike — If you remix, transform, or build upon the material,
--     you must distribute your contributions under the same license as
--     the original.

--     No additional restrictions — You may not apply legal terms or
--     technological measures that legally restrict others from doing
--     anything the license permits.

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

package ace_pkg is

  --for constants
  constant lfsr_c_sz    :  integer := 7;
  subtype lfsr_c_output is std_logic_vector(0 to lfsr_c_sz+2);

  ------------------------------------------------------------
  
  constant half_word_sz : natural := 32;
  constant word_sz      : natural := 2*half_word_sz;  

  subtype half_word     is std_logic_vector( 0 to half_word_sz - 1 );
  subtype word          is std_logic_vector( 0 to word_sz - 1 );
  type word_vector  	is array( natural range <> ) of word; 
  type half_word_vector is array( natural range <> ) of half_word;

  ------------------------------------------------------------
  -- A, B, C, D, E

  constant state_sz          : natural := 320;
  constant word_max_idx      : natural := state_sz / word_sz - 1;
  constant half_word_max_idx : natural := state_sz / half_word_sz - 1;

  constant key_sz            : natural := 128;
  constant nonce_sz          : natural := 128;

  subtype word_state_ty is word_vector ( 0 to word_max_idx );
  constant a_idx : natural := 0;
  constant b_idx : natural := 1;
  constant c_idx : natural := 2;
  constant d_idx : natural := 3;
  constant e_idx : natural := 4;

  subtype half_word_data is  half_word_vector ( 0 to 1 );

  subtype half_word_state_ty is half_word_vector ( 0 to half_word_max_idx );
  constant a0_idx : natural := 1;
  constant a1_idx : natural := 0;
  constant b0_idx : natural := 3;
  constant b1_idx : natural := 2;
  constant c0_idx : natural := 5;
  constant c1_idx : natural := 4;
  constant d0_idx : natural := 7;
  constant d1_idx : natural := 6;
  constant e0_idx : natural := 9;
  constant e1_idx : natural := 8;
  
  function b2x( b : boolean ) return std_logic;
  function half_words_to_words( st : half_word_state_ty ) return word_state_ty;
  function words_to_half_words( st : word_state_ty ) return half_word_state_ty;

  ------------------------------------------------------------
  -- mode

  subtype mode_ty is std_logic_vector( 1 downto 0 );     -- top lvl input
  constant encrypt_mode : mode_ty := ( 1 => '0', 0 => '0' );
  constant decrypt_mode : mode_ty := ( 1 => '0', 0 => '1' );
  constant absorb_mode  : mode_ty := ( 1 => '1', 0 => '0' );
  constant squeeze_mode : mode_ty := ( 1 => '1', 0 => '1' );

  subtype domsep_ty is std_logic_vector( 1 downto 0 );    -- top lvl input

  -- derived control (from counter and more)
  subtype ace_ctl_ty is std_logic_vector( 7 downto 0 );  
  constant absorb_idx       : natural := 0;
  constant replace_idx      : natural := 1;
  constant output_idx       : natural := 2;
  constant endstep_idx      : natural := 3;
  constant permoff_idx      : natural := 4;
  constant squeeze_idx      : natural := 5;
  constant lfsr_c_reset_idx : natural := 6;
  constant lfsr_c_en_idx    : natural := 7;

  -- extras cntl for load, init, fin, tag, sqeeze
  subtype onehot_ty is std_logic_vector( 3 downto 0);		-- extrs cntl for load, init, fin, tag, sqeeze

  ------------------------------------------------------------
  -- round and step counters

  -- use last bit for end ACE perm - for o_ready
  -- -> i_valid will reset the counter!
  -- counter only runs if msb = 0
  constant bits_counter : natural := 8; 
  subtype count_ty is unsigned( bits_counter - 1 downto 0 );
  
  ----------------------------------------------------------------------
  -- standard vhdl operators

  -- function "sll"( a : half_word; n : natural ) return half_word;
  
  function onehot_rotate (a : onehot_ty) return onehot_ty;
  function vector_to_data ( st : half_word_data ) return word;
  function data_to_vector ( st : word ) return half_word_data;
  ----------------------------------------------------------------------
    
end package;

----------------------------------------------------------------------
--
----------------------------------------------------------------------

package body ace_pkg is

  function onehot_rotate (a : onehot_ty)
    return onehot_ty
  is
    variable z : onehot_ty;
  begin
      z(onehot_ty'high downto 1) := a(onehot_ty'high - 1 downto 0);
      z(0) := a(onehot_ty'high);
    return z;
  end function;

  function b2x( b : boolean ) return std_logic is
  begin
    if b then
      return '1';
    else
      return '0';
    end if;
  end function;  

  ------------------------------------------------------------
  -- standard vhdl operators cast to state
  ------------------------------------------------------------

--  function "sll"( a : half_word; n : natural ) return half_word is
--  begin
--    return half_word( std_logic_vector( a ) sll n );
--  end function;
  
  ------------------------------------------------------------
  -- state functions
  ------------------------------------------------------------

  function half_words_to_words( st : half_word_state_ty )	---- CHECK THIS!!!!!! PLEASE
    return word_state_ty
  is
    variable i : natural;
    variable z : word_state_ty;
  begin
    main_loop : for i in 0 to word_max_idx loop
     z(i)(0 to half_word_sz - 1)  := st(2*i);
     z(i)(half_word_sz to word_sz - 1)  := st(2*i+1);
    end loop;
  return z;
  end function;

  function words_to_half_words( st : word_state_ty )
    return half_word_state_ty
  is
    variable i : natural;
    variable z : half_word_state_ty;
  begin
    main_loop : for i in 0 to word_max_idx loop
      z(2*i)     := st(i)(0 to half_word_sz - 1);
      z(2*i+1) := st(i)(half_word_sz to word_sz - 1);
    end loop;
  return z;
  end function;

  function data_to_vector( st : word )
    return half_word_data
  is
    variable z : half_word_data;
  begin
    z(0) := st(0 to half_word_sz - 1);
    z(1) := st(half_word_sz to word_sz - 1);
    return z;
  end function;


  function vector_to_data( st : half_word_data )
    return word
  is
    variable z : word;
  begin
    z(0 to half_word_sz - 1) := st(0);
    z(half_word_sz to word_sz - 1) := st(1);
    return z;
  end function;


end package body;