blob: 4f372001896eb4c62584ad98bf6dcc3fdd51e3db [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
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020010enc_dec_buf:cipher_id:cipher_string:key_len:length:pad_mode:
Paul Bakker23986e52011-04-24 08:57:21 +000011 size_t length = {length};
Paul Bakker8123e9d2011-01-06 15:37:30 +000012 unsigned char key[32];
13 unsigned char iv[16];
14
15 const cipher_info_t *cipher_info;
16 cipher_context_t ctx_dec;
17 cipher_context_t ctx_enc;
18
19 unsigned char inbuf[64];
20 unsigned char encbuf[64];
21 unsigned char decbuf[64];
22
Paul Bakker23986e52011-04-24 08:57:21 +000023 size_t outlen = 0;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020024 size_t total_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +000025
26 memset( key, 0, 32 );
27 memset( iv , 0, 16 );
28
29 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
30 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
31
32 memset( inbuf, 5, 64 );
33 memset( encbuf, 0, 64 );
34 memset( decbuf, 0, 64 );
35
36 /* Check and get info structures */
37 cipher_info = cipher_info_from_type( {cipher_id} );
38 TEST_ASSERT( NULL != cipher_info );
39 TEST_ASSERT( cipher_info_from_string( "{cipher_string}" ) == cipher_info );
40
41 /* Initialise enc and dec contexts */
42 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
43 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
44
45 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, {key_len}, POLARSSL_DECRYPT ) );
46 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, {key_len}, POLARSSL_ENCRYPT ) );
47
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020048 if( -1 != {pad_mode} )
49 {
50 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, {pad_mode} ) );
51 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, {pad_mode} ) );
52 }
53
Paul Bakker8123e9d2011-01-06 15:37:30 +000054 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
55 TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) );
56
Paul Bakker8123e9d2011-01-06 15:37:30 +000057 /* encode length number of bytes from inbuf */
58 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020059 total_len = outlen;
60
61 TEST_ASSERT( total_len == length ||
62 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
63 total_len < length &&
64 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000065
Paul Bakker8123e9d2011-01-06 15:37:30 +000066 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020067 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000068
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020069 TEST_ASSERT( total_len == length ||
70 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
71 total_len > length &&
72 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000073
74 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020075 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
76 total_len = outlen;
77
78 TEST_ASSERT( total_len == length ||
79 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
80 total_len < length &&
81 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000082
Paul Bakker8123e9d2011-01-06 15:37:30 +000083 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020084 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000085
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020086 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +000087
88 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
89
90 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
91 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
92END_CASE
93
94BEGIN_CASE
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +020095enc_fail:cipher_id:pad_mode:key_len:length:ret:
96 size_t length = {length};
97 unsigned char key[32];
98 unsigned char iv[16];
99
100 const cipher_info_t *cipher_info;
101 cipher_context_t ctx;
102
103 unsigned char inbuf[64];
104 unsigned char encbuf[64];
105
106 size_t outlen = 0;
107
108 memset( key, 0, 32 );
109 memset( iv , 0, 16 );
110
111 memset( &ctx, 0, sizeof( ctx ) );
112
113 memset( inbuf, 5, 64 );
114 memset( encbuf, 0, 64 );
115
116 /* Check and get info structures */
117 cipher_info = cipher_info_from_type( {cipher_id} );
118 TEST_ASSERT( NULL != cipher_info );
119
120 /* Initialise context */
121 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
122 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, {key_len}, POLARSSL_ENCRYPT ) );
123 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, {pad_mode} ) );
124 TEST_ASSERT( 0 == cipher_reset( &ctx, iv ) );
125
126 /* encode length number of bytes from inbuf */
127 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
128 TEST_ASSERT( {ret} == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
129
130 /* done */
131 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
132END_CASE
133
134BEGIN_CASE
Paul Bakker8123e9d2011-01-06 15:37:30 +0000135dec_empty_buf:
136 unsigned char key[32];
137 unsigned char iv[16];
138
139 cipher_context_t ctx_dec;
140 const cipher_info_t *cipher_info;
141
142 unsigned char encbuf[64];
143 unsigned char decbuf[64];
144
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000145 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000146
147 memset( key, 0, 32 );
148 memset( iv , 0, 16 );
149
150 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
151
152 memset( encbuf, 0, 64 );
153 memset( decbuf, 0, 64 );
154
155 /* Initialise enc and dec contexts */
156 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
157 TEST_ASSERT( NULL != cipher_info);
158
159 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
160
161 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
162
163 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
164
165 /* decode 0-byte string */
166 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
167 TEST_ASSERT( 0 == outlen );
Paul Bakkerc65ab342011-06-09 15:44:37 +0000168 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000169 TEST_ASSERT( 0 == outlen );
170
171 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
172END_CASE
173
174BEGIN_CASE
175enc_dec_buf_multipart:cipher_id:key_len:first_length:second_length:
Paul Bakker23986e52011-04-24 08:57:21 +0000176 size_t first_length = {first_length};
177 size_t second_length = {second_length};
178 size_t length = first_length + second_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000179 unsigned char key[32];
180 unsigned char iv[16];
181
182 cipher_context_t ctx_dec;
183 cipher_context_t ctx_enc;
184 const cipher_info_t *cipher_info;
185
186 unsigned char inbuf[64];
187 unsigned char encbuf[64];
188 unsigned char decbuf[64];
189
Paul Bakker23986e52011-04-24 08:57:21 +0000190 size_t outlen = 0;
191 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000192
193 memset( key, 0, 32 );
194 memset( iv , 0, 16 );
195
196 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
197 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
198
199 memset( inbuf, 5, 64 );
200 memset( encbuf, 0, 64 );
201 memset( decbuf, 0, 64 );
202
203 /* Initialise enc and dec contexts */
204 cipher_info = cipher_info_from_type( {cipher_id} );
205 TEST_ASSERT( NULL != cipher_info);
206
207 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
208 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
209
210 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, {key_len}, POLARSSL_DECRYPT ) );
211 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, {key_len}, POLARSSL_ENCRYPT ) );
212
213 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
214 TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) );
215
Paul Bakker8123e9d2011-01-06 15:37:30 +0000216 /* encode length number of bytes from inbuf */
217 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
218 totaloutlen = outlen;
219 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
220 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200221 TEST_ASSERT( totaloutlen == length ||
222 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
223 totaloutlen < length &&
224 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
225
Paul Bakker8123e9d2011-01-06 15:37:30 +0000226 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, 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 <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000232
233 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200234 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
235 totaloutlen = outlen;
236
237 TEST_ASSERT( totaloutlen == length ||
238 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
239 totaloutlen < length &&
240 totaloutlen + cipher_get_block_size( &ctx_dec ) > length ) );
241
Paul Bakker8123e9d2011-01-06 15:37:30 +0000242 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200243 totaloutlen += outlen;
244
245 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000246
247 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
248
249 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
250 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
251END_CASE
252
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200253BEGIN_CASE
254set_padding:cipher_id:pad_mode:ret:
255 const cipher_info_t *cipher_info;
256 cipher_context_t ctx;
257
258 cipher_info = cipher_info_from_type( {cipher_id} );
259 TEST_ASSERT( NULL != cipher_info );
260 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
261
262 TEST_ASSERT( {ret} == cipher_set_padding_mode( &ctx, {pad_mode} ) );
263
264 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
265END_CASE
Paul Bakker8123e9d2011-01-06 15:37:30 +0000266
267BEGIN_CASE
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200268check_padding:pad_mode:input:ret:dlen:
269 cipher_info_t cipher_info;
270 cipher_context_t ctx;
271 unsigned char input[16];
272 size_t ilen, dlen;
273
274 /* build a fake context just for getting access to get_padding */
275 memset( &ctx, 0, sizeof( ctx ) );
276 cipher_info.mode = POLARSSL_MODE_CBC;
277 ctx.cipher_info = &cipher_info;
278
279 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, {pad_mode} ) );
280
281 ilen = unhexify( input, {input} );
282
283 TEST_ASSERT( {ret} == ctx.get_padding( input, ilen, &dlen ) );
284 if( 0 == {ret} )
285 TEST_ASSERT( dlen == {dlen} );
286END_CASE
287
288BEGIN_CASE
Paul Bakker8123e9d2011-01-06 15:37:30 +0000289cipher_selftest:
290{
291 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
292}
293END_CASE