Skip to content
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

[pentest] Fix masking off for AES-SCA #25932

Merged
merged 3 commits into from
Jan 20, 2025

Conversation

nasahlpa
Copy link
Member

This PR consists of three different commits:

  • The first two commits tidy up the code base and prepare it for the third commit
  • The third commit makes sure that we correctly (i.e., after a dif_aes_start()) do a reseed.

Before this commit, turning off the masks for AES gave us the following TVLA figure:
aes_masks_off_before
As shown in the figure, leakage is only detected in the first AES rounds. However, with masking disabled, we expect leakage in all rounds.

With these changes, the masking off now works again:
aes_masks_off_after

Previously, the pentest_set_trigger_high/low functions were called
at different places inside the code. However, this is not really
- instead we could do it only inside the aes_encrypt function.

This simplifies the code.

Signed-off-by: Pascal Nasahl <[email protected]>
When porting the AES SCA tests from the old simpleserial to the new
uJSON framework, we also copied the aes_sca_error_t structure. This
structure was used to indicate whether a function inside the aes_sca
program returned with an error.

Instead, we simply can use the default status_t with the TRY()
construct. This (a) simplifies the code and (b) follows the
coding style of the OpenTitan code base.

Signed-off-by: Pascal Nasahl <[email protected]>
@nasahlpa nasahlpa requested a review from vogelpi January 17, 2025 16:26
@nasahlpa nasahlpa marked this pull request as ready for review January 17, 2025 16:26
@nasahlpa nasahlpa requested a review from a team as a code owner January 17, 2025 16:26
@nasahlpa nasahlpa requested review from jon-flatley and removed request for a team January 17, 2025 16:26
@nasahlpa nasahlpa added the CherryPick:earlgrey_1.0.0 This PR should be cherry-picked to earlgrey_1.0.0 label Jan 17, 2025
Comment on lines -566 to -571
pentest_set_trigger_high();
for (uint32_t i = 0; i < num_encryptions; ++i) {
aes_key_mask_and_config(batch_keys[i], kAesKeyLength);
aes_encrypt(batch_plaintexts[i], kAesTextLength);
}
pentest_set_trigger_low();
Copy link
Contributor

Choose a reason for hiding this comment

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

We moved those functions out of the encrypt to not have to repeatedly call it inside the hot loop during batch encryptions. I think it did have a notable performance impact but don't remember the numbers to be honest.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've checked whether this makes a big difference:

this PR:
simpleserial aes_fvsr_key_batch 20 batches: 615 traces / s
simpleserial aes_fvsr_key_batch 50 batches: 1444 traces / s

before this PR:
simpleserial aes_fvsr_key_batch 20 batches: 617 traces / s
simpleserial aes_fvsr_key_batch 50 batches: 1450 traces / s

So I think the change is reasonable :-)

aes_sca_wait_for_idle();
SS_CHECK_DIF_OK(dif_aes_trigger(&aes, kDifAesTriggerPrngReseed));
aes_sca_wait_for_idle();
SS_CHECK_DIF_OK(dif_aes_trigger(&aes, kDifAesTriggerDataOutClear));
Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't remember this was needed but it turns out it really is! It maybe worth to add a comment why.

Copy link
Member Author

Choose a reason for hiding this comment

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

I quickly checked, we actually do not need to clear the data out registers with dif_aes_trigger(&aes, kDifAesTriggerDataOutClear). Only triggering the Prng Reseed is sufficient and gives us the TVLA plot from above.

Added a comment & removed the data register clearing.

AES_TESTUTILS_WAIT_FOR_STATUS(&aes, kDifAesStatusIdle, true, kTestTimeout);
TRY(dif_aes_trigger(&aes, kDifAesTriggerPrngReseed));
AES_TESTUTILS_WAIT_FOR_STATUS(&aes, kDifAesStatusIdle, true, kTestTimeout);
TRY(dif_aes_trigger(&aes, kDifAesTriggerDataOutClear));
Copy link
Contributor

Choose a reason for hiding this comment

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

Also here I would add a comment.

Copy link
Member Author

Choose a reason for hiding this comment

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

Please see above.

Comment on lines 204 to 210
#if !OT_IS_ENGLISH_BREAKFAST
if (transaction.force_masks) {
// Disable masking. Force the masking PRNG output value to 0.
TRY(aes_sca_load_fixed_seed());
}
#endif

Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC, it doesn't hurt to call this on the CW305. Because the trigger register is there but the reseeding is just ignored (there is no CSRNG though which is why we need to exclude the CSRNG setup).

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems when adding this also for CW305, the code does not compile anymore. So I've kept the if guards.

Copy link
Contributor

@vogelpi vogelpi left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @nasahlpa !

With the recent changes in the PRNG, it is necessary to reseed the
internal AES state after the AUX register has been set by using the
dif_aes_start() function. We need to do this when we want to switch
off masking for AES SCA tests.

Signed-off-by: Pascal Nasahl <[email protected]>
@nasahlpa nasahlpa merged commit 45d184c into lowRISC:master Jan 20, 2025
38 checks passed
@nasahlpa nasahlpa deleted the fix_aes_masks_off branch January 20, 2025 14:24
Copy link

Backport failed for earlgrey_1.0.0, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin earlgrey_1.0.0
git worktree add -d .worktree/backport-25932-to-earlgrey_1.0.0 origin/earlgrey_1.0.0
cd .worktree/backport-25932-to-earlgrey_1.0.0
git switch --create backport-25932-to-earlgrey_1.0.0
git cherry-pick -x 5b864fede5d3630cb62436cb0cd6114322e9088f e91a4d2037e4249724f5581ba604588e261f5dce 8a379c6e26e5902cddd8063942c5b09ba224bafb

@github-actions github-actions bot added the Manually CherryPick This PR should be manually cherry picked. label Jan 20, 2025
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 Manually CherryPick This PR should be manually cherry picked.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants