diff --git a/dtls_prng.h b/dtls_prng.h index c445c80d..af02e827 100644 --- a/dtls_prng.h +++ b/dtls_prng.h @@ -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); diff --git a/platform-specific/dtls_prng_contiki.c b/platform-specific/dtls_prng_contiki.c index 4730f77b..48befb30 100644 --- a/platform-specific/dtls_prng_contiki.c +++ b/platform-specific/dtls_prng_contiki.c @@ -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(); @@ -43,7 +44,7 @@ dtls_prng(unsigned char *buf, size_t len) } memcpy(buf, &v, len); - return len; + return 1; } #endif /* ! HAVE_PRNG */ diff --git a/platform-specific/dtls_prng_espidf.c b/platform-specific/dtls_prng_espidf.c index 084bcecb..c342b516 100644 --- a/platform-specific/dtls_prng_espidf.c +++ b/platform-specific/dtls_prng_espidf.c @@ -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 diff --git a/platform-specific/dtls_prng_lwip.c b/platform-specific/dtls_prng_lwip.c index 40ad3ef0..192750c8 100644 --- a/platform-specific/dtls_prng_lwip.c +++ b/platform-specific/dtls_prng_lwip.c @@ -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)); @@ -32,7 +31,7 @@ dtls_prng(unsigned char *buf, size_t len) { } memcpy(buf, &v, len); - return k_len; + return 1; } void diff --git a/platform-specific/dtls_prng_posix.c b/platform-specific/dtls_prng_posix.c index 2c3f3e71..ebb22b44 100644 --- a/platform-specific/dtls_prng_posix.c +++ b/platform-specific/dtls_prng_posix.c @@ -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)) @@ -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 */ } diff --git a/platform-specific/dtls_prng_riot.c b/platform-specific/dtls_prng_riot.c index 3a03b7ff..6bfa1ec3 100644 --- a/platform-specific/dtls_prng_riot.c +++ b/platform-specific/dtls_prng_riot.c @@ -25,7 +25,7 @@ int dtls_prng(unsigned char *buf, size_t len) { random_bytes(buf, len); - return len; + return 1; } void diff --git a/platform-specific/dtls_prng_win.c b/platform-specific/dtls_prng_win.c index a71633b2..287a5b8b 100644 --- a/platform-specific/dtls_prng_win.c +++ b/platform-specific/dtls_prng_win.c @@ -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 diff --git a/platform-specific/dtls_prng_zephyr.c b/platform-specific/dtls_prng_zephyr.c index b9576378..245c97b8 100644 --- a/platform-specific/dtls_prng_zephyr.c +++ b/platform-specific/dtls_prng_zephyr.c @@ -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 diff --git a/tests/unit-tests/test_ecc.c b/tests/unit-tests/test_ecc.c index a2dc99fe..5476b54b 100644 --- a/tests/unit-tests/test_ecc.c +++ b/tests/unit-tests/test_ecc.c @@ -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);