[kmac] Moving KDF struct to KMAC IP

Previously KeyMgr KDF interface was defined in KeyMgr IP. The interface
has been used for KeyMgr to initiate KMAC operation via side channel
interface.

As rom_ctrl and otp_ctrl plan to use KMAC as signature verification
function, KMAC needs to have more general application interface now.

This commit is to move the KDF struct into KMAC and renames it to
`app_{req/rsp}_t`.

Signed-off-by: Eunchan Kim <eunchan@opentitan.org>
diff --git a/hw/dv/sv/keymgr_kmac_agent/keymgr_kmac_agent.core b/hw/dv/sv/keymgr_kmac_agent/keymgr_kmac_agent.core
index d60c594..d6a4561 100644
--- a/hw/dv/sv/keymgr_kmac_agent/keymgr_kmac_agent.core
+++ b/hw/dv/sv/keymgr_kmac_agent/keymgr_kmac_agent.core
@@ -11,6 +11,7 @@
       - lowrisc:dv:dv_lib
       - lowrisc:dv:push_pull_agent
       - lowrisc:ip:keymgr_pkg
+      - lowrisc:ip:kmac_pkg
     files:
       - keymgr_kmac_agent_pkg.sv
       - keymgr_kmac_intf.sv
diff --git a/hw/dv/sv/keymgr_kmac_agent/keymgr_kmac_intf.sv b/hw/dv/sv/keymgr_kmac_agent/keymgr_kmac_intf.sv
index ae262fa..a95f882 100644
--- a/hw/dv/sv/keymgr_kmac_agent/keymgr_kmac_intf.sv
+++ b/hw/dv/sv/keymgr_kmac_agent/keymgr_kmac_intf.sv
@@ -10,8 +10,8 @@
   dv_utils_pkg::if_mode_e if_mode; // interface mode - Host or Device
 
   // interface pins used to connect with DUT
-  wire kmac_data_req_t kmac_data_req;
-  wire kmac_data_rsp_t kmac_data_rsp;
+  wire kmac_pkg::app_req_t kmac_data_req;
+  wire kmac_pkg::app_rsp_t kmac_data_rsp;
 
   // interface pins used in driver/monitor
   push_pull_if #(.HostDataWidth(keymgr_kmac_agent_pkg::KMAC_REQ_DATA_WIDTH))
diff --git a/hw/ip/keymgr/data/keymgr.hjson b/hw/ip/keymgr/data/keymgr.hjson
index e6647cc..fe41388 100644
--- a/hw/ip/keymgr/data/keymgr.hjson
+++ b/hw/ip/keymgr/data/keymgr.hjson
@@ -49,11 +49,11 @@
       act:     "req",
       package: "keymgr_pkg",  // Origin package (only needs for the requester)
     },
-    { struct:  "kmac_data",   // kmac_data_req_t, kmac_data_rsp_t
+    { struct:  "app",   // kmac_pkg::app_req_t, kmac_pkg::app_rsp_t
       type:    "req_rsp",
       name:    "kmac_data",   // kmac_data_o (req), kmac_data_i (rsp)
       act:     "req",
-      package: "keymgr_pkg",  // Origin package (only needs for the requester)
+      package: "kmac_pkg",  // Origin package (only needs for the requester)
     },
     { struct:  "otp_keymgr_key",
       type:    "uni",
diff --git a/hw/ip/keymgr/dv/env/keymgr_if.sv b/hw/ip/keymgr/dv/env/keymgr_if.sv
index 1cd5b28..d5cfea5 100644
--- a/hw/ip/keymgr/dv/env/keymgr_if.sv
+++ b/hw/ip/keymgr/dv/env/keymgr_if.sv
@@ -22,8 +22,8 @@
   keymgr_pkg::hw_key_req_t aes_key_exp;
 
   // connect KDF interface for assertion check
-  wire keymgr_pkg::kmac_data_req_t kmac_data_req;
-  wire keymgr_pkg::kmac_data_rsp_t kmac_data_rsp;
+  wire kmac_pkg::app_req_t kmac_data_req;
+  wire kmac_pkg::app_rsp_t kmac_data_rsp;
 
   // connect EDN for assertion check
   wire edn_clk, edn_rst_n, edn_req, edn_ack;
diff --git a/hw/ip/keymgr/keymgr.core b/hw/ip/keymgr/keymgr.core
index 0d1c4cb..6f91717 100644
--- a/hw/ip/keymgr/keymgr.core
+++ b/hw/ip/keymgr/keymgr.core
@@ -13,6 +13,7 @@
       - lowrisc:prim:lfsr
       - lowrisc:prim:lc_sync
       - lowrisc:ip:keymgr_pkg
+      - lowrisc:ip:kmac_pkg
     files:
       - rtl/keymgr_reg_top.sv
       - rtl/keymgr_sideload_key_ctrl.sv
diff --git a/hw/ip/keymgr/rtl/keymgr.sv b/hw/ip/keymgr/rtl/keymgr.sv
index 47fde28..afab3b7 100644
--- a/hw/ip/keymgr/rtl/keymgr.sv
+++ b/hw/ip/keymgr/rtl/keymgr.sv
@@ -41,8 +41,8 @@
   output hw_key_req_t kmac_key_o,
 
   // data interface to/from crypto modules
-  output kmac_data_req_t kmac_data_o,
-  input kmac_data_rsp_t kmac_data_i,
+  output kmac_pkg::app_req_t kmac_data_o,
+  input  kmac_pkg::app_rsp_t kmac_data_i,
 
   // the following signals should eventually be wrapped into structs from other modules
   input lc_ctrl_pkg::lc_tx_t lc_keymgr_en_i,
diff --git a/hw/ip/keymgr/rtl/keymgr_kmac_if.sv b/hw/ip/keymgr/rtl/keymgr_kmac_if.sv
index 5194c21..c8abab0 100644
--- a/hw/ip/keymgr/rtl/keymgr_kmac_if.sv
+++ b/hw/ip/keymgr/rtl/keymgr_kmac_if.sv
@@ -26,8 +26,8 @@
   output logic [Shares-1:0][KeyWidth-1:0] data_o,
 
   // actual connection to kmac
-  output kmac_data_req_t kmac_data_o,
-  input kmac_data_rsp_t kmac_data_i,
+  output kmac_pkg::app_req_t kmac_data_o,
+  input  kmac_pkg::app_rsp_t kmac_data_i,
 
   // entropy input
   output logic prng_en_o,
diff --git a/hw/ip/keymgr/rtl/keymgr_pkg.sv b/hw/ip/keymgr/rtl/keymgr_pkg.sv
index 58ce0a6..7e8c544 100644
--- a/hw/ip/keymgr/rtl/keymgr_pkg.sv
+++ b/hw/ip/keymgr/rtl/keymgr_pkg.sv
@@ -163,40 +163,6 @@
     key_share1: KeyWidth'(32'hFACEBEEF)
   };
 
-  typedef struct packed {
-    logic valid;
-    logic [KmacDataIfWidth-1:0] data;
-    logic [KmacDataIfWidth/8-1:0] strb;
-    // last indicates the last beat of the data. strb can be partial only with
-    // last.
-    logic last;
-  } kmac_data_req_t;
-
-  typedef struct packed {
-    logic ready;
-    logic done;
-    logic [KeyWidth-1:0] digest_share0;
-    logic [KeyWidth-1:0] digest_share1;
-    // Error is valid when done is high. If any error occurs during KDF, KMAC
-    // returns the garbage digest data with error. The KeyMgr discards the
-    // digest and may re-initiate the process.
-    logic error;
-  } kmac_data_rsp_t;
-
-  parameter kmac_data_req_t KMAC_DATA_REQ_DEFAULT = '{
-    valid: 1'b 0,
-    data: '0,
-    strb: '0,
-    last: 1'b 0
-  };
-  parameter kmac_data_rsp_t KMAC_DATA_RSP_DEFAULT = '{
-    ready: 1'b1,
-    done:  1'b1,
-    digest_share0: KeyWidth'(32'hDEADBEEF),
-    digest_share1: KeyWidth'(32'hFACEBEEF),
-    error: 1'b1
-  };
-
   // The following structs should be sourced from other modules
   // defined here temporarily
 
diff --git a/hw/ip/kmac/data/kmac.hjson b/hw/ip/kmac/data/kmac.hjson
index 63574ad..00eaebe 100644
--- a/hw/ip/kmac/data/kmac.hjson
+++ b/hw/ip/kmac/data/kmac.hjson
@@ -68,11 +68,11 @@
       act:     "rcv"
       package: "keymgr_pkg"
     }
-    { struct:  "kmac_data"
+    { struct:  "app"
       type:    "req_rsp"
-      name:    "keymgr_kdf"
+      name:    "app"
       act:     "rsp"
-      package: "keymgr_pkg"
+      package: "kmac_pkg"
     }
     { struct:  "edn"
       type:    "req_rsp"
diff --git a/hw/ip/kmac/dv/env/kmac_env_pkg.sv b/hw/ip/kmac/dv/env/kmac_env_pkg.sv
index c6d230c..49cf875 100644
--- a/hw/ip/kmac/dv/env/kmac_env_pkg.sv
+++ b/hw/ip/kmac/dv/env/kmac_env_pkg.sv
@@ -64,10 +64,10 @@
   parameter int SIDELOAD_KEY_SIZE = $bits(hw_key_req_t);
   // KDF request data has 1 bit for last, and the rest are for data/strb.
   // We subtract 1 from the width of the struct as it includes the valid handshake signal.
-  parameter int KDF_DATA_SIZE = $bits(kmac_data_req_t) - 1;
+  parameter int KDF_DATA_SIZE = $bits(app_req_t) - 1;
   // KDF response data has 2 bits for done/error signals and the rest are for digest shares.
   // We subtract 1 from the struct width as it includes the ready handshake signal.
-  parameter int KDF_DIGEST_SIZE = $bits(kmac_data_rsp_t) - 1;
+  parameter int KDF_DIGEST_SIZE = $bits(app_rsp_t) - 1;
 
   // types
 
diff --git a/hw/ip/kmac/dv/tb.sv b/hw/ip/kmac/dv/tb.sv
index 2ae4462..78d075d 100644
--- a/hw/ip/kmac/dv/tb.sv
+++ b/hw/ip/kmac/dv/tb.sv
@@ -21,8 +21,8 @@
   // keymgr/kmac sideload wires
   keymgr_pkg::hw_key_req_t kmac_sideload_key;
   // kdf wires
-  keymgr_pkg::kmac_data_req_t kdf_req;
-  keymgr_pkg::kmac_data_rsp_t kdf_rsp;
+  kmac_pkg::app_req_t kdf_req;
+  kmac_pkg::app_rsp_t kdf_rsp;
 
   // interfaces
   clk_rst_if clk_rst_if(.clk(clk), .rst_n(rst_n));
@@ -56,8 +56,8 @@
     //
     // TODO: this is set to 0 for the time being to get the csr tests passing.
     //       this will eventually be hooked up to the kmac<->keymgr agent.
-    .keymgr_kdf_i       (keymgr_kmac_if.kmac_data_req ),
-    .keymgr_kdf_o       (keymgr_kmac_if.kmac_data_rsp ),
+    .app_i       (keymgr_kmac_if.kmac_data_req ),
+    .app_o       (keymgr_kmac_if.kmac_data_rsp ),
 
     // Interrupts
     .intr_kmac_done_o   (interrupts[KmacDone]         ),
diff --git a/hw/ip/kmac/kmac.core b/hw/ip/kmac/kmac.core
index 661a037..68778bb 100644
--- a/hw/ip/kmac/kmac.core
+++ b/hw/ip/kmac/kmac.core
@@ -16,8 +16,8 @@
       - lowrisc:ip:sha3
       - lowrisc:ip:edn_pkg
       - lowrisc:prim:edn_req
+      - lowrisc:ip:kmac_pkg
     files:
-      - rtl/kmac_pkg.sv
       - rtl/kmac_core.sv
       - rtl/kmac_msgfifo.sv
       - rtl/kmac_staterd.sv
diff --git a/hw/ip/kmac/rtl/kmac.sv b/hw/ip/kmac/rtl/kmac.sv
index ea75505..a3b3230 100644
--- a/hw/ip/kmac/rtl/kmac.sv
+++ b/hw/ip/kmac/rtl/kmac.sv
@@ -32,8 +32,8 @@
   input keymgr_pkg::hw_key_req_t keymgr_key_i,
 
   // KeyMgr KDF data path
-  input  keymgr_pkg::kmac_data_req_t keymgr_kdf_i,
-  output keymgr_pkg::kmac_data_rsp_t keymgr_kdf_o,
+  input  app_req_t app_i,
+  output app_rsp_t app_o,
 
   // EDN interface
   output edn_pkg::edn_req_t entropy_o,
@@ -685,8 +685,8 @@
     .keymgr_key_i,
 
     // KeyMgr data in / digest out interface
-    .keymgr_data_i (keymgr_kdf_i),
-    .keymgr_data_o (keymgr_kdf_o),
+    .app_i (app_i),
+    .app_o (app_o),
 
     // Secret Key output to KMAC Core
     .key_data_o (key_data),
diff --git a/hw/ip/kmac/rtl/kmac_keymgr.sv b/hw/ip/kmac/rtl/kmac_keymgr.sv
index 49d1738..6194bcb 100644
--- a/hw/ip/kmac/rtl/kmac_keymgr.sv
+++ b/hw/ip/kmac/rtl/kmac_keymgr.sv
@@ -29,8 +29,8 @@
   input keymgr_pkg::hw_key_req_t keymgr_key_i,
 
   // KeyMgr Data in/ Digest out interface + control signals
-  input  keymgr_pkg::kmac_data_req_t keymgr_data_i,
-  output keymgr_pkg::kmac_data_rsp_t keymgr_data_o,
+  input  app_req_t app_i,
+  output app_rsp_t app_o,
 
   // to KMAC Core: Secret key
   output [MaxKeyLen-1:0] key_data_o [Share],
@@ -90,7 +90,7 @@
 
   // Digest width is same to the key width `keymgr_pkg::KeyWidth`.
   localparam int KeyMgrKeyW = $bits(keymgr_key_i.key_share0);
-  localparam int KeyMgrDigestW = $bits(keymgr_data_o.digest_share0);
+  localparam int KeyMgrDigestW = $bits(app_o.digest_share0);
 
   localparam key_len_e KeyLen [5] = '{Key128, Key192, Key256, Key384, Key512};
 
@@ -171,7 +171,7 @@
 
   keyctrl_st_e st, st_d;
 
-  // keymgr_pkg::keymgr_data_rsp_t signals
+  // app_rsp_t signals
   // The state machine controls mux selection, which controls the ready signal
   // the other responses are controled in separate logic. So define the signals
   // here and merge them to the response.
@@ -179,7 +179,7 @@
   logic keymgr_digest_done;
   logic [KeyMgrDigestW-1:0] keymgr_digest [2];
 
-  assign keymgr_data_o = '{
+  assign app_o = '{
     ready:         keymgr_data_ready,
     done:          keymgr_digest_done,
     digest_share0: keymgr_digest[0],
@@ -225,11 +225,11 @@
 
     unique case (st)
       StIdle: begin
-        if (keymgr_data_i.valid && keymgr_key_i.valid) begin
+        if (app_i.valid && keymgr_key_i.valid) begin
           st_d = StKeyMgrMsg;
           // KeyMgr initiates the data
           cmd_o = CmdStart;
-        end else if (keymgr_data_i.valid && !keymgr_key_i.valid) begin
+        end else if (app_i.valid && !keymgr_key_i.valid) begin
           st_d = StKeyMgrErrKeyNotValid;
 
           fsm_err.valid = 1'b 1;
@@ -248,7 +248,7 @@
         mux_sel = SelKeyMgr;
         // Wait until the completion (done) from KeyMgr?
         // Or absorb completion?
-        if (keymgr_data_i.valid && keymgr_data_o.ready && keymgr_data_i.last) begin
+        if (app_i.valid && app_o.ready && app_i.last) begin
           st_d = StKeyMgrOutLen;
         end else begin
           st_d = StKeyMgrMsg;
@@ -328,11 +328,11 @@
 
     unique case (mux_sel)
       SelKeyMgr: begin
-        kmac_valid_o = keymgr_data_i.valid;
-        kmac_data_o  = keymgr_data_i.data;
+        kmac_valid_o = app_i.valid;
+        kmac_data_o  = app_i.data;
         // Expand strb to bits. prim_packer inside MSG_FIFO accepts the bit masks
-        for (int i = 0 ; i < $bits(keymgr_data_i.strb) ; i++) begin
-          kmac_mask_o[8*i+:8] = {8{keymgr_data_i.strb[i]}};
+        for (int i = 0 ; i < $bits(app_i.strb) ; i++) begin
+          kmac_mask_o[8*i+:8] = {8{app_i.strb[i]}};
         end
         keymgr_data_ready = kmac_ready_i;
       end
diff --git a/hw/ip/kmac/rtl/kmac_pkg.sv b/hw/ip/kmac/rtl/kmac_pkg.sv
index 43a219e..6e0403e 100644
--- a/hw/ip/kmac/rtl/kmac_pkg.sv
+++ b/hw/ip/kmac/rtl/kmac_pkg.sv
@@ -90,6 +90,47 @@
     EntropyModeSw   = 2'h 2
   } entropy_mode_e;
 
+  ///////////////////////////
+  // Application interface //
+  ///////////////////////////
+  // MsgWidth : 64
+  // MsgStrbW : 8
+  parameter int unsigned AppDigestW = 256;
+  typedef struct packed {
+    logic valid;
+    logic [MsgWidth-1:0] data;
+    logic [MsgStrbW-1:0] strb;
+    // last indicates the last beat of the data. strb can be partial only with
+    // last.
+    logic last;
+  } app_req_t;
+
+  typedef struct packed {
+    logic ready;
+    logic done;
+    logic [AppDigestW-1:0] digest_share0;
+    logic [AppDigestW-1:0] digest_share1;
+    // Error is valid when done is high. If any error occurs during KDF, KMAC
+    // returns the garbage digest data with error. The KeyMgr discards the
+    // digest and may re-initiate the process.
+    logic error;
+  } app_rsp_t;
+
+  parameter app_req_t APP_REQ_DEFAULT = '{
+    valid: 1'b 0,
+    data: '0,
+    strb: '0,
+    last: 1'b 0
+  };
+  parameter app_rsp_t APP_RSP_DEFAULT = '{
+    ready: 1'b1,
+    done:  1'b1,
+    digest_share0: AppDigestW'(32'hDEADBEEF),
+    digest_share1: AppDigestW'(32'hFACEBEEF),
+    error: 1'b1
+  };
+
+
   ////////////////////
   // Error Handling //
   ////////////////////
diff --git a/hw/top_earlgrey/data/autogen/top_earlgrey.gen.hjson b/hw/top_earlgrey/data/autogen/top_earlgrey.gen.hjson
index 127cacc..c61a128 100644
--- a/hw/top_earlgrey/data/autogen/top_earlgrey.gen.hjson
+++ b/hw/top_earlgrey/data/autogen/top_earlgrey.gen.hjson
@@ -3851,15 +3851,16 @@
           index: -1
         }
         {
-          name: keymgr_kdf
-          struct: kmac_data
-          package: keymgr_pkg
+          name: app
+          struct: app
+          package: kmac_pkg
           type: req_rsp
           act: rsp
           width: 1
           inst_name: kmac
           default: ""
-          top_signame: keymgr_kmac_data
+          end_idx: -1
+          top_signame: kmac_app
           index: -1
         }
         {
@@ -4108,15 +4109,14 @@
         }
         {
           name: kmac_data
-          struct: kmac_data
-          package: keymgr_pkg
+          struct: app
+          package: kmac_pkg
           type: req_rsp
           act: req
           width: 1
           inst_name: keymgr
           default: ""
-          end_idx: -1
-          top_signame: keymgr_kmac_data
+          top_signame: kmac_app
           index: -1
         }
         {
@@ -5479,9 +5479,9 @@
       [
         kmac.keymgr_key
       ]
-      keymgr.kmac_data:
+      kmac.app:
       [
-        kmac.keymgr_kdf
+        keymgr.kmac_data
       ]
       clkmgr_aon.idle:
       [
@@ -11267,15 +11267,16 @@
         index: -1
       }
       {
-        name: keymgr_kdf
-        struct: kmac_data
-        package: keymgr_pkg
+        name: app
+        struct: app
+        package: kmac_pkg
         type: req_rsp
         act: rsp
         width: 1
         inst_name: kmac
         default: ""
-        top_signame: keymgr_kmac_data
+        end_idx: -1
+        top_signame: kmac_app
         index: -1
       }
       {
@@ -11363,15 +11364,14 @@
       }
       {
         name: kmac_data
-        struct: kmac_data
-        package: keymgr_pkg
+        struct: app
+        package: kmac_pkg
         type: req_rsp
         act: req
         width: 1
         inst_name: keymgr
         default: ""
-        end_idx: -1
-        top_signame: keymgr_kmac_data
+        top_signame: kmac_app
         index: -1
       }
       {
@@ -13906,24 +13906,24 @@
         default: ""
       }
       {
-        package: keymgr_pkg
-        struct: kmac_data_req
-        signame: keymgr_kmac_data_req
+        package: kmac_pkg
+        struct: app_req
+        signame: kmac_app_req
         width: 1
         type: req_rsp
         end_idx: -1
-        act: req
+        act: rsp
         suffix: req
         default: ""
       }
       {
-        package: keymgr_pkg
-        struct: kmac_data_rsp
-        signame: keymgr_kmac_data_rsp
+        package: kmac_pkg
+        struct: app_rsp
+        signame: kmac_app_rsp
         width: 1
         type: req_rsp
         end_idx: -1
-        act: req
+        act: rsp
         suffix: rsp
         default: ""
       }
diff --git a/hw/top_earlgrey/data/top_earlgrey.hjson b/hw/top_earlgrey/data/top_earlgrey.hjson
index 7e65c5d..ae8b022 100755
--- a/hw/top_earlgrey/data/top_earlgrey.hjson
+++ b/hw/top_earlgrey/data/top_earlgrey.hjson
@@ -893,7 +893,10 @@
       // KeyMgr Sideload & KDF function
       'otp_ctrl.otp_keymgr_key' : ['keymgr.otp_key'],
       'keymgr.kmac_key'         : ['kmac.keymgr_key']
-      'keymgr.kmac_data'        : ['kmac.keymgr_kdf']
+
+      // KMAC Application Interface
+      'kmac.app'                : ['keymgr.kmac_data']
+
       // The idle connection is automatically connected through topgen.
       // The user does not need to explicitly declare anything other than
       // an empty list.
diff --git a/hw/top_earlgrey/rtl/autogen/top_earlgrey.sv b/hw/top_earlgrey/rtl/autogen/top_earlgrey.sv
index 4976651..908b319 100644
--- a/hw/top_earlgrey/rtl/autogen/top_earlgrey.sv
+++ b/hw/top_earlgrey/rtl/autogen/top_earlgrey.sv
@@ -470,8 +470,8 @@
   edn_pkg::edn_rsp_t [6:0] edn1_edn_rsp;
   otp_ctrl_pkg::otp_keymgr_key_t       otp_ctrl_otp_keymgr_key;
   keymgr_pkg::hw_key_req_t       keymgr_kmac_key;
-  keymgr_pkg::kmac_data_req_t       keymgr_kmac_data_req;
-  keymgr_pkg::kmac_data_rsp_t       keymgr_kmac_data_rsp;
+  kmac_pkg::app_req_t       kmac_app_req;
+  kmac_pkg::app_rsp_t       kmac_app_rsp;
   logic [3:0] clkmgr_aon_idle;
   jtag_pkg::jtag_req_t       pinmux_aon_lc_jtag_req;
   jtag_pkg::jtag_rsp_t       pinmux_aon_lc_jtag_rsp;
@@ -1965,8 +1965,8 @@
 
       // Inter-module signals
       .keymgr_key_i(keymgr_kmac_key),
-      .keymgr_kdf_i(keymgr_kmac_data_req),
-      .keymgr_kdf_o(keymgr_kmac_data_rsp),
+      .app_i(kmac_app_req),
+      .app_o(kmac_app_rsp),
       .entropy_o(edn0_edn_req[3]),
       .entropy_i(edn0_edn_rsp[3]),
       .idle_o(clkmgr_aon_idle[2]),
@@ -2010,8 +2010,8 @@
       .aes_key_o(),
       .hmac_key_o(),
       .kmac_key_o(keymgr_kmac_key),
-      .kmac_data_o(keymgr_kmac_data_req),
-      .kmac_data_i(keymgr_kmac_data_rsp),
+      .kmac_data_o(kmac_app_req),
+      .kmac_data_i(kmac_app_rsp),
       .otp_key_i(otp_ctrl_otp_keymgr_key),
       .otp_hw_cfg_i(otp_ctrl_otp_hw_cfg),
       .flash_i(flash_ctrl_keymgr),