blob: aa82daae9bc87bc671ba9ca4393be16631e16904 [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é-Gonnard9241be72013-08-31 17:31:03 +020079 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen,
80 tag, 16 ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020081 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000082
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020083 TEST_ASSERT( total_len == length ||
84 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
85 total_len > length &&
86 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000087
88 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020089 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
90 total_len = outlen;
91
92 TEST_ASSERT( total_len == length ||
93 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
94 total_len < length &&
95 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000096
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +020097 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen,
98 tag, 16 ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020099 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000100
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200101 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000102
103 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
104
105 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
106 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200107}
Paul Bakker33b43f12013-08-20 11:48:36 +0200108/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000109
Paul Bakker33b43f12013-08-20 11:48:36 +0200110/* BEGIN_CASE */
111void enc_fail( int cipher_id, int pad_mode, int key_len,
112 int length_val, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200113{
Paul Bakker33b43f12013-08-20 11:48:36 +0200114 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200115 unsigned char key[32];
116 unsigned char iv[16];
117
118 const cipher_info_t *cipher_info;
119 cipher_context_t ctx;
120
121 unsigned char inbuf[64];
122 unsigned char encbuf[64];
123
124 size_t outlen = 0;
125
126 memset( key, 0, 32 );
127 memset( iv , 0, 16 );
128
129 memset( &ctx, 0, sizeof( ctx ) );
130
131 memset( inbuf, 5, 64 );
132 memset( encbuf, 0, 64 );
133
134 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +0200135 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200136 TEST_ASSERT( NULL != cipher_info );
137
138 /* Initialise context */
139 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200140 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
141 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200142 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200143 TEST_ASSERT( 0 == cipher_reset( &ctx ) );
144 TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200145
146 /* encode length number of bytes from inbuf */
147 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200148 TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen, NULL, 0 ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200149
150 /* done */
151 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200152}
Paul Bakker33b43f12013-08-20 11:48:36 +0200153/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200154
Paul Bakker33b43f12013-08-20 11:48:36 +0200155/* BEGIN_CASE */
156void dec_empty_buf()
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200157{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000158 unsigned char key[32];
159 unsigned char iv[16];
160
161 cipher_context_t ctx_dec;
162 const cipher_info_t *cipher_info;
163
164 unsigned char encbuf[64];
165 unsigned char decbuf[64];
166
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000167 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000168
169 memset( key, 0, 32 );
170 memset( iv , 0, 16 );
171
172 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
173
174 memset( encbuf, 0, 64 );
175 memset( decbuf, 0, 64 );
176
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200177 /* Initialise context */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000178 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
179 TEST_ASSERT( NULL != cipher_info);
180
181 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
182
183 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
184
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200185 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
186
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200187 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
188
189 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000190
191 /* decode 0-byte string */
192 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
193 TEST_ASSERT( 0 == outlen );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200194 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish(
195 &ctx_dec, decbuf + outlen, &outlen, NULL, 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000196 TEST_ASSERT( 0 == outlen );
197
198 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200199}
Paul Bakker33b43f12013-08-20 11:48:36 +0200200/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000201
Paul Bakker33b43f12013-08-20 11:48:36 +0200202/* BEGIN_CASE */
203void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
204 int second_length_val )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200205{
Paul Bakker33b43f12013-08-20 11:48:36 +0200206 size_t first_length = first_length_val;
207 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000208 size_t length = first_length + second_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000209 unsigned char key[32];
210 unsigned char iv[16];
211
212 cipher_context_t ctx_dec;
213 cipher_context_t ctx_enc;
214 const cipher_info_t *cipher_info;
215
216 unsigned char inbuf[64];
217 unsigned char encbuf[64];
218 unsigned char decbuf[64];
219
Paul Bakker23986e52011-04-24 08:57:21 +0000220 size_t outlen = 0;
221 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000222
223 memset( key, 0, 32 );
224 memset( iv , 0, 16 );
225
226 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
227 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
228
229 memset( inbuf, 5, 64 );
230 memset( encbuf, 0, 64 );
231 memset( decbuf, 0, 64 );
232
233 /* Initialise enc and dec contexts */
Paul Bakker33b43f12013-08-20 11:48:36 +0200234 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000235 TEST_ASSERT( NULL != cipher_info);
236
237 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
238 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
239
Paul Bakker33b43f12013-08-20 11:48:36 +0200240 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
241 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000242
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200243 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
244 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
245
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200246 TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
247 TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
248
249 TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
250 TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000251
Paul Bakker8123e9d2011-01-06 15:37:30 +0000252 /* encode length number of bytes from inbuf */
253 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
254 totaloutlen = outlen;
255 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
256 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200257 TEST_ASSERT( totaloutlen == length ||
258 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
259 totaloutlen < length &&
260 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
261
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200262 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen,
263 NULL, 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000264 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200265 TEST_ASSERT( totaloutlen == length ||
266 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
267 totaloutlen > length &&
268 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000269
270 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200271 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
272 totaloutlen = outlen;
273
274 TEST_ASSERT( totaloutlen == length ||
275 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
276 totaloutlen < length &&
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200277 totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200278
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200279 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen,
280 NULL, 0 ) );
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 */