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