blob: f1be3cec2f0cf67ba963144597a2f98e491e98f2 [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
Ronald Cron2dbba992020-06-10 11:42:32 +020026 TEST_ASSERT( mbedtls_test_hexcmp( output, hex_dst_string->x,
27 16, hex_dst_string->len ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000028 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020029
Paul Bakkerbd51b262014-07-10 15:26:12 +020030exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000032}
Paul Bakker33b43f12013-08-20 11:48:36 +020033/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000034
Paul Bakker33b43f12013-08-20 11:48:36 +020035/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010036void aes_decrypt_ecb( data_t * key_str, data_t * src_str,
37 data_t * hex_dst_string, int setkey_result )
Paul Bakker367dae42009-06-28 21:50:27 +000038{
Paul Bakker367dae42009-06-28 21:50:27 +000039 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 mbedtls_aes_context ctx;
Paul Bakker367dae42009-06-28 21:50:27 +000041
Paul Bakker367dae42009-06-28 21:50:27 +000042 memset(output, 0x00, 100);
43
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050044 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000045
Azim Khand30ca132017-06-09 04:32:58 +010046 TEST_ASSERT( mbedtls_aes_setkey_dec( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020047 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000048 {
Azim Khand30ca132017-06-09 04:32:58 +010049 TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_DECRYPT, src_str->x, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000050
Ronald Cron2dbba992020-06-10 11:42:32 +020051 TEST_ASSERT( mbedtls_test_hexcmp( output, hex_dst_string->x,
52 16, hex_dst_string->len ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000053 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020054
Paul Bakkerbd51b262014-07-10 15:26:12 +020055exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000057}
Paul Bakker33b43f12013-08-20 11:48:36 +020058/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +010061void aes_encrypt_cbc( data_t * key_str, data_t * iv_str,
62 data_t * src_str, data_t * hex_dst_string,
Azim Khand30ca132017-06-09 04:32:58 +010063 int cbc_result )
Paul Bakker367dae42009-06-28 21:50:27 +000064{
Paul Bakker367dae42009-06-28 21:50:27 +000065 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 mbedtls_aes_context ctx;
Paul Bakker367dae42009-06-28 21:50:27 +000067
Paul Bakker367dae42009-06-28 21:50:27 +000068 memset(output, 0x00, 100);
69
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050070 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000071
Azim Khand30ca132017-06-09 04:32:58 +010072 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
73 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 +020074 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +000075 {
Paul Bakker367dae42009-06-28 21:50:27 +000076
Ronald Cron2dbba992020-06-10 11:42:32 +020077 TEST_ASSERT( mbedtls_test_hexcmp( output, hex_dst_string->x,
78 src_str->len,
79 hex_dst_string->len ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000080 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020081
Paul Bakkerbd51b262014-07-10 15:26:12 +020082exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000084}
Paul Bakker33b43f12013-08-20 11:48:36 +020085/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +010088void aes_decrypt_cbc( data_t * key_str, data_t * iv_str,
89 data_t * src_str, data_t * hex_dst_string,
Azim Khand30ca132017-06-09 04:32:58 +010090 int cbc_result )
Paul Bakker367dae42009-06-28 21:50:27 +000091{
Paul Bakker367dae42009-06-28 21:50:27 +000092 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_aes_context ctx;
Paul Bakker367dae42009-06-28 21:50:27 +000094
Paul Bakker367dae42009-06-28 21:50:27 +000095 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000097
Azim Khand30ca132017-06-09 04:32:58 +010098 mbedtls_aes_setkey_dec( &ctx, key_str->x, key_str->len * 8 );
99 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 +0200100 if( cbc_result == 0)
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000101 {
Paul Bakker367dae42009-06-28 21:50:27 +0000102
Ronald Cron2dbba992020-06-10 11:42:32 +0200103 TEST_ASSERT( mbedtls_test_hexcmp( output, hex_dst_string->x,
104 src_str->len,
105 hex_dst_string->len ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000106 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200107
Paul Bakkerbd51b262014-07-10 15:26:12 +0200108exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000110}
Paul Bakker33b43f12013-08-20 11:48:36 +0200111/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000112
Aorimn5f778012016-06-09 23:22:58 +0200113/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100114void aes_encrypt_xts( char *hex_key_string, char *hex_data_unit_string,
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100115 char *hex_src_string, char *hex_dst_string )
Aorimn5f778012016-06-09 23:22:58 +0200116{
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100117 enum { AES_BLOCK_SIZE = 16 };
118 unsigned char *data_unit = NULL;
119 unsigned char *key = NULL;
120 unsigned char *src = NULL;
121 unsigned char *dst = NULL;
122 unsigned char *output = NULL;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100123 mbedtls_aes_xts_context ctx;
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100124 size_t key_len, src_len, dst_len, data_unit_len;
Aorimn5f778012016-06-09 23:22:58 +0200125
Jaeden Amero9366feb2018-05-29 18:55:17 +0100126 mbedtls_aes_xts_init( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200127
Ronald Cronf73ab002020-06-10 10:57:28 +0200128 data_unit = mbedtls_test_unhexify_alloc( hex_data_unit_string,
129 &data_unit_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100130 TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
Aorimn5f778012016-06-09 23:22:58 +0200131
Ronald Crona256c702020-06-10 10:53:11 +0200132 key = mbedtls_test_unhexify_alloc( hex_key_string, &key_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100133 TEST_ASSERT( key_len % 2 == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200134
Ronald Crona256c702020-06-10 10:53:11 +0200135 src = mbedtls_test_unhexify_alloc( hex_src_string, &src_len );
136 dst = mbedtls_test_unhexify_alloc( hex_dst_string, &dst_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100137 TEST_ASSERT( src_len == dst_len );
Aorimn5f778012016-06-09 23:22:58 +0200138
Ronald Cron690f3eb2020-06-10 10:42:18 +0200139 output = mbedtls_test_zero_alloc( dst_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100140
141 TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == 0 );
142 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, src_len,
143 data_unit, src, output ) == 0 );
144
145 TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200146
147exit:
Jaeden Amero9366feb2018-05-29 18:55:17 +0100148 mbedtls_aes_xts_free( &ctx );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100149 mbedtls_free( data_unit );
150 mbedtls_free( key );
151 mbedtls_free( src );
152 mbedtls_free( dst );
153 mbedtls_free( output );
Aorimn5f778012016-06-09 23:22:58 +0200154}
155/* END_CASE */
156
157/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100158void aes_decrypt_xts( char *hex_key_string, char *hex_data_unit_string,
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100159 char *hex_dst_string, char *hex_src_string )
Aorimn5f778012016-06-09 23:22:58 +0200160{
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100161 enum { AES_BLOCK_SIZE = 16 };
162 unsigned char *data_unit = NULL;
163 unsigned char *key = NULL;
164 unsigned char *src = NULL;
165 unsigned char *dst = NULL;
166 unsigned char *output = NULL;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100167 mbedtls_aes_xts_context ctx;
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100168 size_t key_len, src_len, dst_len, data_unit_len;
Aorimn5f778012016-06-09 23:22:58 +0200169
Jaeden Amero9366feb2018-05-29 18:55:17 +0100170 mbedtls_aes_xts_init( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200171
Ronald Cronf73ab002020-06-10 10:57:28 +0200172 data_unit = mbedtls_test_unhexify_alloc( hex_data_unit_string,
173 &data_unit_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100174 TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
Aorimn5f778012016-06-09 23:22:58 +0200175
Ronald Crona256c702020-06-10 10:53:11 +0200176 key = mbedtls_test_unhexify_alloc( hex_key_string, &key_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100177 TEST_ASSERT( key_len % 2 == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200178
Ronald Crona256c702020-06-10 10:53:11 +0200179 src = mbedtls_test_unhexify_alloc( hex_src_string, &src_len );
180 dst = mbedtls_test_unhexify_alloc( hex_dst_string, &dst_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100181 TEST_ASSERT( src_len == dst_len );
Aorimn5f778012016-06-09 23:22:58 +0200182
Ronald Cron690f3eb2020-06-10 10:42:18 +0200183 output = mbedtls_test_zero_alloc( dst_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100184
185 TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == 0 );
186 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_DECRYPT, src_len,
187 data_unit, src, output ) == 0 );
188
189 TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200190
191exit:
Jaeden Amero9366feb2018-05-29 18:55:17 +0100192 mbedtls_aes_xts_free( &ctx );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100193 mbedtls_free( data_unit );
194 mbedtls_free( key );
195 mbedtls_free( src );
196 mbedtls_free( dst );
197 mbedtls_free( output );
Aorimn5f778012016-06-09 23:22:58 +0200198}
199/* END_CASE */
200
Jaeden Amero425382d2018-04-28 17:26:25 +0100201/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
202void aes_crypt_xts_size( int size, int retval )
203{
204 mbedtls_aes_xts_context ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500205 const unsigned char src[16] = { 0 };
206 unsigned char output[16];
Jaeden Amero425382d2018-04-28 17:26:25 +0100207 unsigned char data_unit[16];
208 size_t length = size;
209
210 mbedtls_aes_xts_init( &ctx );
211 memset( data_unit, 0x00, sizeof( data_unit ) );
212
213
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500214 /* Valid pointers are passed for builds with MBEDTLS_CHECK_PARAMS, as
215 * otherwise we wouldn't get to the size check we're interested in. */
Jaeden Amero425382d2018-04-28 17:26:25 +0100216 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, length, data_unit, src, output ) == retval );
217}
218/* END_CASE */
219
Jaeden Amero142383e2018-05-31 10:40:34 +0100220/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
221void aes_crypt_xts_keysize( int size, int retval )
222{
223 mbedtls_aes_xts_context ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500224 const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
Jaeden Amero142383e2018-05-31 10:40:34 +0100225 size_t key_len = size;
226
227 mbedtls_aes_xts_init( &ctx );
228
229 TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == retval );
230 TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == retval );
231exit:
232 mbedtls_aes_xts_free( &ctx );
233}
234/* END_CASE */
Jaeden Amero425382d2018-04-28 17:26:25 +0100235
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100238void aes_encrypt_cfb128( data_t * key_str, data_t * iv_str,
239 data_t * src_str, data_t * hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000240{
Paul Bakker367dae42009-06-28 21:50:27 +0000241 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000243 size_t iv_offset = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000244
Paul Bakker367dae42009-06-28 21:50:27 +0000245 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000247
Paul Bakker367dae42009-06-28 21:50:27 +0000248
Azim Khand30ca132017-06-09 04:32:58 +0100249 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
250 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 +0000251
Ronald Cron2dbba992020-06-10 11:42:32 +0200252 TEST_ASSERT( mbedtls_test_hexcmp( output, hex_dst_string->x,
253 16, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200254
Paul Bakkerbd51b262014-07-10 15:26:12 +0200255exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000257}
Paul Bakker33b43f12013-08-20 11:48:36 +0200258/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100261void aes_decrypt_cfb128( data_t * key_str, data_t * iv_str,
262 data_t * src_str, data_t * hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000263{
Paul Bakker367dae42009-06-28 21:50:27 +0000264 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000266 size_t iv_offset = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000267
Paul Bakker367dae42009-06-28 21:50:27 +0000268 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000270
Paul Bakker367dae42009-06-28 21:50:27 +0000271
Azim Khand30ca132017-06-09 04:32:58 +0100272 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
273 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 +0000274
Ronald Cron2dbba992020-06-10 11:42:32 +0200275 TEST_ASSERT( mbedtls_test_hexcmp( output, hex_dst_string->x,
276 16, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200277
Paul Bakkerbd51b262014-07-10 15:26:12 +0200278exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000280}
Paul Bakker33b43f12013-08-20 11:48:36 +0200281/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100284void aes_encrypt_cfb8( data_t * key_str, data_t * iv_str,
285 data_t * src_str, data_t * hex_dst_string )
Paul Bakker556efba2014-01-24 15:38:12 +0100286{
Paul Bakker556efba2014-01-24 15:38:12 +0100287 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100289
Paul Bakker556efba2014-01-24 15:38:12 +0100290 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100292
Paul Bakker556efba2014-01-24 15:38:12 +0100293
Azim Khand30ca132017-06-09 04:32:58 +0100294 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
295 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 +0100296
Ronald Cron2dbba992020-06-10 11:42:32 +0200297 TEST_ASSERT( mbedtls_test_hexcmp( output, hex_dst_string->x,
298 src_str->len,
299 hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200300
Paul Bakkerbd51b262014-07-10 15:26:12 +0200301exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100303}
304/* END_CASE */
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100307void aes_decrypt_cfb8( data_t * key_str, data_t * iv_str,
308 data_t * src_str, data_t * hex_dst_string )
Paul Bakker556efba2014-01-24 15:38:12 +0100309{
Paul Bakker556efba2014-01-24 15:38:12 +0100310 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100312
Paul Bakker556efba2014-01-24 15:38:12 +0100313 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100315
Paul Bakker556efba2014-01-24 15:38:12 +0100316
Azim Khand30ca132017-06-09 04:32:58 +0100317 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
318 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 +0100319
Ronald Cron2dbba992020-06-10 11:42:32 +0200320 TEST_ASSERT( mbedtls_test_hexcmp( output, hex_dst_string->x,
321 src_str->len,
322 hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200323
Paul Bakkerbd51b262014-07-10 15:26:12 +0200324exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100326}
327/* END_CASE */
328
Simon Butcher03018842018-04-22 22:57:58 +0100329/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */
330void aes_encrypt_ofb( int fragment_size, char *hex_key_string,
Simon Butcher00131442018-05-22 22:40:36 +0100331 char *hex_iv_string, char *hex_src_string,
332 char *hex_dst_string )
Simon Butcher03018842018-04-22 22:57:58 +0100333{
Simon Butchere416bf92018-06-02 18:28:32 +0100334 unsigned char key_str[32];
335 unsigned char iv_str[16];
336 unsigned char src_str[64];
337 unsigned char dst_str[64];
338 unsigned char output[32];
Simon Butcher03018842018-04-22 22:57:58 +0100339 mbedtls_aes_context ctx;
340 size_t iv_offset = 0;
341 int in_buffer_len;
342 unsigned char* src_str_next;
Simon Butcherdbe7fbf2018-04-29 14:51:35 +0100343 int key_len;
Simon Butcher03018842018-04-22 22:57:58 +0100344
Simon Butcherb7836e12018-06-02 18:36:49 +0100345 memset( key_str, 0x00, sizeof( key_str ) );
346 memset( iv_str, 0x00, sizeof( iv_str ) );
347 memset( src_str, 0x00, sizeof( src_str ) );
348 memset( dst_str, 0x00, sizeof( dst_str ) );
349 memset( output, 0x00, sizeof( output ) );
Simon Butcher03018842018-04-22 22:57:58 +0100350 mbedtls_aes_init( &ctx );
351
Simon Butchere416bf92018-06-02 18:28:32 +0100352 TEST_ASSERT( strlen( hex_key_string ) <= ( 32 * 2 ) );
353 TEST_ASSERT( strlen( hex_iv_string ) <= ( 16 * 2 ) );
354 TEST_ASSERT( strlen( hex_src_string ) <= ( 64 * 2 ) );
355 TEST_ASSERT( strlen( hex_dst_string ) <= ( 64 * 2 ) );
356
Ronald Cron72d628f2020-06-08 17:05:57 +0200357 key_len = mbedtls_test_unhexify( key_str, hex_key_string );
358 mbedtls_test_unhexify( iv_str, hex_iv_string );
359 in_buffer_len = mbedtls_test_unhexify( src_str, hex_src_string );
Simon Butcher03018842018-04-22 22:57:58 +0100360
Simon Butcherad4e4932018-04-29 00:43:47 +0100361 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100362 src_str_next = src_str;
363
364 while( in_buffer_len > 0 )
365 {
366 TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset,
367 iv_str, src_str_next, output ) == 0 );
368
Ronald Cron72d628f2020-06-08 17:05:57 +0200369 mbedtls_test_hexify( dst_str, output, fragment_size );
Simon Butcher03018842018-04-22 22:57:58 +0100370 TEST_ASSERT( strncmp( (char *) dst_str, hex_dst_string,
Simon Butcher00131442018-05-22 22:40:36 +0100371 ( 2 * fragment_size ) ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100372
373 in_buffer_len -= fragment_size;
374 hex_dst_string += ( fragment_size * 2 );
375 src_str_next += fragment_size;
376
377 if( in_buffer_len < fragment_size )
378 fragment_size = in_buffer_len;
379 }
380
381exit:
382 mbedtls_aes_free( &ctx );
383}
384/* END_CASE */
385
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500386/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
387void aes_check_params( )
388{
389 mbedtls_aes_context aes_ctx;
390#if defined(MBEDTLS_CIPHER_MODE_XTS)
391 mbedtls_aes_xts_context xts_ctx;
392#endif
393 const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
394 const unsigned char in[16] = { 0 };
395 unsigned char out[16];
396 size_t size;
397 const int valid_mode = MBEDTLS_AES_ENCRYPT;
398 const int invalid_mode = 42;
399
400 TEST_INVALID_PARAM( mbedtls_aes_init( NULL ) );
401#if defined(MBEDTLS_CIPHER_MODE_XTS)
402 TEST_INVALID_PARAM( mbedtls_aes_xts_init( NULL ) );
403#endif
404
405 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
406 mbedtls_aes_setkey_enc( NULL, key, 128 ) );
407 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
408 mbedtls_aes_setkey_enc( &aes_ctx, NULL, 128 ) );
409
410 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
411 mbedtls_aes_setkey_dec( NULL, key, 128 ) );
412 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
413 mbedtls_aes_setkey_dec( &aes_ctx, NULL, 128 ) );
414
415#if defined(MBEDTLS_CIPHER_MODE_XTS)
416 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
417 mbedtls_aes_xts_setkey_enc( NULL, key, 128 ) );
418 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
419 mbedtls_aes_xts_setkey_enc( &xts_ctx, NULL, 128 ) );
420
421 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
422 mbedtls_aes_xts_setkey_dec( NULL, key, 128 ) );
423 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
424 mbedtls_aes_xts_setkey_dec( &xts_ctx, NULL, 128 ) );
425#endif
426
427
428 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
429 mbedtls_aes_crypt_ecb( NULL,
430 valid_mode, in, out ) );
431 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
432 mbedtls_aes_crypt_ecb( &aes_ctx,
433 invalid_mode, in, out ) );
434 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
435 mbedtls_aes_crypt_ecb( &aes_ctx,
436 valid_mode, NULL, out ) );
437 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
438 mbedtls_aes_crypt_ecb( &aes_ctx,
439 valid_mode, in, NULL ) );
440
441#if defined(MBEDTLS_CIPHER_MODE_CBC)
442 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
443 mbedtls_aes_crypt_cbc( NULL,
444 valid_mode, 16,
445 out, in, out ) );
446 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
447 mbedtls_aes_crypt_cbc( &aes_ctx,
448 invalid_mode, 16,
449 out, in, out ) );
450 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
451 mbedtls_aes_crypt_cbc( &aes_ctx,
452 valid_mode, 16,
453 NULL, in, out ) );
454 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
455 mbedtls_aes_crypt_cbc( &aes_ctx,
456 valid_mode, 16,
457 out, NULL, out ) );
458 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
459 mbedtls_aes_crypt_cbc( &aes_ctx,
460 valid_mode, 16,
461 out, in, NULL ) );
462#endif /* MBEDTLS_CIPHER_MODE_CBC */
463
464#if defined(MBEDTLS_CIPHER_MODE_XTS)
465 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
466 mbedtls_aes_crypt_xts( NULL,
467 valid_mode, 16,
468 in, in, out ) );
469 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
470 mbedtls_aes_crypt_xts( &xts_ctx,
471 invalid_mode, 16,
472 in, in, out ) );
473 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
474 mbedtls_aes_crypt_xts( &xts_ctx,
475 valid_mode, 16,
476 NULL, in, out ) );
477 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
478 mbedtls_aes_crypt_xts( &xts_ctx,
479 valid_mode, 16,
480 in, NULL, out ) );
481 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
482 mbedtls_aes_crypt_xts( &xts_ctx,
483 valid_mode, 16,
484 in, in, NULL ) );
485#endif /* MBEDTLS_CIPHER_MODE_XTS */
486
487#if defined(MBEDTLS_CIPHER_MODE_CFB)
488 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
489 mbedtls_aes_crypt_cfb128( NULL,
490 valid_mode, 16,
491 &size, out, in, out ) );
492 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
493 mbedtls_aes_crypt_cfb128( &aes_ctx,
494 invalid_mode, 16,
495 &size, out, in, out ) );
496 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
497 mbedtls_aes_crypt_cfb128( &aes_ctx,
498 valid_mode, 16,
499 NULL, out, in, out ) );
500 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
501 mbedtls_aes_crypt_cfb128( &aes_ctx,
502 valid_mode, 16,
503 &size, NULL, in, out ) );
504 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
505 mbedtls_aes_crypt_cfb128( &aes_ctx,
506 valid_mode, 16,
507 &size, out, NULL, out ) );
508 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
509 mbedtls_aes_crypt_cfb128( &aes_ctx,
510 valid_mode, 16,
511 &size, out, in, NULL ) );
512
513
514 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
515 mbedtls_aes_crypt_cfb8( NULL,
516 valid_mode, 16,
517 out, in, out ) );
518 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
519 mbedtls_aes_crypt_cfb8( &aes_ctx,
520 invalid_mode, 16,
521 out, in, out ) );
522 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
523 mbedtls_aes_crypt_cfb8( &aes_ctx,
524 valid_mode, 16,
525 NULL, in, out ) );
526 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
527 mbedtls_aes_crypt_cfb8( &aes_ctx,
528 valid_mode, 16,
529 out, NULL, out ) );
530 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
531 mbedtls_aes_crypt_cfb8( &aes_ctx,
532 valid_mode, 16,
533 out, in, NULL ) );
534#endif /* MBEDTLS_CIPHER_MODE_CFB */
535
536#if defined(MBEDTLS_CIPHER_MODE_OFB)
537 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
538 mbedtls_aes_crypt_ofb( NULL, 16,
539 &size, out, in, out ) );
540 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
541 mbedtls_aes_crypt_ofb( &aes_ctx, 16,
542 NULL, out, in, out ) );
543 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
544 mbedtls_aes_crypt_ofb( &aes_ctx, 16,
545 &size, NULL, in, out ) );
546 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
547 mbedtls_aes_crypt_ofb( &aes_ctx, 16,
548 &size, out, NULL, out ) );
549 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
550 mbedtls_aes_crypt_ofb( &aes_ctx, 16,
551 &size, out, in, NULL ) );
552#endif /* MBEDTLS_CIPHER_MODE_OFB */
553
554#if defined(MBEDTLS_CIPHER_MODE_CTR)
555 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
556 mbedtls_aes_crypt_ctr( NULL, 16, &size, out,
557 out, in, out ) );
558 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
559 mbedtls_aes_crypt_ctr( &aes_ctx, 16, NULL, out,
560 out, in, out ) );
561 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
562 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, NULL,
563 out, in, out ) );
564 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
565 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
566 NULL, in, out ) );
567 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
568 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
569 out, NULL, out ) );
570 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
571 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
572 out, in, NULL ) );
573#endif /* MBEDTLS_CIPHER_MODE_CTR */
574}
575/* END_CASE */
576
577/* BEGIN_CASE */
578void aes_misc_params( )
579{
580#if defined(MBEDTLS_CIPHER_MODE_CBC) || \
581 defined(MBEDTLS_CIPHER_MODE_XTS) || \
582 defined(MBEDTLS_CIPHER_MODE_CFB) || \
583 defined(MBEDTLS_CIPHER_MODE_OFB)
584 mbedtls_aes_context aes_ctx;
585 const unsigned char in[16] = { 0 };
586 unsigned char out[16];
587#endif
588#if defined(MBEDTLS_CIPHER_MODE_XTS)
589 mbedtls_aes_xts_context xts_ctx;
590#endif
591#if defined(MBEDTLS_CIPHER_MODE_CFB) || \
592 defined(MBEDTLS_CIPHER_MODE_OFB)
593 size_t size;
594#endif
595
596 /* These calls accept NULL */
597 TEST_VALID_PARAM( mbedtls_aes_free( NULL ) );
598#if defined(MBEDTLS_CIPHER_MODE_XTS)
599 TEST_VALID_PARAM( mbedtls_aes_xts_free( NULL ) );
600#endif
601
602#if defined(MBEDTLS_CIPHER_MODE_CBC)
603 TEST_ASSERT( mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT,
604 15,
605 out, in, out )
606 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
607 TEST_ASSERT( mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT,
608 17,
609 out, in, out )
610 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
611#endif
612
613#if defined(MBEDTLS_CIPHER_MODE_XTS)
614 TEST_ASSERT( mbedtls_aes_crypt_xts( &xts_ctx, MBEDTLS_AES_ENCRYPT,
615 15,
616 in, in, out )
617 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
618 TEST_ASSERT( mbedtls_aes_crypt_xts( &xts_ctx, MBEDTLS_AES_ENCRYPT,
619 (1 << 24) + 1,
620 in, in, out )
621 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
622#endif
623
624#if defined(MBEDTLS_CIPHER_MODE_CFB)
625 size = 16;
626 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &aes_ctx, MBEDTLS_AES_ENCRYPT, 16,
627 &size, out, in, out )
628 == MBEDTLS_ERR_AES_BAD_INPUT_DATA );
629#endif
630
631#if defined(MBEDTLS_CIPHER_MODE_OFB)
632 size = 16;
633 TEST_ASSERT( mbedtls_aes_crypt_ofb( &aes_ctx, 16, &size, out, in, out )
634 == MBEDTLS_ERR_AES_BAD_INPUT_DATA );
635#endif
636}
637/* END_CASE */
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100640void aes_selftest( )
Paul Bakker3d360822009-07-05 11:29:38 +0000641{
Andres AG93012e82016-09-09 09:10:28 +0100642 TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000643}
Paul Bakker33b43f12013-08-20 11:48:36 +0200644/* END_CASE */