Skip to content

Commit bfe68e4

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 b248d02 commit bfe68e4

File tree

17 files changed

+92
-61
lines changed

17 files changed

+92
-61
lines changed

examples/basic/test_only_rng/notrandombytes.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static void surf(void)
8787
}
8888
}
8989

90-
void randombytes(uint8_t *buf, size_t n)
90+
int randombytes(uint8_t *buf, size_t n)
9191
{
9292
while (n > 0)
9393
{
@@ -110,4 +110,5 @@ void randombytes(uint8_t *buf, size_t n)
110110
++buf;
111111
--n;
112112
}
113+
return 0;
113114
}

examples/basic/test_only_rng/notrandombytes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
*/
3131

3232
void randombytes_reset(void);
33-
void randombytes(uint8_t *buf, size_t n);
33+
int randombytes(uint8_t *buf, size_t n);
3434

3535
#endif /* !NOTRANDOMBYTES_H */

examples/bring_your_own_fips202/test_only_rng/notrandombytes.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static void surf(void)
8787
}
8888
}
8989

90-
void randombytes(uint8_t *buf, size_t n)
90+
int randombytes(uint8_t *buf, size_t n)
9191
{
9292
while (n > 0)
9393
{
@@ -110,4 +110,5 @@ void randombytes(uint8_t *buf, size_t n)
110110
++buf;
111111
--n;
112112
}
113+
return 0;
113114
}

examples/bring_your_own_fips202/test_only_rng/notrandombytes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
*/
3131

3232
void randombytes_reset(void);
33-
void randombytes(uint8_t *buf, size_t n);
33+
int randombytes(uint8_t *buf, size_t n);
3434

3535
#endif /* !NOTRANDOMBYTES_H */

examples/bring_your_own_fips202_static/test_only_rng/notrandombytes.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static void surf(void)
8787
}
8888
}
8989

90-
void randombytes(uint8_t *buf, size_t n)
90+
int randombytes(uint8_t *buf, size_t n)
9191
{
9292
while (n > 0)
9393
{
@@ -110,4 +110,5 @@ void randombytes(uint8_t *buf, size_t n)
110110
++buf;
111111
--n;
112112
}
113+
return 0;
113114
}

examples/bring_your_own_fips202_static/test_only_rng/notrandombytes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
*/
3131

3232
void randombytes_reset(void);
33-
void randombytes(uint8_t *buf, size_t n);
33+
int randombytes(uint8_t *buf, size_t n);
3434

3535
#endif /* !NOTRANDOMBYTES_H */

integration/liboqs/config_aarch64.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,10 @@
174174
#include <oqs/rand.h>
175175
#include <stdint.h>
176176
#include "../../mldsa/src/sys.h"
177-
static MLD_INLINE void mld_randombytes(uint8_t *ptr, size_t len)
177+
static MLD_INLINE int mld_randombytes(uint8_t *ptr, size_t len)
178178
{
179179
OQS_randombytes(ptr, len);
180+
return 0;
180181
}
181182
#endif /* !__ASSEMBLER__ */
182183

integration/liboqs/config_c.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,10 @@
178178
#include <oqs/rand.h>
179179
#include <stdint.h>
180180
#include "../../mldsa/src/sys.h"
181-
static MLD_INLINE void mld_randombytes(uint8_t *ptr, size_t len)
181+
static MLD_INLINE int mld_randombytes(uint8_t *ptr, size_t len)
182182
{
183183
OQS_randombytes(ptr, len);
184+
return 0;
184185
}
185186
#endif /* !__ASSEMBLER__ */
186187

integration/liboqs/config_x86_64.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@
176176
#include <oqs/rand.h>
177177
#include <stdint.h>
178178
#include "../../mldsa/src/sys.h"
179-
static MLD_INLINE void mld_randombytes(uint8_t *ptr, size_t len)
179+
static MLD_INLINE int mld_randombytes(uint8_t *ptr, size_t len)
180180
{
181181
OQS_randombytes(ptr, len);
182+
return 0;
182183
}
183184
#endif /* !__ASSEMBLER__ */
184185

mldsa/src/randombytes.h

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

1414
#if !defined(MLD_CONFIG_NO_RANDOMIZED_API)
1515
#if !defined(MLD_CONFIG_CUSTOM_RANDOMBYTES)
16-
void randombytes(uint8_t *out, size_t outlen);
17-
static MLD_INLINE void mld_randombytes(uint8_t *out, size_t outlen)
16+
int randombytes(uint8_t *out, size_t outlen);
17+
static MLD_INLINE int mld_randombytes(uint8_t *out, size_t outlen)
1818
__contract__(
1919
requires(memory_no_alias(out, outlen))
2020
assigns(memory_slice(out, outlen))
21-
) { randombytes(out, outlen); }
21+
) { return randombytes(out, outlen); }
2222
#endif /* !MLD_CONFIG_CUSTOM_RANDOMBYTES */
2323
#endif /* !MLD_CONFIG_NO_RANDOMIZED_API */
2424
#endif /* !MLD_RANDOMBYTES_H */

0 commit comments

Comments
 (0)