blob: b1814fab05ea5b61ec920ef6dad091fd58f9d2ff [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é-Gonnard9241be72013-08-31 17:31:03 +020061 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv, 16, ad, 13 ) );
62 TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv, 16, ad, 13 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000063
Paul Bakker8123e9d2011-01-06 15:37:30 +000064 /* encode length number of bytes from inbuf */
65 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020066 total_len = outlen;
67
68 TEST_ASSERT( total_len == length ||
69 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
70 total_len < length &&
71 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000072
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +020073 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen,
74 tag, 16 ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020075 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000076
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020077 TEST_ASSERT( total_len == length ||
78 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
79 total_len > length &&
80 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000081
82 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020083 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
84 total_len = outlen;
85
86 TEST_ASSERT( total_len == length ||
87 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
88 total_len < length &&
89 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000090
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +020091 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen,
92 tag, 16 ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020093 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000094
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020095 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +000096
97 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
98
99 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
100 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200101}
Paul Bakker33b43f12013-08-20 11:48:36 +0200102/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000103
Paul Bakker33b43f12013-08-20 11:48:36 +0200104/* BEGIN_CASE */
105void enc_fail( int cipher_id, int pad_mode, int key_len,
106 int length_val, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200107{
Paul Bakker33b43f12013-08-20 11:48:36 +0200108 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200109 unsigned char key[32];
110 unsigned char iv[16];
111
112 const cipher_info_t *cipher_info;
113 cipher_context_t ctx;
114
115 unsigned char inbuf[64];
116 unsigned char encbuf[64];
117
118 size_t outlen = 0;
119
120 memset( key, 0, 32 );
121 memset( iv , 0, 16 );
122
123 memset( &ctx, 0, sizeof( ctx ) );
124
125 memset( inbuf, 5, 64 );
126 memset( encbuf, 0, 64 );
127
128 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +0200129 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200130 TEST_ASSERT( NULL != cipher_info );
131
132 /* Initialise context */
133 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200134 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
135 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200136 TEST_ASSERT( 0 == cipher_reset( &ctx, iv, 16, NULL, 0 ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200137
138 /* encode length number of bytes from inbuf */
139 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200140 TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen, NULL, 0 ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200141
142 /* done */
143 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200144}
Paul Bakker33b43f12013-08-20 11:48:36 +0200145/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200146
Paul Bakker33b43f12013-08-20 11:48:36 +0200147/* BEGIN_CASE */
148void dec_empty_buf()
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200149{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000150 unsigned char key[32];
151 unsigned char iv[16];
152
153 cipher_context_t ctx_dec;
154 const cipher_info_t *cipher_info;
155
156 unsigned char encbuf[64];
157 unsigned char decbuf[64];
158
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000159 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000160
161 memset( key, 0, 32 );
162 memset( iv , 0, 16 );
163
164 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
165
166 memset( encbuf, 0, 64 );
167 memset( decbuf, 0, 64 );
168
169 /* Initialise enc and dec contexts */
170 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
171 TEST_ASSERT( NULL != cipher_info);
172
173 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
174
175 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
176
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200177 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv, 16, NULL, 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000178
179 /* decode 0-byte string */
180 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
181 TEST_ASSERT( 0 == outlen );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200182 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish(
183 &ctx_dec, decbuf + outlen, &outlen, NULL, 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000184 TEST_ASSERT( 0 == outlen );
185
186 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200187}
Paul Bakker33b43f12013-08-20 11:48:36 +0200188/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000189
Paul Bakker33b43f12013-08-20 11:48:36 +0200190/* BEGIN_CASE */
191void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
192 int second_length_val )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200193{
Paul Bakker33b43f12013-08-20 11:48:36 +0200194 size_t first_length = first_length_val;
195 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000196 size_t length = first_length + second_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000197 unsigned char key[32];
198 unsigned char iv[16];
199
200 cipher_context_t ctx_dec;
201 cipher_context_t ctx_enc;
202 const cipher_info_t *cipher_info;
203
204 unsigned char inbuf[64];
205 unsigned char encbuf[64];
206 unsigned char decbuf[64];
207
Paul Bakker23986e52011-04-24 08:57:21 +0000208 size_t outlen = 0;
209 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000210
211 memset( key, 0, 32 );
212 memset( iv , 0, 16 );
213
214 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
215 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
216
217 memset( inbuf, 5, 64 );
218 memset( encbuf, 0, 64 );
219 memset( decbuf, 0, 64 );
220
221 /* Initialise enc and dec contexts */
Paul Bakker33b43f12013-08-20 11:48:36 +0200222 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000223 TEST_ASSERT( NULL != cipher_info);
224
225 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
226 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
227
Paul Bakker33b43f12013-08-20 11:48:36 +0200228 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
229 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000230
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200231 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv, 16, NULL, 0 ) );
232 TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv, 16, NULL, 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000233
Paul Bakker8123e9d2011-01-06 15:37:30 +0000234 /* encode length number of bytes from inbuf */
235 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
236 totaloutlen = outlen;
237 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
238 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200239 TEST_ASSERT( totaloutlen == length ||
240 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
241 totaloutlen < length &&
242 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
243
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200244 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen,
245 NULL, 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000246 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200247 TEST_ASSERT( totaloutlen == length ||
248 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
249 totaloutlen > length &&
250 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000251
252 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200253 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
254 totaloutlen = outlen;
255
256 TEST_ASSERT( totaloutlen == length ||
257 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
258 totaloutlen < length &&
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200259 totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200260
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200261 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen,
262 NULL, 0 ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200263 totaloutlen += outlen;
264
265 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000266
267 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
268
269 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
270 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200271}
Paul Bakker33b43f12013-08-20 11:48:36 +0200272/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000273
Paul Bakker33b43f12013-08-20 11:48:36 +0200274/* BEGIN_CASE */
275void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200276{
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200277 const cipher_info_t *cipher_info;
278 cipher_context_t ctx;
279
Paul Bakker33b43f12013-08-20 11:48:36 +0200280 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200281 TEST_ASSERT( NULL != cipher_info );
282 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
283
Paul Bakker33b43f12013-08-20 11:48:36 +0200284 TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200285
286 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200287}
Paul Bakker33b43f12013-08-20 11:48:36 +0200288/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000289
Paul Bakker33b43f12013-08-20 11:48:36 +0200290/* BEGIN_CASE */
291void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200292{
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200293 cipher_info_t cipher_info;
294 cipher_context_t ctx;
295 unsigned char input[16];
296 size_t ilen, dlen;
297
298 /* build a fake context just for getting access to get_padding */
299 memset( &ctx, 0, sizeof( ctx ) );
300 cipher_info.mode = POLARSSL_MODE_CBC;
301 ctx.cipher_info = &cipher_info;
302
Paul Bakker33b43f12013-08-20 11:48:36 +0200303 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200304
Paul Bakker33b43f12013-08-20 11:48:36 +0200305 ilen = unhexify( input, input_str );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200306
Paul Bakker33b43f12013-08-20 11:48:36 +0200307 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
308 if( 0 == ret )
309 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200310}
Paul Bakker33b43f12013-08-20 11:48:36 +0200311/* END_CASE */
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200312
Paul Bakker33b43f12013-08-20 11:48:36 +0200313/* BEGIN_CASE */
314void cipher_selftest()
Paul Bakker8123e9d2011-01-06 15:37:30 +0000315{
316 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
317}
Paul Bakker33b43f12013-08-20 11:48:36 +0200318/* END_CASE */