blob: 1f7943a448c0612520198c988968ae4abf96fb77 [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];
17
18 const cipher_info_t *cipher_info;
19 cipher_context_t ctx_dec;
20 cipher_context_t ctx_enc;
21
22 unsigned char inbuf[64];
23 unsigned char encbuf[64];
24 unsigned char decbuf[64];
25
Paul Bakker23986e52011-04-24 08:57:21 +000026 size_t outlen = 0;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020027 size_t total_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +000028
29 memset( key, 0, 32 );
30 memset( iv , 0, 16 );
31
32 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
33 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
34
35 memset( inbuf, 5, 64 );
36 memset( encbuf, 0, 64 );
37 memset( decbuf, 0, 64 );
38
39 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +020040 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +000041 TEST_ASSERT( NULL != cipher_info );
Paul Bakker33b43f12013-08-20 11:48:36 +020042 TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +000043
44 /* Initialise enc and dec contexts */
45 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
46 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
47
Paul Bakker33b43f12013-08-20 11:48:36 +020048 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
49 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000050
Paul Bakker33b43f12013-08-20 11:48:36 +020051 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020052 {
Paul Bakker33b43f12013-08-20 11:48:36 +020053 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) );
54 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +020055 }
56
Paul Bakker8123e9d2011-01-06 15:37:30 +000057 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
58 TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) );
59
Paul Bakker8123e9d2011-01-06 15:37:30 +000060 /* encode length number of bytes from inbuf */
61 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020062 total_len = outlen;
63
64 TEST_ASSERT( total_len == length ||
65 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
66 total_len < length &&
67 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000068
Paul Bakker8123e9d2011-01-06 15:37:30 +000069 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020070 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000071
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020072 TEST_ASSERT( total_len == length ||
73 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
74 total_len > length &&
75 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000076
77 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020078 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
79 total_len = outlen;
80
81 TEST_ASSERT( total_len == length ||
82 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
83 total_len < length &&
84 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000085
Paul Bakker8123e9d2011-01-06 15:37:30 +000086 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020087 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000088
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020089 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +000090
91 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
92
93 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
94 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +020095}
Paul Bakker33b43f12013-08-20 11:48:36 +020096/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +000097
Paul Bakker33b43f12013-08-20 11:48:36 +020098/* BEGIN_CASE */
99void enc_fail( int cipher_id, int pad_mode, int key_len,
100 int length_val, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200101{
Paul Bakker33b43f12013-08-20 11:48:36 +0200102 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200103 unsigned char key[32];
104 unsigned char iv[16];
105
106 const cipher_info_t *cipher_info;
107 cipher_context_t ctx;
108
109 unsigned char inbuf[64];
110 unsigned char encbuf[64];
111
112 size_t outlen = 0;
113
114 memset( key, 0, 32 );
115 memset( iv , 0, 16 );
116
117 memset( &ctx, 0, sizeof( ctx ) );
118
119 memset( inbuf, 5, 64 );
120 memset( encbuf, 0, 64 );
121
122 /* Check and get info structures */
Paul Bakker33b43f12013-08-20 11:48:36 +0200123 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200124 TEST_ASSERT( NULL != cipher_info );
125
126 /* Initialise context */
127 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200128 TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
129 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200130 TEST_ASSERT( 0 == cipher_reset( &ctx, iv ) );
131
132 /* encode length number of bytes from inbuf */
133 TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200134 TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200135
136 /* done */
137 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200138}
Paul Bakker33b43f12013-08-20 11:48:36 +0200139/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200140
Paul Bakker33b43f12013-08-20 11:48:36 +0200141/* BEGIN_CASE */
142void dec_empty_buf()
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200143{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000144 unsigned char key[32];
145 unsigned char iv[16];
146
147 cipher_context_t ctx_dec;
148 const cipher_info_t *cipher_info;
149
150 unsigned char encbuf[64];
151 unsigned char decbuf[64];
152
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000153 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000154
155 memset( key, 0, 32 );
156 memset( iv , 0, 16 );
157
158 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
159
160 memset( encbuf, 0, 64 );
161 memset( decbuf, 0, 64 );
162
163 /* Initialise enc and dec contexts */
164 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
165 TEST_ASSERT( NULL != cipher_info);
166
167 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
168
169 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
170
171 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
172
173 /* decode 0-byte string */
174 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
175 TEST_ASSERT( 0 == outlen );
Paul Bakkerc65ab342011-06-09 15:44:37 +0000176 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000177 TEST_ASSERT( 0 == outlen );
178
179 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200180}
Paul Bakker33b43f12013-08-20 11:48:36 +0200181/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000182
Paul Bakker33b43f12013-08-20 11:48:36 +0200183/* BEGIN_CASE */
184void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
185 int second_length_val )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200186{
Paul Bakker33b43f12013-08-20 11:48:36 +0200187 size_t first_length = first_length_val;
188 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000189 size_t length = first_length + second_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000190 unsigned char key[32];
191 unsigned char iv[16];
192
193 cipher_context_t ctx_dec;
194 cipher_context_t ctx_enc;
195 const cipher_info_t *cipher_info;
196
197 unsigned char inbuf[64];
198 unsigned char encbuf[64];
199 unsigned char decbuf[64];
200
Paul Bakker23986e52011-04-24 08:57:21 +0000201 size_t outlen = 0;
202 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203
204 memset( key, 0, 32 );
205 memset( iv , 0, 16 );
206
207 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
208 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
209
210 memset( inbuf, 5, 64 );
211 memset( encbuf, 0, 64 );
212 memset( decbuf, 0, 64 );
213
214 /* Initialise enc and dec contexts */
Paul Bakker33b43f12013-08-20 11:48:36 +0200215 cipher_info = cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000216 TEST_ASSERT( NULL != cipher_info);
217
218 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
219 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
220
Paul Bakker33b43f12013-08-20 11:48:36 +0200221 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
222 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000223
224 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
225 TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) );
226
Paul Bakker8123e9d2011-01-06 15:37:30 +0000227 /* encode length number of bytes from inbuf */
228 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
229 totaloutlen = outlen;
230 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
231 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200232 TEST_ASSERT( totaloutlen == length ||
233 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
234 totaloutlen < length &&
235 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
236
Paul Bakker8123e9d2011-01-06 15:37:30 +0000237 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, 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 <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000243
244 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200245 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
246 totaloutlen = outlen;
247
248 TEST_ASSERT( totaloutlen == length ||
249 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
250 totaloutlen < length &&
251 totaloutlen + cipher_get_block_size( &ctx_dec ) > length ) );
252
Paul Bakker8123e9d2011-01-06 15:37:30 +0000253 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200254 totaloutlen += outlen;
255
256 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000257
258 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
259
260 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
261 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200262}
Paul Bakker33b43f12013-08-20 11:48:36 +0200263/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000264
Paul Bakker33b43f12013-08-20 11:48:36 +0200265/* BEGIN_CASE */
266void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200267{
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200268 const cipher_info_t *cipher_info;
269 cipher_context_t ctx;
270
Paul Bakker33b43f12013-08-20 11:48:36 +0200271 cipher_info = cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200272 TEST_ASSERT( NULL != cipher_info );
273 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
274
Paul Bakker33b43f12013-08-20 11:48:36 +0200275 TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200276
277 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200278}
Paul Bakker33b43f12013-08-20 11:48:36 +0200279/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000280
Paul Bakker33b43f12013-08-20 11:48:36 +0200281/* BEGIN_CASE */
282void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200283{
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200284 cipher_info_t cipher_info;
285 cipher_context_t ctx;
286 unsigned char input[16];
287 size_t ilen, dlen;
288
289 /* build a fake context just for getting access to get_padding */
290 memset( &ctx, 0, sizeof( ctx ) );
291 cipher_info.mode = POLARSSL_MODE_CBC;
292 ctx.cipher_info = &cipher_info;
293
Paul Bakker33b43f12013-08-20 11:48:36 +0200294 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200295
Paul Bakker33b43f12013-08-20 11:48:36 +0200296 ilen = unhexify( input, input_str );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200297
Paul Bakker33b43f12013-08-20 11:48:36 +0200298 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
299 if( 0 == ret )
300 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200301}
Paul Bakker33b43f12013-08-20 11:48:36 +0200302/* END_CASE */
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200303
Paul Bakker33b43f12013-08-20 11:48:36 +0200304/* BEGIN_CASE */
305void cipher_selftest()
Paul Bakker8123e9d2011-01-06 15:37:30 +0000306{
307 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
308}
Paul Bakker33b43f12013-08-20 11:48:36 +0200309/* END_CASE */