blob: 892036b6005d28af398acd42fc9aa0c7f780d741 [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 */
15void enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
16 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +020017{
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020018 size_t length = length_val, outlen, total_len, i;
Paul Bakker8123e9d2011-01-06 15:37:30 +000019 unsigned char key[32];
20 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +020021 unsigned char ad[13];
22 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020023 unsigned char inbuf[64];
24 unsigned char encbuf[64];
25 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +000026
27 const cipher_info_t *cipher_info;
28 cipher_context_t ctx_dec;
29 cipher_context_t ctx_enc;
30
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020031 /*
32 * Prepare contexts
33 */
Paul Bakker8123e9d2011-01-06 15:37:30 +000034 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
35 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020036
37 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000038
39 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +020040 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +000041 TEST_ASSERT( NULL != cipher_info );
Paul Bakker33b43f12013-08-20 11:48:36 +020042 TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +000043
44 /* Initialise enc and dec contexts */
45 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
46 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020047
Paul Bakker33b43f12013-08-20 11:48:36 +020048 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
49 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000050
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +020051#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +020052 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020053 {
Paul Bakker33b43f12013-08-20 11:48:36 +020054 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) );
55 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020056 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +020057#else
58 (void) pad_mode;
59#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020060
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020061 /*
62 * Do a few encode/decode cycles
63 */
64 for( i = 0; i < 3; i++ )
65 {
66 memset( iv , 0x00 + i, sizeof( iv ) );
67 memset( ad, 0x10 + i, sizeof( ad ) );
68 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
69
70 memset( encbuf, 0, sizeof( encbuf ) );
71 memset( decbuf, 0, sizeof( decbuf ) );
72 memset( tag, 0, sizeof( tag ) );
73
74 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
75 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +020076
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +020077 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
78 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
79
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020080#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020081 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
82 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020083#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +000084
Paul Bakker8123e9d2011-01-06 15:37:30 +000085 /* encode length number of bytes from inbuf */
86 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020087 total_len = outlen;
88
89 TEST_ASSERT( total_len == length ||
90 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
91 total_len < length &&
92 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000093
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +020094 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020095 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000096
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020097#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020098 TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020099#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200100
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200101 TEST_ASSERT( total_len == length ||
102 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
103 total_len > length &&
104 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000105
106 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200107 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
108 total_len = outlen;
109
110 TEST_ASSERT( total_len == length ||
111 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
112 total_len < length &&
113 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000114
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200115 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200116 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000117
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200118#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200119 TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200120#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200121
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200122 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200123 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000124 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200125 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000126
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200127 /*
128 * Done
129 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000130 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
131 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200132}
Paul Bakker33b43f12013-08-20 11:48:36 +0200133/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000134
Paul Bakker33b43f12013-08-20 11:48:36 +0200135/* BEGIN_CASE */
136void enc_fail( int cipher_id, int pad_mode, int key_len,
137 int length_val, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200138{
Paul Bakker33b43f12013-08-20 11:48:36 +0200139 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200140 unsigned char key[32];
141 unsigned char iv[16];
142
143 const cipher_info_t *cipher_info;
144 cipher_context_t ctx;
145
146 unsigned char inbuf[64];
147 unsigned char encbuf[64];
148
149 size_t outlen = 0;
150
151 memset( key, 0, 32 );
152 memset( iv , 0, 16 );
153
154 memset( &ctx, 0, sizeof( ctx ) );
155
156 memset( inbuf, 5, 64 );
157 memset( encbuf, 0, 64 );
158
159 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +0200160 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200161 TEST_ASSERT( NULL != cipher_info );
162
163 /* Initialise context */
164 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200165 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200166#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200167 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200168#else
169 (void) pad_mode;
170#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200171 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200172 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200173#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200174 TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200175#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200176
177 /* encode length number of bytes from inbuf */
178 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200179 TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200180
181 /* done */
182 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200183}
Paul Bakker33b43f12013-08-20 11:48:36 +0200184/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200185
Paul Bakker33b43f12013-08-20 11:48:36 +0200186/* BEGIN_CASE */
187void dec_empty_buf()
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200188{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000189 unsigned char key[32];
190 unsigned char iv[16];
191
192 cipher_context_t ctx_dec;
193 const cipher_info_t *cipher_info;
194
195 unsigned char encbuf[64];
196 unsigned char decbuf[64];
197
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000198 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000199
200 memset( key, 0, 32 );
201 memset( iv , 0, 16 );
202
203 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
204
205 memset( encbuf, 0, 64 );
206 memset( decbuf, 0, 64 );
207
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200208 /* Initialise context */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000209 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
210 TEST_ASSERT( NULL != cipher_info);
211
212 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
213
214 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
215
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200216 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
217
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200218 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
219
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200220#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200221 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200222#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000223
224 /* decode 0-byte string */
225 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
226 TEST_ASSERT( 0 == outlen );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200227 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish(
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200228 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000229 TEST_ASSERT( 0 == outlen );
230
231 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200232}
Paul Bakker33b43f12013-08-20 11:48:36 +0200233/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000234
Paul Bakker33b43f12013-08-20 11:48:36 +0200235/* BEGIN_CASE */
236void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
237 int second_length_val )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200238{
Paul Bakker33b43f12013-08-20 11:48:36 +0200239 size_t first_length = first_length_val;
240 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000241 size_t length = first_length + second_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000242 unsigned char key[32];
243 unsigned char iv[16];
244
245 cipher_context_t ctx_dec;
246 cipher_context_t ctx_enc;
247 const cipher_info_t *cipher_info;
248
249 unsigned char inbuf[64];
250 unsigned char encbuf[64];
251 unsigned char decbuf[64];
252
Paul Bakker23986e52011-04-24 08:57:21 +0000253 size_t outlen = 0;
254 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000255
256 memset( key, 0, 32 );
257 memset( iv , 0, 16 );
258
259 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
260 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
261
262 memset( inbuf, 5, 64 );
263 memset( encbuf, 0, 64 );
264 memset( decbuf, 0, 64 );
265
266 /* Initialise enc and dec contexts */
Paul Bakker33b43f12013-08-20 11:48:36 +0200267 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000268 TEST_ASSERT( NULL != cipher_info);
269
270 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
271 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
272
Paul Bakker33b43f12013-08-20 11:48:36 +0200273 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
274 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000275
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200276 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
277 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
278
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200279 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
280 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
281
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200282#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200283 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
284 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200285#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000286
Paul Bakker8123e9d2011-01-06 15:37:30 +0000287 /* encode length number of bytes from inbuf */
288 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
289 totaloutlen = outlen;
290 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
291 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200292 TEST_ASSERT( totaloutlen == length ||
293 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
294 totaloutlen < length &&
295 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
296
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200297 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000298 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200299 TEST_ASSERT( totaloutlen == length ||
300 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
301 totaloutlen > length &&
302 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000303
304 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200305 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
306 totaloutlen = outlen;
307
308 TEST_ASSERT( totaloutlen == length ||
309 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
310 totaloutlen < length &&
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200311 totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200312
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200313 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200314 totaloutlen += outlen;
315
316 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000317
318 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
319
320 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
321 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200322}
Paul Bakker33b43f12013-08-20 11:48:36 +0200323/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000324
Paul Bakker33b43f12013-08-20 11:48:36 +0200325/* BEGIN_CASE */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200326void decrypt_test_vec( int cipher_id, int pad_mode,
327 char *hex_key, char *hex_iv,
328 char *hex_cipher, char *hex_clear,
329 char *hex_ad, char *hex_tag,
330 int finish_result, int tag_result )
331{
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200332 unsigned char key[50];
333 unsigned char iv[50];
334 unsigned char cipher[200];
335 unsigned char clear[200];
336 unsigned char ad[200];
337 unsigned char tag[20];
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200338 size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
339 cipher_context_t ctx;
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200340 unsigned char output[200];
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200341 size_t outlen, total_len;
342
343 memset( key, 0x00, sizeof( key ) );
344 memset( iv, 0x00, sizeof( iv ) );
345 memset( cipher, 0x00, sizeof( cipher ) );
346 memset( clear, 0x00, sizeof( clear ) );
347 memset( ad, 0x00, sizeof( ad ) );
348 memset( tag, 0x00, sizeof( tag ) );
349 memset( output, 0x00, sizeof( output ) );
350
351 key_len = unhexify( key, hex_key );
352 iv_len = unhexify( iv, hex_iv );
353 cipher_len = unhexify( cipher, hex_cipher );
354 clear_len = unhexify( clear, hex_clear );
355 ad_len = unhexify( ad, hex_ad );
356 tag_len = unhexify( tag, hex_tag );
357
358 /* Prepare context */
359 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
360 cipher_info_from_type( cipher_id ) ) );
361 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200362#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200363 if( pad_mode != -1 )
364 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200365#else
366 (void) pad_mode;
367#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200368 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
369 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
370#if defined(POLARSSL_CIPHER_MODE_AEAD)
371 TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
372#endif /* POLARSSL_CIPHER_MODE_AEAD */
373
374 /* decode buffer and check tag */
375 total_len = 0;
376 TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
377 total_len += outlen;
378 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
379 &outlen ) );
380 total_len += outlen;
381#if defined(POLARSSL_CIPHER_MODE_AEAD)
382 TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
383#endif /* POLARSSL_CIPHER_MODE_AEAD */
384
385 /* check plaintext only if everything went fine */
386 if( 0 == finish_result && 0 == tag_result )
387 {
388 TEST_ASSERT( total_len == clear_len );
389 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
390 }
391
392 cipher_free_ctx( &ctx );
393}
394/* END_CASE */
395
396/* BEGIN_CASE */
Paul Bakker5e0efa72013-09-08 23:04:04 +0200397void test_vec_ecb( int cipher_id, int operation, char *hex_key,
398 char *hex_input, char *hex_result,
399 int finish_result )
400{
401 unsigned char key[50];
402 unsigned char input[16];
403 unsigned char result[16];
404 size_t key_len;
405 cipher_context_t ctx;
406 unsigned char output[32];
407 size_t outlen;
408
409 memset( key, 0x00, sizeof( key ) );
410 memset( input, 0x00, sizeof( input ) );
411 memset( result, 0x00, sizeof( result ) );
412 memset( output, 0x00, sizeof( output ) );
413
414 /* Prepare context */
415 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
416 cipher_info_from_type( cipher_id ) ) );
417
418 key_len = unhexify( key, hex_key );
419 TEST_ASSERT( unhexify( input, hex_input ) ==
420 (int) cipher_get_block_size( &ctx ) );
421 TEST_ASSERT( unhexify( result, hex_result ) ==
422 (int) cipher_get_block_size( &ctx ) );
423
424 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) );
425
426 TEST_ASSERT( 0 == cipher_update( &ctx, input,
427 cipher_get_block_size( &ctx ),
428 output, &outlen ) );
429 TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) );
430 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
431 &outlen ) );
432 TEST_ASSERT( 0 == outlen );
433
434 /* check plaintext only if everything went fine */
435 if( 0 == finish_result )
436 TEST_ASSERT( 0 == memcmp( output, result,
437 cipher_get_block_size( &ctx ) ) );
438
439 cipher_free_ctx( &ctx );
440}
441/* END_CASE */
442
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200443/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +0200444void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200445{
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200446 const cipher_info_t *cipher_info;
447 cipher_context_t ctx;
448
Paul Bakker33b43f12013-08-20 11:48:36 +0200449 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200450 TEST_ASSERT( NULL != cipher_info );
451 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
452
Paul Bakker33b43f12013-08-20 11:48:36 +0200453 TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200454
455 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200456}
Paul Bakker33b43f12013-08-20 11:48:36 +0200457/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000458
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200459/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200460void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200461{
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200462 cipher_info_t cipher_info;
463 cipher_context_t ctx;
464 unsigned char input[16];
465 size_t ilen, dlen;
466
467 /* build a fake context just for getting access to get_padding */
468 memset( &ctx, 0, sizeof( ctx ) );
469 cipher_info.mode = POLARSSL_MODE_CBC;
470 ctx.cipher_info = &cipher_info;
471
Paul Bakker33b43f12013-08-20 11:48:36 +0200472 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200473
Paul Bakker33b43f12013-08-20 11:48:36 +0200474 ilen = unhexify( input, input_str );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200475
Paul Bakker33b43f12013-08-20 11:48:36 +0200476 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
477 if( 0 == ret )
478 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200479}
Paul Bakker33b43f12013-08-20 11:48:36 +0200480/* END_CASE */
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200481
Paul Bakker33b43f12013-08-20 11:48:36 +0200482/* BEGIN_CASE */
483void cipher_selftest()
Paul Bakker8123e9d2011-01-06 15:37:30 +0000484{
485 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
486}
Paul Bakker33b43f12013-08-20 11:48:36 +0200487/* END_CASE */