[kmac] How should key shares be used?
I've been implementing the KMAC operation for the KMAC DIF and have found that placing the key into share 0 and setting share 1 to all zero works as I expect but placing the key into share 1 and setting share 0 to zero does not.
Is this the expected behavior and if so how should the key shares be used? I was operating under the assumption that the key is the two share XOR'd together (<share 0> XOR <share 1>) but I am now unsure that I am correct.
I haven't dug much into this yet so it could be a bug in my code.
maybe this is an unrelated/already-known question, but are you testing the unmasked or masked KMAC configuration?
If unmasked, the key is simply share0, but if masked, the key is then the XOR of share0 and share1.
I'm testing earlgrey which I think has EnMasking set to 0. I thought that only affected the output state though. This statement implied to me that the key was always masked:
The message FIFO does not generate the masked message data. Incoming message bistream is not sensitive to the leakage. So if the EnMasking parameter is set, the second share of the message bistream is zero. The secret key, however, is stored as masked form always.
The sideload interface for the key also always uses the two key shares:
KMAC/SHA3 HWIP has an option to receive the secret key from KeyMgr via sideload key interface. The software should set !!CFG.sideload to use the KeyMgr sideloaded key in SW-initiated KMAC operation. keymgr_pkg::hw_key_t defines the structure of the sideloaded key. KeyMgr provides the sideloaded key in two-share masked form regardless of the compile-time parameter EnMasking. If EnMasking is not defined, the KMAC merges the shared key to the unmasked form before uses the key.
From a software perspective it would be nice if the two shares were just combined with XOR when EnMasking is 0. That way the software interface is unchanged regardless of the configuration. This is a nice property of the output state where the two shares may always be merged by software since the second share is set to zero if masking is disabled.
I'm testing earlgrey which I think has EnMasking set to 0. I thought that only affected the output state though. This statement implied to me that the key was always masked:
The message FIFO does not generate the masked message data. Incoming message bistream is not sensitive to the leakage. So if the EnMasking parameter is set, the second share of the message bistream is zero. The secret key, however, is stored as masked form always.
Ahh I see.
The EnMasking parameter also affects how the key is treated, assuming that it is provided by SW rather than sideloaded from the keymgr.
In this case where we are using the unmasked form of KMAC, only share0 of the key is used, but internally both share0 and share1 are stored separately and can be XORed if needed (this is the "masked form" referred to by the spec).
The sideload interface for the key also always uses the two key shares:
KMAC/SHA3 HWIP has an option to receive the secret key from KeyMgr via sideload key interface. The software should set !!CFG.sideload to use the KeyMgr sideloaded key in SW-initiated KMAC operation. keymgr_pkg::hw_key_t defines the structure of the sideloaded key. KeyMgr provides the sideloaded key in two-share masked form regardless of the compile-time parameter EnMasking. If EnMasking is not defined, the KMAC merges the shared key to the unmasked form before uses the key.
Yup, that's correct. So in this case of unmasked KMAC, share0 would contain the XORed key from the sideload interface, and share1 wouldn't be used at all.
From a software perspective it would be nice if the two shares were just combined with XOR when EnMasking is 0. That way the software interface is unchanged regardless of the configuration. This is a nice property of the output state where the two shares may always be merged by software since the second share is set to zero if masking is disabled.
I can't really speak for any potential security issues as a result of doing this - tagging @cdgori @eunchan @tjaychen for any input on this one.
Michael, your understanding is correct.Key is always two share and the sw interface is same regardless of EnMasking parameter
Let me double check the designagain.
On Fri, May 21, 2021, 6:22 AM udinator @.***> wrote:
I'm testing earlgrey which I think has EnMasking set to 0. I thought that only affected the output state though. This statement implied to me that the key was always masked:
The message FIFO does not generate the masked message data. Incoming message bistream is not sensitive to the leakage. So if the EnMasking parameter is set, the second share of the message bistream is zero. The secret key, however, is stored as masked form always.
Ahh I see. The EnMasking parameter also affects how the key is treated, assuming that it is provided by SW rather than sideloaded from the keymgr. In this case where we are using the unmasked form of KMAC, only share0 of the key is used, but internally both share0 and share1 are stored separately and can be XORed if needed (this is the "masked form" referred to by the spec).
The sideload interface for the key also always uses the two key shares:
KMAC/SHA3 HWIP has an option to receive the secret key from KeyMgr via sideload key interface. The software should set !!CFG.sideload to use the KeyMgr sideloaded key in SW-initiated KMAC operation. keymgr_pkg::hw_key_t defines the structure of the sideloaded key. KeyMgr provides the sideloaded key in two-share masked form regardless of the compile-time parameter EnMasking. If EnMasking is not defined, the KMAC merges the shared key to the unmasked form before uses the key.
Yup, that's correct.
From a software perspective it would be nice if the two shares were just combined with XOR when EnMasking is 0. That way the software interface is unchanged regardless of the configuration. This is a nice property of the output state where the two shares may always be merged by software since the second share is set to zero if masking is disabled.
I can't really speak for any potential security issues as a result of doing this - tagging @cdgori https://github.com/cdgori @eunchan https://github.com/eunchan @tjaychen https://github.com/tjaychen for any input on this one.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lowRISC/opentitan/issues/6637#issuecomment-845486021, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJDG3RZJURPYPO3UYEHC4DTOV4QBANCNFSM45GNSJBA .
Hum, I double checked the design. Only key_share0 is used if EnMasking is not set to 1.
Having consistent DIF is good.
However, looks like the design intention (I almost forgot it already) was to have smaller size of the design and smaller size of the registers too. So, key_share1 register should be removed. It, however, requires the KMAC to be templat-ize, which isn't what I prefer.
Anyway, I think I can make it XOR-ed.
CC: @cdgori
On May 21, 2021, at 7:04 AM, Eunchan Kim @.***> wrote:
Michael, your understanding is correct.Key is always two share and the sw interface is same regardless of EnMasking parameter
Let me double check the designagain.
On Fri, May 21, 2021, 6:22 AM udinator @.*** @.***>> wrote:
I'm testing earlgrey which I think has EnMasking set to 0. I thought that only affected the output state though. This statement implied to me that the key was always masked:
The message FIFO does not generate the masked message data. Incoming message bistream is not sensitive to the leakage. So if the EnMasking parameter is set, the second share of the message bistream is zero. The secret key, however, is stored as masked form always.
Ahh I see. The EnMasking parameter also affects how the key is treated, assuming that it is provided by SW rather than sideloaded from the keymgr. In this case where we are using the unmasked form of KMAC, only share0 of the key is used, but internally both share0 and share1 are stored separately and can be XORed if needed (this is the "masked form" referred to by the spec).
The sideload interface for the key also always uses the two key shares:
KMAC/SHA3 HWIP has an option to receive the secret key from KeyMgr via sideload key interface. The software should set !!CFG.sideload to use the KeyMgr sideloaded key in SW-initiated KMAC operation. keymgr_pkg::hw_key_t defines the structure of the sideloaded key. KeyMgr provides the sideloaded key in two-share masked form regardless of the compile-time parameter EnMasking. If EnMasking is not defined, the KMAC merges the shared key to the unmasked form before uses the key.
Yup, that's correct.
From a software perspective it would be nice if the two shares were just combined with XOR when EnMasking is 0. That way the software interface is unchanged regardless of the configuration. This is a nice property of the output state where the two shares may always be merged by software since the second share is set to zero if masking is disabled.
I can't really speak for any potential security issues as a result of doing this - tagging @cdgori https://github.com/cdgori @eunchan https://github.com/eunchan @tjaychen https://github.com/tjaychen for any input on this one.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lowRISC/opentitan/issues/6637#issuecomment-845486021, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJDG3RZJURPYPO3UYEHC4DTOV4QBANCNFSM45GNSJBA.
Thanks for digging into this @eunchan and @udinator. I'll avoid adding tests for masked keys for the time being then.
I'm having a little bit of difficulty following here.
Just making sure I understand - EnMasking is a "build" parameter? Some of this discussion seems to also turn on whether EnMasking is defined vs set to 0 as well? I don't understand what happens if EnMasking is not defined at all (my initial gut reaction is it should behave the same as set to 0 but perhaps I am wrong)
From my miles-high/pretty-abstract view, if masking is not enabled in the design (i.e. the key is being carried around in single share) then it should be stored/moved in "share0" throughout, and @eunchan is correct, in theory the optimization should allow for key_share1 to be removed from the design as unused.
What's the SW need for the symmetry/ability to put an unmasked key in share1 in a design with EnMasking set to 0?
(Note, in theory, some future version of the design might have 3 shares to provide 2nd-order SCA resistance, or even more shares - the order of SCA resistance is basically num_shares-1 for these kind of designs, and the necessary order of resistance is dictated by the threat model. I would really hope that in a case with num_shares > 2, we don't want to have EnMasking set to zero and yet be able to put the key freely in any of share0/1/2/n-1? This is why I think the key should always be kept share0 if EnMasking is 0.)
If EnMasking=1, I would not expect software to ever be merging the shares, since that will potentially leak information.
What's the SW need for the symmetry/ability to put an unmasked key in share1 in a design with EnMasking set to 0?
The issue isn't so much that we want to be able to put an unmasked key is share1. The issue is that the effect of the EnMasking parameter on the way the key is interpreted is not clear from the software interface documentation.
It would be nice if the interface was independent of the EnMasking parameter since it means that tests that set multiple shares will work regardless of the value of the EnMasking parameter. But if this is too costly or otherwise not reasonable then some clarifications of its effect on the interface in the documentation would suffice I think.
We might also want to consider exposing the EnMasking parameter to the software though so it knows how many shares the keys have and can type key data structures appropriately.
It is good to have those compile-time parameters to be SW readable registers, maybe "CAPable" register is a good candidate.
However, I am worried about the SW binary size if SW DIF handles every configurations with a binary. In this particular case (EnMasking), would it be better to have build-time parameter in the SW too so that any unnecessary part is removed if EnMasking is not set?
On May 22, 2021, at 6:39 AM, Michael Munday @.***> wrote:
What's the SW need for the symmetry/ability to put an unmasked key in share1 in a design with EnMasking set to 0?
The issue isn't so much that we want to be able to put an unmasked key is share1. The issue is that the effect of the EnMasking parameter on the way the key is interpreted is not clear from the software interface documentation.
It would be nice if the interface was independent of the EnMasking parameter since it means that tests that set multiple shares will work regardless of the value of the EnMasking parameter. But if this is too costly or otherwise not reasonable then some clarifications of its effect on the interface in the documentation would suffice I think.
We might also want to consider exposing the EnMasking parameter to the software though so it knows how many shares the keys have and can type key data structures appropriately.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lowRISC/opentitan/issues/6637#issuecomment-846276419, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJDG3UXYIL2ZYJZC7LA5VDTO3HIDANCNFSM45GNSJBA.
However, I am worried about the SW binary size if SW DIF handles every configurations with a binary.
I'm not really worried about this. I don't think the overhead will be that much and production code can cut down the DIF to match the required functionality anyway (like we do for the mask ROM drivers).
would it be better to have build-time parameter in the SW too so that any unnecessary part is removed if EnMasking is not set?
Yes, I think adding something to the generated software constants would be good (e.g. #define KMAC_PARAM_NUM_ACTIVE_KEY_SHARES 1 for EnMasking=0). The DIF API itself probably won't make use of this since the DIF APIs are independent of the hardware parameterization but the DIF implementation and other production APIs could use it.
(Though we would probably want a getter for this setting in the DIF such as dif_kmac_get_num_active_key_shares(uint32_t *n).)
I am ok with just using share0 when EnMasking=0. The leakage characterization will focus mostly on EnMasking=1, and thus it is ok for the DIF to focus on the masked interface. I agree the KMAC_PARAM_NUM_ACTIVE_KEY_SHARES would be nice to have, but I think it is ok to leave as is as the EnMasking seems to be well documented in the spec.
thus it is ok for the DIF to focus on the masked interface
Should we enable masking (EnMasking=1) in earlgrey then? Currently it is disabled.
EnMasking seems to be well documented in the spec
The software interface documentation makes no mention of the fact that the EnMasking parameter affects the key share interface and, as I mentioned above, hints at the opposite situation being true. I do think we should document that share 1 is ignored when EnMasking=0.
thus it is ok for the DIF to focus on the masked interface
Should we enable masking (EnMasking=1) in earlgrey then? Currently it is disabled.
It is enabled in _asic, but disabled on FPGAs due to the size issue.
EnMasking seems to be well documented in the spec
The software interface documentation makes no mention of the fact that the EnMasking parameter affects the key share interface and, as I mentioned above, hints at the opposite situation being true. I do think we should document that share 1 is ignored when EnMasking=0.
I think we can close this ticket. Feel free to open up again if any further questions for key share in EnMasking = 0