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