../src/lowrisc_ip_aes_0.6/rtl/aes_sbox_canright.sv Cov: 100%

   1: // Copyright lowRISC contributors.
   2: // Licensed under the Apache License, Version 2.0, see LICENSE for details.
   3: // SPDX-License-Identifier: Apache-2.0
   4: //
   5: // AES Canright SBox #4
   6: //
   7: // For details, see the technical report: Canright, "A very compact Rijndael S-box"
   8: // available at https://hdl.handle.net/10945/25608
   9: 
  10: module aes_sbox_canright (
  11:   input  aes_pkg::ciph_op_e op_i,
  12:   input  logic [7:0]        data_i,
  13:   output logic [7:0]        data_o
  14: );
  15: 
  16:   import aes_pkg::*;
  17:   import aes_sbox_canright_pkg::*;
  18: 
  19:   ///////////////
  20:   // Functions //
  21:   ///////////////
  22: 
  23:   // Inverse in GF(2^4), using normal basis [alpha^8, alpha^2]
  24:   // (see Figure 12 in the technical report)
  25:   function automatic logic [3:0] aes_inverse_gf2p4(logic [3:0] gamma);
  26:     logic [3:0] delta;
  27:     logic [1:0] a, b, c, d;
  28:     a          = gamma[3:2] ^ gamma[1:0];
  29:     b          = aes_mul_gf2p2(gamma[3:2], gamma[1:0]);
  30:     c          = aes_scale_omega2_gf2p2(aes_square_gf2p2(a));
  31:     d          = aes_square_gf2p2(c ^ b);
  32:     delta[3:2] = aes_mul_gf2p2(d, gamma[1:0]);
  33:     delta[1:0] = aes_mul_gf2p2(d, gamma[3:2]);
  34:     return delta;
  35:   endfunction
  36: 
  37:   // Inverse in GF(2^8), using normal basis [d^16, d]
  38:   // (see Figure 11 in the technical report)
  39:   function automatic logic [7:0] aes_inverse_gf2p8(logic [7:0] gamma);
  40:     logic [7:0] delta;
  41:     logic [3:0] a, b, c, d;
  42:     a          = gamma[7:4] ^ gamma[3:0];
  43:     b          = aes_mul_gf2p4(gamma[7:4], gamma[3:0]);
  44:     c          = aes_square_scale_gf2p4_gf2p2(a);
  45:     d          = aes_inverse_gf2p4(c ^ b);
  46:     delta[7:4] = aes_mul_gf2p4(d, gamma[3:0]);
  47:     delta[3:0] = aes_mul_gf2p4(d, gamma[7:4]);
  48:     return delta;
  49:   endfunction
  50: 
  51:   ///////////////////
  52:   // Canright SBox //
  53:   ///////////////////
  54: 
  55:   logic [7:0] data_basis_x, data_inverse;
  56: 
  57:   // Convert to normal basis X.
  58:   assign data_basis_x = (op_i == CIPH_FWD) ? aes_mvm(data_i, A2X) :
  59:                                              aes_mvm(data_i ^ 8'h63, S2X);
  60: 
  61:   // Do the inversion in normal basis X.
  62:   assign data_inverse = aes_inverse_gf2p8(data_basis_x);
  63: 
  64:   // Convert to basis S or A.
  65:   assign data_o       = (op_i == CIPH_FWD) ? aes_mvm(data_inverse, X2S) ^ 8'h63 :
  66:                                              aes_mvm(data_inverse, X2A);
  67: 
  68: endmodule
  69: