blob: a0f1b13eb05c6bdf336b4ecc35c32ce6fba17fca [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 Khand30ca132017-06-09 04:32:58 +010011void aes_encrypt_ecb( HexParam_t * key_str, HexParam_t * src_str,
12 HexParam_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);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000019
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 Khand30ca132017-06-09 04:32:58 +010035void aes_decrypt_ecb( HexParam_t * key_str, HexParam_t * src_str,
36 HexParam_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);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000043
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 Khand30ca132017-06-09 04:32:58 +010059void aes_encrypt_cbc( HexParam_t * key_str, HexParam_t * iv_str,
60 HexParam_t * src_str, HexParam_t * hex_dst_string,
61 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);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000068
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 Khand30ca132017-06-09 04:32:58 +010084void aes_decrypt_cbc( HexParam_t * key_str, HexParam_t * iv_str,
85 HexParam_t * src_str, HexParam_t * hex_dst_string,
86 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
Paul Bakker367dae42009-06-28 21:50:27 +000094
Azim Khand30ca132017-06-09 04:32:58 +010095 mbedtls_aes_setkey_dec( &ctx, key_str->x, key_str->len * 8 );
96 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 +020097 if( cbc_result == 0)
Paul Bakkerf3ccc682010-03-18 21:21:02 +000098 {
Paul Bakker367dae42009-06-28 21:50:27 +000099
Azim Khand30ca132017-06-09 04:32:58 +0100100 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000101 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200102
Paul Bakkerbd51b262014-07-10 15:26:12 +0200103exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000105}
Paul Bakker33b43f12013-08-20 11:48:36 +0200106/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000107
Aorimn5f778012016-06-09 23:22:58 +0200108/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100109void aes_encrypt_xts( char *hex_key_string, char *hex_data_unit_string,
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100110 char *hex_src_string, char *hex_dst_string )
Aorimn5f778012016-06-09 23:22:58 +0200111{
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100112 enum { AES_BLOCK_SIZE = 16 };
113 unsigned char *data_unit = NULL;
114 unsigned char *key = NULL;
115 unsigned char *src = NULL;
116 unsigned char *dst = NULL;
117 unsigned char *output = NULL;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100118 mbedtls_aes_xts_context ctx;
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100119 size_t key_len, src_len, dst_len, data_unit_len;
Aorimn5f778012016-06-09 23:22:58 +0200120
Jaeden Amero9366feb2018-05-29 18:55:17 +0100121 mbedtls_aes_xts_init( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200122
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100123 data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len );
124 TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
Aorimn5f778012016-06-09 23:22:58 +0200125
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100126 key = unhexify_alloc( hex_key_string, &key_len );
127 TEST_ASSERT( key_len % 2 == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200128
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100129 src = unhexify_alloc( hex_src_string, &src_len );
130 dst = unhexify_alloc( hex_dst_string, &dst_len );
131 TEST_ASSERT( src_len == dst_len );
Aorimn5f778012016-06-09 23:22:58 +0200132
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100133 output = zero_alloc( dst_len );
134
135 TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == 0 );
136 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, src_len,
137 data_unit, src, output ) == 0 );
138
139 TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200140
141exit:
Jaeden Amero9366feb2018-05-29 18:55:17 +0100142 mbedtls_aes_xts_free( &ctx );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100143 mbedtls_free( data_unit );
144 mbedtls_free( key );
145 mbedtls_free( src );
146 mbedtls_free( dst );
147 mbedtls_free( output );
Aorimn5f778012016-06-09 23:22:58 +0200148}
149/* END_CASE */
150
151/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100152void aes_decrypt_xts( char *hex_key_string, char *hex_data_unit_string,
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100153 char *hex_dst_string, char *hex_src_string )
Aorimn5f778012016-06-09 23:22:58 +0200154{
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100155 enum { AES_BLOCK_SIZE = 16 };
156 unsigned char *data_unit = NULL;
157 unsigned char *key = NULL;
158 unsigned char *src = NULL;
159 unsigned char *dst = NULL;
160 unsigned char *output = NULL;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100161 mbedtls_aes_xts_context ctx;
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100162 size_t key_len, src_len, dst_len, data_unit_len;
Aorimn5f778012016-06-09 23:22:58 +0200163
Jaeden Amero9366feb2018-05-29 18:55:17 +0100164 mbedtls_aes_xts_init( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200165
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100166 data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len );
167 TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
Aorimn5f778012016-06-09 23:22:58 +0200168
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100169 key = unhexify_alloc( hex_key_string, &key_len );
170 TEST_ASSERT( key_len % 2 == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200171
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100172 src = unhexify_alloc( hex_src_string, &src_len );
173 dst = unhexify_alloc( hex_dst_string, &dst_len );
174 TEST_ASSERT( src_len == dst_len );
Aorimn5f778012016-06-09 23:22:58 +0200175
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100176 output = zero_alloc( dst_len );
177
178 TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == 0 );
179 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_DECRYPT, src_len,
180 data_unit, src, output ) == 0 );
181
182 TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200183
184exit:
Jaeden Amero9366feb2018-05-29 18:55:17 +0100185 mbedtls_aes_xts_free( &ctx );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100186 mbedtls_free( data_unit );
187 mbedtls_free( key );
188 mbedtls_free( src );
189 mbedtls_free( dst );
190 mbedtls_free( output );
Aorimn5f778012016-06-09 23:22:58 +0200191}
192/* END_CASE */
193
Jaeden Amero425382d2018-04-28 17:26:25 +0100194/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
195void aes_crypt_xts_size( int size, int retval )
196{
197 mbedtls_aes_xts_context ctx;
198 const unsigned char *src = NULL;
199 unsigned char *output = NULL;
200 unsigned char data_unit[16];
201 size_t length = size;
202
203 mbedtls_aes_xts_init( &ctx );
204 memset( data_unit, 0x00, sizeof( data_unit ) );
205
206
207 /* Note that this function will most likely crash on failure, as NULL
208 * parameters will be used. In the passing case, the length check in
209 * mbedtls_aes_crypt_xts() will prevent any accesses to parameters by
210 * exiting the function early. */
211 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, length, data_unit, src, output ) == retval );
212}
213/* END_CASE */
214
Jaeden Amero142383e2018-05-31 10:40:34 +0100215/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
216void aes_crypt_xts_keysize( int size, int retval )
217{
218 mbedtls_aes_xts_context ctx;
219 const unsigned char *key = NULL;
220 size_t key_len = size;
221
222 mbedtls_aes_xts_init( &ctx );
223
224 TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == retval );
225 TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == retval );
226exit:
227 mbedtls_aes_xts_free( &ctx );
228}
229/* END_CASE */
Jaeden Amero425382d2018-04-28 17:26:25 +0100230
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khand30ca132017-06-09 04:32:58 +0100233void aes_encrypt_cfb128( HexParam_t * key_str, HexParam_t * iv_str,
234 HexParam_t * src_str, HexParam_t * hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000235{
Paul Bakker367dae42009-06-28 21:50:27 +0000236 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000238 size_t iv_offset = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000239
Paul Bakker367dae42009-06-28 21:50:27 +0000240 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000242
Paul Bakker367dae42009-06-28 21:50:27 +0000243
Azim Khand30ca132017-06-09 04:32:58 +0100244 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
245 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 +0000246
Azim Khand30ca132017-06-09 04:32:58 +0100247 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200248
Paul Bakkerbd51b262014-07-10 15:26:12 +0200249exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000251}
Paul Bakker33b43f12013-08-20 11:48:36 +0200252/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khand30ca132017-06-09 04:32:58 +0100255void aes_decrypt_cfb128( HexParam_t * key_str, HexParam_t * iv_str,
256 HexParam_t * src_str, HexParam_t * hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000257{
Paul Bakker367dae42009-06-28 21:50:27 +0000258 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000260 size_t iv_offset = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000261
Paul Bakker367dae42009-06-28 21:50:27 +0000262 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000264
Paul Bakker367dae42009-06-28 21:50:27 +0000265
Azim Khand30ca132017-06-09 04:32:58 +0100266 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
267 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 +0000268
Azim Khand30ca132017-06-09 04:32:58 +0100269 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200270
Paul Bakkerbd51b262014-07-10 15:26:12 +0200271exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000273}
Paul Bakker33b43f12013-08-20 11:48:36 +0200274/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khand30ca132017-06-09 04:32:58 +0100277void aes_encrypt_cfb8( HexParam_t * key_str, HexParam_t * iv_str,
278 HexParam_t * src_str, HexParam_t * hex_dst_string )
Paul Bakker556efba2014-01-24 15:38:12 +0100279{
Paul Bakker556efba2014-01-24 15:38:12 +0100280 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100282
Paul Bakker556efba2014-01-24 15:38:12 +0100283 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100285
Paul Bakker556efba2014-01-24 15:38:12 +0100286
Azim Khand30ca132017-06-09 04:32:58 +0100287 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
288 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 +0100289
Azim Khand30ca132017-06-09 04:32:58 +0100290 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200291
Paul Bakkerbd51b262014-07-10 15:26:12 +0200292exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100294}
295/* END_CASE */
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khand30ca132017-06-09 04:32:58 +0100298void aes_decrypt_cfb8( HexParam_t * key_str, HexParam_t * iv_str,
299 HexParam_t * src_str, HexParam_t * hex_dst_string )
Paul Bakker556efba2014-01-24 15:38:12 +0100300{
Paul Bakker556efba2014-01-24 15:38:12 +0100301 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100303
Paul Bakker556efba2014-01-24 15:38:12 +0100304 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100306
Paul Bakker556efba2014-01-24 15:38:12 +0100307
Azim Khand30ca132017-06-09 04:32:58 +0100308 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
309 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 +0100310
Azim Khand30ca132017-06-09 04:32:58 +0100311 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200312
Paul Bakkerbd51b262014-07-10 15:26:12 +0200313exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100315}
316/* END_CASE */
317
Simon Butcher03018842018-04-22 22:57:58 +0100318/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */
319void aes_encrypt_ofb( int fragment_size, char *hex_key_string,
Simon Butcher00131442018-05-22 22:40:36 +0100320 char *hex_iv_string, char *hex_src_string,
321 char *hex_dst_string )
Simon Butcher03018842018-04-22 22:57:58 +0100322{
Simon Butchere416bf92018-06-02 18:28:32 +0100323 unsigned char key_str[32];
324 unsigned char iv_str[16];
325 unsigned char src_str[64];
326 unsigned char dst_str[64];
327 unsigned char output[32];
Simon Butcher03018842018-04-22 22:57:58 +0100328 mbedtls_aes_context ctx;
329 size_t iv_offset = 0;
330 int in_buffer_len;
331 unsigned char* src_str_next;
Simon Butcherdbe7fbf2018-04-29 14:51:35 +0100332 int key_len;
Simon Butcher03018842018-04-22 22:57:58 +0100333
Simon Butcherb7836e12018-06-02 18:36:49 +0100334 memset( key_str, 0x00, sizeof( key_str ) );
335 memset( iv_str, 0x00, sizeof( iv_str ) );
336 memset( src_str, 0x00, sizeof( src_str ) );
337 memset( dst_str, 0x00, sizeof( dst_str ) );
338 memset( output, 0x00, sizeof( output ) );
Simon Butcher03018842018-04-22 22:57:58 +0100339 mbedtls_aes_init( &ctx );
340
Simon Butchere416bf92018-06-02 18:28:32 +0100341 TEST_ASSERT( strlen( hex_key_string ) <= ( 32 * 2 ) );
342 TEST_ASSERT( strlen( hex_iv_string ) <= ( 16 * 2 ) );
343 TEST_ASSERT( strlen( hex_src_string ) <= ( 64 * 2 ) );
344 TEST_ASSERT( strlen( hex_dst_string ) <= ( 64 * 2 ) );
345
Simon Butcher03018842018-04-22 22:57:58 +0100346 key_len = unhexify( key_str, hex_key_string );
Simon Butcherdbe7fbf2018-04-29 14:51:35 +0100347 unhexify( iv_str, hex_iv_string );
Simon Butcher03018842018-04-22 22:57:58 +0100348 in_buffer_len = unhexify( src_str, hex_src_string );
349
Simon Butcherad4e4932018-04-29 00:43:47 +0100350 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100351 src_str_next = src_str;
352
353 while( in_buffer_len > 0 )
354 {
355 TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset,
356 iv_str, src_str_next, output ) == 0 );
357
358 hexify( dst_str, output, fragment_size );
359 TEST_ASSERT( strncmp( (char *) dst_str, hex_dst_string,
Simon Butcher00131442018-05-22 22:40:36 +0100360 ( 2 * fragment_size ) ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100361
362 in_buffer_len -= fragment_size;
363 hex_dst_string += ( fragment_size * 2 );
364 src_str_next += fragment_size;
365
366 if( in_buffer_len < fragment_size )
367 fragment_size = in_buffer_len;
368 }
369
370exit:
371 mbedtls_aes_free( &ctx );
372}
373/* END_CASE */
374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100376void aes_selftest( )
Paul Bakker3d360822009-07-05 11:29:38 +0000377{
Andres AG93012e82016-09-09 09:10:28 +0100378 TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000379}
Paul Bakker33b43f12013-08-20 11:48:36 +0200380/* END_CASE */