xref: /freebsd/crypto/openssl/test/params_api_test.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
4*e0c4386eSCy Schubert  *
5*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
6*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
7*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
8*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
9*e0c4386eSCy Schubert  */
10*e0c4386eSCy Schubert 
11*e0c4386eSCy Schubert #include <string.h>
12*e0c4386eSCy Schubert #include "testutil.h"
13*e0c4386eSCy Schubert #include "internal/nelem.h"
14*e0c4386eSCy Schubert #include "internal/endian.h"
15*e0c4386eSCy Schubert #include <openssl/params.h>
16*e0c4386eSCy Schubert #include <openssl/bn.h>
17*e0c4386eSCy Schubert 
18*e0c4386eSCy Schubert /* The maximum size of the static buffers used to test most things */
19*e0c4386eSCy Schubert #define MAX_LEN 20
20*e0c4386eSCy Schubert 
swap_copy(unsigned char * out,const void * in,size_t len)21*e0c4386eSCy Schubert static void swap_copy(unsigned char *out, const void *in, size_t len)
22*e0c4386eSCy Schubert {
23*e0c4386eSCy Schubert     size_t j;
24*e0c4386eSCy Schubert 
25*e0c4386eSCy Schubert     for (j = 0; j < len; j++)
26*e0c4386eSCy Schubert         out[j] = ((unsigned char *)in)[len - j - 1];
27*e0c4386eSCy Schubert }
28*e0c4386eSCy Schubert 
29*e0c4386eSCy Schubert /*
30*e0c4386eSCy Schubert  * A memory copy that converts the native byte ordering either to or from
31*e0c4386eSCy Schubert  * little endian format.
32*e0c4386eSCy Schubert  *
33*e0c4386eSCy Schubert  * On a little endian machine copying either is just a memcpy(3), on a
34*e0c4386eSCy Schubert  * big endian machine copying from native to or from little endian involves
35*e0c4386eSCy Schubert  * byte reversal.
36*e0c4386eSCy Schubert  */
le_copy(unsigned char * out,const void * in,size_t len)37*e0c4386eSCy Schubert static void le_copy(unsigned char *out, const void *in, size_t len)
38*e0c4386eSCy Schubert {
39*e0c4386eSCy Schubert     DECLARE_IS_ENDIAN;
40*e0c4386eSCy Schubert 
41*e0c4386eSCy Schubert     if (IS_LITTLE_ENDIAN)
42*e0c4386eSCy Schubert         memcpy(out, in, len);
43*e0c4386eSCy Schubert     else
44*e0c4386eSCy Schubert         swap_copy(out, in, len);
45*e0c4386eSCy Schubert }
46*e0c4386eSCy Schubert 
47*e0c4386eSCy Schubert static const struct {
48*e0c4386eSCy Schubert     size_t len;
49*e0c4386eSCy Schubert     unsigned char value[MAX_LEN];
50*e0c4386eSCy Schubert } raw_values[] = {
51*e0c4386eSCy Schubert     { 1, { 0x47 } },
52*e0c4386eSCy Schubert     { 1, { 0xd0 } },
53*e0c4386eSCy Schubert     { 2, { 0x01, 0xe9 } },
54*e0c4386eSCy Schubert     { 2, { 0xff, 0x53 } },
55*e0c4386eSCy Schubert     { 3, { 0x16, 0xff, 0x7c } },
56*e0c4386eSCy Schubert     { 3, { 0xa8, 0x9c, 0x0e } },
57*e0c4386eSCy Schubert     { 4, { 0x38, 0x27, 0xbf, 0x3b } },
58*e0c4386eSCy Schubert     { 4, { 0x9f, 0x26, 0x48, 0x22 } },
59*e0c4386eSCy Schubert     { 5, { 0x30, 0x65, 0xfa, 0xe4, 0x81 } },
60*e0c4386eSCy Schubert     { 5, { 0xd1, 0x76, 0x01, 0x1b, 0xcd } },
61*e0c4386eSCy Schubert     { 8, { 0x59, 0xb2, 0x1a, 0xe9, 0x2a, 0xd8, 0x46, 0x40 } },
62*e0c4386eSCy Schubert     { 8, { 0xb4, 0xae, 0xbd, 0xb4, 0xdd, 0x04, 0xb1, 0x4c } },
63*e0c4386eSCy Schubert     { 16, { 0x61, 0xe8, 0x7e, 0x31, 0xe9, 0x33, 0x83, 0x3d,
64*e0c4386eSCy Schubert             0x87, 0x99, 0xc7, 0xd8, 0x5d, 0xa9, 0x8b, 0x42 } },
65*e0c4386eSCy Schubert     { 16, { 0xee, 0x6e, 0x8b, 0xc3, 0xec, 0xcf, 0x37, 0xcc,
66*e0c4386eSCy Schubert             0x89, 0x67, 0xf2, 0x68, 0x33, 0xa0, 0x14, 0xb0 } },
67*e0c4386eSCy Schubert };
68*e0c4386eSCy Schubert 
test_param_type_extra(OSSL_PARAM * param,const unsigned char * cmp,size_t width)69*e0c4386eSCy Schubert static int test_param_type_extra(OSSL_PARAM *param, const unsigned char *cmp,
70*e0c4386eSCy Schubert                                  size_t width)
71*e0c4386eSCy Schubert {
72*e0c4386eSCy Schubert     int32_t i32;
73*e0c4386eSCy Schubert     int64_t i64;
74*e0c4386eSCy Schubert     size_t s, sz;
75*e0c4386eSCy Schubert     unsigned char buf[MAX_LEN];
76*e0c4386eSCy Schubert     const int bit32 = param->data_size <= sizeof(int32_t);
77*e0c4386eSCy Schubert     const int sizet = param->data_size <= sizeof(size_t);
78*e0c4386eSCy Schubert     const int signd = param->data_type == OSSL_PARAM_INTEGER;
79*e0c4386eSCy Schubert 
80*e0c4386eSCy Schubert     /*
81*e0c4386eSCy Schubert      * Set the unmodified sentinal directly because there is no param array
82*e0c4386eSCy Schubert      * for these tests.
83*e0c4386eSCy Schubert      */
84*e0c4386eSCy Schubert     param->return_size = OSSL_PARAM_UNMODIFIED;
85*e0c4386eSCy Schubert     if (signd) {
86*e0c4386eSCy Schubert         if ((bit32 && !TEST_true(OSSL_PARAM_get_int32(param, &i32)))
87*e0c4386eSCy Schubert             || !TEST_true(OSSL_PARAM_get_int64(param, &i64)))
88*e0c4386eSCy Schubert             return 0;
89*e0c4386eSCy Schubert     } else {
90*e0c4386eSCy Schubert         if ((bit32
91*e0c4386eSCy Schubert              && !TEST_true(OSSL_PARAM_get_uint32(param, (uint32_t *)&i32)))
92*e0c4386eSCy Schubert             || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64))
93*e0c4386eSCy Schubert             || (sizet && !TEST_true(OSSL_PARAM_get_size_t(param, &s))))
94*e0c4386eSCy Schubert             return 0;
95*e0c4386eSCy Schubert     }
96*e0c4386eSCy Schubert     if (!TEST_false(OSSL_PARAM_modified(param)))
97*e0c4386eSCy Schubert         return 0;
98*e0c4386eSCy Schubert 
99*e0c4386eSCy Schubert     /* Check signed types */
100*e0c4386eSCy Schubert     if (bit32) {
101*e0c4386eSCy Schubert         le_copy(buf, &i32, sizeof(i32));
102*e0c4386eSCy Schubert         sz = sizeof(i32) < width ? sizeof(i32) : width;
103*e0c4386eSCy Schubert         if (!TEST_mem_eq(buf, sz, cmp, sz))
104*e0c4386eSCy Schubert             return 0;
105*e0c4386eSCy Schubert     }
106*e0c4386eSCy Schubert     le_copy(buf, &i64, sizeof(i64));
107*e0c4386eSCy Schubert     sz = sizeof(i64) < width ? sizeof(i64) : width;
108*e0c4386eSCy Schubert     if (!TEST_mem_eq(buf, sz, cmp, sz))
109*e0c4386eSCy Schubert         return 0;
110*e0c4386eSCy Schubert     if (sizet && !signd) {
111*e0c4386eSCy Schubert         le_copy(buf, &s, sizeof(s));
112*e0c4386eSCy Schubert         sz = sizeof(s) < width ? sizeof(s) : width;
113*e0c4386eSCy Schubert         if (!TEST_mem_eq(buf, sz, cmp, sz))
114*e0c4386eSCy Schubert             return 0;
115*e0c4386eSCy Schubert     }
116*e0c4386eSCy Schubert 
117*e0c4386eSCy Schubert     /* Check a widening write if possible */
118*e0c4386eSCy Schubert     if (sizeof(size_t) > width) {
119*e0c4386eSCy Schubert         if (signd) {
120*e0c4386eSCy Schubert             if (!TEST_true(OSSL_PARAM_set_int32(param, 12345))
121*e0c4386eSCy Schubert                 || !TEST_true(OSSL_PARAM_get_int64(param, &i64))
122*e0c4386eSCy Schubert                 || !TEST_size_t_eq((size_t)i64, 12345))
123*e0c4386eSCy Schubert                 return 0;
124*e0c4386eSCy Schubert         } else {
125*e0c4386eSCy Schubert             if (!TEST_true(OSSL_PARAM_set_uint32(param, 12345))
126*e0c4386eSCy Schubert                 || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64))
127*e0c4386eSCy Schubert                 || !TEST_size_t_eq((size_t)i64, 12345))
128*e0c4386eSCy Schubert                 return 0;
129*e0c4386eSCy Schubert         }
130*e0c4386eSCy Schubert         if (!TEST_true(OSSL_PARAM_modified(param)))
131*e0c4386eSCy Schubert             return 0;
132*e0c4386eSCy Schubert     }
133*e0c4386eSCy Schubert     return 1;
134*e0c4386eSCy Schubert }
135*e0c4386eSCy Schubert 
136*e0c4386eSCy Schubert /*
137*e0c4386eSCy Schubert  * The test cases for each of the bastic integral types are similar.
138*e0c4386eSCy Schubert  * For each type, a param of that type is set and an attempt to read it
139*e0c4386eSCy Schubert  * get is made.  Finally, the above function is called to verify that
140*e0c4386eSCy Schubert  * the params can be read as other types.
141*e0c4386eSCy Schubert  *
142*e0c4386eSCy Schubert  * All the real work is done via byte buffers which are converted to machine
143*e0c4386eSCy Schubert  * byte order and to little endian for comparisons.  Narrower values are best
144*e0c4386eSCy Schubert  * compared using little endian because their values and positions don't
145*e0c4386eSCy Schubert  * change.
146*e0c4386eSCy Schubert  */
147*e0c4386eSCy Schubert 
test_param_int(int n)148*e0c4386eSCy Schubert static int test_param_int(int n)
149*e0c4386eSCy Schubert {
150*e0c4386eSCy Schubert     int in, out;
151*e0c4386eSCy Schubert     unsigned char buf[MAX_LEN], cmp[sizeof(int)];
152*e0c4386eSCy Schubert     const size_t len = raw_values[n].len >= sizeof(int) ?
153*e0c4386eSCy Schubert                        sizeof(int) : raw_values[n].len;
154*e0c4386eSCy Schubert     OSSL_PARAM param = OSSL_PARAM_int("a", NULL);
155*e0c4386eSCy Schubert 
156*e0c4386eSCy Schubert     memset(buf, 0, sizeof(buf));
157*e0c4386eSCy Schubert     le_copy(buf, raw_values[n].value, sizeof(in));
158*e0c4386eSCy Schubert     memcpy(&in, buf, sizeof(in));
159*e0c4386eSCy Schubert     param.data = &out;
160*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_set_int(&param, in)))
161*e0c4386eSCy Schubert         return 0;
162*e0c4386eSCy Schubert     le_copy(cmp, &out, sizeof(out));
163*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
164*e0c4386eSCy Schubert         return 0;
165*e0c4386eSCy Schubert     in = 0;
166*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_get_int(&param, &in)))
167*e0c4386eSCy Schubert         return 0;
168*e0c4386eSCy Schubert     le_copy(cmp, &in, sizeof(in));
169*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
170*e0c4386eSCy Schubert         return 0;
171*e0c4386eSCy Schubert     param.data = &out;
172*e0c4386eSCy Schubert     return test_param_type_extra(&param, raw_values[n].value, sizeof(int));
173*e0c4386eSCy Schubert }
174*e0c4386eSCy Schubert 
test_param_long(int n)175*e0c4386eSCy Schubert static int test_param_long(int n)
176*e0c4386eSCy Schubert {
177*e0c4386eSCy Schubert     long int in, out;
178*e0c4386eSCy Schubert     unsigned char buf[MAX_LEN], cmp[sizeof(long int)];
179*e0c4386eSCy Schubert     const size_t len = raw_values[n].len >= sizeof(long int)
180*e0c4386eSCy Schubert                        ? sizeof(long int) : raw_values[n].len;
181*e0c4386eSCy Schubert     OSSL_PARAM param = OSSL_PARAM_long("a", NULL);
182*e0c4386eSCy Schubert 
183*e0c4386eSCy Schubert     memset(buf, 0, sizeof(buf));
184*e0c4386eSCy Schubert     le_copy(buf, raw_values[n].value, sizeof(in));
185*e0c4386eSCy Schubert     memcpy(&in, buf, sizeof(in));
186*e0c4386eSCy Schubert     param.data = &out;
187*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_set_long(&param, in)))
188*e0c4386eSCy Schubert         return 0;
189*e0c4386eSCy Schubert     le_copy(cmp, &out, sizeof(out));
190*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
191*e0c4386eSCy Schubert         return 0;
192*e0c4386eSCy Schubert     in = 0;
193*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_get_long(&param, &in)))
194*e0c4386eSCy Schubert         return 0;
195*e0c4386eSCy Schubert     le_copy(cmp, &in, sizeof(in));
196*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
197*e0c4386eSCy Schubert         return 0;
198*e0c4386eSCy Schubert     param.data = &out;
199*e0c4386eSCy Schubert     return test_param_type_extra(&param, raw_values[n].value, sizeof(long int));
200*e0c4386eSCy Schubert }
201*e0c4386eSCy Schubert 
test_param_uint(int n)202*e0c4386eSCy Schubert static int test_param_uint(int n)
203*e0c4386eSCy Schubert {
204*e0c4386eSCy Schubert     unsigned int in, out;
205*e0c4386eSCy Schubert     unsigned char buf[MAX_LEN], cmp[sizeof(unsigned int)];
206*e0c4386eSCy Schubert     const size_t len = raw_values[n].len >= sizeof(unsigned int) ? sizeof(unsigned int) : raw_values[n].len;
207*e0c4386eSCy Schubert     OSSL_PARAM param = OSSL_PARAM_uint("a", NULL);
208*e0c4386eSCy Schubert 
209*e0c4386eSCy Schubert     memset(buf, 0, sizeof(buf));
210*e0c4386eSCy Schubert     le_copy(buf, raw_values[n].value, sizeof(in));
211*e0c4386eSCy Schubert     memcpy(&in, buf, sizeof(in));
212*e0c4386eSCy Schubert     param.data = &out;
213*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_set_uint(&param, in)))
214*e0c4386eSCy Schubert         return 0;
215*e0c4386eSCy Schubert     le_copy(cmp, &out, sizeof(out));
216*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
217*e0c4386eSCy Schubert         return 0;
218*e0c4386eSCy Schubert     in = 0;
219*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_get_uint(&param, &in)))
220*e0c4386eSCy Schubert         return 0;
221*e0c4386eSCy Schubert     le_copy(cmp, &in, sizeof(in));
222*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
223*e0c4386eSCy Schubert         return 0;
224*e0c4386eSCy Schubert     param.data = &out;
225*e0c4386eSCy Schubert     return test_param_type_extra(&param, raw_values[n].value, sizeof(unsigned int));
226*e0c4386eSCy Schubert }
227*e0c4386eSCy Schubert 
test_param_ulong(int n)228*e0c4386eSCy Schubert static int test_param_ulong(int n)
229*e0c4386eSCy Schubert {
230*e0c4386eSCy Schubert     unsigned long int in, out;
231*e0c4386eSCy Schubert     unsigned char buf[MAX_LEN], cmp[sizeof(unsigned long int)];
232*e0c4386eSCy Schubert     const size_t len = raw_values[n].len >= sizeof(unsigned long int)
233*e0c4386eSCy Schubert                        ? sizeof(unsigned long int) : raw_values[n].len;
234*e0c4386eSCy Schubert     OSSL_PARAM param = OSSL_PARAM_ulong("a", NULL);
235*e0c4386eSCy Schubert 
236*e0c4386eSCy Schubert     memset(buf, 0, sizeof(buf));
237*e0c4386eSCy Schubert     le_copy(buf, raw_values[n].value, sizeof(in));
238*e0c4386eSCy Schubert     memcpy(&in, buf, sizeof(in));
239*e0c4386eSCy Schubert     param.data = &out;
240*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_set_ulong(&param, in)))
241*e0c4386eSCy Schubert         return 0;
242*e0c4386eSCy Schubert     le_copy(cmp, &out, sizeof(out));
243*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
244*e0c4386eSCy Schubert         return 0;
245*e0c4386eSCy Schubert     in = 0;
246*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_get_ulong(&param, &in)))
247*e0c4386eSCy Schubert         return 0;
248*e0c4386eSCy Schubert     le_copy(cmp, &in, sizeof(in));
249*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
250*e0c4386eSCy Schubert         return 0;
251*e0c4386eSCy Schubert     param.data = &out;
252*e0c4386eSCy Schubert     return test_param_type_extra(&param, raw_values[n].value, sizeof(unsigned long int));
253*e0c4386eSCy Schubert }
254*e0c4386eSCy Schubert 
test_param_int32(int n)255*e0c4386eSCy Schubert static int test_param_int32(int n)
256*e0c4386eSCy Schubert {
257*e0c4386eSCy Schubert     int32_t in, out;
258*e0c4386eSCy Schubert     unsigned char buf[MAX_LEN], cmp[sizeof(int32_t)];
259*e0c4386eSCy Schubert     const size_t len = raw_values[n].len >= sizeof(int32_t)
260*e0c4386eSCy Schubert                        ? sizeof(int32_t) : raw_values[n].len;
261*e0c4386eSCy Schubert     OSSL_PARAM param = OSSL_PARAM_int32("a", NULL);
262*e0c4386eSCy Schubert 
263*e0c4386eSCy Schubert     memset(buf, 0, sizeof(buf));
264*e0c4386eSCy Schubert     le_copy(buf, raw_values[n].value, sizeof(in));
265*e0c4386eSCy Schubert     memcpy(&in, buf, sizeof(in));
266*e0c4386eSCy Schubert     param.data = &out;
267*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_set_int32(&param, in)))
268*e0c4386eSCy Schubert         return 0;
269*e0c4386eSCy Schubert     le_copy(cmp, &out, sizeof(out));
270*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
271*e0c4386eSCy Schubert         return 0;
272*e0c4386eSCy Schubert     in = 0;
273*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_get_int32(&param, &in)))
274*e0c4386eSCy Schubert         return 0;
275*e0c4386eSCy Schubert     le_copy(cmp, &in, sizeof(in));
276*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
277*e0c4386eSCy Schubert         return 0;
278*e0c4386eSCy Schubert     param.data = &out;
279*e0c4386eSCy Schubert     return test_param_type_extra(&param, raw_values[n].value, sizeof(int32_t));
280*e0c4386eSCy Schubert }
281*e0c4386eSCy Schubert 
test_param_uint32(int n)282*e0c4386eSCy Schubert static int test_param_uint32(int n)
283*e0c4386eSCy Schubert {
284*e0c4386eSCy Schubert     uint32_t in, out;
285*e0c4386eSCy Schubert     unsigned char buf[MAX_LEN], cmp[sizeof(uint32_t)];
286*e0c4386eSCy Schubert     const size_t len = raw_values[n].len >= sizeof(uint32_t)
287*e0c4386eSCy Schubert                        ? sizeof(uint32_t) : raw_values[n].len;
288*e0c4386eSCy Schubert     OSSL_PARAM param = OSSL_PARAM_uint32("a", NULL);
289*e0c4386eSCy Schubert 
290*e0c4386eSCy Schubert     memset(buf, 0, sizeof(buf));
291*e0c4386eSCy Schubert     le_copy(buf, raw_values[n].value, sizeof(in));
292*e0c4386eSCy Schubert     memcpy(&in, buf, sizeof(in));
293*e0c4386eSCy Schubert     param.data = &out;
294*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_set_uint32(&param, in)))
295*e0c4386eSCy Schubert         return 0;
296*e0c4386eSCy Schubert     le_copy(cmp, &out, sizeof(out));
297*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
298*e0c4386eSCy Schubert         return 0;
299*e0c4386eSCy Schubert     in = 0;
300*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_get_uint32(&param, &in)))
301*e0c4386eSCy Schubert         return 0;
302*e0c4386eSCy Schubert     le_copy(cmp, &in, sizeof(in));
303*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
304*e0c4386eSCy Schubert         return 0;
305*e0c4386eSCy Schubert     param.data = &out;
306*e0c4386eSCy Schubert     return test_param_type_extra(&param, raw_values[n].value, sizeof(uint32_t));
307*e0c4386eSCy Schubert }
308*e0c4386eSCy Schubert 
test_param_int64(int n)309*e0c4386eSCy Schubert static int test_param_int64(int n)
310*e0c4386eSCy Schubert {
311*e0c4386eSCy Schubert     int64_t in, out;
312*e0c4386eSCy Schubert     unsigned char buf[MAX_LEN], cmp[sizeof(int64_t)];
313*e0c4386eSCy Schubert     const size_t len = raw_values[n].len >= sizeof(int64_t)
314*e0c4386eSCy Schubert                        ? sizeof(int64_t) : raw_values[n].len;
315*e0c4386eSCy Schubert     OSSL_PARAM param = OSSL_PARAM_int64("a", NULL);
316*e0c4386eSCy Schubert 
317*e0c4386eSCy Schubert     memset(buf, 0, sizeof(buf));
318*e0c4386eSCy Schubert     le_copy(buf, raw_values[n].value, sizeof(in));
319*e0c4386eSCy Schubert     memcpy(&in, buf, sizeof(in));
320*e0c4386eSCy Schubert     param.data = &out;
321*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_set_int64(&param, in)))
322*e0c4386eSCy Schubert         return 0;
323*e0c4386eSCy Schubert     le_copy(cmp, &out, sizeof(out));
324*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
325*e0c4386eSCy Schubert         return 0;
326*e0c4386eSCy Schubert     in = 0;
327*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_get_int64(&param, &in)))
328*e0c4386eSCy Schubert         return 0;
329*e0c4386eSCy Schubert     le_copy(cmp, &in, sizeof(in));
330*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
331*e0c4386eSCy Schubert         return 0;
332*e0c4386eSCy Schubert     param.data = &out;
333*e0c4386eSCy Schubert     return test_param_type_extra(&param, raw_values[n].value, sizeof(int64_t));
334*e0c4386eSCy Schubert }
335*e0c4386eSCy Schubert 
test_param_uint64(int n)336*e0c4386eSCy Schubert static int test_param_uint64(int n)
337*e0c4386eSCy Schubert {
338*e0c4386eSCy Schubert     uint64_t in, out;
339*e0c4386eSCy Schubert     unsigned char buf[MAX_LEN], cmp[sizeof(uint64_t)];
340*e0c4386eSCy Schubert     const size_t len = raw_values[n].len >= sizeof(uint64_t)
341*e0c4386eSCy Schubert                        ? sizeof(uint64_t) : raw_values[n].len;
342*e0c4386eSCy Schubert     OSSL_PARAM param = OSSL_PARAM_uint64("a", NULL);
343*e0c4386eSCy Schubert 
344*e0c4386eSCy Schubert     memset(buf, 0, sizeof(buf));
345*e0c4386eSCy Schubert     le_copy(buf, raw_values[n].value, sizeof(in));
346*e0c4386eSCy Schubert     memcpy(&in, buf, sizeof(in));
347*e0c4386eSCy Schubert     param.data = &out;
348*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_set_uint64(&param, in)))
349*e0c4386eSCy Schubert         return 0;
350*e0c4386eSCy Schubert     le_copy(cmp, &out, sizeof(out));
351*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
352*e0c4386eSCy Schubert         return 0;
353*e0c4386eSCy Schubert     in = 0;
354*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_get_uint64(&param, &in)))
355*e0c4386eSCy Schubert         return 0;
356*e0c4386eSCy Schubert     le_copy(cmp, &in, sizeof(in));
357*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
358*e0c4386eSCy Schubert         return 0;
359*e0c4386eSCy Schubert     param.data = &out;
360*e0c4386eSCy Schubert     return test_param_type_extra(&param, raw_values[n].value, sizeof(uint64_t));
361*e0c4386eSCy Schubert }
362*e0c4386eSCy Schubert 
test_param_size_t(int n)363*e0c4386eSCy Schubert static int test_param_size_t(int n)
364*e0c4386eSCy Schubert {
365*e0c4386eSCy Schubert     size_t in, out;
366*e0c4386eSCy Schubert     unsigned char buf[MAX_LEN], cmp[sizeof(size_t)];
367*e0c4386eSCy Schubert     const size_t len = raw_values[n].len >= sizeof(size_t)
368*e0c4386eSCy Schubert                        ? sizeof(size_t) : raw_values[n].len;
369*e0c4386eSCy Schubert     OSSL_PARAM param = OSSL_PARAM_size_t("a", NULL);
370*e0c4386eSCy Schubert 
371*e0c4386eSCy Schubert     memset(buf, 0, sizeof(buf));
372*e0c4386eSCy Schubert     le_copy(buf, raw_values[n].value, sizeof(in));
373*e0c4386eSCy Schubert     memcpy(&in, buf, sizeof(in));
374*e0c4386eSCy Schubert     param.data = &out;
375*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_set_size_t(&param, in)))
376*e0c4386eSCy Schubert         return 0;
377*e0c4386eSCy Schubert     le_copy(cmp, &out, sizeof(out));
378*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
379*e0c4386eSCy Schubert         return 0;
380*e0c4386eSCy Schubert     in = 0;
381*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_get_size_t(&param, &in)))
382*e0c4386eSCy Schubert         return 0;
383*e0c4386eSCy Schubert     le_copy(cmp, &in, sizeof(in));
384*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
385*e0c4386eSCy Schubert         return 0;
386*e0c4386eSCy Schubert     param.data = &out;
387*e0c4386eSCy Schubert     return test_param_type_extra(&param, raw_values[n].value, sizeof(size_t));
388*e0c4386eSCy Schubert }
389*e0c4386eSCy Schubert 
test_param_time_t(int n)390*e0c4386eSCy Schubert static int test_param_time_t(int n)
391*e0c4386eSCy Schubert {
392*e0c4386eSCy Schubert     time_t in, out;
393*e0c4386eSCy Schubert     unsigned char buf[MAX_LEN], cmp[sizeof(time_t)];
394*e0c4386eSCy Schubert     const size_t len = raw_values[n].len >= sizeof(time_t)
395*e0c4386eSCy Schubert                        ? sizeof(time_t) : raw_values[n].len;
396*e0c4386eSCy Schubert     OSSL_PARAM param = OSSL_PARAM_time_t("a", NULL);
397*e0c4386eSCy Schubert 
398*e0c4386eSCy Schubert     memset(buf, 0, sizeof(buf));
399*e0c4386eSCy Schubert     le_copy(buf, raw_values[n].value, sizeof(in));
400*e0c4386eSCy Schubert     memcpy(&in, buf, sizeof(in));
401*e0c4386eSCy Schubert     param.data = &out;
402*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_set_time_t(&param, in)))
403*e0c4386eSCy Schubert         return 0;
404*e0c4386eSCy Schubert     le_copy(cmp, &out, sizeof(out));
405*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
406*e0c4386eSCy Schubert         return 0;
407*e0c4386eSCy Schubert     in = 0;
408*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_get_time_t(&param, &in)))
409*e0c4386eSCy Schubert         return 0;
410*e0c4386eSCy Schubert     le_copy(cmp, &in, sizeof(in));
411*e0c4386eSCy Schubert     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
412*e0c4386eSCy Schubert         return 0;
413*e0c4386eSCy Schubert     param.data = &out;
414*e0c4386eSCy Schubert     return test_param_type_extra(&param, raw_values[n].value, sizeof(size_t));
415*e0c4386eSCy Schubert }
416*e0c4386eSCy Schubert 
test_param_bignum(int n)417*e0c4386eSCy Schubert static int test_param_bignum(int n)
418*e0c4386eSCy Schubert {
419*e0c4386eSCy Schubert     unsigned char buf[MAX_LEN], bnbuf[MAX_LEN];
420*e0c4386eSCy Schubert     const size_t len = raw_values[n].len;
421*e0c4386eSCy Schubert     BIGNUM *b = NULL, *c = NULL;
422*e0c4386eSCy Schubert     OSSL_PARAM param = OSSL_PARAM_DEFN("bn", OSSL_PARAM_UNSIGNED_INTEGER,
423*e0c4386eSCy Schubert                                        NULL, 0);
424*e0c4386eSCy Schubert     int ret = 0;
425*e0c4386eSCy Schubert 
426*e0c4386eSCy Schubert     param.data = bnbuf;
427*e0c4386eSCy Schubert     param.data_size = len;
428*e0c4386eSCy Schubert 
429*e0c4386eSCy Schubert     le_copy(buf, raw_values[n].value, len);
430*e0c4386eSCy Schubert     if (!TEST_ptr(b = BN_lebin2bn(raw_values[n].value, (int)len, NULL)))
431*e0c4386eSCy Schubert         goto err;
432*e0c4386eSCy Schubert 
433*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_set_BN(&param, b))
434*e0c4386eSCy Schubert         || !TEST_mem_eq(bnbuf, param.return_size, buf, param.return_size))
435*e0c4386eSCy Schubert         goto err;
436*e0c4386eSCy Schubert     param.data_size = param.return_size;
437*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_get_BN(&param, &c))
438*e0c4386eSCy Schubert         || !TEST_BN_eq(b, c))
439*e0c4386eSCy Schubert         goto err;
440*e0c4386eSCy Schubert 
441*e0c4386eSCy Schubert     ret = 1;
442*e0c4386eSCy Schubert err:
443*e0c4386eSCy Schubert     BN_free(b);
444*e0c4386eSCy Schubert     BN_free(c);
445*e0c4386eSCy Schubert     return ret;
446*e0c4386eSCy Schubert }
447*e0c4386eSCy Schubert 
test_param_real(void)448*e0c4386eSCy Schubert static int test_param_real(void)
449*e0c4386eSCy Schubert {
450*e0c4386eSCy Schubert     double p;
451*e0c4386eSCy Schubert     OSSL_PARAM param = OSSL_PARAM_double("r", NULL);
452*e0c4386eSCy Schubert 
453*e0c4386eSCy Schubert     param.data = &p;
454*e0c4386eSCy Schubert     return TEST_true(OSSL_PARAM_set_double(&param, 3.14159))
455*e0c4386eSCy Schubert            && TEST_double_eq(p, 3.14159);
456*e0c4386eSCy Schubert }
457*e0c4386eSCy Schubert 
test_param_construct(int tstid)458*e0c4386eSCy Schubert static int test_param_construct(int tstid)
459*e0c4386eSCy Schubert {
460*e0c4386eSCy Schubert     static const char *int_names[] = {
461*e0c4386eSCy Schubert         "int", "long", "int32", "int64"
462*e0c4386eSCy Schubert     };
463*e0c4386eSCy Schubert     static const char *uint_names[] = {
464*e0c4386eSCy Schubert         "uint", "ulong", "uint32", "uint64", "size_t"
465*e0c4386eSCy Schubert     };
466*e0c4386eSCy Schubert     static const unsigned char bn_val[16] = {
467*e0c4386eSCy Schubert         0xac, 0x75, 0x22, 0x7d, 0x81, 0x06, 0x7a, 0x23,
468*e0c4386eSCy Schubert         0xa6, 0xed, 0x87, 0xc7, 0xab, 0xf4, 0x73, 0x22
469*e0c4386eSCy Schubert     };
470*e0c4386eSCy Schubert     OSSL_PARAM *p = NULL, *p1 = NULL;
471*e0c4386eSCy Schubert     static const OSSL_PARAM params_empty[] = {
472*e0c4386eSCy Schubert         OSSL_PARAM_END
473*e0c4386eSCy Schubert     };
474*e0c4386eSCy Schubert     OSSL_PARAM params[20];
475*e0c4386eSCy Schubert     char buf[100], buf2[100], *bufp, *bufp2;
476*e0c4386eSCy Schubert     unsigned char ubuf[100];
477*e0c4386eSCy Schubert     void *vp, *vpn = NULL, *vp2;
478*e0c4386eSCy Schubert     OSSL_PARAM *cp;
479*e0c4386eSCy Schubert     int i, n = 0, ret = 0;
480*e0c4386eSCy Schubert     unsigned int u;
481*e0c4386eSCy Schubert     long int l;
482*e0c4386eSCy Schubert     unsigned long int ul;
483*e0c4386eSCy Schubert     int32_t i32;
484*e0c4386eSCy Schubert     uint32_t u32;
485*e0c4386eSCy Schubert     int64_t i64;
486*e0c4386eSCy Schubert     uint64_t u64;
487*e0c4386eSCy Schubert     size_t j, k, s;
488*e0c4386eSCy Schubert     double d, d2;
489*e0c4386eSCy Schubert     BIGNUM *bn = NULL, *bn2 = NULL;
490*e0c4386eSCy Schubert 
491*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_int("int", &i);
492*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_uint("uint", &u);
493*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_long("long", &l);
494*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_ulong("ulong", &ul);
495*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_int32("int32", &i32);
496*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_int64("int64", &i64);
497*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_uint32("uint32", &u32);
498*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_uint64("uint64", &u64);
499*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_size_t("size_t", &s);
500*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_double("double", &d);
501*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_BN("bignum", ubuf, sizeof(ubuf));
502*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_utf8_string("utf8str", buf, sizeof(buf));
503*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_octet_string("octstr", buf, sizeof(buf));
504*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_utf8_ptr("utf8ptr", &bufp, 0);
505*e0c4386eSCy Schubert     params[n++] = OSSL_PARAM_construct_octet_ptr("octptr", &vp, 0);
506*e0c4386eSCy Schubert     params[n] = OSSL_PARAM_construct_end();
507*e0c4386eSCy Schubert 
508*e0c4386eSCy Schubert     switch(tstid) {
509*e0c4386eSCy Schubert     case 0:
510*e0c4386eSCy Schubert         p = params;
511*e0c4386eSCy Schubert         break;
512*e0c4386eSCy Schubert     case 1:
513*e0c4386eSCy Schubert         p = OSSL_PARAM_merge(params, params_empty);
514*e0c4386eSCy Schubert         break;
515*e0c4386eSCy Schubert     case 2:
516*e0c4386eSCy Schubert         p = OSSL_PARAM_dup(params);
517*e0c4386eSCy Schubert         break;
518*e0c4386eSCy Schubert     default:
519*e0c4386eSCy Schubert         p1 = OSSL_PARAM_dup(params);
520*e0c4386eSCy Schubert         p = OSSL_PARAM_merge(p1, params_empty);
521*e0c4386eSCy Schubert         break;
522*e0c4386eSCy Schubert     }
523*e0c4386eSCy Schubert 
524*e0c4386eSCy Schubert     /* Search failure */
525*e0c4386eSCy Schubert     if (!TEST_ptr_null(OSSL_PARAM_locate(p, "fnord")))
526*e0c4386eSCy Schubert         goto err;
527*e0c4386eSCy Schubert 
528*e0c4386eSCy Schubert     /* All signed integral types */
529*e0c4386eSCy Schubert     for (j = 0; j < OSSL_NELEM(int_names); j++) {
530*e0c4386eSCy Schubert         if (!TEST_ptr(cp = OSSL_PARAM_locate(p, int_names[j]))
531*e0c4386eSCy Schubert             || !TEST_true(OSSL_PARAM_set_int32(cp, (int32_t)(3 + j)))
532*e0c4386eSCy Schubert             || !TEST_true(OSSL_PARAM_get_int64(cp, &i64))
533*e0c4386eSCy Schubert             || !TEST_size_t_eq(cp->data_size, cp->return_size)
534*e0c4386eSCy Schubert             || !TEST_size_t_eq((size_t)i64, 3 + j)) {
535*e0c4386eSCy Schubert             TEST_note("iteration %zu var %s", j + 1, int_names[j]);
536*e0c4386eSCy Schubert             goto err;
537*e0c4386eSCy Schubert         }
538*e0c4386eSCy Schubert     }
539*e0c4386eSCy Schubert     /* All unsigned integral types */
540*e0c4386eSCy Schubert     for (j = 0; j < OSSL_NELEM(uint_names); j++) {
541*e0c4386eSCy Schubert         if (!TEST_ptr(cp = OSSL_PARAM_locate(p, uint_names[j]))
542*e0c4386eSCy Schubert             || !TEST_true(OSSL_PARAM_set_uint32(cp, (uint32_t)(3 + j)))
543*e0c4386eSCy Schubert             || !TEST_true(OSSL_PARAM_get_uint64(cp, &u64))
544*e0c4386eSCy Schubert             || !TEST_size_t_eq(cp->data_size, cp->return_size)
545*e0c4386eSCy Schubert             || !TEST_size_t_eq((size_t)u64, 3 + j)) {
546*e0c4386eSCy Schubert             TEST_note("iteration %zu var %s", j + 1, uint_names[j]);
547*e0c4386eSCy Schubert             goto err;
548*e0c4386eSCy Schubert         }
549*e0c4386eSCy Schubert     }
550*e0c4386eSCy Schubert     /* Real */
551*e0c4386eSCy Schubert     if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "double"))
552*e0c4386eSCy Schubert         || !TEST_true(OSSL_PARAM_set_double(cp, 3.14))
553*e0c4386eSCy Schubert         || !TEST_true(OSSL_PARAM_get_double(cp, &d2))
554*e0c4386eSCy Schubert         || !TEST_size_t_eq(cp->return_size, sizeof(double))
555*e0c4386eSCy Schubert         || !TEST_double_eq(d2, 3.14)
556*e0c4386eSCy Schubert         || (tstid <= 1 && !TEST_double_eq(d, d2)))
557*e0c4386eSCy Schubert         goto err;
558*e0c4386eSCy Schubert     /* UTF8 string */
559*e0c4386eSCy Schubert     bufp = NULL;
560*e0c4386eSCy Schubert     if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "utf8str"))
561*e0c4386eSCy Schubert         || !TEST_true(OSSL_PARAM_set_utf8_string(cp, "abcdef"))
562*e0c4386eSCy Schubert         || !TEST_size_t_eq(cp->return_size, sizeof("abcdef") - 1)
563*e0c4386eSCy Schubert         || !TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, 0))
564*e0c4386eSCy Schubert         || !TEST_str_eq(bufp, "abcdef")) {
565*e0c4386eSCy Schubert         OPENSSL_free(bufp);
566*e0c4386eSCy Schubert         goto err;
567*e0c4386eSCy Schubert     }
568*e0c4386eSCy Schubert     OPENSSL_free(bufp);
569*e0c4386eSCy Schubert     bufp = buf2;
570*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, sizeof(buf2)))
571*e0c4386eSCy Schubert         || !TEST_str_eq(buf2, "abcdef"))
572*e0c4386eSCy Schubert         goto err;
573*e0c4386eSCy Schubert     /* UTF8 pointer */
574*e0c4386eSCy Schubert     /* Note that the size of a UTF8 string does *NOT* include the NUL byte */
575*e0c4386eSCy Schubert     bufp = buf;
576*e0c4386eSCy Schubert     if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "utf8ptr"))
577*e0c4386eSCy Schubert         || !TEST_true(OSSL_PARAM_set_utf8_ptr(cp, "tuvwxyz"))
578*e0c4386eSCy Schubert         || !TEST_size_t_eq(cp->return_size, sizeof("tuvwxyz") - 1)
579*e0c4386eSCy Schubert         || !TEST_true(OSSL_PARAM_get_utf8_ptr(cp, (const char **)&bufp2))
580*e0c4386eSCy Schubert         || !TEST_str_eq(bufp2, "tuvwxyz")
581*e0c4386eSCy Schubert         || (tstid <= 1 && !TEST_ptr_eq(bufp2, bufp)))
582*e0c4386eSCy Schubert         goto err;
583*e0c4386eSCy Schubert     /* OCTET string */
584*e0c4386eSCy Schubert     if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "octstr"))
585*e0c4386eSCy Schubert         || !TEST_true(OSSL_PARAM_set_octet_string(cp, "abcdefghi",
586*e0c4386eSCy Schubert                                                   sizeof("abcdefghi")))
587*e0c4386eSCy Schubert         || !TEST_size_t_eq(cp->return_size, sizeof("abcdefghi")))
588*e0c4386eSCy Schubert         goto err;
589*e0c4386eSCy Schubert     /* Match the return size to avoid trailing garbage bytes */
590*e0c4386eSCy Schubert     cp->data_size = cp->return_size;
591*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_get_octet_string(cp, &vpn, 0, &s))
592*e0c4386eSCy Schubert         || !TEST_size_t_eq(s, sizeof("abcdefghi"))
593*e0c4386eSCy Schubert         || !TEST_mem_eq(vpn, sizeof("abcdefghi"),
594*e0c4386eSCy Schubert                         "abcdefghi", sizeof("abcdefghi")))
595*e0c4386eSCy Schubert         goto err;
596*e0c4386eSCy Schubert     vp = buf2;
597*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_get_octet_string(cp, &vp, sizeof(buf2), &s))
598*e0c4386eSCy Schubert         || !TEST_size_t_eq(s, sizeof("abcdefghi"))
599*e0c4386eSCy Schubert         || !TEST_mem_eq(vp, sizeof("abcdefghi"),
600*e0c4386eSCy Schubert                         "abcdefghi", sizeof("abcdefghi")))
601*e0c4386eSCy Schubert         goto err;
602*e0c4386eSCy Schubert     /* OCTET pointer */
603*e0c4386eSCy Schubert     vp = &l;
604*e0c4386eSCy Schubert     if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "octptr"))
605*e0c4386eSCy Schubert         || !TEST_true(OSSL_PARAM_set_octet_ptr(cp, &ul, sizeof(ul)))
606*e0c4386eSCy Schubert         || !TEST_size_t_eq(cp->return_size, sizeof(ul))
607*e0c4386eSCy Schubert         || (tstid <= 1 && !TEST_ptr_eq(vp, &ul)))
608*e0c4386eSCy Schubert         goto err;
609*e0c4386eSCy Schubert     /* Match the return size to avoid trailing garbage bytes */
610*e0c4386eSCy Schubert     cp->data_size = cp->return_size;
611*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PARAM_get_octet_ptr(cp, (const void **)&vp2, &k))
612*e0c4386eSCy Schubert         || !TEST_size_t_eq(k, sizeof(ul))
613*e0c4386eSCy Schubert         || (tstid <= 1 && !TEST_ptr_eq(vp2, vp)))
614*e0c4386eSCy Schubert         goto err;
615*e0c4386eSCy Schubert     /* BIGNUM */
616*e0c4386eSCy Schubert     if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "bignum"))
617*e0c4386eSCy Schubert         || !TEST_ptr(bn = BN_lebin2bn(bn_val, (int)sizeof(bn_val), NULL))
618*e0c4386eSCy Schubert         || !TEST_true(OSSL_PARAM_set_BN(cp, bn))
619*e0c4386eSCy Schubert         || !TEST_size_t_eq(cp->data_size, cp->return_size))
620*e0c4386eSCy Schubert         goto err;
621*e0c4386eSCy Schubert     /* Match the return size to avoid trailing garbage bytes */
622*e0c4386eSCy Schubert     cp->data_size = cp->return_size;
623*e0c4386eSCy Schubert     if(!TEST_true(OSSL_PARAM_get_BN(cp, &bn2))
624*e0c4386eSCy Schubert         || !TEST_BN_eq(bn, bn2))
625*e0c4386eSCy Schubert         goto err;
626*e0c4386eSCy Schubert     ret = 1;
627*e0c4386eSCy Schubert err:
628*e0c4386eSCy Schubert     if (p != params)
629*e0c4386eSCy Schubert         OPENSSL_free(p);
630*e0c4386eSCy Schubert     OPENSSL_free(p1);
631*e0c4386eSCy Schubert     OPENSSL_free(vpn);
632*e0c4386eSCy Schubert     BN_free(bn);
633*e0c4386eSCy Schubert     BN_free(bn2);
634*e0c4386eSCy Schubert     return ret;
635*e0c4386eSCy Schubert }
636*e0c4386eSCy Schubert 
test_param_modified(void)637*e0c4386eSCy Schubert static int test_param_modified(void)
638*e0c4386eSCy Schubert {
639*e0c4386eSCy Schubert     OSSL_PARAM param[3] = { OSSL_PARAM_int("a", NULL),
640*e0c4386eSCy Schubert                             OSSL_PARAM_int("b", NULL),
641*e0c4386eSCy Schubert                             OSSL_PARAM_END };
642*e0c4386eSCy Schubert     int a, b;
643*e0c4386eSCy Schubert 
644*e0c4386eSCy Schubert     param->data = &a;
645*e0c4386eSCy Schubert     param[1].data = &b;
646*e0c4386eSCy Schubert     if (!TEST_false(OSSL_PARAM_modified(param))
647*e0c4386eSCy Schubert             && !TEST_true(OSSL_PARAM_set_int32(param, 1234))
648*e0c4386eSCy Schubert             && !TEST_true(OSSL_PARAM_modified(param))
649*e0c4386eSCy Schubert             && !TEST_false(OSSL_PARAM_modified(param + 1))
650*e0c4386eSCy Schubert             && !TEST_true(OSSL_PARAM_set_int32(param + 1, 1))
651*e0c4386eSCy Schubert             && !TEST_true(OSSL_PARAM_modified(param + 1)))
652*e0c4386eSCy Schubert         return 0;
653*e0c4386eSCy Schubert     OSSL_PARAM_set_all_unmodified(param);
654*e0c4386eSCy Schubert     if (!TEST_false(OSSL_PARAM_modified(param))
655*e0c4386eSCy Schubert             && !TEST_true(OSSL_PARAM_set_int32(param, 4321))
656*e0c4386eSCy Schubert             && !TEST_true(OSSL_PARAM_modified(param))
657*e0c4386eSCy Schubert             && !TEST_false(OSSL_PARAM_modified(param + 1))
658*e0c4386eSCy Schubert             && !TEST_true(OSSL_PARAM_set_int32(param + 1, 2))
659*e0c4386eSCy Schubert             && !TEST_true(OSSL_PARAM_modified(param + 1)))
660*e0c4386eSCy Schubert         return 0;
661*e0c4386eSCy Schubert     return 1;
662*e0c4386eSCy Schubert }
663*e0c4386eSCy Schubert 
test_param_copy_null(void)664*e0c4386eSCy Schubert static int test_param_copy_null(void)
665*e0c4386eSCy Schubert {
666*e0c4386eSCy Schubert     int ret, val;
667*e0c4386eSCy Schubert     int a = 1, b = 2, i = 0;
668*e0c4386eSCy Schubert     OSSL_PARAM *cp1 = NULL, *cp2 = NULL, *p;
669*e0c4386eSCy Schubert     OSSL_PARAM param[3];
670*e0c4386eSCy Schubert 
671*e0c4386eSCy Schubert     param[i++] = OSSL_PARAM_construct_int("a", &a);
672*e0c4386eSCy Schubert     param[i++] = OSSL_PARAM_construct_int("b", &b);
673*e0c4386eSCy Schubert     param[i] = OSSL_PARAM_construct_end();
674*e0c4386eSCy Schubert 
675*e0c4386eSCy Schubert     ret = TEST_ptr_null(OSSL_PARAM_dup(NULL))
676*e0c4386eSCy Schubert           && TEST_ptr(cp1 = OSSL_PARAM_merge(NULL, param))
677*e0c4386eSCy Schubert           && TEST_ptr(p = OSSL_PARAM_locate(cp1, "a"))
678*e0c4386eSCy Schubert           && TEST_true(OSSL_PARAM_get_int(p, &val))
679*e0c4386eSCy Schubert           && TEST_int_eq(val, 1)
680*e0c4386eSCy Schubert           && TEST_ptr(p = OSSL_PARAM_locate(cp1, "b"))
681*e0c4386eSCy Schubert           && TEST_true(OSSL_PARAM_get_int(p, &val))
682*e0c4386eSCy Schubert           && TEST_int_eq(val, 2)
683*e0c4386eSCy Schubert           && TEST_ptr(cp2 = OSSL_PARAM_merge(param, NULL))
684*e0c4386eSCy Schubert           && TEST_ptr(p = OSSL_PARAM_locate(cp2, "a"))
685*e0c4386eSCy Schubert           && TEST_true(OSSL_PARAM_get_int(p, &val))
686*e0c4386eSCy Schubert           && TEST_int_eq(val, 1)
687*e0c4386eSCy Schubert           && TEST_ptr(p = OSSL_PARAM_locate(cp2, "b"))
688*e0c4386eSCy Schubert           && TEST_true(OSSL_PARAM_get_int(p, &val))
689*e0c4386eSCy Schubert           && TEST_int_eq(val, 2)
690*e0c4386eSCy Schubert           && TEST_ptr_null(OSSL_PARAM_merge(NULL, NULL));
691*e0c4386eSCy Schubert     OSSL_PARAM_free(cp2);
692*e0c4386eSCy Schubert     OSSL_PARAM_free(cp1);
693*e0c4386eSCy Schubert     return ret;
694*e0c4386eSCy Schubert }
695*e0c4386eSCy Schubert 
setup_tests(void)696*e0c4386eSCy Schubert int setup_tests(void)
697*e0c4386eSCy Schubert {
698*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_param_int, OSSL_NELEM(raw_values));
699*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_param_long, OSSL_NELEM(raw_values));
700*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_param_uint, OSSL_NELEM(raw_values));
701*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_param_ulong, OSSL_NELEM(raw_values));
702*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_param_int32, OSSL_NELEM(raw_values));
703*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_param_uint32, OSSL_NELEM(raw_values));
704*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_param_size_t, OSSL_NELEM(raw_values));
705*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_param_time_t, OSSL_NELEM(raw_values));
706*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_param_int64, OSSL_NELEM(raw_values));
707*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_param_uint64, OSSL_NELEM(raw_values));
708*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_param_bignum, OSSL_NELEM(raw_values));
709*e0c4386eSCy Schubert     ADD_TEST(test_param_real);
710*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_param_construct, 4);
711*e0c4386eSCy Schubert     ADD_TEST(test_param_modified);
712*e0c4386eSCy Schubert     ADD_TEST(test_param_copy_null);
713*e0c4386eSCy Schubert     return 1;
714*e0c4386eSCy Schubert }
715