Add DH keygen to providers
[openssl.git] / test / evp_pkey_provided_test.c
1 /*
2  * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <string.h> /* memset */
11 #include <openssl/evp.h>
12 #include <openssl/pem.h>
13 #include <openssl/serializer.h>
14 #include <openssl/provider.h>
15 #include <openssl/param_build.h>
16 #include <openssl/core_names.h>
17 #include "crypto/ecx.h"
18 #include "crypto/evp.h"          /* For the internal API */
19 #include "crypto/bn_dh.h"        /* _bignum_ffdhe2048_p */
20 #include "internal/nelem.h"
21 #include "testutil.h"
22
23 static char *datadir = NULL;
24
25 #define PRIV_TEXT    0
26 #define PRIV_PEM     1
27 #define PRIV_DER     2
28 #define PUB_TEXT     3
29 #define PUB_PEM      4
30 #define PUB_DER      5
31
32 static void stripcr(char *buf, size_t *len)
33 {
34     size_t i;
35     char *curr, *writ;
36
37     for (i = *len, curr = buf, writ = buf; i > 0; i--, curr++) {
38         if (*curr == '\r') {
39             (*len)--;
40             continue;
41         }
42         if (curr != writ)
43             *writ = *curr;
44         writ++;
45     }
46 }
47
48 static int compare_with_file(const char *alg, int type, BIO *membio)
49 {
50     char filename[80];
51     BIO *file = NULL;
52     char buf[4096];
53     char *memdata, *fullfile = NULL;
54     const char *suffix;
55     size_t readbytes;
56     int ret = 0;
57     int len;
58     size_t slen;
59
60     switch (type) {
61     case PRIV_TEXT:
62         suffix = "priv.txt";
63         break;
64
65     case PRIV_PEM:
66         suffix = "priv.pem";
67         break;
68
69     case PRIV_DER:
70         suffix = "priv.der";
71         break;
72
73     case PUB_TEXT:
74         suffix = "pub.txt";
75         break;
76
77     case PUB_PEM:
78         suffix = "pub.pem";
79         break;
80
81     case PUB_DER:
82         suffix = "pub.der";
83         break;
84
85     default:
86         TEST_error("Invalid file type");
87         goto err;
88     }
89
90     BIO_snprintf(filename, sizeof(filename), "%s.%s", alg, suffix);
91     fullfile = test_mk_file_path(datadir, filename);
92     if (!TEST_ptr(fullfile))
93         goto err;
94
95     file = BIO_new_file(fullfile, "rb");
96     if (!TEST_ptr(file))
97         goto err;
98
99     if (!TEST_true(BIO_read_ex(file, buf, sizeof(buf), &readbytes))
100             || !TEST_true(BIO_eof(file))
101             || !TEST_size_t_lt(readbytes, sizeof(buf)))
102         goto err;
103
104     len = BIO_get_mem_data(membio, &memdata);
105     if (!TEST_int_gt(len, 0))
106         goto err;
107
108     slen = len;
109     if (type != PRIV_DER && type != PUB_DER) {
110         stripcr(memdata, &slen);
111         stripcr(buf, &readbytes);
112     }
113
114     if (!TEST_mem_eq(memdata, slen, buf, readbytes))
115         goto err;
116
117     ret = 1;
118  err:
119     OPENSSL_free(fullfile);
120     (void)BIO_reset(membio);
121     BIO_free(file);
122     return ret;
123 }
124
125 static int test_print_key_using_pem(const char *alg, const EVP_PKEY *pk)
126 {
127     BIO *membio = BIO_new(BIO_s_mem());
128     int ret = 0;
129
130     if (!TEST_ptr(membio))
131         goto err;
132
133     if (!TEST_true(EVP_PKEY_print_private(membio, pk, 0, NULL))
134         || !TEST_true(compare_with_file(alg, PRIV_TEXT, membio))
135         /* Public key in PEM form */
136         || !TEST_true(PEM_write_bio_PUBKEY(membio, pk))
137         || !TEST_true(compare_with_file(alg, PUB_PEM, membio))
138         /* Unencrypted private key in PEM form */
139         || !TEST_true(PEM_write_bio_PrivateKey(membio, pk,
140                                                NULL, NULL, 0, NULL, NULL))
141         || !TEST_true(compare_with_file(alg, PRIV_PEM, membio))
142         /* Encrypted private key in PEM form */
143         || !TEST_true(PEM_write_bio_PrivateKey(bio_out, pk, EVP_aes_256_cbc(),
144                                                (unsigned char *)"pass", 4,
145                                                NULL, NULL)))
146         goto err;
147
148     ret = 1;
149  err:
150     BIO_free(membio);
151     return ret;
152 }
153
154 static int test_print_key_type_using_serializer(const char *alg, int type,
155                                                 const EVP_PKEY *pk)
156 {
157     const char *pq;
158     OSSL_SERIALIZER_CTX *ctx = NULL;
159     BIO *membio = BIO_new(BIO_s_mem());
160     int ret = 0;
161
162     switch (type) {
163     case PRIV_TEXT:
164         pq = OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ;
165         break;
166
167     case PRIV_PEM:
168         pq = OSSL_SERIALIZER_PrivateKey_TO_PEM_PQ;
169         break;
170
171     case PRIV_DER:
172         pq = OSSL_SERIALIZER_PrivateKey_TO_DER_PQ;
173         break;
174
175     case PUB_TEXT:
176         pq = OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ;
177         break;
178
179     case PUB_PEM:
180         pq = OSSL_SERIALIZER_PUBKEY_TO_PEM_PQ;
181         break;
182
183     case PUB_DER:
184         pq = OSSL_SERIALIZER_PUBKEY_TO_DER_PQ;
185         break;
186
187     default:
188         TEST_error("Invalid serialization type");
189         goto err;
190     }
191
192     if (!TEST_ptr(membio))
193         goto err;
194
195     /* Make a context, it's valid for several prints */
196     TEST_note("Setting up a OSSL_SERIALIZER context with passphrase");
197     if (!TEST_ptr(ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pk, pq))
198         /* Check that this operation is supported */
199         || !TEST_ptr(OSSL_SERIALIZER_CTX_get_serializer(ctx)))
200         goto err;
201
202     /* Use no cipher.  This should give us an unencrypted PEM */
203     TEST_note("Testing with no encryption");
204     if (!TEST_true(OSSL_SERIALIZER_to_bio(ctx, membio))
205         || !TEST_true(compare_with_file(alg, type, membio)))
206         goto err;
207
208     if (type == PRIV_PEM) {
209         /* Set a passphrase to be used later */
210         if (!TEST_true(OSSL_SERIALIZER_CTX_set_passphrase(ctx,
211                                                           (unsigned char *)"pass",
212                                                           4)))
213             goto err;
214
215         /* Use a valid cipher name */
216         TEST_note("Displaying PEM encrypted with AES-256-CBC");
217         if (!TEST_true(OSSL_SERIALIZER_CTX_set_cipher(ctx, "AES-256-CBC", NULL))
218             || !TEST_true(OSSL_SERIALIZER_to_bio(ctx, bio_out)))
219             goto err;
220
221         /* Use an invalid cipher name, which should generate no output */
222         TEST_note("NOT Displaying PEM encrypted with (invalid) FOO");
223         if (!TEST_false(OSSL_SERIALIZER_CTX_set_cipher(ctx, "FOO", NULL))
224             || !TEST_false(OSSL_SERIALIZER_to_bio(ctx, bio_out)))
225             goto err;
226
227         /* Clear the cipher.  This should give us an unencrypted PEM again */
228         TEST_note("Testing with encryption cleared (no encryption)");
229         if (!TEST_true(OSSL_SERIALIZER_CTX_set_cipher(ctx, NULL, NULL))
230             || !TEST_true(OSSL_SERIALIZER_to_bio(ctx, membio))
231             || !TEST_true(compare_with_file(alg, type, membio)))
232             goto err;
233     }
234     ret = 1;
235 err:
236     BIO_free(membio);
237     OSSL_SERIALIZER_CTX_free(ctx);
238     return ret;
239 }
240
241 static int test_print_key_using_serializer(const char *alg, const EVP_PKEY *pk)
242 {
243     int i;
244     int ret = 1;
245
246     for (i = 0; i < 6; i++)
247         ret = ret && test_print_key_type_using_serializer(alg, i, pk);
248
249     return ret;
250 }
251
252 /* Array indexes used in test_fromdata_rsa */
253 #define N       0
254 #define E       1
255 #define D       2
256 #define P       3
257 #define Q       4
258 #define DP      5
259 #define DQ      6
260 #define QINV    7
261
262 static int test_fromdata_rsa(void)
263 {
264     int ret = 0, i;
265     EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
266     EVP_PKEY *pk = NULL, *copy_pk = NULL;
267     /*
268      * 32-bit RSA key, extracted from this command,
269      * executed with OpenSSL 1.0.2:
270      *
271      * openssl genrsa 32 | openssl rsa -text
272      */
273     static unsigned long key_numbers[] = {
274         0xbc747fc5,              /* N */
275         0x10001,                 /* E */
276         0x7b133399,              /* D */
277         0xe963,                  /* P */
278         0xceb7,                  /* Q */
279         0x8599,                  /* DP */
280         0xbd87,                  /* DQ */
281         0xcc3b,                  /* QINV */
282     };
283     OSSL_PARAM fromdata_params[] = {
284         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_N, &key_numbers[N]),
285         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_E, &key_numbers[E]),
286         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_D, &key_numbers[D]),
287         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR1, &key_numbers[P]),
288         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR2, &key_numbers[Q]),
289         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT1, &key_numbers[DP]),
290         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT2, &key_numbers[DQ]),
291         OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, &key_numbers[QINV]),
292         OSSL_PARAM_END
293     };
294     BIGNUM *bn = BN_new();
295     BIGNUM *bn_from = BN_new();
296
297     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL)))
298         goto err;
299
300     if (!TEST_true(EVP_PKEY_key_fromdata_init(ctx))
301         || !TEST_true(EVP_PKEY_fromdata(ctx, &pk, fromdata_params))
302         || !TEST_int_eq(EVP_PKEY_bits(pk), 32)
303         || !TEST_int_eq(EVP_PKEY_security_bits(pk), 8)
304         || !TEST_int_eq(EVP_PKEY_size(pk), 4))
305         goto err;
306
307     if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
308         goto err;
309
310     if (!TEST_true(EVP_PKEY_check(key_ctx))
311         || !TEST_true(EVP_PKEY_public_check(key_ctx))
312         || !TEST_true(EVP_PKEY_private_check(key_ctx))
313         || !TEST_true(EVP_PKEY_pairwise_check(key_ctx)))
314         goto err;
315
316     /* EVP_PKEY_copy_parameters() should fail for RSA */
317     if (!TEST_ptr(copy_pk = EVP_PKEY_new())
318         || !TEST_false(EVP_PKEY_copy_parameters(copy_pk, pk)))
319         goto err;
320
321     for (i = 0; fromdata_params[i].key != NULL; ++i) {
322         if (!TEST_true(BN_set_word(bn_from, key_numbers[i]))
323             || !TEST_true(EVP_PKEY_get_bn_param(pk, fromdata_params[i].key, &bn))
324             || !TEST_BN_eq(bn, bn_from))
325             goto err;
326     }
327     ret = test_print_key_using_pem("RSA", pk)
328           && test_print_key_using_serializer("RSA", pk);
329  err:
330     BN_free(bn_from);
331     BN_free(bn);
332     EVP_PKEY_free(pk);
333     EVP_PKEY_free(copy_pk);
334     EVP_PKEY_CTX_free(key_ctx);
335     EVP_PKEY_CTX_free(ctx);
336
337     return ret;
338 }
339
340 static int test_evp_pkey_get_bn_param_large(void)
341 {
342     int ret = 0;
343     EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
344     EVP_PKEY *pk = NULL;
345     OSSL_PARAM_BLD *bld = NULL;
346     OSSL_PARAM *fromdata_params = NULL;
347     BIGNUM *n = NULL, *e = NULL, *d = NULL, *n_out = NULL;
348     /*
349      * The buffer size chosen here for n_data larger than the buffer used
350      * internally in EVP_PKEY_get_bn_param.
351      */
352     static unsigned char n_data[2050];
353     static const unsigned char e_data[] = {
354         0x1, 0x00, 0x01
355     };
356     static const unsigned char d_data[]= {
357        0x99, 0x33, 0x13, 0x7b
358     };
359
360     /* N is a large buffer */
361     memset(n_data, 0xCE, sizeof(n_data));
362
363     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
364         || !TEST_ptr(n = BN_bin2bn(n_data, sizeof(n_data), NULL))
365         || !TEST_ptr(e = BN_bin2bn(e_data, sizeof(e_data), NULL))
366         || !TEST_ptr(d = BN_bin2bn(d_data, sizeof(d_data), NULL))
367         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n))
368         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e))
369         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d))
370         || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))
371         || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL))
372         || !TEST_true(EVP_PKEY_key_fromdata_init(ctx))
373         || !TEST_true(EVP_PKEY_fromdata(ctx, &pk, fromdata_params))
374         || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))
375         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_RSA_N, &n_out))
376         || !TEST_BN_eq(n, n_out))
377         goto err;
378     ret = 1;
379  err:
380     BN_free(n_out);
381     BN_free(n);
382     BN_free(e);
383     BN_free(d);
384     EVP_PKEY_free(pk);
385     EVP_PKEY_CTX_free(key_ctx);
386     EVP_PKEY_CTX_free(ctx);
387     OSSL_PARAM_BLD_free_params(fromdata_params);
388     OSSL_PARAM_BLD_free(bld);
389     return ret;
390 }
391
392
393 #ifndef OPENSSL_NO_DH
394 static int test_fromdata_dh_named_group(void)
395 {
396     int ret = 0;
397     int gindex = 0, pcounter = 0, hindex = 0;
398     EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
399     EVP_PKEY *pk = NULL, *copy_pk = NULL;
400     size_t len;
401     BIGNUM *pub = NULL, *priv = NULL;
402     BIGNUM *pub_out = NULL, *priv_out = NULL;
403     BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL;
404     OSSL_PARAM *fromdata_params = NULL;
405     OSSL_PARAM_BLD *bld = NULL;
406     char name_out[80];
407     unsigned char seed_out[32];
408
409     /*
410      * DH key data was generated using the following:
411      * openssl genpkey -algorithm DH -pkeyopt group:ffdhe2048 -text
412      */
413     static const unsigned char priv_data[] = {
414         0x88, 0x85, 0xe7, 0x9f, 0xee, 0x6d, 0xc5, 0x7c, 0x78, 0xaf, 0x63, 0x5d,
415         0x38, 0x2a, 0xd0, 0xed, 0x56, 0x4b, 0x47, 0x21, 0x2b, 0xfa, 0x55, 0xfa,
416         0x87, 0xe8, 0xa9, 0x7b,
417     };
418     static const unsigned char pub_data[] = {
419         0x00, 0xd6, 0x2d, 0x77, 0xe0, 0xd3, 0x7d, 0xf8, 0xeb, 0x98, 0x50, 0xa1,
420         0x82, 0x22, 0x65, 0xd5, 0xd9, 0xfe, 0xc9, 0x3f, 0xbe, 0x16, 0x83, 0xbd,
421         0x33, 0xe9, 0xc6, 0x93, 0xcf, 0x08, 0xaf, 0x83, 0xfa, 0x80, 0x8a, 0x6c,
422         0x64, 0xdf, 0x70, 0x64, 0xd5, 0x0a, 0x7c, 0x5a, 0x72, 0xda, 0x66, 0xe6,
423         0xf9, 0xf5, 0x31, 0x21, 0x92, 0xb0, 0x60, 0x1a, 0xb5, 0xd3, 0xf0, 0xa5,
424         0xfa, 0x48, 0x95, 0x2e, 0x38, 0xd9, 0xc5, 0xe6, 0xda, 0xfb, 0x6c, 0x03,
425         0x9d, 0x4b, 0x69, 0xb7, 0x95, 0xe4, 0x5c, 0xc0, 0x93, 0x4f, 0x48, 0xd9,
426         0x7e, 0x06, 0x22, 0xb2, 0xde, 0xf3, 0x79, 0x24, 0xed, 0xe1, 0xd1, 0x4a,
427         0x57, 0xf1, 0x40, 0x86, 0x70, 0x42, 0x25, 0xc5, 0x27, 0x68, 0xc9, 0xfa,
428         0xe5, 0x8e, 0x62, 0x7e, 0xff, 0x49, 0x6c, 0x5b, 0xb5, 0xba, 0xf9, 0xef,
429         0x9a, 0x1a, 0x10, 0xd4, 0x81, 0x53, 0xcf, 0x83, 0x04, 0x18, 0x1c, 0xe1,
430         0xdb, 0xe1, 0x65, 0xa9, 0x7f, 0xe1, 0x33, 0xeb, 0xc3, 0x4f, 0xe3, 0xb7,
431         0x22, 0xf7, 0x1c, 0x09, 0x4f, 0xed, 0xc6, 0x07, 0x8e, 0x78, 0x05, 0x8f,
432         0x7c, 0x96, 0xd9, 0x12, 0xe0, 0x81, 0x74, 0x1a, 0xe9, 0x13, 0xc0, 0x20,
433         0x82, 0x65, 0xbb, 0x42, 0x3b, 0xed, 0x08, 0x6a, 0x84, 0x4f, 0xea, 0x77,
434         0x14, 0x32, 0xf9, 0xed, 0xc2, 0x12, 0xd6, 0xc5, 0xc6, 0xb3, 0xe5, 0xf2,
435         0x6e, 0xf6, 0x16, 0x7f, 0x37, 0xde, 0xbc, 0x09, 0xc7, 0x06, 0x6b, 0x12,
436         0xbc, 0xad, 0x2d, 0x49, 0x25, 0xd5, 0xdc, 0xf4, 0x18, 0x14, 0xd2, 0xf0,
437         0xf1, 0x1d, 0x1f, 0x3a, 0xaa, 0x15, 0x55, 0xbb, 0x0d, 0x7f, 0xbe, 0x67,
438         0xa1, 0xa7, 0xf0, 0xaa, 0xb3, 0xfb, 0x41, 0x82, 0x39, 0x49, 0x93, 0xbc,
439         0xa8, 0xee, 0x72, 0x13, 0x45, 0x65, 0x15, 0x42, 0x17, 0xaa, 0xd8, 0xab,
440         0xcf, 0x33, 0x42, 0x83, 0x42
441     };
442     static const char group_name[] = "ffdhe2048";
443
444     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
445         || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL))
446         || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL))
447         || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
448                                                       OSSL_PKEY_PARAM_FFC_GROUP,
449                                                       group_name, 0))
450         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub))
451         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv))
452         || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
453         goto err;
454
455     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL)))
456         goto err;
457
458     if (!TEST_true(EVP_PKEY_key_fromdata_init(ctx))
459         || !TEST_true(EVP_PKEY_fromdata(ctx, &pk, fromdata_params))
460         || !TEST_int_eq(EVP_PKEY_bits(pk), 2048)
461         || !TEST_int_eq(EVP_PKEY_security_bits(pk), 112)
462         || !TEST_int_eq(EVP_PKEY_size(pk), 256))
463         goto err;
464
465     if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_FFC_GROUP,
466                                                   name_out, sizeof(name_out),
467                                                   &len))
468         || !TEST_str_eq(name_out, group_name)
469         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY,
470                                             &pub_out))
471
472         || !TEST_BN_eq(pub, pub_out)
473         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY,
474                                             &priv_out))
475         || !TEST_BN_eq(priv, priv_out)
476         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p))
477         || !TEST_BN_eq(&_bignum_ffdhe2048_p, p)
478         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q))
479         || !TEST_ptr(q)
480         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g))
481         || !TEST_BN_eq(&_bignum_const_2, g)
482         || !TEST_false(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_COFACTOR,
483                                              &j))
484         || !TEST_ptr_null(j)
485         || !TEST_false(EVP_PKEY_get_octet_string_param(pk,
486                                                        OSSL_PKEY_PARAM_FFC_SEED,
487                                                        seed_out,
488                                                        sizeof(seed_out), &len))
489         || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_GINDEX,
490                                              &gindex))
491         || !TEST_int_eq(gindex, -1)
492         || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H, &hindex))
493         || !TEST_int_eq(hindex, 0)
494         || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_PCOUNTER,
495                                              &pcounter))
496         || !TEST_int_eq(pcounter, -1))
497         goto err;
498
499     if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
500         goto err;
501
502     if (!TEST_true(EVP_PKEY_check(key_ctx))
503         || !TEST_true(EVP_PKEY_public_check(key_ctx))
504         || !TEST_true(EVP_PKEY_private_check(key_ctx))
505         || !TEST_true(EVP_PKEY_pairwise_check(key_ctx)))
506         goto err;
507
508     if (!TEST_ptr(copy_pk = EVP_PKEY_new())
509         || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk)))
510         goto err;
511
512     ret = test_print_key_using_pem("DH", pk)
513           && test_print_key_using_serializer("DH", pk);
514 err:
515     BN_free(p);
516     BN_free(q);
517     BN_free(g);
518     BN_free(j);
519     BN_free(pub);
520     BN_free(priv);
521     BN_free(pub_out);
522     BN_free(priv_out);
523     EVP_PKEY_free(copy_pk);
524     EVP_PKEY_free(pk);
525     EVP_PKEY_CTX_free(ctx);
526     EVP_PKEY_CTX_free(key_ctx);
527     OSSL_PARAM_BLD_free_params(fromdata_params);
528     OSSL_PARAM_BLD_free(bld);
529
530     return ret;
531 }
532
533 static int test_fromdata_dh_fips186_4(void)
534 {
535     int ret = 0;
536     int gindex = 0, pcounter = 0, hindex = 0;
537     EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
538     EVP_PKEY *pk = NULL;
539     size_t len;
540     BIGNUM *pub = NULL, *priv = NULL;
541     BIGNUM *pub_out = NULL, *priv_out = NULL;
542     BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL;
543     OSSL_PARAM_BLD *bld = NULL;
544     OSSL_PARAM *fromdata_params = NULL;
545     char name_out[80];
546     unsigned char seed_out[32];
547
548     /*
549      * DH key data was generated using the following:
550      * openssl genpkey -algorithm DH -pkeyopt group:ffdhe2048 -text
551      */
552     static const unsigned char priv_data[] = {
553        0x88, 0x85, 0xe7, 0x9f, 0xee, 0x6d, 0xc5, 0x7c, 0x78, 0xaf, 0x63, 0x5d,
554        0x38, 0x2a, 0xd0, 0xed, 0x56, 0x4b, 0x47, 0x21, 0x2b, 0xfa, 0x55, 0xfa,
555        0x87, 0xe8, 0xa9, 0x7b,
556     };
557     static const unsigned char pub_data[] = {
558        0xd6, 0x2d, 0x77, 0xe0, 0xd3, 0x7d, 0xf8, 0xeb, 0x98, 0x50, 0xa1, 0x82,
559        0x22, 0x65, 0xd5, 0xd9, 0xfe, 0xc9, 0x3f, 0xbe, 0x16, 0x83, 0xbd, 0x33,
560        0xe9, 0xc6, 0x93, 0xcf, 0x08, 0xaf, 0x83, 0xfa, 0x80, 0x8a, 0x6c, 0x64,
561        0xdf, 0x70, 0x64, 0xd5, 0x0a, 0x7c, 0x5a, 0x72, 0xda, 0x66, 0xe6, 0xf9,
562        0xf5, 0x31, 0x21, 0x92, 0xb0, 0x60, 0x1a, 0xb5, 0xd3, 0xf0, 0xa5, 0xfa,
563        0x48, 0x95, 0x2e, 0x38, 0xd9, 0xc5, 0xe6, 0xda, 0xfb, 0x6c, 0x03, 0x9d,
564        0x4b, 0x69, 0xb7, 0x95, 0xe4, 0x5c, 0xc0, 0x93, 0x4f, 0x48, 0xd9, 0x7e,
565        0x06, 0x22, 0xb2, 0xde, 0xf3, 0x79, 0x24, 0xed, 0xe1, 0xd1, 0x4a, 0x57,
566        0xf1, 0x40, 0x86, 0x70, 0x42, 0x25, 0xc5, 0x27, 0x68, 0xc9, 0xfa, 0xe5,
567        0x8e, 0x62, 0x7e, 0xff, 0x49, 0x6c, 0x5b, 0xb5, 0xba, 0xf9, 0xef, 0x9a,
568        0x1a, 0x10, 0xd4, 0x81, 0x53, 0xcf, 0x83, 0x04, 0x18, 0x1c, 0xe1, 0xdb,
569        0xe1, 0x65, 0xa9, 0x7f, 0xe1, 0x33, 0xeb, 0xc3, 0x4f, 0xe3, 0xb7, 0x22,
570        0xf7, 0x1c, 0x09, 0x4f, 0xed, 0xc6, 0x07, 0x8e, 0x78, 0x05, 0x8f, 0x7c,
571        0x96, 0xd9, 0x12, 0xe0, 0x81, 0x74, 0x1a, 0xe9, 0x13, 0xc0, 0x20, 0x82,
572        0x65, 0xbb, 0x42, 0x3b, 0xed, 0x08, 0x6a, 0x84, 0x4f, 0xea, 0x77, 0x14,
573        0x32, 0xf9, 0xed, 0xc2, 0x12, 0xd6, 0xc5, 0xc6, 0xb3, 0xe5, 0xf2, 0x6e,
574        0xf6, 0x16, 0x7f, 0x37, 0xde, 0xbc, 0x09, 0xc7, 0x06, 0x6b, 0x12, 0xbc,
575        0xad, 0x2d, 0x49, 0x25, 0xd5, 0xdc, 0xf4, 0x18, 0x14, 0xd2, 0xf0, 0xf1,
576        0x1d, 0x1f, 0x3a, 0xaa, 0x15, 0x55, 0xbb, 0x0d, 0x7f, 0xbe, 0x67, 0xa1,
577        0xa7, 0xf0, 0xaa, 0xb3, 0xfb, 0x41, 0x82, 0x39, 0x49, 0x93, 0xbc, 0xa8,
578        0xee, 0x72, 0x13, 0x45, 0x65, 0x15, 0x42, 0x17, 0xaa, 0xd8, 0xab, 0xcf,
579        0x33, 0x42, 0x83, 0x42
580     };
581     static const char group_name[] = "ffdhe2048";
582
583
584     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
585         || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL))
586         || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL))
587         || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
588                                                       OSSL_PKEY_PARAM_FFC_GROUP,
589                                                       group_name, 0))
590         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub))
591         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv))
592         || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
593         goto err;
594
595     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL)))
596         goto err;
597
598     if (!TEST_true(EVP_PKEY_key_fromdata_init(ctx))
599         || !TEST_true(EVP_PKEY_fromdata(ctx, &pk, fromdata_params))
600         || !TEST_int_eq(EVP_PKEY_bits(pk), 2048)
601         || !TEST_int_eq(EVP_PKEY_security_bits(pk), 112)
602         || !TEST_int_eq(EVP_PKEY_size(pk), 256))
603         goto err;
604
605     if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_FFC_GROUP,
606                                                   name_out, sizeof(name_out),
607                                                   &len))
608         || !TEST_str_eq(name_out, group_name)
609         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY,
610                                             &pub_out))
611         || !TEST_BN_eq(pub, pub_out)
612         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY,
613                                             &priv_out))
614         || !TEST_BN_eq(priv, priv_out)
615         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p))
616         || !TEST_BN_eq(&_bignum_ffdhe2048_p, p)
617         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q))
618         || !TEST_ptr(q)
619         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g))
620         || !TEST_BN_eq(&_bignum_const_2, g)
621         || !TEST_false(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_COFACTOR,
622                                              &j))
623         || !TEST_ptr_null(j)
624         || !TEST_false(EVP_PKEY_get_octet_string_param(pk,
625                                                        OSSL_PKEY_PARAM_FFC_SEED,
626                                                        seed_out,
627                                                        sizeof(seed_out), &len))
628         || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_GINDEX,
629                                              &gindex))
630         || !TEST_int_eq(gindex, -1)
631         || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H, &hindex))
632         || !TEST_int_eq(hindex, 0)
633         || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_PCOUNTER,
634                                              &pcounter))
635         || !TEST_int_eq(pcounter, -1))
636         goto err;
637
638     if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
639         goto err;
640
641     if (!TEST_true(EVP_PKEY_check(key_ctx))
642         || !TEST_true(EVP_PKEY_public_check(key_ctx))
643         || !TEST_true(EVP_PKEY_private_check(key_ctx))
644         || !TEST_true(EVP_PKEY_pairwise_check(key_ctx)))
645         goto err;
646
647     ret = test_print_key_using_pem("DH", pk)
648           && test_print_key_using_serializer("DH", pk);
649 err:
650     BN_free(p);
651     BN_free(q);
652     BN_free(g);
653     BN_free(j);
654     BN_free(pub);
655     BN_free(priv);
656     BN_free(pub_out);
657     BN_free(priv_out);
658     EVP_PKEY_free(pk);
659     EVP_PKEY_CTX_free(ctx);
660     EVP_PKEY_CTX_free(key_ctx);
661     OSSL_PARAM_BLD_free_params(fromdata_params);
662     OSSL_PARAM_BLD_free(bld);
663
664     return ret;
665 }
666
667 #endif
668
669
670
671 #ifndef OPENSSL_NO_EC
672 /* Array indexes used in test_fromdata_ecx */
673 # define PRIV_KEY        0
674 # define PUB_KEY         1
675
676 # define X25519_IDX      0
677 # define X448_IDX        1
678 # define ED25519_IDX     2
679 # define ED448_IDX       3
680
681 static int test_fromdata_ecx(int tst)
682 {
683     int ret = 0;
684     EVP_PKEY_CTX *ctx = NULL;
685     EVP_PKEY *pk = NULL, *copy_pk = NULL;
686     const char *alg = NULL;
687     size_t len;
688     unsigned char out_pub[ED448_KEYLEN];
689     unsigned char out_priv[ED448_KEYLEN];
690
691     /* ED448_KEYLEN > X448_KEYLEN > X25519_KEYLEN == ED25519_KEYLEN */
692     static unsigned char key_numbers[4][2][ED448_KEYLEN] = {
693         /* X25519: Keys from RFC 7748 6.1 */
694         {
695             /* Private Key */
696             {
697                 0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d, 0x3c, 0x16,
698                 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45, 0xdf, 0x4c, 0x2f, 0x87,
699                 0xeb, 0xc0, 0x99, 0x2a, 0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9,
700                 0x2c, 0x2a
701             },
702             /* Public Key */
703             {
704                 0x85, 0x20, 0xf0, 0x09, 0x89, 0x30, 0xa7, 0x54, 0x74, 0x8b,
705                 0x7d, 0xdc, 0xb4, 0x3e, 0xf7, 0x5a, 0x0d, 0xbf, 0x3a, 0x0d,
706                 0x26, 0x38, 0x1a, 0xf4, 0xeb, 0xa4, 0xa9, 0x8e, 0xaa, 0x9b,
707                 0x4e, 0x6a
708             }
709         },
710         /* X448: Keys from RFC 7748 6.2 */
711         {
712             /* Private Key */
713             {
714                 0x9a, 0x8f, 0x49, 0x25, 0xd1, 0x51, 0x9f, 0x57, 0x75, 0xcf,
715                 0x46, 0xb0, 0x4b, 0x58, 0x00, 0xd4, 0xee, 0x9e, 0xe8, 0xba,
716                 0xe8, 0xbc, 0x55, 0x65, 0xd4, 0x98, 0xc2, 0x8d, 0xd9, 0xc9,
717                 0xba, 0xf5, 0x74, 0xa9, 0x41, 0x97, 0x44, 0x89, 0x73, 0x91,
718                 0x00, 0x63, 0x82, 0xa6, 0xf1, 0x27, 0xab, 0x1d, 0x9a, 0xc2,
719                 0xd8, 0xc0, 0xa5, 0x98, 0x72, 0x6b
720             },
721             /* Public Key */
722             {
723                 0x9b, 0x08, 0xf7, 0xcc, 0x31, 0xb7, 0xe3, 0xe6, 0x7d, 0x22,
724                 0xd5, 0xae, 0xa1, 0x21, 0x07, 0x4a, 0x27, 0x3b, 0xd2, 0xb8,
725                 0x3d, 0xe0, 0x9c, 0x63, 0xfa, 0xa7, 0x3d, 0x2c, 0x22, 0xc5,
726                 0xd9, 0xbb, 0xc8, 0x36, 0x64, 0x72, 0x41, 0xd9, 0x53, 0xd4,
727                 0x0c, 0x5b, 0x12, 0xda, 0x88, 0x12, 0x0d, 0x53, 0x17, 0x7f,
728                 0x80, 0xe5, 0x32, 0xc4, 0x1f, 0xa0
729             }
730         },
731         /* ED25519: Keys from RFC 8032 */
732         {
733             /* Private Key */
734             {
735                 0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84,
736                 0x4a, 0xf4, 0x92, 0xec, 0x2c, 0xc4, 0x44, 0x49, 0xc5, 0x69,
737                 0x7b, 0x32, 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae,
738                 0x7f, 0x60
739             },
740             /* Public Key */
741             {
742                 0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b,
743                 0xfe, 0xd3, 0xc9, 0x64, 0x07, 0x3a, 0x0e, 0xe1, 0x72, 0xf3,
744                 0xda, 0xa6, 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07,
745                 0x51, 0x1a
746             }
747         },
748         /* ED448: Keys from RFC 8032 */
749         {
750             /* Private Key */
751             {
752                 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10, 0xd6, 0x32,
753                 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf, 0x6c, 0x92, 0x9f, 0x34,
754                 0xdd, 0xfa, 0x8c, 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3,
755                 0x48, 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
756                 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f, 0x03, 0x2e,
757                 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9, 0x5b
758             },
759             /* Public Key */
760             {
761                 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7,
762                 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
763                 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9,
764                 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
765                 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa,
766                 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61, 0x80
767             }
768         }
769     };
770     OSSL_PARAM x25519_fromdata_params[] = {
771         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
772                                 key_numbers[X25519_IDX][PRIV_KEY],
773                                 X25519_KEYLEN),
774         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
775                                 key_numbers[X25519_IDX][PUB_KEY],
776                                 X25519_KEYLEN),
777         OSSL_PARAM_END
778     };
779     OSSL_PARAM x448_fromdata_params[] = {
780         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
781                                 key_numbers[X448_IDX][PRIV_KEY],
782                                 X448_KEYLEN),
783         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
784                                 key_numbers[X448_IDX][PUB_KEY],
785                                 X448_KEYLEN),
786         OSSL_PARAM_END
787     };
788     OSSL_PARAM ed25519_fromdata_params[] = {
789         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
790                                 key_numbers[ED25519_IDX][PRIV_KEY],
791                                 ED25519_KEYLEN),
792         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
793                                 key_numbers[ED25519_IDX][PUB_KEY],
794                                 ED25519_KEYLEN),
795         OSSL_PARAM_END
796     };
797     OSSL_PARAM ed448_fromdata_params[] = {
798         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
799                                 key_numbers[ED448_IDX][PRIV_KEY],
800                                 ED448_KEYLEN),
801         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
802                                 key_numbers[ED448_IDX][PUB_KEY],
803                                 ED448_KEYLEN),
804         OSSL_PARAM_END
805     };
806     OSSL_PARAM *fromdata_params = NULL;
807     int bits = 0, security_bits = 0, size = 0;
808
809     switch (tst) {
810     case X25519_IDX:
811         fromdata_params = x25519_fromdata_params;
812         bits = X25519_BITS;
813         security_bits = X25519_SECURITY_BITS;
814         size = X25519_KEYLEN;
815         alg = "X25519";
816         break;
817
818     case X448_IDX:
819         fromdata_params = x448_fromdata_params;
820         bits = X448_BITS;
821         security_bits = X448_SECURITY_BITS;
822         size = X448_KEYLEN;
823         alg = "X448";
824         break;
825
826     case ED25519_IDX:
827         fromdata_params = ed25519_fromdata_params;
828         bits = ED25519_BITS;
829         security_bits = ED25519_SECURITY_BITS;
830         size = ED25519_KEYLEN;
831         alg = "ED25519";
832         break;
833
834     case ED448_IDX:
835         fromdata_params = ed448_fromdata_params;
836         bits = ED448_BITS;
837         security_bits = ED448_SECURITY_BITS;
838         size = ED448_KEYLEN;
839         alg = "ED448";
840         break;
841     }
842
843     ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL);
844     if (!TEST_ptr(ctx))
845         goto err;
846
847     if (!TEST_true(EVP_PKEY_key_fromdata_init(ctx))
848         || !TEST_true(EVP_PKEY_fromdata(ctx, &pk, fromdata_params))
849         || !TEST_int_eq(EVP_PKEY_bits(pk), bits)
850         || !TEST_int_eq(EVP_PKEY_security_bits(pk), security_bits)
851         || !TEST_int_eq(EVP_PKEY_size(pk), size))
852         goto err;
853
854     if (!TEST_ptr(copy_pk = EVP_PKEY_new())
855         || !TEST_false(EVP_PKEY_copy_parameters(copy_pk, pk)))
856         goto err;
857
858     if (!TEST_true(EVP_PKEY_get_octet_string_param(
859                        pk, fromdata_params[PRIV_KEY].key,
860                        out_priv, sizeof(out_priv), &len))
861         || !TEST_mem_eq(out_priv, len,
862                         fromdata_params[PRIV_KEY].data,
863                         fromdata_params[PRIV_KEY].data_size)
864         || !TEST_true(EVP_PKEY_get_octet_string_param(
865                           pk, fromdata_params[PUB_KEY].key,
866                           out_pub, sizeof(out_pub), &len))
867         || !TEST_mem_eq(out_pub, len,
868                         fromdata_params[PUB_KEY].data,
869                         fromdata_params[PUB_KEY].data_size))
870         goto err;
871
872     ret = test_print_key_using_pem(alg, pk)
873           && test_print_key_using_serializer(alg, pk);
874
875 err:
876     EVP_PKEY_free(pk);
877     EVP_PKEY_free(copy_pk);
878     EVP_PKEY_CTX_free(ctx);
879
880     return ret;
881 }
882
883 #define CURVE_NAME 2
884
885 static int test_fromdata_ec(void)
886 {
887     int ret = 0;
888     EVP_PKEY_CTX *ctx = NULL;
889     EVP_PKEY *pk = NULL, *copy_pk = NULL;
890     OSSL_PARAM_BLD *bld = NULL;
891     BIGNUM *ec_priv_bn = NULL;
892     BIGNUM *bn_priv = NULL;
893     OSSL_PARAM *fromdata_params = NULL;
894     const char *alg = "EC";
895     const char *curve = "prime256v1";
896     /* UNCOMPRESSED FORMAT */
897     static const unsigned char ec_pub_keydata[] = {
898        POINT_CONVERSION_UNCOMPRESSED,
899        0x1b, 0x93, 0x67, 0x55, 0x1c, 0x55, 0x9f, 0x63,
900        0xd1, 0x22, 0xa4, 0xd8, 0xd1, 0x0a, 0x60, 0x6d,
901        0x02, 0xa5, 0x77, 0x57, 0xc8, 0xa3, 0x47, 0x73,
902        0x3a, 0x6a, 0x08, 0x28, 0x39, 0xbd, 0xc9, 0xd2,
903        0x80, 0xec, 0xe9, 0xa7, 0x08, 0x29, 0x71, 0x2f,
904        0xc9, 0x56, 0x82, 0xee, 0x9a, 0x85, 0x0f, 0x6d,
905        0x7f, 0x59, 0x5f, 0x8c, 0xd1, 0x96, 0x0b, 0xdf,
906        0x29, 0x3e, 0x49, 0x07, 0x88, 0x3f, 0x9a, 0x29
907     };
908     static const unsigned char ec_priv_keydata[] = {
909         0x33, 0xd0, 0x43, 0x83, 0xa9, 0x89, 0x56, 0x03,
910         0xd2, 0xd7, 0xfe, 0x6b, 0x01, 0x6f, 0xe4, 0x59,
911         0xcc, 0x0d, 0x9a, 0x24, 0x6c, 0x86, 0x1b, 0x2e,
912         0xdc, 0x4b, 0x4d, 0x35, 0x43, 0xe1, 0x1b, 0xad
913     };
914     const int compressed_sz = 1 + (sizeof(ec_pub_keydata) - 1) / 2;
915     unsigned char out_pub[sizeof(ec_pub_keydata)];
916     char out_curve_name[80];
917     const OSSL_PARAM *gettable = NULL;
918     size_t len;
919
920
921     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()))
922         goto err;
923     if (!TEST_ptr(ec_priv_bn = BN_bin2bn(ec_priv_keydata,
924                                          sizeof(ec_priv_keydata), NULL)))
925         goto err;
926
927     if (OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_NAME,
928                                         curve, 0) <= 0)
929         goto err;
930     if (OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY,
931                                          ec_pub_keydata,
932                                          sizeof(ec_pub_keydata)) <= 0)
933         goto err;
934     if (OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, ec_priv_bn) <= 0)
935         goto err;
936     if (!TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
937         goto err;
938     ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL);
939     if (!TEST_ptr(ctx))
940         goto err;
941
942     if (!TEST_true(EVP_PKEY_key_fromdata_init(ctx))
943         || !TEST_true(EVP_PKEY_fromdata(ctx, &pk, fromdata_params))
944         || !TEST_int_eq(EVP_PKEY_bits(pk), 256)
945         || !TEST_int_eq(EVP_PKEY_security_bits(pk), 128)
946         || !TEST_int_eq(EVP_PKEY_size(pk), 2 + 35 * 2))
947         goto err;
948
949     if (!TEST_ptr(copy_pk = EVP_PKEY_new())
950         || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk)))
951         goto err;
952
953     if (!TEST_ptr(gettable = EVP_PKEY_gettable_params(pk))
954         || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_NAME))
955         || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_PUB_KEY))
956         || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_PRIV_KEY)))
957         goto err;
958
959     if (!EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_EC_NAME,
960                                         out_curve_name, sizeof(out_curve_name),
961                                         &len)
962         || !TEST_str_eq(out_curve_name, curve)
963         || !EVP_PKEY_get_octet_string_param(pk, OSSL_PKEY_PARAM_PUB_KEY,
964                                             out_pub, sizeof(out_pub), &len)
965         || !TEST_true(out_pub[0] == (POINT_CONVERSION_COMPRESSED + 1))
966         || !TEST_mem_eq(out_pub + 1, len - 1,
967                         ec_pub_keydata + 1, compressed_sz - 1)
968         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY,
969                                             &bn_priv))
970         || !TEST_BN_eq(ec_priv_bn, bn_priv))
971         goto err;
972
973     ret = test_print_key_using_pem(alg, pk)
974           && test_print_key_using_serializer(alg, pk);
975 err:
976     BN_free(bn_priv);
977     BN_free(ec_priv_bn);
978     OSSL_PARAM_BLD_free_params(fromdata_params);
979     OSSL_PARAM_BLD_free(bld);
980     EVP_PKEY_free(pk);
981     EVP_PKEY_free(copy_pk);
982     EVP_PKEY_CTX_free(ctx);
983     return ret;
984 }
985
986 #endif /* OPENSSL_NO_EC */
987
988 #ifndef OPENSSL_NO_DSA
989 static int test_fromdata_dsa_fips186_4(void)
990 {
991     int ret = 0;
992     EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
993     EVP_PKEY *pk = NULL, *copy_pk = NULL;
994     BIGNUM *pub = NULL, *priv = NULL;
995     BIGNUM *p = NULL, *q = NULL, *g = NULL;
996     BIGNUM *pub_out = NULL, *priv_out = NULL;
997     BIGNUM *p_out = NULL, *q_out = NULL, *g_out = NULL, *j_out = NULL;
998     int gindex_out = 0, pcounter_out = 0, hindex_out = 0;
999     char name_out[80];
1000     unsigned char seed_out[32];
1001     size_t len;
1002     OSSL_PARAM_BLD *bld = NULL;
1003     OSSL_PARAM *fromdata_params = NULL;
1004
1005     /*
1006      * DSA parameter data was generated using the following:
1007      * openssl genpkey -genparam -algorithm DSA -pkeyopt pbits:2048 \
1008      *                 -pkeyopt qbits:256 -pkeyopt type:0 \
1009      *                 -pkeyopt gindex:1 -out dsa_params.pem -text
1010      */
1011     static const unsigned char p_data[] = {
1012         0x00, 0xa0, 0xb7, 0x02, 0xc4, 0xac, 0xa6, 0x42, 0xab, 0xf2, 0x34, 0x0b,
1013         0x22, 0x47, 0x1f, 0x33, 0xcf, 0xd5, 0x04, 0xe4, 0x3e, 0xec, 0xa1, 0x21,
1014         0xc8, 0x41, 0x2b, 0xef, 0xb8, 0x1f, 0x0b, 0x5b, 0x88, 0x8b, 0x67, 0xf8,
1015         0x68, 0x6d, 0x7c, 0x4d, 0x96, 0x5f, 0x3c, 0x66, 0xef, 0x58, 0x34, 0xd7,
1016         0xf6, 0xa2, 0x1b, 0xad, 0xc8, 0x12, 0x52, 0xb8, 0xe8, 0x2a, 0x63, 0xcc,
1017         0xea, 0xe7, 0x4e, 0xc8, 0x34, 0x4c, 0x58, 0x59, 0x0a, 0xc2, 0x4a, 0xe4,
1018         0xb4, 0x64, 0x20, 0xf4, 0xf6, 0x0a, 0xcf, 0x86, 0x01, 0x6c, 0x7f, 0x23,
1019         0x4a, 0x51, 0x07, 0x99, 0x42, 0x28, 0x7a, 0xff, 0x18, 0x67, 0x52, 0x64,
1020         0xf2, 0x9a, 0x62, 0x30, 0xc3, 0x00, 0xde, 0x23, 0xe9, 0x11, 0x95, 0x7e,
1021         0xd1, 0x3d, 0x8d, 0xb4, 0x0e, 0x9f, 0x9e, 0xb1, 0x30, 0x03, 0xf0, 0x73,
1022         0xa8, 0x40, 0x48, 0x42, 0x7b, 0x60, 0xa0, 0xc4, 0xf2, 0x3b, 0x2d, 0x0a,
1023         0x0c, 0xb8, 0x19, 0xfb, 0xb4, 0xf8, 0xe0, 0x2a, 0xc7, 0xf1, 0xc0, 0xc6,
1024         0x86, 0x14, 0x60, 0x12, 0x0f, 0xc0, 0xde, 0x4a, 0x67, 0xec, 0xc7, 0xde,
1025         0x76, 0x21, 0x1a, 0x55, 0x7f, 0x86, 0xc3, 0x97, 0x98, 0xce, 0xf5, 0xcd,
1026         0xf0, 0xe7, 0x12, 0xd6, 0x93, 0xee, 0x1b, 0x9b, 0x61, 0xef, 0x05, 0x8c,
1027         0x45, 0x46, 0xd9, 0x64, 0x6f, 0xbe, 0x27, 0xaa, 0x67, 0x01, 0xcc, 0x71,
1028         0xb1, 0x60, 0xce, 0x21, 0xd8, 0x51, 0x17, 0x27, 0x0d, 0x90, 0x3d, 0x18,
1029         0x7c, 0x87, 0x15, 0x8e, 0x48, 0x4c, 0x6c, 0xc5, 0x72, 0xeb, 0xb7, 0x56,
1030         0xf5, 0x6b, 0x60, 0x8f, 0xc2, 0xfd, 0x3f, 0x46, 0x5c, 0x00, 0x91, 0x85,
1031         0x79, 0x45, 0x5b, 0x1c, 0x82, 0xc4, 0x87, 0x50, 0x79, 0xba, 0xcc, 0x1c,
1032         0x32, 0x7e, 0x2e, 0xb8, 0x2e, 0xc5, 0x4e, 0xd1, 0x9b, 0xdb, 0x66, 0x79,
1033         0x7c, 0xfe, 0xaf, 0x6a, 0x05
1034     };
1035     static const unsigned char q_data[] = {
1036         0xa8, 0xcd, 0xf4, 0x33, 0x7b, 0x13, 0x0a, 0x24, 0xc1, 0xde, 0x4a, 0x04,
1037         0x7b, 0x4b, 0x71, 0x51, 0x32, 0xe9, 0x47, 0x74, 0xbd, 0x0c, 0x21, 0x40,
1038         0x84, 0x12, 0x0a, 0x17, 0x73, 0xdb, 0x29, 0xc7
1039     };
1040     static const unsigned char g_data[] = {
1041         0x6c, 0xc6, 0xa4, 0x3e, 0x61, 0x84, 0xc1, 0xff, 0x6f, 0x4a, 0x1a, 0x6b,
1042         0xb0, 0x24, 0x4b, 0xd2, 0x92, 0x5b, 0x29, 0x5c, 0x61, 0xb8, 0xc9, 0x2b,
1043         0xd6, 0xf7, 0x59, 0xfd, 0xd8, 0x70, 0x66, 0x77, 0xfc, 0xc1, 0xa4, 0xd4,
1044         0xb0, 0x1e, 0xd5, 0xbf, 0x59, 0x98, 0xb3, 0x66, 0x8b, 0xf4, 0x2e, 0xe6,
1045         0x12, 0x3e, 0xcc, 0xf8, 0x02, 0xb8, 0xc6, 0xc3, 0x47, 0xd2, 0xf5, 0xaa,
1046         0x0c, 0x5f, 0x51, 0xf5, 0xd0, 0x4c, 0x55, 0x3d, 0x07, 0x73, 0xa6, 0x57,
1047         0xce, 0x5a, 0xad, 0x42, 0x0c, 0x13, 0x0f, 0xe2, 0x31, 0x25, 0x8e, 0x72,
1048         0x12, 0x73, 0x10, 0xdb, 0x7f, 0x79, 0xeb, 0x59, 0xfc, 0xfe, 0xf7, 0x0c,
1049         0x1a, 0x81, 0x53, 0x96, 0x22, 0xb8, 0xe7, 0x58, 0xd8, 0x67, 0x80, 0x60,
1050         0xad, 0x8b, 0x55, 0x1c, 0x91, 0xf0, 0x72, 0x9a, 0x7e, 0xad, 0x37, 0xf1,
1051         0x77, 0x18, 0x96, 0x8a, 0x68, 0x70, 0xfc, 0x71, 0xa9, 0xa2, 0xe8, 0x35,
1052         0x27, 0x78, 0xf2, 0xef, 0x59, 0x36, 0x6d, 0x7c, 0xb6, 0x98, 0xd8, 0x1e,
1053         0xfa, 0x25, 0x73, 0x97, 0x45, 0x58, 0xe3, 0xae, 0xbd, 0x52, 0x54, 0x05,
1054         0xd8, 0x26, 0x26, 0xba, 0xba, 0x05, 0xb5, 0xe9, 0xe5, 0x76, 0xae, 0x25,
1055         0xdd, 0xfc, 0x10, 0x89, 0x5a, 0xa9, 0xee, 0x59, 0xc5, 0x79, 0x8b, 0xeb,
1056         0x1e, 0x2c, 0x61, 0xab, 0x0d, 0xd1, 0x10, 0x04, 0x91, 0x32, 0x77, 0x4a,
1057         0xa6, 0x64, 0x53, 0xda, 0x4c, 0xd7, 0x3a, 0x29, 0xd4, 0xf3, 0x82, 0x25,
1058         0x1d, 0x6f, 0x4a, 0x7f, 0xd3, 0x08, 0x3b, 0x42, 0x30, 0x10, 0xd8, 0xd0,
1059         0x97, 0x3a, 0xeb, 0x92, 0x63, 0xec, 0x93, 0x2b, 0x6f, 0x32, 0xd8, 0xcd,
1060         0x80, 0xd3, 0xc0, 0x4c, 0x03, 0xd5, 0xca, 0xbc, 0x8f, 0xc7, 0x43, 0x53,
1061         0x64, 0x66, 0x1c, 0x82, 0x2d, 0xfb, 0xff, 0x39, 0xba, 0xd6, 0x42, 0x62,
1062         0x02, 0x6f, 0x96, 0x36
1063     };
1064     static const unsigned char seed_data[] = {
1065         0x64, 0x46, 0x07, 0x32, 0x8d, 0x70, 0x9c, 0xb3, 0x8a, 0x35, 0xde, 0x62,
1066         0x00, 0xf2, 0x6d, 0x52, 0x37, 0x4d, 0xb3, 0x84, 0xe1, 0x9d, 0x41, 0x04,
1067         0xda, 0x7b, 0xdc, 0x0d, 0x8b, 0x5e, 0xe0, 0x84
1068     };
1069     const int gindex = 1;
1070     const int pcounter = 53;
1071     /*
1072      * The keypair was generated using
1073      * openssl genpkey -paramfile dsa_params.pem --pkeyopt pcounter:53 \
1074      *                 -pkeyopt gindex:1 \
1075      *                 -pkeyopt hexseed:644607328d709cb38a35de6200f26d -text
1076      */
1077     static const unsigned char priv_data[] = {
1078         0x00, 0x8f, 0xc5, 0x9e, 0xd0, 0xf7, 0x2a, 0x0b, 0x66, 0xf1, 0x32, 0x73,
1079         0xae, 0xf6, 0xd9, 0xd4, 0xdb, 0x2d, 0x96, 0x55, 0x89, 0xff, 0xef, 0xa8,
1080         0x5f, 0x47, 0x8f, 0xca, 0x02, 0x8a, 0xe1, 0x35, 0x90
1081     };
1082     static const unsigned char pub_data[] = {
1083         0x44, 0x19, 0xc9, 0x46, 0x45, 0x57, 0xc1, 0xa9, 0xd8, 0x30, 0x99, 0x29,
1084         0x6a, 0x4b, 0x63, 0x71, 0x69, 0x96, 0x35, 0x17, 0xb2, 0x62, 0x9b, 0x80,
1085         0x0a, 0x95, 0x9d, 0x6a, 0xc0, 0x32, 0x0d, 0x07, 0x5f, 0x19, 0x44, 0x02,
1086         0xf1, 0xbd, 0xce, 0xdf, 0x10, 0xf8, 0x02, 0x5d, 0x7d, 0x98, 0x8a, 0x73,
1087         0x89, 0x00, 0xb6, 0x24, 0xd6, 0x33, 0xe7, 0xcf, 0x8b, 0x49, 0x2a, 0xaf,
1088         0x13, 0x1c, 0xb2, 0x52, 0x15, 0xfd, 0x9b, 0xd5, 0x40, 0x4a, 0x1a, 0xda,
1089         0x29, 0x4c, 0x92, 0x7e, 0x66, 0x06, 0xdb, 0x61, 0x86, 0xac, 0xb5, 0xda,
1090         0x3c, 0x7d, 0x73, 0x7e, 0x54, 0x32, 0x68, 0xa5, 0x02, 0xbc, 0x59, 0x47,
1091         0x84, 0xd3, 0x87, 0x71, 0x5f, 0xeb, 0x43, 0x45, 0x24, 0xd3, 0xec, 0x08,
1092         0x52, 0xc2, 0x89, 0x2d, 0x9c, 0x1a, 0xcc, 0x91, 0x65, 0x5d, 0xa3, 0xa1,
1093         0x35, 0x31, 0x10, 0x1c, 0x3a, 0xa8, 0x4d, 0x18, 0xd5, 0x06, 0xaf, 0xb2,
1094         0xec, 0x5c, 0x89, 0x9e, 0x90, 0x86, 0x10, 0x01, 0xeb, 0x51, 0xd5, 0x1b,
1095         0x9c, 0xcb, 0x66, 0x07, 0x3f, 0xc4, 0x6e, 0x0a, 0x1b, 0x73, 0xa0, 0x4b,
1096         0x5f, 0x4d, 0xab, 0x35, 0x28, 0xfa, 0xda, 0x3a, 0x0c, 0x08, 0xe8, 0xf3,
1097         0xef, 0x42, 0x67, 0xbc, 0x21, 0xf2, 0xc2, 0xb8, 0xff, 0x1a, 0x81, 0x05,
1098         0x68, 0x73, 0x62, 0xdf, 0xd7, 0xab, 0x0f, 0x22, 0x89, 0x57, 0x96, 0xd4,
1099         0x93, 0xaf, 0xa1, 0x21, 0xa3, 0x48, 0xe9, 0xf0, 0x97, 0x47, 0xa0, 0x27,
1100         0xba, 0x87, 0xb8, 0x15, 0x5f, 0xff, 0x2c, 0x50, 0x41, 0xf1, 0x7e, 0xc6,
1101         0x81, 0xc4, 0x51, 0xf1, 0xfd, 0xd6, 0x86, 0xf7, 0x69, 0x97, 0xf1, 0x49,
1102         0xc9, 0xf9, 0xf4, 0x9b, 0xf4, 0xe8, 0x85, 0xa7, 0xbd, 0x36, 0x55, 0x4a,
1103         0x3d, 0xe8, 0x65, 0x09, 0x7b, 0xb7, 0x12, 0x64, 0xd2, 0x0a, 0x53, 0x60,
1104         0x48, 0xd1, 0x8a, 0xbd
1105     };
1106
1107     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
1108         || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL))
1109         || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL))
1110         || !TEST_ptr(p = BN_bin2bn(p_data, sizeof(p_data), NULL))
1111         || !TEST_ptr(q = BN_bin2bn(q_data, sizeof(q_data), NULL))
1112         || !TEST_ptr(g = BN_bin2bn(g_data, sizeof(g_data), NULL))
1113
1114         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
1115         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
1116         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
1117         || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
1118                                                        OSSL_PKEY_PARAM_FFC_SEED,
1119                                                        seed_data,
1120                                                        sizeof(seed_data)))
1121         || !TEST_true(OSSL_PARAM_BLD_push_int(bld, OSSL_PKEY_PARAM_FFC_GINDEX,
1122                                               gindex))
1123         || !TEST_true(OSSL_PARAM_BLD_push_int(bld,
1124                                               OSSL_PKEY_PARAM_FFC_PCOUNTER,
1125                                               pcounter))
1126         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
1127                                              pub))
1128         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
1129                                              priv))
1130         || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
1131         goto err;
1132
1133     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL)))
1134         goto err;
1135
1136     if (!TEST_true(EVP_PKEY_key_fromdata_init(ctx))
1137         || !TEST_true(EVP_PKEY_fromdata(ctx, &pk, fromdata_params))
1138         || !TEST_int_eq(EVP_PKEY_bits(pk), 2048)
1139         || !TEST_int_eq(EVP_PKEY_security_bits(pk), 112)
1140         || !TEST_int_eq(EVP_PKEY_size(pk), 2 + 2 * (3 + sizeof(q_data))))
1141         goto err;
1142
1143     if (!TEST_false(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_FFC_GROUP,
1144                                                    name_out, sizeof(name_out),
1145                                                    &len))
1146         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY,
1147                                             &pub_out))
1148         || !TEST_BN_eq(pub, pub_out)
1149         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY,
1150                                             &priv_out))
1151         || !TEST_BN_eq(priv, priv_out)
1152         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p_out))
1153         || !TEST_BN_eq(p, p_out)
1154         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q_out))
1155         || !TEST_BN_eq(q, q_out)
1156         || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g_out))
1157         || !TEST_BN_eq(g, g_out)
1158         || !TEST_false(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_COFACTOR,
1159                                              &j_out))
1160         || !TEST_ptr_null(j_out)
1161         || !TEST_true(EVP_PKEY_get_octet_string_param(pk,
1162                                                       OSSL_PKEY_PARAM_FFC_SEED,
1163                                                       seed_out, sizeof(seed_out),
1164                                                       &len))
1165         || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_GINDEX,
1166                                              &gindex_out))
1167         || !TEST_int_eq(gindex, gindex_out)
1168         || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H,
1169                                              &hindex_out))
1170         || !TEST_int_eq(hindex_out, 0)
1171         || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_PCOUNTER,
1172                                              &pcounter_out))
1173         || !TEST_int_eq(pcounter, pcounter_out))
1174         goto err;
1175
1176     if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
1177         goto err;
1178
1179     if (!TEST_true(EVP_PKEY_check(key_ctx))
1180         || !TEST_true(EVP_PKEY_public_check(key_ctx))
1181         || !TEST_true(EVP_PKEY_private_check(key_ctx))
1182         || !TEST_true(EVP_PKEY_pairwise_check(key_ctx)))
1183         goto err;
1184
1185     if (!TEST_ptr(copy_pk = EVP_PKEY_new())
1186         || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk)))
1187         goto err;
1188
1189     ret = test_print_key_using_pem("DSA", pk)
1190           && test_print_key_using_serializer("DSA", pk);
1191  err:
1192     OSSL_PARAM_BLD_free_params(fromdata_params);
1193     OSSL_PARAM_BLD_free(bld);
1194     BN_free(p);
1195     BN_free(q);
1196     BN_free(g);
1197     BN_free(pub);
1198     BN_free(priv);
1199     BN_free(p_out);
1200     BN_free(q_out);
1201     BN_free(g_out);
1202     BN_free(pub_out);
1203     BN_free(priv_out);
1204     BN_free(j_out);
1205     EVP_PKEY_free(pk);
1206     EVP_PKEY_free(copy_pk);
1207     EVP_PKEY_CTX_free(ctx);
1208     EVP_PKEY_CTX_free(key_ctx);
1209
1210     return ret;
1211 }
1212 #endif /* OPENSSL_NO_DSA */
1213
1214
1215 int setup_tests(void)
1216 {
1217     if (!test_skip_common_options()) {
1218         TEST_error("Error parsing test options\n");
1219         return 0;
1220     }
1221
1222     if (!TEST_ptr(datadir = test_get_argument(0)))
1223         return 0;
1224
1225     ADD_TEST(test_evp_pkey_get_bn_param_large);
1226     ADD_TEST(test_fromdata_rsa);
1227 #ifndef OPENSSL_NO_DH
1228     ADD_TEST(test_fromdata_dh_fips186_4);
1229     ADD_TEST(test_fromdata_dh_named_group);
1230 #endif
1231 #ifndef OPENSSL_NO_DSA
1232     ADD_TEST(test_fromdata_dsa_fips186_4);
1233 #endif
1234 #ifndef OPENSSL_NO_EC
1235     ADD_ALL_TESTS(test_fromdata_ecx, 4);
1236     ADD_TEST(test_fromdata_ec);
1237 #endif
1238     return 1;
1239 }