Skip to content

Commit 2e24d9e

Browse files
committed
API: add failure mode support for randombytes()
Change randombytes() to return int (0 on success, non-zero on failure) instead of void, allowing callers to detect and handle RNG failures. Updated function signature, all call sites to check return values and test files to use CHECK macro. Signed-off-by: Andreas Hatziiliou <andreas.hatziiliou@savoirfairelinux.com>
1 parent 72b9197 commit 2e24d9e

File tree

14 files changed

+49
-36
lines changed

14 files changed

+49
-36
lines changed

examples/monolithic_build_multilevel_native/multilevel_config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,9 @@
327327
#include <stdint.h>
328328
#include "sys.h"
329329
#include "test_only_rng/notrandombytes.h"
330-
static MLK_INLINE void mlk_randombytes(uint8_t *ptr, size_t len)
330+
static MLK_INLINE int mlk_randombytes(uint8_t *ptr, size_t len)
331331
{
332-
randombytes(ptr, len);
332+
return randombytes(ptr, len);
333333
}
334334
#endif /* !__ASSEMBLER__ */
335335

integration/liboqs/config_aarch64.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
* consumer.
183183
*
184184
* If this option is not set, mlkem-native expects a function
185-
* void randombytes(uint8_t *out, size_t outlen).
185+
* int randombytes(uint8_t *out, size_t outlen).
186186
*
187187
* Set this option and define `mlk_randombytes` if you want to
188188
* use a custom method to sample randombytes with a different name
@@ -194,9 +194,10 @@
194194
#include <oqs/rand.h>
195195
#include <stdint.h>
196196
#include "../../mlkem/src/sys.h"
197-
static MLK_INLINE void mlk_randombytes(uint8_t *ptr, size_t len)
197+
static MLK_INLINE int mlk_randombytes(uint8_t *ptr, size_t len)
198198
{
199199
OQS_randombytes(ptr, len);
200+
return 0;
200201
}
201202
#endif /* !__ASSEMBLER__ */
202203

integration/liboqs/config_c.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
* consumer.
146146
*
147147
* If this option is not set, mlkem-native expects a function
148-
* void randombytes(uint8_t *out, size_t outlen).
148+
* int randombytes(uint8_t *out, size_t outlen).
149149
*
150150
* Set this option and define `mlk_randombytes` if you want to
151151
* use a custom method to sample randombytes with a different name
@@ -157,9 +157,10 @@
157157
#include <oqs/rand.h>
158158
#include <stdint.h>
159159
#include "../../mlkem/src/sys.h"
160-
static MLK_INLINE void mlk_randombytes(uint8_t *ptr, size_t len)
160+
static MLK_INLINE int mlk_randombytes(uint8_t *ptr, size_t len)
161161
{
162162
OQS_randombytes(ptr, len);
163+
return 0;
163164
}
164165
#endif /* !__ASSEMBLER__ */
165166

integration/liboqs/config_x86_64.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
* consumer.
183183
*
184184
* If this option is not set, mlkem-native expects a function
185-
* void randombytes(uint8_t *out, size_t outlen).
185+
* int randombytes(uint8_t *out, size_t outlen).
186186
*
187187
* Set this option and define `mlk_randombytes` if you want to
188188
* use a custom method to sample randombytes with a different name
@@ -194,9 +194,10 @@
194194
#include <oqs/rand.h>
195195
#include <stdint.h>
196196
#include "../../mlkem/src/sys.h"
197-
static MLK_INLINE void mlk_randombytes(uint8_t *ptr, size_t len)
197+
static MLK_INLINE int mlk_randombytes(uint8_t *ptr, size_t len)
198198
{
199199
OQS_randombytes(ptr, len);
200+
return 0;
200201
}
201202
#endif /* !__ASSEMBLER__ */
202203

mlkem/src/config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@
294294
* consumer.
295295
*
296296
* If this option is not set, mlkem-native expects a function
297-
* void randombytes(uint8_t *out, size_t outlen).
297+
* int randombytes(uint8_t *out, size_t outlen).
298298
*
299299
* Set this option and define `mlk_randombytes` if you want to
300300
* use a custom method to sample randombytes with a different name
@@ -305,7 +305,7 @@
305305
#if !defined(__ASSEMBLER__)
306306
#include <stdint.h>
307307
#include "sys.h"
308-
static MLK_INLINE void mlk_randombytes(uint8_t *ptr, size_t len)
308+
static MLK_INLINE int mlk_randombytes(uint8_t *ptr, size_t len)
309309
{
310310
... your implementation ...
311311
}

mlkem/src/kem.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,10 @@ int crypto_kem_keypair(uint8_t pk[MLKEM_INDCCA_PUBLICKEYBYTES],
202202
MLK_ALIGN uint8_t coins[2 * MLKEM_SYMBYTES];
203203

204204
/* Acquire necessary randomness, and mark it as secret. */
205-
mlk_randombytes(coins, 2 * MLKEM_SYMBYTES);
205+
if (mlk_randombytes(coins, 2 * MLKEM_SYMBYTES) != 0)
206+
{
207+
return -1;
208+
}
206209
MLK_CT_TESTING_SECRET(coins, sizeof(coins));
207210

208211
res = crypto_kem_keypair_derand(pk, sk, coins);
@@ -263,7 +266,10 @@ int crypto_kem_enc(uint8_t ct[MLKEM_INDCCA_CIPHERTEXTBYTES],
263266
int res;
264267
MLK_ALIGN uint8_t coins[MLKEM_SYMBYTES];
265268

266-
mlk_randombytes(coins, MLKEM_SYMBYTES);
269+
if (mlk_randombytes(coins, MLKEM_SYMBYTES) != 0)
270+
{
271+
return -1;
272+
}
267273
MLK_CT_TESTING_SECRET(coins, sizeof(coins));
268274

269275
res = crypto_kem_enc_derand(ct, ss, pk, coins);

mlkem/src/randombytes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313
#if !defined(MLK_CONFIG_NO_RANDOMIZED_API)
1414
#if !defined(MLK_CONFIG_CUSTOM_RANDOMBYTES)
15-
void randombytes(uint8_t *out, size_t outlen);
16-
static MLK_INLINE void mlk_randombytes(uint8_t *out, size_t outlen)
15+
int randombytes(uint8_t *out, size_t outlen);
16+
static MLK_INLINE int mlk_randombytes(uint8_t *out, size_t outlen)
1717
__contract__(
1818
requires(memory_no_alias(out, outlen))
19-
assigns(memory_slice(out, outlen))) { randombytes(out, outlen); }
19+
assigns(memory_slice(out, outlen))) { return randombytes(out, outlen); }
2020
#endif /* !MLK_CONFIG_CUSTOM_RANDOMBYTES */
2121
#endif /* !MLK_CONFIG_NO_RANDOMIZED_API */
2222
#endif /* !MLK_RANDOMBYTES_H */

test/bench_components_mlkem.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ static int cmp_uint64_t(const void *a, const void *b)
2727
return (int)((*((const uint64_t *)a)) - (*((const uint64_t *)b)));
2828
}
2929

30-
#define BENCH(txt, code) \
31-
for (i = 0; i < NTESTS; i++) \
32-
{ \
33-
randombytes((uint8_t *)data0, sizeof(data0)); \
34-
randombytes((uint8_t *)data1, sizeof(data1)); \
35-
randombytes((uint8_t *)data2, sizeof(data2)); \
36-
randombytes((uint8_t *)data3, sizeof(data3)); \
37-
randombytes((uint8_t *)data4, sizeof(data4)); \
30+
#define BENCH(txt, code) \
31+
for (i = 0; i < NTESTS; i++) \
32+
{ \
33+
CHECK(randombytes((uint8_t *)data0, sizeof(data0)) == 0); \
34+
CHECK(randombytes((uint8_t *)data1, sizeof(data1)) == 0); \
35+
CHECK(randombytes((uint8_t *)data2, sizeof(data2)) == 0); \
36+
CHECK(randombytes((uint8_t *)data3, sizeof(data3)) == 0); \
37+
CHECK(randombytes((uint8_t *)data4, sizeof(data4)) == 0); \
3838
for (j = 0; j < NWARMUP; j++) \
3939
{ \
4040
code; \

test/bench_mlkem.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ static int bench(void)
8080
for (i = 0; i < NTESTS; i++)
8181
{
8282
int ret = 0;
83-
randombytes(kg_rand, 2 * CRYPTO_BYTES);
84-
randombytes(enc_rand, CRYPTO_BYTES);
83+
CHECK(randombytes(kg_rand, 2 * CRYPTO_BYTES) == 0);
84+
CHECK(randombytes(enc_rand, CRYPTO_BYTES) == 0);
8585

8686
/* Key-pair generation */
8787
for (j = 0; j < NWARMUP; j++)

test/configs.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ configs:
3737
#include <stdint.h>
3838
#include "../mlkem/src/sys.h"
3939
#include "notrandombytes/notrandombytes.h"
40-
static MLK_INLINE void mlk_randombytes(uint8_t *ptr, size_t len)
40+
static MLK_INLINE int mlk_randombytes(uint8_t *ptr, size_t len)
4141
{
42-
randombytes(ptr, len);
42+
return randombytes(ptr, len);
4343
}
4444
#endif /* !__ASSEMBLER__ */
4545
@@ -367,9 +367,9 @@ configs:
367367
#include <stdint.h>
368368
#include "sys.h"
369369
#include "test_only_rng/notrandombytes.h"
370-
static MLK_INLINE void mlk_randombytes(uint8_t *ptr, size_t len)
370+
static MLK_INLINE int mlk_randombytes(uint8_t *ptr, size_t len)
371371
{
372-
randombytes(ptr, len);
372+
return randombytes(ptr, len);
373373
}
374374
#endif /* !__ASSEMBLER__ */
375375

0 commit comments

Comments
 (0)