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 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 132 | void md_text(char *text_md_name, char *text_src_string, |
| 133 | data_t *hash) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 134 | { |
| 135 | char md_name[100]; |
| 136 | unsigned char src_str[1000]; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 137 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 138 | const mbedtls_md_info_t *md_info = NULL; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 139 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 140 | memset(md_name, 0x00, 100); |
| 141 | memset(src_str, 0x00, 1000); |
| 142 | memset(output, 0x00, 100); |
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 | strncpy((char *) src_str, text_src_string, sizeof(src_str) - 1); |
| 145 | strncpy((char *) md_name, text_md_name, sizeof(md_name) - 1); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 146 | md_info = mbedtls_md_info_from_string(md_name); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 147 | TEST_ASSERT(md_info != NULL); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 148 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 149 | TEST_ASSERT(0 == mbedtls_md(md_info, src_str, strlen((char *) src_str), output)); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 150 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 151 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 152 | mbedtls_md_get_size(md_info), |
| 153 | hash->len) == 0); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 154 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 155 | /* END_CASE */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 156 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 157 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 158 | void md_hex(char *text_md_name, data_t *src_str, data_t *hash) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 159 | { |
| 160 | char md_name[100]; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 161 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 162 | const mbedtls_md_info_t *md_info = NULL; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 163 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 164 | memset(md_name, 0x00, 100); |
| 165 | memset(output, 0x00, 100); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 166 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 167 | strncpy((char *) md_name, text_md_name, sizeof(md_name) - 1); |
| 168 | md_info = mbedtls_md_info_from_string(md_name); |
| 169 | TEST_ASSERT(md_info != NULL); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 170 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 171 | 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] | 172 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 173 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 174 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 175 | mbedtls_md_get_size(md_info), |
| 176 | hash->len) == 0); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 177 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 178 | /* END_CASE */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 179 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 180 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 181 | void md_text_multi(char *text_md_name, char *text_src_string, |
| 182 | data_t *hash) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 183 | { |
| 184 | char md_name[100]; |
| 185 | unsigned char src_str[1000]; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 186 | unsigned char output[100]; |
Paul Bakker | e35afa2 | 2016-07-13 17:09:14 +0100 | [diff] [blame] | 187 | int halfway, len; |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 189 | const mbedtls_md_info_t *md_info = NULL; |
Paul Bakker | 97c53c2 | 2016-07-13 17:20:22 +0100 | [diff] [blame] | 190 | mbedtls_md_context_t ctx, ctx_copy; |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 191 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 192 | mbedtls_md_init(&ctx); |
| 193 | mbedtls_md_init(&ctx_copy); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 194 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 195 | memset(md_name, 0x00, 100); |
| 196 | memset(src_str, 0x00, 1000); |
| 197 | memset(output, 0x00, 100); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 198 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 199 | strncpy((char *) src_str, text_src_string, sizeof(src_str) - 1); |
| 200 | strncpy((char *) md_name, text_md_name, sizeof(md_name) - 1); |
| 201 | len = strlen((char *) src_str); |
Paul Bakker | e35afa2 | 2016-07-13 17:09:14 +0100 | [diff] [blame] | 202 | halfway = len / 2; |
| 203 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | md_info = mbedtls_md_info_from_string(md_name); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 205 | TEST_ASSERT(md_info != NULL); |
| 206 | TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 0)); |
| 207 | TEST_ASSERT(0 == mbedtls_md_setup(&ctx_copy, md_info, 0)); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 208 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 209 | TEST_ASSERT(0 == mbedtls_md_starts(&ctx)); |
| 210 | TEST_ASSERT(ctx.md_ctx != NULL); |
| 211 | TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str, halfway)); |
| 212 | TEST_ASSERT(0 == mbedtls_md_clone(&ctx_copy, &ctx)); |
Paul Bakker | 97c53c2 | 2016-07-13 17:20:22 +0100 | [diff] [blame] | 213 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 214 | TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str + halfway, len - halfway)); |
| 215 | TEST_ASSERT(0 == mbedtls_md_finish(&ctx, output)); |
| 216 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 217 | mbedtls_md_get_size(md_info), |
| 218 | hash->len) == 0); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 219 | |
Paul Bakker | 97c53c2 | 2016-07-13 17:20:22 +0100 | [diff] [blame] | 220 | /* Test clone */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 221 | memset(output, 0x00, 100); |
Paul Bakker | 97c53c2 | 2016-07-13 17:20:22 +0100 | [diff] [blame] | 222 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 223 | TEST_ASSERT(0 == mbedtls_md_update(&ctx_copy, src_str + halfway, len - halfway)); |
| 224 | TEST_ASSERT(0 == mbedtls_md_finish(&ctx_copy, output)); |
| 225 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 226 | mbedtls_md_get_size(md_info), |
| 227 | hash->len) == 0); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 228 | |
| 229 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 230 | mbedtls_md_free(&ctx); |
| 231 | mbedtls_md_free(&ctx_copy); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 232 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 233 | /* END_CASE */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 234 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 235 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 236 | void md_hex_multi(char *text_md_name, data_t *src_str, data_t *hash) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 237 | { |
| 238 | char md_name[100]; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 239 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 240 | const mbedtls_md_info_t *md_info = NULL; |
Paul Bakker | 97c53c2 | 2016-07-13 17:20:22 +0100 | [diff] [blame] | 241 | mbedtls_md_context_t ctx, ctx_copy; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 242 | int halfway; |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 243 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 244 | mbedtls_md_init(&ctx); |
| 245 | mbedtls_md_init(&ctx_copy); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 246 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 247 | memset(md_name, 0x00, 100); |
| 248 | memset(output, 0x00, 100); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 249 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 250 | strncpy((char *) md_name, text_md_name, sizeof(md_name) - 1); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 251 | md_info = mbedtls_md_info_from_string(md_name); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 252 | TEST_ASSERT(md_info != NULL); |
| 253 | TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 0)); |
| 254 | TEST_ASSERT(0 == mbedtls_md_setup(&ctx_copy, md_info, 0)); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 255 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 256 | halfway = src_str->len / 2; |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 257 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 258 | TEST_ASSERT(0 == mbedtls_md_starts(&ctx)); |
| 259 | TEST_ASSERT(ctx.md_ctx != NULL); |
| 260 | TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str->x, halfway)); |
| 261 | TEST_ASSERT(0 == mbedtls_md_clone(&ctx_copy, &ctx)); |
Paul Bakker | 97c53c2 | 2016-07-13 17:20:22 +0100 | [diff] [blame] | 262 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 263 | TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str->x + halfway, src_str->len - halfway)); |
| 264 | TEST_ASSERT(0 == mbedtls_md_finish(&ctx, output)); |
| 265 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 266 | mbedtls_md_get_size(md_info), |
| 267 | hash->len) == 0); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 268 | |
Paul Bakker | 97c53c2 | 2016-07-13 17:20:22 +0100 | [diff] [blame] | 269 | /* Test clone */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 270 | memset(output, 0x00, 100); |
Paul Bakker | 97c53c2 | 2016-07-13 17:20:22 +0100 | [diff] [blame] | 271 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 272 | TEST_ASSERT(0 == mbedtls_md_update(&ctx_copy, src_str->x + halfway, src_str->len - halfway)); |
| 273 | TEST_ASSERT(0 == mbedtls_md_finish(&ctx_copy, output)); |
| 274 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 275 | mbedtls_md_get_size(md_info), |
| 276 | hash->len) == 0); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 277 | |
| 278 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 279 | mbedtls_md_free(&ctx); |
| 280 | mbedtls_md_free(&ctx_copy); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 281 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 282 | /* END_CASE */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 283 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 284 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 285 | void mbedtls_md_hmac(char *text_md_name, int trunc_size, |
| 286 | data_t *key_str, data_t *src_str, |
| 287 | data_t *hash) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 288 | { |
| 289 | char md_name[100]; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 290 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 291 | const mbedtls_md_info_t *md_info = NULL; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 292 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 293 | memset(md_name, 0x00, 100); |
| 294 | memset(output, 0x00, 100); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 295 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 296 | strncpy((char *) md_name, text_md_name, sizeof(md_name) - 1); |
| 297 | md_info = mbedtls_md_info_from_string(md_name); |
| 298 | TEST_ASSERT(md_info != NULL); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 299 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 300 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 301 | TEST_ASSERT(mbedtls_md_hmac(md_info, key_str->x, key_str->len, src_str->x, src_str->len, |
| 302 | output) == 0); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 303 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 304 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 305 | trunc_size, hash->len) == 0); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 306 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 307 | /* END_CASE */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 308 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 309 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 310 | void md_hmac_multi(char *text_md_name, int trunc_size, data_t *key_str, |
| 311 | data_t *src_str, data_t *hash) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 312 | { |
| 313 | char md_name[100]; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 314 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 315 | const mbedtls_md_info_t *md_info = NULL; |
| 316 | mbedtls_md_context_t ctx; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 317 | int halfway; |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 318 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 319 | mbedtls_md_init(&ctx); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 320 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 321 | memset(md_name, 0x00, 100); |
| 322 | memset(output, 0x00, 100); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 323 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 324 | strncpy((char *) md_name, text_md_name, sizeof(md_name) - 1); |
| 325 | md_info = mbedtls_md_info_from_string(md_name); |
| 326 | TEST_ASSERT(md_info != NULL); |
| 327 | TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 1)); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 328 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 329 | halfway = src_str->len / 2; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 330 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 331 | TEST_ASSERT(0 == mbedtls_md_hmac_starts(&ctx, key_str->x, key_str->len)); |
| 332 | TEST_ASSERT(ctx.md_ctx != NULL); |
| 333 | TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x, halfway)); |
| 334 | TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway)); |
| 335 | TEST_ASSERT(0 == mbedtls_md_hmac_finish(&ctx, output)); |
Manuel Pégourié-Gonnard | 59ba4e9 | 2014-03-29 14:43:44 +0100 | [diff] [blame] | 336 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 337 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 338 | trunc_size, hash->len) == 0); |
Manuel Pégourié-Gonnard | 59ba4e9 | 2014-03-29 14:43:44 +0100 | [diff] [blame] | 339 | |
| 340 | /* Test again, for reset() */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 341 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 59ba4e9 | 2014-03-29 14:43:44 +0100 | [diff] [blame] | 342 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 343 | TEST_ASSERT(0 == mbedtls_md_hmac_reset(&ctx)); |
| 344 | TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x, halfway)); |
| 345 | TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x + halfway, src_str->len - halfway)); |
| 346 | TEST_ASSERT(0 == mbedtls_md_hmac_finish(&ctx, output)); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 347 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 348 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 349 | trunc_size, hash->len) == 0); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 350 | |
| 351 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 352 | mbedtls_md_free(&ctx); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 353 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 354 | /* END_CASE */ |
Paul Bakker | 428b9ba | 2013-09-15 15:20:37 +0200 | [diff] [blame] | 355 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 356 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 357 | void mbedtls_md_file(char *text_md_name, char *filename, |
| 358 | data_t *hash) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 359 | { |
| 360 | char md_name[100]; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 361 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 362 | const mbedtls_md_info_t *md_info = NULL; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 363 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 364 | memset(md_name, 0x00, 100); |
| 365 | memset(output, 0x00, 100); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 366 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 367 | strncpy((char *) md_name, text_md_name, sizeof(md_name) - 1); |
| 368 | md_info = mbedtls_md_info_from_string(md_name); |
| 369 | TEST_ASSERT(md_info != NULL); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 370 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 371 | TEST_ASSERT(mbedtls_md_file(md_info, filename, output) == 0); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 372 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 373 | TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x, |
| 374 | mbedtls_md_get_size(md_info), |
| 375 | hash->len) == 0); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 376 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 377 | /* END_CASE */ |