1 |
2 |
Amr_Salah |
/*
|
2 |
|
|
Project : AES
|
3 |
|
|
Standard doc. : FIPS 197
|
4 |
|
|
Module name : RoundKeyGen block
|
5 |
|
|
Dependancy :
|
6 |
|
|
Design doc. :
|
7 |
|
|
References :
|
8 |
|
|
Description : This module is used to perform the process
|
9 |
|
|
of round key generation from input key
|
10 |
|
|
this module is the basic block of key expantion module
|
11 |
|
|
Owner : Amr Salah
|
12 |
|
|
*/
|
13 |
|
|
|
14 |
|
|
`timescale 1 ns/1 ps
|
15 |
|
|
|
16 |
|
|
module RoundKeyGen
|
17 |
|
|
#
|
18 |
|
|
(
|
19 |
|
|
parameter KEY_L = 128, //key length
|
20 |
|
|
parameter WORD = 32 //a parameter to represent WORD = 4 bytes = 32 bit
|
21 |
|
|
)
|
22 |
|
|
(
|
23 |
|
|
input clk, //system clk
|
24 |
|
|
input reset, //asynch active low reset
|
25 |
|
|
input [WORD-1:0] RCON_Word, //round constant word
|
26 |
|
|
input valid_in, //input valid signal
|
27 |
|
|
input [KEY_L-1:0] key, //input key
|
28 |
|
|
output reg [KEY_L-1:0]round_key, //round key
|
29 |
|
|
output reg valid_out //output valid signal
|
30 |
|
|
);
|
31 |
|
|
|
32 |
|
|
wire [WORD-1:0] Key_RotWord;
|
33 |
|
|
reg [KEY_L-1:0] Key_FirstStage;
|
34 |
|
|
reg [KEY_L-1:0] Key_SecondStage;
|
35 |
|
|
reg [KEY_L-1:0] round_key_delayed;
|
36 |
|
|
reg valid_FirstStage;
|
37 |
|
|
reg valid_round_key;
|
38 |
|
|
wire [WORD-1:0] Key_SubBytes;
|
39 |
|
|
wire subbytes_valid_out;
|
40 |
|
|
wire [KEY_L-1:0] temp_round_key;
|
41 |
|
|
|
42 |
|
|
//The keygeneration stages should be balanced with the 4 round stages(SubBytes-ShiftRows-MixColumns-AddRoundKey)
|
43 |
|
|
//in order to let the round key and the data meet at the same time in the AddRoundKey module
|
44 |
|
|
|
45 |
|
|
/******************************************First Stage Register***********************************************************/
|
46 |
|
|
always @(posedge clk or negedge reset)
|
47 |
|
|
if(!reset)begin
|
48 |
|
|
valid_FirstStage <= 1'b0;
|
49 |
|
|
Key_FirstStage <= 'b0;
|
50 |
|
|
end else begin
|
51 |
|
|
if(valid_in)begin
|
52 |
|
|
Key_FirstStage <= key;
|
53 |
|
|
end
|
54 |
|
|
valid_FirstStage <= valid_in;
|
55 |
|
|
end
|
56 |
|
|
/***********************************************Second Stage Register*******************************************************/
|
57 |
|
|
always @(posedge clk or negedge reset)
|
58 |
|
|
if(!reset)begin
|
59 |
|
|
Key_SecondStage <= 'b0;
|
60 |
|
|
end else begin
|
61 |
|
|
if(valid_FirstStage)begin
|
62 |
|
|
Key_SecondStage <= Key_FirstStage;
|
63 |
|
|
end
|
64 |
|
|
end
|
65 |
|
|
/*******************************************************RotWord****************************************************************/
|
66 |
|
|
assign Key_RotWord = {Key_FirstStage[WORD-9:0],Key_FirstStage[WORD-1:WORD-8]}; //rotation of the least word in key
|
67 |
|
|
|
68 |
|
|
/**************************************************SubBytes (Parallel to second stage register)*******************************/
|
69 |
|
|
//perform subbytes operation on the result word of rotword step
|
70 |
|
|
SubBytes #(WORD) SUB_U (clk,reset,valid_FirstStage,Key_RotWord,subbytes_valid_out,Key_SubBytes);
|
71 |
|
|
|
72 |
|
|
/***************************************************Round Key calculations ***********************************************/
|
73 |
|
|
assign temp_round_key[4*WORD-1:3*WORD] = Key_SecondStage[4*WORD-1:3*WORD] ^ Key_SubBytes ^ RCON_Word;
|
74 |
|
|
assign temp_round_key[3*WORD-1:2*WORD] = Key_SecondStage[3*WORD-1:2*WORD] ^ temp_round_key[4*WORD-1:3*WORD] ;
|
75 |
|
|
assign temp_round_key[2*WORD-1:WORD] = Key_SecondStage[2*WORD-1:WORD] ^ temp_round_key[3*WORD-1:2*WORD];
|
76 |
|
|
assign temp_round_key[WORD-1:0] = Key_SecondStage[WORD-1:0] ^ temp_round_key[2*WORD-1:WORD];
|
77 |
|
|
|
78 |
|
|
/***************************************************Roundkey Register (Third Stage)******************************************/
|
79 |
|
|
always @(posedge clk or negedge reset)
|
80 |
|
|
if(!reset)begin
|
81 |
|
|
round_key_delayed <= 'b0;
|
82 |
|
|
valid_round_key <= 1'b0;
|
83 |
|
|
end else begin
|
84 |
|
|
if(subbytes_valid_out)begin
|
85 |
|
|
round_key_delayed <= temp_round_key;
|
86 |
|
|
end
|
87 |
|
|
valid_round_key <= subbytes_valid_out;
|
88 |
|
|
end
|
89 |
|
|
/****************************************Out Put Register (Fourth Stage)*********************************************/
|
90 |
|
|
always @(posedge clk or negedge reset)
|
91 |
|
|
if(!reset)begin
|
92 |
|
|
valid_out <= 1'b0;
|
93 |
|
|
round_key <= 'b0;
|
94 |
|
|
end else begin
|
95 |
|
|
if(valid_round_key)begin
|
96 |
|
|
round_key <= round_key_delayed;
|
97 |
|
|
end
|
98 |
|
|
valid_out <= valid_round_key;
|
99 |
|
|
end
|
100 |
|
|
|
101 |
|
|
endmodule
|