[Cryptech-Commits] [core/cipher/chacha] 02/02: Debugged pipeline register and state update. All test cases ok.

git at cryptech.is git at cryptech.is
Thu Aug 23 10:58:09 UTC 2018


This is an automated email from the git hooks/post-receive script.

joachim at secworks.se pushed a commit to branch timing_fix
in repository core/cipher/chacha.

commit 702c57e301342005ea6f854fbeeb862d78b75360
Author: Joachim Strömbergson <joachim at secworks.se>
AuthorDate: Thu Aug 23 12:45:23 2018 +0200

    Debugged pipeline register and state update. All test cases ok.
---
 src/rtl/chacha_core.v   | 35 +++++++++++++++++++++++++++--------
 src/rtl/chacha_qr.v     |  4 ++--
 src/tb/tb_chacha_core.v | 28 +++++++++++++++++++---------
 3 files changed, 48 insertions(+), 19 deletions(-)

diff --git a/src/rtl/chacha_core.v b/src/rtl/chacha_core.v
index d68f783..32f37f5 100644
--- a/src/rtl/chacha_core.v
+++ b/src/rtl/chacha_core.v
@@ -80,9 +80,10 @@ module chacha_core(
 
   localparam CTRL_IDLE     = 3'h0;
   localparam CTRL_INIT     = 3'h1;
-  localparam CTRL_ROUNDS   = 3'h2;
-  localparam CTRL_FINALIZE = 3'h3;
-  localparam CTRL_DONE     = 3'h4;
+  localparam CTRL_ROUNDS0  = 3'h2;
+  localparam CTRL_ROUNDS1  = 3'h3;
+  localparam CTRL_FINALIZE = 3'h4;
+  localparam CTRL_DONE     = 3'h5;
 
 
   //----------------------------------------------------------------
@@ -390,7 +391,6 @@ module chacha_core(
 
       for (i = 0 ; i < 16 ; i = i + 1)
         state_new[i] = 32'h0;
-      state_we = 0;
 
       qr0_a = 32'h0;
       qr0_b = 32'h0;
@@ -413,12 +413,10 @@ module chacha_core(
         begin
           for (i = 0 ; i < 16 ; i = i + 1)
             state_new[i] = init_state_word[i];
-          state_we   = 1;
         end // if (init_state)
 
       if (update_state)
         begin
-          state_we = 1;
           case (qr_ctr_reg)
             QR0:
               begin
@@ -621,6 +619,7 @@ module chacha_core(
     begin : chacha_ctrl_fsm
       init_state         = 0;
       update_state       = 0;
+      state_we           = 0;
       update_output      = 0;
       qr_ctr_inc         = 0;
       qr_ctr_rst         = 0;
@@ -651,16 +650,26 @@ module chacha_core(
         CTRL_INIT:
           begin
             init_state      = 1;
+            state_we        = 1;
             qr_ctr_rst      = 1;
             dr_ctr_rst      = 1;
-            chacha_ctrl_new = CTRL_ROUNDS;
+            chacha_ctrl_new = CTRL_ROUNDS0;
             chacha_ctrl_we  = 1;
           end
 
-        CTRL_ROUNDS:
+        CTRL_ROUNDS0:
           begin
+            update_state    = 1;
+            chacha_ctrl_new = CTRL_ROUNDS1;
+            chacha_ctrl_we  = 1;
+          end
+
+        CTRL_ROUNDS1:
+          begin
+            state_we     = 1;
             update_state = 1;
             qr_ctr_inc   = 1;
+
             if (qr_ctr_reg == QR1)
               begin
                 dr_ctr_inc = 1;
@@ -669,6 +678,16 @@ module chacha_core(
                     chacha_ctrl_new = CTRL_FINALIZE;
                     chacha_ctrl_we  = 1;
                   end
+                else
+                  begin
+                    chacha_ctrl_new = CTRL_ROUNDS0;
+                    chacha_ctrl_we  = 1;
+                  end
+              end
+            else
+              begin
+                chacha_ctrl_new = CTRL_ROUNDS0;
+                chacha_ctrl_we  = 1;
               end
           end
 
diff --git a/src/rtl/chacha_qr.v b/src/rtl/chacha_qr.v
index 5189030..ba68d51 100644
--- a/src/rtl/chacha_qr.v
+++ b/src/rtl/chacha_qr.v
@@ -130,7 +130,7 @@ module chacha_qr(
       a0 = a + b;
       a0_new = a0;
 
-      d0 = d ^ a0;
+      d0 = d ^ a0_reg;
       d1 = {d0[15 : 0], d0[31 : 16]};
 
       c0 = c + d1;
@@ -139,7 +139,7 @@ module chacha_qr(
       b0 = b ^ c0;
       b1 = {b0[19 : 0], b0[31 : 20]};
 
-      a1 = a0 + b1;
+      a1 = a0_reg + b1;
 //      a1_new = a1;
 
       d2 = d1 ^ a1;
diff --git a/src/tb/tb_chacha_core.v b/src/tb/tb_chacha_core.v
index 1e729d2..bcd9e0f 100644
--- a/src/tb/tb_chacha_core.v
+++ b/src/tb/tb_chacha_core.v
@@ -162,14 +162,17 @@ module tb_chacha_core();
       if (display_cycle_ctr)
         begin
           $display("cycle = %08x:", cycle_ctr);
-          $display("");
+          $display("-----------------");
         end
 
       // Display FSM control state and QR, DR counters.
       if (display_ctrl_and_ctrs)
         begin
-          $display("chacha_ctrl_reg = %01x", dut.chacha_ctrl_reg);
+          $display("Control and status:");
+          $display("init  = 0x%01x, next  = 0x%01x", dut.init, dut.next);
+          $display("ready = 0x%01x, valid = 0x%01x", dut.ready, dut.data_out_valid);
           $display("qr_ctr_reg = %01x, dr_ctr_reg = %01x", dut.qr_ctr_reg, dut.dr_ctr_reg);
+          $display("chacha_ctrl_reg = %01x", dut.chacha_ctrl_reg);
           $display("");
         end
 
@@ -193,17 +196,24 @@ module tb_chacha_core();
           $display("state13_reg = 0x%08x, state13_new = 0x%08x", dut.state_reg[13], dut.state_new[13]);
           $display("state14_reg = 0x%08x, state14_new = 0x%08x", dut.state_reg[14], dut.state_new[14]);
           $display("state15_reg = 0x%08x, state15_new = 0x%08x", dut.state_reg[15], dut.state_new[15]);
-          $display("state_we    = 0x%01x", dut.state_we);
+          $display("init_state  = 0x%01x, update_state = 0x%01x, state_we = 0x%01x",
+                   dut.init_state, dut.update_state, dut.state_we);
           $display("");
         end
 
       // Display the qround input and outputs.
       if (display_qround)
         begin
-          $display("a      = %08x, b      = %08x, c      = %08x, d      = %08x", dut.qr0_a, dut.qr0_b, dut.qr0_c, dut.qr0_d);
-          $display("qr0_a_prim = %08x, qr0_b_prim = %08x, qr0_c_prim = %08x, qr0_d_prim = %08x", dut.qr0_a_prim, dut.qr0_b_prim, dut.qr0_c_prim, dut.qr0_d_prim);
+          $display("QR0:");
+          $display("a      = 0x%08x, b      = 0x%08x, c      = 0x%08x, d      = 0x%08x",
+                   dut.qr0_a, dut.qr0_b, dut.qr0_c, dut.qr0_d);
+          $display("a_prim = 0x%08x, b_prim = 0x%08x, c_prim = 0x%08x, d_prim = 0x%08x",
+                   dut.qr0_a_prim, dut.qr0_b_prim, dut.qr0_c_prim, dut.qr0_d_prim);
+          $display("a0     = 0x%08x, a1     = 0x%08x", dut.qr0.qr.a0, dut.qr0.qr.a1);
+          $display("a0_reg = 0x%08x, a0_new = 0x%08x", dut.qr0.a0_reg, dut.qr0.a0_new);
           $display("");
         end
+      $display("");
     end // dut_monitor
 
 
@@ -415,21 +425,21 @@ module tb_chacha_core();
       dump_state();
 
       // Wait for valid flag and check results.
-      @(posedge dut.data_out_valid);
+      @(posedge tb_core_data_out_valid);
       dump_state();
 
       if (tb_core_data_out == expected)
         begin
           $display("*** TC %0d-%0d successful", major, minor);
+          $display("Got:      0x%064x", tb_core_data_out);
           $display("");
         end
       else
         begin
           $display("*** ERROR: TC %0d-%0d not successful", major, minor);
-          $display("Expected: 0x%064x", expected);
           $display("Got:      0x%064x", tb_core_data_out);
+          $display("Expected: 0x%064x", expected);
           $display("");
-
           error_ctr = error_ctr + 1;
         end
     end
@@ -510,7 +520,7 @@ module tb_chacha_core();
       $display("   -- Testbench for chacha_core started --");
       $display("");
 
-      set_display_prefs(0, 0, 1, 1, 0);
+      set_display_prefs(1, 1, 1, 1, 1);
       qr_tests();
       init_dut();
       $display("*** State at init:");



More information about the Commits mailing list