test_suite_pkcs5.function 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/pkcs5.h"
  3. /* END_HEADER */
  4. /* BEGIN_DEPENDENCIES
  5. * depends_on:MBEDTLS_PKCS5_C
  6. * END_DEPENDENCIES
  7. */
  8. /* BEGIN_CASE */
  9. void pbkdf2_hmac( int hash, data_t * pw_str, data_t * salt_str,
  10. int it_cnt, int key_len, data_t * result_key_string )
  11. {
  12. mbedtls_md_context_t ctx;
  13. const mbedtls_md_info_t *info;
  14. unsigned char key[100];
  15. mbedtls_md_init( &ctx );
  16. info = mbedtls_md_info_from_type( hash );
  17. TEST_ASSERT( info != NULL );
  18. TEST_ASSERT( mbedtls_md_setup( &ctx, info, 1 ) == 0 );
  19. TEST_ASSERT( mbedtls_pkcs5_pbkdf2_hmac( &ctx, pw_str->x, pw_str->len, salt_str->x, salt_str->len,
  20. it_cnt, key_len, key ) == 0 );
  21. TEST_ASSERT( mbedtls_test_hexcmp( key, result_key_string->x,
  22. key_len, result_key_string->len ) == 0 );
  23. exit:
  24. mbedtls_md_free( &ctx );
  25. }
  26. /* END_CASE */
  27. /* BEGIN_CASE depends_on:MBEDTLS_ASN1_PARSE_C */
  28. void mbedtls_pkcs5_pbes2( int params_tag, data_t *params_hex, data_t *pw,
  29. data_t *data, int ref_ret, data_t *ref_out )
  30. {
  31. int my_ret;
  32. mbedtls_asn1_buf params;
  33. unsigned char *my_out = NULL;
  34. params.tag = params_tag;
  35. params.p = params_hex->x;
  36. params.len = params_hex->len;
  37. my_out = mbedtls_test_zero_alloc( ref_out->len );
  38. my_ret = mbedtls_pkcs5_pbes2( &params, MBEDTLS_PKCS5_DECRYPT,
  39. pw->x, pw->len, data->x, data->len, my_out );
  40. TEST_ASSERT( my_ret == ref_ret );
  41. if( ref_ret == 0 )
  42. TEST_ASSERT( memcmp( my_out, ref_out->x, ref_out->len ) == 0 );
  43. exit:
  44. mbedtls_free( my_out );
  45. }
  46. /* END_CASE */
  47. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  48. void pkcs5_selftest( )
  49. {
  50. TEST_ASSERT( mbedtls_pkcs5_self_test( 1 ) == 0 );
  51. }
  52. /* END_CASE */