Skip to content

Commit 1b3b661

Browse files
committed
AES GCM trampoline functions to fix delocator
1 parent be91c0f commit 1b3b661

File tree

3 files changed

+64
-25
lines changed

3 files changed

+64
-25
lines changed

crypto/fipsmodule/aes/mode_wrappers.c

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,33 @@
5858
// function pointer calculation in AES_ctr128_encrypt. Without it,
5959
// on AArch64 there is risk of the calculations requiring a PC-relative
6060
// offset outside of the range (-1MB,1MB) addressable using `ADR`.
61+
static inline void aes_hw_encrypt_wrapper(const uint8_t *in, uint8_t *out,
62+
const AES_KEY *key) {
63+
aes_hw_encrypt(in, out, key);
64+
}
65+
66+
static inline void vpaes_encrypt_wrapper(const uint8_t *in, uint8_t *out,
67+
const AES_KEY *key) {
68+
vpaes_encrypt(in, out, key);
69+
}
70+
71+
static inline void aes_nohw_encrypt_wrapper(const uint8_t *in, uint8_t *out,
72+
const AES_KEY *key) {
73+
aes_nohw_encrypt(in, out, key);
74+
}
75+
6176
static inline void aes_hw_ctr32_encrypt_blocks_wrapper(const uint8_t *in,
62-
uint8_t *out, size_t len,
63-
const AES_KEY *key,
64-
const uint8_t ivec[16])
65-
{
66-
aes_hw_ctr32_encrypt_blocks(in, out, len, key, ivec);
77+
uint8_t *out, size_t len,
78+
const AES_KEY *key,
79+
const uint8_t ivec[16]) {
80+
aes_hw_ctr32_encrypt_blocks(in, out, len, key, ivec);
81+
}
82+
83+
static inline void aes_nohw_ctr32_encrypt_blocks_wrapper(const uint8_t *in,
84+
uint8_t *out, size_t len,
85+
const AES_KEY *key,
86+
const uint8_t ivec[16]) {
87+
aes_nohw_ctr32_encrypt_blocks(in, out, len, key, ivec);
6788
}
6889

6990
#if defined(VPAES_CTR32)
@@ -73,12 +94,7 @@ static inline void vpaes_ctr32_encrypt_blocks_wrapper(const uint8_t *in,
7394
const uint8_t ivec[16]) {
7495
vpaes_ctr32_encrypt_blocks(in, out, len, key, ivec);
7596
}
76-
#else // VPAES_CTR32
77-
static inline void vpaes_encrypt_wrapper(const uint8_t *in, uint8_t *out,
78-
const AES_KEY *key) {
79-
vpaes_encrypt(in, out, key);
80-
}
81-
#endif // !VPAES_CTR32
97+
#endif
8298

8399
void AES_ctr128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
84100
const AES_KEY *key, uint8_t ivec[AES_BLOCK_SIZE],
@@ -98,7 +114,7 @@ void AES_ctr128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
98114
#endif
99115
} else {
100116
CRYPTO_ctr128_encrypt_ctr32(in, out, len, key, ivec, ecount_buf, num,
101-
aes_nohw_ctr32_encrypt_blocks);
117+
aes_nohw_ctr32_encrypt_blocks_wrapper);
102118
}
103119

104120
FIPS_service_indicator_update_state();

crypto/fipsmodule/cipher/e_aes.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,40 +304,40 @@ ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_KEY *gcm_key,
304304
if (hwaes_capable()) {
305305
aes_hw_set_encrypt_key(key, (int)key_bytes * 8, aes_key);
306306
if (gcm_key != NULL) {
307-
CRYPTO_gcm128_init_key(gcm_key, aes_key, aes_hw_encrypt, 1);
307+
CRYPTO_gcm128_init_key(gcm_key, aes_key, aes_hw_encrypt_wrapper, 1);
308308
}
309309
if (out_block) {
310-
*out_block = aes_hw_encrypt;
310+
*out_block = aes_hw_encrypt_wrapper;
311311
}
312-
return aes_hw_ctr32_encrypt_blocks;
312+
return aes_hw_ctr32_encrypt_blocks_wrapper;
313313
}
314314

315315
if (vpaes_capable()) {
316316
vpaes_set_encrypt_key(key, (int)key_bytes * 8, aes_key);
317317
if (out_block) {
318-
*out_block = vpaes_encrypt;
318+
*out_block = vpaes_encrypt_wrapper;
319319
}
320320
if (gcm_key != NULL) {
321-
CRYPTO_gcm128_init_key(gcm_key, aes_key, vpaes_encrypt, 0);
321+
CRYPTO_gcm128_init_key(gcm_key, aes_key, vpaes_encrypt_wrapper, 0);
322322
}
323323
#if defined(BSAES)
324324
assert(bsaes_capable());
325325
return vpaes_ctr32_encrypt_blocks_with_bsaes;
326326
#elif defined(VPAES_CTR32)
327-
return vpaes_ctr32_encrypt_blocks;
327+
return vpaes_ctr32_encrypt_blocks_wrapper;
328328
#else
329329
return NULL;
330330
#endif
331331
}
332332

333333
aes_nohw_set_encrypt_key(key, (int)key_bytes * 8, aes_key);
334334
if (gcm_key != NULL) {
335-
CRYPTO_gcm128_init_key(gcm_key, aes_key, aes_nohw_encrypt, 0);
335+
CRYPTO_gcm128_init_key(gcm_key, aes_key, aes_nohw_encrypt_wrapper, 0);
336336
}
337337
if (out_block) {
338-
*out_block = aes_nohw_encrypt;
338+
*out_block = aes_nohw_encrypt_wrapper;
339339
}
340-
return aes_nohw_ctr32_encrypt_blocks;
340+
return aes_nohw_ctr32_encrypt_blocks_wrapper;
341341
}
342342

343343
#if defined(OPENSSL_32_BIT)

crypto/fipsmodule/modes/gcm.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,29 @@ static size_t hw_gcm_decrypt(const uint8_t *in, uint8_t *out, size_t len,
224224

225225
#endif // HW_GCM && AARCH64
226226

227+
// Trampolines for GCM function pointers to avoid delocator issues with adr
228+
// on AArch64. Without these wrappers, the function pointer calculations
229+
// may require PC-relative offsets outside the addressable range.
230+
#if defined(GHASH_ASM_ARM)
231+
static inline void gcm_gmult_v8_wrapper(uint8_t Xi[16], const u128 Htable[16]) {
232+
gcm_gmult_v8(Xi, Htable);
233+
}
234+
235+
static inline void gcm_ghash_v8_wrapper(uint8_t Xi[16], const u128 Htable[16],
236+
const uint8_t *inp, size_t len) {
237+
gcm_ghash_v8(Xi, Htable, inp, len);
238+
}
239+
240+
static inline void gcm_gmult_neon_wrapper(uint8_t Xi[16], const u128 Htable[16]) {
241+
gcm_gmult_neon(Xi, Htable);
242+
}
243+
244+
static inline void gcm_ghash_neon_wrapper(uint8_t Xi[16], const u128 Htable[16],
245+
const uint8_t *inp, size_t len) {
246+
gcm_ghash_neon(Xi, Htable, inp, len);
247+
}
248+
#endif
249+
227250
void CRYPTO_ghash_init(gmult_func *out_mult, ghash_func *out_hash,
228251
u128 out_table[16], int *out_is_avx,
229252
const uint8_t gcm_key[16]) {
@@ -278,15 +301,15 @@ void CRYPTO_ghash_init(gmult_func *out_mult, ghash_func *out_hash,
278301
#elif defined(GHASH_ASM_ARM)
279302
if (gcm_pmull_capable()) {
280303
gcm_init_v8(out_table, H);
281-
*out_mult = gcm_gmult_v8;
282-
*out_hash = gcm_ghash_v8;
304+
*out_mult = gcm_gmult_v8_wrapper;
305+
*out_hash = gcm_ghash_v8_wrapper;
283306
return;
284307
}
285308

286309
if (gcm_neon_capable()) {
287310
gcm_init_neon(out_table, H);
288-
*out_mult = gcm_gmult_neon;
289-
*out_hash = gcm_ghash_neon;
311+
*out_mult = gcm_gmult_neon_wrapper;
312+
*out_hash = gcm_ghash_neon_wrapper;
290313
return;
291314
}
292315
#elif defined(GHASH_ASM_PPC64LE)

0 commit comments

Comments
 (0)