blob: 3afdff606b0d38d1f468d69b5980907d48c2ebc9 [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
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 Bakkerd2a2d612014-07-01 15:45:49 +0200210 cipher_free( &ctx_dec );
211 cipher_free( &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
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200234 cipher_init( &ctx );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200235
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é-Gonnard8f625632014-06-24 15:26:28 +0200253#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200254 TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200255#endif
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 */
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200262 cipher_free( &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 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200282
283 cipher_init( &ctx_dec );
284
Paul Bakker8123e9d2011-01-06 15:37:30 +0000285 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);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200291
Paul Bakker8123e9d2011-01-06 15:37:30 +0000292 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é-Gonnard8f625632014-06-24 15:26:28 +0200300#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200301 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200302#endif
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
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200311 cipher_free( &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 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200338
339 cipher_init( &ctx_dec );
340 cipher_init( &ctx_enc );
341
Paul Bakker8123e9d2011-01-06 15:37:30 +0000342 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);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200349
Paul Bakker8123e9d2011-01-06 15:37:30 +0000350 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
351 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200352
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é-Gonnard8f625632014-06-24 15:26:28 +0200362#if defined(POLARSSL_GCM_C)
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é-Gonnard8f625632014-06-24 15:26:28 +0200365#endif
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
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200400 cipher_free( &ctx_dec );
401 cipher_free( &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;
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200419#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200420 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
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200426 cipher_init( &ctx );
427
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200428 memset( key, 0x00, sizeof( key ) );
429 memset( iv, 0x00, sizeof( iv ) );
430 memset( cipher, 0x00, sizeof( cipher ) );
431 memset( clear, 0x00, sizeof( clear ) );
432 memset( ad, 0x00, sizeof( ad ) );
433 memset( tag, 0x00, sizeof( tag ) );
434 memset( output, 0x00, sizeof( output ) );
435
436 key_len = unhexify( key, hex_key );
437 iv_len = unhexify( iv, hex_iv );
438 cipher_len = unhexify( cipher, hex_cipher );
439 clear_len = unhexify( clear, hex_clear );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200440#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200441 ad_len = unhexify( ad, hex_ad );
442 tag_len = unhexify( tag, hex_tag );
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200443#else
444 ((void) hex_ad);
445 ((void) hex_tag);
446#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200447
448 /* Prepare context */
449 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
450 cipher_info_from_type( cipher_id ) ) );
451 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200452#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200453 if( pad_mode != -1 )
454 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200455#else
456 (void) pad_mode;
457#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200458 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
459 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200460#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200461 TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200462#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200463
464 /* decode buffer and check tag */
465 total_len = 0;
466 TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
467 total_len += outlen;
468 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
469 &outlen ) );
470 total_len += outlen;
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200471#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200472 TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200473#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200474
475 /* check plaintext only if everything went fine */
476 if( 0 == finish_result && 0 == tag_result )
477 {
478 TEST_ASSERT( total_len == clear_len );
479 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
480 }
481
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200482 cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200483}
484/* END_CASE */
485
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200486/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_AEAD */
487void auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv,
488 char *hex_ad, char *hex_cipher,
489 char *hex_tag, char *hex_clear )
490{
491 int ret;
492 unsigned char key[50];
493 unsigned char iv[50];
494 unsigned char cipher[200];
495 unsigned char clear[200];
496 unsigned char ad[200];
497 unsigned char tag[20];
498 unsigned char my_tag[20];
499 size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
500 cipher_context_t ctx;
501 unsigned char output[200];
502 size_t outlen;
503
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200504 cipher_init( &ctx );
505
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200506 memset( key, 0x00, sizeof( key ) );
507 memset( iv, 0x00, sizeof( iv ) );
508 memset( cipher, 0x00, sizeof( cipher ) );
509 memset( clear, 0x00, sizeof( clear ) );
510 memset( ad, 0x00, sizeof( ad ) );
511 memset( tag, 0x00, sizeof( tag ) );
512 memset( my_tag, 0xFF, sizeof( my_tag ) );
513 memset( output, 0xFF, sizeof( output ) );
514
515 key_len = unhexify( key, hex_key );
516 iv_len = unhexify( iv, hex_iv );
517 cipher_len = unhexify( cipher, hex_cipher );
518 ad_len = unhexify( ad, hex_ad );
519 tag_len = unhexify( tag, hex_tag );
520
521 /* Prepare context */
522 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
523 cipher_info_from_type( cipher_id ) ) );
524 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
525
526 /* decode buffer and check tag */
527 ret = cipher_auth_decrypt( &ctx, iv, iv_len, ad, ad_len,
528 cipher, cipher_len, output, &outlen,
529 tag, tag_len );
530
531 /* make sure we didn't overwrite */
532 TEST_ASSERT( output[outlen + 0] == 0xFF );
533 TEST_ASSERT( output[outlen + 1] == 0xFF );
534
535 /* make sure the message is rejected if it should be */
536 if( strcmp( hex_clear, "FAIL" ) == 0 )
537 {
538 TEST_ASSERT( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED );
539 goto cleanup;
540 }
541
542 /* otherwise, make sure it was decrypted properly */
543 TEST_ASSERT( ret == 0 );
544
545 clear_len = unhexify( clear, hex_clear );
546 TEST_ASSERT( outlen == clear_len );
547 TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 );
548
549 /* then encrypt the clear and make sure we get the same ciphertext and tag */
550 memset( output, 0xFF, sizeof( output ) );
551 outlen = 0;
552
553 ret = cipher_auth_encrypt( &ctx, iv, iv_len, ad, ad_len,
554 clear, clear_len, output, &outlen,
555 my_tag, tag_len );
556 TEST_ASSERT( ret == 0 );
557
558 TEST_ASSERT( outlen == clear_len );
559 TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 );
560 TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 );
561
562 /* make sure we didn't overwrite */
563 TEST_ASSERT( output[outlen + 0] == 0xFF );
564 TEST_ASSERT( output[outlen + 1] == 0xFF );
565 TEST_ASSERT( my_tag[tag_len + 0] == 0xFF );
566 TEST_ASSERT( my_tag[tag_len + 1] == 0xFF );
567
568
569cleanup:
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200570 cipher_free( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200571}
572/* END_CASE */
573
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200574/* BEGIN_CASE */
Paul Bakker5e0efa72013-09-08 23:04:04 +0200575void test_vec_ecb( int cipher_id, int operation, char *hex_key,
576 char *hex_input, char *hex_result,
577 int finish_result )
578{
579 unsigned char key[50];
580 unsigned char input[16];
581 unsigned char result[16];
582 size_t key_len;
583 cipher_context_t ctx;
584 unsigned char output[32];
585 size_t outlen;
586
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200587 cipher_init( &ctx );
588
Paul Bakker5e0efa72013-09-08 23:04:04 +0200589 memset( key, 0x00, sizeof( key ) );
590 memset( input, 0x00, sizeof( input ) );
591 memset( result, 0x00, sizeof( result ) );
592 memset( output, 0x00, sizeof( output ) );
593
594 /* Prepare context */
595 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
596 cipher_info_from_type( cipher_id ) ) );
597
598 key_len = unhexify( key, hex_key );
599 TEST_ASSERT( unhexify( input, hex_input ) ==
600 (int) cipher_get_block_size( &ctx ) );
601 TEST_ASSERT( unhexify( result, hex_result ) ==
602 (int) cipher_get_block_size( &ctx ) );
603
604 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) );
605
606 TEST_ASSERT( 0 == cipher_update( &ctx, input,
607 cipher_get_block_size( &ctx ),
608 output, &outlen ) );
609 TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) );
610 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
611 &outlen ) );
612 TEST_ASSERT( 0 == outlen );
613
614 /* check plaintext only if everything went fine */
615 if( 0 == finish_result )
616 TEST_ASSERT( 0 == memcmp( output, result,
617 cipher_get_block_size( &ctx ) ) );
618
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200619 cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200620}
621/* END_CASE */
622
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200623/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +0200624void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200625{
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200626 const cipher_info_t *cipher_info;
627 cipher_context_t ctx;
628
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200629 cipher_init( &ctx );
630
Paul Bakker33b43f12013-08-20 11:48:36 +0200631 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200632 TEST_ASSERT( NULL != cipher_info );
633 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
634
Paul Bakker33b43f12013-08-20 11:48:36 +0200635 TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200636
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200637 cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200638}
Paul Bakker33b43f12013-08-20 11:48:36 +0200639/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000640
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200641/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200642void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200643{
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200644 cipher_info_t cipher_info;
645 cipher_context_t ctx;
646 unsigned char input[16];
647 size_t ilen, dlen;
648
649 /* build a fake context just for getting access to get_padding */
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200650 cipher_init( &ctx );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200651 cipher_info.mode = POLARSSL_MODE_CBC;
652 ctx.cipher_info = &cipher_info;
653
Paul Bakker33b43f12013-08-20 11:48:36 +0200654 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200655
Paul Bakker33b43f12013-08-20 11:48:36 +0200656 ilen = unhexify( input, input_str );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200657
Paul Bakker33b43f12013-08-20 11:48:36 +0200658 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
659 if( 0 == ret )
660 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200661}
Paul Bakker33b43f12013-08-20 11:48:36 +0200662/* END_CASE */
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200663
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200664/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200665void cipher_selftest()
Paul Bakker8123e9d2011-01-06 15:37:30 +0000666{
667 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
668}
Paul Bakker33b43f12013-08-20 11:48:36 +0200669/* END_CASE */