blob: 1d453db19bb8554aa7b263529ead3524bfa70e69 [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 */
Ronald Cron9ed40732020-06-25 09:03:34 +0200330void aes_encrypt_ofb( int fragment_size, data_t *key_str,
331 data_t *iv_str, data_t *src_str,
332 char *expected_output_string)
Simon Butcher03018842018-04-22 22:57:58 +0100333{
Simon Butchere416bf92018-06-02 18:28:32 +0100334 unsigned char output[32];
Ronald Crondf02eb02020-06-25 13:57:05 +0200335 unsigned char output_string[65];
Simon Butcher03018842018-04-22 22:57:58 +0100336 mbedtls_aes_context ctx;
337 size_t iv_offset = 0;
338 int in_buffer_len;
339 unsigned char* src_str_next;
Simon Butcher03018842018-04-22 22:57:58 +0100340
Simon Butcherb7836e12018-06-02 18:36:49 +0100341 memset( output, 0x00, sizeof( output ) );
Ronald Crondf02eb02020-06-25 13:57:05 +0200342 memset( output_string, 0x00, sizeof( output_string ) );
Simon Butcher03018842018-04-22 22:57:58 +0100343 mbedtls_aes_init( &ctx );
344
Ronald Crondf02eb02020-06-25 13:57:05 +0200345 TEST_ASSERT( (size_t)fragment_size < sizeof( output ) );
Simon Butchere416bf92018-06-02 18:28:32 +0100346
Ronald Cron9ed40732020-06-25 09:03:34 +0200347 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str->x,
348 key_str->len * 8 ) == 0 );
349 in_buffer_len = src_str->len;
350 src_str_next = src_str->x;
Simon Butcher03018842018-04-22 22:57:58 +0100351
352 while( in_buffer_len > 0 )
353 {
354 TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset,
Ronald Cron9ed40732020-06-25 09:03:34 +0200355 iv_str->x, src_str_next, output ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100356
Ronald Crondf02eb02020-06-25 13:57:05 +0200357 mbedtls_test_hexify( output_string, output, fragment_size );
Ronald Cron9ed40732020-06-25 09:03:34 +0200358 TEST_ASSERT( strncmp( (char *) output_string, expected_output_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;
Ronald Cron9ed40732020-06-25 09:03:34 +0200362 expected_output_string += ( fragment_size * 2 );
Simon Butcher03018842018-04-22 22:57:58 +0100363 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 */