blob: 471bcef282233a249f14fda8527270173adecc7d [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
4#if defined(POLARSSL_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
10 * depends_on:POLARSSL_CIPHER_C
11 * END_DEPENDENCIES
12 */
Paul Bakker5690efc2011-05-26 13:16:06 +000013
Paul Bakker33b43f12013-08-20 11:48:36 +020014/* BEGIN_CASE */
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +010015void cipher_list( )
16{
17 const int *cipher_type;
18
19 for( cipher_type = cipher_list(); *cipher_type != 0; cipher_type++ )
20 TEST_ASSERT( cipher_info_from_type( *cipher_type ) != NULL );
21}
22/* END_CASE */
23
24/* BEGIN_CASE */
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020025void cipher_null_args( )
26{
27 cipher_context_t ctx;
28 const cipher_info_t *info = cipher_info_from_type( *( cipher_list() ) );
29 unsigned char buf[1] = { 0 };
30 size_t olen;
31
Paul Bakkerd2a2d612014-07-01 15:45:49 +020032 cipher_init( &ctx );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020033
34 TEST_ASSERT( cipher_get_block_size( NULL ) == 0 );
35 TEST_ASSERT( cipher_get_block_size( &ctx ) == 0 );
36
37 TEST_ASSERT( cipher_get_cipher_mode( NULL ) == POLARSSL_MODE_NONE );
38 TEST_ASSERT( cipher_get_cipher_mode( &ctx ) == POLARSSL_MODE_NONE );
39
40 TEST_ASSERT( cipher_get_iv_size( NULL ) == 0 );
41 TEST_ASSERT( cipher_get_iv_size( &ctx ) == 0 );
42
43 TEST_ASSERT( cipher_info_from_string( NULL ) == NULL );
44
45 TEST_ASSERT( cipher_init_ctx( &ctx, NULL )
46 == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
47 TEST_ASSERT( cipher_init_ctx( NULL, info )
48 == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
49
50 TEST_ASSERT( cipher_setkey( NULL, buf, 0, POLARSSL_ENCRYPT )
51 == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
52 TEST_ASSERT( cipher_setkey( &ctx, buf, 0, POLARSSL_ENCRYPT )
53 == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
54
55 TEST_ASSERT( cipher_set_iv( NULL, buf, 0 )
56 == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
57 TEST_ASSERT( cipher_set_iv( &ctx, buf, 0 )
58 == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
59
60 TEST_ASSERT( cipher_reset( NULL ) == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
61 TEST_ASSERT( cipher_reset( &ctx ) == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
62
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +020063#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020064 TEST_ASSERT( cipher_update_ad( NULL, buf, 0 )
65 == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
66 TEST_ASSERT( cipher_update_ad( &ctx, buf, 0 )
67 == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
68#endif
69
70 TEST_ASSERT( cipher_update( NULL, buf, 0, buf, &olen )
71 == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
72 TEST_ASSERT( cipher_update( &ctx, buf, 0, buf, &olen )
73 == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
74
75 TEST_ASSERT( cipher_finish( NULL, buf, &olen )
76 == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
77 TEST_ASSERT( cipher_finish( &ctx, buf, &olen )
78 == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
79
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +020080#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020081 TEST_ASSERT( cipher_write_tag( NULL, buf, olen )
82 == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
83 TEST_ASSERT( cipher_write_tag( &ctx, buf, olen )
84 == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
85
86 TEST_ASSERT( cipher_check_tag( NULL, buf, olen )
87 == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
88 TEST_ASSERT( cipher_check_tag( &ctx, buf, olen )
89 == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
90#endif
91}
92/* END_CASE */
93
94/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +020095void enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
96 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +020097{
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020098 size_t length = length_val, outlen, total_len, i;
Paul Bakker8123e9d2011-01-06 15:37:30 +000099 unsigned char key[32];
100 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200101 unsigned char ad[13];
102 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200103 unsigned char inbuf[64];
104 unsigned char encbuf[64];
105 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000106
107 const cipher_info_t *cipher_info;
108 cipher_context_t ctx_dec;
109 cipher_context_t ctx_enc;
110
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200111 /*
112 * Prepare contexts
113 */
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200114 cipher_init( &ctx_dec );
115 cipher_init( &ctx_enc );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200116
117 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000118
119 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +0200120 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000121 TEST_ASSERT( NULL != cipher_info );
Paul Bakker33b43f12013-08-20 11:48:36 +0200122 TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000123
124 /* Initialise enc and dec contexts */
125 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
126 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200127
Paul Bakker33b43f12013-08-20 11:48:36 +0200128 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
129 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000130
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200131#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200132 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200133 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200134 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) );
135 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200136 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200137#else
138 (void) pad_mode;
139#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200140
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200141 /*
142 * Do a few encode/decode cycles
143 */
144 for( i = 0; i < 3; i++ )
145 {
146 memset( iv , 0x00 + i, sizeof( iv ) );
147 memset( ad, 0x10 + i, sizeof( ad ) );
148 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
149
150 memset( encbuf, 0, sizeof( encbuf ) );
151 memset( decbuf, 0, sizeof( decbuf ) );
152 memset( tag, 0, sizeof( tag ) );
153
154 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
155 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200156
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200157 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
158 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
159
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200160#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200161 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
162 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200163#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164
Paul Bakker8123e9d2011-01-06 15:37:30 +0000165 /* encode length number of bytes from inbuf */
166 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200167 total_len = outlen;
168
169 TEST_ASSERT( total_len == length ||
170 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
171 total_len < length &&
172 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000173
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200174 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200175 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000176
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200177#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200178 TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200179#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200180
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200181 TEST_ASSERT( total_len == length ||
182 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
183 total_len > length &&
184 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000185
186 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200187 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
188 total_len = outlen;
189
190 TEST_ASSERT( total_len == length ||
191 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
192 total_len < length &&
193 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000194
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200195 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200196 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000197
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200198#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200199 TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200200#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200201
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200202 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200203 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000204 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200205 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200207 /*
208 * Done
209 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200210exit:
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200211 cipher_free( &ctx_dec );
212 cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200213}
Paul Bakker33b43f12013-08-20 11:48:36 +0200214/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000215
Paul Bakker33b43f12013-08-20 11:48:36 +0200216/* BEGIN_CASE */
217void enc_fail( int cipher_id, int pad_mode, int key_len,
218 int length_val, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200219{
Paul Bakker33b43f12013-08-20 11:48:36 +0200220 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200221 unsigned char key[32];
222 unsigned char iv[16];
223
224 const cipher_info_t *cipher_info;
225 cipher_context_t ctx;
226
227 unsigned char inbuf[64];
228 unsigned char encbuf[64];
229
230 size_t outlen = 0;
231
232 memset( key, 0, 32 );
233 memset( iv , 0, 16 );
234
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200235 cipher_init( &ctx );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200236
237 memset( inbuf, 5, 64 );
238 memset( encbuf, 0, 64 );
239
240 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +0200241 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200242 TEST_ASSERT( NULL != cipher_info );
243
244 /* Initialise context */
245 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200246 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200247#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200248 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200249#else
250 (void) pad_mode;
251#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200252 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200253 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200254#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200255 TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200256#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200257
258 /* encode length number of bytes from inbuf */
259 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200260 TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200261
262 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200263exit:
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200264 cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200265}
Paul Bakker33b43f12013-08-20 11:48:36 +0200266/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200267
Paul Bakker33b43f12013-08-20 11:48:36 +0200268/* BEGIN_CASE */
269void dec_empty_buf()
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200270{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000271 unsigned char key[32];
272 unsigned char iv[16];
273
274 cipher_context_t ctx_dec;
275 const cipher_info_t *cipher_info;
276
277 unsigned char encbuf[64];
278 unsigned char decbuf[64];
279
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000280 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000281
282 memset( key, 0, 32 );
283 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200284
285 cipher_init( &ctx_dec );
286
Paul Bakker8123e9d2011-01-06 15:37:30 +0000287 memset( encbuf, 0, 64 );
288 memset( decbuf, 0, 64 );
289
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200290 /* Initialise context */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000291 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
292 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200293
Paul Bakker8123e9d2011-01-06 15:37:30 +0000294 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
295
296 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
297
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200298 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
299
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200300 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
301
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200302#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200303 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200304#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000305
306 /* decode 0-byte string */
307 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
308 TEST_ASSERT( 0 == outlen );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200309 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish(
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200310 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000311 TEST_ASSERT( 0 == outlen );
312
Paul Bakkerbd51b262014-07-10 15:26:12 +0200313exit:
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200314 cipher_free( &ctx_dec );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200315}
Paul Bakker33b43f12013-08-20 11:48:36 +0200316/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000317
Paul Bakker33b43f12013-08-20 11:48:36 +0200318/* BEGIN_CASE */
319void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
320 int second_length_val )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200321{
Paul Bakker33b43f12013-08-20 11:48:36 +0200322 size_t first_length = first_length_val;
323 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000324 size_t length = first_length + second_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000325 unsigned char key[32];
326 unsigned char iv[16];
327
328 cipher_context_t ctx_dec;
329 cipher_context_t ctx_enc;
330 const cipher_info_t *cipher_info;
331
332 unsigned char inbuf[64];
333 unsigned char encbuf[64];
334 unsigned char decbuf[64];
335
Paul Bakker23986e52011-04-24 08:57:21 +0000336 size_t outlen = 0;
337 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000338
339 memset( key, 0, 32 );
340 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200341
342 cipher_init( &ctx_dec );
343 cipher_init( &ctx_enc );
344
Paul Bakker8123e9d2011-01-06 15:37:30 +0000345 memset( inbuf, 5, 64 );
346 memset( encbuf, 0, 64 );
347 memset( decbuf, 0, 64 );
348
349 /* Initialise enc and dec contexts */
Paul Bakker33b43f12013-08-20 11:48:36 +0200350 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000351 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200352
Paul Bakker8123e9d2011-01-06 15:37:30 +0000353 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
354 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200355
Paul Bakker33b43f12013-08-20 11:48:36 +0200356 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
357 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000358
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200359 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
360 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
361
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200362 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
363 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
364
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200365#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200366 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
367 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200368#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000369
Paul Bakker8123e9d2011-01-06 15:37:30 +0000370 /* encode length number of bytes from inbuf */
371 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
372 totaloutlen = outlen;
373 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
374 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200375 TEST_ASSERT( totaloutlen == length ||
376 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
377 totaloutlen < length &&
378 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
379
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200380 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000381 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200382 TEST_ASSERT( totaloutlen == length ||
383 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
384 totaloutlen > length &&
385 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000386
387 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200388 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
389 totaloutlen = outlen;
390
391 TEST_ASSERT( totaloutlen == length ||
392 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
393 totaloutlen < length &&
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200394 totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200395
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200396 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200397 totaloutlen += outlen;
398
399 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000400
401 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
402
Paul Bakkerbd51b262014-07-10 15:26:12 +0200403exit:
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200404 cipher_free( &ctx_dec );
405 cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200406}
Paul Bakker33b43f12013-08-20 11:48:36 +0200407/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000408
Paul Bakker33b43f12013-08-20 11:48:36 +0200409/* BEGIN_CASE */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200410void decrypt_test_vec( int cipher_id, int pad_mode,
411 char *hex_key, char *hex_iv,
412 char *hex_cipher, char *hex_clear,
413 char *hex_ad, char *hex_tag,
414 int finish_result, int tag_result )
415{
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200416 unsigned char key[50];
417 unsigned char iv[50];
418 unsigned char cipher[200];
419 unsigned char clear[200];
420 unsigned char ad[200];
421 unsigned char tag[20];
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200422 size_t key_len, iv_len, cipher_len, clear_len;
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200423#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200424 size_t ad_len, tag_len;
425#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200426 cipher_context_t ctx;
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200427 unsigned char output[200];
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200428 size_t outlen, total_len;
429
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200430 cipher_init( &ctx );
431
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200432 memset( key, 0x00, sizeof( key ) );
433 memset( iv, 0x00, sizeof( iv ) );
434 memset( cipher, 0x00, sizeof( cipher ) );
435 memset( clear, 0x00, sizeof( clear ) );
436 memset( ad, 0x00, sizeof( ad ) );
437 memset( tag, 0x00, sizeof( tag ) );
438 memset( output, 0x00, sizeof( output ) );
439
440 key_len = unhexify( key, hex_key );
441 iv_len = unhexify( iv, hex_iv );
442 cipher_len = unhexify( cipher, hex_cipher );
443 clear_len = unhexify( clear, hex_clear );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200444#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200445 ad_len = unhexify( ad, hex_ad );
446 tag_len = unhexify( tag, hex_tag );
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200447#else
448 ((void) hex_ad);
449 ((void) hex_tag);
450#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200451
452 /* Prepare context */
453 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
454 cipher_info_from_type( cipher_id ) ) );
455 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200456#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200457 if( pad_mode != -1 )
458 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200459#else
460 (void) pad_mode;
461#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200462 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
463 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200464#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200465 TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200466#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200467
468 /* decode buffer and check tag */
469 total_len = 0;
470 TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
471 total_len += outlen;
472 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
473 &outlen ) );
474 total_len += outlen;
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200475#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200476 TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200477#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200478
479 /* check plaintext only if everything went fine */
480 if( 0 == finish_result && 0 == tag_result )
481 {
482 TEST_ASSERT( total_len == clear_len );
483 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
484 }
485
Paul Bakkerbd51b262014-07-10 15:26:12 +0200486exit:
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200487 cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200488}
489/* END_CASE */
490
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200491/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_AEAD */
492void auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv,
493 char *hex_ad, char *hex_cipher,
494 char *hex_tag, char *hex_clear )
495{
496 int ret;
497 unsigned char key[50];
498 unsigned char iv[50];
499 unsigned char cipher[200];
500 unsigned char clear[200];
501 unsigned char ad[200];
502 unsigned char tag[20];
503 unsigned char my_tag[20];
504 size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
505 cipher_context_t ctx;
506 unsigned char output[200];
507 size_t outlen;
508
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200509 cipher_init( &ctx );
510
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200511 memset( key, 0x00, sizeof( key ) );
512 memset( iv, 0x00, sizeof( iv ) );
513 memset( cipher, 0x00, sizeof( cipher ) );
514 memset( clear, 0x00, sizeof( clear ) );
515 memset( ad, 0x00, sizeof( ad ) );
516 memset( tag, 0x00, sizeof( tag ) );
517 memset( my_tag, 0xFF, sizeof( my_tag ) );
518 memset( output, 0xFF, sizeof( output ) );
519
520 key_len = unhexify( key, hex_key );
521 iv_len = unhexify( iv, hex_iv );
522 cipher_len = unhexify( cipher, hex_cipher );
523 ad_len = unhexify( ad, hex_ad );
524 tag_len = unhexify( tag, hex_tag );
525
526 /* Prepare context */
527 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
528 cipher_info_from_type( cipher_id ) ) );
529 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
530
531 /* decode buffer and check tag */
532 ret = cipher_auth_decrypt( &ctx, iv, iv_len, ad, ad_len,
533 cipher, cipher_len, output, &outlen,
534 tag, tag_len );
535
536 /* make sure we didn't overwrite */
537 TEST_ASSERT( output[outlen + 0] == 0xFF );
538 TEST_ASSERT( output[outlen + 1] == 0xFF );
539
540 /* make sure the message is rejected if it should be */
541 if( strcmp( hex_clear, "FAIL" ) == 0 )
542 {
543 TEST_ASSERT( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200544 goto exit;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200545 }
546
547 /* otherwise, make sure it was decrypted properly */
548 TEST_ASSERT( ret == 0 );
549
550 clear_len = unhexify( clear, hex_clear );
551 TEST_ASSERT( outlen == clear_len );
552 TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 );
553
554 /* then encrypt the clear and make sure we get the same ciphertext and tag */
555 memset( output, 0xFF, sizeof( output ) );
556 outlen = 0;
557
558 ret = cipher_auth_encrypt( &ctx, iv, iv_len, ad, ad_len,
559 clear, clear_len, output, &outlen,
560 my_tag, tag_len );
561 TEST_ASSERT( ret == 0 );
562
563 TEST_ASSERT( outlen == clear_len );
564 TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 );
565 TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 );
566
567 /* make sure we didn't overwrite */
568 TEST_ASSERT( output[outlen + 0] == 0xFF );
569 TEST_ASSERT( output[outlen + 1] == 0xFF );
570 TEST_ASSERT( my_tag[tag_len + 0] == 0xFF );
571 TEST_ASSERT( my_tag[tag_len + 1] == 0xFF );
572
573
Paul Bakkerbd51b262014-07-10 15:26:12 +0200574exit:
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200575 cipher_free( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200576}
577/* END_CASE */
578
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200579/* BEGIN_CASE */
Paul Bakker5e0efa72013-09-08 23:04:04 +0200580void test_vec_ecb( int cipher_id, int operation, char *hex_key,
581 char *hex_input, char *hex_result,
582 int finish_result )
583{
584 unsigned char key[50];
585 unsigned char input[16];
586 unsigned char result[16];
587 size_t key_len;
588 cipher_context_t ctx;
589 unsigned char output[32];
590 size_t outlen;
591
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200592 cipher_init( &ctx );
593
Paul Bakker5e0efa72013-09-08 23:04:04 +0200594 memset( key, 0x00, sizeof( key ) );
595 memset( input, 0x00, sizeof( input ) );
596 memset( result, 0x00, sizeof( result ) );
597 memset( output, 0x00, sizeof( output ) );
598
599 /* Prepare context */
600 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
601 cipher_info_from_type( cipher_id ) ) );
602
603 key_len = unhexify( key, hex_key );
604 TEST_ASSERT( unhexify( input, hex_input ) ==
605 (int) cipher_get_block_size( &ctx ) );
606 TEST_ASSERT( unhexify( result, hex_result ) ==
607 (int) cipher_get_block_size( &ctx ) );
608
609 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) );
610
611 TEST_ASSERT( 0 == cipher_update( &ctx, input,
612 cipher_get_block_size( &ctx ),
613 output, &outlen ) );
614 TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) );
615 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
616 &outlen ) );
617 TEST_ASSERT( 0 == outlen );
618
619 /* check plaintext only if everything went fine */
620 if( 0 == finish_result )
621 TEST_ASSERT( 0 == memcmp( output, result,
622 cipher_get_block_size( &ctx ) ) );
623
Paul Bakkerbd51b262014-07-10 15:26:12 +0200624exit:
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200625 cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200626}
627/* END_CASE */
628
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200629/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +0200630void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200631{
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200632 const cipher_info_t *cipher_info;
633 cipher_context_t ctx;
634
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200635 cipher_init( &ctx );
636
Paul Bakker33b43f12013-08-20 11:48:36 +0200637 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200638 TEST_ASSERT( NULL != cipher_info );
639 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
640
Paul Bakker33b43f12013-08-20 11:48:36 +0200641 TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200642
Paul Bakkerbd51b262014-07-10 15:26:12 +0200643exit:
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200644 cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200645}
Paul Bakker33b43f12013-08-20 11:48:36 +0200646/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000647
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200648/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200649void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200650{
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200651 cipher_info_t cipher_info;
652 cipher_context_t ctx;
653 unsigned char input[16];
654 size_t ilen, dlen;
655
656 /* build a fake context just for getting access to get_padding */
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200657 cipher_init( &ctx );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200658 cipher_info.mode = POLARSSL_MODE_CBC;
659 ctx.cipher_info = &cipher_info;
660
Paul Bakker33b43f12013-08-20 11:48:36 +0200661 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200662
Paul Bakker33b43f12013-08-20 11:48:36 +0200663 ilen = unhexify( input, input_str );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200664
Paul Bakker33b43f12013-08-20 11:48:36 +0200665 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
666 if( 0 == ret )
667 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200668}
Paul Bakker33b43f12013-08-20 11:48:36 +0200669/* END_CASE */
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200670
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200671/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200672void cipher_selftest()
Paul Bakker8123e9d2011-01-06 15:37:30 +0000673{
674 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
675}
Paul Bakker33b43f12013-08-20 11:48:36 +0200676/* END_CASE */