blob: 841bd1d6e32c1c4655faffac7d272c6cb21d8fa5 [file] [log] [blame]
Paul Elliottd6635432021-11-18 22:35:48 +00001/* BEGIN_HEADER */
2#include "mbedtls/pkcs12.h"
Paul Elliott6e7deb12021-12-03 18:55:31 +00003#include "common.h"
Paul Elliottd6635432021-11-18 22:35:48 +00004
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +02005#include "mbedtls/legacy_or_psa.h"
Andrzej Kurek7bd12c52022-08-24 10:47:10 -04006
Paul Elliottd6635432021-11-18 22:35:48 +00007typedef enum
8{
9 USE_NULL_INPUT = 0,
10 USE_GIVEN_INPUT = 1,
Paul Elliottd6635432021-11-18 22:35:48 +000011} input_usage_method_t;
12
13/* END_HEADER */
14
15/* BEGIN_DEPENDENCIES
Paul Elliott3584ae42021-11-30 16:21:27 +000016 * depends_on:MBEDTLS_PKCS12_C
Paul Elliottd6635432021-11-18 22:35:48 +000017 * END_DEPENDENCIES
18 */
19
20/* BEGIN_CASE */
Paul Elliottdf006952021-11-30 16:31:10 +000021void pkcs12_derive_key( int md_type, int key_size_arg,
22 data_t *password_arg, int password_usage,
23 data_t *salt_arg, int salt_usage,
Paul Elliott6e7deb12021-12-03 18:55:31 +000024 int iterations,
25 data_t* expected_output, int expected_status )
Paul Elliottd6635432021-11-18 22:35:48 +000026
27{
28 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
29 unsigned char *output_data = NULL;
30
31 unsigned char *password = NULL;
32 size_t password_len = 0;
33 unsigned char *salt = NULL;
34 size_t salt_len = 0;
35 size_t key_size = key_size_arg;
36
37 if( password_usage == USE_GIVEN_INPUT )
Paul Elliottd6635432021-11-18 22:35:48 +000038 password = password_arg->x;
Paul Elliott4768a302021-11-30 16:39:51 +000039
40 password_len = password_arg->len;
Paul Elliottd6635432021-11-18 22:35:48 +000041
42 if( salt_usage == USE_GIVEN_INPUT )
Paul Elliottd6635432021-11-18 22:35:48 +000043 salt = salt_arg->x;
Paul Elliott4768a302021-11-30 16:39:51 +000044
45 salt_len = salt_arg->len;
Paul Elliottd6635432021-11-18 22:35:48 +000046
47 ASSERT_ALLOC( output_data, key_size );
48
49 ret = mbedtls_pkcs12_derivation( output_data,
50 key_size,
51 password,
52 password_len,
53 salt,
54 salt_len,
55 md_type,
56 MBEDTLS_PKCS12_DERIVE_KEY,
57 iterations );
58
59 TEST_EQUAL( ret, expected_status );
60
Paul Elliott6e7deb12021-12-03 18:55:31 +000061 if( expected_status == 0 )
62 {
63 ASSERT_COMPARE( expected_output->x, expected_output->len,
64 output_data, key_size );
65 }
66
Paul Elliottd6635432021-11-18 22:35:48 +000067exit:
68 mbedtls_free( output_data );
69
70}
71/* END_CASE */