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