Home | History | Annotate | Line # | Download | only in test
      1  1.1  christos /*
      2  1.1  christos  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
      3  1.1  christos  * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      6  1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      7  1.1  christos  * in the file LICENSE in the source distribution or at
      8  1.1  christos  * https://www.openssl.org/source/license.html
      9  1.1  christos  */
     10  1.1  christos 
     11  1.1  christos #include <string.h>
     12  1.1  christos #include "testutil.h"
     13  1.1  christos #include "internal/nelem.h"
     14  1.1  christos #include "internal/endian.h"
     15  1.1  christos #include <openssl/params.h>
     16  1.1  christos #include <openssl/bn.h>
     17  1.1  christos 
     18  1.1  christos /* The maximum size of the static buffers used to test most things */
     19  1.1  christos #define MAX_LEN 20
     20  1.1  christos 
     21  1.1  christos static void swap_copy(unsigned char *out, const void *in, size_t len)
     22  1.1  christos {
     23  1.1  christos     size_t j;
     24  1.1  christos 
     25  1.1  christos     for (j = 0; j < len; j++)
     26  1.1  christos         out[j] = ((unsigned char *)in)[len - j - 1];
     27  1.1  christos }
     28  1.1  christos 
     29  1.1  christos /*
     30  1.1  christos  * A memory copy that converts the native byte ordering either to or from
     31  1.1  christos  * little endian format.
     32  1.1  christos  *
     33  1.1  christos  * On a little endian machine copying either is just a memcpy(3), on a
     34  1.1  christos  * big endian machine copying from native to or from little endian involves
     35  1.1  christos  * byte reversal.
     36  1.1  christos  */
     37  1.1  christos static void le_copy(unsigned char *out, const void *in, size_t len)
     38  1.1  christos {
     39  1.1  christos     DECLARE_IS_ENDIAN;
     40  1.1  christos 
     41  1.1  christos     if (IS_LITTLE_ENDIAN)
     42  1.1  christos         memcpy(out, in, len);
     43  1.1  christos     else
     44  1.1  christos         swap_copy(out, in, len);
     45  1.1  christos }
     46  1.1  christos 
     47  1.1  christos static const struct {
     48  1.1  christos     size_t len;
     49  1.1  christos     unsigned char value[MAX_LEN];
     50  1.1  christos } raw_values[] = {
     51  1.1  christos     { 1, { 0x47 } },
     52  1.1  christos     { 1, { 0xd0 } },
     53  1.1  christos     { 2, { 0x01, 0xe9 } },
     54  1.1  christos     { 2, { 0xff, 0x53 } },
     55  1.1  christos     { 3, { 0x16, 0xff, 0x7c } },
     56  1.1  christos     { 3, { 0xa8, 0x9c, 0x0e } },
     57  1.1  christos     { 4, { 0x38, 0x27, 0xbf, 0x3b } },
     58  1.1  christos     { 4, { 0x9f, 0x26, 0x48, 0x22 } },
     59  1.1  christos     { 5, { 0x30, 0x65, 0xfa, 0xe4, 0x81 } },
     60  1.1  christos     { 5, { 0xd1, 0x76, 0x01, 0x1b, 0xcd } },
     61  1.1  christos     { 8, { 0x59, 0xb2, 0x1a, 0xe9, 0x2a, 0xd8, 0x46, 0x40 } },
     62  1.1  christos     { 8, { 0xb4, 0xae, 0xbd, 0xb4, 0xdd, 0x04, 0xb1, 0x4c } },
     63  1.1  christos     { 16, { 0x61, 0xe8, 0x7e, 0x31, 0xe9, 0x33, 0x83, 0x3d,
     64  1.1  christos             0x87, 0x99, 0xc7, 0xd8, 0x5d, 0xa9, 0x8b, 0x42 } },
     65  1.1  christos     { 16, { 0xee, 0x6e, 0x8b, 0xc3, 0xec, 0xcf, 0x37, 0xcc,
     66  1.1  christos             0x89, 0x67, 0xf2, 0x68, 0x33, 0xa0, 0x14, 0xb0 } },
     67  1.1  christos };
     68  1.1  christos 
     69  1.1  christos static int test_param_type_extra(OSSL_PARAM *param, const unsigned char *cmp,
     70  1.1  christos                                  size_t width)
     71  1.1  christos {
     72  1.1  christos     int32_t i32;
     73  1.1  christos     int64_t i64;
     74  1.1  christos     size_t s, sz;
     75  1.1  christos     unsigned char buf[MAX_LEN];
     76  1.1  christos     const int bit32 = param->data_size <= sizeof(int32_t);
     77  1.1  christos     const int sizet = param->data_size <= sizeof(size_t);
     78  1.1  christos     const int signd = param->data_type == OSSL_PARAM_INTEGER;
     79  1.1  christos 
     80  1.1  christos     /*
     81  1.1  christos      * Set the unmodified sentinal directly because there is no param array
     82  1.1  christos      * for these tests.
     83  1.1  christos      */
     84  1.1  christos     param->return_size = OSSL_PARAM_UNMODIFIED;
     85  1.1  christos     if (signd) {
     86  1.1  christos         if ((bit32 && !TEST_true(OSSL_PARAM_get_int32(param, &i32)))
     87  1.1  christos             || !TEST_true(OSSL_PARAM_get_int64(param, &i64)))
     88  1.1  christos             return 0;
     89  1.1  christos     } else {
     90  1.1  christos         if ((bit32
     91  1.1  christos              && !TEST_true(OSSL_PARAM_get_uint32(param, (uint32_t *)&i32)))
     92  1.1  christos             || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64))
     93  1.1  christos             || (sizet && !TEST_true(OSSL_PARAM_get_size_t(param, &s))))
     94  1.1  christos             return 0;
     95  1.1  christos     }
     96  1.1  christos     if (!TEST_false(OSSL_PARAM_modified(param)))
     97  1.1  christos         return 0;
     98  1.1  christos 
     99  1.1  christos     /* Check signed types */
    100  1.1  christos     if (bit32) {
    101  1.1  christos         le_copy(buf, &i32, sizeof(i32));
    102  1.1  christos         sz = sizeof(i32) < width ? sizeof(i32) : width;
    103  1.1  christos         if (!TEST_mem_eq(buf, sz, cmp, sz))
    104  1.1  christos             return 0;
    105  1.1  christos     }
    106  1.1  christos     le_copy(buf, &i64, sizeof(i64));
    107  1.1  christos     sz = sizeof(i64) < width ? sizeof(i64) : width;
    108  1.1  christos     if (!TEST_mem_eq(buf, sz, cmp, sz))
    109  1.1  christos         return 0;
    110  1.1  christos     if (sizet && !signd) {
    111  1.1  christos         le_copy(buf, &s, sizeof(s));
    112  1.1  christos         sz = sizeof(s) < width ? sizeof(s) : width;
    113  1.1  christos         if (!TEST_mem_eq(buf, sz, cmp, sz))
    114  1.1  christos             return 0;
    115  1.1  christos     }
    116  1.1  christos 
    117  1.1  christos     /* Check a widening write if possible */
    118  1.1  christos     if (sizeof(size_t) > width) {
    119  1.1  christos         if (signd) {
    120  1.1  christos             if (!TEST_true(OSSL_PARAM_set_int32(param, 12345))
    121  1.1  christos                 || !TEST_true(OSSL_PARAM_get_int64(param, &i64))
    122  1.1  christos                 || !TEST_size_t_eq((size_t)i64, 12345))
    123  1.1  christos                 return 0;
    124  1.1  christos         } else {
    125  1.1  christos             if (!TEST_true(OSSL_PARAM_set_uint32(param, 12345))
    126  1.1  christos                 || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64))
    127  1.1  christos                 || !TEST_size_t_eq((size_t)i64, 12345))
    128  1.1  christos                 return 0;
    129  1.1  christos         }
    130  1.1  christos         if (!TEST_true(OSSL_PARAM_modified(param)))
    131  1.1  christos             return 0;
    132  1.1  christos     }
    133  1.1  christos     return 1;
    134  1.1  christos }
    135  1.1  christos 
    136  1.1  christos /*
    137  1.1  christos  * The test cases for each of the bastic integral types are similar.
    138  1.1  christos  * For each type, a param of that type is set and an attempt to read it
    139  1.1  christos  * get is made.  Finally, the above function is called to verify that
    140  1.1  christos  * the params can be read as other types.
    141  1.1  christos  *
    142  1.1  christos  * All the real work is done via byte buffers which are converted to machine
    143  1.1  christos  * byte order and to little endian for comparisons.  Narrower values are best
    144  1.1  christos  * compared using little endian because their values and positions don't
    145  1.1  christos  * change.
    146  1.1  christos  */
    147  1.1  christos 
    148  1.1  christos static int test_param_int(int n)
    149  1.1  christos {
    150  1.1  christos     int in, out;
    151  1.1  christos     unsigned char buf[MAX_LEN], cmp[sizeof(int)];
    152  1.1  christos     const size_t len = raw_values[n].len >= sizeof(int) ?
    153  1.1  christos                        sizeof(int) : raw_values[n].len;
    154  1.1  christos     OSSL_PARAM param = OSSL_PARAM_int("a", NULL);
    155  1.1  christos 
    156  1.1  christos     memset(buf, 0, sizeof(buf));
    157  1.1  christos     le_copy(buf, raw_values[n].value, sizeof(in));
    158  1.1  christos     memcpy(&in, buf, sizeof(in));
    159  1.1  christos     param.data = &out;
    160  1.1  christos     if (!TEST_true(OSSL_PARAM_set_int(&param, in)))
    161  1.1  christos         return 0;
    162  1.1  christos     le_copy(cmp, &out, sizeof(out));
    163  1.1  christos     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
    164  1.1  christos         return 0;
    165  1.1  christos     in = 0;
    166  1.1  christos     if (!TEST_true(OSSL_PARAM_get_int(&param, &in)))
    167  1.1  christos         return 0;
    168  1.1  christos     le_copy(cmp, &in, sizeof(in));
    169  1.1  christos     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
    170  1.1  christos         return 0;
    171  1.1  christos     param.data = &out;
    172  1.1  christos     return test_param_type_extra(&param, raw_values[n].value, sizeof(int));
    173  1.1  christos }
    174  1.1  christos 
    175  1.1  christos static int test_param_long(int n)
    176  1.1  christos {
    177  1.1  christos     long int in, out;
    178  1.1  christos     unsigned char buf[MAX_LEN], cmp[sizeof(long int)];
    179  1.1  christos     const size_t len = raw_values[n].len >= sizeof(long int)
    180  1.1  christos                        ? sizeof(long int) : raw_values[n].len;
    181  1.1  christos     OSSL_PARAM param = OSSL_PARAM_long("a", NULL);
    182  1.1  christos 
    183  1.1  christos     memset(buf, 0, sizeof(buf));
    184  1.1  christos     le_copy(buf, raw_values[n].value, sizeof(in));
    185  1.1  christos     memcpy(&in, buf, sizeof(in));
    186  1.1  christos     param.data = &out;
    187  1.1  christos     if (!TEST_true(OSSL_PARAM_set_long(&param, in)))
    188  1.1  christos         return 0;
    189  1.1  christos     le_copy(cmp, &out, sizeof(out));
    190  1.1  christos     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
    191  1.1  christos         return 0;
    192  1.1  christos     in = 0;
    193  1.1  christos     if (!TEST_true(OSSL_PARAM_get_long(&param, &in)))
    194  1.1  christos         return 0;
    195  1.1  christos     le_copy(cmp, &in, sizeof(in));
    196  1.1  christos     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
    197  1.1  christos         return 0;
    198  1.1  christos     param.data = &out;
    199  1.1  christos     return test_param_type_extra(&param, raw_values[n].value, sizeof(long int));
    200  1.1  christos }
    201  1.1  christos 
    202  1.1  christos static int test_param_uint(int n)
    203  1.1  christos {
    204  1.1  christos     unsigned int in, out;
    205  1.1  christos     unsigned char buf[MAX_LEN], cmp[sizeof(unsigned int)];
    206  1.1  christos     const size_t len = raw_values[n].len >= sizeof(unsigned int) ? sizeof(unsigned int) : raw_values[n].len;
    207  1.1  christos     OSSL_PARAM param = OSSL_PARAM_uint("a", NULL);
    208  1.1  christos 
    209  1.1  christos     memset(buf, 0, sizeof(buf));
    210  1.1  christos     le_copy(buf, raw_values[n].value, sizeof(in));
    211  1.1  christos     memcpy(&in, buf, sizeof(in));
    212  1.1  christos     param.data = &out;
    213  1.1  christos     if (!TEST_true(OSSL_PARAM_set_uint(&param, in)))
    214  1.1  christos         return 0;
    215  1.1  christos     le_copy(cmp, &out, sizeof(out));
    216  1.1  christos     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
    217  1.1  christos         return 0;
    218  1.1  christos     in = 0;
    219  1.1  christos     if (!TEST_true(OSSL_PARAM_get_uint(&param, &in)))
    220  1.1  christos         return 0;
    221  1.1  christos     le_copy(cmp, &in, sizeof(in));
    222  1.1  christos     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
    223  1.1  christos         return 0;
    224  1.1  christos     param.data = &out;
    225  1.1  christos     return test_param_type_extra(&param, raw_values[n].value, sizeof(unsigned int));
    226  1.1  christos }
    227  1.1  christos 
    228  1.1  christos static int test_param_ulong(int n)
    229  1.1  christos {
    230  1.1  christos     unsigned long int in, out;
    231  1.1  christos     unsigned char buf[MAX_LEN], cmp[sizeof(unsigned long int)];
    232  1.1  christos     const size_t len = raw_values[n].len >= sizeof(unsigned long int)
    233  1.1  christos                        ? sizeof(unsigned long int) : raw_values[n].len;
    234  1.1  christos     OSSL_PARAM param = OSSL_PARAM_ulong("a", NULL);
    235  1.1  christos 
    236  1.1  christos     memset(buf, 0, sizeof(buf));
    237  1.1  christos     le_copy(buf, raw_values[n].value, sizeof(in));
    238  1.1  christos     memcpy(&in, buf, sizeof(in));
    239  1.1  christos     param.data = &out;
    240  1.1  christos     if (!TEST_true(OSSL_PARAM_set_ulong(&param, in)))
    241  1.1  christos         return 0;
    242  1.1  christos     le_copy(cmp, &out, sizeof(out));
    243  1.1  christos     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
    244  1.1  christos         return 0;
    245  1.1  christos     in = 0;
    246  1.1  christos     if (!TEST_true(OSSL_PARAM_get_ulong(&param, &in)))
    247  1.1  christos         return 0;
    248  1.1  christos     le_copy(cmp, &in, sizeof(in));
    249  1.1  christos     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
    250  1.1  christos         return 0;
    251  1.1  christos     param.data = &out;
    252  1.1  christos     return test_param_type_extra(&param, raw_values[n].value, sizeof(unsigned long int));
    253  1.1  christos }
    254  1.1  christos 
    255  1.1  christos static int test_param_int32(int n)
    256  1.1  christos {
    257  1.1  christos     int32_t in, out;
    258  1.1  christos     unsigned char buf[MAX_LEN], cmp[sizeof(int32_t)];
    259  1.1  christos     const size_t len = raw_values[n].len >= sizeof(int32_t)
    260  1.1  christos                        ? sizeof(int32_t) : raw_values[n].len;
    261  1.1  christos     OSSL_PARAM param = OSSL_PARAM_int32("a", NULL);
    262  1.1  christos 
    263  1.1  christos     memset(buf, 0, sizeof(buf));
    264  1.1  christos     le_copy(buf, raw_values[n].value, sizeof(in));
    265  1.1  christos     memcpy(&in, buf, sizeof(in));
    266  1.1  christos     param.data = &out;
    267  1.1  christos     if (!TEST_true(OSSL_PARAM_set_int32(&param, in)))
    268  1.1  christos         return 0;
    269  1.1  christos     le_copy(cmp, &out, sizeof(out));
    270  1.1  christos     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
    271  1.1  christos         return 0;
    272  1.1  christos     in = 0;
    273  1.1  christos     if (!TEST_true(OSSL_PARAM_get_int32(&param, &in)))
    274  1.1  christos         return 0;
    275  1.1  christos     le_copy(cmp, &in, sizeof(in));
    276  1.1  christos     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
    277  1.1  christos         return 0;
    278  1.1  christos     param.data = &out;
    279  1.1  christos     return test_param_type_extra(&param, raw_values[n].value, sizeof(int32_t));
    280  1.1  christos }
    281  1.1  christos 
    282  1.1  christos static int test_param_uint32(int n)
    283  1.1  christos {
    284  1.1  christos     uint32_t in, out;
    285  1.1  christos     unsigned char buf[MAX_LEN], cmp[sizeof(uint32_t)];
    286  1.1  christos     const size_t len = raw_values[n].len >= sizeof(uint32_t)
    287  1.1  christos                        ? sizeof(uint32_t) : raw_values[n].len;
    288  1.1  christos     OSSL_PARAM param = OSSL_PARAM_uint32("a", NULL);
    289  1.1  christos 
    290  1.1  christos     memset(buf, 0, sizeof(buf));
    291  1.1  christos     le_copy(buf, raw_values[n].value, sizeof(in));
    292  1.1  christos     memcpy(&in, buf, sizeof(in));
    293  1.1  christos     param.data = &out;
    294  1.1  christos     if (!TEST_true(OSSL_PARAM_set_uint32(&param, in)))
    295  1.1  christos         return 0;
    296  1.1  christos     le_copy(cmp, &out, sizeof(out));
    297  1.1  christos     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
    298  1.1  christos         return 0;
    299  1.1  christos     in = 0;
    300  1.1  christos     if (!TEST_true(OSSL_PARAM_get_uint32(&param, &in)))
    301  1.1  christos         return 0;
    302  1.1  christos     le_copy(cmp, &in, sizeof(in));
    303  1.1  christos     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
    304  1.1  christos         return 0;
    305  1.1  christos     param.data = &out;
    306  1.1  christos     return test_param_type_extra(&param, raw_values[n].value, sizeof(uint32_t));
    307  1.1  christos }
    308  1.1  christos 
    309  1.1  christos static int test_param_int64(int n)
    310  1.1  christos {
    311  1.1  christos     int64_t in, out;
    312  1.1  christos     unsigned char buf[MAX_LEN], cmp[sizeof(int64_t)];
    313  1.1  christos     const size_t len = raw_values[n].len >= sizeof(int64_t)
    314  1.1  christos                        ? sizeof(int64_t) : raw_values[n].len;
    315  1.1  christos     OSSL_PARAM param = OSSL_PARAM_int64("a", NULL);
    316  1.1  christos 
    317  1.1  christos     memset(buf, 0, sizeof(buf));
    318  1.1  christos     le_copy(buf, raw_values[n].value, sizeof(in));
    319  1.1  christos     memcpy(&in, buf, sizeof(in));
    320  1.1  christos     param.data = &out;
    321  1.1  christos     if (!TEST_true(OSSL_PARAM_set_int64(&param, in)))
    322  1.1  christos         return 0;
    323  1.1  christos     le_copy(cmp, &out, sizeof(out));
    324  1.1  christos     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
    325  1.1  christos         return 0;
    326  1.1  christos     in = 0;
    327  1.1  christos     if (!TEST_true(OSSL_PARAM_get_int64(&param, &in)))
    328  1.1  christos         return 0;
    329  1.1  christos     le_copy(cmp, &in, sizeof(in));
    330  1.1  christos     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
    331  1.1  christos         return 0;
    332  1.1  christos     param.data = &out;
    333  1.1  christos     return test_param_type_extra(&param, raw_values[n].value, sizeof(int64_t));
    334  1.1  christos }
    335  1.1  christos 
    336  1.1  christos static int test_param_uint64(int n)
    337  1.1  christos {
    338  1.1  christos     uint64_t in, out;
    339  1.1  christos     unsigned char buf[MAX_LEN], cmp[sizeof(uint64_t)];
    340  1.1  christos     const size_t len = raw_values[n].len >= sizeof(uint64_t)
    341  1.1  christos                        ? sizeof(uint64_t) : raw_values[n].len;
    342  1.1  christos     OSSL_PARAM param = OSSL_PARAM_uint64("a", NULL);
    343  1.1  christos 
    344  1.1  christos     memset(buf, 0, sizeof(buf));
    345  1.1  christos     le_copy(buf, raw_values[n].value, sizeof(in));
    346  1.1  christos     memcpy(&in, buf, sizeof(in));
    347  1.1  christos     param.data = &out;
    348  1.1  christos     if (!TEST_true(OSSL_PARAM_set_uint64(&param, in)))
    349  1.1  christos         return 0;
    350  1.1  christos     le_copy(cmp, &out, sizeof(out));
    351  1.1  christos     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
    352  1.1  christos         return 0;
    353  1.1  christos     in = 0;
    354  1.1  christos     if (!TEST_true(OSSL_PARAM_get_uint64(&param, &in)))
    355  1.1  christos         return 0;
    356  1.1  christos     le_copy(cmp, &in, sizeof(in));
    357  1.1  christos     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
    358  1.1  christos         return 0;
    359  1.1  christos     param.data = &out;
    360  1.1  christos     return test_param_type_extra(&param, raw_values[n].value, sizeof(uint64_t));
    361  1.1  christos }
    362  1.1  christos 
    363  1.1  christos static int test_param_size_t(int n)
    364  1.1  christos {
    365  1.1  christos     size_t in, out;
    366  1.1  christos     unsigned char buf[MAX_LEN], cmp[sizeof(size_t)];
    367  1.1  christos     const size_t len = raw_values[n].len >= sizeof(size_t)
    368  1.1  christos                        ? sizeof(size_t) : raw_values[n].len;
    369  1.1  christos     OSSL_PARAM param = OSSL_PARAM_size_t("a", NULL);
    370  1.1  christos 
    371  1.1  christos     memset(buf, 0, sizeof(buf));
    372  1.1  christos     le_copy(buf, raw_values[n].value, sizeof(in));
    373  1.1  christos     memcpy(&in, buf, sizeof(in));
    374  1.1  christos     param.data = &out;
    375  1.1  christos     if (!TEST_true(OSSL_PARAM_set_size_t(&param, in)))
    376  1.1  christos         return 0;
    377  1.1  christos     le_copy(cmp, &out, sizeof(out));
    378  1.1  christos     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
    379  1.1  christos         return 0;
    380  1.1  christos     in = 0;
    381  1.1  christos     if (!TEST_true(OSSL_PARAM_get_size_t(&param, &in)))
    382  1.1  christos         return 0;
    383  1.1  christos     le_copy(cmp, &in, sizeof(in));
    384  1.1  christos     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
    385  1.1  christos         return 0;
    386  1.1  christos     param.data = &out;
    387  1.1  christos     return test_param_type_extra(&param, raw_values[n].value, sizeof(size_t));
    388  1.1  christos }
    389  1.1  christos 
    390  1.1  christos static int test_param_time_t(int n)
    391  1.1  christos {
    392  1.1  christos     time_t in, out;
    393  1.1  christos     unsigned char buf[MAX_LEN], cmp[sizeof(time_t)];
    394  1.1  christos     const size_t len = raw_values[n].len >= sizeof(time_t)
    395  1.1  christos                        ? sizeof(time_t) : raw_values[n].len;
    396  1.1  christos     OSSL_PARAM param = OSSL_PARAM_time_t("a", NULL);
    397  1.1  christos 
    398  1.1  christos     memset(buf, 0, sizeof(buf));
    399  1.1  christos     le_copy(buf, raw_values[n].value, sizeof(in));
    400  1.1  christos     memcpy(&in, buf, sizeof(in));
    401  1.1  christos     param.data = &out;
    402  1.1  christos     if (!TEST_true(OSSL_PARAM_set_time_t(&param, in)))
    403  1.1  christos         return 0;
    404  1.1  christos     le_copy(cmp, &out, sizeof(out));
    405  1.1  christos     if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
    406  1.1  christos         return 0;
    407  1.1  christos     in = 0;
    408  1.1  christos     if (!TEST_true(OSSL_PARAM_get_time_t(&param, &in)))
    409  1.1  christos         return 0;
    410  1.1  christos     le_copy(cmp, &in, sizeof(in));
    411  1.1  christos     if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
    412  1.1  christos         return 0;
    413  1.1  christos     param.data = &out;
    414  1.1  christos     return test_param_type_extra(&param, raw_values[n].value, sizeof(size_t));
    415  1.1  christos }
    416  1.1  christos 
    417  1.1  christos static int test_param_bignum(int n)
    418  1.1  christos {
    419  1.1  christos     unsigned char buf[MAX_LEN], bnbuf[MAX_LEN];
    420  1.1  christos     const size_t len = raw_values[n].len;
    421  1.1  christos     BIGNUM *b = NULL, *c = NULL;
    422  1.1  christos     OSSL_PARAM param = OSSL_PARAM_DEFN("bn", OSSL_PARAM_UNSIGNED_INTEGER,
    423  1.1  christos                                        NULL, 0);
    424  1.1  christos     int ret = 0;
    425  1.1  christos 
    426  1.1  christos     param.data = bnbuf;
    427  1.1  christos     param.data_size = len;
    428  1.1  christos 
    429  1.1  christos     le_copy(buf, raw_values[n].value, len);
    430  1.1  christos     if (!TEST_ptr(b = BN_lebin2bn(raw_values[n].value, (int)len, NULL)))
    431  1.1  christos         goto err;
    432  1.1  christos 
    433  1.1  christos     if (!TEST_true(OSSL_PARAM_set_BN(&param, b))
    434  1.1  christos         || !TEST_mem_eq(bnbuf, param.return_size, buf, param.return_size))
    435  1.1  christos         goto err;
    436  1.1  christos     param.data_size = param.return_size;
    437  1.1  christos     if (!TEST_true(OSSL_PARAM_get_BN(&param, &c))
    438  1.1  christos         || !TEST_BN_eq(b, c))
    439  1.1  christos         goto err;
    440  1.1  christos 
    441  1.1  christos     ret = 1;
    442  1.1  christos err:
    443  1.1  christos     BN_free(b);
    444  1.1  christos     BN_free(c);
    445  1.1  christos     return ret;
    446  1.1  christos }
    447  1.1  christos 
    448  1.1  christos static int test_param_real(void)
    449  1.1  christos {
    450  1.1  christos     double p;
    451  1.1  christos     OSSL_PARAM param = OSSL_PARAM_double("r", NULL);
    452  1.1  christos 
    453  1.1  christos     param.data = &p;
    454  1.1  christos     return TEST_true(OSSL_PARAM_set_double(&param, 3.14159))
    455  1.1  christos            && TEST_double_eq(p, 3.14159);
    456  1.1  christos }
    457  1.1  christos 
    458  1.1  christos static int test_param_construct(int tstid)
    459  1.1  christos {
    460  1.1  christos     static const char *int_names[] = {
    461  1.1  christos         "int", "long", "int32", "int64"
    462  1.1  christos     };
    463  1.1  christos     static const char *uint_names[] = {
    464  1.1  christos         "uint", "ulong", "uint32", "uint64", "size_t"
    465  1.1  christos     };
    466  1.1  christos     static const unsigned char bn_val[16] = {
    467  1.1  christos         0xac, 0x75, 0x22, 0x7d, 0x81, 0x06, 0x7a, 0x23,
    468  1.1  christos         0xa6, 0xed, 0x87, 0xc7, 0xab, 0xf4, 0x73, 0x22
    469  1.1  christos     };
    470  1.1  christos     OSSL_PARAM *p = NULL, *p1 = NULL;
    471  1.1  christos     static const OSSL_PARAM params_empty[] = {
    472  1.1  christos         OSSL_PARAM_END
    473  1.1  christos     };
    474  1.1  christos     OSSL_PARAM params[20];
    475  1.1  christos     char buf[100], buf2[100], *bufp, *bufp2;
    476  1.1  christos     unsigned char ubuf[100];
    477  1.1  christos     void *vp, *vpn = NULL, *vp2;
    478  1.1  christos     OSSL_PARAM *cp;
    479  1.1  christos     int i, n = 0, ret = 0;
    480  1.1  christos     unsigned int u;
    481  1.1  christos     long int l;
    482  1.1  christos     unsigned long int ul;
    483  1.1  christos     int32_t i32;
    484  1.1  christos     uint32_t u32;
    485  1.1  christos     int64_t i64;
    486  1.1  christos     uint64_t u64;
    487  1.1  christos     size_t j, k, s;
    488  1.1  christos     double d, d2;
    489  1.1  christos     BIGNUM *bn = NULL, *bn2 = NULL;
    490  1.1  christos 
    491  1.1  christos     params[n++] = OSSL_PARAM_construct_int("int", &i);
    492  1.1  christos     params[n++] = OSSL_PARAM_construct_uint("uint", &u);
    493  1.1  christos     params[n++] = OSSL_PARAM_construct_long("long", &l);
    494  1.1  christos     params[n++] = OSSL_PARAM_construct_ulong("ulong", &ul);
    495  1.1  christos     params[n++] = OSSL_PARAM_construct_int32("int32", &i32);
    496  1.1  christos     params[n++] = OSSL_PARAM_construct_int64("int64", &i64);
    497  1.1  christos     params[n++] = OSSL_PARAM_construct_uint32("uint32", &u32);
    498  1.1  christos     params[n++] = OSSL_PARAM_construct_uint64("uint64", &u64);
    499  1.1  christos     params[n++] = OSSL_PARAM_construct_size_t("size_t", &s);
    500  1.1  christos     params[n++] = OSSL_PARAM_construct_double("double", &d);
    501  1.1  christos     params[n++] = OSSL_PARAM_construct_BN("bignum", ubuf, sizeof(ubuf));
    502  1.1  christos     params[n++] = OSSL_PARAM_construct_utf8_string("utf8str", buf, sizeof(buf));
    503  1.1  christos     params[n++] = OSSL_PARAM_construct_octet_string("octstr", buf, sizeof(buf));
    504  1.1  christos     params[n++] = OSSL_PARAM_construct_utf8_ptr("utf8ptr", &bufp, 0);
    505  1.1  christos     params[n++] = OSSL_PARAM_construct_octet_ptr("octptr", &vp, 0);
    506  1.1  christos     params[n] = OSSL_PARAM_construct_end();
    507  1.1  christos 
    508  1.1  christos     switch(tstid) {
    509  1.1  christos     case 0:
    510  1.1  christos         p = params;
    511  1.1  christos         break;
    512  1.1  christos     case 1:
    513  1.1  christos         p = OSSL_PARAM_merge(params, params_empty);
    514  1.1  christos         break;
    515  1.1  christos     case 2:
    516  1.1  christos         p = OSSL_PARAM_dup(params);
    517  1.1  christos         break;
    518  1.1  christos     default:
    519  1.1  christos         p1 = OSSL_PARAM_dup(params);
    520  1.1  christos         p = OSSL_PARAM_merge(p1, params_empty);
    521  1.1  christos         break;
    522  1.1  christos     }
    523  1.1  christos 
    524  1.1  christos     /* Search failure */
    525  1.1  christos     if (!TEST_ptr_null(OSSL_PARAM_locate(p, "fnord")))
    526  1.1  christos         goto err;
    527  1.1  christos 
    528  1.1  christos     /* All signed integral types */
    529  1.1  christos     for (j = 0; j < OSSL_NELEM(int_names); j++) {
    530  1.1  christos         if (!TEST_ptr(cp = OSSL_PARAM_locate(p, int_names[j]))
    531  1.1  christos             || !TEST_true(OSSL_PARAM_set_int32(cp, (int32_t)(3 + j)))
    532  1.1  christos             || !TEST_true(OSSL_PARAM_get_int64(cp, &i64))
    533  1.1  christos             || !TEST_size_t_eq(cp->data_size, cp->return_size)
    534  1.1  christos             || !TEST_size_t_eq((size_t)i64, 3 + j)) {
    535  1.1  christos             TEST_note("iteration %zu var %s", j + 1, int_names[j]);
    536  1.1  christos             goto err;
    537  1.1  christos         }
    538  1.1  christos     }
    539  1.1  christos     /* All unsigned integral types */
    540  1.1  christos     for (j = 0; j < OSSL_NELEM(uint_names); j++) {
    541  1.1  christos         if (!TEST_ptr(cp = OSSL_PARAM_locate(p, uint_names[j]))
    542  1.1  christos             || !TEST_true(OSSL_PARAM_set_uint32(cp, (uint32_t)(3 + j)))
    543  1.1  christos             || !TEST_true(OSSL_PARAM_get_uint64(cp, &u64))
    544  1.1  christos             || !TEST_size_t_eq(cp->data_size, cp->return_size)
    545  1.1  christos             || !TEST_size_t_eq((size_t)u64, 3 + j)) {
    546  1.1  christos             TEST_note("iteration %zu var %s", j + 1, uint_names[j]);
    547  1.1  christos             goto err;
    548  1.1  christos         }
    549  1.1  christos     }
    550  1.1  christos     /* Real */
    551  1.1  christos     if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "double"))
    552  1.1  christos         || !TEST_true(OSSL_PARAM_set_double(cp, 3.14))
    553  1.1  christos         || !TEST_true(OSSL_PARAM_get_double(cp, &d2))
    554  1.1  christos         || !TEST_size_t_eq(cp->return_size, sizeof(double))
    555  1.1  christos         || !TEST_double_eq(d2, 3.14)
    556  1.1  christos         || (tstid <= 1 && !TEST_double_eq(d, d2)))
    557  1.1  christos         goto err;
    558  1.1  christos     /* UTF8 string */
    559  1.1  christos     bufp = NULL;
    560  1.1  christos     if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "utf8str"))
    561  1.1  christos         || !TEST_true(OSSL_PARAM_set_utf8_string(cp, "abcdef"))
    562  1.1  christos         || !TEST_size_t_eq(cp->return_size, sizeof("abcdef") - 1)
    563  1.1  christos         || !TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, 0))
    564  1.1  christos         || !TEST_str_eq(bufp, "abcdef")) {
    565  1.1  christos         OPENSSL_free(bufp);
    566  1.1  christos         goto err;
    567  1.1  christos     }
    568  1.1  christos     OPENSSL_free(bufp);
    569  1.1  christos     bufp = buf2;
    570  1.1  christos     if (!TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, sizeof(buf2)))
    571  1.1  christos         || !TEST_str_eq(buf2, "abcdef"))
    572  1.1  christos         goto err;
    573  1.1  christos     /* UTF8 pointer */
    574  1.1  christos     /* Note that the size of a UTF8 string does *NOT* include the NUL byte */
    575  1.1  christos     bufp = buf;
    576  1.1  christos     if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "utf8ptr"))
    577  1.1  christos         || !TEST_true(OSSL_PARAM_set_utf8_ptr(cp, "tuvwxyz"))
    578  1.1  christos         || !TEST_size_t_eq(cp->return_size, sizeof("tuvwxyz") - 1)
    579  1.1  christos         || !TEST_true(OSSL_PARAM_get_utf8_ptr(cp, (const char **)&bufp2))
    580  1.1  christos         || !TEST_str_eq(bufp2, "tuvwxyz")
    581  1.1  christos         || (tstid <= 1 && !TEST_ptr_eq(bufp2, bufp)))
    582  1.1  christos         goto err;
    583  1.1  christos     /* OCTET string */
    584  1.1  christos     if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "octstr"))
    585  1.1  christos         || !TEST_true(OSSL_PARAM_set_octet_string(cp, "abcdefghi",
    586  1.1  christos                                                   sizeof("abcdefghi")))
    587  1.1  christos         || !TEST_size_t_eq(cp->return_size, sizeof("abcdefghi")))
    588  1.1  christos         goto err;
    589  1.1  christos     /* Match the return size to avoid trailing garbage bytes */
    590  1.1  christos     cp->data_size = cp->return_size;
    591  1.1  christos     if (!TEST_true(OSSL_PARAM_get_octet_string(cp, &vpn, 0, &s))
    592  1.1  christos         || !TEST_size_t_eq(s, sizeof("abcdefghi"))
    593  1.1  christos         || !TEST_mem_eq(vpn, sizeof("abcdefghi"),
    594  1.1  christos                         "abcdefghi", sizeof("abcdefghi")))
    595  1.1  christos         goto err;
    596  1.1  christos     vp = buf2;
    597  1.1  christos     if (!TEST_true(OSSL_PARAM_get_octet_string(cp, &vp, sizeof(buf2), &s))
    598  1.1  christos         || !TEST_size_t_eq(s, sizeof("abcdefghi"))
    599  1.1  christos         || !TEST_mem_eq(vp, sizeof("abcdefghi"),
    600  1.1  christos                         "abcdefghi", sizeof("abcdefghi")))
    601  1.1  christos         goto err;
    602  1.1  christos     /* OCTET pointer */
    603  1.1  christos     vp = &l;
    604  1.1  christos     if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "octptr"))
    605  1.1  christos         || !TEST_true(OSSL_PARAM_set_octet_ptr(cp, &ul, sizeof(ul)))
    606  1.1  christos         || !TEST_size_t_eq(cp->return_size, sizeof(ul))
    607  1.1  christos         || (tstid <= 1 && !TEST_ptr_eq(vp, &ul)))
    608  1.1  christos         goto err;
    609  1.1  christos     /* Match the return size to avoid trailing garbage bytes */
    610  1.1  christos     cp->data_size = cp->return_size;
    611  1.1  christos     if (!TEST_true(OSSL_PARAM_get_octet_ptr(cp, (const void **)&vp2, &k))
    612  1.1  christos         || !TEST_size_t_eq(k, sizeof(ul))
    613  1.1  christos         || (tstid <= 1 && !TEST_ptr_eq(vp2, vp)))
    614  1.1  christos         goto err;
    615  1.1  christos     /* BIGNUM */
    616  1.1  christos     if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "bignum"))
    617  1.1  christos         || !TEST_ptr(bn = BN_lebin2bn(bn_val, (int)sizeof(bn_val), NULL))
    618  1.1  christos         || !TEST_true(OSSL_PARAM_set_BN(cp, bn))
    619  1.1  christos         || !TEST_size_t_eq(cp->data_size, cp->return_size))
    620  1.1  christos         goto err;
    621  1.1  christos     /* Match the return size to avoid trailing garbage bytes */
    622  1.1  christos     cp->data_size = cp->return_size;
    623  1.1  christos     if(!TEST_true(OSSL_PARAM_get_BN(cp, &bn2))
    624  1.1  christos         || !TEST_BN_eq(bn, bn2))
    625  1.1  christos         goto err;
    626  1.1  christos     ret = 1;
    627  1.1  christos err:
    628  1.1  christos     if (p != params)
    629  1.1  christos         OPENSSL_free(p);
    630  1.1  christos     OPENSSL_free(p1);
    631  1.1  christos     OPENSSL_free(vpn);
    632  1.1  christos     BN_free(bn);
    633  1.1  christos     BN_free(bn2);
    634  1.1  christos     return ret;
    635  1.1  christos }
    636  1.1  christos 
    637  1.1  christos static int test_param_modified(void)
    638  1.1  christos {
    639  1.1  christos     OSSL_PARAM param[3] = { OSSL_PARAM_int("a", NULL),
    640  1.1  christos                             OSSL_PARAM_int("b", NULL),
    641  1.1  christos                             OSSL_PARAM_END };
    642  1.1  christos     int a, b;
    643  1.1  christos 
    644  1.1  christos     param->data = &a;
    645  1.1  christos     param[1].data = &b;
    646  1.1  christos     if (!TEST_false(OSSL_PARAM_modified(param))
    647  1.1  christos             && !TEST_true(OSSL_PARAM_set_int32(param, 1234))
    648  1.1  christos             && !TEST_true(OSSL_PARAM_modified(param))
    649  1.1  christos             && !TEST_false(OSSL_PARAM_modified(param + 1))
    650  1.1  christos             && !TEST_true(OSSL_PARAM_set_int32(param + 1, 1))
    651  1.1  christos             && !TEST_true(OSSL_PARAM_modified(param + 1)))
    652  1.1  christos         return 0;
    653  1.1  christos     OSSL_PARAM_set_all_unmodified(param);
    654  1.1  christos     if (!TEST_false(OSSL_PARAM_modified(param))
    655  1.1  christos             && !TEST_true(OSSL_PARAM_set_int32(param, 4321))
    656  1.1  christos             && !TEST_true(OSSL_PARAM_modified(param))
    657  1.1  christos             && !TEST_false(OSSL_PARAM_modified(param + 1))
    658  1.1  christos             && !TEST_true(OSSL_PARAM_set_int32(param + 1, 2))
    659  1.1  christos             && !TEST_true(OSSL_PARAM_modified(param + 1)))
    660  1.1  christos         return 0;
    661  1.1  christos     return 1;
    662  1.1  christos }
    663  1.1  christos 
    664  1.1  christos static int test_param_copy_null(void)
    665  1.1  christos {
    666  1.1  christos     int ret, val;
    667  1.1  christos     int a = 1, b = 2, i = 0;
    668  1.1  christos     OSSL_PARAM *cp1 = NULL, *cp2 = NULL, *p;
    669  1.1  christos     OSSL_PARAM param[3];
    670  1.1  christos 
    671  1.1  christos     param[i++] = OSSL_PARAM_construct_int("a", &a);
    672  1.1  christos     param[i++] = OSSL_PARAM_construct_int("b", &b);
    673  1.1  christos     param[i] = OSSL_PARAM_construct_end();
    674  1.1  christos 
    675  1.1  christos     ret = TEST_ptr_null(OSSL_PARAM_dup(NULL))
    676  1.1  christos           && TEST_ptr(cp1 = OSSL_PARAM_merge(NULL, param))
    677  1.1  christos           && TEST_ptr(p = OSSL_PARAM_locate(cp1, "a"))
    678  1.1  christos           && TEST_true(OSSL_PARAM_get_int(p, &val))
    679  1.1  christos           && TEST_int_eq(val, 1)
    680  1.1  christos           && TEST_ptr(p = OSSL_PARAM_locate(cp1, "b"))
    681  1.1  christos           && TEST_true(OSSL_PARAM_get_int(p, &val))
    682  1.1  christos           && TEST_int_eq(val, 2)
    683  1.1  christos           && TEST_ptr(cp2 = OSSL_PARAM_merge(param, NULL))
    684  1.1  christos           && TEST_ptr(p = OSSL_PARAM_locate(cp2, "a"))
    685  1.1  christos           && TEST_true(OSSL_PARAM_get_int(p, &val))
    686  1.1  christos           && TEST_int_eq(val, 1)
    687  1.1  christos           && TEST_ptr(p = OSSL_PARAM_locate(cp2, "b"))
    688  1.1  christos           && TEST_true(OSSL_PARAM_get_int(p, &val))
    689  1.1  christos           && TEST_int_eq(val, 2)
    690  1.1  christos           && TEST_ptr_null(OSSL_PARAM_merge(NULL, NULL));
    691  1.1  christos     OSSL_PARAM_free(cp2);
    692  1.1  christos     OSSL_PARAM_free(cp1);
    693  1.1  christos     return ret;
    694  1.1  christos }
    695  1.1  christos 
    696  1.1  christos int setup_tests(void)
    697  1.1  christos {
    698  1.1  christos     ADD_ALL_TESTS(test_param_int, OSSL_NELEM(raw_values));
    699  1.1  christos     ADD_ALL_TESTS(test_param_long, OSSL_NELEM(raw_values));
    700  1.1  christos     ADD_ALL_TESTS(test_param_uint, OSSL_NELEM(raw_values));
    701  1.1  christos     ADD_ALL_TESTS(test_param_ulong, OSSL_NELEM(raw_values));
    702  1.1  christos     ADD_ALL_TESTS(test_param_int32, OSSL_NELEM(raw_values));
    703  1.1  christos     ADD_ALL_TESTS(test_param_uint32, OSSL_NELEM(raw_values));
    704  1.1  christos     ADD_ALL_TESTS(test_param_size_t, OSSL_NELEM(raw_values));
    705  1.1  christos     ADD_ALL_TESTS(test_param_time_t, OSSL_NELEM(raw_values));
    706  1.1  christos     ADD_ALL_TESTS(test_param_int64, OSSL_NELEM(raw_values));
    707  1.1  christos     ADD_ALL_TESTS(test_param_uint64, OSSL_NELEM(raw_values));
    708  1.1  christos     ADD_ALL_TESTS(test_param_bignum, OSSL_NELEM(raw_values));
    709  1.1  christos     ADD_TEST(test_param_real);
    710  1.1  christos     ADD_ALL_TESTS(test_param_construct, 4);
    711  1.1  christos     ADD_TEST(test_param_modified);
    712  1.1  christos     ADD_TEST(test_param_copy_null);
    713  1.1  christos     return 1;
    714  1.1  christos }
    715