Rich Salz | 440e5d8 | 2016-05-17 14:20:24 -0400 | [diff] [blame] | 1 | /* |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 2 | * Copyright 2014-2017 The OpenSSL Project Authors. All Rights Reserved. |
Emilia Kasper | 5a3d21c | 2014-08-28 15:33:34 +0200 | [diff] [blame] | 3 | * |
Rich Salz | 440e5d8 | 2016-05-17 14:20:24 -0400 | [diff] [blame] | 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
Emilia Kasper | 5a3d21c | 2014-08-28 15:33:34 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
Emilia Kasper | 5a3d21c | 2014-08-28 15:33:34 +0200 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | |
Rich Salz | 176db6d | 2017-08-22 08:35:43 -0400 | [diff] [blame^] | 13 | #include "internal/nelem.h" |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 14 | #include "internal/constant_time_locl.h" |
| 15 | #include "testutil.h" |
Richard Levitte | c76da13 | 2016-11-05 11:38:29 +0100 | [diff] [blame] | 16 | #include "internal/numbers.h" |
| 17 | |
Emilia Kasper | 294d1e3 | 2014-08-28 19:43:49 +0200 | [diff] [blame] | 18 | static const unsigned int CONSTTIME_TRUE = (unsigned)(~0); |
Emilia Kasper | 5a3d21c | 2014-08-28 15:33:34 +0200 | [diff] [blame] | 19 | static const unsigned int CONSTTIME_FALSE = 0; |
Emilia Kasper | 294d1e3 | 2014-08-28 19:43:49 +0200 | [diff] [blame] | 20 | static const unsigned char CONSTTIME_TRUE_8 = 0xff; |
Emilia Kasper | 5a3d21c | 2014-08-28 15:33:34 +0200 | [diff] [blame] | 21 | static const unsigned char CONSTTIME_FALSE_8 = 0; |
Matt Caswell | be2ef0e | 2016-10-25 15:29:35 +0100 | [diff] [blame] | 22 | static const size_t CONSTTIME_TRUE_S = ~((size_t)0); |
| 23 | static const size_t CONSTTIME_FALSE_S = 0; |
Rich Salz | 9018f3c | 2017-06-20 15:21:21 -0400 | [diff] [blame] | 24 | static uint64_t CONSTTIME_TRUE_64 = (uint64_t)(~(uint64_t)0); |
| 25 | static uint64_t CONSTTIME_FALSE_64 = 0; |
Emilia Kasper | 5a3d21c | 2014-08-28 15:33:34 +0200 | [diff] [blame] | 26 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 27 | static int test_binary_op(unsigned int (*op) (unsigned int a, unsigned int b), |
| 28 | const char *op_name, unsigned int a, unsigned int b, |
| 29 | int is_true) |
| 30 | { |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 31 | if (is_true && !TEST_uint_eq(op(a, b), CONSTTIME_TRUE)) |
| 32 | return 0; |
| 33 | if (!is_true && !TEST_uint_eq(op(a, b), CONSTTIME_FALSE)) |
| 34 | return 0; |
| 35 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 36 | } |
Emilia Kasper | 5a3d21c | 2014-08-28 15:33:34 +0200 | [diff] [blame] | 37 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 38 | static int test_binary_op_8(unsigned |
| 39 | char (*op) (unsigned int a, unsigned int b), |
| 40 | const char *op_name, unsigned int a, |
| 41 | unsigned int b, int is_true) |
| 42 | { |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 43 | if (is_true && !TEST_uint_eq(op(a, b), CONSTTIME_TRUE_8)) |
| 44 | return 0; |
| 45 | if (!is_true && !TEST_uint_eq(op(a, b), CONSTTIME_FALSE_8)) |
| 46 | return 0; |
| 47 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 48 | } |
Emilia Kasper | 5a3d21c | 2014-08-28 15:33:34 +0200 | [diff] [blame] | 49 | |
Matt Caswell | be2ef0e | 2016-10-25 15:29:35 +0100 | [diff] [blame] | 50 | static int test_binary_op_s(size_t (*op) (size_t a, size_t b), |
| 51 | const char *op_name, size_t a, size_t b, |
| 52 | int is_true) |
| 53 | { |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 54 | if (is_true && !TEST_size_t_eq(op(a,b), CONSTTIME_TRUE_S)) |
| 55 | return 0; |
| 56 | if (!is_true && !TEST_uint_eq(op(a,b), CONSTTIME_FALSE_S)) |
| 57 | return 0; |
| 58 | return 1; |
Matt Caswell | be2ef0e | 2016-10-25 15:29:35 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Rich Salz | 9018f3c | 2017-06-20 15:21:21 -0400 | [diff] [blame] | 61 | static int test_binary_op_64(uint64_t (*op)(uint64_t a, uint64_t b), |
| 62 | const char *op_name, uint64_t a, uint64_t b, |
| 63 | int is_true) |
| 64 | { |
| 65 | uint64_t c = op(a, b); |
| 66 | |
| 67 | if (is_true && c != CONSTTIME_TRUE_64) { |
| 68 | TEST_error("TRUE %s op failed", op_name); |
Rich Salz | 12997aa | 2017-08-15 09:42:38 -0400 | [diff] [blame] | 69 | BIO_printf(bio_err, "a=%jx b=%jx\n", a, b); |
Rich Salz | 9018f3c | 2017-06-20 15:21:21 -0400 | [diff] [blame] | 70 | return 0; |
| 71 | } else if (!is_true && c != CONSTTIME_FALSE_64) { |
| 72 | TEST_error("FALSE %s op failed", op_name); |
Rich Salz | 12997aa | 2017-08-15 09:42:38 -0400 | [diff] [blame] | 73 | BIO_printf(bio_err, "a=%jx b=%jx\n", a, b); |
Rich Salz | 9018f3c | 2017-06-20 15:21:21 -0400 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | |
Emilia Kasper | 5a3d21c | 2014-08-28 15:33:34 +0200 | [diff] [blame] | 80 | static int test_is_zero(unsigned int a) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 81 | { |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 82 | if (a == 0 && !TEST_uint_eq(constant_time_is_zero(a), CONSTTIME_TRUE)) |
| 83 | return 0; |
| 84 | if (a != 0 && !TEST_uint_eq(constant_time_is_zero(a), CONSTTIME_FALSE)) |
| 85 | return 0; |
| 86 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 87 | } |
Emilia Kasper | 5a3d21c | 2014-08-28 15:33:34 +0200 | [diff] [blame] | 88 | |
| 89 | static int test_is_zero_8(unsigned int a) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 90 | { |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 91 | if (a == 0 && !TEST_uint_eq(constant_time_is_zero_8(a), CONSTTIME_TRUE_8)) |
| 92 | return 0; |
| 93 | if (a != 0 && !TEST_uint_eq(constant_time_is_zero_8(a), CONSTTIME_FALSE_8)) |
| 94 | return 0; |
| 95 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 96 | } |
Emilia Kasper | 5a3d21c | 2014-08-28 15:33:34 +0200 | [diff] [blame] | 97 | |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 98 | static int test_is_zero_s(unsigned int a) |
Matt Caswell | be2ef0e | 2016-10-25 15:29:35 +0100 | [diff] [blame] | 99 | { |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 100 | if (a == 0 && !TEST_size_t_eq(constant_time_is_zero_s(a), CONSTTIME_TRUE_S)) |
| 101 | return 0; |
| 102 | if (a != 0 && !TEST_uint_eq(constant_time_is_zero_s(a), CONSTTIME_FALSE_S)) |
| 103 | return 0; |
| 104 | return 1; |
Matt Caswell | be2ef0e | 2016-10-25 15:29:35 +0100 | [diff] [blame] | 105 | } |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 106 | |
Emilia Kasper | 294d1e3 | 2014-08-28 19:43:49 +0200 | [diff] [blame] | 107 | static int test_select(unsigned int a, unsigned int b) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 108 | { |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 109 | if (!TEST_uint_eq(constant_time_select(CONSTTIME_TRUE, a, b), a)) |
| 110 | return 0; |
| 111 | if (!TEST_uint_eq(constant_time_select(CONSTTIME_FALSE, a, b), b)) |
| 112 | return 0; |
| 113 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 114 | } |
Emilia Kasper | 294d1e3 | 2014-08-28 19:43:49 +0200 | [diff] [blame] | 115 | |
| 116 | static int test_select_8(unsigned char a, unsigned char b) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 117 | { |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 118 | if (!TEST_uint_eq(constant_time_select_8(CONSTTIME_TRUE_8, a, b), a)) |
| 119 | return 0; |
| 120 | if (!TEST_uint_eq(constant_time_select_8(CONSTTIME_FALSE_8, a, b), b)) |
| 121 | return 0; |
| 122 | return 1; |
| 123 | } |
| 124 | |
| 125 | static int test_select_s(unsigned char a, unsigned char b) |
| 126 | { |
| 127 | if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_TRUE_S, a, b), a)) |
| 128 | return 0; |
| 129 | if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_FALSE_S, a, b), b)) |
| 130 | return 0; |
| 131 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 132 | } |
Emilia Kasper | 294d1e3 | 2014-08-28 19:43:49 +0200 | [diff] [blame] | 133 | |
Rich Salz | 9018f3c | 2017-06-20 15:21:21 -0400 | [diff] [blame] | 134 | static int test_select_64(uint64_t a, uint64_t b) |
| 135 | { |
| 136 | uint64_t selected = constant_time_select_64(CONSTTIME_TRUE_64, a, b); |
| 137 | |
| 138 | if (selected != a) { |
| 139 | TEST_error("test_select_64 TRUE failed"); |
Rich Salz | 12997aa | 2017-08-15 09:42:38 -0400 | [diff] [blame] | 140 | BIO_printf(bio_err, "a=%jx b=%jx got %jx wanted a\n", a, b, selected); |
Rich Salz | 9018f3c | 2017-06-20 15:21:21 -0400 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | selected = constant_time_select_64(CONSTTIME_FALSE_64, a, b); |
| 144 | if (selected != b) { |
Rich Salz | 12997aa | 2017-08-15 09:42:38 -0400 | [diff] [blame] | 145 | BIO_printf(bio_err, "a=%jx b=%jx got %jx wanted b\n", a, b, selected); |
Rich Salz | 9018f3c | 2017-06-20 15:21:21 -0400 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | return 1; |
| 149 | } |
| 150 | |
Emilia Kasper | 294d1e3 | 2014-08-28 19:43:49 +0200 | [diff] [blame] | 151 | static int test_select_int(int a, int b) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 152 | { |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 153 | if (!TEST_int_eq(constant_time_select_int(CONSTTIME_TRUE, a, b), a)) |
| 154 | return 0; |
| 155 | if (!TEST_int_eq(constant_time_select_int(CONSTTIME_FALSE, a, b), b)) |
| 156 | return 0; |
| 157 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 158 | } |
Emilia Kasper | 455b65d | 2014-09-04 13:04:42 +0200 | [diff] [blame] | 159 | |
| 160 | static int test_eq_int_8(int a, int b) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 161 | { |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 162 | if (a == b && !TEST_int_eq(constant_time_eq_int_8(a, b), CONSTTIME_TRUE_8)) |
| 163 | return 0; |
| 164 | if (a != b && !TEST_int_eq(constant_time_eq_int_8(a, b), CONSTTIME_FALSE_8)) |
| 165 | return 0; |
| 166 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 167 | } |
Emilia Kasper | 294d1e3 | 2014-08-28 19:43:49 +0200 | [diff] [blame] | 168 | |
Matt Caswell | be2ef0e | 2016-10-25 15:29:35 +0100 | [diff] [blame] | 169 | static int test_eq_s(size_t a, size_t b) |
| 170 | { |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 171 | if (a == b && !TEST_size_t_eq(constant_time_eq_s(a, b), CONSTTIME_TRUE_S)) |
| 172 | return 0; |
| 173 | if (a != b && !TEST_int_eq(constant_time_eq_s(a, b), CONSTTIME_FALSE_S)) |
| 174 | return 0; |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | static int test_eq_int(int a, int b) |
| 179 | { |
| 180 | if (a == b && !TEST_uint_eq(constant_time_eq_int(a, b), CONSTTIME_TRUE)) |
| 181 | return 0; |
| 182 | if (a != b && !TEST_uint_eq(constant_time_eq_int(a, b), CONSTTIME_FALSE)) |
| 183 | return 0; |
| 184 | return 1; |
Matt Caswell | be2ef0e | 2016-10-25 15:29:35 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Rich Salz | 9018f3c | 2017-06-20 15:21:21 -0400 | [diff] [blame] | 187 | static unsigned int test_values[] = { |
| 188 | 0, 1, 1024, 12345, 32000, UINT_MAX / 2 - 1, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 189 | UINT_MAX / 2, UINT_MAX / 2 + 1, UINT_MAX - 1, |
| 190 | UINT_MAX |
| 191 | }; |
Emilia Kasper | 5a3d21c | 2014-08-28 15:33:34 +0200 | [diff] [blame] | 192 | |
Rich Salz | 9018f3c | 2017-06-20 15:21:21 -0400 | [diff] [blame] | 193 | static unsigned char test_values_8[] = { |
| 194 | 0, 1, 2, 20, 32, 127, 128, 129, 255 |
| 195 | }; |
Emilia Kasper | 294d1e3 | 2014-08-28 19:43:49 +0200 | [diff] [blame] | 196 | |
Rich Salz | 9018f3c | 2017-06-20 15:21:21 -0400 | [diff] [blame] | 197 | static int signed_test_values[] = { |
| 198 | 0, 1, -1, 1024, -1024, 12345, -12345, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 199 | 32000, -32000, INT_MAX, INT_MIN, INT_MAX - 1, |
| 200 | INT_MIN + 1 |
| 201 | }; |
Emilia Kasper | 294d1e3 | 2014-08-28 19:43:49 +0200 | [diff] [blame] | 202 | |
Rich Salz | 9018f3c | 2017-06-20 15:21:21 -0400 | [diff] [blame] | 203 | static size_t test_values_s[] = { |
| 204 | 0, 1, 1024, 12345, 32000, SIZE_MAX / 2 - 1, |
Matt Caswell | be2ef0e | 2016-10-25 15:29:35 +0100 | [diff] [blame] | 205 | SIZE_MAX / 2, SIZE_MAX / 2 + 1, SIZE_MAX - 1, |
| 206 | SIZE_MAX |
| 207 | }; |
| 208 | |
Rich Salz | 9018f3c | 2017-06-20 15:21:21 -0400 | [diff] [blame] | 209 | static uint64_t test_values_64[] = { |
| 210 | 0, 1, 1024, 12345, 32000, 32000000, 32000000001, UINT64_MAX / 2, |
| 211 | UINT64_MAX / 2 + 1, UINT64_MAX - 1, UINT64_MAX |
| 212 | }; |
| 213 | |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 214 | static int test_sizeofs(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 215 | { |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 216 | if (!TEST_uint_eq(OSSL_NELEM(test_values), OSSL_NELEM(test_values_s))) |
| 217 | return 0; |
| 218 | return 1; |
| 219 | } |
Emilia Kasper | 5a3d21c | 2014-08-28 15:33:34 +0200 | [diff] [blame] | 220 | |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 221 | static int test_binops(int i) |
| 222 | { |
| 223 | unsigned int a = test_values[i]; |
| 224 | unsigned int g = test_values_s[i]; |
| 225 | int j; |
| 226 | int ret = 1; |
Matt Caswell | be2ef0e | 2016-10-25 15:29:35 +0100 | [diff] [blame] | 227 | |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 228 | if (!test_is_zero(a) || !test_is_zero_8(a) || !test_is_zero_s(g)) |
| 229 | ret = 0; |
| 230 | |
| 231 | for (j = 0; j < (int)OSSL_NELEM(test_values); ++j) { |
| 232 | unsigned int b = test_values[j]; |
| 233 | unsigned int h = test_values[j]; |
| 234 | |
| 235 | if (!test_select(a, b) |
| 236 | || !test_select_s(g, h) |
| 237 | || !test_eq_s(g, h) |
| 238 | || !test_binary_op(&constant_time_lt, "ct_lt", |
| 239 | a, b, a < b) |
| 240 | || !test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8", |
| 241 | a, b, a < b) |
| 242 | || !test_binary_op_s(&constant_time_lt_s, "constant_time_lt_s", |
| 243 | g, h, g < h) |
| 244 | || !test_binary_op(&constant_time_lt, "constant_time_lt", |
| 245 | b, a, b < a) |
| 246 | || !test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8", |
| 247 | b, a, b < a) |
| 248 | || !test_binary_op_s(&constant_time_lt_s, "constant_time_lt_s", |
| 249 | h, g, h < g) |
| 250 | || !test_binary_op(&constant_time_ge, "constant_time_ge", |
| 251 | a, b, a >= b) |
| 252 | || !test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8", |
| 253 | a, b, a >= b) |
| 254 | || !test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s", |
| 255 | g, h, g >= h) |
| 256 | || !test_binary_op(&constant_time_ge, "constant_time_ge", |
| 257 | b, a, b >= a) |
| 258 | || !test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8", |
| 259 | b, a, b >= a) |
| 260 | || !test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s", |
| 261 | h, g, h >= g) |
| 262 | || !test_binary_op(&constant_time_eq, "constant_time_eq", |
| 263 | a, b, a == b) |
| 264 | || !test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8", |
| 265 | a, b, a == b) |
| 266 | || !test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s", |
| 267 | g, h, g == h) |
| 268 | || !test_binary_op(&constant_time_eq, "constant_time_eq", |
| 269 | b, a, b == a) |
| 270 | || !test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8", |
| 271 | b, a, b == a) |
| 272 | || !test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s", |
| 273 | h, g, h == g)) { |
| 274 | ret = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 277 | return ret; |
| 278 | } |
Emilia Kasper | 294d1e3 | 2014-08-28 19:43:49 +0200 | [diff] [blame] | 279 | |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 280 | static int test_signed(int i) |
| 281 | { |
| 282 | int c = signed_test_values[i]; |
| 283 | unsigned int j; |
| 284 | int ret = 1; |
Emilia Kasper | 294d1e3 | 2014-08-28 19:43:49 +0200 | [diff] [blame] | 285 | |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 286 | for (j = 0; j < OSSL_NELEM(signed_test_values); ++j) { |
| 287 | int d = signed_test_values[j]; |
Emilia Kasper | 5a3d21c | 2014-08-28 15:33:34 +0200 | [diff] [blame] | 288 | |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 289 | if (!test_select_int(c, d) |
| 290 | || !test_eq_int(c, d) |
| 291 | || !test_eq_int_8(c, d)) |
| 292 | ret = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 293 | } |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | static int test_8values(int i) |
| 298 | { |
| 299 | unsigned char e = test_values_8[i]; |
| 300 | unsigned int j; |
| 301 | int ret = 1; |
| 302 | |
| 303 | for (j = 0; j < sizeof(test_values_8); ++j) { |
| 304 | unsigned char f = test_values_8[j]; |
| 305 | |
| 306 | if (!test_select_8(e, f)) |
| 307 | ret = 0; |
| 308 | } |
| 309 | return ret; |
| 310 | } |
| 311 | |
Rich Salz | 9018f3c | 2017-06-20 15:21:21 -0400 | [diff] [blame] | 312 | static int test_64values(int i) |
| 313 | { |
| 314 | uint64_t g = test_values_64[i]; |
| 315 | int j, ret = 1; |
| 316 | |
| 317 | for (j = i + 1; j < (int)OSSL_NELEM(test_values_64); j++) { |
| 318 | uint64_t h = test_values_64[j]; |
| 319 | |
| 320 | if (!test_binary_op_64(&constant_time_lt_64, "constant_time_lt_64", |
| 321 | g, h, g < h) |
| 322 | || !test_select_64(g, h)) { |
| 323 | TEST_info("test_64values failed i=%d j=%d", i, j); |
| 324 | ret = 0; |
| 325 | } |
| 326 | } |
| 327 | return ret; |
| 328 | } |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 329 | |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 330 | int setup_tests(void) |
Rich Salz | 3304d57 | 2017-04-18 14:50:00 -0400 | [diff] [blame] | 331 | { |
| 332 | ADD_TEST(test_sizeofs); |
| 333 | ADD_ALL_TESTS(test_binops, OSSL_NELEM(test_values)); |
| 334 | ADD_ALL_TESTS(test_signed, OSSL_NELEM(signed_test_values)); |
Matt Caswell | e390850 | 2017-06-23 10:10:51 +0100 | [diff] [blame] | 335 | ADD_ALL_TESTS(test_8values, OSSL_NELEM(test_values_8)); |
| 336 | ADD_ALL_TESTS(test_64values, OSSL_NELEM(test_values_64)); |
Pauli | ad88741 | 2017-07-18 11:48:27 +1000 | [diff] [blame] | 337 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 338 | } |