From ec5c67a5c12b52435686c2c7df03fe42d467ec3b Mon Sep 17 00:00:00 2001 From: David Harris Date: Fri, 24 May 2024 13:48:53 -0700 Subject: [PATCH] AES cleanup --- src/ieu/aes/aes64e.sv | 2 +- src/ieu/aes/aesmixcolumns8.sv | 2 +- src/ieu/bmu/bitmanipalu.sv | 4 ++-- src/ieu/kmu/zknde32.sv | 7 ++++--- src/ieu/kmu/zknde64.sv | 7 ++++--- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/ieu/aes/aes64e.sv b/src/ieu/aes/aes64e.sv index 7435f4327..c1ca9013e 100644 --- a/src/ieu/aes/aes64e.sv +++ b/src/ieu/aes/aes64e.sv @@ -48,7 +48,7 @@ module aes64e( // Apply MixColumns operations aesmixcolumns32 mw0(SboxOut[31:0], MixcolOut[31:0]); - aesmixcolumns32 mw1(SboxOut[63:32], MixcolOut[63:32]); + aesmixcolumns32 mw1(SboxOut[63:32], MixcolOut[63:32]); // Skip mixcolumns on last round mux2 #(64) resultmux(MixcolOut, SboxOut, finalround, result); diff --git a/src/ieu/aes/aesmixcolumns8.sv b/src/ieu/aes/aesmixcolumns8.sv index 66ab6534f..256f728d0 100644 --- a/src/ieu/aes/aesmixcolumns8.sv +++ b/src/ieu/aes/aesmixcolumns8.sv @@ -27,7 +27,7 @@ module aesmixcolumns8( - input logic [7:0] a, + input logic [7:0] a, output logic [31:0] y ); diff --git a/src/ieu/bmu/bitmanipalu.sv b/src/ieu/bmu/bitmanipalu.sv index b0af3e347..36feff63e 100644 --- a/src/ieu/bmu/bitmanipalu.sv +++ b/src/ieu/bmu/bitmanipalu.sv @@ -113,8 +113,8 @@ module bitmanipalu import cvw::*; #(parameter cvw_t P) ( // ZKND and ZKNE AES decryption and encryption if (P.ZKND_SUPPORTED | P.ZKNE_SUPPORTED) begin: zknde - if (P.XLEN == 32) zknde32 #(P) ZKN32(.A(ABMU), .B(BBMU), .Funct7, .round(Rs2E[3:0]), .ZKNSelect(ZBBSelect[3:0]), .ZKNDEResult); - else zknde64 #(P) ZKN64(.A(ABMU), .B(BBMU), .Funct7, .round(Rs2E[3:0]), .ZKNSelect(ZBBSelect[3:0]), .ZKNDEResult); + if (P.XLEN == 32) zknde32 #(P) ZKN32(.A(ABMU), .B(BBMU), .bs(Funct7[6:5]), .round(Rs2E[3:0]), .ZKNSelect(ZBBSelect[3:0]), .ZKNDEResult); + else zknde64 #(P) ZKN64(.A(ABMU), .B(BBMU), .round(Rs2E[3:0]), .ZKNSelect(ZBBSelect[3:0]), .ZKNDEResult); end else assign ZKNDEResult = '0; // ZKNH Unit diff --git a/src/ieu/kmu/zknde32.sv b/src/ieu/kmu/zknde32.sv index 4c845599c..7e482d757 100644 --- a/src/ieu/kmu/zknde32.sv +++ b/src/ieu/kmu/zknde32.sv @@ -28,7 +28,7 @@ module zknde32 import cvw::*; #(parameter cvw_t P) ( input logic [31:0] A, B, - input logic [6:0] Funct7, + input logic [1:0] bs, input logic [3:0] round, input logic [3:0] ZKNSelect, output logic [31:0] ZKNDEResult @@ -39,7 +39,7 @@ module zknde32 import cvw::*; #(parameter cvw_t P) ( logic [31:0] ZKNEResult, ZKNDResult, rotin, rotout; // Initial shamt and Sbox input selection steps shared between encrypt and decrypt - assign shamt = {Funct7[6:5], 3'b0}; // shamt = bs * 8 (convert bytes to bits) + assign shamt = {bs, 3'b0}; // shamt = bs * 8 (convert bytes to bits) assign SboxIn = B[shamt +: 8]; // select byte bs of rs2 // Handle logic specific to encrypt or decrypt @@ -55,6 +55,7 @@ module zknde32 import cvw::*; #(parameter cvw_t P) ( assign rotin = ZKNEResult; // final rotate and XOR steps shared between encrypt and decrypt - rotate #(32) mrot(rotin, shamt, rotout); // Rotate the mixcolumns output left by shamt (bs * 8) + mux4 #(32) mrotmux(rotin, {rotin[23:0], rotin[31:24]}, + {rotin[15:0], rotin[31:16]}, {rotin[7:0], rotin[31:8]}, bs, rotout); // Rotate the mixcolumns output left by shamt (bs * 8) assign ZKNDEResult = A ^ rotout; // xor with running value (A = rs1) endmodule diff --git a/src/ieu/kmu/zknde64.sv b/src/ieu/kmu/zknde64.sv index 2a2b6cc10..9c2566718 100644 --- a/src/ieu/kmu/zknde64.sv +++ b/src/ieu/kmu/zknde64.sv @@ -28,7 +28,6 @@ module zknde64 import cvw::*; #(parameter cvw_t P) ( input logic [63:0] A, B, - input logic [6:0] Funct7, input logic [3:0] round, input logic [3:0] ZKNSelect, output logic [63:0] ZKNDEResult @@ -39,11 +38,13 @@ module zknde64 import cvw::*; #(parameter cvw_t P) ( if (P.ZKND_SUPPORTED) // ZKND supports aes64ds, aes64dsm, aes64im aes64d aes64d(.rs1(A), .rs2(B), .finalround(ZKNSelect[2]), .aes64im(ZKNSelect[3]), .result(aes64dRes)); // decode AES - if (P.ZKNE_SUPPORTED) // ZKNE supports aes64es, aes64esm + if (P.ZKNE_SUPPORTED) begin // ZKNE supports aes64es, aes64esm aes64e aes64e(.rs1(A), .rs2(B), .finalround(ZKNSelect[2]), .Sbox0Out, .SboxEIn, .result(aes64eRes)); + mux2 #(32) sboxmux(SboxEIn, SboxKIn, ZKNSelect[1], Sbox0In); + end else + assign Sbox0In = SboxKIn; // One S Box is always needed for aes64ks1i and is also needed for aes64e if that is supported. Put it at the top level to allow sharing - mux2 #(32) sboxmux(SboxEIn, SboxKIn, ZKNSelect[1], Sbox0In); aessbox32 sbox(Sbox0In, Sbox0Out); // Substitute bytes of value obtained for tmp2 using Rijndael sbox // Both ZKND and ZKNE support aes64ks1i and aes64ks2 instructions