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"
14 #include <openssl/params.h>
15 #include <openssl/bn.h>
17 /* The maximum size of the static buffers used to test most things */
20 static void swap_copy(unsigned char *out, const void *in, size_t len)
24 for (j = 0; j < len; j++)
25 out[j] = ((unsigned char *)in)[len - j - 1];
28 static void copy_to_le(unsigned char *out, const void *in, size_t len)
31 swap_copy(out, in, len);
37 static void copy_be_to_native(unsigned char *out, const void *in, size_t len)
42 swap_copy(out, in, len);
48 unsigned char value[MAX_LEN];
50 { 4, { 0x38, 0x27, 0xbf, 0x3b } },
51 { 4, { 0x9f, 0x26, 0x48, 0x22 } },
52 { 8, { 0x59, 0xb2, 0x1a, 0xe9, 0x2a, 0xd8, 0x46, 0x40 } },
53 { 8, { 0xb4, 0xae, 0xbd, 0xb4, 0xdd, 0x04, 0xb1, 0x4c } },
54 { 16, { 0x61, 0xe8, 0x7e, 0x31, 0xe9, 0x33, 0x83, 0x3d,
55 0x87, 0x99, 0xc7, 0xd8, 0x5d, 0xa9, 0x8b, 0x42 } },
56 { 16, { 0xee, 0x6e, 0x8b, 0xc3, 0xec, 0xcf, 0x37, 0xcc,
57 0x89, 0x67, 0xf2, 0x68, 0x33, 0xa0, 0x14, 0xb0 } },
60 static int test_param_type_extra(const OSSL_PARAM *param, unsigned char *cmp,
66 unsigned char buf[MAX_LEN];
67 const int bit32 = param->data_size == sizeof(int32_t);
68 const int sizet = bit32 && sizeof(size_t) > sizeof(int32_t);
69 const int signd = param->data_type == OSSL_PARAM_INTEGER;
72 if ((bit32 && !TEST_true(OSSL_PARAM_get_int32(param, &i32)))
73 || !TEST_true(OSSL_PARAM_get_int64(param, &i64)))
77 && !TEST_true(OSSL_PARAM_get_uint32(param, (uint32_t *)&i32)))
78 || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64))
79 || (sizet && !TEST_true(OSSL_PARAM_get_size_t(param, &s))))
83 /* Check signed types */
85 copy_to_le(buf, &i32, sizeof(i32));
86 sz = sizeof(i32) < width ? sizeof(i32) : width;
87 if (!TEST_mem_eq(buf, sz, cmp, sz))
90 copy_to_le(buf, &i64, sizeof(i64));
91 sz = sizeof(i64) < width ? sizeof(i64) : width;
92 if (!TEST_mem_eq(buf, sz, cmp, sz))
94 if (sizet && !signd) {
95 copy_to_le(buf, &s, sizeof(s));
96 sz = sizeof(s) < width ? sizeof(s) : width;
97 if (!TEST_mem_eq(buf, sz, cmp, sz))
101 /* Check a widening write if possible */
102 if (sizeof(size_t) > width) {
104 if (!TEST_true(OSSL_PARAM_set_int32(param, 12345))
105 || !TEST_true(OSSL_PARAM_get_int64(param, &i64))
106 || !TEST_size_t_eq((size_t)i64, 12345))
109 if (!TEST_true(OSSL_PARAM_set_uint32(param, 12345))
110 || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64))
111 || !TEST_size_t_eq((size_t)i64, 12345))
119 * The test cases for each of the bastic integral types are similar.
120 * For each type, a param of that type is set and an attempt to read it
121 * get is made. Finally, the above function is called to verify that
122 * the params can be read as other types.
124 * All the real work is done via byte buffers which are converted to machine
125 * byte order and to little endian for comparisons. Narrower values are best
126 * compared using little endian because their values and positions don't
130 static int test_param_int(int n)
133 unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(int)];
134 const size_t len = raw_values[n].len >= sizeof(int) ?
135 sizeof(int) : raw_values[n].len;
136 OSSL_PARAM param = OSSL_PARAM_int("a", NULL);
138 memset(buf, 0, sizeof(buf));
139 memset(le, 0, sizeof(le));
140 copy_be_to_native(buf, raw_values[n].value, len);
141 swap_copy(le, raw_values[n].value, len);
142 memcpy(&in, buf, sizeof(in));
144 if (!TEST_true(OSSL_PARAM_set_int(¶m, in)))
146 copy_to_le(cmp, &out, sizeof(out));
147 if (!TEST_mem_eq(cmp, len, le, len))
151 if (!TEST_true(OSSL_PARAM_get_int(¶m, &in)))
153 copy_to_le(cmp, &in, sizeof(in));
154 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
157 return test_param_type_extra(¶m, le, sizeof(int));
160 static int test_param_long(int n)
163 unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(long int)];
164 const size_t len = raw_values[n].len >= sizeof(long int)
165 ? sizeof(long int) : raw_values[n].len;
166 OSSL_PARAM param = OSSL_PARAM_long("a", NULL);
168 memset(buf, 0, sizeof(buf));
169 memset(le, 0, sizeof(le));
170 copy_be_to_native(buf, raw_values[n].value, len);
171 swap_copy(le, raw_values[n].value, len);
172 memcpy(&in, buf, sizeof(in));
174 if (!TEST_true(OSSL_PARAM_set_long(¶m, in)))
176 copy_to_le(cmp, &out, sizeof(out));
177 if (!TEST_mem_eq(cmp, len, le, len))
181 if (!TEST_true(OSSL_PARAM_get_long(¶m, &in)))
183 copy_to_le(cmp, &in, sizeof(in));
184 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
187 return test_param_type_extra(¶m, le, sizeof(long int));
190 static int test_param_uint(int n)
192 unsigned int in, out;
193 unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(unsigned int)];
194 const size_t len = raw_values[n].len >= sizeof(unsigned int) ? sizeof(unsigned int) : raw_values[n].len;
195 OSSL_PARAM param = OSSL_PARAM_uint("a", NULL);
196 memset(buf, 0, sizeof(buf));
197 memset(le, 0, sizeof(le));
198 copy_be_to_native(buf, raw_values[n].value, len);
199 swap_copy(le, raw_values[n].value, len);
200 memcpy(&in, buf, sizeof(in));
202 if (!TEST_true(OSSL_PARAM_set_uint(¶m, in)))
204 copy_to_le(cmp, &out, sizeof(out));
205 if (!TEST_mem_eq(cmp, len, le, len))
209 if (!TEST_true(OSSL_PARAM_get_uint(¶m, &in)))
211 copy_to_le(cmp, &in, sizeof(in));
212 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
215 return test_param_type_extra(¶m, le, sizeof(unsigned int));
218 static int test_param_ulong(int n)
220 unsigned long int in, out;
221 unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(unsigned long int)];
222 const size_t len = raw_values[n].len >= sizeof(unsigned long int)
223 ? sizeof(unsigned long int) : raw_values[n].len;
224 OSSL_PARAM param = OSSL_PARAM_ulong("a", NULL);
225 memset(buf, 0, sizeof(buf));
226 memset(le, 0, sizeof(le));
227 copy_be_to_native(buf, raw_values[n].value, len);
228 swap_copy(le, raw_values[n].value, len);
229 memcpy(&in, buf, sizeof(in));
231 if (!TEST_true(OSSL_PARAM_set_ulong(¶m, in)))
233 copy_to_le(cmp, &out, sizeof(out));
234 if (!TEST_mem_eq(cmp, len, le, len))
238 if (!TEST_true(OSSL_PARAM_get_ulong(¶m, &in)))
240 copy_to_le(cmp, &in, sizeof(in));
241 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
244 return test_param_type_extra(¶m, le, sizeof(unsigned long int));
247 static int test_param_int32(int n)
250 unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(int32_t)];
251 const size_t len = raw_values[n].len >= sizeof(int32_t)
252 ? sizeof(int32_t) : raw_values[n].len;
253 OSSL_PARAM param = OSSL_PARAM_int32("a", NULL);
254 memset(buf, 0, sizeof(buf));
255 memset(le, 0, sizeof(le));
256 copy_be_to_native(buf, raw_values[n].value, len);
257 swap_copy(le, raw_values[n].value, len);
258 memcpy(&in, buf, sizeof(in));
260 if (!TEST_true(OSSL_PARAM_set_int32(¶m, in)))
262 copy_to_le(cmp, &out, sizeof(out));
263 if (!TEST_mem_eq(cmp, len, le, len))
267 if (!TEST_true(OSSL_PARAM_get_int32(¶m, &in)))
269 copy_to_le(cmp, &in, sizeof(in));
270 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
273 return test_param_type_extra(¶m, le, sizeof(int32_t));
276 static int test_param_uint32(int n)
279 unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(uint32_t)];
280 const size_t len = raw_values[n].len >= sizeof(uint32_t)
281 ? sizeof(uint32_t) : raw_values[n].len;
282 OSSL_PARAM param = OSSL_PARAM_uint32("a", NULL);
283 memset(buf, 0, sizeof(buf));
284 memset(le, 0, sizeof(le));
285 copy_be_to_native(buf, raw_values[n].value, len);
286 swap_copy(le, raw_values[n].value, len);
287 memcpy(&in, buf, sizeof(in));
289 if (!TEST_true(OSSL_PARAM_set_uint32(¶m, in)))
291 copy_to_le(cmp, &out, sizeof(out));
292 if (!TEST_mem_eq(cmp, len, le, len))
296 if (!TEST_true(OSSL_PARAM_get_uint32(¶m, &in)))
298 copy_to_le(cmp, &in, sizeof(in));
299 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
302 return test_param_type_extra(¶m, le, sizeof(uint32_t));
305 static int test_param_int64(int n)
308 unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(int64_t)];
309 const size_t len = raw_values[n].len >= sizeof(int64_t)
310 ? sizeof(int64_t) : raw_values[n].len;
311 OSSL_PARAM param = OSSL_PARAM_int64("a", NULL);
312 memset(buf, 0, sizeof(buf));
313 memset(le, 0, sizeof(le));
314 copy_be_to_native(buf, raw_values[n].value, len);
315 swap_copy(le, raw_values[n].value, len);
316 memcpy(&in, buf, sizeof(in));
318 if (!TEST_true(OSSL_PARAM_set_int64(¶m, in)))
320 copy_to_le(cmp, &out, sizeof(out));
321 if (!TEST_mem_eq(cmp, len, le, len))
325 if (!TEST_true(OSSL_PARAM_get_int64(¶m, &in)))
327 copy_to_le(cmp, &in, sizeof(in));
328 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
331 return test_param_type_extra(¶m, le, sizeof(int64_t));
334 static int test_param_uint64(int n)
337 unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(uint64_t)];
338 const size_t len = raw_values[n].len >= sizeof(uint64_t)
339 ? sizeof(uint64_t) : raw_values[n].len;
340 OSSL_PARAM param = OSSL_PARAM_uint64("a", NULL);
341 memset(buf, 0, sizeof(buf));
342 memset(le, 0, sizeof(le));
343 copy_be_to_native(buf, raw_values[n].value, len);
344 swap_copy(le, raw_values[n].value, len);
345 memcpy(&in, buf, sizeof(in));
347 if (!TEST_true(OSSL_PARAM_set_uint64(¶m, in)))
349 copy_to_le(cmp, &out, sizeof(out));
350 if (!TEST_mem_eq(cmp, len, le, len))
354 if (!TEST_true(OSSL_PARAM_get_uint64(¶m, &in)))
356 copy_to_le(cmp, &in, sizeof(in));
357 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
360 return test_param_type_extra(¶m, le, sizeof(uint64_t));
363 static int test_param_size_t(int n)
366 unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(size_t)];
367 const size_t len = raw_values[n].len >= sizeof(size_t)
368 ? sizeof(size_t) : raw_values[n].len;
369 OSSL_PARAM param = OSSL_PARAM_size_t("a", NULL);
370 memset(buf, 0, sizeof(buf));
371 memset(le, 0, sizeof(le));
372 copy_be_to_native(buf, raw_values[n].value, len);
373 swap_copy(le, raw_values[n].value, len);
374 memcpy(&in, buf, sizeof(in));
376 if (!TEST_true(OSSL_PARAM_set_size_t(¶m, in)))
378 copy_to_le(cmp, &out, sizeof(out));
379 if (!TEST_mem_eq(cmp, len, le, len))
383 if (!TEST_true(OSSL_PARAM_get_size_t(¶m, &in)))
385 copy_to_le(cmp, &in, sizeof(in));
386 if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
389 return test_param_type_extra(¶m, le, sizeof(size_t));
392 static int test_param_bignum(int n)
394 unsigned char buf[MAX_LEN], bnbuf[MAX_LEN], le[MAX_LEN];
395 const size_t len = raw_values[n].len;
397 BIGNUM *b = NULL, *c = NULL;
398 OSSL_PARAM param = OSSL_PARAM_DEFN("bn", OSSL_PARAM_UNSIGNED_INTEGER,
403 param.data_size = len;
404 param.return_size = &bnsize;
406 copy_be_to_native(buf, raw_values[n].value, len);
407 swap_copy(le, raw_values[n].value, len);
408 if (!TEST_ptr(b = BN_bin2bn(raw_values[n].value, (int)len, NULL)))
411 if (!TEST_true(OSSL_PARAM_set_BN(¶m, b))
412 || !TEST_mem_eq(bnbuf, bnsize, buf, bnsize))
414 param.data_size = *param.return_size;
415 if (!TEST_true(OSSL_PARAM_get_BN(¶m, &c))
416 || !TEST_BN_eq(b, c))
426 static int test_param_real(void)
429 OSSL_PARAM param = OSSL_PARAM_double("r", NULL);
432 return TEST_true(OSSL_PARAM_set_double(¶m, 3.14159))
433 && TEST_double_eq(p, 3.14159);
437 * The tests are a bit special in that they are trying to do both sides
438 * of the param passing. This means that the OSSL_PARAM structure needs to
439 * be updated so that a get call matches size with the corresponding set call.
440 * This is not a problem in normal usage because the owner of the OSSL_PARAM
441 * "knows" the size of what it wants to put in and gets the size back via the
442 * return_size pointer when it needs to get data out. That is, the owner
443 * does not need to call these APIs since it has direct access.
445 * The result is that the tests need the locate call to return a non-const
446 * pointer at times. Hence the cast here.
448 static OSSL_PARAM *locate(OSSL_PARAM *p, const char *name)
450 return (OSSL_PARAM *)OSSL_PARAM_locate(p, name);
453 static int test_param_construct(void)
455 static const char *int_names[] = {
456 "int", "long", "int32", "int64"
458 static const char *uint_names[] = {
459 "uint", "ulong", "uint32", "uint64", "size_t"
461 static const unsigned char bn_val[16] = {
462 0xac, 0x75, 0x22, 0x7d, 0x81, 0x06, 0x7a, 0x23,
463 0xa6, 0xed, 0x87, 0xc7, 0xab, 0xf4, 0x73, 0x22
465 OSSL_PARAM params[20];
466 char buf[100], buf2[100], *bufp, *bufp2;
467 unsigned char ubuf[100];
468 void *vp, *vpn = NULL, *vp2;
470 const OSSL_PARAM *cp;
471 static const OSSL_PARAM pend = OSSL_PARAM_END;
472 int i, n = 0, ret = 0;
475 unsigned long int ul;
482 BIGNUM *bn = NULL, *bn2 = NULL;
484 params[n++] = OSSL_PARAM_construct_int("int", &i, &sz);
485 params[n++] = OSSL_PARAM_construct_uint("uint", &u, &sz);
486 params[n++] = OSSL_PARAM_construct_long("long", &l, &sz);
487 params[n++] = OSSL_PARAM_construct_ulong("ulong", &ul, &sz);
488 params[n++] = OSSL_PARAM_construct_int32("int32", &i32, &sz);
489 params[n++] = OSSL_PARAM_construct_int64("int64", &i64, &sz);
490 params[n++] = OSSL_PARAM_construct_uint32("uint32", &u32, &sz);
491 params[n++] = OSSL_PARAM_construct_uint64("uint64", &u64, &sz);
492 params[n++] = OSSL_PARAM_construct_size_t("size_t", &s, &sz);
493 params[n++] = OSSL_PARAM_construct_double("double", &d, &sz);
494 params[n++] = OSSL_PARAM_construct_BN("bignum", ubuf, sizeof(ubuf), &sz);
495 params[n++] = OSSL_PARAM_construct_utf8_string("utf8str", buf, sizeof(buf),
497 params[n++] = OSSL_PARAM_construct_octet_string("octstr", buf, sizeof(buf),
499 params[n++] = OSSL_PARAM_construct_utf8_ptr("utf8ptr", &bufp, &sz);
500 params[n++] = OSSL_PARAM_construct_octet_ptr("octptr", &vp, &sz);
504 if (!TEST_ptr_null(OSSL_PARAM_locate(params, "fnord")))
507 /* All signed integral types */
508 for (j = 0; j < OSSL_NELEM(int_names); j++) {
509 if (!TEST_ptr(cp = OSSL_PARAM_locate(params, int_names[j]))
510 || !TEST_true(OSSL_PARAM_set_int32(cp, (int32_t)(3 + j)))
511 || !TEST_true(OSSL_PARAM_get_int64(cp, &i64))
512 || !TEST_size_t_eq(cp->data_size, sz)
513 || !TEST_size_t_eq((size_t)i64, 3 + j)) {
514 TEST_note("iteration %zu var %s", j + 1, int_names[j]);
518 /* All unsigned integral types */
519 for (j = 0; j < OSSL_NELEM(uint_names); j++) {
520 if (!TEST_ptr(cp = OSSL_PARAM_locate(params, uint_names[j]))
521 || !TEST_true(OSSL_PARAM_set_uint32(cp, (uint32_t)(3 + j)))
522 || !TEST_true(OSSL_PARAM_get_uint64(cp, &u64))
523 || !TEST_size_t_eq(cp->data_size, sz)
524 || !TEST_size_t_eq((size_t)u64, 3 + j)) {
525 TEST_note("iteration %zu var %s", j + 1, uint_names[j]);
530 if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "double"))
531 || !TEST_true(OSSL_PARAM_set_double(cp, 3.14))
532 || !TEST_true(OSSL_PARAM_get_double(cp, &d2))
533 || !TEST_size_t_eq(sz, sizeof(double))
534 || !TEST_double_eq(d, d2))
538 if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "utf8str"))
539 || !TEST_true(OSSL_PARAM_set_utf8_string(cp, "abcdef"))
540 || !TEST_size_t_eq(sz, sizeof("abcdef"))
541 || !TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, 0))
542 || !TEST_str_eq(bufp, "abcdef"))
546 if (!TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, sizeof(buf2)))
547 || !TEST_str_eq(buf2, "abcdef"))
552 if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "utf8ptr"))
553 || !TEST_true(OSSL_PARAM_set_utf8_ptr(cp, "tuvwxyz"))
554 || !TEST_size_t_eq(sz, sizeof("tuvwxyz"))
555 || !TEST_str_eq(bufp, "tuvwxyz")
556 || !TEST_true(OSSL_PARAM_get_utf8_ptr(cp, (const char **)&bufp2))
557 || !TEST_ptr_eq(bufp2, bufp))
560 if (!TEST_ptr(p = locate(params, "octstr"))
561 || !TEST_true(OSSL_PARAM_set_octet_string(p, "abcdefghi",
562 sizeof("abcdefghi")))
563 || !TEST_size_t_eq(sz, sizeof("abcdefghi")))
565 /* Match the return size to avoid trailing garbage bytes */
566 p->data_size = *p->return_size;
567 if (!TEST_true(OSSL_PARAM_get_octet_string(p, &vpn, 0, &s))
568 || !TEST_size_t_eq(s, sizeof("abcdefghi"))
569 || !TEST_mem_eq(vpn, sizeof("abcdefghi"),
570 "abcdefghi", sizeof("abcdefghi")))
573 if (!TEST_true(OSSL_PARAM_get_octet_string(p, &vp, sizeof(buf2), &s))
574 || !TEST_size_t_eq(s, sizeof("abcdefghi"))
575 || !TEST_mem_eq(vp, sizeof("abcdefghi"),
576 "abcdefghi", sizeof("abcdefghi")))
581 if (!TEST_ptr(p = locate(params, "octptr"))
582 || !TEST_true(OSSL_PARAM_set_octet_ptr(p, &ul, sizeof(ul)))
583 || !TEST_size_t_eq(sz, sizeof(ul))
584 || !TEST_ptr_eq(vp, &ul))
586 /* Match the return size to avoid trailing garbage bytes */
587 p->data_size = *p->return_size;
588 if (!TEST_true(OSSL_PARAM_get_octet_ptr(p, (const void **)&vp2, &k))
589 || !TEST_size_t_eq(k, sizeof(ul))
590 || !TEST_ptr_eq(vp2, vp))
593 if (!TEST_ptr(p = locate(params, "bignum"))
594 || !TEST_ptr(bn = BN_lebin2bn(bn_val, (int)sizeof(bn_val), NULL))
595 || !TEST_true(OSSL_PARAM_set_BN(p, bn))
596 || !TEST_size_t_eq(sz, sizeof(bn_val)))
598 /* Match the return size to avoid trailing garbage bytes */
599 p->data_size = *p->return_size;
600 if(!TEST_true(OSSL_PARAM_get_BN(p, &bn2))
601 || !TEST_BN_eq(bn, bn2))
611 int setup_tests(void)
613 ADD_ALL_TESTS(test_param_int, OSSL_NELEM(raw_values));
614 ADD_ALL_TESTS(test_param_long, OSSL_NELEM(raw_values));
615 ADD_ALL_TESTS(test_param_uint, OSSL_NELEM(raw_values));
616 ADD_ALL_TESTS(test_param_ulong, OSSL_NELEM(raw_values));
617 ADD_ALL_TESTS(test_param_int32, OSSL_NELEM(raw_values));
618 ADD_ALL_TESTS(test_param_uint32, OSSL_NELEM(raw_values));
619 ADD_ALL_TESTS(test_param_size_t, OSSL_NELEM(raw_values));
620 ADD_ALL_TESTS(test_param_int64, OSSL_NELEM(raw_values));
621 ADD_ALL_TESTS(test_param_uint64, OSSL_NELEM(raw_values));
622 ADD_ALL_TESTS(test_param_bignum, OSSL_NELEM(raw_values));
623 ADD_TEST(test_param_real);
624 ADD_TEST(test_param_construct);