rand_seed() initialization failed in OpenSSL 3.0.7

264 Views Asked by At

We are trying to port OpenSSL 3.0.7 on RTOS & getting a failure during Rand initialization. Same code/logic is working on 1.1.1 & 1.0.2 OpenSSL versions.

Can you please help us with the steps/function calls to be used for initializing rand seed in Openssl 3.0.7 Can you please help to check what could be the issue in below logic.

Code for Rand initialization in application using openssl 1.0.2 & 1.1.1

RAND_DRBG *master = NULL; master = RAND_DRBG_get0_master(); if(!master) { return 0; } Note: RAND_DRBG_get0_master is depreciated in OpenSSL 3.0.7

Tried using RAND_get0_public() but result was failure since Master was NULL after invoking the function. EVP_RAND_CTX *master = NULL; master = RAND_get0_public(NULL);

1

There are 1 best solutions below

3
Camille G. On

Here is the Code for OpenSSL 3.0+ :

#include <openssl/evp.h>

unsigned char buf[32];
EVP_DRBG *drbg = EVP_DRBG_new(NULL, NULL);
if (drbg != NULL) {
  if (EVP_DRBG_instantiate(drbg, NULL, 0, NULL, 0)) {
    if (EVP_DRBG_bytes(drbg, buf, sizeof(buf))) {
      // 32 bytes of random data...
    }
  }
  EVP_DRBG_free(drbg);
}

Here is the code for OpenSSL 1.1. :

Use RAND_DRBG_new to obtain a random generator.

unsigned char buf[32];
OPENSSL_CTX *ctx = OPENSSL_CTX_new();
RAND_DRBG *drbg = RAND_DRBG_new(ctx, DRBG_TYPE_CTR, NULL);
if (drbg != NULL) {
  if (RAND_DRBG_instantiate(drbg, NULL, 0, NULL, 0)) {
    if (RAND_DRBG_bytes(drbg, buf, sizeof(buf))) {
      // 32 bit of random data...
    }
  }
  RAND_DRBG_free(drbg);
}
OPENSSL_CTX_free(ctx);