blob: 09ae2e08a7af0185b1f1dd0b71598137f6669663 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +00002#include <polarssl/cipher.h>
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +02003
4#if defined(POLARSSL_GCM_C)
5#include <polarssl/gcm.h>
6#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
32 memset( &ctx, 0, sizeof( cipher_context_t ) );
33
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
63#if defined(POLARSSL_CIPHER_MODE_AEAD)
64 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
80#if defined(POLARSSL_CIPHER_MODE_AEAD)
81 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 Bakker8123e9d2011-01-06 15:37:30 +0000114 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
115 memset( &ctx_enc, 0, sizeof( 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é-Gonnard43a47802013-09-03 16:35:53 +0200160#if defined(POLARSSL_CIPHER_MODE_AEAD)
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é-Gonnard43a47802013-09-03 16:35:53 +0200163#endif /* POLARSSL_CIPHER_MODE_AEAD */
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é-Gonnard43a47802013-09-03 16:35:53 +0200177#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200178 TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200179#endif /* POLARSSL_CIPHER_MODE_AEAD */
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é-Gonnard43a47802013-09-03 16:35:53 +0200198#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200199 TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200200#endif /* POLARSSL_CIPHER_MODE_AEAD */
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 Bakker8123e9d2011-01-06 15:37:30 +0000210 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
211 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200212}
Paul Bakker33b43f12013-08-20 11:48:36 +0200213/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000214
Paul Bakker33b43f12013-08-20 11:48:36 +0200215/* BEGIN_CASE */
216void enc_fail( int cipher_id, int pad_mode, int key_len,
217 int length_val, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200218{
Paul Bakker33b43f12013-08-20 11:48:36 +0200219 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200220 unsigned char key[32];
221 unsigned char iv[16];
222
223 const cipher_info_t *cipher_info;
224 cipher_context_t ctx;
225
226 unsigned char inbuf[64];
227 unsigned char encbuf[64];
228
229 size_t outlen = 0;
230
231 memset( key, 0, 32 );
232 memset( iv , 0, 16 );
233
234 memset( &ctx, 0, sizeof( ctx ) );
235
236 memset( inbuf, 5, 64 );
237 memset( encbuf, 0, 64 );
238
239 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +0200240 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200241 TEST_ASSERT( NULL != cipher_info );
242
243 /* Initialise context */
244 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200245 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200246#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200247 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200248#else
249 (void) pad_mode;
250#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200251 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200252 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200253#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200254 TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200255#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200256
257 /* encode length number of bytes from inbuf */
258 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200259 TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200260
261 /* done */
262 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200263}
Paul Bakker33b43f12013-08-20 11:48:36 +0200264/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200265
Paul Bakker33b43f12013-08-20 11:48:36 +0200266/* BEGIN_CASE */
267void dec_empty_buf()
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200268{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000269 unsigned char key[32];
270 unsigned char iv[16];
271
272 cipher_context_t ctx_dec;
273 const cipher_info_t *cipher_info;
274
275 unsigned char encbuf[64];
276 unsigned char decbuf[64];
277
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000278 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000279
280 memset( key, 0, 32 );
281 memset( iv , 0, 16 );
282
283 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
284
285 memset( encbuf, 0, 64 );
286 memset( decbuf, 0, 64 );
287
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200288 /* Initialise context */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000289 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
290 TEST_ASSERT( NULL != cipher_info);
291
292 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
293
294 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
295
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200296 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
297
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200298 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
299
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200300#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200301 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200302#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000303
304 /* decode 0-byte string */
305 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
306 TEST_ASSERT( 0 == outlen );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200307 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish(
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200308 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000309 TEST_ASSERT( 0 == outlen );
310
311 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200312}
Paul Bakker33b43f12013-08-20 11:48:36 +0200313/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000314
Paul Bakker33b43f12013-08-20 11:48:36 +0200315/* BEGIN_CASE */
316void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
317 int second_length_val )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200318{
Paul Bakker33b43f12013-08-20 11:48:36 +0200319 size_t first_length = first_length_val;
320 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000321 size_t length = first_length + second_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000322 unsigned char key[32];
323 unsigned char iv[16];
324
325 cipher_context_t ctx_dec;
326 cipher_context_t ctx_enc;
327 const cipher_info_t *cipher_info;
328
329 unsigned char inbuf[64];
330 unsigned char encbuf[64];
331 unsigned char decbuf[64];
332
Paul Bakker23986e52011-04-24 08:57:21 +0000333 size_t outlen = 0;
334 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000335
336 memset( key, 0, 32 );
337 memset( iv , 0, 16 );
338
339 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
340 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
341
342 memset( inbuf, 5, 64 );
343 memset( encbuf, 0, 64 );
344 memset( decbuf, 0, 64 );
345
346 /* Initialise enc and dec contexts */
Paul Bakker33b43f12013-08-20 11:48:36 +0200347 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000348 TEST_ASSERT( NULL != cipher_info);
349
350 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
351 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
352
Paul Bakker33b43f12013-08-20 11:48:36 +0200353 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
354 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000355
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200356 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
357 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
358
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200359 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
360 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
361
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200362#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200363 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
364 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200365#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000366
Paul Bakker8123e9d2011-01-06 15:37:30 +0000367 /* encode length number of bytes from inbuf */
368 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
369 totaloutlen = outlen;
370 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
371 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200372 TEST_ASSERT( totaloutlen == length ||
373 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
374 totaloutlen < length &&
375 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
376
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200377 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200379 TEST_ASSERT( totaloutlen == length ||
380 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
381 totaloutlen > length &&
382 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000383
384 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200385 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
386 totaloutlen = outlen;
387
388 TEST_ASSERT( totaloutlen == length ||
389 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
390 totaloutlen < length &&
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200391 totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200392
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200393 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200394 totaloutlen += outlen;
395
396 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000397
398 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
399
400 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
401 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200402}
Paul Bakker33b43f12013-08-20 11:48:36 +0200403/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000404
Paul Bakker33b43f12013-08-20 11:48:36 +0200405/* BEGIN_CASE */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200406void decrypt_test_vec( int cipher_id, int pad_mode,
407 char *hex_key, char *hex_iv,
408 char *hex_cipher, char *hex_clear,
409 char *hex_ad, char *hex_tag,
410 int finish_result, int tag_result )
411{
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200412 unsigned char key[50];
413 unsigned char iv[50];
414 unsigned char cipher[200];
415 unsigned char clear[200];
416 unsigned char ad[200];
417 unsigned char tag[20];
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200418 size_t key_len, iv_len, cipher_len, clear_len;
419#if defined(POLARSSL_CIPHER_MODE_AEAD)
420 size_t ad_len, tag_len;
421#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200422 cipher_context_t ctx;
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200423 unsigned char output[200];
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200424 size_t outlen, total_len;
425
426 memset( key, 0x00, sizeof( key ) );
427 memset( iv, 0x00, sizeof( iv ) );
428 memset( cipher, 0x00, sizeof( cipher ) );
429 memset( clear, 0x00, sizeof( clear ) );
430 memset( ad, 0x00, sizeof( ad ) );
431 memset( tag, 0x00, sizeof( tag ) );
432 memset( output, 0x00, sizeof( output ) );
433
434 key_len = unhexify( key, hex_key );
435 iv_len = unhexify( iv, hex_iv );
436 cipher_len = unhexify( cipher, hex_cipher );
437 clear_len = unhexify( clear, hex_clear );
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200438#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200439 ad_len = unhexify( ad, hex_ad );
440 tag_len = unhexify( tag, hex_tag );
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200441#else
442 ((void) hex_ad);
443 ((void) hex_tag);
444#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200445
446 /* Prepare context */
447 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
448 cipher_info_from_type( cipher_id ) ) );
449 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200450#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200451 if( pad_mode != -1 )
452 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200453#else
454 (void) pad_mode;
455#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200456 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
457 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
458#if defined(POLARSSL_CIPHER_MODE_AEAD)
459 TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
460#endif /* POLARSSL_CIPHER_MODE_AEAD */
461
462 /* decode buffer and check tag */
463 total_len = 0;
464 TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
465 total_len += outlen;
466 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
467 &outlen ) );
468 total_len += outlen;
469#if defined(POLARSSL_CIPHER_MODE_AEAD)
470 TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
471#endif /* POLARSSL_CIPHER_MODE_AEAD */
472
473 /* check plaintext only if everything went fine */
474 if( 0 == finish_result && 0 == tag_result )
475 {
476 TEST_ASSERT( total_len == clear_len );
477 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
478 }
479
480 cipher_free_ctx( &ctx );
481}
482/* END_CASE */
483
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200484/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_AEAD */
485void auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv,
486 char *hex_ad, char *hex_cipher,
487 char *hex_tag, char *hex_clear )
488{
489 int ret;
490 unsigned char key[50];
491 unsigned char iv[50];
492 unsigned char cipher[200];
493 unsigned char clear[200];
494 unsigned char ad[200];
495 unsigned char tag[20];
496 unsigned char my_tag[20];
497 size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
498 cipher_context_t ctx;
499 unsigned char output[200];
500 size_t outlen;
501
502 memset( key, 0x00, sizeof( key ) );
503 memset( iv, 0x00, sizeof( iv ) );
504 memset( cipher, 0x00, sizeof( cipher ) );
505 memset( clear, 0x00, sizeof( clear ) );
506 memset( ad, 0x00, sizeof( ad ) );
507 memset( tag, 0x00, sizeof( tag ) );
508 memset( my_tag, 0xFF, sizeof( my_tag ) );
509 memset( output, 0xFF, sizeof( output ) );
510
511 key_len = unhexify( key, hex_key );
512 iv_len = unhexify( iv, hex_iv );
513 cipher_len = unhexify( cipher, hex_cipher );
514 ad_len = unhexify( ad, hex_ad );
515 tag_len = unhexify( tag, hex_tag );
516
517 /* Prepare context */
518 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
519 cipher_info_from_type( cipher_id ) ) );
520 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
521
522 /* decode buffer and check tag */
523 ret = cipher_auth_decrypt( &ctx, iv, iv_len, ad, ad_len,
524 cipher, cipher_len, output, &outlen,
525 tag, tag_len );
526
527 /* make sure we didn't overwrite */
528 TEST_ASSERT( output[outlen + 0] == 0xFF );
529 TEST_ASSERT( output[outlen + 1] == 0xFF );
530
531 /* make sure the message is rejected if it should be */
532 if( strcmp( hex_clear, "FAIL" ) == 0 )
533 {
534 TEST_ASSERT( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED );
535 goto cleanup;
536 }
537
538 /* otherwise, make sure it was decrypted properly */
539 TEST_ASSERT( ret == 0 );
540
541 clear_len = unhexify( clear, hex_clear );
542 TEST_ASSERT( outlen == clear_len );
543 TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 );
544
545 /* then encrypt the clear and make sure we get the same ciphertext and tag */
546 memset( output, 0xFF, sizeof( output ) );
547 outlen = 0;
548
549 ret = cipher_auth_encrypt( &ctx, iv, iv_len, ad, ad_len,
550 clear, clear_len, output, &outlen,
551 my_tag, tag_len );
552 TEST_ASSERT( ret == 0 );
553
554 TEST_ASSERT( outlen == clear_len );
555 TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 );
556 TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 );
557
558 /* make sure we didn't overwrite */
559 TEST_ASSERT( output[outlen + 0] == 0xFF );
560 TEST_ASSERT( output[outlen + 1] == 0xFF );
561 TEST_ASSERT( my_tag[tag_len + 0] == 0xFF );
562 TEST_ASSERT( my_tag[tag_len + 1] == 0xFF );
563
564
565cleanup:
566 cipher_free_ctx( &ctx );
567}
568/* END_CASE */
569
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200570/* BEGIN_CASE */
Paul Bakker5e0efa72013-09-08 23:04:04 +0200571void test_vec_ecb( int cipher_id, int operation, char *hex_key,
572 char *hex_input, char *hex_result,
573 int finish_result )
574{
575 unsigned char key[50];
576 unsigned char input[16];
577 unsigned char result[16];
578 size_t key_len;
579 cipher_context_t ctx;
580 unsigned char output[32];
581 size_t outlen;
582
583 memset( key, 0x00, sizeof( key ) );
584 memset( input, 0x00, sizeof( input ) );
585 memset( result, 0x00, sizeof( result ) );
586 memset( output, 0x00, sizeof( output ) );
587
588 /* Prepare context */
589 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
590 cipher_info_from_type( cipher_id ) ) );
591
592 key_len = unhexify( key, hex_key );
593 TEST_ASSERT( unhexify( input, hex_input ) ==
594 (int) cipher_get_block_size( &ctx ) );
595 TEST_ASSERT( unhexify( result, hex_result ) ==
596 (int) cipher_get_block_size( &ctx ) );
597
598 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) );
599
600 TEST_ASSERT( 0 == cipher_update( &ctx, input,
601 cipher_get_block_size( &ctx ),
602 output, &outlen ) );
603 TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) );
604 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
605 &outlen ) );
606 TEST_ASSERT( 0 == outlen );
607
608 /* check plaintext only if everything went fine */
609 if( 0 == finish_result )
610 TEST_ASSERT( 0 == memcmp( output, result,
611 cipher_get_block_size( &ctx ) ) );
612
613 cipher_free_ctx( &ctx );
614}
615/* END_CASE */
616
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200617/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +0200618void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200619{
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200620 const cipher_info_t *cipher_info;
621 cipher_context_t ctx;
622
Paul Bakker33b43f12013-08-20 11:48:36 +0200623 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200624 TEST_ASSERT( NULL != cipher_info );
625 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
626
Paul Bakker33b43f12013-08-20 11:48:36 +0200627 TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200628
629 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200630}
Paul Bakker33b43f12013-08-20 11:48:36 +0200631/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000632
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200633/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200634void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200635{
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200636 cipher_info_t cipher_info;
637 cipher_context_t ctx;
638 unsigned char input[16];
639 size_t ilen, dlen;
640
641 /* build a fake context just for getting access to get_padding */
642 memset( &ctx, 0, sizeof( ctx ) );
643 cipher_info.mode = POLARSSL_MODE_CBC;
644 ctx.cipher_info = &cipher_info;
645
Paul Bakker33b43f12013-08-20 11:48:36 +0200646 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200647
Paul Bakker33b43f12013-08-20 11:48:36 +0200648 ilen = unhexify( input, input_str );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200649
Paul Bakker33b43f12013-08-20 11:48:36 +0200650 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
651 if( 0 == ret )
652 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200653}
Paul Bakker33b43f12013-08-20 11:48:36 +0200654/* END_CASE */
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200655
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200656/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200657void cipher_selftest()
Paul Bakker8123e9d2011-01-06 15:37:30 +0000658{
659 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
660}
Paul Bakker33b43f12013-08-20 11:48:36 +0200661/* END_CASE */