test_suite_md.function 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/md.h"
  3. /* END_HEADER */
  4. /* BEGIN_DEPENDENCIES
  5. * depends_on:MBEDTLS_MD_C
  6. * END_DEPENDENCIES
  7. */
  8. /* BEGIN_CASE */
  9. void mbedtls_md_process( )
  10. {
  11. const int *md_type_ptr;
  12. const mbedtls_md_info_t *info;
  13. mbedtls_md_context_t ctx;
  14. unsigned char buf[150];
  15. mbedtls_md_init( &ctx );
  16. /*
  17. * Very minimal testing of mbedtls_md_process, just make sure the various
  18. * xxx_process_wrap() function pointers are valid. (Testing that they
  19. * indeed do the right thing whould require messing with the internal
  20. * state of the underlying mbedtls_md/sha context.)
  21. *
  22. * Also tests that mbedtls_md_list() only returns valid MDs.
  23. */
  24. for( md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++ )
  25. {
  26. info = mbedtls_md_info_from_type( *md_type_ptr );
  27. TEST_ASSERT( info != NULL );
  28. TEST_ASSERT( mbedtls_md_setup( &ctx, info, 0 ) == 0 );
  29. TEST_ASSERT( mbedtls_md_process( &ctx, buf ) == 0 );
  30. mbedtls_md_free( &ctx );
  31. }
  32. exit:
  33. mbedtls_md_free( &ctx );
  34. }
  35. /* END_CASE */
  36. /* BEGIN_CASE */
  37. void md_null_args( )
  38. {
  39. mbedtls_md_context_t ctx;
  40. const mbedtls_md_info_t *info = mbedtls_md_info_from_type( *( mbedtls_md_list() ) );
  41. unsigned char buf[1] = { 0 };
  42. mbedtls_md_init( &ctx );
  43. TEST_ASSERT( mbedtls_md_get_size( NULL ) == 0 );
  44. TEST_ASSERT( mbedtls_md_get_type( NULL ) == MBEDTLS_MD_NONE );
  45. TEST_ASSERT( mbedtls_md_get_name( NULL ) == NULL );
  46. TEST_ASSERT( mbedtls_md_info_from_string( NULL ) == NULL );
  47. TEST_ASSERT( mbedtls_md_setup( &ctx, NULL, 0 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  48. TEST_ASSERT( mbedtls_md_setup( NULL, info, 0 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  49. TEST_ASSERT( mbedtls_md_starts( NULL ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  50. TEST_ASSERT( mbedtls_md_starts( &ctx ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  51. TEST_ASSERT( mbedtls_md_update( NULL, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  52. TEST_ASSERT( mbedtls_md_update( &ctx, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  53. TEST_ASSERT( mbedtls_md_finish( NULL, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  54. TEST_ASSERT( mbedtls_md_finish( &ctx, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  55. TEST_ASSERT( mbedtls_md( NULL, buf, 1, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  56. #if defined(MBEDTLS_FS_IO)
  57. TEST_ASSERT( mbedtls_md_file( NULL, "", buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  58. #endif
  59. TEST_ASSERT( mbedtls_md_hmac_starts( NULL, buf, 1 )
  60. == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  61. TEST_ASSERT( mbedtls_md_hmac_starts( &ctx, buf, 1 )
  62. == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  63. TEST_ASSERT( mbedtls_md_hmac_update( NULL, buf, 1 )
  64. == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  65. TEST_ASSERT( mbedtls_md_hmac_update( &ctx, buf, 1 )
  66. == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  67. TEST_ASSERT( mbedtls_md_hmac_finish( NULL, buf )
  68. == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  69. TEST_ASSERT( mbedtls_md_hmac_finish( &ctx, buf )
  70. == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  71. TEST_ASSERT( mbedtls_md_hmac_reset( NULL ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  72. TEST_ASSERT( mbedtls_md_hmac_reset( &ctx ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  73. TEST_ASSERT( mbedtls_md_hmac( NULL, buf, 1, buf, 1, buf )
  74. == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  75. TEST_ASSERT( mbedtls_md_process( NULL, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  76. TEST_ASSERT( mbedtls_md_process( &ctx, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  77. /* Ok, this is not NULL arg but NULL return... */
  78. TEST_ASSERT( mbedtls_md_info_from_type( MBEDTLS_MD_NONE ) == NULL );
  79. TEST_ASSERT( mbedtls_md_info_from_string( "no such md" ) == NULL );
  80. }
  81. /* END_CASE */
  82. /* BEGIN_CASE */
  83. void md_info( int md_type, char * md_name, int md_size )
  84. {
  85. const mbedtls_md_info_t *md_info;
  86. const int *md_type_ptr;
  87. int found;
  88. md_info = mbedtls_md_info_from_type( md_type );
  89. TEST_ASSERT( md_info != NULL );
  90. TEST_ASSERT( md_info == mbedtls_md_info_from_string( md_name ) );
  91. TEST_ASSERT( mbedtls_md_get_type( md_info ) == (mbedtls_md_type_t) md_type );
  92. TEST_ASSERT( mbedtls_md_get_size( md_info ) == (unsigned char) md_size );
  93. TEST_ASSERT( strcmp( mbedtls_md_get_name( md_info ), md_name ) == 0 );
  94. found = 0;
  95. for( md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++ )
  96. if( *md_type_ptr == md_type )
  97. found = 1;
  98. TEST_ASSERT( found == 1 );
  99. }
  100. /* END_CASE */
  101. /* BEGIN_CASE */
  102. void md_text( char * text_md_name, char * text_src_string,
  103. data_t * hash )
  104. {
  105. char md_name[100];
  106. unsigned char src_str[1000];
  107. unsigned char output[100];
  108. const mbedtls_md_info_t *md_info = NULL;
  109. memset( md_name, 0x00, 100 );
  110. memset( src_str, 0x00, 1000 );
  111. memset( output, 0x00, 100 );
  112. strncpy( (char *) src_str, text_src_string, sizeof( src_str ) - 1 );
  113. strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
  114. md_info = mbedtls_md_info_from_string(md_name);
  115. TEST_ASSERT( md_info != NULL );
  116. TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str, strlen( (char *) src_str ), output ) );
  117. TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
  118. mbedtls_md_get_size( md_info ),
  119. hash->len ) == 0 );
  120. }
  121. /* END_CASE */
  122. /* BEGIN_CASE */
  123. void md_hex( char * text_md_name, data_t * src_str, data_t * hash )
  124. {
  125. char md_name[100];
  126. unsigned char output[100];
  127. const mbedtls_md_info_t *md_info = NULL;
  128. memset( md_name, 0x00, 100 );
  129. memset( output, 0x00, 100 );
  130. strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
  131. md_info = mbedtls_md_info_from_string( md_name );
  132. TEST_ASSERT( md_info != NULL );
  133. TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str->x, src_str->len, output ) );
  134. TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
  135. mbedtls_md_get_size( md_info ),
  136. hash->len ) == 0 );
  137. }
  138. /* END_CASE */
  139. /* BEGIN_CASE */
  140. void md_text_multi( char * text_md_name, char * text_src_string,
  141. data_t * hash )
  142. {
  143. char md_name[100];
  144. unsigned char src_str[1000];
  145. unsigned char output[100];
  146. int halfway, len;
  147. const mbedtls_md_info_t *md_info = NULL;
  148. mbedtls_md_context_t ctx, ctx_copy;
  149. mbedtls_md_init( &ctx );
  150. mbedtls_md_init( &ctx_copy );
  151. memset( md_name, 0x00, 100 );
  152. memset( src_str, 0x00, 1000 );
  153. memset( output, 0x00, 100 );
  154. strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
  155. strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
  156. len = strlen( (char *) src_str );
  157. halfway = len / 2;
  158. md_info = mbedtls_md_info_from_string(md_name);
  159. TEST_ASSERT( md_info != NULL );
  160. TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) );
  161. TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) );
  162. TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) );
  163. TEST_ASSERT ( ctx.md_ctx != NULL );
  164. TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str, halfway ) );
  165. TEST_ASSERT ( 0 == mbedtls_md_clone( &ctx_copy, &ctx ) );
  166. TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str + halfway, len - halfway ) );
  167. TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) );
  168. TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
  169. mbedtls_md_get_size( md_info ),
  170. hash->len) == 0 );
  171. /* Test clone */
  172. memset( output, 0x00, 100 );
  173. TEST_ASSERT ( 0 == mbedtls_md_update( &ctx_copy, src_str + halfway, len - halfway ) );
  174. TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx_copy, output ) );
  175. TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
  176. mbedtls_md_get_size( md_info ),
  177. hash->len ) == 0 );
  178. exit:
  179. mbedtls_md_free( &ctx );
  180. mbedtls_md_free( &ctx_copy );
  181. }
  182. /* END_CASE */
  183. /* BEGIN_CASE */
  184. void md_hex_multi( char * text_md_name, data_t * src_str, data_t * hash )
  185. {
  186. char md_name[100];
  187. unsigned char output[100];
  188. const mbedtls_md_info_t *md_info = NULL;
  189. mbedtls_md_context_t ctx, ctx_copy;
  190. int halfway;
  191. mbedtls_md_init( &ctx );
  192. mbedtls_md_init( &ctx_copy );
  193. memset( md_name, 0x00, 100 );
  194. memset( output, 0x00, 100 );
  195. strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
  196. md_info = mbedtls_md_info_from_string(md_name);
  197. TEST_ASSERT( md_info != NULL );
  198. TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) );
  199. TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) );
  200. halfway = src_str->len / 2;
  201. TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) );
  202. TEST_ASSERT ( ctx.md_ctx != NULL );
  203. TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str->x, halfway ) );
  204. TEST_ASSERT ( 0 == mbedtls_md_clone( &ctx_copy, &ctx ) );
  205. TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str->x + halfway, src_str->len - halfway) );
  206. TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) );
  207. TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
  208. mbedtls_md_get_size( md_info ),
  209. hash->len ) == 0 );
  210. /* Test clone */
  211. memset( output, 0x00, 100 );
  212. TEST_ASSERT ( 0 == mbedtls_md_update( &ctx_copy, src_str->x + halfway, src_str->len - halfway ) );
  213. TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx_copy, output ) );
  214. TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
  215. mbedtls_md_get_size( md_info ),
  216. hash->len ) == 0 );
  217. exit:
  218. mbedtls_md_free( &ctx );
  219. mbedtls_md_free( &ctx_copy );
  220. }
  221. /* END_CASE */
  222. /* BEGIN_CASE */
  223. void mbedtls_md_hmac( char * text_md_name, int trunc_size,
  224. data_t * key_str, data_t * src_str,
  225. data_t * hash )
  226. {
  227. char md_name[100];
  228. unsigned char output[100];
  229. const mbedtls_md_info_t *md_info = NULL;
  230. memset( md_name, 0x00, 100 );
  231. memset( output, 0x00, 100 );
  232. strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
  233. md_info = mbedtls_md_info_from_string( md_name );
  234. TEST_ASSERT( md_info != NULL );
  235. TEST_ASSERT ( mbedtls_md_hmac( md_info, key_str->x, key_str->len, src_str->x, src_str->len, output ) == 0 );
  236. TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
  237. trunc_size, hash->len ) == 0 );
  238. }
  239. /* END_CASE */
  240. /* BEGIN_CASE */
  241. void md_hmac_multi( char * text_md_name, int trunc_size, data_t * key_str,
  242. data_t * src_str, data_t * hash )
  243. {
  244. char md_name[100];
  245. unsigned char output[100];
  246. const mbedtls_md_info_t *md_info = NULL;
  247. mbedtls_md_context_t ctx;
  248. int halfway;
  249. mbedtls_md_init( &ctx );
  250. memset( md_name, 0x00, 100 );
  251. memset( output, 0x00, 100 );
  252. strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
  253. md_info = mbedtls_md_info_from_string( md_name );
  254. TEST_ASSERT( md_info != NULL );
  255. TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 1 ) );
  256. halfway = src_str->len / 2;
  257. TEST_ASSERT ( 0 == mbedtls_md_hmac_starts( &ctx, key_str->x, key_str->len ) );
  258. TEST_ASSERT ( ctx.md_ctx != NULL );
  259. TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x, halfway ) );
  260. TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x + halfway, src_str->len - halfway ) );
  261. TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) );
  262. TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
  263. trunc_size, hash->len ) == 0 );
  264. /* Test again, for reset() */
  265. memset( output, 0x00, 100 );
  266. TEST_ASSERT ( 0 == mbedtls_md_hmac_reset( &ctx ) );
  267. TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x, halfway ) );
  268. TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str->x + halfway, src_str->len - halfway ) );
  269. TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) );
  270. TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
  271. trunc_size, hash->len ) == 0 );
  272. exit:
  273. mbedtls_md_free( &ctx );
  274. }
  275. /* END_CASE */
  276. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
  277. void mbedtls_md_file( char * text_md_name, char * filename,
  278. data_t * hash )
  279. {
  280. char md_name[100];
  281. unsigned char output[100];
  282. const mbedtls_md_info_t *md_info = NULL;
  283. memset( md_name, 0x00, 100 );
  284. memset( output, 0x00, 100 );
  285. strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
  286. md_info = mbedtls_md_info_from_string( md_name );
  287. TEST_ASSERT( md_info != NULL );
  288. TEST_ASSERT( mbedtls_md_file( md_info, filename, output ) == 0 );
  289. TEST_ASSERT( mbedtls_test_hexcmp( output, hash->x,
  290. mbedtls_md_get_size( md_info ),
  291. hash->len ) == 0 );
  292. }
  293. /* END_CASE */