blob: 03be2b818fa164daf29768ce7b6a620011ffd240 [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 */
Paul Bakker33b43f12013-08-20 11:48:36 +020025void enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
26 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +020027{
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020028 size_t length = length_val, outlen, total_len, i;
Paul Bakker8123e9d2011-01-06 15:37:30 +000029 unsigned char key[32];
30 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +020031 unsigned char ad[13];
32 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020033 unsigned char inbuf[64];
34 unsigned char encbuf[64];
35 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +000036
37 const cipher_info_t *cipher_info;
38 cipher_context_t ctx_dec;
39 cipher_context_t ctx_enc;
40
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020041 /*
42 * Prepare contexts
43 */
Paul Bakker8123e9d2011-01-06 15:37:30 +000044 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
45 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020046
47 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000048
49 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +020050 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +000051 TEST_ASSERT( NULL != cipher_info );
Paul Bakker33b43f12013-08-20 11:48:36 +020052 TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +000053
54 /* Initialise enc and dec contexts */
55 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
56 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020057
Paul Bakker33b43f12013-08-20 11:48:36 +020058 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
59 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000060
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +020061#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +020062 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020063 {
Paul Bakker33b43f12013-08-20 11:48:36 +020064 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) );
65 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020066 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +020067#else
68 (void) pad_mode;
69#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020070
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020071 /*
72 * Do a few encode/decode cycles
73 */
74 for( i = 0; i < 3; i++ )
75 {
76 memset( iv , 0x00 + i, sizeof( iv ) );
77 memset( ad, 0x10 + i, sizeof( ad ) );
78 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
79
80 memset( encbuf, 0, sizeof( encbuf ) );
81 memset( decbuf, 0, sizeof( decbuf ) );
82 memset( tag, 0, sizeof( tag ) );
83
84 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
85 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +020086
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +020087 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
88 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
89
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020090#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020091 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
92 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020093#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +000094
Paul Bakker8123e9d2011-01-06 15:37:30 +000095 /* encode length number of bytes from inbuf */
96 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020097 total_len = outlen;
98
99 TEST_ASSERT( total_len == length ||
100 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
101 total_len < length &&
102 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000103
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200104 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200105 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000106
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200107#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200108 TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200109#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200110
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200111 TEST_ASSERT( total_len == length ||
112 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
113 total_len > length &&
114 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000115
116 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200117 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
118 total_len = outlen;
119
120 TEST_ASSERT( total_len == length ||
121 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
122 total_len < length &&
123 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000124
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200125 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200126 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000127
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200128#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200129 TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200130#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200131
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200132 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200133 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000134 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200135 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000136
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200137 /*
138 * Done
139 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000140 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
141 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200142}
Paul Bakker33b43f12013-08-20 11:48:36 +0200143/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000144
Paul Bakker33b43f12013-08-20 11:48:36 +0200145/* BEGIN_CASE */
146void enc_fail( int cipher_id, int pad_mode, int key_len,
147 int length_val, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200148{
Paul Bakker33b43f12013-08-20 11:48:36 +0200149 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200150 unsigned char key[32];
151 unsigned char iv[16];
152
153 const cipher_info_t *cipher_info;
154 cipher_context_t ctx;
155
156 unsigned char inbuf[64];
157 unsigned char encbuf[64];
158
159 size_t outlen = 0;
160
161 memset( key, 0, 32 );
162 memset( iv , 0, 16 );
163
164 memset( &ctx, 0, sizeof( ctx ) );
165
166 memset( inbuf, 5, 64 );
167 memset( encbuf, 0, 64 );
168
169 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +0200170 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200171 TEST_ASSERT( NULL != cipher_info );
172
173 /* Initialise context */
174 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200175 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200176#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200177 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200178#else
179 (void) pad_mode;
180#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200181 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200182 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200183#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200184 TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200185#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200186
187 /* encode length number of bytes from inbuf */
188 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200189 TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200190
191 /* done */
192 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200193}
Paul Bakker33b43f12013-08-20 11:48:36 +0200194/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200195
Paul Bakker33b43f12013-08-20 11:48:36 +0200196/* BEGIN_CASE */
197void dec_empty_buf()
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200198{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000199 unsigned char key[32];
200 unsigned char iv[16];
201
202 cipher_context_t ctx_dec;
203 const cipher_info_t *cipher_info;
204
205 unsigned char encbuf[64];
206 unsigned char decbuf[64];
207
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000208 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000209
210 memset( key, 0, 32 );
211 memset( iv , 0, 16 );
212
213 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
214
215 memset( encbuf, 0, 64 );
216 memset( decbuf, 0, 64 );
217
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200218 /* Initialise context */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000219 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
220 TEST_ASSERT( NULL != cipher_info);
221
222 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
223
224 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
225
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200226 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
227
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200228 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
229
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200230#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200231 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200232#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000233
234 /* decode 0-byte string */
235 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
236 TEST_ASSERT( 0 == outlen );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200237 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish(
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200238 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000239 TEST_ASSERT( 0 == outlen );
240
241 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200242}
Paul Bakker33b43f12013-08-20 11:48:36 +0200243/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000244
Paul Bakker33b43f12013-08-20 11:48:36 +0200245/* BEGIN_CASE */
246void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
247 int second_length_val )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200248{
Paul Bakker33b43f12013-08-20 11:48:36 +0200249 size_t first_length = first_length_val;
250 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000251 size_t length = first_length + second_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000252 unsigned char key[32];
253 unsigned char iv[16];
254
255 cipher_context_t ctx_dec;
256 cipher_context_t ctx_enc;
257 const cipher_info_t *cipher_info;
258
259 unsigned char inbuf[64];
260 unsigned char encbuf[64];
261 unsigned char decbuf[64];
262
Paul Bakker23986e52011-04-24 08:57:21 +0000263 size_t outlen = 0;
264 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265
266 memset( key, 0, 32 );
267 memset( iv , 0, 16 );
268
269 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
270 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
271
272 memset( inbuf, 5, 64 );
273 memset( encbuf, 0, 64 );
274 memset( decbuf, 0, 64 );
275
276 /* Initialise enc and dec contexts */
Paul Bakker33b43f12013-08-20 11:48:36 +0200277 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000278 TEST_ASSERT( NULL != cipher_info);
279
280 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
281 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
282
Paul Bakker33b43f12013-08-20 11:48:36 +0200283 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
284 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000285
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200286 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
287 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
288
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200289 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
290 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
291
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200292#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200293 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
294 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200295#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000296
Paul Bakker8123e9d2011-01-06 15:37:30 +0000297 /* encode length number of bytes from inbuf */
298 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
299 totaloutlen = outlen;
300 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
301 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200302 TEST_ASSERT( totaloutlen == length ||
303 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
304 totaloutlen < length &&
305 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
306
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200307 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000308 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200309 TEST_ASSERT( totaloutlen == length ||
310 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
311 totaloutlen > length &&
312 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000313
314 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200315 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
316 totaloutlen = outlen;
317
318 TEST_ASSERT( totaloutlen == length ||
319 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
320 totaloutlen < length &&
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200321 totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200322
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200323 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200324 totaloutlen += outlen;
325
326 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000327
328 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
329
330 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
331 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200332}
Paul Bakker33b43f12013-08-20 11:48:36 +0200333/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000334
Paul Bakker33b43f12013-08-20 11:48:36 +0200335/* BEGIN_CASE */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200336void decrypt_test_vec( int cipher_id, int pad_mode,
337 char *hex_key, char *hex_iv,
338 char *hex_cipher, char *hex_clear,
339 char *hex_ad, char *hex_tag,
340 int finish_result, int tag_result )
341{
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200342 unsigned char key[50];
343 unsigned char iv[50];
344 unsigned char cipher[200];
345 unsigned char clear[200];
346 unsigned char ad[200];
347 unsigned char tag[20];
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200348 size_t key_len, iv_len, cipher_len, clear_len;
349#if defined(POLARSSL_CIPHER_MODE_AEAD)
350 size_t ad_len, tag_len;
351#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200352 cipher_context_t ctx;
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200353 unsigned char output[200];
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200354 size_t outlen, total_len;
355
356 memset( key, 0x00, sizeof( key ) );
357 memset( iv, 0x00, sizeof( iv ) );
358 memset( cipher, 0x00, sizeof( cipher ) );
359 memset( clear, 0x00, sizeof( clear ) );
360 memset( ad, 0x00, sizeof( ad ) );
361 memset( tag, 0x00, sizeof( tag ) );
362 memset( output, 0x00, sizeof( output ) );
363
364 key_len = unhexify( key, hex_key );
365 iv_len = unhexify( iv, hex_iv );
366 cipher_len = unhexify( cipher, hex_cipher );
367 clear_len = unhexify( clear, hex_clear );
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200368#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200369 ad_len = unhexify( ad, hex_ad );
370 tag_len = unhexify( tag, hex_tag );
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200371#else
372 ((void) hex_ad);
373 ((void) hex_tag);
374#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200375
376 /* Prepare context */
377 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
378 cipher_info_from_type( cipher_id ) ) );
379 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200380#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200381 if( pad_mode != -1 )
382 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200383#else
384 (void) pad_mode;
385#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200386 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
387 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
388#if defined(POLARSSL_CIPHER_MODE_AEAD)
389 TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
390#endif /* POLARSSL_CIPHER_MODE_AEAD */
391
392 /* decode buffer and check tag */
393 total_len = 0;
394 TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
395 total_len += outlen;
396 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
397 &outlen ) );
398 total_len += outlen;
399#if defined(POLARSSL_CIPHER_MODE_AEAD)
400 TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
401#endif /* POLARSSL_CIPHER_MODE_AEAD */
402
403 /* check plaintext only if everything went fine */
404 if( 0 == finish_result && 0 == tag_result )
405 {
406 TEST_ASSERT( total_len == clear_len );
407 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
408 }
409
410 cipher_free_ctx( &ctx );
411}
412/* END_CASE */
413
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200414/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_AEAD */
415void auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv,
416 char *hex_ad, char *hex_cipher,
417 char *hex_tag, char *hex_clear )
418{
419 int ret;
420 unsigned char key[50];
421 unsigned char iv[50];
422 unsigned char cipher[200];
423 unsigned char clear[200];
424 unsigned char ad[200];
425 unsigned char tag[20];
426 unsigned char my_tag[20];
427 size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
428 cipher_context_t ctx;
429 unsigned char output[200];
430 size_t outlen;
431
432 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( my_tag, 0xFF, sizeof( my_tag ) );
439 memset( output, 0xFF, sizeof( output ) );
440
441 key_len = unhexify( key, hex_key );
442 iv_len = unhexify( iv, hex_iv );
443 cipher_len = unhexify( cipher, hex_cipher );
444 ad_len = unhexify( ad, hex_ad );
445 tag_len = unhexify( tag, hex_tag );
446
447 /* Prepare context */
448 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
449 cipher_info_from_type( cipher_id ) ) );
450 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
451
452 /* decode buffer and check tag */
453 ret = cipher_auth_decrypt( &ctx, iv, iv_len, ad, ad_len,
454 cipher, cipher_len, output, &outlen,
455 tag, tag_len );
456
457 /* make sure we didn't overwrite */
458 TEST_ASSERT( output[outlen + 0] == 0xFF );
459 TEST_ASSERT( output[outlen + 1] == 0xFF );
460
461 /* make sure the message is rejected if it should be */
462 if( strcmp( hex_clear, "FAIL" ) == 0 )
463 {
464 TEST_ASSERT( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED );
465 goto cleanup;
466 }
467
468 /* otherwise, make sure it was decrypted properly */
469 TEST_ASSERT( ret == 0 );
470
471 clear_len = unhexify( clear, hex_clear );
472 TEST_ASSERT( outlen == clear_len );
473 TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 );
474
475 /* then encrypt the clear and make sure we get the same ciphertext and tag */
476 memset( output, 0xFF, sizeof( output ) );
477 outlen = 0;
478
479 ret = cipher_auth_encrypt( &ctx, iv, iv_len, ad, ad_len,
480 clear, clear_len, output, &outlen,
481 my_tag, tag_len );
482 TEST_ASSERT( ret == 0 );
483
484 TEST_ASSERT( outlen == clear_len );
485 TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 );
486 TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 );
487
488 /* make sure we didn't overwrite */
489 TEST_ASSERT( output[outlen + 0] == 0xFF );
490 TEST_ASSERT( output[outlen + 1] == 0xFF );
491 TEST_ASSERT( my_tag[tag_len + 0] == 0xFF );
492 TEST_ASSERT( my_tag[tag_len + 1] == 0xFF );
493
494
495cleanup:
496 cipher_free_ctx( &ctx );
497}
498/* END_CASE */
499
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200500/* BEGIN_CASE */
Paul Bakker5e0efa72013-09-08 23:04:04 +0200501void test_vec_ecb( int cipher_id, int operation, char *hex_key,
502 char *hex_input, char *hex_result,
503 int finish_result )
504{
505 unsigned char key[50];
506 unsigned char input[16];
507 unsigned char result[16];
508 size_t key_len;
509 cipher_context_t ctx;
510 unsigned char output[32];
511 size_t outlen;
512
513 memset( key, 0x00, sizeof( key ) );
514 memset( input, 0x00, sizeof( input ) );
515 memset( result, 0x00, sizeof( result ) );
516 memset( output, 0x00, sizeof( output ) );
517
518 /* Prepare context */
519 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
520 cipher_info_from_type( cipher_id ) ) );
521
522 key_len = unhexify( key, hex_key );
523 TEST_ASSERT( unhexify( input, hex_input ) ==
524 (int) cipher_get_block_size( &ctx ) );
525 TEST_ASSERT( unhexify( result, hex_result ) ==
526 (int) cipher_get_block_size( &ctx ) );
527
528 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) );
529
530 TEST_ASSERT( 0 == cipher_update( &ctx, input,
531 cipher_get_block_size( &ctx ),
532 output, &outlen ) );
533 TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) );
534 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
535 &outlen ) );
536 TEST_ASSERT( 0 == outlen );
537
538 /* check plaintext only if everything went fine */
539 if( 0 == finish_result )
540 TEST_ASSERT( 0 == memcmp( output, result,
541 cipher_get_block_size( &ctx ) ) );
542
543 cipher_free_ctx( &ctx );
544}
545/* END_CASE */
546
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200547/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +0200548void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200549{
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200550 const cipher_info_t *cipher_info;
551 cipher_context_t ctx;
552
Paul Bakker33b43f12013-08-20 11:48:36 +0200553 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200554 TEST_ASSERT( NULL != cipher_info );
555 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
556
Paul Bakker33b43f12013-08-20 11:48:36 +0200557 TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200558
559 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200560}
Paul Bakker33b43f12013-08-20 11:48:36 +0200561/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000562
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200563/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200564void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200565{
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200566 cipher_info_t cipher_info;
567 cipher_context_t ctx;
568 unsigned char input[16];
569 size_t ilen, dlen;
570
571 /* build a fake context just for getting access to get_padding */
572 memset( &ctx, 0, sizeof( ctx ) );
573 cipher_info.mode = POLARSSL_MODE_CBC;
574 ctx.cipher_info = &cipher_info;
575
Paul Bakker33b43f12013-08-20 11:48:36 +0200576 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200577
Paul Bakker33b43f12013-08-20 11:48:36 +0200578 ilen = unhexify( input, input_str );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200579
Paul Bakker33b43f12013-08-20 11:48:36 +0200580 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
581 if( 0 == ret )
582 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200583}
Paul Bakker33b43f12013-08-20 11:48:36 +0200584/* END_CASE */
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200585
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200586/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200587void cipher_selftest()
Paul Bakker8123e9d2011-01-06 15:37:30 +0000588{
589 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
590}
Paul Bakker33b43f12013-08-20 11:48:36 +0200591/* END_CASE */