Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Self-test demonstration program |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Mateusz Starzyk | 6c2e9b6 | 2021-05-19 17:54:54 +0200 | [diff] [blame] | 20 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 21 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 22 | #include "mbedtls/build_info.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/entropy.h" |
| 25 | #include "mbedtls/hmac_drbg.h" |
| 26 | #include "mbedtls/ctr_drbg.h" |
| 27 | #include "mbedtls/dhm.h" |
| 28 | #include "mbedtls/gcm.h" |
| 29 | #include "mbedtls/ccm.h" |
Robert Cragie | dc5c7b9 | 2015-12-11 15:49:45 +0000 | [diff] [blame] | 30 | #include "mbedtls/cmac.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/md5.h" |
| 32 | #include "mbedtls/ripemd160.h" |
| 33 | #include "mbedtls/sha1.h" |
| 34 | #include "mbedtls/sha256.h" |
| 35 | #include "mbedtls/sha512.h" |
Pol Henarejos | 7dbd5d1 | 2022-05-20 20:42:33 +0200 | [diff] [blame] | 36 | #include "mbedtls/sha3.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 37 | #include "mbedtls/des.h" |
| 38 | #include "mbedtls/aes.h" |
| 39 | #include "mbedtls/camellia.h" |
Markku-Juhani O. Saarinen | 3c0b53b | 2017-11-30 16:00:34 +0000 | [diff] [blame] | 40 | #include "mbedtls/aria.h" |
Daniel King | 4d8f87b | 2016-05-18 10:09:28 -0300 | [diff] [blame] | 41 | #include "mbedtls/chacha20.h" |
| 42 | #include "mbedtls/poly1305.h" |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 43 | #include "mbedtls/chachapoly.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 44 | #include "mbedtls/base64.h" |
| 45 | #include "mbedtls/bignum.h" |
| 46 | #include "mbedtls/rsa.h" |
| 47 | #include "mbedtls/x509.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 48 | #include "mbedtls/pkcs5.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 49 | #include "mbedtls/ecp.h" |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 50 | #include "mbedtls/ecjpake.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 51 | #include "mbedtls/timing.h" |
Ron Eldor | 9ab746c | 2018-07-15 09:33:07 +0300 | [diff] [blame] | 52 | #include "mbedtls/nist_kw.h" |
Dave Rodgman | 8f5a29a | 2022-04-11 12:59:45 +0100 | [diff] [blame] | 53 | #include "mbedtls/debug.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 54 | |
Hanno Becker | 4ab3850 | 2018-10-16 13:22:44 +0100 | [diff] [blame] | 55 | #include <limits.h> |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 56 | #include <string.h> |
| 57 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 58 | #include "mbedtls/platform.h" |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 59 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 61 | #include "mbedtls/memory_buffer_alloc.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 62 | #endif |
| 63 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 64 | |
Gilles Peskine | 6fc21f6 | 2019-09-17 18:18:58 +0200 | [diff] [blame] | 65 | #if defined MBEDTLS_SELF_TEST |
| 66 | /* Sanity check for malloc. This is not expected to fail, and is rather |
| 67 | * intended to display potentially useful information about the platform, |
| 68 | * in particular the behavior of malloc(0). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | static int calloc_self_test(int verbose) |
Gilles Peskine | 6fc21f6 | 2019-09-17 18:18:58 +0200 | [diff] [blame] | 70 | { |
| 71 | int failures = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | void *empty1 = mbedtls_calloc(0, 1); |
| 73 | void *empty2 = mbedtls_calloc(0, 1); |
| 74 | void *buffer1 = mbedtls_calloc(1, 1); |
| 75 | void *buffer2 = mbedtls_calloc(1, 1); |
Andrzej Kurek | e35f3a2 | 2023-05-04 17:29:55 -0400 | [diff] [blame] | 76 | unsigned int buffer_3_size = 256; |
| 77 | unsigned int buffer_4_size = 4097; /* Allocate more than the usual page size */ |
| 78 | unsigned char *buffer3 = mbedtls_calloc(buffer_3_size, 1); |
| 79 | unsigned char *buffer4 = mbedtls_calloc(buffer_4_size, 1); |
Andrzej Kurek | aae3208 | 2023-05-06 08:52:50 -0400 | [diff] [blame^] | 80 | #pragma GCC diagnostic push |
| 81 | #pragma GCC diagnostic ignored "-Walloc-size-larger-than=" |
| 82 | unsigned char *buffer5 = mbedtls_calloc(SIZE_MAX/2, SIZE_MAX/2); |
| 83 | #pragma GCC diagnostic pop |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 84 | if (empty1 == NULL && empty2 == NULL) { |
| 85 | if (verbose) { |
Andrzej Kurek | 9032711 | 2023-04-27 09:30:18 -0400 | [diff] [blame] | 86 | mbedtls_printf(" CALLOC(0,1): passed (NULL)\n"); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | } |
| 88 | } else if (empty1 == NULL || empty2 == NULL) { |
| 89 | if (verbose) { |
Andrzej Kurek | 9032711 | 2023-04-27 09:30:18 -0400 | [diff] [blame] | 90 | mbedtls_printf(" CALLOC(0,1): failed (mix of NULL and non-NULL)\n"); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | } |
Gilles Peskine | 6fc21f6 | 2019-09-17 18:18:58 +0200 | [diff] [blame] | 92 | ++failures; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | } else if (empty1 == empty2) { |
| 94 | if (verbose) { |
Andrzej Kurek | 9032711 | 2023-04-27 09:30:18 -0400 | [diff] [blame] | 95 | mbedtls_printf(" CALLOC(0,1): passed (same non-null)\n"); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | } |
| 97 | } else { |
| 98 | if (verbose) { |
Andrzej Kurek | 9032711 | 2023-04-27 09:30:18 -0400 | [diff] [blame] | 99 | mbedtls_printf(" CALLOC(0,1): passed (distinct non-null)\n"); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | mbedtls_free(empty1); |
| 104 | mbedtls_free(empty2); |
| 105 | |
| 106 | empty1 = mbedtls_calloc(1, 0); |
| 107 | empty2 = mbedtls_calloc(1, 0); |
| 108 | if (empty1 == NULL && empty2 == NULL) { |
| 109 | if (verbose) { |
| 110 | mbedtls_printf(" CALLOC(1,0): passed (NULL)\n"); |
| 111 | } |
| 112 | } else if (empty1 == NULL || empty2 == NULL) { |
| 113 | if (verbose) { |
| 114 | mbedtls_printf(" CALLOC(1,0): failed (mix of NULL and non-NULL)\n"); |
| 115 | } |
| 116 | ++failures; |
| 117 | } else if (empty1 == empty2) { |
| 118 | if (verbose) { |
| 119 | mbedtls_printf(" CALLOC(1,0): passed (same non-null)\n"); |
| 120 | } |
| 121 | } else { |
| 122 | if (verbose) { |
| 123 | mbedtls_printf(" CALLOC(1,0): passed (distinct non-null)\n"); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | } |
Gilles Peskine | 6fc21f6 | 2019-09-17 18:18:58 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | if (buffer1 == NULL || buffer2 == NULL) { |
| 128 | if (verbose) { |
| 129 | mbedtls_printf(" CALLOC(1): failed (NULL)\n"); |
| 130 | } |
Gilles Peskine | 6fc21f6 | 2019-09-17 18:18:58 +0200 | [diff] [blame] | 131 | ++failures; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 132 | } else if (buffer1 == buffer2) { |
| 133 | if (verbose) { |
| 134 | mbedtls_printf(" CALLOC(1): failed (same buffer twice)\n"); |
| 135 | } |
Gilles Peskine | 6fc21f6 | 2019-09-17 18:18:58 +0200 | [diff] [blame] | 136 | ++failures; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | } else { |
| 138 | if (verbose) { |
| 139 | mbedtls_printf(" CALLOC(1): passed\n"); |
| 140 | } |
Gilles Peskine | 6fc21f6 | 2019-09-17 18:18:58 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | mbedtls_free(buffer1); |
| 144 | buffer1 = mbedtls_calloc(1, 1); |
| 145 | if (buffer1 == NULL) { |
| 146 | if (verbose) { |
| 147 | mbedtls_printf(" CALLOC(1 again): failed (NULL)\n"); |
| 148 | } |
Gilles Peskine | 6fc21f6 | 2019-09-17 18:18:58 +0200 | [diff] [blame] | 149 | ++failures; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | } else { |
| 151 | if (verbose) { |
| 152 | mbedtls_printf(" CALLOC(1 again): passed\n"); |
| 153 | } |
Gilles Peskine | 6fc21f6 | 2019-09-17 18:18:58 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Andrzej Kurek | e35f3a2 | 2023-05-04 17:29:55 -0400 | [diff] [blame] | 156 | for (unsigned int i = 0; i < buffer_3_size; i++) { |
Andrzej Kurek | 9032711 | 2023-04-27 09:30:18 -0400 | [diff] [blame] | 157 | if (buffer3[i] != 0) { |
| 158 | ++failures; |
| 159 | if (verbose) { |
Andrzej Kurek | e35f3a2 | 2023-05-04 17:29:55 -0400 | [diff] [blame] | 160 | mbedtls_printf(" CALLOC(%u): failed (memory not initialized to 0)\n", |
| 161 | buffer_3_size); |
| 162 | } |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | for (unsigned int i = 0; i < buffer_4_size; i++) { |
| 168 | if (buffer4[i] != 0) { |
| 169 | ++failures; |
| 170 | if (verbose) { |
| 171 | mbedtls_printf(" CALLOC(%u): failed (memory not initialized to 0)\n", |
| 172 | buffer_4_size); |
Andrzej Kurek | 9032711 | 2023-04-27 09:30:18 -0400 | [diff] [blame] | 173 | } |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
Andrzej Kurek | aae3208 | 2023-05-06 08:52:50 -0400 | [diff] [blame^] | 178 | if (buffer5 != NULL) { |
| 179 | ++failures; |
| 180 | if (verbose) { |
| 181 | mbedtls_printf(" CALLOC(SIZE_MAX/2, SIZE_MAX/2): failed (returned a valid pointer)\n"); |
| 182 | } |
| 183 | } |
| 184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 185 | if (verbose) { |
| 186 | mbedtls_printf("\n"); |
| 187 | } |
| 188 | mbedtls_free(empty1); |
| 189 | mbedtls_free(empty2); |
| 190 | mbedtls_free(buffer1); |
| 191 | mbedtls_free(buffer2); |
Andrzej Kurek | 9032711 | 2023-04-27 09:30:18 -0400 | [diff] [blame] | 192 | mbedtls_free(buffer3); |
Andrzej Kurek | e35f3a2 | 2023-05-04 17:29:55 -0400 | [diff] [blame] | 193 | mbedtls_free(buffer4); |
Andrzej Kurek | aae3208 | 2023-05-06 08:52:50 -0400 | [diff] [blame^] | 194 | mbedtls_free(buffer5); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | return failures; |
Gilles Peskine | 6fc21f6 | 2019-09-17 18:18:58 +0200 | [diff] [blame] | 196 | } |
| 197 | #endif /* MBEDTLS_SELF_TEST */ |
| 198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 199 | static int test_snprintf(size_t n, const char *ref_buf, int ref_ret) |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 200 | { |
| 201 | int ret; |
| 202 | char buf[10] = "xxxxxxxxx"; |
Manuel Pégourié-Gonnard | 4b00f08 | 2015-06-26 11:24:32 +0200 | [diff] [blame] | 203 | const char ref[10] = "xxxxxxxxx"; |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 205 | ret = mbedtls_snprintf(buf, n, "%s", "123"); |
| 206 | if (ret < 0 || (size_t) ret >= n) { |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 207 | ret = -1; |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | if (strncmp(ref_buf, buf, sizeof(buf)) != 0 || |
| 211 | ref_ret != ret || |
| 212 | memcmp(buf + n, ref + n, sizeof(buf) - n) != 0) { |
| 213 | return 1; |
| 214 | } |
| 215 | |
| 216 | return 0; |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 217 | } |
| 218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 219 | static int run_test_snprintf(void) |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 220 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 221 | return test_snprintf(0, "xxxxxxxxx", -1) != 0 || |
| 222 | test_snprintf(1, "", -1) != 0 || |
| 223 | test_snprintf(2, "1", -1) != 0 || |
| 224 | test_snprintf(3, "12", -1) != 0 || |
| 225 | test_snprintf(4, "123", 3) != 0 || |
| 226 | test_snprintf(5, "123", 3) != 0; |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Simon Butcher | b6a73c9 | 2016-06-18 22:45:37 +0100 | [diff] [blame] | 229 | /* |
| 230 | * Check if a seed file is present, and if not create one for the entropy |
| 231 | * self-test. If this fails, we attempt the test anyway, so no error is passed |
| 232 | * back. |
| 233 | */ |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 234 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C) |
| 235 | #if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | static void create_entropy_seed_file(void) |
Simon Butcher | b6a73c9 | 2016-06-18 22:45:37 +0100 | [diff] [blame] | 237 | { |
| 238 | int result; |
| 239 | size_t output_len = 0; |
| 240 | unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 241 | |
| 242 | /* Attempt to read the entropy seed file. If this fails - attempt to write |
| 243 | * to the file to ensure one is present. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 244 | result = mbedtls_platform_std_nv_seed_read(seed_value, |
| 245 | MBEDTLS_ENTROPY_BLOCK_SIZE); |
| 246 | if (0 == result) { |
Simon Butcher | b6a73c9 | 2016-06-18 22:45:37 +0100 | [diff] [blame] | 247 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 248 | } |
Simon Butcher | b6a73c9 | 2016-06-18 22:45:37 +0100 | [diff] [blame] | 249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 250 | result = mbedtls_platform_entropy_poll(NULL, |
| 251 | seed_value, |
| 252 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 253 | &output_len); |
| 254 | if (0 != result) { |
Simon Butcher | b6a73c9 | 2016-06-18 22:45:37 +0100 | [diff] [blame] | 255 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | } |
Simon Butcher | b6a73c9 | 2016-06-18 22:45:37 +0100 | [diff] [blame] | 257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | if (MBEDTLS_ENTROPY_BLOCK_SIZE != output_len) { |
Simon Butcher | b6a73c9 | 2016-06-18 22:45:37 +0100 | [diff] [blame] | 259 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 260 | } |
Simon Butcher | b6a73c9 | 2016-06-18 22:45:37 +0100 | [diff] [blame] | 261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 262 | mbedtls_platform_std_nv_seed_write(seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE); |
Simon Butcher | b6a73c9 | 2016-06-18 22:45:37 +0100 | [diff] [blame] | 263 | } |
| 264 | #endif |
| 265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 266 | int mbedtls_entropy_self_test_wrapper(int verbose) |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 267 | { |
| 268 | #if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 269 | create_entropy_seed_file(); |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 270 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | return mbedtls_entropy_self_test(verbose); |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 272 | } |
| 273 | #endif |
| 274 | |
| 275 | #if defined(MBEDTLS_SELF_TEST) |
| 276 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | int mbedtls_memory_buffer_alloc_free_and_self_test(int verbose) |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 278 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 279 | if (verbose != 0) { |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 280 | #if defined(MBEDTLS_MEMORY_DEBUG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 281 | mbedtls_memory_buffer_alloc_status(); |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 282 | #endif |
| 283 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | mbedtls_memory_buffer_alloc_free(); |
| 285 | return mbedtls_memory_buffer_alloc_self_test(verbose); |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 286 | } |
| 287 | #endif |
| 288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 289 | typedef struct { |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 290 | const char *name; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | int (*function)(int); |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 292 | } selftest_t; |
| 293 | |
| 294 | const selftest_t selftests[] = |
| 295 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 296 | { "calloc", calloc_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 297 | #if defined(MBEDTLS_MD5_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 298 | { "md5", mbedtls_md5_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 299 | #endif |
| 300 | #if defined(MBEDTLS_RIPEMD160_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 301 | { "ripemd160", mbedtls_ripemd160_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 302 | #endif |
| 303 | #if defined(MBEDTLS_SHA1_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 304 | { "sha1", mbedtls_sha1_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 305 | #endif |
Valerio Setti | 46e8fd8 | 2022-12-14 10:58:02 +0100 | [diff] [blame] | 306 | #if defined(MBEDTLS_SHA224_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 307 | { "sha224", mbedtls_sha224_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 308 | #endif |
| 309 | #if defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | { "sha256", mbedtls_sha256_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 311 | #endif |
Valerio Setti | 898e7a3 | 2022-12-14 08:55:53 +0100 | [diff] [blame] | 312 | #if defined(MBEDTLS_SHA384_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 313 | { "sha384", mbedtls_sha384_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 314 | #endif |
| 315 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 316 | { "sha512", mbedtls_sha512_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 317 | #endif |
Pol Henarejos | 7dbd5d1 | 2022-05-20 20:42:33 +0200 | [diff] [blame] | 318 | #if defined(MBEDTLS_SHA3_C) |
Pol Henarejos | b3b220c | 2023-02-08 12:52:18 +0100 | [diff] [blame] | 319 | { "sha3", mbedtls_sha3_self_test }, |
Pol Henarejos | 7dbd5d1 | 2022-05-20 20:42:33 +0200 | [diff] [blame] | 320 | #endif |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 321 | #if defined(MBEDTLS_DES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | { "des", mbedtls_des_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 323 | #endif |
| 324 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 325 | { "aes", mbedtls_aes_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 326 | #endif |
| 327 | #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | { "gcm", mbedtls_gcm_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 329 | #endif |
| 330 | #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 331 | { "ccm", mbedtls_ccm_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 332 | #endif |
Ron Eldor | 9ab746c | 2018-07-15 09:33:07 +0300 | [diff] [blame] | 333 | #if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 334 | { "nist_kw", mbedtls_nist_kw_self_test }, |
Ron Eldor | 9ab746c | 2018-07-15 09:33:07 +0300 | [diff] [blame] | 335 | #endif |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 336 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 337 | { "cmac", mbedtls_cmac_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 338 | #endif |
Daniel King | 4d8f87b | 2016-05-18 10:09:28 -0300 | [diff] [blame] | 339 | #if defined(MBEDTLS_CHACHA20_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | { "chacha20", mbedtls_chacha20_self_test }, |
Daniel King | 4d8f87b | 2016-05-18 10:09:28 -0300 | [diff] [blame] | 341 | #endif |
| 342 | #if defined(MBEDTLS_POLY1305_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 343 | { "poly1305", mbedtls_poly1305_self_test }, |
Daniel King | 4d8f87b | 2016-05-18 10:09:28 -0300 | [diff] [blame] | 344 | #endif |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 345 | #if defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 346 | { "chacha20-poly1305", mbedtls_chachapoly_self_test }, |
Daniel King | 4d8f87b | 2016-05-18 10:09:28 -0300 | [diff] [blame] | 347 | #endif |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 348 | #if defined(MBEDTLS_BASE64_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 349 | { "base64", mbedtls_base64_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 350 | #endif |
| 351 | #if defined(MBEDTLS_BIGNUM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 352 | { "mpi", mbedtls_mpi_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 353 | #endif |
| 354 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 355 | { "rsa", mbedtls_rsa_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 356 | #endif |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 357 | #if defined(MBEDTLS_CAMELLIA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 358 | { "camellia", mbedtls_camellia_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 359 | #endif |
Markku-Juhani O. Saarinen | 3c0b53b | 2017-11-30 16:00:34 +0000 | [diff] [blame] | 360 | #if defined(MBEDTLS_ARIA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 361 | { "aria", mbedtls_aria_self_test }, |
Markku-Juhani O. Saarinen | 3c0b53b | 2017-11-30 16:00:34 +0000 | [diff] [blame] | 362 | #endif |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 363 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 364 | { "ctr_drbg", mbedtls_ctr_drbg_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 365 | #endif |
| 366 | #if defined(MBEDTLS_HMAC_DRBG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 367 | { "hmac_drbg", mbedtls_hmac_drbg_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 368 | #endif |
| 369 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 370 | { "ecp", mbedtls_ecp_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 371 | #endif |
| 372 | #if defined(MBEDTLS_ECJPAKE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 373 | { "ecjpake", mbedtls_ecjpake_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 374 | #endif |
| 375 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 376 | { "dhm", mbedtls_dhm_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 377 | #endif |
| 378 | #if defined(MBEDTLS_ENTROPY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 379 | { "entropy", mbedtls_entropy_self_test_wrapper }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 380 | #endif |
| 381 | #if defined(MBEDTLS_PKCS5_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 382 | { "pkcs5", mbedtls_pkcs5_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 383 | #endif |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 384 | /* Heap test comes last */ |
| 385 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | { "memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test }, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 387 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 388 | { NULL, NULL } |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 389 | }; |
Gilles Peskine | c82fbb4 | 2017-12-15 15:01:27 +0100 | [diff] [blame] | 390 | #endif /* MBEDTLS_SELF_TEST */ |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 392 | int main(int argc, char *argv[]) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 393 | { |
Gilles Peskine | c82fbb4 | 2017-12-15 15:01:27 +0100 | [diff] [blame] | 394 | #if defined(MBEDTLS_SELF_TEST) |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 395 | const selftest_t *test; |
Gilles Peskine | c82fbb4 | 2017-12-15 15:01:27 +0100 | [diff] [blame] | 396 | #endif /* MBEDTLS_SELF_TEST */ |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 397 | char **argp; |
| 398 | int v = 1; /* v=1 for verbose mode */ |
| 399 | int exclude_mode = 0; |
| 400 | int suites_tested = 0, suites_failed = 0; |
Paul Bakker | 70940ca | 2016-07-14 14:30:45 +0100 | [diff] [blame] | 401 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 402 | unsigned char buf[1000000]; |
| 403 | #endif |
Manuel Pégourié-Gonnard | d14acbc | 2015-05-29 11:26:37 +0200 | [diff] [blame] | 404 | void *pointer; |
| 405 | |
| 406 | /* |
Dave Rodgman | 5464773 | 2023-02-10 16:16:17 +0000 | [diff] [blame] | 407 | * Check some basic platform requirements as specified in README.md |
| 408 | */ |
| 409 | if (SIZE_MAX < INT_MAX || SIZE_MAX < UINT_MAX) { |
| 410 | mbedtls_printf("SIZE_MAX must be at least as big as INT_MAX and UINT_MAX\n"); |
| 411 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 412 | } |
| 413 | |
| 414 | if (sizeof(int) < 4) { |
| 415 | mbedtls_printf("int must be at least 32 bits\n"); |
| 416 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 417 | } |
| 418 | |
| 419 | if (sizeof(size_t) < 4) { |
| 420 | mbedtls_printf("size_t must be at least 32 bits\n"); |
| 421 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 422 | } |
| 423 | |
| 424 | uint32_t endian_test = 0x12345678; |
| 425 | char *p = (char *) &endian_test; |
| 426 | if (!(p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78) && |
| 427 | !(p[3] == 0x12 && p[2] == 0x34 && p[1] == 0x56 && p[0] == 0x78)) { |
| 428 | mbedtls_printf("Mixed-endian platforms are not supported\n"); |
| 429 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 430 | } |
| 431 | |
| 432 | /* |
Manuel Pégourié-Gonnard | d14acbc | 2015-05-29 11:26:37 +0200 | [diff] [blame] | 433 | * The C standard doesn't guarantee that all-bits-0 is the representation |
| 434 | * of a NULL pointer. We do however use that in our code for initializing |
| 435 | * structures, which should work on every modern platform. Let's be sure. |
| 436 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 437 | memset(&pointer, 0, sizeof(void *)); |
| 438 | if (pointer != NULL) { |
| 439 | mbedtls_printf("all-bits-zero is not a NULL pointer\n"); |
| 440 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
Manuel Pégourié-Gonnard | d14acbc | 2015-05-29 11:26:37 +0200 | [diff] [blame] | 441 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 442 | |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 443 | /* |
Hanno Becker | 4ab3850 | 2018-10-16 13:22:44 +0100 | [diff] [blame] | 444 | * The C standard allows padding bits in the representation |
| 445 | * of standard integer types, but our code does currently not |
| 446 | * support them. |
| 447 | * |
| 448 | * Here we check that the underlying C implementation doesn't |
| 449 | * use padding bits, and fail cleanly if it does. |
| 450 | * |
| 451 | * The check works by casting the maximum value representable |
| 452 | * by a given integer type into the unpadded integer type of the |
| 453 | * same bit-width and checking that it agrees with the maximum value |
| 454 | * of that unpadded type. For example, for a 4-byte int, |
| 455 | * MAX_INT should be 0x7fffffff in int32_t. This assumes that |
| 456 | * CHAR_BIT == 8, which is checked in check_config.h. |
Hanno Becker | baae59c | 2018-10-19 17:32:45 +0100 | [diff] [blame] | 457 | * |
| 458 | * We assume that [u]intxx_t exist and that they don't |
| 459 | * have padding bits, as the standard requires. |
Hanno Becker | 4ab3850 | 2018-10-16 13:22:44 +0100 | [diff] [blame] | 460 | */ |
| 461 | |
| 462 | #define CHECK_PADDING_SIGNED(TYPE, NAME) \ |
| 463 | do \ |
| 464 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | if (sizeof(TYPE) == 2 || sizeof(TYPE) == 4 || \ |
| 466 | sizeof(TYPE) == 8) { \ |
| 467 | if ((sizeof(TYPE) == 2 && \ |
| 468 | (int16_t) NAME ## _MAX != 0x7FFF) || \ |
| 469 | (sizeof(TYPE) == 4 && \ |
| 470 | (int32_t) NAME ## _MAX != 0x7FFFFFFF) || \ |
| 471 | (sizeof(TYPE) == 8 && \ |
| 472 | (int64_t) NAME ## _MAX != 0x7FFFFFFFFFFFFFFF)) \ |
Dave Rodgman | e2e7e94 | 2022-04-08 12:41:23 +0100 | [diff] [blame] | 473 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | mbedtls_printf("Type '" #TYPE "' has padding bits\n"); \ |
| 475 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); \ |
Dave Rodgman | e2e7e94 | 2022-04-08 12:41:23 +0100 | [diff] [blame] | 476 | } \ |
| 477 | } else { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 478 | mbedtls_printf("Padding checks only implemented for types of size 2, 4 or 8" \ |
| 479 | " - cannot check type '" #TYPE "' of size %" MBEDTLS_PRINTF_SIZET "\n", \ |
| 480 | sizeof(TYPE)); \ |
| 481 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); \ |
Hanno Becker | 4ab3850 | 2018-10-16 13:22:44 +0100 | [diff] [blame] | 482 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | } while (0) |
Hanno Becker | 4ab3850 | 2018-10-16 13:22:44 +0100 | [diff] [blame] | 484 | |
| 485 | #define CHECK_PADDING_UNSIGNED(TYPE, NAME) \ |
| 486 | do \ |
| 487 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 488 | if ((sizeof(TYPE) == 2 && \ |
| 489 | (uint16_t) NAME ## _MAX != 0xFFFF) || \ |
| 490 | (sizeof(TYPE) == 4 && \ |
| 491 | (uint32_t) NAME ## _MAX != 0xFFFFFFFF) || \ |
| 492 | (sizeof(TYPE) == 8 && \ |
| 493 | (uint64_t) NAME ## _MAX != 0xFFFFFFFFFFFFFFFF)) \ |
Hanno Becker | 4ab3850 | 2018-10-16 13:22:44 +0100 | [diff] [blame] | 494 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | mbedtls_printf("Type '" #TYPE "' has padding bits\n"); \ |
| 496 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); \ |
Hanno Becker | 4ab3850 | 2018-10-16 13:22:44 +0100 | [diff] [blame] | 497 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 498 | } while (0) |
Hanno Becker | 4ab3850 | 2018-10-16 13:22:44 +0100 | [diff] [blame] | 499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 500 | CHECK_PADDING_SIGNED(short, SHRT); |
| 501 | CHECK_PADDING_SIGNED(int, INT); |
| 502 | CHECK_PADDING_SIGNED(long, LONG); |
| 503 | CHECK_PADDING_SIGNED(long long, LLONG); |
| 504 | CHECK_PADDING_SIGNED(ptrdiff_t, PTRDIFF); |
Hanno Becker | 4ab3850 | 2018-10-16 13:22:44 +0100 | [diff] [blame] | 505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 506 | CHECK_PADDING_UNSIGNED(unsigned short, USHRT); |
| 507 | CHECK_PADDING_UNSIGNED(unsigned, UINT); |
| 508 | CHECK_PADDING_UNSIGNED(unsigned long, ULONG); |
| 509 | CHECK_PADDING_UNSIGNED(unsigned long long, ULLONG); |
| 510 | CHECK_PADDING_UNSIGNED(size_t, SIZE); |
Hanno Becker | 4ab3850 | 2018-10-16 13:22:44 +0100 | [diff] [blame] | 511 | |
| 512 | #undef CHECK_PADDING_SIGNED |
| 513 | #undef CHECK_PADDING_UNSIGNED |
| 514 | |
| 515 | /* |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 516 | * Make sure we have a snprintf that correctly zero-terminates |
| 517 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 518 | if (run_test_snprintf() != 0) { |
| 519 | mbedtls_printf("the snprintf implementation is broken\n"); |
| 520 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 521 | } |
| 522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 523 | for (argp = argv + (argc >= 1 ? 1 : argc); *argp != NULL; ++argp) { |
| 524 | if (strcmp(*argp, "--quiet") == 0 || |
| 525 | strcmp(*argp, "-q") == 0) { |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 526 | v = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 527 | } else if (strcmp(*argp, "--exclude") == 0 || |
| 528 | strcmp(*argp, "-x") == 0) { |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 529 | exclude_mode = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 530 | } else { |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 531 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 532 | } |
SimonB | 5a8afb8 | 2016-03-11 00:40:54 +0000 | [diff] [blame] | 533 | } |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 535 | if (v != 0) { |
| 536 | mbedtls_printf("\n"); |
| 537 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 538 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 539 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 135b98e | 2011-05-25 11:13:47 +0000 | [diff] [blame] | 540 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 541 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 542 | mbedtls_memory_buffer_alloc_init(buf, sizeof(buf)); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 543 | #endif |
| 544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 545 | if (*argp != NULL && exclude_mode == 0) { |
Gilles Peskine | c82fbb4 | 2017-12-15 15:01:27 +0100 | [diff] [blame] | 546 | /* Run the specified tests */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 547 | for (; *argp != NULL; argp++) { |
| 548 | for (test = selftests; test->name != NULL; test++) { |
| 549 | if (!strcmp(*argp, test->name)) { |
| 550 | if (test->function(v) != 0) { |
Gilles Peskine | c82fbb4 | 2017-12-15 15:01:27 +0100 | [diff] [blame] | 551 | suites_failed++; |
| 552 | } |
| 553 | suites_tested++; |
| 554 | break; |
| 555 | } |
| 556 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 557 | if (test->name == NULL) { |
| 558 | mbedtls_printf(" Test suite %s not available -> failed\n\n", *argp); |
Gilles Peskine | c82fbb4 | 2017-12-15 15:01:27 +0100 | [diff] [blame] | 559 | suites_failed++; |
| 560 | } |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 561 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 562 | } else { |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 563 | /* Run all the tests except excluded ones */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 564 | for (test = selftests; test->name != NULL; test++) { |
| 565 | if (exclude_mode) { |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 566 | char **excluded; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 567 | for (excluded = argp; *excluded != NULL; ++excluded) { |
| 568 | if (!strcmp(*excluded, test->name)) { |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 569 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 570 | } |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 571 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 572 | if (*excluded) { |
| 573 | if (v) { |
| 574 | mbedtls_printf(" Skip: %s\n", test->name); |
| 575 | } |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 576 | continue; |
| 577 | } |
| 578 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 579 | if (test->function(v) != 0) { |
Gilles Peskine | c82fbb4 | 2017-12-15 15:01:27 +0100 | [diff] [blame] | 580 | suites_failed++; |
| 581 | } |
| 582 | suites_tested++; |
| 583 | } |
SimonB | 5a8afb8 | 2016-03-11 00:40:54 +0000 | [diff] [blame] | 584 | } |
Manuel Pégourié-Gonnard | 5ba1d52 | 2014-11-27 11:33:55 +0100 | [diff] [blame] | 585 | |
Paul Bakker | 70940ca | 2016-07-14 14:30:45 +0100 | [diff] [blame] | 586 | #else |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 587 | (void) exclude_mode; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 588 | mbedtls_printf(" MBEDTLS_SELF_TEST not defined.\n"); |
Paul Bakker | 70940ca | 2016-07-14 14:30:45 +0100 | [diff] [blame] | 589 | #endif |
| 590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 591 | if (v != 0) { |
| 592 | mbedtls_printf(" Executed %d test suites\n\n", suites_tested); |
SimonB | 5a8afb8 | 2016-03-11 00:40:54 +0000 | [diff] [blame] | 593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 594 | if (suites_failed > 0) { |
| 595 | mbedtls_printf(" [ %d tests FAIL ]\n\n", suites_failed); |
| 596 | } else { |
| 597 | mbedtls_printf(" [ All tests PASS ]\n\n"); |
SimonB | 5a8afb8 | 2016-03-11 00:40:54 +0000 | [diff] [blame] | 598 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 599 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 601 | if (suites_failed > 0) { |
| 602 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 603 | } |
SimonB | 5a8afb8 | 2016-03-11 00:40:54 +0000 | [diff] [blame] | 604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 605 | mbedtls_exit(MBEDTLS_EXIT_SUCCESS); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 606 | } |