blob: 887cee84d347eaf5f3f544a435380d49b77ea27b [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/aes.h"
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006 * depends_on:MBEDTLS_AES_C
Paul Bakker33b43f12013-08-20 11:48:36 +02007 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010011void aes_encrypt_ecb( data_t * key_str, data_t * src_str,
12 data_t * hex_dst_string, int setkey_result )
Paul Bakker367dae42009-06-28 21:50:27 +000013{
Paul Bakker367dae42009-06-28 21:50:27 +000014 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015 mbedtls_aes_context ctx;
Paul Bakker367dae42009-06-28 21:50:27 +000016
Paul Bakker367dae42009-06-28 21:50:27 +000017 memset(output, 0x00, 100);
18
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050019 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000020
Azim Khand30ca132017-06-09 04:32:58 +010021 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020022 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000023 {
Azim Khand30ca132017-06-09 04:32:58 +010024 TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_ENCRYPT, src_str->x, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000025
Azim Khand30ca132017-06-09 04:32:58 +010026 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000027 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020028
Paul Bakkerbd51b262014-07-10 15:26:12 +020029exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000031}
Paul Bakker33b43f12013-08-20 11:48:36 +020032/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000033
Paul Bakker33b43f12013-08-20 11:48:36 +020034/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010035void aes_decrypt_ecb( data_t * key_str, data_t * src_str,
36 data_t * hex_dst_string, int setkey_result )
Paul Bakker367dae42009-06-28 21:50:27 +000037{
Paul Bakker367dae42009-06-28 21:50:27 +000038 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 mbedtls_aes_context ctx;
Paul Bakker367dae42009-06-28 21:50:27 +000040
Paul Bakker367dae42009-06-28 21:50:27 +000041 memset(output, 0x00, 100);
42
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050043 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000044
Azim Khand30ca132017-06-09 04:32:58 +010045 TEST_ASSERT( mbedtls_aes_setkey_dec( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020046 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000047 {
Azim Khand30ca132017-06-09 04:32:58 +010048 TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_DECRYPT, src_str->x, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000049
Azim Khand30ca132017-06-09 04:32:58 +010050 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000051 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020052
Paul Bakkerbd51b262014-07-10 15:26:12 +020053exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000055}
Paul Bakker33b43f12013-08-20 11:48:36 +020056/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +010059void aes_encrypt_cbc( data_t * key_str, data_t * iv_str,
60 data_t * src_str, data_t * hex_dst_string,
Azim Khand30ca132017-06-09 04:32:58 +010061 int cbc_result )
Paul Bakker367dae42009-06-28 21:50:27 +000062{
Paul Bakker367dae42009-06-28 21:50:27 +000063 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 mbedtls_aes_context ctx;
Paul Bakker367dae42009-06-28 21:50:27 +000065
Paul Bakker367dae42009-06-28 21:50:27 +000066 memset(output, 0x00, 100);
67
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050068 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000069
Azim Khand30ca132017-06-09 04:32:58 +010070 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
71 TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020072 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +000073 {
Paul Bakker367dae42009-06-28 21:50:27 +000074
Azim Khand30ca132017-06-09 04:32:58 +010075 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000076 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020077
Paul Bakkerbd51b262014-07-10 15:26:12 +020078exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000080}
Paul Bakker33b43f12013-08-20 11:48:36 +020081/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +010084void aes_decrypt_cbc( data_t * key_str, data_t * iv_str,
85 data_t * src_str, data_t * hex_dst_string,
Azim Khand30ca132017-06-09 04:32:58 +010086 int cbc_result )
Paul Bakker367dae42009-06-28 21:50:27 +000087{
Paul Bakker367dae42009-06-28 21:50:27 +000088 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_aes_context ctx;
Paul Bakker367dae42009-06-28 21:50:27 +000090
Paul Bakker367dae42009-06-28 21:50:27 +000091 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000093
Azim Khand30ca132017-06-09 04:32:58 +010094 mbedtls_aes_setkey_dec( &ctx, key_str->x, key_str->len * 8 );
95 TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020096 if( cbc_result == 0)
Paul Bakkerf3ccc682010-03-18 21:21:02 +000097 {
Paul Bakker367dae42009-06-28 21:50:27 +000098
Azim Khand30ca132017-06-09 04:32:58 +010099 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000100 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200101
Paul Bakkerbd51b262014-07-10 15:26:12 +0200102exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000104}
Paul Bakker33b43f12013-08-20 11:48:36 +0200105/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000106
Aorimn5f778012016-06-09 23:22:58 +0200107/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100108void aes_encrypt_xts( char *hex_key_string, char *hex_data_unit_string,
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100109 char *hex_src_string, char *hex_dst_string )
Aorimn5f778012016-06-09 23:22:58 +0200110{
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100111 enum { AES_BLOCK_SIZE = 16 };
112 unsigned char *data_unit = NULL;
113 unsigned char *key = NULL;
114 unsigned char *src = NULL;
115 unsigned char *dst = NULL;
116 unsigned char *output = NULL;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100117 mbedtls_aes_xts_context ctx;
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100118 size_t key_len, src_len, dst_len, data_unit_len;
Aorimn5f778012016-06-09 23:22:58 +0200119
Jaeden Amero9366feb2018-05-29 18:55:17 +0100120 mbedtls_aes_xts_init( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200121
Ronald Crona256c702020-06-10 10:53:11 +0200122 data_unit = mbedtls_test_unhexify_alloc( hex_data_unit_string, &data_unit_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100123 TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
Aorimn5f778012016-06-09 23:22:58 +0200124
Ronald Crona256c702020-06-10 10:53:11 +0200125 key = mbedtls_test_unhexify_alloc( hex_key_string, &key_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100126 TEST_ASSERT( key_len % 2 == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200127
Ronald Crona256c702020-06-10 10:53:11 +0200128 src = mbedtls_test_unhexify_alloc( hex_src_string, &src_len );
129 dst = mbedtls_test_unhexify_alloc( hex_dst_string, &dst_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100130 TEST_ASSERT( src_len == dst_len );
Aorimn5f778012016-06-09 23:22:58 +0200131
Ronald Cron690f3eb2020-06-10 10:42:18 +0200132 output = mbedtls_test_zero_alloc( dst_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100133
134 TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == 0 );
135 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, src_len,
136 data_unit, src, output ) == 0 );
137
138 TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200139
140exit:
Jaeden Amero9366feb2018-05-29 18:55:17 +0100141 mbedtls_aes_xts_free( &ctx );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100142 mbedtls_free( data_unit );
143 mbedtls_free( key );
144 mbedtls_free( src );
145 mbedtls_free( dst );
146 mbedtls_free( output );
Aorimn5f778012016-06-09 23:22:58 +0200147}
148/* END_CASE */
149
150/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100151void aes_decrypt_xts( char *hex_key_string, char *hex_data_unit_string,
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100152 char *hex_dst_string, char *hex_src_string )
Aorimn5f778012016-06-09 23:22:58 +0200153{
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100154 enum { AES_BLOCK_SIZE = 16 };
155 unsigned char *data_unit = NULL;
156 unsigned char *key = NULL;
157 unsigned char *src = NULL;
158 unsigned char *dst = NULL;
159 unsigned char *output = NULL;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100160 mbedtls_aes_xts_context ctx;
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100161 size_t key_len, src_len, dst_len, data_unit_len;
Aorimn5f778012016-06-09 23:22:58 +0200162
Jaeden Amero9366feb2018-05-29 18:55:17 +0100163 mbedtls_aes_xts_init( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200164
Ronald Crona256c702020-06-10 10:53:11 +0200165 data_unit = mbedtls_test_unhexify_alloc( hex_data_unit_string, &data_unit_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100166 TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
Aorimn5f778012016-06-09 23:22:58 +0200167
Ronald Crona256c702020-06-10 10:53:11 +0200168 key = mbedtls_test_unhexify_alloc( hex_key_string, &key_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100169 TEST_ASSERT( key_len % 2 == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200170
Ronald Crona256c702020-06-10 10:53:11 +0200171 src = mbedtls_test_unhexify_alloc( hex_src_string, &src_len );
172 dst = mbedtls_test_unhexify_alloc( hex_dst_string, &dst_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100173 TEST_ASSERT( src_len == dst_len );
Aorimn5f778012016-06-09 23:22:58 +0200174
Ronald Cron690f3eb2020-06-10 10:42:18 +0200175 output = mbedtls_test_zero_alloc( dst_len );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100176
177 TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == 0 );
178 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_DECRYPT, src_len,
179 data_unit, src, output ) == 0 );
180
181 TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200182
183exit:
Jaeden Amero9366feb2018-05-29 18:55:17 +0100184 mbedtls_aes_xts_free( &ctx );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100185 mbedtls_free( data_unit );
186 mbedtls_free( key );
187 mbedtls_free( src );
188 mbedtls_free( dst );
189 mbedtls_free( output );
Aorimn5f778012016-06-09 23:22:58 +0200190}
191/* END_CASE */
192
Jaeden Amero425382d2018-04-28 17:26:25 +0100193/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
194void aes_crypt_xts_size( int size, int retval )
195{
196 mbedtls_aes_xts_context ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500197 const unsigned char src[16] = { 0 };
198 unsigned char output[16];
Jaeden Amero425382d2018-04-28 17:26:25 +0100199 unsigned char data_unit[16];
200 size_t length = size;
201
202 mbedtls_aes_xts_init( &ctx );
203 memset( data_unit, 0x00, sizeof( data_unit ) );
204
205
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500206 /* Valid pointers are passed for builds with MBEDTLS_CHECK_PARAMS, as
207 * otherwise we wouldn't get to the size check we're interested in. */
Jaeden Amero425382d2018-04-28 17:26:25 +0100208 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, length, data_unit, src, output ) == retval );
209}
210/* END_CASE */
211
Jaeden Amero142383e2018-05-31 10:40:34 +0100212/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
213void aes_crypt_xts_keysize( int size, int retval )
214{
215 mbedtls_aes_xts_context ctx;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500216 const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
Jaeden Amero142383e2018-05-31 10:40:34 +0100217 size_t key_len = size;
218
219 mbedtls_aes_xts_init( &ctx );
220
221 TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == retval );
222 TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == retval );
223exit:
224 mbedtls_aes_xts_free( &ctx );
225}
226/* END_CASE */
Jaeden Amero425382d2018-04-28 17:26:25 +0100227
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100230void aes_encrypt_cfb128( data_t * key_str, data_t * iv_str,
231 data_t * src_str, data_t * hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000232{
Paul Bakker367dae42009-06-28 21:50:27 +0000233 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000235 size_t iv_offset = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000236
Paul Bakker367dae42009-06-28 21:50:27 +0000237 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000239
Paul Bakker367dae42009-06-28 21:50:27 +0000240
Azim Khand30ca132017-06-09 04:32:58 +0100241 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
242 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 +0000243
Azim Khand30ca132017-06-09 04:32:58 +0100244 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200245
Paul Bakkerbd51b262014-07-10 15:26:12 +0200246exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000248}
Paul Bakker33b43f12013-08-20 11:48:36 +0200249/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100252void aes_decrypt_cfb128( data_t * key_str, data_t * iv_str,
253 data_t * src_str, data_t * hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000254{
Paul Bakker367dae42009-06-28 21:50:27 +0000255 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000257 size_t iv_offset = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000258
Paul Bakker367dae42009-06-28 21:50:27 +0000259 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000261
Paul Bakker367dae42009-06-28 21:50:27 +0000262
Azim Khand30ca132017-06-09 04:32:58 +0100263 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
264 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 +0000265
Azim Khand30ca132017-06-09 04:32:58 +0100266 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200267
Paul Bakkerbd51b262014-07-10 15:26:12 +0200268exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000270}
Paul Bakker33b43f12013-08-20 11:48:36 +0200271/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100274void aes_encrypt_cfb8( data_t * key_str, data_t * iv_str,
275 data_t * src_str, data_t * hex_dst_string )
Paul Bakker556efba2014-01-24 15:38:12 +0100276{
Paul Bakker556efba2014-01-24 15:38:12 +0100277 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100279
Paul Bakker556efba2014-01-24 15:38:12 +0100280 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100282
Paul Bakker556efba2014-01-24 15:38:12 +0100283
Azim Khand30ca132017-06-09 04:32:58 +0100284 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
285 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 +0100286
Azim Khand30ca132017-06-09 04:32:58 +0100287 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200288
Paul Bakkerbd51b262014-07-10 15:26:12 +0200289exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100291}
292/* END_CASE */
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100295void aes_decrypt_cfb8( data_t * key_str, data_t * iv_str,
296 data_t * src_str, data_t * hex_dst_string )
Paul Bakker556efba2014-01-24 15:38:12 +0100297{
Paul Bakker556efba2014-01-24 15:38:12 +0100298 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100300
Paul Bakker556efba2014-01-24 15:38:12 +0100301 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100303
Paul Bakker556efba2014-01-24 15:38:12 +0100304
Azim Khand30ca132017-06-09 04:32:58 +0100305 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
306 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 +0100307
Azim Khand30ca132017-06-09 04:32:58 +0100308 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200309
Paul Bakkerbd51b262014-07-10 15:26:12 +0200310exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100312}
313/* END_CASE */
314
Simon Butcher03018842018-04-22 22:57:58 +0100315/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */
316void aes_encrypt_ofb( int fragment_size, char *hex_key_string,
Simon Butcher00131442018-05-22 22:40:36 +0100317 char *hex_iv_string, char *hex_src_string,
318 char *hex_dst_string )
Simon Butcher03018842018-04-22 22:57:58 +0100319{
Simon Butchere416bf92018-06-02 18:28:32 +0100320 unsigned char key_str[32];
321 unsigned char iv_str[16];
322 unsigned char src_str[64];
323 unsigned char dst_str[64];
324 unsigned char output[32];
Simon Butcher03018842018-04-22 22:57:58 +0100325 mbedtls_aes_context ctx;
326 size_t iv_offset = 0;
327 int in_buffer_len;
328 unsigned char* src_str_next;
Simon Butcherdbe7fbf2018-04-29 14:51:35 +0100329 int key_len;
Simon Butcher03018842018-04-22 22:57:58 +0100330
Simon Butcherb7836e12018-06-02 18:36:49 +0100331 memset( key_str, 0x00, sizeof( key_str ) );
332 memset( iv_str, 0x00, sizeof( iv_str ) );
333 memset( src_str, 0x00, sizeof( src_str ) );
334 memset( dst_str, 0x00, sizeof( dst_str ) );
335 memset( output, 0x00, sizeof( output ) );
Simon Butcher03018842018-04-22 22:57:58 +0100336 mbedtls_aes_init( &ctx );
337
Simon Butchere416bf92018-06-02 18:28:32 +0100338 TEST_ASSERT( strlen( hex_key_string ) <= ( 32 * 2 ) );
339 TEST_ASSERT( strlen( hex_iv_string ) <= ( 16 * 2 ) );
340 TEST_ASSERT( strlen( hex_src_string ) <= ( 64 * 2 ) );
341 TEST_ASSERT( strlen( hex_dst_string ) <= ( 64 * 2 ) );
342
Ronald Cron72d628f2020-06-08 17:05:57 +0200343 key_len = mbedtls_test_unhexify( key_str, hex_key_string );
344 mbedtls_test_unhexify( iv_str, hex_iv_string );
345 in_buffer_len = mbedtls_test_unhexify( src_str, hex_src_string );
Simon Butcher03018842018-04-22 22:57:58 +0100346
Simon Butcherad4e4932018-04-29 00:43:47 +0100347 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100348 src_str_next = src_str;
349
350 while( in_buffer_len > 0 )
351 {
352 TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset,
353 iv_str, src_str_next, output ) == 0 );
354
Ronald Cron72d628f2020-06-08 17:05:57 +0200355 mbedtls_test_hexify( dst_str, output, fragment_size );
Simon Butcher03018842018-04-22 22:57:58 +0100356 TEST_ASSERT( strncmp( (char *) dst_str, hex_dst_string,
Simon Butcher00131442018-05-22 22:40:36 +0100357 ( 2 * fragment_size ) ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100358
359 in_buffer_len -= fragment_size;
360 hex_dst_string += ( fragment_size * 2 );
361 src_str_next += fragment_size;
362
363 if( in_buffer_len < fragment_size )
364 fragment_size = in_buffer_len;
365 }
366
367exit:
368 mbedtls_aes_free( &ctx );
369}
370/* END_CASE */
371
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500372/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
373void aes_check_params( )
374{
375 mbedtls_aes_context aes_ctx;
376#if defined(MBEDTLS_CIPHER_MODE_XTS)
377 mbedtls_aes_xts_context xts_ctx;
378#endif
379 const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
380 const unsigned char in[16] = { 0 };
381 unsigned char out[16];
382 size_t size;
383 const int valid_mode = MBEDTLS_AES_ENCRYPT;
384 const int invalid_mode = 42;
385
386 TEST_INVALID_PARAM( mbedtls_aes_init( NULL ) );
387#if defined(MBEDTLS_CIPHER_MODE_XTS)
388 TEST_INVALID_PARAM( mbedtls_aes_xts_init( NULL ) );
389#endif
390
391 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
392 mbedtls_aes_setkey_enc( NULL, key, 128 ) );
393 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
394 mbedtls_aes_setkey_enc( &aes_ctx, NULL, 128 ) );
395
396 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
397 mbedtls_aes_setkey_dec( NULL, key, 128 ) );
398 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
399 mbedtls_aes_setkey_dec( &aes_ctx, NULL, 128 ) );
400
401#if defined(MBEDTLS_CIPHER_MODE_XTS)
402 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
403 mbedtls_aes_xts_setkey_enc( NULL, key, 128 ) );
404 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
405 mbedtls_aes_xts_setkey_enc( &xts_ctx, NULL, 128 ) );
406
407 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
408 mbedtls_aes_xts_setkey_dec( NULL, key, 128 ) );
409 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
410 mbedtls_aes_xts_setkey_dec( &xts_ctx, NULL, 128 ) );
411#endif
412
413
414 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
415 mbedtls_aes_crypt_ecb( NULL,
416 valid_mode, in, out ) );
417 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
418 mbedtls_aes_crypt_ecb( &aes_ctx,
419 invalid_mode, in, out ) );
420 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
421 mbedtls_aes_crypt_ecb( &aes_ctx,
422 valid_mode, NULL, out ) );
423 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
424 mbedtls_aes_crypt_ecb( &aes_ctx,
425 valid_mode, in, NULL ) );
426
427#if defined(MBEDTLS_CIPHER_MODE_CBC)
428 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
429 mbedtls_aes_crypt_cbc( NULL,
430 valid_mode, 16,
431 out, in, out ) );
432 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
433 mbedtls_aes_crypt_cbc( &aes_ctx,
434 invalid_mode, 16,
435 out, in, out ) );
436 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
437 mbedtls_aes_crypt_cbc( &aes_ctx,
438 valid_mode, 16,
439 NULL, in, out ) );
440 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
441 mbedtls_aes_crypt_cbc( &aes_ctx,
442 valid_mode, 16,
443 out, NULL, out ) );
444 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
445 mbedtls_aes_crypt_cbc( &aes_ctx,
446 valid_mode, 16,
447 out, in, NULL ) );
448#endif /* MBEDTLS_CIPHER_MODE_CBC */
449
450#if defined(MBEDTLS_CIPHER_MODE_XTS)
451 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
452 mbedtls_aes_crypt_xts( NULL,
453 valid_mode, 16,
454 in, in, out ) );
455 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
456 mbedtls_aes_crypt_xts( &xts_ctx,
457 invalid_mode, 16,
458 in, in, out ) );
459 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
460 mbedtls_aes_crypt_xts( &xts_ctx,
461 valid_mode, 16,
462 NULL, in, out ) );
463 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
464 mbedtls_aes_crypt_xts( &xts_ctx,
465 valid_mode, 16,
466 in, NULL, out ) );
467 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
468 mbedtls_aes_crypt_xts( &xts_ctx,
469 valid_mode, 16,
470 in, in, NULL ) );
471#endif /* MBEDTLS_CIPHER_MODE_XTS */
472
473#if defined(MBEDTLS_CIPHER_MODE_CFB)
474 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
475 mbedtls_aes_crypt_cfb128( NULL,
476 valid_mode, 16,
477 &size, out, in, out ) );
478 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
479 mbedtls_aes_crypt_cfb128( &aes_ctx,
480 invalid_mode, 16,
481 &size, out, in, out ) );
482 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
483 mbedtls_aes_crypt_cfb128( &aes_ctx,
484 valid_mode, 16,
485 NULL, out, in, out ) );
486 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
487 mbedtls_aes_crypt_cfb128( &aes_ctx,
488 valid_mode, 16,
489 &size, NULL, in, out ) );
490 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
491 mbedtls_aes_crypt_cfb128( &aes_ctx,
492 valid_mode, 16,
493 &size, out, NULL, out ) );
494 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
495 mbedtls_aes_crypt_cfb128( &aes_ctx,
496 valid_mode, 16,
497 &size, out, in, NULL ) );
498
499
500 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
501 mbedtls_aes_crypt_cfb8( NULL,
502 valid_mode, 16,
503 out, in, out ) );
504 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
505 mbedtls_aes_crypt_cfb8( &aes_ctx,
506 invalid_mode, 16,
507 out, in, out ) );
508 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
509 mbedtls_aes_crypt_cfb8( &aes_ctx,
510 valid_mode, 16,
511 NULL, in, out ) );
512 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
513 mbedtls_aes_crypt_cfb8( &aes_ctx,
514 valid_mode, 16,
515 out, NULL, out ) );
516 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
517 mbedtls_aes_crypt_cfb8( &aes_ctx,
518 valid_mode, 16,
519 out, in, NULL ) );
520#endif /* MBEDTLS_CIPHER_MODE_CFB */
521
522#if defined(MBEDTLS_CIPHER_MODE_OFB)
523 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
524 mbedtls_aes_crypt_ofb( NULL, 16,
525 &size, out, in, out ) );
526 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
527 mbedtls_aes_crypt_ofb( &aes_ctx, 16,
528 NULL, out, in, out ) );
529 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
530 mbedtls_aes_crypt_ofb( &aes_ctx, 16,
531 &size, NULL, in, out ) );
532 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
533 mbedtls_aes_crypt_ofb( &aes_ctx, 16,
534 &size, out, NULL, out ) );
535 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
536 mbedtls_aes_crypt_ofb( &aes_ctx, 16,
537 &size, out, in, NULL ) );
538#endif /* MBEDTLS_CIPHER_MODE_OFB */
539
540#if defined(MBEDTLS_CIPHER_MODE_CTR)
541 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
542 mbedtls_aes_crypt_ctr( NULL, 16, &size, out,
543 out, in, out ) );
544 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
545 mbedtls_aes_crypt_ctr( &aes_ctx, 16, NULL, out,
546 out, in, out ) );
547 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
548 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, NULL,
549 out, in, out ) );
550 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
551 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
552 NULL, in, out ) );
553 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
554 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
555 out, NULL, out ) );
556 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
557 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
558 out, in, NULL ) );
559#endif /* MBEDTLS_CIPHER_MODE_CTR */
560}
561/* END_CASE */
562
563/* BEGIN_CASE */
564void aes_misc_params( )
565{
566#if defined(MBEDTLS_CIPHER_MODE_CBC) || \
567 defined(MBEDTLS_CIPHER_MODE_XTS) || \
568 defined(MBEDTLS_CIPHER_MODE_CFB) || \
569 defined(MBEDTLS_CIPHER_MODE_OFB)
570 mbedtls_aes_context aes_ctx;
571 const unsigned char in[16] = { 0 };
572 unsigned char out[16];
573#endif
574#if defined(MBEDTLS_CIPHER_MODE_XTS)
575 mbedtls_aes_xts_context xts_ctx;
576#endif
577#if defined(MBEDTLS_CIPHER_MODE_CFB) || \
578 defined(MBEDTLS_CIPHER_MODE_OFB)
579 size_t size;
580#endif
581
582 /* These calls accept NULL */
583 TEST_VALID_PARAM( mbedtls_aes_free( NULL ) );
584#if defined(MBEDTLS_CIPHER_MODE_XTS)
585 TEST_VALID_PARAM( mbedtls_aes_xts_free( NULL ) );
586#endif
587
588#if defined(MBEDTLS_CIPHER_MODE_CBC)
589 TEST_ASSERT( mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT,
590 15,
591 out, in, out )
592 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
593 TEST_ASSERT( mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT,
594 17,
595 out, in, out )
596 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
597#endif
598
599#if defined(MBEDTLS_CIPHER_MODE_XTS)
600 TEST_ASSERT( mbedtls_aes_crypt_xts( &xts_ctx, MBEDTLS_AES_ENCRYPT,
601 15,
602 in, in, out )
603 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
604 TEST_ASSERT( mbedtls_aes_crypt_xts( &xts_ctx, MBEDTLS_AES_ENCRYPT,
605 (1 << 24) + 1,
606 in, in, out )
607 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
608#endif
609
610#if defined(MBEDTLS_CIPHER_MODE_CFB)
611 size = 16;
612 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &aes_ctx, MBEDTLS_AES_ENCRYPT, 16,
613 &size, out, in, out )
614 == MBEDTLS_ERR_AES_BAD_INPUT_DATA );
615#endif
616
617#if defined(MBEDTLS_CIPHER_MODE_OFB)
618 size = 16;
619 TEST_ASSERT( mbedtls_aes_crypt_ofb( &aes_ctx, 16, &size, out, in, out )
620 == MBEDTLS_ERR_AES_BAD_INPUT_DATA );
621#endif
622}
623/* END_CASE */
624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100626void aes_selftest( )
Paul Bakker3d360822009-07-05 11:29:38 +0000627{
Andres AG93012e82016-09-09 09:10:28 +0100628 TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000629}
Paul Bakker33b43f12013-08-20 11:48:36 +0200630/* END_CASE */