Skip to content

Commit 9124900

Browse files
committed
ML-KEM fixes
* fix -Wconversion warnings * allow APIs without RNG usage in case WC_NO_RNG is defined
1 parent 1b8b5ed commit 9124900

File tree

5 files changed

+506
-410
lines changed

5 files changed

+506
-410
lines changed

.github/workflows/wolfCrypt-Wconversion.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ jobs:
2323
'--enable-smallstack --disable-asm --enable-cryptonly --enable-all-crypto --disable-examples --disable-benchmark --disable-crypttests CPPFLAGS="-Wconversion -Warith-conversion -Wenum-conversion -Wfloat-conversion -Wsign-conversion"',
2424
'--enable-smallstack --enable-intelasm --enable-cryptonly --enable-all-crypto --disable-examples --disable-benchmark --disable-crypttests CPPFLAGS="-Wconversion -Warith-conversion -Wenum-conversion -Wfloat-conversion -Wsign-conversion"',
2525
'--enable-cryptonly --enable-all-crypto --disable-examples --disable-benchmark --disable-crypttests CPPFLAGS="-Wconversion -Warith-conversion -Wenum-conversion -Wfloat-conversion -Wsign-conversion -DNO_INT128"',
26-
'--enable-cryptonly --enable-all-crypto --disable-examples --disable-benchmark --disable-crypttests CPPFLAGS="-Wdeclaration-after-statement -Wconversion -Warith-conversion -Wenum-conversion -Wfloat-conversion -Wsign-conversion" --enable-32bit CFLAGS=-m32'
26+
'--enable-cryptonly --enable-all-crypto --disable-examples --disable-benchmark --disable-crypttests CPPFLAGS="-Wdeclaration-after-statement -Wconversion -Warith-conversion -Wenum-conversion -Wfloat-conversion -Wsign-conversion" --enable-32bit CFLAGS=-m32',
27+
'--enable-cryptonly --enable-all-crypto --disable-examples --disable-benchmark --disable-crypttests --enable-mlkem=yes,small CPPFLAGS="-Wconversion -Warith-conversion -Wenum-conversion -Wfloat-conversion -Wsign-conversion -DNO_INT128"',
28+
'--enable-cryptonly --enable-all-crypto --disable-examples --disable-benchmark --disable-crypttests --enable-mlkem=yes,no-large-code CPPFLAGS="-Wconversion -Warith-conversion -Wenum-conversion -Wfloat-conversion -Wsign-conversion -DNO_INT128"',
29+
'--enable-smallstack --enable-cryptonly --enable-all-crypto --disable-examples --disable-benchmark --disable-crypttests --enable-mlkem=yes CPPFLAGS="-Wconversion -Warith-conversion -Wenum-conversion -Wfloat-conversion -Wsign-conversion -DNO_INT128"',
2730
]
2831
name: build library
2932
if: github.repository_owner == 'wolfssl'

configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,9 @@ do
16841684
small)
16851685
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_MLKEM_SMALL"
16861686
;;
1687+
no-large-code)
1688+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_MLKEM_NO_LARGE_CODE"
1689+
;;
16871690
cache-a)
16881691
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_MLKEM_CACHE_A"
16891692
;;

wolfcrypt/src/wc_mlkem.c

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ int wc_MlKemKey_Free(MlKemKey* key)
368368
*/
369369
int wc_MlKemKey_MakeKey(MlKemKey* key, WC_RNG* rng)
370370
{
371+
#ifndef WC_NO_RNG
371372
int ret = 0;
372373
unsigned char rand[WC_ML_KEM_MAKEKEY_RAND_SZ];
373374

@@ -397,6 +398,11 @@ int wc_MlKemKey_MakeKey(MlKemKey* key, WC_RNG* rng)
397398

398399
/* Step 4: return ret != 0 on falsum or internal key generation failure. */
399400
return ret;
401+
#else
402+
(void)key;
403+
(void)rng;
404+
return NOT_COMPILED_IN;
405+
#endif /* WC_NO_RNG */
400406
}
401407

402408
/**
@@ -516,16 +522,17 @@ int wc_MlKemKey_MakeKeyWithRandom(MlKemKey* key, const unsigned char* rand,
516522
#ifndef WOLFSSL_MLKEM_MAKEKEY_SMALL_MEM
517523
#ifndef WOLFSSL_MLKEM_CACHE_A
518524
/* e (v) | a (m) */
519-
e = (sword16*)XMALLOC((k + 1) * k * MLKEM_N * sizeof(sword16),
525+
e = (sword16*)XMALLOC((size_t)(k + 1) * (size_t)k *
526+
(size_t)MLKEM_N * sizeof(sword16),
520527
key->heap, DYNAMIC_TYPE_TMP_BUFFER);
521528
#else
522529
/* e (v) */
523-
e = (sword16*)XMALLOC(k * MLKEM_N * sizeof(sword16),
530+
e = (sword16*)XMALLOC((size_t)k * (size_t)MLKEM_N * sizeof(sword16),
524531
key->heap, DYNAMIC_TYPE_TMP_BUFFER);
525532
#endif
526533
#else
527534
/* e (v) */
528-
e = (sword16*)XMALLOC(k * MLKEM_N * sizeof(sword16),
535+
e = (sword16*)XMALLOC((size_t)k * (size_t)MLKEM_N * sizeof(sword16),
529536
key->heap, DYNAMIC_TYPE_TMP_BUFFER);
530537
#endif
531538
if (e == NULL) {
@@ -557,7 +564,7 @@ int wc_MlKemKey_MakeKeyWithRandom(MlKemKey* key, const unsigned char* rand,
557564
#endif
558565
#ifndef WOLFSSL_NO_ML_KEM
559566
{
560-
buf[0] = k;
567+
buf[0] = (byte)k;
561568
/* Expand 33 bytes of random to 32.
562569
* Alg 13: Step 1: (rho,sigma) <- G(d||k)
563570
*/
@@ -582,7 +589,7 @@ int wc_MlKemKey_MakeKeyWithRandom(MlKemKey* key, const unsigned char* rand,
582589
/* Generate noise using PRF.
583590
* Alg 13: Steps 8-15: generate s and e
584591
*/
585-
ret = mlkem_get_noise(&key->prf, k, s, e, NULL, sigma);
592+
ret = mlkem_get_noise(&key->prf, (int)k, s, e, NULL, sigma);
586593
}
587594
if (ret == 0) {
588595
/* Generate the matrix A.
@@ -819,10 +826,12 @@ static int mlkemkey_encapsulate(MlKemKey* key, const byte* m, byte* r, byte* c)
819826
if (ret == 0) {
820827
/* Allocate dynamic memory for all matrices, vectors and polynomials. */
821828
#ifndef WOLFSSL_MLKEM_ENCAPSULATE_SMALL_MEM
822-
y = (sword16*)XMALLOC(((k + 3) * k + 3) * MLKEM_N * sizeof(sword16),
829+
y = (sword16*)XMALLOC(((size_t)(k + 3) * (size_t)k + 3) *
830+
(size_t)MLKEM_N * sizeof(sword16),
823831
key->heap, DYNAMIC_TYPE_TMP_BUFFER);
824832
#else
825-
y = (sword16*)XMALLOC(3 * k * MLKEM_N * sizeof(sword16), key->heap,
833+
y = (sword16*)XMALLOC((size_t)3 * (size_t)k *
834+
(size_t)MLKEM_N * sizeof(sword16), key->heap,
826835
DYNAMIC_TYPE_TMP_BUFFER);
827836
#endif
828837
if (y == NULL) {
@@ -849,7 +858,7 @@ static int mlkemkey_encapsulate(MlKemKey* key, const byte* m, byte* r, byte* c)
849858
/* Generate noise using PRF.
850859
* Steps 9-17: generate y, e_1, e_2
851860
*/
852-
ret = mlkem_get_noise(&key->prf, k, y, e1, e2, r);
861+
ret = mlkem_get_noise(&key->prf, (int)k, y, e1, e2, r);
853862
}
854863
#ifdef WOLFSSL_MLKEM_CACHE_A
855864
if ((ret == 0) && ((key->flags & MLKEM_FLAG_A_SET) != 0)) {
@@ -870,7 +879,7 @@ static int mlkemkey_encapsulate(MlKemKey* key, const byte* m, byte* r, byte* c)
870879
if (ret == 0) {
871880
/* Generate the transposed matrix.
872881
* Step 4-8: generate matrix A_hat */
873-
ret = mlkem_gen_matrix(&key->prf, a, k, key->pubSeed, 1);
882+
ret = mlkem_gen_matrix(&key->prf, a, (int)k, key->pubSeed, 1);
874883
}
875884
if (ret == 0) {
876885
/* Assign remaining allocated dynamic memory to pointers.
@@ -880,7 +889,7 @@ static int mlkemkey_encapsulate(MlKemKey* key, const byte* m, byte* r, byte* c)
880889

881890
/* Perform encapsulation maths.
882891
* Steps 18-19, 21: calculate u and v */
883-
mlkem_encapsulate(key->pub, u, v, a, y, e1, e2, mu, k);
892+
mlkem_encapsulate(key->pub, u, v, a, y, e1, e2, mu, (int)k);
884893
}
885894
#else /* WOLFSSL_MLKEM_ENCAPSULATE_SMALL_MEM */
886895
if (ret == 0) {
@@ -892,7 +901,7 @@ static int mlkemkey_encapsulate(MlKemKey* key, const byte* m, byte* r, byte* c)
892901
mlkem_prf_init(&key->prf);
893902
/* Generate noise using PRF.
894903
* Steps 9-12: generate y */
895-
ret = mlkem_get_noise(&key->prf, k, y, NULL, NULL, r);
904+
ret = mlkem_get_noise(&key->prf, (int)k, y, NULL, NULL, r);
896905
}
897906
if (ret == 0) {
898907
/* Assign remaining allocated dynamic memory to pointers.
@@ -903,7 +912,7 @@ static int mlkemkey_encapsulate(MlKemKey* key, const byte* m, byte* r, byte* c)
903912
/* Perform encapsulation maths.
904913
* Steps 13-17: generate e_1 and e_2
905914
* Steps 18-19, 21: calculate u and v */
906-
ret = mlkem_encapsulate_seeds(key->pub, &key->prf, u, a, y, k, m,
915+
ret = mlkem_encapsulate_seeds(key->pub, &key->prf, u, a, y, (int)k, m,
907916
key->pubSeed, r);
908917
}
909918
#endif /* WOLFSSL_MLKEM_ENCAPSULATE_SMALL_MEM */
@@ -977,6 +986,7 @@ static int mlkemkey_encapsulate(MlKemKey* key, const byte* m, byte* r, byte* c)
977986
int wc_MlKemKey_Encapsulate(MlKemKey* key, unsigned char* c, unsigned char* k,
978987
WC_RNG* rng)
979988
{
989+
#ifndef WC_NO_RNG
980990
int ret = 0;
981991
unsigned char m[WC_ML_KEM_ENC_RAND_SZ];
982992

@@ -1001,6 +1011,13 @@ int wc_MlKemKey_Encapsulate(MlKemKey* key, unsigned char* c, unsigned char* k,
10011011

10021012
/* Step 3: return ret != 0 on falsum or internal key generation failure. */
10031013
return ret;
1014+
#else
1015+
(void)key;
1016+
(void)c;
1017+
(void)k;
1018+
(void)rng;
1019+
return NOT_COMPILED_IN;
1020+
#endif /* WC_NO_RNG */
10041021
}
10051022

10061023
/**
@@ -1344,7 +1361,7 @@ static MLKEM_NOINLINE int mlkemkey_decapsulate(MlKemKey* key, byte* m,
13441361

13451362
/* Decapsulate the cipher text into polynomial.
13461363
* Step 6: w <- v' - InvNTT(s_hat_trans o NTT(u')) */
1347-
mlkem_decapsulate(key->priv, w, u, v, k);
1364+
mlkem_decapsulate(key->priv, w, u, v, (int)k);
13481365

13491366
/* Convert the polynomial into a array of bytes (message).
13501367
* Step 7: m <- ByteEncode_1(Compress_1(w)) */
@@ -1498,7 +1515,7 @@ int wc_MlKemKey_Decapsulate(MlKemKey* key, unsigned char* ss,
14981515
}
14991516
if (ret == 0) {
15001517
/* Compare generated cipher text with that passed in. */
1501-
fail = mlkem_cmp(ct, cmp, ctSz);
1518+
fail = mlkem_cmp(ct, cmp, (int)ctSz);
15021519

15031520
#if defined(WOLFSSL_MLKEM_KYBER) && !defined(WOLFSSL_NO_ML_KEM)
15041521
if (key->type & MLKEM_KYBER)
@@ -1527,7 +1544,7 @@ int wc_MlKemKey_Decapsulate(MlKemKey* key, unsigned char* ss,
15271544
if (ret == 0) {
15281545
/* Set secret to kr or fake secret on comparison failure. */
15291546
for (i = 0; i < WC_ML_KEM_SYM_SZ; i++) {
1530-
ss[i] = kr[i] ^ ((kr[i] ^ msg[i]) & fail);
1547+
ss[i] = (byte)(kr[i] ^ ((kr[i] ^ msg[i]) & fail));
15311548
}
15321549
}
15331550
}
@@ -1568,7 +1585,7 @@ static void mlkemkey_decode_public(sword16* pub, byte* pubSeed, const byte* p,
15681585

15691586
/* Decode public key that is vector of polynomials.
15701587
* Step 2: t <- ByteDecode_12(ek_PKE[0 : 384k]) */
1571-
mlkem_from_bytes(pub, p, k);
1588+
mlkem_from_bytes(pub, p, (int)k);
15721589
p += k * WC_ML_KEM_POLY_SIZE;
15731590

15741591
/* Read public key seed.
@@ -1684,7 +1701,7 @@ int wc_MlKemKey_DecodePrivateKey(MlKemKey* key, const unsigned char* in,
16841701
/* Decode private key that is vector of polynomials.
16851702
* Alg 18 Step 1: dk_PKE <- dk[0 : 384k]
16861703
* Alg 15 Step 5: s_hat <- ByteDecode_12(dk_PKE) */
1687-
mlkem_from_bytes(key->priv, p, k);
1704+
mlkem_from_bytes(key->priv, p, (int)k);
16881705
p += k * WC_ML_KEM_POLY_SIZE;
16891706

16901707
/* Decode the public key that is after the private key. */
@@ -1793,7 +1810,7 @@ int wc_MlKemKey_DecodePublicKey(MlKemKey* key, const unsigned char* in,
17931810

17941811
if (ret == 0) {
17951812
mlkemkey_decode_public(key->pub, key->pubSeed, p, k);
1796-
ret = mlkem_check_public(key->pub, k);
1813+
ret = mlkem_check_public(key->pub, (int)k);
17971814
}
17981815
if (ret == 0) {
17991816
/* Calculate public hash. */
@@ -2038,7 +2055,7 @@ int wc_MlKemKey_EncodePrivateKey(MlKemKey* key, unsigned char* out, word32 len)
20382055

20392056
if (ret == 0) {
20402057
/* Encode private key that is vector of polynomials. */
2041-
mlkem_to_bytes(p, key->priv, k);
2058+
mlkem_to_bytes(p, key->priv, (int)k);
20422059
p += WC_ML_KEM_POLY_SIZE * k;
20432060

20442061
/* Encode public key. */
@@ -2155,7 +2172,7 @@ int wc_MlKemKey_EncodePublicKey(MlKemKey* key, unsigned char* out, word32 len)
21552172
int i;
21562173

21572174
/* Encode public key polynomial by polynomial. */
2158-
mlkem_to_bytes(p, key->pub, k);
2175+
mlkem_to_bytes(p, key->pub, (int)k);
21592176
p += k * WC_ML_KEM_POLY_SIZE;
21602177

21612178
/* Append public seed. */

0 commit comments

Comments
 (0)