blob: ad65a1b3675baca5af53808e07803f879ea65c08 [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 Khanf1aaec92017-05-30 14:23:15 +010011void aes_encrypt_ecb( uint8_t * key_str, uint32_t key_len, uint8_t * src_str,
12 uint32_t src_str_len, uint8_t * hex_dst_string,
13 uint32_t hex_dst_string_len, int setkey_result )
Paul Bakker367dae42009-06-28 21:50:27 +000014{
Paul Bakker367dae42009-06-28 21:50:27 +000015 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016 mbedtls_aes_context ctx;
Paul Bakker367dae42009-06-28 21:50:27 +000017
Paul Bakker367dae42009-06-28 21:50:27 +000018 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000020
Paul Bakker367dae42009-06-28 21:50:27 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020023 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025 TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_ENCRYPT, src_str, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000026
Azim Khanf1aaec92017-05-30 14:23:15 +010027 TEST_ASSERT( hexcmp( output, hex_dst_string, 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 Khanf1aaec92017-05-30 14:23:15 +010036void aes_decrypt_ecb( uint8_t * key_str, uint32_t key_len, uint8_t * src_str,
37 uint32_t src_str_len, uint8_t * hex_dst_string,
38 uint32_t hex_dst_string_len, int setkey_result )
Paul Bakker367dae42009-06-28 21:50:27 +000039{
Paul Bakker367dae42009-06-28 21:50:27 +000040 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041 mbedtls_aes_context ctx;
Paul Bakker367dae42009-06-28 21:50:27 +000042
Paul Bakker367dae42009-06-28 21:50:27 +000043 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000045
Paul Bakker367dae42009-06-28 21:50:27 +000046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 TEST_ASSERT( mbedtls_aes_setkey_dec( &ctx, key_str, key_len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020048 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_DECRYPT, src_str, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +000051
Azim Khanf1aaec92017-05-30 14:23:15 +010052 TEST_ASSERT( hexcmp( output, hex_dst_string, 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 Khanf1aaec92017-05-30 14:23:15 +010061void aes_encrypt_cbc( uint8_t * key_str, uint32_t key_len, uint8_t * iv_str,
62 uint32_t iv_str_len, uint8_t * src_str,
63 uint32_t data_len, uint8_t * hex_dst_string,
64 uint32_t hex_dst_string_len, int cbc_result )
Paul Bakker367dae42009-06-28 21:50:27 +000065{
Paul Bakker367dae42009-06-28 21:50:27 +000066 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 mbedtls_aes_context ctx;
Paul Bakker367dae42009-06-28 21:50:27 +000068
Paul Bakker367dae42009-06-28 21:50:27 +000069 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000071
Paul Bakker367dae42009-06-28 21:50:27 +000072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
74 TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_ENCRYPT, data_len, iv_str, src_str, output ) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020075 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +000076 {
Paul Bakker367dae42009-06-28 21:50:27 +000077
Azim Khanf1aaec92017-05-30 14:23:15 +010078 TEST_ASSERT( hexcmp( output, hex_dst_string, data_len, hex_dst_string_len ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000079 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020080
Paul Bakkerbd51b262014-07-10 15:26:12 +020081exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000083}
Paul Bakker33b43f12013-08-20 11:48:36 +020084/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khanf1aaec92017-05-30 14:23:15 +010087void aes_decrypt_cbc( uint8_t * key_str, uint32_t key_len, uint8_t * iv_str,
88 uint32_t iv_str_len, uint8_t * src_str,
89 uint32_t data_len, uint8_t * hex_dst_string,
90 uint32_t hex_dst_string_len, 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
Paul Bakker367dae42009-06-28 21:50:27 +000098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_aes_setkey_dec( &ctx, key_str, key_len * 8 );
100 TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_DECRYPT, data_len, iv_str, src_str, output ) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200101 if( cbc_result == 0)
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000102 {
Paul Bakker367dae42009-06-28 21:50:27 +0000103
Azim Khanf1aaec92017-05-30 14:23:15 +0100104 TEST_ASSERT( hexcmp( output, hex_dst_string, data_len, hex_dst_string_len ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000105 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200106
Paul Bakkerbd51b262014-07-10 15:26:12 +0200107exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000109}
Paul Bakker33b43f12013-08-20 11:48:36 +0200110/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000111
Aorimn5f778012016-06-09 23:22:58 +0200112/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100113void aes_encrypt_xts( char *hex_key_string, char *hex_data_unit_string,
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100114 char *hex_src_string, char *hex_dst_string )
Aorimn5f778012016-06-09 23:22:58 +0200115{
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100116 enum { AES_BLOCK_SIZE = 16 };
117 unsigned char *data_unit = NULL;
118 unsigned char *key = NULL;
119 unsigned char *src = NULL;
120 unsigned char *dst = NULL;
121 unsigned char *output = NULL;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100122 mbedtls_aes_xts_context ctx;
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100123 size_t key_len, src_len, dst_len, data_unit_len;
Aorimn5f778012016-06-09 23:22:58 +0200124
Jaeden Amero9366feb2018-05-29 18:55:17 +0100125 mbedtls_aes_xts_init( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200126
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100127 data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len );
128 TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
Aorimn5f778012016-06-09 23:22:58 +0200129
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100130 key = unhexify_alloc( hex_key_string, &key_len );
131 TEST_ASSERT( key_len % 2 == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200132
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100133 src = unhexify_alloc( hex_src_string, &src_len );
134 dst = unhexify_alloc( hex_dst_string, &dst_len );
135 TEST_ASSERT( src_len == dst_len );
Aorimn5f778012016-06-09 23:22:58 +0200136
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100137 output = zero_alloc( dst_len );
138
139 TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == 0 );
140 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, src_len,
141 data_unit, src, output ) == 0 );
142
143 TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200144
145exit:
Jaeden Amero9366feb2018-05-29 18:55:17 +0100146 mbedtls_aes_xts_free( &ctx );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100147 mbedtls_free( data_unit );
148 mbedtls_free( key );
149 mbedtls_free( src );
150 mbedtls_free( dst );
151 mbedtls_free( output );
Aorimn5f778012016-06-09 23:22:58 +0200152}
153/* END_CASE */
154
155/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100156void aes_decrypt_xts( char *hex_key_string, char *hex_data_unit_string,
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100157 char *hex_dst_string, char *hex_src_string )
Aorimn5f778012016-06-09 23:22:58 +0200158{
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100159 enum { AES_BLOCK_SIZE = 16 };
160 unsigned char *data_unit = NULL;
161 unsigned char *key = NULL;
162 unsigned char *src = NULL;
163 unsigned char *dst = NULL;
164 unsigned char *output = NULL;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100165 mbedtls_aes_xts_context ctx;
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100166 size_t key_len, src_len, dst_len, data_unit_len;
Aorimn5f778012016-06-09 23:22:58 +0200167
Jaeden Amero9366feb2018-05-29 18:55:17 +0100168 mbedtls_aes_xts_init( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200169
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100170 data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len );
171 TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
Aorimn5f778012016-06-09 23:22:58 +0200172
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100173 key = unhexify_alloc( hex_key_string, &key_len );
174 TEST_ASSERT( key_len % 2 == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200175
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100176 src = unhexify_alloc( hex_src_string, &src_len );
177 dst = unhexify_alloc( hex_dst_string, &dst_len );
178 TEST_ASSERT( src_len == dst_len );
Aorimn5f778012016-06-09 23:22:58 +0200179
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100180 output = zero_alloc( dst_len );
181
182 TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == 0 );
183 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_DECRYPT, src_len,
184 data_unit, src, output ) == 0 );
185
186 TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200187
188exit:
Jaeden Amero9366feb2018-05-29 18:55:17 +0100189 mbedtls_aes_xts_free( &ctx );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100190 mbedtls_free( data_unit );
191 mbedtls_free( key );
192 mbedtls_free( src );
193 mbedtls_free( dst );
194 mbedtls_free( output );
Aorimn5f778012016-06-09 23:22:58 +0200195}
196/* END_CASE */
197
Jaeden Amero425382d2018-04-28 17:26:25 +0100198/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
199void aes_crypt_xts_size( int size, int retval )
200{
201 mbedtls_aes_xts_context ctx;
202 const unsigned char *src = NULL;
203 unsigned char *output = NULL;
204 unsigned char data_unit[16];
205 size_t length = size;
206
207 mbedtls_aes_xts_init( &ctx );
208 memset( data_unit, 0x00, sizeof( data_unit ) );
209
210
211 /* Note that this function will most likely crash on failure, as NULL
212 * parameters will be used. In the passing case, the length check in
213 * mbedtls_aes_crypt_xts() will prevent any accesses to parameters by
214 * exiting the function early. */
215 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, length, data_unit, src, output ) == retval );
216}
217/* END_CASE */
218
Jaeden Amero142383e2018-05-31 10:40:34 +0100219/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
220void aes_crypt_xts_keysize( int size, int retval )
221{
222 mbedtls_aes_xts_context ctx;
223 const unsigned char *key = NULL;
224 size_t key_len = size;
225
226 mbedtls_aes_xts_init( &ctx );
227
228 TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == retval );
229 TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == retval );
230exit:
231 mbedtls_aes_xts_free( &ctx );
232}
233/* END_CASE */
Jaeden Amero425382d2018-04-28 17:26:25 +0100234
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khanf1aaec92017-05-30 14:23:15 +0100237void aes_encrypt_cfb128( uint8_t * key_str, uint32_t key_len,
238 uint8_t * iv_str, uint32_t iv_str_len,
239 uint8_t * src_str, uint32_t src_str_len,
240 uint8_t * hex_dst_string, uint32_t hex_dst_string_len
241 )
Paul Bakker367dae42009-06-28 21:50:27 +0000242{
Paul Bakker367dae42009-06-28 21:50:27 +0000243 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000245 size_t iv_offset = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000246
Paul Bakker367dae42009-06-28 21:50:27 +0000247 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000249
Paul Bakker367dae42009-06-28 21:50:27 +0000250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
252 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_ENCRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000253
Azim Khanf1aaec92017-05-30 14:23:15 +0100254 TEST_ASSERT( hexcmp( output, hex_dst_string, 16, hex_dst_string_len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200255
Paul Bakkerbd51b262014-07-10 15:26:12 +0200256exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000258}
Paul Bakker33b43f12013-08-20 11:48:36 +0200259/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khanf1aaec92017-05-30 14:23:15 +0100262void aes_decrypt_cfb128( uint8_t * key_str, uint32_t key_len,
263 uint8_t * iv_str, uint32_t iv_str_len,
264 uint8_t * src_str, uint32_t src_str_len,
265 uint8_t * hex_dst_string, uint32_t hex_dst_string_len
266 )
Paul Bakker367dae42009-06-28 21:50:27 +0000267{
Paul Bakker367dae42009-06-28 21:50:27 +0000268 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000270 size_t iv_offset = 0;
Paul Bakker367dae42009-06-28 21:50:27 +0000271
Paul Bakker367dae42009-06-28 21:50:27 +0000272 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000274
Paul Bakker367dae42009-06-28 21:50:27 +0000275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
277 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_DECRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000278
Azim Khanf1aaec92017-05-30 14:23:15 +0100279 TEST_ASSERT( hexcmp( output, hex_dst_string, 16, hex_dst_string_len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200280
Paul Bakkerbd51b262014-07-10 15:26:12 +0200281exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000283}
Paul Bakker33b43f12013-08-20 11:48:36 +0200284/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khanf1aaec92017-05-30 14:23:15 +0100287void aes_encrypt_cfb8( uint8_t * key_str, uint32_t key_len, uint8_t * iv_str,
288 uint32_t iv_str_len, uint8_t * src_str,
289 uint32_t src_len, uint8_t * hex_dst_string,
290 uint32_t hex_dst_string_len )
Paul Bakker556efba2014-01-24 15:38:12 +0100291{
Paul Bakker556efba2014-01-24 15:38:12 +0100292 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100294
Paul Bakker556efba2014-01-24 15:38:12 +0100295 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100297
Paul Bakker556efba2014-01-24 15:38:12 +0100298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
300 TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_ENCRYPT, src_len, iv_str, src_str, output ) == 0 );
Paul Bakker556efba2014-01-24 15:38:12 +0100301
Azim Khanf1aaec92017-05-30 14:23:15 +0100302 TEST_ASSERT( hexcmp( output, hex_dst_string, src_len, hex_dst_string_len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200303
Paul Bakkerbd51b262014-07-10 15:26:12 +0200304exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100306}
307/* END_CASE */
308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khanf1aaec92017-05-30 14:23:15 +0100310void aes_decrypt_cfb8( uint8_t * key_str, uint32_t key_len, uint8_t * iv_str,
311 uint32_t iv_str_len, uint8_t * src_str,
312 uint32_t src_len, uint8_t * hex_dst_string,
313 uint32_t hex_dst_string_len )
Paul Bakker556efba2014-01-24 15:38:12 +0100314{
Paul Bakker556efba2014-01-24 15:38:12 +0100315 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100317
Paul Bakker556efba2014-01-24 15:38:12 +0100318 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100320
Paul Bakker556efba2014-01-24 15:38:12 +0100321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
323 TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_DECRYPT, src_len, iv_str, src_str, output ) == 0 );
Paul Bakker556efba2014-01-24 15:38:12 +0100324
Azim Khanf1aaec92017-05-30 14:23:15 +0100325 TEST_ASSERT( hexcmp( output, hex_dst_string, src_len, hex_dst_string_len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200326
Paul Bakkerbd51b262014-07-10 15:26:12 +0200327exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100329}
330/* END_CASE */
331
Simon Butcher03018842018-04-22 22:57:58 +0100332/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */
333void aes_encrypt_ofb( int fragment_size, char *hex_key_string,
Simon Butcher00131442018-05-22 22:40:36 +0100334 char *hex_iv_string, char *hex_src_string,
335 char *hex_dst_string )
Simon Butcher03018842018-04-22 22:57:58 +0100336{
Simon Butchere416bf92018-06-02 18:28:32 +0100337 unsigned char key_str[32];
338 unsigned char iv_str[16];
339 unsigned char src_str[64];
340 unsigned char dst_str[64];
341 unsigned char output[32];
Simon Butcher03018842018-04-22 22:57:58 +0100342 mbedtls_aes_context ctx;
343 size_t iv_offset = 0;
344 int in_buffer_len;
345 unsigned char* src_str_next;
Simon Butcherdbe7fbf2018-04-29 14:51:35 +0100346 int key_len;
Simon Butcher03018842018-04-22 22:57:58 +0100347
Simon Butcherb7836e12018-06-02 18:36:49 +0100348 memset( key_str, 0x00, sizeof( key_str ) );
349 memset( iv_str, 0x00, sizeof( iv_str ) );
350 memset( src_str, 0x00, sizeof( src_str ) );
351 memset( dst_str, 0x00, sizeof( dst_str ) );
352 memset( output, 0x00, sizeof( output ) );
Simon Butcher03018842018-04-22 22:57:58 +0100353 mbedtls_aes_init( &ctx );
354
Simon Butchere416bf92018-06-02 18:28:32 +0100355 TEST_ASSERT( strlen( hex_key_string ) <= ( 32 * 2 ) );
356 TEST_ASSERT( strlen( hex_iv_string ) <= ( 16 * 2 ) );
357 TEST_ASSERT( strlen( hex_src_string ) <= ( 64 * 2 ) );
358 TEST_ASSERT( strlen( hex_dst_string ) <= ( 64 * 2 ) );
359
Simon Butcher03018842018-04-22 22:57:58 +0100360 key_len = unhexify( key_str, hex_key_string );
Simon Butcherdbe7fbf2018-04-29 14:51:35 +0100361 unhexify( iv_str, hex_iv_string );
Simon Butcher03018842018-04-22 22:57:58 +0100362 in_buffer_len = unhexify( src_str, hex_src_string );
363
Simon Butcherad4e4932018-04-29 00:43:47 +0100364 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100365 src_str_next = src_str;
366
367 while( in_buffer_len > 0 )
368 {
369 TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset,
370 iv_str, src_str_next, output ) == 0 );
371
372 hexify( dst_str, output, fragment_size );
373 TEST_ASSERT( strncmp( (char *) dst_str, hex_dst_string,
Simon Butcher00131442018-05-22 22:40:36 +0100374 ( 2 * fragment_size ) ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100375
376 in_buffer_len -= fragment_size;
377 hex_dst_string += ( fragment_size * 2 );
378 src_str_next += fragment_size;
379
380 if( in_buffer_len < fragment_size )
381 fragment_size = in_buffer_len;
382 }
383
384exit:
385 mbedtls_aes_free( &ctx );
386}
387/* END_CASE */
388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100390void aes_selftest( )
Paul Bakker3d360822009-07-05 11:29:38 +0000391{
Andres AG93012e82016-09-09 09:10:28 +0100392 TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000393}
Paul Bakker33b43f12013-08-20 11:48:36 +0200394/* END_CASE */