blob: 898d98dc71147002cdfa8ee1715dc66a1d831a1d [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
Paul Bakker33b43f12013-08-20 11:48:36 +020051 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020052 {
Paul Bakker33b43f12013-08-20 11:48:36 +020053 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) );
54 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020055 }
56
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020057 /*
58 * Do a few encode/decode cycles
59 */
60 for( i = 0; i < 3; i++ )
61 {
62 memset( iv , 0x00 + i, sizeof( iv ) );
63 memset( ad, 0x10 + i, sizeof( ad ) );
64 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
65
66 memset( encbuf, 0, sizeof( encbuf ) );
67 memset( decbuf, 0, sizeof( decbuf ) );
68 memset( tag, 0, sizeof( tag ) );
69
70 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
71 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +020072
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +020073 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
74 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
75
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020076#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020077 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
78 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020079#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +000080
Paul Bakker8123e9d2011-01-06 15:37:30 +000081 /* encode length number of bytes from inbuf */
82 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020083 total_len = outlen;
84
85 TEST_ASSERT( total_len == length ||
86 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
87 total_len < length &&
88 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000089
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +020090 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020091 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000092
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020093#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +020094 TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020095#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +020096
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020097 TEST_ASSERT( total_len == length ||
98 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
99 total_len > length &&
100 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000101
102 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200103 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
104 total_len = outlen;
105
106 TEST_ASSERT( total_len == length ||
107 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
108 total_len < length &&
109 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000110
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200111 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200112 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000113
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200114#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200115 TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200116#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200117
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200118 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200119 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000120 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200121 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000122
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200123 /*
124 * Done
125 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000126 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
127 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200128}
Paul Bakker33b43f12013-08-20 11:48:36 +0200129/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000130
Paul Bakker33b43f12013-08-20 11:48:36 +0200131/* BEGIN_CASE */
132void enc_fail( int cipher_id, int pad_mode, int key_len,
133 int length_val, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200134{
Paul Bakker33b43f12013-08-20 11:48:36 +0200135 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200136 unsigned char key[32];
137 unsigned char iv[16];
138
139 const cipher_info_t *cipher_info;
140 cipher_context_t ctx;
141
142 unsigned char inbuf[64];
143 unsigned char encbuf[64];
144
145 size_t outlen = 0;
146
147 memset( key, 0, 32 );
148 memset( iv , 0, 16 );
149
150 memset( &ctx, 0, sizeof( ctx ) );
151
152 memset( inbuf, 5, 64 );
153 memset( encbuf, 0, 64 );
154
155 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +0200156 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200157 TEST_ASSERT( NULL != cipher_info );
158
159 /* Initialise context */
160 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200161 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
162 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200163 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200164 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200165#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200166 TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200167#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200168
169 /* encode length number of bytes from inbuf */
170 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200171 TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200172
173 /* done */
174 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200175}
Paul Bakker33b43f12013-08-20 11:48:36 +0200176/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200177
Paul Bakker33b43f12013-08-20 11:48:36 +0200178/* BEGIN_CASE */
179void dec_empty_buf()
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200180{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000181 unsigned char key[32];
182 unsigned char iv[16];
183
184 cipher_context_t ctx_dec;
185 const cipher_info_t *cipher_info;
186
187 unsigned char encbuf[64];
188 unsigned char decbuf[64];
189
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000190 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000191
192 memset( key, 0, 32 );
193 memset( iv , 0, 16 );
194
195 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
196
197 memset( encbuf, 0, 64 );
198 memset( decbuf, 0, 64 );
199
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200200 /* Initialise context */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000201 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
202 TEST_ASSERT( NULL != cipher_info);
203
204 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
205
206 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
207
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200208 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
209
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200210 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
211
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200212#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200213 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200214#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000215
216 /* decode 0-byte string */
217 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
218 TEST_ASSERT( 0 == outlen );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200219 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish(
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200220 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000221 TEST_ASSERT( 0 == outlen );
222
223 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200224}
Paul Bakker33b43f12013-08-20 11:48:36 +0200225/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000226
Paul Bakker33b43f12013-08-20 11:48:36 +0200227/* BEGIN_CASE */
228void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
229 int second_length_val )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200230{
Paul Bakker33b43f12013-08-20 11:48:36 +0200231 size_t first_length = first_length_val;
232 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000233 size_t length = first_length + second_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000234 unsigned char key[32];
235 unsigned char iv[16];
236
237 cipher_context_t ctx_dec;
238 cipher_context_t ctx_enc;
239 const cipher_info_t *cipher_info;
240
241 unsigned char inbuf[64];
242 unsigned char encbuf[64];
243 unsigned char decbuf[64];
244
Paul Bakker23986e52011-04-24 08:57:21 +0000245 size_t outlen = 0;
246 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000247
248 memset( key, 0, 32 );
249 memset( iv , 0, 16 );
250
251 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
252 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
253
254 memset( inbuf, 5, 64 );
255 memset( encbuf, 0, 64 );
256 memset( decbuf, 0, 64 );
257
258 /* Initialise enc and dec contexts */
Paul Bakker33b43f12013-08-20 11:48:36 +0200259 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000260 TEST_ASSERT( NULL != cipher_info);
261
262 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
263 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
264
Paul Bakker33b43f12013-08-20 11:48:36 +0200265 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
266 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000267
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200268 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
269 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
270
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200271 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
272 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
273
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200274#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200275 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
276 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200277#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000278
Paul Bakker8123e9d2011-01-06 15:37:30 +0000279 /* encode length number of bytes from inbuf */
280 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
281 totaloutlen = outlen;
282 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
283 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200284 TEST_ASSERT( totaloutlen == length ||
285 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
286 totaloutlen < length &&
287 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
288
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200289 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000290 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200291 TEST_ASSERT( totaloutlen == length ||
292 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
293 totaloutlen > length &&
294 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000295
296 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200297 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
298 totaloutlen = outlen;
299
300 TEST_ASSERT( totaloutlen == length ||
301 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
302 totaloutlen < length &&
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200303 totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200304
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200305 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200306 totaloutlen += outlen;
307
308 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000309
310 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
311
312 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
313 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200314}
Paul Bakker33b43f12013-08-20 11:48:36 +0200315/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000316
Paul Bakker33b43f12013-08-20 11:48:36 +0200317/* BEGIN_CASE */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200318void decrypt_test_vec( int cipher_id, int pad_mode,
319 char *hex_key, char *hex_iv,
320 char *hex_cipher, char *hex_clear,
321 char *hex_ad, char *hex_tag,
322 int finish_result, int tag_result )
323{
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200324 unsigned char key[50];
325 unsigned char iv[50];
326 unsigned char cipher[200];
327 unsigned char clear[200];
328 unsigned char ad[200];
329 unsigned char tag[20];
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200330 size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
331 cipher_context_t ctx;
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200332 unsigned char output[200];
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200333 size_t outlen, total_len;
334
335 memset( key, 0x00, sizeof( key ) );
336 memset( iv, 0x00, sizeof( iv ) );
337 memset( cipher, 0x00, sizeof( cipher ) );
338 memset( clear, 0x00, sizeof( clear ) );
339 memset( ad, 0x00, sizeof( ad ) );
340 memset( tag, 0x00, sizeof( tag ) );
341 memset( output, 0x00, sizeof( output ) );
342
343 key_len = unhexify( key, hex_key );
344 iv_len = unhexify( iv, hex_iv );
345 cipher_len = unhexify( cipher, hex_cipher );
346 clear_len = unhexify( clear, hex_clear );
347 ad_len = unhexify( ad, hex_ad );
348 tag_len = unhexify( tag, hex_tag );
349
350 /* Prepare context */
351 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
352 cipher_info_from_type( cipher_id ) ) );
353 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
354 if( pad_mode != -1 )
355 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
356 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
357 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
358#if defined(POLARSSL_CIPHER_MODE_AEAD)
359 TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
360#endif /* POLARSSL_CIPHER_MODE_AEAD */
361
362 /* decode buffer and check tag */
363 total_len = 0;
364 TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
365 total_len += outlen;
366 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
367 &outlen ) );
368 total_len += outlen;
369#if defined(POLARSSL_CIPHER_MODE_AEAD)
370 TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
371#endif /* POLARSSL_CIPHER_MODE_AEAD */
372
373 /* check plaintext only if everything went fine */
374 if( 0 == finish_result && 0 == tag_result )
375 {
376 TEST_ASSERT( total_len == clear_len );
377 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
378 }
379
380 cipher_free_ctx( &ctx );
381}
382/* END_CASE */
383
384/* BEGIN_CASE */
Paul Bakker5e0efa72013-09-08 23:04:04 +0200385void test_vec_ecb( int cipher_id, int operation, char *hex_key,
386 char *hex_input, char *hex_result,
387 int finish_result )
388{
389 unsigned char key[50];
390 unsigned char input[16];
391 unsigned char result[16];
392 size_t key_len;
393 cipher_context_t ctx;
394 unsigned char output[32];
395 size_t outlen;
396
397 memset( key, 0x00, sizeof( key ) );
398 memset( input, 0x00, sizeof( input ) );
399 memset( result, 0x00, sizeof( result ) );
400 memset( output, 0x00, sizeof( output ) );
401
402 /* Prepare context */
403 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
404 cipher_info_from_type( cipher_id ) ) );
405
406 key_len = unhexify( key, hex_key );
407 TEST_ASSERT( unhexify( input, hex_input ) ==
408 (int) cipher_get_block_size( &ctx ) );
409 TEST_ASSERT( unhexify( result, hex_result ) ==
410 (int) cipher_get_block_size( &ctx ) );
411
412 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) );
413
414 TEST_ASSERT( 0 == cipher_update( &ctx, input,
415 cipher_get_block_size( &ctx ),
416 output, &outlen ) );
417 TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) );
418 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
419 &outlen ) );
420 TEST_ASSERT( 0 == outlen );
421
422 /* check plaintext only if everything went fine */
423 if( 0 == finish_result )
424 TEST_ASSERT( 0 == memcmp( output, result,
425 cipher_get_block_size( &ctx ) ) );
426
427 cipher_free_ctx( &ctx );
428}
429/* END_CASE */
430
431/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +0200432void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200433{
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200434 const cipher_info_t *cipher_info;
435 cipher_context_t ctx;
436
Paul Bakker33b43f12013-08-20 11:48:36 +0200437 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200438 TEST_ASSERT( NULL != cipher_info );
439 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
440
Paul Bakker33b43f12013-08-20 11:48:36 +0200441 TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200442
443 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200444}
Paul Bakker33b43f12013-08-20 11:48:36 +0200445/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000446
Paul Bakker33b43f12013-08-20 11:48:36 +0200447/* BEGIN_CASE */
448void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200449{
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200450 cipher_info_t cipher_info;
451 cipher_context_t ctx;
452 unsigned char input[16];
453 size_t ilen, dlen;
454
455 /* build a fake context just for getting access to get_padding */
456 memset( &ctx, 0, sizeof( ctx ) );
457 cipher_info.mode = POLARSSL_MODE_CBC;
458 ctx.cipher_info = &cipher_info;
459
Paul Bakker33b43f12013-08-20 11:48:36 +0200460 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200461
Paul Bakker33b43f12013-08-20 11:48:36 +0200462 ilen = unhexify( input, input_str );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200463
Paul Bakker33b43f12013-08-20 11:48:36 +0200464 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
465 if( 0 == ret )
466 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200467}
Paul Bakker33b43f12013-08-20 11:48:36 +0200468/* END_CASE */
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200469
Paul Bakker33b43f12013-08-20 11:48:36 +0200470/* BEGIN_CASE */
471void cipher_selftest()
Paul Bakker8123e9d2011-01-06 15:37:30 +0000472{
473 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
474}
Paul Bakker33b43f12013-08-20 11:48:36 +0200475/* END_CASE */