2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
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
13 #include "internal/nelem.h"
15 #include <openssl/params.h>
16 #include <openssl/bn.h>
18 /* The maximum size of the static buffers used to test most things */
21 static void swap_copy(unsigned char *out, const void *in, size_t len)
25 for (j = 0; j < len; j++)
26 out[j] = ((unsigned char *)in)[len - j - 1];
29 static void copy_to_le(unsigned char *out, const void *in, size_t len)
36 swap_copy(out, in, len);
39 static void copy_be_to_native(unsigned char *out, const void *in, size_t len)
44 swap_copy(out, in, len);
51 unsigned char value[MAX_LEN];
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 } },
63 static int test_param_type_extra(const OSSL_PARAM *param, unsigned char *cmp,
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;
75 if ((bit32 && !TEST_true(OSSL_PARAM_get_int32(param, &i32)))
76 || !TEST_true(OSSL_PARAM_get_int64(param, &i64)))
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))))
86 /* Check signed types */
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))
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))
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))
104 /* Check a widening write if possible */
105 if (sizeof(size_t) > width) {
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))
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))
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.
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
133 static int test_param_int(int n)
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);
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));
147 if (!TEST_true(OSSL_PARAM_set_int(¶m, in)))
149 copy_to_le(cmp, &out, sizeof(out));
150 if (!TEST_mem_eq(cmp, len, le, len))
154 if (!TEST_true(OSSL_PARAM_get_int(¶m, &in)))
156 copy_to_le(cmp, &in, sizeof(in));
157 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
160 return test_param_type_extra(¶m, le, sizeof(int));
163 static int test_param_long(int n)
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);
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));
177 if (!TEST_true(OSSL_PARAM_set_long(¶m, in)))
179 copy_to_le(cmp, &out, sizeof(out));
180 if (!TEST_mem_eq(cmp, len, le, len))
184 if (!TEST_true(OSSL_PARAM_get_long(¶m, &in)))
186 copy_to_le(cmp, &in, sizeof(in));
187 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
190 return test_param_type_extra(¶m, le, sizeof(long int));
193 static int test_param_uint(int n)
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));
205 if (!TEST_true(OSSL_PARAM_set_uint(¶m, in)))
207 copy_to_le(cmp, &out, sizeof(out));
208 if (!TEST_mem_eq(cmp, len, le, len))
212 if (!TEST_true(OSSL_PARAM_get_uint(¶m, &in)))
214 copy_to_le(cmp, &in, sizeof(in));
215 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
218 return test_param_type_extra(¶m, le, sizeof(unsigned int));
221 static int test_param_ulong(int n)
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));
234 if (!TEST_true(OSSL_PARAM_set_ulong(¶m, in)))
236 copy_to_le(cmp, &out, sizeof(out));
237 if (!TEST_mem_eq(cmp, len, le, len))
241 if (!TEST_true(OSSL_PARAM_get_ulong(¶m, &in)))
243 copy_to_le(cmp, &in, sizeof(in));
244 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
247 return test_param_type_extra(¶m, le, sizeof(unsigned long int));
250 static int test_param_int32(int n)
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));
263 if (!TEST_true(OSSL_PARAM_set_int32(¶m, in)))
265 copy_to_le(cmp, &out, sizeof(out));
266 if (!TEST_mem_eq(cmp, len, le, len))
270 if (!TEST_true(OSSL_PARAM_get_int32(¶m, &in)))
272 copy_to_le(cmp, &in, sizeof(in));
273 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
276 return test_param_type_extra(¶m, le, sizeof(int32_t));
279 static int test_param_uint32(int n)
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));
292 if (!TEST_true(OSSL_PARAM_set_uint32(¶m, in)))
294 copy_to_le(cmp, &out, sizeof(out));
295 if (!TEST_mem_eq(cmp, len, le, len))
299 if (!TEST_true(OSSL_PARAM_get_uint32(¶m, &in)))
301 copy_to_le(cmp, &in, sizeof(in));
302 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
305 return test_param_type_extra(¶m, le, sizeof(uint32_t));
308 static int test_param_int64(int n)
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));
321 if (!TEST_true(OSSL_PARAM_set_int64(¶m, in)))
323 copy_to_le(cmp, &out, sizeof(out));
324 if (!TEST_mem_eq(cmp, len, le, len))
328 if (!TEST_true(OSSL_PARAM_get_int64(¶m, &in)))
330 copy_to_le(cmp, &in, sizeof(in));
331 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
334 return test_param_type_extra(¶m, le, sizeof(int64_t));
337 static int test_param_uint64(int n)
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));
350 if (!TEST_true(OSSL_PARAM_set_uint64(¶m, in)))
352 copy_to_le(cmp, &out, sizeof(out));
353 if (!TEST_mem_eq(cmp, len, le, len))
357 if (!TEST_true(OSSL_PARAM_get_uint64(¶m, &in)))
359 copy_to_le(cmp, &in, sizeof(in));
360 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
363 return test_param_type_extra(¶m, le, sizeof(uint64_t));
366 static int test_param_size_t(int n)
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));
379 if (!TEST_true(OSSL_PARAM_set_size_t(¶m, in)))
381 copy_to_le(cmp, &out, sizeof(out));
382 if (!TEST_mem_eq(cmp, len, le, len))
386 if (!TEST_true(OSSL_PARAM_get_size_t(¶m, &in)))
388 copy_to_le(cmp, &in, sizeof(in));
389 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
392 return test_param_type_extra(¶m, le, sizeof(size_t));
395 static int test_param_bignum(int n)
397 unsigned char buf[MAX_LEN], bnbuf[MAX_LEN], le[MAX_LEN];
398 const size_t len = raw_values[n].len;
400 BIGNUM *b = NULL, *c = NULL;
401 OSSL_PARAM param = OSSL_PARAM_DEFN("bn", OSSL_PARAM_UNSIGNED_INTEGER,
406 param.data_size = len;
407 param.return_size = &bnsize;
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)))
414 if (!TEST_true(OSSL_PARAM_set_BN(¶m, b))
415 || !TEST_mem_eq(bnbuf, bnsize, buf, bnsize))
417 param.data_size = *param.return_size;
418 if (!TEST_true(OSSL_PARAM_get_BN(¶m, &c))
419 || !TEST_BN_eq(b, c))
429 static int test_param_real(void)
432 OSSL_PARAM param = OSSL_PARAM_double("r", NULL);
435 return TEST_true(OSSL_PARAM_set_double(¶m, 3.14159))
436 && TEST_double_eq(p, 3.14159);
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.
448 * The result is that the tests need the locate call to return a non-const
449 * pointer at times. Hence the cast here.
451 static OSSL_PARAM *locate(OSSL_PARAM *p, const char *name)
453 return (OSSL_PARAM *)OSSL_PARAM_locate(p, name);
456 static int test_param_construct(void)
458 static const char *int_names[] = {
459 "int", "long", "int32", "int64"
461 static const char *uint_names[] = {
462 "uint", "ulong", "uint32", "uint64", "size_t"
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
468 OSSL_PARAM params[20];
469 char buf[100], buf2[100], *bufp, *bufp2;
470 unsigned char ubuf[100];
471 void *vp, *vpn = NULL, *vp2;
473 const OSSL_PARAM *cp;
474 static const OSSL_PARAM pend = OSSL_PARAM_END;
475 int i, n = 0, ret = 0;
478 unsigned long int ul;
485 BIGNUM *bn = NULL, *bn2 = NULL;
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),
500 params[n++] = OSSL_PARAM_construct_octet_string("octstr", buf, sizeof(buf),
502 params[n++] = OSSL_PARAM_construct_utf8_ptr("utf8ptr", &bufp, &sz);
503 params[n++] = OSSL_PARAM_construct_octet_ptr("octptr", &vp, &sz);
507 if (!TEST_ptr_null(OSSL_PARAM_locate(params, "fnord")))
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]);
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]);
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))
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"))
549 if (!TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, sizeof(buf2)))
550 || !TEST_str_eq(buf2, "abcdef"))
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))
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")))
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")))
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")))
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))
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))
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)))
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))
614 int setup_tests(void)
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);