helpers.function 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #line 1 "helpers.function"
  2. /*----------------------------------------------------------------------------*/
  3. /* Headers */
  4. #include <stdlib.h>
  5. #if defined(MBEDTLS_PLATFORM_C)
  6. #include "mbedtls/platform.h"
  7. #else
  8. #include <stdio.h>
  9. #define mbedtls_fprintf fprintf
  10. #define mbedtls_snprintf snprintf
  11. #define mbedtls_calloc calloc
  12. #define mbedtls_free free
  13. #define mbedtls_exit exit
  14. #define mbedtls_time time
  15. #define mbedtls_time_t time_t
  16. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  17. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  18. #endif
  19. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  20. #include "mbedtls/memory_buffer_alloc.h"
  21. #endif
  22. #ifdef _MSC_VER
  23. #include <basetsd.h>
  24. typedef UINT32 uint32_t;
  25. #define strncasecmp _strnicmp
  26. #define strcasecmp _stricmp
  27. #else
  28. #include <stdint.h>
  29. #endif
  30. #include <string.h>
  31. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  32. #include <unistd.h>
  33. #endif
  34. /*----------------------------------------------------------------------------*/
  35. /* Constants */
  36. #define DEPENDENCY_SUPPORTED 0
  37. #define DEPENDENCY_NOT_SUPPORTED 1
  38. #define KEY_VALUE_MAPPING_FOUND 0
  39. #define KEY_VALUE_MAPPING_NOT_FOUND -1
  40. #define DISPATCH_TEST_SUCCESS 0
  41. #define DISPATCH_TEST_FN_NOT_FOUND 1
  42. #define DISPATCH_INVALID_TEST_DATA 2
  43. #define DISPATCH_UNSUPPORTED_SUITE 3
  44. /*----------------------------------------------------------------------------*/
  45. /* Macros */
  46. #define TEST_ASSERT( TEST ) \
  47. do { \
  48. if( ! (TEST) ) \
  49. { \
  50. test_fail( #TEST, __LINE__, __FILE__ ); \
  51. goto exit; \
  52. } \
  53. } while( 0 )
  54. #define assert(a) if( !( a ) ) \
  55. { \
  56. mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
  57. __FILE__, __LINE__, #a ); \
  58. mbedtls_exit( 1 ); \
  59. }
  60. /*
  61. * 32-bit integer manipulation macros (big endian)
  62. */
  63. #ifndef GET_UINT32_BE
  64. #define GET_UINT32_BE(n,b,i) \
  65. { \
  66. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  67. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  68. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  69. | ( (uint32_t) (b)[(i) + 3] ); \
  70. }
  71. #endif
  72. #ifndef PUT_UINT32_BE
  73. #define PUT_UINT32_BE(n,b,i) \
  74. { \
  75. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  76. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  77. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  78. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  79. }
  80. #endif
  81. /*----------------------------------------------------------------------------*/
  82. /* Global variables */
  83. static int test_errors = 0;
  84. /*----------------------------------------------------------------------------*/
  85. /* Helper Functions */
  86. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  87. static int redirect_output( FILE** out_stream, const char* path )
  88. {
  89. int stdout_fd = dup( fileno( *out_stream ) );
  90. if( stdout_fd == -1 )
  91. {
  92. return -1;
  93. }
  94. fflush( *out_stream );
  95. fclose( *out_stream );
  96. *out_stream = fopen( path, "w" );
  97. if( *out_stream == NULL )
  98. {
  99. return -1;
  100. }
  101. return stdout_fd;
  102. }
  103. static int restore_output( FILE** out_stream, int old_fd )
  104. {
  105. fflush( *out_stream );
  106. fclose( *out_stream );
  107. *out_stream = fdopen( old_fd, "w" );
  108. if( *out_stream == NULL )
  109. {
  110. return -1;
  111. }
  112. return 0;
  113. }
  114. static void close_output( FILE* out_stream )
  115. {
  116. fclose( out_stream );
  117. }
  118. #endif /* __unix__ || __APPLE__ __MACH__ */
  119. static int unhexify( unsigned char *obuf, const char *ibuf )
  120. {
  121. unsigned char c, c2;
  122. int len = strlen( ibuf ) / 2;
  123. assert( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
  124. while( *ibuf != 0 )
  125. {
  126. c = *ibuf++;
  127. if( c >= '0' && c <= '9' )
  128. c -= '0';
  129. else if( c >= 'a' && c <= 'f' )
  130. c -= 'a' - 10;
  131. else if( c >= 'A' && c <= 'F' )
  132. c -= 'A' - 10;
  133. else
  134. assert( 0 );
  135. c2 = *ibuf++;
  136. if( c2 >= '0' && c2 <= '9' )
  137. c2 -= '0';
  138. else if( c2 >= 'a' && c2 <= 'f' )
  139. c2 -= 'a' - 10;
  140. else if( c2 >= 'A' && c2 <= 'F' )
  141. c2 -= 'A' - 10;
  142. else
  143. assert( 0 );
  144. *obuf++ = ( c << 4 ) | c2;
  145. }
  146. return len;
  147. }
  148. static void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
  149. {
  150. unsigned char l, h;
  151. while( len != 0 )
  152. {
  153. h = *ibuf / 16;
  154. l = *ibuf % 16;
  155. if( h < 10 )
  156. *obuf++ = '0' + h;
  157. else
  158. *obuf++ = 'a' + h - 10;
  159. if( l < 10 )
  160. *obuf++ = '0' + l;
  161. else
  162. *obuf++ = 'a' + l - 10;
  163. ++ibuf;
  164. len--;
  165. }
  166. }
  167. /**
  168. * Allocate and zeroize a buffer.
  169. *
  170. * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
  171. *
  172. * For convenience, dies if allocation fails.
  173. */
  174. static unsigned char *zero_alloc( size_t len )
  175. {
  176. void *p;
  177. size_t actual_len = ( len != 0 ) ? len : 1;
  178. p = mbedtls_calloc( 1, actual_len );
  179. assert( p != NULL );
  180. memset( p, 0x00, actual_len );
  181. return( p );
  182. }
  183. /**
  184. * Allocate and fill a buffer from hex data.
  185. *
  186. * The buffer is sized exactly as needed. This allows to detect buffer
  187. * overruns (including overreads) when running the test suite under valgrind.
  188. *
  189. * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
  190. *
  191. * For convenience, dies if allocation fails.
  192. */
  193. static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
  194. {
  195. unsigned char *obuf;
  196. *olen = strlen( ibuf ) / 2;
  197. if( *olen == 0 )
  198. return( zero_alloc( *olen ) );
  199. obuf = mbedtls_calloc( 1, *olen );
  200. assert( obuf != NULL );
  201. (void) unhexify( obuf, ibuf );
  202. return( obuf );
  203. }
  204. /**
  205. * This function just returns data from rand().
  206. * Although predictable and often similar on multiple
  207. * runs, this does not result in identical random on
  208. * each run. So do not use this if the results of a
  209. * test depend on the random data that is generated.
  210. *
  211. * rng_state shall be NULL.
  212. */
  213. static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
  214. {
  215. #if !defined(__OpenBSD__)
  216. size_t i;
  217. if( rng_state != NULL )
  218. rng_state = NULL;
  219. for( i = 0; i < len; ++i )
  220. output[i] = rand();
  221. #else
  222. if( rng_state != NULL )
  223. rng_state = NULL;
  224. arc4random_buf( output, len );
  225. #endif /* !OpenBSD */
  226. return( 0 );
  227. }
  228. /**
  229. * This function only returns zeros
  230. *
  231. * rng_state shall be NULL.
  232. */
  233. static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
  234. {
  235. if( rng_state != NULL )
  236. rng_state = NULL;
  237. memset( output, 0, len );
  238. return( 0 );
  239. }
  240. typedef struct
  241. {
  242. unsigned char *buf;
  243. size_t length;
  244. } rnd_buf_info;
  245. /**
  246. * This function returns random based on a buffer it receives.
  247. *
  248. * rng_state shall be a pointer to a rnd_buf_info structure.
  249. *
  250. * The number of bytes released from the buffer on each call to
  251. * the random function is specified by per_call. (Can be between
  252. * 1 and 4)
  253. *
  254. * After the buffer is empty it will return rand();
  255. */
  256. static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
  257. {
  258. rnd_buf_info *info = (rnd_buf_info *) rng_state;
  259. size_t use_len;
  260. if( rng_state == NULL )
  261. return( rnd_std_rand( NULL, output, len ) );
  262. use_len = len;
  263. if( len > info->length )
  264. use_len = info->length;
  265. if( use_len )
  266. {
  267. memcpy( output, info->buf, use_len );
  268. info->buf += use_len;
  269. info->length -= use_len;
  270. }
  271. if( len - use_len > 0 )
  272. return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
  273. return( 0 );
  274. }
  275. /**
  276. * Info structure for the pseudo random function
  277. *
  278. * Key should be set at the start to a test-unique value.
  279. * Do not forget endianness!
  280. * State( v0, v1 ) should be set to zero.
  281. */
  282. typedef struct
  283. {
  284. uint32_t key[16];
  285. uint32_t v0, v1;
  286. } rnd_pseudo_info;
  287. /**
  288. * This function returns random based on a pseudo random function.
  289. * This means the results should be identical on all systems.
  290. * Pseudo random is based on the XTEA encryption algorithm to
  291. * generate pseudorandom.
  292. *
  293. * rng_state shall be a pointer to a rnd_pseudo_info structure.
  294. */
  295. static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
  296. {
  297. rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
  298. uint32_t i, *k, sum, delta=0x9E3779B9;
  299. unsigned char result[4], *out = output;
  300. if( rng_state == NULL )
  301. return( rnd_std_rand( NULL, output, len ) );
  302. k = info->key;
  303. while( len > 0 )
  304. {
  305. size_t use_len = ( len > 4 ) ? 4 : len;
  306. sum = 0;
  307. for( i = 0; i < 32; i++ )
  308. {
  309. info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
  310. + info->v1 ) ^ ( sum + k[sum & 3] );
  311. sum += delta;
  312. info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
  313. + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
  314. }
  315. PUT_UINT32_BE( info->v0, result, 0 );
  316. memcpy( out, result, use_len );
  317. len -= use_len;
  318. out += 4;
  319. }
  320. return( 0 );
  321. }
  322. static void test_fail( const char *test, int line_no, const char* filename )
  323. {
  324. test_errors++;
  325. if( test_errors == 1 )
  326. mbedtls_fprintf( stdout, "FAILED\n" );
  327. mbedtls_fprintf( stdout, " %s\n at line %d, %s\n", test, line_no,
  328. filename );
  329. }