blob: d247bab2a738a9d48074091d51921d248637dae8 [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
64 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, ad, 13 ) );
65 TEST_ASSERT( 0 == cipher_reset( &ctx_enc, ad, 13 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000066
Paul Bakker8123e9d2011-01-06 15:37:30 +000067 /* encode length number of bytes from inbuf */
68 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020069 total_len = outlen;
70
71 TEST_ASSERT( total_len == length ||
72 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
73 total_len < length &&
74 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000075
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +020076 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen,
77 tag, 16 ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020078 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000079
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020080 TEST_ASSERT( total_len == length ||
81 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
82 total_len > length &&
83 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000084
85 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020086 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
87 total_len = outlen;
88
89 TEST_ASSERT( total_len == length ||
90 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
91 total_len < length &&
92 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000093
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +020094 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen,
95 tag, 16 ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020096 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000097
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020098 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +000099
100 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
101
102 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
103 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200104}
Paul Bakker33b43f12013-08-20 11:48:36 +0200105/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000106
Paul Bakker33b43f12013-08-20 11:48:36 +0200107/* BEGIN_CASE */
108void enc_fail( int cipher_id, int pad_mode, int key_len,
109 int length_val, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200110{
Paul Bakker33b43f12013-08-20 11:48:36 +0200111 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200112 unsigned char key[32];
113 unsigned char iv[16];
114
115 const cipher_info_t *cipher_info;
116 cipher_context_t ctx;
117
118 unsigned char inbuf[64];
119 unsigned char encbuf[64];
120
121 size_t outlen = 0;
122
123 memset( key, 0, 32 );
124 memset( iv , 0, 16 );
125
126 memset( &ctx, 0, sizeof( ctx ) );
127
128 memset( inbuf, 5, 64 );
129 memset( encbuf, 0, 64 );
130
131 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +0200132 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200133 TEST_ASSERT( NULL != cipher_info );
134
135 /* Initialise context */
136 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200137 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
138 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200139 TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
140 TEST_ASSERT( 0 == cipher_reset( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200141
142 /* encode length number of bytes from inbuf */
143 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200144 TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen, NULL, 0 ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200145
146 /* done */
147 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200148}
Paul Bakker33b43f12013-08-20 11:48:36 +0200149/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200150
Paul Bakker33b43f12013-08-20 11:48:36 +0200151/* BEGIN_CASE */
152void dec_empty_buf()
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200153{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000154 unsigned char key[32];
155 unsigned char iv[16];
156
157 cipher_context_t ctx_dec;
158 const cipher_info_t *cipher_info;
159
160 unsigned char encbuf[64];
161 unsigned char decbuf[64];
162
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000163 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164
165 memset( key, 0, 32 );
166 memset( iv , 0, 16 );
167
168 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
169
170 memset( encbuf, 0, 64 );
171 memset( decbuf, 0, 64 );
172
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200173 /* Initialise context */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000174 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
175 TEST_ASSERT( NULL != cipher_info);
176
177 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
178
179 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
180
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200181 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
182
183 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, NULL, 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000184
185 /* decode 0-byte string */
186 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
187 TEST_ASSERT( 0 == outlen );
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200188 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish(
189 &ctx_dec, decbuf + outlen, &outlen, NULL, 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000190 TEST_ASSERT( 0 == outlen );
191
192 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200193}
Paul Bakker33b43f12013-08-20 11:48:36 +0200194/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000195
Paul Bakker33b43f12013-08-20 11:48:36 +0200196/* BEGIN_CASE */
197void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
198 int second_length_val )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200199{
Paul Bakker33b43f12013-08-20 11:48:36 +0200200 size_t first_length = first_length_val;
201 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000202 size_t length = first_length + second_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203 unsigned char key[32];
204 unsigned char iv[16];
205
206 cipher_context_t ctx_dec;
207 cipher_context_t ctx_enc;
208 const cipher_info_t *cipher_info;
209
210 unsigned char inbuf[64];
211 unsigned char encbuf[64];
212 unsigned char decbuf[64];
213
Paul Bakker23986e52011-04-24 08:57:21 +0000214 size_t outlen = 0;
215 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000216
217 memset( key, 0, 32 );
218 memset( iv , 0, 16 );
219
220 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
221 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
222
223 memset( inbuf, 5, 64 );
224 memset( encbuf, 0, 64 );
225 memset( decbuf, 0, 64 );
226
227 /* Initialise enc and dec contexts */
Paul Bakker33b43f12013-08-20 11:48:36 +0200228 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000229 TEST_ASSERT( NULL != cipher_info);
230
231 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
232 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
233
Paul Bakker33b43f12013-08-20 11:48:36 +0200234 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
235 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000236
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200237 TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
238 TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
239
240 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, NULL, 0 ) );
241 TEST_ASSERT( 0 == cipher_reset( &ctx_enc, NULL, 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000242
Paul Bakker8123e9d2011-01-06 15:37:30 +0000243 /* encode length number of bytes from inbuf */
244 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
245 totaloutlen = outlen;
246 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
247 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200248 TEST_ASSERT( totaloutlen == length ||
249 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
250 totaloutlen < length &&
251 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
252
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200253 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen,
254 NULL, 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000255 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200256 TEST_ASSERT( totaloutlen == length ||
257 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
258 totaloutlen > length &&
259 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000260
261 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200262 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
263 totaloutlen = outlen;
264
265 TEST_ASSERT( totaloutlen == length ||
266 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
267 totaloutlen < length &&
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200268 totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200269
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200270 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen,
271 NULL, 0 ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200272 totaloutlen += outlen;
273
274 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000275
276 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
277
278 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
279 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200280}
Paul Bakker33b43f12013-08-20 11:48:36 +0200281/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000282
Paul Bakker33b43f12013-08-20 11:48:36 +0200283/* BEGIN_CASE */
284void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200285{
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200286 const cipher_info_t *cipher_info;
287 cipher_context_t ctx;
288
Paul Bakker33b43f12013-08-20 11:48:36 +0200289 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200290 TEST_ASSERT( NULL != cipher_info );
291 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
292
Paul Bakker33b43f12013-08-20 11:48:36 +0200293 TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200294
295 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200296}
Paul Bakker33b43f12013-08-20 11:48:36 +0200297/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000298
Paul Bakker33b43f12013-08-20 11:48:36 +0200299/* BEGIN_CASE */
300void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200301{
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200302 cipher_info_t cipher_info;
303 cipher_context_t ctx;
304 unsigned char input[16];
305 size_t ilen, dlen;
306
307 /* build a fake context just for getting access to get_padding */
308 memset( &ctx, 0, sizeof( ctx ) );
309 cipher_info.mode = POLARSSL_MODE_CBC;
310 ctx.cipher_info = &cipher_info;
311
Paul Bakker33b43f12013-08-20 11:48:36 +0200312 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200313
Paul Bakker33b43f12013-08-20 11:48:36 +0200314 ilen = unhexify( input, input_str );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200315
Paul Bakker33b43f12013-08-20 11:48:36 +0200316 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
317 if( 0 == ret )
318 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200319}
Paul Bakker33b43f12013-08-20 11:48:36 +0200320/* END_CASE */
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200321
Paul Bakker33b43f12013-08-20 11:48:36 +0200322/* BEGIN_CASE */
323void cipher_selftest()
Paul Bakker8123e9d2011-01-06 15:37:30 +0000324{
325 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
326}
Paul Bakker33b43f12013-08-20 11:48:36 +0200327/* END_CASE */