void FIPS_drbg_free(DRBG_CTX *dctx)
{
- dctx->uninstantiate(dctx);
+ if (dctx->uninstantiate)
+ dctx->uninstantiate(dctx);
OPENSSL_cleanse(dctx, sizeof(DRBG_CTX));
OPENSSL_free(dctx);
}
int FIPS_drbg_instantiate(DRBG_CTX *dctx,
const unsigned char *pers, size_t perslen)
{
- size_t entlen, noncelen;
+ size_t entlen = 0, noncelen = 0;
+ unsigned char *nonce = NULL, *entropy = NULL;
#if 0
/* Put here so error script picks them up */
dctx->status = DRBG_STATUS_ERROR;
- entlen = dctx->get_entropy(dctx, dctx->entropy, dctx->strength,
+ entlen = dctx->get_entropy(dctx, &entropy, dctx->strength,
dctx->min_entropy, dctx->max_entropy);
if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
if (dctx->max_nonce > 0)
{
-
- noncelen = dctx->get_nonce(dctx, dctx->nonce,
+ noncelen = dctx->get_nonce(dctx, &nonce,
dctx->strength / 2,
dctx->min_nonce, dctx->max_nonce);
}
}
- else
- noncelen = 0;
if (!dctx->instantiate(dctx,
- dctx->entropy, entlen,
- dctx->nonce, noncelen,
+ entropy, entlen,
+ nonce, noncelen,
pers, perslen))
{
r = FIPS_R_ERROR_INSTANTIATING_DRBG;
end:
- OPENSSL_cleanse(dctx->entropy, sizeof(dctx->entropy));
- OPENSSL_cleanse(dctx->nonce, sizeof(dctx->nonce));
+ if (entropy && dctx->cleanup_entropy)
+ dctx->cleanup_entropy(dctx, entropy, entlen);
+
+ if (nonce && dctx->cleanup_nonce)
+ dctx->cleanup_nonce(dctx, nonce, noncelen);
if (dctx->status == DRBG_STATUS_READY)
return 1;
int FIPS_drbg_reseed(DRBG_CTX *dctx,
const unsigned char *adin, size_t adinlen)
{
+ unsigned char *entropy = NULL;
size_t entlen;
int r = 0;
dctx->status = DRBG_STATUS_ERROR;
- entlen = dctx->get_entropy(dctx, dctx->entropy, dctx->strength,
+ entlen = dctx->get_entropy(dctx, &entropy, dctx->strength,
dctx->min_entropy, dctx->max_entropy);
if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
goto end;
}
- if (!dctx->reseed(dctx, dctx->entropy, entlen, adin, adinlen))
+ if (!dctx->reseed(dctx, entropy, entlen, adin, adinlen))
goto end;
dctx->status = DRBG_STATUS_READY;
dctx->reseed_counter = 1;
end:
- OPENSSL_cleanse(dctx->entropy, sizeof(dctx->entropy));
+
+ if (entropy && dctx->cleanup_entropy)
+ dctx->cleanup_entropy(dctx, entropy, entlen);
if (dctx->status == DRBG_STATUS_READY)
return 1;
{
int rv;
if (!dctx->uninstantiate)
- return 1;
- rv = dctx->uninstantiate(dctx);
+ rv = 1;
+ else
+ rv = dctx->uninstantiate(dctx);
/* Although we'd like to cleanse here we can't because we have to
* test the uninstantiate really zeroes the data.
*/
return rv;
}
-int FIPS_drbg_set_test_mode(DRBG_CTX *dctx,
- size_t (*get_entropy)(DRBG_CTX *ctx, unsigned char *out,
+int FIPS_drbg_set_callbacks(DRBG_CTX *dctx,
+ size_t (*get_entropy)(DRBG_CTX *ctx, unsigned char **pout,
+ int entropy, size_t min_len, size_t max_len),
+ void (*cleanup_entropy)(DRBG_CTX *ctx, unsigned char *out, size_t olen),
+ size_t (*get_nonce)(DRBG_CTX *ctx, unsigned char **pout,
int entropy, size_t min_len, size_t max_len),
- size_t (*get_nonce)(DRBG_CTX *ctx, unsigned char *out,
- int entropy, size_t min_len, size_t max_len))
+ void (*cleanup_nonce)(DRBG_CTX *ctx, unsigned char *out, size_t olen))
{
if (dctx->status != DRBG_STATUS_UNINITIALISED)
return 0;
- dctx->flags |= DRBG_FLAG_TEST;
dctx->get_entropy = get_entropy;
+ dctx->cleanup_entropy = cleanup_entropy;
dctx->get_nonce = get_nonce;
+ dctx->cleanup_nonce = cleanup_nonce;
return 1;
}