ace_tb.vhd 14.1 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

--     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
lwc-tester committed
62 63 64
  generic ( period : real    --:= 10.0
          ; EDH    : std_logic_vector(0 to 2) := "111"  -- ENCRYPTION, DECRYPTION, HASH bits. example: 'xx0' = don't do HASH, 'xx1' = do HASH
         );
lwc-tester committed
65 66 67 68
end entity;

architecture main of ace_tb is

lwc-tester committed
69
--constant EDH          : std_logic_vector(0 to 2) := "100"; -- ENCRYPTION, DECRYPTION, HASH bits. example: 'xx0' = don't do HASH, 'xx1' = do HASH
lwc-tester committed
70 71 72 73 74 75 76 77
  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;
lwc-tester committed
78
  constant hold         : time := clk_period / 10;
lwc-tester committed
79 80 81 82 83 84 85 86 87 88 89 90 91
  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;
lwc-tester committed
92
  signal i_mode_buf     : mode_ty;
lwc-tester committed
93 94 95 96 97
  signal ctl_control    : ace_ctl_ty;
  signal ctl_onehot     : onehot_ty;  

  signal i_const        : lfsr_c_output;
  signal i_dom_sep      : domsep_ty; 
lwc-tester committed
98
  signal i_dom_sep_buf  : domsep_ty; 
lwc-tester committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  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

lwc-tester committed
117 118 119 120 121 122 123 124 125 126
  i_dom_sep_proc : process(i_dom_sep_buf)
  begin
    i_dom_sep <= i_dom_sep_buf after hold;
  end process;

  i_mode_proc : process(i_mode_buf)
  begin
    i_mode <= i_mode_buf after hold;
  end process;

lwc-tester committed
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
  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');  
lwc-tester committed
187
    i_dom_sep_buf <= (others => 'X');
lwc-tester committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    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);

lwc-tester committed
212
      i_mode_buf <= encrypt_mode;
lwc-tester committed
213 214


lwc-tester committed
215
      
lwc-tester committed
216 217 218 219 220 221 222 223 224 225 226 227 228
      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" );

lwc-tester committed
229
      i_dom_sep_buf <= "00";
lwc-tester committed
230

lwc-tester committed
231
      
lwc-tester committed
232 233
      drive_data( clk, k0, hold, i_data, i_valid );
      wait until o_ready = '1' and rising_edge(clk);
lwc-tester committed
234
      
lwc-tester committed
235 236 237 238 239 240
      drive_data( clk, k1, hold, i_data, i_valid );
      wait until o_ready = '1' and rising_edge(clk);

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

lwc-tester committed
241 242
      i_dom_sep_buf <= "01";
          
lwc-tester committed
243 244 245 246 247 248 249
      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;

lwc-tester committed
250 251
      i_dom_sep_buf <= "10";
      
lwc-tester committed
252 253 254 255
      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" );
lwc-tester committed
256
      i_dom_sep_buf <= "00";
lwc-tester committed
257

lwc-tester committed
258
      
lwc-tester committed
259 260
      drive_data( clk, k0, hold, i_data, i_valid );
      wait until o_ready = '1' and rising_edge(clk);
lwc-tester committed
261
      
lwc-tester committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
      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');  
lwc-tester committed
284
      i_dom_sep_buf   <= (others => 'X');
lwc-tester committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

      ------------------------ 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);

lwc-tester committed
304
    i_mode_buf <= decrypt_mode;
lwc-tester committed
305

lwc-tester committed
306
    
lwc-tester committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    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" );

lwc-tester committed
322
    i_dom_sep_buf <= "00";
lwc-tester committed
323

lwc-tester committed
324
    
lwc-tester committed
325 326
    drive_data( clk, k0, hold, i_data, i_valid );
    wait until o_ready = '1' and rising_edge(clk);
lwc-tester committed
327
    
lwc-tester committed
328 329 330 331 332 333 334
    drive_data( clk, k1, hold, i_data, i_valid );
    wait until o_ready = '1' and rising_edge(clk);

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

    report( "AD PROCESSING PHASE" );

lwc-tester committed
335 336
    i_dom_sep_buf <= "01";
        
lwc-tester committed
337 338 339 340 341 342 343 344 345
    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;

lwc-tester committed
346 347
    i_dom_sep_buf <= "10";
    
lwc-tester committed
348 349 350 351 352
    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" );
lwc-tester committed
353
    i_dom_sep_buf <= "00";
lwc-tester committed
354

lwc-tester committed
355
    
lwc-tester committed
356 357
    drive_data( clk, k0, hold, i_data, i_valid );
    wait until o_ready = '1' and rising_edge(clk);
lwc-tester committed
358
    
lwc-tester committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    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');  
lwc-tester committed
383
    i_dom_sep_buf <= (others => 'X');
lwc-tester committed
384 385 386 387 388 389 390 391 392 393 394

    ------------------------ 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" );
lwc-tester committed
395
    i_mode_buf <= absorb_mode;
lwc-tester committed
396 397 398 399 400 401 402 403 404 405

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

lwc-tester committed
406 407
    i_dom_sep_buf <= "00";
    report( "HASH DRIVE ALL" );
lwc-tester committed
408 409
    drive_all(PLAINTEXT_TAG, stim_file_path, hold, enc_delay, false, clk, o_ready, o_data, i_data, i_valid, i_padding, bits_pad);

lwc-tester committed
410

lwc-tester committed
411 412
    --------------------------------- Squeeze -------------------------------

lwc-tester committed
413
    
lwc-tester committed
414 415

    tb_state <= tbHash;
lwc-tester committed
416
    i_mode_buf   <= squeeze_mode;
lwc-tester committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

  end if;

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

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

  end process;

end architecture main;