1/* BEGIN_HEADER */ 2#include "mbedtls/md.h" 3#include "md_psa.h" 4 5#include "mbedtls/oid.h" 6#include "mbedtls/asn1.h" 7 8#define MD_PSA(md, psa) \ 9 TEST_EQUAL(mbedtls_md_psa_alg_from_type(md), psa); \ 10 TEST_EQUAL(mbedtls_md_type_from_psa_alg(psa), md); 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#if defined(MBEDTLS_PSA_CRYPTO_C) 41 /* Ensure that we can convert to and from a psa_algorithm_t */ 42 psa_algorithm_t p = mbedtls_md_psa_alg_from_type(*md_type_ptr); 43 TEST_ASSERT(p != PSA_ALG_NONE); 44 TEST_EQUAL(*md_type_ptr, mbedtls_md_type_from_psa_alg(p)); 45#endif 46 47#if defined(MBEDTLS_OID_C) 48 mbedtls_asn1_buf asn1; 49 /* Check that we have an OID definition */ 50 TEST_EQUAL(mbedtls_oid_get_oid_by_md((mbedtls_md_type_t) *md_type_ptr, 51 (const char **) &asn1.p, &asn1.len), 0); 52 /* Check that this OID definition maps back to the correct mbedtls_md_type_t */ 53 mbedtls_md_type_t m; 54 TEST_EQUAL(mbedtls_oid_get_md_alg(&asn1, &m), 0); 55 TEST_EQUAL(m, *md_type_ptr); 56#endif 57 } 58 59exit: 60 mbedtls_md_free(&ctx); 61 MD_PSA_DONE(); 62} 63/* END_CASE */ 64 65/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C */ 66void md_to_from_psa() 67{ 68 /* We use a simplified implementation that relies on numerical values 69 * being aligned, so make sure they remain so. */ 70 MD_PSA(MBEDTLS_MD_MD5, PSA_ALG_MD5); 71 MD_PSA(MBEDTLS_MD_RIPEMD160, PSA_ALG_RIPEMD160); 72 MD_PSA(MBEDTLS_MD_SHA1, PSA_ALG_SHA_1); 73 MD_PSA(MBEDTLS_MD_SHA224, PSA_ALG_SHA_224); 74 MD_PSA(MBEDTLS_MD_SHA256, PSA_ALG_SHA_256); 75 MD_PSA(MBEDTLS_MD_SHA384, PSA_ALG_SHA_384); 76 MD_PSA(MBEDTLS_MD_SHA512, PSA_ALG_SHA_512); 77 MD_PSA(MBEDTLS_MD_SHA3_224, PSA_ALG_SHA3_224); 78 MD_PSA(MBEDTLS_MD_SHA3_256, PSA_ALG_SHA3_256); 79 MD_PSA(MBEDTLS_MD_SHA3_384, PSA_ALG_SHA3_384); 80 MD_PSA(MBEDTLS_MD_SHA3_512, PSA_ALG_SHA3_512); 81 82 /* Don't test for NONE<->NONE as this is not guaranteed */ 83} 84/* END_CASE */ 85 86/* BEGIN_CASE */ 87void md_null_args() 88{ 89 mbedtls_md_context_t ctx; 90#if defined(MBEDTLS_MD_C) 91 const mbedtls_md_info_t *info = mbedtls_md_info_from_type(*(mbedtls_md_list())); 92#endif 93 unsigned char buf[1] = { 0 }; 94 95 MD_PSA_INIT(); 96 mbedtls_md_init(&ctx); 97 98 TEST_EQUAL(0, mbedtls_md_get_size(NULL)); 99#if defined(MBEDTLS_MD_C) 100 TEST_EQUAL(mbedtls_md_get_type(NULL), MBEDTLS_MD_NONE); 101 TEST_ASSERT(mbedtls_md_get_name(NULL) == NULL); 102 103 TEST_ASSERT(mbedtls_md_info_from_string(NULL) == NULL); 104 TEST_ASSERT(mbedtls_md_info_from_ctx(NULL) == NULL); 105 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == NULL); 106#endif /* MBEDTLS_MD_C */ 107 108 TEST_EQUAL(mbedtls_md_setup(&ctx, NULL, 0), MBEDTLS_ERR_MD_BAD_INPUT_DATA); 109#if defined(MBEDTLS_MD_C) 110 TEST_EQUAL(mbedtls_md_setup(NULL, info, 0), MBEDTLS_ERR_MD_BAD_INPUT_DATA); 111 112 TEST_EQUAL(mbedtls_md_starts(NULL), MBEDTLS_ERR_MD_BAD_INPUT_DATA); 113 TEST_EQUAL(mbedtls_md_starts(&ctx), MBEDTLS_ERR_MD_BAD_INPUT_DATA); 114 115 TEST_EQUAL(mbedtls_md_update(NULL, buf, 1), MBEDTLS_ERR_MD_BAD_INPUT_DATA); 116 TEST_EQUAL(mbedtls_md_update(&ctx, buf, 1), MBEDTLS_ERR_MD_BAD_INPUT_DATA); 117 118 TEST_EQUAL(mbedtls_md_finish(NULL, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA); 119 TEST_EQUAL(mbedtls_md_finish(&ctx, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA); 120#endif 121 122 TEST_EQUAL(mbedtls_md(NULL, buf, 1, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA); 123 124#if defined(MBEDTLS_MD_C) 125#if defined(MBEDTLS_FS_IO) 126 TEST_EQUAL(mbedtls_md_file(NULL, "", buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA); 127#endif 128 129 TEST_EQUAL(mbedtls_md_hmac_starts(NULL, buf, 1), 130 MBEDTLS_ERR_MD_BAD_INPUT_DATA); 131 TEST_EQUAL(mbedtls_md_hmac_starts(&ctx, buf, 1), 132 MBEDTLS_ERR_MD_BAD_INPUT_DATA); 133 134 TEST_EQUAL(mbedtls_md_hmac_update(NULL, buf, 1), 135 MBEDTLS_ERR_MD_BAD_INPUT_DATA); 136 TEST_EQUAL(mbedtls_md_hmac_update(&ctx, buf, 1), 137 MBEDTLS_ERR_MD_BAD_INPUT_DATA); 138 139 TEST_EQUAL(mbedtls_md_hmac_finish(NULL, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA); 140 TEST_EQUAL(mbedtls_md_hmac_finish(&ctx, buf), MBEDTLS_ERR_MD_BAD_INPUT_DATA); 141 142 TEST_EQUAL(mbedtls_md_hmac_reset(NULL), MBEDTLS_ERR_MD_BAD_INPUT_DATA); 143 TEST_EQUAL(mbedtls_md_hmac_reset(&ctx), MBEDTLS_ERR_MD_BAD_INPUT_DATA); 144 145 TEST_EQUAL(mbedtls_md_hmac(NULL, buf, 1, buf, 1, buf), 146 MBEDTLS_ERR_MD_BAD_INPUT_DATA); 147#endif /* MBEDTLS_MD_C */ 148 149 /* Ok, this is not NULL arg but NULL return... */ 150 TEST_ASSERT(mbedtls_md_info_from_type(MBEDTLS_MD_NONE) == NULL); 151#if defined(MBEDTLS_MD_C) 152 TEST_ASSERT(mbedtls_md_info_from_string("no such md") == NULL); 153#endif 154 155exit: 156 MD_PSA_DONE(); 157} 158/* END_CASE */ 159 160/* BEGIN_CASE */ 161void md_info(int md_type, char *md_name, int md_size) 162{ 163 const mbedtls_md_info_t *md_info; 164#if defined(MBEDTLS_MD_C) 165 const int *md_type_ptr; 166#else 167 (void) md_name; 168#endif 169 170 /* Note: PSA Crypto init not needed for info functions */ 171 172 md_info = mbedtls_md_info_from_type(md_type); 173 TEST_ASSERT(md_info != NULL); 174#if defined(MBEDTLS_MD_C) 175 TEST_ASSERT(md_info == mbedtls_md_info_from_string(md_name)); 176#endif 177 178 TEST_EQUAL(mbedtls_md_get_type(md_info), (mbedtls_md_type_t) md_type); 179 TEST_EQUAL(mbedtls_md_get_size(md_info), (unsigned char) md_size); 180#if defined(MBEDTLS_MD_C) 181 TEST_EQUAL(0, strcmp(mbedtls_md_get_name(md_info), md_name)); 182 183 int found = 0; 184 for (md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++) { 185 if (*md_type_ptr == md_type) { 186 found = 1; 187 } 188 } 189 TEST_EQUAL(found, 1); 190#endif /* MBEDTLS_MD_C */ 191} 192/* END_CASE */ 193 194/* BEGIN_CASE */ 195void md_text(int md_type, char *text_src_string, data_t *hash) 196{ 197 unsigned char *src = (unsigned char *) text_src_string; 198 size_t src_len = strlen(text_src_string); 199 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 }; 200 const mbedtls_md_info_t *md_info = NULL; 201 202 MD_PSA_INIT(); 203 204 md_info = mbedtls_md_info_from_type(md_type); 205 TEST_ASSERT(md_info != NULL); 206 207 TEST_EQUAL(0, mbedtls_md(md_info, src, src_len, output)); 208 209 TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len); 210 211exit: 212 MD_PSA_DONE(); 213} 214/* END_CASE */ 215 216/* BEGIN_CASE */ 217void md_hex(int md_type, data_t *src_str, data_t *hash) 218{ 219 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 }; 220 const mbedtls_md_info_t *md_info = NULL; 221 222 MD_PSA_INIT(); 223 224 md_info = mbedtls_md_info_from_type(md_type); 225 TEST_ASSERT(md_info != NULL); 226 227 TEST_EQUAL(0, mbedtls_md(md_info, src_str->x, src_str->len, output)); 228 229 230 TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len); 231 232exit: 233 MD_PSA_DONE(); 234} 235/* END_CASE */ 236 237/* BEGIN_CASE */ 238void md_text_multi(int md_type, char *text_src_string, 239 data_t *hash) 240{ 241 unsigned char *src = (unsigned char *) text_src_string; 242 size_t src_len = strlen(text_src_string); 243 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 }; 244 size_t halfway; 245 246 const mbedtls_md_info_t *md_info = NULL; 247 mbedtls_md_context_t ctx, ctx_copy; 248 249 MD_PSA_INIT(); 250 251 mbedtls_md_init(&ctx); 252 mbedtls_md_init(&ctx_copy); 253 254 halfway = src_len / 2; 255 256 md_info = mbedtls_md_info_from_type(md_type); 257 TEST_ASSERT(md_info != NULL); 258 TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 0)); 259 TEST_EQUAL(0, mbedtls_md_setup(&ctx_copy, md_info, 0)); 260#if defined(MBEDTLS_MD_C) 261 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info); 262 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info); 263#endif /* MBEDTLS_MD_C */ 264 265 TEST_EQUAL(0, mbedtls_md_starts(&ctx)); 266 TEST_ASSERT(ctx.md_ctx != NULL); 267 TEST_EQUAL(0, mbedtls_md_update(&ctx, src, halfway)); 268 TEST_EQUAL(0, mbedtls_md_clone(&ctx_copy, &ctx)); 269 270 TEST_EQUAL(0, mbedtls_md_update(&ctx, src + halfway, src_len - halfway)); 271 TEST_EQUAL(0, mbedtls_md_finish(&ctx, output)); 272 TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len); 273 274 /* Test clone */ 275 memset(output, 0x00, sizeof(output)); 276 277 TEST_EQUAL(0, mbedtls_md_update(&ctx_copy, src + halfway, src_len - halfway)); 278 TEST_EQUAL(0, mbedtls_md_finish(&ctx_copy, output)); 279 TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len); 280 281exit: 282 mbedtls_md_free(&ctx); 283 mbedtls_md_free(&ctx_copy); 284 MD_PSA_DONE(); 285} 286/* END_CASE */ 287 288/* BEGIN_CASE */ 289void md_hex_multi(int md_type, data_t *src_str, data_t *hash) 290{ 291 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 }; 292 const mbedtls_md_info_t *md_info = NULL; 293 mbedtls_md_context_t ctx, ctx_copy; 294 int halfway; 295 296 MD_PSA_INIT(); 297 298 mbedtls_md_init(&ctx); 299 mbedtls_md_init(&ctx_copy); 300 301 md_info = mbedtls_md_info_from_type(md_type); 302 TEST_ASSERT(md_info != NULL); 303 TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 0)); 304 TEST_EQUAL(0, mbedtls_md_setup(&ctx_copy, md_info, 0)); 305#if defined(MBEDTLS_MD_C) 306 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info); 307 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info); 308#endif /* MBEDTLS_MD_C */ 309 310 halfway = src_str->len / 2; 311 312 TEST_EQUAL(0, mbedtls_md_starts(&ctx)); 313 TEST_ASSERT(ctx.md_ctx != NULL); 314 TEST_EQUAL(0, mbedtls_md_update(&ctx, src_str->x, halfway)); 315 TEST_EQUAL(0, mbedtls_md_clone(&ctx_copy, &ctx)); 316 317 TEST_EQUAL(0, mbedtls_md_update(&ctx, src_str->x + halfway, src_str->len - halfway)); 318 TEST_EQUAL(0, mbedtls_md_finish(&ctx, output)); 319 TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len); 320 321 /* Test clone */ 322 memset(output, 0x00, sizeof(output)); 323 324 TEST_EQUAL(0, mbedtls_md_update(&ctx_copy, src_str->x + halfway, src_str->len - halfway)); 325 TEST_EQUAL(0, mbedtls_md_finish(&ctx_copy, output)); 326 TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len); 327 328exit: 329 mbedtls_md_free(&ctx); 330 mbedtls_md_free(&ctx_copy); 331 MD_PSA_DONE(); 332} 333/* END_CASE */ 334 335/* BEGIN_CASE depends_on:MBEDTLS_MD_C */ 336void mbedtls_md_hmac(int md_type, int trunc_size, 337 data_t *key_str, data_t *src_str, 338 data_t *hash) 339{ 340 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 }; 341 const mbedtls_md_info_t *md_info = NULL; 342 343 MD_PSA_INIT(); 344 345 md_info = mbedtls_md_info_from_type(md_type); 346 TEST_ASSERT(md_info != NULL); 347 348 349 TEST_EQUAL(0, mbedtls_md_hmac(md_info, key_str->x, key_str->len, 350 src_str->x, src_str->len, output)); 351 352 TEST_MEMORY_COMPARE(output, trunc_size, hash->x, hash->len); 353 354exit: 355 MD_PSA_DONE(); 356} 357/* END_CASE */ 358 359/* BEGIN_CASE depends_on:MBEDTLS_MD_C */ 360void md_hmac_multi(int md_type, int trunc_size, data_t *key_str, 361 data_t *src_str, data_t *hash) 362{ 363 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 }; 364 const mbedtls_md_info_t *md_info = NULL; 365 mbedtls_md_context_t ctx; 366 int halfway; 367 368 MD_PSA_INIT(); 369 370 mbedtls_md_init(&ctx); 371 372 md_info = mbedtls_md_info_from_type(md_type); 373 TEST_ASSERT(md_info != NULL); 374 TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 1)); 375#if defined(MBEDTLS_MD_C) 376 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info); 377#endif 378 379 halfway = src_str->len / 2; 380 381 TEST_EQUAL(0, mbedtls_md_hmac_starts(&ctx, key_str->x, key_str->len)); 382 TEST_ASSERT(ctx.md_ctx != NULL); 383 TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x, halfway)); 384 TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway)); 385 TEST_EQUAL(0, mbedtls_md_hmac_finish(&ctx, output)); 386 387 TEST_MEMORY_COMPARE(output, trunc_size, hash->x, hash->len); 388 389 /* Test again, for reset() */ 390 memset(output, 0x00, sizeof(output)); 391 392 TEST_EQUAL(0, mbedtls_md_hmac_reset(&ctx)); 393 TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x, halfway)); 394 TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway)); 395 TEST_EQUAL(0, mbedtls_md_hmac_finish(&ctx, output)); 396 397 TEST_MEMORY_COMPARE(output, trunc_size, hash->x, hash->len); 398 399exit: 400 mbedtls_md_free(&ctx); 401 MD_PSA_DONE(); 402} 403/* END_CASE */ 404 405/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_MD_C */ 406void mbedtls_md_file(int md_type, char *filename, 407 data_t *hash) 408{ 409 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 }; 410 const mbedtls_md_info_t *md_info = NULL; 411 412 MD_PSA_INIT(); 413 414 md_info = mbedtls_md_info_from_type(md_type); 415 TEST_ASSERT(md_info != NULL); 416 417 TEST_EQUAL(0, mbedtls_md_file(md_info, filename, output)); 418 419 TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len); 420 421exit: 422 MD_PSA_DONE(); 423} 424/* END_CASE */ 425 426/* BEGIN_CASE */ 427void md_psa_dynamic_dispatch(int md_type, int pre_psa_ret, int post_psa_engine) 428{ 429 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type); 430 mbedtls_md_context_t ctx1, ctx2; 431 432 /* Intentionally no PSA init here! (Will be done later.) */ 433 434 mbedtls_md_init(&ctx1); 435 mbedtls_md_init(&ctx2); 436 437 TEST_ASSERT(md_info != NULL); 438 439 /* Before PSA crypto init */ 440 TEST_EQUAL(pre_psa_ret, mbedtls_md_setup(&ctx1, md_info, 0)); 441 TEST_EQUAL(pre_psa_ret, mbedtls_md_setup(&ctx2, md_info, 0)); 442 443#if defined(MBEDTLS_MD_SOME_PSA) 444 TEST_EQUAL(ctx1.engine, MBEDTLS_MD_ENGINE_LEGACY); 445 TEST_EQUAL(ctx2.engine, MBEDTLS_MD_ENGINE_LEGACY); 446#endif 447 448 /* Reset ctx1 but keep ctx2 for the cloning test */ 449 mbedtls_md_free(&ctx1); 450 mbedtls_md_init(&ctx1); 451 452 /* Now initilize PSA Crypto */ 453 MD_PSA_INIT(); 454 455 /* After PSA Crypto init */ 456 TEST_EQUAL(0, mbedtls_md_setup(&ctx1, md_info, 0)); 457#if defined(MBEDTLS_MD_SOME_PSA) 458 TEST_EQUAL(ctx1.engine, post_psa_engine); 459#endif 460 461 /* Cloning test */ 462 if (pre_psa_ret == 0) { 463 int exp_clone_ret = post_psa_engine == MBEDTLS_MD_ENGINE_PSA 464 ? MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE 465 : 0; 466 TEST_EQUAL(exp_clone_ret, mbedtls_md_clone(&ctx2, &ctx1)); 467 } 468 469exit: 470 mbedtls_md_free(&ctx1); 471 mbedtls_md_free(&ctx2); 472 MD_PSA_DONE(); 473} 474/* END_CASE */ 475