Skip to content

Usage of insufficient random number generators

Description

The detected function is not sufficient at generating security-related random numbers, such as those used in key and nonce creation. Consider using the libsodium library's `randombytes_random` function instead. More information on libsodium's random number generators can be found here: https://libsodium.gitbook.io/doc/generating_random_data. If FIPS validation is required, consider using OpenSSLs `RAND_bytes` family of functions after enabling the `FIPS_mode_set`. For more information on OpenSSL random numbers please see: https://wiki.openssl.org/index.php/Random_Numbers

Examples

Insecure Code

c
srand(time(NULL));
int random_number = rand();

Secure Code

c
#include <sodium.h>
unsigned char random_bytes[16];
sodium_randombytes_buf(random_bytes, 16);

Remediation

Replace the insufficient random number generator with a secure alternative, such as libsodium's `randombytes_random` or OpenSSL's `RAND_bytes`.

Rule Details

FieldValue
IDCODE-0611
CategoryCrypto
SeverityMEDIUM
CWECWE-327
ConfidenceHIGH
ImpactMEDIUM
LikelihoodMEDIUM
ExploitabilityMODERATE
Tagsrandom number generator, security
OWASPA3:2017-Sensitive Data Exposure, A02:2021-Cryptographic Failures