test_suite_constant_time_hmac.function 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* BEGIN_HEADER */
  2. #include <mbedtls/constant_time.h>
  3. #include <mbedtls/legacy_or_psa.h>
  4. #include <mbedtls/md.h>
  5. #include <constant_time_internal.h>
  6. #include <hash_info.h>
  7. #include <test/constant_flow.h>
  8. /* END_HEADER */
  9. /* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
  10. void ssl_cf_hmac(int hash)
  11. {
  12. /*
  13. * Test the function mbedtls_ct_hmac() against a reference
  14. * implementation.
  15. */
  16. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  17. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  18. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  19. psa_algorithm_t alg;
  20. psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
  21. #else
  22. mbedtls_md_context_t ctx, ref_ctx;
  23. const mbedtls_md_info_t *md_info;
  24. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  25. size_t out_len, block_size;
  26. size_t min_in_len, in_len, max_in_len, i;
  27. /* TLS additional data is 13 bytes (hence the "lucky 13" name) */
  28. unsigned char add_data[13];
  29. unsigned char ref_out[MBEDTLS_HASH_MAX_SIZE];
  30. unsigned char *data = NULL;
  31. unsigned char *out = NULL;
  32. unsigned char rec_num = 0;
  33. USE_PSA_INIT();
  34. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  35. alg = PSA_ALG_HMAC(mbedtls_hash_info_psa_from_md(hash));
  36. out_len = PSA_HASH_LENGTH(alg);
  37. block_size = PSA_HASH_BLOCK_LENGTH(alg);
  38. /* mbedtls_ct_hmac() requires the key to be exportable */
  39. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
  40. PSA_KEY_USAGE_VERIFY_HASH);
  41. psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(alg));
  42. psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
  43. #else
  44. mbedtls_md_init(&ctx);
  45. mbedtls_md_init(&ref_ctx);
  46. md_info = mbedtls_md_info_from_type(hash);
  47. TEST_ASSERT(md_info != NULL);
  48. out_len = mbedtls_md_get_size(md_info);
  49. TEST_ASSERT(out_len != 0);
  50. block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64;
  51. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  52. /* Use allocated out buffer to catch overwrites */
  53. ASSERT_ALLOC(out, out_len);
  54. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  55. /* Set up dummy key */
  56. memset(ref_out, 42, sizeof(ref_out));
  57. TEST_EQUAL(PSA_SUCCESS, psa_import_key(&attributes,
  58. ref_out, out_len,
  59. &key));
  60. #else
  61. /* Set up contexts with the given hash and a dummy key */
  62. TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 1));
  63. TEST_EQUAL(0, mbedtls_md_setup(&ref_ctx, md_info, 1));
  64. memset(ref_out, 42, sizeof(ref_out));
  65. TEST_EQUAL(0, mbedtls_md_hmac_starts(&ctx, ref_out, out_len));
  66. TEST_EQUAL(0, mbedtls_md_hmac_starts(&ref_ctx, ref_out, out_len));
  67. memset(ref_out, 0, sizeof(ref_out));
  68. #endif
  69. /*
  70. * Test all possible lengths up to a point. The difference between
  71. * max_in_len and min_in_len is at most 255, and make sure they both vary
  72. * by at least one block size.
  73. */
  74. for (max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++) {
  75. mbedtls_test_set_step(max_in_len * 10000);
  76. /* Use allocated in buffer to catch overreads */
  77. ASSERT_ALLOC(data, max_in_len);
  78. min_in_len = max_in_len > 255 ? max_in_len - 255 : 0;
  79. for (in_len = min_in_len; in_len <= max_in_len; in_len++) {
  80. mbedtls_test_set_step(max_in_len * 10000 + in_len);
  81. /* Set up dummy data and add_data */
  82. rec_num++;
  83. memset(add_data, rec_num, sizeof(add_data));
  84. for (i = 0; i < in_len; i++) {
  85. data[i] = (i & 0xff) ^ rec_num;
  86. }
  87. /* Get the function's result */
  88. TEST_CF_SECRET(&in_len, sizeof(in_len));
  89. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  90. TEST_EQUAL(0, mbedtls_ct_hmac(key, PSA_ALG_HMAC(alg),
  91. add_data, sizeof(add_data),
  92. data, in_len,
  93. min_in_len, max_in_len,
  94. out));
  95. #else
  96. TEST_EQUAL(0, mbedtls_ct_hmac(&ctx, add_data, sizeof(add_data),
  97. data, in_len,
  98. min_in_len, max_in_len,
  99. out));
  100. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  101. TEST_CF_PUBLIC(&in_len, sizeof(in_len));
  102. TEST_CF_PUBLIC(out, out_len);
  103. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  104. TEST_EQUAL(PSA_SUCCESS, psa_mac_verify_setup(&operation,
  105. key, alg));
  106. TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation, add_data,
  107. sizeof(add_data)));
  108. TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation,
  109. data, in_len));
  110. TEST_EQUAL(PSA_SUCCESS, psa_mac_verify_finish(&operation,
  111. out, out_len));
  112. #else
  113. /* Compute the reference result */
  114. TEST_EQUAL(0, mbedtls_md_hmac_update(&ref_ctx, add_data,
  115. sizeof(add_data)));
  116. TEST_EQUAL(0, mbedtls_md_hmac_update(&ref_ctx, data, in_len));
  117. TEST_EQUAL(0, mbedtls_md_hmac_finish(&ref_ctx, ref_out));
  118. TEST_EQUAL(0, mbedtls_md_hmac_reset(&ref_ctx));
  119. /* Compare */
  120. ASSERT_COMPARE(out, out_len, ref_out, out_len);
  121. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  122. }
  123. mbedtls_free(data);
  124. data = NULL;
  125. }
  126. exit:
  127. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  128. psa_mac_abort(&operation);
  129. psa_destroy_key(key);
  130. #else
  131. mbedtls_md_free(&ref_ctx);
  132. mbedtls_md_free(&ctx);
  133. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  134. mbedtls_free(data);
  135. mbedtls_free(out);
  136. USE_PSA_DONE();
  137. }
  138. /* END_CASE */