Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6      1.1  christos  * in the file LICENSE in the source distribution or at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  */
      9      1.1  christos 
     10      1.1  christos #include <string.h> /* memset */
     11      1.1  christos #include <openssl/evp.h>
     12      1.1  christos #include <openssl/pem.h>
     13      1.1  christos #include <openssl/encoder.h>
     14      1.1  christos #include <openssl/provider.h>
     15      1.1  christos #include <openssl/param_build.h>
     16      1.1  christos #include <openssl/core_names.h>
     17      1.1  christos #include <openssl/sha.h>
     18      1.1  christos #include "crypto/ecx.h"
     19  1.1.1.2  christos #include "crypto/evp.h" /* For the internal API */
     20  1.1.1.2  christos #include "crypto/bn_dh.h" /* _bignum_ffdhe2048_p */
     21      1.1  christos #include "internal/nelem.h"
     22      1.1  christos #include "testutil.h"
     23      1.1  christos 
     24      1.1  christos static char *datadir = NULL;
     25      1.1  christos 
     26      1.1  christos /*
     27      1.1  christos  * Do not change the order of the following defines unless you also
     28      1.1  christos  * update the for loop bounds used inside test_print_key_using_encoder() and
     29      1.1  christos  * test_print_key_using_encoder_public().
     30      1.1  christos  */
     31  1.1.1.2  christos #define PRIV_TEXT 0
     32  1.1.1.2  christos #define PRIV_PEM 1
     33  1.1.1.2  christos #define PRIV_DER 2
     34  1.1.1.2  christos #define PUB_TEXT 3
     35  1.1.1.2  christos #define PUB_PEM 4
     36  1.1.1.2  christos #define PUB_DER 5
     37      1.1  christos 
     38      1.1  christos static void stripcr(char *buf, size_t *len)
     39      1.1  christos {
     40      1.1  christos     size_t i;
     41      1.1  christos     char *curr, *writ;
     42      1.1  christos 
     43      1.1  christos     for (i = *len, curr = buf, writ = buf; i > 0; i--, curr++) {
     44      1.1  christos         if (*curr == '\r') {
     45      1.1  christos             (*len)--;
     46      1.1  christos             continue;
     47      1.1  christos         }
     48      1.1  christos         if (curr != writ)
     49      1.1  christos             *writ = *curr;
     50      1.1  christos         writ++;
     51      1.1  christos     }
     52      1.1  christos }
     53      1.1  christos 
     54      1.1  christos static int compare_with_file(const char *alg, int type, BIO *membio)
     55      1.1  christos {
     56      1.1  christos     char filename[80];
     57      1.1  christos     BIO *file = NULL;
     58      1.1  christos     char buf[4096];
     59      1.1  christos     char *memdata, *fullfile = NULL;
     60      1.1  christos     const char *suffix;
     61      1.1  christos     size_t readbytes;
     62      1.1  christos     int ret = 0;
     63      1.1  christos     int len;
     64      1.1  christos     size_t slen;
     65      1.1  christos 
     66      1.1  christos     switch (type) {
     67      1.1  christos     case PRIV_TEXT:
     68      1.1  christos         suffix = "priv.txt";
     69      1.1  christos         break;
     70      1.1  christos 
     71      1.1  christos     case PRIV_PEM:
     72      1.1  christos         suffix = "priv.pem";
     73      1.1  christos         break;
     74      1.1  christos 
     75      1.1  christos     case PRIV_DER:
     76      1.1  christos         suffix = "priv.der";
     77      1.1  christos         break;
     78      1.1  christos 
     79      1.1  christos     case PUB_TEXT:
     80      1.1  christos         suffix = "pub.txt";
     81      1.1  christos         break;
     82      1.1  christos 
     83      1.1  christos     case PUB_PEM:
     84      1.1  christos         suffix = "pub.pem";
     85      1.1  christos         break;
     86      1.1  christos 
     87      1.1  christos     case PUB_DER:
     88      1.1  christos         suffix = "pub.der";
     89      1.1  christos         break;
     90      1.1  christos 
     91      1.1  christos     default:
     92      1.1  christos         TEST_error("Invalid file type");
     93      1.1  christos         goto err;
     94      1.1  christos     }
     95      1.1  christos 
     96      1.1  christos     BIO_snprintf(filename, sizeof(filename), "%s.%s", alg, suffix);
     97      1.1  christos     fullfile = test_mk_file_path(datadir, filename);
     98      1.1  christos     if (!TEST_ptr(fullfile))
     99      1.1  christos         goto err;
    100      1.1  christos 
    101      1.1  christos     file = BIO_new_file(fullfile, "rb");
    102      1.1  christos     if (!TEST_ptr(file))
    103      1.1  christos         goto err;
    104      1.1  christos 
    105      1.1  christos     if (!TEST_true(BIO_read_ex(file, buf, sizeof(buf), &readbytes))
    106  1.1.1.2  christos         || !TEST_true(BIO_eof(file))
    107  1.1.1.2  christos         || !TEST_size_t_lt(readbytes, sizeof(buf)))
    108      1.1  christos         goto err;
    109      1.1  christos 
    110      1.1  christos     len = BIO_get_mem_data(membio, &memdata);
    111      1.1  christos     if (!TEST_int_gt(len, 0))
    112      1.1  christos         goto err;
    113      1.1  christos 
    114      1.1  christos     slen = len;
    115      1.1  christos     if (type != PRIV_DER && type != PUB_DER) {
    116      1.1  christos         stripcr(memdata, &slen);
    117      1.1  christos         stripcr(buf, &readbytes);
    118      1.1  christos     }
    119      1.1  christos 
    120      1.1  christos     if (!TEST_mem_eq(memdata, slen, buf, readbytes))
    121      1.1  christos         goto err;
    122      1.1  christos 
    123      1.1  christos     ret = 1;
    124  1.1.1.2  christos err:
    125      1.1  christos     OPENSSL_free(fullfile);
    126      1.1  christos     (void)BIO_reset(membio);
    127      1.1  christos     BIO_free(file);
    128      1.1  christos     return ret;
    129      1.1  christos }
    130      1.1  christos 
    131      1.1  christos static int pass_cb(char *buf, int size, int rwflag, void *u)
    132      1.1  christos {
    133      1.1  christos     return 0;
    134      1.1  christos }
    135      1.1  christos 
    136      1.1  christos static int pass_cb_error(char *buf, int size, int rwflag, void *u)
    137      1.1  christos {
    138      1.1  christos     return -1;
    139      1.1  christos }
    140      1.1  christos 
    141      1.1  christos static int test_print_key_using_pem(const char *alg, const EVP_PKEY *pk)
    142      1.1  christos {
    143      1.1  christos     BIO *membio = BIO_new(BIO_s_mem());
    144      1.1  christos     int ret = 0;
    145      1.1  christos 
    146      1.1  christos     if (!TEST_ptr(membio))
    147      1.1  christos         goto err;
    148      1.1  christos 
    149      1.1  christos     if (/* Output Encrypted private key in PEM form */
    150      1.1  christos         !TEST_true(PEM_write_bio_PrivateKey(bio_out, pk, EVP_aes_256_cbc(),
    151  1.1.1.2  christos             (unsigned char *)"pass", 4,
    152  1.1.1.2  christos             NULL, NULL))
    153      1.1  christos         /* Output zero-length passphrase encrypted private key in PEM form */
    154      1.1  christos         || !TEST_true(PEM_write_bio_PKCS8PrivateKey(bio_out, pk,
    155  1.1.1.2  christos             EVP_aes_256_cbc(),
    156  1.1.1.2  christos             (const char *)~0, 0,
    157  1.1.1.2  christos             NULL, NULL))
    158      1.1  christos         || !TEST_true(PEM_write_bio_PKCS8PrivateKey(bio_out, pk,
    159  1.1.1.2  christos             EVP_aes_256_cbc(),
    160  1.1.1.2  christos             NULL, 0, NULL, ""))
    161      1.1  christos         || !TEST_true(PEM_write_bio_PKCS8PrivateKey(bio_out, pk,
    162  1.1.1.2  christos             EVP_aes_256_cbc(),
    163  1.1.1.2  christos             NULL, 0, pass_cb, NULL))
    164      1.1  christos         || !TEST_false(PEM_write_bio_PKCS8PrivateKey(bio_out, pk,
    165  1.1.1.2  christos             EVP_aes_256_cbc(),
    166  1.1.1.2  christos             NULL, 0, pass_cb_error,
    167  1.1.1.2  christos             NULL))
    168      1.1  christos #ifndef OPENSSL_NO_DES
    169      1.1  christos         || !TEST_true(PEM_write_bio_PKCS8PrivateKey_nid(
    170      1.1  christos             bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
    171      1.1  christos             (const char *)~0, 0, NULL, NULL))
    172      1.1  christos         || !TEST_true(PEM_write_bio_PKCS8PrivateKey_nid(
    173      1.1  christos             bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NULL, 0,
    174      1.1  christos             NULL, ""))
    175      1.1  christos         || !TEST_true(PEM_write_bio_PKCS8PrivateKey_nid(
    176      1.1  christos             bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NULL, 0,
    177      1.1  christos             pass_cb, NULL))
    178      1.1  christos         || !TEST_false(PEM_write_bio_PKCS8PrivateKey_nid(
    179      1.1  christos             bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NULL, 0,
    180      1.1  christos             pass_cb_error, NULL))
    181      1.1  christos #endif
    182      1.1  christos         /* Private key in text form */
    183      1.1  christos         || !TEST_int_gt(EVP_PKEY_print_private(membio, pk, 0, NULL), 0)
    184      1.1  christos         || !TEST_true(compare_with_file(alg, PRIV_TEXT, membio))
    185      1.1  christos         /* Public key in PEM form */
    186      1.1  christos         || !TEST_true(PEM_write_bio_PUBKEY(membio, pk))
    187      1.1  christos         || !TEST_true(compare_with_file(alg, PUB_PEM, membio))
    188      1.1  christos         /* Unencrypted private key in PEM form */
    189      1.1  christos         || !TEST_true(PEM_write_bio_PrivateKey(membio, pk,
    190  1.1.1.2  christos             NULL, NULL, 0, NULL, NULL))
    191      1.1  christos         || !TEST_true(compare_with_file(alg, PRIV_PEM, membio))
    192      1.1  christos         /* NULL key */
    193      1.1  christos         || !TEST_false(PEM_write_bio_PrivateKey(membio, NULL,
    194  1.1.1.2  christos             NULL, NULL, 0, NULL, NULL))
    195      1.1  christos         || !TEST_false(PEM_write_bio_PrivateKey_traditional(membio, NULL,
    196  1.1.1.2  christos             NULL, NULL, 0, NULL, NULL)))
    197      1.1  christos         goto err;
    198      1.1  christos 
    199      1.1  christos     ret = 1;
    200  1.1.1.2  christos err:
    201      1.1  christos     BIO_free(membio);
    202      1.1  christos     return ret;
    203      1.1  christos }
    204      1.1  christos 
    205      1.1  christos static int test_print_key_type_using_encoder(const char *alg, int type,
    206  1.1.1.2  christos     const EVP_PKEY *pk)
    207      1.1  christos {
    208      1.1  christos     const char *output_type, *output_structure;
    209      1.1  christos     int selection;
    210      1.1  christos     OSSL_ENCODER_CTX *ctx = NULL;
    211      1.1  christos     BIO *membio = BIO_new(BIO_s_mem());
    212      1.1  christos     int ret = 0;
    213      1.1  christos 
    214      1.1  christos     switch (type) {
    215      1.1  christos     case PRIV_TEXT:
    216      1.1  christos         output_type = "TEXT";
    217      1.1  christos         output_structure = NULL;
    218      1.1  christos         selection = OSSL_KEYMGMT_SELECT_KEYPAIR
    219      1.1  christos             | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
    220      1.1  christos         break;
    221      1.1  christos 
    222      1.1  christos     case PRIV_PEM:
    223      1.1  christos         output_type = "PEM";
    224      1.1  christos         output_structure = "PrivateKeyInfo";
    225      1.1  christos         selection = OSSL_KEYMGMT_SELECT_KEYPAIR
    226      1.1  christos             | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
    227      1.1  christos         break;
    228      1.1  christos 
    229      1.1  christos     case PRIV_DER:
    230      1.1  christos         output_type = "DER";
    231      1.1  christos         output_structure = "PrivateKeyInfo";
    232      1.1  christos         selection = OSSL_KEYMGMT_SELECT_KEYPAIR
    233      1.1  christos             | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
    234      1.1  christos         break;
    235      1.1  christos 
    236      1.1  christos     case PUB_TEXT:
    237      1.1  christos         output_type = "TEXT";
    238      1.1  christos         output_structure = NULL;
    239      1.1  christos         selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY
    240      1.1  christos             | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
    241      1.1  christos         break;
    242      1.1  christos 
    243      1.1  christos     case PUB_PEM:
    244      1.1  christos         output_type = "PEM";
    245      1.1  christos         output_structure = "SubjectPublicKeyInfo";
    246      1.1  christos         selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY
    247      1.1  christos             | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
    248      1.1  christos         break;
    249      1.1  christos 
    250      1.1  christos     case PUB_DER:
    251      1.1  christos         output_type = "DER";
    252      1.1  christos         output_structure = "SubjectPublicKeyInfo";
    253      1.1  christos         selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY
    254      1.1  christos             | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
    255      1.1  christos         break;
    256      1.1  christos 
    257      1.1  christos     default:
    258      1.1  christos         TEST_error("Invalid encoding type");
    259      1.1  christos         goto err;
    260      1.1  christos     }
    261      1.1  christos 
    262      1.1  christos     if (!TEST_ptr(membio))
    263      1.1  christos         goto err;
    264      1.1  christos 
    265      1.1  christos     /* Make a context, it's valid for several prints */
    266      1.1  christos     TEST_note("Setting up a OSSL_ENCODER context with passphrase");
    267      1.1  christos     if (!TEST_ptr(ctx = OSSL_ENCODER_CTX_new_for_pkey(pk, selection,
    268  1.1.1.2  christos                       output_type,
    269  1.1.1.2  christos                       output_structure,
    270  1.1.1.2  christos                       NULL))
    271      1.1  christos         /* Check that this operation is supported */
    272      1.1  christos         || !TEST_int_ne(OSSL_ENCODER_CTX_get_num_encoders(ctx), 0))
    273      1.1  christos         goto err;
    274      1.1  christos 
    275      1.1  christos     /* Use no cipher.  This should give us an unencrypted PEM */
    276      1.1  christos     TEST_note("Testing with no encryption");
    277      1.1  christos     if (!TEST_true(OSSL_ENCODER_to_bio(ctx, membio))
    278      1.1  christos         || !TEST_true(compare_with_file(alg, type, membio)))
    279      1.1  christos         goto err;
    280      1.1  christos 
    281      1.1  christos     if (type == PRIV_PEM) {
    282      1.1  christos         /* Set a passphrase to be used later */
    283      1.1  christos         if (!TEST_true(OSSL_ENCODER_CTX_set_passphrase(ctx,
    284  1.1.1.2  christos                 (unsigned char *)"pass",
    285  1.1.1.2  christos                 4)))
    286      1.1  christos             goto err;
    287      1.1  christos 
    288      1.1  christos         /* Use a valid cipher name */
    289      1.1  christos         TEST_note("Displaying PEM encrypted with AES-256-CBC");
    290      1.1  christos         if (!TEST_true(OSSL_ENCODER_CTX_set_cipher(ctx, "AES-256-CBC", NULL))
    291      1.1  christos             || !TEST_true(OSSL_ENCODER_to_bio(ctx, bio_out)))
    292      1.1  christos             goto err;
    293      1.1  christos 
    294      1.1  christos         /* Use an invalid cipher name, which should generate no output */
    295      1.1  christos         TEST_note("NOT Displaying PEM encrypted with (invalid) FOO");
    296      1.1  christos         if (!TEST_false(OSSL_ENCODER_CTX_set_cipher(ctx, "FOO", NULL))
    297      1.1  christos             || !TEST_false(OSSL_ENCODER_to_bio(ctx, bio_out)))
    298      1.1  christos             goto err;
    299      1.1  christos 
    300      1.1  christos         /* Clear the cipher.  This should give us an unencrypted PEM again */
    301      1.1  christos         TEST_note("Testing with encryption cleared (no encryption)");
    302      1.1  christos         if (!TEST_true(OSSL_ENCODER_CTX_set_cipher(ctx, NULL, NULL))
    303      1.1  christos             || !TEST_true(OSSL_ENCODER_to_bio(ctx, membio))
    304      1.1  christos             || !TEST_true(compare_with_file(alg, type, membio)))
    305      1.1  christos             goto err;
    306      1.1  christos     }
    307      1.1  christos     ret = 1;
    308      1.1  christos err:
    309      1.1  christos     BIO_free(membio);
    310      1.1  christos     OSSL_ENCODER_CTX_free(ctx);
    311      1.1  christos     return ret;
    312      1.1  christos }
    313      1.1  christos 
    314      1.1  christos static int test_print_key_using_encoder(const char *alg, const EVP_PKEY *pk)
    315      1.1  christos {
    316      1.1  christos     int i;
    317      1.1  christos     int ret = 1;
    318      1.1  christos 
    319      1.1  christos     for (i = PRIV_TEXT; i <= PUB_DER; i++)
    320      1.1  christos         ret = ret && test_print_key_type_using_encoder(alg, i, pk);
    321      1.1  christos 
    322      1.1  christos     return ret;
    323      1.1  christos }
    324      1.1  christos 
    325      1.1  christos #ifndef OPENSSL_NO_ECX
    326      1.1  christos static int test_print_key_using_encoder_public(const char *alg,
    327  1.1.1.2  christos     const EVP_PKEY *pk)
    328      1.1  christos {
    329      1.1  christos     int i;
    330      1.1  christos     int ret = 1;
    331      1.1  christos 
    332      1.1  christos     for (i = PUB_TEXT; i <= PUB_DER; i++)
    333      1.1  christos         ret = ret && test_print_key_type_using_encoder(alg, i, pk);
    334      1.1  christos 
    335      1.1  christos     return ret;
    336      1.1  christos }
    337      1.1  christos #endif
    338      1.1  christos 
    339      1.1  christos /* Array indexes used in test_fromdata_rsa */
    340  1.1.1.2  christos #define N 0
    341  1.1.1.2  christos #define E 1
    342  1.1.1.2  christos #define D 2
    343  1.1.1.2  christos #define P 3
    344  1.1.1.2  christos #define Q 4
    345  1.1.1.2  christos #define DP 5
    346  1.1.1.2  christos #define DQ 6
    347  1.1.1.2  christos #define QINV 7
    348      1.1  christos 
    349      1.1  christos static int test_fromdata_rsa(void)
    350      1.1  christos {
    351      1.1  christos     int ret = 0, i;
    352      1.1  christos     EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
    353      1.1  christos     EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL;
    354      1.1  christos     /*
    355      1.1  christos      * 32-bit RSA key, extracted from this command,
    356      1.1  christos      * executed with OpenSSL 1.0.2:
    357      1.1  christos      *
    358      1.1  christos      * openssl genrsa 32 | openssl rsa -text
    359      1.1  christos      */
    360      1.1  christos     static unsigned long key_numbers[] = {
    361  1.1.1.2  christos         0xbc747fc5, /* N */
    362  1.1.1.2  christos         0x10001, /* E */
    363  1.1.1.2  christos         0x7b133399, /* D */
    364  1.1.1.2  christos         0xe963, /* P */
    365  1.1.1.2  christos         0xceb7, /* Q */
    366  1.1.1.2  christos         0x8599, /* DP */
    367  1.1.1.2  christos         0xbd87, /* DQ */
    368  1.1.1.2  christos         0xcc3b, /* QINV */
    369      1.1  christos     };
    370      1.1  christos     OSSL_PARAM fromdata_params[] = {
    371      1.1  christos         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_N, &key_numbers[N]),
    372      1.1  christos         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_E, &key_numbers[E]),
    373      1.1  christos         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_D, &key_numbers[D]),
    374      1.1  christos         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR1, &key_numbers[P]),
    375      1.1  christos         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR2, &key_numbers[Q]),
    376      1.1  christos         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT1, &key_numbers[DP]),
    377      1.1  christos         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT2, &key_numbers[DQ]),
    378      1.1  christos         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, &key_numbers[QINV]),
    379      1.1  christos         OSSL_PARAM_END
    380      1.1  christos     };
    381      1.1  christos     BIGNUM *bn = BN_new();
    382      1.1  christos     BIGNUM *bn_from = BN_new();
    383      1.1  christos 
    384      1.1  christos     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL)))
    385      1.1  christos         goto err;
    386      1.1  christos 
    387      1.1  christos     if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
    388      1.1  christos         || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
    389  1.1.1.2  christos                             fromdata_params),
    390  1.1.1.2  christos             1))
    391      1.1  christos         goto err;
    392      1.1  christos 
    393      1.1  christos     for (;;) {
    394      1.1  christos         ret = 0;
    395      1.1  christos         if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 32)
    396      1.1  christos             || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 8)
    397      1.1  christos             || !TEST_int_eq(EVP_PKEY_get_size(pk), 4)
    398      1.1  christos             || !TEST_false(EVP_PKEY_missing_parameters(pk)))
    399      1.1  christos             goto err;
    400      1.1  christos 
    401      1.1  christos         EVP_PKEY_CTX_free(key_ctx);
    402      1.1  christos         if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
    403      1.1  christos             goto err;
    404      1.1  christos 
    405      1.1  christos         if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0)
    406      1.1  christos             || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0)
    407      1.1  christos             || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0)
    408      1.1  christos             || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0))
    409      1.1  christos             goto err;
    410      1.1  christos 
    411      1.1  christos         /* EVP_PKEY_copy_parameters() should fail for RSA */
    412      1.1  christos         if (!TEST_ptr(copy_pk = EVP_PKEY_new())
    413      1.1  christos             || !TEST_false(EVP_PKEY_copy_parameters(copy_pk, pk)))
    414      1.1  christos             goto err;
    415      1.1  christos         EVP_PKEY_free(copy_pk);
    416      1.1  christos         copy_pk = NULL;
    417      1.1  christos 
    418      1.1  christos         ret = test_print_key_using_pem("RSA", pk)
    419  1.1.1.2  christos             && test_print_key_using_encoder("RSA", pk);
    420      1.1  christos 
    421      1.1  christos         if (!ret || dup_pk != NULL)
    422      1.1  christos             break;
    423      1.1  christos 
    424      1.1  christos         if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
    425      1.1  christos             goto err;
    426      1.1  christos         ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
    427      1.1  christos         EVP_PKEY_free(pk);
    428      1.1  christos         pk = dup_pk;
    429      1.1  christos         if (!ret)
    430      1.1  christos             goto err;
    431      1.1  christos     }
    432  1.1.1.2  christos err:
    433      1.1  christos     /* for better diagnostics always compare key params */
    434      1.1  christos     for (i = 0; fromdata_params[i].key != NULL; ++i) {
    435      1.1  christos         if (!TEST_true(BN_set_word(bn_from, key_numbers[i]))
    436      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, fromdata_params[i].key,
    437  1.1.1.2  christos                 &bn))
    438      1.1  christos             || !TEST_BN_eq(bn, bn_from))
    439      1.1  christos             ret = 0;
    440      1.1  christos     }
    441      1.1  christos     BN_free(bn_from);
    442      1.1  christos     BN_free(bn);
    443      1.1  christos     EVP_PKEY_free(pk);
    444      1.1  christos     EVP_PKEY_free(copy_pk);
    445      1.1  christos     EVP_PKEY_CTX_free(key_ctx);
    446      1.1  christos     EVP_PKEY_CTX_free(ctx);
    447      1.1  christos 
    448      1.1  christos     return ret;
    449      1.1  christos }
    450      1.1  christos 
    451      1.1  christos struct check_data {
    452      1.1  christos     const char *pname;
    453      1.1  christos     BIGNUM *comparebn;
    454      1.1  christos };
    455      1.1  christos 
    456      1.1  christos static int do_fromdata_rsa_derive(OSSL_PARAM *fromdata_params,
    457  1.1.1.2  christos     struct check_data check[],
    458  1.1.1.2  christos     int expected_nbits, int expected_sbits,
    459  1.1.1.2  christos     int expected_ksize)
    460      1.1  christos {
    461      1.1  christos     const OSSL_PARAM *check_param = NULL;
    462      1.1  christos     BIGNUM *check_bn = NULL;
    463      1.1  christos     OSSL_PARAM *todata_params = NULL;
    464      1.1  christos     EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
    465      1.1  christos     EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL;
    466      1.1  christos     int i;
    467      1.1  christos     int ret = 0;
    468      1.1  christos 
    469      1.1  christos     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL))
    470      1.1  christos         || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
    471      1.1  christos         || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
    472  1.1.1.2  christos                             fromdata_params),
    473  1.1.1.2  christos             1))
    474      1.1  christos         goto err;
    475      1.1  christos 
    476      1.1  christos     /*
    477      1.1  christos      * get the generated key parameters back and validate that the
    478      1.1  christos      * exponents/coeffs are correct
    479      1.1  christos      */
    480      1.1  christos     if (!TEST_int_eq(EVP_PKEY_todata(pk, EVP_PKEY_KEYPAIR, &todata_params), 1))
    481      1.1  christos         goto err;
    482      1.1  christos 
    483      1.1  christos     for (i = 0; check[i].pname != NULL; i++) {
    484      1.1  christos         if (!TEST_ptr(check_param = OSSL_PARAM_locate_const(todata_params,
    485  1.1.1.2  christos                           check[i].pname)))
    486      1.1  christos             goto err;
    487      1.1  christos         if (!TEST_int_eq(OSSL_PARAM_get_BN(check_param, &check_bn), 1))
    488      1.1  christos             goto err;
    489      1.1  christos         if (!TEST_BN_eq(check_bn, check[i].comparebn)) {
    490      1.1  christos             TEST_info("Data mismatch for parameter %s", check[i].pname);
    491      1.1  christos             goto err;
    492      1.1  christos         }
    493      1.1  christos         BN_free(check_bn);
    494      1.1  christos         check_bn = NULL;
    495      1.1  christos     }
    496      1.1  christos 
    497      1.1  christos     for (;;) {
    498      1.1  christos         if (!TEST_int_eq(EVP_PKEY_get_bits(pk), expected_nbits)
    499      1.1  christos             || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), expected_sbits)
    500      1.1  christos             || !TEST_int_eq(EVP_PKEY_get_size(pk), expected_ksize)
    501      1.1  christos             || !TEST_false(EVP_PKEY_missing_parameters(pk)))
    502      1.1  christos             goto err;
    503      1.1  christos 
    504      1.1  christos         EVP_PKEY_CTX_free(key_ctx);
    505      1.1  christos         if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
    506      1.1  christos             goto err;
    507      1.1  christos 
    508      1.1  christos         if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0)
    509      1.1  christos             || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0)
    510      1.1  christos             || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0)
    511      1.1  christos             || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0))
    512      1.1  christos             goto err;
    513      1.1  christos 
    514      1.1  christos         /* EVP_PKEY_copy_parameters() should fail for RSA */
    515      1.1  christos         if (!TEST_ptr(copy_pk = EVP_PKEY_new())
    516      1.1  christos             || !TEST_false(EVP_PKEY_copy_parameters(copy_pk, pk)))
    517      1.1  christos             goto err;
    518      1.1  christos         EVP_PKEY_free(copy_pk);
    519      1.1  christos         copy_pk = NULL;
    520      1.1  christos 
    521      1.1  christos         if (dup_pk != NULL)
    522      1.1  christos             break;
    523      1.1  christos 
    524      1.1  christos         if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
    525      1.1  christos             goto err;
    526      1.1  christos         if (!TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1)) {
    527      1.1  christos             EVP_PKEY_free(dup_pk);
    528      1.1  christos             goto err;
    529      1.1  christos         }
    530      1.1  christos         EVP_PKEY_free(pk);
    531      1.1  christos         pk = dup_pk;
    532      1.1  christos     }
    533      1.1  christos     ret = 1;
    534      1.1  christos err:
    535      1.1  christos     BN_free(check_bn);
    536      1.1  christos     EVP_PKEY_free(pk);
    537      1.1  christos     EVP_PKEY_CTX_free(ctx);
    538      1.1  christos     EVP_PKEY_CTX_free(key_ctx);
    539      1.1  christos     OSSL_PARAM_free(fromdata_params);
    540      1.1  christos     OSSL_PARAM_free(todata_params);
    541      1.1  christos     return ret;
    542      1.1  christos }
    543      1.1  christos 
    544      1.1  christos static int test_fromdata_rsa_derive_from_pq_sp800(void)
    545      1.1  christos {
    546      1.1  christos     OSSL_PARAM_BLD *bld = NULL;
    547      1.1  christos     BIGNUM *n = NULL, *e = NULL, *d = NULL, *p = NULL, *q = NULL;
    548      1.1  christos     BIGNUM *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
    549      1.1  christos     OSSL_PARAM *fromdata_params = NULL;
    550      1.1  christos     struct check_data cdata[4];
    551      1.1  christos     int ret = 0;
    552      1.1  christos     /*
    553      1.1  christos      * 512-bit RSA key, extracted from this command,
    554      1.1  christos      * openssl genrsa 512 | openssl rsa -text
    555      1.1  christos      * Note: When generating a key with EVP_PKEY_fromdata, and using
    556      1.1  christos      * crt derivation, openssl requires a minimum of 512 bits of n data,
    557      1.1  christos      * and 2048 bits in the FIPS case
    558      1.1  christos      */
    559  1.1.1.2  christos     static unsigned char n_data[] = { 0x00, 0xc7, 0x06, 0xd8, 0x6b, 0x3c, 0x4f, 0xb7, 0x95, 0x42, 0x44, 0x90,
    560  1.1.1.2  christos         0xbd, 0xef, 0xf3, 0xc4, 0xb5, 0xa8, 0x55, 0x9e, 0x33, 0xa3, 0x04, 0x3a,
    561  1.1.1.2  christos         0x90, 0xe5, 0x13, 0xff, 0x87, 0x69, 0x15, 0xa4, 0x8a, 0x17, 0x10, 0xcc,
    562  1.1.1.2  christos         0xdf, 0xf9, 0xc5, 0x0f, 0xf1, 0x12, 0xff, 0x12, 0x11, 0xe5, 0x6b, 0x5c,
    563  1.1.1.2  christos         0x83, 0xd9, 0x43, 0xd1, 0x8a, 0x7e, 0xa6, 0x60, 0x07, 0x2e, 0xbb, 0x03,
    564  1.1.1.2  christos         0x17, 0x2d, 0xec, 0x17, 0x87 };
    565  1.1.1.2  christos     static unsigned char e_data[] = { 0x01, 0x00, 0x01 };
    566  1.1.1.2  christos     static unsigned char d_data[] = { 0x1e, 0x5e, 0x5d, 0x07, 0x7f, 0xdc, 0x6a, 0x16, 0xcc, 0x55, 0xca, 0x00,
    567  1.1.1.2  christos         0x31, 0x6c, 0xf0, 0xc7, 0x07, 0x38, 0x89, 0x3b, 0x37, 0xd4, 0x9d, 0x5b,
    568  1.1.1.2  christos         0x1e, 0x99, 0x3e, 0x94, 0x5a, 0xe4, 0x82, 0x86, 0x8a, 0x78, 0x34, 0x09,
    569  1.1.1.2  christos         0x37, 0xd5, 0xe7, 0xb4, 0xef, 0x5f, 0x83, 0x94, 0xff, 0xe5, 0x36, 0x79,
    570  1.1.1.2  christos         0x10, 0x0c, 0x38, 0xc5, 0x3a, 0x33, 0xa6, 0x7c, 0x3c, 0xcc, 0x98, 0xe0,
    571  1.1.1.2  christos         0xf5, 0xdb, 0xe6, 0x81 };
    572  1.1.1.2  christos     static unsigned char p_data[] = { 0x00, 0xf6, 0x61, 0x38, 0x0e, 0x1f, 0x82, 0x7c, 0xb8, 0xba, 0x00, 0xd3,
    573  1.1.1.2  christos         0xac, 0xdc, 0x4e, 0x6b, 0x7e, 0xf7, 0x58, 0xf3, 0xd9, 0xd8, 0x21, 0xed,
    574  1.1.1.2  christos         0x54, 0xa3, 0x36, 0xd2, 0x2c, 0x5f, 0x06, 0x7d, 0xc5 };
    575  1.1.1.2  christos     static unsigned char q_data[] = { 0x00, 0xce, 0xcc, 0x4a, 0xa5, 0x4f, 0xd6, 0x73, 0xd0, 0x20, 0xc3, 0x98,
    576  1.1.1.2  christos         0x64, 0x20, 0x9b, 0xc1, 0x23, 0xd8, 0x5c, 0x82, 0x4f, 0xe8, 0xa5, 0x32,
    577  1.1.1.2  christos         0xcd, 0x7e, 0x97, 0xb4, 0xde, 0xf6, 0x4c, 0x80, 0xdb };
    578  1.1.1.2  christos     static unsigned char dmp1_data[] = { 0x00, 0xd1, 0x07, 0xb6, 0x79, 0x34, 0xfe, 0x8e, 0x36, 0x63, 0x88, 0xa4,
    579  1.1.1.2  christos         0x0e, 0x3a, 0x73, 0x45, 0xfc, 0x58, 0x7a, 0x5d, 0x98, 0xeb, 0x28, 0x0d,
    580  1.1.1.2  christos         0xa5, 0x0b, 0x3c, 0x4d, 0xa0, 0x5b, 0x96, 0xb4, 0x49 };
    581  1.1.1.2  christos     static unsigned char dmq1_data[] = { 0x5b, 0x47, 0x02, 0xdf, 0xaa, 0xb8, 0xae, 0x8f, 0xbc, 0x16, 0x79, 0x6a,
    582  1.1.1.2  christos         0x20, 0x96, 0x7f, 0x0e, 0x92, 0x4e, 0x6a, 0xda, 0x58, 0x86, 0xaa, 0x40,
    583  1.1.1.2  christos         0xd7, 0xd2, 0xa0, 0x6c, 0x15, 0x6c, 0xb9, 0x27 };
    584  1.1.1.2  christos     static unsigned char iqmp_data[] = { 0x00, 0xa0, 0xd6, 0xf0, 0xe8, 0x17, 0x9e, 0xe7, 0xe6, 0x99, 0x12, 0xd6,
    585  1.1.1.2  christos         0xd9, 0x43, 0xcf, 0xed, 0x37, 0x29, 0xf5, 0x6c, 0x3e, 0xc1, 0x7f, 0x2e,
    586  1.1.1.2  christos         0x31, 0x3f, 0x64, 0x34, 0x66, 0x68, 0x5c, 0x22, 0x08 };
    587      1.1  christos 
    588      1.1  christos     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
    589      1.1  christos         || !TEST_ptr(n = BN_bin2bn(n_data, sizeof(n_data), NULL))
    590      1.1  christos         || !TEST_ptr(e = BN_bin2bn(e_data, sizeof(e_data), NULL))
    591      1.1  christos         || !TEST_ptr(d = BN_bin2bn(d_data, sizeof(d_data), NULL))
    592      1.1  christos         || !TEST_ptr(p = BN_bin2bn(p_data, sizeof(p_data), NULL))
    593      1.1  christos         || !TEST_ptr(q = BN_bin2bn(q_data, sizeof(q_data), NULL))
    594      1.1  christos         || !TEST_ptr(dmp1 = BN_bin2bn(dmp1_data, sizeof(dmp1_data), NULL))
    595      1.1  christos         || !TEST_ptr(dmq1 = BN_bin2bn(dmq1_data, sizeof(dmq1_data), NULL))
    596      1.1  christos         || !TEST_ptr(iqmp = BN_bin2bn(iqmp_data, sizeof(iqmp_data), NULL))
    597      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n))
    598      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e))
    599      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d))
    600      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR1,
    601  1.1.1.2  christos             p))
    602      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR2,
    603  1.1.1.2  christos             q))
    604      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_int(bld,
    605  1.1.1.2  christos             OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ, 1))
    606      1.1  christos         || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
    607      1.1  christos         goto err;
    608      1.1  christos 
    609      1.1  christos     cdata[0].pname = OSSL_PKEY_PARAM_RSA_EXPONENT1;
    610      1.1  christos     cdata[0].comparebn = dmp1;
    611      1.1  christos     cdata[1].pname = OSSL_PKEY_PARAM_RSA_EXPONENT2;
    612      1.1  christos     cdata[1].comparebn = dmq1;
    613      1.1  christos     cdata[2].pname = OSSL_PKEY_PARAM_RSA_COEFFICIENT1;
    614      1.1  christos     cdata[2].comparebn = iqmp;
    615      1.1  christos     cdata[3].pname = NULL;
    616      1.1  christos     cdata[3].comparebn = NULL;
    617      1.1  christos 
    618      1.1  christos     ret = do_fromdata_rsa_derive(fromdata_params, cdata, 512, 56, 64);
    619      1.1  christos 
    620      1.1  christos err:
    621      1.1  christos     BN_free(n);
    622      1.1  christos     BN_free(e);
    623      1.1  christos     BN_free(d);
    624      1.1  christos     BN_free(p);
    625      1.1  christos     BN_free(q);
    626      1.1  christos     BN_free(dmp1);
    627      1.1  christos     BN_free(dmq1);
    628      1.1  christos     BN_free(iqmp);
    629      1.1  christos     OSSL_PARAM_BLD_free(bld);
    630      1.1  christos     return ret;
    631      1.1  christos }
    632      1.1  christos 
    633      1.1  christos static int test_fromdata_rsa_derive_from_pq_multiprime(void)
    634      1.1  christos {
    635      1.1  christos     OSSL_PARAM_BLD *bld = NULL;
    636      1.1  christos     BIGNUM *n = NULL, *e = NULL, *d = NULL;
    637      1.1  christos     BIGNUM *p = NULL, *q = NULL, *p2 = NULL;
    638      1.1  christos     BIGNUM *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
    639      1.1  christos     BIGNUM *exp3 = NULL, *coeff2 = NULL;
    640      1.1  christos     OSSL_PARAM *fromdata_params = NULL;
    641      1.1  christos     struct check_data cdata[12];
    642      1.1  christos     int ret = 0;
    643      1.1  christos     /*
    644      1.1  christos      * multiprime RSA key,  extracted from this command,
    645      1.1  christos      * openssl genrsa -primes 3 | openssl rsa -text
    646      1.1  christos      * Note: When generating a key with EVP_PKEY_fromdata,  and using
    647      1.1  christos      * crt derivation,  openssl requires a minimum of 512 bits of n data,
    648      1.1  christos      * and 2048 bits in the FIPS case
    649      1.1  christos      */
    650  1.1.1.2  christos     static unsigned char n_data[] = { 0x00, 0x95, 0x78, 0x21, 0xe0, 0xca, 0x94, 0x6c, 0x0b, 0x86, 0x2a, 0x01,
    651  1.1.1.2  christos         0xde, 0xd9, 0xab, 0xee, 0x88, 0x4a, 0x27, 0x4f, 0xcc, 0x5f, 0xf1, 0x71,
    652  1.1.1.2  christos         0xe1, 0x0b, 0xc3, 0xd1, 0x88, 0x76, 0xf0, 0x83, 0x03, 0x93, 0x7e, 0x39,
    653  1.1.1.2  christos         0xfa, 0x47, 0x89, 0x34, 0x27, 0x18, 0x19, 0x97, 0xfc, 0xd4, 0xfe, 0xe5,
    654  1.1.1.2  christos         0x8a, 0xa9, 0x11, 0x83, 0xb5, 0x15, 0x4a, 0x29, 0xa6, 0xa6, 0xd0, 0x6e,
    655  1.1.1.2  christos         0x0c, 0x7f, 0x61, 0x8f, 0x7e, 0x7c, 0xfb, 0xfc, 0x04, 0x8b, 0xca, 0x44,
    656  1.1.1.2  christos         0xf8, 0x59, 0x0b, 0x22, 0x6f, 0x3f, 0x92, 0x23, 0x98, 0xb5, 0xc8, 0xf7,
    657  1.1.1.2  christos         0xff, 0xf7, 0xac, 0x6b, 0x36, 0xb3, 0xaf, 0x39, 0xde, 0x66, 0x38, 0x51,
    658  1.1.1.2  christos         0x9f, 0xbe, 0xe2, 0xfc, 0xe4, 0x6f, 0x1a, 0x0f, 0x7a, 0xde, 0x7f, 0x0f,
    659  1.1.1.2  christos         0x4e, 0xbc, 0xed, 0xa2, 0x99, 0xc5, 0xd1, 0xbf, 0x8f, 0xba, 0x92, 0x91,
    660  1.1.1.2  christos         0xe4, 0x00, 0x91, 0xbb, 0x67, 0x36, 0x7d, 0x00, 0x50, 0xda, 0x28, 0x38,
    661  1.1.1.2  christos         0xdc, 0x9f, 0xfe, 0x3f, 0x24, 0x5a, 0x0d, 0xe1, 0x8d, 0xe9, 0x45, 0x2c,
    662  1.1.1.2  christos         0xd7, 0xf2, 0x67, 0x8c, 0x0c, 0x6e, 0xdb, 0xc8, 0x8b, 0x6b, 0x38, 0x30,
    663  1.1.1.2  christos         0x21, 0x94, 0xc0, 0xe3, 0xd7, 0xe0, 0x23, 0xd3, 0xd4, 0xfa, 0xdb, 0xb9,
    664  1.1.1.2  christos         0xfe, 0x1a, 0xcc, 0xc9, 0x79, 0x19, 0x35, 0x18, 0x42, 0x30, 0xc4, 0xb5,
    665  1.1.1.2  christos         0x92, 0x33, 0x1e, 0xd4, 0xc4, 0xc0, 0x9d, 0x55, 0x37, 0xd4, 0xef, 0x54,
    666  1.1.1.2  christos         0x71, 0x81, 0x09, 0x15, 0xdb, 0x11, 0x38, 0x6b, 0x35, 0x93, 0x11, 0xdc,
    667  1.1.1.2  christos         0xb1, 0x6c, 0xd6, 0xa4, 0x37, 0x84, 0xf3, 0xb2, 0x2f, 0x1b, 0xd6, 0x05,
    668  1.1.1.2  christos         0x9f, 0x0e, 0x5c, 0x98, 0x29, 0x2f, 0x95, 0xb6, 0x55, 0xbd, 0x24, 0x44,
    669  1.1.1.2  christos         0xc5, 0xc8, 0xa2, 0x76, 0x1e, 0xf8, 0x82, 0x8a, 0xdf, 0x34, 0x72, 0x7e,
    670  1.1.1.2  christos         0xdd, 0x65, 0x4b, 0xfc, 0x6c, 0x1c, 0x96, 0x70, 0xe2, 0x69, 0xb5, 0x12,
    671  1.1.1.2  christos         0x1b, 0x59, 0x67, 0x14, 0x9d };
    672  1.1.1.2  christos     static unsigned char e_data[] = { 0x01, 0x00, 0x01 };
    673  1.1.1.2  christos     static unsigned char d_data[] = { 0x64, 0x57, 0x4d, 0x86, 0xf6, 0xf8, 0x44, 0xc0, 0x47, 0xc5, 0x13, 0x94,
    674  1.1.1.2  christos         0x63, 0x54, 0x84, 0xc1, 0x81, 0xe6, 0x7a, 0x2f, 0x9d, 0x89, 0x1d, 0x06,
    675  1.1.1.2  christos         0x13, 0x3b, 0xd6, 0x02, 0x62, 0xb6, 0x7b, 0x7d, 0x7f, 0x1a, 0x92, 0x19,
    676  1.1.1.2  christos         0x6e, 0xc4, 0xb0, 0xfa, 0x3d, 0xb7, 0x90, 0xcc, 0xee, 0xc0, 0x5f, 0xa0,
    677  1.1.1.2  christos         0x82, 0x77, 0x7b, 0x8f, 0xa9, 0x47, 0x2c, 0x46, 0xf0, 0x5d, 0xa4, 0x43,
    678  1.1.1.2  christos         0x47, 0x90, 0x5b, 0x20, 0x73, 0x0f, 0x46, 0xd4, 0x56, 0x73, 0xe7, 0x71,
    679  1.1.1.2  christos         0x41, 0x75, 0xb4, 0x1c, 0x32, 0xf5, 0x0c, 0x68, 0x8c, 0x40, 0xea, 0x1c,
    680  1.1.1.2  christos         0x30, 0x12, 0xa2, 0x65, 0x02, 0x27, 0x98, 0x4e, 0x0a, 0xbf, 0x2b, 0x72,
    681  1.1.1.2  christos         0xb2, 0x5c, 0xe3, 0xbe, 0x3e, 0xc7, 0xdb, 0x9b, 0xa2, 0x4a, 0x90, 0xc0,
    682  1.1.1.2  christos         0xa7, 0xb0, 0x00, 0xf1, 0x6a, 0xff, 0xa3, 0x77, 0xf7, 0x71, 0xa2, 0x41,
    683  1.1.1.2  christos         0xe9, 0x6e, 0x7c, 0x38, 0x24, 0x46, 0xd5, 0x5c, 0x49, 0x2a, 0xe6, 0xee,
    684  1.1.1.2  christos         0x27, 0x4b, 0x2e, 0x6f, 0x16, 0x54, 0x2d, 0x37, 0x36, 0x01, 0x39, 0x2b,
    685  1.1.1.2  christos         0x23, 0x4b, 0xb4, 0x65, 0x25, 0x4d, 0x7f, 0x72, 0x20, 0x7f, 0x5d, 0xec,
    686  1.1.1.2  christos         0x50, 0xba, 0xbb, 0xaa, 0x9c, 0x3c, 0x1d, 0xa1, 0x40, 0x2c, 0x6a, 0x8b,
    687  1.1.1.2  christos         0x5f, 0x2e, 0xe0, 0xa6, 0xf7, 0x9e, 0x03, 0xb5, 0x44, 0x5f, 0x74, 0xc7,
    688  1.1.1.2  christos         0x9f, 0x89, 0x2b, 0x71, 0x2f, 0x66, 0x9f, 0x03, 0x6c, 0x96, 0xd0, 0x23,
    689  1.1.1.2  christos         0x36, 0x4d, 0xa1, 0xf0, 0x82, 0xcc, 0x43, 0xe7, 0x08, 0x93, 0x40, 0x18,
    690  1.1.1.2  christos         0xc0, 0x39, 0x73, 0x83, 0xe2, 0xec, 0x9b, 0x81, 0x9d, 0x4c, 0x86, 0xaa,
    691  1.1.1.2  christos         0x59, 0xa8, 0x67, 0x1c, 0x80, 0xdc, 0x6f, 0x7f, 0x23, 0x6b, 0x7d, 0x2c,
    692  1.1.1.2  christos         0x56, 0x99, 0xa0, 0x89, 0x7e, 0xdb, 0x8b, 0x7a, 0xaa, 0x03, 0x8e, 0x8e,
    693  1.1.1.2  christos         0x8e, 0x3a, 0x58, 0xb4, 0x03, 0x6b, 0x65, 0xfa, 0x92, 0x0a, 0x96, 0x93,
    694  1.1.1.2  christos         0xa6, 0x07, 0x60, 0x01 };
    695  1.1.1.2  christos     static unsigned char p_data[] = { 0x06, 0x55, 0x7f, 0xbd, 0xfd, 0xa8, 0x4c, 0x94, 0x5e, 0x10, 0x8a, 0x54,
    696  1.1.1.2  christos         0x37, 0xf3, 0x64, 0x37, 0x3a, 0xca, 0x18, 0x1b, 0xdd, 0x71, 0xa5, 0x94,
    697  1.1.1.2  christos         0xc9, 0x31, 0x59, 0xa5, 0x89, 0xe9, 0xc4, 0xba, 0x55, 0x90, 0x6d, 0x9c,
    698  1.1.1.2  christos         0xcc, 0x52, 0x5d, 0x44, 0xa8, 0xbc, 0x2b, 0x3b, 0x8c, 0xbd, 0x96, 0xfa,
    699  1.1.1.2  christos         0xcd, 0x54, 0x63, 0xe3, 0xc8, 0xfe, 0x5e, 0xc6, 0x73, 0x98, 0x14, 0x7a,
    700  1.1.1.2  christos         0x54, 0x0e, 0xe7, 0x75, 0x49, 0x93, 0x20, 0x33, 0x17, 0xa9, 0x34, 0xa8,
    701  1.1.1.2  christos         0xee, 0xaf, 0x3a, 0xcc, 0xf5, 0x69, 0xfc, 0x30, 0x1a, 0xdf, 0x49, 0x61,
    702  1.1.1.2  christos         0xa4, 0xd1 };
    703  1.1.1.2  christos     static unsigned char p2_data[] = { 0x03, 0xe2, 0x41, 0x3d, 0xb1, 0xdd, 0xad, 0xd7, 0x3b, 0xf8, 0xab, 0x32,
    704  1.1.1.2  christos         0x27, 0x8b, 0xac, 0x95, 0xc0, 0x1a, 0x3f, 0x80, 0x8e, 0x21, 0xa9, 0xb8,
    705  1.1.1.2  christos         0xa2, 0xed, 0xcf, 0x97, 0x5c, 0x61, 0x10, 0x94, 0x1b, 0xd0, 0xbe, 0x88,
    706  1.1.1.2  christos         0xc2, 0xa7, 0x20, 0xe5, 0xa5, 0xc2, 0x7a, 0x7e, 0xf0, 0xd1, 0xe4, 0x13,
    707  1.1.1.2  christos         0x75, 0xb9, 0x62, 0x90, 0xf1, 0xc3, 0x5b, 0x8c, 0xe9, 0xa9, 0x5b, 0xb7,
    708  1.1.1.2  christos         0x6d, 0xdc, 0xcd, 0x12, 0xea, 0x97, 0x05, 0x04, 0x25, 0x2a, 0x93, 0xd1,
    709  1.1.1.2  christos         0x4e, 0x05, 0x1a, 0x50, 0xa2, 0x67, 0xb8, 0x4b, 0x09, 0x15, 0x65, 0x6c,
    710  1.1.1.2  christos         0x66, 0x2d };
    711  1.1.1.2  christos     static unsigned char q_data[] = { 0x06, 0x13, 0x74, 0x6e, 0xde, 0x7c, 0x33, 0xc2, 0xe7, 0x05, 0x2c, 0xeb,
    712  1.1.1.2  christos         0x25, 0x7d, 0x4a, 0x07, 0x7e, 0x03, 0xcf, 0x6a, 0x23, 0x36, 0x25, 0x23,
    713  1.1.1.2  christos         0xf6, 0x5d, 0xde, 0xa3, 0x0f, 0x82, 0xe6, 0x4b, 0xec, 0x39, 0xbf, 0x37,
    714  1.1.1.2  christos         0x1f, 0x4f, 0x56, 0x1e, 0xd8, 0x62, 0x32, 0x5c, 0xf5, 0x37, 0x75, 0x20,
    715  1.1.1.2  christos         0xe2, 0x7e, 0x56, 0x82, 0xc6, 0x35, 0xd3, 0x4d, 0xfa, 0x6c, 0xc3, 0x93,
    716  1.1.1.2  christos         0xf0, 0x60, 0x53, 0x78, 0x95, 0xee, 0xf9, 0x8b, 0x2c, 0xaf, 0xb1, 0x47,
    717  1.1.1.2  christos         0x5c, 0x29, 0x0d, 0x2a, 0x47, 0x7f, 0xd0, 0x7a, 0x4e, 0x26, 0x7b, 0x47,
    718  1.1.1.2  christos         0xfb, 0x61 };
    719  1.1.1.2  christos     static unsigned char dmp1_data[] = { 0x01, 0x13, 0x3a, 0x1f, 0x91, 0x92, 0xa3, 0x8c, 0xfb, 0x7a, 0x6b, 0x40,
    720  1.1.1.2  christos         0x68, 0x4e, 0xd3, 0xcf, 0xdc, 0x16, 0xb9, 0x88, 0xe1, 0x49, 0x8d, 0x05,
    721  1.1.1.2  christos         0x78, 0x30, 0xfc, 0x3a, 0x70, 0xf2, 0x51, 0x06, 0x1f, 0xc7, 0xe8, 0x13,
    722  1.1.1.2  christos         0x19, 0x4b, 0x51, 0xb1, 0x79, 0xc2, 0x96, 0xc4, 0x00, 0xdb, 0x9d, 0x68,
    723  1.1.1.2  christos         0xec, 0xb9, 0x4a, 0x4b, 0x3b, 0xae, 0x91, 0x7f, 0xb5, 0xd7, 0x36, 0x82,
    724  1.1.1.2  christos         0x9d, 0x09, 0xfa, 0x97, 0x99, 0xe9, 0x73, 0x29, 0xb8, 0xf6, 0x6b, 0x8d,
    725  1.1.1.2  christos         0xd1, 0x15, 0xc5, 0x31, 0x4c, 0xe6, 0xb4, 0x7b, 0xa5, 0xd4, 0x08, 0xac,
    726  1.1.1.2  christos         0x9e, 0x41 };
    727  1.1.1.2  christos     static unsigned char dmq1_data[] = { 0x05, 0xcd, 0x33, 0xc2, 0xdd, 0x3b, 0xb8, 0xec, 0xe4, 0x4c, 0x03, 0xcc,
    728  1.1.1.2  christos         0xef, 0xba, 0x07, 0x22, 0xca, 0x47, 0x77, 0x18, 0x40, 0x50, 0xe5, 0xfb,
    729  1.1.1.2  christos         0xc5, 0xb5, 0x71, 0xed, 0x3e, 0xd5, 0x5d, 0x72, 0xa7, 0x37, 0xa8, 0x86,
    730  1.1.1.2  christos         0x48, 0xa6, 0x27, 0x74, 0x42, 0x66, 0xd8, 0xf1, 0xfb, 0xcf, 0x1d, 0x4e,
    731  1.1.1.2  christos         0xee, 0x15, 0x76, 0x23, 0x5e, 0x81, 0x6c, 0xa7, 0x2b, 0x74, 0x08, 0xf7,
    732  1.1.1.2  christos         0x4c, 0x71, 0x9d, 0xa2, 0x29, 0x7f, 0xca, 0xd5, 0x02, 0x31, 0x2c, 0x54,
    733  1.1.1.2  christos         0x18, 0x02, 0xb6, 0xa8, 0x65, 0x26, 0xfc, 0xf8, 0x9b, 0x80, 0x90, 0xfc,
    734  1.1.1.2  christos         0x75, 0x61 };
    735  1.1.1.2  christos     static unsigned char iqmp_data[] = { 0x05, 0x78, 0xf8, 0xdd, 0x1c, 0x6f, 0x3d, 0xaf, 0x53, 0x84, 0x32, 0xa9,
    736  1.1.1.2  christos         0x35, 0x52, 0xf3, 0xd0, 0x4d, 0xf8, 0x09, 0x85, 0x3d, 0x72, 0x20, 0x8b,
    737  1.1.1.2  christos         0x47, 0xba, 0xc8, 0xce, 0xac, 0xd9, 0x76, 0x90, 0x05, 0x88, 0x63, 0x8a,
    738  1.1.1.2  christos         0x10, 0x2b, 0xcd, 0xd3, 0xbe, 0x8c, 0x16, 0x60, 0x6a, 0xfd, 0xce, 0xc7,
    739  1.1.1.2  christos         0x9f, 0xfa, 0xbb, 0xe3, 0xa6, 0xde, 0xc2, 0x8f, 0x1d, 0x25, 0xdc, 0x41,
    740  1.1.1.2  christos         0xcb, 0xa4, 0xeb, 0x76, 0xc9, 0xdc, 0x8e, 0x49, 0x0e, 0xe4, 0x7c, 0xd2,
    741  1.1.1.2  christos         0xd5, 0x6e, 0x26, 0x3c, 0x0b, 0xd3, 0xc5, 0x20, 0x4e, 0x4b, 0xb6, 0xf7,
    742  1.1.1.2  christos         0xae, 0xef };
    743  1.1.1.2  christos     static unsigned char exp3_data[] = { 0x02, 0x7d, 0x16, 0x24, 0xfc, 0x35, 0xf9, 0xd0, 0xb3, 0x02, 0xf2, 0x5f,
    744  1.1.1.2  christos         0xde, 0xeb, 0x27, 0x19, 0x85, 0xd0, 0xcb, 0xe4, 0x0a, 0x2f, 0x13, 0xdb,
    745  1.1.1.2  christos         0xd5, 0xba, 0xe0, 0x8c, 0x32, 0x8b, 0x97, 0xdd, 0xef, 0xbc, 0xe0, 0x7a,
    746  1.1.1.2  christos         0x2d, 0x90, 0x7e, 0x09, 0xe9, 0x1f, 0x26, 0xf2, 0xf4, 0x48, 0xea, 0x06,
    747  1.1.1.2  christos         0x76, 0x26, 0xe6, 0x3b, 0xce, 0x4e, 0xc9, 0xf9, 0x0f, 0x38, 0x90, 0x26,
    748  1.1.1.2  christos         0x87, 0x65, 0x36, 0x9a, 0xea, 0x6a, 0xfe, 0xb1, 0xdb, 0x46, 0xdf, 0x14,
    749  1.1.1.2  christos         0xfd, 0x13, 0x53, 0xfb, 0x5b, 0x35, 0x6e, 0xe7, 0xd5, 0xd8, 0x39, 0xf7,
    750  1.1.1.2  christos         0x2d, 0xb9 };
    751  1.1.1.2  christos     static unsigned char coeff2_data[] = { 0x01, 0xba, 0x66, 0x0a, 0xa2, 0x86, 0xc0, 0x57, 0x7f, 0x4e, 0x68, 0xb1,
    752  1.1.1.2  christos         0x86, 0x63, 0x23, 0x5b, 0x0e, 0xeb, 0x93, 0x42, 0xd1, 0xaa, 0x15, 0x13,
    753  1.1.1.2  christos         0xcc, 0x29, 0x71, 0x8a, 0xb0, 0xe0, 0xc9, 0x67, 0xde, 0x1a, 0x7c, 0x1a,
    754  1.1.1.2  christos         0xef, 0xa7, 0x08, 0x85, 0xb3, 0xae, 0x98, 0x99, 0xde, 0xaf, 0x09, 0x38,
    755  1.1.1.2  christos         0xfc, 0x46, 0x29, 0x5f, 0x4f, 0x7e, 0x01, 0x6c, 0x50, 0x13, 0x95, 0x91,
    756  1.1.1.2  christos         0x4c, 0x0f, 0x00, 0xba, 0xca, 0x40, 0xa3, 0xd0, 0x58, 0xb6, 0x62, 0x4c,
    757  1.1.1.2  christos         0xd1, 0xb6, 0xd3, 0x29, 0x5d, 0x82, 0xb3, 0x3d, 0x61, 0xbe, 0x5d, 0xf0,
    758  1.1.1.2  christos         0x4b, 0xf4 };
    759      1.1  christos 
    760      1.1  christos     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
    761      1.1  christos         || !TEST_ptr(n = BN_bin2bn(n_data, sizeof(n_data), NULL))
    762      1.1  christos         || !TEST_ptr(e = BN_bin2bn(e_data, sizeof(e_data), NULL))
    763      1.1  christos         || !TEST_ptr(d = BN_bin2bn(d_data, sizeof(d_data), NULL))
    764      1.1  christos         || !TEST_ptr(p = BN_bin2bn(p_data, sizeof(p_data), NULL))
    765      1.1  christos         || !TEST_ptr(q = BN_bin2bn(q_data, sizeof(q_data), NULL))
    766      1.1  christos         || !TEST_ptr(p2 = BN_bin2bn(p2_data, sizeof(p2_data), NULL))
    767      1.1  christos         || !TEST_ptr(exp3 = BN_bin2bn(exp3_data, sizeof(exp3_data), NULL))
    768      1.1  christos         || !TEST_ptr(coeff2 = BN_bin2bn(coeff2_data, sizeof(coeff2_data), NULL))
    769      1.1  christos         || !TEST_ptr(dmp1 = BN_bin2bn(dmp1_data, sizeof(dmp1_data), NULL))
    770      1.1  christos         || !TEST_ptr(dmq1 = BN_bin2bn(dmq1_data, sizeof(dmq1_data), NULL))
    771      1.1  christos         || !TEST_ptr(iqmp = BN_bin2bn(iqmp_data, sizeof(iqmp_data), NULL))
    772      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n))
    773      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e))
    774      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d))
    775      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR1,
    776  1.1.1.2  christos             p))
    777      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR2,
    778  1.1.1.2  christos             q))
    779      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR3,
    780  1.1.1.2  christos             p2))
    781      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_int(bld,
    782  1.1.1.2  christos             OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ, 1))
    783      1.1  christos         || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
    784      1.1  christos         goto err;
    785      1.1  christos 
    786      1.1  christos     cdata[0].pname = OSSL_PKEY_PARAM_RSA_EXPONENT1;
    787      1.1  christos     cdata[0].comparebn = dmp1;
    788      1.1  christos     cdata[1].pname = OSSL_PKEY_PARAM_RSA_EXPONENT2;
    789      1.1  christos     cdata[1].comparebn = dmq1;
    790      1.1  christos     cdata[2].pname = OSSL_PKEY_PARAM_RSA_COEFFICIENT1;
    791      1.1  christos     cdata[2].comparebn = iqmp;
    792      1.1  christos     cdata[3].pname = OSSL_PKEY_PARAM_RSA_EXPONENT3;
    793      1.1  christos     cdata[3].comparebn = exp3;
    794      1.1  christos     cdata[4].pname = OSSL_PKEY_PARAM_RSA_COEFFICIENT2;
    795      1.1  christos     cdata[4].comparebn = coeff2;
    796      1.1  christos     cdata[5].pname = OSSL_PKEY_PARAM_RSA_N;
    797      1.1  christos     cdata[5].comparebn = n;
    798      1.1  christos     cdata[6].pname = OSSL_PKEY_PARAM_RSA_E;
    799      1.1  christos     cdata[6].comparebn = e;
    800      1.1  christos     cdata[7].pname = OSSL_PKEY_PARAM_RSA_D;
    801      1.1  christos     cdata[7].comparebn = d;
    802      1.1  christos     cdata[8].pname = OSSL_PKEY_PARAM_RSA_FACTOR1;
    803      1.1  christos     cdata[8].comparebn = p;
    804      1.1  christos     cdata[9].pname = OSSL_PKEY_PARAM_RSA_FACTOR2;
    805      1.1  christos     cdata[9].comparebn = q;
    806      1.1  christos     cdata[10].pname = OSSL_PKEY_PARAM_RSA_FACTOR3;
    807      1.1  christos     cdata[10].comparebn = p2;
    808      1.1  christos     cdata[11].pname = NULL;
    809      1.1  christos     cdata[11].comparebn = NULL;
    810      1.1  christos 
    811      1.1  christos     ret = do_fromdata_rsa_derive(fromdata_params, cdata, 2048, 112, 256);
    812      1.1  christos 
    813      1.1  christos err:
    814      1.1  christos     BN_free(n);
    815      1.1  christos     BN_free(e);
    816      1.1  christos     BN_free(d);
    817      1.1  christos     BN_free(p);
    818      1.1  christos     BN_free(p2);
    819      1.1  christos     BN_free(q);
    820      1.1  christos     BN_free(dmp1);
    821      1.1  christos     BN_free(dmq1);
    822      1.1  christos     BN_free(iqmp);
    823      1.1  christos     BN_free(exp3);
    824      1.1  christos     BN_free(coeff2);
    825      1.1  christos     OSSL_PARAM_BLD_free(bld);
    826      1.1  christos     return ret;
    827      1.1  christos }
    828      1.1  christos 
    829      1.1  christos static int test_evp_pkey_get_bn_param_large(void)
    830      1.1  christos {
    831      1.1  christos     int ret = 0;
    832      1.1  christos     EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
    833      1.1  christos     EVP_PKEY *pk = NULL;
    834      1.1  christos     OSSL_PARAM_BLD *bld = NULL;
    835      1.1  christos     OSSL_PARAM *fromdata_params = NULL;
    836      1.1  christos     BIGNUM *n = NULL, *e = NULL, *d = NULL, *n_out = NULL;
    837      1.1  christos     /*
    838      1.1  christos      * The buffer size chosen here for n_data larger than the buffer used
    839      1.1  christos      * internally in EVP_PKEY_get_bn_param.
    840      1.1  christos      */
    841      1.1  christos     static unsigned char n_data[2050];
    842      1.1  christos     static const unsigned char e_data[] = {
    843      1.1  christos         0x1, 0x00, 0x01
    844      1.1  christos     };
    845      1.1  christos     static const unsigned char d_data[] = {
    846  1.1.1.2  christos         0x99, 0x33, 0x13, 0x7b
    847      1.1  christos     };
    848      1.1  christos 
    849      1.1  christos     /* N is a large buffer */
    850      1.1  christos     memset(n_data, 0xCE, sizeof(n_data));
    851      1.1  christos 
    852      1.1  christos     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
    853      1.1  christos         || !TEST_ptr(n = BN_bin2bn(n_data, sizeof(n_data), NULL))
    854      1.1  christos         || !TEST_ptr(e = BN_bin2bn(e_data, sizeof(e_data), NULL))
    855      1.1  christos         || !TEST_ptr(d = BN_bin2bn(d_data, sizeof(d_data), NULL))
    856      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n))
    857      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e))
    858      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d))
    859      1.1  christos         || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))
    860      1.1  christos         || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL))
    861      1.1  christos         || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
    862      1.1  christos         || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
    863  1.1.1.2  christos                             fromdata_params),
    864  1.1.1.2  christos             1)
    865      1.1  christos         || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))
    866      1.1  christos         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_RSA_N, &n_out))
    867      1.1  christos         || !TEST_BN_eq(n, n_out))
    868      1.1  christos         goto err;
    869      1.1  christos     ret = 1;
    870  1.1.1.2  christos err:
    871      1.1  christos     BN_free(n_out);
    872      1.1  christos     BN_free(n);
    873      1.1  christos     BN_free(e);
    874      1.1  christos     BN_free(d);
    875      1.1  christos     EVP_PKEY_free(pk);
    876      1.1  christos     EVP_PKEY_CTX_free(key_ctx);
    877      1.1  christos     EVP_PKEY_CTX_free(ctx);
    878      1.1  christos     OSSL_PARAM_free(fromdata_params);
    879      1.1  christos     OSSL_PARAM_BLD_free(bld);
    880      1.1  christos     return ret;
    881      1.1  christos }
    882      1.1  christos 
    883      1.1  christos #ifndef OPENSSL_NO_DH
    884      1.1  christos static int test_fromdata_dh_named_group(void)
    885      1.1  christos {
    886      1.1  christos     int ret = 0;
    887      1.1  christos     int gindex = 0, pcounter = 0, hindex = 0;
    888      1.1  christos     EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
    889      1.1  christos     EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL;
    890      1.1  christos     size_t len;
    891      1.1  christos     BIGNUM *pub = NULL, *priv = NULL;
    892      1.1  christos     BIGNUM *pub_out = NULL, *priv_out = NULL;
    893      1.1  christos     BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL;
    894      1.1  christos     OSSL_PARAM *fromdata_params = NULL;
    895      1.1  christos     OSSL_PARAM_BLD *bld = NULL;
    896      1.1  christos     char name_out[80];
    897      1.1  christos     unsigned char seed_out[32];
    898      1.1  christos 
    899      1.1  christos     /*
    900      1.1  christos      * DH key data was generated using the following:
    901      1.1  christos      * openssl genpkey -algorithm DH -pkeyopt group:ffdhe2048
    902      1.1  christos      *                 -pkeyopt priv_len:224 -text
    903      1.1  christos      */
    904      1.1  christos     static const unsigned char priv_data[] = {
    905  1.1.1.2  christos         0x88,
    906  1.1.1.2  christos         0x85,
    907  1.1.1.2  christos         0xe7,
    908  1.1.1.2  christos         0x9f,
    909  1.1.1.2  christos         0xee,
    910  1.1.1.2  christos         0x6d,
    911  1.1.1.2  christos         0xc5,
    912  1.1.1.2  christos         0x7c,
    913  1.1.1.2  christos         0x78,
    914  1.1.1.2  christos         0xaf,
    915  1.1.1.2  christos         0x63,
    916  1.1.1.2  christos         0x5d,
    917  1.1.1.2  christos         0x38,
    918  1.1.1.2  christos         0x2a,
    919  1.1.1.2  christos         0xd0,
    920  1.1.1.2  christos         0xed,
    921  1.1.1.2  christos         0x56,
    922  1.1.1.2  christos         0x4b,
    923  1.1.1.2  christos         0x47,
    924  1.1.1.2  christos         0x21,
    925  1.1.1.2  christos         0x2b,
    926  1.1.1.2  christos         0xfa,
    927  1.1.1.2  christos         0x55,
    928  1.1.1.2  christos         0xfa,
    929  1.1.1.2  christos         0x87,
    930  1.1.1.2  christos         0xe8,
    931  1.1.1.2  christos         0xa9,
    932  1.1.1.2  christos         0x7b,
    933      1.1  christos     };
    934      1.1  christos     static const unsigned char pub_data[] = {
    935      1.1  christos         0x00, 0xd6, 0x2d, 0x77, 0xe0, 0xd3, 0x7d, 0xf8, 0xeb, 0x98, 0x50, 0xa1,
    936      1.1  christos         0x82, 0x22, 0x65, 0xd5, 0xd9, 0xfe, 0xc9, 0x3f, 0xbe, 0x16, 0x83, 0xbd,
    937      1.1  christos         0x33, 0xe9, 0xc6, 0x93, 0xcf, 0x08, 0xaf, 0x83, 0xfa, 0x80, 0x8a, 0x6c,
    938      1.1  christos         0x64, 0xdf, 0x70, 0x64, 0xd5, 0x0a, 0x7c, 0x5a, 0x72, 0xda, 0x66, 0xe6,
    939      1.1  christos         0xf9, 0xf5, 0x31, 0x21, 0x92, 0xb0, 0x60, 0x1a, 0xb5, 0xd3, 0xf0, 0xa5,
    940      1.1  christos         0xfa, 0x48, 0x95, 0x2e, 0x38, 0xd9, 0xc5, 0xe6, 0xda, 0xfb, 0x6c, 0x03,
    941      1.1  christos         0x9d, 0x4b, 0x69, 0xb7, 0x95, 0xe4, 0x5c, 0xc0, 0x93, 0x4f, 0x48, 0xd9,
    942      1.1  christos         0x7e, 0x06, 0x22, 0xb2, 0xde, 0xf3, 0x79, 0x24, 0xed, 0xe1, 0xd1, 0x4a,
    943      1.1  christos         0x57, 0xf1, 0x40, 0x86, 0x70, 0x42, 0x25, 0xc5, 0x27, 0x68, 0xc9, 0xfa,
    944      1.1  christos         0xe5, 0x8e, 0x62, 0x7e, 0xff, 0x49, 0x6c, 0x5b, 0xb5, 0xba, 0xf9, 0xef,
    945      1.1  christos         0x9a, 0x1a, 0x10, 0xd4, 0x81, 0x53, 0xcf, 0x83, 0x04, 0x18, 0x1c, 0xe1,
    946      1.1  christos         0xdb, 0xe1, 0x65, 0xa9, 0x7f, 0xe1, 0x33, 0xeb, 0xc3, 0x4f, 0xe3, 0xb7,
    947      1.1  christos         0x22, 0xf7, 0x1c, 0x09, 0x4f, 0xed, 0xc6, 0x07, 0x8e, 0x78, 0x05, 0x8f,
    948      1.1  christos         0x7c, 0x96, 0xd9, 0x12, 0xe0, 0x81, 0x74, 0x1a, 0xe9, 0x13, 0xc0, 0x20,
    949      1.1  christos         0x82, 0x65, 0xbb, 0x42, 0x3b, 0xed, 0x08, 0x6a, 0x84, 0x4f, 0xea, 0x77,
    950      1.1  christos         0x14, 0x32, 0xf9, 0xed, 0xc2, 0x12, 0xd6, 0xc5, 0xc6, 0xb3, 0xe5, 0xf2,
    951      1.1  christos         0x6e, 0xf6, 0x16, 0x7f, 0x37, 0xde, 0xbc, 0x09, 0xc7, 0x06, 0x6b, 0x12,
    952      1.1  christos         0xbc, 0xad, 0x2d, 0x49, 0x25, 0xd5, 0xdc, 0xf4, 0x18, 0x14, 0xd2, 0xf0,
    953      1.1  christos         0xf1, 0x1d, 0x1f, 0x3a, 0xaa, 0x15, 0x55, 0xbb, 0x0d, 0x7f, 0xbe, 0x67,
    954      1.1  christos         0xa1, 0xa7, 0xf0, 0xaa, 0xb3, 0xfb, 0x41, 0x82, 0x39, 0x49, 0x93, 0xbc,
    955      1.1  christos         0xa8, 0xee, 0x72, 0x13, 0x45, 0x65, 0x15, 0x42, 0x17, 0xaa, 0xd8, 0xab,
    956      1.1  christos         0xcf, 0x33, 0x42, 0x83, 0x42
    957      1.1  christos     };
    958      1.1  christos     static const char group_name[] = "ffdhe2048";
    959      1.1  christos     static const long priv_len = 224;
    960      1.1  christos 
    961      1.1  christos     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
    962      1.1  christos         || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL))
    963      1.1  christos         || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL))
    964      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
    965  1.1.1.2  christos             OSSL_PKEY_PARAM_GROUP_NAME,
    966  1.1.1.2  christos             group_name, 0))
    967      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_long(bld, OSSL_PKEY_PARAM_DH_PRIV_LEN,
    968  1.1.1.2  christos             priv_len))
    969      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub))
    970      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv))
    971      1.1  christos         || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
    972      1.1  christos         goto err;
    973      1.1  christos 
    974      1.1  christos     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL)))
    975      1.1  christos         goto err;
    976      1.1  christos 
    977      1.1  christos     if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
    978      1.1  christos         || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
    979  1.1.1.2  christos                             fromdata_params),
    980  1.1.1.2  christos             1))
    981      1.1  christos         goto err;
    982      1.1  christos 
    983      1.1  christos     /*
    984      1.1  christos      * A few extra checks of EVP_PKEY_get_utf8_string_param() to see that
    985      1.1  christos      * it behaves as expected with regards to string length and terminating
    986      1.1  christos      * NUL byte.
    987      1.1  christos      */
    988      1.1  christos     if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk,
    989  1.1.1.2  christos             OSSL_PKEY_PARAM_GROUP_NAME,
    990  1.1.1.2  christos             NULL, sizeof(name_out),
    991  1.1.1.2  christos             &len))
    992      1.1  christos         || !TEST_size_t_eq(len, sizeof(group_name) - 1)
    993      1.1  christos         /* Just enough space to hold the group name and a terminating NUL */
    994      1.1  christos         || !TEST_true(EVP_PKEY_get_utf8_string_param(pk,
    995  1.1.1.2  christos             OSSL_PKEY_PARAM_GROUP_NAME,
    996  1.1.1.2  christos             name_out,
    997  1.1.1.2  christos             sizeof(group_name),
    998  1.1.1.2  christos             &len))
    999      1.1  christos         || !TEST_size_t_eq(len, sizeof(group_name) - 1)
   1000      1.1  christos         /* Too small buffer to hold the terminating NUL byte */
   1001      1.1  christos         || !TEST_false(EVP_PKEY_get_utf8_string_param(pk,
   1002  1.1.1.2  christos             OSSL_PKEY_PARAM_GROUP_NAME,
   1003  1.1.1.2  christos             name_out,
   1004  1.1.1.2  christos             sizeof(group_name) - 1,
   1005  1.1.1.2  christos             &len))
   1006      1.1  christos         /* Too small buffer to hold the whole group name, even! */
   1007      1.1  christos         || !TEST_false(EVP_PKEY_get_utf8_string_param(pk,
   1008  1.1.1.2  christos             OSSL_PKEY_PARAM_GROUP_NAME,
   1009  1.1.1.2  christos             name_out,
   1010  1.1.1.2  christos             sizeof(group_name) - 2,
   1011  1.1.1.2  christos             &len)))
   1012      1.1  christos         goto err;
   1013      1.1  christos 
   1014      1.1  christos     for (;;) {
   1015      1.1  christos         ret = 0;
   1016      1.1  christos         if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048)
   1017      1.1  christos             || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112)
   1018      1.1  christos             || !TEST_int_eq(EVP_PKEY_get_size(pk), 256)
   1019      1.1  christos             || !TEST_false(EVP_PKEY_missing_parameters(pk)))
   1020      1.1  christos             goto err;
   1021      1.1  christos 
   1022      1.1  christos         if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk,
   1023  1.1.1.2  christos                 OSSL_PKEY_PARAM_GROUP_NAME,
   1024  1.1.1.2  christos                 name_out,
   1025  1.1.1.2  christos                 sizeof(name_out),
   1026  1.1.1.2  christos                 &len))
   1027      1.1  christos             || !TEST_str_eq(name_out, group_name)
   1028      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY,
   1029  1.1.1.2  christos                 &pub_out))
   1030      1.1  christos 
   1031      1.1  christos             || !TEST_BN_eq(pub, pub_out)
   1032      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY,
   1033  1.1.1.2  christos                 &priv_out))
   1034      1.1  christos             || !TEST_BN_eq(priv, priv_out)
   1035      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p))
   1036      1.1  christos             || !TEST_BN_eq(&ossl_bignum_ffdhe2048_p, p)
   1037      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q))
   1038      1.1  christos             || !TEST_ptr(q)
   1039      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g))
   1040      1.1  christos             || !TEST_BN_eq(&ossl_bignum_const_2, g)
   1041      1.1  christos             || !TEST_false(EVP_PKEY_get_bn_param(pk,
   1042  1.1.1.2  christos                 OSSL_PKEY_PARAM_FFC_COFACTOR,
   1043  1.1.1.2  christos                 &j))
   1044      1.1  christos             || !TEST_ptr_null(j)
   1045      1.1  christos             || !TEST_false(EVP_PKEY_get_octet_string_param(pk,
   1046  1.1.1.2  christos                 OSSL_PKEY_PARAM_FFC_SEED,
   1047  1.1.1.2  christos                 seed_out,
   1048  1.1.1.2  christos                 sizeof(seed_out),
   1049  1.1.1.2  christos                 &len))
   1050      1.1  christos             || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_GINDEX,
   1051  1.1.1.2  christos                 &gindex))
   1052      1.1  christos             || !TEST_int_eq(gindex, -1)
   1053      1.1  christos             || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H,
   1054  1.1.1.2  christos                 &hindex))
   1055      1.1  christos             || !TEST_int_eq(hindex, 0)
   1056      1.1  christos             || !TEST_true(EVP_PKEY_get_int_param(pk,
   1057  1.1.1.2  christos                 OSSL_PKEY_PARAM_FFC_PCOUNTER,
   1058  1.1.1.2  christos                 &pcounter))
   1059      1.1  christos             || !TEST_int_eq(pcounter, -1))
   1060      1.1  christos             goto err;
   1061      1.1  christos         BN_free(p);
   1062      1.1  christos         p = NULL;
   1063      1.1  christos         BN_free(q);
   1064      1.1  christos         q = NULL;
   1065      1.1  christos         BN_free(g);
   1066      1.1  christos         g = NULL;
   1067      1.1  christos         BN_free(j);
   1068      1.1  christos         j = NULL;
   1069      1.1  christos         BN_free(pub_out);
   1070      1.1  christos         pub_out = NULL;
   1071      1.1  christos         BN_free(priv_out);
   1072      1.1  christos         priv_out = NULL;
   1073      1.1  christos 
   1074      1.1  christos         if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
   1075      1.1  christos             goto err;
   1076      1.1  christos 
   1077      1.1  christos         if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0)
   1078      1.1  christos             || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0)
   1079      1.1  christos             || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0)
   1080      1.1  christos             || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0))
   1081      1.1  christos             goto err;
   1082      1.1  christos         EVP_PKEY_CTX_free(key_ctx);
   1083      1.1  christos         key_ctx = NULL;
   1084      1.1  christos 
   1085      1.1  christos         if (!TEST_ptr(copy_pk = EVP_PKEY_new())
   1086      1.1  christos             || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk)))
   1087      1.1  christos             goto err;
   1088      1.1  christos         EVP_PKEY_free(copy_pk);
   1089      1.1  christos         copy_pk = NULL;
   1090      1.1  christos 
   1091      1.1  christos         ret = test_print_key_using_pem("DH", pk)
   1092  1.1.1.2  christos             && test_print_key_using_encoder("DH", pk);
   1093      1.1  christos 
   1094      1.1  christos         if (!ret || dup_pk != NULL)
   1095      1.1  christos             break;
   1096      1.1  christos 
   1097      1.1  christos         if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
   1098      1.1  christos             goto err;
   1099      1.1  christos         ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
   1100      1.1  christos         EVP_PKEY_free(pk);
   1101      1.1  christos         pk = dup_pk;
   1102      1.1  christos         if (!ret)
   1103      1.1  christos             goto err;
   1104      1.1  christos     }
   1105      1.1  christos err:
   1106      1.1  christos     BN_free(p);
   1107      1.1  christos     BN_free(q);
   1108      1.1  christos     BN_free(g);
   1109      1.1  christos     BN_free(j);
   1110      1.1  christos     BN_free(pub);
   1111      1.1  christos     BN_free(priv);
   1112      1.1  christos     BN_free(pub_out);
   1113      1.1  christos     BN_free(priv_out);
   1114      1.1  christos     EVP_PKEY_free(copy_pk);
   1115      1.1  christos     EVP_PKEY_free(pk);
   1116      1.1  christos     EVP_PKEY_CTX_free(ctx);
   1117      1.1  christos     EVP_PKEY_CTX_free(key_ctx);
   1118      1.1  christos     OSSL_PARAM_free(fromdata_params);
   1119      1.1  christos     OSSL_PARAM_BLD_free(bld);
   1120      1.1  christos 
   1121      1.1  christos     return ret;
   1122      1.1  christos }
   1123      1.1  christos 
   1124      1.1  christos static int test_fromdata_dh_fips186_4(void)
   1125      1.1  christos {
   1126      1.1  christos     int ret = 0;
   1127      1.1  christos     int gindex = 0, pcounter = 0, hindex = 0;
   1128      1.1  christos     EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
   1129      1.1  christos     EVP_PKEY *pk = NULL, *dup_pk = NULL;
   1130      1.1  christos     size_t len;
   1131      1.1  christos     BIGNUM *pub = NULL, *priv = NULL;
   1132      1.1  christos     BIGNUM *pub_out = NULL, *priv_out = NULL;
   1133      1.1  christos     BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL;
   1134      1.1  christos     OSSL_PARAM_BLD *bld = NULL;
   1135      1.1  christos     OSSL_PARAM *fromdata_params = NULL;
   1136      1.1  christos     char name_out[80];
   1137      1.1  christos     unsigned char seed_out[32];
   1138      1.1  christos 
   1139      1.1  christos     /*
   1140      1.1  christos      * DH key data was generated using the following:
   1141      1.1  christos      * openssl genpkey -algorithm DH
   1142      1.1  christos      *                 -pkeyopt group:ffdhe2048 -pkeyopt priv_len:224 -text
   1143      1.1  christos      */
   1144      1.1  christos     static const unsigned char priv_data[] = {
   1145  1.1.1.2  christos         0x88,
   1146  1.1.1.2  christos         0x85,
   1147  1.1.1.2  christos         0xe7,
   1148  1.1.1.2  christos         0x9f,
   1149  1.1.1.2  christos         0xee,
   1150  1.1.1.2  christos         0x6d,
   1151  1.1.1.2  christos         0xc5,
   1152  1.1.1.2  christos         0x7c,
   1153  1.1.1.2  christos         0x78,
   1154  1.1.1.2  christos         0xaf,
   1155  1.1.1.2  christos         0x63,
   1156  1.1.1.2  christos         0x5d,
   1157  1.1.1.2  christos         0x38,
   1158  1.1.1.2  christos         0x2a,
   1159  1.1.1.2  christos         0xd0,
   1160  1.1.1.2  christos         0xed,
   1161  1.1.1.2  christos         0x56,
   1162  1.1.1.2  christos         0x4b,
   1163  1.1.1.2  christos         0x47,
   1164  1.1.1.2  christos         0x21,
   1165  1.1.1.2  christos         0x2b,
   1166  1.1.1.2  christos         0xfa,
   1167  1.1.1.2  christos         0x55,
   1168  1.1.1.2  christos         0xfa,
   1169  1.1.1.2  christos         0x87,
   1170  1.1.1.2  christos         0xe8,
   1171  1.1.1.2  christos         0xa9,
   1172  1.1.1.2  christos         0x7b,
   1173      1.1  christos     };
   1174      1.1  christos     static const unsigned char pub_data[] = {
   1175  1.1.1.2  christos         0xd6, 0x2d, 0x77, 0xe0, 0xd3, 0x7d, 0xf8, 0xeb, 0x98, 0x50, 0xa1, 0x82,
   1176  1.1.1.2  christos         0x22, 0x65, 0xd5, 0xd9, 0xfe, 0xc9, 0x3f, 0xbe, 0x16, 0x83, 0xbd, 0x33,
   1177  1.1.1.2  christos         0xe9, 0xc6, 0x93, 0xcf, 0x08, 0xaf, 0x83, 0xfa, 0x80, 0x8a, 0x6c, 0x64,
   1178  1.1.1.2  christos         0xdf, 0x70, 0x64, 0xd5, 0x0a, 0x7c, 0x5a, 0x72, 0xda, 0x66, 0xe6, 0xf9,
   1179  1.1.1.2  christos         0xf5, 0x31, 0x21, 0x92, 0xb0, 0x60, 0x1a, 0xb5, 0xd3, 0xf0, 0xa5, 0xfa,
   1180  1.1.1.2  christos         0x48, 0x95, 0x2e, 0x38, 0xd9, 0xc5, 0xe6, 0xda, 0xfb, 0x6c, 0x03, 0x9d,
   1181  1.1.1.2  christos         0x4b, 0x69, 0xb7, 0x95, 0xe4, 0x5c, 0xc0, 0x93, 0x4f, 0x48, 0xd9, 0x7e,
   1182  1.1.1.2  christos         0x06, 0x22, 0xb2, 0xde, 0xf3, 0x79, 0x24, 0xed, 0xe1, 0xd1, 0x4a, 0x57,
   1183  1.1.1.2  christos         0xf1, 0x40, 0x86, 0x70, 0x42, 0x25, 0xc5, 0x27, 0x68, 0xc9, 0xfa, 0xe5,
   1184  1.1.1.2  christos         0x8e, 0x62, 0x7e, 0xff, 0x49, 0x6c, 0x5b, 0xb5, 0xba, 0xf9, 0xef, 0x9a,
   1185  1.1.1.2  christos         0x1a, 0x10, 0xd4, 0x81, 0x53, 0xcf, 0x83, 0x04, 0x18, 0x1c, 0xe1, 0xdb,
   1186  1.1.1.2  christos         0xe1, 0x65, 0xa9, 0x7f, 0xe1, 0x33, 0xeb, 0xc3, 0x4f, 0xe3, 0xb7, 0x22,
   1187  1.1.1.2  christos         0xf7, 0x1c, 0x09, 0x4f, 0xed, 0xc6, 0x07, 0x8e, 0x78, 0x05, 0x8f, 0x7c,
   1188  1.1.1.2  christos         0x96, 0xd9, 0x12, 0xe0, 0x81, 0x74, 0x1a, 0xe9, 0x13, 0xc0, 0x20, 0x82,
   1189  1.1.1.2  christos         0x65, 0xbb, 0x42, 0x3b, 0xed, 0x08, 0x6a, 0x84, 0x4f, 0xea, 0x77, 0x14,
   1190  1.1.1.2  christos         0x32, 0xf9, 0xed, 0xc2, 0x12, 0xd6, 0xc5, 0xc6, 0xb3, 0xe5, 0xf2, 0x6e,
   1191  1.1.1.2  christos         0xf6, 0x16, 0x7f, 0x37, 0xde, 0xbc, 0x09, 0xc7, 0x06, 0x6b, 0x12, 0xbc,
   1192  1.1.1.2  christos         0xad, 0x2d, 0x49, 0x25, 0xd5, 0xdc, 0xf4, 0x18, 0x14, 0xd2, 0xf0, 0xf1,
   1193  1.1.1.2  christos         0x1d, 0x1f, 0x3a, 0xaa, 0x15, 0x55, 0xbb, 0x0d, 0x7f, 0xbe, 0x67, 0xa1,
   1194  1.1.1.2  christos         0xa7, 0xf0, 0xaa, 0xb3, 0xfb, 0x41, 0x82, 0x39, 0x49, 0x93, 0xbc, 0xa8,
   1195  1.1.1.2  christos         0xee, 0x72, 0x13, 0x45, 0x65, 0x15, 0x42, 0x17, 0xaa, 0xd8, 0xab, 0xcf,
   1196  1.1.1.2  christos         0x33, 0x42, 0x83, 0x42
   1197      1.1  christos     };
   1198      1.1  christos     static const char group_name[] = "ffdhe2048";
   1199      1.1  christos     static const long priv_len = 224;
   1200      1.1  christos 
   1201      1.1  christos     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
   1202      1.1  christos         || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL))
   1203      1.1  christos         || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL))
   1204      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
   1205  1.1.1.2  christos             OSSL_PKEY_PARAM_GROUP_NAME,
   1206  1.1.1.2  christos             group_name, 0))
   1207      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_long(bld, OSSL_PKEY_PARAM_DH_PRIV_LEN,
   1208  1.1.1.2  christos             priv_len))
   1209      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub))
   1210      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv))
   1211      1.1  christos         || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
   1212      1.1  christos         goto err;
   1213      1.1  christos 
   1214      1.1  christos     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL)))
   1215      1.1  christos         goto err;
   1216      1.1  christos 
   1217      1.1  christos     if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
   1218      1.1  christos         || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
   1219  1.1.1.2  christos                             fromdata_params),
   1220  1.1.1.2  christos             1))
   1221      1.1  christos         goto err;
   1222      1.1  christos 
   1223      1.1  christos     for (;;) {
   1224      1.1  christos         ret = 0;
   1225      1.1  christos         if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048)
   1226      1.1  christos             || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112)
   1227      1.1  christos             || !TEST_int_eq(EVP_PKEY_get_size(pk), 256)
   1228      1.1  christos             || !TEST_false(EVP_PKEY_missing_parameters(pk)))
   1229      1.1  christos             goto err;
   1230      1.1  christos 
   1231      1.1  christos         if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk,
   1232  1.1.1.2  christos                 OSSL_PKEY_PARAM_GROUP_NAME,
   1233  1.1.1.2  christos                 name_out,
   1234  1.1.1.2  christos                 sizeof(name_out),
   1235  1.1.1.2  christos                 &len))
   1236      1.1  christos             || !TEST_str_eq(name_out, group_name)
   1237      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY,
   1238  1.1.1.2  christos                 &pub_out))
   1239      1.1  christos             || !TEST_BN_eq(pub, pub_out)
   1240      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY,
   1241  1.1.1.2  christos                 &priv_out))
   1242      1.1  christos             || !TEST_BN_eq(priv, priv_out)
   1243      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p))
   1244      1.1  christos             || !TEST_BN_eq(&ossl_bignum_ffdhe2048_p, p)
   1245      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q))
   1246      1.1  christos             || !TEST_ptr(q)
   1247      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g))
   1248      1.1  christos             || !TEST_BN_eq(&ossl_bignum_const_2, g)
   1249      1.1  christos             || !TEST_false(EVP_PKEY_get_bn_param(pk,
   1250  1.1.1.2  christos                 OSSL_PKEY_PARAM_FFC_COFACTOR,
   1251  1.1.1.2  christos                 &j))
   1252      1.1  christos             || !TEST_ptr_null(j)
   1253      1.1  christos             || !TEST_false(EVP_PKEY_get_octet_string_param(pk,
   1254  1.1.1.2  christos                 OSSL_PKEY_PARAM_FFC_SEED,
   1255  1.1.1.2  christos                 seed_out,
   1256  1.1.1.2  christos                 sizeof(seed_out),
   1257  1.1.1.2  christos                 &len))
   1258      1.1  christos             || !TEST_true(EVP_PKEY_get_int_param(pk,
   1259  1.1.1.2  christos                 OSSL_PKEY_PARAM_FFC_GINDEX,
   1260  1.1.1.2  christos                 &gindex))
   1261      1.1  christos             || !TEST_int_eq(gindex, -1)
   1262      1.1  christos             || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H,
   1263  1.1.1.2  christos                 &hindex))
   1264      1.1  christos             || !TEST_int_eq(hindex, 0)
   1265      1.1  christos             || !TEST_true(EVP_PKEY_get_int_param(pk,
   1266  1.1.1.2  christos                 OSSL_PKEY_PARAM_FFC_PCOUNTER,
   1267  1.1.1.2  christos                 &pcounter))
   1268      1.1  christos             || !TEST_int_eq(pcounter, -1))
   1269      1.1  christos             goto err;
   1270      1.1  christos         BN_free(p);
   1271      1.1  christos         p = NULL;
   1272      1.1  christos         BN_free(q);
   1273      1.1  christos         q = NULL;
   1274      1.1  christos         BN_free(g);
   1275      1.1  christos         g = NULL;
   1276      1.1  christos         BN_free(j);
   1277      1.1  christos         j = NULL;
   1278      1.1  christos         BN_free(pub_out);
   1279      1.1  christos         pub_out = NULL;
   1280      1.1  christos         BN_free(priv_out);
   1281      1.1  christos         priv_out = NULL;
   1282      1.1  christos 
   1283      1.1  christos         if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
   1284      1.1  christos             goto err;
   1285      1.1  christos 
   1286      1.1  christos         if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0)
   1287      1.1  christos             || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0)
   1288      1.1  christos             || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0)
   1289      1.1  christos             || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0))
   1290      1.1  christos             goto err;
   1291      1.1  christos         EVP_PKEY_CTX_free(key_ctx);
   1292      1.1  christos         key_ctx = NULL;
   1293      1.1  christos 
   1294      1.1  christos         ret = test_print_key_using_pem("DH", pk)
   1295  1.1.1.2  christos             && test_print_key_using_encoder("DH", pk);
   1296      1.1  christos 
   1297      1.1  christos         if (!ret || dup_pk != NULL)
   1298      1.1  christos             break;
   1299      1.1  christos 
   1300      1.1  christos         if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
   1301      1.1  christos             goto err;
   1302      1.1  christos         ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
   1303      1.1  christos         EVP_PKEY_free(pk);
   1304      1.1  christos         pk = dup_pk;
   1305      1.1  christos         if (!ret)
   1306      1.1  christos             goto err;
   1307      1.1  christos     }
   1308      1.1  christos err:
   1309      1.1  christos     BN_free(p);
   1310      1.1  christos     BN_free(q);
   1311      1.1  christos     BN_free(g);
   1312      1.1  christos     BN_free(j);
   1313      1.1  christos     BN_free(pub);
   1314      1.1  christos     BN_free(priv);
   1315      1.1  christos     BN_free(pub_out);
   1316      1.1  christos     BN_free(priv_out);
   1317      1.1  christos     EVP_PKEY_free(pk);
   1318      1.1  christos     EVP_PKEY_CTX_free(ctx);
   1319      1.1  christos     EVP_PKEY_CTX_free(key_ctx);
   1320      1.1  christos     OSSL_PARAM_free(fromdata_params);
   1321      1.1  christos     OSSL_PARAM_BLD_free(bld);
   1322      1.1  christos 
   1323      1.1  christos     return ret;
   1324      1.1  christos }
   1325      1.1  christos 
   1326      1.1  christos #endif
   1327      1.1  christos 
   1328      1.1  christos #ifndef OPENSSL_NO_EC
   1329  1.1.1.2  christos #ifndef OPENSSL_NO_ECX
   1330      1.1  christos /* Array indexes used in test_fromdata_ecx */
   1331  1.1.1.2  christos #define PRIV_KEY 0
   1332  1.1.1.2  christos #define PUB_KEY 1
   1333      1.1  christos 
   1334  1.1.1.2  christos #define X25519_IDX 0
   1335  1.1.1.2  christos #define X448_IDX 1
   1336  1.1.1.2  christos #define ED25519_IDX 2
   1337  1.1.1.2  christos #define ED448_IDX 3
   1338      1.1  christos 
   1339      1.1  christos /*
   1340      1.1  christos  * tst uses indexes 0 ... (3 * 4 - 1)
   1341      1.1  christos  * For the 4 ECX key types (X25519_IDX..ED448_IDX)
   1342      1.1  christos  * 0..3  = public + private key.
   1343      1.1  christos  * 4..7  = private key (This will generate the public key from the private key)
   1344      1.1  christos  * 8..11 = public key
   1345      1.1  christos  */
   1346      1.1  christos static int test_fromdata_ecx(int tst)
   1347      1.1  christos {
   1348      1.1  christos     int ret = 0;
   1349      1.1  christos     EVP_PKEY_CTX *ctx = NULL, *ctx2 = NULL;
   1350      1.1  christos     EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL;
   1351      1.1  christos     const char *alg = NULL;
   1352      1.1  christos     size_t len;
   1353      1.1  christos     unsigned char out_pub[ED448_KEYLEN];
   1354      1.1  christos     unsigned char out_priv[ED448_KEYLEN];
   1355      1.1  christos     OSSL_PARAM params[3] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
   1356      1.1  christos 
   1357      1.1  christos     /* ED448_KEYLEN > X448_KEYLEN > X25519_KEYLEN == ED25519_KEYLEN */
   1358      1.1  christos     static unsigned char key_numbers[4][2][ED448_KEYLEN] = {
   1359      1.1  christos         /* X25519: Keys from RFC 7748 6.1 */
   1360      1.1  christos         {
   1361      1.1  christos             /* Private Key */
   1362      1.1  christos             {
   1363      1.1  christos                 0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d, 0x3c, 0x16,
   1364      1.1  christos                 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45, 0xdf, 0x4c, 0x2f, 0x87,
   1365      1.1  christos                 0xeb, 0xc0, 0x99, 0x2a, 0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9,
   1366  1.1.1.2  christos                 0x2c, 0x2a },
   1367      1.1  christos             /* Public Key */
   1368      1.1  christos             {
   1369      1.1  christos                 0x85, 0x20, 0xf0, 0x09, 0x89, 0x30, 0xa7, 0x54, 0x74, 0x8b,
   1370      1.1  christos                 0x7d, 0xdc, 0xb4, 0x3e, 0xf7, 0x5a, 0x0d, 0xbf, 0x3a, 0x0d,
   1371      1.1  christos                 0x26, 0x38, 0x1a, 0xf4, 0xeb, 0xa4, 0xa9, 0x8e, 0xaa, 0x9b,
   1372  1.1.1.2  christos                 0x4e, 0x6a } },
   1373      1.1  christos         /* X448: Keys from RFC 7748 6.2 */
   1374      1.1  christos         {
   1375      1.1  christos             /* Private Key */
   1376      1.1  christos             {
   1377      1.1  christos                 0x9a, 0x8f, 0x49, 0x25, 0xd1, 0x51, 0x9f, 0x57, 0x75, 0xcf,
   1378      1.1  christos                 0x46, 0xb0, 0x4b, 0x58, 0x00, 0xd4, 0xee, 0x9e, 0xe8, 0xba,
   1379      1.1  christos                 0xe8, 0xbc, 0x55, 0x65, 0xd4, 0x98, 0xc2, 0x8d, 0xd9, 0xc9,
   1380      1.1  christos                 0xba, 0xf5, 0x74, 0xa9, 0x41, 0x97, 0x44, 0x89, 0x73, 0x91,
   1381      1.1  christos                 0x00, 0x63, 0x82, 0xa6, 0xf1, 0x27, 0xab, 0x1d, 0x9a, 0xc2,
   1382  1.1.1.2  christos                 0xd8, 0xc0, 0xa5, 0x98, 0x72, 0x6b },
   1383      1.1  christos             /* Public Key */
   1384      1.1  christos             {
   1385      1.1  christos                 0x9b, 0x08, 0xf7, 0xcc, 0x31, 0xb7, 0xe3, 0xe6, 0x7d, 0x22,
   1386      1.1  christos                 0xd5, 0xae, 0xa1, 0x21, 0x07, 0x4a, 0x27, 0x3b, 0xd2, 0xb8,
   1387      1.1  christos                 0x3d, 0xe0, 0x9c, 0x63, 0xfa, 0xa7, 0x3d, 0x2c, 0x22, 0xc5,
   1388      1.1  christos                 0xd9, 0xbb, 0xc8, 0x36, 0x64, 0x72, 0x41, 0xd9, 0x53, 0xd4,
   1389      1.1  christos                 0x0c, 0x5b, 0x12, 0xda, 0x88, 0x12, 0x0d, 0x53, 0x17, 0x7f,
   1390  1.1.1.2  christos                 0x80, 0xe5, 0x32, 0xc4, 0x1f, 0xa0 } },
   1391      1.1  christos         /* ED25519: Keys from RFC 8032 */
   1392      1.1  christos         {
   1393      1.1  christos             /* Private Key */
   1394      1.1  christos             {
   1395      1.1  christos                 0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84,
   1396      1.1  christos                 0x4a, 0xf4, 0x92, 0xec, 0x2c, 0xc4, 0x44, 0x49, 0xc5, 0x69,
   1397      1.1  christos                 0x7b, 0x32, 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae,
   1398  1.1.1.2  christos                 0x7f, 0x60 },
   1399      1.1  christos             /* Public Key */
   1400      1.1  christos             {
   1401      1.1  christos                 0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b,
   1402      1.1  christos                 0xfe, 0xd3, 0xc9, 0x64, 0x07, 0x3a, 0x0e, 0xe1, 0x72, 0xf3,
   1403      1.1  christos                 0xda, 0xa6, 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07,
   1404  1.1.1.2  christos                 0x51, 0x1a } },
   1405      1.1  christos         /* ED448: Keys from RFC 8032 */
   1406      1.1  christos         {
   1407      1.1  christos             /* Private Key */
   1408      1.1  christos             {
   1409      1.1  christos                 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10, 0xd6, 0x32,
   1410      1.1  christos                 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf, 0x6c, 0x92, 0x9f, 0x34,
   1411      1.1  christos                 0xdd, 0xfa, 0x8c, 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3,
   1412      1.1  christos                 0x48, 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
   1413      1.1  christos                 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f, 0x03, 0x2e,
   1414  1.1.1.2  christos                 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9, 0x5b },
   1415      1.1  christos             /* Public Key */
   1416      1.1  christos             {
   1417      1.1  christos                 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7,
   1418      1.1  christos                 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
   1419      1.1  christos                 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9,
   1420      1.1  christos                 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
   1421      1.1  christos                 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa,
   1422  1.1.1.2  christos                 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61, 0x80 } }
   1423      1.1  christos     };
   1424      1.1  christos     OSSL_PARAM x25519_fromdata_params[] = {
   1425      1.1  christos         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
   1426  1.1.1.2  christos             key_numbers[X25519_IDX][PRIV_KEY],
   1427  1.1.1.2  christos             X25519_KEYLEN),
   1428      1.1  christos         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
   1429  1.1.1.2  christos             key_numbers[X25519_IDX][PUB_KEY],
   1430  1.1.1.2  christos             X25519_KEYLEN),
   1431      1.1  christos         OSSL_PARAM_END
   1432      1.1  christos     };
   1433      1.1  christos     OSSL_PARAM x448_fromdata_params[] = {
   1434      1.1  christos         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
   1435  1.1.1.2  christos             key_numbers[X448_IDX][PRIV_KEY],
   1436  1.1.1.2  christos             X448_KEYLEN),
   1437      1.1  christos         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
   1438  1.1.1.2  christos             key_numbers[X448_IDX][PUB_KEY],
   1439  1.1.1.2  christos             X448_KEYLEN),
   1440      1.1  christos         OSSL_PARAM_END
   1441      1.1  christos     };
   1442      1.1  christos     OSSL_PARAM ed25519_fromdata_params[] = {
   1443      1.1  christos         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
   1444  1.1.1.2  christos             key_numbers[ED25519_IDX][PRIV_KEY],
   1445  1.1.1.2  christos             ED25519_KEYLEN),
   1446      1.1  christos         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
   1447  1.1.1.2  christos             key_numbers[ED25519_IDX][PUB_KEY],
   1448  1.1.1.2  christos             ED25519_KEYLEN),
   1449      1.1  christos         OSSL_PARAM_END
   1450      1.1  christos     };
   1451      1.1  christos     OSSL_PARAM ed448_fromdata_params[] = {
   1452      1.1  christos         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
   1453  1.1.1.2  christos             key_numbers[ED448_IDX][PRIV_KEY],
   1454  1.1.1.2  christos             ED448_KEYLEN),
   1455      1.1  christos         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
   1456  1.1.1.2  christos             key_numbers[ED448_IDX][PUB_KEY],
   1457  1.1.1.2  christos             ED448_KEYLEN),
   1458      1.1  christos         OSSL_PARAM_END
   1459      1.1  christos     };
   1460      1.1  christos     OSSL_PARAM *fromdata_params = NULL;
   1461      1.1  christos     int bits = 0, security_bits = 0, size = 0;
   1462      1.1  christos     OSSL_PARAM *orig_fromdata_params = NULL;
   1463      1.1  christos 
   1464      1.1  christos     switch (tst & 3) {
   1465      1.1  christos     case X25519_IDX:
   1466      1.1  christos         fromdata_params = x25519_fromdata_params;
   1467      1.1  christos         bits = X25519_BITS;
   1468      1.1  christos         security_bits = X25519_SECURITY_BITS;
   1469      1.1  christos         size = X25519_KEYLEN;
   1470      1.1  christos         alg = "X25519";
   1471      1.1  christos         break;
   1472      1.1  christos 
   1473      1.1  christos     case X448_IDX:
   1474      1.1  christos         fromdata_params = x448_fromdata_params;
   1475      1.1  christos         bits = X448_BITS;
   1476      1.1  christos         security_bits = X448_SECURITY_BITS;
   1477      1.1  christos         size = X448_KEYLEN;
   1478      1.1  christos         alg = "X448";
   1479      1.1  christos         break;
   1480      1.1  christos 
   1481      1.1  christos     case ED25519_IDX:
   1482      1.1  christos         fromdata_params = ed25519_fromdata_params;
   1483      1.1  christos         bits = ED25519_BITS;
   1484      1.1  christos         security_bits = ED25519_SECURITY_BITS;
   1485      1.1  christos         size = ED25519_SIGSIZE;
   1486      1.1  christos         alg = "ED25519";
   1487      1.1  christos         break;
   1488      1.1  christos 
   1489      1.1  christos     case ED448_IDX:
   1490      1.1  christos         fromdata_params = ed448_fromdata_params;
   1491      1.1  christos         bits = ED448_BITS;
   1492      1.1  christos         security_bits = ED448_SECURITY_BITS;
   1493      1.1  christos         size = ED448_SIGSIZE;
   1494      1.1  christos         alg = "ED448";
   1495      1.1  christos         break;
   1496      1.1  christos     default:
   1497      1.1  christos         goto err;
   1498      1.1  christos     }
   1499      1.1  christos 
   1500      1.1  christos     ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL);
   1501      1.1  christos     if (!TEST_ptr(ctx))
   1502      1.1  christos         goto err;
   1503      1.1  christos 
   1504      1.1  christos     orig_fromdata_params = fromdata_params;
   1505      1.1  christos     if (tst > 7) {
   1506      1.1  christos         /* public key only */
   1507      1.1  christos         fromdata_params++;
   1508      1.1  christos     } else if (tst > 3) {
   1509      1.1  christos         /* private key only */
   1510      1.1  christos         params[0] = fromdata_params[0];
   1511      1.1  christos         params[1] = fromdata_params[2];
   1512      1.1  christos         fromdata_params = params;
   1513      1.1  christos     }
   1514      1.1  christos 
   1515      1.1  christos     if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
   1516      1.1  christos         || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
   1517  1.1.1.2  christos                             fromdata_params),
   1518  1.1.1.2  christos             1))
   1519      1.1  christos         goto err;
   1520      1.1  christos 
   1521      1.1  christos     for (;;) {
   1522      1.1  christos         ret = 0;
   1523      1.1  christos         if (!TEST_int_eq(EVP_PKEY_get_bits(pk), bits)
   1524      1.1  christos             || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), security_bits)
   1525      1.1  christos             || !TEST_int_eq(EVP_PKEY_get_size(pk), size)
   1526      1.1  christos             || !TEST_false(EVP_PKEY_missing_parameters(pk)))
   1527      1.1  christos             goto err;
   1528      1.1  christos 
   1529      1.1  christos         if (!TEST_ptr(ctx2 = EVP_PKEY_CTX_new_from_pkey(NULL, pk, NULL)))
   1530      1.1  christos             goto err;
   1531      1.1  christos         if (tst <= 7) {
   1532      1.1  christos             if (!TEST_int_gt(EVP_PKEY_check(ctx2), 0))
   1533      1.1  christos                 goto err;
   1534      1.1  christos             if (!TEST_true(EVP_PKEY_get_octet_string_param(
   1535  1.1.1.2  christos                     pk, orig_fromdata_params[PRIV_KEY].key,
   1536  1.1.1.2  christos                     out_priv, sizeof(out_priv), &len))
   1537      1.1  christos                 || !TEST_mem_eq(out_priv, len,
   1538  1.1.1.2  christos                     orig_fromdata_params[PRIV_KEY].data,
   1539  1.1.1.2  christos                     orig_fromdata_params[PRIV_KEY].data_size)
   1540      1.1  christos                 || !TEST_true(EVP_PKEY_get_octet_string_param(
   1541  1.1.1.2  christos                     pk, orig_fromdata_params[PUB_KEY].key,
   1542  1.1.1.2  christos                     out_pub, sizeof(out_pub), &len))
   1543      1.1  christos                 || !TEST_mem_eq(out_pub, len,
   1544  1.1.1.2  christos                     orig_fromdata_params[PUB_KEY].data,
   1545  1.1.1.2  christos                     orig_fromdata_params[PUB_KEY].data_size))
   1546      1.1  christos                 goto err;
   1547      1.1  christos         } else {
   1548      1.1  christos             /* The private key check should fail if there is only a public key */
   1549      1.1  christos             if (!TEST_int_gt(EVP_PKEY_public_check(ctx2), 0)
   1550      1.1  christos                 || !TEST_int_le(EVP_PKEY_private_check(ctx2), 0)
   1551      1.1  christos                 || !TEST_int_le(EVP_PKEY_check(ctx2), 0))
   1552      1.1  christos                 goto err;
   1553      1.1  christos         }
   1554      1.1  christos         EVP_PKEY_CTX_free(ctx2);
   1555      1.1  christos         ctx2 = NULL;
   1556      1.1  christos 
   1557      1.1  christos         if (!TEST_ptr(copy_pk = EVP_PKEY_new())
   1558  1.1.1.2  christos             /* This should succeed because there are no parameters to copy */
   1559      1.1  christos             || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk)))
   1560      1.1  christos             goto err;
   1561      1.1  christos         if (!TEST_ptr(ctx2 = EVP_PKEY_CTX_new_from_pkey(NULL, copy_pk, NULL))
   1562  1.1.1.2  christos             /* This should fail because copy_pk has no pubkey */
   1563      1.1  christos             || !TEST_int_le(EVP_PKEY_public_check(ctx2), 0))
   1564      1.1  christos             goto err;
   1565      1.1  christos         EVP_PKEY_CTX_free(ctx2);
   1566      1.1  christos         ctx2 = NULL;
   1567      1.1  christos         EVP_PKEY_free(copy_pk);
   1568      1.1  christos         copy_pk = NULL;
   1569      1.1  christos 
   1570      1.1  christos         if (tst > 7)
   1571      1.1  christos             ret = test_print_key_using_encoder_public(alg, pk);
   1572      1.1  christos         else
   1573      1.1  christos             ret = test_print_key_using_pem(alg, pk)
   1574  1.1.1.2  christos                 && test_print_key_using_encoder(alg, pk);
   1575      1.1  christos 
   1576      1.1  christos         if (!ret || dup_pk != NULL)
   1577      1.1  christos             break;
   1578      1.1  christos 
   1579      1.1  christos         if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
   1580      1.1  christos             goto err;
   1581      1.1  christos         ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
   1582      1.1  christos         EVP_PKEY_free(pk);
   1583      1.1  christos         pk = dup_pk;
   1584      1.1  christos         if (!ret)
   1585      1.1  christos             goto err;
   1586      1.1  christos     }
   1587      1.1  christos 
   1588      1.1  christos err:
   1589      1.1  christos     EVP_PKEY_free(pk);
   1590      1.1  christos     EVP_PKEY_free(copy_pk);
   1591      1.1  christos     EVP_PKEY_CTX_free(ctx);
   1592      1.1  christos     EVP_PKEY_CTX_free(ctx2);
   1593      1.1  christos 
   1594      1.1  christos     return ret;
   1595      1.1  christos }
   1596  1.1.1.2  christos #endif /* OPENSSL_NO_ECX */
   1597      1.1  christos 
   1598      1.1  christos static int test_fromdata_ec(void)
   1599      1.1  christos {
   1600      1.1  christos     int ret = 0;
   1601      1.1  christos     EVP_PKEY_CTX *ctx = NULL;
   1602      1.1  christos     EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL;
   1603      1.1  christos     OSSL_PARAM_BLD *bld = NULL;
   1604      1.1  christos     BIGNUM *ec_priv_bn = NULL;
   1605      1.1  christos     BIGNUM *bn_priv = NULL;
   1606      1.1  christos     OSSL_PARAM *fromdata_params = NULL;
   1607      1.1  christos     const char *alg = "EC";
   1608      1.1  christos     const char *curve = "prime256v1";
   1609      1.1  christos     const char bad_curve[] = "nonexistent-curve";
   1610      1.1  christos     OSSL_PARAM nokey_params[2] = {
   1611  1.1.1.2  christos         OSSL_PARAM_END,
   1612  1.1.1.2  christos         OSSL_PARAM_END
   1613      1.1  christos     };
   1614      1.1  christos     /* UNCOMPRESSED FORMAT */
   1615      1.1  christos     static const unsigned char ec_pub_keydata[] = {
   1616  1.1.1.2  christos         POINT_CONVERSION_UNCOMPRESSED,
   1617  1.1.1.2  christos         0x1b, 0x93, 0x67, 0x55, 0x1c, 0x55, 0x9f, 0x63,
   1618  1.1.1.2  christos         0xd1, 0x22, 0xa4, 0xd8, 0xd1, 0x0a, 0x60, 0x6d,
   1619  1.1.1.2  christos         0x02, 0xa5, 0x77, 0x57, 0xc8, 0xa3, 0x47, 0x73,
   1620  1.1.1.2  christos         0x3a, 0x6a, 0x08, 0x28, 0x39, 0xbd, 0xc9, 0xd2,
   1621  1.1.1.2  christos         0x80, 0xec, 0xe9, 0xa7, 0x08, 0x29, 0x71, 0x2f,
   1622  1.1.1.2  christos         0xc9, 0x56, 0x82, 0xee, 0x9a, 0x85, 0x0f, 0x6d,
   1623  1.1.1.2  christos         0x7f, 0x59, 0x5f, 0x8c, 0xd1, 0x96, 0x0b, 0xdf,
   1624  1.1.1.2  christos         0x29, 0x3e, 0x49, 0x07, 0x88, 0x3f, 0x9a, 0x29
   1625      1.1  christos     };
   1626      1.1  christos     /* SAME BUT COMPRESSED FORMAT */
   1627      1.1  christos     static const unsigned char ec_pub_keydata_compressed[] = {
   1628  1.1.1.2  christos         POINT_CONVERSION_COMPRESSED + 1,
   1629  1.1.1.2  christos         0x1b, 0x93, 0x67, 0x55, 0x1c, 0x55, 0x9f, 0x63,
   1630  1.1.1.2  christos         0xd1, 0x22, 0xa4, 0xd8, 0xd1, 0x0a, 0x60, 0x6d,
   1631  1.1.1.2  christos         0x02, 0xa5, 0x77, 0x57, 0xc8, 0xa3, 0x47, 0x73,
   1632  1.1.1.2  christos         0x3a, 0x6a, 0x08, 0x28, 0x39, 0xbd, 0xc9, 0xd2
   1633      1.1  christos     };
   1634      1.1  christos     static const unsigned char ec_priv_keydata[] = {
   1635      1.1  christos         0x33, 0xd0, 0x43, 0x83, 0xa9, 0x89, 0x56, 0x03,
   1636      1.1  christos         0xd2, 0xd7, 0xfe, 0x6b, 0x01, 0x6f, 0xe4, 0x59,
   1637      1.1  christos         0xcc, 0x0d, 0x9a, 0x24, 0x6c, 0x86, 0x1b, 0x2e,
   1638      1.1  christos         0xdc, 0x4b, 0x4d, 0x35, 0x43, 0xe1, 0x1b, 0xad
   1639      1.1  christos     };
   1640      1.1  christos     unsigned char out_pub[sizeof(ec_pub_keydata)];
   1641      1.1  christos     char out_curve_name[80];
   1642      1.1  christos     const OSSL_PARAM *gettable = NULL;
   1643      1.1  christos     size_t len;
   1644      1.1  christos     EC_GROUP *group = NULL;
   1645      1.1  christos     BIGNUM *group_a = NULL;
   1646      1.1  christos     BIGNUM *group_b = NULL;
   1647      1.1  christos     BIGNUM *group_p = NULL;
   1648      1.1  christos     BIGNUM *a = NULL;
   1649      1.1  christos     BIGNUM *b = NULL;
   1650      1.1  christos     BIGNUM *p = NULL;
   1651      1.1  christos 
   1652      1.1  christos     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()))
   1653      1.1  christos         goto err;
   1654      1.1  christos     if (!TEST_ptr(ec_priv_bn = BN_bin2bn(ec_priv_keydata,
   1655  1.1.1.2  christos                       sizeof(ec_priv_keydata), NULL)))
   1656      1.1  christos         goto err;
   1657      1.1  christos 
   1658      1.1  christos     if (OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME,
   1659  1.1.1.2  christos             curve, 0)
   1660  1.1.1.2  christos         <= 0)
   1661      1.1  christos         goto err;
   1662      1.1  christos     /*
   1663      1.1  christos      * We intentionally provide the input point in compressed format,
   1664      1.1  christos      * and avoid setting `OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT`.
   1665      1.1  christos      *
   1666      1.1  christos      * Later on we check what format is used when exporting the
   1667      1.1  christos      * `OSSL_PKEY_PARAM_PUB_KEY` and expect to default to uncompressed
   1668      1.1  christos      * format.
   1669      1.1  christos      */
   1670      1.1  christos     if (OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY,
   1671  1.1.1.2  christos             ec_pub_keydata_compressed,
   1672  1.1.1.2  christos             sizeof(ec_pub_keydata_compressed))
   1673  1.1.1.2  christos         <= 0)
   1674      1.1  christos         goto err;
   1675      1.1  christos     if (OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, ec_priv_bn) <= 0)
   1676      1.1  christos         goto err;
   1677      1.1  christos     if (!TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
   1678      1.1  christos         goto err;
   1679      1.1  christos     ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL);
   1680      1.1  christos     if (!TEST_ptr(ctx))
   1681      1.1  christos         goto err;
   1682      1.1  christos 
   1683      1.1  christos     /* try importing parameters with bad curve first */
   1684  1.1.1.2  christos     nokey_params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
   1685  1.1.1.2  christos         (char *)bad_curve, sizeof(bad_curve));
   1686      1.1  christos     if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
   1687      1.1  christos         || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEY_PARAMETERS,
   1688  1.1.1.2  christos                             nokey_params),
   1689  1.1.1.2  christos             0)
   1690      1.1  christos         || !TEST_ptr_null(pk))
   1691      1.1  christos         goto err;
   1692      1.1  christos 
   1693      1.1  christos     if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
   1694      1.1  christos         || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
   1695  1.1.1.2  christos                             fromdata_params),
   1696  1.1.1.2  christos             1))
   1697      1.1  christos         goto err;
   1698      1.1  christos 
   1699      1.1  christos     for (;;) {
   1700      1.1  christos         ret = 0;
   1701      1.1  christos         if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 256)
   1702      1.1  christos             || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 128)
   1703      1.1  christos             || !TEST_int_eq(EVP_PKEY_get_size(pk), 2 + 35 * 2)
   1704      1.1  christos             || !TEST_false(EVP_PKEY_missing_parameters(pk)))
   1705      1.1  christos             goto err;
   1706      1.1  christos 
   1707      1.1  christos         if (!TEST_ptr(copy_pk = EVP_PKEY_new())
   1708      1.1  christos             || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk)))
   1709      1.1  christos             goto err;
   1710      1.1  christos         EVP_PKEY_free(copy_pk);
   1711      1.1  christos         copy_pk = NULL;
   1712      1.1  christos 
   1713      1.1  christos         if (!TEST_ptr(gettable = EVP_PKEY_gettable_params(pk))
   1714      1.1  christos             || !TEST_ptr(OSSL_PARAM_locate_const(gettable,
   1715  1.1.1.2  christos                 OSSL_PKEY_PARAM_GROUP_NAME))
   1716      1.1  christos             || !TEST_ptr(OSSL_PARAM_locate_const(gettable,
   1717  1.1.1.2  christos                 OSSL_PKEY_PARAM_PUB_KEY))
   1718      1.1  christos             || !TEST_ptr(OSSL_PARAM_locate_const(gettable,
   1719  1.1.1.2  christos                 OSSL_PKEY_PARAM_PRIV_KEY)))
   1720      1.1  christos             goto err;
   1721      1.1  christos 
   1722      1.1  christos         if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(OBJ_sn2nid(curve)))
   1723      1.1  christos             || !TEST_ptr(group_p = BN_new())
   1724      1.1  christos             || !TEST_ptr(group_a = BN_new())
   1725      1.1  christos             || !TEST_ptr(group_b = BN_new())
   1726      1.1  christos             || !TEST_true(EC_GROUP_get_curve(group, group_p, group_a, group_b, NULL)))
   1727      1.1  christos             goto err;
   1728      1.1  christos 
   1729      1.1  christos         if (!TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_EC_A, &a))
   1730      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_EC_B, &b))
   1731      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_EC_P, &p)))
   1732      1.1  christos             goto err;
   1733      1.1  christos 
   1734      1.1  christos         if (!TEST_BN_eq(group_p, p) || !TEST_BN_eq(group_a, a)
   1735      1.1  christos             || !TEST_BN_eq(group_b, b))
   1736      1.1  christos             goto err;
   1737      1.1  christos 
   1738      1.1  christos         EC_GROUP_free(group);
   1739      1.1  christos         group = NULL;
   1740      1.1  christos         BN_free(group_p);
   1741      1.1  christos         group_p = NULL;
   1742      1.1  christos         BN_free(group_a);
   1743      1.1  christos         group_a = NULL;
   1744      1.1  christos         BN_free(group_b);
   1745      1.1  christos         group_b = NULL;
   1746      1.1  christos 
   1747      1.1  christos         if (!EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME,
   1748  1.1.1.2  christos                 out_curve_name,
   1749  1.1.1.2  christos                 sizeof(out_curve_name),
   1750  1.1.1.2  christos                 &len)
   1751      1.1  christos             || !TEST_str_eq(out_curve_name, curve)
   1752      1.1  christos             || !EVP_PKEY_get_octet_string_param(pk, OSSL_PKEY_PARAM_PUB_KEY,
   1753  1.1.1.2  christos                 out_pub, sizeof(out_pub), &len)
   1754      1.1  christos 
   1755      1.1  christos             /*
   1756      1.1  christos              * Our providers use uncompressed format by default if
   1757      1.1  christos              * `OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT` was not
   1758      1.1  christos              * explicitly set, irrespective of the format used for the
   1759      1.1  christos              * input point given as a param to create this key.
   1760      1.1  christos              */
   1761      1.1  christos             || !TEST_true(out_pub[0] == POINT_CONVERSION_UNCOMPRESSED)
   1762      1.1  christos             || !TEST_mem_eq(out_pub + 1, len - 1,
   1763  1.1.1.2  christos                 ec_pub_keydata + 1, sizeof(ec_pub_keydata) - 1)
   1764      1.1  christos 
   1765      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY,
   1766  1.1.1.2  christos                 &bn_priv))
   1767      1.1  christos             || !TEST_BN_eq(ec_priv_bn, bn_priv))
   1768      1.1  christos             goto err;
   1769      1.1  christos         BN_free(bn_priv);
   1770      1.1  christos         bn_priv = NULL;
   1771      1.1  christos 
   1772      1.1  christos         ret = test_print_key_using_pem(alg, pk)
   1773  1.1.1.2  christos             && test_print_key_using_encoder(alg, pk);
   1774      1.1  christos 
   1775      1.1  christos         if (!ret || dup_pk != NULL)
   1776      1.1  christos             break;
   1777      1.1  christos 
   1778      1.1  christos         if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
   1779      1.1  christos             goto err;
   1780      1.1  christos         ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
   1781      1.1  christos         EVP_PKEY_free(pk);
   1782      1.1  christos         pk = dup_pk;
   1783      1.1  christos         if (!ret)
   1784      1.1  christos             goto err;
   1785      1.1  christos     }
   1786      1.1  christos 
   1787      1.1  christos err:
   1788      1.1  christos     EC_GROUP_free(group);
   1789      1.1  christos     BN_free(group_a);
   1790      1.1  christos     BN_free(group_b);
   1791      1.1  christos     BN_free(group_p);
   1792      1.1  christos     BN_free(a);
   1793      1.1  christos     BN_free(b);
   1794      1.1  christos     BN_free(p);
   1795      1.1  christos     BN_free(bn_priv);
   1796      1.1  christos     BN_free(ec_priv_bn);
   1797      1.1  christos     OSSL_PARAM_free(fromdata_params);
   1798      1.1  christos     OSSL_PARAM_BLD_free(bld);
   1799      1.1  christos     EVP_PKEY_free(pk);
   1800      1.1  christos     EVP_PKEY_free(copy_pk);
   1801      1.1  christos     EVP_PKEY_CTX_free(ctx);
   1802      1.1  christos     return ret;
   1803      1.1  christos }
   1804      1.1  christos 
   1805      1.1  christos static int test_ec_dup_no_operation(void)
   1806      1.1  christos {
   1807      1.1  christos     int ret = 0;
   1808      1.1  christos     EVP_PKEY_CTX *pctx = NULL, *ctx = NULL, *kctx = NULL;
   1809      1.1  christos     EVP_PKEY *param = NULL, *pkey = NULL;
   1810      1.1  christos 
   1811      1.1  christos     if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL))
   1812      1.1  christos         || !TEST_int_gt(EVP_PKEY_paramgen_init(pctx), 0)
   1813      1.1  christos         || !TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
   1814  1.1.1.2  christos                             NID_X9_62_prime256v1),
   1815  1.1.1.2  christos             0)
   1816      1.1  christos         || !TEST_int_gt(EVP_PKEY_paramgen(pctx, &param), 0)
   1817      1.1  christos         || !TEST_ptr(param))
   1818      1.1  christos         goto err;
   1819      1.1  christos 
   1820      1.1  christos     EVP_PKEY_CTX_free(pctx);
   1821      1.1  christos     pctx = NULL;
   1822      1.1  christos 
   1823      1.1  christos     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(NULL, param, NULL))
   1824      1.1  christos         || !TEST_ptr(kctx = EVP_PKEY_CTX_dup(ctx))
   1825      1.1  christos         || !TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0)
   1826      1.1  christos         || !TEST_int_gt(EVP_PKEY_keygen(kctx, &pkey), 0))
   1827      1.1  christos         goto err;
   1828      1.1  christos     ret = 1;
   1829      1.1  christos err:
   1830      1.1  christos     EVP_PKEY_free(pkey);
   1831      1.1  christos     EVP_PKEY_free(param);
   1832      1.1  christos     EVP_PKEY_CTX_free(ctx);
   1833      1.1  christos     EVP_PKEY_CTX_free(kctx);
   1834      1.1  christos     EVP_PKEY_CTX_free(pctx);
   1835      1.1  christos     return ret;
   1836      1.1  christos }
   1837      1.1  christos 
   1838      1.1  christos /* Test that keygen doesn't support EVP_PKEY_CTX_dup */
   1839      1.1  christos static int test_ec_dup_keygen_operation(void)
   1840      1.1  christos {
   1841      1.1  christos     int ret = 0;
   1842      1.1  christos     EVP_PKEY_CTX *pctx = NULL, *ctx = NULL, *kctx = NULL;
   1843      1.1  christos     EVP_PKEY *param = NULL, *pkey = NULL;
   1844      1.1  christos 
   1845      1.1  christos     if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL))
   1846      1.1  christos         || !TEST_int_gt(EVP_PKEY_paramgen_init(pctx), 0)
   1847      1.1  christos         || !TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
   1848  1.1.1.2  christos                             NID_X9_62_prime256v1),
   1849  1.1.1.2  christos             0)
   1850      1.1  christos         || !TEST_int_gt(EVP_PKEY_paramgen(pctx, &param), 0)
   1851      1.1  christos         || !TEST_ptr(param))
   1852      1.1  christos         goto err;
   1853      1.1  christos 
   1854      1.1  christos     EVP_PKEY_CTX_free(pctx);
   1855      1.1  christos     pctx = NULL;
   1856      1.1  christos 
   1857      1.1  christos     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(NULL, param, NULL))
   1858      1.1  christos         || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
   1859      1.1  christos         || !TEST_ptr_null(kctx = EVP_PKEY_CTX_dup(ctx)))
   1860      1.1  christos         goto err;
   1861      1.1  christos     ret = 1;
   1862      1.1  christos err:
   1863      1.1  christos     EVP_PKEY_free(pkey);
   1864      1.1  christos     EVP_PKEY_free(param);
   1865      1.1  christos     EVP_PKEY_CTX_free(ctx);
   1866      1.1  christos     EVP_PKEY_CTX_free(kctx);
   1867      1.1  christos     EVP_PKEY_CTX_free(pctx);
   1868      1.1  christos     return ret;
   1869      1.1  christos }
   1870      1.1  christos 
   1871      1.1  christos #endif /* OPENSSL_NO_EC */
   1872      1.1  christos 
   1873      1.1  christos #ifndef OPENSSL_NO_DSA
   1874      1.1  christos static int test_fromdata_dsa_fips186_4(void)
   1875      1.1  christos {
   1876      1.1  christos     int ret = 0;
   1877      1.1  christos     EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
   1878      1.1  christos     EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL;
   1879      1.1  christos     BIGNUM *pub = NULL, *priv = NULL;
   1880      1.1  christos     BIGNUM *p = NULL, *q = NULL, *g = NULL;
   1881      1.1  christos     BIGNUM *pub_out = NULL, *priv_out = NULL;
   1882      1.1  christos     BIGNUM *p_out = NULL, *q_out = NULL, *g_out = NULL, *j_out = NULL;
   1883      1.1  christos     int gindex_out = 0, pcounter_out = 0, hindex_out = 0;
   1884      1.1  christos     char name_out[80];
   1885      1.1  christos     unsigned char seed_out[32];
   1886      1.1  christos     size_t len;
   1887      1.1  christos     OSSL_PARAM_BLD *bld = NULL;
   1888      1.1  christos     OSSL_PARAM *fromdata_params = NULL;
   1889      1.1  christos 
   1890      1.1  christos     /*
   1891      1.1  christos      * DSA parameter data was generated using the following:
   1892      1.1  christos      * openssl genpkey -genparam -algorithm DSA -pkeyopt pbits:2048 \
   1893      1.1  christos      *                 -pkeyopt qbits:256 -pkeyopt type:0 \
   1894      1.1  christos      *                 -pkeyopt gindex:1 -out dsa_params.pem -text
   1895      1.1  christos      */
   1896      1.1  christos     static const unsigned char p_data[] = {
   1897      1.1  christos         0x00, 0xa0, 0xb7, 0x02, 0xc4, 0xac, 0xa6, 0x42, 0xab, 0xf2, 0x34, 0x0b,
   1898      1.1  christos         0x22, 0x47, 0x1f, 0x33, 0xcf, 0xd5, 0x04, 0xe4, 0x3e, 0xec, 0xa1, 0x21,
   1899      1.1  christos         0xc8, 0x41, 0x2b, 0xef, 0xb8, 0x1f, 0x0b, 0x5b, 0x88, 0x8b, 0x67, 0xf8,
   1900      1.1  christos         0x68, 0x6d, 0x7c, 0x4d, 0x96, 0x5f, 0x3c, 0x66, 0xef, 0x58, 0x34, 0xd7,
   1901      1.1  christos         0xf6, 0xa2, 0x1b, 0xad, 0xc8, 0x12, 0x52, 0xb8, 0xe8, 0x2a, 0x63, 0xcc,
   1902      1.1  christos         0xea, 0xe7, 0x4e, 0xc8, 0x34, 0x4c, 0x58, 0x59, 0x0a, 0xc2, 0x4a, 0xe4,
   1903      1.1  christos         0xb4, 0x64, 0x20, 0xf4, 0xf6, 0x0a, 0xcf, 0x86, 0x01, 0x6c, 0x7f, 0x23,
   1904      1.1  christos         0x4a, 0x51, 0x07, 0x99, 0x42, 0x28, 0x7a, 0xff, 0x18, 0x67, 0x52, 0x64,
   1905      1.1  christos         0xf2, 0x9a, 0x62, 0x30, 0xc3, 0x00, 0xde, 0x23, 0xe9, 0x11, 0x95, 0x7e,
   1906      1.1  christos         0xd1, 0x3d, 0x8d, 0xb4, 0x0e, 0x9f, 0x9e, 0xb1, 0x30, 0x03, 0xf0, 0x73,
   1907      1.1  christos         0xa8, 0x40, 0x48, 0x42, 0x7b, 0x60, 0xa0, 0xc4, 0xf2, 0x3b, 0x2d, 0x0a,
   1908      1.1  christos         0x0c, 0xb8, 0x19, 0xfb, 0xb4, 0xf8, 0xe0, 0x2a, 0xc7, 0xf1, 0xc0, 0xc6,
   1909      1.1  christos         0x86, 0x14, 0x60, 0x12, 0x0f, 0xc0, 0xde, 0x4a, 0x67, 0xec, 0xc7, 0xde,
   1910      1.1  christos         0x76, 0x21, 0x1a, 0x55, 0x7f, 0x86, 0xc3, 0x97, 0x98, 0xce, 0xf5, 0xcd,
   1911      1.1  christos         0xf0, 0xe7, 0x12, 0xd6, 0x93, 0xee, 0x1b, 0x9b, 0x61, 0xef, 0x05, 0x8c,
   1912      1.1  christos         0x45, 0x46, 0xd9, 0x64, 0x6f, 0xbe, 0x27, 0xaa, 0x67, 0x01, 0xcc, 0x71,
   1913      1.1  christos         0xb1, 0x60, 0xce, 0x21, 0xd8, 0x51, 0x17, 0x27, 0x0d, 0x90, 0x3d, 0x18,
   1914      1.1  christos         0x7c, 0x87, 0x15, 0x8e, 0x48, 0x4c, 0x6c, 0xc5, 0x72, 0xeb, 0xb7, 0x56,
   1915      1.1  christos         0xf5, 0x6b, 0x60, 0x8f, 0xc2, 0xfd, 0x3f, 0x46, 0x5c, 0x00, 0x91, 0x85,
   1916      1.1  christos         0x79, 0x45, 0x5b, 0x1c, 0x82, 0xc4, 0x87, 0x50, 0x79, 0xba, 0xcc, 0x1c,
   1917      1.1  christos         0x32, 0x7e, 0x2e, 0xb8, 0x2e, 0xc5, 0x4e, 0xd1, 0x9b, 0xdb, 0x66, 0x79,
   1918      1.1  christos         0x7c, 0xfe, 0xaf, 0x6a, 0x05
   1919      1.1  christos     };
   1920      1.1  christos     static const unsigned char q_data[] = {
   1921      1.1  christos         0xa8, 0xcd, 0xf4, 0x33, 0x7b, 0x13, 0x0a, 0x24, 0xc1, 0xde, 0x4a, 0x04,
   1922      1.1  christos         0x7b, 0x4b, 0x71, 0x51, 0x32, 0xe9, 0x47, 0x74, 0xbd, 0x0c, 0x21, 0x40,
   1923      1.1  christos         0x84, 0x12, 0x0a, 0x17, 0x73, 0xdb, 0x29, 0xc7
   1924      1.1  christos     };
   1925      1.1  christos     static const unsigned char g_data[] = {
   1926      1.1  christos         0x6c, 0xc6, 0xa4, 0x3e, 0x61, 0x84, 0xc1, 0xff, 0x6f, 0x4a, 0x1a, 0x6b,
   1927      1.1  christos         0xb0, 0x24, 0x4b, 0xd2, 0x92, 0x5b, 0x29, 0x5c, 0x61, 0xb8, 0xc9, 0x2b,
   1928      1.1  christos         0xd6, 0xf7, 0x59, 0xfd, 0xd8, 0x70, 0x66, 0x77, 0xfc, 0xc1, 0xa4, 0xd4,
   1929      1.1  christos         0xb0, 0x1e, 0xd5, 0xbf, 0x59, 0x98, 0xb3, 0x66, 0x8b, 0xf4, 0x2e, 0xe6,
   1930      1.1  christos         0x12, 0x3e, 0xcc, 0xf8, 0x02, 0xb8, 0xc6, 0xc3, 0x47, 0xd2, 0xf5, 0xaa,
   1931      1.1  christos         0x0c, 0x5f, 0x51, 0xf5, 0xd0, 0x4c, 0x55, 0x3d, 0x07, 0x73, 0xa6, 0x57,
   1932      1.1  christos         0xce, 0x5a, 0xad, 0x42, 0x0c, 0x13, 0x0f, 0xe2, 0x31, 0x25, 0x8e, 0x72,
   1933      1.1  christos         0x12, 0x73, 0x10, 0xdb, 0x7f, 0x79, 0xeb, 0x59, 0xfc, 0xfe, 0xf7, 0x0c,
   1934      1.1  christos         0x1a, 0x81, 0x53, 0x96, 0x22, 0xb8, 0xe7, 0x58, 0xd8, 0x67, 0x80, 0x60,
   1935      1.1  christos         0xad, 0x8b, 0x55, 0x1c, 0x91, 0xf0, 0x72, 0x9a, 0x7e, 0xad, 0x37, 0xf1,
   1936      1.1  christos         0x77, 0x18, 0x96, 0x8a, 0x68, 0x70, 0xfc, 0x71, 0xa9, 0xa2, 0xe8, 0x35,
   1937      1.1  christos         0x27, 0x78, 0xf2, 0xef, 0x59, 0x36, 0x6d, 0x7c, 0xb6, 0x98, 0xd8, 0x1e,
   1938      1.1  christos         0xfa, 0x25, 0x73, 0x97, 0x45, 0x58, 0xe3, 0xae, 0xbd, 0x52, 0x54, 0x05,
   1939      1.1  christos         0xd8, 0x26, 0x26, 0xba, 0xba, 0x05, 0xb5, 0xe9, 0xe5, 0x76, 0xae, 0x25,
   1940      1.1  christos         0xdd, 0xfc, 0x10, 0x89, 0x5a, 0xa9, 0xee, 0x59, 0xc5, 0x79, 0x8b, 0xeb,
   1941      1.1  christos         0x1e, 0x2c, 0x61, 0xab, 0x0d, 0xd1, 0x10, 0x04, 0x91, 0x32, 0x77, 0x4a,
   1942      1.1  christos         0xa6, 0x64, 0x53, 0xda, 0x4c, 0xd7, 0x3a, 0x29, 0xd4, 0xf3, 0x82, 0x25,
   1943      1.1  christos         0x1d, 0x6f, 0x4a, 0x7f, 0xd3, 0x08, 0x3b, 0x42, 0x30, 0x10, 0xd8, 0xd0,
   1944      1.1  christos         0x97, 0x3a, 0xeb, 0x92, 0x63, 0xec, 0x93, 0x2b, 0x6f, 0x32, 0xd8, 0xcd,
   1945      1.1  christos         0x80, 0xd3, 0xc0, 0x4c, 0x03, 0xd5, 0xca, 0xbc, 0x8f, 0xc7, 0x43, 0x53,
   1946      1.1  christos         0x64, 0x66, 0x1c, 0x82, 0x2d, 0xfb, 0xff, 0x39, 0xba, 0xd6, 0x42, 0x62,
   1947      1.1  christos         0x02, 0x6f, 0x96, 0x36
   1948      1.1  christos     };
   1949      1.1  christos     static const unsigned char seed_data[] = {
   1950      1.1  christos         0x64, 0x46, 0x07, 0x32, 0x8d, 0x70, 0x9c, 0xb3, 0x8a, 0x35, 0xde, 0x62,
   1951      1.1  christos         0x00, 0xf2, 0x6d, 0x52, 0x37, 0x4d, 0xb3, 0x84, 0xe1, 0x9d, 0x41, 0x04,
   1952      1.1  christos         0xda, 0x7b, 0xdc, 0x0d, 0x8b, 0x5e, 0xe0, 0x84
   1953      1.1  christos     };
   1954      1.1  christos     const int gindex = 1;
   1955      1.1  christos     const int pcounter = 53;
   1956      1.1  christos     /*
   1957      1.1  christos      * The keypair was generated using
   1958      1.1  christos      * openssl genpkey -paramfile dsa_params.pem --pkeyopt pcounter:53 \
   1959      1.1  christos      *                 -pkeyopt gindex:1 \
   1960      1.1  christos      *                 -pkeyopt hexseed:644607328d709cb38a35de6200f26d -text
   1961      1.1  christos      */
   1962      1.1  christos     static const unsigned char priv_data[] = {
   1963      1.1  christos         0x00, 0x8f, 0xc5, 0x9e, 0xd0, 0xf7, 0x2a, 0x0b, 0x66, 0xf1, 0x32, 0x73,
   1964      1.1  christos         0xae, 0xf6, 0xd9, 0xd4, 0xdb, 0x2d, 0x96, 0x55, 0x89, 0xff, 0xef, 0xa8,
   1965      1.1  christos         0x5f, 0x47, 0x8f, 0xca, 0x02, 0x8a, 0xe1, 0x35, 0x90
   1966      1.1  christos     };
   1967      1.1  christos     static const unsigned char pub_data[] = {
   1968      1.1  christos         0x44, 0x19, 0xc9, 0x46, 0x45, 0x57, 0xc1, 0xa9, 0xd8, 0x30, 0x99, 0x29,
   1969      1.1  christos         0x6a, 0x4b, 0x63, 0x71, 0x69, 0x96, 0x35, 0x17, 0xb2, 0x62, 0x9b, 0x80,
   1970      1.1  christos         0x0a, 0x95, 0x9d, 0x6a, 0xc0, 0x32, 0x0d, 0x07, 0x5f, 0x19, 0x44, 0x02,
   1971      1.1  christos         0xf1, 0xbd, 0xce, 0xdf, 0x10, 0xf8, 0x02, 0x5d, 0x7d, 0x98, 0x8a, 0x73,
   1972      1.1  christos         0x89, 0x00, 0xb6, 0x24, 0xd6, 0x33, 0xe7, 0xcf, 0x8b, 0x49, 0x2a, 0xaf,
   1973      1.1  christos         0x13, 0x1c, 0xb2, 0x52, 0x15, 0xfd, 0x9b, 0xd5, 0x40, 0x4a, 0x1a, 0xda,
   1974      1.1  christos         0x29, 0x4c, 0x92, 0x7e, 0x66, 0x06, 0xdb, 0x61, 0x86, 0xac, 0xb5, 0xda,
   1975      1.1  christos         0x3c, 0x7d, 0x73, 0x7e, 0x54, 0x32, 0x68, 0xa5, 0x02, 0xbc, 0x59, 0x47,
   1976      1.1  christos         0x84, 0xd3, 0x87, 0x71, 0x5f, 0xeb, 0x43, 0x45, 0x24, 0xd3, 0xec, 0x08,
   1977      1.1  christos         0x52, 0xc2, 0x89, 0x2d, 0x9c, 0x1a, 0xcc, 0x91, 0x65, 0x5d, 0xa3, 0xa1,
   1978      1.1  christos         0x35, 0x31, 0x10, 0x1c, 0x3a, 0xa8, 0x4d, 0x18, 0xd5, 0x06, 0xaf, 0xb2,
   1979      1.1  christos         0xec, 0x5c, 0x89, 0x9e, 0x90, 0x86, 0x10, 0x01, 0xeb, 0x51, 0xd5, 0x1b,
   1980      1.1  christos         0x9c, 0xcb, 0x66, 0x07, 0x3f, 0xc4, 0x6e, 0x0a, 0x1b, 0x73, 0xa0, 0x4b,
   1981      1.1  christos         0x5f, 0x4d, 0xab, 0x35, 0x28, 0xfa, 0xda, 0x3a, 0x0c, 0x08, 0xe8, 0xf3,
   1982      1.1  christos         0xef, 0x42, 0x67, 0xbc, 0x21, 0xf2, 0xc2, 0xb8, 0xff, 0x1a, 0x81, 0x05,
   1983      1.1  christos         0x68, 0x73, 0x62, 0xdf, 0xd7, 0xab, 0x0f, 0x22, 0x89, 0x57, 0x96, 0xd4,
   1984      1.1  christos         0x93, 0xaf, 0xa1, 0x21, 0xa3, 0x48, 0xe9, 0xf0, 0x97, 0x47, 0xa0, 0x27,
   1985      1.1  christos         0xba, 0x87, 0xb8, 0x15, 0x5f, 0xff, 0x2c, 0x50, 0x41, 0xf1, 0x7e, 0xc6,
   1986      1.1  christos         0x81, 0xc4, 0x51, 0xf1, 0xfd, 0xd6, 0x86, 0xf7, 0x69, 0x97, 0xf1, 0x49,
   1987      1.1  christos         0xc9, 0xf9, 0xf4, 0x9b, 0xf4, 0xe8, 0x85, 0xa7, 0xbd, 0x36, 0x55, 0x4a,
   1988      1.1  christos         0x3d, 0xe8, 0x65, 0x09, 0x7b, 0xb7, 0x12, 0x64, 0xd2, 0x0a, 0x53, 0x60,
   1989      1.1  christos         0x48, 0xd1, 0x8a, 0xbd
   1990      1.1  christos     };
   1991      1.1  christos 
   1992      1.1  christos     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
   1993      1.1  christos         || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL))
   1994      1.1  christos         || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL))
   1995      1.1  christos         || !TEST_ptr(p = BN_bin2bn(p_data, sizeof(p_data), NULL))
   1996      1.1  christos         || !TEST_ptr(q = BN_bin2bn(q_data, sizeof(q_data), NULL))
   1997      1.1  christos         || !TEST_ptr(g = BN_bin2bn(g_data, sizeof(g_data), NULL))
   1998      1.1  christos 
   1999      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
   2000      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
   2001      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
   2002      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
   2003  1.1.1.2  christos             OSSL_PKEY_PARAM_FFC_SEED,
   2004  1.1.1.2  christos             seed_data,
   2005  1.1.1.2  christos             sizeof(seed_data)))
   2006      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_int(bld, OSSL_PKEY_PARAM_FFC_GINDEX,
   2007  1.1.1.2  christos             gindex))
   2008      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_int(bld,
   2009  1.1.1.2  christos             OSSL_PKEY_PARAM_FFC_PCOUNTER,
   2010  1.1.1.2  christos             pcounter))
   2011      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
   2012  1.1.1.2  christos             pub))
   2013      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
   2014  1.1.1.2  christos             priv))
   2015      1.1  christos         || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
   2016      1.1  christos         goto err;
   2017      1.1  christos 
   2018      1.1  christos     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL)))
   2019      1.1  christos         goto err;
   2020      1.1  christos 
   2021      1.1  christos     if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
   2022      1.1  christos         || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
   2023  1.1.1.2  christos                             fromdata_params),
   2024  1.1.1.2  christos             1))
   2025      1.1  christos         goto err;
   2026      1.1  christos 
   2027      1.1  christos     for (;;) {
   2028      1.1  christos         ret = 0;
   2029      1.1  christos         if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048)
   2030      1.1  christos             || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112)
   2031      1.1  christos             || !TEST_int_eq(EVP_PKEY_get_size(pk), 2 + 2 * (3 + sizeof(q_data)))
   2032      1.1  christos             || !TEST_false(EVP_PKEY_missing_parameters(pk)))
   2033      1.1  christos             goto err;
   2034      1.1  christos 
   2035      1.1  christos         if (!TEST_false(EVP_PKEY_get_utf8_string_param(pk,
   2036  1.1.1.2  christos                 OSSL_PKEY_PARAM_GROUP_NAME,
   2037  1.1.1.2  christos                 name_out,
   2038  1.1.1.2  christos                 sizeof(name_out),
   2039  1.1.1.2  christos                 &len))
   2040      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY,
   2041  1.1.1.2  christos                 &pub_out))
   2042      1.1  christos             || !TEST_BN_eq(pub, pub_out)
   2043      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY,
   2044  1.1.1.2  christos                 &priv_out))
   2045      1.1  christos             || !TEST_BN_eq(priv, priv_out)
   2046      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P,
   2047  1.1.1.2  christos                 &p_out))
   2048      1.1  christos             || !TEST_BN_eq(p, p_out)
   2049      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q,
   2050  1.1.1.2  christos                 &q_out))
   2051      1.1  christos             || !TEST_BN_eq(q, q_out)
   2052      1.1  christos             || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G,
   2053  1.1.1.2  christos                 &g_out))
   2054      1.1  christos             || !TEST_BN_eq(g, g_out)
   2055      1.1  christos             || !TEST_false(EVP_PKEY_get_bn_param(pk,
   2056  1.1.1.2  christos                 OSSL_PKEY_PARAM_FFC_COFACTOR,
   2057  1.1.1.2  christos                 &j_out))
   2058      1.1  christos             || !TEST_ptr_null(j_out)
   2059      1.1  christos             || !TEST_true(EVP_PKEY_get_octet_string_param(pk,
   2060  1.1.1.2  christos                 OSSL_PKEY_PARAM_FFC_SEED,
   2061  1.1.1.2  christos                 seed_out,
   2062  1.1.1.2  christos                 sizeof(seed_out),
   2063  1.1.1.2  christos                 &len))
   2064      1.1  christos             || !TEST_true(EVP_PKEY_get_int_param(pk,
   2065  1.1.1.2  christos                 OSSL_PKEY_PARAM_FFC_GINDEX,
   2066  1.1.1.2  christos                 &gindex_out))
   2067      1.1  christos             || !TEST_int_eq(gindex, gindex_out)
   2068      1.1  christos             || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H,
   2069  1.1.1.2  christos                 &hindex_out))
   2070      1.1  christos             || !TEST_int_eq(hindex_out, 0)
   2071      1.1  christos             || !TEST_true(EVP_PKEY_get_int_param(pk,
   2072  1.1.1.2  christos                 OSSL_PKEY_PARAM_FFC_PCOUNTER,
   2073  1.1.1.2  christos                 &pcounter_out))
   2074      1.1  christos             || !TEST_int_eq(pcounter, pcounter_out))
   2075      1.1  christos             goto err;
   2076      1.1  christos         BN_free(p_out);
   2077      1.1  christos         p_out = NULL;
   2078      1.1  christos         BN_free(q_out);
   2079      1.1  christos         q_out = NULL;
   2080      1.1  christos         BN_free(g_out);
   2081      1.1  christos         g_out = NULL;
   2082      1.1  christos         BN_free(j_out);
   2083      1.1  christos         j_out = NULL;
   2084      1.1  christos         BN_free(pub_out);
   2085      1.1  christos         pub_out = NULL;
   2086      1.1  christos         BN_free(priv_out);
   2087      1.1  christos         priv_out = NULL;
   2088      1.1  christos 
   2089      1.1  christos         if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
   2090      1.1  christos             goto err;
   2091      1.1  christos 
   2092      1.1  christos         if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0)
   2093      1.1  christos             || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0)
   2094      1.1  christos             || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0)
   2095      1.1  christos             || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0))
   2096      1.1  christos             goto err;
   2097      1.1  christos         EVP_PKEY_CTX_free(key_ctx);
   2098      1.1  christos         key_ctx = NULL;
   2099      1.1  christos 
   2100      1.1  christos         if (!TEST_ptr(copy_pk = EVP_PKEY_new())
   2101      1.1  christos             || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk)))
   2102      1.1  christos             goto err;
   2103      1.1  christos         EVP_PKEY_free(copy_pk);
   2104      1.1  christos         copy_pk = NULL;
   2105      1.1  christos 
   2106      1.1  christos         ret = test_print_key_using_pem("DSA", pk)
   2107  1.1.1.2  christos             && test_print_key_using_encoder("DSA", pk);
   2108      1.1  christos 
   2109      1.1  christos         if (!ret || dup_pk != NULL)
   2110      1.1  christos             break;
   2111      1.1  christos 
   2112      1.1  christos         if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
   2113      1.1  christos             goto err;
   2114      1.1  christos         ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
   2115      1.1  christos         EVP_PKEY_free(pk);
   2116      1.1  christos         pk = dup_pk;
   2117      1.1  christos         if (!ret)
   2118      1.1  christos             goto err;
   2119      1.1  christos     }
   2120      1.1  christos 
   2121  1.1.1.2  christos err:
   2122      1.1  christos     OSSL_PARAM_free(fromdata_params);
   2123      1.1  christos     OSSL_PARAM_BLD_free(bld);
   2124      1.1  christos     BN_free(p);
   2125      1.1  christos     BN_free(q);
   2126      1.1  christos     BN_free(g);
   2127      1.1  christos     BN_free(pub);
   2128      1.1  christos     BN_free(priv);
   2129      1.1  christos     BN_free(p_out);
   2130      1.1  christos     BN_free(q_out);
   2131      1.1  christos     BN_free(g_out);
   2132      1.1  christos     BN_free(pub_out);
   2133      1.1  christos     BN_free(priv_out);
   2134      1.1  christos     BN_free(j_out);
   2135      1.1  christos     EVP_PKEY_free(pk);
   2136      1.1  christos     EVP_PKEY_free(copy_pk);
   2137      1.1  christos     EVP_PKEY_CTX_free(ctx);
   2138      1.1  christos     EVP_PKEY_CTX_free(key_ctx);
   2139      1.1  christos 
   2140      1.1  christos     return ret;
   2141      1.1  christos }
   2142      1.1  christos 
   2143      1.1  christos static int test_check_dsa(void)
   2144      1.1  christos {
   2145      1.1  christos     int ret = 0;
   2146      1.1  christos     EVP_PKEY_CTX *ctx = NULL;
   2147      1.1  christos 
   2148      1.1  christos     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL))
   2149      1.1  christos         || !TEST_int_le(EVP_PKEY_check(ctx), 0)
   2150      1.1  christos         || !TEST_int_le(EVP_PKEY_public_check(ctx), 0)
   2151      1.1  christos         || !TEST_int_le(EVP_PKEY_private_check(ctx), 0)
   2152      1.1  christos         || !TEST_int_le(EVP_PKEY_pairwise_check(ctx), 0))
   2153  1.1.1.2  christos         goto err;
   2154      1.1  christos 
   2155      1.1  christos     ret = 1;
   2156  1.1.1.2  christos err:
   2157      1.1  christos     EVP_PKEY_CTX_free(ctx);
   2158      1.1  christos 
   2159      1.1  christos     return ret;
   2160      1.1  christos }
   2161      1.1  christos #endif /* OPENSSL_NO_DSA */
   2162      1.1  christos 
   2163      1.1  christos static OSSL_PARAM *do_construct_hkdf_params(char *digest, char *key,
   2164  1.1.1.2  christos     size_t keylen, char *salt)
   2165      1.1  christos {
   2166      1.1  christos     OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 5);
   2167      1.1  christos     OSSL_PARAM *p = params;
   2168      1.1  christos 
   2169      1.1  christos     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, digest, 0);
   2170      1.1  christos     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
   2171  1.1.1.2  christos         salt, strlen(salt));
   2172      1.1  christos     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
   2173  1.1.1.2  christos         (unsigned char *)key, keylen);
   2174      1.1  christos     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MODE,
   2175  1.1.1.2  christos         "EXTRACT_ONLY", 0);
   2176      1.1  christos     *p = OSSL_PARAM_construct_end();
   2177      1.1  christos 
   2178      1.1  christos     return params;
   2179      1.1  christos }
   2180      1.1  christos 
   2181      1.1  christos static int test_evp_pkey_ctx_dup_kdf(void)
   2182      1.1  christos {
   2183      1.1  christos     int ret = 0;
   2184      1.1  christos     size_t len = 0, dlen = 0;
   2185      1.1  christos     EVP_PKEY_CTX *pctx = NULL, *dctx = NULL;
   2186      1.1  christos     OSSL_PARAM *params = NULL;
   2187      1.1  christos 
   2188      1.1  christos     if (!TEST_ptr(params = do_construct_hkdf_params("sha256", "secret", 6,
   2189  1.1.1.2  christos                       "salt")))
   2190      1.1  christos         goto err;
   2191      1.1  christos     if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(NULL, "HKDF", NULL)))
   2192      1.1  christos         goto err;
   2193      1.1  christos     if (!TEST_int_eq(EVP_PKEY_derive_init_ex(pctx, params), 1))
   2194      1.1  christos         goto err;
   2195      1.1  christos     if (!TEST_ptr(dctx = EVP_PKEY_CTX_dup(pctx)))
   2196      1.1  christos         goto err;
   2197      1.1  christos     if (!TEST_int_eq(EVP_PKEY_derive(pctx, NULL, &len), 1)
   2198      1.1  christos         || !TEST_size_t_eq(len, SHA256_DIGEST_LENGTH)
   2199      1.1  christos         || !TEST_int_eq(EVP_PKEY_derive(dctx, NULL, &dlen), 1)
   2200      1.1  christos         || !TEST_size_t_eq(dlen, SHA256_DIGEST_LENGTH))
   2201      1.1  christos         goto err;
   2202      1.1  christos     ret = 1;
   2203      1.1  christos err:
   2204      1.1  christos     OPENSSL_free(params);
   2205      1.1  christos     EVP_PKEY_CTX_free(dctx);
   2206      1.1  christos     EVP_PKEY_CTX_free(pctx);
   2207      1.1  christos     return ret;
   2208      1.1  christos }
   2209      1.1  christos 
   2210      1.1  christos static const char *name_dup_algs[] = {
   2211      1.1  christos #ifndef OPENSSL_NO_ECX
   2212      1.1  christos     "ED25519",
   2213      1.1  christos #endif
   2214      1.1  christos #ifndef OPENSSL_NO_ML_KEM
   2215      1.1  christos     "ML-KEM-512",
   2216      1.1  christos #endif
   2217      1.1  christos #ifndef OPENSSL_NO_ML_DSA
   2218      1.1  christos     "ML-DSA-44",
   2219      1.1  christos #endif
   2220      1.1  christos     NULL
   2221      1.1  christos };
   2222      1.1  christos 
   2223      1.1  christos static int test_name_dup(int idx)
   2224      1.1  christos {
   2225      1.1  christos     const char *alg = name_dup_algs[idx];
   2226      1.1  christos     EVP_PKEY *key = NULL;
   2227      1.1  christos     EVP_PKEY_CTX *factory = NULL, *ctx = NULL;
   2228      1.1  christos     int i, ret = 0;
   2229      1.1  christos 
   2230      1.1  christos     if (alg == NULL
   2231      1.1  christos         || (factory = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL)) == NULL)
   2232      1.1  christos         return 1;
   2233      1.1  christos     TEST_info("Testing fresh context dup for: %s", alg);
   2234      1.1  christos 
   2235      1.1  christos     /* Run twice to check that *repeated* use works */
   2236      1.1  christos     for (i = 0; i < 2; ++i) {
   2237      1.1  christos         EVP_PKEY_CTX_free(ctx);
   2238      1.1  christos         EVP_PKEY_free(key);
   2239      1.1  christos         key = NULL;
   2240      1.1  christos         if (!TEST_ptr(ctx = EVP_PKEY_CTX_dup(factory))
   2241      1.1  christos             || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
   2242      1.1  christos             || !TEST_int_gt(EVP_PKEY_keygen(ctx, &key), 0)) {
   2243      1.1  christos             ERR_print_errors(bio_err);
   2244      1.1  christos             goto end;
   2245      1.1  christos         }
   2246      1.1  christos     }
   2247      1.1  christos     ret = 1;
   2248      1.1  christos 
   2249  1.1.1.2  christos end:
   2250      1.1  christos     EVP_PKEY_CTX_free(factory);
   2251      1.1  christos     EVP_PKEY_CTX_free(ctx);
   2252      1.1  christos     EVP_PKEY_free(key);
   2253      1.1  christos 
   2254      1.1  christos     return ret;
   2255      1.1  christos }
   2256      1.1  christos 
   2257      1.1  christos int setup_tests(void)
   2258      1.1  christos {
   2259      1.1  christos     if (!test_skip_common_options()) {
   2260      1.1  christos         TEST_error("Error parsing test options\n");
   2261      1.1  christos         return 0;
   2262      1.1  christos     }
   2263      1.1  christos 
   2264      1.1  christos     if (!TEST_ptr(datadir = test_get_argument(0)))
   2265      1.1  christos         return 0;
   2266      1.1  christos 
   2267      1.1  christos     ADD_TEST(test_evp_pkey_ctx_dup_kdf);
   2268      1.1  christos     ADD_ALL_TESTS(test_name_dup, OSSL_NELEM(name_dup_algs));
   2269      1.1  christos     ADD_TEST(test_evp_pkey_get_bn_param_large);
   2270      1.1  christos     ADD_TEST(test_fromdata_rsa);
   2271      1.1  christos     ADD_TEST(test_fromdata_rsa_derive_from_pq_sp800);
   2272      1.1  christos     ADD_TEST(test_fromdata_rsa_derive_from_pq_multiprime);
   2273      1.1  christos #ifndef OPENSSL_NO_DH
   2274      1.1  christos     ADD_TEST(test_fromdata_dh_fips186_4);
   2275      1.1  christos     ADD_TEST(test_fromdata_dh_named_group);
   2276      1.1  christos #endif
   2277      1.1  christos #ifndef OPENSSL_NO_DSA
   2278      1.1  christos     ADD_TEST(test_check_dsa);
   2279      1.1  christos     ADD_TEST(test_fromdata_dsa_fips186_4);
   2280      1.1  christos #endif
   2281      1.1  christos #ifndef OPENSSL_NO_EC
   2282  1.1.1.2  christos #ifndef OPENSSL_NO_ECX
   2283      1.1  christos     ADD_ALL_TESTS(test_fromdata_ecx, 4 * 3);
   2284  1.1.1.2  christos #endif
   2285      1.1  christos     ADD_TEST(test_fromdata_ec);
   2286      1.1  christos     ADD_TEST(test_ec_dup_no_operation);
   2287      1.1  christos     ADD_TEST(test_ec_dup_keygen_operation);
   2288      1.1  christos #endif
   2289      1.1  christos     return 1;
   2290      1.1  christos }
   2291