../src/lowrisc_ip_hmac_0.1/rtl/hmac_pkg.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: 
   6: package hmac_pkg;
   7: 
   8:   // this currently uses the
   9:   // fully asynchronous implemenation
  10:   localparam int NumAlerts = 1;
  11:   localparam logic [NumAlerts-1:0] AlertAsyncOn = NumAlerts'(1'b1);
  12: 
  13:   localparam int MsgFifoDepth = 16;
  14: 
  15:   localparam int NumRound = 64;   // SHA-224, SHA-256
  16: 
  17:   typedef logic [31:0] sha_word_t;
  18:   localparam int WordByte = $bits(sha_word_t)/8;
  19: 
  20:   typedef struct packed {
  21:     sha_word_t           data;
  22:     logic [WordByte-1:0] mask;
  23:   } sha_fifo_t;
  24: 
  25: 
  26:   localparam sha_word_t InitHash [8]= '{
  27:     32'h 6a09_e667, 32'h bb67_ae85, 32'h 3c6e_f372, 32'h a54f_f53a,
  28:     32'h 510e_527f, 32'h 9b05_688c, 32'h 1f83_d9ab, 32'h 5be0_cd19
  29:   };
  30: 
  31:   localparam sha_word_t CubicRootPrime [64] = '{
  32:     32'h 428a_2f98, 32'h 7137_4491, 32'h b5c0_fbcf, 32'h e9b5_dba5,
  33:     32'h 3956_c25b, 32'h 59f1_11f1, 32'h 923f_82a4, 32'h ab1c_5ed5,
  34:     32'h d807_aa98, 32'h 1283_5b01, 32'h 2431_85be, 32'h 550c_7dc3,
  35:     32'h 72be_5d74, 32'h 80de_b1fe, 32'h 9bdc_06a7, 32'h c19b_f174,
  36:     32'h e49b_69c1, 32'h efbe_4786, 32'h 0fc1_9dc6, 32'h 240c_a1cc,
  37:     32'h 2de9_2c6f, 32'h 4a74_84aa, 32'h 5cb0_a9dc, 32'h 76f9_88da,
  38:     32'h 983e_5152, 32'h a831_c66d, 32'h b003_27c8, 32'h bf59_7fc7,
  39:     32'h c6e0_0bf3, 32'h d5a7_9147, 32'h 06ca_6351, 32'h 1429_2967,
  40:     32'h 27b7_0a85, 32'h 2e1b_2138, 32'h 4d2c_6dfc, 32'h 5338_0d13,
  41:     32'h 650a_7354, 32'h 766a_0abb, 32'h 81c2_c92e, 32'h 9272_2c85,
  42:     32'h a2bf_e8a1, 32'h a81a_664b, 32'h c24b_8b70, 32'h c76c_51a3,
  43:     32'h d192_e819, 32'h d699_0624, 32'h f40e_3585, 32'h 106a_a070,
  44:     32'h 19a4_c116, 32'h 1e37_6c08, 32'h 2748_774c, 32'h 34b0_bcb5,
  45:     32'h 391c_0cb3, 32'h 4ed8_aa4a, 32'h 5b9c_ca4f, 32'h 682e_6ff3,
  46:     32'h 748f_82ee, 32'h 78a5_636f, 32'h 84c8_7814, 32'h 8cc7_0208,
  47:     32'h 90be_fffa, 32'h a450_6ceb, 32'h bef9_a3f7, 32'h c671_78f2
  48:   };
  49: 
  50:   function automatic sha_word_t conv_endian( input sha_word_t v, input logic swap);
  51:     sha_word_t conv_data = {<<8{v}};
  52:     conv_endian = (swap) ? conv_data : v ;
  53:   endfunction : conv_endian
  54: 
  55:   function automatic sha_word_t rotr( input sha_word_t v , input int amt );
  56:     rotr = (v >> amt) | (v << (32-amt));
  57:   endfunction : rotr
  58: 
  59:   function automatic sha_word_t shiftr( input sha_word_t v, input int amt );
  60:     shiftr = (v >> amt);
  61:   endfunction : shiftr
  62: 
  63:   function automatic sha_word_t [7:0] compress( input sha_word_t w, input sha_word_t k,
  64:                                                 input sha_word_t [7:0] h_i);
  65:     automatic sha_word_t sigma_0, sigma_1, ch, maj, temp1, temp2;
  66: 
  67:     sigma_1 = rotr(h_i[4], 6) ^ rotr(h_i[4], 11) ^ rotr(h_i[4], 25);
  68:     ch = (h_i[4] & h_i[5]) ^ (~h_i[4] & h_i[6]);
  69:     temp1 = (h_i[7] + sigma_1 + ch + k + w);
  70:     sigma_0 = rotr(h_i[0], 2) ^ rotr(h_i[0], 13) ^ rotr(h_i[0], 22);
  71:     maj = (h_i[0] & h_i[1]) ^ (h_i[0] & h_i[2]) ^ (h_i[1] & h_i[2]);
  72:     temp2 = (sigma_0 + maj);
  73: 
  74:     compress[7] = h_i[6];          // h = g
  75:     compress[6] = h_i[5];          // g = f
  76:     compress[5] = h_i[4];          // f = e
  77:     compress[4] = h_i[3] + temp1;  // e = (d + temp1)
  78:     compress[3] = h_i[2];          // d = c
  79:     compress[2] = h_i[1];          // c = b
  80:     compress[1] = h_i[0];          // b = a
  81:     compress[0] = (temp1 + temp2); // a = (temp1 + temp2)
  82:   endfunction : compress
  83: 
  84:   function automatic sha_word_t calc_w(input sha_word_t w_0,
  85:                                        input sha_word_t w_1,
  86:                                        input sha_word_t w_9,
  87:                                        input sha_word_t w_14);
  88:     automatic sha_word_t sum0, sum1;
  89:     sum0 = rotr(w_1,   7) ^ rotr(w_1,  18) ^ shiftr(w_1,   3);
  90:     sum1 = rotr(w_14, 17) ^ rotr(w_14, 19) ^ shiftr(w_14, 10);
  91:     calc_w = w_0 + sum0 + w_9 + sum1;
  92:   endfunction : calc_w
  93: 
  94:   typedef enum logic [31:0] {
  95:     NoError                    = 32'h 0000_0000,
  96:     SwPushMsgWhenShaDisabled   = 32'h 0000_0001,
  97:     SwHashStartWhenShaDisabled = 32'h 0000_0002,
  98:     SwUpdateSecretKeyInProcess = 32'h 0000_0003,
  99:     SwHashStartWhenActive      = 32'h 0000_0004,
 100:     SwPushMsgWhenDisallowed    = 32'h 0000_0005
 101:   } err_code_e;
 102: 
 103: endpackage : hmac_pkg
 104: