Latest update.
This commit is contained in:
+160
-40
@@ -10,12 +10,17 @@
|
||||
#include <string.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/kdf.h>
|
||||
#include <openssl/rand_drbg.h>
|
||||
#include "internal/cryptlib.h"
|
||||
#include "internal/nelem.h"
|
||||
#include "self_test.h"
|
||||
#include "self_test_data.inc"
|
||||
#include "../../crypto/rand/rand_local.h"
|
||||
|
||||
static int self_test_digest(const ST_KAT_DIGEST *t, OSSL_ST_EVENT *event,
|
||||
#define DRBG_PARAM_ENTROPY "DRBG-ENTROPY"
|
||||
#define DRBG_PARAM_NONCE "DRBG-NONCE"
|
||||
|
||||
static int self_test_digest(const ST_KAT_DIGEST *t, OSSL_SELF_TEST *st,
|
||||
OPENSSL_CTX *libctx)
|
||||
{
|
||||
int ok = 0;
|
||||
@@ -24,7 +29,7 @@ static int self_test_digest(const ST_KAT_DIGEST *t, OSSL_ST_EVENT *event,
|
||||
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
|
||||
EVP_MD *md = EVP_MD_fetch(libctx, t->algorithm, NULL);
|
||||
|
||||
SELF_TEST_EVENT_onbegin(event, OSSL_SELF_TEST_TYPE_KAT_DIGEST, t->desc);
|
||||
OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_DIGEST, t->desc);
|
||||
|
||||
if (ctx == NULL
|
||||
|| md == NULL
|
||||
@@ -34,14 +39,14 @@ static int self_test_digest(const ST_KAT_DIGEST *t, OSSL_ST_EVENT *event,
|
||||
goto err;
|
||||
|
||||
/* Optional corruption */
|
||||
SELF_TEST_EVENT_oncorrupt_byte(event, out);
|
||||
OSSL_SELF_TEST_oncorrupt_byte(st, out);
|
||||
|
||||
if (out_len != t->expected_len
|
||||
|| memcmp(out, t->expected, out_len) != 0)
|
||||
goto err;
|
||||
ok = 1;
|
||||
err:
|
||||
SELF_TEST_EVENT_onend(event, ok);
|
||||
OSSL_SELF_TEST_onend(st, ok);
|
||||
EVP_MD_free(md);
|
||||
EVP_MD_CTX_free(ctx);
|
||||
|
||||
@@ -81,7 +86,7 @@ static int cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
|
||||
}
|
||||
|
||||
/* Test a single KAT for encrypt/decrypt */
|
||||
static int self_test_cipher(const ST_KAT_CIPHER *t, OSSL_ST_EVENT *event,
|
||||
static int self_test_cipher(const ST_KAT_CIPHER *t, OSSL_SELF_TEST *st,
|
||||
OPENSSL_CTX *libctx)
|
||||
{
|
||||
int ret = 0, encrypt = 1, len, ct_len = 0, pt_len = 0;
|
||||
@@ -90,54 +95,54 @@ static int self_test_cipher(const ST_KAT_CIPHER *t, OSSL_ST_EVENT *event,
|
||||
unsigned char ct_buf[256] = { 0 };
|
||||
unsigned char pt_buf[256] = { 0 };
|
||||
|
||||
SELF_TEST_EVENT_onbegin(event, OSSL_SELF_TEST_TYPE_KAT_CIPHER, t->base.desc);
|
||||
OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_CIPHER, t->base.desc);
|
||||
|
||||
ctx = EVP_CIPHER_CTX_new();
|
||||
if (ctx == NULL)
|
||||
goto end;
|
||||
goto err;
|
||||
cipher = EVP_CIPHER_fetch(libctx, t->base.algorithm, "");
|
||||
if (cipher == NULL)
|
||||
goto end;
|
||||
goto err;
|
||||
|
||||
/* Encrypt plain text message */
|
||||
if (!cipher_init(ctx, cipher, t, encrypt)
|
||||
|| !EVP_CipherUpdate(ctx, ct_buf, &len, t->base.pt, t->base.pt_len)
|
||||
|| !EVP_CipherFinal_ex(ctx, ct_buf + len, &ct_len))
|
||||
goto end;
|
||||
goto err;
|
||||
|
||||
SELF_TEST_EVENT_oncorrupt_byte(event, ct_buf);
|
||||
OSSL_SELF_TEST_oncorrupt_byte(st, ct_buf);
|
||||
ct_len += len;
|
||||
if (ct_len != (int)t->base.expected_len
|
||||
|| memcmp(t->base.expected, ct_buf, ct_len) != 0)
|
||||
goto end;
|
||||
goto err;
|
||||
|
||||
if (t->tag != NULL) {
|
||||
unsigned char tag[16] = { 0 };
|
||||
|
||||
if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, t->tag_len, tag)
|
||||
|| memcmp(tag, t->tag, t->tag_len) != 0)
|
||||
goto end;
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!(cipher_init(ctx, cipher, t, !encrypt)
|
||||
&& EVP_CipherUpdate(ctx, pt_buf, &len, ct_buf, ct_len)
|
||||
&& EVP_CipherFinal_ex(ctx, pt_buf + len, &pt_len)))
|
||||
goto end;
|
||||
goto err;
|
||||
pt_len += len;
|
||||
|
||||
if (pt_len != (int)t->base.pt_len
|
||||
|| memcmp(pt_buf, t->base.pt, pt_len) != 0)
|
||||
goto end;
|
||||
goto err;
|
||||
|
||||
ret = 1;
|
||||
end:
|
||||
err:
|
||||
EVP_CIPHER_free(cipher);
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
SELF_TEST_EVENT_onend(event, ret);
|
||||
OSSL_SELF_TEST_onend(st, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int self_test_kdf(const ST_KAT_KDF *t, OSSL_ST_EVENT *event,
|
||||
static int self_test_kdf(const ST_KAT_KDF *t, OSSL_SELF_TEST *st,
|
||||
OPENSSL_CTX *libctx)
|
||||
{
|
||||
int ret = 0;
|
||||
@@ -149,7 +154,7 @@ static int self_test_kdf(const ST_KAT_KDF *t, OSSL_ST_EVENT *event,
|
||||
const OSSL_PARAM *settables = NULL;
|
||||
|
||||
numparams = OSSL_NELEM(params);
|
||||
SELF_TEST_EVENT_onbegin(event, OSSL_SELF_TEST_TYPE_KAT_KDF, t->desc);
|
||||
OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_KDF, t->desc);
|
||||
|
||||
/* Zeroize the params array to avoid mem leaks on error */
|
||||
for (i = 0; i < numparams; ++i)
|
||||
@@ -158,38 +163,140 @@ static int self_test_kdf(const ST_KAT_KDF *t, OSSL_ST_EVENT *event,
|
||||
kdf = EVP_KDF_fetch(libctx, t->algorithm, "");
|
||||
ctx = EVP_KDF_CTX_new(kdf);
|
||||
if (ctx == NULL)
|
||||
goto end;
|
||||
goto err;
|
||||
|
||||
settables = EVP_KDF_settable_ctx_params(kdf);
|
||||
for (i = 0; t->ctrls[i].name != NULL; ++i) {
|
||||
if (!ossl_assert(i < (numparams - 1)))
|
||||
goto end;
|
||||
goto err;
|
||||
if (!OSSL_PARAM_allocate_from_text(¶ms[i], settables,
|
||||
t->ctrls[i].name,
|
||||
t->ctrls[i].value,
|
||||
strlen(t->ctrls[i].value)))
|
||||
goto end;
|
||||
strlen(t->ctrls[i].value), NULL))
|
||||
goto err;
|
||||
}
|
||||
if (!EVP_KDF_CTX_set_params(ctx, params))
|
||||
goto end;
|
||||
goto err;
|
||||
|
||||
if (t->expected_len > sizeof(out))
|
||||
goto end;
|
||||
goto err;
|
||||
if (EVP_KDF_derive(ctx, out, t->expected_len) <= 0)
|
||||
goto end;
|
||||
goto err;
|
||||
|
||||
SELF_TEST_EVENT_oncorrupt_byte(event, out);
|
||||
OSSL_SELF_TEST_oncorrupt_byte(st, out);
|
||||
|
||||
if (memcmp(out, t->expected, t->expected_len) != 0)
|
||||
goto end;
|
||||
goto err;
|
||||
|
||||
ret = 1;
|
||||
end:
|
||||
err:
|
||||
for (i = 0; params[i].key != NULL; ++i)
|
||||
OPENSSL_free(params[i].data);
|
||||
EVP_KDF_free(kdf);
|
||||
EVP_KDF_CTX_free(ctx);
|
||||
SELF_TEST_EVENT_onend(event, ret);
|
||||
OSSL_SELF_TEST_onend(st, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static size_t drbg_kat_entropy_cb(RAND_DRBG *drbg, unsigned char **pout,
|
||||
int entropy, size_t min_len, size_t max_len,
|
||||
int prediction_resistance)
|
||||
{
|
||||
OSSL_PARAM *drbg_params = RAND_DRBG_get_callback_data(drbg);
|
||||
OSSL_PARAM *p = OSSL_PARAM_locate(drbg_params, DRBG_PARAM_ENTROPY);
|
||||
|
||||
if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
|
||||
return 0;
|
||||
*pout = (unsigned char *)p->data;
|
||||
return p->data_size;
|
||||
}
|
||||
|
||||
static size_t drbg_kat_nonce_cb(RAND_DRBG *drbg, unsigned char **pout,
|
||||
int entropy, size_t min_len, size_t max_len)
|
||||
{
|
||||
OSSL_PARAM *drbg_params = RAND_DRBG_get_callback_data(drbg);
|
||||
OSSL_PARAM *p = OSSL_PARAM_locate(drbg_params, DRBG_PARAM_NONCE);
|
||||
|
||||
if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
|
||||
return 0;
|
||||
*pout = (unsigned char *)p->data;
|
||||
return p->data_size;
|
||||
}
|
||||
|
||||
static int self_test_drbg(const ST_KAT_DRBG *t, OSSL_SELF_TEST *st,
|
||||
OPENSSL_CTX *libctx)
|
||||
{
|
||||
int ret = 0;
|
||||
unsigned char out[256];
|
||||
RAND_DRBG *drbg = NULL;
|
||||
unsigned int flags = 0;
|
||||
int prediction_resistance = 1; /* Causes a reseed */
|
||||
OSSL_PARAM drbg_params[3] = {
|
||||
OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
|
||||
};
|
||||
static const unsigned char zero[sizeof(drbg->data)] = { 0 };
|
||||
|
||||
OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_DRBG, t->desc);
|
||||
|
||||
if (strcmp(t->desc, OSSL_SELF_TEST_DESC_DRBG_HMAC) == 0)
|
||||
flags |= RAND_DRBG_FLAG_HMAC;
|
||||
|
||||
drbg = RAND_DRBG_new_ex(libctx, t->nid, flags, NULL);
|
||||
if (drbg == NULL)
|
||||
goto err;
|
||||
|
||||
if (!RAND_DRBG_set_callback_data(drbg, drbg_params))
|
||||
goto err;
|
||||
|
||||
if (!RAND_DRBG_set_callbacks(drbg, drbg_kat_entropy_cb, NULL,
|
||||
drbg_kat_nonce_cb, NULL))
|
||||
goto err;
|
||||
|
||||
drbg_params[0] =
|
||||
OSSL_PARAM_construct_octet_string(DRBG_PARAM_ENTROPY,
|
||||
(void *)t->entropyin, t->entropyinlen);
|
||||
drbg_params[1] =
|
||||
OSSL_PARAM_construct_octet_string(DRBG_PARAM_NONCE,
|
||||
(void *)t->nonce, t->noncelen);
|
||||
|
||||
if (!RAND_DRBG_instantiate(drbg, t->persstr, t->persstrlen))
|
||||
goto err;
|
||||
|
||||
drbg_params[0] =
|
||||
OSSL_PARAM_construct_octet_string(DRBG_PARAM_ENTROPY,
|
||||
(void *)t->entropyinpr1,
|
||||
t->entropyinpr1len);
|
||||
|
||||
if (!RAND_DRBG_generate(drbg, out, t->expectedlen, prediction_resistance,
|
||||
t->entropyaddin1, t->entropyaddin1len))
|
||||
goto err;
|
||||
|
||||
drbg_params[0] =
|
||||
OSSL_PARAM_construct_octet_string(DRBG_PARAM_ENTROPY,
|
||||
(void *)t->entropyinpr2,
|
||||
t->entropyinpr2len);
|
||||
/* This calls RAND_DRBG_reseed() internally when prediction_resistance = 1 */
|
||||
if (!RAND_DRBG_generate(drbg, out, t->expectedlen, prediction_resistance,
|
||||
t->entropyaddin2, t->entropyaddin2len))
|
||||
goto err;
|
||||
|
||||
OSSL_SELF_TEST_oncorrupt_byte(st, out);
|
||||
|
||||
if (memcmp(out, t->expected, t->expectedlen) != 0)
|
||||
goto err;
|
||||
|
||||
if (!RAND_DRBG_uninstantiate(drbg))
|
||||
goto err;
|
||||
/*
|
||||
* Check that the DRBG data has been zeroized after RAND_DRBG_uninstantiate.
|
||||
*/
|
||||
if (memcmp((unsigned char *)&drbg->data, zero, sizeof(drbg->data)) != 0)
|
||||
goto err;
|
||||
|
||||
ret = 1;
|
||||
err:
|
||||
RAND_DRBG_free(drbg);
|
||||
OSSL_SELF_TEST_onend(st, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -198,34 +305,45 @@ end:
|
||||
* All tests are run regardless of if they fail or not.
|
||||
* Return 0 if any test fails.
|
||||
*/
|
||||
static int self_test_digests(OSSL_ST_EVENT *event, OPENSSL_CTX *libctx)
|
||||
static int self_test_digests(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
|
||||
{
|
||||
int i, ret = 1;
|
||||
|
||||
for (i = 0; i < (int)OSSL_NELEM(st_kat_digest_tests); ++i) {
|
||||
if (!self_test_digest(&st_kat_digest_tests[i], event, libctx))
|
||||
if (!self_test_digest(&st_kat_digest_tests[i], st, libctx))
|
||||
ret = 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int self_test_ciphers(OSSL_ST_EVENT *event, OPENSSL_CTX *libctx)
|
||||
static int self_test_ciphers(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
|
||||
{
|
||||
int i, ret = 1;
|
||||
|
||||
for (i = 0; i < (int)OSSL_NELEM(st_kat_cipher_tests); ++i) {
|
||||
if (!self_test_cipher(&st_kat_cipher_tests[i], event, libctx))
|
||||
if (!self_test_cipher(&st_kat_cipher_tests[i], st, libctx))
|
||||
ret = 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int self_test_kdfs(OSSL_ST_EVENT *event, OPENSSL_CTX *libctx)
|
||||
static int self_test_kdfs(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
|
||||
{
|
||||
int i, ret = 1;
|
||||
|
||||
for (i = 0; i < (int)OSSL_NELEM(st_kat_kdf_tests); ++i) {
|
||||
if (!self_test_kdf(&st_kat_kdf_tests[i], event, libctx))
|
||||
if (!self_test_kdf(&st_kat_kdf_tests[i], st, libctx))
|
||||
ret = 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int self_test_drbgs(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
|
||||
{
|
||||
int i, ret = 1;
|
||||
|
||||
for (i = 0; i < (int)OSSL_NELEM(st_kat_drbg_tests); ++i) {
|
||||
if (!self_test_drbg(&st_kat_drbg_tests[i], st, libctx))
|
||||
ret = 0;
|
||||
}
|
||||
return ret;
|
||||
@@ -236,17 +354,19 @@ static int self_test_kdfs(OSSL_ST_EVENT *event, OPENSSL_CTX *libctx)
|
||||
* Return 1 is successful, otherwise return 0.
|
||||
* This runs all the tests regardless of if any fail.
|
||||
*
|
||||
* TODO(3.0) Add self tests for KA, DRBG, Sign/Verify when they become available
|
||||
* TODO(3.0) Add self tests for KA, Sign/Verify when they become available
|
||||
*/
|
||||
int SELF_TEST_kats(OSSL_ST_EVENT *event, OPENSSL_CTX *libctx)
|
||||
int SELF_TEST_kats(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
|
||||
{
|
||||
int ret = 1;
|
||||
|
||||
if (!self_test_digests(event, libctx))
|
||||
if (!self_test_digests(st, libctx))
|
||||
ret = 0;
|
||||
if (!self_test_ciphers(event, libctx))
|
||||
if (!self_test_ciphers(st, libctx))
|
||||
ret = 0;
|
||||
if (!self_test_kdfs(event, libctx))
|
||||
if (!self_test_kdfs(st, libctx))
|
||||
ret = 0;
|
||||
if (!self_test_drbgs(st, libctx))
|
||||
ret = 0;
|
||||
|
||||
return ret;
|
||||
|
||||
Reference in New Issue
Block a user