Skip to content

Commit 3d9a9ac

Browse files
authoredJul 24, 2023
Add BoringSSL Dispatch Test for aarch64 (aws#1093)
add armv8 dispatch tests checking for the following function hits: * aes_hw_ctr32_encrypt_blocks * aes_hw_encrypt * aes_gcm_enc_kernel * aes_hw_set_encrypt_key * vpaes_encrypt * vpaes_set_encrypt_key * sha256_block_armv8 * aesv8_gcm_8x_enc_128 * sha512_block_armv8
1 parent feca631 commit 3d9a9ac

26 files changed

+359
-30
lines changed
 

‎crypto/fipsmodule/aes/asm/aesv8-armx.pl

+25
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@
9999
.Lenc_key:
100100
___
101101
$code.=<<___ if ($flavour =~ /64/);
102+
#ifdef BORINGSSL_DISPATCH_TEST
103+
.extern BORINGSSL_function_hit
104+
adrp x6,:pg_hi21:BORINGSSL_function_hit
105+
add x6, x6, :lo12:BORINGSSL_function_hit
106+
mov w7, #1
107+
strb w7, [x6,#3] // kFlag_aes_hw_set_encrypt_key
108+
#endif
102109
// Armv8.3-A PAuth: even though x30 is pushed to stack it is not popped later.
103110
AARCH64_VALID_CALL_TARGET
104111
stp x29,x30,[sp,#-16]!
@@ -343,6 +350,17 @@ ()
343350
.type ${prefix}_${dir}crypt,%function
344351
.align 5
345352
${prefix}_${dir}crypt:
353+
___
354+
$code.=<<___ if ($flavour =~ /64/);
355+
#ifdef BORINGSSL_DISPATCH_TEST
356+
.extern BORINGSSL_function_hit
357+
adrp x6,:pg_hi21:BORINGSSL_function_hit
358+
add x6, x6, :lo12:BORINGSSL_function_hit
359+
mov w7, #1
360+
strb w7, [x6,#1] // kFlag_aes_hw_encrypt
361+
#endif
362+
___
363+
$code.=<<___;
346364
AARCH64_VALID_CALL_TARGET
347365
ldr $rounds,[$key,#240]
348366
vld1.32 {$rndkey0},[$key],#16
@@ -722,6 +740,13 @@ ()
722740
${prefix}_ctr32_encrypt_blocks:
723741
___
724742
$code.=<<___ if ($flavour =~ /64/);
743+
#ifdef BORINGSSL_DISPATCH_TEST
744+
.extern BORINGSSL_function_hit
745+
adrp x6,:pg_hi21:BORINGSSL_function_hit
746+
add x6, x6, :lo12:BORINGSSL_function_hit
747+
mov w7, #1
748+
strb w7, [x6] // kFlag_aes_hw_ctr32_encrypt_blocks
749+
#endif
725750
// Armv8.3-A PAuth: even though x30 is pushed to stack it is not popped later.
726751
AARCH64_VALID_CALL_TARGET
727752
stp x29,x30,[sp,#-16]!

‎crypto/fipsmodule/aes/asm/vpaes-armv8.pl

+14
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,13 @@
264264
.type vpaes_encrypt,%function
265265
.align 4
266266
vpaes_encrypt:
267+
#ifdef BORINGSSL_DISPATCH_TEST
268+
.extern BORINGSSL_function_hit
269+
adrp x6,:pg_hi21:BORINGSSL_function_hit
270+
add x6, x6, :lo12:BORINGSSL_function_hit
271+
mov w7, #1
272+
strb w7, [x6,#4] // kFlag_vpaes_encrypt
273+
#endif
267274
AARCH64_SIGN_LINK_REGISTER
268275
stp x29,x30,[sp,#-16]!
269276
add x29,sp,#0
@@ -1072,6 +1079,13 @@
10721079
.type vpaes_set_encrypt_key,%function
10731080
.align 4
10741081
vpaes_set_encrypt_key:
1082+
#ifdef BORINGSSL_DISPATCH_TEST
1083+
.extern BORINGSSL_function_hit
1084+
adrp x6,:pg_hi21:BORINGSSL_function_hit
1085+
add x6, x6, :lo12:BORINGSSL_function_hit
1086+
mov w7, #1
1087+
strb w7, [x6,#5] // kFlag_vpaes_set_encrypt_key
1088+
#endif
10751089
AARCH64_SIGN_LINK_REGISTER
10761090
stp x29,x30,[sp,#-16]!
10771091
add x29,sp,#0

‎crypto/fipsmodule/cpucap/cpucap.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ HIDDEN uint32_t OPENSSL_armcap_P = 0;
8585

8686
#if defined(BORINGSSL_DISPATCH_TEST)
8787
// This value must be explicitly initialized to zero. See similar comment above.
88-
HIDDEN uint8_t BORINGSSL_function_hit[8] = {0};
89-
#endif
88+
HIDDEN uint8_t BORINGSSL_function_hit[9] = {0};
89+
#endif // BORINGSSL_DISPATCH_TEST
9090

9191
// This variable is used only for testing purposes to ensure that the library
9292
// constructor is executed and the capability variable is initialized.

‎crypto/fipsmodule/modes/asm/aesv8-gcm-armv8-unroll8.pl

+7
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@
262262
.type aesv8_gcm_8x_enc_128,%function
263263
.align 4
264264
aesv8_gcm_8x_enc_128:
265+
#ifdef BORINGSSL_DISPATCH_TEST
266+
.extern BORINGSSL_function_hit
267+
adrp x6,:pg_hi21:BORINGSSL_function_hit
268+
add x6, x6, :lo12:BORINGSSL_function_hit
269+
mov w7, #1
270+
strb w7, [x6,#7] // kFlag_aesv8_gcm_8x_enc_128
271+
#endif
265272
AARCH64_VALID_CALL_TARGET
266273
cbz x1, .L128_enc_ret
267274
stp d8, d9, [sp, #-80]!

‎crypto/fipsmodule/modes/asm/aesv8-gcm-armv8.pl

+7
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,13 @@
288288
.type aes_gcm_enc_kernel,%function
289289
.align 4
290290
aes_gcm_enc_kernel:
291+
#ifdef BORINGSSL_DISPATCH_TEST
292+
.extern BORINGSSL_function_hit
293+
adrp x6,:pg_hi21:BORINGSSL_function_hit
294+
add x6, x6, :lo12:BORINGSSL_function_hit
295+
mov w7, #1
296+
strb w7, [x6,#2] // kFlag_aes_gcm_enc_kernel
297+
#endif
291298
AARCH64_SIGN_LINK_REGISTER
292299
stp x29, x30, [sp, #-128]!
293300
mov x29, sp

‎crypto/fipsmodule/sha/asm/sha512-armv8.pl

+14
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,13 @@ sub BODY_00_xx {
362362
.align 6
363363
sha256_block_armv8:
364364
.Lv8_entry:
365+
#ifdef BORINGSSL_DISPATCH_TEST
366+
.extern BORINGSSL_function_hit
367+
adrp x6,:pg_hi21:BORINGSSL_function_hit
368+
add x6, x6, :lo12:BORINGSSL_function_hit
369+
mov w7, #1
370+
strb w7, [x6,#6] // kFlag_sha256_hw
371+
#endif
365372
// Armv8.3-A PAuth: even though x30 is pushed to stack it is not popped later.
366373
stp x29,x30,[sp,#-16]!
367374
add x29,sp,#0
@@ -448,6 +455,13 @@ sub BODY_00_xx {
448455
.align 6
449456
sha512_block_armv8:
450457
.Lv8_entry:
458+
#ifdef BORINGSSL_DISPATCH_TEST
459+
.extern BORINGSSL_function_hit
460+
adrp x6,:pg_hi21:BORINGSSL_function_hit
461+
add x6, x6, :lo12:BORINGSSL_function_hit
462+
mov w7, #1
463+
strb w7, [x6,#8] // kFlag_sha512_hw
464+
#endif
451465
stp x29,x30,[sp,#-16]!
452466
add x29,sp,#0
453467

‎crypto/impl_dispatch_test.cc

+68-27
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ class ImplDispatchTest : public ::testing::Test {
3636
public:
3737
void SetUp() override {
3838
#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
39-
aesni_ = CRYPTO_is_AESNI_capable();
39+
aes_hw_ = CRYPTO_is_AESNI_capable();
4040
avx_movbe_ = CRYPTO_is_AVX_capable() && CRYPTO_is_MOVBE_capable();
41-
ssse3_ = CRYPTO_is_SSSE3_capable();
41+
aes_vpaes_ = CRYPTO_is_SSSE3_capable();
4242
sha_ext_ = CRYPTO_is_SHAEXT_capable();
43-
vaes_vpclmulqdq_ =
44-
(OPENSSL_ia32cap_P[2] & 0xC0030000) && // AVX512{F+DQ+BW+VL}
45-
(((OPENSSL_ia32cap_P[3] >> 9) & 0x3) == 0x3); // VAES + VPCLMULQDQ
43+
vaes_vpclmulqdq_ = CRYPTO_is_AVX512_capable() &&
44+
CRYPTO_is_VAES_capable() &&
45+
CRYPTO_is_VPCLMULQDQ_capable();
4646
is_x86_64_ =
4747
#if defined(OPENSSL_X86_64)
4848
true;
@@ -61,7 +61,14 @@ class ImplDispatchTest : public ::testing::Test {
6161
#else
6262
false;
6363
#endif // MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX
64-
#endif // X86 || X86_64
64+
#elif defined(OPENSSL_AARCH64)
65+
aes_hw_ = CRYPTO_is_ARMv8_AES_capable();
66+
aes_vpaes_ = CRYPTO_is_NEON_capable();
67+
aes_gcm_pmull_ = CRYPTO_is_ARMv8_PMULL_capable();
68+
aes_gcm_8x_ = CRYPTO_is_ARMv8_GCM_8x_capable();
69+
sha_ext_ = OPENSSL_armcap_P & ARMV8_SHA256;
70+
sha_512_ext_ = OPENSSL_armcap_P & ARMV8_SHA512;
71+
#endif
6572
}
6673

6774
protected:
@@ -88,50 +95,70 @@ class ImplDispatchTest : public ::testing::Test {
8895
}
8996
}
9097

98+
bool aes_hw_ = false;
99+
bool aes_vpaes_ = false;
100+
bool sha_ext_ = false;
91101
#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
92102
bool vaes_vpclmulqdq_ = false;
93-
bool aesni_ = false;
94103
bool avx_movbe_ = false;
95-
bool ssse3_ = false;
96-
bool sha_ext_ = false;
97104
bool is_x86_64_ = false;
98105
bool is_assembler_too_old = false;
99106
bool is_assembler_too_old_avx512 = false;
107+
#else // AARCH64
108+
bool aes_gcm_pmull_ = false;
109+
bool aes_gcm_8x_ = false;
110+
bool sha_512_ext_ = false;
100111
#endif
112+
101113
};
102114

103-
#if !defined(OPENSSL_NO_ASM) && \
104-
(defined(OPENSSL_X86) || defined(OPENSSL_X86_64))
115+
#if !defined(OPENSSL_NO_ASM) && (defined(OPENSSL_X86) || \
116+
defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64))
105117

106118
constexpr size_t kFlag_aes_hw_ctr32_encrypt_blocks = 0;
107119
constexpr size_t kFlag_aes_hw_encrypt = 1;
108-
constexpr size_t kFlag_aesni_gcm_encrypt = 2;
109120
constexpr size_t kFlag_aes_hw_set_encrypt_key = 3;
110121
constexpr size_t kFlag_vpaes_encrypt = 4;
111122
constexpr size_t kFlag_vpaes_set_encrypt_key = 5;
112-
constexpr size_t kFlag_sha256_shaext = 6;
123+
constexpr size_t kFlag_sha256_hw = 6;
124+
#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
125+
constexpr size_t kFlag_aesni_gcm_encrypt = 2;
113126
constexpr size_t kFlag_aes_gcm_encrypt_avx512 = 7;
127+
#else // AARCH64
128+
constexpr size_t kFlag_aes_gcm_enc_kernel = 2;
129+
constexpr size_t kFlag_aesv8_gcm_8x_enc_128 = 7;
130+
constexpr size_t kFlag_sha512_hw = 8;
131+
#endif
114132

115133
TEST_F(ImplDispatchTest, AEAD_AES_GCM) {
116134
AssertFunctionsHit(
117135
{
118-
{kFlag_aes_hw_ctr32_encrypt_blocks, aesni_ &&
136+
{kFlag_aes_hw_encrypt, aes_hw_},
137+
{kFlag_aes_hw_set_encrypt_key, aes_hw_},
138+
{kFlag_vpaes_encrypt, aes_vpaes_ && !aes_hw_},
139+
{kFlag_vpaes_set_encrypt_key, aes_vpaes_ && !aes_hw_},
140+
#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
141+
{kFlag_aes_hw_ctr32_encrypt_blocks, aes_hw_ &&
119142
(is_assembler_too_old || !vaes_vpclmulqdq_)},
120-
{kFlag_aes_hw_encrypt, aesni_},
121-
{kFlag_aes_hw_set_encrypt_key, aesni_},
122143
{kFlag_aesni_gcm_encrypt,
123-
is_x86_64_ && aesni_ && avx_movbe_ &&
144+
is_x86_64_ && aes_hw_ && avx_movbe_ &&
124145
!is_assembler_too_old && !vaes_vpclmulqdq_},
125-
{kFlag_vpaes_encrypt, ssse3_ && !aesni_},
126-
{kFlag_vpaes_set_encrypt_key, ssse3_ && !aesni_},
127146
{kFlag_aes_gcm_encrypt_avx512,
128-
is_x86_64_ && aesni_ &&
147+
is_x86_64_ && aes_hw_ &&
129148
!is_assembler_too_old_avx512 &&
130149
vaes_vpclmulqdq_},
150+
#else // AARCH64
151+
{kFlag_aes_hw_ctr32_encrypt_blocks, aes_hw_ &&
152+
!aes_gcm_pmull_ && !aes_gcm_8x_},
153+
{kFlag_aes_gcm_enc_kernel, aes_hw_ &&
154+
aes_gcm_pmull_ && !aes_gcm_8x_},
155+
{kFlag_aesv8_gcm_8x_enc_128, aes_hw_ &&
156+
aes_gcm_pmull_ && aes_gcm_8x_}
157+
#endif
131158
},
132159
[] {
133160
const uint8_t kZeros[16] = {0};
134-
const uint8_t kPlaintext[40] = {1, 2, 3, 4, 0};
161+
const uint8_t kPlaintext[256] = {1, 2, 3, 4, 0};
135162
uint8_t ciphertext[sizeof(kPlaintext) + 16];
136163
size_t ciphertext_len;
137164
bssl::ScopedEVP_AEAD_CTX ctx;
@@ -148,8 +175,8 @@ TEST_F(ImplDispatchTest, AEAD_AES_GCM) {
148175
TEST_F(ImplDispatchTest, AES_set_encrypt_key) {
149176
AssertFunctionsHit(
150177
{
151-
{kFlag_aes_hw_set_encrypt_key, aesni_},
152-
{kFlag_vpaes_set_encrypt_key, ssse3_ && !aesni_},
178+
{kFlag_aes_hw_set_encrypt_key, aes_hw_},
179+
{kFlag_vpaes_set_encrypt_key, aes_vpaes_ && !aes_hw_},
153180
},
154181
[] {
155182
AES_KEY key;
@@ -165,8 +192,8 @@ TEST_F(ImplDispatchTest, AES_single_block) {
165192

166193
AssertFunctionsHit(
167194
{
168-
{kFlag_aes_hw_encrypt, aesni_},
169-
{kFlag_vpaes_encrypt, ssse3_ && !aesni_},
195+
{kFlag_aes_hw_encrypt, aes_hw_},
196+
{kFlag_vpaes_encrypt, aes_vpaes_ && !aes_hw_},
170197
},
171198
[&key] {
172199
uint8_t in[AES_BLOCK_SIZE] = {0};
@@ -178,7 +205,7 @@ TEST_F(ImplDispatchTest, AES_single_block) {
178205
TEST_F(ImplDispatchTest, SHA256) {
179206
AssertFunctionsHit(
180207
{
181-
{kFlag_sha256_shaext, sha_ext_},
208+
{kFlag_sha256_hw, sha_ext_},
182209
},
183210
[] {
184211
const uint8_t in[32] = {0};
@@ -187,6 +214,20 @@ TEST_F(ImplDispatchTest, SHA256) {
187214
});
188215
}
189216

190-
#endif // X86 || X86_64
217+
#ifdef OPENSSL_AARCH64
218+
TEST_F(ImplDispatchTest, SHA512) {
219+
AssertFunctionsHit(
220+
{
221+
{kFlag_sha512_hw, sha_512_ext_},
222+
},
223+
[] {
224+
const uint8_t in[32] = {0};
225+
uint8_t out[SHA512_DIGEST_LENGTH];
226+
SHA512(in, 32, out);
227+
});
228+
}
229+
#endif // OPENSSL_AARCH64
230+
231+
#endif // !OPENSSL_NO_ASM && (OPENSSL_X86 || OPENSSL_X86_64 || OPENSSL_AARCH64)
191232

192233
#endif // DISPATCH_TEST && !SHARED_LIBRARY

‎crypto/internal.h

+12-1
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,7 @@ OPENSSL_INLINE int boringssl_fips_break_test(const char *test) {
10131013

10141014
// BORINGSSL_function_hit is an array of flags. The following functions will
10151015
// set these flags if BORINGSSL_DISPATCH_TEST is defined.
1016+
// On x86 and x86_64:
10161017
// 0: aes_hw_ctr32_encrypt_blocks
10171018
// 1: aes_hw_encrypt
10181019
// 2: aesni_gcm_encrypt
@@ -1021,7 +1022,17 @@ OPENSSL_INLINE int boringssl_fips_break_test(const char *test) {
10211022
// 5: vpaes_set_encrypt_key
10221023
// 6: sha256_block_data_order_shaext
10231024
// 7: aes_gcm_encrypt_avx512
1024-
extern uint8_t BORINGSSL_function_hit[8];
1025+
// On AARCH64:
1026+
// 0: aes_hw_ctr32_encrypt_blocks
1027+
// 1: aes_hw_encrypt
1028+
// 2: aes_gcm_enc_kernel
1029+
// 3: aes_hw_set_encrypt_key
1030+
// 4: vpaes_encrypt
1031+
// 5: vpaes_set_encrypt_key
1032+
// 6: sha256_block_armv8
1033+
// 7: aesv8_gcm_8x_enc_128
1034+
// 8: sha512_block_armv8
1035+
extern uint8_t BORINGSSL_function_hit[9];
10251036
#endif // BORINGSSL_DISPATCH_TEST
10261037

10271038
#if !defined(AWSLC_FIPS) && !defined(BORINGSSL_SHARED_LIBRARY)

‎generated-src/ios-aarch64/crypto/fipsmodule/aesv8-armx.S

+28
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ Lrcon:
3232
.align 5
3333
_aes_hw_set_encrypt_key:
3434
Lenc_key:
35+
#ifdef BORINGSSL_DISPATCH_TEST
36+
37+
adrp x6,_BORINGSSL_function_hit@PAGE
38+
add x6, x6, _BORINGSSL_function_hit@PAGEOFF
39+
mov w7, #1
40+
strb w7, [x6,#3] // kFlag_aes_hw_set_encrypt_key
41+
#endif
3542
// Armv8.3-A PAuth: even though x30 is pushed to stack it is not popped later.
3643
AARCH64_VALID_CALL_TARGET
3744
stp x29,x30,[sp,#-16]!
@@ -244,6 +251,13 @@ Ldec_key_abort:
244251

245252
.align 5
246253
_aes_hw_encrypt:
254+
#ifdef BORINGSSL_DISPATCH_TEST
255+
256+
adrp x6,_BORINGSSL_function_hit@PAGE
257+
add x6, x6, _BORINGSSL_function_hit@PAGEOFF
258+
mov w7, #1
259+
strb w7, [x6,#1] // kFlag_aes_hw_encrypt
260+
#endif
247261
AARCH64_VALID_CALL_TARGET
248262
ldr w3,[x2,#240]
249263
ld1 {v0.4s},[x2],#16
@@ -275,6 +289,13 @@ Loop_enc:
275289

276290
.align 5
277291
_aes_hw_decrypt:
292+
#ifdef BORINGSSL_DISPATCH_TEST
293+
294+
adrp x6,_BORINGSSL_function_hit@PAGE
295+
add x6, x6, _BORINGSSL_function_hit@PAGEOFF
296+
mov w7, #1
297+
strb w7, [x6,#1] // kFlag_aes_hw_encrypt
298+
#endif
278299
AARCH64_VALID_CALL_TARGET
279300
ldr w3,[x2,#240]
280301
ld1 {v0.4s},[x2],#16
@@ -599,6 +620,13 @@ Lcbc_abort:
599620

600621
.align 5
601622
_aes_hw_ctr32_encrypt_blocks:
623+
#ifdef BORINGSSL_DISPATCH_TEST
624+
625+
adrp x6,_BORINGSSL_function_hit@PAGE
626+
add x6, x6, _BORINGSSL_function_hit@PAGEOFF
627+
mov w7, #1
628+
strb w7, [x6] // kFlag_aes_hw_ctr32_encrypt_blocks
629+
#endif
602630
// Armv8.3-A PAuth: even though x30 is pushed to stack it is not popped later.
603631
AARCH64_VALID_CALL_TARGET
604632
stp x29,x30,[sp,#-16]!

0 commit comments

Comments
 (0)
Please sign in to comment.