Skip to content

Commit b6f9851

Browse files
committed
Add runtime dispatch (FIPS202)
Signed-off-by: willieyz <willie.zhao@chelpis.com>
1 parent ad639ab commit b6f9851

File tree

18 files changed

+739
-22
lines changed

18 files changed

+739
-22
lines changed

.github/actions/config-variations/action.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ inputs:
77
description: 'GitHub token'
88
required: true
99
tests:
10-
description: 'List of tests to run (space-separated IDs) or "all" for all tests. Available IDs: pct-enabled, pct-enabled-broken, custom-zeroize, native-cap-ON, native-cap-OFF, native-cap-CPUID_AVX2, no-asm, serial-fips202, custom-randombytes, custom-memcpy, custom-memset, custom-stdlib'
10+
description: 'List of tests to run (space-separated IDs) or "all" for all tests. Available IDs: pct-enabled, pct-enabled-broken, custom-zeroize, native-cap-ON, native-cap-OFF, native-cap-ID_AA64PFR1_EL1, native-cap-CPUID_AVX2, no-asm, serial-fips202, custom-randombytes, custom-memcpy, custom-memset, custom-stdlib'
1111
required: false
1212
default: 'all'
1313
opt:
@@ -85,6 +85,19 @@ runs:
8585
acvp: true
8686
opt: ${{ inputs.opt }}
8787
examples: false # Some examples use a custom config themselves
88+
- name: "Custom native capability functions (ID_AA64PFR1_EL1 detection)"
89+
if: ${{ (inputs.tests == 'all' || contains(inputs.tests, 'native-cap-ID_AA64PFR1_EL1')) && runner.os == 'Linux' && runner.arch == 'ARM64' }}
90+
uses: ./.github/actions/multi-functest
91+
with:
92+
gh_token: ${{ inputs.gh_token }}
93+
compile_mode: native
94+
cflags: "-std=c11 -march=armv8.4-a+sha3 -D_GNU_SOURCE -DMLD_CONFIG_FILE=\\\\\\\"../../test/custom_native_capability_config_ID_AA64PFR1_EL1.h\\\\\\\" -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all"
95+
ldflags: "-fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all"
96+
func: true
97+
kat: true
98+
acvp: true
99+
opt: ${{ inputs.opt }}
100+
examples: false # Some examples use a custom config themselves
88101
- name: "Custom native capability functions (CPUID AVX2 detection)"
89102
if: ${{ (inputs.tests == 'all' || contains(inputs.tests, 'native-cap-CPUID_AVX2')) && runner.os == 'Linux' && runner.arch == 'X64' }}
90103
uses: ./.github/actions/multi-functest

BIBLIOGRAPHY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ source code and documentation.
4343
- [test/custom_native_capability_config_0.h](test/custom_native_capability_config_0.h)
4444
- [test/custom_native_capability_config_1.h](test/custom_native_capability_config_1.h)
4545
- [test/custom_native_capability_config_CPUID_AVX2.h](test/custom_native_capability_config_CPUID_AVX2.h)
46+
- [test/custom_native_capability_config_ID_AA64PFR1_EL1.h](test/custom_native_capability_config_ID_AA64PFR1_EL1.h)
4647
- [test/custom_randombytes_config.h](test/custom_randombytes_config.h)
4748
- [test/custom_stdlib_config.h](test/custom_stdlib_config.h)
4849
- [test/custom_zeroize_config.h](test/custom_zeroize_config.h)
@@ -94,6 +95,7 @@ source code and documentation.
9495
- [test/custom_native_capability_config_0.h](test/custom_native_capability_config_0.h)
9596
- [test/custom_native_capability_config_1.h](test/custom_native_capability_config_1.h)
9697
- [test/custom_native_capability_config_CPUID_AVX2.h](test/custom_native_capability_config_CPUID_AVX2.h)
98+
- [test/custom_native_capability_config_ID_AA64PFR1_EL1.h](test/custom_native_capability_config_ID_AA64PFR1_EL1.h)
9799
- [test/custom_randombytes_config.h](test/custom_randombytes_config.h)
98100
- [test/custom_stdlib_config.h](test/custom_stdlib_config.h)
99101
- [test/custom_zeroize_config.h](test/custom_zeroize_config.h)

dev/fips202/aarch64/x1_scalar.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
#define MLD_FIPS202_AARCH64_NEED_X1_SCALAR
1414

1515
#if !defined(__ASSEMBLER__)
16+
#include "../api.h"
1617
#include "src/fips202_native_aarch64.h"
17-
static MLD_INLINE void mld_keccak_f1600_x1_native(uint64_t *state)
18+
static MLD_INLINE int mld_keccak_f1600_x1_native(uint64_t *state)
1819
{
1920
mld_keccak_f1600_x1_scalar_asm(state, mld_keccakf1600_round_constants);
21+
return MLD_NATIVE_FUNC_SUCCESS;
2022
}
2123
#endif /* !__ASSEMBLER__ */
2224

dev/fips202/aarch64/x1_v84a.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@
1717
#define MLD_FIPS202_AARCH64_NEED_X1_V84A
1818

1919
#if !defined(__ASSEMBLER__)
20+
#include "../api.h"
2021
#include "src/fips202_native_aarch64.h"
21-
static MLD_INLINE void mld_keccak_f1600_x1_native(uint64_t *state)
22+
static MLD_INLINE int mld_keccak_f1600_x1_native(uint64_t *state)
2223
{
24+
if (!mld_sys_check_capability(MLD_SYS_CAP_SHA3))
25+
{
26+
return MLD_NATIVE_FUNC_FALLBACK;
27+
}
28+
2329
mld_keccak_f1600_x1_v84a_asm(state, mld_keccakf1600_round_constants);
30+
return MLD_NATIVE_FUNC_SUCCESS;
2431
}
2532
#endif /* !__ASSEMBLER__ */
2633

dev/fips202/aarch64/x2_v84a.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@
1717
#define MLD_FIPS202_AARCH64_NEED_X2_V84A
1818

1919
#if !defined(__ASSEMBLER__)
20+
#include "../api.h"
2021
#include "src/fips202_native_aarch64.h"
2122

2223

23-
static MLD_INLINE void mld_keccak_f1600_x4_native(uint64_t *state)
24+
static MLD_INLINE int mld_keccak_f1600_x4_native(uint64_t *state)
2425
{
26+
if (!mld_sys_check_capability(MLD_SYS_CAP_SHA3))
27+
{
28+
return MLD_NATIVE_FUNC_FALLBACK;
29+
}
30+
2531
mld_keccak_f1600_x2_v84a_asm(state + 0 * 25, mld_keccakf1600_round_constants);
2632
mld_keccak_f1600_x2_v84a_asm(state + 2 * 25, mld_keccakf1600_round_constants);
33+
return MLD_NATIVE_FUNC_SUCCESS;
2734
}
2835
#endif /* !__ASSEMBLER__ */
2936

dev/fips202/aarch64/x4_v8a_scalar.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
#define MLD_FIPS202_AARCH64_NEED_X4_V8A_SCALAR_HYBRID
1414

1515
#if !defined(__ASSEMBLER__)
16+
#include "../api.h"
1617
#include "src/fips202_native_aarch64.h"
17-
static MLD_INLINE void mld_keccak_f1600_x4_native(uint64_t *state)
18+
static MLD_INLINE int mld_keccak_f1600_x4_native(uint64_t *state)
1819
{
1920
mld_keccak_f1600_x4_scalar_v8a_hybrid_asm(state,
2021
mld_keccakf1600_round_constants);
22+
return MLD_NATIVE_FUNC_SUCCESS;
2123
}
2224
#endif /* !__ASSEMBLER__ */
2325

dev/fips202/aarch64/x4_v8a_v84a_scalar.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@
1717
#define MLD_FIPS202_AARCH64_NEED_X4_V8A_V84A_SCALAR_HYBRID
1818

1919
#if !defined(__ASSEMBLER__)
20+
#include "../api.h"
2021
#include "src/fips202_native_aarch64.h"
21-
static MLD_INLINE void mld_keccak_f1600_x4_native(uint64_t *state)
22+
static MLD_INLINE int mld_keccak_f1600_x4_native(uint64_t *state)
2223
{
24+
if (!mld_sys_check_capability(MLD_SYS_CAP_SHA3))
25+
{
26+
return MLD_NATIVE_FUNC_FALLBACK;
27+
}
28+
2329
mld_keccak_f1600_x4_scalar_v8a_v84a_hybrid_asm(
2430
state, mld_keccakf1600_round_constants);
31+
return MLD_NATIVE_FUNC_SUCCESS;
2532
}
2633
#endif /* !__ASSEMBLER__ */
2734

mldsa/mldsa_native.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,8 @@
481481
#if defined(MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202)
482482
/* mldsa/src/fips202/native/api.h */
483483
#undef MLD_FIPS202_NATIVE_API_H
484+
#undef MLD_NATIVE_FUNC_FALLBACK
485+
#undef MLD_NATIVE_FUNC_SUCCESS
484486
/* mldsa/src/fips202/native/auto.h */
485487
#undef MLD_FIPS202_NATIVE_AUTO_H
486488
#if defined(MLD_SYS_AARCH64)

mldsa/src/fips202/keccakf1600.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,17 @@ void mld_keccakf1600x4_xor_bytes(uint64_t *state, const unsigned char *data0,
113113
void mld_keccakf1600x4_permute(uint64_t *state)
114114
{
115115
#if defined(MLD_USE_FIPS202_X4_NATIVE)
116-
mld_keccak_f1600_x4_native(state);
117-
#else
116+
if (mld_keccak_f1600_x4_native(state) == MLD_NATIVE_FUNC_SUCCESS)
117+
{
118+
return;
119+
}
120+
#endif /* MLD_USE_FIPS202_X4_NATIVE */
118121
mld_keccakf1600_permute(state + MLD_KECCAK_LANES * 0);
119122
mld_keccakf1600_permute(state + MLD_KECCAK_LANES * 1);
120123
mld_keccakf1600_permute(state + MLD_KECCAK_LANES * 2);
121124
mld_keccakf1600_permute(state + MLD_KECCAK_LANES * 3);
122-
#endif /* !MLD_USE_FIPS202_X4_NATIVE */
123125
}
124126

125-
#if !defined(MLD_USE_FIPS202_X1_NATIVE)
126127
static const uint64_t mld_KeccakF_RoundConstants[MLD_KECCAK_NROUNDS] = {
127128
(uint64_t)0x0000000000000001ULL, (uint64_t)0x0000000000008082ULL,
128129
(uint64_t)0x800000000000808aULL, (uint64_t)0x8000000080008000ULL,
@@ -137,7 +138,7 @@ static const uint64_t mld_KeccakF_RoundConstants[MLD_KECCAK_NROUNDS] = {
137138
(uint64_t)0x8000000080008081ULL, (uint64_t)0x8000000000008080ULL,
138139
(uint64_t)0x0000000080000001ULL, (uint64_t)0x8000000080008008ULL};
139140

140-
void mld_keccakf1600_permute(uint64_t *state)
141+
static void mld_keccakf1600_permute_c(uint64_t *state)
141142
{
142143
unsigned round;
143144

@@ -402,12 +403,17 @@ void mld_keccakf1600_permute(uint64_t *state)
402403
state[23] = Aso;
403404
state[24] = Asu;
404405
}
405-
#else /* !MLD_USE_FIPS202_X1_NATIVE */
406+
406407
void mld_keccakf1600_permute(uint64_t *state)
407408
{
408-
mld_keccak_f1600_x1_native(state);
409-
}
409+
#if defined(MLD_USE_FIPS202_X1_NATIVE)
410+
if (mld_keccak_f1600_x1_native(state) == MLD_NATIVE_FUNC_SUCCESS)
411+
{
412+
return;
413+
}
410414
#endif /* MLD_USE_FIPS202_X1_NATIVE */
415+
mld_keccakf1600_permute_c(state);
416+
}
411417

412418
#else /* !MLD_CONFIG_MULTILEVEL_NO_SHARED */
413419

mldsa/src/fips202/native/aarch64/x1_scalar.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
#define MLD_FIPS202_AARCH64_NEED_X1_SCALAR
1414

1515
#if !defined(__ASSEMBLER__)
16+
#include "../api.h"
1617
#include "src/fips202_native_aarch64.h"
17-
static MLD_INLINE void mld_keccak_f1600_x1_native(uint64_t *state)
18+
static MLD_INLINE int mld_keccak_f1600_x1_native(uint64_t *state)
1819
{
1920
mld_keccak_f1600_x1_scalar_asm(state, mld_keccakf1600_round_constants);
21+
return MLD_NATIVE_FUNC_SUCCESS;
2022
}
2123
#endif /* !__ASSEMBLER__ */
2224

0 commit comments

Comments
 (0)