test_suite_psa_crypto_hash.function 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* BEGIN_HEADER */
  2. #include <stdint.h>
  3. /* END_HEADER */
  4. /* BEGIN_DEPENDENCIES
  5. * depends_on:MBEDTLS_PSA_CRYPTO_C
  6. * END_DEPENDENCIES
  7. */
  8. /* BEGIN_CASE */
  9. void hash_finish(int alg_arg, data_t *input, data_t *expected_hash)
  10. {
  11. psa_algorithm_t alg = alg_arg;
  12. unsigned char actual_hash[PSA_HASH_MAX_SIZE];
  13. size_t actual_hash_length;
  14. psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  15. PSA_ASSERT(psa_crypto_init());
  16. PSA_ASSERT(psa_hash_setup(&operation, alg));
  17. PSA_ASSERT(psa_hash_update(&operation,
  18. input->x, input->len));
  19. PSA_ASSERT(psa_hash_finish(&operation,
  20. actual_hash, sizeof(actual_hash),
  21. &actual_hash_length));
  22. ASSERT_COMPARE(expected_hash->x, expected_hash->len,
  23. actual_hash, actual_hash_length);
  24. exit:
  25. psa_hash_abort(&operation);
  26. PSA_DONE();
  27. }
  28. /* END_CASE */
  29. /* BEGIN_CASE */
  30. void hash_verify(int alg_arg, data_t *input, data_t *expected_hash)
  31. {
  32. psa_algorithm_t alg = alg_arg;
  33. psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  34. PSA_ASSERT(psa_crypto_init());
  35. PSA_ASSERT(psa_hash_setup(&operation, alg));
  36. PSA_ASSERT(psa_hash_update(&operation,
  37. input->x,
  38. input->len));
  39. PSA_ASSERT(psa_hash_verify(&operation,
  40. expected_hash->x,
  41. expected_hash->len));
  42. exit:
  43. psa_hash_abort(&operation);
  44. PSA_DONE();
  45. }
  46. /* END_CASE */
  47. /* BEGIN_CASE */
  48. void hash_multi_part(int alg_arg, data_t *input, data_t *expected_hash)
  49. {
  50. psa_algorithm_t alg = alg_arg;
  51. unsigned char actual_hash[PSA_HASH_MAX_SIZE];
  52. size_t actual_hash_length;
  53. psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  54. psa_hash_operation_t operation2 = PSA_HASH_OPERATION_INIT;
  55. uint32_t len = 0;
  56. PSA_ASSERT(psa_crypto_init());
  57. do {
  58. memset(actual_hash, 0, sizeof(actual_hash));
  59. PSA_ASSERT(psa_hash_setup(&operation, alg));
  60. PSA_ASSERT(psa_hash_update(&operation,
  61. input->x, len));
  62. PSA_ASSERT(psa_hash_clone(&operation, &operation2));
  63. PSA_ASSERT(psa_hash_update(&operation,
  64. input->x + len, input->len - len));
  65. PSA_ASSERT(psa_hash_update(&operation2,
  66. input->x + len, input->len - len));
  67. PSA_ASSERT(psa_hash_finish(&operation,
  68. actual_hash, sizeof(actual_hash),
  69. &actual_hash_length));
  70. ASSERT_COMPARE(expected_hash->x, expected_hash->len,
  71. actual_hash, actual_hash_length);
  72. PSA_ASSERT(psa_hash_finish(&operation2,
  73. actual_hash, sizeof(actual_hash),
  74. &actual_hash_length));
  75. ASSERT_COMPARE(expected_hash->x, expected_hash->len,
  76. actual_hash, actual_hash_length);
  77. } while (len++ != input->len);
  78. exit:
  79. psa_hash_abort(&operation);
  80. psa_hash_abort(&operation2);
  81. PSA_DONE();
  82. }
  83. /* END_CASE */