Paul Elliott | d663543 | 2021-11-18 22:35:48 +0000 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
| 2 | #include "mbedtls/pkcs12.h" |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 3 | #include "mbedtls/oid.h" |
Paul Elliott | 6e7deb1 | 2021-12-03 18:55:31 +0000 | [diff] [blame] | 4 | #include "common.h" |
Paul Elliott | d663543 | 2021-11-18 22:35:48 +0000 | [diff] [blame] | 5 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6 | typedef enum { |
| 7 | USE_NULL_INPUT = 0, |
| 8 | USE_GIVEN_INPUT = 1, |
Paul Elliott | d663543 | 2021-11-18 22:35:48 +0000 | [diff] [blame] | 9 | } input_usage_method_t; |
| 10 | |
| 11 | /* END_HEADER */ |
| 12 | |
| 13 | /* BEGIN_DEPENDENCIES |
Paul Elliott | 3584ae4 | 2021-11-30 16:21:27 +0000 | [diff] [blame] | 14 | * depends_on:MBEDTLS_PKCS12_C |
Paul Elliott | d663543 | 2021-11-18 22:35:48 +0000 | [diff] [blame] | 15 | * END_DEPENDENCIES |
| 16 | */ |
| 17 | |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 18 | /* BEGIN_CASE MBEDTLS_ASN1_PARSE_C*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 19 | void pkcs12_derive_key(int md_type, int key_size_arg, |
| 20 | data_t *password_arg, int password_usage, |
| 21 | data_t *salt_arg, int salt_usage, |
| 22 | int iterations, |
| 23 | data_t *expected_output, int expected_status) |
Paul Elliott | d663543 | 2021-11-18 22:35:48 +0000 | [diff] [blame] | 24 | |
| 25 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 26 | unsigned char *output_data = NULL; |
Paul Elliott | d663543 | 2021-11-18 22:35:48 +0000 | [diff] [blame] | 27 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 28 | unsigned char *password = NULL; |
| 29 | size_t password_len = 0; |
| 30 | unsigned char *salt = NULL; |
| 31 | size_t salt_len = 0; |
| 32 | size_t key_size = key_size_arg; |
Paul Elliott | d663543 | 2021-11-18 22:35:48 +0000 | [diff] [blame] | 33 | |
Manuel Pégourié-Gonnard | be97afe | 2023-03-16 10:00:54 +0100 | [diff] [blame] | 34 | MD_PSA_INIT(); |
| 35 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 36 | if (password_usage == USE_GIVEN_INPUT) { |
| 37 | password = password_arg->x; |
| 38 | } |
Paul Elliott | 4768a30 | 2021-11-30 16:39:51 +0000 | [diff] [blame] | 39 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 40 | password_len = password_arg->len; |
Paul Elliott | d663543 | 2021-11-18 22:35:48 +0000 | [diff] [blame] | 41 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 42 | if (salt_usage == USE_GIVEN_INPUT) { |
| 43 | salt = salt_arg->x; |
| 44 | } |
Paul Elliott | 4768a30 | 2021-11-30 16:39:51 +0000 | [diff] [blame] | 45 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 46 | salt_len = salt_arg->len; |
Paul Elliott | d663543 | 2021-11-18 22:35:48 +0000 | [diff] [blame] | 47 | |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 48 | ASSERT_ALLOC(output_data, key_size); |
Paul Elliott | d663543 | 2021-11-18 22:35:48 +0000 | [diff] [blame] | 49 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 50 | int ret = mbedtls_pkcs12_derivation(output_data, |
Gilles Peskine | a844b4b | 2022-09-15 21:05:04 +0200 | [diff] [blame] | 51 | key_size, |
| 52 | password, |
| 53 | password_len, |
| 54 | salt, |
| 55 | salt_len, |
| 56 | md_type, |
| 57 | MBEDTLS_PKCS12_DERIVE_KEY, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 58 | iterations); |
Paul Elliott | d663543 | 2021-11-18 22:35:48 +0000 | [diff] [blame] | 59 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 60 | TEST_EQUAL(ret, expected_status); |
Paul Elliott | d663543 | 2021-11-18 22:35:48 +0000 | [diff] [blame] | 61 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 62 | if (expected_status == 0) { |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 63 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 64 | output_data, key_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 65 | } |
Paul Elliott | 6e7deb1 | 2021-12-03 18:55:31 +0000 | [diff] [blame] | 66 | |
Paul Elliott | d663543 | 2021-11-18 22:35:48 +0000 | [diff] [blame] | 67 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 68 | mbedtls_free(output_data); |
Manuel Pégourié-Gonnard | be97afe | 2023-03-16 10:00:54 +0100 | [diff] [blame] | 69 | MD_PSA_DONE(); |
Paul Elliott | d663543 | 2021-11-18 22:35:48 +0000 | [diff] [blame] | 70 | } |
| 71 | /* END_CASE */ |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 72 | |
| 73 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_PARSE_C */ |
Waleed Elmelegy | 15de809 | 2023-09-05 15:51:48 +0100 | [diff] [blame^] | 74 | void pkcs12_pbe_encrypt(int cipher, int md, data_t *params_hex, data_t *pw, |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 75 | data_t *data, int ref_ret, data_t *ref_out) |
| 76 | { |
| 77 | int my_ret; |
Waleed Elmelegy | 15de809 | 2023-09-05 15:51:48 +0100 | [diff] [blame^] | 78 | mbedtls_asn1_buf pbe_params; |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 79 | unsigned char *my_out = NULL; |
Waleed Elmelegy | 15de809 | 2023-09-05 15:51:48 +0100 | [diff] [blame^] | 80 | mbedtls_cipher_type_t cipher_alg = (mbedtls_cipher_type_t) cipher; |
| 81 | mbedtls_md_type_t md_alg = (mbedtls_md_type_t) md; |
| 82 | size_t block_size; |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 83 | |
| 84 | MD_PSA_INIT(); |
| 85 | |
Waleed Elmelegy | 15de809 | 2023-09-05 15:51:48 +0100 | [diff] [blame^] | 86 | block_size = mbedtls_cipher_info_get_block_size(mbedtls_cipher_info_from_type(cipher_alg)); |
| 87 | ASSERT_ALLOC(my_out, ((data->len/block_size) + 1) * block_size); |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 88 | |
Waleed Elmelegy | 15de809 | 2023-09-05 15:51:48 +0100 | [diff] [blame^] | 89 | pbe_params.tag = params_hex->x[0]; |
| 90 | pbe_params.len = params_hex->x[1]; |
| 91 | pbe_params.p = params_hex->x + 2; |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 92 | |
| 93 | my_ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_ENCRYPT, cipher_alg, |
| 94 | md_alg, pw->x, pw->len, data->x, data->len, my_out); |
| 95 | TEST_EQUAL(my_ret, ref_ret); |
| 96 | if (ref_ret == 0) { |
| 97 | ASSERT_COMPARE(my_out, ref_out->len, |
| 98 | ref_out->x, ref_out->len); |
| 99 | } |
| 100 | |
| 101 | exit: |
| 102 | mbedtls_free(my_out); |
| 103 | MD_PSA_DONE(); |
| 104 | } |
| 105 | /* END_CASE */ |
| 106 | |
| 107 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_PARSE_C */ |
Waleed Elmelegy | 15de809 | 2023-09-05 15:51:48 +0100 | [diff] [blame^] | 108 | void pkcs12_pbe_decrypt(int cipher, int md, data_t *params_hex, data_t *pw, |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 109 | data_t *data, int ref_ret, data_t *ref_out) |
| 110 | { |
| 111 | int my_ret; |
Waleed Elmelegy | 15de809 | 2023-09-05 15:51:48 +0100 | [diff] [blame^] | 112 | mbedtls_asn1_buf pbe_params; |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 113 | unsigned char *my_out = NULL; |
Waleed Elmelegy | 15de809 | 2023-09-05 15:51:48 +0100 | [diff] [blame^] | 114 | mbedtls_cipher_type_t cipher_alg = (mbedtls_cipher_type_t) cipher; |
| 115 | mbedtls_md_type_t md_alg = (mbedtls_md_type_t) md; |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 116 | |
| 117 | MD_PSA_INIT(); |
| 118 | |
Waleed Elmelegy | 15de809 | 2023-09-05 15:51:48 +0100 | [diff] [blame^] | 119 | ASSERT_ALLOC(my_out, data->len); |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 120 | |
Waleed Elmelegy | 15de809 | 2023-09-05 15:51:48 +0100 | [diff] [blame^] | 121 | pbe_params.tag = params_hex->x[0]; |
| 122 | pbe_params.len = params_hex->x[1]; |
| 123 | pbe_params.p = params_hex->x + 2; |
Waleed Elmelegy | 255db80 | 2023-09-04 15:11:22 +0100 | [diff] [blame] | 124 | |
| 125 | my_ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT, cipher_alg, |
| 126 | md_alg, pw->x, pw->len, data->x, data->len, my_out); |
| 127 | TEST_EQUAL(my_ret, ref_ret); |
| 128 | if (ref_ret == 0) { |
| 129 | ASSERT_COMPARE(my_out, ref_out->len, |
| 130 | ref_out->x, ref_out->len); |
| 131 | } |
| 132 | |
| 133 | exit: |
| 134 | mbedtls_free(my_out); |
| 135 | MD_PSA_DONE(); |
| 136 | } |
| 137 | /* END_CASE */ |