Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Self-test demonstration program |
| 3 | * |
| 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
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. |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 18 | * |
| 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 20 | */ |
| 21 | |
| 22 | #if !defined(POLARSSL_CONFIG_FILE) |
| 23 | #include "mbedtls/config.h" |
| 24 | #else |
| 25 | #include MBEDTLS_CONFIG_FILE |
| 26 | #endif |
| 27 | |
| 28 | #include "mbedtls/entropy.h" |
| 29 | #include "mbedtls/hmac_drbg.h" |
| 30 | #include "mbedtls/ctr_drbg.h" |
| 31 | #include "mbedtls/dhm.h" |
| 32 | #include "mbedtls/gcm.h" |
| 33 | #include "mbedtls/ccm.h" |
| 34 | #include "mbedtls/md2.h" |
| 35 | #include "mbedtls/md4.h" |
| 36 | #include "mbedtls/md5.h" |
| 37 | #include "mbedtls/ripemd160.h" |
| 38 | #include "mbedtls/sha1.h" |
| 39 | #include "mbedtls/sha256.h" |
| 40 | #include "mbedtls/sha512.h" |
| 41 | #include "mbedtls/arc4.h" |
| 42 | #include "mbedtls/des.h" |
| 43 | #include "mbedtls/aes.h" |
| 44 | #include "mbedtls/camellia.h" |
| 45 | #include "mbedtls/base64.h" |
| 46 | #include "mbedtls/bignum.h" |
| 47 | #include "mbedtls/rsa.h" |
| 48 | #include "mbedtls/x509.h" |
| 49 | #include "mbedtls/xtea.h" |
| 50 | #include "mbedtls/pkcs5.h" |
| 51 | #include "mbedtls/ecp.h" |
| 52 | |
| 53 | #include <stdio.h> |
| 54 | #include <string.h> |
| 55 | |
| 56 | #if defined(MBEDTLS_PLATFORM_C) |
| 57 | #include "mbedtls/platform.h" |
| 58 | #else |
| 59 | #include <stdio.h> |
| 60 | #define mbedtls_printf printf |
| 61 | #endif |
| 62 | |
| 63 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 64 | #include "mbedtls/memory_buffer_alloc.h" |
| 65 | #endif |
| 66 | |
| 67 | int selftest( int argc, char *argv[] ) |
| 68 | { |
| 69 | int ret = 0, v; |
| 70 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 71 | unsigned char buf[1000000]; |
| 72 | #endif |
| 73 | |
| 74 | if( argc == 2 && strcmp( argv[1], "-quiet" ) == 0 ) |
| 75 | v = 0; |
| 76 | else |
| 77 | { |
| 78 | v = 1; |
| 79 | mbedtls_printf( "\n" ); |
| 80 | } |
| 81 | |
| 82 | #if defined(MBEDTLS_SELF_TEST) |
| 83 | |
| 84 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 85 | mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) ); |
| 86 | #endif |
| 87 | |
| 88 | #if defined(MBEDTLS_MD2_C) |
| 89 | if( ( ret = mbedtls_md2_self_test( v ) ) != 0 ) |
| 90 | return( ret ); |
| 91 | #endif |
| 92 | |
| 93 | #if defined(MBEDTLS_MD4_C) |
| 94 | if( ( ret = mbedtls_md4_self_test( v ) ) != 0 ) |
| 95 | return( ret ); |
| 96 | #endif |
| 97 | |
| 98 | #if defined(MBEDTLS_MD5_C) |
| 99 | if( ( ret = mbedtls_md5_self_test( v ) ) != 0 ) |
| 100 | return( ret ); |
| 101 | #endif |
| 102 | |
| 103 | #if defined(MBEDTLS_RIPEMD160_C) |
| 104 | if( ( ret = mbedtls_ripemd160_self_test( v ) ) != 0 ) |
| 105 | return( ret ); |
| 106 | #endif |
| 107 | |
| 108 | #if defined(MBEDTLS_SHA1_C) |
| 109 | if( ( ret = mbedtls_sha1_self_test( v ) ) != 0 ) |
| 110 | return( ret ); |
| 111 | #endif |
| 112 | |
| 113 | #if defined(MBEDTLS_SHA256_C) |
| 114 | if( ( ret = mbedtls_sha256_self_test( v ) ) != 0 ) |
| 115 | return( ret ); |
| 116 | #endif |
| 117 | |
| 118 | #if defined(MBEDTLS_SHA512_C) |
| 119 | if( ( ret = mbedtls_sha512_self_test( v ) ) != 0 ) |
| 120 | return( ret ); |
| 121 | #endif |
| 122 | |
| 123 | #if defined(MBEDTLS_ARC4_C) |
| 124 | if( ( ret = mbedtls_arc4_self_test( v ) ) != 0 ) |
| 125 | return( ret ); |
| 126 | #endif |
| 127 | |
| 128 | #if defined(MBEDTLS_DES_C) |
| 129 | if( ( ret = mbedtls_des_self_test( v ) ) != 0 ) |
| 130 | return( ret ); |
| 131 | #endif |
| 132 | |
| 133 | #if defined(MBEDTLS_AES_C) |
| 134 | if( ( ret = mbedtls_aes_self_test( v ) ) != 0 ) |
| 135 | return( ret ); |
| 136 | #endif |
| 137 | |
| 138 | #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C) |
| 139 | if( ( ret = mbedtls_gcm_self_test( v ) ) != 0 ) |
| 140 | return( ret ); |
| 141 | #endif |
| 142 | |
| 143 | #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C) |
| 144 | if( ( ret = mbedtls_ccm_self_test( v ) ) != 0 ) |
| 145 | return( ret ); |
| 146 | #endif |
| 147 | |
| 148 | #if defined(MBEDTLS_BASE64_C) |
| 149 | if( ( ret = mbedtls_base64_self_test( v ) ) != 0 ) |
| 150 | return( ret ); |
| 151 | #endif |
| 152 | |
| 153 | #if defined(MBEDTLS_BIGNUM_C) |
| 154 | if( ( ret = mbedtls_mpi_self_test( v ) ) != 0 ) |
| 155 | return( ret ); |
| 156 | #endif |
| 157 | |
| 158 | #if defined(MBEDTLS_RSA_C) |
| 159 | if( ( ret = mbedtls_rsa_self_test( v ) ) != 0 ) |
| 160 | return( ret ); |
| 161 | #endif |
| 162 | |
| 163 | #if defined(MBEDTLS_X509_USE_C) |
| 164 | if( ( ret = mbedtls_x509_self_test( v ) ) != 0 ) |
| 165 | return( ret ); |
| 166 | #endif |
| 167 | |
| 168 | #if defined(MBEDTLS_XTEA_C) |
| 169 | if( ( ret = mbedtls_xtea_self_test( v ) ) != 0 ) |
| 170 | return( ret ); |
| 171 | #endif |
| 172 | |
| 173 | #if defined(MBEDTLS_CAMELLIA_C) |
| 174 | if( ( ret = mbedtls_camellia_self_test( v ) ) != 0 ) |
| 175 | return( ret ); |
| 176 | #endif |
| 177 | |
| 178 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 179 | if( ( ret = mbedtls_ctr_drbg_self_test( v ) ) != 0 ) |
| 180 | return( ret ); |
| 181 | #endif |
| 182 | |
| 183 | #if defined(MBEDTLS_HMAC_DRBG_C) |
| 184 | if( ( ret = mbedtls_hmac_drbg_self_test( v ) ) != 0 ) |
| 185 | return( ret ); |
| 186 | #endif |
| 187 | |
| 188 | #if defined(MBEDTLS_ECP_C) |
| 189 | if( ( ret = mbedtls_ecp_self_test( v ) ) != 0 ) |
| 190 | return( ret ); |
| 191 | #endif |
| 192 | |
| 193 | #if defined(MBEDTLS_DHM_C) |
| 194 | if( ( ret = mbedtls_dhm_self_test( v ) ) != 0 ) |
| 195 | return( ret ); |
| 196 | #endif |
| 197 | |
| 198 | #if defined(MBEDTLS_ENTROPY_C) |
| 199 | if( ( ret = mbedtls_entropy_self_test( v ) ) != 0 ) |
| 200 | return( ret ); |
| 201 | #endif |
| 202 | |
| 203 | #if defined(MBEDTLS_PKCS5_C) |
| 204 | if( ( ret = mbedtls_pkcs5_self_test( v ) ) != 0 ) |
| 205 | return( ret ); |
| 206 | #endif |
| 207 | |
| 208 | #if defined(MBEDTLS_TIMING_C) |
| 209 | if( ( ret = mbedtls_timing_self_test( v ) ) != 0 ) |
| 210 | return( ret ); |
| 211 | #endif |
| 212 | |
| 213 | #else |
| 214 | mbedtls_printf( " POLARSSL_SELF_TEST not defined.\n" ); |
| 215 | #endif |
| 216 | |
| 217 | if( v != 0 ) |
| 218 | { |
| 219 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG) |
| 220 | mbedtls_memory_buffer_alloc_status(); |
| 221 | #endif |
| 222 | } |
| 223 | |
| 224 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 225 | mbedtls_memory_buffer_alloc_free(); |
| 226 | |
| 227 | if( ( ret = mbedtls_memory_buffer_alloc_self_test( v ) ) != 0 ) |
| 228 | return( ret ); |
| 229 | #endif |
| 230 | |
| 231 | if( v != 0 ) |
| 232 | { |
| 233 | mbedtls_printf( " [ All tests passed ]\n\n" ); |
| 234 | #if defined(_WIN32) |
| 235 | mbedtls_printf( " Press Enter to exit this program.\n" ); |
| 236 | fflush( stdout ); getchar(); |
| 237 | #endif |
| 238 | } |
| 239 | |
| 240 | return( ret ); |
| 241 | } |
| 242 | |
| 243 | #if defined(TARGET_LIKE_MBED) |
| 244 | |
| 245 | #include "mbed/test_env.h" |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame] | 246 | #include "minar/minar.h" |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 247 | |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame] | 248 | static void run() { |
Manuel Pégourié-Gonnard | bd5bbec | 2015-08-06 18:10:17 +0200 | [diff] [blame] | 249 | /* Use 115200 bps for consistency with other examples */ |
| 250 | Serial pc(USBTX, USBRX); |
| 251 | pc.baud(115200); |
| 252 | |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 253 | MBED_HOSTTEST_TIMEOUT(40); |
| 254 | MBED_HOSTTEST_SELECT(default); |
| 255 | MBED_HOSTTEST_DESCRIPTION(mbed TLS selftest program); |
| 256 | MBED_HOSTTEST_START("MBEDTLS_SELFTEST"); |
| 257 | MBED_HOSTTEST_RESULT(selftest(0, NULL) == 0); |
| 258 | } |
| 259 | |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame] | 260 | void app_start(int, char*[]) { |
| 261 | minar::Scheduler::postCallback(FunctionPointer0<void>(run).bind()); |
| 262 | } |
| 263 | |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 264 | #else |
| 265 | |
| 266 | int main() { |
| 267 | return selftest(0, NULL); |
| 268 | } |
| 269 | |
| 270 | #endif |