test_suite_pkcs12.function 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/pkcs12.h"
  3. #include "mbedtls/error.h"
  4. typedef enum
  5. {
  6. USE_NULL_INPUT = 0,
  7. USE_GIVEN_INPUT = 1,
  8. } input_usage_method_t;
  9. /* END_HEADER */
  10. /* BEGIN_DEPENDENCIES
  11. * depends_on:MBEDTLS_PKCS12_C
  12. * END_DEPENDENCIES
  13. */
  14. /* BEGIN_CASE */
  15. void pkcs12_derive_key( int md_type, int key_size_arg,
  16. data_t *password_arg, int password_usage,
  17. data_t *salt_arg, int salt_usage,
  18. int iterations,
  19. data_t* expected_output, int expected_status )
  20. {
  21. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  22. unsigned char *output_data = NULL;
  23. unsigned char *password = NULL;
  24. size_t password_len = 0;
  25. unsigned char *salt = NULL;
  26. size_t salt_len = 0;
  27. size_t key_size = key_size_arg;
  28. if( password_usage == USE_GIVEN_INPUT )
  29. password = password_arg->x;
  30. password_len = password_arg->len;
  31. if( salt_usage == USE_GIVEN_INPUT )
  32. salt = salt_arg->x;
  33. salt_len = salt_arg->len;
  34. ASSERT_ALLOC( output_data, key_size );
  35. ret = mbedtls_pkcs12_derivation( output_data,
  36. key_size,
  37. password,
  38. password_len,
  39. salt,
  40. salt_len,
  41. md_type,
  42. MBEDTLS_PKCS12_DERIVE_KEY,
  43. iterations );
  44. TEST_EQUAL( ret, expected_status );
  45. if( expected_status == 0 )
  46. {
  47. ASSERT_COMPARE( expected_output->x, expected_output->len,
  48. output_data, key_size );
  49. }
  50. exit:
  51. mbedtls_free( output_data );
  52. }
  53. /* END_CASE */