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

Enabling DIT flag in AArch64. #1687

Merged
merged 16 commits into from
Aug 9, 2024
Merged

Enabling DIT flag in AArch64. #1687

merged 16 commits into from
Aug 9, 2024

Conversation

nebeid
Copy link
Contributor

@nebeid nebeid commented Jul 2, 2024

Issues:

Addresses CryptoAlg-2501

Description of changes:

  • Add a compile flag to enable it.
  • DIT APIs and macro are available for external use.
  • Add an option -dit to the speed test that's available in a build that used the compile flag.
    Enabling it makes the benchmarks closer to not having DIT enabled.
    This is to suggest that the macro can be used by AWS-LC callers in their own scope
    around multiple calls to the library to improve the performance since,
    in that case, DIT would not switch on and off at the entry/exit of the library functions.

Call-outs:

N/A

Testing:

  • A test is added to CI with the compile flag that enables DIT set to ON.
  • Tested with printouts the value of the DIT when it's expected to be turned 1 or 0.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.

- Add a compile flag to enable it.
- DIT APIs and macro are available for external use.
  - Add the macro to Speed() commented out. That is where it was experimented with.
    Enabling it there make the benchmarks similar to not having DIT enabled.
    This is to suggest that the macro can be used by AWS-LC callers in their own scope
    around multiple calls to the library to improve the performance since DIT would not
    switch on and off at the entry/exit of the library functions.
@nebeid nebeid requested a review from a team as a code owner July 2, 2024 14:54
@nebeid nebeid changed the title Enabling DIT flag. Enabling DIT flag in AArch64. Jul 2, 2024
@codecov-commenter
Copy link

codecov-commenter commented Jul 2, 2024

Codecov Report

Attention: Patch coverage is 77.35849% with 12 lines in your changes missing coverage. Please review.

Project coverage is 78.44%. Comparing base (5fd0f08) to head (76e06d1).

Files Patch % Lines
crypto/fipsmodule/dh/dh.c 75.00% 4 Missing ⚠️
crypto/fipsmodule/evp/evp.c 0.00% 4 Missing ⚠️
crypto/fipsmodule/rsa/rsa.c 81.81% 4 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1687   +/-   ##
=======================================
  Coverage   78.43%   78.44%           
=======================================
  Files         580      580           
  Lines       96756    96779   +23     
  Branches    13877    13864   -13     
=======================================
+ Hits        75888    75915   +27     
+ Misses      20249    20248    -1     
+ Partials      619      616    -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

CMakeLists.txt Outdated
@@ -23,6 +23,7 @@ option(ENABLE_DILITHIUM "Enable Dilithium signatures in the EVP API" OFF)
option(DISABLE_PERL "Disable Perl for AWS-LC" OFF)
option(DISABLE_GO "Disable Go for AWS-LC" OFF)
option(ENABLE_FIPS_ENTROPY_CPU_JITTER "Enable FIPS entropy source: CPU Jitter" OFF)
option(ENABLE_DATA_INDEPENDENT_TIMING "Enable Data-Independent Timing (DIT) flag" OFF)
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe a bit more specific?

Suggested change
option(ENABLE_DATA_INDEPENDENT_TIMING "Enable Data-Independent Timing (DIT) flag" OFF)
option(ENABLE_DATA_INDEPENDENT_TIMING_AARCH64 "Enable Data-Independent Timing (DIT) flag" OFF)

CMakeLists.txt Outdated
@@ -801,6 +802,11 @@ else()
set(ARCH "generic")
endif()

#if(ENABLE_DATA_INDEPENDENT_TIMING AND ARCH EQUAL "aarch64")
Copy link
Contributor

Choose a reason for hiding this comment

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

leftover?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed. Thanks.

crypto/fipsmodule/cpucap/cpu_aarch64.c Outdated Show resolved Hide resolved
// Rt = 0x1f
uint64_t armv8_enable_dit(void) {
uint64_t original_dit = armv8_get_dit();
if (CRYPTO_is_ARMv8_DIT_capable() && original_dit != 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

do we have to check original_dit != 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed. Thanks.

uint64_t armv8_enable_dit(void);
void armv8_restore_dit(volatile uint64_t *original_dit);

#define ENABLE_DIT_ALL \
Copy link
Contributor

Choose a reason for hiding this comment

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

seems like this is unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, thanks dkostic. Removed.

include/openssl/crypto.h Show resolved Hide resolved
OPENSSL_UNUSED = armv8_enable_dit();


#define ENABLE_DIT_AUTO_DISABLE \
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 having both enable and disable in the name is a bit confusing. can we do SET_DIT_AUTO_DISABLE or something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

include/openssl/crypto.h Show resolved Hide resolved
@torben-hansen torben-hansen self-requested a review July 7, 2024 14:30
// https://github.com/ashwio/arm64-sysreg-lib/blob/d421e249a026f6f14653cb6f9c4edd8c5d898595/include/sysreg/dit.h#L286
#define DIT_REGISTER s3_3_c4_c2_5

uint64_t armv8_get_dit(void) {
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 this function can be static since it's not used outside this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

crypto/fipsmodule/cpucap/cpu_aarch64.c Show resolved Hide resolved
crypto/fipsmodule/cpucap/cpu_aarch64.c Outdated Show resolved Hide resolved
dkostic
dkostic previously approved these changes Aug 5, 2024
@AGSaidi
Copy link
Member

AGSaidi commented Aug 5, 2024

I don't see any documentation about how, if all the DIT twiddling cause perf degradation, at a higher level in the program DIT could be enabled. Is it worth documentating that somewhere to try and define an API for that that other crypto libs would also adopt?

@@ -114,6 +114,7 @@ int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) {
return 0;
}
GUARD_PTR(out);
SET_DIT_AUTO_DISABLE;
Copy link
Contributor

Choose a reason for hiding this comment

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

can we have a convention to always add at the beginning of a function? That the error path might take a negligible amount of extra time is fine IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


const BIGNUM *DH_get0_pub_key(const DH *dh) { return dh->pub_key; }
const BIGNUM *DH_get0_pub_key(const DH *dh) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Needed when simply returning a pointer?

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 adopted the mind set of protecting access to structs that contain secret data that can cause memory prefetching.

@@ -97,6 +97,7 @@ DH *DH_new_by_nid(int nid) {
}
Copy link
Contributor

Choose a reason for hiding this comment

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

missed calculate_rfc7919_DH_from_p()?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh I guess those are public parameters...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, I don't see any secret used or accessed (even nearby) in this function.

crypto/fipsmodule/ec/ec.c Show resolved Hide resolved
tool/speed.cc Outdated
@@ -2584,6 +2584,7 @@ static bool parseStringVectorToIntegerVector(
}

bool Speed(const std::vector<std::string> &args) {
//SET_DIT_AUTO_DISABLE;
Copy link
Contributor

Choose a reason for hiding this comment

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

You can add this as an option to the speed tool

@@ -58,7 +58,7 @@
#include <openssl/mem.h>

#include "../../internal.h"

#include "../service_indicator/internal.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

why include?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed. Thanks.

- Also add documentation to the user on the use of the macro
and the functions.
- Enable the use of DIT flag in speed test via a command line option.
@nebeid
Copy link
Contributor Author

nebeid commented Aug 7, 2024

I don't see any documentation about how, if all the DIT twiddling cause perf degradation, at a higher level in the program DIT could be enabled. Is it worth documentating that somewhere to try and define an API for that that other crypto libs would also adopt?

Thank you @AGSaidi, I added the documentation to BUILDING.md and to crypto.h.

@nebeid nebeid merged commit 697b277 into aws:main Aug 9, 2024
106 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants