blob: be238d48d6f36ca4a3d1e48df2b66af562630517 [file] [log] [blame]
Paul Bakkere896fea2009-07-06 06:40:23 +00001BEGIN_HEADER
2#include <polarssl/des.h>
3END_HEADER
4
Paul Bakker5690efc2011-05-26 13:16:06 +00005BEGIN_DEPENDENCIES
6depends_on:POLARSSL_DES_C
7END_DEPENDENCIES
8
Paul Bakkere896fea2009-07-06 06:40:23 +00009BEGIN_CASE
10des_encrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
11{
12 unsigned char key_str[100];
13 unsigned char src_str[100];
14 unsigned char dst_str[100];
15 unsigned char output[100];
16 des_context ctx;
17
18 memset(key_str, 0x00, 100);
19 memset(src_str, 0x00, 100);
20 memset(dst_str, 0x00, 100);
21 memset(output, 0x00, 100);
22
23 unhexify( key_str, {hex_key_string} );
24 unhexify( src_str, {hex_src_string} );
25
26 des_setkey_enc( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000027 TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000028 hexify( dst_str, output, 8 );
29
30 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
31}
32END_CASE
33
34BEGIN_CASE
35des_decrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
36{
37 unsigned char key_str[100];
38 unsigned char src_str[100];
39 unsigned char dst_str[100];
40 unsigned char output[100];
41 des_context ctx;
42
43 memset(key_str, 0x00, 100);
44 memset(src_str, 0x00, 100);
45 memset(dst_str, 0x00, 100);
46 memset(output, 0x00, 100);
47
48 unhexify( key_str, {hex_key_string} );
49 unhexify( src_str, {hex_src_string} );
50
51 des_setkey_dec( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000052 TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000053 hexify( dst_str, output, 8 );
54
55 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
56}
57END_CASE
58
59BEGIN_CASE
Paul Bakkerf3ccc682010-03-18 21:21:02 +000060des_encrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result
Paul Bakkere896fea2009-07-06 06:40:23 +000061{
62 unsigned char key_str[100];
63 unsigned char iv_str[100];
64 unsigned char src_str[100];
65 unsigned char dst_str[100];
66 unsigned char output[100];
67 des_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000068 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +000069
70 memset(key_str, 0x00, 100);
71 memset(iv_str, 0x00, 100);
72 memset(src_str, 0x00, 100);
73 memset(dst_str, 0x00, 100);
74 memset(output, 0x00, 100);
75
76 unhexify( key_str, {hex_key_string} );
77 unhexify( iv_str, {hex_iv_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +000078 src_len = unhexify( src_str, {hex_src_string} );
Paul Bakkere896fea2009-07-06 06:40:23 +000079
80 des_setkey_enc( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000081 TEST_ASSERT( des_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == {cbc_result} );
82 if( {cbc_result} == 0 )
83 {
84 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +000085
Paul Bakkerf3ccc682010-03-18 21:21:02 +000086 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
87 }
Paul Bakkere896fea2009-07-06 06:40:23 +000088}
89END_CASE
90
91BEGIN_CASE
Paul Bakkerf3ccc682010-03-18 21:21:02 +000092des_decrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result
Paul Bakkere896fea2009-07-06 06:40:23 +000093{
94 unsigned char key_str[100];
95 unsigned char iv_str[100];
96 unsigned char src_str[100];
97 unsigned char dst_str[100];
98 unsigned char output[100];
99 des_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000100 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000101
102 memset(key_str, 0x00, 100);
103 memset(iv_str, 0x00, 100);
104 memset(src_str, 0x00, 100);
105 memset(dst_str, 0x00, 100);
106 memset(output, 0x00, 100);
107
108 unhexify( key_str, {hex_key_string} );
109 unhexify( iv_str, {hex_iv_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000110 src_len = unhexify( src_str, {hex_src_string} );
Paul Bakkere896fea2009-07-06 06:40:23 +0000111
112 des_setkey_dec( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000113 TEST_ASSERT( des_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == {cbc_result} );
114 if( {cbc_result} == 0 )
115 {
116 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000117
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000118 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
119 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000120}
121END_CASE
122
123BEGIN_CASE
124des3_encrypt_ecb:key_count:hex_key_string:hex_src_string:hex_dst_string
125{
126 unsigned char key_str[100];
127 unsigned char src_str[100];
128 unsigned char dst_str[100];
129 unsigned char output[100];
130 des3_context ctx;
131
132 memset(key_str, 0x00, 100);
133 memset(src_str, 0x00, 100);
134 memset(dst_str, 0x00, 100);
135 memset(output, 0x00, 100);
136
137 unhexify( key_str, {hex_key_string} );
138 unhexify( src_str, {hex_src_string} );
139
140 if( {key_count} == 2 )
141 des3_set2key_enc( &ctx, key_str );
142 else if( {key_count} == 3 )
143 des3_set3key_enc( &ctx, key_str );
144 else
145 TEST_ASSERT( 0 );
146
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000147 TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000148 hexify( dst_str, output, 8 );
149
150 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
151}
152END_CASE
153
154BEGIN_CASE
155des3_decrypt_ecb:key_count:hex_key_string:hex_src_string:hex_dst_string
156{
157 unsigned char key_str[100];
158 unsigned char src_str[100];
159 unsigned char dst_str[100];
160 unsigned char output[100];
161 des3_context ctx;
162
163 memset(key_str, 0x00, 100);
164 memset(src_str, 0x00, 100);
165 memset(dst_str, 0x00, 100);
166 memset(output, 0x00, 100);
167
168 unhexify( key_str, {hex_key_string} );
169 unhexify( src_str, {hex_src_string} );
170
171 if( {key_count} == 2 )
172 des3_set2key_dec( &ctx, key_str );
173 else if( {key_count} == 3 )
174 des3_set3key_dec( &ctx, key_str );
175 else
176 TEST_ASSERT( 0 );
177
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000178 TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000179 hexify( dst_str, output, 8 );
180
181 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
182}
183END_CASE
184
185BEGIN_CASE
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000186des3_encrypt_cbc:key_count:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result
Paul Bakkere896fea2009-07-06 06:40:23 +0000187{
188 unsigned char key_str[100];
189 unsigned char iv_str[100];
190 unsigned char src_str[100];
191 unsigned char dst_str[100];
192 unsigned char output[100];
193 des3_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000194 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000195
196 memset(key_str, 0x00, 100);
197 memset(iv_str, 0x00, 100);
198 memset(src_str, 0x00, 100);
199 memset(dst_str, 0x00, 100);
200 memset(output, 0x00, 100);
201
202 unhexify( key_str, {hex_key_string} );
203 unhexify( iv_str, {hex_iv_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000204 src_len = unhexify( src_str, {hex_src_string} );
Paul Bakkere896fea2009-07-06 06:40:23 +0000205
206 if( {key_count} == 2 )
207 des3_set2key_enc( &ctx, key_str );
208 else if( {key_count} == 3 )
209 des3_set3key_enc( &ctx, key_str );
210 else
211 TEST_ASSERT( 0 );
212
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000213 TEST_ASSERT( des3_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == {cbc_result} );
Paul Bakker02722ea2011-05-25 11:34:44 +0000214
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000215 if( {cbc_result} == 0 )
216 {
217 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000218
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000219 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
220 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000221}
222END_CASE
223
224BEGIN_CASE
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000225des3_decrypt_cbc:key_count:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result
Paul Bakkere896fea2009-07-06 06:40:23 +0000226{
227 unsigned char key_str[100];
228 unsigned char iv_str[100];
229 unsigned char src_str[100];
230 unsigned char dst_str[100];
231 unsigned char output[100];
232 des3_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000233 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000234
235 memset(key_str, 0x00, 100);
236 memset(iv_str, 0x00, 100);
237 memset(src_str, 0x00, 100);
238 memset(dst_str, 0x00, 100);
239 memset(output, 0x00, 100);
240
241 unhexify( key_str, {hex_key_string} );
242 unhexify( iv_str, {hex_iv_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000243 src_len = unhexify( src_str, {hex_src_string} );
Paul Bakkere896fea2009-07-06 06:40:23 +0000244
245 if( {key_count} == 2 )
246 des3_set2key_dec( &ctx, key_str );
247 else if( {key_count} == 3 )
248 des3_set3key_dec( &ctx, key_str );
249 else
250 TEST_ASSERT( 0 );
251
Paul Bakker02722ea2011-05-25 11:34:44 +0000252 TEST_ASSERT( des3_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == {cbc_result} );
253
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000254 if( {cbc_result} == 0 )
255 {
256 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000257
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000258 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
259 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000260}
261END_CASE
262
263BEGIN_CASE
Paul Bakker1f87fb62011-01-15 17:32:24 +0000264des_key_parity_run:
265{
266 int i, j, cnt;
267 unsigned char key[DES_KEY_SIZE];
268 unsigned int parity;
269
270 memset( key, 0, DES_KEY_SIZE );
271 cnt = 0;
272
273 // Iterate through all possible byte values
274 //
275 for( i = 0; i < 32; i++ )
276 {
277 for( j = 0; j < 8; j++ )
278 key[j] = cnt++;
279
280 // Set the key parity according to the table
281 //
282 des_key_set_parity( key );
283
284 // Check the parity with a function
285 //
286 for( j = 0; j < 8; j++ )
287 {
288 parity = key[j] ^ ( key[j] >> 4 );
289 parity = parity ^
290 ( parity >> 1 ) ^
291 ( parity >> 2 ) ^
292 ( parity >> 3 );
293 parity &= 1;
294
295 if( parity != 1 )
296 TEST_ASSERT( 0 );
297 }
298
299 // Check the parity with the table
300 //
301 TEST_ASSERT( des_key_check_key_parity( key ) == 0 );
302 }
303}
304END_CASE
305
306BEGIN_CASE
Paul Bakkere896fea2009-07-06 06:40:23 +0000307des_selftest:
308{
309 TEST_ASSERT( des_self_test( 0 ) == 0 );
310}
311END_CASE