[kmac] Possible HW changes for reducing noise during SCA of SHA3/KMAC
During SCA, we try to minimize the noise in measurements as much as possible by disabling any peripherals that we are not interested in. Another step that we take is to put Ibex to sleep during the target operation to reduce the noise that it generates.
For SCA of AES, we first load the data to AES_DATA_IN_* registers, then start the encryption manually by writing to the AES_TRIGGER register. AES is configured to start 40 cycles after this which gives us enough time to put Ibex to sleep after which we capture traces of the encryption operation.
It would be great if could do something similar in KMAC, e.g. by not processing the data written into the FIFO until a write to a register + some delay and generating an interrupt when the absorb step is complete. Note that interrupt is not necessary since we can also use the timer to wake Ibex.
Please see #7757 and these comments (1, 2) in that PR for context and more information.
cc: @eunchan, @moidx, @vogelpi, @vrozic, @mundaym.
So, if I understand correctly, this is for SCA right? If we have that feature in KMAC, would it hurt the Side-channel leakage in prod ? Based on your description, running Hashing ops whenever the module can actually creates a noise (due to the Ibex) so that it disturbs the SCA.
I think we can't put that in KMAC as KMAC DV is cycle-accurate, so that delaying the process takes time to change DV environment. We can, however, put that only for FPGA by using ifdef inside the design. But the delay can't be configurable.
CC: @cdgori @tjaychen @udinator
@eunchan i think @alphan is referring to something like this.
So to your point, yes this is definitely something we will need to disable for production.
although @alphan , how exactly should this work? Because Eunchan i think specifically added a feature to ignore incoming data until software has told KMAC to start. I think this was done largely so that random data writes could not eventually lock-up the kmac (@eunchan let me know if i'm wrong).
Sounds like what we really want to be able to do is to fill up the FIFO, and then push "start" effectively.
@sriyerg Let's continue the discussion, but keep in mind that we have a pretty high bar for new feature requests. What's the impact if we don't do this?
@dsandrs i don't really view this as a "feature", specifically, not something I would expect DV to verify. This is more like a SCA specific optimization, and if we do it, should not show up in the stuff DV has to look at.
This of course means the change probably needs to be tiny.
This change is basically so that we can decouple the noise from Ibex in the power traces. If there is a parameter we can use to insert a delay, it makes our live much easier for SCA evaluation. This parameter does not need DV coverage, and as @tjaychen suggested, would be identical to this one and only used in pre-si:
https://github.com/lowRISC/opentitan/blob/master/hw/ip/aes/rtl/aes.sv#L19
i should add that this might be slightly more complicated for kmac.. since kmac actually encodes the sequence that it will not accept software data until we start the operation. Eunchan will know best on whether this change is doable, but it is a case we specifically didn't allow.
On Thu, Aug 19, 2021 at 10:41 AM moidx @.***> wrote:
This change is basically so that we can decouple the noise from Ibex in the power traces. If there is a parameter we can use to insert a delay, it makes our live much easier for SCA evaluation. This parameter does not need DV coverage, and as @tjaychen https://github.com/tjaychen suggested, would be identical to this one and only used in pre-si:
https://github.com/lowRISC/opentitan/blob/master/hw/ip/aes/rtl/aes.sv#L19
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lowRISC/opentitan/issues/7841#issuecomment-902113030, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAH2RST3D2IFGM3EXPKCRXDT5U633ANCNFSM5COKL6LQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .
I think we can't put that in KMAC as KMAC DV is cycle-accurate, so that delaying the process takes time to change DV environment. We can, however, put that only for FPGA by using
ifdefinside the design. But the delay can't be configurable.
@eunchan @alphan @moidx just to make sure I'm on the same page, the SCA-related change here is referring to stopping the msgFIFO from automatically sending data to SHA3 logic once it has filled up even if message isn't complete? If so, I think that has a pretty significant DV impact.
@udinator in the SCA application, we are currently only using messages that can fit the msgFIFO.
Ideally, the sequence would be that Ibex 1) configures KMAC, 2) writes the data into the msgFIFO, and 3) triggers the processing by writing to the CMD register. From triggering the processing until it actually starts we need a delay such that Ibex can go to sleep. This delay would be configurable via a Sec parameter. It wouldn't be enabled on ASIC and also wouldn't need verification.
If I understand correctly, we have the following sequence currently:
- configure KMAC
- write START into CMD register: this triggers the loading of the key and thus is relevant for SCA already
- write data into the msgFIFO: since Ibex and the bus are involved, this heavily disturbs the measurement of 2.
- write PROCESS into CMD register: this triggers the flushing of the msgFIFO into KMAC and then triggers the actual SHA3 operation we want to measure.
Now for SCA we need to change this to 1. 3. 2. 4. For this we need to modify the DIF (or we can simply implement a custom function as part of the SCA library). At the moment, the DIF only allows writing the msgFIFO after START has been written to the command register. But there seems to be no such restriction in the hardware.
What we need in hardware is a delay on the reg2hw.cmd.cmd.qe signal inside kmac.sv. Basically, if Ibex writes to the command register it must be able to go into sleep before KMAC sees the new command. This change is very simple and we use it only on FPGA. See this example from AES: https://github.com/lowRISC/opentitan/blob/master/hw/ip/aes/rtl/aes_control.sv#L207-L227
In addition, things could be improved if we added a new command START_AND_PROCESS that would trigger the two operations one after another. We could also control this command with a parameter to ignore it on ASIC. This change would simplify the trace processing and massively improve capture speed (Ibex not involved at all during capture, fewer samples per trace).
Changing the order cannot be an option in KMAC. It already built on the assumption of START -> Msg -> Process . So the logic discards any unwanted data access (basically outside of START <-> Process).
I really don't know how it can be achieved to store the message inside MsgFIFO and initiate the hash process WITHOUT heavily modifying the KMAC DV environment. The design also requires to be changed a lot.
One option would be to make a separate KMAC only for SCA analysis. There will be no DV for that. Top will be unique. DIF for KMAC also will be unique.
I don't see any other option unless we are willing to spend significant time on changing the Design and DV.
On Aug 20, 2021, at 2:15 AM, Pirmin Vogel @.***> wrote:
@udinator https://github.com/udinator in the SCA application, we are currently only using messages that can fit the msgFIFO.
Ideally, the sequence would be that Ibex 1) configures KMAC, 2) writes the data into the msgFIFO, and 3) triggers the processing by writing to the CMD register. From triggering the processing until it actually starts we need a delay such that Ibex can go to sleep. This delay would be configurable via a Sec parameter. It wouldn't be enabled on ASIC and also wouldn't need verification.
If I understand correctly, we have the following sequence currently:
configure KMAC write START into CMD register: this triggers the loading of the key and thus is relevant for SCA already write data into the msgFIFO: since Ibex and the bus are involved, this heavily disturbs the measurement of 2. write PROCESS into CMD register: this triggers the flushing of the msgFIFO into KMAC and then triggers the actual SHA3 operation we want to measure. Now for SCA we need to change this to 1. 3. 2. 4. For this we need to modify the DIF (or we can simply implement a custom function as part of the SCA library). At the moment, the DIF only allows writing the msgFIFO after START has been written to the command register. But there seems to be no such restriction in the hardware.
What we need in hardware is a delay on the reg2hw.cmd.cmd.qe signal inside kmac.sv. Basically, if Ibex writes to the command register it must be able to go into sleep before KMAC sees the new command. This change is very simple and we use it only on FPGA. See this example from AES: https://github.com/lowRISC/opentitan/blob/master/hw/ip/aes/rtl/aes_control.sv#L207-L227 https://github.com/lowRISC/opentitan/blob/master/hw/ip/aes/rtl/aes_control.sv#L207-L227 In addition, things could be improved if we added a new command START_AND_PROCESS that would trigger the two operations one after another. We could also control this command with a parameter to ignore it on ASIC. This change would simplify the trace processing and massively improve capture speed (Ibex not involved at all during capture, fewer samples per trace).
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lowRISC/opentitan/issues/7841#issuecomment-902556049, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJDG3TD23PKZ736LTD34RTT5YMLVANCNFSM5COKL6LQ. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email.
@udinator in the SCA application, we are currently only using messages that can fit the msgFIFO.
@vogelpi thanks for the clarifications on this.
I agree with @eunchan, this seems like a change to the fundamental KMAC design, which is going to have a large impact on both design and DV sides.
Hey @eunchan, @udinator ,
I wouldn't worry too much about the DV environment. Whether we add a Sec parameter to support the desired behavior or designed a separate KMAC version only for SCA, we wouldn't verify it to the same extent of the real KMAC. We do the exact same thing for AES. There we also transfer the actual output data of every 100th block to the capture system and run a model in parallel to detect if something would go wrong in the analyzed system. This is sufficient for verifying the SCA IP version.
I've been looking at the KMAC RTL and I think I've found the code responsible for discarding data input outside of START <-> PROCESS: the FSM inside kmac_app.sv first wants to see the START command before going in StSw state where the muxes are set up correctly. I think we could work around that. Or are there other places that need modification @eunchan ?
I am sorry for being persistent about this @eunchan and @udinator . Out intention is to experimentally verify the masking inside KMAC. The countermeasures added on top (like the one we are discussing to bypass) are also important but ultimately the masking is the most fundamental CM against SCA and we need to be sure it's done right. The issues with the noise generated from Ibex are that 1) we can't filter it out using some post-processing as it's overlaid with KMAC operation, and 2) due to the large interconnect being involved, the noise is much stronger than the actual signal, especially on FPGA. This makes the verification of the masking extremely difficult if not impossible.
HI Pirmin,
I understand why you try to change the KMAC flow and its importance. I am worrying more of the way of the implementation.
First thing first, I want to confirm my assumption on this issue. "The change for KMAC in order to capture the power signature is for FPGA and silicon both."
I think eventually we may need to do Side Channel Analysis on the silicon too. In that case, I assume the Ibex power noise will be visible again unless KMAC adopts the change of the process on the silicon
If we choose to use the suggested scheme on silicon too, it degrades the KMAC security as the attacker can effectively separate out the KMAC from other modules' power noises. We may block this behavior on PROD and only allow on the TEST. If both flows co-exist in the KMAC design, I assume DV folks will be nervous not verifying it :)
If the change is wholly for FPGA, then as I suggested in the previous thread, it would be better to be a separate IP (`kmac_sca`) to lessen the worries from DV folks. If the change is implemented as a build-time parameter and never planned to be used on silicon, it just disturb the code readability in my opinion.
So, the main purpose is to verify the effectiveness of the DOM AND logic inside the SHA3. Then, wouldn't it be better to prepare the IP specific to the matter? sha3.sv itself is standalone IP (works quite similar to the prim cells). So, it won't be difficult to make another IP only for the purpose. SW can write secret key directly to MSG FIFO and fire the SHA3 up.
Thanks, Eunchan
On Aug 23, 2021, at 3:07 AM, Pirmin Vogel @.***> wrote:
Hey @eunchan https://github.com/eunchan, @udinator https://github.com/udinator , I wouldn't worry too much about the DV environment. Whether we add a Sec parameter to support the desired behavior or designed a separate KMAC version only for SCA, we wouldn't verify it to the same extent of the real KMAC. We do the exact same thing for AES. There we also transfer the actual output data of every 100th block to the capture system and run a model in parallel to detect if something would go wrong in the analyzed system. This is sufficient for verifying the SCA IP version.
I've been looking at the KMAC RTL and I think I've found the code responsible for discarding data input outside of START <-> PROCESS: the FSM inside kmac_app.sv first wants to see the START command before going in StSw state where the muxes are set up correctly. I think we could work around that. Or are there other places that need modification @eunchan https://github.com/eunchan ?
I am sorry for being persistent about this @eunchan https://github.com/eunchan and @udinator https://github.com/udinator . Out intention is to experimentally verify the masking inside KMAC. The countermeasures added on top (like the one we are discussing to bypass) are also important but ultimately the masking is the most fundamental CM against SCA and we need to be sure it's done right. The issues with the noise generated from Ibex are that 1) we can't filter it out using some post-processing as it's overlaid with KMAC operation, and 2) due to the large interconnect being involved, the noise is much stronger than the actual signal, especially on FPGA. This makes the verification of the masking extremely difficult if not impossible.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lowRISC/opentitan/issues/7841#issuecomment-903627296, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJDG3UFEXQY3SLXXD4DKBTT6IMV3ANCNFSM5COKL6LQ. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email.
Thanks for getting back Eunchan.
First thing first, I want to confirm my assumption on this issue. "The change for KMAC in order to capture the power signature is for FPGA and silicon both."
It's for FPGA only. For ASIC we wouldn't do this for the exact reasons you've mentioned. The noise created by Ibex and especially the TL-UL interconnect on ASIC will be different. My gut feeling tells me it's going to be less on ASIC. The reason I believe so is that routing delay and hence the power required for a signal to travel longer distances are far more significant on FPGA.
If the change is wholly for FPGA, then as I suggested in the previous thread, it would be better to be a separate IP (
kmac_sca) to lessen the worries from DV folks. If the change is implemented as a build-time parameter and never planned to be used on silicon, it just disturb the code readability in my opinion.
I would like to get an idea of much would need to be changed. If it really turns out that we need to change hundreds of lines of code, then I agree with you and we should create a separate IP. If it's just few lines of code, it would be more efficient to use parameters as for AES. I am convinced we need to do a couple of iterations of experiments and will need to repeat those whenever we do significant changes to the RTL. Having a separate SCA version of the IP would require us to keep the two aligned + additional software/DIF support which is also work.
So, the main purpose is to verify the effectiveness of the DOM AND logic inside the SHA3. Then, wouldn't it be better to prepare the IP specific to the matter? sha3.sv itself is standalone IP (works quite similar to the prim cells). So, it won't be difficult to make another IP only for the purpose. SW can write secret key directly to MSG FIFO and fire the SHA3 up.
Good point about sha3.sv, I wasn't aware of that. Based on my experience with AES, I say it's best if we can test the full KMAC IP and not just the SHA3 core. I've stumbled upon cases where the DOM S-Boxes inside the cipher core itself were working correctly. But there was still absolutely detrimental leakage due to how the cipher core was operated by the bigger IP and how the randomness was used. Sadly, such issues cannot be discovered e.g. by formal masking verification tools as the cipher cores get too big very quickly resulting in exponentially increasing tool run times.
So, the consensus here is that we need the IP specific for the SCA "on FPGA". That's good.
The item to decide is if we modify the current KMAC HWIP or prep the new IP (slightly modified version of KMAC).
The blocking logic in MSG_FIFO is a line. But the KMAC has a few other places that checks the protocol whether SW initiates the Start -> Message -> Process -> {Run} -> Done. I think we need to modify those lines (+ many assertions too). So personally, I assume the end result will be quite difficult to digest the code at once.
Could you try the modification on the current KMAC ? If you change the blocking condition, I think the SW will receive some KMAC err. The SW may ignore the error interrupt. But I can't guarantee the KMAC IP waits reset in certain cases or moves forward with errors. That one I need to double check the current KMAC design.
On Aug 23, 2021, at 9:41 AM, Pirmin Vogel @.***> wrote:
Thanks for getting back Eunchan.
First thing first, I want to confirm my assumption on this issue. "The change for KMAC in order to capture the power signature is for FPGA and silicon both."
It's for FPGA only. For ASIC we wouldn't do this for the exact reasons you've mentioned. The noise created by Ibex and especially the TL-UL interconnect on ASIC will be different. My gut feeling tells me it's going to be less on ASIC. The reason I believe so is that routing delay and hence the power required for a signal to travel longer distances are far more significant on FPGA.
If the change is wholly for FPGA, then as I suggested in the previous thread, it would be better to be a separate IP (kmac_sca) to lessen the worries from DV folks. If the change is implemented as a build-time parameter and never planned to be used on silicon, it just disturb the code readability in my opinion.
I would like to get an idea of much would need to be changed. If it really turns out that we need to change hundreds of lines of code, then I agree with you and we should create a separate IP. If it's just few lines of code, it would be more efficient to use parameters as for AES. I am convinced we need to do a couple of iterations of experiments and will need to repeat those whenever we do significant changes to the RTL. Having a separate SCA version of the IP would require us to keep the two aligned + additional software/DIF support which is also work.
So, the main purpose is to verify the effectiveness of the DOM AND logic inside the SHA3. Then, wouldn't it be better to prepare the IP specific to the matter? sha3.sv itself is standalone IP (works quite similar to the prim cells). So, it won't be difficult to make another IP only for the purpose. SW can write secret key directly to MSG FIFO and fire the SHA3 up.
Good point about sha3.sv, I wasn't aware of that. Based on my experience with AES, I say it's best if we can test the full KMAC IP and not just the SHA3 core. I've stumbled upon cases where the DOM S-Boxes inside the cipher core itself were working correctly. But there was still absolutely detrimental leakage due to how the cipher core was operated by the bigger IP and how the randomness was used. Sadly, such issues cannot be discovered e.g. by formal masking verification tools as the cipher cores get too big very quickly resulting in exponentially increasing tool run times.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lowRISC/opentitan/issues/7841#issuecomment-903936253, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJDG3V7LNLRHEFJYDN3MD3T6J2ZZANCNFSM5COKL6LQ. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email.
Good to hear we are on the same page!
I can have a look at this later this week. I'll probably quickly start to use the Verilator Earl Grey simulation such that I can also edit the configuration sequence/software as desired. Which means, I won't care too much about the Assertions in the beginning.
Regarding the blocking logic of the MSG_FIFO. Could you please point me to the exact location. I don't know the design that well. But from having a first look I thought this would be inside kmac_app.sv. Basically, the FSM in there first wants to see START before jumping into StSw where the muxes are configured to forward data from TL-UL to the message FIFO. Or did I get this wrong?
On Aug 24, 2021, at 6:31 AM, Pirmin Vogel @.***> wrote:
Good to hear we are on the same page!
I can have a look at this later this week. I'll probably quickly start to use the Verilator Earl Grey simulation such that I can also edit the configuration sequence/software as desired. Which means, I won't care too much about the Assertions in the beginning.
Regarding the blocking logic of the MSG_FIFO. Could you please point me to the exact location. I don't know the design that well. But from having a first look I thought this would be inside kmac_app.sv. Basically, the FSM in there first wants to see START before jumping into StSw where the muxes are configured to forward data from TL-UL to the message FIFO. Or did I get this wrong?
Your finding is correct. kmac_app.sv:564 is the location that the KMAC reports error when the SW tries to push the data while not in the Sw Access mode.
Thanks @eunchan for the confirmation. I've had a look and managed to implement all the features we need for SCA of KMAC (timer, write message FIFO while idle, pass START and PROCESS commands shortly after each other). The changes are confined to just two KMAC RTL files and it's around 100 lines of code. The changes are confined inside two or three code blocks. This stuff is just enabled on the CW310 FPGA board. Enabling it on ASIC will cause lint errors to reduce the risk of accidentally enabling it.
I believe this should be okay. Please take a look at the PR and let me know your opinion.
@vogelpi can we close this one? I believe your PR already resolve this issue.