ace_tb.vhd 13.5 KB
Newer Older
lwc-tester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
-- 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
-- http://comsec.uwaterloo.ca


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

--     Share — copy and redistribute the material in any medium or format
--     Adapt — remix, transform, and build upon the material

--     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.

use std.textio.all;

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

use work.util_unsynth.all;
use work.ace_unsynth.all;
use work.ace_pkg.all;

--use work.util_estream.all;
--use work.util_unsynth.all;

--use work.wg_gf_pkg.all;
--use work.wg_pkg.all;
--use work.wg_unsynth_pkg.all;
--use work.wg_config_pkg.all;

entity ace_tb is
  generic (
        period            : real    --:= 10.0
  );
end entity;

architecture main of ace_tb is

  constant EDH          : std_logic_vector(0 to 2) := "111"; -- ENCRYPTION / DECRYPTION / HASH: '0' = don't do, '1' = do
  constant stim_file_path   : string := "dp_tv/nist_test.tv";
  constant output_file_path : string := "dp_tv/output.tv";





  constant clk_period   : time := period * 1 ns;
  constant hold         : time := clk_period / 2;
  constant after_reset  : time := 0 * clk_period;        -- delay between reset and loading
  constant load_delay   : time := 0 * clk_period;        -- delay between K, N data
  constant init_delay   : time := 0 * clk_period;        -- delay between load permutation and initialization
  constant procad_delay : time := 0 * clk_period;        -- AD data delay
  constant enc_delay    : time := 0 * clk_period;        -- encryption data delay
  constant dec_delay    : time := 0 * clk_period;        -- decryption data delay
  constant absorb_delay : time := 0 * clk_period;        -- delay between absorb data
  constant mode_delay   : time := 0 * clk_period;        -- delay between two modes

  signal clk            : std_logic := '0';
  signal reset          : std_logic := '0';

  signal i_mode         : mode_ty;
  signal ctl_control    : ace_ctl_ty;
  signal ctl_onehot     : onehot_ty;  

  signal i_const        : lfsr_c_output;
  signal i_dom_sep      : domsep_ty; 
  signal i_valid        : std_logic := '0';
  signal i_data         : word;
  signal ctl_count      : count_ty;
  signal o_valid,
         ctl_valid,
         o_ready,
         i_padding      : std_logic;
  signal o_data         : word;
        
  file output_file      : text open write_mode is output_file_path;

  type tb_state_ty is (tbEncrypt, tbDecrypt, tbTag, tbHash, tbNull);
  signal tb_state : tb_state_ty;

  signal bits_pad       : natural;

begin

  uut : entity work.ace port map
      ( clk       => clk
      , reset     => reset
      , i_mode    => i_mode
      , i_dom_sep => i_dom_sep
      , i_valid   => i_valid
      , i_data    => i_data
      , i_padding => i_padding
      , o_valid   => o_valid
      , o_ready   => o_ready
      , o_data    => o_data
      );

  clk <= not clk after clk_period / 2;

  reading_proc : process
    variable msg  : line;
    variable done : boolean;
  begin

    done := false;
    while not(done) loop
      wait until rising_edge(clk);
        done := (o_valid = '1');
      end loop;

      if tb_state = tbEncrypt then
        write(  msg, "Ciphertext: " & to_hex_string_normal( o_data( 0 to bits_pad-1 ) ) );
      elsif tb_state = tbDecrypt then
        write(  msg, "Plaintext:  " & to_hex_string_normal( o_data( 0 to bits_pad-1 ) ) );
      elsif tb_state = tbTag then
        write(  msg, "Tag:            " & to_hex_string_normal( o_data ) );
      elsif tb_state = tbHash then
        write(  msg, "Hash:       " & to_hex_string_normal( o_data ) );
      else
        write(  msg, "N/A:      " & to_hex_string_normal( o_data ) );
      end if;

    writeline( output_file, msg);

  end process;

  stimulus_proc : process
    variable num_bits   : natural;
    variable k0,k1,n0,
             n1,ad,m,
             m_next,iv  : std_logic_vector(0 to word_sz - 1);
    variable key_stim   : std_logic_vector(0 to key_sz - 1);
    variable nonce_stim : std_logic_vector(0 to nonce_sz  - 1);
    variable done       : boolean;
    variable i, j       : natural;
    variable data_buf   : word;
    variable data128    : std_logic_vector(0 to 127);
    variable tag        : tag_ty;
  begin


    ------------------------- SIGNAL DEFAULTS ----------------------------
    i_padding <= '0';    
    i_data    <= (others => 'X');  
    i_dom_sep <= (others => 'X');
    tb_state  <= tbNull;

    if EDH(0) = '1' then

      ------------------------------------------------------------------------------
      ---------------------------------- Encryption --------------------------------
      ------------------------------------------------------------------------------

      -------------------------------- Reset -------------------------------

      wait for clk_period;
      drive_reset( clk, reset, hold );
      wait until rising_edge(clk);

      -------------------------------- Load --------------------------------

      report( "LOADING PHASE" );
      read_key_nonce( key_stim, nonce_stim, stim_file_path );

      k0 := key_stim(0 to word_sz - 1);
      k1 := key_stim(word_sz to key_sz - 1);
      n0 := nonce_stim(0 to word_sz - 1);
      n1 := nonce_stim(word_sz to key_sz - 1);

      i_mode <= encrypt_mode;


      wait for hold;
      drive_data( clk, k0, hold, i_data, i_valid );        wait for load_delay;
      drive_data( clk, k1, hold, i_data, i_valid );        wait for load_delay;
      drive_data( clk, n0, hold, i_data, i_valid );        wait for load_delay;
      drive_data( clk, n1, hold, i_data, i_valid );

      -------------------------------- LoadPerm ------------------------------
      report( "PERMUTATION AFTER LOAD" );
      wait until o_ready = '1' and rising_edge(clk); 
      wait for init_delay;

      ---------------------------------- Init --------------------------------
      report( "INITIALIZATION PHASE" );

      i_dom_sep <= "00";

      drive_data( clk, k0, hold, i_data, i_valid );
      wait until o_ready = '1' and rising_edge(clk);
      drive_data( clk, k1, hold, i_data, i_valid );
      wait until o_ready = '1' and rising_edge(clk);

      ---------------------------------- ProcAD --------------------------------
      report( "AD PROCESSING PHASE" );

      i_dom_sep <= "01";    
      drive_all(AD_TAG, stim_file_path, hold, procad_delay, false, clk, o_ready, o_data, i_data, i_valid, i_padding, bits_pad);
      
      ---------------------------------- Encrypt ------------------------------
      report( "ENCRYPTION PHASE" );

      tb_state <= tbEncrypt;

      i_dom_sep <= "10";
      drive_all(PLAINTEXT_TAG, stim_file_path, hold, enc_delay, false, clk, o_ready, o_data, i_data, i_valid, i_padding, bits_pad);
  
      ---------------------------------- Final --------------------------------
      report( "FINALIZATION PHASE" );
      i_dom_sep <= "00";

      drive_data( clk, k0, hold, i_data, i_valid );
      wait until o_ready = '1' and rising_edge(clk);
      drive_data( clk, k1, hold, i_data, i_valid );

      ---------------------------------- Tag ----------------------------------
      report( "TAG PHASE" );
      tb_state <= tbTag;
      wait until o_ready = '1' and rising_edge(clk);

    end if;

      ------------------------------------------------------------------------------
      ------------------------------------------------------------------------------
      ------------------------------------------------------------------------------
      ---------------------------------- Decryption --------------------------------
      ------------------------------------------------------------------------------
      ------------------------------------------------------------------------------
      ------------------------------------------------------------------------------

    if EDH(1) = '1' then
      if EDH(0) = '1' then wait for mode_delay; end if;
      ------------------------- SIGNAL DEFAULTS ----------------------------
    
      i_data <= (others => 'X');  
      i_dom_sep   <= (others => 'X');

      ------------------------ READING STIM FILES  -------------------------

      -------------------------------- Reset -------------------------------

      wait for clk_period;
      drive_reset( clk, reset, hold );
      wait until rising_edge(clk);

      -------------------------------- Load --------------------------------

    report( "LOADING PHASE" );
    read_key_nonce( key_stim, nonce_stim, stim_file_path );

    k0 := key_stim(0 to word_sz - 1);
    k1 := key_stim(word_sz to key_sz - 1);
    n0 := nonce_stim(0 to word_sz - 1);
    n1 := nonce_stim(word_sz to key_sz - 1);

    i_mode <= decrypt_mode;

    wait for hold;
    drive_data( clk, k0, hold, i_data, i_valid );        wait for load_delay;
    drive_data( clk, k1, hold, i_data, i_valid );        wait for load_delay;
    drive_data( clk, n0, hold, i_data, i_valid );        wait for load_delay;
    drive_data( clk, n1, hold, i_data, i_valid );


   -------------------------------- LoadPerm ------------------------------
    report( "LOADING PERMUTATION" );    
    wait until o_ready = '1' and rising_edge(clk); 
    wait for init_delay;

    ---------------------------------- Init --------------------------------

    report( "INITIALIZATION PHASE" );

    i_dom_sep <= "00";

    drive_data( clk, k0, hold, i_data, i_valid );
    wait until o_ready = '1' and rising_edge(clk);
    drive_data( clk, k1, hold, i_data, i_valid );
    wait until o_ready = '1' and rising_edge(clk);

   ---------------------------------- ProcAD --------------------------------

    report( "AD PROCESSING PHASE" );

    i_dom_sep <= "01";    
    drive_all(AD_TAG, stim_file_path, hold, procad_delay, false, clk, o_ready, o_data, i_data, i_valid, i_padding, bits_pad);

   
    ---------------------------------- Decrypt ------------------------------

    report( "DECRYPTION PHASE" );

    tb_state <= tbDecrypt;

    i_dom_sep <= "10";
    drive_all(CIPHERTEXT_TAG, stim_file_path, hold, enc_delay, true, clk, o_ready, o_data, i_data, i_valid, i_padding, bits_pad);    

    ---------------------------------- Final --------------------------------

    report( "FINALIZATION PHASE" );
    i_dom_sep <= "00";

    drive_data( clk, k0, hold, i_data, i_valid );
    wait until o_ready = '1' and rising_edge(clk);
    drive_data( clk, k1, hold, i_data, i_valid );

    ---------------------------------- Tag ----------------------------------

    report( "TAG PHASE" );
    tb_state <= tbTag;
    wait until o_ready = '1' and rising_edge(clk);



  end if;
      ------------------------------------------------------------------------------
      ------------------------------------------------------------------------------
      ------------------------------------------------------------------------------
      ------------------------------------ HASH ------------------------------------
      ------------------------------------------------------------------------------
      ------------------------------------------------------------------------------
      ------------------------------------------------------------------------------

  if EDH(2) = '1' then
    if EDH(1) = '1' or EDH(0) = '1' then wait for mode_delay; end if;

    ------------------------- SIGNAL DEFAULTS ----------------------------
    i_data    <= (others => 'X');  
    i_dom_sep <= (others => 'X');

    ------------------------ READING STIM FILES  -------------------------
    iv := x"8040400000000000";

    -------------------------------- Reset -------------------------------
    wait for clk_period;
    drive_reset( clk, reset, hold );
    wait until rising_edge(clk);

    -------------------------------- Load --------------------------------
    report( "LOADING PHASE" );
    i_mode <= absorb_mode;

    wait for hold;
    drive_data( clk, iv, hold, i_data, i_valid );        wait for load_delay;

   -------------------------------- LoadPerm ------------------------------
    report( "LOADING PERMUTATION" );    
    wait until o_ready = '1' and rising_edge(clk); 
    wait for absorb_delay;

    --------------------------------- Absorb -------------------------------

    i_dom_sep <= "00";
    drive_all(PLAINTEXT_TAG, stim_file_path, hold, enc_delay, false, clk, o_ready, o_data, i_data, i_valid, i_padding, bits_pad);

    --------------------------------- Squeeze -------------------------------

    wait for hold;

    tb_state <= tbHash;
    i_mode   <= squeeze_mode;

  end if;

    wait until o_ready = '1' and rising_edge(clk);

    assert false
    report ("SIMULATION IS FINISHED")
    severity failure;
        

  end process;

end architecture main;