main.cpp 6.2 KB

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