blob: ddb9576e3b150a849aea7f6795c3d3a20e0094ab [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 */
Azim Khanf1aaec92017-05-30 14:23:15 +010015void 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 */
Azim Khanf1aaec92017-05-30 14:23:15 +010025void cipher_null_args( )
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020026{
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 */
Azim Khanf1aaec92017-05-30 14:23:15 +010095void cipher_special_behaviours( )
Paul Bakker6a9c7252016-07-14 13:46:10 +010096{
97 const mbedtls_cipher_info_t *cipher_info;
98 mbedtls_cipher_context_t ctx;
99 unsigned char input[32];
100 unsigned char output[32];
101 unsigned char iv[32];
102 size_t olen = 0;
103
104 mbedtls_cipher_init( &ctx );
105 memset( input, 0, sizeof( input ) );
106 memset( output, 0, sizeof( output ) );
107 memset( iv, 0, sizeof( iv ) );
108
109 /* Check and get info structures */
110 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
111 TEST_ASSERT( NULL != cipher_info );
112
113 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
114
115 /* IV too big */
116 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
117 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
118
119 /* IV too small */
120 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
121 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
122
123 /* Update ECB with partial block */
124 TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
125 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
126
127exit:
128 mbedtls_cipher_free( &ctx );
129}
130/* END_CASE */
131
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200132/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100133void enc_dec_buf( int cipher_id, char * cipher_string, int key_len,
Paul Bakker33b43f12013-08-20 11:48:36 +0200134 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200135{
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200136 size_t length = length_val, outlen, total_len, i, block_size;
Jaeden Amerod906b812018-06-08 11:03:16 +0100137 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000138 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200139 unsigned char ad[13];
140 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200141 unsigned char inbuf[64];
142 unsigned char encbuf[64];
143 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 const mbedtls_cipher_info_t *cipher_info;
146 mbedtls_cipher_context_t ctx_dec;
147 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000148
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200149 /*
150 * Prepare contexts
151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_cipher_init( &ctx_dec );
153 mbedtls_cipher_init( &ctx_enc );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200154
155 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000156
157 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000159 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000161
162 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200163 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
164 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
167 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200170 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
173 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200174 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200175#else
176 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200178
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200179 /*
180 * Do a few encode/decode cycles
181 */
182 for( i = 0; i < 3; i++ )
183 {
184 memset( iv , 0x00 + i, sizeof( iv ) );
185 memset( ad, 0x10 + i, sizeof( ad ) );
186 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
187
188 memset( encbuf, 0, sizeof( encbuf ) );
189 memset( decbuf, 0, sizeof( decbuf ) );
190 memset( tag, 0, sizeof( tag ) );
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
193 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
196 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200197
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200198#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
200 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200201#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000202
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200203 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
204 TEST_ASSERT( block_size != 0 );
205
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200208 total_len = outlen;
209
210 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200211 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200212 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200213 total_len + block_size > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200216 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000217
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200218#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200220#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200221
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200222 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200223 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200224 total_len > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200225 total_len <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000226
227 /* decode the previously encoded string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200229 total_len = outlen;
230
231 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200232 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200233 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200234 total_len + block_size >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200237 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000238
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200239#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200241#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200242
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200243 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200244 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000245 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200246 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000247
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200248 /*
249 * Done
250 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200251exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_cipher_free( &ctx_dec );
253 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200254}
Paul Bakker33b43f12013-08-20 11:48:36 +0200255/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000256
Paul Bakker33b43f12013-08-20 11:48:36 +0200257/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100258void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val,
259 int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200260{
Paul Bakker33b43f12013-08-20 11:48:36 +0200261 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200262 unsigned char key[32];
263 unsigned char iv[16];
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 const mbedtls_cipher_info_t *cipher_info;
266 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200267
268 unsigned char inbuf[64];
269 unsigned char encbuf[64];
270
271 size_t outlen = 0;
272
273 memset( key, 0, 32 );
274 memset( iv , 0, 16 );
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200277
278 memset( inbuf, 5, 64 );
279 memset( encbuf, 0, 64 );
280
281 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200283 TEST_ASSERT( NULL != cipher_info );
284
285 /* Initialise context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200286 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
288#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
289 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200290#else
291 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
293 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
294 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200295#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200297#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200298
299 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
301 TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200302
303 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200304exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200306}
Paul Bakker33b43f12013-08-20 11:48:36 +0200307/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200308
Paul Bakker33b43f12013-08-20 11:48:36 +0200309/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100310void dec_empty_buf( )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200311{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000312 unsigned char key[32];
313 unsigned char iv[16];
314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_cipher_context_t ctx_dec;
316 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000317
318 unsigned char encbuf[64];
319 unsigned char decbuf[64];
320
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000321 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000322
323 memset( key, 0, 32 );
324 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_cipher_init( &ctx_dec );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200327
Paul Bakker8123e9d2011-01-06 15:37:30 +0000328 memset( encbuf, 0, 64 );
329 memset( decbuf, 0, 64 );
330
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200331 /* Initialise context */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200334
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200335 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, 128, MBEDTLS_DECRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200342
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200343#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200345#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000346
347 /* decode 0-byte string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000349 TEST_ASSERT( 0 == outlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 TEST_ASSERT( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED == mbedtls_cipher_finish(
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200351 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000352 TEST_ASSERT( 0 == outlen );
353
Paul Bakkerbd51b262014-07-10 15:26:12 +0200354exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_cipher_free( &ctx_dec );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200356}
Paul Bakker33b43f12013-08-20 11:48:36 +0200357/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000358
Paul Bakker33b43f12013-08-20 11:48:36 +0200359/* BEGIN_CASE */
360void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700361 int second_length_val, int pad_mode,
362 int first_encrypt_output_len, int second_encrypt_output_len,
363 int first_decrypt_output_len, int second_decrypt_output_len )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200364{
Paul Bakker33b43f12013-08-20 11:48:36 +0200365 size_t first_length = first_length_val;
366 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000367 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200368 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000369 unsigned char key[32];
370 unsigned char iv[16];
371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_cipher_context_t ctx_dec;
373 mbedtls_cipher_context_t ctx_enc;
374 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000375
376 unsigned char inbuf[64];
377 unsigned char encbuf[64];
378 unsigned char decbuf[64];
379
Paul Bakker23986e52011-04-24 08:57:21 +0000380 size_t outlen = 0;
381 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000382
383 memset( key, 0, 32 );
384 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_cipher_init( &ctx_dec );
387 mbedtls_cipher_init( &ctx_enc );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200388
Paul Bakker8123e9d2011-01-06 15:37:30 +0000389 memset( inbuf, 5, 64 );
390 memset( encbuf, 0, 64 );
391 memset( decbuf, 0, 64 );
392
393 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000395 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200396
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200397 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
398 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
401 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000402
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700403#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
404 if( -1 != pad_mode )
405 {
406 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
407 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
408 }
409#else
410 (void) pad_mode;
411#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
414 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
417 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200418
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200419#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
421 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200422#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000423
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200424 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
425 TEST_ASSERT( block_size != 0 );
426
Paul Bakker8123e9d2011-01-06 15:37:30 +0000427 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700429 TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000430 totaloutlen = outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700432 TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000433 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200434 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200435 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200436 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200437 totaloutlen + block_size > length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000440 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200441 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200442 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200443 totaloutlen > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200444 totaloutlen <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000445
446 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700447 second_length = totaloutlen - first_length;
448 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
449 TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200450 totaloutlen = outlen;
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700451 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
452 TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
453 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200454
455 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200456 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200457 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200458 totaloutlen + block_size >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200459
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700460 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200461 totaloutlen += outlen;
462
463 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000464
465 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
466
Paul Bakkerbd51b262014-07-10 15:26:12 +0200467exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_cipher_free( &ctx_dec );
469 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200470}
Paul Bakker33b43f12013-08-20 11:48:36 +0200471/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000472
Paul Bakker33b43f12013-08-20 11:48:36 +0200473/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100474void decrypt_test_vec( int cipher_id, int pad_mode, HexParam_t * key,
475 HexParam_t * iv, HexParam_t * cipher,
476 HexParam_t * clear, HexParam_t * ad, HexParam_t * tag,
477 int finish_result, int tag_result )
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200478{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200479 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200481 size_t outlen, total_len;
482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200484
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200485 memset( output, 0x00, sizeof( output ) );
486
Azim Khanf1aaec92017-05-30 14:23:15 +0100487#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100488 ((void) ad);
489 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200490#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200491
492 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200493 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_cipher_info_from_type( cipher_id ) ) );
Azim Khand30ca132017-06-09 04:32:58 +0100495 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200497 if( pad_mode != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200499#else
500 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Azim Khand30ca132017-06-09 04:32:58 +0100502 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv->x, iv->len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200504#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100505 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad->x, ad->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200506#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200507
Azim Khand30ca132017-06-09 04:32:58 +0100508 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200509 total_len = 0;
Azim Khand30ca132017-06-09 04:32:58 +0100510 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher->x, cipher->len, output, &outlen ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200511 total_len += outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200513 &outlen ) );
514 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200515#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100516 TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag->x, tag->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200517#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200518
519 /* check plaintext only if everything went fine */
520 if( 0 == finish_result && 0 == tag_result )
521 {
Azim Khand30ca132017-06-09 04:32:58 +0100522 TEST_ASSERT( total_len == clear->len );
523 TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200524 }
525
Paul Bakkerbd51b262014-07-10 15:26:12 +0200526exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200528}
529/* END_CASE */
530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */
Azim Khand30ca132017-06-09 04:32:58 +0100532void auth_crypt_tv( int cipher_id, HexParam_t * key, HexParam_t * iv,
533 HexParam_t * ad, HexParam_t * cipher, HexParam_t * tag,
534 char * result, HexParam_t * clear )
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200535{
536 int ret;
Manuel Pégourié-Gonnard69767d12018-05-09 12:25:18 +0200537 unsigned char output[267]; /* above + 2 (overwrite check) */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200538 unsigned char my_tag[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200540 size_t outlen;
541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200543
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200544 memset( output, 0xFF, sizeof( output ) );
Azim Khan46c9b1f2017-05-31 20:46:35 +0100545 memset( my_tag, 0xFF, sizeof( my_tag ) );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200546
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200547
548 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200549 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_cipher_info_from_type( cipher_id ) ) );
Azim Khand30ca132017-06-09 04:32:58 +0100551 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200552
Azim Khand30ca132017-06-09 04:32:58 +0100553 /* decode buffer and check tag->x */
554 ret = mbedtls_cipher_auth_decrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
555 cipher->x, cipher->len, output, &outlen,
556 tag->x, tag->len );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200557
558 /* make sure we didn't overwrite */
559 TEST_ASSERT( output[outlen + 0] == 0xFF );
560 TEST_ASSERT( output[outlen + 1] == 0xFF );
561
562 /* make sure the message is rejected if it should be */
Azim Khan46c9b1f2017-05-31 20:46:35 +0100563 if( strcmp( result, "FAIL" ) == 0 )
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200566 goto exit;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200567 }
568
569 /* otherwise, make sure it was decrypted properly */
570 TEST_ASSERT( ret == 0 );
571
Azim Khand30ca132017-06-09 04:32:58 +0100572 TEST_ASSERT( outlen == clear->len );
573 TEST_ASSERT( memcmp( output, clear->x, clear->len ) == 0 );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200574
Azim Khand30ca132017-06-09 04:32:58 +0100575 /* then encrypt the clear->x and make sure we get the same ciphertext and tag->x */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200576 memset( output, 0xFF, sizeof( output ) );
577 outlen = 0;
578
Azim Khand30ca132017-06-09 04:32:58 +0100579 ret = mbedtls_cipher_auth_encrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
580 clear->x, clear->len, output, &outlen,
581 my_tag, tag->len );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200582 TEST_ASSERT( ret == 0 );
583
Azim Khand30ca132017-06-09 04:32:58 +0100584 TEST_ASSERT( outlen == clear->len );
585 TEST_ASSERT( memcmp( output, cipher->x, clear->len ) == 0 );
586 TEST_ASSERT( memcmp( my_tag, tag->x, tag->len ) == 0 );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200587
588 /* make sure we didn't overwrite */
589 TEST_ASSERT( output[outlen + 0] == 0xFF );
590 TEST_ASSERT( output[outlen + 1] == 0xFF );
Azim Khand30ca132017-06-09 04:32:58 +0100591 TEST_ASSERT( my_tag[tag->len + 0] == 0xFF );
592 TEST_ASSERT( my_tag[tag->len + 1] == 0xFF );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200593
594
Paul Bakkerbd51b262014-07-10 15:26:12 +0200595exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200597}
598/* END_CASE */
599
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200600/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100601void test_vec_ecb( int cipher_id, int operation, HexParam_t * key,
602 HexParam_t * input, HexParam_t * result, int finish_result
603 )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200604{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200606 unsigned char output[32];
607 size_t outlen;
608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200610
Paul Bakker5e0efa72013-09-08 23:04:04 +0200611 memset( output, 0x00, sizeof( output ) );
612
613 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200614 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_cipher_info_from_type( cipher_id ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200616
Paul Bakker5e0efa72013-09-08 23:04:04 +0200617
Azim Khand30ca132017-06-09 04:32:58 +0100618 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200619
Azim Khand30ca132017-06-09 04:32:58 +0100620 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_cipher_get_block_size( &ctx ),
Paul Bakker5e0efa72013-09-08 23:04:04 +0200622 output, &outlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
624 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200625 &outlen ) );
626 TEST_ASSERT( 0 == outlen );
627
628 /* check plaintext only if everything went fine */
629 if( 0 == finish_result )
Azim Khand30ca132017-06-09 04:32:58 +0100630 TEST_ASSERT( 0 == memcmp( output, result->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_cipher_get_block_size( &ctx ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200632
Paul Bakkerbd51b262014-07-10 15:26:12 +0200633exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 mbedtls_cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200635}
636/* END_CASE */
637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +0200639void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 const mbedtls_cipher_info_t *cipher_info;
642 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200647 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200648 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200651
Paul Bakkerbd51b262014-07-10 15:26:12 +0200652exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200654}
Paul Bakker33b43f12013-08-20 11:48:36 +0200655/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khand30ca132017-06-09 04:32:58 +0100658void check_padding( int pad_mode, HexParam_t * input, int ret, int dlen_check
659 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200660{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_cipher_info_t cipher_info;
662 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +0100663 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200664
665 /* build a fake context just for getting access to get_padding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_cipher_init( &ctx );
667 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200668 ctx.cipher_info = &cipher_info;
669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200671
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200672
Azim Khand30ca132017-06-09 04:32:58 +0100673 TEST_ASSERT( ret == ctx.get_padding( input->x, input->len, &dlen ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200674 if( 0 == ret )
675 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200676}
Paul Bakker33b43f12013-08-20 11:48:36 +0200677/* END_CASE */