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