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

[sca] Add AES PRNG for aes-fvsr-key-batch capture #20238

Merged
merged 1 commit into from
Nov 15, 2023

Conversation

vrozic
Copy link
Contributor

@vrozic vrozic commented Oct 31, 2023

In order to comply with the Derived Test Requirements for AES TVLA all input data needs to be generated using AES-based PRNG. This commit replaces the currently used Mersenne twister PRNG with SW AES running on ibex.
The change is made only for aes-fvsr-key-batch capture.

@vrozic vrozic force-pushed the aes-sw-prng branch 5 times, most recently from 7994bfb to e44ea5f Compare November 1, 2023 18:18
@vrozic vrozic marked this pull request as ready for review November 2, 2023 08:20
@vrozic vrozic requested a review from a team as a code owner November 2, 2023 08:20
@vrozic vrozic requested review from jon-flatley and removed request for a team November 2, 2023 08:20
@vogelpi vogelpi requested review from vogelpi and removed request for jon-flatley November 3, 2023 09:30
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.

Thanks for working on this @vrozic . I haven't done an in-depth review of the C code as you're still working on this to improve the performance. Howefver, I've left a comment to document the purpose of this AES implementation for others in the project.

sw/device/sca/lib/aes.c Outdated Show resolved Hide resolved
@vrozic vrozic force-pushed the aes-sw-prng branch 6 times, most recently from 4f351d9 to 3dde8d3 Compare November 8, 2023 16:36
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.

This looks pretty good, nice work @vrozic !

I have left some comments but nothing major. In general, some of the literals in the c implementation should be replaced by enums. Further, it would probably be good of if someone from our software team could provide feedback as well.

sw/device/sca/aes_serial.c Outdated Show resolved Hide resolved
sw/device/sca/aes_serial.c Outdated Show resolved Hide resolved
sw/device/sca/aes_serial.c Outdated Show resolved Hide resolved
sw/device/sca/aes_serial.c Outdated Show resolved Hide resolved
sw/device/sca/aes_serial.c Outdated Show resolved Hide resolved
sw/device/sca/lib/aes.h Outdated Show resolved Hide resolved
sw/device/sca/lib/aes.c Outdated Show resolved Hide resolved
sw/device/sca/lib/aes.c Outdated Show resolved Hide resolved
sw/device/sca/aes_serial.c Outdated Show resolved Hide resolved
sw/device/sca/lib/aes.h Show resolved Hide resolved
@vrozic vrozic force-pushed the aes-sw-prng branch 2 times, most recently from b1ab68c to c086534 Compare November 10, 2023 16:44
@vrozic vrozic force-pushed the aes-sw-prng branch 2 times, most recently from 4b1a3d3 to ad5d5f9 Compare November 10, 2023 17:05
Copy link
Member

@HU90m HU90m left a comment

Choose a reason for hiding this comment

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

The code looks good to me

/**
* An array to store pre-computed round keys derived from key_gen.
*/
uint32_t key_gen_round_keys[(kAesKeyLength / 4) * 11];
Copy link
Member

Choose a reason for hiding this comment

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

Is it worth explicitly zero initializing this? It would be nice if there wasn't undefined behavior in a case someone accidentally sends a b or g command before initializing (with a d command).

That said I don't know this part of the code base well, so maybe this isn't a concern.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair point @HU90m . I think there are no specific concerns regarding this but it also wouldn't hurt to zero initialize this. I am in favor of initializing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree it's a good idea to initialize this array, but rather than zeros I would initialize it to the round-key values derived from key_gen. This way we can also get rid of computing key schedule.
I've pushed these changes.

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.

This looks good, thanks @vrozic!

I have one remaining question and I think it would be good to zero initialize the round key array as suggested by Hugo.

sw/device/sca/lib/aes.c Outdated Show resolved Hide resolved
sw/device/sca/lib/aes.c Outdated Show resolved Hide resolved
sw/device/sca/lib/aes.c Outdated Show resolved Hide resolved
Comment on lines 112 to 128
uint8_t key_fixed[kAesTextLength] = {0x81, 0x1E, 0x37, 0x31, 0xB0, 0x12,
0x0A, 0x78, 0x42, 0x78, 0x1E, 0x22,
0xB2, 0x5C, 0xDD, 0xF9};
Copy link
Contributor

Choose a reason for hiding this comment

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

Also this one is fixed and never changes right? So, it could be const as well. Or would this be changing if we would do fvsr data TVLA?

Copy link
Contributor

Choose a reason for hiding this comment

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

If yes, I would leave this as is.

Copy link
Contributor

Choose a reason for hiding this comment

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

These variable should be static to limit the scope to this file only.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If yes, I would leave this as is.

We would indeed need to use a different constant for fvsr data TVLA. So for now I would keep this as a variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These variable should be static to limit the scope to this file only.

Thanks for catching this. I've changed it now.

Copy link
Contributor

@engdoreis engdoreis left a comment

Choose a reason for hiding this comment

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

Great work @vrozic, I left some suggestions related to code style. Otherwise it LGTM.

sw/device/sca/aes_serial.c Outdated Show resolved Hide resolved
Comment on lines 112 to 128
uint8_t key_fixed[kAesTextLength] = {0x81, 0x1E, 0x37, 0x31, 0xB0, 0x12,
0x0A, 0x78, 0x42, 0x78, 0x1E, 0x22,
0xB2, 0x5C, 0xDD, 0xF9};
Copy link
Contributor

Choose a reason for hiding this comment

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

These variable should be static to limit the scope to this file only.

sw/device/sca/aes_serial.c Outdated Show resolved Hide resolved
sw/device/sca/lib/aes.c Outdated Show resolved Hide resolved
sw/device/sca/lib/aes.c Outdated Show resolved Hide resolved
sw/device/sca/lib/aes.c Outdated Show resolved Hide resolved
sw/device/sca/lib/aes.h Outdated Show resolved Hide resolved
sw/device/sca/lib/aes.h Outdated Show resolved Hide resolved
sw/device/sca/lib/aes.h Outdated Show resolved Hide resolved
sw/device/sca/lib/aes.c Outdated Show resolved Hide resolved
In order to comply with the Derived Test Requirements for AES TVLA
all input data needs to be generated using AES-based PRNG.
This commit replaces the currently used Mersenne twister PRNG with
SW AES running on ibex.

Signed-off-by: Vladimir Rozic <vrozic@lowrisc.org>
Copy link
Contributor

@engdoreis engdoreis left a comment

Choose a reason for hiding this comment

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

Thanks @vrozic for the changes.

@engdoreis engdoreis merged commit 93c9a87 into lowRISC:master Nov 15, 2023
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status:Ready to merge PR is ready to be merged by a committer.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants