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/aes.h" |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 3 | /* END_HEADER */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +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_AES_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 aes_encrypt_ecb(data_t *key_str, data_t *src_str, |
| 12 | data_t *dst, int setkey_result) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 13 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 14 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 15 | mbedtls_aes_context ctx; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 16 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 17 | memset(output, 0x00, 100); |
| 18 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 19 | mbedtls_aes_init(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 20 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 21 | TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == setkey_result); |
| 22 | if (setkey_result == 0) { |
| 23 | TEST_ASSERT(mbedtls_aes_crypt_ecb(&ctx, MBEDTLS_AES_ENCRYPT, src_str->x, output) == 0); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 24 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 25 | TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0); |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 26 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 27 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 28 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 29 | mbedtls_aes_free(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 30 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 31 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 32 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 33 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 34 | void aes_decrypt_ecb(data_t *key_str, data_t *src_str, |
| 35 | data_t *dst, int setkey_result) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 36 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 37 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 38 | mbedtls_aes_context ctx; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 39 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 40 | memset(output, 0x00, 100); |
| 41 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 42 | mbedtls_aes_init(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 43 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 44 | TEST_ASSERT(mbedtls_aes_setkey_dec(&ctx, key_str->x, key_str->len * 8) == setkey_result); |
| 45 | if (setkey_result == 0) { |
| 46 | TEST_ASSERT(mbedtls_aes_crypt_ecb(&ctx, MBEDTLS_AES_DECRYPT, src_str->x, output) == 0); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 47 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 48 | TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0); |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 49 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 50 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 51 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 52 | mbedtls_aes_free(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 53 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 54 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 55 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 57 | void aes_encrypt_cbc(data_t *key_str, data_t *iv_str, |
| 58 | data_t *src_str, data_t *dst, |
| 59 | int cbc_result) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 60 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 61 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | mbedtls_aes_context ctx; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 63 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 64 | memset(output, 0x00, 100); |
| 65 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 66 | mbedtls_aes_init(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 67 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 68 | TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0); |
| 69 | TEST_ASSERT(mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x, |
| 70 | src_str->x, output) == cbc_result); |
| 71 | if (cbc_result == 0) { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 72 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 73 | TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, |
| 74 | src_str->len, dst->len) == 0); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 75 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 76 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 77 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 78 | mbedtls_aes_free(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 79 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 80 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 81 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 83 | void aes_decrypt_cbc(data_t *key_str, data_t *iv_str, |
| 84 | data_t *src_str, data_t *dst, |
| 85 | int cbc_result) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 86 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 87 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 88 | mbedtls_aes_context ctx; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 89 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 90 | memset(output, 0x00, 100); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 91 | mbedtls_aes_init(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 92 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 93 | TEST_ASSERT(mbedtls_aes_setkey_dec(&ctx, key_str->x, key_str->len * 8) == 0); |
| 94 | TEST_ASSERT(mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x, |
| 95 | src_str->x, output) == cbc_result); |
| 96 | if (cbc_result == 0) { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 97 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 98 | TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, |
| 99 | src_str->len, dst->len) == 0); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 100 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 101 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 102 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 103 | mbedtls_aes_free(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 104 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 105 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 106 | |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 107 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 108 | void aes_encrypt_xts(char *hex_key_string, char *hex_data_unit_string, |
| 109 | char *hex_src_string, char *hex_dst_string) |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 110 | { |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 111 | enum { AES_BLOCK_SIZE = 16 }; |
| 112 | unsigned char *data_unit = NULL; |
| 113 | unsigned char *key = NULL; |
| 114 | unsigned char *src = NULL; |
| 115 | unsigned char *dst = NULL; |
| 116 | unsigned char *output = NULL; |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 117 | mbedtls_aes_xts_context ctx; |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 118 | size_t key_len, src_len, dst_len, data_unit_len; |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 119 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 120 | mbedtls_aes_xts_init(&ctx); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 121 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 122 | data_unit = mbedtls_test_unhexify_alloc(hex_data_unit_string, |
| 123 | &data_unit_len); |
| 124 | TEST_ASSERT(data_unit_len == AES_BLOCK_SIZE); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 125 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 126 | key = mbedtls_test_unhexify_alloc(hex_key_string, &key_len); |
| 127 | TEST_ASSERT(key_len % 2 == 0); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 128 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 129 | src = mbedtls_test_unhexify_alloc(hex_src_string, &src_len); |
| 130 | dst = mbedtls_test_unhexify_alloc(hex_dst_string, &dst_len); |
| 131 | TEST_ASSERT(src_len == dst_len); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 132 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 133 | output = mbedtls_test_zero_alloc(dst_len); |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 134 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 135 | TEST_ASSERT(mbedtls_aes_xts_setkey_enc(&ctx, key, key_len * 8) == 0); |
| 136 | TEST_ASSERT(mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, src_len, |
| 137 | data_unit, src, output) == 0); |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 138 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 139 | TEST_ASSERT(memcmp(output, dst, dst_len) == 0); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 140 | |
| 141 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 142 | mbedtls_aes_xts_free(&ctx); |
| 143 | mbedtls_free(data_unit); |
| 144 | mbedtls_free(key); |
| 145 | mbedtls_free(src); |
| 146 | mbedtls_free(dst); |
| 147 | mbedtls_free(output); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 148 | } |
| 149 | /* END_CASE */ |
| 150 | |
| 151 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 152 | void aes_decrypt_xts(char *hex_key_string, char *hex_data_unit_string, |
| 153 | char *hex_dst_string, char *hex_src_string) |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 154 | { |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 155 | enum { AES_BLOCK_SIZE = 16 }; |
| 156 | unsigned char *data_unit = NULL; |
| 157 | unsigned char *key = NULL; |
| 158 | unsigned char *src = NULL; |
| 159 | unsigned char *dst = NULL; |
| 160 | unsigned char *output = NULL; |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 161 | mbedtls_aes_xts_context ctx; |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 162 | size_t key_len, src_len, dst_len, data_unit_len; |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 163 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 164 | mbedtls_aes_xts_init(&ctx); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 165 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 166 | data_unit = mbedtls_test_unhexify_alloc(hex_data_unit_string, |
| 167 | &data_unit_len); |
| 168 | TEST_ASSERT(data_unit_len == AES_BLOCK_SIZE); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 169 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 170 | key = mbedtls_test_unhexify_alloc(hex_key_string, &key_len); |
| 171 | TEST_ASSERT(key_len % 2 == 0); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 172 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 173 | src = mbedtls_test_unhexify_alloc(hex_src_string, &src_len); |
| 174 | dst = mbedtls_test_unhexify_alloc(hex_dst_string, &dst_len); |
| 175 | TEST_ASSERT(src_len == dst_len); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 176 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 177 | output = mbedtls_test_zero_alloc(dst_len); |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 178 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 179 | TEST_ASSERT(mbedtls_aes_xts_setkey_dec(&ctx, key, key_len * 8) == 0); |
| 180 | TEST_ASSERT(mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_DECRYPT, src_len, |
| 181 | data_unit, src, output) == 0); |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 182 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 183 | TEST_ASSERT(memcmp(output, dst, dst_len) == 0); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 184 | |
| 185 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 186 | mbedtls_aes_xts_free(&ctx); |
| 187 | mbedtls_free(data_unit); |
| 188 | mbedtls_free(key); |
| 189 | mbedtls_free(src); |
| 190 | mbedtls_free(dst); |
| 191 | mbedtls_free(output); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 192 | } |
| 193 | /* END_CASE */ |
| 194 | |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 195 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 196 | void aes_crypt_xts_size(int size, int retval) |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 197 | { |
| 198 | mbedtls_aes_xts_context ctx; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 199 | const unsigned char src[16] = { 0 }; |
| 200 | unsigned char output[16]; |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 201 | unsigned char data_unit[16]; |
| 202 | size_t length = size; |
| 203 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 204 | mbedtls_aes_xts_init(&ctx); |
| 205 | memset(data_unit, 0x00, sizeof(data_unit)); |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 206 | |
| 207 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 208 | /* Valid pointers are passed for builds with MBEDTLS_CHECK_PARAMS, as |
| 209 | * otherwise we wouldn't get to the size check we're interested in. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 210 | TEST_ASSERT(mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, length, data_unit, src, |
| 211 | output) == retval); |
JoeSubbiani | 67889a5 | 2021-06-17 16:12:23 +0100 | [diff] [blame] | 212 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 213 | mbedtls_aes_xts_free(&ctx); |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 214 | } |
| 215 | /* END_CASE */ |
| 216 | |
Jaeden Amero | 142383e | 2018-05-31 10:40:34 +0100 | [diff] [blame] | 217 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 218 | void aes_crypt_xts_keysize(int size, int retval) |
Jaeden Amero | 142383e | 2018-05-31 10:40:34 +0100 | [diff] [blame] | 219 | { |
| 220 | mbedtls_aes_xts_context ctx; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 221 | const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; |
Jaeden Amero | 142383e | 2018-05-31 10:40:34 +0100 | [diff] [blame] | 222 | size_t key_len = size; |
| 223 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 224 | mbedtls_aes_xts_init(&ctx); |
Jaeden Amero | 142383e | 2018-05-31 10:40:34 +0100 | [diff] [blame] | 225 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 226 | TEST_ASSERT(mbedtls_aes_xts_setkey_enc(&ctx, key, key_len * 8) == retval); |
| 227 | TEST_ASSERT(mbedtls_aes_xts_setkey_dec(&ctx, key, key_len * 8) == retval); |
Jaeden Amero | 142383e | 2018-05-31 10:40:34 +0100 | [diff] [blame] | 228 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 229 | mbedtls_aes_xts_free(&ctx); |
Jaeden Amero | 142383e | 2018-05-31 10:40:34 +0100 | [diff] [blame] | 230 | } |
| 231 | /* END_CASE */ |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 232 | |
| 233 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 235 | void aes_encrypt_cfb128(data_t *key_str, data_t *iv_str, |
| 236 | data_t *src_str, data_t *dst) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 237 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 238 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | mbedtls_aes_context ctx; |
Paul Bakker | cd43a0b | 2011-06-09 13:55:44 +0000 | [diff] [blame] | 240 | size_t iv_offset = 0; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 241 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 242 | memset(output, 0x00, 100); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 243 | mbedtls_aes_init(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 244 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 245 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 246 | TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0); |
| 247 | TEST_ASSERT(mbedtls_aes_crypt_cfb128(&ctx, MBEDTLS_AES_ENCRYPT, 16, &iv_offset, iv_str->x, |
| 248 | src_str->x, output) == 0); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 249 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 250 | TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 251 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 252 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 253 | mbedtls_aes_free(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 254 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 255 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 256 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 258 | void aes_decrypt_cfb128(data_t *key_str, data_t *iv_str, |
| 259 | data_t *src_str, data_t *dst) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 260 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 261 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | mbedtls_aes_context ctx; |
Paul Bakker | cd43a0b | 2011-06-09 13:55:44 +0000 | [diff] [blame] | 263 | size_t iv_offset = 0; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 264 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 265 | memset(output, 0x00, 100); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 266 | mbedtls_aes_init(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 267 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 268 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 269 | TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0); |
| 270 | TEST_ASSERT(mbedtls_aes_crypt_cfb128(&ctx, MBEDTLS_AES_DECRYPT, 16, &iv_offset, iv_str->x, |
| 271 | src_str->x, output) == 0); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 272 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 273 | TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 274 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 275 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 276 | mbedtls_aes_free(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 277 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 278 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 279 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 281 | void aes_encrypt_cfb8(data_t *key_str, data_t *iv_str, |
| 282 | data_t *src_str, data_t *dst) |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 283 | { |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 284 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 285 | mbedtls_aes_context ctx; |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 286 | |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 287 | memset(output, 0x00, 100); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 288 | mbedtls_aes_init(&ctx); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 289 | |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 290 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 291 | TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0); |
| 292 | TEST_ASSERT(mbedtls_aes_crypt_cfb8(&ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x, |
| 293 | src_str->x, output) == 0); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 294 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 295 | TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, |
| 296 | src_str->len, dst->len) == 0); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 297 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 298 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 299 | mbedtls_aes_free(&ctx); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 300 | } |
| 301 | /* END_CASE */ |
| 302 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 303 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 304 | void aes_decrypt_cfb8(data_t *key_str, data_t *iv_str, |
| 305 | data_t *src_str, data_t *dst) |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 306 | { |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 307 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 308 | mbedtls_aes_context ctx; |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 309 | |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 310 | memset(output, 0x00, 100); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 311 | mbedtls_aes_init(&ctx); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 312 | |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 313 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 314 | TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0); |
| 315 | TEST_ASSERT(mbedtls_aes_crypt_cfb8(&ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x, |
| 316 | src_str->x, output) == 0); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 317 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 318 | TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, |
| 319 | src_str->len, dst->len) == 0); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 320 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 321 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 322 | mbedtls_aes_free(&ctx); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 323 | } |
| 324 | /* END_CASE */ |
| 325 | |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 326 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 327 | void aes_encrypt_ofb(int fragment_size, data_t *key_str, |
| 328 | data_t *iv_str, data_t *src_str, |
| 329 | data_t *expected_output) |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 330 | { |
Simon Butcher | e416bf9 | 2018-06-02 18:28:32 +0100 | [diff] [blame] | 331 | unsigned char output[32]; |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 332 | mbedtls_aes_context ctx; |
| 333 | size_t iv_offset = 0; |
| 334 | int in_buffer_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 335 | unsigned char *src_str_next; |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 336 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 337 | memset(output, 0x00, sizeof(output)); |
| 338 | mbedtls_aes_init(&ctx); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 339 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 340 | TEST_ASSERT((size_t) fragment_size < sizeof(output)); |
Simon Butcher | e416bf9 | 2018-06-02 18:28:32 +0100 | [diff] [blame] | 341 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 342 | TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, |
| 343 | key_str->len * 8) == 0); |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 344 | in_buffer_len = src_str->len; |
| 345 | src_str_next = src_str->x; |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 346 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 347 | while (in_buffer_len > 0) { |
| 348 | TEST_ASSERT(mbedtls_aes_crypt_ofb(&ctx, fragment_size, &iv_offset, |
| 349 | iv_str->x, src_str_next, output) == 0); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 350 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 351 | TEST_ASSERT(memcmp(output, expected_output->x, fragment_size) == 0); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 352 | |
| 353 | in_buffer_len -= fragment_size; |
Ronald Cron | 55d97f2 | 2020-06-26 17:00:30 +0200 | [diff] [blame] | 354 | expected_output->x += fragment_size; |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 355 | src_str_next += fragment_size; |
| 356 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 357 | if (in_buffer_len < fragment_size) { |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 358 | fragment_size = in_buffer_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 359 | } |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 363 | mbedtls_aes_free(&ctx); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 364 | } |
| 365 | /* END_CASE */ |
| 366 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 367 | /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 368 | void aes_check_params() |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 369 | { |
| 370 | mbedtls_aes_context aes_ctx; |
| 371 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
| 372 | mbedtls_aes_xts_context xts_ctx; |
| 373 | #endif |
| 374 | const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; |
| 375 | const unsigned char in[16] = { 0 }; |
| 376 | unsigned char out[16]; |
| 377 | size_t size; |
| 378 | const int valid_mode = MBEDTLS_AES_ENCRYPT; |
| 379 | const int invalid_mode = 42; |
| 380 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 381 | TEST_INVALID_PARAM(mbedtls_aes_init(NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 382 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 383 | TEST_INVALID_PARAM(mbedtls_aes_xts_init(NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 384 | #endif |
| 385 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 386 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 387 | mbedtls_aes_setkey_enc(NULL, key, 128)); |
| 388 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 389 | mbedtls_aes_setkey_enc(&aes_ctx, NULL, 128)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 390 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 391 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 392 | mbedtls_aes_setkey_dec(NULL, key, 128)); |
| 393 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 394 | mbedtls_aes_setkey_dec(&aes_ctx, NULL, 128)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 395 | |
| 396 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 397 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 398 | mbedtls_aes_xts_setkey_enc(NULL, key, 128)); |
| 399 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 400 | mbedtls_aes_xts_setkey_enc(&xts_ctx, NULL, 128)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 401 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 402 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 403 | mbedtls_aes_xts_setkey_dec(NULL, key, 128)); |
| 404 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 405 | mbedtls_aes_xts_setkey_dec(&xts_ctx, NULL, 128)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 406 | #endif |
| 407 | |
| 408 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 409 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 410 | mbedtls_aes_crypt_ecb(NULL, |
| 411 | valid_mode, in, out)); |
| 412 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 413 | mbedtls_aes_crypt_ecb(&aes_ctx, |
| 414 | invalid_mode, in, out)); |
| 415 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 416 | mbedtls_aes_crypt_ecb(&aes_ctx, |
| 417 | valid_mode, NULL, out)); |
| 418 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 419 | mbedtls_aes_crypt_ecb(&aes_ctx, |
| 420 | valid_mode, in, NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 421 | |
| 422 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 423 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 424 | mbedtls_aes_crypt_cbc(NULL, |
| 425 | valid_mode, 16, |
| 426 | out, in, out)); |
| 427 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 428 | mbedtls_aes_crypt_cbc(&aes_ctx, |
| 429 | invalid_mode, 16, |
| 430 | out, in, out)); |
| 431 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 432 | mbedtls_aes_crypt_cbc(&aes_ctx, |
| 433 | valid_mode, 16, |
| 434 | NULL, in, out)); |
| 435 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 436 | mbedtls_aes_crypt_cbc(&aes_ctx, |
| 437 | valid_mode, 16, |
| 438 | out, NULL, out)); |
| 439 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 440 | mbedtls_aes_crypt_cbc(&aes_ctx, |
| 441 | valid_mode, 16, |
| 442 | out, in, NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 443 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 444 | |
| 445 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 446 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 447 | mbedtls_aes_crypt_xts(NULL, |
| 448 | valid_mode, 16, |
| 449 | in, in, out)); |
| 450 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 451 | mbedtls_aes_crypt_xts(&xts_ctx, |
| 452 | invalid_mode, 16, |
| 453 | in, in, out)); |
| 454 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 455 | mbedtls_aes_crypt_xts(&xts_ctx, |
| 456 | valid_mode, 16, |
| 457 | NULL, in, out)); |
| 458 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 459 | mbedtls_aes_crypt_xts(&xts_ctx, |
| 460 | valid_mode, 16, |
| 461 | in, NULL, out)); |
| 462 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 463 | mbedtls_aes_crypt_xts(&xts_ctx, |
| 464 | valid_mode, 16, |
| 465 | in, in, NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 466 | #endif /* MBEDTLS_CIPHER_MODE_XTS */ |
| 467 | |
| 468 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 469 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 470 | mbedtls_aes_crypt_cfb128(NULL, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 471 | valid_mode, 16, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 472 | &size, out, in, out)); |
| 473 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 474 | mbedtls_aes_crypt_cfb128(&aes_ctx, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 475 | invalid_mode, 16, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 476 | &size, out, in, out)); |
| 477 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 478 | mbedtls_aes_crypt_cfb128(&aes_ctx, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 479 | valid_mode, 16, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 480 | NULL, out, in, out)); |
| 481 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 482 | mbedtls_aes_crypt_cfb128(&aes_ctx, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 483 | valid_mode, 16, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 484 | &size, NULL, in, out)); |
| 485 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 486 | mbedtls_aes_crypt_cfb128(&aes_ctx, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 487 | valid_mode, 16, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 488 | &size, out, NULL, out)); |
| 489 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 490 | mbedtls_aes_crypt_cfb128(&aes_ctx, |
| 491 | valid_mode, 16, |
| 492 | &size, out, in, NULL)); |
| 493 | |
| 494 | |
| 495 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 496 | mbedtls_aes_crypt_cfb8(NULL, |
| 497 | valid_mode, 16, |
| 498 | out, in, out)); |
| 499 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 500 | mbedtls_aes_crypt_cfb8(&aes_ctx, |
| 501 | invalid_mode, 16, |
| 502 | out, in, out)); |
| 503 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 504 | mbedtls_aes_crypt_cfb8(&aes_ctx, |
| 505 | valid_mode, 16, |
| 506 | NULL, in, out)); |
| 507 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 508 | mbedtls_aes_crypt_cfb8(&aes_ctx, |
| 509 | valid_mode, 16, |
| 510 | out, NULL, out)); |
| 511 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 512 | mbedtls_aes_crypt_cfb8(&aes_ctx, |
| 513 | valid_mode, 16, |
| 514 | out, in, NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 515 | #endif /* MBEDTLS_CIPHER_MODE_CFB */ |
| 516 | |
| 517 | #if defined(MBEDTLS_CIPHER_MODE_OFB) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 518 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 519 | mbedtls_aes_crypt_ofb(NULL, 16, |
| 520 | &size, out, in, out)); |
| 521 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 522 | mbedtls_aes_crypt_ofb(&aes_ctx, 16, |
| 523 | NULL, out, in, out)); |
| 524 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 525 | mbedtls_aes_crypt_ofb(&aes_ctx, 16, |
| 526 | &size, NULL, in, out)); |
| 527 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 528 | mbedtls_aes_crypt_ofb(&aes_ctx, 16, |
| 529 | &size, out, NULL, out)); |
| 530 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 531 | mbedtls_aes_crypt_ofb(&aes_ctx, 16, |
| 532 | &size, out, in, NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 533 | #endif /* MBEDTLS_CIPHER_MODE_OFB */ |
| 534 | |
| 535 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 536 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 537 | mbedtls_aes_crypt_ctr(NULL, 16, &size, out, |
| 538 | out, in, out)); |
| 539 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 540 | mbedtls_aes_crypt_ctr(&aes_ctx, 16, NULL, out, |
| 541 | out, in, out)); |
| 542 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 543 | mbedtls_aes_crypt_ctr(&aes_ctx, 16, &size, NULL, |
| 544 | out, in, out)); |
| 545 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 546 | mbedtls_aes_crypt_ctr(&aes_ctx, 16, &size, out, |
| 547 | NULL, in, out)); |
| 548 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 549 | mbedtls_aes_crypt_ctr(&aes_ctx, 16, &size, out, |
| 550 | out, NULL, out)); |
| 551 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 552 | mbedtls_aes_crypt_ctr(&aes_ctx, 16, &size, out, |
| 553 | out, in, NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 554 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
| 555 | } |
| 556 | /* END_CASE */ |
| 557 | |
| 558 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 559 | void aes_misc_params() |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 560 | { |
| 561 | #if defined(MBEDTLS_CIPHER_MODE_CBC) || \ |
| 562 | defined(MBEDTLS_CIPHER_MODE_XTS) || \ |
| 563 | defined(MBEDTLS_CIPHER_MODE_CFB) || \ |
| 564 | defined(MBEDTLS_CIPHER_MODE_OFB) |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 565 | const unsigned char in[16] = { 0 }; |
| 566 | unsigned char out[16]; |
| 567 | #endif |
Andrzej Kurek | 8ffd8a6 | 2022-09-27 07:54:16 -0400 | [diff] [blame] | 568 | #if defined(MBEDTLS_CIPHER_MODE_CBC) || \ |
| 569 | defined(MBEDTLS_CIPHER_MODE_CFB) || \ |
| 570 | defined(MBEDTLS_CIPHER_MODE_OFB) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 571 | mbedtls_aes_context aes_ctx; |
Andrzej Kurek | 8ffd8a6 | 2022-09-27 07:54:16 -0400 | [diff] [blame] | 572 | #endif |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 573 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
| 574 | mbedtls_aes_xts_context xts_ctx; |
| 575 | #endif |
| 576 | #if defined(MBEDTLS_CIPHER_MODE_CFB) || \ |
| 577 | defined(MBEDTLS_CIPHER_MODE_OFB) |
| 578 | size_t size; |
| 579 | #endif |
| 580 | |
| 581 | /* These calls accept NULL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 582 | TEST_VALID_PARAM(mbedtls_aes_free(NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 583 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 584 | TEST_VALID_PARAM(mbedtls_aes_xts_free(NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 585 | #endif |
| 586 | |
| 587 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 588 | TEST_ASSERT(mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT, |
| 589 | 15, |
| 590 | out, in, out) |
| 591 | == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH); |
| 592 | TEST_ASSERT(mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT, |
| 593 | 17, |
| 594 | out, in, out) |
| 595 | == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 596 | #endif |
| 597 | |
| 598 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 599 | TEST_ASSERT(mbedtls_aes_crypt_xts(&xts_ctx, MBEDTLS_AES_ENCRYPT, |
| 600 | 15, |
| 601 | in, in, out) |
| 602 | == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH); |
| 603 | TEST_ASSERT(mbedtls_aes_crypt_xts(&xts_ctx, MBEDTLS_AES_ENCRYPT, |
| 604 | (1 << 24) + 1, |
| 605 | in, in, out) |
| 606 | == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 607 | #endif |
| 608 | |
| 609 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
| 610 | size = 16; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 611 | TEST_ASSERT(mbedtls_aes_crypt_cfb128(&aes_ctx, MBEDTLS_AES_ENCRYPT, 16, |
| 612 | &size, out, in, out) |
| 613 | == MBEDTLS_ERR_AES_BAD_INPUT_DATA); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 614 | #endif |
| 615 | |
| 616 | #if defined(MBEDTLS_CIPHER_MODE_OFB) |
| 617 | size = 16; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 618 | TEST_ASSERT(mbedtls_aes_crypt_ofb(&aes_ctx, 16, &size, out, in, out) |
| 619 | == MBEDTLS_ERR_AES_BAD_INPUT_DATA); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 620 | #endif |
| 621 | } |
| 622 | /* END_CASE */ |
| 623 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 624 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 625 | void aes_selftest() |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 626 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 627 | TEST_ASSERT(mbedtls_aes_self_test(1) == 0); |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 628 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 629 | /* END_CASE */ |