Skip to content

[crypto] Re-mask AES-GCM keys #27647

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

nasahlpa
Copy link
Member

As suggested in #27243, this commit regularly remasks the key that is used for AES-GCM.

@nasahlpa nasahlpa requested a review from johannheyszl July 17, 2025 09:21
@nasahlpa nasahlpa force-pushed the aes_gcm_refresh_key branch 2 times, most recently from 3e46299 to e04ff10 Compare July 17, 2025 10:14
@nasahlpa nasahlpa marked this pull request as ready for review July 17, 2025 17:11
@nasahlpa nasahlpa requested a review from a team as a code owner July 17, 2025 17:11
@nasahlpa nasahlpa requested review from engdoreis, moidx, h-filali, andrea-caforio, felixmiller, vogelpi and cfrantz and removed request for a team and engdoreis July 17, 2025 17:11
@nasahlpa nasahlpa added the CherryPick:earlgrey_1.0.0 This PR should be cherry-picked to earlgrey_1.0.0 label Jul 17, 2025
HARDENED_TRY(hardened_xor((uint32_t *)internal_ctx->key.key_shares[1], mask,
internal_ctx->key.key_len));
} else {
HARDENED_CHECK_EQ(internal_ctx->key.sideload, kHardenedBoolTrue);
Copy link
Contributor

Choose a reason for hiding this comment

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

Shall we return error upon calling remask with a sideload key to notify user?

sideloaded keys are always remasked on every power cycle iiuc

Copy link
Member Author

Choose a reason for hiding this comment

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

gcm_remask_key() cannot be called by the user - it is only called by the CL internally in otcrypto_aes_gcm_update_encrypted_data, otcrypto_aes_gcm_decrypt_final, or otcrypto_aes_gcm_encrypt_final.

In this case, I think just returning OTCRYPTO_OK for this function in the sideloaded key case should be fine.

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks sgtm

@@ -427,6 +455,8 @@ otcrypto_status_t otcrypto_aes_gcm_update_encrypted_data(
aes_gcm_context_t internal_ctx;
gcm_context_restore(ctx, &internal_ctx);
HARDENED_TRY(load_key_if_sideloaded(internal_ctx.key));
// Remask the key if needed.
Copy link
Contributor

Choose a reason for hiding this comment

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

if needed refers to whether not sideloaded, right? we could say this explicitly

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, good point. I've updated the comment.

@nasahlpa nasahlpa force-pushed the aes_gcm_refresh_key branch from e04ff10 to 8ffb16e Compare July 21, 2025 09:41
* @param word_len Length in words of each operand.
* @return Result of the operation.
*/
status_t hardened_xor(uint32_t *OT_RESTRICT x, const uint32_t *OT_RESTRICT y,
Copy link
Member Author

Choose a reason for hiding this comment

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

Need to check if it is OK to expose this function here or move to the hardened lib.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think there are two options:

  1. Move it to hardened lib, which will require creating a couple of hook functions to initialize and get entropy.
  2. Move it to random_order as the function implementation already relies on random_order primitives.

It may be easier to go with (2) for now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for your suggestion @moidx

I tried adding it to random_order but there I am creating a cycle dependency as I would need to include hardened_memory as a dependency, which depends on random_order.

Would moving it to hardened_memory, as I just did in the first commit of this PR, be OK as well?

Copy link
Contributor

@moidx moidx left a comment

Choose a reason for hiding this comment

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

Thanks @nasahlpa, I will take a second look after you respond to the hardened_xor comment.

* @param word_len Length in words of each operand.
* @return Result of the operation.
*/
status_t hardened_xor(uint32_t *OT_RESTRICT x, const uint32_t *OT_RESTRICT y,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there are two options:

  1. Move it to hardened lib, which will require creating a couple of hook functions to initialize and get entropy.
  2. Move it to random_order as the function implementation already relies on random_order primitives.

It may be easier to go with (2) for now.

@moidx moidx self-requested a review July 22, 2025 20:33
As this function is useful also outside of keyblob, so move it to
the hardened_memory library.

Signed-off-by: Pascal Nasahl <nasahlpa@lowrisc.org>
@nasahlpa nasahlpa force-pushed the aes_gcm_refresh_key branch from 8ffb16e to 6dd1b96 Compare July 23, 2025 06:10
As suggested in lowRISC#27243, this commit regularly remasks
the key that is used for AES-GCM.

Signed-off-by: Pascal Nasahl <nasahlpa@lowrisc.org>
@nasahlpa nasahlpa force-pushed the aes_gcm_refresh_key branch from 6dd1b96 to 7251046 Compare July 23, 2025 07:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CherryPick:earlgrey_1.0.0 This PR should be cherry-picked to earlgrey_1.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants