test_suite_md.function 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/md.h"
  3. #if defined(MBEDTLS_MD_SOME_PSA)
  4. #define MD_PSA_INIT() PSA_INIT()
  5. #define MD_PSA_DONE() PSA_DONE()
  6. #else /* MBEDTLS_MD_SOME_PSA */
  7. #define MD_PSA_INIT() ((void) 0)
  8. #define MD_PSA_DONE() ((void) 0)
  9. #endif /* MBEDTLS_MD_SOME_PSA */
  10. /* END_HEADER */
  11. /* BEGIN_DEPENDENCIES
  12. * depends_on:MBEDTLS_MD_LIGHT
  13. * END_DEPENDENCIES
  14. */
  15. /* BEGIN_CASE depends_on:MBEDTLS_MD_C */
  16. void mbedtls_md_list()
  17. {
  18. const int *md_type_ptr;
  19. const mbedtls_md_info_t *info;
  20. mbedtls_md_context_t ctx;
  21. unsigned char out[MBEDTLS_MD_MAX_SIZE] = { 0 };
  22. MD_PSA_INIT();
  23. mbedtls_md_init(&ctx);
  24. /*
  25. * Test that mbedtls_md_list() only returns valid MDs.
  26. */
  27. for (md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++) {
  28. info = mbedtls_md_info_from_type(*md_type_ptr);
  29. TEST_ASSERT(info != NULL);
  30. TEST_EQUAL(0, mbedtls_md_setup(&ctx, info, 0));
  31. TEST_EQUAL(0, mbedtls_md_starts(&ctx));
  32. TEST_EQUAL(0, mbedtls_md_finish(&ctx, out));
  33. mbedtls_md_free(&ctx);
  34. }
  35. exit:
  36. mbedtls_md_free(&ctx);
  37. MD_PSA_DONE();
  38. }
  39. /* END_CASE */
  40. /* BEGIN_CASE */
  41. void md_null_args()
  42. {
  43. mbedtls_md_context_t ctx;
  44. #if defined(MBEDTLS_MD_C)
  45. const mbedtls_md_info_t *info = mbedtls_md_info_from_type(*(mbedtls_md_list()));
  46. #endif
  47. unsigned char buf[1] = { 0 };
  48. MD_PSA_INIT();
  49. mbedtls_md_init(&ctx);
  50. TEST_EQUAL(0, mbedtls_md_get_size(NULL));
  51. #if defined(MBEDTLS_MD_C)
  52. TEST_EQUAL(mbedtls_md_get_type(NULL), MBEDTLS_MD_NONE);
  53. TEST_ASSERT(mbedtls_md_get_name(NULL) == NULL);
  54. TEST_ASSERT(mbedtls_md_info_from_string(NULL) == NULL);
  55. TEST_ASSERT(mbedtls_md_info_from_ctx(NULL) == NULL);
  56. TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == NULL);
  57. #endif /* MBEDTLS_MD_C */
  58. TEST_EQUAL(mbedtls_md_setup(&ctx, NULL, 0), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  59. #if defined(MBEDTLS_MD_C)
  60. TEST_EQUAL(mbedtls_md_setup(NULL, info, 0), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  61. #endif
  62. TEST_EQUAL(mbedtls_md_starts(NULL), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  63. TEST_EQUAL(mbedtls_md_starts(&ctx), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  64. TEST_EQUAL(mbedtls_md_update(NULL, buf, 1), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  65. TEST_EQUAL(mbedtls_md_update(&ctx, buf, 1), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  66. TEST_EQUAL(mbedtls_md_finish(NULL, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  67. TEST_EQUAL(mbedtls_md_finish(&ctx, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  68. TEST_EQUAL(mbedtls_md(NULL, buf, 1, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  69. #if defined(MBEDTLS_MD_C)
  70. #if defined(MBEDTLS_FS_IO)
  71. TEST_EQUAL(mbedtls_md_file(NULL, "", buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  72. #endif
  73. TEST_EQUAL(mbedtls_md_hmac_starts(NULL, buf, 1),
  74. MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  75. TEST_EQUAL(mbedtls_md_hmac_starts(&ctx, buf, 1),
  76. MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  77. TEST_EQUAL(mbedtls_md_hmac_update(NULL, buf, 1),
  78. MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  79. TEST_EQUAL(mbedtls_md_hmac_update(&ctx, buf, 1),
  80. MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  81. TEST_EQUAL(mbedtls_md_hmac_finish(NULL, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  82. TEST_EQUAL(mbedtls_md_hmac_finish(&ctx, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  83. TEST_EQUAL(mbedtls_md_hmac_reset(NULL), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  84. TEST_EQUAL(mbedtls_md_hmac_reset(&ctx), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  85. TEST_EQUAL(mbedtls_md_hmac(NULL, buf, 1, buf, 1, buf),
  86. MBEDTLS_ERR_MD_BAD_INPUT_DATA);
  87. #endif /* MBEDTLS_MD_C */
  88. /* Ok, this is not NULL arg but NULL return... */
  89. TEST_ASSERT(mbedtls_md_info_from_type(MBEDTLS_MD_NONE) == NULL);
  90. #if defined(MBEDTLS_MD_C)
  91. TEST_ASSERT(mbedtls_md_info_from_string("no such md") == NULL);
  92. #endif
  93. exit:
  94. MD_PSA_DONE();
  95. }
  96. /* END_CASE */
  97. /* BEGIN_CASE */
  98. void md_info(int md_type, char *md_name, int md_size)
  99. {
  100. const mbedtls_md_info_t *md_info;
  101. #if defined(MBEDTLS_MD_C)
  102. const int *md_type_ptr;
  103. #else
  104. (void) md_name;
  105. #endif
  106. /* Note: PSA Crypto init not needed for info functions */
  107. md_info = mbedtls_md_info_from_type(md_type);
  108. TEST_ASSERT(md_info != NULL);
  109. #if defined(MBEDTLS_MD_C)
  110. TEST_ASSERT(md_info == mbedtls_md_info_from_string(md_name));
  111. #endif
  112. TEST_EQUAL(mbedtls_md_get_type(md_info), (mbedtls_md_type_t) md_type);
  113. TEST_EQUAL(mbedtls_md_get_size(md_info), (unsigned char) md_size);
  114. #if defined(MBEDTLS_MD_C)
  115. TEST_EQUAL(0, strcmp(mbedtls_md_get_name(md_info), md_name));
  116. int found = 0;
  117. for (md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++) {
  118. if (*md_type_ptr == md_type) {
  119. found = 1;
  120. }
  121. }
  122. TEST_EQUAL(found, 1);
  123. #endif /* MBEDTLS_MD_C */
  124. }
  125. /* END_CASE */
  126. /* BEGIN_CASE */
  127. void md_text(int md_type, char *text_src_string, data_t *hash)
  128. {
  129. unsigned char *src = (unsigned char *) text_src_string;
  130. size_t src_len = strlen(text_src_string);
  131. unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
  132. const mbedtls_md_info_t *md_info = NULL;
  133. MD_PSA_INIT();
  134. md_info = mbedtls_md_info_from_type(md_type);
  135. TEST_ASSERT(md_info != NULL);
  136. TEST_EQUAL(0, mbedtls_md(md_info, src, src_len, output));
  137. ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
  138. exit:
  139. MD_PSA_DONE();
  140. }
  141. /* END_CASE */
  142. /* BEGIN_CASE */
  143. void md_hex(int md_type, data_t *src_str, data_t *hash)
  144. {
  145. unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
  146. const mbedtls_md_info_t *md_info = NULL;
  147. MD_PSA_INIT();
  148. md_info = mbedtls_md_info_from_type(md_type);
  149. TEST_ASSERT(md_info != NULL);
  150. TEST_EQUAL(0, mbedtls_md(md_info, src_str->x, src_str->len, output));
  151. ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
  152. exit:
  153. MD_PSA_DONE();
  154. }
  155. /* END_CASE */
  156. /* BEGIN_CASE */
  157. void md_text_multi(int md_type, char *text_src_string,
  158. data_t *hash)
  159. {
  160. unsigned char *src = (unsigned char *) text_src_string;
  161. size_t src_len = strlen(text_src_string);
  162. unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
  163. size_t halfway;
  164. const mbedtls_md_info_t *md_info = NULL;
  165. mbedtls_md_context_t ctx, ctx_copy;
  166. MD_PSA_INIT();
  167. mbedtls_md_init(&ctx);
  168. mbedtls_md_init(&ctx_copy);
  169. halfway = src_len / 2;
  170. md_info = mbedtls_md_info_from_type(md_type);
  171. TEST_ASSERT(md_info != NULL);
  172. TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 0));
  173. TEST_EQUAL(0, mbedtls_md_setup(&ctx_copy, md_info, 0));
  174. #if defined(MBEDTLS_MD_C)
  175. TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
  176. TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info);
  177. #endif /* MBEDTLS_MD_C */
  178. TEST_EQUAL(0, mbedtls_md_starts(&ctx));
  179. TEST_ASSERT(ctx.md_ctx != NULL);
  180. TEST_EQUAL(0, mbedtls_md_update(&ctx, src, halfway));
  181. TEST_EQUAL(0, mbedtls_md_clone(&ctx_copy, &ctx));
  182. TEST_EQUAL(0, mbedtls_md_update(&ctx, src + halfway, src_len - halfway));
  183. TEST_EQUAL(0, mbedtls_md_finish(&ctx, output));
  184. ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
  185. /* Test clone */
  186. memset(output, 0x00, sizeof(output));
  187. TEST_EQUAL(0, mbedtls_md_update(&ctx_copy, src + halfway, src_len - halfway));
  188. TEST_EQUAL(0, mbedtls_md_finish(&ctx_copy, output));
  189. ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
  190. exit:
  191. mbedtls_md_free(&ctx);
  192. mbedtls_md_free(&ctx_copy);
  193. MD_PSA_DONE();
  194. }
  195. /* END_CASE */
  196. /* BEGIN_CASE */
  197. void md_hex_multi(int md_type, data_t *src_str, data_t *hash)
  198. {
  199. unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
  200. const mbedtls_md_info_t *md_info = NULL;
  201. mbedtls_md_context_t ctx, ctx_copy;
  202. int halfway;
  203. MD_PSA_INIT();
  204. mbedtls_md_init(&ctx);
  205. mbedtls_md_init(&ctx_copy);
  206. md_info = mbedtls_md_info_from_type(md_type);
  207. TEST_ASSERT(md_info != NULL);
  208. TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 0));
  209. TEST_EQUAL(0, mbedtls_md_setup(&ctx_copy, md_info, 0));
  210. #if defined(MBEDTLS_MD_C)
  211. TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
  212. TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info);
  213. #endif /* MBEDTLS_MD_C */
  214. halfway = src_str->len / 2;
  215. TEST_EQUAL(0, mbedtls_md_starts(&ctx));
  216. TEST_ASSERT(ctx.md_ctx != NULL);
  217. TEST_EQUAL(0, mbedtls_md_update(&ctx, src_str->x, halfway));
  218. TEST_EQUAL(0, mbedtls_md_clone(&ctx_copy, &ctx));
  219. TEST_EQUAL(0, mbedtls_md_update(&ctx, src_str->x + halfway, src_str->len - halfway));
  220. TEST_EQUAL(0, mbedtls_md_finish(&ctx, output));
  221. ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
  222. /* Test clone */
  223. memset(output, 0x00, sizeof(output));
  224. TEST_EQUAL(0, mbedtls_md_update(&ctx_copy, src_str->x + halfway, src_str->len - halfway));
  225. TEST_EQUAL(0, mbedtls_md_finish(&ctx_copy, output));
  226. ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
  227. exit:
  228. mbedtls_md_free(&ctx);
  229. mbedtls_md_free(&ctx_copy);
  230. MD_PSA_DONE();
  231. }
  232. /* END_CASE */
  233. /* BEGIN_CASE depends_on:MBEDTLS_MD_C */
  234. void mbedtls_md_hmac(int md_type, int trunc_size,
  235. data_t *key_str, data_t *src_str,
  236. data_t *hash)
  237. {
  238. unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
  239. const mbedtls_md_info_t *md_info = NULL;
  240. MD_PSA_INIT();
  241. md_info = mbedtls_md_info_from_type(md_type);
  242. TEST_ASSERT(md_info != NULL);
  243. TEST_EQUAL(0, mbedtls_md_hmac(md_info, key_str->x, key_str->len,
  244. src_str->x, src_str->len, output));
  245. ASSERT_COMPARE(output, trunc_size, hash->x, hash->len);
  246. exit:
  247. MD_PSA_DONE();
  248. }
  249. /* END_CASE */
  250. /* BEGIN_CASE depends_on:MBEDTLS_MD_C */
  251. void md_hmac_multi(int md_type, int trunc_size, data_t *key_str,
  252. data_t *src_str, data_t *hash)
  253. {
  254. unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
  255. const mbedtls_md_info_t *md_info = NULL;
  256. mbedtls_md_context_t ctx;
  257. int halfway;
  258. MD_PSA_INIT();
  259. mbedtls_md_init(&ctx);
  260. md_info = mbedtls_md_info_from_type(md_type);
  261. TEST_ASSERT(md_info != NULL);
  262. TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 1));
  263. #if defined(MBEDTLS_MD_C)
  264. TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info);
  265. #endif
  266. halfway = src_str->len / 2;
  267. TEST_EQUAL(0, mbedtls_md_hmac_starts(&ctx, key_str->x, key_str->len));
  268. TEST_ASSERT(ctx.md_ctx != NULL);
  269. TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
  270. TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
  271. TEST_EQUAL(0, mbedtls_md_hmac_finish(&ctx, output));
  272. ASSERT_COMPARE(output, trunc_size, hash->x, hash->len);
  273. /* Test again, for reset() */
  274. memset(output, 0x00, sizeof(output));
  275. TEST_EQUAL(0, mbedtls_md_hmac_reset(&ctx));
  276. TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x, halfway));
  277. TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway));
  278. TEST_EQUAL(0, mbedtls_md_hmac_finish(&ctx, output));
  279. ASSERT_COMPARE(output, trunc_size, hash->x, hash->len);
  280. exit:
  281. mbedtls_md_free(&ctx);
  282. MD_PSA_DONE();
  283. }
  284. /* END_CASE */
  285. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_MD_C */
  286. void mbedtls_md_file(int md_type, char *filename,
  287. data_t *hash)
  288. {
  289. unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
  290. const mbedtls_md_info_t *md_info = NULL;
  291. MD_PSA_INIT();
  292. md_info = mbedtls_md_info_from_type(md_type);
  293. TEST_ASSERT(md_info != NULL);
  294. TEST_EQUAL(0, mbedtls_md_file(md_info, filename, output));
  295. ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
  296. exit:
  297. MD_PSA_DONE();
  298. }
  299. /* END_CASE */
  300. /* BEGIN_CASE */
  301. void md_psa_dynamic_dispatch(int md_type, int pre_psa_ret, int post_psa_engine)
  302. {
  303. const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type);
  304. TEST_ASSERT(md_info != NULL);
  305. mbedtls_md_context_t ctx1, ctx2;
  306. /* Intentionally no PSA init here! (Will be done later.) */
  307. mbedtls_md_init(&ctx1);
  308. mbedtls_md_init(&ctx2);
  309. /* Before PSA crypto init */
  310. TEST_EQUAL(pre_psa_ret, mbedtls_md_setup(&ctx1, md_info, 0));
  311. TEST_EQUAL(pre_psa_ret, mbedtls_md_setup(&ctx2, md_info, 0));
  312. #if defined(MBEDTLS_MD_SOME_PSA)
  313. TEST_EQUAL(ctx1.engine, MBEDTLS_MD_ENGINE_LEGACY);
  314. TEST_EQUAL(ctx2.engine, MBEDTLS_MD_ENGINE_LEGACY);
  315. #endif
  316. /* Reset ctx1 but keep ctx2 for the cloning test */
  317. mbedtls_md_free(&ctx1);
  318. mbedtls_md_init(&ctx1);
  319. /* Now initilize PSA Crypto */
  320. MD_PSA_INIT();
  321. /* After PSA Crypto init */
  322. TEST_EQUAL(0, mbedtls_md_setup(&ctx1, md_info, 0));
  323. #if defined(MBEDTLS_MD_SOME_PSA)
  324. TEST_EQUAL(ctx1.engine, post_psa_engine);
  325. #endif
  326. /* Cloning test */
  327. if (pre_psa_ret == 0) {
  328. int exp_clone_ret = post_psa_engine == MBEDTLS_MD_ENGINE_PSA
  329. ? MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE
  330. : 0;
  331. TEST_EQUAL(exp_clone_ret, mbedtls_md_clone(&ctx2, &ctx1));
  332. }
  333. exit:
  334. mbedtls_md_free(&ctx1);
  335. mbedtls_md_free(&ctx2);
  336. MD_PSA_DONE();
  337. }
  338. /* END_CASE */