aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoachim StroĢˆmbergson <joachim@secworks.se>2018-06-29 10:42:29 +0200
committerJoachim StroĢˆmbergson <joachim@secworks.se>2018-06-29 10:42:29 +0200
commitd03e0136f3d48d1c5168921b2997bb629204edf6 (patch)
tree527be491c81324a6f843489dedfeafab1769adbe
parentfd6c91994c051744265730d8e88fb0855c5770fa (diff)
Completed first version of core that should have all functionality needed to do key wrap.
-rw-r--r--src/rtl/keywrap_core.v147
1 files changed, 122 insertions, 25 deletions
diff --git a/src/rtl/keywrap_core.v b/src/rtl/keywrap_core.v
index e3e0723..14768d8 100644
--- a/src/rtl/keywrap_core.v
+++ b/src/rtl/keywrap_core.v
@@ -71,10 +71,18 @@ module keywrap_core (
//----------------------------------------------------------------
localparam OUTER_LOOP_MAX = 6;
- localparam CTRL_IDLE = 3'h0;
- localparam CTRL_INIT = 3'h1;
- localparam CTRL_NEXT = 3'h2;
- localparam CTRL_STEP1 = 3'h3;
+
+ localparam CTRL_IDLE = 4'h0;
+
+ localparam CTRL_INIT_START = 4'h1;
+ localparam CTRL_INIT_WAIT = 4'h2;
+
+ localparam CTRL_NEXT_START = 4'h4;
+ localparam CTRL_NEXT_LOOP = 4'h5;
+ localparam CTRL_NEXT_WAIT = 4'h6;
+ localparam CTRL_NEXT_UPDATE = 4'h7;
+ localparam CTRL_NEXT_CHECK = 4'h8;
+ localparam CTRL_NEXT_FINALIZE = 4'h9;
//----------------------------------------------------------------
@@ -89,6 +97,10 @@ module keywrap_core (
reg ready_new;
reg ready_we;
+ reg valid_reg;
+ reg valid_new;
+ reg valid_we;
+
reg [13 : 0] api_addr_ctr_reg;
reg [13 : 0] api_addr_ctr_new;
reg api_addr_ctr_we;
@@ -109,15 +121,14 @@ module keywrap_core (
reg iteration_ctr_set;
reg iteration_ctr_rst;
- reg [2 : 0] keywrap_core_ctrl_reg;
- reg [2 : 0] keywrap_core_ctrl_new;
+ reg [3 : 0] keywrap_core_ctrl_reg;
+ reg [3 : 0] keywrap_core_ctrl_new;
reg keywrap_core_ctrl_we;
//----------------------------------------------------------------
// Wires.
//----------------------------------------------------------------
- reg aes_encdec;
reg aes_init;
reg aes_next;
wire aes_ready;
@@ -128,7 +139,6 @@ module keywrap_core (
reg update_state;
reg core_we;
- reg [12 : 0] core_addr;
reg [63 : 0] core_wr_data;
wire [63 : 0] core_rd_data;
@@ -145,7 +155,7 @@ module keywrap_core (
.api_rd_data(api_rd_data),
.core_we(core_we),
- .core_addr(core_addr),
+ .core_addr(block_ctr_reg),
.core_wr_data(core_wr_data),
.core_rd_data(core_rd_data)
);
@@ -155,7 +165,7 @@ module keywrap_core (
.clk(clk),
.reset_n(reset_n),
- .encdec(aes_encdec),
+ .encdec(encdec),
.init(aes_init),
.next(aes_next),
@@ -174,6 +184,8 @@ module keywrap_core (
// Assignments for ports.
//----------------------------------------------------------------
assign a_result = a_reg;
+ assign ready = ready_reg;
+ assign valid = valid_reg;
//----------------------------------------------------------------
@@ -185,6 +197,7 @@ module keywrap_core (
begin
a_reg <= 64'h0;
ready_reg <= 1'h0;
+ valid_reg <= 1'h0;
api_addr_ctr_reg <= 14'h0;
block_ctr_reg <= 13'h0;
iteration_ctr_reg <= 3'h0;
@@ -199,6 +212,9 @@ module keywrap_core (
if (ready_we)
ready_reg <= ready_new;
+ if (valid_we)
+ valid_reg <= valid_new;
+
if (api_addr_ctr_we)
api_addr_ctr_reg <= api_addr_ctr_new;
@@ -221,10 +237,11 @@ module keywrap_core (
//----------------------------------------------------------------
always @*
begin : keywrap_dp
- reg xor_val;
+ reg [63 : 0] xor_val;
- a_we = 1'h0;
- a_new = 32'h0;
+ a_new = 64'h0;
+ a_we = 1'h0;
+ core_we = 1'h0;
aes_block = {a_reg, core_rd_data};
core_wr_data = aes_result[63 : 0];
@@ -232,14 +249,15 @@ module keywrap_core (
if (init_a)
begin
- a_we = 1'h1;
a_new = a_init;
+ a_we = 1'h1;
end
if (update_state)
begin
- a_we = 1'h1;
- a_new = aes_result[127 : 64] ^ xor_val;
+ a_new = aes_result[127 : 64] ^ xor_val;
+ a_we = 1'h1;
+ core_we = 1'h1;
end
end
@@ -328,9 +346,12 @@ module keywrap_core (
//----------------------------------------------------------------
always @*
begin : keywrap_core_ctrl
+ ready_new = 1'h0;
+ ready_we = 1'h0;
+ valid_new = 1'h0;
+ valid_we = 1'h0;
init_a = 1'h0;
update_state = 1'h0;
- aes_encdec = 1'h0;
aes_init = 1'h0;
aes_next = 1'h0;
api_addr_ctr_rst = 1'h0;
@@ -344,49 +365,125 @@ module keywrap_core (
keywrap_core_ctrl_new = CTRL_IDLE;
keywrap_core_ctrl_we = 1'h0;
+
+ if (api_cs && api_we)
+ api_addr_ctr_inc = 1'h1;
+
+
case (keywrap_core_ctrl_reg)
CTRL_IDLE:
begin
if (init)
begin
- keywrap_core_ctrl_new = CTRL_INIT;
+ ready_new = 1'h0;
+ ready_we = 1'h1;
+ valid_new = 1'h0;
+ valid_we = 1'h1;
+ keywrap_core_ctrl_new = CTRL_INIT_START;
keywrap_core_ctrl_we = 1'h1;
end
if (next)
begin
- keywrap_core_ctrl_new = CTRL_NEXT;
+ ready_new = 1'h0;
+ ready_we = 1'h1;
+ keywrap_core_ctrl_new = CTRL_NEXT_START;
keywrap_core_ctrl_we = 1'h1;
end
end
- CTRL_INIT:
+ CTRL_INIT_START:
begin
api_addr_ctr_rst = 1'h1;
- keywrap_core_ctrl_new = CTRL_IDLE;
+ aes_init = 1'h1;
+ keywrap_core_ctrl_new = CTRL_INIT_WAIT;
keywrap_core_ctrl_we = 1'h1;
end
+ CTRL_INIT_WAIT:
+ begin
+ if (aes_ready)
+ begin
+ ready_new = 1'h1;
+ ready_we = 1'h1;
+ keywrap_core_ctrl_new = CTRL_IDLE;
+ keywrap_core_ctrl_we = 1'h1;
+ end
+ end
+
- CTRL_NEXT:
+ CTRL_NEXT_START:
begin
init_a = 1'h1;
block_ctr_rst = 1'h1;
iteration_ctr_rst = 1'h1;
- keywrap_core_ctrl_new = CTRL_STEP1;
+ keywrap_core_ctrl_new = CTRL_NEXT_LOOP;
keywrap_core_ctrl_we = 1'h1;
end
- CTRL_STEP1:
+ CTRL_NEXT_LOOP:
+ begin
+ aes_next = 1'h1;
+ keywrap_core_ctrl_new = CTRL_NEXT_WAIT;
+ keywrap_core_ctrl_we = 1'h1;
+ end
+
+
+ CTRL_NEXT_WAIT:
+ begin
+ if (aes_ready)
+ begin
+ keywrap_core_ctrl_new = CTRL_NEXT_UPDATE;
+ keywrap_core_ctrl_we = 1'h1;
+ end
+ end
+
+
+ CTRL_NEXT_UPDATE:
begin
update_state = 1'h1;
- keywrap_core_ctrl_new = CTRL_IDLE;
+ keywrap_core_ctrl_new = CTRL_NEXT_CHECK;
keywrap_core_ctrl_we = 1'h1;
end
+ CTRL_NEXT_CHECK:
+ begin
+ if (block_ctr_reg < rlen)
+ begin
+ block_ctr_inc = 1'h1;
+ keywrap_core_ctrl_new = CTRL_NEXT_LOOP;
+ keywrap_core_ctrl_we = 1'h1;
+ end
+
+ else if (iteration_ctr_reg < OUTER_LOOP_MAX)
+ begin
+ block_ctr_rst = 1'h1;
+ iteration_ctr_inc = 1'h1;
+ keywrap_core_ctrl_new = CTRL_NEXT_LOOP;
+ keywrap_core_ctrl_we = 1'h1;
+ end
+
+ else
+ begin
+ keywrap_core_ctrl_new = CTRL_NEXT_FINALIZE;
+ keywrap_core_ctrl_we = 1'h1;
+ end
+ end
+
+
+ CTRL_NEXT_FINALIZE:
+ begin
+ ready_new = 1'h1;
+ ready_we = 1'h1;
+ valid_new = 1'h1;
+ valid_we = 1'h1;
+ keywrap_core_ctrl_new = CTRL_IDLE;
+ keywrap_core_ctrl_we = 1'h1;
+ end
+
default:
begin