blob: d5eb1ef108b458829341737fa9dac7bf9f5e3c72 [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,
Ronald Crondf02eb02020-06-25 13:57:05 +0200332 char *hex_expected_output_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];
Simon Butchere416bf92018-06-02 18:28:32 +0100337 unsigned char output[32];
Ronald Crondf02eb02020-06-25 13:57:05 +0200338 unsigned char output_string[65];
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 ) );
Simon Butcherb7836e12018-06-02 18:36:49 +0100348 memset( output, 0x00, sizeof( output ) );
Ronald Crondf02eb02020-06-25 13:57:05 +0200349 memset( output_string, 0x00, sizeof( output_string ) );
Simon Butcher03018842018-04-22 22:57:58 +0100350 mbedtls_aes_init( &ctx );
351
Ronald Crondf02eb02020-06-25 13:57:05 +0200352 TEST_ASSERT( (size_t)fragment_size < sizeof( output ) );
Simon Butchere416bf92018-06-02 18:28:32 +0100353 TEST_ASSERT( strlen( hex_key_string ) <= ( 32 * 2 ) );
354 TEST_ASSERT( strlen( hex_iv_string ) <= ( 16 * 2 ) );
355 TEST_ASSERT( strlen( hex_src_string ) <= ( 64 * 2 ) );
Ronald Crondf02eb02020-06-25 13:57:05 +0200356 TEST_ASSERT( strlen( hex_expected_output_string ) <= ( 64 * 2 ) );
Simon Butchere416bf92018-06-02 18:28:32 +0100357
Ronald Cron72d628f2020-06-08 17:05:57 +0200358 key_len = mbedtls_test_unhexify( key_str, hex_key_string );
359 mbedtls_test_unhexify( iv_str, hex_iv_string );
360 in_buffer_len = mbedtls_test_unhexify( src_str, hex_src_string );
Simon Butcher03018842018-04-22 22:57:58 +0100361
Simon Butcherad4e4932018-04-29 00:43:47 +0100362 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100363 src_str_next = src_str;
364
365 while( in_buffer_len > 0 )
366 {
367 TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset,
368 iv_str, src_str_next, output ) == 0 );
369
Ronald Crondf02eb02020-06-25 13:57:05 +0200370 mbedtls_test_hexify( output_string, output, fragment_size );
371 TEST_ASSERT( strncmp( (char *) output_string, hex_expected_output_string,
Simon Butcher00131442018-05-22 22:40:36 +0100372 ( 2 * fragment_size ) ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100373
374 in_buffer_len -= fragment_size;
Ronald Crondf02eb02020-06-25 13:57:05 +0200375 hex_expected_output_string += ( fragment_size * 2 );
Simon Butcher03018842018-04-22 22:57:58 +0100376 src_str_next += fragment_size;
377
378 if( in_buffer_len < fragment_size )
379 fragment_size = in_buffer_len;
380 }
381
382exit:
383 mbedtls_aes_free( &ctx );
384}
385/* END_CASE */
386
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500387/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
388void aes_check_params( )
389{
390 mbedtls_aes_context aes_ctx;
391#if defined(MBEDTLS_CIPHER_MODE_XTS)
392 mbedtls_aes_xts_context xts_ctx;
393#endif
394 const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
395 const unsigned char in[16] = { 0 };
396 unsigned char out[16];
397 size_t size;
398 const int valid_mode = MBEDTLS_AES_ENCRYPT;
399 const int invalid_mode = 42;
400
401 TEST_INVALID_PARAM( mbedtls_aes_init( NULL ) );
402#if defined(MBEDTLS_CIPHER_MODE_XTS)
403 TEST_INVALID_PARAM( mbedtls_aes_xts_init( NULL ) );
404#endif
405
406 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
407 mbedtls_aes_setkey_enc( NULL, key, 128 ) );
408 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
409 mbedtls_aes_setkey_enc( &aes_ctx, NULL, 128 ) );
410
411 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
412 mbedtls_aes_setkey_dec( NULL, key, 128 ) );
413 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
414 mbedtls_aes_setkey_dec( &aes_ctx, NULL, 128 ) );
415
416#if defined(MBEDTLS_CIPHER_MODE_XTS)
417 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
418 mbedtls_aes_xts_setkey_enc( NULL, key, 128 ) );
419 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
420 mbedtls_aes_xts_setkey_enc( &xts_ctx, NULL, 128 ) );
421
422 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
423 mbedtls_aes_xts_setkey_dec( NULL, key, 128 ) );
424 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
425 mbedtls_aes_xts_setkey_dec( &xts_ctx, NULL, 128 ) );
426#endif
427
428
429 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
430 mbedtls_aes_crypt_ecb( NULL,
431 valid_mode, in, out ) );
432 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
433 mbedtls_aes_crypt_ecb( &aes_ctx,
434 invalid_mode, in, out ) );
435 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
436 mbedtls_aes_crypt_ecb( &aes_ctx,
437 valid_mode, NULL, out ) );
438 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
439 mbedtls_aes_crypt_ecb( &aes_ctx,
440 valid_mode, in, NULL ) );
441
442#if defined(MBEDTLS_CIPHER_MODE_CBC)
443 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
444 mbedtls_aes_crypt_cbc( NULL,
445 valid_mode, 16,
446 out, in, out ) );
447 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
448 mbedtls_aes_crypt_cbc( &aes_ctx,
449 invalid_mode, 16,
450 out, in, out ) );
451 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
452 mbedtls_aes_crypt_cbc( &aes_ctx,
453 valid_mode, 16,
454 NULL, in, out ) );
455 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
456 mbedtls_aes_crypt_cbc( &aes_ctx,
457 valid_mode, 16,
458 out, NULL, out ) );
459 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
460 mbedtls_aes_crypt_cbc( &aes_ctx,
461 valid_mode, 16,
462 out, in, NULL ) );
463#endif /* MBEDTLS_CIPHER_MODE_CBC */
464
465#if defined(MBEDTLS_CIPHER_MODE_XTS)
466 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
467 mbedtls_aes_crypt_xts( NULL,
468 valid_mode, 16,
469 in, in, out ) );
470 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
471 mbedtls_aes_crypt_xts( &xts_ctx,
472 invalid_mode, 16,
473 in, in, out ) );
474 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
475 mbedtls_aes_crypt_xts( &xts_ctx,
476 valid_mode, 16,
477 NULL, in, out ) );
478 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
479 mbedtls_aes_crypt_xts( &xts_ctx,
480 valid_mode, 16,
481 in, NULL, out ) );
482 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
483 mbedtls_aes_crypt_xts( &xts_ctx,
484 valid_mode, 16,
485 in, in, NULL ) );
486#endif /* MBEDTLS_CIPHER_MODE_XTS */
487
488#if defined(MBEDTLS_CIPHER_MODE_CFB)
489 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
490 mbedtls_aes_crypt_cfb128( NULL,
491 valid_mode, 16,
492 &size, out, in, out ) );
493 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
494 mbedtls_aes_crypt_cfb128( &aes_ctx,
495 invalid_mode, 16,
496 &size, out, in, out ) );
497 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
498 mbedtls_aes_crypt_cfb128( &aes_ctx,
499 valid_mode, 16,
500 NULL, out, in, out ) );
501 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
502 mbedtls_aes_crypt_cfb128( &aes_ctx,
503 valid_mode, 16,
504 &size, NULL, in, out ) );
505 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
506 mbedtls_aes_crypt_cfb128( &aes_ctx,
507 valid_mode, 16,
508 &size, out, NULL, out ) );
509 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
510 mbedtls_aes_crypt_cfb128( &aes_ctx,
511 valid_mode, 16,
512 &size, out, in, NULL ) );
513
514
515 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
516 mbedtls_aes_crypt_cfb8( NULL,
517 valid_mode, 16,
518 out, in, out ) );
519 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
520 mbedtls_aes_crypt_cfb8( &aes_ctx,
521 invalid_mode, 16,
522 out, in, out ) );
523 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
524 mbedtls_aes_crypt_cfb8( &aes_ctx,
525 valid_mode, 16,
526 NULL, in, out ) );
527 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
528 mbedtls_aes_crypt_cfb8( &aes_ctx,
529 valid_mode, 16,
530 out, NULL, out ) );
531 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
532 mbedtls_aes_crypt_cfb8( &aes_ctx,
533 valid_mode, 16,
534 out, in, NULL ) );
535#endif /* MBEDTLS_CIPHER_MODE_CFB */
536
537#if defined(MBEDTLS_CIPHER_MODE_OFB)
538 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
539 mbedtls_aes_crypt_ofb( NULL, 16,
540 &size, out, in, out ) );
541 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
542 mbedtls_aes_crypt_ofb( &aes_ctx, 16,
543 NULL, out, in, out ) );
544 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
545 mbedtls_aes_crypt_ofb( &aes_ctx, 16,
546 &size, NULL, in, out ) );
547 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
548 mbedtls_aes_crypt_ofb( &aes_ctx, 16,
549 &size, out, NULL, out ) );
550 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
551 mbedtls_aes_crypt_ofb( &aes_ctx, 16,
552 &size, out, in, NULL ) );
553#endif /* MBEDTLS_CIPHER_MODE_OFB */
554
555#if defined(MBEDTLS_CIPHER_MODE_CTR)
556 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
557 mbedtls_aes_crypt_ctr( NULL, 16, &size, out,
558 out, in, out ) );
559 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
560 mbedtls_aes_crypt_ctr( &aes_ctx, 16, NULL, out,
561 out, in, out ) );
562 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
563 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, NULL,
564 out, in, out ) );
565 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
566 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
567 NULL, in, out ) );
568 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
569 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
570 out, NULL, out ) );
571 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
572 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
573 out, in, NULL ) );
574#endif /* MBEDTLS_CIPHER_MODE_CTR */
575}
576/* END_CASE */
577
578/* BEGIN_CASE */
579void aes_misc_params( )
580{
581#if defined(MBEDTLS_CIPHER_MODE_CBC) || \
582 defined(MBEDTLS_CIPHER_MODE_XTS) || \
583 defined(MBEDTLS_CIPHER_MODE_CFB) || \
584 defined(MBEDTLS_CIPHER_MODE_OFB)
585 mbedtls_aes_context aes_ctx;
586 const unsigned char in[16] = { 0 };
587 unsigned char out[16];
588#endif
589#if defined(MBEDTLS_CIPHER_MODE_XTS)
590 mbedtls_aes_xts_context xts_ctx;
591#endif
592#if defined(MBEDTLS_CIPHER_MODE_CFB) || \
593 defined(MBEDTLS_CIPHER_MODE_OFB)
594 size_t size;
595#endif
596
597 /* These calls accept NULL */
598 TEST_VALID_PARAM( mbedtls_aes_free( NULL ) );
599#if defined(MBEDTLS_CIPHER_MODE_XTS)
600 TEST_VALID_PARAM( mbedtls_aes_xts_free( NULL ) );
601#endif
602
603#if defined(MBEDTLS_CIPHER_MODE_CBC)
604 TEST_ASSERT( mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT,
605 15,
606 out, in, out )
607 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
608 TEST_ASSERT( mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT,
609 17,
610 out, in, out )
611 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
612#endif
613
614#if defined(MBEDTLS_CIPHER_MODE_XTS)
615 TEST_ASSERT( mbedtls_aes_crypt_xts( &xts_ctx, MBEDTLS_AES_ENCRYPT,
616 15,
617 in, in, out )
618 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
619 TEST_ASSERT( mbedtls_aes_crypt_xts( &xts_ctx, MBEDTLS_AES_ENCRYPT,
620 (1 << 24) + 1,
621 in, in, out )
622 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
623#endif
624
625#if defined(MBEDTLS_CIPHER_MODE_CFB)
626 size = 16;
627 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &aes_ctx, MBEDTLS_AES_ENCRYPT, 16,
628 &size, out, in, out )
629 == MBEDTLS_ERR_AES_BAD_INPUT_DATA );
630#endif
631
632#if defined(MBEDTLS_CIPHER_MODE_OFB)
633 size = 16;
634 TEST_ASSERT( mbedtls_aes_crypt_ofb( &aes_ctx, 16, &size, out, in, out )
635 == MBEDTLS_ERR_AES_BAD_INPUT_DATA );
636#endif
637}
638/* END_CASE */
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100641void aes_selftest( )
Paul Bakker3d360822009-07-05 11:29:38 +0000642{
Andres AG93012e82016-09-09 09:10:28 +0100643 TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000644}
Paul Bakker33b43f12013-08-20 11:48:36 +0200645/* END_CASE */