blob: c2b49781379ab52e1ecf5eb979897a39ca6525d2 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/aes.h"
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006 * depends_on:MBEDTLS_AES_C
Paul Bakker33b43f12013-08-20 11:48:36 +02007 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010011void aes_encrypt_ecb( data_t * key_str, data_t * src_str,
12 data_t * hex_dst_string, int setkey_result )
Paul Bakker367dae42009-06-28 21:50:27 +000013{
Paul Bakker367dae42009-06-28 21:50:27 +000014 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015 mbedtls_aes_context ctx;
Paul Bakker367dae42009-06-28 21:50:27 +000016
Paul Bakker367dae42009-06-28 21:50:27 +000017 memset(output, 0x00, 100);
18
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050019 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000020
Azim Khand30ca132017-06-09 04:32:58 +010021 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020022 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000023 {
Azim Khand30ca132017-06-09 04:32:58 +010024 TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_ENCRYPT, src_str->x, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000025
Azim Khand30ca132017-06-09 04:32:58 +010026 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000027 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020028
Paul Bakkerbd51b262014-07-10 15:26:12 +020029exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000031}
Paul Bakker33b43f12013-08-20 11:48:36 +020032/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000033
Paul Bakker33b43f12013-08-20 11:48:36 +020034/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010035void aes_decrypt_ecb( data_t * key_str, data_t * src_str,
36 data_t * hex_dst_string, int setkey_result )
Paul Bakker367dae42009-06-28 21:50:27 +000037{
Paul Bakker367dae42009-06-28 21:50:27 +000038 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 mbedtls_aes_context ctx;
Paul Bakker367dae42009-06-28 21:50:27 +000040
Paul Bakker367dae42009-06-28 21:50:27 +000041 memset(output, 0x00, 100);
42
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050043 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000044
Azim Khand30ca132017-06-09 04:32:58 +010045 TEST_ASSERT( mbedtls_aes_setkey_dec( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020046 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000047 {
Azim Khand30ca132017-06-09 04:32:58 +010048 TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_DECRYPT, src_str->x, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000049
Azim Khand30ca132017-06-09 04:32:58 +010050 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000051 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020052
Paul Bakkerbd51b262014-07-10 15:26:12 +020053exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000055}
Paul Bakker33b43f12013-08-20 11:48:36 +020056/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +010059void aes_encrypt_cbc( data_t * key_str, data_t * iv_str,
60 data_t * src_str, data_t * hex_dst_string,
Azim Khand30ca132017-06-09 04:32:58 +010061 int cbc_result )
Paul Bakker367dae42009-06-28 21:50:27 +000062{
Paul Bakker367dae42009-06-28 21:50:27 +000063 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 mbedtls_aes_context ctx;
Paul Bakker367dae42009-06-28 21:50:27 +000065
Paul Bakker367dae42009-06-28 21:50:27 +000066 memset(output, 0x00, 100);
67
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050068 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000069
Azim Khand30ca132017-06-09 04:32:58 +010070 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
71 TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020072 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +000073 {
Paul Bakker367dae42009-06-28 21:50:27 +000074
Azim Khand30ca132017-06-09 04:32:58 +010075 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000076 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020077
Paul Bakkerbd51b262014-07-10 15:26:12 +020078exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000080}
Paul Bakker33b43f12013-08-20 11:48:36 +020081/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +010084void aes_decrypt_cbc( data_t * key_str, data_t * iv_str,
85 data_t * src_str, data_t * hex_dst_string,
Azim Khand30ca132017-06-09 04:32:58 +010086 int cbc_result )
Paul Bakker367dae42009-06-28 21:50:27 +000087{
Paul Bakker367dae42009-06-28 21:50:27 +000088 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_aes_context ctx;
Paul Bakker367dae42009-06-28 21:50:27 +000090
Paul Bakker367dae42009-06-28 21:50:27 +000091 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000093
Azim Khand30ca132017-06-09 04:32:58 +010094 mbedtls_aes_setkey_dec( &ctx, key_str->x, key_str->len * 8 );
95 TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020096 if( cbc_result == 0)
Paul Bakkerf3ccc682010-03-18 21:21:02 +000097 {
Paul Bakker367dae42009-06-28 21:50:27 +000098
Azim Khand30ca132017-06-09 04:32:58 +010099 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000100 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200101
Paul Bakkerbd51b262014-07-10 15:26:12 +0200102exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000104}
Paul Bakker33b43f12013-08-20 11:48:36 +0200105/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000106
Aorimn5f778012016-06-09 23:22:58 +0200107/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100108void aes_encrypt_xts( char *hex_key_string, char *hex_data_unit_string,
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100109 char *hex_src_string, char *hex_dst_string )
Aorimn5f778012016-06-09 23:22:58 +0200110{
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100111 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 Amero9366feb2018-05-29 18:55:17 +0100117 mbedtls_aes_xts_context ctx;
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100118 size_t key_len, src_len, dst_len, data_unit_len;
Aorimn5f778012016-06-09 23:22:58 +0200119
Jaeden Amero9366feb2018-05-29 18:55:17 +0100120 mbedtls_aes_xts_init( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200121
Ronald Cronf73ab002020-06-10 10:57:28 +0200122 data_unit = mbedtls_test_unhexify_alloc( hex_data_unit_string,
123 &data_unit_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100124 TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
Aorimn5f778012016-06-09 23:22:58 +0200125
Ronald Crona256c702020-06-10 10:53:11 +0200126 key = mbedtls_test_unhexify_alloc( hex_key_string, &key_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100127 TEST_ASSERT( key_len % 2 == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200128
Ronald Crona256c702020-06-10 10:53:11 +0200129 src = mbedtls_test_unhexify_alloc( hex_src_string, &src_len );
130 dst = mbedtls_test_unhexify_alloc( hex_dst_string, &dst_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100131 TEST_ASSERT( src_len == dst_len );
Aorimn5f778012016-06-09 23:22:58 +0200132
Ronald Cron690f3eb2020-06-10 10:42:18 +0200133 output = mbedtls_test_zero_alloc( dst_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100134
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 );
138
139 TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200140
141exit:
Jaeden Amero9366feb2018-05-29 18:55:17 +0100142 mbedtls_aes_xts_free( &ctx );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100143 mbedtls_free( data_unit );
144 mbedtls_free( key );
145 mbedtls_free( src );
146 mbedtls_free( dst );
147 mbedtls_free( output );
Aorimn5f778012016-06-09 23:22:58 +0200148}
149/* END_CASE */
150
151/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100152void aes_decrypt_xts( char *hex_key_string, char *hex_data_unit_string,
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100153 char *hex_dst_string, char *hex_src_string )
Aorimn5f778012016-06-09 23:22:58 +0200154{
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100155 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 Amero9366feb2018-05-29 18:55:17 +0100161 mbedtls_aes_xts_context ctx;
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100162 size_t key_len, src_len, dst_len, data_unit_len;
Aorimn5f778012016-06-09 23:22:58 +0200163
Jaeden Amero9366feb2018-05-29 18:55:17 +0100164 mbedtls_aes_xts_init( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200165
Ronald Cronf73ab002020-06-10 10:57:28 +0200166 data_unit = mbedtls_test_unhexify_alloc( hex_data_unit_string,
167 &data_unit_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100168 TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
Aorimn5f778012016-06-09 23:22:58 +0200169
Ronald Crona256c702020-06-10 10:53:11 +0200170 key = mbedtls_test_unhexify_alloc( hex_key_string, &key_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100171 TEST_ASSERT( key_len % 2 == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200172
Ronald Crona256c702020-06-10 10:53:11 +0200173 src = mbedtls_test_unhexify_alloc( hex_src_string, &src_len );
174 dst = mbedtls_test_unhexify_alloc( hex_dst_string, &dst_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100175 TEST_ASSERT( src_len == dst_len );
Aorimn5f778012016-06-09 23:22:58 +0200176
Ronald Cron690f3eb2020-06-10 10:42:18 +0200177 output = mbedtls_test_zero_alloc( dst_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100178
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 );
182
183 TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200184
185exit:
Jaeden Amero9366feb2018-05-29 18:55:17 +0100186 mbedtls_aes_xts_free( &ctx );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100187 mbedtls_free( data_unit );
188 mbedtls_free( key );
189 mbedtls_free( src );
190 mbedtls_free( dst );
191 mbedtls_free( output );
Aorimn5f778012016-06-09 23:22:58 +0200192}
193/* END_CASE */
194
Jaeden Amero425382d2018-04-28 17:26:25 +0100195/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
196void aes_crypt_xts_size( int size, int retval )
197{
198 mbedtls_aes_xts_context ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500199 const unsigned char src[16] = { 0 };
200 unsigned char output[16];
Jaeden Amero425382d2018-04-28 17:26:25 +0100201 unsigned char data_unit[16];
202 size_t length = size;
203
204 mbedtls_aes_xts_init( &ctx );
205 memset( data_unit, 0x00, sizeof( data_unit ) );
206
207
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500208 /* 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. */
Jaeden Amero425382d2018-04-28 17:26:25 +0100210 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, length, data_unit, src, output ) == retval );
211}
212/* END_CASE */
213
Jaeden Amero142383e2018-05-31 10:40:34 +0100214/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
215void aes_crypt_xts_keysize( int size, int retval )
216{
217 mbedtls_aes_xts_context ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500218 const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
Jaeden Amero142383e2018-05-31 10:40:34 +0100219 size_t key_len = size;
220
221 mbedtls_aes_xts_init( &ctx );
222
223 TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == retval );
224 TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == retval );
225exit:
226 mbedtls_aes_xts_free( &ctx );
227}
228/* END_CASE */
Jaeden Amero425382d2018-04-28 17:26:25 +0100229
230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100232void aes_encrypt_cfb128( data_t * key_str, data_t * iv_str,
233 data_t * src_str, data_t * hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000234{
Paul Bakker367dae42009-06-28 21:50:27 +0000235 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000237 size_t iv_offset = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000238
Paul Bakker367dae42009-06-28 21:50:27 +0000239 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000241
Paul Bakker367dae42009-06-28 21:50:27 +0000242
Azim Khand30ca132017-06-09 04:32:58 +0100243 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
244 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_ENCRYPT, 16, &iv_offset, iv_str->x, src_str->x, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000245
Azim Khand30ca132017-06-09 04:32:58 +0100246 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200247
Paul Bakkerbd51b262014-07-10 15:26:12 +0200248exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000250}
Paul Bakker33b43f12013-08-20 11:48:36 +0200251/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100254void aes_decrypt_cfb128( data_t * key_str, data_t * iv_str,
255 data_t * src_str, data_t * hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000256{
Paul Bakker367dae42009-06-28 21:50:27 +0000257 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000259 size_t iv_offset = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000260
Paul Bakker367dae42009-06-28 21:50:27 +0000261 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000263
Paul Bakker367dae42009-06-28 21:50:27 +0000264
Azim Khand30ca132017-06-09 04:32:58 +0100265 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
266 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_DECRYPT, 16, &iv_offset, iv_str->x, src_str->x, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000267
Azim Khand30ca132017-06-09 04:32:58 +0100268 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200269
Paul Bakkerbd51b262014-07-10 15:26:12 +0200270exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000272}
Paul Bakker33b43f12013-08-20 11:48:36 +0200273/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100276void aes_encrypt_cfb8( data_t * key_str, data_t * iv_str,
277 data_t * src_str, data_t * hex_dst_string )
Paul Bakker556efba2014-01-24 15:38:12 +0100278{
Paul Bakker556efba2014-01-24 15:38:12 +0100279 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100281
Paul Bakker556efba2014-01-24 15:38:12 +0100282 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100284
Paul Bakker556efba2014-01-24 15:38:12 +0100285
Azim Khand30ca132017-06-09 04:32:58 +0100286 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
287 TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == 0 );
Paul Bakker556efba2014-01-24 15:38:12 +0100288
Azim Khand30ca132017-06-09 04:32:58 +0100289 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200290
Paul Bakkerbd51b262014-07-10 15:26:12 +0200291exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100293}
294/* END_CASE */
295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100297void aes_decrypt_cfb8( data_t * key_str, data_t * iv_str,
298 data_t * src_str, data_t * hex_dst_string )
Paul Bakker556efba2014-01-24 15:38:12 +0100299{
Paul Bakker556efba2014-01-24 15:38:12 +0100300 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100302
Paul Bakker556efba2014-01-24 15:38:12 +0100303 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100305
Paul Bakker556efba2014-01-24 15:38:12 +0100306
Azim Khand30ca132017-06-09 04:32:58 +0100307 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
308 TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == 0 );
Paul Bakker556efba2014-01-24 15:38:12 +0100309
Azim Khand30ca132017-06-09 04:32:58 +0100310 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200311
Paul Bakkerbd51b262014-07-10 15:26:12 +0200312exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100314}
315/* END_CASE */
316
Simon Butcher03018842018-04-22 22:57:58 +0100317/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */
318void aes_encrypt_ofb( int fragment_size, char *hex_key_string,
Simon Butcher00131442018-05-22 22:40:36 +0100319 char *hex_iv_string, char *hex_src_string,
320 char *hex_dst_string )
Simon Butcher03018842018-04-22 22:57:58 +0100321{
Simon Butchere416bf92018-06-02 18:28:32 +0100322 unsigned char key_str[32];
323 unsigned char iv_str[16];
324 unsigned char src_str[64];
325 unsigned char dst_str[64];
326 unsigned char output[32];
Simon Butcher03018842018-04-22 22:57:58 +0100327 mbedtls_aes_context ctx;
328 size_t iv_offset = 0;
329 int in_buffer_len;
330 unsigned char* src_str_next;
Simon Butcherdbe7fbf2018-04-29 14:51:35 +0100331 int key_len;
Simon Butcher03018842018-04-22 22:57:58 +0100332
Simon Butcherb7836e12018-06-02 18:36:49 +0100333 memset( key_str, 0x00, sizeof( key_str ) );
334 memset( iv_str, 0x00, sizeof( iv_str ) );
335 memset( src_str, 0x00, sizeof( src_str ) );
336 memset( dst_str, 0x00, sizeof( dst_str ) );
337 memset( output, 0x00, sizeof( output ) );
Simon Butcher03018842018-04-22 22:57:58 +0100338 mbedtls_aes_init( &ctx );
339
Simon Butchere416bf92018-06-02 18:28:32 +0100340 TEST_ASSERT( strlen( hex_key_string ) <= ( 32 * 2 ) );
341 TEST_ASSERT( strlen( hex_iv_string ) <= ( 16 * 2 ) );
342 TEST_ASSERT( strlen( hex_src_string ) <= ( 64 * 2 ) );
343 TEST_ASSERT( strlen( hex_dst_string ) <= ( 64 * 2 ) );
344
Ronald Cron72d628f2020-06-08 17:05:57 +0200345 key_len = mbedtls_test_unhexify( key_str, hex_key_string );
346 mbedtls_test_unhexify( iv_str, hex_iv_string );
347 in_buffer_len = mbedtls_test_unhexify( src_str, hex_src_string );
Simon Butcher03018842018-04-22 22:57:58 +0100348
Simon Butcherad4e4932018-04-29 00:43:47 +0100349 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100350 src_str_next = src_str;
351
352 while( in_buffer_len > 0 )
353 {
354 TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset,
355 iv_str, src_str_next, output ) == 0 );
356
Ronald Cron72d628f2020-06-08 17:05:57 +0200357 mbedtls_test_hexify( dst_str, output, fragment_size );
Simon Butcher03018842018-04-22 22:57:58 +0100358 TEST_ASSERT( strncmp( (char *) dst_str, hex_dst_string,
Simon Butcher00131442018-05-22 22:40:36 +0100359 ( 2 * fragment_size ) ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100360
361 in_buffer_len -= fragment_size;
362 hex_dst_string += ( fragment_size * 2 );
363 src_str_next += fragment_size;
364
365 if( in_buffer_len < fragment_size )
366 fragment_size = in_buffer_len;
367 }
368
369exit:
370 mbedtls_aes_free( &ctx );
371}
372/* END_CASE */
373
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500374/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
375void aes_check_params( )
376{
377 mbedtls_aes_context aes_ctx;
378#if defined(MBEDTLS_CIPHER_MODE_XTS)
379 mbedtls_aes_xts_context xts_ctx;
380#endif
381 const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
382 const unsigned char in[16] = { 0 };
383 unsigned char out[16];
384 size_t size;
385 const int valid_mode = MBEDTLS_AES_ENCRYPT;
386 const int invalid_mode = 42;
387
388 TEST_INVALID_PARAM( mbedtls_aes_init( NULL ) );
389#if defined(MBEDTLS_CIPHER_MODE_XTS)
390 TEST_INVALID_PARAM( mbedtls_aes_xts_init( NULL ) );
391#endif
392
393 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
394 mbedtls_aes_setkey_enc( NULL, key, 128 ) );
395 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
396 mbedtls_aes_setkey_enc( &aes_ctx, NULL, 128 ) );
397
398 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
399 mbedtls_aes_setkey_dec( NULL, key, 128 ) );
400 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
401 mbedtls_aes_setkey_dec( &aes_ctx, NULL, 128 ) );
402
403#if defined(MBEDTLS_CIPHER_MODE_XTS)
404 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
405 mbedtls_aes_xts_setkey_enc( NULL, key, 128 ) );
406 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
407 mbedtls_aes_xts_setkey_enc( &xts_ctx, NULL, 128 ) );
408
409 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
410 mbedtls_aes_xts_setkey_dec( NULL, key, 128 ) );
411 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
412 mbedtls_aes_xts_setkey_dec( &xts_ctx, NULL, 128 ) );
413#endif
414
415
416 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
417 mbedtls_aes_crypt_ecb( NULL,
418 valid_mode, in, out ) );
419 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
420 mbedtls_aes_crypt_ecb( &aes_ctx,
421 invalid_mode, in, out ) );
422 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
423 mbedtls_aes_crypt_ecb( &aes_ctx,
424 valid_mode, NULL, out ) );
425 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
426 mbedtls_aes_crypt_ecb( &aes_ctx,
427 valid_mode, in, NULL ) );
428
429#if defined(MBEDTLS_CIPHER_MODE_CBC)
430 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
431 mbedtls_aes_crypt_cbc( NULL,
432 valid_mode, 16,
433 out, in, out ) );
434 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
435 mbedtls_aes_crypt_cbc( &aes_ctx,
436 invalid_mode, 16,
437 out, in, out ) );
438 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
439 mbedtls_aes_crypt_cbc( &aes_ctx,
440 valid_mode, 16,
441 NULL, in, out ) );
442 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
443 mbedtls_aes_crypt_cbc( &aes_ctx,
444 valid_mode, 16,
445 out, NULL, out ) );
446 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
447 mbedtls_aes_crypt_cbc( &aes_ctx,
448 valid_mode, 16,
449 out, in, NULL ) );
450#endif /* MBEDTLS_CIPHER_MODE_CBC */
451
452#if defined(MBEDTLS_CIPHER_MODE_XTS)
453 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
454 mbedtls_aes_crypt_xts( NULL,
455 valid_mode, 16,
456 in, in, out ) );
457 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
458 mbedtls_aes_crypt_xts( &xts_ctx,
459 invalid_mode, 16,
460 in, in, out ) );
461 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
462 mbedtls_aes_crypt_xts( &xts_ctx,
463 valid_mode, 16,
464 NULL, in, out ) );
465 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
466 mbedtls_aes_crypt_xts( &xts_ctx,
467 valid_mode, 16,
468 in, NULL, out ) );
469 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
470 mbedtls_aes_crypt_xts( &xts_ctx,
471 valid_mode, 16,
472 in, in, NULL ) );
473#endif /* MBEDTLS_CIPHER_MODE_XTS */
474
475#if defined(MBEDTLS_CIPHER_MODE_CFB)
476 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
477 mbedtls_aes_crypt_cfb128( NULL,
478 valid_mode, 16,
479 &size, out, in, out ) );
480 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
481 mbedtls_aes_crypt_cfb128( &aes_ctx,
482 invalid_mode, 16,
483 &size, out, in, out ) );
484 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
485 mbedtls_aes_crypt_cfb128( &aes_ctx,
486 valid_mode, 16,
487 NULL, out, in, out ) );
488 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
489 mbedtls_aes_crypt_cfb128( &aes_ctx,
490 valid_mode, 16,
491 &size, NULL, in, out ) );
492 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
493 mbedtls_aes_crypt_cfb128( &aes_ctx,
494 valid_mode, 16,
495 &size, out, NULL, out ) );
496 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
497 mbedtls_aes_crypt_cfb128( &aes_ctx,
498 valid_mode, 16,
499 &size, out, in, NULL ) );
500
501
502 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
503 mbedtls_aes_crypt_cfb8( NULL,
504 valid_mode, 16,
505 out, in, out ) );
506 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
507 mbedtls_aes_crypt_cfb8( &aes_ctx,
508 invalid_mode, 16,
509 out, in, out ) );
510 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
511 mbedtls_aes_crypt_cfb8( &aes_ctx,
512 valid_mode, 16,
513 NULL, in, out ) );
514 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
515 mbedtls_aes_crypt_cfb8( &aes_ctx,
516 valid_mode, 16,
517 out, NULL, out ) );
518 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
519 mbedtls_aes_crypt_cfb8( &aes_ctx,
520 valid_mode, 16,
521 out, in, NULL ) );
522#endif /* MBEDTLS_CIPHER_MODE_CFB */
523
524#if defined(MBEDTLS_CIPHER_MODE_OFB)
525 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
526 mbedtls_aes_crypt_ofb( NULL, 16,
527 &size, out, in, out ) );
528 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
529 mbedtls_aes_crypt_ofb( &aes_ctx, 16,
530 NULL, out, in, out ) );
531 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
532 mbedtls_aes_crypt_ofb( &aes_ctx, 16,
533 &size, NULL, in, out ) );
534 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
535 mbedtls_aes_crypt_ofb( &aes_ctx, 16,
536 &size, out, NULL, out ) );
537 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
538 mbedtls_aes_crypt_ofb( &aes_ctx, 16,
539 &size, out, in, NULL ) );
540#endif /* MBEDTLS_CIPHER_MODE_OFB */
541
542#if defined(MBEDTLS_CIPHER_MODE_CTR)
543 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
544 mbedtls_aes_crypt_ctr( NULL, 16, &size, out,
545 out, in, out ) );
546 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
547 mbedtls_aes_crypt_ctr( &aes_ctx, 16, NULL, out,
548 out, in, out ) );
549 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
550 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, NULL,
551 out, in, out ) );
552 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
553 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
554 NULL, in, out ) );
555 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
556 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
557 out, NULL, out ) );
558 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
559 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
560 out, in, NULL ) );
561#endif /* MBEDTLS_CIPHER_MODE_CTR */
562}
563/* END_CASE */
564
565/* BEGIN_CASE */
566void aes_misc_params( )
567{
568#if defined(MBEDTLS_CIPHER_MODE_CBC) || \
569 defined(MBEDTLS_CIPHER_MODE_XTS) || \
570 defined(MBEDTLS_CIPHER_MODE_CFB) || \
571 defined(MBEDTLS_CIPHER_MODE_OFB)
572 mbedtls_aes_context aes_ctx;
573 const unsigned char in[16] = { 0 };
574 unsigned char out[16];
575#endif
576#if defined(MBEDTLS_CIPHER_MODE_XTS)
577 mbedtls_aes_xts_context xts_ctx;
578#endif
579#if defined(MBEDTLS_CIPHER_MODE_CFB) || \
580 defined(MBEDTLS_CIPHER_MODE_OFB)
581 size_t size;
582#endif
583
584 /* These calls accept NULL */
585 TEST_VALID_PARAM( mbedtls_aes_free( NULL ) );
586#if defined(MBEDTLS_CIPHER_MODE_XTS)
587 TEST_VALID_PARAM( mbedtls_aes_xts_free( NULL ) );
588#endif
589
590#if defined(MBEDTLS_CIPHER_MODE_CBC)
591 TEST_ASSERT( mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT,
592 15,
593 out, in, out )
594 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
595 TEST_ASSERT( mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT,
596 17,
597 out, in, out )
598 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
599#endif
600
601#if defined(MBEDTLS_CIPHER_MODE_XTS)
602 TEST_ASSERT( mbedtls_aes_crypt_xts( &xts_ctx, MBEDTLS_AES_ENCRYPT,
603 15,
604 in, in, out )
605 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
606 TEST_ASSERT( mbedtls_aes_crypt_xts( &xts_ctx, MBEDTLS_AES_ENCRYPT,
607 (1 << 24) + 1,
608 in, in, out )
609 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
610#endif
611
612#if defined(MBEDTLS_CIPHER_MODE_CFB)
613 size = 16;
614 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &aes_ctx, MBEDTLS_AES_ENCRYPT, 16,
615 &size, out, in, out )
616 == MBEDTLS_ERR_AES_BAD_INPUT_DATA );
617#endif
618
619#if defined(MBEDTLS_CIPHER_MODE_OFB)
620 size = 16;
621 TEST_ASSERT( mbedtls_aes_crypt_ofb( &aes_ctx, 16, &size, out, in, out )
622 == MBEDTLS_ERR_AES_BAD_INPUT_DATA );
623#endif
624}
625/* END_CASE */
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100628void aes_selftest( )
Paul Bakker3d360822009-07-05 11:29:38 +0000629{
Andres AG93012e82016-09-09 09:10:28 +0100630 TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000631}
Paul Bakker33b43f12013-08-20 11:48:36 +0200632/* END_CASE */