Add data driven SELF TEST code for signatures and key agreement
[openssl.git] / providers / fips / self_test_kats.c
index 128e2aa1182802b15e46dcc7583851e289f177b1..f02dbcd85cfad696d1ed7cd1b2d3aa0f989808fb 100644 (file)
@@ -11,6 +11,8 @@
 #include <openssl/evp.h>
 #include <openssl/kdf.h>
 #include <openssl/rand_drbg.h>
+#include <openssl/core_names.h>
+#include <openssl/param_build.h>
 #include "internal/cryptlib.h"
 #include "internal/nelem.h"
 #include "self_test.h"
@@ -20,7 +22,7 @@
 #define DRBG_PARAM_ENTROPY "DRBG-ENTROPY"
 #define DRBG_PARAM_NONCE   "DRBG-NONCE"
 
-static int self_test_digest(const ST_KAT_DIGEST *t, OSSL_ST_EVENT *event,
+static int self_test_digest(const ST_KAT_DIGEST *t, OSSL_SELF_TEST *st,
                             OPENSSL_CTX *libctx)
 {
     int ok = 0;
@@ -29,7 +31,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
@@ -39,17 +41,16 @@ 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);
     EVP_MD_free(md);
     EVP_MD_CTX_free(ctx);
-
+    OSSL_SELF_TEST_onend(st, ok);
     return ok;
 }
 
@@ -86,7 +87,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;
@@ -95,7 +96,7 @@ 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)
@@ -110,7 +111,7 @@ static int self_test_cipher(const ST_KAT_CIPHER *t, OSSL_ST_EVENT *event,
             || !EVP_CipherFinal_ex(ctx, ct_buf + len, &ct_len))
         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)
@@ -138,43 +139,83 @@ static int self_test_cipher(const ST_KAT_CIPHER *t, OSSL_ST_EVENT *event,
 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 add_params(OSSL_PARAM_BLD *bld, const ST_KAT_PARAM *params,
+                      BN_CTX *ctx)
+{
+    int ret = 0;
+    const ST_KAT_PARAM *p;
+
+    if (params == NULL)
+        return 1;
+    for (p = params; p->data != NULL; ++p)
+    {
+        switch (p->type) {
+        case OSSL_PARAM_UNSIGNED_INTEGER: {
+            BIGNUM *bn = BN_CTX_get(ctx);
+
+            if (bn == NULL
+                || (BN_bin2bn(p->data, p->data_len, bn) == NULL)
+                || !OSSL_PARAM_BLD_push_BN(bld, p->name, bn))
+                goto err;
+            break;
+        }
+        case OSSL_PARAM_UTF8_STRING: {
+            if (!OSSL_PARAM_BLD_push_utf8_string(bld, p->name, p->data, 0))
+                goto err;
+            break;
+        }
+        case OSSL_PARAM_OCTET_STRING: {
+            if (!OSSL_PARAM_BLD_push_octet_string(bld, p->name, p->data,
+                                                  p->data_len))
+                goto err;
+            break;
+        }
+        default:
+            break;
+        }
+    }
+    ret = 1;
+err:
+    return ret;
+}
+
+static int self_test_kdf(const ST_KAT_KDF *t, OSSL_SELF_TEST *st,
                          OPENSSL_CTX *libctx)
 {
     int ret = 0;
-    int i, numparams;
     unsigned char out[64];
     EVP_KDF *kdf = NULL;
     EVP_KDF_CTX *ctx = NULL;
-    OSSL_PARAM params[16];
-    const OSSL_PARAM *settables = NULL;
+    BN_CTX *bnctx = NULL;
+    OSSL_PARAM *params  = NULL;
+    OSSL_PARAM_BLD *bld = 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)
-        params[i] = OSSL_PARAM_construct_end();
+    bld = OSSL_PARAM_BLD_new();
+    if (bld == NULL)
+        goto err;
 
     kdf = EVP_KDF_fetch(libctx, t->algorithm, "");
+    if (kdf == NULL)
+        goto err;
+
     ctx = EVP_KDF_CTX_new(kdf);
     if (ctx == NULL)
         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 err;
-        if (!OSSL_PARAM_allocate_from_text(&params[i], settables,
-                                           t->ctrls[i].name,
-                                           t->ctrls[i].value,
-                                           strlen(t->ctrls[i].value), NULL))
-            goto err;
-    }
+    bnctx = BN_CTX_new_ex(libctx);
+    if (bnctx == NULL)
+        goto err;
+    if (!add_params(bld, t->params, bnctx))
+        goto err;
+    params = OSSL_PARAM_BLD_to_param(bld);
+    if (params == NULL)
+        goto err;
     if (!EVP_KDF_CTX_set_params(ctx, params))
         goto err;
 
@@ -183,18 +224,19 @@ static int self_test_kdf(const ST_KAT_KDF *t, OSSL_ST_EVENT *event,
     if (EVP_KDF_derive(ctx, out, t->expected_len) <= 0)
         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 err;
 
     ret = 1;
 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);
+    BN_CTX_free(bnctx);
+    OSSL_PARAM_BLD_free_params(params);
+    OSSL_PARAM_BLD_free(bld);
+    OSSL_SELF_TEST_onend(st, ret);
     return ret;
 }
 
@@ -223,7 +265,7 @@ static size_t drbg_kat_nonce_cb(RAND_DRBG *drbg, unsigned char **pout,
     return p->data_size;
 }
 
-static int self_test_drbg(const ST_KAT_DRBG *t, OSSL_ST_EVENT *event,
+static int self_test_drbg(const ST_KAT_DRBG *t, OSSL_SELF_TEST *st,
                           OPENSSL_CTX *libctx)
 {
     int ret = 0;
@@ -234,8 +276,9 @@ static int self_test_drbg(const ST_KAT_DRBG *t, OSSL_ST_EVENT *event,
     OSSL_PARAM drbg_params[3] = {
         OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
     };
+    static const unsigned char zero[sizeof(drbg->data)] = { 0 };
 
-    SELF_TEST_EVENT_onbegin(event, OSSL_SELF_TEST_TYPE_DRBG, t->desc);
+    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;
@@ -279,7 +322,7 @@ static int self_test_drbg(const ST_KAT_DRBG *t, OSSL_ST_EVENT *event,
                             t->entropyaddin2, t->entropyaddin2len))
         goto err;
 
-    SELF_TEST_EVENT_oncorrupt_byte(event, out);
+    OSSL_SELF_TEST_oncorrupt_byte(st, out);
 
     if (memcmp(out, t->expected, t->expectedlen) != 0)
         goto err;
@@ -287,24 +330,177 @@ static int self_test_drbg(const ST_KAT_DRBG *t, OSSL_ST_EVENT *event,
     if (!RAND_DRBG_uninstantiate(drbg))
         goto err;
     /*
-     * TODO(3.0) : Check that the DRBG data has been zeroed after
-     * RAND_DRBG_uninstantiate. Its a bit hard currently to do this when
-     * the drbg->data is reinitialized by this call..
+     * Check that the DRBG data has been zeroized after RAND_DRBG_uninstantiate.
      */
-#if 0
-    {
-        size_t i, sz = sizeof(drbg->data);
-        unsigned char *p = (unsigned char *)&drbg->data;
+    if (memcmp((unsigned char *)&drbg->data, zero, sizeof(drbg->data)) != 0)
+        goto err;
 
-        for (i = 0; i < sz; ++i)
-            if (*p++ != 0)
-                goto err;
-    }
-#endif
     ret = 1;
 err:
     RAND_DRBG_free(drbg);
-    SELF_TEST_EVENT_onend(event, ret);
+    OSSL_SELF_TEST_onend(st, ret);
+    return ret;
+}
+
+
+
+static int self_test_ka(const ST_KAT_KAS *t,
+                        OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
+{
+    int ret = 0;
+    EVP_PKEY_CTX *kactx = NULL, *dctx = NULL;
+    EVP_PKEY *pkey = NULL, *peerkey = NULL;
+    OSSL_PARAM *params = NULL;
+    OSSL_PARAM *params_peer = NULL;
+    unsigned char secret[256];
+    size_t secret_len;
+    OSSL_PARAM_BLD *bld = NULL;
+    BN_CTX *bnctx = NULL;
+
+    OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_KA, t->desc);
+
+    bnctx = BN_CTX_new_ex(libctx);
+    if (bnctx == NULL)
+        goto err;
+
+    bld = OSSL_PARAM_BLD_new();
+    if (bld == NULL)
+        goto err;
+
+    if (!add_params(bld, t->key_group, bnctx)
+        || !add_params(bld, t->key_host_data, bnctx))
+        goto err;
+    params = OSSL_PARAM_BLD_to_param(bld);
+
+    if (!add_params(bld, t->key_group, bnctx)
+        || !add_params(bld, t->key_peer_data, bnctx))
+        goto err;
+
+    params_peer = OSSL_PARAM_BLD_to_param(bld);
+    if (params == NULL || params_peer == NULL)
+        goto err;
+
+    /* Create a EVP_PKEY_CTX to load the DH keys into */
+    kactx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, "");
+    if (kactx == NULL)
+        goto err;
+    if (EVP_PKEY_key_fromdata_init(kactx) <= 0
+        || EVP_PKEY_fromdata(kactx, &pkey, params) <= 0)
+        goto err;
+    if (EVP_PKEY_key_fromdata_init(kactx) <= 0
+        || EVP_PKEY_fromdata(kactx, &peerkey, params_peer) <= 0)
+        goto err;
+
+    /* Create a EVP_PKEY_CTX to perform key derivation */
+    dctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL);
+    if (dctx == NULL)
+        goto err;
+
+    if (EVP_PKEY_derive_init(dctx) <= 0
+        || EVP_PKEY_derive_set_peer(dctx, peerkey) <= 0
+        || EVP_PKEY_derive(dctx, secret, &secret_len) <= 0)
+        goto err;
+
+    OSSL_SELF_TEST_oncorrupt_byte(st, secret);
+
+    if (secret_len != t->expected_len
+        || memcmp(secret, t->expected, t->expected_len) != 0)
+        goto err;
+    ret = 1;
+err:
+    BN_CTX_free(bnctx);
+    EVP_PKEY_free(pkey);
+    EVP_PKEY_free(peerkey);
+    EVP_PKEY_CTX_free(kactx);
+    EVP_PKEY_CTX_free(dctx);
+    OSSL_PARAM_BLD_free_params(params_peer);
+    OSSL_PARAM_BLD_free_params(params);
+    OSSL_PARAM_BLD_free(bld);
+    OSSL_SELF_TEST_onend(st, ret);
+    return ret;
+}
+
+static int self_test_sign(const ST_KAT_SIGN *t,
+                         OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
+{
+    int ret = 0;
+    OSSL_PARAM *params = NULL, *params_sig = NULL;
+    OSSL_PARAM_BLD *bld = NULL;
+    EVP_PKEY_CTX *sctx = NULL, *kctx = NULL;
+    EVP_PKEY *pkey = NULL;
+    unsigned char sig[256];
+    BN_CTX *bnctx = NULL;
+    size_t siglen = 0;
+    static const unsigned char dgst[] = {
+        0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
+        0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
+        0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
+    };
+
+    OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_SIGNATURE, t->desc);
+
+    bnctx = BN_CTX_new_ex(libctx);
+    if (bnctx == NULL)
+        goto err;
+
+    bld = OSSL_PARAM_BLD_new();
+    if (bld == NULL)
+        goto err;
+
+    if (!add_params(bld, t->key, bnctx))
+        goto err;
+    params = OSSL_PARAM_BLD_to_param(bld);
+
+    /* Create a EVP_PKEY_CTX to load the DSA key into */
+    kctx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, "");
+    if (kctx == NULL || params == NULL)
+        goto err;
+    if (EVP_PKEY_key_fromdata_init(kctx) <= 0
+        || EVP_PKEY_fromdata(kctx, &pkey, params) <= 0)
+        goto err;
+
+    /* Create a EVP_PKEY_CTX to use for the signing operation */
+    sctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL);
+    if (sctx == NULL
+        || EVP_PKEY_sign_init(sctx) <= 0)
+        goto err;
+
+    /* set signature parameters */
+    if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_SIGNATURE_PARAM_DIGEST,
+                                         t->mdalgorithm,
+                                         strlen(t->mdalgorithm) + 1))
+        goto err;
+    params_sig = OSSL_PARAM_BLD_to_param(bld);
+    if (EVP_PKEY_CTX_set_params(sctx, params_sig) <= 0)
+        goto err;
+
+    if (EVP_PKEY_sign(sctx, sig, &siglen, dgst, sizeof(dgst)) <= 0
+        || EVP_PKEY_verify_init(sctx) <= 0
+        || EVP_PKEY_CTX_set_params(sctx, params_sig) <= 0)
+        goto err;
+
+    /*
+     * Used by RSA, for other key types where the signature changes, we
+     * can only use the verify.
+     */
+    if (t->sig_expected != NULL
+        && (siglen != t->sig_expected_len
+            || memcmp(sig, t->sig_expected, t->sig_expected_len) != 0))
+        goto err;
+
+    OSSL_SELF_TEST_oncorrupt_byte(st, sig);
+    if (EVP_PKEY_verify(sctx, sig, siglen, dgst, sizeof(dgst)) <= 0)
+        goto err;
+    ret = 1;
+err:
+    BN_CTX_free(bnctx);
+    EVP_PKEY_free(pkey);
+    EVP_PKEY_CTX_free(kctx);
+    EVP_PKEY_CTX_free(sctx);
+    OSSL_PARAM_BLD_free_params(params);
+    OSSL_PARAM_BLD_free_params(params_sig);
+    OSSL_PARAM_BLD_free(bld);
+    OSSL_SELF_TEST_onend(st, ret);
     return ret;
 }
 
@@ -313,45 +509,67 @@ err:
  * 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_ST_EVENT *event, OPENSSL_CTX *libctx)
+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], event, libctx))
+        if (!self_test_drbg(&st_kat_drbg_tests[i], st, libctx))
+            ret = 0;
+    }
+    return ret;
+}
+
+static int self_test_kas(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
+{
+    int i, ret = 1;
+
+    for (i = 0; i < (int)OSSL_NELEM(st_kat_kas_tests); ++i) {
+        if (!self_test_ka(&st_kat_kas_tests[i], st, libctx))
+            ret = 0;
+    }
+    return ret;
+}
+
+static int self_test_signatures(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
+{
+    int i, ret = 1;
+
+    for (i = 0; i < (int)OSSL_NELEM(st_kat_sign_tests); ++i) {
+        if (!self_test_sign(&st_kat_sign_tests[i], st, libctx))
             ret = 0;
     }
     return ret;
@@ -361,20 +579,22 @@ static int self_test_drbgs(OSSL_ST_EVENT *event, OPENSSL_CTX *libctx)
  * Run the algorithm KAT's.
  * 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, 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(st, libctx))
+        ret = 0;
+    if (!self_test_signatures(st, libctx))
         ret = 0;
-    if (!self_test_ciphers(event, libctx))
+    if (!self_test_kdfs(st, libctx))
         ret = 0;
-    if (!self_test_kdfs(event, libctx))
+    if (!self_test_drbgs(st, libctx))
         ret = 0;
-    if (!self_test_drbgs(event, libctx))
+    if (!self_test_kas(st, libctx))
         ret = 0;
 
     return ret;