opentitan icon indicating copy to clipboard operation
opentitan copied to clipboard

[hmac] Add support for SHA / HMAC context switch

Open tjaychen opened this issue 7 years ago • 36 comments

can i make a wishlist request (long term)? This is something that's come up a few times. It seems that a lot of software use cases end up in a situation where multiple threads want to use the SHA engine at the same time. However, if each is using the engine to SHA an indeterminate amount of data, it means that the SHA engine itself needs to context switch (ie, a thread cannot hold on to it forever). So it would be nice to be able to save SHA context and restore another to proceed with data dumping.

Just something to think about. Even if we don't need it, I am sure someone in the OT community will eventually.

tjaychen avatar May 31 '19 23:05 tjaychen

Seems good. It would also ensure that what is in the context is clearly identified. Which could be an advantage if the frequency target requires a multiple cycle pipeline per round, putting 2 (or N) copies of the context allows parallel use of the same hardware. (Probably not an issue at the frequencies of an MCU, but possible if we want a higher performance version of the IP for a BMC or 64-bit system with a similar interface)

mdhayter avatar Jun 01 '19 12:06 mdhayter

One comment here. Those contexts shouldn't be visible to the processor, right? Then, we could implement internal SRAM and just store the internal variables and reload. If the number of threads aren't that many, then just one more shadow set of registers might be enough.

In HMAC mode, it gets a little bit more complex as the module also takes in the secret key (from key manager eventually) so that the second path using outer key pad needs the original secret key. Or, we could just store hashed value of the outer key pad.

Anyway, its good idea!

eunchan avatar Jun 26 '19 22:06 eunchan

Hey eunchan, Maybe we can check with Miguel / Chris.

Specifically for hmac, I don't actually think we need to keep the secret key. If the software needs to context switch, it feels okay to me that when they context switch back, they request the secret key from key manager or input it again.

For the Sha context being visible...I don't feel like exposure to the processor is that bad. Sha is not secret by nature, and the alternative to context switch would be software just directly computing. So it almost feels okay to me to just expose it, and let software save and restore. I'm not a cryptographer by any stretch of the imagination, so we should double check with people who know better.

On Wed, Jun 26, 2019, 15:31 Eunchan Kim [email protected] wrote:

One comment here. Those contexts shouldn't be visible to the processor, right? Then, we could implement internal SRAM and just store the internal variables and reload. If the number of threads aren't that many, then just one more shadow set of registers might be enough.

In HMAC mode, it gets a little bit more complex as the module also takes in the secret key (from key manager eventually) so that the second path using outer key pad needs the original secret key. Or, we could just store hashed value of the outer key pad.

Anyway, its good idea!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/lowRISC/opentitan/issues/49?email_source=notifications&email_token=AAH2RSXTKW5XFK3Z74Z2INLP4PU5DA5CNFSM4HR6WPQKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYU773Q#issuecomment-506068974, or mute the thread https://github.com/notifications/unsubscribe-auth/AAH2RSQJNO27T2D22RSUZSDP4PU5DANCNFSM4HR6WPQA .

tjaychen avatar Jun 27 '19 17:06 tjaychen

If we wanted, we could enforce a rule in the HW where if the "secret backdoor key and data" mode is enabled, the context switch isn't allowed. This is strictly for the large undefined-sized "front door" SHA/HMAC operations anyway, where the processor is providing the data.

sjgitty avatar Jun 27 '19 17:06 sjgitty

On a mission to clean up old issues, scraping from stalest to freshest.

I think we can close this as it is being tracked in the 2020 planning for HMAC improvements, unless @eunchan you would prefer to keep it open until the specification is updated?

sjgitty avatar Dec 05 '19 02:12 sjgitty

it may be helpful to confer with Chris / Felix on how useful they think this is.

On Wed, Dec 4, 2019 at 6:31 PM Scott Johnson [email protected] wrote:

On a mission to clean up old issues, scraping from stalest to freshest.

I think we can close this as it is being tracked in the 2020 planning for HMAC improvements, unless @eunchan https://github.com/eunchan you would prefer to keep it open until the specification is updated?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/lowRISC/opentitan/issues/49?email_source=notifications&email_token=AAH2RSTYOSQBRTRKU6RXDXTQXBRZJA5CNFSM4ISTJVJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEF7ID3Y#issuecomment-561938927, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAH2RSROH67YMVPUCC7V523QXBRZJANCNFSM4ISTJVJQ .

tjaychen avatar Dec 05 '19 04:12 tjaychen

With Scott's suggestion that sideloading prevents context-saving/swapping, I think it's interesting and pretty safe to implement.

But as to whether we need it or not, I don't have a good sense of the workloads we expect, and if two SHA'ing "threads" are going to be contending for the hardware. @tjaychen you said this came up in the past, so I'm assuming it's real, but maybe an example would help. (The one I imagine right away is verifying integrity on a boot image or some other huge structure but needing to be able to do various housekeeping stuff like ACKs/responses to messages that require hashes, and blocking on the huge-payload hash would make this hard/impossible.)

cdgori avatar Dec 06 '19 00:12 cdgori

Yes your description of where this came up is right. Except the difference is that the "huge payload" was actually coming off-chip from another client. This client does not provide information on exactly how much information needed to be hashed, other than periodically supplying data and eventually a "done". Because of this, there was no good way of knowing when to release the hardware and use it for internal house-keeping things. I cannot remember if this was a usecase in production, or something someone thought could happen and thus had to design for (I feel like it's the second)

On Thu, Dec 5, 2019 at 4:57 PM Chris Gori [email protected] wrote:

With Scott's suggestion that sideloading prevents context-saving/swapping, I think it's interesting and pretty safe to implement.

But as to whether we need it or not, I don't have a good sense of the workloads we expect, and if two SHA'ing "threads" are going to be contending for the hardware. @tjaychen https://github.com/tjaychen you said this came up in the past, so I'm assuming it's real, but maybe an example would help. (The one I imagine right away is verifying integrity on a boot image or some other huge structure but needing to be able to do various housekeeping stuff like ACKs/responses to messages that require hashes, and blocking on the huge-payload hash would make this hard/impossible.)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lowRISC/opentitan/issues/49?email_source=notifications&email_token=AAH2RSUPVGOC4DKGX3B4QJLQXGPPPA5CNFSM4ISTJVJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGCVEHA#issuecomment-562385436, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAH2RSWY2TZNZQ7IRNTBOJTQXGPPPANCNFSM4ISTJVJQ .

tjaychen avatar Dec 06 '19 01:12 tjaychen

This is evidently a real case scenario in a certain product whereby they're forced to use SW hash / HMAC when the HW unit is busy. Unfortunate situation that could be alleviated with this feature (assuming it is worth the cost).

On Thu, Dec 5, 2019 at 5:04 PM tjaychen [email protected] wrote:

Yes your description of where this came up is right. Except the difference is that the "huge payload" was actually coming off-chip from another client. This client does not provide information on exactly how much information needed to be hashed, other than periodically supplying data and eventually a "done". Because of this, there was no good way of knowing when to release the hardware and use it for internal house-keeping things. I cannot remember if this was a usecase in production, or something someone thought could happen and thus had to design for (I feel like it's the second)

On Thu, Dec 5, 2019 at 4:57 PM Chris Gori [email protected] wrote:

With Scott's suggestion that sideloading prevents context-saving/swapping, I think it's interesting and pretty safe to implement.

But as to whether we need it or not, I don't have a good sense of the workloads we expect, and if two SHA'ing "threads" are going to be contending for the hardware. @tjaychen https://github.com/tjaychen you said this came up in the past, so I'm assuming it's real, but maybe an example would help. (The one I imagine right away is verifying integrity on a boot image or some other huge structure but needing to be able to do various housekeeping stuff like ACKs/responses to messages that require hashes, and blocking on the huge-payload hash would make this hard/impossible.)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub < https://github.com/lowRISC/opentitan/issues/49?email_source=notifications&email_token=AAH2RSUPVGOC4DKGX3B4QJLQXGPPPA5CNFSM4ISTJVJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGCVEHA#issuecomment-562385436 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/AAH2RSWY2TZNZQ7IRNTBOJTQXGPPPANCNFSM4ISTJVJQ

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/lowRISC/opentitan/issues/49?email_source=notifications&email_token=AJZQKVI3TIXEBDD2EVSGS3TQXGQI7A5CNFSM4ISTJVJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGCVQHY#issuecomment-562386975, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJZQKVLKBN3AREA5BPMEIKTQXGQI7ANCNFSM4ISTJVJQ .

sjgitty avatar Dec 06 '19 02:12 sjgitty

I feel a little ... conflicted then.

If there is truly a need for an "interrupting" / quick HMAC (rather than just a SHA), where would the key for the interrupting HMAC come from? If we disallow context switching when sideloading then the implication is that will be a SW-managed/held key, which I'd prefer not to rely on / force usage of.

Handling context swapping/restoring with sideloaded keys is definitely opening a much bigger can of worms though. It's hard for me to balance these two things out, unless we understand the use-case better, I think.

cdgori avatar Dec 09 '19 19:12 cdgori

I believe the use case is a) processor-given key for HMAC (or even keyless SHA-256) is active on a large hash task; b) secure-given key is requested for HMAC. In this scenario, task a) is interrupted with a save of the internal state, task b) is allowed to proceed, then task a) does a restore and continues. This seems safe in that task a) is not exposing anything secret via the save/restore process, and task b) is un-interruptable. wdyt?

On Mon, Dec 9, 2019 at 11:50 AM Chris Gori [email protected] wrote:

I feel a little ... conflicted then.

If there is truly a need for an "interrupting" / quick HMAC (rather than just a SHA), where would the key for the interrupting HMAC come from? If we disallow context switching when sideloading then the implication is that will be a SW-managed/held key, which I'd prefer not to rely on / force usage of.

Handling context swapping/restoring with sideloaded keys is definitely opening a much bigger can of worms though. It's hard for me to balance these two things out, unless we understand the use-case better, I think.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/lowRISC/opentitan/issues/49?email_source=notifications&email_token=AJZQKVN5AIJJYKQZ4VPKJSLQX2ORLA5CNFSM4ISTJVJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGKOF3I#issuecomment-563405549, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJZQKVL4FY3FGDKXYGS7I3TQX2ORLANCNFSM4ISTJVJQ .

sjgitty avatar Dec 10 '19 02:12 sjgitty

Personally i think we still need to find a way to make the secure-given key case context switchable. If we for example require the secure case to not be switchable, it would restrict it to a smaller number of use cases. That might lead to a user eventually deciding to abandon the secure use case altogether and just do everything in SW.

I definitely get Chris' point there are dangers in allowing the secure case to be context swapped. But I think it makes sense to closely examine those risks and find a way to make it usable (with some restrictions that the software would need to manage) - ie, always accumulate to one block size before pumping it into hardware.

tjaychen avatar Dec 11 '19 18:12 tjaychen

Scott's example does not present too many risks to me, since the context-switched-out operation is using a non-sideloaded key. (If we stick to some rule that says that sideloaded keys are non-context-switchable that makes the analysis very simple.)

Tim's comment that we might need to figure out how to make a sideloaded key context-switchable will require a lot more study. (but he is correct that if we make conditions for using sideload too restrictive such that no one uses it, we have failed.)

For sure doing things on a block boundary is going to be a good idea. I think that most of my concerns will be handled if we do a good job of random-wiping the context-swap-storage area after the "pop" from that stack happens, or possibly before the "push" onto that stack.

(I think most of the pathological cases that my spidey-sense is worried about come from crafting special messages to HMAC and triggering the context switch such that a known-intermediate-key overwrites an unknown-key in a way that leaks information about that unknown-key, but there may be others I have not considered.)

cdgori avatar Jan 06 '20 21:01 cdgori

After discussing with the software teams, this sounds like it would be a very nice feature to have. However, I don't think this should be milestone gating and can theory be something we add for production.

In any case, there are clear software workarounds.

How does everyone think about this? We can hopefully defer the issue until later.

tjaychen avatar Sep 22 '20 01:09 tjaychen

(I think this holds for KMAC context switch too.)

My 2c is this falls into the category of post-ES "improvements" that we can consider for production. We have too many of these nice-to-have features that we're still considering that we need to get off the fence on. IMO it falls into the bucket of "not needed to prove analog / certification / tricky product features" therefore goes into that bucket of post-ES. But I'm on the side of enlarging that bucket, and we should run this by SW, CB, and Dom to get their opinion on it. @domrizz0 @gkelly @mdhayter

sjgitty avatar Sep 22 '20 04:09 sjgitty

yes we had an initial conversation with CB. And that conclusion was that kmac context switching is not necessary (see #3479 HMAC context switching on the other hand, is very nice to have. However everyone acknowledges it is not a must have immediately.

tjaychen avatar Sep 22 '20 06:09 tjaychen

this issue has already been clarified in other issues and can be closed.

tjaychen avatar Feb 19 '21 05:02 tjaychen

Re-open this issue as discussed in #13497

eunchan avatar Jul 07 '22 17:07 eunchan

CC: @jadephilipoom @msfschaffner @tjaychen

eunchan avatar Jul 07 '22 17:07 eunchan

Most of my earlier comments/concerns are reduced/eliminated (for HMAC) because we decided to not implement sideload. Thus, the key material is software-originated/observable in all cases by the processor, regardless of context switch. Software does probably need to take best-effort care to compartmentalize access to individual HMAC keys though.

KMAC context switch (if used/allowed) needs to be carefully considered because all of my concerns about leaking sideloaded keys are applicable then. My comments about block boundary and random wipe are probably the minimum necessary to allow this, we might be able to do better.

cdgori avatar Jul 12 '22 17:07 cdgori