test_suite_md.function 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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, char *hex_hash_string )
  103. {
  104. char md_name[100];
  105. unsigned char src_str[1000];
  106. unsigned char hash_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( hash_str, 0x00, 1000 );
  112. memset( output, 0x00, 100 );
  113. strncpy( (char *) src_str, text_src_string, sizeof( src_str ) - 1 );
  114. strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
  115. md_info = mbedtls_md_info_from_string(md_name);
  116. TEST_ASSERT( md_info != NULL );
  117. TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str, strlen( (char *) src_str ), output ) );
  118. hexify( hash_str, output, mbedtls_md_get_size( md_info ) );
  119. TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
  120. }
  121. /* END_CASE */
  122. /* BEGIN_CASE */
  123. void md_hex( char *text_md_name, char *hex_src_string, char *hex_hash_string )
  124. {
  125. char md_name[100];
  126. unsigned char src_str[10000];
  127. unsigned char hash_str[10000];
  128. unsigned char output[100];
  129. int src_len;
  130. const mbedtls_md_info_t *md_info = NULL;
  131. memset( md_name, 0x00, 100 );
  132. memset( src_str, 0x00, 10000 );
  133. memset( hash_str, 0x00, 10000 );
  134. memset( output, 0x00, 100 );
  135. strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
  136. md_info = mbedtls_md_info_from_string( md_name );
  137. TEST_ASSERT( md_info != NULL );
  138. src_len = unhexify( src_str, hex_src_string );
  139. TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str, src_len, output ) );
  140. hexify( hash_str, output, mbedtls_md_get_size( md_info ) );
  141. TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
  142. }
  143. /* END_CASE */
  144. /* BEGIN_CASE */
  145. void md_text_multi( char *text_md_name, char *text_src_string,
  146. char *hex_hash_string )
  147. {
  148. char md_name[100];
  149. unsigned char src_str[1000];
  150. unsigned char hash_str[1000];
  151. unsigned char output[100];
  152. int halfway, len;
  153. const mbedtls_md_info_t *md_info = NULL;
  154. mbedtls_md_context_t ctx, ctx_copy;
  155. mbedtls_md_init( &ctx );
  156. mbedtls_md_init( &ctx_copy );
  157. memset( md_name, 0x00, 100 );
  158. memset( src_str, 0x00, 1000 );
  159. memset( hash_str, 0x00, 1000 );
  160. memset( output, 0x00, 100 );
  161. strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
  162. strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
  163. len = strlen( (char *) src_str );
  164. halfway = len / 2;
  165. md_info = mbedtls_md_info_from_string(md_name);
  166. TEST_ASSERT( md_info != NULL );
  167. TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) );
  168. TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) );
  169. TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) );
  170. TEST_ASSERT ( ctx.md_ctx != NULL );
  171. TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str, halfway ) );
  172. TEST_ASSERT ( 0 == mbedtls_md_clone( &ctx_copy, &ctx ) );
  173. TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str + halfway, len - halfway ) );
  174. TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) );
  175. hexify( hash_str, output, mbedtls_md_get_size( md_info ) );
  176. TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
  177. /* Test clone */
  178. memset( hash_str, 0x00, 1000 );
  179. memset( output, 0x00, 100 );
  180. TEST_ASSERT ( 0 == mbedtls_md_update( &ctx_copy, src_str + halfway, len - halfway ) );
  181. TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx_copy, output ) );
  182. hexify( hash_str, output, mbedtls_md_get_size( md_info ) );
  183. TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
  184. exit:
  185. mbedtls_md_free( &ctx );
  186. mbedtls_md_free( &ctx_copy );
  187. }
  188. /* END_CASE */
  189. /* BEGIN_CASE */
  190. void md_hex_multi( char *text_md_name, char *hex_src_string,
  191. char *hex_hash_string )
  192. {
  193. char md_name[100];
  194. unsigned char src_str[10000];
  195. unsigned char hash_str[10000];
  196. unsigned char output[100];
  197. int src_len, halfway;
  198. const mbedtls_md_info_t *md_info = NULL;
  199. mbedtls_md_context_t ctx, ctx_copy;
  200. mbedtls_md_init( &ctx );
  201. mbedtls_md_init( &ctx_copy );
  202. memset( md_name, 0x00, 100 );
  203. memset( src_str, 0x00, 10000 );
  204. memset( hash_str, 0x00, 10000 );
  205. memset( output, 0x00, 100 );
  206. strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
  207. md_info = mbedtls_md_info_from_string(md_name);
  208. TEST_ASSERT( md_info != NULL );
  209. TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) );
  210. TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) );
  211. src_len = unhexify( src_str, hex_src_string );
  212. halfway = src_len / 2;
  213. TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) );
  214. TEST_ASSERT ( ctx.md_ctx != NULL );
  215. TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str, halfway ) );
  216. TEST_ASSERT ( 0 == mbedtls_md_clone( &ctx_copy, &ctx ) );
  217. TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str + halfway, src_len - halfway) );
  218. TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) );
  219. hexify( hash_str, output, mbedtls_md_get_size( md_info ) );
  220. TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
  221. /* Test clone */
  222. memset( hash_str, 0x00, 10000 );
  223. memset( output, 0x00, 100 );
  224. TEST_ASSERT ( 0 == mbedtls_md_update( &ctx_copy, src_str + halfway, src_len - halfway ) );
  225. TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx_copy, output ) );
  226. hexify( hash_str, output, mbedtls_md_get_size( md_info ) );
  227. TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
  228. exit:
  229. mbedtls_md_free( &ctx );
  230. mbedtls_md_free( &ctx_copy );
  231. }
  232. /* END_CASE */
  233. /* BEGIN_CASE */
  234. void mbedtls_md_hmac( char *text_md_name, int trunc_size, char *hex_key_string,
  235. char *hex_src_string, char *hex_hash_string )
  236. {
  237. char md_name[100];
  238. unsigned char src_str[10000];
  239. unsigned char key_str[10000];
  240. unsigned char hash_str[10000];
  241. unsigned char output[100];
  242. int key_len, src_len;
  243. const mbedtls_md_info_t *md_info = NULL;
  244. memset( md_name, 0x00, 100 );
  245. memset( src_str, 0x00, 10000 );
  246. memset( key_str, 0x00, 10000 );
  247. memset( hash_str, 0x00, 10000 );
  248. memset( output, 0x00, 100 );
  249. strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
  250. md_info = mbedtls_md_info_from_string( md_name );
  251. TEST_ASSERT( md_info != NULL );
  252. key_len = unhexify( key_str, hex_key_string );
  253. src_len = unhexify( src_str, hex_src_string );
  254. TEST_ASSERT ( mbedtls_md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 );
  255. hexify( hash_str, output, mbedtls_md_get_size( md_info ) );
  256. TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
  257. }
  258. /* END_CASE */
  259. /* BEGIN_CASE */
  260. void md_hmac_multi( char *text_md_name, int trunc_size, char *hex_key_string,
  261. char *hex_src_string, char *hex_hash_string )
  262. {
  263. char md_name[100];
  264. unsigned char src_str[10000];
  265. unsigned char key_str[10000];
  266. unsigned char hash_str[10000];
  267. unsigned char output[100];
  268. int key_len, src_len, halfway;
  269. const mbedtls_md_info_t *md_info = NULL;
  270. mbedtls_md_context_t ctx;
  271. mbedtls_md_init( &ctx );
  272. memset( md_name, 0x00, 100 );
  273. memset( src_str, 0x00, 10000 );
  274. memset( key_str, 0x00, 10000 );
  275. memset( hash_str, 0x00, 10000 );
  276. memset( output, 0x00, 100 );
  277. strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
  278. md_info = mbedtls_md_info_from_string( md_name );
  279. TEST_ASSERT( md_info != NULL );
  280. TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 1 ) );
  281. key_len = unhexify( key_str, hex_key_string );
  282. src_len = unhexify( src_str, hex_src_string );
  283. halfway = src_len / 2;
  284. TEST_ASSERT ( 0 == mbedtls_md_hmac_starts( &ctx, key_str, key_len ) );
  285. TEST_ASSERT ( ctx.md_ctx != NULL );
  286. TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str, halfway ) );
  287. TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str + halfway, src_len - halfway ) );
  288. TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) );
  289. hexify( hash_str, output, mbedtls_md_get_size( md_info ) );
  290. TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
  291. /* Test again, for reset() */
  292. memset( hash_str, 0x00, 10000 );
  293. memset( output, 0x00, 100 );
  294. TEST_ASSERT ( 0 == mbedtls_md_hmac_reset( &ctx ) );
  295. TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str, halfway ) );
  296. TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str + halfway, src_len - halfway ) );
  297. TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) );
  298. hexify( hash_str, output, mbedtls_md_get_size( md_info ) );
  299. TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
  300. exit:
  301. mbedtls_md_free( &ctx );
  302. }
  303. /* END_CASE */
  304. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
  305. void mbedtls_md_file( char *text_md_name, char *filename, char *hex_hash_string )
  306. {
  307. char md_name[100];
  308. unsigned char hash_str[1000];
  309. unsigned char output[100];
  310. const mbedtls_md_info_t *md_info = NULL;
  311. memset( md_name, 0x00, 100 );
  312. memset( hash_str, 0x00, 1000 );
  313. memset( output, 0x00, 100 );
  314. strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 );
  315. md_info = mbedtls_md_info_from_string( md_name );
  316. TEST_ASSERT( md_info != NULL );
  317. TEST_ASSERT( mbedtls_md_file( md_info, filename, output ) == 0 );
  318. hexify( hash_str, output, mbedtls_md_get_size( md_info ) );
  319. TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
  320. }
  321. /* END_CASE */