Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions dtls_prng.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
* implementation for prng(). You might want to change prng() to use
* a better PRNG on your specific platform.
*
* @param buf The buffer to fill
* @param len the length of the buffer to fill
* @param buf The buffer to fill.
* @param len the length of the buffer to fill.
*
* @return 1 buffer filled
* @return 1 buffer filled, else 0 on error.
*/
int dtls_prng(unsigned char *buf, size_t len);

Expand Down
5 changes: 3 additions & 2 deletions platform-specific/dtls_prng_contiki.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ int
dtls_prng(unsigned char *buf, size_t len)
{
#ifdef HAVE_PRNG
/* contiki_prng_impl() must return 0 on failure, else 1. */
return contiki_prng_impl(buf, len);
#else /* ! HAVE_PRNG */
/**
* Fills \p buf with \p len random bytes. This is the default
* implementation for prng(). You might want to change prng() to use
* implementation for dtls_prng(). You might want to change dtls_prng() to use
* a better PRNG on your specific platform.
*/
unsigned short v = random_rand();
Expand All @@ -43,7 +44,7 @@ dtls_prng(unsigned char *buf, size_t len)
}

memcpy(buf, &v, len);
return len;
return 1;
}
#endif /* ! HAVE_PRNG */

Expand Down
4 changes: 2 additions & 2 deletions platform-specific/dtls_prng_espidf.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@

/**
* Fills \p buf with \p len random bytes. This is the default
* implementation for prng(). You might want to change prng() to use
* implementation for dtls_prng(). You might want to change dtls_prng() to use
* a better PRNG on your specific platform.
*/
int
dtls_prng(unsigned char *buf, size_t len) {
esp_fill_random(buf, len);
return len;
return 1;
}

void
Expand Down
3 changes: 1 addition & 2 deletions platform-specific/dtls_prng_lwip.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
int
dtls_prng(unsigned char *buf, size_t len) {
u32_t v = LWIP_RAND();
size_t k_len = len;

while (len > sizeof(v)) {
memcpy(buf, &v, sizeof(v));
Expand All @@ -32,7 +31,7 @@ dtls_prng(unsigned char *buf, size_t len) {
}

memcpy(buf, &v, len);
return k_len;
return 1;
}

void
Expand Down
9 changes: 4 additions & 5 deletions platform-specific/dtls_prng_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@

/**
* Fills @p buf with @p len random bytes. This is the default
* implementation for prng(). You might want to change prng() to use
* implementation for dtls_prng(). You might want to change dtls_prng() to use
* a better PRNG on your specific platform.
*/
int
dtls_prng(unsigned char *buf, size_t len) {
#ifdef HAVE_GETRANDOM
return getrandom(buf, len, 0);
return getrandom(buf, len, 0) > 0 ? 1 : 0;
#elif defined(HAVE_RANDOM)

#define RAND_BYTES (RAND_MAX >= 0xffffff ? 3 : (RAND_MAX >= 0xffff ? 2 : 1))
Expand All @@ -64,14 +64,13 @@ dtls_prng(unsigned char *buf, size_t len) {
}
}
}
return len;
return 1;
#else /*!HAVE_GETRANDOM && !HAVE_RANDOM */
#error "CVE-2021-34430: using rand() for crypto randoms is not secure!"
#error "Please update you C-library and rerun the auto-configuration."
size_t klen = len;
while (len--)
*buf++ = rand() & 0xFF;
return klen;
return 1;
#endif /* !HAVE_GETRANDOM */
}

Expand Down
2 changes: 1 addition & 1 deletion platform-specific/dtls_prng_riot.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
int
dtls_prng(unsigned char *buf, size_t len) {
random_bytes(buf, len);
return len;
return 1;
}

void
Expand Down
6 changes: 3 additions & 3 deletions platform-specific/dtls_prng_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ int
dtls_prng(unsigned char *buf, size_t len) {
errno_t err;
unsigned int number;
size_t klen = len;

while (len--) {
err = rand_s(&number);
if (err != 0) {
dtls_emerg("PRNG failed\n");
return err;
return 0;
}
*buf++ = number & 0xFF;
}
return klen;
return 1;
}

void
Expand Down
3 changes: 1 addition & 2 deletions platform-specific/dtls_prng_zephyr.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@

int
dtls_prng(unsigned char *buf, size_t len) {
sys_csrand_get(buf, len);
return len;
return sys_csrand_get(buf, len) == 0 ? 1 : 0;
}

void
Expand Down
4 changes: 2 additions & 2 deletions tests/unit-tests/test_ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ t_test_ecc_dh(void) {
int ret;

ret = dtls_prng((void *)secretA, sizeof(secretA));
CU_ASSERT(ret > 1);
CU_ASSERT(ret == 1);

ret = dtls_prng((void *)secretB, sizeof(secretB));
CU_ASSERT(ret > 1);
CU_ASSERT(ret == 1);

ecc_ec_mult(BasePointx, BasePointy, secretA, tempx, tempy);
ecc_ec_mult(BasePointx, BasePointy, secretB, tempBx1, tempBy1);
Expand Down
Loading