blob: c0aa831ea9545f77374376519f41a59107b8585e [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/cipher.h"
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +02003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00005#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +02006#endif
Paul Bakker33b43f12013-08-20 11:48:36 +02007/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +00008
Paul Bakker33b43f12013-08-20 11:48:36 +02009/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010 * depends_on:MBEDTLS_CIPHER_C
Paul Bakker33b43f12013-08-20 11:48:36 +020011 * END_DEPENDENCIES
12 */
Paul Bakker5690efc2011-05-26 13:16:06 +000013
Paul Bakker33b43f12013-08-20 11:48:36 +020014/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015void mbedtls_cipher_list( )
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +010016{
17 const int *cipher_type;
18
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019 for( cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++ )
20 TEST_ASSERT( mbedtls_cipher_info_from_type( *cipher_type ) != NULL );
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +010021}
22/* END_CASE */
23
24/* BEGIN_CASE */
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020025void cipher_null_args( )
26{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027 mbedtls_cipher_context_t ctx;
28 const mbedtls_cipher_info_t *info = mbedtls_cipher_info_from_type( *( mbedtls_cipher_list() ) );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020029 unsigned char buf[1] = { 0 };
30 size_t olen;
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034 TEST_ASSERT( mbedtls_cipher_get_block_size( NULL ) == 0 );
35 TEST_ASSERT( mbedtls_cipher_get_block_size( &ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037 TEST_ASSERT( mbedtls_cipher_get_cipher_mode( NULL ) == MBEDTLS_MODE_NONE );
38 TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &ctx ) == MBEDTLS_MODE_NONE );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 TEST_ASSERT( mbedtls_cipher_get_iv_size( NULL ) == 0 );
41 TEST_ASSERT( mbedtls_cipher_get_iv_size( &ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 TEST_ASSERT( mbedtls_cipher_info_from_string( NULL ) == NULL );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020044
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +020045 TEST_ASSERT( mbedtls_cipher_setup( &ctx, NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +020047 TEST_ASSERT( mbedtls_cipher_setup( NULL, info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 TEST_ASSERT( mbedtls_cipher_setkey( NULL, buf, 0, MBEDTLS_ENCRYPT )
51 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
52 TEST_ASSERT( mbedtls_cipher_setkey( &ctx, buf, 0, MBEDTLS_ENCRYPT )
53 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 TEST_ASSERT( mbedtls_cipher_set_iv( NULL, buf, 0 )
56 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
57 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, buf, 0 )
58 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 TEST_ASSERT( mbedtls_cipher_reset( NULL ) == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
61 TEST_ASSERT( mbedtls_cipher_reset( &ctx ) == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020062
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020063#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 TEST_ASSERT( mbedtls_cipher_update_ad( NULL, buf, 0 )
65 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
66 TEST_ASSERT( mbedtls_cipher_update_ad( &ctx, buf, 0 )
67 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020068#endif
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 TEST_ASSERT( mbedtls_cipher_update( NULL, buf, 0, buf, &olen )
71 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
72 TEST_ASSERT( mbedtls_cipher_update( &ctx, buf, 0, buf, &olen )
73 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 TEST_ASSERT( mbedtls_cipher_finish( NULL, buf, &olen )
76 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
77 TEST_ASSERT( mbedtls_cipher_finish( &ctx, buf, &olen )
78 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020079
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020080#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 TEST_ASSERT( mbedtls_cipher_write_tag( NULL, buf, olen )
82 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
83 TEST_ASSERT( mbedtls_cipher_write_tag( &ctx, buf, olen )
84 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 TEST_ASSERT( mbedtls_cipher_check_tag( NULL, buf, olen )
87 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
88 TEST_ASSERT( mbedtls_cipher_check_tag( &ctx, buf, olen )
89 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020090#endif
91}
92/* END_CASE */
93
Paul Bakker6a9c7252016-07-14 13:46:10 +010094/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
95void cipher_special_behaviours( )
96{
97 const mbedtls_cipher_info_t *cipher_info;
98 mbedtls_cipher_context_t ctx;
99 unsigned char input[32];
100 unsigned char output[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300101#if defined (MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100102 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300103#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100104 size_t olen = 0;
105
106 mbedtls_cipher_init( &ctx );
107 memset( input, 0, sizeof( input ) );
108 memset( output, 0, sizeof( output ) );
Ron Eldor6f90ed82017-09-26 12:08:54 +0300109#if defined (MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100110 memset( iv, 0, sizeof( iv ) );
111
112 /* Check and get info structures */
Ron Eldor7b012442017-09-25 17:03:12 +0300113 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
Paul Bakker6a9c7252016-07-14 13:46:10 +0100114 TEST_ASSERT( NULL != cipher_info );
115
116 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
117
118 /* IV too big */
119 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
120 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
121
122 /* IV too small */
123 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
124 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
125
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300126 mbedtls_cipher_free( &ctx );
Ron Eldor6f90ed82017-09-26 12:08:54 +0300127#endif /* MBEDTLS_CIPHER_MODE_CBC */
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300128 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
Ron Eldor7b012442017-09-25 17:03:12 +0300129 TEST_ASSERT( NULL != cipher_info );
130
131 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
132
Paul Bakker6a9c7252016-07-14 13:46:10 +0100133 /* Update ECB with partial block */
134 TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
135 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
136
137exit:
138 mbedtls_cipher_free( &ctx );
139}
140/* END_CASE */
141
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200142/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +0200143void enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
144 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200145{
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200146 size_t length = length_val, outlen, total_len, i, block_size;
Jaeden Amerod906b812018-06-08 11:03:16 +0100147 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000148 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200149 unsigned char ad[13];
150 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200151 unsigned char inbuf[64];
152 unsigned char encbuf[64];
153 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 const mbedtls_cipher_info_t *cipher_info;
156 mbedtls_cipher_context_t ctx_dec;
157 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000158
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200159 /*
160 * Prepare contexts
161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_cipher_init( &ctx_dec );
163 mbedtls_cipher_init( &ctx_enc );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200164
165 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000166
167 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000169 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000171
172 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200173 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
174 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
177 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200180 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
183 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200184 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200185#else
186 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200188
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200189 /*
190 * Do a few encode/decode cycles
191 */
192 for( i = 0; i < 3; i++ )
193 {
194 memset( iv , 0x00 + i, sizeof( iv ) );
195 memset( ad, 0x10 + i, sizeof( ad ) );
196 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
197
198 memset( encbuf, 0, sizeof( encbuf ) );
199 memset( decbuf, 0, sizeof( decbuf ) );
200 memset( tag, 0, sizeof( tag ) );
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
203 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
206 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200207
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200208#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
210 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200211#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000212
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200213 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
214 TEST_ASSERT( block_size != 0 );
215
Paul Bakker8123e9d2011-01-06 15:37:30 +0000216 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200218 total_len = outlen;
219
220 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200221 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200222 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200223 total_len + block_size > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200226 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000227
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200228#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200230#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200231
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200232 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200233 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200234 total_len > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200235 total_len <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000236
237 /* decode the previously encoded string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200239 total_len = outlen;
240
241 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200242 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200243 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200244 total_len + block_size >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200247 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000248
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200249#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200251#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200252
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200253 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200254 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000255 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200256 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000257
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200258 /*
259 * Done
260 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200261exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_cipher_free( &ctx_dec );
263 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200264}
Paul Bakker33b43f12013-08-20 11:48:36 +0200265/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000266
Paul Bakker33b43f12013-08-20 11:48:36 +0200267/* BEGIN_CASE */
268void enc_fail( int cipher_id, int pad_mode, int key_len,
269 int length_val, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200270{
Paul Bakker33b43f12013-08-20 11:48:36 +0200271 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200272 unsigned char key[32];
273 unsigned char iv[16];
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 const mbedtls_cipher_info_t *cipher_info;
276 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200277
278 unsigned char inbuf[64];
279 unsigned char encbuf[64];
280
281 size_t outlen = 0;
282
283 memset( key, 0, 32 );
284 memset( iv , 0, 16 );
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200287
288 memset( inbuf, 5, 64 );
289 memset( encbuf, 0, 64 );
290
291 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200293 TEST_ASSERT( NULL != cipher_info );
294
295 /* Initialise context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200296 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
298#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
299 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200300#else
301 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
303 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
304 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200305#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200307#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200308
309 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
311 TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200312
313 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200314exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200316}
Paul Bakker33b43f12013-08-20 11:48:36 +0200317/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200318
Paul Bakker33b43f12013-08-20 11:48:36 +0200319/* BEGIN_CASE */
320void dec_empty_buf()
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200321{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000322 unsigned char key[32];
323 unsigned char iv[16];
324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_cipher_context_t ctx_dec;
326 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000327
328 unsigned char encbuf[64];
329 unsigned char decbuf[64];
330
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000331 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000332
333 memset( key, 0, 32 );
334 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_cipher_init( &ctx_dec );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200337
Paul Bakker8123e9d2011-01-06 15:37:30 +0000338 memset( encbuf, 0, 64 );
339 memset( decbuf, 0, 64 );
340
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200341 /* Initialise context */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000343 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200344
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200345 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, 128, MBEDTLS_DECRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200352
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200353#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200355#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000356
357 /* decode 0-byte string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000359 TEST_ASSERT( 0 == outlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 TEST_ASSERT( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED == mbedtls_cipher_finish(
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200361 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000362 TEST_ASSERT( 0 == outlen );
363
Paul Bakkerbd51b262014-07-10 15:26:12 +0200364exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_cipher_free( &ctx_dec );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200366}
Paul Bakker33b43f12013-08-20 11:48:36 +0200367/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000368
Paul Bakker33b43f12013-08-20 11:48:36 +0200369/* BEGIN_CASE */
370void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700371 int second_length_val, int pad_mode,
372 int first_encrypt_output_len, int second_encrypt_output_len,
373 int first_decrypt_output_len, int second_decrypt_output_len )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200374{
Paul Bakker33b43f12013-08-20 11:48:36 +0200375 size_t first_length = first_length_val;
376 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000377 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200378 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000379 unsigned char key[32];
380 unsigned char iv[16];
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_cipher_context_t ctx_dec;
383 mbedtls_cipher_context_t ctx_enc;
384 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385
386 unsigned char inbuf[64];
387 unsigned char encbuf[64];
388 unsigned char decbuf[64];
389
Paul Bakker23986e52011-04-24 08:57:21 +0000390 size_t outlen = 0;
391 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000392
393 memset( key, 0, 32 );
394 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_cipher_init( &ctx_dec );
397 mbedtls_cipher_init( &ctx_enc );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200398
Paul Bakker8123e9d2011-01-06 15:37:30 +0000399 memset( inbuf, 5, 64 );
400 memset( encbuf, 0, 64 );
401 memset( decbuf, 0, 64 );
402
403 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000405 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200406
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200407 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
408 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
411 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000412
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700413#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
414 if( -1 != pad_mode )
415 {
416 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
417 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
418 }
419#else
420 (void) pad_mode;
421#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
424 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
427 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200428
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200429#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
431 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200432#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000433
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200434 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
435 TEST_ASSERT( block_size != 0 );
436
Paul Bakker8123e9d2011-01-06 15:37:30 +0000437 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700439 TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000440 totaloutlen = outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700442 TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000443 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200444 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200445 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200446 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200447 totaloutlen + block_size > length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000450 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200451 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200452 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200453 totaloutlen > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200454 totaloutlen <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000455
456 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700457 second_length = totaloutlen - first_length;
458 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
459 TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200460 totaloutlen = outlen;
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700461 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
462 TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
463 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200464
465 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200466 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200467 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200468 totaloutlen + block_size >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200469
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700470 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200471 totaloutlen += outlen;
472
473 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000474
475 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
476
Paul Bakkerbd51b262014-07-10 15:26:12 +0200477exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_cipher_free( &ctx_dec );
479 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200480}
Paul Bakker33b43f12013-08-20 11:48:36 +0200481/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000482
Paul Bakker33b43f12013-08-20 11:48:36 +0200483/* BEGIN_CASE */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200484void decrypt_test_vec( int cipher_id, int pad_mode,
485 char *hex_key, char *hex_iv,
486 char *hex_cipher, char *hex_clear,
487 char *hex_ad, char *hex_tag,
488 int finish_result, int tag_result )
489{
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200490 unsigned char key[50];
491 unsigned char iv[50];
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200492 unsigned char cipher[265]; /* max length of test data so far */
493 unsigned char clear[265];
494 unsigned char output[265];
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200495 unsigned char ad[200];
496 unsigned char tag[20];
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200497 size_t key_len, iv_len, cipher_len, clear_len;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200498#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200499 size_t ad_len, tag_len;
500#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200502 size_t outlen, total_len;
503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200505
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200506 memset( key, 0x00, sizeof( key ) );
507 memset( iv, 0x00, sizeof( iv ) );
508 memset( cipher, 0x00, sizeof( cipher ) );
509 memset( clear, 0x00, sizeof( clear ) );
510 memset( ad, 0x00, sizeof( ad ) );
511 memset( tag, 0x00, sizeof( tag ) );
512 memset( output, 0x00, sizeof( output ) );
513
514 key_len = unhexify( key, hex_key );
515 iv_len = unhexify( iv, hex_iv );
516 cipher_len = unhexify( cipher, hex_cipher );
517 clear_len = unhexify( clear, hex_clear );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200518#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200519 ad_len = unhexify( ad, hex_ad );
520 tag_len = unhexify( tag, hex_tag );
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200521#else
522 ((void) hex_ad);
523 ((void) hex_tag);
524#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200525
526 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200527 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_cipher_info_from_type( cipher_id ) ) );
529 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, MBEDTLS_DECRYPT ) );
530#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200531 if( pad_mode != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200533#else
534 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
536 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, iv_len ) );
537 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200538#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad, ad_len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200540#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200541
542 /* decode buffer and check tag */
543 total_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200545 total_len += outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200547 &outlen ) );
548 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200549#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag, tag_len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200551#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200552
553 /* check plaintext only if everything went fine */
554 if( 0 == finish_result && 0 == tag_result )
555 {
556 TEST_ASSERT( total_len == clear_len );
557 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
558 }
559
Paul Bakkerbd51b262014-07-10 15:26:12 +0200560exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200562}
563/* END_CASE */
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200566void auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv,
567 char *hex_ad, char *hex_cipher,
568 char *hex_tag, char *hex_clear )
569{
570 int ret;
571 unsigned char key[50];
572 unsigned char iv[50];
Manuel Pégourié-Gonnard69767d12018-05-09 12:25:18 +0200573 unsigned char cipher[265]; /* max size of test data so far */
574 unsigned char clear[265];
575 unsigned char output[267]; /* above + 2 (overwrite check) */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200576 unsigned char ad[200];
577 unsigned char tag[20];
578 unsigned char my_tag[20];
579 size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200581 size_t outlen;
582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200584
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200585 memset( key, 0x00, sizeof( key ) );
586 memset( iv, 0x00, sizeof( iv ) );
587 memset( cipher, 0x00, sizeof( cipher ) );
588 memset( clear, 0x00, sizeof( clear ) );
589 memset( ad, 0x00, sizeof( ad ) );
590 memset( tag, 0x00, sizeof( tag ) );
591 memset( my_tag, 0xFF, sizeof( my_tag ) );
592 memset( output, 0xFF, sizeof( output ) );
593
594 key_len = unhexify( key, hex_key );
595 iv_len = unhexify( iv, hex_iv );
596 cipher_len = unhexify( cipher, hex_cipher );
597 ad_len = unhexify( ad, hex_ad );
598 tag_len = unhexify( tag, hex_tag );
599
600 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200601 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_cipher_info_from_type( cipher_id ) ) );
603 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200604
605 /* decode buffer and check tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 ret = mbedtls_cipher_auth_decrypt( &ctx, iv, iv_len, ad, ad_len,
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200607 cipher, cipher_len, output, &outlen,
608 tag, tag_len );
609
610 /* make sure we didn't overwrite */
611 TEST_ASSERT( output[outlen + 0] == 0xFF );
612 TEST_ASSERT( output[outlen + 1] == 0xFF );
613
614 /* make sure the message is rejected if it should be */
615 if( strcmp( hex_clear, "FAIL" ) == 0 )
616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200618 goto exit;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200619 }
620
621 /* otherwise, make sure it was decrypted properly */
622 TEST_ASSERT( ret == 0 );
623
624 clear_len = unhexify( clear, hex_clear );
625 TEST_ASSERT( outlen == clear_len );
626 TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 );
627
628 /* then encrypt the clear and make sure we get the same ciphertext and tag */
629 memset( output, 0xFF, sizeof( output ) );
630 outlen = 0;
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 ret = mbedtls_cipher_auth_encrypt( &ctx, iv, iv_len, ad, ad_len,
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200633 clear, clear_len, output, &outlen,
634 my_tag, tag_len );
635 TEST_ASSERT( ret == 0 );
636
637 TEST_ASSERT( outlen == clear_len );
638 TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 );
639 TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 );
640
641 /* make sure we didn't overwrite */
642 TEST_ASSERT( output[outlen + 0] == 0xFF );
643 TEST_ASSERT( output[outlen + 1] == 0xFF );
644 TEST_ASSERT( my_tag[tag_len + 0] == 0xFF );
645 TEST_ASSERT( my_tag[tag_len + 1] == 0xFF );
646
647
Paul Bakkerbd51b262014-07-10 15:26:12 +0200648exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200650}
651/* END_CASE */
652
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200653/* BEGIN_CASE */
Paul Bakker5e0efa72013-09-08 23:04:04 +0200654void test_vec_ecb( int cipher_id, int operation, char *hex_key,
655 char *hex_input, char *hex_result,
656 int finish_result )
657{
658 unsigned char key[50];
659 unsigned char input[16];
660 unsigned char result[16];
661 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200663 unsigned char output[32];
664 size_t outlen;
665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200667
Paul Bakker5e0efa72013-09-08 23:04:04 +0200668 memset( key, 0x00, sizeof( key ) );
669 memset( input, 0x00, sizeof( input ) );
670 memset( result, 0x00, sizeof( result ) );
671 memset( output, 0x00, sizeof( output ) );
672
673 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200674 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_cipher_info_from_type( cipher_id ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200676
677 key_len = unhexify( key, hex_key );
678 TEST_ASSERT( unhexify( input, hex_input ) ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 (int) mbedtls_cipher_get_block_size( &ctx ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200680 TEST_ASSERT( unhexify( result, hex_result ) ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 (int) mbedtls_cipher_get_block_size( &ctx ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, operation ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input,
686 mbedtls_cipher_get_block_size( &ctx ),
Paul Bakker5e0efa72013-09-08 23:04:04 +0200687 output, &outlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
689 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200690 &outlen ) );
691 TEST_ASSERT( 0 == outlen );
692
693 /* check plaintext only if everything went fine */
694 if( 0 == finish_result )
695 TEST_ASSERT( 0 == memcmp( output, result,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_cipher_get_block_size( &ctx ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200697
Paul Bakkerbd51b262014-07-10 15:26:12 +0200698exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200700}
701/* END_CASE */
702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Ron Eldor7b012442017-09-25 17:03:12 +0300704void test_vec_crypt( int cipher_id, int operation, char *hex_key,
705 char *hex_iv, char *hex_input, char *hex_result,
706 int finish_result )
707{
708 unsigned char key[50];
709 unsigned char input[16];
710 unsigned char result[16];
711 unsigned char iv[16];
712 size_t key_len, iv_len, inputlen, resultlen;
713 mbedtls_cipher_context_t ctx;
714 unsigned char output[32];
715 size_t outlen;
716
717 mbedtls_cipher_init( &ctx );
718
719 memset( key, 0x00, sizeof( key ) );
720 memset( input, 0x00, sizeof( input ) );
721 memset( result, 0x00, sizeof( result ) );
722 memset( output, 0x00, sizeof( output ) );
723 memset( iv, 0x00, sizeof( iv ) );
724
725 /* Prepare context */
726 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
727 mbedtls_cipher_info_from_type( cipher_id ) ) );
728
729 key_len = unhexify( key, hex_key );
730 inputlen = unhexify( input, hex_input );
731 resultlen = unhexify( result, hex_result );
732
733 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, operation ) );
734 if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
735 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
736
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300737 iv_len = unhexify( iv, hex_iv );
Ron Eldor7b012442017-09-25 17:03:12 +0300738
739 TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv_len ? iv : NULL,
740 iv_len, input, inputlen,
741 output, &outlen ) );
742 TEST_ASSERT( resultlen == outlen );
743 /* check plaintext only if everything went fine */
744 if( 0 == finish_result )
745 TEST_ASSERT( 0 == memcmp( output, result, outlen ) );
746
747exit:
748 mbedtls_cipher_free( &ctx );
749}
750/* END_CASE */
751
752/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +0200753void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200754{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 const mbedtls_cipher_info_t *cipher_info;
756 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200761 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200762 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200765
Paul Bakkerbd51b262014-07-10 15:26:12 +0200766exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200768}
Paul Bakker33b43f12013-08-20 11:48:36 +0200769/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200772void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200773{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_cipher_info_t cipher_info;
775 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200776 unsigned char input[16];
777 size_t ilen, dlen;
778
779 /* build a fake context just for getting access to get_padding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_cipher_init( &ctx );
781 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200782 ctx.cipher_info = &cipher_info;
783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200785
Paul Bakker33b43f12013-08-20 11:48:36 +0200786 ilen = unhexify( input, input_str );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200787
Paul Bakker33b43f12013-08-20 11:48:36 +0200788 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
789 if( 0 == ret )
790 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200791}
Paul Bakker33b43f12013-08-20 11:48:36 +0200792/* END_CASE */