test_suite_md.function 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. BEGIN_HEADER
  2. #include <polarssl/md.h>
  3. #include <polarssl/md2.h>
  4. #include <polarssl/md4.h>
  5. #include <polarssl/md5.h>
  6. #include <polarssl/sha1.h>
  7. #include <polarssl/sha2.h>
  8. #include <polarssl/sha4.h>
  9. END_HEADER
  10. BEGIN_DEPENDENCIES
  11. depends_on:POLARSSL_MD_C
  12. END_DEPENDENCIES
  13. BEGIN_CASE
  14. md_text:text_md_name:text_src_string:hex_hash_string
  15. {
  16. char md_name[100];
  17. unsigned char src_str[1000];
  18. unsigned char hash_str[1000];
  19. unsigned char output[100];
  20. const md_info_t *md_info = NULL;
  21. memset(md_name, 0x00, 100);
  22. memset(src_str, 0x00, 1000);
  23. memset(hash_str, 0x00, 1000);
  24. memset(output, 0x00, 100);
  25. strcpy( (char *) src_str, {text_src_string} );
  26. strncpy( (char *) md_name, {text_md_name}, 100 );
  27. md_info = md_info_from_string(md_name);
  28. TEST_ASSERT( md_info != NULL );
  29. TEST_ASSERT ( 0 == md( md_info, src_str, strlen( (char *) src_str ), output ) );
  30. hexify( hash_str, output, md_get_size(md_info) );
  31. TEST_ASSERT( strcmp( (char *) hash_str, {hex_hash_string} ) == 0 );
  32. }
  33. END_CASE
  34. BEGIN_CASE
  35. md_hex:text_md_name:hex_src_string:hex_hash_string
  36. {
  37. char md_name[100];
  38. unsigned char src_str[10000];
  39. unsigned char hash_str[10000];
  40. unsigned char output[100];
  41. int src_len;
  42. const md_info_t *md_info = NULL;
  43. memset(md_name, 0x00, 100);
  44. memset(src_str, 0x00, 10000);
  45. memset(hash_str, 0x00, 10000);
  46. memset(output, 0x00, 100);
  47. strncpy( (char *) md_name, {text_md_name}, 100 );
  48. md_info = md_info_from_string(md_name);
  49. TEST_ASSERT( md_info != NULL );
  50. src_len = unhexify( src_str, {hex_src_string} );
  51. TEST_ASSERT ( 0 == md( md_info, src_str, src_len, output ) );
  52. hexify( hash_str, output, md_get_size(md_info) );
  53. TEST_ASSERT( strcmp( (char *) hash_str, {hex_hash_string} ) == 0 );
  54. }
  55. END_CASE
  56. BEGIN_CASE
  57. md_text_multi:text_md_name:text_src_string:hex_hash_string
  58. {
  59. char md_name[100];
  60. unsigned char src_str[1000];
  61. unsigned char hash_str[1000];
  62. unsigned char output[100];
  63. const md_info_t *md_info = NULL;
  64. md_context_t ctx = MD_CONTEXT_T_INIT;
  65. memset(md_name, 0x00, 100);
  66. memset(src_str, 0x00, 1000);
  67. memset(hash_str, 0x00, 1000);
  68. memset(output, 0x00, 100);
  69. strcpy( (char *) src_str, {text_src_string} );
  70. strncpy( (char *) md_name, {text_md_name}, 100 );
  71. md_info = md_info_from_string(md_name);
  72. TEST_ASSERT( md_info != NULL );
  73. TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
  74. TEST_ASSERT ( 0 == md_starts( &ctx ) );
  75. TEST_ASSERT ( ctx.md_ctx != NULL );
  76. TEST_ASSERT ( 0 == md_update( &ctx, src_str, strlen( (char *) src_str ) ) );
  77. TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
  78. TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
  79. hexify( hash_str, output, md_get_size(md_info) );
  80. TEST_ASSERT( strcmp( (char *) hash_str, {hex_hash_string} ) == 0 );
  81. }
  82. END_CASE
  83. BEGIN_CASE
  84. md_hex_multi:text_md_name:hex_src_string:hex_hash_string
  85. {
  86. char md_name[100];
  87. unsigned char src_str[10000];
  88. unsigned char hash_str[10000];
  89. unsigned char output[100];
  90. int src_len;
  91. const md_info_t *md_info = NULL;
  92. md_context_t ctx = MD_CONTEXT_T_INIT;
  93. memset(md_name, 0x00, 100);
  94. memset(src_str, 0x00, 10000);
  95. memset(hash_str, 0x00, 10000);
  96. memset(output, 0x00, 100);
  97. strncpy( (char *) md_name, {text_md_name}, 100 );
  98. md_info = md_info_from_string(md_name);
  99. TEST_ASSERT( md_info != NULL );
  100. TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
  101. src_len = unhexify( src_str, {hex_src_string} );
  102. TEST_ASSERT ( 0 == md_starts( &ctx ) );
  103. TEST_ASSERT ( ctx.md_ctx != NULL );
  104. TEST_ASSERT ( 0 == md_update( &ctx, src_str, src_len ) );
  105. TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
  106. TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
  107. hexify( hash_str, output, md_get_size(md_info) );
  108. TEST_ASSERT( strcmp( (char *) hash_str, {hex_hash_string} ) == 0 );
  109. }
  110. END_CASE
  111. BEGIN_CASE
  112. md_hmac:text_md_name:trunc_size:hex_key_string:hex_src_string:hex_hash_string
  113. {
  114. char md_name[100];
  115. unsigned char src_str[10000];
  116. unsigned char key_str[10000];
  117. unsigned char hash_str[10000];
  118. unsigned char output[100];
  119. int key_len, src_len;
  120. const md_info_t *md_info = NULL;
  121. memset(md_name, 0x00, 100);
  122. memset(src_str, 0x00, 10000);
  123. memset(key_str, 0x00, 10000);
  124. memset(hash_str, 0x00, 10000);
  125. memset(output, 0x00, 100);
  126. strncpy( (char *) md_name, {text_md_name}, 100 );
  127. md_info = md_info_from_string( md_name );
  128. TEST_ASSERT( md_info != NULL );
  129. key_len = unhexify( key_str, {hex_key_string} );
  130. src_len = unhexify( src_str, {hex_src_string} );
  131. TEST_ASSERT ( md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 );
  132. hexify( hash_str, output, md_get_size(md_info) );
  133. TEST_ASSERT( strncmp( (char *) hash_str, {hex_hash_string}, {trunc_size} * 2 ) == 0 );
  134. }
  135. END_CASE
  136. BEGIN_CASE
  137. md_hmac_multi:text_md_name:trunc_size:hex_key_string:hex_src_string:hex_hash_string
  138. {
  139. char md_name[100];
  140. unsigned char src_str[10000];
  141. unsigned char key_str[10000];
  142. unsigned char hash_str[10000];
  143. unsigned char output[100];
  144. int key_len, src_len;
  145. const md_info_t *md_info = NULL;
  146. md_context_t ctx = MD_CONTEXT_T_INIT;
  147. memset(md_name, 0x00, 100);
  148. memset(src_str, 0x00, 10000);
  149. memset(key_str, 0x00, 10000);
  150. memset(hash_str, 0x00, 10000);
  151. memset(output, 0x00, 100);
  152. strncpy( (char *) md_name, {text_md_name}, 100 );
  153. md_info = md_info_from_string( md_name );
  154. TEST_ASSERT( md_info != NULL );
  155. TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
  156. key_len = unhexify( key_str, {hex_key_string} );
  157. src_len = unhexify( src_str, {hex_src_string} );
  158. TEST_ASSERT ( 0 == md_hmac_starts( &ctx, key_str, key_len ) );
  159. TEST_ASSERT ( ctx.md_ctx != NULL );
  160. TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
  161. TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
  162. TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
  163. hexify( hash_str, output, md_get_size(md_info) );
  164. TEST_ASSERT( strncmp( (char *) hash_str, {hex_hash_string}, {trunc_size} * 2 ) == 0 );
  165. }
  166. END_CASE
  167. BEGIN_CASE
  168. md_file:text_md_name:filename:hex_hash_string
  169. {
  170. char md_name[100];
  171. unsigned char hash_str[1000];
  172. unsigned char output[100];
  173. const md_info_t *md_info = NULL;
  174. memset(md_name, 0x00, 100);
  175. memset(hash_str, 0x00, 1000);
  176. memset(output, 0x00, 100);
  177. strncpy( (char *) md_name, {text_md_name}, 100 );
  178. md_info = md_info_from_string( md_name );
  179. TEST_ASSERT( md_info != NULL );
  180. md_file( md_info, {filename}, output);
  181. hexify( hash_str, output, md_get_size(md_info) );
  182. TEST_ASSERT( strcmp( (char *) hash_str, {hex_hash_string} ) == 0 );
  183. }
  184. END_CASE