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