6767
6868#ifdef WOLFSSL_WC_KYBER
6969
70+ #ifdef NO_INLINE
71+ #include <wolfssl/wolfcrypt/misc.h>
72+ #else
73+ #define WOLFSSL_MISC_INCLUDED
74+ #include <wolfcrypt/src/misc.c>
75+ #endif
76+
7077/* Declared in wc_kyber.c to stop compiler optimizer from simplifying. */
7178extern volatile sword16 kyber_opt_blocker ;
7279
@@ -1560,14 +1567,11 @@ static int kyber_gen_matrix_k3_avx2(sword16* a, byte* seed, int transposed)
15601567 a += 4 * KYBER_N ;
15611568 }
15621569
1563- state [0 ] = ((word64 * )seed )[0 ];
1564- state [1 ] = ((word64 * )seed )[1 ];
1565- state [2 ] = ((word64 * )seed )[2 ];
1566- state [3 ] = ((word64 * )seed )[3 ];
1570+ readUnalignedWords64 (state , seed , 4 );
15671571 /* Transposed value same as not. */
15681572 state [4 ] = 0x1f0000 + (2 << 8 ) + 2 ;
15691573 XMEMSET (state + 5 , 0 , sizeof (* state ) * (25 - 5 ));
1570- state [20 ] = 0x8000000000000000UL ;
1574+ state [20 ] = W64LIT ( 0x8000000000000000 ) ;
15711575 for (i = 0 ; i < GEN_MATRIX_SIZE ; i += SHA3_128_BYTES ) {
15721576 if (IS_INTEL_BMI2 (cpuid_flags )) {
15731577 sha3_block_bmi2 (state );
@@ -1748,14 +1752,11 @@ static int kyber_gen_matrix_k2_aarch64(sword16* a, byte* seed, int transposed)
17481752
17491753 a += 3 * KYBER_N ;
17501754
1751- state [0 ] = ((word64 * )seed )[0 ];
1752- state [1 ] = ((word64 * )seed )[1 ];
1753- state [2 ] = ((word64 * )seed )[2 ];
1754- state [3 ] = ((word64 * )seed )[3 ];
1755+ readUnalignedWords64 (state , seed , 4 );
17551756 /* Transposed value same as not. */
17561757 state [4 ] = 0x1f0000 + (1 << 8 ) + 1 ;
17571758 XMEMSET (state + 5 , 0 , sizeof (* state ) * (25 - 5 ));
1758- state [20 ] = 0x8000000000000000UL ;
1759+ state [20 ] = W64LIT ( 0x8000000000000000 ) ;
17591760 BlockSha3 (state );
17601761 p = (byte * )state ;
17611762 ctr0 = kyber_rej_uniform_neon (a , KYBER_N , p , XOF_BLOCK_SIZE );
@@ -1899,14 +1900,11 @@ static int kyber_gen_matrix_k4_aarch64(sword16* a, byte* seed, int transposed)
18991900 a += 3 * KYBER_N ;
19001901 }
19011902
1902- state [0 ] = ((word64 * )seed )[0 ];
1903- state [1 ] = ((word64 * )seed )[1 ];
1904- state [2 ] = ((word64 * )seed )[2 ];
1905- state [3 ] = ((word64 * )seed )[3 ];
1903+ readUnalignedWords64 (state , seed , 4 );
19061904 /* Transposed value same as not. */
19071905 state [4 ] = 0x1f0000 + (3 << 8 ) + 3 ;
19081906 XMEMSET (state + 5 , 0 , sizeof (* state ) * (25 - 5 ));
1909- state [20 ] = 0x8000000000000000UL ;
1907+ state [20 ] = W64LIT ( 0x8000000000000000 ) ;
19101908 BlockSha3 (state );
19111909 p = (byte * )state ;
19121910 ctr0 = kyber_rej_uniform_neon (a , KYBER_N , p , XOF_BLOCK_SIZE );
@@ -2047,18 +2045,15 @@ static int kyber_prf(wc_Shake* shake256, byte* out, unsigned int outLen,
20472045 const byte * key )
20482046{
20492047#ifdef USE_INTEL_SPEEDUP
2050- int i ;
20512048 word64 state [25 ];
20522049
20532050 (void )shake256 ;
20542051
2055- for (i = 0 ; i < KYBER_SYM_SZ / 8 ; i ++ ) {
2056- state [i ] = ((word64 * )key )[i ];
2057- }
2052+ readUnalignedWords64 (state , key , KYBER_SYM_SZ / sizeof (word64 ));
20582053 state [KYBER_SYM_SZ / 8 ] = 0x1f00 | key [KYBER_SYM_SZ ];
20592054 XMEMSET (state + KYBER_SYM_SZ / 8 + 1 , 0 ,
20602055 (25 - KYBER_SYM_SZ / 8 - 1 ) * sizeof (word64 ));
2061- state [WC_SHA3_256_COUNT - 1 ] = 0x8000000000000000UL ;
2056+ state [WC_SHA3_256_COUNT - 1 ] = W64LIT ( 0x8000000000000000 ) ;
20622057
20632058 if (IS_INTEL_BMI2 (cpuid_flags )) {
20642059 sha3_block_bmi2 (state );
@@ -2098,15 +2093,12 @@ static int kyber_prf(wc_Shake* shake256, byte* out, unsigned int outLen,
20982093int kyber_kdf (byte * seed , int seedLen , byte * out , int outLen )
20992094{
21002095 word64 state [25 ];
2101- int i ;
2102- int len64 = seedLen / 8 ;
2096+ word32 len64 = seedLen / 8 ;
21032097
2104- for (i = 0 ; i < len64 ; i ++ ) {
2105- state [i ] = ((word64 * )seed )[i ];
2106- }
2098+ readUnalignedWords64 (state , seed , len64 );
21072099 state [len64 ] = 0x1f ;
21082100 XMEMSET (state + len64 + 1 , 0 , (25 - len64 - 1 ) * sizeof (word64 ));
2109- state [WC_SHA3_256_COUNT - 1 ] = 0x8000000000000000UL ;
2101+ state [WC_SHA3_256_COUNT - 1 ] = W64LIT ( 0x8000000000000000 ) ;
21102102
21112103 if (IS_INTEL_BMI2 (cpuid_flags )) {
21122104 sha3_block_bmi2 (state );
@@ -2136,15 +2128,12 @@ int kyber_kdf(byte* seed, int seedLen, byte* out, int outLen)
21362128int kyber_kdf (byte * seed , int seedLen , byte * out , int outLen )
21372129{
21382130 word64 state [25 ];
2139- int i ;
2140- int len64 = seedLen / 8 ;
2131+ word32 len64 = seedLen / 8 ;
21412132
2142- for (i = 0 ; i < len64 ; i ++ ) {
2143- state [i ] = ((word64 * )seed )[i ];
2144- }
2133+ readUnalignedWords64 (state , seed , len64 );
21452134 state [len64 ] = 0x1f ;
21462135 XMEMSET (state + len64 + 1 , 0 , (25 - len64 - 1 ) * sizeof (word64 ));
2147- state [WC_SHA3_256_COUNT - 1 ] = 0x8000000000000000UL ;
2136+ state [WC_SHA3_256_COUNT - 1 ] = W64LIT ( 0x8000000000000000 ) ;
21482137
21492138 BlockSha3 (state );
21502139 XMEMCPY (out , state , outLen );
@@ -2199,10 +2188,11 @@ static unsigned int kyber_rej_uniform_c(sword16* p, unsigned int len,
21992188 i = 0 ;
22002189 for (j = 0 ; j < minJ ; j += 6 ) {
22012190 /* Use 48 bits (6 bytes) as four 12-bit integers. */
2202- sword16 v0 = (* (word64 * )r ) & 0xfff ;
2203- sword16 v1 = ((* (word64 * )r ) >> 12 ) & 0xfff ;
2204- sword16 v2 = ((* (word64 * )r ) >> 24 ) & 0xfff ;
2205- sword16 v3 = ((* (word64 * )r ) >> 36 ) & 0xfff ;
2191+ word64 r_word = readUnalignedWord64 (r );
2192+ sword16 v0 = r_word & 0xfff ;
2193+ sword16 v1 = (r_word >> 12 ) & 0xfff ;
2194+ sword16 v2 = (r_word >> 24 ) & 0xfff ;
2195+ sword16 v3 = (r_word >> 36 ) & 0xfff ;
22062196
22072197 p [i ] = v0 & (0 - (v0 < KYBER_Q ));
22082198 i += v0 < KYBER_Q ;
@@ -2219,10 +2209,11 @@ static unsigned int kyber_rej_uniform_c(sword16* p, unsigned int len,
22192209 if (j < rLen ) {
22202210 for (; (i + 4 < len ) && (j < rLen ); j += 6 ) {
22212211 /* Use 48 bits (6 bytes) as four 12-bit integers. */
2222- sword16 v0 = (* (word64 * )r ) & 0xfff ;
2223- sword16 v1 = ((* (word64 * )r ) >> 12 ) & 0xfff ;
2224- sword16 v2 = ((* (word64 * )r ) >> 24 ) & 0xfff ;
2225- sword16 v3 = ((* (word64 * )r ) >> 36 ) & 0xfff ;
2212+ word64 r_word = readUnalignedWord64 (r );
2213+ sword16 v0 = r_word & 0xfff ;
2214+ sword16 v1 = (r_word >> 12 ) & 0xfff ;
2215+ sword16 v2 = (r_word >> 24 ) & 0xfff ;
2216+ sword16 v3 = (r_word >> 36 ) & 0xfff ;
22262217
22272218 p [i ] = v0 ;
22282219 i += v0 < KYBER_Q ;
@@ -2238,10 +2229,11 @@ static unsigned int kyber_rej_uniform_c(sword16* p, unsigned int len,
22382229 }
22392230 for (; (i < len ) && (j < rLen ); j += 6 ) {
22402231 /* Use 48 bits (6 bytes) as four 12-bit integers. */
2241- sword16 v0 = (* (word64 * )r ) & 0xfff ;
2242- sword16 v1 = ((* (word64 * )r ) >> 12 ) & 0xfff ;
2243- sword16 v2 = ((* (word64 * )r ) >> 24 ) & 0xfff ;
2244- sword16 v3 = ((* (word64 * )r ) >> 36 ) & 0xfff ;
2232+ word64 r_word = readUnalignedWord64 (r );
2233+ sword16 v0 = r_word & 0xfff ;
2234+ sword16 v1 = (r_word >> 12 ) & 0xfff ;
2235+ sword16 v2 = (r_word >> 24 ) & 0xfff ;
2236+ sword16 v3 = (r_word >> 36 ) & 0xfff ;
22452237
22462238 /* Reject first 12-bit integer if greater than or equal to q. */
22472239 if (v0 < KYBER_Q ) {
@@ -2511,9 +2503,9 @@ static void kyber_cbd_eta2(sword16* p, const byte* r)
25112503 #endif
25122504 /* Take the next 8 bytes, little endian, as a 64 bit value. */
25132505 #ifdef BIG_ENDIAN_ORDER
2514- word64 t = ByteReverseWord64 (* ( word64 * ) r );
2506+ word64 t = ByteReverseWord64 (readUnalignedWord64 ( r ) );
25152507 #else
2516- word64 t = * ( word64 * ) r ;
2508+ word64 t = readUnalignedWord64 ( r ) ;
25172509 #endif
25182510 word64 d ;
25192511 /* Add second bits to first. */
@@ -3023,7 +3015,7 @@ static void kyber_get_noise_eta3_aarch64(byte* rand, byte* seed, byte o)
30233015 state [3 ] = ((word64 * )seed )[3 ];
30243016 state [4 ] = 0x1f00 + o ;
30253017 XMEMSET (state + 5 , 0 , sizeof (* state ) * (25 - 5 ));
3026- state [16 ] = 0x8000000000000000UL ;
3018+ state [16 ] = W64LIT ( 0x8000000000000000 ) ;
30273019 BlockSha3 (state );
30283020 XMEMCPY (rand , state , SHA3_256_BYTES );
30293021 BlockSha3 (state );
@@ -3083,7 +3075,7 @@ static void kyber_get_noise_eta2_aarch64(byte* rand, byte* seed, byte o)
30833075 /* Transposed value same as not. */
30843076 state [4 ] = 0x1f00 + o ;
30853077 XMEMSET (state + 5 , 0 , sizeof (* state ) * (25 - 5 ));
3086- state [16 ] = 0x8000000000000000UL ;
3078+ state [16 ] = W64LIT ( 0x8000000000000000 ) ;
30873079 BlockSha3 (state );
30883080}
30893081
0 commit comments