blob: 636691071a21a828fe7c87fb9dd1259d97831260 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001BEGIN_HEADER
Paul Bakker8123e9d2011-01-06 15:37:30 +00002#include <polarssl/cipher.h>
3END_HEADER
4
Paul Bakker5690efc2011-05-26 13:16:06 +00005BEGIN_DEPENDENCIES
6depends_on:POLARSSL_CIPHER_C
7END_DEPENDENCIES
8
Paul Bakker8123e9d2011-01-06 15:37:30 +00009BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +020010enc_dec_buf:#cipher_id:cipher_string:#key_len:#length_val:#pad_mode
11{
12 size_t length = {length_val};
Paul Bakker8123e9d2011-01-06 15:37:30 +000013 unsigned char key[32];
14 unsigned char iv[16];
15
16 const cipher_info_t *cipher_info;
17 cipher_context_t ctx_dec;
18 cipher_context_t ctx_enc;
19
20 unsigned char inbuf[64];
21 unsigned char encbuf[64];
22 unsigned char decbuf[64];
23
Paul Bakker23986e52011-04-24 08:57:21 +000024 size_t outlen = 0;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020025 size_t total_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +000026
27 memset( key, 0, 32 );
28 memset( iv , 0, 16 );
29
30 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
31 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
32
33 memset( inbuf, 5, 64 );
34 memset( encbuf, 0, 64 );
35 memset( decbuf, 0, 64 );
36
37 /* Check and get info structures */
38 cipher_info = cipher_info_from_type( {cipher_id} );
39 TEST_ASSERT( NULL != cipher_info );
Paul Bakkerdbd443d2013-08-16 13:38:47 +020040 TEST_ASSERT( cipher_info_from_string( {cipher_string} ) == cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +000041
42 /* Initialise enc and dec contexts */
43 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
44 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
45
46 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, {key_len}, POLARSSL_DECRYPT ) );
47 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, {key_len}, POLARSSL_ENCRYPT ) );
48
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020049 if( -1 != {pad_mode} )
50 {
51 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, {pad_mode} ) );
52 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, {pad_mode} ) );
53 }
54
Paul Bakker8123e9d2011-01-06 15:37:30 +000055 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
56 TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) );
57
Paul Bakker8123e9d2011-01-06 15:37:30 +000058 /* encode length number of bytes from inbuf */
59 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020060 total_len = outlen;
61
62 TEST_ASSERT( total_len == length ||
63 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
64 total_len < length &&
65 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000066
Paul Bakker8123e9d2011-01-06 15:37:30 +000067 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020068 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000069
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020070 TEST_ASSERT( total_len == length ||
71 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
72 total_len > length &&
73 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000074
75 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020076 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
77 total_len = outlen;
78
79 TEST_ASSERT( total_len == length ||
80 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
81 total_len < length &&
82 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000083
Paul Bakker8123e9d2011-01-06 15:37:30 +000084 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020085 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000086
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020087 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +000088
89 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
90
91 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
92 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +020093}
Paul Bakker8123e9d2011-01-06 15:37:30 +000094END_CASE
95
96BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +020097enc_fail:#cipher_id:#pad_mode:#key_len:#length_val:#ret
98{
99 size_t length = {length_val};
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200100 unsigned char key[32];
101 unsigned char iv[16];
102
103 const cipher_info_t *cipher_info;
104 cipher_context_t ctx;
105
106 unsigned char inbuf[64];
107 unsigned char encbuf[64];
108
109 size_t outlen = 0;
110
111 memset( key, 0, 32 );
112 memset( iv , 0, 16 );
113
114 memset( &ctx, 0, sizeof( ctx ) );
115
116 memset( inbuf, 5, 64 );
117 memset( encbuf, 0, 64 );
118
119 /* Check and get info structures */
120 cipher_info = cipher_info_from_type( {cipher_id} );
121 TEST_ASSERT( NULL != cipher_info );
122
123 /* Initialise context */
124 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
125 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, {key_len}, POLARSSL_ENCRYPT ) );
126 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, {pad_mode} ) );
127 TEST_ASSERT( 0 == cipher_reset( &ctx, iv ) );
128
129 /* encode length number of bytes from inbuf */
130 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
131 TEST_ASSERT( {ret} == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
132
133 /* done */
134 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200135}
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200136END_CASE
137
138BEGIN_CASE
Paul Bakker8123e9d2011-01-06 15:37:30 +0000139dec_empty_buf:
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200140{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000141 unsigned char key[32];
142 unsigned char iv[16];
143
144 cipher_context_t ctx_dec;
145 const cipher_info_t *cipher_info;
146
147 unsigned char encbuf[64];
148 unsigned char decbuf[64];
149
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000150 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000151
152 memset( key, 0, 32 );
153 memset( iv , 0, 16 );
154
155 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
156
157 memset( encbuf, 0, 64 );
158 memset( decbuf, 0, 64 );
159
160 /* Initialise enc and dec contexts */
161 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
162 TEST_ASSERT( NULL != cipher_info);
163
164 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
165
166 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
167
168 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
169
170 /* decode 0-byte string */
171 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
172 TEST_ASSERT( 0 == outlen );
Paul Bakkerc65ab342011-06-09 15:44:37 +0000173 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000174 TEST_ASSERT( 0 == outlen );
175
176 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200177}
Paul Bakker8123e9d2011-01-06 15:37:30 +0000178END_CASE
179
180BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200181enc_dec_buf_multipart:#cipher_id:#key_len:#first_length_val:#second_length_val
182{
183 size_t first_length = {first_length_val};
184 size_t second_length = {second_length_val};
Paul Bakker23986e52011-04-24 08:57:21 +0000185 size_t length = first_length + second_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000186 unsigned char key[32];
187 unsigned char iv[16];
188
189 cipher_context_t ctx_dec;
190 cipher_context_t ctx_enc;
191 const cipher_info_t *cipher_info;
192
193 unsigned char inbuf[64];
194 unsigned char encbuf[64];
195 unsigned char decbuf[64];
196
Paul Bakker23986e52011-04-24 08:57:21 +0000197 size_t outlen = 0;
198 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000199
200 memset( key, 0, 32 );
201 memset( iv , 0, 16 );
202
203 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
204 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
205
206 memset( inbuf, 5, 64 );
207 memset( encbuf, 0, 64 );
208 memset( decbuf, 0, 64 );
209
210 /* Initialise enc and dec contexts */
211 cipher_info = cipher_info_from_type( {cipher_id} );
212 TEST_ASSERT( NULL != cipher_info);
213
214 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
215 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
216
217 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, {key_len}, POLARSSL_DECRYPT ) );
218 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, {key_len}, POLARSSL_ENCRYPT ) );
219
220 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
221 TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) );
222
Paul Bakker8123e9d2011-01-06 15:37:30 +0000223 /* encode length number of bytes from inbuf */
224 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
225 totaloutlen = outlen;
226 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
227 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200228 TEST_ASSERT( totaloutlen == length ||
229 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
230 totaloutlen < length &&
231 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
232
Paul Bakker8123e9d2011-01-06 15:37:30 +0000233 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
234 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200235 TEST_ASSERT( totaloutlen == length ||
236 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
237 totaloutlen > length &&
238 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000239
240 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200241 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
242 totaloutlen = outlen;
243
244 TEST_ASSERT( totaloutlen == length ||
245 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
246 totaloutlen < length &&
247 totaloutlen + cipher_get_block_size( &ctx_dec ) > length ) );
248
Paul Bakker8123e9d2011-01-06 15:37:30 +0000249 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200250 totaloutlen += outlen;
251
252 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000253
254 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
255
256 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
257 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200258}
Paul Bakker8123e9d2011-01-06 15:37:30 +0000259END_CASE
260
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200261BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200262set_padding:#cipher_id:#pad_mode:#ret
263{
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200264 const cipher_info_t *cipher_info;
265 cipher_context_t ctx;
266
267 cipher_info = cipher_info_from_type( {cipher_id} );
268 TEST_ASSERT( NULL != cipher_info );
269 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
270
271 TEST_ASSERT( {ret} == cipher_set_padding_mode( &ctx, {pad_mode} ) );
272
273 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200274}
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200275END_CASE
Paul Bakker8123e9d2011-01-06 15:37:30 +0000276
277BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200278check_padding:#pad_mode:input_str:#ret:#dlen_check
279{
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200280 cipher_info_t cipher_info;
281 cipher_context_t ctx;
282 unsigned char input[16];
283 size_t ilen, dlen;
284
285 /* build a fake context just for getting access to get_padding */
286 memset( &ctx, 0, sizeof( ctx ) );
287 cipher_info.mode = POLARSSL_MODE_CBC;
288 ctx.cipher_info = &cipher_info;
289
290 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, {pad_mode} ) );
291
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200292 ilen = unhexify( input, {input_str} );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200293
294 TEST_ASSERT( {ret} == ctx.get_padding( input, ilen, &dlen ) );
295 if( 0 == {ret} )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200296 TEST_ASSERT( dlen == (size_t) {dlen_check} );
297}
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200298END_CASE
299
300BEGIN_CASE
Paul Bakker8123e9d2011-01-06 15:37:30 +0000301cipher_selftest:
302{
303 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
304}
305END_CASE