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/md.h" |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 3 | /* END_HEADER */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [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_MD_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 7 | * END_DEPENDENCIES |
| 8 | */ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [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 mbedtls_md_process() |
Manuel Pégourié-Gonnard | f301383 | 2014-03-29 15:54:50 +0100 | [diff] [blame] | 12 | { |
| 13 | const int *md_type_ptr; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 14 | const mbedtls_md_info_t *info; |
| 15 | mbedtls_md_context_t ctx; |
Manuel Pégourié-Gonnard | edb242f | 2014-04-02 17:52:04 +0200 | [diff] [blame] | 16 | unsigned char buf[150]; |
Manuel Pégourié-Gonnard | f301383 | 2014-03-29 15:54:50 +0100 | [diff] [blame] | 17 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 18 | mbedtls_md_init(&ctx); |
| 19 | memset(buf, 0, sizeof(buf)); |
Manuel Pégourié-Gonnard | edb242f | 2014-04-02 17:52:04 +0200 | [diff] [blame] | 20 | |
| 21 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | * Very minimal testing of mbedtls_md_process, just make sure the various |
Manuel Pégourié-Gonnard | edb242f | 2014-04-02 17:52:04 +0200 | [diff] [blame] | 23 | * xxx_process_wrap() function pointers are valid. (Testing that they |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 24 | * indeed do the right thing would require messing with the internal |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | * state of the underlying mbedtls_md/sha context.) |
Manuel Pégourié-Gonnard | edb242f | 2014-04-02 17:52:04 +0200 | [diff] [blame] | 26 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | * Also tests that mbedtls_md_list() only returns valid MDs. |
Manuel Pégourié-Gonnard | edb242f | 2014-04-02 17:52:04 +0200 | [diff] [blame] | 28 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 29 | for (md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++) { |
| 30 | info = mbedtls_md_info_from_type(*md_type_ptr); |
| 31 | TEST_ASSERT(info != NULL); |
| 32 | TEST_ASSERT(mbedtls_md_setup(&ctx, info, 0) == 0); |
| 33 | TEST_ASSERT(mbedtls_md_starts(&ctx) == 0); |
| 34 | TEST_ASSERT(mbedtls_md_process(&ctx, buf) == 0); |
| 35 | mbedtls_md_free(&ctx); |
Manuel Pégourié-Gonnard | edb242f | 2014-04-02 17:52:04 +0200 | [diff] [blame] | 36 | } |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 37 | |
| 38 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 39 | mbedtls_md_free(&ctx); |
Manuel Pégourié-Gonnard | f301383 | 2014-03-29 15:54:50 +0100 | [diff] [blame] | 40 | } |
| 41 | /* END_CASE */ |
| 42 | |
| 43 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 44 | void md_null_args() |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 45 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | mbedtls_md_context_t ctx; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 47 | const mbedtls_md_info_t *info = mbedtls_md_info_from_type(*(mbedtls_md_list())); |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 48 | unsigned char buf[1] = { 0 }; |
| 49 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 50 | mbedtls_md_init(&ctx); |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 51 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 52 | TEST_ASSERT(mbedtls_md_get_size(NULL) == 0); |
| 53 | TEST_ASSERT(mbedtls_md_get_type(NULL) == MBEDTLS_MD_NONE); |
| 54 | TEST_ASSERT(mbedtls_md_get_name(NULL) == NULL); |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 55 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 56 | TEST_ASSERT(mbedtls_md_info_from_string(NULL) == NULL); |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 57 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 58 | TEST_ASSERT(mbedtls_md_setup(&ctx, NULL, 0) == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
| 59 | TEST_ASSERT(mbedtls_md_setup(NULL, info, 0) == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 60 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 61 | TEST_ASSERT(mbedtls_md_starts(NULL) == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
| 62 | TEST_ASSERT(mbedtls_md_starts(&ctx) == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 63 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 64 | TEST_ASSERT(mbedtls_md_update(NULL, buf, 1) == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
| 65 | TEST_ASSERT(mbedtls_md_update(&ctx, buf, 1) == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 66 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 67 | TEST_ASSERT(mbedtls_md_finish(NULL, buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
| 68 | TEST_ASSERT(mbedtls_md_finish(&ctx, buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 69 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 70 | TEST_ASSERT(mbedtls_md(NULL, buf, 1, buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 71 | |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 72 | #if defined(MBEDTLS_FS_IO) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 73 | TEST_ASSERT(mbedtls_md_file(NULL, "", buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 74 | #endif |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 75 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 76 | TEST_ASSERT(mbedtls_md_hmac_starts(NULL, buf, 1) |
| 77 | == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
| 78 | TEST_ASSERT(mbedtls_md_hmac_starts(&ctx, buf, 1) |
| 79 | == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 80 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 81 | TEST_ASSERT(mbedtls_md_hmac_update(NULL, buf, 1) |
| 82 | == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
| 83 | TEST_ASSERT(mbedtls_md_hmac_update(&ctx, buf, 1) |
| 84 | == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 85 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 86 | TEST_ASSERT(mbedtls_md_hmac_finish(NULL, buf) |
| 87 | == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
| 88 | TEST_ASSERT(mbedtls_md_hmac_finish(&ctx, buf) |
| 89 | == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 90 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 91 | TEST_ASSERT(mbedtls_md_hmac_reset(NULL) == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
| 92 | TEST_ASSERT(mbedtls_md_hmac_reset(&ctx) == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 93 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 94 | TEST_ASSERT(mbedtls_md_hmac(NULL, buf, 1, buf, 1, buf) |
| 95 | == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 96 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 97 | TEST_ASSERT(mbedtls_md_process(NULL, buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
| 98 | TEST_ASSERT(mbedtls_md_process(&ctx, buf) == MBEDTLS_ERR_MD_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | 19d644b | 2015-03-26 12:42:35 +0100 | [diff] [blame] | 99 | |
| 100 | /* Ok, this is not NULL arg but NULL return... */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 101 | TEST_ASSERT(mbedtls_md_info_from_type(MBEDTLS_MD_NONE) == NULL); |
| 102 | TEST_ASSERT(mbedtls_md_info_from_string("no such md") == NULL); |
Manuel Pégourié-Gonnard | b25f816 | 2014-06-13 16:34:30 +0200 | [diff] [blame] | 103 | } |
| 104 | /* END_CASE */ |
| 105 | |
| 106 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 107 | void md_info(int md_type, char *md_name, int md_size) |
Manuel Pégourié-Gonnard | f301383 | 2014-03-29 15:54:50 +0100 | [diff] [blame] | 108 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | const mbedtls_md_info_t *md_info; |
Manuel Pégourié-Gonnard | f301383 | 2014-03-29 15:54:50 +0100 | [diff] [blame] | 110 | const int *md_type_ptr; |
| 111 | int found; |
| 112 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 113 | md_info = mbedtls_md_info_from_type(md_type); |
| 114 | TEST_ASSERT(md_info != NULL); |
| 115 | TEST_ASSERT(md_info == mbedtls_md_info_from_string(md_name)); |
Manuel Pégourié-Gonnard | f301383 | 2014-03-29 15:54:50 +0100 | [diff] [blame] | 116 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 117 | TEST_ASSERT(mbedtls_md_get_type(md_info) == (mbedtls_md_type_t) md_type); |
| 118 | TEST_ASSERT(mbedtls_md_get_size(md_info) == (unsigned char) md_size); |
| 119 | TEST_ASSERT(strcmp(mbedtls_md_get_name(md_info), md_name) == 0); |
Manuel Pégourié-Gonnard | f301383 | 2014-03-29 15:54:50 +0100 | [diff] [blame] | 120 | |
| 121 | found = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 122 | for (md_type_ptr = mbedtls_md_list(); *md_type_ptr != 0; md_type_ptr++) { |
| 123 | if (*md_type_ptr == md_type) { |
Manuel Pégourié-Gonnard | f301383 | 2014-03-29 15:54:50 +0100 | [diff] [blame] | 124 | found = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | TEST_ASSERT(found == 1); |
Manuel Pégourié-Gonnard | f301383 | 2014-03-29 15:54:50 +0100 | [diff] [blame] | 128 | } |
| 129 | /* END_CASE */ |
| 130 | |
| 131 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | a876bd2 | 2023-02-03 12:13:10 +0100 | [diff] [blame] | 132 | void md_text(int md_type, char *text_src_string, data_t *hash) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 133 | { |
Manuel Pégourié-Gonnard | 96108b6 | 2023-02-03 12:32:41 +0100 | [diff] [blame^] | 134 | unsigned char *src = (unsigned char *) text_src_string; |
| 135 | size_t src_len = strlen(text_src_string); |
Manuel Pégourié-Gonnard | 2a5e213 | 2023-02-03 12:25:53 +0100 | [diff] [blame] | 136 | unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0}; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | const mbedtls_md_info_t *md_info = NULL; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 138 | |
Manuel Pégourié-Gonnard | a876bd2 | 2023-02-03 12:13:10 +0100 | [diff] [blame] | 139 | md_info = mbedtls_md_info_from_type(md_type); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 140 | TEST_ASSERT(md_info != NULL); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 141 | |
Manuel Pégourié-Gonnard | 96108b6 | 2023-02-03 12:32:41 +0100 | [diff] [blame^] | 142 | TEST_ASSERT(0 == mbedtls_md(md_info, src, src_len, output)); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 143 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 144 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 145 | mbedtls_md_get_size(md_info), |
| 146 | hash->len) == 0); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 147 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 148 | /* END_CASE */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 149 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 150 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | a876bd2 | 2023-02-03 12:13:10 +0100 | [diff] [blame] | 151 | void md_hex(int md_type, data_t *src_str, data_t *hash) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 152 | { |
Manuel Pégourié-Gonnard | 2a5e213 | 2023-02-03 12:25:53 +0100 | [diff] [blame] | 153 | unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0}; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | const mbedtls_md_info_t *md_info = NULL; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 155 | |
Manuel Pégourié-Gonnard | a876bd2 | 2023-02-03 12:13:10 +0100 | [diff] [blame] | 156 | md_info = mbedtls_md_info_from_type(md_type); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 157 | TEST_ASSERT(md_info != NULL); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 158 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 159 | TEST_ASSERT(0 == mbedtls_md(md_info, src_str->x, src_str->len, output)); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 160 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 161 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 162 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 163 | mbedtls_md_get_size(md_info), |
| 164 | hash->len) == 0); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 165 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 166 | /* END_CASE */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 167 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 168 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | a876bd2 | 2023-02-03 12:13:10 +0100 | [diff] [blame] | 169 | void md_text_multi(int md_type, char *text_src_string, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 170 | data_t *hash) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 171 | { |
Manuel Pégourié-Gonnard | 96108b6 | 2023-02-03 12:32:41 +0100 | [diff] [blame^] | 172 | unsigned char *src = (unsigned char *) text_src_string; |
| 173 | size_t src_len = strlen(text_src_string); |
Manuel Pégourié-Gonnard | 2a5e213 | 2023-02-03 12:25:53 +0100 | [diff] [blame] | 174 | unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0}; |
Manuel Pégourié-Gonnard | 96108b6 | 2023-02-03 12:32:41 +0100 | [diff] [blame^] | 175 | size_t halfway; |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 176 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | const mbedtls_md_info_t *md_info = NULL; |
Paul Bakker | 97c53c2 | 2016-07-13 17:20:22 +0100 | [diff] [blame] | 178 | mbedtls_md_context_t ctx, ctx_copy; |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 179 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 180 | mbedtls_md_init(&ctx); |
| 181 | mbedtls_md_init(&ctx_copy); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 182 | |
Manuel Pégourié-Gonnard | 96108b6 | 2023-02-03 12:32:41 +0100 | [diff] [blame^] | 183 | halfway = src_len / 2; |
Paul Bakker | e35afa2 | 2016-07-13 17:09:14 +0100 | [diff] [blame] | 184 | |
Manuel Pégourié-Gonnard | a876bd2 | 2023-02-03 12:13:10 +0100 | [diff] [blame] | 185 | md_info = mbedtls_md_info_from_type(md_type); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 186 | TEST_ASSERT(md_info != NULL); |
| 187 | TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 0)); |
| 188 | TEST_ASSERT(0 == mbedtls_md_setup(&ctx_copy, md_info, 0)); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 189 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 190 | TEST_ASSERT(0 == mbedtls_md_starts(&ctx)); |
| 191 | TEST_ASSERT(ctx.md_ctx != NULL); |
Manuel Pégourié-Gonnard | 96108b6 | 2023-02-03 12:32:41 +0100 | [diff] [blame^] | 192 | TEST_ASSERT(0 == mbedtls_md_update(&ctx, src, halfway)); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 193 | TEST_ASSERT(0 == mbedtls_md_clone(&ctx_copy, &ctx)); |
Paul Bakker | 97c53c2 | 2016-07-13 17:20:22 +0100 | [diff] [blame] | 194 | |
Manuel Pégourié-Gonnard | 96108b6 | 2023-02-03 12:32:41 +0100 | [diff] [blame^] | 195 | TEST_ASSERT(0 == mbedtls_md_update(&ctx, src + halfway, src_len - halfway)); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 196 | TEST_ASSERT(0 == mbedtls_md_finish(&ctx, output)); |
| 197 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 198 | mbedtls_md_get_size(md_info), |
| 199 | hash->len) == 0); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 200 | |
Paul Bakker | 97c53c2 | 2016-07-13 17:20:22 +0100 | [diff] [blame] | 201 | /* Test clone */ |
Manuel Pégourié-Gonnard | 2a5e213 | 2023-02-03 12:25:53 +0100 | [diff] [blame] | 202 | memset(output, 0x00, sizeof(output)); |
Paul Bakker | 97c53c2 | 2016-07-13 17:20:22 +0100 | [diff] [blame] | 203 | |
Manuel Pégourié-Gonnard | 96108b6 | 2023-02-03 12:32:41 +0100 | [diff] [blame^] | 204 | TEST_ASSERT(0 == mbedtls_md_update(&ctx_copy, src + halfway, src_len - halfway)); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 205 | TEST_ASSERT(0 == mbedtls_md_finish(&ctx_copy, output)); |
| 206 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 207 | mbedtls_md_get_size(md_info), |
| 208 | hash->len) == 0); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 209 | |
| 210 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 211 | mbedtls_md_free(&ctx); |
| 212 | mbedtls_md_free(&ctx_copy); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 213 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 214 | /* END_CASE */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 215 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 216 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | a876bd2 | 2023-02-03 12:13:10 +0100 | [diff] [blame] | 217 | void md_hex_multi(int md_type, data_t *src_str, data_t *hash) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 218 | { |
Manuel Pégourié-Gonnard | 2a5e213 | 2023-02-03 12:25:53 +0100 | [diff] [blame] | 219 | unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0}; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 220 | const mbedtls_md_info_t *md_info = NULL; |
Paul Bakker | 97c53c2 | 2016-07-13 17:20:22 +0100 | [diff] [blame] | 221 | mbedtls_md_context_t ctx, ctx_copy; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 222 | int halfway; |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 223 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 224 | mbedtls_md_init(&ctx); |
| 225 | mbedtls_md_init(&ctx_copy); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 226 | |
Manuel Pégourié-Gonnard | a876bd2 | 2023-02-03 12:13:10 +0100 | [diff] [blame] | 227 | md_info = mbedtls_md_info_from_type(md_type); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 228 | TEST_ASSERT(md_info != NULL); |
| 229 | TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 0)); |
| 230 | TEST_ASSERT(0 == mbedtls_md_setup(&ctx_copy, md_info, 0)); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 231 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 232 | halfway = src_str->len / 2; |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 233 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 234 | TEST_ASSERT(0 == mbedtls_md_starts(&ctx)); |
| 235 | TEST_ASSERT(ctx.md_ctx != NULL); |
| 236 | TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str->x, halfway)); |
| 237 | TEST_ASSERT(0 == mbedtls_md_clone(&ctx_copy, &ctx)); |
Paul Bakker | 97c53c2 | 2016-07-13 17:20:22 +0100 | [diff] [blame] | 238 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 239 | TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str->x + halfway, src_str->len - halfway)); |
| 240 | TEST_ASSERT(0 == mbedtls_md_finish(&ctx, output)); |
| 241 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 242 | mbedtls_md_get_size(md_info), |
| 243 | hash->len) == 0); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 244 | |
Paul Bakker | 97c53c2 | 2016-07-13 17:20:22 +0100 | [diff] [blame] | 245 | /* Test clone */ |
Manuel Pégourié-Gonnard | 2a5e213 | 2023-02-03 12:25:53 +0100 | [diff] [blame] | 246 | memset(output, 0x00, sizeof(output)); |
Paul Bakker | 97c53c2 | 2016-07-13 17:20:22 +0100 | [diff] [blame] | 247 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 248 | TEST_ASSERT(0 == mbedtls_md_update(&ctx_copy, src_str->x + halfway, src_str->len - halfway)); |
| 249 | TEST_ASSERT(0 == mbedtls_md_finish(&ctx_copy, output)); |
| 250 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 251 | mbedtls_md_get_size(md_info), |
| 252 | hash->len) == 0); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 253 | |
| 254 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 255 | mbedtls_md_free(&ctx); |
| 256 | mbedtls_md_free(&ctx_copy); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 257 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 258 | /* END_CASE */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 259 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 260 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | a876bd2 | 2023-02-03 12:13:10 +0100 | [diff] [blame] | 261 | void mbedtls_md_hmac(int md_type, int trunc_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 262 | data_t *key_str, data_t *src_str, |
| 263 | data_t *hash) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 264 | { |
Manuel Pégourié-Gonnard | 2a5e213 | 2023-02-03 12:25:53 +0100 | [diff] [blame] | 265 | unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0}; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 266 | const mbedtls_md_info_t *md_info = NULL; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 267 | |
Manuel Pégourié-Gonnard | a876bd2 | 2023-02-03 12:13:10 +0100 | [diff] [blame] | 268 | md_info = mbedtls_md_info_from_type(md_type); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 269 | TEST_ASSERT(md_info != NULL); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 270 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 271 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 272 | TEST_ASSERT(mbedtls_md_hmac(md_info, key_str->x, key_str->len, src_str->x, src_str->len, |
| 273 | output) == 0); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 274 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 275 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 276 | trunc_size, hash->len) == 0); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 277 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 278 | /* END_CASE */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 279 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 280 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | a876bd2 | 2023-02-03 12:13:10 +0100 | [diff] [blame] | 281 | void md_hmac_multi(int md_type, int trunc_size, data_t *key_str, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 282 | data_t *src_str, data_t *hash) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 283 | { |
Manuel Pégourié-Gonnard | 2a5e213 | 2023-02-03 12:25:53 +0100 | [diff] [blame] | 284 | unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0}; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 285 | const mbedtls_md_info_t *md_info = NULL; |
| 286 | mbedtls_md_context_t ctx; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 287 | int halfway; |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 288 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 289 | mbedtls_md_init(&ctx); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 290 | |
Manuel Pégourié-Gonnard | a876bd2 | 2023-02-03 12:13:10 +0100 | [diff] [blame] | 291 | md_info = mbedtls_md_info_from_type(md_type); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 292 | TEST_ASSERT(md_info != NULL); |
| 293 | TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 1)); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 294 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 295 | halfway = src_str->len / 2; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 296 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 297 | TEST_ASSERT(0 == mbedtls_md_hmac_starts(&ctx, key_str->x, key_str->len)); |
| 298 | TEST_ASSERT(ctx.md_ctx != NULL); |
| 299 | TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x, halfway)); |
| 300 | TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway)); |
| 301 | TEST_ASSERT(0 == mbedtls_md_hmac_finish(&ctx, output)); |
Manuel Pégourié-Gonnard | 59ba4e9 | 2014-03-29 14:43:44 +0100 | [diff] [blame] | 302 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 303 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 304 | trunc_size, hash->len) == 0); |
Manuel Pégourié-Gonnard | 59ba4e9 | 2014-03-29 14:43:44 +0100 | [diff] [blame] | 305 | |
| 306 | /* Test again, for reset() */ |
Manuel Pégourié-Gonnard | 2a5e213 | 2023-02-03 12:25:53 +0100 | [diff] [blame] | 307 | memset(output, 0x00, sizeof(output)); |
Manuel Pégourié-Gonnard | 59ba4e9 | 2014-03-29 14:43:44 +0100 | [diff] [blame] | 308 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 309 | TEST_ASSERT(0 == mbedtls_md_hmac_reset(&ctx)); |
| 310 | TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x, halfway)); |
| 311 | TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway)); |
| 312 | TEST_ASSERT(0 == mbedtls_md_hmac_finish(&ctx, output)); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 313 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 314 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 315 | trunc_size, hash->len) == 0); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 316 | |
| 317 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 318 | mbedtls_md_free(&ctx); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 319 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 320 | /* END_CASE */ |
Paul Bakker | 428b9ba | 2013-09-15 15:20:37 +0200 | [diff] [blame] | 321 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 322 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | a876bd2 | 2023-02-03 12:13:10 +0100 | [diff] [blame] | 323 | void mbedtls_md_file(int md_type, char *filename, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 324 | data_t *hash) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 325 | { |
Manuel Pégourié-Gonnard | 2a5e213 | 2023-02-03 12:25:53 +0100 | [diff] [blame] | 326 | unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0}; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 327 | const mbedtls_md_info_t *md_info = NULL; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 328 | |
Manuel Pégourié-Gonnard | a876bd2 | 2023-02-03 12:13:10 +0100 | [diff] [blame] | 329 | md_info = mbedtls_md_info_from_type(md_type); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 330 | TEST_ASSERT(md_info != NULL); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 331 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 332 | TEST_ASSERT(mbedtls_md_file(md_info, filename, output) == 0); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 333 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 334 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 335 | mbedtls_md_get_size(md_info), |
| 336 | hash->len) == 0); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 337 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 338 | /* END_CASE */ |