test_suite_hkdf.function 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/hkdf.h"
  3. #include "mbedtls/md_internal.h"
  4. /* END_HEADER */
  5. /* BEGIN_DEPENDENCIES
  6. * depends_on:MBEDTLS_HKDF_C
  7. * END_DEPENDENCIES
  8. */
  9. /* BEGIN_CASE */
  10. void test_hkdf( int md_alg, data_t *ikm, data_t *salt, data_t *info,
  11. data_t *expected_okm )
  12. {
  13. int ret;
  14. unsigned char okm[128] = { '\0' };
  15. const mbedtls_md_info_t *md = mbedtls_md_info_from_type( md_alg );
  16. TEST_ASSERT( md != NULL );
  17. TEST_ASSERT( expected_okm->len <= sizeof( okm ) );
  18. ret = mbedtls_hkdf( md, salt->x, salt->len, ikm->x, ikm->len,
  19. info->x, info->len, okm, expected_okm->len );
  20. TEST_ASSERT( ret == 0 );
  21. ASSERT_COMPARE( okm , expected_okm->len,
  22. expected_okm->x, expected_okm->len );
  23. }
  24. /* END_CASE */
  25. /* BEGIN_CASE */
  26. void test_hkdf_extract( int md_alg, char *hex_ikm_string,
  27. char *hex_salt_string, char *hex_prk_string )
  28. {
  29. int ret;
  30. unsigned char *ikm = NULL;
  31. unsigned char *salt = NULL;
  32. unsigned char *prk = NULL;
  33. unsigned char *output_prk = NULL;
  34. size_t ikm_len, salt_len, prk_len, output_prk_len;
  35. const mbedtls_md_info_t *md = mbedtls_md_info_from_type( md_alg );
  36. TEST_ASSERT( md != NULL );
  37. output_prk_len = mbedtls_md_get_size( md );
  38. output_prk = mbedtls_calloc( 1, output_prk_len );
  39. ikm = mbedtls_test_unhexify_alloc( hex_ikm_string, &ikm_len );
  40. salt = mbedtls_test_unhexify_alloc( hex_salt_string, &salt_len );
  41. prk = mbedtls_test_unhexify_alloc( hex_prk_string, &prk_len );
  42. ret = mbedtls_hkdf_extract( md, salt, salt_len, ikm, ikm_len, output_prk );
  43. TEST_ASSERT( ret == 0 );
  44. ASSERT_COMPARE( output_prk, output_prk_len, prk, prk_len );
  45. exit:
  46. mbedtls_free(ikm);
  47. mbedtls_free(salt);
  48. mbedtls_free(prk);
  49. mbedtls_free(output_prk);
  50. }
  51. /* END_CASE */
  52. /* BEGIN_CASE */
  53. void test_hkdf_expand( int md_alg, char *hex_info_string,
  54. char *hex_prk_string, char *hex_okm_string )
  55. {
  56. enum { OKM_LEN = 1024 };
  57. int ret;
  58. unsigned char *info = NULL;
  59. unsigned char *prk = NULL;
  60. unsigned char *okm = NULL;
  61. unsigned char *output_okm = NULL;
  62. size_t info_len, prk_len, okm_len;
  63. const mbedtls_md_info_t *md = mbedtls_md_info_from_type( md_alg );
  64. TEST_ASSERT( md != NULL );
  65. output_okm = mbedtls_calloc( OKM_LEN, 1 );
  66. prk = mbedtls_test_unhexify_alloc( hex_prk_string, &prk_len );
  67. info = mbedtls_test_unhexify_alloc( hex_info_string, &info_len );
  68. okm = mbedtls_test_unhexify_alloc( hex_okm_string, &okm_len );
  69. TEST_ASSERT( prk_len == mbedtls_md_get_size( md ) );
  70. TEST_ASSERT( okm_len < OKM_LEN );
  71. ret = mbedtls_hkdf_expand( md, prk, prk_len, info, info_len,
  72. output_okm, OKM_LEN );
  73. TEST_ASSERT( ret == 0 );
  74. ASSERT_COMPARE( output_okm, okm_len, okm, okm_len );
  75. exit:
  76. mbedtls_free(info);
  77. mbedtls_free(prk);
  78. mbedtls_free(okm);
  79. mbedtls_free(output_okm);
  80. }
  81. /* END_CASE */
  82. /* BEGIN_CASE */
  83. void test_hkdf_extract_ret( int hash_len, int ret )
  84. {
  85. int output_ret;
  86. unsigned char *salt = NULL;
  87. unsigned char *ikm = NULL;
  88. unsigned char *prk = NULL;
  89. size_t salt_len, ikm_len;
  90. struct mbedtls_md_info_t fake_md_info;
  91. memset( &fake_md_info, 0, sizeof( fake_md_info ) );
  92. fake_md_info.type = MBEDTLS_MD_NONE;
  93. fake_md_info.size = hash_len;
  94. prk = mbedtls_calloc( MBEDTLS_MD_MAX_SIZE, 1 );
  95. salt_len = 0;
  96. ikm_len = 0;
  97. output_ret = mbedtls_hkdf_extract( &fake_md_info, salt, salt_len,
  98. ikm, ikm_len, prk );
  99. TEST_ASSERT( output_ret == ret );
  100. exit:
  101. mbedtls_free(prk);
  102. }
  103. /* END_CASE */
  104. /* BEGIN_CASE */
  105. void test_hkdf_expand_ret( int hash_len, int prk_len, int okm_len, int ret )
  106. {
  107. int output_ret;
  108. unsigned char *info = NULL;
  109. unsigned char *prk = NULL;
  110. unsigned char *okm = NULL;
  111. size_t info_len;
  112. struct mbedtls_md_info_t fake_md_info;
  113. memset( &fake_md_info, 0, sizeof( fake_md_info ) );
  114. fake_md_info.type = MBEDTLS_MD_NONE;
  115. fake_md_info.size = hash_len;
  116. info_len = 0;
  117. if (prk_len > 0)
  118. prk = mbedtls_calloc( prk_len, 1 );
  119. if (okm_len > 0)
  120. okm = mbedtls_calloc( okm_len, 1 );
  121. output_ret = mbedtls_hkdf_expand( &fake_md_info, prk, prk_len,
  122. info, info_len, okm, okm_len );
  123. TEST_ASSERT( output_ret == ret );
  124. exit:
  125. mbedtls_free(prk);
  126. mbedtls_free(okm);
  127. }
  128. /* END_CASE */