blob: 4c436f10271bb98c5ea94c3935a3580c8ede1b2a [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
10enc_dec_buf:cipher_id:cipher_string:key_len:length:
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
48 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
49 TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) );
50
Paul Bakker8123e9d2011-01-06 15:37:30 +000051 /* encode length number of bytes from inbuf */
52 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020053 total_len = outlen;
54
55 TEST_ASSERT( total_len == length ||
56 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
57 total_len < length &&
58 total_len + cipher_get_block_size( &ctx_enc ) > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000059
Paul Bakker8123e9d2011-01-06 15:37:30 +000060 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020061 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +000062
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020063 TEST_ASSERT( total_len == length ||
64 ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
65 total_len > length &&
66 total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +000067
68 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +020069 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
70 total_len = outlen;
71
72 TEST_ASSERT( total_len == length ||
73 ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
74 total_len < length &&
75 total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +000076
Paul Bakker8123e9d2011-01-06 15:37:30 +000077 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
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 );
Paul Bakker8123e9d2011-01-06 15:37:30 +000081
82 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
83
84 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
85 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
86END_CASE
87
88BEGIN_CASE
89dec_empty_buf:
90 unsigned char key[32];
91 unsigned char iv[16];
92
93 cipher_context_t ctx_dec;
94 const cipher_info_t *cipher_info;
95
96 unsigned char encbuf[64];
97 unsigned char decbuf[64];
98
Paul Bakkerf4a3f302011-04-24 15:53:29 +000099 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000100
101 memset( key, 0, 32 );
102 memset( iv , 0, 16 );
103
104 memset( &ctx_dec, 0, sizeof( ctx_dec ) );
105
106 memset( encbuf, 0, 64 );
107 memset( decbuf, 0, 64 );
108
109 /* Initialise enc and dec contexts */
110 cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
111 TEST_ASSERT( NULL != cipher_info);
112
113 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
114
115 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
116
117 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
118
119 /* decode 0-byte string */
120 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
121 TEST_ASSERT( 0 == outlen );
Paul Bakkerc65ab342011-06-09 15:44:37 +0000122 TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000123 TEST_ASSERT( 0 == outlen );
124
125 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
126END_CASE
127
128BEGIN_CASE
129enc_dec_buf_multipart:cipher_id:key_len:first_length:second_length:
Paul Bakker23986e52011-04-24 08:57:21 +0000130 size_t first_length = {first_length};
131 size_t second_length = {second_length};
132 size_t length = first_length + second_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000133 unsigned char key[32];
134 unsigned char iv[16];
135
136 cipher_context_t ctx_dec;
137 cipher_context_t ctx_enc;
138 const cipher_info_t *cipher_info;
139
140 unsigned char inbuf[64];
141 unsigned char encbuf[64];
142 unsigned char decbuf[64];
143
Paul Bakker23986e52011-04-24 08:57:21 +0000144 size_t outlen = 0;
145 size_t totaloutlen = 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 memset( &ctx_enc, 0, sizeof( ctx_enc ) );
152
153 memset( inbuf, 5, 64 );
154 memset( encbuf, 0, 64 );
155 memset( decbuf, 0, 64 );
156
157 /* Initialise enc and dec contexts */
158 cipher_info = cipher_info_from_type( {cipher_id} );
159 TEST_ASSERT( NULL != cipher_info);
160
161 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
162 TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
163
164 TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, {key_len}, POLARSSL_DECRYPT ) );
165 TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, {key_len}, POLARSSL_ENCRYPT ) );
166
167 TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
168 TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) );
169
Paul Bakker8123e9d2011-01-06 15:37:30 +0000170 /* encode length number of bytes from inbuf */
171 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
172 totaloutlen = outlen;
173 TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
174 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200175 TEST_ASSERT( totaloutlen == length ||
176 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
177 totaloutlen < length &&
178 totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
179
Paul Bakker8123e9d2011-01-06 15:37:30 +0000180 TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
181 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200182 TEST_ASSERT( totaloutlen == length ||
183 ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
184 totaloutlen > length &&
185 totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000186
187 /* decode the previously encoded string */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200188 TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
189 totaloutlen = outlen;
190
191 TEST_ASSERT( totaloutlen == length ||
192 ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
193 totaloutlen < length &&
194 totaloutlen + cipher_get_block_size( &ctx_dec ) > length ) );
195
Paul Bakker8123e9d2011-01-06 15:37:30 +0000196 TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200197 totaloutlen += outlen;
198
199 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000200
201 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
202
203 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
204 TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
205END_CASE
206
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200207BEGIN_CASE
208set_padding:cipher_id:pad_mode:ret:
209 const cipher_info_t *cipher_info;
210 cipher_context_t ctx;
211
212 cipher_info = cipher_info_from_type( {cipher_id} );
213 TEST_ASSERT( NULL != cipher_info );
214 TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
215
216 TEST_ASSERT( {ret} == cipher_set_padding_mode( &ctx, {pad_mode} ) );
217
218 TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
219END_CASE
Paul Bakker8123e9d2011-01-06 15:37:30 +0000220
221BEGIN_CASE
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200222check_padding:pad_mode:input:ret:dlen:
223 cipher_info_t cipher_info;
224 cipher_context_t ctx;
225 unsigned char input[16];
226 size_t ilen, dlen;
227
228 /* build a fake context just for getting access to get_padding */
229 memset( &ctx, 0, sizeof( ctx ) );
230 cipher_info.mode = POLARSSL_MODE_CBC;
231 ctx.cipher_info = &cipher_info;
232
233 TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, {pad_mode} ) );
234
235 ilen = unhexify( input, {input} );
236
237 TEST_ASSERT( {ret} == ctx.get_padding( input, ilen, &dlen ) );
238 if( 0 == {ret} )
239 TEST_ASSERT( dlen == {dlen} );
240END_CASE
241
242BEGIN_CASE
Paul Bakker8123e9d2011-01-06 15:37:30 +0000243cipher_selftest:
244{
245 TEST_ASSERT( cipher_self_test( 0 ) == 0 );
246}
247END_CASE