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