test_suite_md.function (14722B)
1 /* BEGIN_HEADER */ 2 #include "mbedtls/md.h" 3 #include "mbedtls/psa_util.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 */ 19 void mbedtls_md_list() 20 { 21 const int *md_type_ptr; 22 const mbedtls_md_info_t *info; 23 mbedtls_md_context_t ctx; 24 mbedtls_md_init(&ctx); 25 unsigned char out[MBEDTLS_MD_MAX_SIZE] = { 0 }; 26 27 MD_PSA_INIT(); 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 59 exit: 60 mbedtls_md_free(&ctx); 61 MD_PSA_DONE(); 62 } 63 /* END_CASE */ 64 65 /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C */ 66 void 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 */ 87 void md_null_args() 88 { 89 mbedtls_md_context_t ctx; 90 mbedtls_md_init(&ctx); 91 #if defined(MBEDTLS_MD_C) 92 const mbedtls_md_info_t *info = mbedtls_md_info_from_type(*(mbedtls_md_list())); 93 #endif 94 unsigned char buf[1] = { 0 }; 95 96 MD_PSA_INIT(); 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 155 exit: 156 MD_PSA_DONE(); 157 } 158 /* END_CASE */ 159 160 /* BEGIN_CASE */ 161 void 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 */ 195 void 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 211 exit: 212 MD_PSA_DONE(); 213 } 214 /* END_CASE */ 215 216 /* BEGIN_CASE */ 217 void 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 232 exit: 233 MD_PSA_DONE(); 234 } 235 /* END_CASE */ 236 237 /* BEGIN_CASE */ 238 void 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 mbedtls_md_init(&ctx); 249 mbedtls_md_init(&ctx_copy); 250 251 MD_PSA_INIT(); 252 253 halfway = src_len / 2; 254 255 md_info = mbedtls_md_info_from_type(md_type); 256 TEST_ASSERT(md_info != NULL); 257 TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 0)); 258 TEST_EQUAL(0, mbedtls_md_setup(&ctx_copy, md_info, 0)); 259 #if defined(MBEDTLS_MD_C) 260 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info); 261 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info); 262 #endif /* MBEDTLS_MD_C */ 263 264 TEST_EQUAL(0, mbedtls_md_starts(&ctx)); 265 TEST_ASSERT(ctx.md_ctx != NULL); 266 TEST_EQUAL(0, mbedtls_md_update(&ctx, src, halfway)); 267 TEST_EQUAL(0, mbedtls_md_clone(&ctx_copy, &ctx)); 268 269 TEST_EQUAL(0, mbedtls_md_update(&ctx, src + halfway, src_len - halfway)); 270 TEST_EQUAL(0, mbedtls_md_finish(&ctx, output)); 271 TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len); 272 273 /* Test clone */ 274 memset(output, 0x00, sizeof(output)); 275 276 TEST_EQUAL(0, mbedtls_md_update(&ctx_copy, src + halfway, src_len - halfway)); 277 TEST_EQUAL(0, mbedtls_md_finish(&ctx_copy, output)); 278 TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len); 279 280 exit: 281 mbedtls_md_free(&ctx); 282 mbedtls_md_free(&ctx_copy); 283 MD_PSA_DONE(); 284 } 285 /* END_CASE */ 286 287 /* BEGIN_CASE */ 288 void md_hex_multi(int md_type, data_t *src_str, data_t *hash) 289 { 290 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 }; 291 const mbedtls_md_info_t *md_info = NULL; 292 mbedtls_md_context_t ctx, ctx_copy; 293 mbedtls_md_init(&ctx); 294 mbedtls_md_init(&ctx_copy); 295 int halfway; 296 297 MD_PSA_INIT(); 298 299 md_info = mbedtls_md_info_from_type(md_type); 300 TEST_ASSERT(md_info != NULL); 301 TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 0)); 302 TEST_EQUAL(0, mbedtls_md_setup(&ctx_copy, md_info, 0)); 303 #if defined(MBEDTLS_MD_C) 304 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info); 305 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx_copy) == md_info); 306 #endif /* MBEDTLS_MD_C */ 307 308 halfway = src_str->len / 2; 309 310 TEST_EQUAL(0, mbedtls_md_starts(&ctx)); 311 TEST_ASSERT(ctx.md_ctx != NULL); 312 TEST_EQUAL(0, mbedtls_md_update(&ctx, src_str->x, halfway)); 313 TEST_EQUAL(0, mbedtls_md_clone(&ctx_copy, &ctx)); 314 315 TEST_EQUAL(0, mbedtls_md_update(&ctx, src_str->x + halfway, src_str->len - halfway)); 316 TEST_EQUAL(0, mbedtls_md_finish(&ctx, output)); 317 TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len); 318 319 /* Test clone */ 320 memset(output, 0x00, sizeof(output)); 321 322 TEST_EQUAL(0, mbedtls_md_update(&ctx_copy, src_str->x + halfway, src_str->len - halfway)); 323 TEST_EQUAL(0, mbedtls_md_finish(&ctx_copy, output)); 324 TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len); 325 326 exit: 327 mbedtls_md_free(&ctx); 328 mbedtls_md_free(&ctx_copy); 329 MD_PSA_DONE(); 330 } 331 /* END_CASE */ 332 333 /* BEGIN_CASE depends_on:MBEDTLS_MD_C */ 334 void mbedtls_md_hmac(int md_type, int trunc_size, 335 data_t *key_str, data_t *src_str, 336 data_t *hash) 337 { 338 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 }; 339 const mbedtls_md_info_t *md_info = NULL; 340 341 MD_PSA_INIT(); 342 343 md_info = mbedtls_md_info_from_type(md_type); 344 TEST_ASSERT(md_info != NULL); 345 346 347 TEST_EQUAL(0, mbedtls_md_hmac(md_info, key_str->x, key_str->len, 348 src_str->x, src_str->len, output)); 349 350 TEST_MEMORY_COMPARE(output, trunc_size, hash->x, hash->len); 351 352 exit: 353 MD_PSA_DONE(); 354 } 355 /* END_CASE */ 356 357 /* BEGIN_CASE depends_on:MBEDTLS_MD_C */ 358 void md_hmac_multi(int md_type, int trunc_size, data_t *key_str, 359 data_t *src_str, data_t *hash) 360 { 361 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 }; 362 const mbedtls_md_info_t *md_info = NULL; 363 mbedtls_md_context_t ctx; 364 mbedtls_md_init(&ctx); 365 int halfway; 366 367 MD_PSA_INIT(); 368 369 md_info = mbedtls_md_info_from_type(md_type); 370 TEST_ASSERT(md_info != NULL); 371 TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 1)); 372 #if defined(MBEDTLS_MD_C) 373 TEST_ASSERT(mbedtls_md_info_from_ctx(&ctx) == md_info); 374 #endif 375 376 halfway = src_str->len / 2; 377 378 TEST_EQUAL(0, mbedtls_md_hmac_starts(&ctx, key_str->x, key_str->len)); 379 TEST_ASSERT(ctx.md_ctx != NULL); 380 TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x, halfway)); 381 TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway)); 382 TEST_EQUAL(0, mbedtls_md_hmac_finish(&ctx, output)); 383 384 TEST_MEMORY_COMPARE(output, trunc_size, hash->x, hash->len); 385 386 /* Test again, for reset() */ 387 memset(output, 0x00, sizeof(output)); 388 389 TEST_EQUAL(0, mbedtls_md_hmac_reset(&ctx)); 390 TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x, halfway)); 391 TEST_EQUAL(0, mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway)); 392 TEST_EQUAL(0, mbedtls_md_hmac_finish(&ctx, output)); 393 394 TEST_MEMORY_COMPARE(output, trunc_size, hash->x, hash->len); 395 396 exit: 397 mbedtls_md_free(&ctx); 398 MD_PSA_DONE(); 399 } 400 /* END_CASE */ 401 402 /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_MD_C */ 403 void mbedtls_md_file(int md_type, char *filename, 404 data_t *hash) 405 { 406 unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 }; 407 const mbedtls_md_info_t *md_info = NULL; 408 409 MD_PSA_INIT(); 410 411 md_info = mbedtls_md_info_from_type(md_type); 412 TEST_ASSERT(md_info != NULL); 413 414 TEST_EQUAL(0, mbedtls_md_file(md_info, filename, output)); 415 416 TEST_MEMORY_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len); 417 418 exit: 419 MD_PSA_DONE(); 420 } 421 /* END_CASE */ 422 423 /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C */ 424 void md_psa_dynamic_dispatch(int md_type, int pre_psa_ret, int post_psa_engine) 425 { 426 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type); 427 mbedtls_md_context_t ctx1, ctx2; 428 429 /* Intentionally no PSA init here! (Will be done later.) */ 430 431 mbedtls_md_init(&ctx1); 432 mbedtls_md_init(&ctx2); 433 434 TEST_ASSERT(md_info != NULL); 435 436 /* Before PSA crypto init */ 437 TEST_EQUAL(pre_psa_ret, mbedtls_md_setup(&ctx1, md_info, 0)); 438 TEST_EQUAL(pre_psa_ret, mbedtls_md_setup(&ctx2, md_info, 0)); 439 440 #if defined(MBEDTLS_MD_SOME_PSA) 441 TEST_EQUAL(ctx1.engine, MBEDTLS_MD_ENGINE_LEGACY); 442 TEST_EQUAL(ctx2.engine, MBEDTLS_MD_ENGINE_LEGACY); 443 #endif 444 445 /* Reset ctx1 but keep ctx2 for the cloning test */ 446 mbedtls_md_free(&ctx1); 447 mbedtls_md_init(&ctx1); 448 449 /* Now initilize PSA Crypto */ 450 MD_PSA_INIT(); 451 452 /* After PSA Crypto init */ 453 TEST_EQUAL(0, mbedtls_md_setup(&ctx1, md_info, 0)); 454 #if defined(MBEDTLS_MD_SOME_PSA) 455 TEST_EQUAL(ctx1.engine, post_psa_engine); 456 #endif 457 458 /* Cloning test */ 459 if (pre_psa_ret == 0) { 460 int exp_clone_ret = post_psa_engine == MBEDTLS_MD_ENGINE_PSA 461 ? MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE 462 : 0; 463 TEST_EQUAL(exp_clone_ret, mbedtls_md_clone(&ctx2, &ctx1)); 464 } 465 466 exit: 467 mbedtls_md_free(&ctx1); 468 mbedtls_md_free(&ctx2); 469 MD_PSA_DONE(); 470 } 471 /* END_CASE */