6969
7070#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
7171
72+ #ifdef WC_MLKEM_NO_ASM
73+ #undef USE_INTEL_SPEEDUP
74+ #undef WOLFSSL_ARMASM
75+ #undef WOLFSSL_RISCV_ASM
76+ #endif
77+
7278#include <wolfssl/wolfcrypt/wc_mlkem.h>
7379#include <wolfssl/wolfcrypt/cpuid.h>
7480
@@ -2481,6 +2487,7 @@ static int mlkem_gen_matrix_k3_avx2(sword16* a, byte* seed, int transposed)
24812487 XMEMSET (state + 5 , 0 , sizeof (* state ) * (25 - 5 ));
24822488 state [20 ] = W64LIT (0x8000000000000000 );
24832489 for (i = 0 ; i < GEN_MATRIX_SIZE ; i += SHA3_128_BYTES ) {
2490+ #ifndef WC_SHA3_NO_ASM
24842491 if (IS_INTEL_BMI2 (cpuid_flags )) {
24852492 sha3_block_bmi2 (state );
24862493 }
@@ -2489,13 +2496,16 @@ static int mlkem_gen_matrix_k3_avx2(sword16* a, byte* seed, int transposed)
24892496 sha3_block_avx2 (state );
24902497 RESTORE_VECTOR_REGISTERS ();
24912498 }
2492- else {
2499+ else
2500+ #endif /* !WC_SHA3_NO_ASM */
2501+ {
24932502 BlockSha3 (state );
24942503 }
24952504 XMEMCPY (rand + i , state , SHA3_128_BYTES );
24962505 }
24972506 ctr0 = mlkem_rej_uniform_n_avx2 (a , MLKEM_N , rand , GEN_MATRIX_SIZE );
24982507 while (ctr0 < MLKEM_N ) {
2508+ #ifndef WC_SHA3_NO_ASM
24992509 if (IS_INTEL_BMI2 (cpuid_flags )) {
25002510 sha3_block_bmi2 (state );
25012511 }
@@ -2504,7 +2514,9 @@ static int mlkem_gen_matrix_k3_avx2(sword16* a, byte* seed, int transposed)
25042514 sha3_block_avx2 (state );
25052515 RESTORE_VECTOR_REGISTERS ();
25062516 }
2507- else {
2517+ else
2518+ #endif /* !WC_SHA3_NO_ASM */
2519+ {
25082520 BlockSha3 (state );
25092521 }
25102522 XMEMCPY (rand , state , SHA3_128_BYTES );
@@ -3054,6 +3066,7 @@ static int mlkem_prf(wc_Shake* shake256, byte* out, unsigned int outLen,
30543066 unsigned int len = min (outLen , WC_SHA3_256_BLOCK_SIZE );
30553067
30563068 /* Perform a block operation on the state for next block of output. */
3069+ #ifndef WC_SHA3_NO_ASM
30573070 if (IS_INTEL_BMI2 (cpuid_flags )) {
30583071 sha3_block_bmi2 (state );
30593072 }
@@ -3062,7 +3075,9 @@ static int mlkem_prf(wc_Shake* shake256, byte* out, unsigned int outLen,
30623075 sha3_block_avx2 (state );
30633076 RESTORE_VECTOR_REGISTERS ();
30643077 }
3065- else {
3078+ else
3079+ #endif /* !WC_SHA3_NO_ASM */
3080+ {
30663081 BlockSha3 (state );
30673082 }
30683083
@@ -3109,14 +3124,17 @@ int mlkem_kdf(byte* seed, int seedLen, byte* out, int outLen)
31093124 XMEMSET (state + len64 + 1 , 0 , (25 - len64 - 1 ) * sizeof (word64 ));
31103125 state [WC_SHA3_256_COUNT - 1 ] = W64LIT (0x8000000000000000 );
31113126
3127+ #ifndef WC_SHA3_NO_ASM
31123128 if (IS_INTEL_BMI2 (cpuid_flags )) {
31133129 sha3_block_bmi2 (state );
31143130 }
31153131 else if (IS_INTEL_AVX2 (cpuid_flags ) && (SAVE_VECTOR_REGISTERS2 () == 0 )) {
31163132 sha3_block_avx2 (state );
31173133 RESTORE_VECTOR_REGISTERS ();
31183134 }
3119- else {
3135+ else
3136+ #endif
3137+ {
31203138 BlockSha3 (state );
31213139 }
31223140 XMEMCPY (out , state , outLen );
@@ -4121,14 +4139,17 @@ static int mlkem_get_noise_eta2_avx2(MLKEM_PRF_T* prf, sword16* p,
41214139 state [WC_SHA3_256_COUNT - 1 ] = W64LIT (0x8000000000000000 );
41224140
41234141 /* Perform a block operation on the state for next block of output. */
4142+ #ifndef WC_SHA3_NO_ASM
41244143 if (IS_INTEL_BMI2 (cpuid_flags )) {
41254144 sha3_block_bmi2 (state );
41264145 }
41274146 else if (IS_INTEL_AVX2 (cpuid_flags ) && (SAVE_VECTOR_REGISTERS2 () == 0 )) {
41284147 sha3_block_avx2 (state );
41294148 RESTORE_VECTOR_REGISTERS ();
41304149 }
4131- else {
4150+ else
4151+ #endif /* !WC_SHA3_NO_ASM */
4152+ {
41324153 BlockSha3 (state );
41334154 }
41344155 mlkem_cbd_eta2_avx2 (p , (byte * )state );
0 commit comments