wage_tb.vhd 12.2 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/wage
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

--     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;
USE STD.textio.all;
USE ieee.std_logic_textio.all;

use work.util_unsynth.all;
use work.wage_unsynth.all;
use work.wage_pkg.all;    


ENTITY wage_tb IS
lwc-tester committed
54 55 56
  generic ( period : real   -- := 10.0
          ; ED     : std_logic_vector(0 to 1) := "11"  -- Encryption | Decryption               
         );
lwc-tester committed
57 58 59 60
END wage_tb; 

ARCHITECTURE main OF wage_tb IS

lwc-tester committed
61
--  constant ED           : std_logic_vector(0 to 1) := "11";  -- Encryption | Decryption
lwc-tester committed
62 63 64 65 66 67 68 69 70

  constant stim_file_path   : string := "dp_tv/nist_test.tv";
  constant output_file_path : string := "dp_tv/output.tv";

  file output_file      : text open write_mode is output_file_path;
  file stim_file        : text;


  constant clk_period   : time  := period * 1 ns;
lwc-tester committed
71
  constant hold         : time  := clk_period / 5;
lwc-tester committed
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
  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 mode_delay   : time  := 0 * clk_period;  -- delay between two modes
  

   --Inputs
  signal clk, reset : std_logic := '0'; 

  
  signal tag_data_2 : std_logic_vector(0 to 127);

  type tb_state_ty is (tbLoad, tbInit, tbProcAD, tbEncrypt, tbDecrypt, tbFinal, tbTag, tbNull);
  signal tb_state_2 : tb_state_ty;   
  signal wage_state_f_2 : state_array;  
  signal wage_state_v_2 : std_logic_vector(N*gf_dim-1 downto 0);
  signal wage_state_a_2 : state_array_temp;
  signal tag_valid  : std_logic := '0';



  signal i_data, o_data : word;
  signal o_valid, i_valid, i_padding, o_ready : std_logic;
lwc-tester committed
98 99 100 101
  signal i_dom_sep     : domsep_ty;
  signal i_dom_sep_buf : domsep_ty;
  signal i_mode        : mode_ty;
  signal i_mode_buf    : mode_ty;
lwc-tester committed
102 103 104 105 106

  signal bits_pad  : natural;

BEGIN  

lwc-tester committed
107 108 109 110
  i_dom_sep_proc : process(i_dom_sep_buf)
  begin
    i_dom_sep <= i_dom_sep_buf after hold;
  end process;
lwc-tester committed
111

lwc-tester committed
112 113 114 115
  i_mode_proc : process(i_mode_buf)
  begin
    i_mode <= i_mode_buf after hold;
  end process;
lwc-tester committed
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

  reading_proc_2 : process
    variable msg    : line;
    variable done, skip_write  : boolean;
    --alias o_data_alias is << signal .wage_dp_tb.uut.o_data : data_io >>;
    variable o_data_var : std_logic_vector(0 to 63);
  begin

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

    o_data_var := o_data;

    if tb_state_2 = tbEncrypt then
      write(  msg, "Ciphertext:      " & to_hex_string_normal( o_data( 0 to bits_pad-1 ) ));
    elsif tb_state_2 = tbDecrypt then
      write(  msg, "Plaintext:       " & to_hex_string_normal( o_data( 0 to bits_pad-1 ) ));
    elsif tb_state_2 = tbTag then
      if tag_valid = '1' then
        write(  msg, "Tag:           " & to_hex_string_normal( tag_data_2 ));
      else
        skip_write := true;
      end if;
    else
      skip_write := true;
    end if;

    if not(skip_write) then writeline( output_file, msg); end if;

  end process;


 
  clk <= not(clk) after clk_period / 2;


   -- Instantiate the Unit Under Test (UUT)
  uut: entity work.wage 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
    );

  stim_proc_2: process
    variable num_bits         : natural;
    variable file_tag         : tag_ty;
    variable key_stim,
             nonce_stim,
             data128          : std_logic_vector(0 to 127);
    variable ad, m, k0,
             k1, data         : std_logic_vector(0 to 63);
    variable tag_vector       : data_io_vector(0 to 8);
    variable kn_load_vector_2 : data_io_vector(0 to 8);

  begin  

    if ED(0) = '1' then
      ------------------------------------------------------------------------------
      ---------------------------------- Encryption --------------------------------
      ------------------------------------------------------------------------------

      tag_data_2 <= (others => 'X');
      i_valid  <= '0';
      i_padding  <= '0';

      tb_state_2 <= tbNull;

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

      wait for clk_period;
      drive_reset( clk, reset, hold );
      wait until rising_edge(clk);
  
      -------------------------------- Load --------------------------------
      report( "LOADING PHASE" );
      tb_state_2 <= tbLoad;

lwc-tester committed
204
      i_dom_sep_buf <= "00";
lwc-tester committed
205 206 207 208 209 210 211 212 213

      read_key_nonce( key_stim, nonce_stim, stim_file_path );
      kn_load_vector_2 := kn_to_data_io_vector (key_stim, nonce_stim);
      k0 := key_stim(0 to 63);
      k1 := key_stim(64 to 127);

      for i in 0 to 8 loop
        data := data_io_to_m(kn_load_vector_2(i));
        drive_data( clk, data, hold, i_data, i_valid );
lwc-tester committed
214
        wait until rising_edge(clk);
lwc-tester committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
      end loop;
     
      -------------------------------- LoadPerm ------------------------------
      report( "PERMUTATION AFTER LOAD" );   
      wait until o_ready = '1' and rising_edge(clk); 
      wait for init_delay;

      ---------------------------------- Init --------------------------------
      report( "INITIALIZATION PHASE" );
      tb_state_2 <= tbInit;

      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" );
      tb_state_2 <= tbProcAD;

lwc-tester committed
235
      i_dom_sep_buf <= "10";
lwc-tester committed
236 237 238 239 240 241 242

      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_2 <= tbEncrypt;

lwc-tester committed
243 244
      i_dom_sep_buf <= "01";
      i_mode_buf <= encrypt_mode;
lwc-tester committed
245 246 247 248 249 250 251

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

      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 );
lwc-tester committed
258
      wait until o_valid = '1' and rising_edge(clk);
lwc-tester committed
259 260 261 262 263

      ---------------------------------- Tag ----------------------------------
      report( "TAG PHASE" );
      tb_state_2 <= tbTag;

lwc-tester committed
264
      --wait for hold;
lwc-tester committed
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
      for i in 0 to 8 loop
        tag_vector(i) := m_to_data_io(o_data);
        report("tag vector was read: " & to_hex_string_normal( o_data ) );
        wait until rising_edge(clk); wait for hold;
      end loop;

      tag_data_2 <= data_io_vector_to_tag(tag_vector);
      tag_valid <= '1';

      wait until rising_edge(clk); wait for hold;
      report("tag is: " & to_hex_string_normal( tag_data_2 ) );
      tag_valid <= '0';

      wait for mode_delay;

    end if;

    if ED(1) = '1' then
      ------------------------------------------------------------------------------
      ---------------------------------- Decryption --------------------------------
      ------------------------------------------------------------------------------
 
      tag_data_2 <= (others => 'X');
      i_valid    <= '0'; 
      i_padding  <= '0';
      tb_state_2 <= tbNull;

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

      wait for clk_period;
      drive_reset( clk, reset, hold );
      wait until rising_edge(clk);
  
      -------------------------------- Load --------------------------------
      report( "LOADING PHASE" );
      tb_state_2 <= tbLoad;

lwc-tester committed
302
      i_dom_sep_buf <= "00";
lwc-tester committed
303 304 305 306 307 308 309 310 311

      read_key_nonce( key_stim, nonce_stim, stim_file_path );
      kn_load_vector_2 := kn_to_data_io_vector (key_stim, nonce_stim);
      k0 := key_stim(0 to 63);
      k1 := key_stim(64 to 127);

      for i in 0 to 8 loop
        data := data_io_to_m(kn_load_vector_2(i));
        drive_data( clk, data, hold, i_data, i_valid );
lwc-tester committed
312
        wait until rising_edge(clk);
lwc-tester committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
      end loop;
     
      -------------------------------- LoadPerm ------------------------------
      report( "PERMUTATION AFTER LOAD" );   
      wait until o_ready = '1' and rising_edge(clk); 
      wait for init_delay;

      ---------------------------------- Init --------------------------------
      report( "INITIALIZATION PHASE" );
      tb_state_2 <= tbInit;

      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" );
      tb_state_2 <= tbProcAD;

lwc-tester committed
333
      i_dom_sep_buf <= "10";
lwc-tester committed
334 335 336 337

      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 ------------------------------
lwc-tester committed
338 339
      report( "DECRYPTION PHASE" );
      tb_state_2 <= tbDecrypt;
lwc-tester committed
340

lwc-tester committed
341 342
      i_dom_sep_buf <= "01";
      i_mode_buf <= decrypt_mode;
lwc-tester committed
343 344 345 346 347 348 349

      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" );
      tb_state_2 <= tbFinal;
  
lwc-tester committed
350
      i_dom_sep_buf <= "00";
lwc-tester committed
351 352 353 354 355

      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 );
lwc-tester committed
356
      wait until o_valid = '1' and rising_edge(clk);
lwc-tester committed
357 358 359 360 361

      ---------------------------------- Tag ----------------------------------
      report( "TAG PHASE" );
      tb_state_2 <= tbTag;

lwc-tester committed
362
      --wait for hold;
lwc-tester committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
      for i in 0 to 8 loop
        tag_vector(i) := m_to_data_io(o_data);
        report("tag vector was read: " & to_hex_string_normal( o_data ) );
        wait until rising_edge(clk); wait for hold;
      end loop;

      tag_data_2 <= data_io_vector_to_tag(tag_vector);
      tag_valid <= '1';

      wait until rising_edge(clk); wait for hold;
      report("tag is: " & to_hex_string_normal( tag_data_2 ) );
      tag_valid <= '0';

      wait for mode_delay;

    end if;

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

  end process;

end;