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

Add AES with armv8 crypto extension #6895

Conversation

yuhaoth
Copy link
Contributor

@yuhaoth yuhaoth commented Jan 10, 2023

Description

fix #5387

Add aes with Armv8 crypto extension

Gatekeeper checklist

@yuhaoth yuhaoth force-pushed the pr/add-aes-with-armv8-crypto-extension branch from aa4a0b0 to ff9386d Compare January 10, 2023 09:55
@yuhaoth yuhaoth changed the title Add AES and GCM with armv8 crypto extension WIP: Add AES and GCM with armv8 crypto extension Jan 10, 2023
@yuhaoth
Copy link
Contributor Author

yuhaoth commented Jan 10, 2023

local tests pass at d0f7759. with gcc 11.3 on ubuntu 22.04

library/aesce.h Outdated
Comment on lines 60 to 61
* \note This function is only for internal use by other library
* functions; you must not call it directly.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is true for every function in library/, only functions declared in include/ are part of the public API.

@yanesca
Copy link
Contributor

yanesca commented Jan 10, 2023

The general direction looks good to me.

Do you think we could split this into two consecutive PRs? (Eg. the first just dealing with AES and the second adding AES-GCM.) I mean, it would make review slightly easier, but if it makes development more inconvenient for you then it doesn't worth it.

library/aesce.c Outdated
if( mode == MBEDTLS_AES_ENCRYPT )
vst1q_u8( &output[0], aesce_encrypt_block( in, keys, ctx->nr ) );
else
vst1q_u8( &output[0], aesce_decrypt_block( in, keys, ctx->nr ) );;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
vst1q_u8( &output[0], aesce_decrypt_block( in, keys, ctx->nr ) );;
vst1q_u8( &output[0], aesce_decrypt_block( in, keys, ctx->nr ) );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, will fix it in exists commit.

@yuhaoth
Copy link
Contributor Author

yuhaoth commented Jan 11, 2023

The general direction looks good to me.

Do you think we could split this into two consecutive PRs? (Eg. the first just dealing with AES and the second adding AES-GCM.) I mean, it would make review slightly easier, but if it makes development more inconvenient for you then it doesn't worth it.

Yes. I will split it if I think it is ready for review.

Beside GCM and AES, I plan to finish some works before review.

  • Clang and GCC versions compatible tests.
  • Add tests for aarch64. Base on qemu or Arm64 node.
  • Improve compile time architecture detection.
  • Improve runtime CPU feature sets detection.

@yuhaoth yuhaoth force-pushed the pr/add-aes-with-armv8-crypto-extension branch from d0f7759 to e1956c3 Compare January 11, 2023 02:27
@yuhaoth yuhaoth force-pushed the pr/add-aes-with-armv8-crypto-extension branch 6 times, most recently from 2022f3c to c737076 Compare January 12, 2023 04:52
library/aesce.c Outdated
#if defined(__clang__)
#error "Clang not supported yet"
#elif defined(__GNUC__)
#pragma GCC target ("arch=armv8-a+crypto")
Copy link
Contributor

Choose a reason for hiding this comment

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

We did this the other way round with the SHA-2 acceleration: when the option is enabled, we check that we've been given the correct compiler flags, rather than silently updating the target from what is specified on the command line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With arch=armv8.2-a+sha3+crypto and -O3, eor3 instruction is generated in assembly code of aesce.c . That cause illegal instruction error. So I disable SHA512 in .travis.yml . But that is not expected.

For time being, I think

  • We should set arch flags per file or per function
  • We should not force user enable the compiler flags. Those flags is only used for building library. If user only want to user library, why he must set the flags?

That is not finalized, I am still thinking and testing the issues. I might change above points :)

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 have posted issues at #5387 (comment)

Copy link
Contributor Author

@yuhaoth yuhaoth Feb 1, 2023

Choose a reason for hiding this comment

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

I have removed it.

And I think we should re-consider the rule due to the illegal instruction error when runtime detection enabled

Copy link
Contributor

Choose a reason for hiding this comment

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

As I've also mentioned on the architecture document, we can't just decide to compile for a different target. If the user wants to compile for a particular processor, we can't decide to compile for a different processor!

And I think we should re-consider the rule due to the illegal instruction error when runtime detection enabled

I'm sorry, I have no idea 1. what rule you're referring to and 2. what this has to do with an error when compiling some code (?) using a particular compiler option (?).

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe the problem is with runtime detection:

  1. You compile in both SHA acceleration (sha3 extension) and AES acceleration (crypto extension) with the intent of shipping it and trusting runtime detection to decide whether it is safe to run the optimised code.
  2. The compiler with -O3 option uses an instruction in the AES code that is only present in the sha3 extension but not in the generic crypto extension.
  3. On a platform where crypto extension is present but sha3 is not, the runtime detection will pass and run the accelerated AES code which uses an instruction that is not present.

(@yuhaoth please correct me if I am wrong.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yanesca , Yes, it is the problem.

And if sha3 extension is present, with sha3 compiler options, we can got best optimization. That's one another point I am thinking about in #7004 .

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'm sorry, I have no idea 1.
GCC and clang support two method to define target CPU modifier.

  • Option 1 with pragma
/* GCC */
#pragma GCC target ("arch=armv8-a+crypto")
/* Clang */
#pragma clang attribute push (__attribute__((target("crypto"))), apply_to=function)
  • Option2 with attribute
/* GCC */
__attribute__((target("arch=armv8-a+crypto")) void function(void);
/* Clang */
__attribute__((target("crypto")) void function(void);

what rule you're referring to and 2. what this has to do with an error when compiling some code (?) using a particular compiler option (?).

# error "Must use minimum -march=armv8.2-a+sha3 for MBEDTLS_SHA512_USE_A64_CRYPTO_*"
and
#error "Must use minimum -march=armv8-a+crypto for MBEDTLS_SHA256_USE_A64_CRYPTO_*"
. Compiler options are forced . If command line does not include the cpu modifier flags, the place will report error.

I prefer does not force them. And if users need best code size optimization, they should compile application and library with same cpu modifier flags.

@yuhaoth yuhaoth changed the title WIP: Add AES and GCM with armv8 crypto extension [WIP] Add AES and GCM with armv8 crypto extension Jan 12, 2023
@yuhaoth yuhaoth force-pushed the pr/add-aes-with-armv8-crypto-extension branch from 7cebf0e to 2876234 Compare January 13, 2023 05:36
@yuhaoth yuhaoth changed the title [WIP] Add AES and GCM with armv8 crypto extension Add AES with armv8 crypto extension Jan 13, 2023
@yuhaoth yuhaoth force-pushed the pr/add-aes-with-armv8-crypto-extension branch from 2876234 to 93c9e24 Compare January 13, 2023 06:15
@yuhaoth yuhaoth force-pushed the pr/add-aes-with-armv8-crypto-extension branch 3 times, most recently from 7b25375 to 18500f6 Compare January 17, 2023 10:47
@yuhaoth yuhaoth force-pushed the pr/add-aes-with-armv8-crypto-extension branch from 3a100de to f7c1dda Compare January 30, 2023 07:22
@yuhaoth yuhaoth added priority-high High priority - will be reviewed soon component-crypto Crypto primitives and low-level interfaces size-m Estimated task size: medium (~1w) labels Jan 30, 2023
@yuhaoth yuhaoth force-pushed the pr/add-aes-with-armv8-crypto-extension branch from f7c1dda to 330a0e0 Compare January 30, 2023 07:47
@yuhaoth yuhaoth mentioned this pull request Jan 30, 2023
3 tasks
Signed-off-by: Jerry Yu <jerry.h.yu@arm.com>
Signed-off-by: Jerry Yu <jerry.h.yu@arm.com>
Signed-off-by: Jerry Yu <jerry.h.yu@arm.com>
yanesca
yanesca previously approved these changes Feb 24, 2023
Copy link
Contributor

@yanesca yanesca left a comment

Choose a reason for hiding this comment

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

LGTM

Signed-off-by: Jerry Yu <jerry.h.yu@arm.com>
Copy link
Contributor

@yanesca yanesca left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@tom-cosgrove-arm tom-cosgrove-arm left a comment

Choose a reason for hiding this comment

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

LGTM

@tom-cosgrove-arm tom-cosgrove-arm added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members, labels Feb 28, 2023
@tom-cosgrove-arm
Copy link
Contributor

CI is fully green

@tom-cosgrove-arm
Copy link
Contributor

@xkqian Have the changes you requested been addressed?

@gilles-peskine-arm gilles-peskine-arm dismissed xkqian’s stale review February 28, 2023 17:15

All requested changes have been performed.

@gilles-peskine-arm
Copy link
Contributor

@xkqian Given that this is very-high-priority and all the changes you requested have been addressed, I'm going ahead and merging this pull request. If you want more changes, we can do them in a follow-up PR.

@gilles-peskine-arm gilles-peskine-arm merged commit b52b788 into Mbed-TLS:development Feb 28, 2023
@xkqian
Copy link
Contributor

xkqian commented Mar 1, 2023

@xkqian Have the changes you requested been addressed?

Yes, all of them have been addressed, thanks for the reminder.

@xkqian
Copy link
Contributor

xkqian commented Mar 1, 2023

@xkqian Given that this is very-high-priority and all the changes you requested have been addressed, I'm going ahead and merging this pull request. If you want more changes, we can do them in a follow-up PR.

Sure, all of my requests have been addressed. go ahead!

@yuhaoth yuhaoth deleted the pr/add-aes-with-armv8-crypto-extension branch March 1, 2023 02:40
@yuhaoth yuhaoth mentioned this pull request Mar 3, 2023
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports component-crypto Crypto primitives and low-level interfaces priority-very-high Highest priority - prioritise this over other review work size-m Estimated task size: medium (~1w)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support armv8 crypto extensions for AES and GCM
6 participants