Skip to content

Commit

Permalink
✨ Made modules work with 192 & 256 keys (#11)
Browse files Browse the repository at this point in the history
* ✨ Made modules work with 192 & 256 keys

* 🚀 Made main module work with three modes of encryption/decryption

* 🔨 Fixed warnings

* 🔨 Fixed issue with FPGA

* ✏️Changed comments

---------

Co-authored-by: AhmedAmrNabil <[email protected]>
  • Loading branch information
AhmedSobhy01 and AhmedAmrNabil authored May 1, 2024
1 parent 28b89c5 commit 234c797
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 115 deletions.
138 changes: 93 additions & 45 deletions AES.v
Original file line number Diff line number Diff line change
@@ -1,38 +1,68 @@
module AES(LED, HEX0, HEX1, HEX2, clk, reset);
localparam Nk = 4;
localparam Nr = 10;

module AES(LED, HEX0, HEX1, HEX2, sel, clk);
input [1:0] sel; // 00 -> 128-bit AES, 01 -> 192-bit AES, 10/11 -> 256-bit AES
input clk;
input reset;

output LED;
output [6:0] HEX0;
output [6:0] HEX1;
output [6:0] HEX2;
output [6:0] HEX3;
output [6:0] HEX4;
output [6:0] HEX5;

// Key
wire [127:0] key = 128'h00_01_02_03_04_05_06_07_08_09_0a_0b_0c_0d_0e_0f;
// Keys
wire [127:0] key128 = 128'h000102030405060708090a0b0c0d0e0f;
wire [191:0] key192 = 192'h000102030405060708090a0b0c0d0e0f1011121314151617;
wire [255:0] key256 = 256'h000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f;

// Key Expansion
wire [(11 * 128) - 1:0] allKeys;
KeyExpansion keysGetter(key, allKeys);
// Nr / Nk
wire [3:0] Nr = sel == 0 ? 4'b1010 : sel == 1 ? 4'b1100 : 4'b1110;

// Data
wire [127:0] data = 128'h00_11_22_33_44_55_66_77_88_99_aa_bb_cc_dd_ee_ff;

// AES
wire [127:0] tempEncryptedOutput;
wire [127:0] tempDecryptedOutput;

// Current round count for encryption and decryption
reg [5:0] count;
initial
count = 0;
reg [5:0] count = 0;

// AES Decrypt Enable
reg AESDecryptEnable = 1'b0;

// Selection Reg
reg [1:0] selReg = 0;

// 128-bit AES
wire aesReset128 = selReg == 0 ? 1'b0 : 1'b1;
wire [127:0] tempEncryptedOutput128;
wire [127:0] tempDecryptedOutput128;
wire [1407:0] allKeys128;

KeyExpansion #(4, 10) keysGetter128(key128, allKeys128);
AESEncrypt #(4, 10) aese128(data, allKeys128, tempEncryptedOutput128, clk, aesReset128);
AESDecrypt #(4, 10) aesd128(tempEncryptedOutput128, allKeys128, tempDecryptedOutput128, clk, AESDecryptEnable, aesReset128);

// 192-bit AES
wire aesReset192 = selReg == 1 ? 1'b0 : 1'b1;
wire [127:0] tempEncryptedOutput192;
wire [127:0] tempDecryptedOutput192;
wire [1663:0] allKeys192;

KeyExpansion #(6, 12) keysGetter192(key192, allKeys192);
AESEncrypt #(6, 12) aese192(data, allKeys192, tempEncryptedOutput192, clk, aesReset192);
AESDecrypt #(6, 12) aesd192(tempEncryptedOutput192, allKeys192, tempDecryptedOutput192, clk, AESDecryptEnable, aesReset192);

// 256-bit AES
wire aesReset256 = (selReg == 2 || selReg == 3) ? 1'b0 : 1'b1;
wire [127:0] tempEncryptedOutput256;
wire [127:0] tempDecryptedOutput256;
wire [1919:0] allKeys256;

KeyExpansion #(8, 14) keysGetter(key256, allKeys256);
AESEncrypt #(8, 14) aese256(data, allKeys256, tempEncryptedOutput256, clk, aesReset256);
AESDecrypt #(8, 14) aesd256(tempEncryptedOutput256, allKeys256, tempDecryptedOutput256, clk, AESDecryptEnable, aesReset256);

// Encrypted and Decrypted Data
wire [127:0] tempEncryptedOutput = selReg == 0 ? tempEncryptedOutput128 : selReg == 1 ? tempEncryptedOutput192 : tempEncryptedOutput256;
wire [127:0] tempDecryptedOutput = selReg == 0 ? tempDecryptedOutput128 : selReg == 1 ? tempDecryptedOutput192 : tempDecryptedOutput256;

// Assign bcdInput based on count
// count = 0 -> Encrypted Data
// count = 0 -> Original Data
// count = 1 to Nr -> Encrypted Data
// count = Nr + 2 -> Decrypted Data
wire [7:0] bcdInput = (count == 0) ? data[7:0] : (count <= Nr + 1) ? tempEncryptedOutput[7:0] : tempDecryptedOutput[7:0];
Expand All @@ -41,60 +71,78 @@ module AES(LED, HEX0, HEX1, HEX2, clk, reset);
wire [11:0] bcdOutput;
Binary2BCD b2b(bcdInput, bcdOutput);

// 7-Segment Logic
// 7-Segment Logic
DisplayDecoder dd1(bcdOutput[3:0], HEX0);
DisplayDecoder dd2(bcdOutput[7:4], HEX1);
DisplayDecoder dd3(bcdOutput[11:8], HEX2);

// Encrypt
// reg AESEncryptEnable = 1'b1;
AESEncrypt AESE(data, allKeys, tempEncryptedOutput, clk,reset);

// Decrypt
reg AESDecryptEnable = 1'b0;
AESDecrypt AESD(tempEncryptedOutput, allKeys, tempDecryptedOutput, clk, AESDecryptEnable,reset);

// LED = 1 if Decrypted Data is same as original data
assign LED = (tempDecryptedOutput == data && count > Nr + 1);

// Previous Selection
reg [1:0] prevSel = 2'b0;

always @(negedge clk) begin
if (reset) begin
count = 0;
AESDecryptEnable = 1'b0;
end
else begin
if (count == Nr)
AESDecryptEnable = 1'b1;
else if (count == ((Nr + 1) * 2))
AESDecryptEnable = 1'b0;
prevSel = selReg;
selReg = sel;

if (prevSel != selReg) begin
prevSel = selReg;
count = 0;
AESDecryptEnable = 1'b0;
end
else begin
if (count == Nr)
AESDecryptEnable = 1'b1;
else if (count == ((Nr + 1) * 2))
AESDecryptEnable = 1'b0;

if (count <= (Nr + 1) * 2)
count <= count + 6'b000001;
end
count = count + 6'b000001;
end
end
endmodule

module AES_DUT();
reg [1:0] sel = 2'b00;
reg clk = 1'b1;
wire LED;
wire [6:0] HEX0, HEX1, HEX2;

AES aes(LED, HEX0, HEX1, HEX2, clk, 1'b0);
AES aes(LED, HEX0, HEX1, HEX2, sel, clk);

reg [5:0] count = 0;
integer count = 0;
initial begin
clk = 1'b1;

forever begin
#10 clk = ~clk;
if (!clk) begin
count = count + 1;
$display("Current Round Count: %d, LED Status: %b, Encrypted State: %h (%0d), Decrypted State: %h (%0d)", count, LED, aes.tempEncryptedOutput, aes.tempEncryptedOutput[7:0], aes.tempDecryptedOutput, aes.tempDecryptedOutput[7:0]);
if (count < 80)
count = count + 1;

$display("Current Round Count: %0d, LED Status: %b, Encrypted State: %h (%0d), Decrypted State: %h (%0d)", count, LED, aes.tempEncryptedOutput, aes.tempEncryptedOutput[7:0], aes.tempDecryptedOutput, aes.tempDecryptedOutput[7:0]);

if (count == 23) begin
$display("==============================================");
$display("Switching to 192-bit AES Encryption and Decryption");
$display("==============================================");
sel = 2'b01;
end
else if (count == 50) begin
$display("==============================================");
$display("Switching to 256-bit AES Encryption and Decryption");
$display("==============================================");
sel = 2'b10;
end
end
end
end

initial begin
$display("AES Encryption and Decryption");
$display("================================");
$display("128-bit AES Encryption and Decryption");
$display("================================");
end
endmodule
57 changes: 49 additions & 8 deletions AESDecrypt.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module AESDecrypt #(parameter Nk = 4, parameter Nr = 10) (data, allKeys, state, clk, enable, reset);
input [127:0] data;
input [(11 * 128) - 1:0] allKeys;
input [((Nr + 1) * 128) - 1:0] allKeys;
input clk;
input enable;
input reset;
Expand All @@ -16,9 +16,9 @@ module AESDecrypt #(parameter Nk = 4, parameter Nr = 10) (data, allKeys, state,
wire [127:0] stateOut;

// Instantiate AES modules needed for decryption
InvShiftRows shft(state, shiftRowsWire);
InvShiftRows shft(state, shiftRowsWire);
InvSubBytes sub(shiftRowsWire, subByteWire);
AddRoundKey addkey(keyInput, allKeys[((12 - roundCount) * 128 - 1) -: 128], afterRoundKey);
AddRoundKey addkey(keyInput, allKeys[(roundCount * 128) - 1 -: 128], afterRoundKey);
InvMixColumns mix(afterRoundKey, mixColumnsWire);

// Assign keyInput based on roundCount
Expand All @@ -42,24 +42,65 @@ module AESDecrypt #(parameter Nk = 4, parameter Nr = 10) (data, allKeys, state,
if (reset)
roundCount = 1;
else if (enable && roundCount <= Nr + 1) begin
state = stateOut;
state = stateOut;
roundCount = roundCount + 6'b000001;
end
end
endmodule

module AESDecrypt_DUT();
module AESDecrypt128_DUT();
localparam Nk = 4;
localparam Nr = 10;

wire [127:0] data = 128'h69c4e0d86a7b0430d8cdb78070b4c55a;
wire [127:0] key = 128'h000102030405060708090a0b0c0d0e0f;
wire [(11 * 128) - 1:0] allKeys;
wire [Nk * 32 - 1:0] key = 128'h000102030405060708090a0b0c0d0e0f;
wire [((Nr + 1) * 128) - 1:0] allKeys;
wire [127:0] out;
reg clk;

KeyExpansion #(Nk, Nr) ke(key, allKeys);
AESDecrypt #(Nk, Nr) aes(data, allKeys, out, clk, 1'b1);

initial begin
clk = 1'b1;
forever #10 clk = ~clk;
end
endmodule

module AESDecrypt192_DUT();
localparam Nk = 6;
localparam Nr = 12;

wire [127:0] data = 128'hdda97ca4864cdfe06eaf70a0ec0d7191;
wire [Nk * 32 - 1:0] key = 192'h000102030405060708090a0b0c0d0e0f1011121314151617;
wire [((Nr + 1) * 128) - 1:0] allKeys;
wire [127:0] out;
reg clk;

KeyExpansion #(Nk, Nr) ke(key, allKeys);
AESDecrypt #(Nk, Nr) aes(data, allKeys, out, clk, 1'b1);

initial begin
clk = 1'b1;
forever #10 clk = ~clk;
end
endmodule

module AESDecrypt256_DUT();
localparam Nk = 8;
localparam Nr = 14;

wire [127:0] data = 128'h8ea2b7ca516745bfeafc49904b496089;
wire [Nk * 32 - 1:0] key = 256'h000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f;
wire [((Nr + 1) * 128) - 1:0] allKeys;
wire [127:0] out;
reg clk;

KeyExpansion ke(key, allKeys);
AESDecrypt aes(data, allKeys, out, clk, 1, 0);

initial begin
clk = 0;
clk = 1'b1;
forever #10 clk = ~clk;
end
endmodule
63 changes: 52 additions & 11 deletions AESEncrypt.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module AESEncrypt #(parameter Nk = 4, parameter Nr = 10) (data, allKeys, state, clk, reset);
input [127:0] data;
input [(11 * 128) - 1:0] allKeys;
input [((Nr + 1) * 128) - 1:0] allKeys;
input clk;
input reset;
output reg [127:0] state = 0; // Holds the state of the AES encryption
Expand All @@ -15,15 +15,15 @@ module AESEncrypt #(parameter Nk = 4, parameter Nr = 10) (data, allKeys, state,

// Instantiate AES modules needed for encryption
SubBytes sub(state, subByteWire);
ShiftRows shft(subByteWire, shiftRowsWire);
MixColumns mix(shiftRowsWire, mixColumnsWire);
AddRoundKey addkey(roundKeyInput , allKeys[(128 * roundCount) - 1 -: 128], stateOut);
ShiftRows shft(subByteWire, shiftRowsWire);
MixColumns mix(shiftRowsWire, mixColumnsWire);
AddRoundKey addkey(roundKeyInput , allKeys[((Nr + 1) * 128) - (roundCount - 1) * 128 - 1 -: 128], stateOut);

// Assign roundKeyInput based on roundCount
// roundCount = 1 -> Data
// roundCount = 2 to Nr -> mixColumnsWire
// roundCount = Nr + 1 -> shiftRowsWire
assign roundKeyInput = (roundCount == 1) ? data : (roundCount < Nr + 1) ? mixColumnsWire: shiftRowsWire;
assign roundKeyInput = (roundCount == 1) ? data : (roundCount < Nr + 1) ? mixColumnsWire : shiftRowsWire;

// Assign state to data on data change and reset roundCount
initial @(data) begin
Expand All @@ -38,21 +38,62 @@ module AESEncrypt #(parameter Nk = 4, parameter Nr = 10) (data, allKeys, state,
roundCount = 1;
end
else if (roundCount <= Nr + 1) begin
state = stateOut;
state = stateOut;
roundCount = roundCount + 6'b000001;
end
end
endmodule

module AESEncrypt_DUT();
module AESEncrypt128_DUT();
localparam Nk = 4;
localparam Nr = 10;

wire [127:0] data = 128'h00112233445566778899aabbccddeeff;
wire [Nk * 32 - 1:0] key = 128'h000102030405060708090a0b0c0d0e0f;
wire [((Nr + 1) * 128) - 1:0] allKeys;
wire [127:0] out;
reg clk;

KeyExpansion #(Nk, Nr) ke(key, allKeys);
AESEncrypt #(Nk, Nr) aes(data, allKeys, out, clk, 1'b0);

initial begin
clk = 0;
forever #10 clk = ~clk;
end
endmodule

module AESEncrypt192_DUT();
localparam Nk = 6;
localparam Nr = 12;

wire [127:0] data = 128'h00112233445566778899aabbccddeeff;
wire [Nk * 32 - 1:0] key = 192'h000102030405060708090a0b0c0d0e0f1011121314151617;
wire [((Nr + 1) * 128) - 1:0] allKeys;
wire [127:0] out;
reg clk;

KeyExpansion #(Nk, Nr) ke(key, allKeys);
AESEncrypt #(Nk, Nr) aes(data, allKeys, out, clk, 1'b0);

initial begin
clk = 0;
forever #10 clk = ~clk;
end
endmodule

module AESEncrypt256_DUT();
localparam Nk = 8;
localparam Nr = 14;

wire [127:0] data = 128'h00112233445566778899aabbccddeeff;
wire [127:0] key = 128'h000102030405060708090a0b0c0d0e0f;
wire [(11 * 128) - 1:0] allKeys;
wire [Nk * 32 - 1:0] key = 256'h000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f;
wire [((Nr + 1) * 128) - 1:0] allKeys;
wire [127:0] out;
reg clk;

KeyExpansion ke(key, allKeys);
AESEncrypt aes(data, allKeys, out, clk, 0);
KeyExpansion #(Nk, Nr) ke(key, allKeys);
AESEncrypt #(Nk, Nr) aes(data, allKeys, out, clk, 1'b0);

initial begin
clk = 0;
Expand Down
Loading

0 comments on commit 234c797

Please sign in to comment.