30da6ed8b1ef22d5b6670d9d44be74cab3f7db2c
[openssl.git] / test / evp_pkey_dparams_test.c
1 /*
2  * Copyright 2019 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 <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "internal/nelem.h"
15 #include <openssl/crypto.h>
16 #include <openssl/bio.h>
17 #include <openssl/bn.h>
18 #include <openssl/rand.h>
19 #include <openssl/err.h>
20 #include <openssl/evp.h>
21 #include <openssl/ec.h>
22 #include "testutil.h"
23
24 static int pkey_param_types[] = {
25 #ifndef OPENSSL_NO_DH
26     EVP_PKEY_DH,
27 #endif
28 #ifndef OPENSSL_NO_DSA
29     EVP_PKEY_DSA,
30 #endif
31 #ifndef OPENSSL_NO_EC
32     EVP_PKEY_EC
33 #endif
34 };
35
36 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
37
38 static int params_bio_test(int id)
39 {
40     int ret;
41     BIO *mem_bio = NULL;
42     EVP_PKEY_CTX *ctx = NULL;
43     EVP_PKEY *params_key = NULL, *out_key = NULL;
44     int type = pkey_param_types[id];
45
46     ret =
47         TEST_ptr(mem_bio = BIO_new(BIO_s_mem()))
48         && TEST_ptr(ctx = EVP_PKEY_CTX_new_id(type, NULL))
49         && TEST_int_gt(EVP_PKEY_paramgen_init(ctx), 0)
50         && (type != EVP_PKEY_EC
51            || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, NID_secp256k1) > 0)
52         && TEST_int_gt(EVP_PKEY_paramgen(ctx, &params_key), 0)
53         && TEST_int_gt(i2d_KeyParams_bio(mem_bio, params_key), 0)
54         && TEST_ptr(d2i_KeyParams_bio(type, &out_key, mem_bio))
55         && TEST_int_gt(EVP_PKEY_cmp_parameters(out_key, params_key), 0);
56
57     BIO_free(mem_bio);
58     EVP_PKEY_CTX_free(ctx);
59     EVP_PKEY_free(params_key);
60     EVP_PKEY_free(out_key);
61     return ret;
62 }
63 #endif
64
65 int setup_tests(void)
66 {
67 #if defined(OPENSSL_NO_DH) && defined(OPENSSL_NO_DSA) && defined(OPENSSL_NO_EC)
68     TEST_note("No DH/DSA/EC support");
69 #else
70     ADD_ALL_TESTS(params_bio_test, OSSL_NELEM(pkey_param_types));
71 #endif
72     return 1;
73 }