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" |
Tom Cosgrove | b5eb831 | 2023-03-20 10:57:42 +0000 | [diff] [blame] | 3 | |
| 4 | /* Test AES with a copied context. |
| 5 | * |
| 6 | * enc and dec must be AES context objects. They don't need to |
| 7 | * be initialized, and are left freed. |
| 8 | */ |
| 9 | static int test_ctx_alignment(const data_t *key, |
| 10 | mbedtls_aes_context *enc, |
| 11 | mbedtls_aes_context *dec) |
| 12 | { |
| 13 | unsigned char plaintext[16] = { |
| 14 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 15 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 16 | }; |
| 17 | unsigned char ciphertext[16]; |
| 18 | unsigned char output[16]; |
| 19 | |
| 20 | // Set key and encrypt with original context |
| 21 | mbedtls_aes_init(enc); |
| 22 | TEST_ASSERT(mbedtls_aes_setkey_enc(enc, key->x, key->len * 8) == 0); |
| 23 | TEST_ASSERT(mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_ENCRYPT, |
| 24 | plaintext, ciphertext) == 0); |
| 25 | |
| 26 | // Set key for decryption with original context |
| 27 | mbedtls_aes_init(dec); |
| 28 | TEST_ASSERT(mbedtls_aes_setkey_dec(dec, key->x, key->len * 8) == 0); |
| 29 | |
| 30 | // Wipe the original context to make sure nothing from it is used |
| 31 | memset(enc, 0, sizeof(*enc)); |
| 32 | mbedtls_aes_free(enc); |
| 33 | |
| 34 | // Decrypt |
| 35 | TEST_ASSERT(mbedtls_aes_crypt_ecb(dec, MBEDTLS_AES_DECRYPT, |
| 36 | ciphertext, output) == 0); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 37 | TEST_MEMORY_COMPARE(plaintext, 16, output, 16); |
Tom Cosgrove | b5eb831 | 2023-03-20 10:57:42 +0000 | [diff] [blame] | 38 | |
| 39 | mbedtls_aes_free(dec); |
| 40 | |
| 41 | return 1; |
| 42 | |
| 43 | exit: |
| 44 | /* Bug: we may be leaving something unfreed. This is harmless |
| 45 | * in our built-in implementations, but might cause a memory leak |
| 46 | * with alternative implementations. */ |
| 47 | return 0; |
| 48 | } |
| 49 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 50 | /* END_HEADER */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 51 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 52 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | * depends_on:MBEDTLS_AES_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 54 | * END_DEPENDENCIES |
| 55 | */ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 56 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 57 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 58 | void aes_encrypt_ecb(data_t *key_str, data_t *src_str, |
| 59 | data_t *dst, int setkey_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) == setkey_result); |
| 69 | if (setkey_result == 0) { |
| 70 | 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] | 71 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 72 | TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0); |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 73 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 74 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 75 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 76 | mbedtls_aes_free(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 77 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 78 | /* END_CASE */ |
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 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 81 | void aes_decrypt_ecb(data_t *key_str, data_t *src_str, |
| 82 | data_t *dst, int setkey_result) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 83 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 84 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 85 | mbedtls_aes_context ctx; |
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 | memset(output, 0x00, 100); |
| 88 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 89 | mbedtls_aes_init(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 90 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 91 | TEST_ASSERT(mbedtls_aes_setkey_dec(&ctx, key_str->x, key_str->len * 8) == setkey_result); |
| 92 | if (setkey_result == 0) { |
| 93 | 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] | 94 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 95 | TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0); |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 96 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 97 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 98 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 99 | mbedtls_aes_free(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 100 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 101 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 102 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 104 | void aes_encrypt_cbc(data_t *key_str, data_t *iv_str, |
| 105 | data_t *src_str, data_t *dst, |
| 106 | int cbc_result) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 107 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 108 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | mbedtls_aes_context ctx; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 110 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 111 | memset(output, 0x00, 100); |
| 112 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 113 | mbedtls_aes_init(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 114 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 115 | TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0); |
| 116 | TEST_ASSERT(mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x, |
| 117 | src_str->x, output) == cbc_result); |
| 118 | if (cbc_result == 0) { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 119 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 120 | TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, |
| 121 | src_str->len, dst->len) == 0); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 122 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 123 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 124 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 125 | mbedtls_aes_free(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 126 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 127 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 128 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 130 | void aes_decrypt_cbc(data_t *key_str, data_t *iv_str, |
| 131 | data_t *src_str, data_t *dst, |
| 132 | int cbc_result) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 133 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 134 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 135 | mbedtls_aes_context ctx; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 136 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 137 | memset(output, 0x00, 100); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 138 | mbedtls_aes_init(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 139 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 140 | TEST_ASSERT(mbedtls_aes_setkey_dec(&ctx, key_str->x, key_str->len * 8) == 0); |
| 141 | TEST_ASSERT(mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x, |
| 142 | src_str->x, output) == cbc_result); |
| 143 | if (cbc_result == 0) { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 144 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 145 | TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, |
| 146 | src_str->len, dst->len) == 0); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 147 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 148 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 149 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 150 | mbedtls_aes_free(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 151 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 152 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 153 | |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 154 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 155 | void aes_encrypt_xts(char *hex_key_string, char *hex_data_unit_string, |
| 156 | char *hex_src_string, char *hex_dst_string) |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 157 | { |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 158 | enum { AES_BLOCK_SIZE = 16 }; |
| 159 | unsigned char *data_unit = NULL; |
| 160 | unsigned char *key = NULL; |
| 161 | unsigned char *src = NULL; |
| 162 | unsigned char *dst = NULL; |
| 163 | unsigned char *output = NULL; |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 164 | mbedtls_aes_xts_context ctx; |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 165 | size_t key_len, src_len, dst_len, data_unit_len; |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 166 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 167 | mbedtls_aes_xts_init(&ctx); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 168 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 169 | data_unit = mbedtls_test_unhexify_alloc(hex_data_unit_string, |
| 170 | &data_unit_len); |
| 171 | TEST_ASSERT(data_unit_len == AES_BLOCK_SIZE); |
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 | key = mbedtls_test_unhexify_alloc(hex_key_string, &key_len); |
| 174 | TEST_ASSERT(key_len % 2 == 0); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 175 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 176 | src = mbedtls_test_unhexify_alloc(hex_src_string, &src_len); |
| 177 | dst = mbedtls_test_unhexify_alloc(hex_dst_string, &dst_len); |
| 178 | TEST_ASSERT(src_len == dst_len); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 179 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 180 | output = mbedtls_test_zero_alloc(dst_len); |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 181 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 182 | TEST_ASSERT(mbedtls_aes_xts_setkey_enc(&ctx, key, key_len * 8) == 0); |
| 183 | TEST_ASSERT(mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, src_len, |
| 184 | data_unit, src, output) == 0); |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 185 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 186 | TEST_ASSERT(memcmp(output, dst, dst_len) == 0); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 187 | |
| 188 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 189 | mbedtls_aes_xts_free(&ctx); |
| 190 | mbedtls_free(data_unit); |
| 191 | mbedtls_free(key); |
| 192 | mbedtls_free(src); |
| 193 | mbedtls_free(dst); |
| 194 | mbedtls_free(output); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 195 | } |
| 196 | /* END_CASE */ |
| 197 | |
| 198 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 199 | void aes_decrypt_xts(char *hex_key_string, char *hex_data_unit_string, |
| 200 | char *hex_dst_string, char *hex_src_string) |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 201 | { |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 202 | enum { AES_BLOCK_SIZE = 16 }; |
| 203 | unsigned char *data_unit = NULL; |
| 204 | unsigned char *key = NULL; |
| 205 | unsigned char *src = NULL; |
| 206 | unsigned char *dst = NULL; |
| 207 | unsigned char *output = NULL; |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 208 | mbedtls_aes_xts_context ctx; |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 209 | size_t key_len, src_len, dst_len, data_unit_len; |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 210 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 211 | mbedtls_aes_xts_init(&ctx); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 212 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 213 | data_unit = mbedtls_test_unhexify_alloc(hex_data_unit_string, |
| 214 | &data_unit_len); |
| 215 | TEST_ASSERT(data_unit_len == AES_BLOCK_SIZE); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 216 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 217 | key = mbedtls_test_unhexify_alloc(hex_key_string, &key_len); |
| 218 | TEST_ASSERT(key_len % 2 == 0); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 219 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 220 | src = mbedtls_test_unhexify_alloc(hex_src_string, &src_len); |
| 221 | dst = mbedtls_test_unhexify_alloc(hex_dst_string, &dst_len); |
| 222 | TEST_ASSERT(src_len == dst_len); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 223 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 224 | output = mbedtls_test_zero_alloc(dst_len); |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 225 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 226 | TEST_ASSERT(mbedtls_aes_xts_setkey_dec(&ctx, key, key_len * 8) == 0); |
| 227 | TEST_ASSERT(mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_DECRYPT, src_len, |
| 228 | data_unit, src, output) == 0); |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 229 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 230 | TEST_ASSERT(memcmp(output, dst, dst_len) == 0); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 231 | |
| 232 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 233 | mbedtls_aes_xts_free(&ctx); |
| 234 | mbedtls_free(data_unit); |
| 235 | mbedtls_free(key); |
| 236 | mbedtls_free(src); |
| 237 | mbedtls_free(dst); |
| 238 | mbedtls_free(output); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 239 | } |
| 240 | /* END_CASE */ |
| 241 | |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 242 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 243 | void aes_crypt_xts_size(int size, int retval) |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 244 | { |
| 245 | mbedtls_aes_xts_context ctx; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 246 | const unsigned char src[16] = { 0 }; |
| 247 | unsigned char output[16]; |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 248 | unsigned char data_unit[16]; |
| 249 | size_t length = size; |
| 250 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 251 | mbedtls_aes_xts_init(&ctx); |
| 252 | memset(data_unit, 0x00, sizeof(data_unit)); |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 253 | |
| 254 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 255 | /* Valid pointers are passed for builds with MBEDTLS_CHECK_PARAMS, as |
| 256 | * 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] | 257 | TEST_ASSERT(mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, length, data_unit, src, |
| 258 | output) == retval); |
JoeSubbiani | 67889a5 | 2021-06-17 16:12:23 +0100 | [diff] [blame] | 259 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 260 | mbedtls_aes_xts_free(&ctx); |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 261 | } |
| 262 | /* END_CASE */ |
| 263 | |
Jaeden Amero | 142383e | 2018-05-31 10:40:34 +0100 | [diff] [blame] | 264 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 265 | void aes_crypt_xts_keysize(int size, int retval) |
Jaeden Amero | 142383e | 2018-05-31 10:40:34 +0100 | [diff] [blame] | 266 | { |
| 267 | mbedtls_aes_xts_context ctx; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 268 | const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; |
Jaeden Amero | 142383e | 2018-05-31 10:40:34 +0100 | [diff] [blame] | 269 | size_t key_len = size; |
| 270 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 271 | mbedtls_aes_xts_init(&ctx); |
Jaeden Amero | 142383e | 2018-05-31 10:40:34 +0100 | [diff] [blame] | 272 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 273 | TEST_ASSERT(mbedtls_aes_xts_setkey_enc(&ctx, key, key_len * 8) == retval); |
| 274 | 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] | 275 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 276 | mbedtls_aes_xts_free(&ctx); |
Jaeden Amero | 142383e | 2018-05-31 10:40:34 +0100 | [diff] [blame] | 277 | } |
| 278 | /* END_CASE */ |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 279 | |
| 280 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 282 | void aes_encrypt_cfb128(data_t *key_str, data_t *iv_str, |
| 283 | data_t *src_str, data_t *dst) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 284 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 285 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 286 | mbedtls_aes_context ctx; |
Paul Bakker | cd43a0b | 2011-06-09 13:55:44 +0000 | [diff] [blame] | 287 | size_t iv_offset = 0; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 288 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 289 | memset(output, 0x00, 100); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 290 | mbedtls_aes_init(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 291 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 292 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 293 | TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0); |
| 294 | TEST_ASSERT(mbedtls_aes_crypt_cfb128(&ctx, MBEDTLS_AES_ENCRYPT, 16, &iv_offset, iv_str->x, |
| 295 | src_str->x, output) == 0); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 296 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 297 | TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 298 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 299 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 300 | mbedtls_aes_free(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 301 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 302 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 303 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 304 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 305 | void aes_decrypt_cfb128(data_t *key_str, data_t *iv_str, |
| 306 | data_t *src_str, data_t *dst) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 307 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 308 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 309 | mbedtls_aes_context ctx; |
Paul Bakker | cd43a0b | 2011-06-09 13:55:44 +0000 | [diff] [blame] | 310 | size_t iv_offset = 0; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 311 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 312 | memset(output, 0x00, 100); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 313 | mbedtls_aes_init(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 314 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 315 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 316 | TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0); |
| 317 | TEST_ASSERT(mbedtls_aes_crypt_cfb128(&ctx, MBEDTLS_AES_DECRYPT, 16, &iv_offset, iv_str->x, |
| 318 | src_str->x, output) == 0); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 319 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 320 | TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 321 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 322 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 323 | mbedtls_aes_free(&ctx); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 324 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 325 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 326 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 327 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 328 | void aes_encrypt_cfb8(data_t *key_str, data_t *iv_str, |
| 329 | data_t *src_str, data_t *dst) |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 330 | { |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 331 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 332 | mbedtls_aes_context ctx; |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 333 | |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 334 | memset(output, 0x00, 100); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 335 | mbedtls_aes_init(&ctx); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 336 | |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 337 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 338 | TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0); |
| 339 | TEST_ASSERT(mbedtls_aes_crypt_cfb8(&ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x, |
| 340 | src_str->x, output) == 0); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 341 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 342 | TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, |
| 343 | src_str->len, dst->len) == 0); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 344 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 345 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 346 | mbedtls_aes_free(&ctx); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 347 | } |
| 348 | /* END_CASE */ |
| 349 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 351 | void aes_decrypt_cfb8(data_t *key_str, data_t *iv_str, |
| 352 | data_t *src_str, data_t *dst) |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 353 | { |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 354 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 355 | mbedtls_aes_context ctx; |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 356 | |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 357 | memset(output, 0x00, 100); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 358 | mbedtls_aes_init(&ctx); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 359 | |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 360 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 361 | TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0); |
| 362 | TEST_ASSERT(mbedtls_aes_crypt_cfb8(&ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x, |
| 363 | src_str->x, output) == 0); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 364 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 365 | TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, |
| 366 | src_str->len, dst->len) == 0); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 367 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 368 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 369 | mbedtls_aes_free(&ctx); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 370 | } |
| 371 | /* END_CASE */ |
| 372 | |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 373 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 374 | void aes_encrypt_ofb(int fragment_size, data_t *key_str, |
| 375 | data_t *iv_str, data_t *src_str, |
| 376 | data_t *expected_output) |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 377 | { |
Simon Butcher | e416bf9 | 2018-06-02 18:28:32 +0100 | [diff] [blame] | 378 | unsigned char output[32]; |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 379 | mbedtls_aes_context ctx; |
| 380 | size_t iv_offset = 0; |
| 381 | int in_buffer_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 382 | unsigned char *src_str_next; |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 383 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 384 | memset(output, 0x00, sizeof(output)); |
| 385 | mbedtls_aes_init(&ctx); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 386 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 387 | TEST_ASSERT((size_t) fragment_size < sizeof(output)); |
Simon Butcher | e416bf9 | 2018-06-02 18:28:32 +0100 | [diff] [blame] | 388 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 389 | TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, |
| 390 | key_str->len * 8) == 0); |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 391 | in_buffer_len = src_str->len; |
| 392 | src_str_next = src_str->x; |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 393 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 394 | while (in_buffer_len > 0) { |
| 395 | TEST_ASSERT(mbedtls_aes_crypt_ofb(&ctx, fragment_size, &iv_offset, |
| 396 | iv_str->x, src_str_next, output) == 0); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 397 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 398 | TEST_ASSERT(memcmp(output, expected_output->x, fragment_size) == 0); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 399 | |
| 400 | in_buffer_len -= fragment_size; |
Ronald Cron | 55d97f2 | 2020-06-26 17:00:30 +0200 | [diff] [blame] | 401 | expected_output->x += fragment_size; |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 402 | src_str_next += fragment_size; |
| 403 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 404 | if (in_buffer_len < fragment_size) { |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 405 | fragment_size = in_buffer_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 406 | } |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 410 | mbedtls_aes_free(&ctx); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 411 | } |
| 412 | /* END_CASE */ |
| 413 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 414 | /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 415 | void aes_check_params() |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 416 | { |
| 417 | mbedtls_aes_context aes_ctx; |
| 418 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
| 419 | mbedtls_aes_xts_context xts_ctx; |
| 420 | #endif |
| 421 | const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; |
| 422 | const unsigned char in[16] = { 0 }; |
| 423 | unsigned char out[16]; |
| 424 | size_t size; |
| 425 | const int valid_mode = MBEDTLS_AES_ENCRYPT; |
| 426 | const int invalid_mode = 42; |
| 427 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 428 | TEST_INVALID_PARAM(mbedtls_aes_init(NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 429 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 430 | TEST_INVALID_PARAM(mbedtls_aes_xts_init(NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 431 | #endif |
| 432 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 433 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 434 | mbedtls_aes_setkey_enc(NULL, key, 128)); |
| 435 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 436 | mbedtls_aes_setkey_enc(&aes_ctx, NULL, 128)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 437 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 438 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 439 | mbedtls_aes_setkey_dec(NULL, key, 128)); |
| 440 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 441 | mbedtls_aes_setkey_dec(&aes_ctx, NULL, 128)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 442 | |
| 443 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 444 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 445 | mbedtls_aes_xts_setkey_enc(NULL, key, 128)); |
| 446 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 447 | mbedtls_aes_xts_setkey_enc(&xts_ctx, NULL, 128)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 448 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 449 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 450 | mbedtls_aes_xts_setkey_dec(NULL, key, 128)); |
| 451 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 452 | mbedtls_aes_xts_setkey_dec(&xts_ctx, NULL, 128)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 453 | #endif |
| 454 | |
| 455 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 456 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 457 | mbedtls_aes_crypt_ecb(NULL, |
| 458 | valid_mode, in, out)); |
| 459 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 460 | mbedtls_aes_crypt_ecb(&aes_ctx, |
| 461 | invalid_mode, in, out)); |
| 462 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 463 | mbedtls_aes_crypt_ecb(&aes_ctx, |
| 464 | valid_mode, NULL, out)); |
| 465 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 466 | mbedtls_aes_crypt_ecb(&aes_ctx, |
| 467 | valid_mode, in, NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 468 | |
| 469 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 470 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 471 | mbedtls_aes_crypt_cbc(NULL, |
| 472 | valid_mode, 16, |
| 473 | out, in, out)); |
| 474 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 475 | mbedtls_aes_crypt_cbc(&aes_ctx, |
| 476 | invalid_mode, 16, |
| 477 | out, in, out)); |
| 478 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 479 | mbedtls_aes_crypt_cbc(&aes_ctx, |
| 480 | valid_mode, 16, |
| 481 | NULL, in, out)); |
| 482 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 483 | mbedtls_aes_crypt_cbc(&aes_ctx, |
| 484 | valid_mode, 16, |
| 485 | out, NULL, out)); |
| 486 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 487 | mbedtls_aes_crypt_cbc(&aes_ctx, |
| 488 | valid_mode, 16, |
| 489 | out, in, NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 490 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 491 | |
| 492 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 493 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 494 | mbedtls_aes_crypt_xts(NULL, |
| 495 | valid_mode, 16, |
| 496 | in, in, out)); |
| 497 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 498 | mbedtls_aes_crypt_xts(&xts_ctx, |
| 499 | invalid_mode, 16, |
| 500 | in, in, out)); |
| 501 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 502 | mbedtls_aes_crypt_xts(&xts_ctx, |
| 503 | valid_mode, 16, |
| 504 | NULL, in, out)); |
| 505 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 506 | mbedtls_aes_crypt_xts(&xts_ctx, |
| 507 | valid_mode, 16, |
| 508 | in, NULL, out)); |
| 509 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 510 | mbedtls_aes_crypt_xts(&xts_ctx, |
| 511 | valid_mode, 16, |
| 512 | in, in, NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 513 | #endif /* MBEDTLS_CIPHER_MODE_XTS */ |
| 514 | |
| 515 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 516 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 517 | mbedtls_aes_crypt_cfb128(NULL, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 518 | valid_mode, 16, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 519 | &size, out, in, out)); |
| 520 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 521 | mbedtls_aes_crypt_cfb128(&aes_ctx, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 522 | invalid_mode, 16, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 523 | &size, out, in, out)); |
| 524 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 525 | mbedtls_aes_crypt_cfb128(&aes_ctx, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 526 | valid_mode, 16, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 527 | NULL, out, in, out)); |
| 528 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 529 | mbedtls_aes_crypt_cfb128(&aes_ctx, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 530 | valid_mode, 16, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 531 | &size, NULL, in, out)); |
| 532 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 533 | mbedtls_aes_crypt_cfb128(&aes_ctx, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 534 | valid_mode, 16, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 535 | &size, out, NULL, out)); |
| 536 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 537 | mbedtls_aes_crypt_cfb128(&aes_ctx, |
| 538 | valid_mode, 16, |
| 539 | &size, out, in, NULL)); |
| 540 | |
| 541 | |
| 542 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 543 | mbedtls_aes_crypt_cfb8(NULL, |
| 544 | valid_mode, 16, |
| 545 | out, in, out)); |
| 546 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 547 | mbedtls_aes_crypt_cfb8(&aes_ctx, |
| 548 | invalid_mode, 16, |
| 549 | out, in, out)); |
| 550 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 551 | mbedtls_aes_crypt_cfb8(&aes_ctx, |
| 552 | valid_mode, 16, |
| 553 | NULL, in, out)); |
| 554 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 555 | mbedtls_aes_crypt_cfb8(&aes_ctx, |
| 556 | valid_mode, 16, |
| 557 | out, NULL, out)); |
| 558 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 559 | mbedtls_aes_crypt_cfb8(&aes_ctx, |
| 560 | valid_mode, 16, |
| 561 | out, in, NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 562 | #endif /* MBEDTLS_CIPHER_MODE_CFB */ |
| 563 | |
| 564 | #if defined(MBEDTLS_CIPHER_MODE_OFB) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 565 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 566 | mbedtls_aes_crypt_ofb(NULL, 16, |
| 567 | &size, out, in, out)); |
| 568 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 569 | mbedtls_aes_crypt_ofb(&aes_ctx, 16, |
| 570 | NULL, out, in, out)); |
| 571 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 572 | mbedtls_aes_crypt_ofb(&aes_ctx, 16, |
| 573 | &size, NULL, in, out)); |
| 574 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 575 | mbedtls_aes_crypt_ofb(&aes_ctx, 16, |
| 576 | &size, out, NULL, out)); |
| 577 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 578 | mbedtls_aes_crypt_ofb(&aes_ctx, 16, |
| 579 | &size, out, in, NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 580 | #endif /* MBEDTLS_CIPHER_MODE_OFB */ |
| 581 | |
| 582 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 583 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 584 | mbedtls_aes_crypt_ctr(NULL, 16, &size, out, |
| 585 | out, in, out)); |
| 586 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 587 | mbedtls_aes_crypt_ctr(&aes_ctx, 16, NULL, out, |
| 588 | out, in, out)); |
| 589 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 590 | mbedtls_aes_crypt_ctr(&aes_ctx, 16, &size, NULL, |
| 591 | out, in, out)); |
| 592 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 593 | mbedtls_aes_crypt_ctr(&aes_ctx, 16, &size, out, |
| 594 | NULL, in, out)); |
| 595 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 596 | mbedtls_aes_crypt_ctr(&aes_ctx, 16, &size, out, |
| 597 | out, NULL, out)); |
| 598 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 599 | mbedtls_aes_crypt_ctr(&aes_ctx, 16, &size, out, |
| 600 | out, in, NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 601 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
| 602 | } |
| 603 | /* END_CASE */ |
| 604 | |
| 605 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 606 | void aes_misc_params() |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 607 | { |
| 608 | #if defined(MBEDTLS_CIPHER_MODE_CBC) || \ |
| 609 | defined(MBEDTLS_CIPHER_MODE_XTS) || \ |
| 610 | defined(MBEDTLS_CIPHER_MODE_CFB) || \ |
| 611 | defined(MBEDTLS_CIPHER_MODE_OFB) |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 612 | const unsigned char in[16] = { 0 }; |
| 613 | unsigned char out[16]; |
| 614 | #endif |
Andrzej Kurek | 8ffd8a6 | 2022-09-27 07:54:16 -0400 | [diff] [blame] | 615 | #if defined(MBEDTLS_CIPHER_MODE_CBC) || \ |
| 616 | defined(MBEDTLS_CIPHER_MODE_CFB) || \ |
| 617 | defined(MBEDTLS_CIPHER_MODE_OFB) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 618 | mbedtls_aes_context aes_ctx; |
Andrzej Kurek | 8ffd8a6 | 2022-09-27 07:54:16 -0400 | [diff] [blame] | 619 | #endif |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 620 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
| 621 | mbedtls_aes_xts_context xts_ctx; |
| 622 | #endif |
| 623 | #if defined(MBEDTLS_CIPHER_MODE_CFB) || \ |
| 624 | defined(MBEDTLS_CIPHER_MODE_OFB) |
| 625 | size_t size; |
| 626 | #endif |
| 627 | |
| 628 | /* These calls accept NULL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 629 | TEST_VALID_PARAM(mbedtls_aes_free(NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 630 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 631 | TEST_VALID_PARAM(mbedtls_aes_xts_free(NULL)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 632 | #endif |
| 633 | |
| 634 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 635 | TEST_ASSERT(mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT, |
| 636 | 15, |
| 637 | out, in, out) |
| 638 | == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH); |
| 639 | TEST_ASSERT(mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT, |
| 640 | 17, |
| 641 | out, in, out) |
| 642 | == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 643 | #endif |
| 644 | |
| 645 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 646 | TEST_ASSERT(mbedtls_aes_crypt_xts(&xts_ctx, MBEDTLS_AES_ENCRYPT, |
| 647 | 15, |
| 648 | in, in, out) |
| 649 | == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH); |
| 650 | TEST_ASSERT(mbedtls_aes_crypt_xts(&xts_ctx, MBEDTLS_AES_ENCRYPT, |
| 651 | (1 << 24) + 1, |
| 652 | in, in, out) |
| 653 | == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 654 | #endif |
| 655 | |
| 656 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
| 657 | size = 16; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 658 | TEST_ASSERT(mbedtls_aes_crypt_cfb128(&aes_ctx, MBEDTLS_AES_ENCRYPT, 16, |
| 659 | &size, out, in, out) |
| 660 | == MBEDTLS_ERR_AES_BAD_INPUT_DATA); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 661 | #endif |
| 662 | |
| 663 | #if defined(MBEDTLS_CIPHER_MODE_OFB) |
| 664 | size = 16; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 665 | TEST_ASSERT(mbedtls_aes_crypt_ofb(&aes_ctx, 16, &size, out, in, out) |
| 666 | == MBEDTLS_ERR_AES_BAD_INPUT_DATA); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 667 | #endif |
| 668 | } |
| 669 | /* END_CASE */ |
| 670 | |
Tom Cosgrove | b5eb831 | 2023-03-20 10:57:42 +0000 | [diff] [blame] | 671 | /* BEGIN_CASE */ |
| 672 | void aes_ecb_context_alignment(data_t *key) |
| 673 | { |
| 674 | /* We test alignment multiple times, with different alignments |
| 675 | * of the context and of the plaintext/ciphertext. */ |
| 676 | |
| 677 | struct align0 { |
| 678 | mbedtls_aes_context ctx; |
| 679 | }; |
| 680 | struct align0 *enc0 = NULL; |
| 681 | struct align0 *dec0 = NULL; |
| 682 | |
| 683 | struct align1 { |
| 684 | char bump; |
| 685 | mbedtls_aes_context ctx; |
| 686 | }; |
| 687 | struct align1 *enc1 = NULL; |
| 688 | struct align1 *dec1 = NULL; |
| 689 | |
| 690 | /* All peak alignment */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 691 | TEST_CALLOC(enc0, 1); |
| 692 | TEST_CALLOC(dec0, 1); |
Tom Cosgrove | b5eb831 | 2023-03-20 10:57:42 +0000 | [diff] [blame] | 693 | if (!test_ctx_alignment(key, &enc0->ctx, &dec0->ctx)) { |
| 694 | goto exit; |
| 695 | } |
| 696 | mbedtls_free(enc0); |
| 697 | enc0 = NULL; |
| 698 | mbedtls_free(dec0); |
| 699 | dec0 = NULL; |
| 700 | |
| 701 | /* Enc aligned, dec not */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 702 | TEST_CALLOC(enc0, 1); |
| 703 | TEST_CALLOC(dec1, 1); |
Tom Cosgrove | b5eb831 | 2023-03-20 10:57:42 +0000 | [diff] [blame] | 704 | if (!test_ctx_alignment(key, &enc0->ctx, &dec1->ctx)) { |
| 705 | goto exit; |
| 706 | } |
| 707 | mbedtls_free(enc0); |
| 708 | enc0 = NULL; |
| 709 | mbedtls_free(dec1); |
| 710 | dec1 = NULL; |
| 711 | |
| 712 | /* Dec aligned, enc not */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 713 | TEST_CALLOC(enc1, 1); |
| 714 | TEST_CALLOC(dec0, 1); |
Tom Cosgrove | b5eb831 | 2023-03-20 10:57:42 +0000 | [diff] [blame] | 715 | if (!test_ctx_alignment(key, &enc1->ctx, &dec0->ctx)) { |
| 716 | goto exit; |
| 717 | } |
| 718 | mbedtls_free(enc1); |
| 719 | enc1 = NULL; |
| 720 | mbedtls_free(dec0); |
| 721 | dec0 = NULL; |
| 722 | |
| 723 | /* Both shifted */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 724 | TEST_CALLOC(enc1, 1); |
| 725 | TEST_CALLOC(dec1, 1); |
Tom Cosgrove | b5eb831 | 2023-03-20 10:57:42 +0000 | [diff] [blame] | 726 | if (!test_ctx_alignment(key, &enc1->ctx, &dec1->ctx)) { |
| 727 | goto exit; |
| 728 | } |
| 729 | mbedtls_free(enc1); |
| 730 | enc1 = NULL; |
| 731 | mbedtls_free(dec1); |
| 732 | dec1 = NULL; |
| 733 | |
| 734 | exit: |
| 735 | mbedtls_free(enc0); |
| 736 | mbedtls_free(dec0); |
| 737 | mbedtls_free(enc1); |
| 738 | mbedtls_free(dec1); |
| 739 | } |
| 740 | /* END_CASE */ |
| 741 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 742 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 743 | void aes_selftest() |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 744 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 745 | TEST_ASSERT(mbedtls_aes_self_test(1) == 0); |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 746 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 747 | /* END_CASE */ |