Skip to content

[otbn] Specify the OTBN side of the KMAC interface#30278

Merged
nasahlpa merged 1 commit into
lowRISC:masterfrom
etterli:otbn-spec-kmac-if
Jun 15, 2026
Merged

[otbn] Specify the OTBN side of the KMAC interface#30278
nasahlpa merged 1 commit into
lowRISC:masterfrom
etterli:otbn-spec-kmac-if

Conversation

@etterli

@etterli etterli commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

The last commit of this PR specifies the CSR/WSR based KMAC interface which OTBN SW can make use of to offload hashing operations.

Only the last commit is relevant. All other commits are part of #30141 (RTL of KMAC side of interface) or #30143 (required DV changes and RTL changes of IPs already using the app interface). Changes are expected for these PRs.

@etterli etterli force-pushed the otbn-spec-kmac-if branch from 008c1dd to 52e97b0 Compare June 3, 2026 12:04
@etterli etterli force-pushed the otbn-spec-kmac-if branch 3 times, most recently from 9e459fa to 94c3b1d Compare June 3, 2026 12:40
Comment thread hw/ip/otbn/data/csr.yml Outdated
Comment thread hw/ip/otbn/data/csr.yml Outdated
Comment thread hw/ip/otbn/data/csr.yml Outdated
Comment thread hw/ip/otbn/data/csr.yml Outdated
Comment thread hw/ip/otbn/data/csr.yml
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
@etterli etterli force-pushed the otbn-spec-kmac-if branch 2 times, most recently from ed19225 to 1dad8a2 Compare June 3, 2026 14:14

@etterli etterli left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fast review @rswarbrick . Your feedback is incorporated.

Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
- In case of a timeout OTBN can either wait longer or abort the execution.
If the OTBN aborts, the interface starts a recovery operation, see error handling for more details.
- Write a message into `KMAC_DATA_S0` and `KMAC_DATA_S1` (share 0 and 1, respectively) and set the corresponding strobe in `KMAC_STRB`.
- All messages except the last one must be 32 bytes (256 bits) long, i.e., `KMAC_STRB = '1`.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this question. What do you mean with 0x3f? There is no value which is 63.

- The digest data always comes in a shared representation.
To recover the plaintext digest the values read from `KMAC_DATA_S0` and `KMAC_DATA_S1` must be XORed.
- Once both WSRs, `KMAC_DATA_S0` and `KMAC_DATA_S1`, have been read the interface clears `KMAC_STATUS.RSP_VALID` and the interface will accept the next digest part from the app interface.
(i.e., the `KMAC_STATUS.RSP_VALID` bit serves as back-pressure.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The KMAC_STATUS.RSP_VALID bit shows the validity of the current response. But the hardware / interface uses it to drive the ready signal of the response channel. Obviously its inverted value; if it is cleared because the current response has been consumed then we can accept the next response.

Comment thread hw/ip/otbn/data/csr.yml Outdated
Comment thread hw/ip/otbn/data/csr.yml Outdated
Comment thread hw/ip/otbn/data/csr.yml Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated

@rswarbrick rswarbrick left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for all the changes!

Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
- In case of a timeout OTBN can either wait longer or abort the execution.
If the OTBN aborts, the interface starts a recovery operation, see error handling for more details.
- Write a message into `KMAC_DATA_S0` and `KMAC_DATA_S1` (share 0 and 1, respectively) and set the corresponding strobe in `KMAC_STRB`.
- All messages except the last one must be 32 bytes (256 bits) long, i.e., `KMAC_STRB = '1`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn! I meant to give the hex value that corresponds to '1, which I assumed had every bit set. Rather stupidly, I then calculated 2*32 - 1, rather than 2^32-1...

Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
If the OTBN aborts, the interface starts a recovery operation, see error handling for more details.
- Write a message into `KMAC_DATA_S0` and `KMAC_DATA_S1` (share 0 and 1, respectively) and set the corresponding strobe in `KMAC_STRB`.
- All messages except the last one must be 32 bytes (256 bits) long, i.e., `KMAC_STRB = '1`.
- The last message is allowed to be a partial message and it can be 1 to 32 bytes long.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, good point! If we don't have a use for the feature, this seems perfectly sensible.

- The digest data always comes in a shared representation.
To recover the plaintext digest the values read from `KMAC_DATA_S0` and `KMAC_DATA_S1` must be XORed.
- Once both WSRs, `KMAC_DATA_S0` and `KMAC_DATA_S1`, have been read the interface clears `KMAC_STATUS.RSP_VALID` and the interface will accept the next digest part from the app interface.
(i.e., the `KMAC_STATUS.RSP_VALID` bit serves as back-pressure.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry! I think I've got confused.

At the moment, the text says "the interface clears KMAC_STATUS.RSP_VALID". Do you mean that the OTBN hardware notices that the two WSRs have been read, then clears the RSP_VALID flag? (That's what I took the text to mean)

Or do you mean that the SW should clear the RSP_VALID flag to accept the data? That would be a back-pressure signal, but I think the active party needs changing: maybe "the OTBN code clears" or something?

Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
@etterli etterli force-pushed the otbn-spec-kmac-if branch from e1cb124 to 72d8f4c Compare June 4, 2026 07:12
Comment thread hw/ip/otbn/data/csr.yml Outdated
Comment thread hw/ip/otbn/data/csr.yml Outdated
Comment thread hw/ip/otbn/data/csr.yml
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md
Comment thread hw/ip/otbn/doc/theory_of_operation.md

@etterli etterli left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @andrea-caforio for the questions. I have answered them and also redesigned the interface such that a process command is always required (a partial message no longer issues a process command).

Comment thread hw/ip/otbn/data/csr.yml Outdated
Comment thread hw/ip/otbn/data/csr.yml
Comment thread hw/ip/otbn/data/csr.yml Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md
@etterli etterli force-pushed the otbn-spec-kmac-if branch 6 times, most recently from f276e15 to 61acfdc Compare June 5, 2026 07:04
Comment thread hw/ip/otbn/doc/theory_of_operation.md
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/data/csr.yml Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/data/csr.yml Outdated
@etterli etterli force-pushed the otbn-spec-kmac-if branch from 61acfdc to 5f41a7d Compare June 5, 2026 09:27

@etterli etterli left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @nasahlpa. I fixed your findings.

Comment thread hw/ip/otbn/data/csr.yml Outdated
Comment thread hw/ip/otbn/data/csr.yml Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
@etterli etterli force-pushed the otbn-spec-kmac-if branch 2 times, most recently from 5f19c2e to d875c5b Compare June 5, 2026 15:08
@etterli

etterli commented Jun 10, 2026

Copy link
Copy Markdown
Contributor Author

The latest version features:

  • Rebased on the newest version of [kmac] Extend KMAC app interface with a dynamic interface type #30143 (minimized DV changes and squashed everything).
  • The KMAC_CTRL CSR is now split into KMAC_CTRL and KMAC_CFG.
  • Introduced a new command CLOSE which OTBN SW must issue after the finish response was checked for errors.
    • This is required to have a way to signal to the interface when OTBN SW has handled the finish response and a new session can be started.
  • Improved the secure wipe behaviour description.

@etterli etterli force-pushed the otbn-spec-kmac-if branch 2 times, most recently from 5c24761 to 4ad932d Compare June 10, 2026 16:16
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
@etterli etterli force-pushed the otbn-spec-kmac-if branch from 4ad932d to 4c5a43e Compare June 11, 2026 12:32
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
- The session request is rejected because:
- The requested hashing configuration is invalid.
- System SW did not set `CFG_SHADOWED.entropy_ready` of the KMAC HWIP before OTBN SW is started.
- This error is reported with the first response after the `KMAC_CTRL.PROCESS` command.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we only get this error after sending the PROCESS command? Already when sending the START command we should know whether the config is valid.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inside the KMAC the invalid config is detected immediately but the interface still goes to the "message absorb" phase. But no data is forwarded to the actual SHA3 core. I designed the interface this way because:

  • For the static interface this behaviour is required for compatibility reasons. I wanted to keep the dynamic behaviour similar. I aggree, this is not a strong argument. We can relatively easily extend the dynamic interface such that it reports this after the START command. This extension is foreseen like the option to send a prefix.
  • This behaviour simplifies the implementation of on application side. There is one state less to implement and verify. And we would need a way that OTBN SW can signal to the interface that it has checked the response. See also answer regarding why the CLOSE command is required.
  • Knowing that the configuration was invalid directly after sending the configuration speeds up only the error case. Usually we can assume OTBN SW send a valid configuration and system SW did setup the KMAC properly. Having to check a response after the START command adds actually latency for the happy case.

I agree, delaying the error response until the full message has been sent is not the cleanest. However, I think this is acceptable. Does this clarify your question and do you accept it like this?
If not, I'm also happy to extend the app interface with such an immediate response.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that's all perfect and reasonable. Thanks for explaining! Let's leave this one open for the future.

Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
- Can occur at any time when receiving digest data.
- See the KMAC HWIP documentation for error causes.

In case the polling times out, OTBN SW can either wait longer or abort the execution completely.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence is ambiguous because it suggests that there is a hardware timeout. I don't think you need this sentence and can instead just say: If any of these errors happens, OTBN SW needs to abort the execution and system SW then has to take care of recovering the KMAC HWIP.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that the 1st error (KMAC_STATUS.READY remains low after..) is non recoverable. And the interface can also not send any PRCOESS/DONE command. This error case happens only if the KMAC fails to accept a request. In this case, OTBN must abort the program execution and system SW must somehow start a recovery on the KMAC side. This is currently not yet supported by KMAC. But this feature then should somehow accept the pending requests such that the "recovery mode" of the OTBN side can end the session.

In contrast, if any of the other two errors occurs, OTBN SW can still end the session regularly (it must).

I will try to rewrite this distinction.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest push now cleans up this section.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this section much clearer now!

My point regarding the timeout is that waiting even longer will not help. If the bit remains low for say more than 100 clock cycles, KMAC is dead. But I agree that this is different from the other two errors.

Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
Comment thread hw/ip/otbn/doc/theory_of_operation.md Outdated
@vogelpi

vogelpi commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

The latest version features:

* Rebased on the newest version of [[kmac] Extend KMAC app interface with a dynamic interface type #30143](https://github.com/lowRISC/opentitan/pull/30143) (minimized DV changes and squashed everything).

* The KMAC_CTRL CSR is now split into KMAC_CTRL and KMAC_CFG.

* Introduced a new command `CLOSE` which OTBN SW must issue after the finish response was checked for errors.
  
  * This is required to have a way to signal to the interface when OTBN SW has handled the finish response and a new session can be started.

Question: why is it not sufficient to just perform the finish response handshake (IIRC a result of sending the DONE command) and then free the session?

* Improved the secure wipe behaviour description.

Thanks, this is a lot simpler now and much easier to understand in my view.

@etterli

etterli commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author
  • Introduced a new command CLOSE which OTBN SW must issue after the finish response was checked for errors.

    • This is required to have a way to signal to the interface when OTBN SW has handled the finish response and a new session can be started.

Question: why is it not sufficient to just perform the finish response handshake (IIRC a result of sending the DONE command) and then free the session?

The flow is:

  • OTBN SW issues the DONE command, signalling that it has read the amount of digest it requires.
  • This triggers the OTBN interface side to send the termination request. It then waits for the finish response.
  • Once the finish response arrives, the RSP_VALID flag is set and the RSP_ERROR flag is updated.
  • In parallel the OTBN SW polls the RSP_VALID flag.
  • Once RSP_VALID = 1, OTBN SW can read the RSP_ERROR flag and handle accordingly (This RSP_ERROR flag must be checked and may not be set, otherwise all digest data must be considered as invalid).

Now the problem is that the interface does not know when OTBN SW did read the RSP_ERROR flag. And thus it does not know when it can clear the RSP_VALID and all other error flags. And this is required to be ready for the next session. The CLOSE command is a clean way how OTBN SW can signal to the interface that it has read all flags and now longer needs the information.

Note that once the finish response arrived, the session is fully terminated on the KMAC side. It is just a OTBN side problem.

I thought about two approaches how to avoid a CLOSE command. But both have awkward consequences.

  • Reading KMAC_STATUS after issuing the DONE command when RSP_VALID is 1 signals the end of the session.
    • I dislike introducing any read side-effects for CSRs. This is very uncommon and error prone.
    • Concretely, this is ugly because it limits the OTBN SW to read the result of the response exactly once. So any polling loop must check the RSP_VALID flag and also temporary store the value of the RSP_ERROR flag. This makes writing SW challenging.
  • Flags in KMAC_STATUS (except READY) keep their value until another START command is issued.
    • This would work but I don't like it that KMAC_STATUS does not have a well defined state after a session.

@etterli etterli force-pushed the otbn-spec-kmac-if branch from 4c5a43e to 6ccdde2 Compare June 11, 2026 15:17
@vogelpi

vogelpi commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Thanks @etterli for the latest updates including the reasoning of the DONE command. I wasn't aware that this is an OTBN-only thing (I thought you would send this also across to KMAC). But like you explained it, it makes sense.

@nasahlpa nasahlpa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Pascal - I had a look into the updates spec and it looks good to me.

This adapts the current simulator-only KMAC interface specification such that the new dynamic
KMAC app interface can be used.

Signed-off-by: Pascal Etterli <pascal.etterli@lowrisc.org>
@etterli etterli force-pushed the otbn-spec-kmac-if branch from 6ccdde2 to 32a9840 Compare June 12, 2026 13:53
@etterli etterli marked this pull request as ready for review June 12, 2026 13:53
@etterli

etterli commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on master to include the now merged KMAC interface implementation.

@etterli

etterli commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

It now also specifies that KMAC_CFG has duplicated fields.

Comment thread hw/ip/otbn/data/csr.yml
This can be SHA3, SHAKE, cSHAKE, or KMAC.
See KMAC HWIP for encoding of values.
Note, cSHAKE uses prefix from KMAC HWIP CSRs (configured by SW), and KMAC always uses hard coded "KMAC" prefix.
15-6: Reserved. Any write is ignored. Always reads as 0.

@etterli etterli Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory all config bits would fit within 12 bits and we could use one ADDI to load the config. But to be prepared for eventual onehot encodings and for better SW code readability I chose a power of two offset. One instruction and one cycle more should not make the difference.

@etterli etterli added the CI:Rerun Rerun failed CI jobs label Jun 15, 2026
@github-actions github-actions Bot removed the CI:Rerun Rerun failed CI jobs label Jun 15, 2026
@nasahlpa nasahlpa added this pull request to the merge queue Jun 15, 2026
Merged via the queue into lowRISC:master with commit 058efa9 Jun 15, 2026
69 of 72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants