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

[aes] Add AES-GCM model and DPI #4

Merged
merged 2 commits into from
Nov 12, 2024

Conversation

nasahlpa
Copy link
Collaborator

This PR consists of two commits:

  • The first commit adds AES-GCM to the AES models
  • The second commit integrates the AES-GCM model

Copy link
Owner

@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 @nasahlpa , this looks great! I have added some comments, most of them are really minor.

@@ -20,7 +20,7 @@ In addition, this directory also contains two example applications.
2. `aes_modes`:
- Shows how to interface the OpenSSL/BoringSSL interface functions.
- Checks the output of BoringSSL/OpenSSL versus expected results.
- Supports ECB, CBC, CTR modes.
- Supports ECB, CBC, CTR, GCM modes.
Copy link
Owner

Choose a reason for hiding this comment

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

also CFB and OFB are supported, I forgot to add that back then.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added.

@@ -14,7 +14,8 @@ typedef enum crypto_mode {
kCryptoAesCfb = 1 << 2,
kCryptoAesOfb = 1 << 3,
kCryptoAesCtr = 1 << 4,
kCryptoAesNone = 1 << 5
kCryptoAesGcm = 1 << 5,
kCryptoAesNone = 1 << 6
Copy link
Owner

Choose a reason for hiding this comment

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

We've changed this to not alter the software interface of AES and maintain binary compatibility. kCryptoAesNone is now 6'b11_1111 instead of 7'b100_0000 (initial approach) or 6'b10_0000 (original design, now used for GCM).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've now changed kCryptoAesNone = 0x3f.

Comment on lines 28 to 31
* @param mode_i Cipher mode: 6'b00_0001 = ECB, 6'00_b0010 = CBC,
* 6'b00_0100 = CFB, 6'b00_1000 = OFB,
* 6'b01_0000 = CTR, 6'b10_0000 = NONE
* @param mode_i Cipher mode: 7'b000_0001 = ECB, 7'000_b0010 = CBC,
* 7'b000_0100 = CFB, 7'b000_1000 = OFB,
* 7'b001_0000 = CTR, 7'b010_0000 = GCM,
* 7'b010_0000 = NONE
Copy link
Owner

Choose a reason for hiding this comment

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

NONE is now 6'b11_1111. I've also left a comment somewhere else about this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

* @param data_o Output data, 1D byte array (open array in SV)
* @param tag_o Output auth. tag, 2D state matrix (3D packed array in SV)
Copy link
Owner

Choose a reason for hiding this comment

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

The output tag should also be a 1D array.

Copy link
Owner

Choose a reason for hiding this comment

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

Sorry, we discussed before to use the same data format for this as for the key and IV. I.e. in SV it's a 4 x 32-bit packed 2D array.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done, @andrea-caforio is testing it and reporting back.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Checked by @andrea-caforio, it is now working. Thanks!

* @param data_o Output data, 2D state matrix (3D packed array in SV)
* @param tag_o Output auth. tag, 2D state matrix (3D packed array in SV)
*/
void c_dpi_aes_crypt_block(const unsigned char impl_i, const unsigned char op_i,
Copy link
Owner

Choose a reason for hiding this comment

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

There is also a call to this function in hw/top_earlgrey/dv/env/seq_lib/chip_sw_keymgr_sideload_aes_vseq.sv which would need to be adjusted.

Copy link
Owner

Choose a reason for hiding this comment

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

Actually, I think we should not change this function at all. Because this is block based anyway. For all the other modes, it's sufficient to pass key, IV, input data to fully define what the output of the next block should be.

This doesn't hold for GCM which also has the internal GHASH state. This is not captured by the IV or any of the other variables.

Copy link
Owner

Choose a reason for hiding this comment

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

So when this function is called with GCM mode as argument, it should just throw an error and abort. WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done, I am now aborting.

*/
void c_dpi_aes_crypt_message(unsigned char impl_i, unsigned char op_i,
const svBitVecVal *mode_i, const svBitVecVal *iv_i,
const svBitVecVal *key_len_i,
const svBitVecVal *key_i,
const svOpenArrayHandle data_i,
svOpenArrayHandle data_o);
const svOpenArrayHandle aad_i,
const svBitVecVal *tag_i, svOpenArrayHandle data_o,
Copy link
Owner

Choose a reason for hiding this comment

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

Nit: line break between tag_i and data_o for readability?

Copy link
Owner

Choose a reason for hiding this comment

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

Damn, this is actually enforced by lint.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Reverted.

This commit adds support for the AES-GCM mode to the
AES models. For the comparison, test vectors specified
by NIST are used.

Signed-off-by: Pascal Nasahl <[email protected]>
@nasahlpa nasahlpa force-pushed the aes-gcm-review branch 4 times, most recently from 33d442a to d277855 Compare November 12, 2024 14:11
@vogelpi
Copy link
Owner

vogelpi commented Nov 12, 2024

Thanks a lot Pascal!

Comment on lines 66 to 73
input bit [5:0] mode_i, // 6'b00_0001 = ECB, 6'00_b0010 = CBC, 6'b00_0100 = CFB,
// 6'b00_1000 = OFB, 6'b01_0000 = CTR, 6'b10_0000 = NONE
input bit [6:0] mode_i, // 7'b000_0001 = ECB, 7'000_b0010 = CBC, 7'b000_0100 = CFB,
// 7'b000_1000 = OFB, 7'b001_0000 = CTR, 7'b010_0000 = GCM,
// 7'b100_0000 = NONE
Copy link
Owner

Choose a reason for hiding this comment

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

Also this one would need to be changed back.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah thanks, I've missed the sv_dpi_aes_crypt_block function. As this is also a block related function, I am removing the tags/aad as well.

@nasahlpa nasahlpa force-pushed the aes-gcm-review branch 5 times, most recently from 0e1deb8 to 402127b Compare November 12, 2024 15:33
This commit enhances the existing AES DPI model with the GCM mode.
Currently, only the OpenSSL/BoringSSL reference implementation
is supported.

Signed-off-by: Pascal Nasahl <[email protected]>
Copy link
Owner

@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.

Perfect, thank you both @andrea-caforio and @nasahlpa !

@vogelpi vogelpi merged commit 1949f42 into vogelpi:aes-gcm-review Nov 12, 2024
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants