blob: c556a2e56530170f2b5f17d254ff498195728456 [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{
Paul Bakker33b43f12013-08-20 11:48:36 +020018 size_t length = length_val;
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];
Paul Bakker8123e9d2011-01-06 15:37:30 +000023
24 const cipher_info_t *cipher_info;
25 cipher_context_t ctx_dec;
26 cipher_context_t ctx_enc;
27
28 unsigned char inbuf[64];
29 unsigned char encbuf[64];
30 unsigned char decbuf[64];
31
Paul Bakker23986e52011-04-24 08:57:21 +000032 size_t outlen = 0;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020033 size_t total_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +000034
35 memset( key, 0, 32 );
36 memset( iv , 0, 16 );
37
38 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
39 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
40
41 memset( inbuf, 5, 64 );
42 memset( encbuf, 0, 64 );
43 memset( decbuf, 0, 64 );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +020044 memset( tag, 0, 16 );
45 memset( ad, 0x2a, 13 );
Paul Bakker8123e9d2011-01-06 15:37:30 +000046
47 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +020048 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +000049 TEST_ASSERT( NULL != cipher_info );
Paul Bakker33b43f12013-08-20 11:48:36 +020050 TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +000051
52 /* Initialise enc and dec contexts */
53 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
54 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
55
Paul Bakker33b43f12013-08-20 11:48:36 +020056 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
57 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000058
Paul Bakker33b43f12013-08-20 11:48:36 +020059 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020060 {
Paul Bakker33b43f12013-08-20 11:48:36 +020061 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) );
62 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020063 }
64
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +020065 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
66 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
67
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +020068 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
69 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
70
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020071#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +020072 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, 13 ) );
73 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, 13 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020074#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +000075
Paul Bakker8123e9d2011-01-06 15:37:30 +000076 /* encode length number of bytes from inbuf */
77 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020078 total_len = outlen;
79
80 TEST_ASSERT( total_len == length ||
81 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
82 total_len < length &&
83 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000084
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +020085 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020086 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000087
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020088#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +020089 TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, 16 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020090#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +020091
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020092 TEST_ASSERT( total_len == length ||
93 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
94 total_len > length &&
95 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000096
97 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020098 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
99 total_len = outlen;
100
101 TEST_ASSERT( total_len == length ||
102 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
103 total_len < length &&
104 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000105
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200106 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200107 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000108
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200109#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200110 TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, 16 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200111#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200112
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200113 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000114
115 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
116
117 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
118 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200119}
Paul Bakker33b43f12013-08-20 11:48:36 +0200120/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000121
Paul Bakker33b43f12013-08-20 11:48:36 +0200122/* BEGIN_CASE */
123void enc_fail( int cipher_id, int pad_mode, int key_len,
124 int length_val, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200125{
Paul Bakker33b43f12013-08-20 11:48:36 +0200126 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200127 unsigned char key[32];
128 unsigned char iv[16];
129
130 const cipher_info_t *cipher_info;
131 cipher_context_t ctx;
132
133 unsigned char inbuf[64];
134 unsigned char encbuf[64];
135
136 size_t outlen = 0;
137
138 memset( key, 0, 32 );
139 memset( iv , 0, 16 );
140
141 memset( &ctx, 0, sizeof( ctx ) );
142
143 memset( inbuf, 5, 64 );
144 memset( encbuf, 0, 64 );
145
146 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +0200147 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200148 TEST_ASSERT( NULL != cipher_info );
149
150 /* Initialise context */
151 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200152 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
153 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200154 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200155 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200156#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200157 TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200158#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200159
160 /* encode length number of bytes from inbuf */
161 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200162 TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200163
164 /* done */
165 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200166}
Paul Bakker33b43f12013-08-20 11:48:36 +0200167/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200168
Paul Bakker33b43f12013-08-20 11:48:36 +0200169/* BEGIN_CASE */
170void dec_empty_buf()
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200171{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000172 unsigned char key[32];
173 unsigned char iv[16];
174
175 cipher_context_t ctx_dec;
176 const cipher_info_t *cipher_info;
177
178 unsigned char encbuf[64];
179 unsigned char decbuf[64];
180
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000181 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000182
183 memset( key, 0, 32 );
184 memset( iv , 0, 16 );
185
186 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
187
188 memset( encbuf, 0, 64 );
189 memset( decbuf, 0, 64 );
190
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200191 /* Initialise context */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000192 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
193 TEST_ASSERT( NULL != cipher_info);
194
195 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
196
197 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
198
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200199 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
200
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200201 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
202
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200203#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200204 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200205#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206
207 /* decode 0-byte string */
208 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
209 TEST_ASSERT( 0 == outlen );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200210 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish(
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200211 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000212 TEST_ASSERT( 0 == outlen );
213
214 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200215}
Paul Bakker33b43f12013-08-20 11:48:36 +0200216/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217
Paul Bakker33b43f12013-08-20 11:48:36 +0200218/* BEGIN_CASE */
219void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
220 int second_length_val )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200221{
Paul Bakker33b43f12013-08-20 11:48:36 +0200222 size_t first_length = first_length_val;
223 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000224 size_t length = first_length + second_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000225 unsigned char key[32];
226 unsigned char iv[16];
227
228 cipher_context_t ctx_dec;
229 cipher_context_t ctx_enc;
230 const cipher_info_t *cipher_info;
231
232 unsigned char inbuf[64];
233 unsigned char encbuf[64];
234 unsigned char decbuf[64];
235
Paul Bakker23986e52011-04-24 08:57:21 +0000236 size_t outlen = 0;
237 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000238
239 memset( key, 0, 32 );
240 memset( iv , 0, 16 );
241
242 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
243 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
244
245 memset( inbuf, 5, 64 );
246 memset( encbuf, 0, 64 );
247 memset( decbuf, 0, 64 );
248
249 /* Initialise enc and dec contexts */
Paul Bakker33b43f12013-08-20 11:48:36 +0200250 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000251 TEST_ASSERT( NULL != cipher_info);
252
253 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
254 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
255
Paul Bakker33b43f12013-08-20 11:48:36 +0200256 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
257 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000258
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200259 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
260 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
261
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200262 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
263 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
264
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200265#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200266 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
267 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200268#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000269
Paul Bakker8123e9d2011-01-06 15:37:30 +0000270 /* encode length number of bytes from inbuf */
271 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
272 totaloutlen = outlen;
273 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
274 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200275 TEST_ASSERT( totaloutlen == length ||
276 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
277 totaloutlen < length &&
278 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
279
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200280 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000281 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200282 TEST_ASSERT( totaloutlen == length ||
283 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
284 totaloutlen > length &&
285 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000286
287 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200288 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
289 totaloutlen = outlen;
290
291 TEST_ASSERT( totaloutlen == length ||
292 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
293 totaloutlen < length &&
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200294 totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200295
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200296 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200297 totaloutlen += outlen;
298
299 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000300
301 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
302
303 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
304 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200305}
Paul Bakker33b43f12013-08-20 11:48:36 +0200306/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000307
Paul Bakker33b43f12013-08-20 11:48:36 +0200308/* BEGIN_CASE */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200309void decrypt_test_vec( int cipher_id, int pad_mode,
310 char *hex_key, char *hex_iv,
311 char *hex_cipher, char *hex_clear,
312 char *hex_ad, char *hex_tag,
313 int finish_result, int tag_result )
314{
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200315 unsigned char key[50];
316 unsigned char iv[50];
317 unsigned char cipher[200];
318 unsigned char clear[200];
319 unsigned char ad[200];
320 unsigned char tag[20];
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200321 size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
322 cipher_context_t ctx;
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200323 unsigned char output[200];
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200324 size_t outlen, total_len;
325
326 memset( key, 0x00, sizeof( key ) );
327 memset( iv, 0x00, sizeof( iv ) );
328 memset( cipher, 0x00, sizeof( cipher ) );
329 memset( clear, 0x00, sizeof( clear ) );
330 memset( ad, 0x00, sizeof( ad ) );
331 memset( tag, 0x00, sizeof( tag ) );
332 memset( output, 0x00, sizeof( output ) );
333
334 key_len = unhexify( key, hex_key );
335 iv_len = unhexify( iv, hex_iv );
336 cipher_len = unhexify( cipher, hex_cipher );
337 clear_len = unhexify( clear, hex_clear );
338 ad_len = unhexify( ad, hex_ad );
339 tag_len = unhexify( tag, hex_tag );
340
341 /* Prepare context */
342 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
343 cipher_info_from_type( cipher_id ) ) );
344 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
345 if( pad_mode != -1 )
346 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
347 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
348 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
349#if defined(POLARSSL_CIPHER_MODE_AEAD)
350 TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
351#endif /* POLARSSL_CIPHER_MODE_AEAD */
352
353 /* decode buffer and check tag */
354 total_len = 0;
355 TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
356 total_len += outlen;
357 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
358 &outlen ) );
359 total_len += outlen;
360#if defined(POLARSSL_CIPHER_MODE_AEAD)
361 TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
362#endif /* POLARSSL_CIPHER_MODE_AEAD */
363
364 /* check plaintext only if everything went fine */
365 if( 0 == finish_result && 0 == tag_result )
366 {
367 TEST_ASSERT( total_len == clear_len );
368 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
369 }
370
371 cipher_free_ctx( &ctx );
372}
373/* END_CASE */
374
375/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +0200376void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200377{
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200378 const cipher_info_t *cipher_info;
379 cipher_context_t ctx;
380
Paul Bakker33b43f12013-08-20 11:48:36 +0200381 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200382 TEST_ASSERT( NULL != cipher_info );
383 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
384
Paul Bakker33b43f12013-08-20 11:48:36 +0200385 TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200386
387 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200388}
Paul Bakker33b43f12013-08-20 11:48:36 +0200389/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000390
Paul Bakker33b43f12013-08-20 11:48:36 +0200391/* BEGIN_CASE */
392void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200393{
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200394 cipher_info_t cipher_info;
395 cipher_context_t ctx;
396 unsigned char input[16];
397 size_t ilen, dlen;
398
399 /* build a fake context just for getting access to get_padding */
400 memset( &ctx, 0, sizeof( ctx ) );
401 cipher_info.mode = POLARSSL_MODE_CBC;
402 ctx.cipher_info = &cipher_info;
403
Paul Bakker33b43f12013-08-20 11:48:36 +0200404 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200405
Paul Bakker33b43f12013-08-20 11:48:36 +0200406 ilen = unhexify( input, input_str );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200407
Paul Bakker33b43f12013-08-20 11:48:36 +0200408 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
409 if( 0 == ret )
410 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200411}
Paul Bakker33b43f12013-08-20 11:48:36 +0200412/* END_CASE */
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200413
Paul Bakker33b43f12013-08-20 11:48:36 +0200414/* BEGIN_CASE */
415void cipher_selftest()
Paul Bakker8123e9d2011-01-06 15:37:30 +0000416{
417 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
418}
Paul Bakker33b43f12013-08-20 11:48:36 +0200419/* END_CASE */