2019-03-29 15:50:48 +08:00
|
|
|
/*
|
2020-06-25 21:13:12 +08:00
|
|
|
* Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
|
2019-03-29 15:50:48 +08:00
|
|
|
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of the FIPS 140-2 section 4.9.2 Conditional Tests.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
2019-04-30 11:43:19 +08:00
|
|
|
#include <openssl/evp.h>
|
2020-06-21 07:19:16 +08:00
|
|
|
#include <openssl/core_dispatch.h>
|
2020-05-08 08:25:03 +08:00
|
|
|
#include <openssl/params.h>
|
2020-09-10 12:01:30 +08:00
|
|
|
#include <openssl/self_test.h>
|
2020-05-08 08:25:03 +08:00
|
|
|
#include "prov/providercommon.h"
|
|
|
|
#include "prov/provider_ctx.h"
|
2019-05-23 23:51:55 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2020-05-08 08:25:03 +08:00
|
|
|
#include "prov/rand_pool.h"
|
2020-05-08 08:22:45 +08:00
|
|
|
#include "drbg_local.h"
|
2020-05-08 08:25:19 +08:00
|
|
|
#include "prov/seeding.h"
|
2019-03-29 15:50:48 +08:00
|
|
|
|
2019-05-23 23:51:55 +08:00
|
|
|
typedef struct crng_test_global_st {
|
|
|
|
unsigned char crngt_prev[EVP_MAX_MD_SIZE];
|
|
|
|
RAND_POOL *crngt_pool;
|
|
|
|
} CRNG_TEST_GLOBAL;
|
2019-03-29 15:50:48 +08:00
|
|
|
|
2020-05-08 08:22:45 +08:00
|
|
|
static int crngt_get_entropy(OPENSSL_CTX *ctx, RAND_POOL *pool,
|
|
|
|
unsigned char *buf, unsigned char *md,
|
|
|
|
unsigned int *md_size)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
size_t n;
|
|
|
|
unsigned char *p;
|
2020-05-08 08:25:03 +08:00
|
|
|
EVP_MD *fmd;
|
2020-05-08 08:22:45 +08:00
|
|
|
|
|
|
|
if (pool == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
n = prov_pool_acquire_entropy(pool);
|
|
|
|
if (n >= CRNGT_BUFSIZ) {
|
2020-05-08 08:25:03 +08:00
|
|
|
fmd = EVP_MD_fetch(ctx, "SHA256", "");
|
2020-05-08 08:22:45 +08:00
|
|
|
if (fmd == NULL)
|
|
|
|
return 0;
|
|
|
|
p = rand_pool_detach(pool);
|
|
|
|
r = EVP_Digest(p, CRNGT_BUFSIZ, md, md_size, fmd, NULL);
|
|
|
|
if (r != 0)
|
|
|
|
memcpy(buf, p, CRNGT_BUFSIZ);
|
|
|
|
rand_pool_reattach(pool, p);
|
|
|
|
EVP_MD_free(fmd);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2019-03-29 15:50:48 +08:00
|
|
|
|
2019-05-23 23:51:55 +08:00
|
|
|
static void rand_crng_ossl_ctx_free(void *vcrngt_glob)
|
2019-03-29 15:50:48 +08:00
|
|
|
{
|
2019-05-23 23:51:55 +08:00
|
|
|
CRNG_TEST_GLOBAL *crngt_glob = vcrngt_glob;
|
2019-03-29 15:50:48 +08:00
|
|
|
|
2019-05-23 23:51:55 +08:00
|
|
|
rand_pool_free(crngt_glob->crngt_pool);
|
|
|
|
OPENSSL_free(crngt_glob);
|
2019-03-29 15:50:48 +08:00
|
|
|
}
|
|
|
|
|
2019-05-23 23:51:55 +08:00
|
|
|
static void *rand_crng_ossl_ctx_new(OPENSSL_CTX *ctx)
|
2019-03-29 15:50:48 +08:00
|
|
|
{
|
2019-04-30 11:43:19 +08:00
|
|
|
unsigned char buf[CRNGT_BUFSIZ];
|
2019-05-23 23:51:55 +08:00
|
|
|
CRNG_TEST_GLOBAL *crngt_glob = OPENSSL_zalloc(sizeof(*crngt_glob));
|
2019-04-30 11:43:19 +08:00
|
|
|
|
2019-05-23 23:51:55 +08:00
|
|
|
if (crngt_glob == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((crngt_glob->crngt_pool
|
2019-07-20 17:22:46 +08:00
|
|
|
= rand_pool_new(0, 1, CRNGT_BUFSIZ, CRNGT_BUFSIZ)) == NULL) {
|
2019-05-23 23:51:55 +08:00
|
|
|
OPENSSL_free(crngt_glob);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-05-23 21:35:42 +08:00
|
|
|
if (crngt_get_entropy(ctx, crngt_glob->crngt_pool, buf,
|
|
|
|
crngt_glob->crngt_prev, NULL)) {
|
2019-04-30 11:43:19 +08:00
|
|
|
OPENSSL_cleanse(buf, sizeof(buf));
|
2019-05-23 23:51:55 +08:00
|
|
|
return crngt_glob;
|
2019-04-30 11:43:19 +08:00
|
|
|
}
|
2019-05-23 23:51:55 +08:00
|
|
|
rand_pool_free(crngt_glob->crngt_pool);
|
|
|
|
OPENSSL_free(crngt_glob);
|
|
|
|
return NULL;
|
2019-03-29 15:50:48 +08:00
|
|
|
}
|
|
|
|
|
2019-05-23 23:51:55 +08:00
|
|
|
static const OPENSSL_CTX_METHOD rand_crng_ossl_ctx_method = {
|
|
|
|
rand_crng_ossl_ctx_new,
|
|
|
|
rand_crng_ossl_ctx_free,
|
|
|
|
};
|
2019-03-29 15:50:48 +08:00
|
|
|
|
2020-09-10 05:09:16 +08:00
|
|
|
static int prov_crngt_compare_previous(const unsigned char *prev,
|
2020-09-02 07:08:09 +08:00
|
|
|
const unsigned char *cur,
|
|
|
|
size_t sz)
|
2020-09-10 05:09:16 +08:00
|
|
|
{
|
|
|
|
const int res = memcmp(prev, cur, sz) != 0;
|
|
|
|
|
|
|
|
if (!res)
|
2020-09-10 12:01:30 +08:00
|
|
|
ossl_set_error_state(OSSL_SELF_TEST_TYPE_CRNG);
|
2020-09-10 05:09:16 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-05-08 08:25:03 +08:00
|
|
|
size_t prov_crngt_get_entropy(PROV_DRBG *drbg,
|
2019-03-29 15:50:48 +08:00
|
|
|
unsigned char **pout,
|
|
|
|
int entropy, size_t min_len, size_t max_len,
|
|
|
|
int prediction_resistance)
|
|
|
|
{
|
2019-04-30 11:43:19 +08:00
|
|
|
unsigned char buf[CRNGT_BUFSIZ], md[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int sz;
|
2019-03-29 15:50:48 +08:00
|
|
|
RAND_POOL *pool;
|
|
|
|
size_t q, r = 0, s, t = 0;
|
2020-09-02 07:08:09 +08:00
|
|
|
int attempts = 3, crng_test_pass = 1;
|
2020-05-08 08:25:03 +08:00
|
|
|
OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(drbg->provctx);
|
2019-05-23 23:51:55 +08:00
|
|
|
CRNG_TEST_GLOBAL *crngt_glob
|
2020-05-08 08:25:03 +08:00
|
|
|
= openssl_ctx_get_data(libctx, OPENSSL_CTX_RAND_CRNGT_INDEX,
|
2019-05-23 23:51:55 +08:00
|
|
|
&rand_crng_ossl_ctx_method);
|
2020-09-02 07:08:09 +08:00
|
|
|
OSSL_CALLBACK *stcb = NULL;
|
|
|
|
void *stcbarg = NULL;
|
|
|
|
OSSL_SELF_TEST *st = NULL;
|
2019-03-29 15:50:48 +08:00
|
|
|
|
2019-05-23 23:51:55 +08:00
|
|
|
if (crngt_glob == NULL)
|
2019-03-29 15:50:48 +08:00
|
|
|
return 0;
|
|
|
|
|
2019-07-20 17:22:46 +08:00
|
|
|
if ((pool = rand_pool_new(entropy, 1, min_len, max_len)) == NULL)
|
2019-03-29 15:50:48 +08:00
|
|
|
return 0;
|
|
|
|
|
2020-09-02 07:08:09 +08:00
|
|
|
OSSL_SELF_TEST_get_callback(libctx, &stcb, &stcbarg);
|
|
|
|
if (stcb != NULL) {
|
|
|
|
st = OSSL_SELF_TEST_new(stcb, stcbarg);
|
|
|
|
if (st == NULL)
|
|
|
|
goto err;
|
|
|
|
OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_CRNG,
|
|
|
|
OSSL_SELF_TEST_DESC_RNG);
|
|
|
|
}
|
|
|
|
|
2019-03-29 15:50:48 +08:00
|
|
|
while ((q = rand_pool_bytes_needed(pool, 1)) > 0 && attempts-- > 0) {
|
|
|
|
s = q > sizeof(buf) ? sizeof(buf) : q;
|
2020-09-02 07:08:09 +08:00
|
|
|
if (!crngt_get_entropy(libctx, crngt_glob->crngt_pool, buf, md, &sz))
|
|
|
|
goto err;
|
|
|
|
/* Force a failure here if the callback returns 1 */
|
|
|
|
if (OSSL_SELF_TEST_oncorrupt_byte(st, md))
|
|
|
|
memcpy(md, crngt_glob->crngt_prev, sz);
|
|
|
|
if (!prov_crngt_compare_previous(crngt_glob->crngt_prev, md, sz)) {
|
|
|
|
crng_test_pass = 0;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!rand_pool_add(pool, buf, s, s * 8))
|
2019-03-29 15:50:48 +08:00
|
|
|
goto err;
|
2019-05-23 23:51:55 +08:00
|
|
|
memcpy(crngt_glob->crngt_prev, md, sz);
|
2019-03-29 15:50:48 +08:00
|
|
|
t += s;
|
|
|
|
attempts++;
|
|
|
|
}
|
|
|
|
r = t;
|
|
|
|
*pout = rand_pool_detach(pool);
|
|
|
|
err:
|
2020-09-02 07:08:09 +08:00
|
|
|
OSSL_SELF_TEST_onend(st, crng_test_pass);
|
|
|
|
OSSL_SELF_TEST_free(st);
|
2019-04-30 11:43:19 +08:00
|
|
|
OPENSSL_cleanse(buf, sizeof(buf));
|
2019-03-29 15:50:48 +08:00
|
|
|
rand_pool_free(pool);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-05-08 08:25:03 +08:00
|
|
|
void prov_crngt_cleanup_entropy(PROV_DRBG *drbg,
|
2019-03-29 15:50:48 +08:00
|
|
|
unsigned char *out, size_t outlen)
|
|
|
|
{
|
|
|
|
OPENSSL_secure_clear_free(out, outlen);
|
|
|
|
}
|