-
Notifications
You must be signed in to change notification settings - Fork 778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[kmac] Abort when sideload key is invalid during operation #22794
[kmac] Abort when sideload key is invalid during operation #22794
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for investigating and fixing this @andreaskurth ! The code looks mostly good but there seems to be a bug in one of the new states which may cause KMAC to send a done pulse on the wrong app interface.
As for the new issue of misbehaving HW app interfaces stalling KMAC: I think the current behavior may be acceptable but it's probably worth to have a separate discussion. Would you mind creating a new GitHub issue please?
85510b9
to
db8e322
Compare
db8e322
to
80d59ed
Compare
93bb7e1
to
8df6407
Compare
Thanks for flagging this, @vogelpi. I have fixed this problem, alongside with two others I found. May I ask you to review the code again?
Yes, I'll create an issue for that when merging this PR. It's tracked in the follow-up tasks in the PR description. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great, thanks @andreaskurth for fixing this and explaining what's going on. Nice work!
5f53b24
to
eae0444
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix @andreaskurth. LGTM, but I must admit I could not fully review the full FSM transitions.
Restoring KMAC from an error has always been a bit tricky from SW side. I will have to keep that in mind that for the cryptolib. This PR really does not touch that clearing mechanism, so I think we should be fine (except for relocating err_processed
bit, for which this PR also includes changes).
The following top level tests might provide some coverage (although they are not targeting this particular fix):
- sw/device/tests/keymgr_sideload_kmac_test.c
- sw/device/tests/kmac_entropy_test.c
Thx for the review @ballifatih. Actually the |
8f27950
to
11fafff
Compare
Alright, I think I fixed the problems indicated by the failing
|
cd2f31e
to
1cb6c45
Compare
Waiting for #22935 (or another fix for the ROM_EXT problem that currently breaks the corresponding CI check) to be merged, then I'll rebase this. I'd rather not merge it without getting a green light from those checks, because this modifies the kmac-keymgr sideload interface, which is relevant for booting into ROM_EXT. To get some confidence from simulation tests, I ran the relevant TLTs (
|
The `CFG_SHADOWED` CSR gets write-locked while the SHA3 module is operating. This makes sense for configuration values but it is problematic for the `err_processed` bit, through which SW acknowledges that it has handled an error occurring while KMAC/SHA3 is operating, because that acknowledgment would then not propagate to KMAC and it would get stuck. Signed-off-by: Andreas Kurth <[email protected]>
This commit fixes four problems in KMAC's error handling: 1) App interface done after SW has processed error Before this commit, when the FSM in `kmac_app` was in its `StError` state and SW acknowledged the error by writing the `err_processed` bit in the `CFG_SHADOWED` CSR, the FSM would go back to the `StIdle` state. If the active HW app interface had not provided all data before this SW-induced transition to the `StIdle` state, KMAC was no longer ready to absorb the remaining data, thereby stalling the HW app interface. This commit extends the FSM in `kmac_app` so that when an error happens it doesn't matter whether SW acknowledges the error before or after the HW app interface has provided all data. In both cases, the FSM now waits for SW to acknowledge *and* the HW app interface to provide all data before it returns to `StIdle`. To implement this, two new states (`StErrorAwaitSw` and `StErrorAwaitApp`) get added. In block-level DV, `kmac_key_error_vseq` waited for the *first* data item instead of the *last* before stopping to send items on the active HW app interface and continuing with the vseq (acknowledging the error from SW and going to the next iteration). This no longer works because KMAC now remains in `StError` until the HW app interface has send the last data item, so that vseq now correctly sends all data items including the last on the HW app interface. This may raise a new issue: If the HW app interface misbehaves, KMAC will remain in the error state until it gets reset. To remediate this, we could add a mechanism for SW to clear KMAC's state regardless of the state of the HW app interface. 2) SHA3 core not told to process Before this commit, when the FSM in `kmac_app` went through the `StError` state, it would not tell the SHA3 core to process the data so that its internal state would get flushed. This commit extends the FSM in `kmac_app` so that `CmdProcess` is signaled on `cmd_o` to the SHA3 core when the error is handled, and lets the FSM wait for the completion of SHA3 (`absorbed_i` input) before returning to idle. To implement this, a new state (`StErrorWaitAbsorbed`) is added. 3) Signaling completion to SW when an error has occurred in SW mode Before this commit, when an error would occur in SW mode (`mux_sel == SelSw`), the `absorbed_o` output, which in turn triggers the `kmac_done` IRQ would not be raised. This case could not happen because no errors were raised in SW mode (such errors will get raised with the next commit), but it's still wrong. This commit extends the FSM in `kmac_app` with a side-band state bit (`err_during_sw_q`) and uses that to set `absorbed_o` when an error that occurred during SW mode got handled. 4) Clearing of `kmac_errchk` Before this commit, the state of the `kmac_errchk` would not get cleared after the error has been handled. Thus the next invocation of KMAC could start with a residual state in `kmac_errchk` and incorrectly fail. This commits adds a MuBi-encoded signal from `kmac_app` to `kmac_errchk` through which the former can clear the latter after an error. Signed-off-by: Andreas Kurth <[email protected]>
When `kmac_app` aborts an operation as soon as the sideload key becomes invalid (which it will do in a couple of commits) the current stability assertions on `key_len_i` and `key_data_i` in `kmac_core` would be violated. This is not a problem: `kmac_core` *should* use an invalid, useless key in that case. To prevent these assertions from failing, this commit introduces a signal with which `kmac_app` indicates to `kmac_core` whether `key_len_i` and `key_data_i` are valid and rewrites the assertions to take that valid signal into account. Signed-off-by: Andreas Kurth <[email protected]>
Signed-off-by: Andreas Kurth <[email protected]>
…function Signed-off-by: Andreas Kurth <[email protected]>
Signed-off-by: Andreas Kurth <[email protected]>
Signed-off-by: Andreas Kurth <[email protected]>
Signed-off-by: Andreas Kurth <[email protected]>
The KMAC HW IP block features an option to load keys from Key Manager via a HW key sideload interface. Prior to this commit, KMAC would: - when used via the SW application interface: *not check at all* if the sideload key is valid (issue lowRISC#10704, lowRISC#16855); - when used via a HW application interface: check if the sideload key is valid *only for a single cycle* when the application interface gets configured (state `StAppCfg` in `kmac_app`). This could lead to cases where KMAC would use an invalid sideload key. This commit fixes the problem by checking whether the sideload key is valid in *every* FSM state in which the sideload key is used. If the sideload key is invalid even for a single cycle (the FSM cannot know whether the key is being used in this exact cycle or not), `kmac_app`'s FSM goes into the `StKeyMgrErrKeyNotValid` state. In that state, the FSM signals the `keymgr_pkg::ErrKeyNotValid` error code in KMAC's `err_code` CSR. The FSM then transitions to the `StError` state, where it drains data from the HW application interface by keeping `app_o.ready` high. The digest output remains all-zero (it can only take a non-zero value in the `StAppWait` state). The FSM exits the `StError` state after SW has signalled that it has processed the error by writing the `processed` bit in the `CFG_SHADOWED` CSR *and* the active HW app interface has sent the last data item. This commit resolves lowRISC#10704 and implements the RTL part of lowRISC#16855. Covering this in DV remains open, although the existing tests (which don't cover this) keep their previous pass rates. Signed-off-by: Andreas Kurth <[email protected]>
Signed-off-by: Andreas Kurth <[email protected]>
Signed-off-by: Andreas Kurth <[email protected]>
1cb6c45
to
b961743
Compare
Update: rebased on top of #22940. |
Thx! With that, CI finally passes. Merging |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM modulo the one hjson bit that might have snuck in (?)
@@ -99,6 +99,7 @@ | |||
{ | |||
name: "{name}_sideload" | |||
uvm_test_seq: kmac_sideload_vseq | |||
run_opts: ["+test_timeout_ns=1_000_000_000"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this supposed to be included? It's very long.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is necessary because kmac now drains all data beats on the HW app interface when there's an error.
Re this being very long -- time is relative, right? :-) kmac block level DV simulates at a relatively high rate (simulated time over wall clock time), and other tests have an even higher timeout (e.g., kmac_long_msg_and_output
above has 10 s).
PR lowRISC#22794 moved the `err_processed` field from the `CFG_SHADOWED` shadow register to the `CMD` register. However, the `shadow_reg_wr` function was not adapted. This function assumes that writing to the `CFG_SHADOWED` reg clears the update error field. As this now can only be done with the `CMD` register, this is not true anymore. Hence, this commit removes this assumption. Closes lowRISC#23083. Signed-off-by: Pascal Nasahl <[email protected]>
PR #22794 moved the `err_processed` field from the `CFG_SHADOWED` shadow register to the `CMD` register. However, the `shadow_reg_wr` function was not adapted. This function assumes that writing to the `CFG_SHADOWED` reg clears the update error field. As this now can only be done with the `CMD` register, this is not true anymore. Hence, this commit removes this assumption. Closes #23083. Signed-off-by: Pascal Nasahl <[email protected]>
The KMAC HW IP block features an option to load keys from Key Manager
via a HW key sideload interface. Prior to this commit, KMAC would:
sideload key is valid (issue [kmac] kmac does not check key validation in sw initiated sequence #10704, [kmac] Expecting error after clearing sideload key #16855);
valid only for a single cycle when the application interface gets
configured (state
StAppCfg
inkmac_app
).This could lead to cases where KMAC would use an invalid sideload key. This PR fixes that by raising an error in these cases.
To enable that, this PR moves the
err_processed
bit from theCFG_SHADOWED
CSR to theCMD
CSR, and this PR fixes three problems with KMAC's error handling (some of which would only occur with the newly introduced error).Please see the commit messages for details on the changes in this PR.
Block-level tests maintain their pass rate (tested with
-i all -rx 0.1
).Follow-up tasks