blob: 5d32bc3d1f2ff32253a1f2340c43d8811e243c10 [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
67 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, 13 ) );
68 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, 13 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000069
Paul Bakker8123e9d2011-01-06 15:37:30 +000070 /* encode length number of bytes from inbuf */
71 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020072 total_len = outlen;
73
74 TEST_ASSERT( total_len == length ||
75 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
76 total_len < length &&
77 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000078
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +020079 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020080 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000081
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +020082 TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, 16 ) );
83
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020084 TEST_ASSERT( total_len == length ||
85 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
86 total_len > length &&
87 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000088
89 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020090 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
91 total_len = outlen;
92
93 TEST_ASSERT( total_len == length ||
94 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
95 total_len < length &&
96 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000097
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +020098 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020099 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000100
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200101 TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, 16 ) );
102
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200103 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000104
105 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
106
107 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
108 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200109}
Paul Bakker33b43f12013-08-20 11:48:36 +0200110/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000111
Paul Bakker33b43f12013-08-20 11:48:36 +0200112/* BEGIN_CASE */
113void enc_fail( int cipher_id, int pad_mode, int key_len,
114 int length_val, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200115{
Paul Bakker33b43f12013-08-20 11:48:36 +0200116 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200117 unsigned char key[32];
118 unsigned char iv[16];
119
120 const cipher_info_t *cipher_info;
121 cipher_context_t ctx;
122
123 unsigned char inbuf[64];
124 unsigned char encbuf[64];
125
126 size_t outlen = 0;
127
128 memset( key, 0, 32 );
129 memset( iv , 0, 16 );
130
131 memset( &ctx, 0, sizeof( ctx ) );
132
133 memset( inbuf, 5, 64 );
134 memset( encbuf, 0, 64 );
135
136 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +0200137 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200138 TEST_ASSERT( NULL != cipher_info );
139
140 /* Initialise context */
141 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200142 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
143 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200144 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200145 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
146 TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200147
148 /* encode length number of bytes from inbuf */
149 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200150 TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200151
152 /* done */
153 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200154}
Paul Bakker33b43f12013-08-20 11:48:36 +0200155/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200156
Paul Bakker33b43f12013-08-20 11:48:36 +0200157/* BEGIN_CASE */
158void dec_empty_buf()
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200159{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000160 unsigned char key[32];
161 unsigned char iv[16];
162
163 cipher_context_t ctx_dec;
164 const cipher_info_t *cipher_info;
165
166 unsigned char encbuf[64];
167 unsigned char decbuf[64];
168
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000169 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000170
171 memset( key, 0, 32 );
172 memset( iv , 0, 16 );
173
174 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
175
176 memset( encbuf, 0, 64 );
177 memset( decbuf, 0, 64 );
178
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200179 /* Initialise context */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000180 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
181 TEST_ASSERT( NULL != cipher_info);
182
183 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
184
185 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
186
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200187 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
188
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200189 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
190
191 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000192
193 /* decode 0-byte string */
194 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
195 TEST_ASSERT( 0 == outlen );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200196 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish(
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200197 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000198 TEST_ASSERT( 0 == outlen );
199
200 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200201}
Paul Bakker33b43f12013-08-20 11:48:36 +0200202/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203
Paul Bakker33b43f12013-08-20 11:48:36 +0200204/* BEGIN_CASE */
205void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
206 int second_length_val )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200207{
Paul Bakker33b43f12013-08-20 11:48:36 +0200208 size_t first_length = first_length_val;
209 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000210 size_t length = first_length + second_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000211 unsigned char key[32];
212 unsigned char iv[16];
213
214 cipher_context_t ctx_dec;
215 cipher_context_t ctx_enc;
216 const cipher_info_t *cipher_info;
217
218 unsigned char inbuf[64];
219 unsigned char encbuf[64];
220 unsigned char decbuf[64];
221
Paul Bakker23986e52011-04-24 08:57:21 +0000222 size_t outlen = 0;
223 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000224
225 memset( key, 0, 32 );
226 memset( iv , 0, 16 );
227
228 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
229 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
230
231 memset( inbuf, 5, 64 );
232 memset( encbuf, 0, 64 );
233 memset( decbuf, 0, 64 );
234
235 /* Initialise enc and dec contexts */
Paul Bakker33b43f12013-08-20 11:48:36 +0200236 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000237 TEST_ASSERT( NULL != cipher_info);
238
239 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
240 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
241
Paul Bakker33b43f12013-08-20 11:48:36 +0200242 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
243 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000244
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200245 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
246 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
247
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200248 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
249 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
250
251 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
252 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000253
Paul Bakker8123e9d2011-01-06 15:37:30 +0000254 /* encode length number of bytes from inbuf */
255 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
256 totaloutlen = outlen;
257 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
258 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200259 TEST_ASSERT( totaloutlen == length ||
260 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
261 totaloutlen < length &&
262 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
263
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200264 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200266 TEST_ASSERT( totaloutlen == length ||
267 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
268 totaloutlen > length &&
269 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000270
271 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200272 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
273 totaloutlen = outlen;
274
275 TEST_ASSERT( totaloutlen == length ||
276 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
277 totaloutlen < length &&
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200278 totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200279
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200280 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200281 totaloutlen += outlen;
282
283 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000284
285 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
286
287 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
288 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200289}
Paul Bakker33b43f12013-08-20 11:48:36 +0200290/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000291
Paul Bakker33b43f12013-08-20 11:48:36 +0200292/* BEGIN_CASE */
293void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200294{
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200295 const cipher_info_t *cipher_info;
296 cipher_context_t ctx;
297
Paul Bakker33b43f12013-08-20 11:48:36 +0200298 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200299 TEST_ASSERT( NULL != cipher_info );
300 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
301
Paul Bakker33b43f12013-08-20 11:48:36 +0200302 TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200303
304 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
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 */
309void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200310{
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200311 cipher_info_t cipher_info;
312 cipher_context_t ctx;
313 unsigned char input[16];
314 size_t ilen, dlen;
315
316 /* build a fake context just for getting access to get_padding */
317 memset( &ctx, 0, sizeof( ctx ) );
318 cipher_info.mode = POLARSSL_MODE_CBC;
319 ctx.cipher_info = &cipher_info;
320
Paul Bakker33b43f12013-08-20 11:48:36 +0200321 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200322
Paul Bakker33b43f12013-08-20 11:48:36 +0200323 ilen = unhexify( input, input_str );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200324
Paul Bakker33b43f12013-08-20 11:48:36 +0200325 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
326 if( 0 == ret )
327 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200328}
Paul Bakker33b43f12013-08-20 11:48:36 +0200329/* END_CASE */
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200330
Paul Bakker33b43f12013-08-20 11:48:36 +0200331/* BEGIN_CASE */
332void cipher_selftest()
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333{
334 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
335}
Paul Bakker33b43f12013-08-20 11:48:36 +0200336/* END_CASE */