From 2e02d9f9ee3e36c3deda4030df86f1bd6156949c Mon Sep 17 00:00:00 2001 From: Anas Ibrahem <139391509+anas-ibrahem@users.noreply.github.com> Date: Mon, 22 Apr 2024 19:52:07 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=8E=20Decrypt=20&=20Decrypt=20Round=20?= =?UTF-8?q?(#7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 😎 Decrypt & Decrypt Round * :lipstick: Code Styling --------- Co-authored-by: AhmedSobhy01 --- AESDecrypt.v | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ AESEncrypt.v | 22 ++++++++----------- DecryptRound.v | 27 ++++++++++++++++++++++++ EncryptRound.v | 22 ++++++++----------- 4 files changed, 102 insertions(+), 26 deletions(-) create mode 100644 AESDecrypt.v diff --git a/AESDecrypt.v b/AESDecrypt.v new file mode 100644 index 0000000..6375b71 --- /dev/null +++ b/AESDecrypt.v @@ -0,0 +1,57 @@ +module AESDecrypt #(parameter Nk = 4,parameter Nr = 10) (data, key, out, clk); + input [127:0] data; + input [Nk * 32 - 1:0] key; + input clk; + output [127:0] out; + + reg [127:0] state; + reg [127:0] keyReg; + reg [3:0] roundCount = 0; + wire [127:0] stateAfterLastRound; + wire [127:0] stateAfterKey; + wire [127:0] stateAfterRound; + + wire [(11 * 128) - 1:0] allKeys; + + KeyExpansion keysGetter( key , allKeys); + AddRoundKey a(state, keyReg , stateAfterKey); + DecryptRound round(state , keyReg , stateAfterRound); + LastDecryptRound lastRound (state , keyReg , stateAfterLastRound); + + assign out = state; + + always @(posedge clk) begin + if (roundCount == 0)begin + keyReg = allKeys [((11 * 128) - 1) -: 128 ] ; + state = data; + end + else if (roundCount == 1) + state <= stateAfterKey; + else if (roundCount < Nr +1) + state <= stateAfterRound; + else if (roundCount == Nr +1) + state <= stateAfterLastRound; + + if (roundCount > 0 && roundCount < Nr + 1) + keyReg <= allKeys [((11 * 128) - roundCount * 128 - 1) -: 128 ]; + + if (roundCount < Nr + 2) + roundCount <= roundCount + 1; + end +endmodule + +module AESDecrypt_DUT(); + reg [127:0] data; + reg [127:0] key; + wire [127:0] out; + reg clk; + + AESDecrypt aes(data,key,out,clk); + + initial begin + key = 128'h000102030405060708090a0b0c0d0e0f; + data = 128'h69c4e0d86a7b0430d8cdb78070b4c55a; + clk = 0; + forever #10 clk = ~clk; + end +endmodule \ No newline at end of file diff --git a/AESEncrypt.v b/AESEncrypt.v index 9644879..b1cd13a 100644 --- a/AESEncrypt.v +++ b/AESEncrypt.v @@ -1,10 +1,11 @@ -module AESEncrypt #(parameter Nk = 4,parameter Nr = 10) (data,key,out,clk); +module AESEncrypt #(parameter Nk = 4,parameter Nr = 10) (data, key, out, clk); input [127:0] data; input [Nk * 32 - 1:0] key; input clk; output [127:0] out; - reg [127:0]state; + + reg [127:0] state; reg [127:0] keyReg; reg [3:0] roundCount = 0; wire [127:0] stateAfterLastRound; @@ -17,31 +18,28 @@ module AESEncrypt #(parameter Nk = 4,parameter Nr = 10) (data,key,out,clk); EncryptRound round(state,keyReg,stateAfterRound); LastEncryptRound lastRound(state,keyReg,stateAfterLastRound); - assign out = state; always @(posedge clk) begin - if(roundCount == 0)begin + if (roundCount == 0) begin keyReg = key; state = data; end - else if(roundCount == 1) + else if (roundCount == 1) state <= stateAfterKey; else if (roundCount < Nr +1) state <= stateAfterRound; - else if(roundCount == Nr +1) + else if (roundCount == Nr +1) state <= stateAfterLastRound; - if(roundCount > 0 && roundCount < Nr + 1) + if (roundCount > 0 && roundCount < Nr + 1) keyReg <= keyWire; - if(roundCount < Nr + 2) + if (roundCount < Nr + 2) roundCount <= roundCount + 1; end - endmodule - module AESEncrypt_DUT(); reg [127:0] data; reg [127:0] key; @@ -56,6 +54,4 @@ module AESEncrypt_DUT(); clk = 0; forever #10 clk = ~clk; end -endmodule - - +endmodule \ No newline at end of file diff --git a/DecryptRound.v b/DecryptRound.v index e69de29..220d191 100644 --- a/DecryptRound.v +++ b/DecryptRound.v @@ -0,0 +1,27 @@ +module DecryptRound(stateIn, key, stateOut); + input [127:0] stateIn; + input [127:0] key; + output [127:0] stateOut; + + wire [127:0] subByteWire; + wire [127:0] shiftRowsWire; + wire [127:0] afterRoundKey; + + InvShiftRows shft(stateIn, shiftRowsWire); + InvSubBytes sub(shiftRowsWire, subByteWire); + AddRoundKey addkey(subByteWire, key, afterRoundKey); + InvMixColumns mix(afterRoundKey, stateOut); +endmodule + +module LastDecryptRound(stateIn, key, stateOut); + input [127:0] stateIn; + input [127:0] key; + output [127:0] stateOut; + + wire [127:0] subByteWire; + wire [127:0] shiftRowsWire; + + InvShiftRows shft(stateIn, shiftRowsWire); + InvSubBytes sub(shiftRowsWire, subByteWire); + AddRoundKey addkey(subByteWire, key, stateOut); +endmodule \ No newline at end of file diff --git a/EncryptRound.v b/EncryptRound.v index 80cc151..6f5104b 100644 --- a/EncryptRound.v +++ b/EncryptRound.v @@ -1,4 +1,4 @@ -module EncryptRound(stateIn,key,stateOut); +module EncryptRound(stateIn, key, stateOut); input [127:0] stateIn; input [127:0] key; output [127:0] stateOut; @@ -7,16 +7,13 @@ module EncryptRound(stateIn,key,stateOut); wire [127:0] shiftRowsWire; wire [127:0] mixColumnsWire; - SubBytes sub(stateIn,subByteWire); - ShiftRows shft(subByteWire,shiftRowsWire); - MixColumns mix(shiftRowsWire,mixColumnsWire); - AddRoundKey addkey(mixColumnsWire,key,stateOut); - + SubBytes sub(stateIn, subByteWire); + ShiftRows shft(subByteWire, shiftRowsWire); + MixColumns mix(shiftRowsWire, mixColumnsWire); + AddRoundKey addkey(mixColumnsWire, key, stateOut); endmodule - - -module LastEncryptRound(stateIn,key,stateOut); +module LastEncryptRound(stateIn, key, stateOut); input [127:0] stateIn; input [127:0] key; output [127:0] stateOut; @@ -24,8 +21,7 @@ module LastEncryptRound(stateIn,key,stateOut); wire [127:0] subByteWire; wire [127:0] shiftRowsWire; - SubBytes sub(stateIn,subByteWire); - ShiftRows shft(subByteWire,shiftRowsWire); - AddRoundKey addkey(shiftRowsWire,key,stateOut); - + SubBytes sub(stateIn, subByteWire); + ShiftRows shft(subByteWire, shiftRowsWire); + AddRoundKey addkey(shiftRowsWire, key, stateOut); endmodule \ No newline at end of file