blob: 9dad06c72d9504fd811c338af83a69dbbd1e97ef [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>
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
6 * depends_on:POLARSSL_CIPHER_C
7 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
11void enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
12 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +020013{
Paul Bakker33b43f12013-08-20 11:48:36 +020014 size_t length = length_val;
Paul Bakker8123e9d2011-01-06 15:37:30 +000015 unsigned char key[32];
16 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +020017 unsigned char ad[13];
18 unsigned char tag[16];
Paul Bakker8123e9d2011-01-06 15:37:30 +000019
20 const cipher_info_t *cipher_info;
21 cipher_context_t ctx_dec;
22 cipher_context_t ctx_enc;
23
24 unsigned char inbuf[64];
25 unsigned char encbuf[64];
26 unsigned char decbuf[64];
27
Paul Bakker23986e52011-04-24 08:57:21 +000028 size_t outlen = 0;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020029 size_t total_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +000030
31 memset( key, 0, 32 );
32 memset( iv , 0, 16 );
33
34 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
35 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
36
37 memset( inbuf, 5, 64 );
38 memset( encbuf, 0, 64 );
39 memset( decbuf, 0, 64 );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +020040 memset( tag, 0, 16 );
41 memset( ad, 0x2a, 13 );
Paul Bakker8123e9d2011-01-06 15:37:30 +000042
43 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +020044 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +000045 TEST_ASSERT( NULL != cipher_info );
Paul Bakker33b43f12013-08-20 11:48:36 +020046 TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +000047
48 /* Initialise enc and dec contexts */
49 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
50 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
51
Paul Bakker33b43f12013-08-20 11:48:36 +020052 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
53 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000054
Paul Bakker33b43f12013-08-20 11:48:36 +020055 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020056 {
Paul Bakker33b43f12013-08-20 11:48:36 +020057 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) );
58 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020059 }
60
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +020061 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
62 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
63
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +020064 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
65 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
66
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020067#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +020068 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, 13 ) );
69 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, 13 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020070#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +000071
Paul Bakker8123e9d2011-01-06 15:37:30 +000072 /* encode length number of bytes from inbuf */
73 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020074 total_len = outlen;
75
76 TEST_ASSERT( total_len == length ||
77 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
78 total_len < length &&
79 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000080
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +020081 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020082 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000083
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020084#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +020085 TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, 16 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +020086#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +020087
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020088 TEST_ASSERT( total_len == length ||
89 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
90 total_len > length &&
91 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000092
93 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020094 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
95 total_len = outlen;
96
97 TEST_ASSERT( total_len == length ||
98 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
99 total_len < length &&
100 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000101
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200102 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200103 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000104
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200105#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200106 TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, 16 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200107#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200108
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200109 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000110
111 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
112
113 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
114 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200115}
Paul Bakker33b43f12013-08-20 11:48:36 +0200116/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000117
Paul Bakker33b43f12013-08-20 11:48:36 +0200118/* BEGIN_CASE */
119void enc_fail( int cipher_id, int pad_mode, int key_len,
120 int length_val, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200121{
Paul Bakker33b43f12013-08-20 11:48:36 +0200122 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200123 unsigned char key[32];
124 unsigned char iv[16];
125
126 const cipher_info_t *cipher_info;
127 cipher_context_t ctx;
128
129 unsigned char inbuf[64];
130 unsigned char encbuf[64];
131
132 size_t outlen = 0;
133
134 memset( key, 0, 32 );
135 memset( iv , 0, 16 );
136
137 memset( &ctx, 0, sizeof( ctx ) );
138
139 memset( inbuf, 5, 64 );
140 memset( encbuf, 0, 64 );
141
142 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +0200143 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200144 TEST_ASSERT( NULL != cipher_info );
145
146 /* Initialise context */
147 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200148 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
149 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200150 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200151 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200152#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200153 TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200154#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200155
156 /* encode length number of bytes from inbuf */
157 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200158 TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200159
160 /* done */
161 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200162}
Paul Bakker33b43f12013-08-20 11:48:36 +0200163/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200164
Paul Bakker33b43f12013-08-20 11:48:36 +0200165/* BEGIN_CASE */
166void dec_empty_buf()
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200167{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000168 unsigned char key[32];
169 unsigned char iv[16];
170
171 cipher_context_t ctx_dec;
172 const cipher_info_t *cipher_info;
173
174 unsigned char encbuf[64];
175 unsigned char decbuf[64];
176
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000177 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000178
179 memset( key, 0, 32 );
180 memset( iv , 0, 16 );
181
182 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
183
184 memset( encbuf, 0, 64 );
185 memset( decbuf, 0, 64 );
186
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200187 /* Initialise context */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000188 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
189 TEST_ASSERT( NULL != cipher_info);
190
191 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
192
193 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
194
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200195 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
196
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200197 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
198
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200199#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200200 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200201#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000202
203 /* decode 0-byte string */
204 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
205 TEST_ASSERT( 0 == outlen );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200206 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish(
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200207 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000208 TEST_ASSERT( 0 == outlen );
209
210 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200211}
Paul Bakker33b43f12013-08-20 11:48:36 +0200212/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000213
Paul Bakker33b43f12013-08-20 11:48:36 +0200214/* BEGIN_CASE */
215void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
216 int second_length_val )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200217{
Paul Bakker33b43f12013-08-20 11:48:36 +0200218 size_t first_length = first_length_val;
219 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000220 size_t length = first_length + second_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000221 unsigned char key[32];
222 unsigned char iv[16];
223
224 cipher_context_t ctx_dec;
225 cipher_context_t ctx_enc;
226 const cipher_info_t *cipher_info;
227
228 unsigned char inbuf[64];
229 unsigned char encbuf[64];
230 unsigned char decbuf[64];
231
Paul Bakker23986e52011-04-24 08:57:21 +0000232 size_t outlen = 0;
233 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000234
235 memset( key, 0, 32 );
236 memset( iv , 0, 16 );
237
238 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
239 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
240
241 memset( inbuf, 5, 64 );
242 memset( encbuf, 0, 64 );
243 memset( decbuf, 0, 64 );
244
245 /* Initialise enc and dec contexts */
Paul Bakker33b43f12013-08-20 11:48:36 +0200246 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000247 TEST_ASSERT( NULL != cipher_info);
248
249 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
250 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
251
Paul Bakker33b43f12013-08-20 11:48:36 +0200252 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
253 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000254
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200255 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
256 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
257
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200258 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
259 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
260
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200261#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200262 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
263 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200264#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265
Paul Bakker8123e9d2011-01-06 15:37:30 +0000266 /* encode length number of bytes from inbuf */
267 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
268 totaloutlen = outlen;
269 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
270 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200271 TEST_ASSERT( totaloutlen == length ||
272 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
273 totaloutlen < length &&
274 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
275
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200276 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000277 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200278 TEST_ASSERT( totaloutlen == length ||
279 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
280 totaloutlen > length &&
281 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000282
283 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200284 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
285 totaloutlen = outlen;
286
287 TEST_ASSERT( totaloutlen == length ||
288 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
289 totaloutlen < length &&
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200290 totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200291
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200292 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200293 totaloutlen += outlen;
294
295 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000296
297 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
298
299 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
300 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200301}
Paul Bakker33b43f12013-08-20 11:48:36 +0200302/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000303
Paul Bakker33b43f12013-08-20 11:48:36 +0200304/* BEGIN_CASE */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200305void decrypt_test_vec( int cipher_id, int pad_mode,
306 char *hex_key, char *hex_iv,
307 char *hex_cipher, char *hex_clear,
308 char *hex_ad, char *hex_tag,
309 int finish_result, int tag_result )
310{
311 unsigned char key[100];
312 unsigned char iv[100];
313 unsigned char cipher[100];
314 unsigned char clear[100];
315 unsigned char ad[100];
316 unsigned char tag[100];
317 size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
318 cipher_context_t ctx;
319 unsigned char output[100];
320 size_t outlen, total_len;
321
322 memset( key, 0x00, sizeof( key ) );
323 memset( iv, 0x00, sizeof( iv ) );
324 memset( cipher, 0x00, sizeof( cipher ) );
325 memset( clear, 0x00, sizeof( clear ) );
326 memset( ad, 0x00, sizeof( ad ) );
327 memset( tag, 0x00, sizeof( tag ) );
328 memset( output, 0x00, sizeof( output ) );
329
330 key_len = unhexify( key, hex_key );
331 iv_len = unhexify( iv, hex_iv );
332 cipher_len = unhexify( cipher, hex_cipher );
333 clear_len = unhexify( clear, hex_clear );
334 ad_len = unhexify( ad, hex_ad );
335 tag_len = unhexify( tag, hex_tag );
336
337 /* Prepare context */
338 TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
339 cipher_info_from_type( cipher_id ) ) );
340 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
341 if( pad_mode != -1 )
342 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
343 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
344 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
345#if defined(POLARSSL_CIPHER_MODE_AEAD)
346 TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
347#endif /* POLARSSL_CIPHER_MODE_AEAD */
348
349 /* decode buffer and check tag */
350 total_len = 0;
351 TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
352 total_len += outlen;
353 TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
354 &outlen ) );
355 total_len += outlen;
356#if defined(POLARSSL_CIPHER_MODE_AEAD)
357 TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
358#endif /* POLARSSL_CIPHER_MODE_AEAD */
359
360 /* check plaintext only if everything went fine */
361 if( 0 == finish_result && 0 == tag_result )
362 {
363 TEST_ASSERT( total_len == clear_len );
364 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
365 }
366
367 cipher_free_ctx( &ctx );
368}
369/* END_CASE */
370
371/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +0200372void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200373{
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200374 const cipher_info_t *cipher_info;
375 cipher_context_t ctx;
376
Paul Bakker33b43f12013-08-20 11:48:36 +0200377 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200378 TEST_ASSERT( NULL != cipher_info );
379 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
380
Paul Bakker33b43f12013-08-20 11:48:36 +0200381 TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200382
383 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200384}
Paul Bakker33b43f12013-08-20 11:48:36 +0200385/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000386
Paul Bakker33b43f12013-08-20 11:48:36 +0200387/* BEGIN_CASE */
388void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200389{
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200390 cipher_info_t cipher_info;
391 cipher_context_t ctx;
392 unsigned char input[16];
393 size_t ilen, dlen;
394
395 /* build a fake context just for getting access to get_padding */
396 memset( &ctx, 0, sizeof( ctx ) );
397 cipher_info.mode = POLARSSL_MODE_CBC;
398 ctx.cipher_info = &cipher_info;
399
Paul Bakker33b43f12013-08-20 11:48:36 +0200400 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200401
Paul Bakker33b43f12013-08-20 11:48:36 +0200402 ilen = unhexify( input, input_str );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200403
Paul Bakker33b43f12013-08-20 11:48:36 +0200404 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
405 if( 0 == ret )
406 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200407}
Paul Bakker33b43f12013-08-20 11:48:36 +0200408/* END_CASE */
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200409
Paul Bakker33b43f12013-08-20 11:48:36 +0200410/* BEGIN_CASE */
411void cipher_selftest()
Paul Bakker8123e9d2011-01-06 15:37:30 +0000412{
413 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
414}
Paul Bakker33b43f12013-08-20 11:48:36 +0200415/* END_CASE */