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