blob: f61f71c3e91c2ba1d80d44b4b719df8ddb64a625 [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
Simon Butcher249b3d62018-12-09 22:18:46 +000019 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
Simon Butcher249b3d62018-12-09 22:18:46 +000043 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
Simon Butcher249b3d62018-12-09 22:18:46 +000068 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
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100122 data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len );
123 TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
Aorimn5f778012016-06-09 23:22:58 +0200124
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100125 key = unhexify_alloc( hex_key_string, &key_len );
126 TEST_ASSERT( key_len % 2 == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200127
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100128 src = unhexify_alloc( hex_src_string, &src_len );
129 dst = unhexify_alloc( hex_dst_string, &dst_len );
130 TEST_ASSERT( src_len == dst_len );
Aorimn5f778012016-06-09 23:22:58 +0200131
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100132 output = zero_alloc( dst_len );
133
134 TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == 0 );
135 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, src_len,
136 data_unit, src, output ) == 0 );
137
138 TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200139
140exit:
Jaeden Amero9366feb2018-05-29 18:55:17 +0100141 mbedtls_aes_xts_free( &ctx );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100142 mbedtls_free( data_unit );
143 mbedtls_free( key );
144 mbedtls_free( src );
145 mbedtls_free( dst );
146 mbedtls_free( output );
Aorimn5f778012016-06-09 23:22:58 +0200147}
148/* END_CASE */
149
150/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100151void aes_decrypt_xts( char *hex_key_string, char *hex_data_unit_string,
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100152 char *hex_dst_string, char *hex_src_string )
Aorimn5f778012016-06-09 23:22:58 +0200153{
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100154 enum { AES_BLOCK_SIZE = 16 };
155 unsigned char *data_unit = NULL;
156 unsigned char *key = NULL;
157 unsigned char *src = NULL;
158 unsigned char *dst = NULL;
159 unsigned char *output = NULL;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100160 mbedtls_aes_xts_context ctx;
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100161 size_t key_len, src_len, dst_len, data_unit_len;
Aorimn5f778012016-06-09 23:22:58 +0200162
Jaeden Amero9366feb2018-05-29 18:55:17 +0100163 mbedtls_aes_xts_init( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200164
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100165 data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len );
166 TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
Aorimn5f778012016-06-09 23:22:58 +0200167
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100168 key = unhexify_alloc( hex_key_string, &key_len );
169 TEST_ASSERT( key_len % 2 == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200170
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100171 src = unhexify_alloc( hex_src_string, &src_len );
172 dst = unhexify_alloc( hex_dst_string, &dst_len );
173 TEST_ASSERT( src_len == dst_len );
Aorimn5f778012016-06-09 23:22:58 +0200174
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100175 output = zero_alloc( dst_len );
176
177 TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == 0 );
178 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_DECRYPT, src_len,
179 data_unit, src, output ) == 0 );
180
181 TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200182
183exit:
Jaeden Amero9366feb2018-05-29 18:55:17 +0100184 mbedtls_aes_xts_free( &ctx );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100185 mbedtls_free( data_unit );
186 mbedtls_free( key );
187 mbedtls_free( src );
188 mbedtls_free( dst );
189 mbedtls_free( output );
Aorimn5f778012016-06-09 23:22:58 +0200190}
191/* END_CASE */
192
Jaeden Amero425382d2018-04-28 17:26:25 +0100193/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
194void aes_crypt_xts_size( int size, int retval )
195{
196 mbedtls_aes_xts_context ctx;
197 const unsigned char *src = NULL;
198 unsigned char *output = NULL;
199 unsigned char data_unit[16];
200 size_t length = size;
201
202 mbedtls_aes_xts_init( &ctx );
203 memset( data_unit, 0x00, sizeof( data_unit ) );
204
205
206 /* Note that this function will most likely crash on failure, as NULL
207 * parameters will be used. In the passing case, the length check in
208 * mbedtls_aes_crypt_xts() will prevent any accesses to parameters by
209 * exiting the function early. */
210 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;
218 const unsigned char *key = NULL;
219 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
Simon Butcher03018842018-04-22 22:57:58 +0100345 key_len = unhexify( key_str, hex_key_string );
Simon Butcherdbe7fbf2018-04-29 14:51:35 +0100346 unhexify( iv_str, hex_iv_string );
Simon Butcher03018842018-04-22 22:57:58 +0100347 in_buffer_len = unhexify( src_str, hex_src_string );
348
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
357 hexify( dst_str, output, fragment_size );
358 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
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100374/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
Simon Butchera6463452018-12-06 17:41:56 +0000375void aes_invalid_param( )
376{
377 mbedtls_aes_context dummy_ctx;
378 const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
379
380 TEST_INVALID_PARAM( mbedtls_aes_init( NULL ) );
381
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100382 TEST_INVALID_PARAM( mbedtls_aes_xts_init( NULL ) );
383
Simon Butchera6463452018-12-06 17:41:56 +0000384 /* mbedtls_aes_setkey_enc() */
385 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
386 mbedtls_aes_setkey_enc( NULL, key, 128 ) );
387
388 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
389 mbedtls_aes_setkey_enc( &dummy_ctx, NULL, 128 ) );
390
391 /* mbedtls_aes_setkey_dec() */
392 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
393 mbedtls_aes_setkey_dec( NULL, key, 128 ) );
394
395 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
396 mbedtls_aes_setkey_dec( &dummy_ctx, NULL, 128 ) );
397
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100398 /* These calls accept NULL */
399 TEST_VALID_PARAM( mbedtls_aes_free( NULL ) );
400 TEST_VALID_PARAM( mbedtls_aes_xts_free( NULL ) );
401
Simon Butchera6463452018-12-06 17:41:56 +0000402exit:
403 return;
404}
405/* END_CASE */
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100408void aes_selftest( )
Paul Bakker3d360822009-07-05 11:29:38 +0000409{
Andres AG93012e82016-09-09 09:10:28 +0100410 TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000411}
Paul Bakker33b43f12013-08-20 11:48:36 +0200412/* END_CASE */