1*b0d17251Schristos /*
2*b0d17251Schristos * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b0d17251Schristos * this file except in compliance with the License. You can obtain a copy
6*b0d17251Schristos * in the file LICENSE in the source distribution or at
7*b0d17251Schristos * https://www.openssl.org/source/license.html
8*b0d17251Schristos */
9*b0d17251Schristos
10*b0d17251Schristos /*
11*b0d17251Schristos
12*b0d17251Schristos * These tests are setup to load null into the default library context.
13*b0d17251Schristos * Any tests are expected to use the created 'libctx' to find algorithms.
14*b0d17251Schristos * The framework runs the tests twice using the 'default' provider or
15*b0d17251Schristos * 'fips' provider as inputs.
16*b0d17251Schristos */
17*b0d17251Schristos
18*b0d17251Schristos /*
19*b0d17251Schristos * DSA/DH low level APIs are deprecated for public use, but still ok for
20*b0d17251Schristos * internal use.
21*b0d17251Schristos */
22*b0d17251Schristos #include "internal/deprecated.h"
23*b0d17251Schristos #include <assert.h>
24*b0d17251Schristos #include <openssl/evp.h>
25*b0d17251Schristos #include <openssl/provider.h>
26*b0d17251Schristos #include <openssl/dsa.h>
27*b0d17251Schristos #include <openssl/dh.h>
28*b0d17251Schristos #include <openssl/safestack.h>
29*b0d17251Schristos #include <openssl/core_dispatch.h>
30*b0d17251Schristos #include <openssl/core_names.h>
31*b0d17251Schristos #include <openssl/x509.h>
32*b0d17251Schristos #include <openssl/encoder.h>
33*b0d17251Schristos #include "testutil.h"
34*b0d17251Schristos #include "internal/nelem.h"
35*b0d17251Schristos #include "crypto/bn_dh.h" /* _bignum_ffdhe2048_p */
36*b0d17251Schristos
37*b0d17251Schristos static OSSL_LIB_CTX *libctx = NULL;
38*b0d17251Schristos static OSSL_PROVIDER *nullprov = NULL;
39*b0d17251Schristos static OSSL_PROVIDER *libprov = NULL;
40*b0d17251Schristos static STACK_OF(OPENSSL_STRING) *cipher_names = NULL;
41*b0d17251Schristos
42*b0d17251Schristos typedef enum OPTION_choice {
43*b0d17251Schristos OPT_ERR = -1,
44*b0d17251Schristos OPT_EOF = 0,
45*b0d17251Schristos OPT_CONFIG_FILE,
46*b0d17251Schristos OPT_PROVIDER_NAME,
47*b0d17251Schristos OPT_TEST_ENUM
48*b0d17251Schristos } OPTION_CHOICE;
49*b0d17251Schristos
test_get_options(void)50*b0d17251Schristos const OPTIONS *test_get_options(void)
51*b0d17251Schristos {
52*b0d17251Schristos static const OPTIONS test_options[] = {
53*b0d17251Schristos OPT_TEST_OPTIONS_DEFAULT_USAGE,
54*b0d17251Schristos { "config", OPT_CONFIG_FILE, '<',
55*b0d17251Schristos "The configuration file to use for the libctx" },
56*b0d17251Schristos { "provider", OPT_PROVIDER_NAME, 's',
57*b0d17251Schristos "The provider to load (The default value is 'default')" },
58*b0d17251Schristos { NULL }
59*b0d17251Schristos };
60*b0d17251Schristos return test_options;
61*b0d17251Schristos }
62*b0d17251Schristos
63*b0d17251Schristos #ifndef OPENSSL_NO_DH
getname(int id)64*b0d17251Schristos static const char *getname(int id)
65*b0d17251Schristos {
66*b0d17251Schristos const char *name[] = {"p", "q", "g" };
67*b0d17251Schristos
68*b0d17251Schristos if (id >= 0 && id < 3)
69*b0d17251Schristos return name[id];
70*b0d17251Schristos return "?";
71*b0d17251Schristos }
72*b0d17251Schristos #endif
73*b0d17251Schristos
74*b0d17251Schristos /*
75*b0d17251Schristos * We're using some DH specific values in this test, so we skip compilation if
76*b0d17251Schristos * we're in a no-dh build.
77*b0d17251Schristos */
78*b0d17251Schristos #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DH)
79*b0d17251Schristos
test_dsa_param_keygen(int tstid)80*b0d17251Schristos static int test_dsa_param_keygen(int tstid)
81*b0d17251Schristos {
82*b0d17251Schristos int ret = 0;
83*b0d17251Schristos int expected;
84*b0d17251Schristos EVP_PKEY_CTX *gen_ctx = NULL;
85*b0d17251Schristos EVP_PKEY *pkey_parm = NULL;
86*b0d17251Schristos EVP_PKEY *pkey = NULL, *dup_pk = NULL;
87*b0d17251Schristos DSA *dsa = NULL;
88*b0d17251Schristos int pind, qind, gind;
89*b0d17251Schristos BIGNUM *p = NULL, *q = NULL, *g = NULL;
90*b0d17251Schristos
91*b0d17251Schristos /*
92*b0d17251Schristos * Just grab some fixed dh p, q, g values for testing,
93*b0d17251Schristos * these 'safe primes' should not be used normally for dsa *.
94*b0d17251Schristos */
95*b0d17251Schristos static const BIGNUM *bn[] = {
96*b0d17251Schristos &ossl_bignum_dh2048_256_p, &ossl_bignum_dh2048_256_q,
97*b0d17251Schristos &ossl_bignum_dh2048_256_g
98*b0d17251Schristos };
99*b0d17251Schristos
100*b0d17251Schristos /*
101*b0d17251Schristos * These tests are using bad values for p, q, g by reusing the values.
102*b0d17251Schristos * A value of 0 uses p, 1 uses q and 2 uses g.
103*b0d17251Schristos * There are 27 different combinations, with only the 1 valid combination.
104*b0d17251Schristos */
105*b0d17251Schristos pind = tstid / 9;
106*b0d17251Schristos qind = (tstid / 3) % 3;
107*b0d17251Schristos gind = tstid % 3;
108*b0d17251Schristos expected = (pind == 0 && qind == 1 && gind == 2);
109*b0d17251Schristos
110*b0d17251Schristos TEST_note("Testing with (p, q, g) = (%s, %s, %s)\n", getname(pind),
111*b0d17251Schristos getname(qind), getname(gind));
112*b0d17251Schristos
113*b0d17251Schristos if (!TEST_ptr(pkey_parm = EVP_PKEY_new())
114*b0d17251Schristos || !TEST_ptr(dsa = DSA_new())
115*b0d17251Schristos || !TEST_ptr(p = BN_dup(bn[pind]))
116*b0d17251Schristos || !TEST_ptr(q = BN_dup(bn[qind]))
117*b0d17251Schristos || !TEST_ptr(g = BN_dup(bn[gind]))
118*b0d17251Schristos || !TEST_true(DSA_set0_pqg(dsa, p, q, g)))
119*b0d17251Schristos goto err;
120*b0d17251Schristos p = q = g = NULL;
121*b0d17251Schristos
122*b0d17251Schristos if (!TEST_true(EVP_PKEY_assign_DSA(pkey_parm, dsa)))
123*b0d17251Schristos goto err;
124*b0d17251Schristos dsa = NULL;
125*b0d17251Schristos
126*b0d17251Schristos if (!TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey_parm, NULL))
127*b0d17251Schristos || !TEST_int_gt(EVP_PKEY_keygen_init(gen_ctx), 0)
128*b0d17251Schristos || !TEST_int_eq(EVP_PKEY_keygen(gen_ctx, &pkey), expected))
129*b0d17251Schristos goto err;
130*b0d17251Schristos
131*b0d17251Schristos if (expected) {
132*b0d17251Schristos if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey))
133*b0d17251Schristos || !TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), 1))
134*b0d17251Schristos goto err;
135*b0d17251Schristos }
136*b0d17251Schristos
137*b0d17251Schristos ret = 1;
138*b0d17251Schristos err:
139*b0d17251Schristos EVP_PKEY_free(pkey);
140*b0d17251Schristos EVP_PKEY_free(dup_pk);
141*b0d17251Schristos EVP_PKEY_CTX_free(gen_ctx);
142*b0d17251Schristos EVP_PKEY_free(pkey_parm);
143*b0d17251Schristos DSA_free(dsa);
144*b0d17251Schristos BN_free(g);
145*b0d17251Schristos BN_free(q);
146*b0d17251Schristos BN_free(p);
147*b0d17251Schristos return ret;
148*b0d17251Schristos }
149*b0d17251Schristos #endif /* OPENSSL_NO_DSA */
150*b0d17251Schristos
151*b0d17251Schristos #ifndef OPENSSL_NO_DH
do_dh_param_keygen(int tstid,const BIGNUM ** bn)152*b0d17251Schristos static int do_dh_param_keygen(int tstid, const BIGNUM **bn)
153*b0d17251Schristos {
154*b0d17251Schristos int ret = 0;
155*b0d17251Schristos int expected;
156*b0d17251Schristos EVP_PKEY_CTX *gen_ctx = NULL;
157*b0d17251Schristos EVP_PKEY *pkey_parm = NULL;
158*b0d17251Schristos EVP_PKEY *pkey = NULL, *dup_pk = NULL;
159*b0d17251Schristos DH *dh = NULL;
160*b0d17251Schristos int pind, qind, gind;
161*b0d17251Schristos BIGNUM *p = NULL, *q = NULL, *g = NULL;
162*b0d17251Schristos
163*b0d17251Schristos /*
164*b0d17251Schristos * These tests are using bad values for p, q, g by reusing the values.
165*b0d17251Schristos * A value of 0 uses p, 1 uses q and 2 uses g.
166*b0d17251Schristos * There are 27 different combinations, with only the 1 valid combination.
167*b0d17251Schristos */
168*b0d17251Schristos pind = tstid / 9;
169*b0d17251Schristos qind = (tstid / 3) % 3;
170*b0d17251Schristos gind = tstid % 3;
171*b0d17251Schristos expected = (pind == 0 && qind == 1 && gind == 2);
172*b0d17251Schristos
173*b0d17251Schristos TEST_note("Testing with (p, q, g) = (%s, %s, %s)", getname(pind),
174*b0d17251Schristos getname(qind), getname(gind));
175*b0d17251Schristos
176*b0d17251Schristos if (!TEST_ptr(pkey_parm = EVP_PKEY_new())
177*b0d17251Schristos || !TEST_ptr(dh = DH_new())
178*b0d17251Schristos || !TEST_ptr(p = BN_dup(bn[pind]))
179*b0d17251Schristos || !TEST_ptr(q = BN_dup(bn[qind]))
180*b0d17251Schristos || !TEST_ptr(g = BN_dup(bn[gind]))
181*b0d17251Schristos || !TEST_true(DH_set0_pqg(dh, p, q, g)))
182*b0d17251Schristos goto err;
183*b0d17251Schristos p = q = g = NULL;
184*b0d17251Schristos
185*b0d17251Schristos if (!TEST_true(EVP_PKEY_assign_DH(pkey_parm, dh)))
186*b0d17251Schristos goto err;
187*b0d17251Schristos dh = NULL;
188*b0d17251Schristos
189*b0d17251Schristos if (!TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey_parm, NULL))
190*b0d17251Schristos || !TEST_int_gt(EVP_PKEY_keygen_init(gen_ctx), 0)
191*b0d17251Schristos || !TEST_int_eq(EVP_PKEY_keygen(gen_ctx, &pkey), expected))
192*b0d17251Schristos goto err;
193*b0d17251Schristos
194*b0d17251Schristos if (expected) {
195*b0d17251Schristos if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey))
196*b0d17251Schristos || !TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), 1))
197*b0d17251Schristos goto err;
198*b0d17251Schristos }
199*b0d17251Schristos
200*b0d17251Schristos ret = 1;
201*b0d17251Schristos err:
202*b0d17251Schristos EVP_PKEY_free(pkey);
203*b0d17251Schristos EVP_PKEY_free(dup_pk);
204*b0d17251Schristos EVP_PKEY_CTX_free(gen_ctx);
205*b0d17251Schristos EVP_PKEY_free(pkey_parm);
206*b0d17251Schristos DH_free(dh);
207*b0d17251Schristos BN_free(g);
208*b0d17251Schristos BN_free(q);
209*b0d17251Schristos BN_free(p);
210*b0d17251Schristos return ret;
211*b0d17251Schristos }
212*b0d17251Schristos
213*b0d17251Schristos /*
214*b0d17251Schristos * Note that we get the fips186-4 path being run for most of these cases since
215*b0d17251Schristos * the internal code will detect that the p, q, g does not match a safe prime
216*b0d17251Schristos * group (Except for when tstid = 5, which sets the correct p, q, g)
217*b0d17251Schristos */
test_dh_safeprime_param_keygen(int tstid)218*b0d17251Schristos static int test_dh_safeprime_param_keygen(int tstid)
219*b0d17251Schristos {
220*b0d17251Schristos static const BIGNUM *bn[] = {
221*b0d17251Schristos &ossl_bignum_ffdhe2048_p, &ossl_bignum_ffdhe2048_q,
222*b0d17251Schristos &ossl_bignum_const_2
223*b0d17251Schristos };
224*b0d17251Schristos return do_dh_param_keygen(tstid, bn);
225*b0d17251Schristos }
226*b0d17251Schristos
dhx_cert_load(void)227*b0d17251Schristos static int dhx_cert_load(void)
228*b0d17251Schristos {
229*b0d17251Schristos int ret = 0;
230*b0d17251Schristos X509 *cert = NULL;
231*b0d17251Schristos BIO *bio = NULL;
232*b0d17251Schristos
233*b0d17251Schristos static const unsigned char dhx_cert[] = {
234*b0d17251Schristos 0x30,0x82,0x03,0xff,0x30,0x82,0x02,0xe7,0xa0,0x03,0x02,0x01,0x02,0x02,0x09,0x00,
235*b0d17251Schristos 0xdb,0xf5,0x4d,0x22,0xa0,0x7a,0x67,0xa6,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,
236*b0d17251Schristos 0xf7,0x0d,0x01,0x01,0x05,0x05,0x00,0x30,0x44,0x31,0x0b,0x30,0x09,0x06,0x03,0x55,
237*b0d17251Schristos 0x04,0x06,0x13,0x02,0x55,0x4b,0x31,0x16,0x30,0x14,0x06,0x03,0x55,0x04,0x0a,0x0c,
238*b0d17251Schristos 0x0d,0x4f,0x70,0x65,0x6e,0x53,0x53,0x4c,0x20,0x47,0x72,0x6f,0x75,0x70,0x31,0x1d,
239*b0d17251Schristos 0x30,0x1b,0x06,0x03,0x55,0x04,0x03,0x0c,0x14,0x54,0x65,0x73,0x74,0x20,0x53,0x2f,
240*b0d17251Schristos 0x4d,0x49,0x4d,0x45,0x20,0x52,0x53,0x41,0x20,0x52,0x6f,0x6f,0x74,0x30,0x1e,0x17,
241*b0d17251Schristos 0x0d,0x31,0x33,0x30,0x38,0x30,0x32,0x31,0x34,0x34,0x39,0x32,0x39,0x5a,0x17,0x0d,
242*b0d17251Schristos 0x32,0x33,0x30,0x36,0x31,0x31,0x31,0x34,0x34,0x39,0x32,0x39,0x5a,0x30,0x44,0x31,
243*b0d17251Schristos 0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x55,0x4b,0x31,0x16,0x30,0x14,
244*b0d17251Schristos 0x06,0x03,0x55,0x04,0x0a,0x0c,0x0d,0x4f,0x70,0x65,0x6e,0x53,0x53,0x4c,0x20,0x47,
245*b0d17251Schristos 0x72,0x6f,0x75,0x70,0x31,0x1d,0x30,0x1b,0x06,0x03,0x55,0x04,0x03,0x0c,0x14,0x54,
246*b0d17251Schristos 0x65,0x73,0x74,0x20,0x53,0x2f,0x4d,0x49,0x4d,0x45,0x20,0x45,0x45,0x20,0x44,0x48,
247*b0d17251Schristos 0x20,0x23,0x31,0x30,0x82,0x01,0xb6,0x30,0x82,0x01,0x2b,0x06,0x07,0x2a,0x86,0x48,
248*b0d17251Schristos 0xce,0x3e,0x02,0x01,0x30,0x82,0x01,0x1e,0x02,0x81,0x81,0x00,0xd4,0x0c,0x4a,0x0c,
249*b0d17251Schristos 0x04,0x72,0x71,0x19,0xdf,0x59,0x19,0xc5,0xaf,0x44,0x7f,0xca,0x8e,0x2b,0xf0,0x09,
250*b0d17251Schristos 0xf5,0xd3,0x25,0xb1,0x73,0x16,0x55,0x89,0xdf,0xfd,0x07,0xaf,0x19,0xd3,0x7f,0xd0,
251*b0d17251Schristos 0x07,0xa2,0xfe,0x3f,0x5a,0xf1,0x01,0xc6,0xf8,0x2b,0xef,0x4e,0x6d,0x03,0x38,0x42,
252*b0d17251Schristos 0xa1,0x37,0xd4,0x14,0xb4,0x00,0x4a,0xb1,0x86,0x5a,0x83,0xce,0xb9,0x08,0x0e,0xc1,
253*b0d17251Schristos 0x99,0x27,0x47,0x8d,0x0b,0x85,0xa8,0x82,0xed,0xcc,0x0d,0xb9,0xb0,0x32,0x7e,0xdf,
254*b0d17251Schristos 0xe8,0xe4,0xf6,0xf6,0xec,0xb3,0xee,0x7a,0x11,0x34,0x65,0x97,0xfc,0x1a,0xb0,0x95,
255*b0d17251Schristos 0x4b,0x19,0xb9,0xa6,0x1c,0xd9,0x01,0x32,0xf7,0x35,0x7c,0x2d,0x5d,0xfe,0xc1,0x85,
256*b0d17251Schristos 0x70,0x49,0xf8,0xcc,0x99,0xd0,0xbe,0xf1,0x5a,0x78,0xc8,0x03,0x02,0x81,0x80,0x69,
257*b0d17251Schristos 0x00,0xfd,0x66,0xf2,0xfc,0x15,0x8b,0x09,0xb8,0xdc,0x4d,0xea,0xaa,0x79,0x55,0xf9,
258*b0d17251Schristos 0xdf,0x46,0xa6,0x2f,0xca,0x2d,0x8f,0x59,0x2a,0xad,0x44,0xa3,0xc6,0x18,0x2f,0x95,
259*b0d17251Schristos 0xb6,0x16,0x20,0xe3,0xd3,0xd1,0x8f,0x03,0xce,0x71,0x7c,0xef,0x3a,0xc7,0x44,0x39,
260*b0d17251Schristos 0x0e,0xe2,0x1f,0xd8,0xd3,0x89,0x2b,0xe7,0x51,0xdc,0x12,0x48,0x4c,0x18,0x4d,0x99,
261*b0d17251Schristos 0x12,0x06,0xe4,0x17,0x02,0x03,0x8c,0x24,0x05,0x8e,0xa6,0x85,0xf2,0x69,0x1b,0xe1,
262*b0d17251Schristos 0x6a,0xdc,0xe2,0x04,0x3a,0x01,0x9d,0x64,0xbe,0xfe,0x45,0xf9,0x44,0x18,0x71,0xbd,
263*b0d17251Schristos 0x2d,0x3e,0x7a,0x6f,0x72,0x7d,0x1a,0x80,0x42,0x57,0xae,0x18,0x6f,0x91,0xd6,0x61,
264*b0d17251Schristos 0x03,0x8a,0x1c,0x89,0x73,0xc7,0x56,0x41,0x03,0xd3,0xf8,0xed,0x65,0xe2,0x85,0x02,
265*b0d17251Schristos 0x15,0x00,0x89,0x94,0xab,0x10,0x67,0x45,0x41,0xad,0x63,0xc6,0x71,0x40,0x8d,0x6b,
266*b0d17251Schristos 0x9e,0x19,0x5b,0xa4,0xc7,0xf5,0x03,0x81,0x84,0x00,0x02,0x81,0x80,0x2f,0x5b,0xde,
267*b0d17251Schristos 0x72,0x02,0x36,0x6b,0x00,0x5e,0x24,0x7f,0x14,0x2c,0x18,0x52,0x42,0x97,0x4b,0xdb,
268*b0d17251Schristos 0x6e,0x15,0x50,0x3c,0x45,0x3e,0x25,0xf3,0xb7,0xc5,0x6e,0xe5,0x52,0xe7,0xc4,0xfb,
269*b0d17251Schristos 0xf4,0xa5,0xf0,0x39,0x12,0x7f,0xbc,0x54,0x1c,0x93,0xb9,0x5e,0xee,0xe9,0x14,0xb0,
270*b0d17251Schristos 0xdf,0xfe,0xfc,0x36,0xe4,0xf2,0xaf,0xfb,0x13,0xc8,0xdf,0x18,0x94,0x1d,0x40,0xb9,
271*b0d17251Schristos 0x71,0xdd,0x4c,0x9c,0xa7,0x03,0x52,0x02,0xb5,0xed,0x71,0x80,0x3e,0x23,0xda,0x28,
272*b0d17251Schristos 0xe5,0xab,0xe7,0x6f,0xf2,0x0a,0x0e,0x00,0x5b,0x7d,0xc6,0x4b,0xd7,0xc7,0xb2,0xc3,
273*b0d17251Schristos 0xba,0x62,0x7f,0x70,0x28,0xa0,0x9d,0x71,0x13,0x70,0xd1,0x9f,0x32,0x2f,0x3e,0xd2,
274*b0d17251Schristos 0xcd,0x1b,0xa4,0xc6,0x72,0xa0,0x74,0x5d,0x71,0xef,0x03,0x43,0x6e,0xa3,0x60,0x30,
275*b0d17251Schristos 0x5e,0x30,0x0c,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x02,0x30,0x00,0x30,
276*b0d17251Schristos 0x0e,0x06,0x03,0x55,0x1d,0x0f,0x01,0x01,0xff,0x04,0x04,0x03,0x02,0x05,0xe0,0x30,
277*b0d17251Schristos 0x1d,0x06,0x03,0x55,0x1d,0x0e,0x04,0x16,0x04,0x14,0x0b,0x5a,0x4d,0x5f,0x7d,0x25,
278*b0d17251Schristos 0xc7,0xf2,0x9d,0xc1,0xaa,0xb7,0x63,0x82,0x2f,0xfa,0x8f,0x32,0xe7,0xc0,0x30,0x1f,
279*b0d17251Schristos 0x06,0x03,0x55,0x1d,0x23,0x04,0x18,0x30,0x16,0x80,0x14,0xdf,0x7e,0x5e,0x88,0x05,
280*b0d17251Schristos 0x24,0x33,0x08,0xdd,0x22,0x81,0x02,0x97,0xcc,0x9a,0xb7,0xb1,0x33,0x27,0x30,0x30,
281*b0d17251Schristos 0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x05,0x05,0x00,0x03,0x82,
282*b0d17251Schristos 0x01,0x01,0x00,0x5a,0xf2,0x63,0xef,0xd3,0x16,0xd7,0xf5,0xaa,0xdd,0x12,0x00,0x36,
283*b0d17251Schristos 0x00,0x21,0xa2,0x7b,0x08,0xd6,0x3b,0x9f,0x62,0xac,0x53,0x1f,0xed,0x4c,0xd1,0x15,
284*b0d17251Schristos 0x34,0x65,0x71,0xee,0x96,0x07,0xa6,0xef,0xb2,0xde,0xd8,0xbb,0x35,0x6e,0x2c,0xe2,
285*b0d17251Schristos 0xd1,0x26,0xef,0x7e,0x94,0xe2,0x88,0x51,0xa4,0x6c,0xaa,0x27,0x2a,0xd3,0xb6,0xc2,
286*b0d17251Schristos 0xf7,0xea,0xc3,0x0b,0xa9,0xb5,0x28,0x37,0xa2,0x63,0x08,0xe4,0x88,0xc0,0x1b,0x16,
287*b0d17251Schristos 0x1b,0xca,0xfd,0x8a,0x07,0x32,0x29,0xa7,0x53,0xb5,0x2d,0x30,0xe4,0xf5,0x16,0xc3,
288*b0d17251Schristos 0xe3,0xc2,0x4c,0x30,0x5d,0x35,0x80,0x1c,0xa2,0xdb,0xe3,0x4b,0x51,0x0d,0x4c,0x60,
289*b0d17251Schristos 0x5f,0xb9,0x46,0xac,0xa8,0x46,0xa7,0x32,0xa7,0x9c,0x76,0xf8,0xe9,0xb5,0x19,0xe2,
290*b0d17251Schristos 0x0c,0xe1,0x0f,0xc6,0x46,0xe2,0x38,0xa7,0x87,0x72,0x6d,0x6c,0xbc,0x88,0x2f,0x9d,
291*b0d17251Schristos 0x2d,0xe5,0xd0,0x7d,0x1e,0xc7,0x5d,0xf8,0x7e,0xb4,0x0b,0xa6,0xf9,0x6c,0xe3,0x7c,
292*b0d17251Schristos 0xb2,0x70,0x6e,0x75,0x9b,0x1e,0x63,0xe1,0x4d,0xb2,0x81,0xd3,0x55,0x38,0x94,0x1a,
293*b0d17251Schristos 0x7a,0xfa,0xbf,0x01,0x18,0x70,0x2d,0x35,0xd3,0xe3,0x10,0x7a,0x9a,0xa7,0x8f,0xf3,
294*b0d17251Schristos 0xbd,0x56,0x55,0x5e,0xd8,0xbd,0x4e,0x16,0x76,0xd0,0x48,0x4c,0xf9,0x51,0x54,0xdf,
295*b0d17251Schristos 0x2d,0xb0,0xc9,0xaa,0x5e,0x42,0x38,0x50,0xbf,0x0f,0xc0,0xd9,0x84,0x44,0x4b,0x42,
296*b0d17251Schristos 0x24,0xec,0x14,0xa3,0xde,0x11,0xdf,0x58,0x7f,0xc2,0x4d,0xb2,0xd5,0x42,0x78,0x6e,
297*b0d17251Schristos 0x52,0x3e,0xad,0xc3,0x5f,0x04,0xc4,0xe6,0x31,0xaa,0x81,0x06,0x8b,0x13,0x4b,0x3c,
298*b0d17251Schristos 0x0e,0x6a,0xb1
299*b0d17251Schristos };
300*b0d17251Schristos
301*b0d17251Schristos if (!TEST_ptr(bio = BIO_new_mem_buf(dhx_cert, sizeof(dhx_cert)))
302*b0d17251Schristos || !TEST_ptr(cert = X509_new_ex(libctx, NULL))
303*b0d17251Schristos || !TEST_ptr(d2i_X509_bio(bio, &cert)))
304*b0d17251Schristos goto err;
305*b0d17251Schristos ret = 1;
306*b0d17251Schristos err:
307*b0d17251Schristos X509_free(cert);
308*b0d17251Schristos BIO_free(bio);
309*b0d17251Schristos return ret;
310*b0d17251Schristos }
311*b0d17251Schristos
312*b0d17251Schristos #endif /* OPENSSL_NO_DH */
313*b0d17251Schristos
test_cipher_reinit(int test_id)314*b0d17251Schristos static int test_cipher_reinit(int test_id)
315*b0d17251Schristos {
316*b0d17251Schristos int ret = 0, diff, ccm, siv, no_null_key;
317*b0d17251Schristos int out1_len = 0, out2_len = 0, out3_len = 0;
318*b0d17251Schristos EVP_CIPHER *cipher = NULL;
319*b0d17251Schristos EVP_CIPHER_CTX *ctx = NULL;
320*b0d17251Schristos unsigned char out1[256];
321*b0d17251Schristos unsigned char out2[256];
322*b0d17251Schristos unsigned char out3[256];
323*b0d17251Schristos unsigned char in[16] = {
324*b0d17251Schristos 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
325*b0d17251Schristos 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10
326*b0d17251Schristos };
327*b0d17251Schristos unsigned char key[64] = {
328*b0d17251Schristos 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
329*b0d17251Schristos 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
330*b0d17251Schristos 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
331*b0d17251Schristos 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
332*b0d17251Schristos 0x02, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
333*b0d17251Schristos 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
334*b0d17251Schristos 0x03, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
335*b0d17251Schristos 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
336*b0d17251Schristos };
337*b0d17251Schristos unsigned char iv[16] = {
338*b0d17251Schristos 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
339*b0d17251Schristos 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00
340*b0d17251Schristos };
341*b0d17251Schristos const char *name = sk_OPENSSL_STRING_value(cipher_names, test_id);
342*b0d17251Schristos
343*b0d17251Schristos if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
344*b0d17251Schristos goto err;
345*b0d17251Schristos
346*b0d17251Schristos TEST_note("Fetching %s\n", name);
347*b0d17251Schristos if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, name, NULL)))
348*b0d17251Schristos goto err;
349*b0d17251Schristos
350*b0d17251Schristos /* ccm fails on the second update - this matches OpenSSL 1_1_1 behaviour */
351*b0d17251Schristos ccm = (EVP_CIPHER_get_mode(cipher) == EVP_CIPH_CCM_MODE);
352*b0d17251Schristos
353*b0d17251Schristos /* siv cannot be called with NULL key as the iv is irrelevant */
354*b0d17251Schristos siv = (EVP_CIPHER_get_mode(cipher) == EVP_CIPH_SIV_MODE);
355*b0d17251Schristos
356*b0d17251Schristos /*
357*b0d17251Schristos * Skip init call with a null key for RC4 as the stream cipher does not
358*b0d17251Schristos * handle reinit (1.1.1 behaviour).
359*b0d17251Schristos */
360*b0d17251Schristos no_null_key = EVP_CIPHER_is_a(cipher, "RC4")
361*b0d17251Schristos || EVP_CIPHER_is_a(cipher, "RC4-40")
362*b0d17251Schristos || EVP_CIPHER_is_a(cipher, "RC4-HMAC-MD5");
363*b0d17251Schristos
364*b0d17251Schristos /* DES3-WRAP uses random every update - so it will give a different value */
365*b0d17251Schristos diff = EVP_CIPHER_is_a(cipher, "DES3-WRAP");
366*b0d17251Schristos
367*b0d17251Schristos if (!TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv))
368*b0d17251Schristos || !TEST_true(EVP_EncryptUpdate(ctx, out1, &out1_len, in, sizeof(in)))
369*b0d17251Schristos || !TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
370*b0d17251Schristos || !TEST_int_eq(EVP_EncryptUpdate(ctx, out2, &out2_len, in, sizeof(in)),
371*b0d17251Schristos ccm ? 0 : 1)
372*b0d17251Schristos || (!no_null_key
373*b0d17251Schristos && (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv))
374*b0d17251Schristos || !TEST_int_eq(EVP_EncryptUpdate(ctx, out3, &out3_len, in, sizeof(in)),
375*b0d17251Schristos ccm || siv ? 0 : 1))))
376*b0d17251Schristos goto err;
377*b0d17251Schristos
378*b0d17251Schristos if (ccm == 0) {
379*b0d17251Schristos if (diff) {
380*b0d17251Schristos if (!TEST_mem_ne(out1, out1_len, out2, out2_len)
381*b0d17251Schristos || !TEST_mem_ne(out1, out1_len, out3, out3_len)
382*b0d17251Schristos || !TEST_mem_ne(out2, out2_len, out3, out3_len))
383*b0d17251Schristos goto err;
384*b0d17251Schristos } else {
385*b0d17251Schristos if (!TEST_mem_eq(out1, out1_len, out2, out2_len)
386*b0d17251Schristos || (!siv && !no_null_key && !TEST_mem_eq(out1, out1_len, out3, out3_len)))
387*b0d17251Schristos goto err;
388*b0d17251Schristos }
389*b0d17251Schristos }
390*b0d17251Schristos ret = 1;
391*b0d17251Schristos err:
392*b0d17251Schristos EVP_CIPHER_free(cipher);
393*b0d17251Schristos EVP_CIPHER_CTX_free(ctx);
394*b0d17251Schristos return ret;
395*b0d17251Schristos }
396*b0d17251Schristos
397*b0d17251Schristos /*
398*b0d17251Schristos * This test only uses a partial block (half the block size) of input for each
399*b0d17251Schristos * EVP_EncryptUpdate() in order to test that the second init/update is not using
400*b0d17251Schristos * a leftover buffer from the first init/update.
401*b0d17251Schristos * Note: some ciphers don't need a full block to produce output.
402*b0d17251Schristos */
test_cipher_reinit_partialupdate(int test_id)403*b0d17251Schristos static int test_cipher_reinit_partialupdate(int test_id)
404*b0d17251Schristos {
405*b0d17251Schristos int ret = 0, in_len;
406*b0d17251Schristos int out1_len = 0, out2_len = 0, out3_len = 0;
407*b0d17251Schristos EVP_CIPHER *cipher = NULL;
408*b0d17251Schristos EVP_CIPHER_CTX *ctx = NULL;
409*b0d17251Schristos unsigned char out1[256];
410*b0d17251Schristos unsigned char out2[256];
411*b0d17251Schristos unsigned char out3[256];
412*b0d17251Schristos static const unsigned char in[32] = {
413*b0d17251Schristos 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
414*b0d17251Schristos 0xba, 0xbe, 0xba, 0xbe, 0x00, 0x00, 0xba, 0xbe,
415*b0d17251Schristos 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
416*b0d17251Schristos 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
417*b0d17251Schristos };
418*b0d17251Schristos static const unsigned char key[64] = {
419*b0d17251Schristos 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
420*b0d17251Schristos 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
421*b0d17251Schristos 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
422*b0d17251Schristos 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
423*b0d17251Schristos 0x02, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
424*b0d17251Schristos 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
425*b0d17251Schristos 0x03, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
426*b0d17251Schristos 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
427*b0d17251Schristos };
428*b0d17251Schristos static const unsigned char iv[16] = {
429*b0d17251Schristos 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
430*b0d17251Schristos 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00
431*b0d17251Schristos };
432*b0d17251Schristos const char *name = sk_OPENSSL_STRING_value(cipher_names, test_id);
433*b0d17251Schristos
434*b0d17251Schristos if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
435*b0d17251Schristos goto err;
436*b0d17251Schristos
437*b0d17251Schristos TEST_note("Fetching %s\n", name);
438*b0d17251Schristos if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, name, NULL)))
439*b0d17251Schristos goto err;
440*b0d17251Schristos
441*b0d17251Schristos in_len = EVP_CIPHER_get_block_size(cipher) / 2;
442*b0d17251Schristos
443*b0d17251Schristos /* skip any ciphers that don't allow partial updates */
444*b0d17251Schristos if (((EVP_CIPHER_get_flags(cipher)
445*b0d17251Schristos & (EVP_CIPH_FLAG_CTS | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) != 0)
446*b0d17251Schristos || EVP_CIPHER_get_mode(cipher) == EVP_CIPH_CCM_MODE
447*b0d17251Schristos || EVP_CIPHER_get_mode(cipher) == EVP_CIPH_XTS_MODE
448*b0d17251Schristos || EVP_CIPHER_get_mode(cipher) == EVP_CIPH_WRAP_MODE) {
449*b0d17251Schristos ret = 1;
450*b0d17251Schristos goto err;
451*b0d17251Schristos }
452*b0d17251Schristos
453*b0d17251Schristos if (!TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv))
454*b0d17251Schristos || !TEST_true(EVP_EncryptUpdate(ctx, out1, &out1_len, in, in_len))
455*b0d17251Schristos || !TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
456*b0d17251Schristos || !TEST_true(EVP_EncryptUpdate(ctx, out2, &out2_len, in, in_len)))
457*b0d17251Schristos goto err;
458*b0d17251Schristos
459*b0d17251Schristos if (!TEST_mem_eq(out1, out1_len, out2, out2_len))
460*b0d17251Schristos goto err;
461*b0d17251Schristos
462*b0d17251Schristos if (EVP_CIPHER_get_mode(cipher) != EVP_CIPH_SIV_MODE) {
463*b0d17251Schristos if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv))
464*b0d17251Schristos || !TEST_true(EVP_EncryptUpdate(ctx, out3, &out3_len, in, in_len)))
465*b0d17251Schristos goto err;
466*b0d17251Schristos
467*b0d17251Schristos if (!TEST_mem_eq(out1, out1_len, out3, out3_len))
468*b0d17251Schristos goto err;
469*b0d17251Schristos }
470*b0d17251Schristos ret = 1;
471*b0d17251Schristos err:
472*b0d17251Schristos EVP_CIPHER_free(cipher);
473*b0d17251Schristos EVP_CIPHER_CTX_free(ctx);
474*b0d17251Schristos return ret;
475*b0d17251Schristos }
476*b0d17251Schristos
477*b0d17251Schristos
name_cmp(const char * const * a,const char * const * b)478*b0d17251Schristos static int name_cmp(const char * const *a, const char * const *b)
479*b0d17251Schristos {
480*b0d17251Schristos return OPENSSL_strcasecmp(*a, *b);
481*b0d17251Schristos }
482*b0d17251Schristos
collect_cipher_names(EVP_CIPHER * cipher,void * cipher_names_list)483*b0d17251Schristos static void collect_cipher_names(EVP_CIPHER *cipher, void *cipher_names_list)
484*b0d17251Schristos {
485*b0d17251Schristos STACK_OF(OPENSSL_STRING) *names = cipher_names_list;
486*b0d17251Schristos const char *name = EVP_CIPHER_get0_name(cipher);
487*b0d17251Schristos char *namedup = NULL;
488*b0d17251Schristos
489*b0d17251Schristos assert(name != NULL);
490*b0d17251Schristos /* the cipher will be freed after returning, strdup is needed */
491*b0d17251Schristos if ((namedup = OPENSSL_strdup(name)) != NULL
492*b0d17251Schristos && !sk_OPENSSL_STRING_push(names, namedup))
493*b0d17251Schristos OPENSSL_free(namedup);
494*b0d17251Schristos }
495*b0d17251Schristos
rsa_keygen(int bits,EVP_PKEY ** pub,EVP_PKEY ** priv)496*b0d17251Schristos static int rsa_keygen(int bits, EVP_PKEY **pub, EVP_PKEY **priv)
497*b0d17251Schristos {
498*b0d17251Schristos int ret = 0;
499*b0d17251Schristos unsigned char *pub_der = NULL;
500*b0d17251Schristos const unsigned char *pp = NULL;
501*b0d17251Schristos size_t len = 0;
502*b0d17251Schristos OSSL_ENCODER_CTX *ectx = NULL;
503*b0d17251Schristos
504*b0d17251Schristos if (!TEST_ptr(*priv = EVP_PKEY_Q_keygen(libctx, NULL, "RSA", bits))
505*b0d17251Schristos || !TEST_ptr(ectx =
506*b0d17251Schristos OSSL_ENCODER_CTX_new_for_pkey(*priv,
507*b0d17251Schristos EVP_PKEY_PUBLIC_KEY,
508*b0d17251Schristos "DER", "type-specific",
509*b0d17251Schristos NULL))
510*b0d17251Schristos || !TEST_true(OSSL_ENCODER_to_data(ectx, &pub_der, &len)))
511*b0d17251Schristos goto err;
512*b0d17251Schristos pp = pub_der;
513*b0d17251Schristos if (!TEST_ptr(d2i_PublicKey(EVP_PKEY_RSA, pub, &pp, len)))
514*b0d17251Schristos goto err;
515*b0d17251Schristos ret = 1;
516*b0d17251Schristos err:
517*b0d17251Schristos OSSL_ENCODER_CTX_free(ectx);
518*b0d17251Schristos OPENSSL_free(pub_der);
519*b0d17251Schristos return ret;
520*b0d17251Schristos }
521*b0d17251Schristos
kem_rsa_gen_recover(void)522*b0d17251Schristos static int kem_rsa_gen_recover(void)
523*b0d17251Schristos {
524*b0d17251Schristos int ret = 0;
525*b0d17251Schristos EVP_PKEY *pub = NULL;
526*b0d17251Schristos EVP_PKEY *priv = NULL;
527*b0d17251Schristos EVP_PKEY_CTX *sctx = NULL, *rctx = NULL, *dctx = NULL;
528*b0d17251Schristos unsigned char secret[256] = { 0, };
529*b0d17251Schristos unsigned char ct[256] = { 0, };
530*b0d17251Schristos unsigned char unwrap[256] = { 0, };
531*b0d17251Schristos size_t ctlen = 0, unwraplen = 0, secretlen = 0;
532*b0d17251Schristos int bits = 2048;
533*b0d17251Schristos
534*b0d17251Schristos ret = TEST_true(rsa_keygen(bits, &pub, &priv))
535*b0d17251Schristos && TEST_ptr(sctx = EVP_PKEY_CTX_new_from_pkey(libctx, pub, NULL))
536*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate_init(sctx, NULL), 1)
537*b0d17251Schristos && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(sctx, "RSASVE"), 1)
538*b0d17251Schristos && TEST_ptr(dctx = EVP_PKEY_CTX_dup(sctx))
539*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate(dctx, NULL, &ctlen, NULL,
540*b0d17251Schristos &secretlen), 1)
541*b0d17251Schristos && TEST_int_eq(ctlen, secretlen)
542*b0d17251Schristos && TEST_int_eq(ctlen, bits / 8)
543*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate(dctx, ct, &ctlen, secret,
544*b0d17251Schristos &secretlen), 1)
545*b0d17251Schristos && TEST_ptr(rctx = EVP_PKEY_CTX_new_from_pkey(libctx, priv, NULL))
546*b0d17251Schristos && TEST_int_eq(EVP_PKEY_decapsulate_init(rctx, NULL), 1)
547*b0d17251Schristos && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(rctx, "RSASVE"), 1)
548*b0d17251Schristos && TEST_int_eq(EVP_PKEY_decapsulate(rctx, NULL, &unwraplen,
549*b0d17251Schristos ct, ctlen), 1)
550*b0d17251Schristos && TEST_int_eq(EVP_PKEY_decapsulate(rctx, unwrap, &unwraplen,
551*b0d17251Schristos ct, ctlen), 1)
552*b0d17251Schristos && TEST_mem_eq(unwrap, unwraplen, secret, secretlen);
553*b0d17251Schristos EVP_PKEY_free(pub);
554*b0d17251Schristos EVP_PKEY_free(priv);
555*b0d17251Schristos EVP_PKEY_CTX_free(rctx);
556*b0d17251Schristos EVP_PKEY_CTX_free(dctx);
557*b0d17251Schristos EVP_PKEY_CTX_free(sctx);
558*b0d17251Schristos return ret;
559*b0d17251Schristos }
560*b0d17251Schristos
561*b0d17251Schristos #ifndef OPENSSL_NO_DES
562*b0d17251Schristos /*
563*b0d17251Schristos * This test makes sure that EVP_CIPHER_CTX_rand_key() works correctly
564*b0d17251Schristos * For fips mode this code would produce an error if the flag is not set.
565*b0d17251Schristos */
test_cipher_tdes_randkey(void)566*b0d17251Schristos static int test_cipher_tdes_randkey(void)
567*b0d17251Schristos {
568*b0d17251Schristos int ret;
569*b0d17251Schristos EVP_CIPHER_CTX *ctx = NULL;
570*b0d17251Schristos EVP_CIPHER *tdes_cipher = NULL, *aes_cipher = NULL;
571*b0d17251Schristos unsigned char key[24] = { 0 };
572*b0d17251Schristos
573*b0d17251Schristos ret = TEST_ptr(aes_cipher = EVP_CIPHER_fetch(libctx, "AES-256-CBC", NULL))
574*b0d17251Schristos && TEST_int_eq(EVP_CIPHER_get_flags(aes_cipher) & EVP_CIPH_RAND_KEY, 0)
575*b0d17251Schristos && TEST_ptr(tdes_cipher = EVP_CIPHER_fetch(libctx, "DES-EDE3-CBC", NULL))
576*b0d17251Schristos && TEST_int_ne(EVP_CIPHER_get_flags(tdes_cipher) & EVP_CIPH_RAND_KEY, 0)
577*b0d17251Schristos && TEST_ptr(ctx = EVP_CIPHER_CTX_new())
578*b0d17251Schristos && TEST_true(EVP_CipherInit_ex(ctx, tdes_cipher, NULL, NULL, NULL, 1))
579*b0d17251Schristos && TEST_int_gt(EVP_CIPHER_CTX_rand_key(ctx, key), 0);
580*b0d17251Schristos
581*b0d17251Schristos EVP_CIPHER_CTX_free(ctx);
582*b0d17251Schristos EVP_CIPHER_free(tdes_cipher);
583*b0d17251Schristos EVP_CIPHER_free(aes_cipher);
584*b0d17251Schristos return ret;
585*b0d17251Schristos }
586*b0d17251Schristos #endif /* OPENSSL_NO_DES */
587*b0d17251Schristos
kem_rsa_params(void)588*b0d17251Schristos static int kem_rsa_params(void)
589*b0d17251Schristos {
590*b0d17251Schristos int ret = 0;
591*b0d17251Schristos EVP_PKEY *pub = NULL;
592*b0d17251Schristos EVP_PKEY *priv = NULL;
593*b0d17251Schristos EVP_PKEY_CTX *pubctx = NULL, *privctx = NULL;
594*b0d17251Schristos unsigned char secret[256] = { 0, };
595*b0d17251Schristos unsigned char ct[256] = { 0, };
596*b0d17251Schristos size_t ctlen = 0, secretlen = 0;
597*b0d17251Schristos
598*b0d17251Schristos ret = TEST_true(rsa_keygen(2048, &pub, &priv))
599*b0d17251Schristos && TEST_ptr(pubctx = EVP_PKEY_CTX_new_from_pkey(libctx, pub, NULL))
600*b0d17251Schristos && TEST_ptr(privctx = EVP_PKEY_CTX_new_from_pkey(libctx, priv, NULL))
601*b0d17251Schristos /* Test setting kem op before the init fails */
602*b0d17251Schristos && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, "RSASVE"), -2)
603*b0d17251Schristos /* Test NULL ctx passed */
604*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate_init(NULL, NULL), 0)
605*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate(NULL, NULL, NULL, NULL, NULL), 0)
606*b0d17251Schristos && TEST_int_eq(EVP_PKEY_decapsulate_init(NULL, NULL), 0)
607*b0d17251Schristos && TEST_int_eq(EVP_PKEY_decapsulate(NULL, NULL, NULL, NULL, 0), 0)
608*b0d17251Schristos /* Test Invalid operation */
609*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, NULL, NULL, NULL), -1)
610*b0d17251Schristos && TEST_int_eq(EVP_PKEY_decapsulate(privctx, NULL, NULL, NULL, 0), 0)
611*b0d17251Schristos /* Wrong key component - no secret should be returned on failure */
612*b0d17251Schristos && TEST_int_eq(EVP_PKEY_decapsulate_init(pubctx, NULL), 1)
613*b0d17251Schristos && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, "RSASVE"), 1)
614*b0d17251Schristos && TEST_int_eq(EVP_PKEY_decapsulate(pubctx, secret, &secretlen, ct,
615*b0d17251Schristos sizeof(ct)), 0)
616*b0d17251Schristos && TEST_uchar_eq(secret[0], 0)
617*b0d17251Schristos /* Test encapsulate fails if the mode is not set */
618*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate_init(pubctx, NULL), 1)
619*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, &ctlen, secret, &secretlen), -2)
620*b0d17251Schristos /* Test setting a bad kem ops fail */
621*b0d17251Schristos && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, "RSA"), 0)
622*b0d17251Schristos && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, NULL), 0)
623*b0d17251Schristos && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(NULL, "RSASVE"), 0)
624*b0d17251Schristos && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(NULL, NULL), 0)
625*b0d17251Schristos /* Test secretlen is optional */
626*b0d17251Schristos && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, "RSASVE"), 1)
627*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, &ctlen, secret, NULL), 1)
628*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, &ctlen, NULL, NULL), 1)
629*b0d17251Schristos /* Test outlen is optional */
630*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, NULL, NULL, &secretlen), 1)
631*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, NULL, secret, &secretlen), 1)
632*b0d17251Schristos /* test that either len must be set if out is NULL */
633*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, NULL, NULL, NULL), 0)
634*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, &ctlen, NULL, NULL), 1)
635*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, NULL, NULL, &secretlen), 1)
636*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, &ctlen, NULL, &secretlen), 1)
637*b0d17251Schristos /* Secret buffer should be set if there is an output buffer */
638*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, &ctlen, NULL, NULL), 0)
639*b0d17251Schristos /* Test that lengths are optional if ct is not NULL */
640*b0d17251Schristos && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, NULL, secret, NULL), 1)
641*b0d17251Schristos /* Pass if secret or secret length are not NULL */
642*b0d17251Schristos && TEST_int_eq(EVP_PKEY_decapsulate_init(privctx, NULL), 1)
643*b0d17251Schristos && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(privctx, "RSASVE"), 1)
644*b0d17251Schristos && TEST_int_eq(EVP_PKEY_decapsulate(privctx, secret, NULL, ct, sizeof(ct)), 1)
645*b0d17251Schristos && TEST_int_eq(EVP_PKEY_decapsulate(privctx, NULL, &secretlen, ct, sizeof(ct)), 1)
646*b0d17251Schristos && TEST_int_eq(secretlen, 256)
647*b0d17251Schristos /* Fail if passed NULL arguments */
648*b0d17251Schristos && TEST_int_eq(EVP_PKEY_decapsulate(privctx, NULL, NULL, ct, sizeof(ct)), 0)
649*b0d17251Schristos && TEST_int_eq(EVP_PKEY_decapsulate(privctx, secret, &secretlen, NULL, 0), 0)
650*b0d17251Schristos && TEST_int_eq(EVP_PKEY_decapsulate(privctx, secret, &secretlen, NULL, sizeof(ct)), 0)
651*b0d17251Schristos && TEST_int_eq(EVP_PKEY_decapsulate(privctx, secret, &secretlen, ct, 0), 0);
652*b0d17251Schristos
653*b0d17251Schristos EVP_PKEY_free(pub);
654*b0d17251Schristos EVP_PKEY_free(priv);
655*b0d17251Schristos EVP_PKEY_CTX_free(pubctx);
656*b0d17251Schristos EVP_PKEY_CTX_free(privctx);
657*b0d17251Schristos return ret;
658*b0d17251Schristos }
659*b0d17251Schristos
660*b0d17251Schristos #ifndef OPENSSL_NO_DH
gen_dh_key(void)661*b0d17251Schristos static EVP_PKEY *gen_dh_key(void)
662*b0d17251Schristos {
663*b0d17251Schristos EVP_PKEY_CTX *gctx = NULL;
664*b0d17251Schristos EVP_PKEY *pkey = NULL;
665*b0d17251Schristos OSSL_PARAM params[2];
666*b0d17251Schristos
667*b0d17251Schristos params[0] = OSSL_PARAM_construct_utf8_string("group", "ffdhe2048", 0);
668*b0d17251Schristos params[1] = OSSL_PARAM_construct_end();
669*b0d17251Schristos
670*b0d17251Schristos if (!TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL))
671*b0d17251Schristos || !TEST_int_gt(EVP_PKEY_keygen_init(gctx), 0)
672*b0d17251Schristos || !TEST_true(EVP_PKEY_CTX_set_params(gctx, params))
673*b0d17251Schristos || !TEST_true(EVP_PKEY_keygen(gctx, &pkey)))
674*b0d17251Schristos goto err;
675*b0d17251Schristos err:
676*b0d17251Schristos EVP_PKEY_CTX_free(gctx);
677*b0d17251Schristos return pkey;
678*b0d17251Schristos }
679*b0d17251Schristos
680*b0d17251Schristos /* Fail if we try to use a dh key */
kem_invalid_keytype(void)681*b0d17251Schristos static int kem_invalid_keytype(void)
682*b0d17251Schristos {
683*b0d17251Schristos int ret = 0;
684*b0d17251Schristos EVP_PKEY *key = NULL;
685*b0d17251Schristos EVP_PKEY_CTX *sctx = NULL;
686*b0d17251Schristos
687*b0d17251Schristos if (!TEST_ptr(key = gen_dh_key()))
688*b0d17251Schristos goto done;
689*b0d17251Schristos
690*b0d17251Schristos if (!TEST_ptr(sctx = EVP_PKEY_CTX_new_from_pkey(libctx, key, NULL)))
691*b0d17251Schristos goto done;
692*b0d17251Schristos if (!TEST_int_eq(EVP_PKEY_encapsulate_init(sctx, NULL), -2))
693*b0d17251Schristos goto done;
694*b0d17251Schristos
695*b0d17251Schristos ret = 1;
696*b0d17251Schristos done:
697*b0d17251Schristos EVP_PKEY_free(key);
698*b0d17251Schristos EVP_PKEY_CTX_free(sctx);
699*b0d17251Schristos return ret;
700*b0d17251Schristos }
701*b0d17251Schristos #endif /* OPENSSL_NO_DH */
702*b0d17251Schristos
setup_tests(void)703*b0d17251Schristos int setup_tests(void)
704*b0d17251Schristos {
705*b0d17251Schristos const char *prov_name = "default";
706*b0d17251Schristos char *config_file = NULL;
707*b0d17251Schristos OPTION_CHOICE o;
708*b0d17251Schristos
709*b0d17251Schristos while ((o = opt_next()) != OPT_EOF) {
710*b0d17251Schristos switch (o) {
711*b0d17251Schristos case OPT_PROVIDER_NAME:
712*b0d17251Schristos prov_name = opt_arg();
713*b0d17251Schristos break;
714*b0d17251Schristos case OPT_CONFIG_FILE:
715*b0d17251Schristos config_file = opt_arg();
716*b0d17251Schristos break;
717*b0d17251Schristos case OPT_TEST_CASES:
718*b0d17251Schristos break;
719*b0d17251Schristos default:
720*b0d17251Schristos case OPT_ERR:
721*b0d17251Schristos return 0;
722*b0d17251Schristos }
723*b0d17251Schristos }
724*b0d17251Schristos
725*b0d17251Schristos if (!test_get_libctx(&libctx, &nullprov, config_file, &libprov, prov_name))
726*b0d17251Schristos return 0;
727*b0d17251Schristos
728*b0d17251Schristos #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DH)
729*b0d17251Schristos ADD_ALL_TESTS(test_dsa_param_keygen, 3 * 3 * 3);
730*b0d17251Schristos #endif
731*b0d17251Schristos #ifndef OPENSSL_NO_DH
732*b0d17251Schristos ADD_ALL_TESTS(test_dh_safeprime_param_keygen, 3 * 3 * 3);
733*b0d17251Schristos ADD_TEST(dhx_cert_load);
734*b0d17251Schristos #endif
735*b0d17251Schristos
736*b0d17251Schristos if (!TEST_ptr(cipher_names = sk_OPENSSL_STRING_new(name_cmp)))
737*b0d17251Schristos return 0;
738*b0d17251Schristos EVP_CIPHER_do_all_provided(libctx, collect_cipher_names, cipher_names);
739*b0d17251Schristos
740*b0d17251Schristos ADD_ALL_TESTS(test_cipher_reinit, sk_OPENSSL_STRING_num(cipher_names));
741*b0d17251Schristos ADD_ALL_TESTS(test_cipher_reinit_partialupdate,
742*b0d17251Schristos sk_OPENSSL_STRING_num(cipher_names));
743*b0d17251Schristos ADD_TEST(kem_rsa_gen_recover);
744*b0d17251Schristos ADD_TEST(kem_rsa_params);
745*b0d17251Schristos #ifndef OPENSSL_NO_DH
746*b0d17251Schristos ADD_TEST(kem_invalid_keytype);
747*b0d17251Schristos #endif
748*b0d17251Schristos #ifndef OPENSSL_NO_DES
749*b0d17251Schristos ADD_TEST(test_cipher_tdes_randkey);
750*b0d17251Schristos #endif
751*b0d17251Schristos return 1;
752*b0d17251Schristos }
753*b0d17251Schristos
754*b0d17251Schristos /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
string_free(char * m)755*b0d17251Schristos static void string_free(char *m)
756*b0d17251Schristos {
757*b0d17251Schristos OPENSSL_free(m);
758*b0d17251Schristos }
759*b0d17251Schristos
cleanup_tests(void)760*b0d17251Schristos void cleanup_tests(void)
761*b0d17251Schristos {
762*b0d17251Schristos sk_OPENSSL_STRING_pop_free(cipher_names, string_free);
763*b0d17251Schristos OSSL_PROVIDER_unload(libprov);
764*b0d17251Schristos OSSL_LIB_CTX_free(libctx);
765*b0d17251Schristos OSSL_PROVIDER_unload(nullprov);
766*b0d17251Schristos }
767