Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

On Windows platforms, the CryptGenRandom BCryptGenRandom() function can be used to generate cryptographically strong random numbers. The exact details of the implementation are unknown, including, for example, what source of entropy CryptGenRandom() uses. The Microsoft Developer Network CryptGenRandomBCryptGenRandom() reference [MSDN] states:

The default random number provider implements an algorithm for generating random numbers that complies with the NIST SP800-90 standard, specifically the CTR_DRBG portion of that standardIf an application has access to a good random source, it can fill the pbBuffer buffer with some random data before calling CryptGenRandom(). The CSP [cryptographic service provider] then uses this data to further randomize its internal seed. It is acceptable to omit the step of initializing the pbBuffer buffer before calling CryptGenRandom().

Code Block
bgColor#ccccff
langc
#include <Windows.h>
#include <wincrypt<bcrypt.h>
#include <stdio.h>
 

#pragma comment(lib, "Bcrypt")

void func(void) {
  BCRYPT_ALG_HANDLE Prov;
  HCRYPTPROVint provBuffer;
  if (CryptAcquireContext(&prov, NULL, NULL,!BCRYPT_SUCCESS(
          BCryptOpenAlgorithmProvider(&Prov, BCRYPT_RNG_ALGORITHM,
               PROV_RSA_FULL, 0)) {
    long int li = 0;
    if (CryptGenRandom(prov, sizeof(li), (BYTE *)&li)) {
      printf("Random number: %ld\n" NULL, li0)));
    } else {
      /* Handlehandle error */
    }
    if (!CryptReleaseContext(provBCRYPT_SUCCESS(BCryptGenRandom(Prov, 0)) {(PUCHAR) (&Buffer),
      /* Handle error */
    }
  } else {
    /* Handle error */
  }
}

Compliant Solution (Windows)

On Windows platforms, the rand_s() function can be used to generate cryptographically strong random numbers.

Code Block
bgColor#ccccff
langc
#include <stdio.h>
#include <stdlib.h>
 
int func(void) {
  unsigned int number;
  errno_t err;

  err = rand_s( &number);
  if (err != 0)sizeof(Buffer), 0))) {
    /* handle error */
  } else {
    printf("Random number: %u%d\n", numberBuffer);
  } BCryptCloseAlgorithmProvider(Prov, 0);
}

Risk Assessment

The use of the rand() function can result in predictable random numbers.

...

Supported, but no explicit checkerUsing a cryptographically weak PRQA QA-CFully implemented

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V
stdlib-use-randFully checked
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-MSC30
Clang
Include Page
Clang_40_V
Clang_40_V
cert-msc30-cChecked by clang-tidy
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
BADFUNC.RANDOM.RANDUse of rand
Compass/ROSE




Coverity
Include Page
Coverity_V
Coverity_V

DONTCALL

Implemented - weak support
Cppcheck Premium

Include Page
Cppcheck Premium_V
Cppcheck Premium_V

premium-cert-msc30-cFully implemented

ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

CC2.MSC30

Fully implemented

Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C5022

C++5029


Klocwork
Include Page
Klocwork_V
Klocwork_V

CERT.MSC.STD_RAND_CALL


LDRA tool suite
Include Page
LDRA_V
LDRA_V
44 SEnhanced enforcement
Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V

CERT_C-MSC30-a

Do not use the rand() function for generating pseudorandom numbers
PC-lint Plus

Include Page
PC-lint Plus_V
PC-lint Plus_V

586

Fully supported

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

Vulnerable pseudo-random number generatorCERT C: Rule MSC30-CChecks for vulnerable pseudo-random number generator (rule fully covered)


RuleChecker

Include Page

PRQA QA-C_v

RuleChecker_V
RuleChecker_V

stdlib-use-randFully checkedPRQA QA-C_v5022

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

...

  • Invocation of other dangerous functions, besides rand().


Bibliography


...