]>
Commit | Line | Data |
---|---|---|
7ffbd7ca | 1 | /* |
33388b44 | 2 | * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved. |
7ffbd7ca P |
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" | |
a9e34e9e | 14 | #include "ossl_test_endian.h" |
7ffbd7ca P |
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 | ||
6ce84e64 P |
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) | |
7ffbd7ca | 38 | { |
045162e5 P |
39 | DECLARE_IS_ENDIAN; |
40 | ||
41 | if (IS_LITTLE_ENDIAN) | |
42 | memcpy(out, in, len); | |
43 | else | |
44 | swap_copy(out, in, len); | |
7ffbd7ca P |
45 | } |
46 | ||
7ffbd7ca P |
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 | ||
4e7991b4 P |
61 | static int test_param_type_extra(OSSL_PARAM *param, const unsigned char *cmp, |
62 | size_t width) | |
7ffbd7ca P |
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 | ||
8d5fb648 | 72 | OSSL_PARAM_set_unmodified(param); |
7ffbd7ca P |
73 | if (signd) { |
74 | if ((bit32 && !TEST_true(OSSL_PARAM_get_int32(param, &i32))) | |
75 | || !TEST_true(OSSL_PARAM_get_int64(param, &i64))) | |
76 | return 0; | |
77 | } else { | |
78 | if ((bit32 | |
79 | && !TEST_true(OSSL_PARAM_get_uint32(param, (uint32_t *)&i32))) | |
80 | || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64)) | |
81 | || (sizet && !TEST_true(OSSL_PARAM_get_size_t(param, &s)))) | |
82 | return 0; | |
83 | } | |
8d5fb648 P |
84 | if (!TEST_false(OSSL_PARAM_modified(param))) |
85 | return 0; | |
7ffbd7ca P |
86 | |
87 | /* Check signed types */ | |
88 | if (bit32) { | |
6ce84e64 | 89 | le_copy(buf, &i32, sizeof(i32)); |
7ffbd7ca P |
90 | sz = sizeof(i32) < width ? sizeof(i32) : width; |
91 | if (!TEST_mem_eq(buf, sz, cmp, sz)) | |
92 | return 0; | |
93 | } | |
6ce84e64 | 94 | le_copy(buf, &i64, sizeof(i64)); |
7ffbd7ca P |
95 | sz = sizeof(i64) < width ? sizeof(i64) : width; |
96 | if (!TEST_mem_eq(buf, sz, cmp, sz)) | |
97 | return 0; | |
98 | if (sizet && !signd) { | |
6ce84e64 | 99 | le_copy(buf, &s, sizeof(s)); |
7ffbd7ca P |
100 | sz = sizeof(s) < width ? sizeof(s) : width; |
101 | if (!TEST_mem_eq(buf, sz, cmp, sz)) | |
102 | return 0; | |
103 | } | |
104 | ||
105 | /* Check a widening write if possible */ | |
106 | if (sizeof(size_t) > width) { | |
107 | if (signd) { | |
108 | if (!TEST_true(OSSL_PARAM_set_int32(param, 12345)) | |
109 | || !TEST_true(OSSL_PARAM_get_int64(param, &i64)) | |
f7f2a55a | 110 | || !TEST_size_t_eq((size_t)i64, 12345)) |
7ffbd7ca P |
111 | return 0; |
112 | } else { | |
113 | if (!TEST_true(OSSL_PARAM_set_uint32(param, 12345)) | |
114 | || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64)) | |
f7f2a55a | 115 | || !TEST_size_t_eq((size_t)i64, 12345)) |
7ffbd7ca P |
116 | return 0; |
117 | } | |
8d5fb648 P |
118 | if (!TEST_true(OSSL_PARAM_modified(param))) |
119 | return 0; | |
7ffbd7ca P |
120 | } |
121 | return 1; | |
122 | } | |
123 | ||
124 | /* | |
125 | * The test cases for each of the bastic integral types are similar. | |
126 | * For each type, a param of that type is set and an attempt to read it | |
127 | * get is made. Finally, the above function is called to verify that | |
128 | * the params can be read as other types. | |
129 | * | |
130 | * All the real work is done via byte buffers which are converted to machine | |
131 | * byte order and to little endian for comparisons. Narrower values are best | |
132 | * compared using little endian because their values and positions don't | |
133 | * change. | |
134 | */ | |
135 | ||
136 | static int test_param_int(int n) | |
137 | { | |
138 | int in, out; | |
6ce84e64 | 139 | unsigned char buf[MAX_LEN], cmp[sizeof(int)]; |
7ffbd7ca P |
140 | const size_t len = raw_values[n].len >= sizeof(int) ? |
141 | sizeof(int) : raw_values[n].len; | |
142 | OSSL_PARAM param = OSSL_PARAM_int("a", NULL); | |
143 | ||
144 | memset(buf, 0, sizeof(buf)); | |
6ce84e64 | 145 | le_copy(buf, raw_values[n].value, sizeof(in)); |
7ffbd7ca P |
146 | memcpy(&in, buf, sizeof(in)); |
147 | param.data = &out; | |
148 | if (!TEST_true(OSSL_PARAM_set_int(¶m, in))) | |
149 | return 0; | |
6ce84e64 P |
150 | le_copy(cmp, &out, sizeof(out)); |
151 | if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) | |
7ffbd7ca P |
152 | return 0; |
153 | in = 0; | |
7ffbd7ca P |
154 | if (!TEST_true(OSSL_PARAM_get_int(¶m, &in))) |
155 | return 0; | |
6ce84e64 P |
156 | le_copy(cmp, &in, sizeof(in)); |
157 | if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) | |
7ffbd7ca P |
158 | return 0; |
159 | param.data = &out; | |
6ce84e64 | 160 | return test_param_type_extra(¶m, raw_values[n].value, sizeof(int)); |
7ffbd7ca P |
161 | } |
162 | ||
163 | static int test_param_long(int n) | |
164 | { | |
165 | long int in, out; | |
6ce84e64 | 166 | unsigned char buf[MAX_LEN], cmp[sizeof(long int)]; |
7ffbd7ca P |
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)); | |
6ce84e64 | 172 | le_copy(buf, raw_values[n].value, sizeof(in)); |
7ffbd7ca P |
173 | memcpy(&in, buf, sizeof(in)); |
174 | param.data = &out; | |
175 | if (!TEST_true(OSSL_PARAM_set_long(¶m, in))) | |
176 | return 0; | |
6ce84e64 P |
177 | le_copy(cmp, &out, sizeof(out)); |
178 | if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) | |
7ffbd7ca P |
179 | return 0; |
180 | in = 0; | |
7ffbd7ca P |
181 | if (!TEST_true(OSSL_PARAM_get_long(¶m, &in))) |
182 | return 0; | |
6ce84e64 P |
183 | le_copy(cmp, &in, sizeof(in)); |
184 | if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) | |
7ffbd7ca P |
185 | return 0; |
186 | param.data = &out; | |
6ce84e64 | 187 | return test_param_type_extra(¶m, raw_values[n].value, sizeof(long int)); |
7ffbd7ca P |
188 | } |
189 | ||
190 | static int test_param_uint(int n) | |
191 | { | |
192 | unsigned int in, out; | |
6ce84e64 | 193 | unsigned char buf[MAX_LEN], cmp[sizeof(unsigned int)]; |
7ffbd7ca P |
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); | |
6ce84e64 | 196 | |
7ffbd7ca | 197 | memset(buf, 0, sizeof(buf)); |
6ce84e64 | 198 | le_copy(buf, raw_values[n].value, sizeof(in)); |
7ffbd7ca P |
199 | memcpy(&in, buf, sizeof(in)); |
200 | param.data = &out; | |
201 | if (!TEST_true(OSSL_PARAM_set_uint(¶m, in))) | |
202 | return 0; | |
6ce84e64 P |
203 | le_copy(cmp, &out, sizeof(out)); |
204 | if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) | |
7ffbd7ca P |
205 | return 0; |
206 | in = 0; | |
7ffbd7ca P |
207 | if (!TEST_true(OSSL_PARAM_get_uint(¶m, &in))) |
208 | return 0; | |
6ce84e64 P |
209 | le_copy(cmp, &in, sizeof(in)); |
210 | if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) | |
7ffbd7ca P |
211 | return 0; |
212 | param.data = &out; | |
6ce84e64 | 213 | return test_param_type_extra(¶m, raw_values[n].value, sizeof(unsigned int)); |
7ffbd7ca P |
214 | } |
215 | ||
216 | static int test_param_ulong(int n) | |
217 | { | |
218 | unsigned long int in, out; | |
6ce84e64 | 219 | unsigned char buf[MAX_LEN], cmp[sizeof(unsigned long int)]; |
7ffbd7ca P |
220 | const size_t len = raw_values[n].len >= sizeof(unsigned long int) |
221 | ? sizeof(unsigned long int) : raw_values[n].len; | |
222 | OSSL_PARAM param = OSSL_PARAM_ulong("a", NULL); | |
6ce84e64 | 223 | |
7ffbd7ca | 224 | memset(buf, 0, sizeof(buf)); |
6ce84e64 | 225 | le_copy(buf, raw_values[n].value, sizeof(in)); |
7ffbd7ca P |
226 | memcpy(&in, buf, sizeof(in)); |
227 | param.data = &out; | |
228 | if (!TEST_true(OSSL_PARAM_set_ulong(¶m, in))) | |
229 | return 0; | |
6ce84e64 P |
230 | le_copy(cmp, &out, sizeof(out)); |
231 | if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) | |
7ffbd7ca P |
232 | return 0; |
233 | in = 0; | |
7ffbd7ca P |
234 | if (!TEST_true(OSSL_PARAM_get_ulong(¶m, &in))) |
235 | return 0; | |
6ce84e64 P |
236 | le_copy(cmp, &in, sizeof(in)); |
237 | if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) | |
7ffbd7ca P |
238 | return 0; |
239 | param.data = &out; | |
6ce84e64 | 240 | return test_param_type_extra(¶m, raw_values[n].value, sizeof(unsigned long int)); |
7ffbd7ca P |
241 | } |
242 | ||
243 | static int test_param_int32(int n) | |
244 | { | |
245 | int32_t in, out; | |
6ce84e64 | 246 | unsigned char buf[MAX_LEN], cmp[sizeof(int32_t)]; |
7ffbd7ca P |
247 | const size_t len = raw_values[n].len >= sizeof(int32_t) |
248 | ? sizeof(int32_t) : raw_values[n].len; | |
249 | OSSL_PARAM param = OSSL_PARAM_int32("a", NULL); | |
6ce84e64 | 250 | |
7ffbd7ca | 251 | memset(buf, 0, sizeof(buf)); |
6ce84e64 | 252 | le_copy(buf, raw_values[n].value, sizeof(in)); |
7ffbd7ca P |
253 | memcpy(&in, buf, sizeof(in)); |
254 | param.data = &out; | |
255 | if (!TEST_true(OSSL_PARAM_set_int32(¶m, in))) | |
256 | return 0; | |
6ce84e64 P |
257 | le_copy(cmp, &out, sizeof(out)); |
258 | if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) | |
7ffbd7ca P |
259 | return 0; |
260 | in = 0; | |
7ffbd7ca P |
261 | if (!TEST_true(OSSL_PARAM_get_int32(¶m, &in))) |
262 | return 0; | |
6ce84e64 P |
263 | le_copy(cmp, &in, sizeof(in)); |
264 | if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) | |
7ffbd7ca P |
265 | return 0; |
266 | param.data = &out; | |
6ce84e64 | 267 | return test_param_type_extra(¶m, raw_values[n].value, sizeof(int32_t)); |
7ffbd7ca P |
268 | } |
269 | ||
270 | static int test_param_uint32(int n) | |
271 | { | |
272 | uint32_t in, out; | |
6ce84e64 | 273 | unsigned char buf[MAX_LEN], cmp[sizeof(uint32_t)]; |
7ffbd7ca P |
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); | |
6ce84e64 | 277 | |
7ffbd7ca | 278 | memset(buf, 0, sizeof(buf)); |
6ce84e64 | 279 | le_copy(buf, raw_values[n].value, sizeof(in)); |
7ffbd7ca P |
280 | memcpy(&in, buf, sizeof(in)); |
281 | param.data = &out; | |
282 | if (!TEST_true(OSSL_PARAM_set_uint32(¶m, in))) | |
283 | return 0; | |
6ce84e64 P |
284 | le_copy(cmp, &out, sizeof(out)); |
285 | if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) | |
7ffbd7ca P |
286 | return 0; |
287 | in = 0; | |
7ffbd7ca P |
288 | if (!TEST_true(OSSL_PARAM_get_uint32(¶m, &in))) |
289 | return 0; | |
6ce84e64 P |
290 | le_copy(cmp, &in, sizeof(in)); |
291 | if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) | |
7ffbd7ca P |
292 | return 0; |
293 | param.data = &out; | |
6ce84e64 | 294 | return test_param_type_extra(¶m, raw_values[n].value, sizeof(uint32_t)); |
7ffbd7ca P |
295 | } |
296 | ||
297 | static int test_param_int64(int n) | |
298 | { | |
299 | int64_t in, out; | |
6ce84e64 | 300 | unsigned char buf[MAX_LEN], cmp[sizeof(int64_t)]; |
7ffbd7ca P |
301 | const size_t len = raw_values[n].len >= sizeof(int64_t) |
302 | ? sizeof(int64_t) : raw_values[n].len; | |
303 | OSSL_PARAM param = OSSL_PARAM_int64("a", NULL); | |
6ce84e64 | 304 | |
7ffbd7ca | 305 | memset(buf, 0, sizeof(buf)); |
6ce84e64 | 306 | le_copy(buf, raw_values[n].value, sizeof(in)); |
7ffbd7ca P |
307 | memcpy(&in, buf, sizeof(in)); |
308 | param.data = &out; | |
309 | if (!TEST_true(OSSL_PARAM_set_int64(¶m, in))) | |
310 | return 0; | |
6ce84e64 P |
311 | le_copy(cmp, &out, sizeof(out)); |
312 | if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) | |
7ffbd7ca P |
313 | return 0; |
314 | in = 0; | |
7ffbd7ca P |
315 | if (!TEST_true(OSSL_PARAM_get_int64(¶m, &in))) |
316 | return 0; | |
6ce84e64 P |
317 | le_copy(cmp, &in, sizeof(in)); |
318 | if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) | |
7ffbd7ca P |
319 | return 0; |
320 | param.data = &out; | |
6ce84e64 | 321 | return test_param_type_extra(¶m, raw_values[n].value, sizeof(int64_t)); |
7ffbd7ca P |
322 | } |
323 | ||
324 | static int test_param_uint64(int n) | |
325 | { | |
326 | uint64_t in, out; | |
6ce84e64 | 327 | unsigned char buf[MAX_LEN], cmp[sizeof(uint64_t)]; |
7ffbd7ca P |
328 | const size_t len = raw_values[n].len >= sizeof(uint64_t) |
329 | ? sizeof(uint64_t) : raw_values[n].len; | |
330 | OSSL_PARAM param = OSSL_PARAM_uint64("a", NULL); | |
6ce84e64 | 331 | |
7ffbd7ca | 332 | memset(buf, 0, sizeof(buf)); |
6ce84e64 | 333 | le_copy(buf, raw_values[n].value, sizeof(in)); |
7ffbd7ca P |
334 | memcpy(&in, buf, sizeof(in)); |
335 | param.data = &out; | |
336 | if (!TEST_true(OSSL_PARAM_set_uint64(¶m, in))) | |
337 | return 0; | |
6ce84e64 P |
338 | le_copy(cmp, &out, sizeof(out)); |
339 | if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) | |
7ffbd7ca P |
340 | return 0; |
341 | in = 0; | |
7ffbd7ca P |
342 | if (!TEST_true(OSSL_PARAM_get_uint64(¶m, &in))) |
343 | return 0; | |
6ce84e64 P |
344 | le_copy(cmp, &in, sizeof(in)); |
345 | if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) | |
7ffbd7ca P |
346 | return 0; |
347 | param.data = &out; | |
6ce84e64 | 348 | return test_param_type_extra(¶m, raw_values[n].value, sizeof(uint64_t)); |
7ffbd7ca P |
349 | } |
350 | ||
351 | static int test_param_size_t(int n) | |
352 | { | |
353 | size_t in, out; | |
6ce84e64 | 354 | unsigned char buf[MAX_LEN], cmp[sizeof(size_t)]; |
7ffbd7ca P |
355 | const size_t len = raw_values[n].len >= sizeof(size_t) |
356 | ? sizeof(size_t) : raw_values[n].len; | |
357 | OSSL_PARAM param = OSSL_PARAM_size_t("a", NULL); | |
6ce84e64 | 358 | |
7ffbd7ca | 359 | memset(buf, 0, sizeof(buf)); |
6ce84e64 | 360 | le_copy(buf, raw_values[n].value, sizeof(in)); |
7ffbd7ca P |
361 | memcpy(&in, buf, sizeof(in)); |
362 | param.data = &out; | |
363 | if (!TEST_true(OSSL_PARAM_set_size_t(¶m, in))) | |
364 | return 0; | |
6ce84e64 P |
365 | le_copy(cmp, &out, sizeof(out)); |
366 | if (!TEST_mem_eq(cmp, len, raw_values[n].value, len)) | |
7ffbd7ca P |
367 | return 0; |
368 | in = 0; | |
7ffbd7ca P |
369 | if (!TEST_true(OSSL_PARAM_get_size_t(¶m, &in))) |
370 | return 0; | |
6ce84e64 P |
371 | le_copy(cmp, &in, sizeof(in)); |
372 | if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in))) | |
7ffbd7ca P |
373 | return 0; |
374 | param.data = &out; | |
6ce84e64 | 375 | return test_param_type_extra(¶m, raw_values[n].value, sizeof(size_t)); |
7ffbd7ca P |
376 | } |
377 | ||
378 | static int test_param_bignum(int n) | |
379 | { | |
6ce84e64 | 380 | unsigned char buf[MAX_LEN], bnbuf[MAX_LEN]; |
7ffbd7ca | 381 | const size_t len = raw_values[n].len; |
7ffbd7ca P |
382 | BIGNUM *b = NULL, *c = NULL; |
383 | OSSL_PARAM param = OSSL_PARAM_DEFN("bn", OSSL_PARAM_UNSIGNED_INTEGER, | |
4e7991b4 | 384 | NULL, 0); |
7ffbd7ca P |
385 | int ret = 0; |
386 | ||
387 | param.data = bnbuf; | |
388 | param.data_size = len; | |
7ffbd7ca | 389 | |
6ce84e64 P |
390 | le_copy(buf, raw_values[n].value, len); |
391 | if (!TEST_ptr(b = BN_lebin2bn(raw_values[n].value, (int)len, NULL))) | |
7ffbd7ca P |
392 | goto err; |
393 | ||
394 | if (!TEST_true(OSSL_PARAM_set_BN(¶m, b)) | |
4e7991b4 | 395 | || !TEST_mem_eq(bnbuf, param.return_size, buf, param.return_size)) |
7ffbd7ca | 396 | goto err; |
4e7991b4 | 397 | param.data_size = param.return_size; |
7ffbd7ca P |
398 | if (!TEST_true(OSSL_PARAM_get_BN(¶m, &c)) |
399 | || !TEST_BN_eq(b, c)) | |
400 | goto err; | |
401 | ||
402 | ret = 1; | |
403 | err: | |
404 | BN_free(b); | |
405 | BN_free(c); | |
406 | return ret; | |
407 | } | |
408 | ||
409 | static int test_param_real(void) | |
410 | { | |
411 | double p; | |
412 | OSSL_PARAM param = OSSL_PARAM_double("r", NULL); | |
413 | ||
414 | param.data = &p; | |
415 | return TEST_true(OSSL_PARAM_set_double(¶m, 3.14159)) | |
416 | && TEST_double_eq(p, 3.14159); | |
417 | } | |
418 | ||
7ffbd7ca P |
419 | static int test_param_construct(void) |
420 | { | |
421 | static const char *int_names[] = { | |
422 | "int", "long", "int32", "int64" | |
423 | }; | |
424 | static const char *uint_names[] = { | |
425 | "uint", "ulong", "uint32", "uint64", "size_t" | |
426 | }; | |
427 | static const unsigned char bn_val[16] = { | |
428 | 0xac, 0x75, 0x22, 0x7d, 0x81, 0x06, 0x7a, 0x23, | |
429 | 0xa6, 0xed, 0x87, 0xc7, 0xab, 0xf4, 0x73, 0x22 | |
430 | }; | |
431 | OSSL_PARAM params[20]; | |
432 | char buf[100], buf2[100], *bufp, *bufp2; | |
433 | unsigned char ubuf[100]; | |
5d677186 | 434 | void *vp, *vpn = NULL, *vp2; |
4e7991b4 | 435 | OSSL_PARAM *cp; |
7ffbd7ca P |
436 | int i, n = 0, ret = 0; |
437 | unsigned int u; | |
438 | long int l; | |
439 | unsigned long int ul; | |
440 | int32_t i32; | |
441 | uint32_t u32; | |
442 | int64_t i64; | |
443 | uint64_t u64; | |
4e7991b4 | 444 | size_t j, k, s; |
7ffbd7ca P |
445 | double d, d2; |
446 | BIGNUM *bn = NULL, *bn2 = NULL; | |
447 | ||
4e7991b4 P |
448 | params[n++] = OSSL_PARAM_construct_int("int", &i); |
449 | params[n++] = OSSL_PARAM_construct_uint("uint", &u); | |
450 | params[n++] = OSSL_PARAM_construct_long("long", &l); | |
451 | params[n++] = OSSL_PARAM_construct_ulong("ulong", &ul); | |
452 | params[n++] = OSSL_PARAM_construct_int32("int32", &i32); | |
453 | params[n++] = OSSL_PARAM_construct_int64("int64", &i64); | |
454 | params[n++] = OSSL_PARAM_construct_uint32("uint32", &u32); | |
455 | params[n++] = OSSL_PARAM_construct_uint64("uint64", &u64); | |
456 | params[n++] = OSSL_PARAM_construct_size_t("size_t", &s); | |
457 | params[n++] = OSSL_PARAM_construct_double("double", &d); | |
458 | params[n++] = OSSL_PARAM_construct_BN("bignum", ubuf, sizeof(ubuf)); | |
459 | params[n++] = OSSL_PARAM_construct_utf8_string("utf8str", buf, sizeof(buf)); | |
460 | params[n++] = OSSL_PARAM_construct_octet_string("octstr", buf, sizeof(buf)); | |
461 | params[n++] = OSSL_PARAM_construct_utf8_ptr("utf8ptr", &bufp, 0); | |
462 | params[n++] = OSSL_PARAM_construct_octet_ptr("octptr", &vp, 0); | |
195852fe | 463 | params[n] = OSSL_PARAM_construct_end(); |
7ffbd7ca P |
464 | |
465 | /* Search failure */ | |
466 | if (!TEST_ptr_null(OSSL_PARAM_locate(params, "fnord"))) | |
467 | goto err; | |
468 | ||
469 | /* All signed integral types */ | |
470 | for (j = 0; j < OSSL_NELEM(int_names); j++) { | |
471 | if (!TEST_ptr(cp = OSSL_PARAM_locate(params, int_names[j])) | |
472 | || !TEST_true(OSSL_PARAM_set_int32(cp, (int32_t)(3 + j))) | |
473 | || !TEST_true(OSSL_PARAM_get_int64(cp, &i64)) | |
4e7991b4 | 474 | || !TEST_size_t_eq(cp->data_size, cp->return_size) |
7ffbd7ca P |
475 | || !TEST_size_t_eq((size_t)i64, 3 + j)) { |
476 | TEST_note("iteration %zu var %s", j + 1, int_names[j]); | |
477 | goto err; | |
478 | } | |
479 | } | |
480 | /* All unsigned integral types */ | |
481 | for (j = 0; j < OSSL_NELEM(uint_names); j++) { | |
482 | if (!TEST_ptr(cp = OSSL_PARAM_locate(params, uint_names[j])) | |
483 | || !TEST_true(OSSL_PARAM_set_uint32(cp, (uint32_t)(3 + j))) | |
484 | || !TEST_true(OSSL_PARAM_get_uint64(cp, &u64)) | |
4e7991b4 | 485 | || !TEST_size_t_eq(cp->data_size, cp->return_size) |
7ffbd7ca P |
486 | || !TEST_size_t_eq((size_t)u64, 3 + j)) { |
487 | TEST_note("iteration %zu var %s", j + 1, uint_names[j]); | |
488 | goto err; | |
489 | } | |
490 | } | |
491 | /* Real */ | |
492 | if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "double")) | |
493 | || !TEST_true(OSSL_PARAM_set_double(cp, 3.14)) | |
494 | || !TEST_true(OSSL_PARAM_get_double(cp, &d2)) | |
4e7991b4 | 495 | || !TEST_size_t_eq(cp->return_size, sizeof(double)) |
7ffbd7ca P |
496 | || !TEST_double_eq(d, d2)) |
497 | goto err; | |
498 | /* UTF8 string */ | |
499 | bufp = NULL; | |
500 | if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "utf8str")) | |
501 | || !TEST_true(OSSL_PARAM_set_utf8_string(cp, "abcdef")) | |
4e7991b4 | 502 | || !TEST_size_t_eq(cp->return_size, sizeof("abcdef")) |
7ffbd7ca P |
503 | || !TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, 0)) |
504 | || !TEST_str_eq(bufp, "abcdef")) | |
505 | goto err; | |
506 | OPENSSL_free(bufp); | |
507 | bufp = buf2; | |
508 | if (!TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, sizeof(buf2))) | |
509 | || !TEST_str_eq(buf2, "abcdef")) | |
510 | goto err; | |
511 | /* UTF8 pointer */ | |
512 | bufp = buf; | |
7ffbd7ca P |
513 | if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "utf8ptr")) |
514 | || !TEST_true(OSSL_PARAM_set_utf8_ptr(cp, "tuvwxyz")) | |
4e7991b4 | 515 | || !TEST_size_t_eq(cp->return_size, sizeof("tuvwxyz")) |
7ffbd7ca P |
516 | || !TEST_str_eq(bufp, "tuvwxyz") |
517 | || !TEST_true(OSSL_PARAM_get_utf8_ptr(cp, (const char **)&bufp2)) | |
518 | || !TEST_ptr_eq(bufp2, bufp)) | |
519 | goto err; | |
520 | /* OCTET string */ | |
4e7991b4 P |
521 | if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "octstr")) |
522 | || !TEST_true(OSSL_PARAM_set_octet_string(cp, "abcdefghi", | |
7ffbd7ca | 523 | sizeof("abcdefghi"))) |
4e7991b4 | 524 | || !TEST_size_t_eq(cp->return_size, sizeof("abcdefghi"))) |
7ffbd7ca P |
525 | goto err; |
526 | /* Match the return size to avoid trailing garbage bytes */ | |
4e7991b4 P |
527 | cp->data_size = cp->return_size; |
528 | if (!TEST_true(OSSL_PARAM_get_octet_string(cp, &vpn, 0, &s)) | |
7ffbd7ca | 529 | || !TEST_size_t_eq(s, sizeof("abcdefghi")) |
5d677186 | 530 | || !TEST_mem_eq(vpn, sizeof("abcdefghi"), |
7ffbd7ca P |
531 | "abcdefghi", sizeof("abcdefghi"))) |
532 | goto err; | |
7ffbd7ca | 533 | vp = buf2; |
4e7991b4 | 534 | if (!TEST_true(OSSL_PARAM_get_octet_string(cp, &vp, sizeof(buf2), &s)) |
7ffbd7ca P |
535 | || !TEST_size_t_eq(s, sizeof("abcdefghi")) |
536 | || !TEST_mem_eq(vp, sizeof("abcdefghi"), | |
537 | "abcdefghi", sizeof("abcdefghi"))) | |
538 | goto err; | |
539 | /* OCTET pointer */ | |
540 | vp = &l; | |
4e7991b4 P |
541 | if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "octptr")) |
542 | || !TEST_true(OSSL_PARAM_set_octet_ptr(cp, &ul, sizeof(ul))) | |
543 | || !TEST_size_t_eq(cp->return_size, sizeof(ul)) | |
7ffbd7ca P |
544 | || !TEST_ptr_eq(vp, &ul)) |
545 | goto err; | |
546 | /* Match the return size to avoid trailing garbage bytes */ | |
4e7991b4 P |
547 | cp->data_size = cp->return_size; |
548 | if (!TEST_true(OSSL_PARAM_get_octet_ptr(cp, (const void **)&vp2, &k)) | |
7ffbd7ca P |
549 | || !TEST_size_t_eq(k, sizeof(ul)) |
550 | || !TEST_ptr_eq(vp2, vp)) | |
551 | goto err; | |
552 | /* BIGNUM */ | |
4e7991b4 | 553 | if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "bignum")) |
7ffbd7ca | 554 | || !TEST_ptr(bn = BN_lebin2bn(bn_val, (int)sizeof(bn_val), NULL)) |
4e7991b4 | 555 | || !TEST_true(OSSL_PARAM_set_BN(cp, bn)) |
2c99372d | 556 | || !TEST_size_t_eq(cp->data_size, cp->return_size)) |
7ffbd7ca P |
557 | goto err; |
558 | /* Match the return size to avoid trailing garbage bytes */ | |
4e7991b4 P |
559 | cp->data_size = cp->return_size; |
560 | if(!TEST_true(OSSL_PARAM_get_BN(cp, &bn2)) | |
7ffbd7ca P |
561 | || !TEST_BN_eq(bn, bn2)) |
562 | goto err; | |
563 | ret = 1; | |
564 | err: | |
5d677186 | 565 | OPENSSL_free(vpn); |
7ffbd7ca P |
566 | BN_free(bn); |
567 | BN_free(bn2); | |
568 | return ret; | |
569 | } | |
570 | ||
571 | int setup_tests(void) | |
572 | { | |
573 | ADD_ALL_TESTS(test_param_int, OSSL_NELEM(raw_values)); | |
574 | ADD_ALL_TESTS(test_param_long, OSSL_NELEM(raw_values)); | |
575 | ADD_ALL_TESTS(test_param_uint, OSSL_NELEM(raw_values)); | |
576 | ADD_ALL_TESTS(test_param_ulong, OSSL_NELEM(raw_values)); | |
577 | ADD_ALL_TESTS(test_param_int32, OSSL_NELEM(raw_values)); | |
578 | ADD_ALL_TESTS(test_param_uint32, OSSL_NELEM(raw_values)); | |
579 | ADD_ALL_TESTS(test_param_size_t, OSSL_NELEM(raw_values)); | |
580 | ADD_ALL_TESTS(test_param_int64, OSSL_NELEM(raw_values)); | |
581 | ADD_ALL_TESTS(test_param_uint64, OSSL_NELEM(raw_values)); | |
582 | ADD_ALL_TESTS(test_param_bignum, OSSL_NELEM(raw_values)); | |
583 | ADD_TEST(test_param_real); | |
584 | ADD_TEST(test_param_construct); | |
585 | return 1; | |
586 | } |