blob: d22c953ac4afec36b3efd93b7f07b93a13a72ef8 [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
95dec_empty_buf:
96 unsigned char key[32];
97 unsigned char iv[16];
98
99 cipher_context_t ctx_dec;
100 const cipher_info_t *cipher_info;
101
102 unsigned char encbuf[64];
103 unsigned char decbuf[64];
104
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000105 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000106
107 memset( key, 0, 32 );
108 memset( iv , 0, 16 );
109
110 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
111
112 memset( encbuf, 0, 64 );
113 memset( decbuf, 0, 64 );
114
115 /* Initialise enc and dec contexts */
116 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
117 TEST_ASSERT( NULL != cipher_info);
118
119 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
120
121 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
122
123 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
124
125 /* decode 0-byte string */
126 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
127 TEST_ASSERT( 0 == outlen );
Paul Bakkerc65ab342011-06-09 15:44:37 +0000128 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000129 TEST_ASSERT( 0 == outlen );
130
131 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
132END_CASE
133
134BEGIN_CASE
135enc_dec_buf_multipart:cipher_id:key_len:first_length:second_length:
Paul Bakker23986e52011-04-24 08:57:21 +0000136 size_t first_length = {first_length};
137 size_t second_length = {second_length};
138 size_t length = first_length + second_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000139 unsigned char key[32];
140 unsigned char iv[16];
141
142 cipher_context_t ctx_dec;
143 cipher_context_t ctx_enc;
144 const cipher_info_t *cipher_info;
145
146 unsigned char inbuf[64];
147 unsigned char encbuf[64];
148 unsigned char decbuf[64];
149
Paul Bakker23986e52011-04-24 08:57:21 +0000150 size_t outlen = 0;
151 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000152
153 memset( key, 0, 32 );
154 memset( iv , 0, 16 );
155
156 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
157 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
158
159 memset( inbuf, 5, 64 );
160 memset( encbuf, 0, 64 );
161 memset( decbuf, 0, 64 );
162
163 /* Initialise enc and dec contexts */
164 cipher_info = cipher_info_from_type( {cipher_id} );
165 TEST_ASSERT( NULL != cipher_info);
166
167 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
168 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
169
170 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, {key_len}, POLARSSL_DECRYPT ) );
171 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, {key_len}, POLARSSL_ENCRYPT ) );
172
173 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
174 TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) );
175
Paul Bakker8123e9d2011-01-06 15:37:30 +0000176 /* encode length number of bytes from inbuf */
177 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
178 totaloutlen = outlen;
179 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
180 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200181 TEST_ASSERT( totaloutlen == length ||
182 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
183 totaloutlen < length &&
184 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
185
Paul Bakker8123e9d2011-01-06 15:37:30 +0000186 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
187 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200188 TEST_ASSERT( totaloutlen == length ||
189 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
190 totaloutlen > length &&
191 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000192
193 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200194 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
195 totaloutlen = outlen;
196
197 TEST_ASSERT( totaloutlen == length ||
198 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
199 totaloutlen < length &&
200 totaloutlen + cipher_get_block_size( &ctx_dec ) > length ) );
201
Paul Bakker8123e9d2011-01-06 15:37:30 +0000202 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200203 totaloutlen += outlen;
204
205 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206
207 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
208
209 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
210 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
211END_CASE
212
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200213BEGIN_CASE
214set_padding:cipher_id:pad_mode:ret:
215 const cipher_info_t *cipher_info;
216 cipher_context_t ctx;
217
218 cipher_info = cipher_info_from_type( {cipher_id} );
219 TEST_ASSERT( NULL != cipher_info );
220 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
221
222 TEST_ASSERT( {ret} == cipher_set_padding_mode( &ctx, {pad_mode} ) );
223
224 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
225END_CASE
Paul Bakker8123e9d2011-01-06 15:37:30 +0000226
227BEGIN_CASE
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200228check_padding:pad_mode:input:ret:dlen:
229 cipher_info_t cipher_info;
230 cipher_context_t ctx;
231 unsigned char input[16];
232 size_t ilen, dlen;
233
234 /* build a fake context just for getting access to get_padding */
235 memset( &ctx, 0, sizeof( ctx ) );
236 cipher_info.mode = POLARSSL_MODE_CBC;
237 ctx.cipher_info = &cipher_info;
238
239 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, {pad_mode} ) );
240
241 ilen = unhexify( input, {input} );
242
243 TEST_ASSERT( {ret} == ctx.get_padding( input, ilen, &dlen ) );
244 if( 0 == {ret} )
245 TEST_ASSERT( dlen == {dlen} );
246END_CASE
247
248BEGIN_CASE
Paul Bakker8123e9d2011-01-06 15:37:30 +0000249cipher_selftest:
250{
251 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
252}
253END_CASE