Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/pkcs5.h" |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 3 | /* END_HEADER */ |
Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 4 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 5 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6 | * depends_on:MBEDTLS_PKCS5_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 7 | * END_DEPENDENCIES |
| 8 | */ |
Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 9 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 10 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 11 | void pbkdf2_hmac(int hash, data_t *pw_str, data_t *salt_str, |
| 12 | int it_cnt, int key_len, data_t *result_key_string) |
Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 13 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 14 | mbedtls_md_context_t ctx; |
| 15 | const mbedtls_md_info_t *info; |
Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 16 | |
Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 17 | unsigned char key[100]; |
| 18 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 19 | mbedtls_md_init(&ctx); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 20 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 21 | info = mbedtls_md_info_from_type(hash); |
| 22 | TEST_ASSERT(info != NULL); |
| 23 | TEST_ASSERT(mbedtls_md_setup(&ctx, info, 1) == 0); |
| 24 | TEST_ASSERT(mbedtls_pkcs5_pbkdf2_hmac(&ctx, pw_str->x, pw_str->len, salt_str->x, salt_str->len, |
| 25 | it_cnt, key_len, key) == 0); |
Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 26 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 27 | TEST_ASSERT(mbedtls_test_hexcmp(key, result_key_string->x, |
| 28 | key_len, result_key_string->len) == 0); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 29 | |
| 30 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 31 | mbedtls_md_free(&ctx); |
Paul Bakker | b0c19a4 | 2013-06-24 19:26:38 +0200 | [diff] [blame] | 32 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 33 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 66aca93 | 2014-06-12 13:14:55 +0200 | [diff] [blame] | 34 | |
Andres Amaya Garcia | 576d474 | 2018-03-27 20:53:56 +0100 | [diff] [blame] | 35 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_PARSE_C */ |
Waleed Elmelegy | 412629c | 2023-07-19 14:01:35 +0100 | [diff] [blame^] | 36 | void pbes2_encrypt(int params_tag, data_t *params_hex, data_t *pw, |
| 37 | data_t *data, int ref_ret, data_t *ref_out) |
| 38 | { |
| 39 | int my_ret; |
| 40 | mbedtls_asn1_buf params; |
| 41 | unsigned char *my_out = NULL; |
| 42 | |
| 43 | MD_PSA_INIT(); |
| 44 | |
| 45 | params.tag = params_tag; |
| 46 | params.p = params_hex->x; |
| 47 | params.len = params_hex->len; |
| 48 | |
| 49 | ASSERT_ALLOC(my_out, ref_out->len); |
| 50 | |
| 51 | my_ret = mbedtls_pkcs5_pbes2(¶ms, MBEDTLS_PKCS5_ENCRYPT, |
| 52 | pw->x, pw->len, data->x, data->len, my_out); |
| 53 | TEST_EQUAL(my_ret, ref_ret); |
| 54 | if (ref_ret == 0) { |
| 55 | ASSERT_COMPARE(my_out, ref_out->len, |
| 56 | ref_out->x, ref_out->len); |
| 57 | } |
| 58 | |
| 59 | exit: |
| 60 | mbedtls_free(my_out); |
| 61 | MD_PSA_DONE(); |
| 62 | } |
| 63 | /* END_CASE */ |
| 64 | |
| 65 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_PARSE_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 66 | void mbedtls_pkcs5_pbes2(int params_tag, data_t *params_hex, data_t *pw, |
| 67 | data_t *data, int ref_ret, data_t *ref_out) |
Manuel Pégourié-Gonnard | 66aca93 | 2014-06-12 13:14:55 +0200 | [diff] [blame] | 68 | { |
| 69 | int my_ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | mbedtls_asn1_buf params; |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 71 | unsigned char *my_out = NULL; |
Manuel Pégourié-Gonnard | 66aca93 | 2014-06-12 13:14:55 +0200 | [diff] [blame] | 72 | |
| 73 | params.tag = params_tag; |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 74 | params.p = params_hex->x; |
| 75 | params.len = params_hex->len; |
Manuel Pégourié-Gonnard | 66aca93 | 2014-06-12 13:14:55 +0200 | [diff] [blame] | 76 | |
Waleed Elmelegy | 412629c | 2023-07-19 14:01:35 +0100 | [diff] [blame^] | 77 | ASSERT_ALLOC(my_out, ref_out->len); |
Manuel Pégourié-Gonnard | 66aca93 | 2014-06-12 13:14:55 +0200 | [diff] [blame] | 78 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 79 | my_ret = mbedtls_pkcs5_pbes2(¶ms, MBEDTLS_PKCS5_DECRYPT, |
| 80 | pw->x, pw->len, data->x, data->len, my_out); |
Waleed Elmelegy | 412629c | 2023-07-19 14:01:35 +0100 | [diff] [blame^] | 81 | TEST_EQUAL(my_ret, ref_ret); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 82 | if (ref_ret == 0) { |
Waleed Elmelegy | 412629c | 2023-07-19 14:01:35 +0100 | [diff] [blame^] | 83 | ASSERT_COMPARE(my_out, ref_out->len, |
| 84 | ref_out->x, ref_out->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 85 | } |
Manuel Pégourié-Gonnard | 66aca93 | 2014-06-12 13:14:55 +0200 | [diff] [blame] | 86 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 87 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 88 | mbedtls_free(my_out); |
Manuel Pégourié-Gonnard | 66aca93 | 2014-06-12 13:14:55 +0200 | [diff] [blame] | 89 | } |
| 90 | /* END_CASE */ |
Paul Bakker | 81c6091 | 2016-07-19 14:54:57 +0100 | [diff] [blame] | 91 | |
| 92 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 93 | void pkcs5_selftest() |
Paul Bakker | 81c6091 | 2016-07-19 14:54:57 +0100 | [diff] [blame] | 94 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 95 | TEST_ASSERT(mbedtls_pkcs5_self_test(1) == 0); |
Paul Bakker | 81c6091 | 2016-07-19 14:54:57 +0100 | [diff] [blame] | 96 | } |
| 97 | /* END_CASE */ |