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