blob: ace064d3117a39e683a19de310335be6789f904c [file] [log] [blame]
Paul Bakkere896fea2009-07-06 06:40:23 +00001BEGIN_HEADER
2#include <polarssl/des.h>
3END_HEADER
4
5BEGIN_CASE
6des_encrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
7{
8 unsigned char key_str[100];
9 unsigned char src_str[100];
10 unsigned char dst_str[100];
11 unsigned char output[100];
12 des_context ctx;
13
14 memset(key_str, 0x00, 100);
15 memset(src_str, 0x00, 100);
16 memset(dst_str, 0x00, 100);
17 memset(output, 0x00, 100);
18
19 unhexify( key_str, {hex_key_string} );
20 unhexify( src_str, {hex_src_string} );
21
22 des_setkey_enc( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000023 TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000024 hexify( dst_str, output, 8 );
25
26 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
27}
28END_CASE
29
30BEGIN_CASE
31des_decrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
32{
33 unsigned char key_str[100];
34 unsigned char src_str[100];
35 unsigned char dst_str[100];
36 unsigned char output[100];
37 des_context ctx;
38
39 memset(key_str, 0x00, 100);
40 memset(src_str, 0x00, 100);
41 memset(dst_str, 0x00, 100);
42 memset(output, 0x00, 100);
43
44 unhexify( key_str, {hex_key_string} );
45 unhexify( src_str, {hex_src_string} );
46
47 des_setkey_dec( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000048 TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000049 hexify( dst_str, output, 8 );
50
51 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
52}
53END_CASE
54
55BEGIN_CASE
Paul Bakkerf3ccc682010-03-18 21:21:02 +000056des_encrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result
Paul Bakkere896fea2009-07-06 06:40:23 +000057{
58 unsigned char key_str[100];
59 unsigned char iv_str[100];
60 unsigned char src_str[100];
61 unsigned char dst_str[100];
62 unsigned char output[100];
63 des_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000064 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +000065
66 memset(key_str, 0x00, 100);
67 memset(iv_str, 0x00, 100);
68 memset(src_str, 0x00, 100);
69 memset(dst_str, 0x00, 100);
70 memset(output, 0x00, 100);
71
72 unhexify( key_str, {hex_key_string} );
73 unhexify( iv_str, {hex_iv_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +000074 src_len = unhexify( src_str, {hex_src_string} );
Paul Bakkere896fea2009-07-06 06:40:23 +000075
76 des_setkey_enc( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000077 TEST_ASSERT( des_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == {cbc_result} );
78 if( {cbc_result} == 0 )
79 {
80 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +000081
Paul Bakkerf3ccc682010-03-18 21:21:02 +000082 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
83 }
Paul Bakkere896fea2009-07-06 06:40:23 +000084}
85END_CASE
86
87BEGIN_CASE
Paul Bakkerf3ccc682010-03-18 21:21:02 +000088des_decrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result
Paul Bakkere896fea2009-07-06 06:40:23 +000089{
90 unsigned char key_str[100];
91 unsigned char iv_str[100];
92 unsigned char src_str[100];
93 unsigned char dst_str[100];
94 unsigned char output[100];
95 des_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000096 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +000097
98 memset(key_str, 0x00, 100);
99 memset(iv_str, 0x00, 100);
100 memset(src_str, 0x00, 100);
101 memset(dst_str, 0x00, 100);
102 memset(output, 0x00, 100);
103
104 unhexify( key_str, {hex_key_string} );
105 unhexify( iv_str, {hex_iv_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000106 src_len = unhexify( src_str, {hex_src_string} );
Paul Bakkere896fea2009-07-06 06:40:23 +0000107
108 des_setkey_dec( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000109 TEST_ASSERT( des_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == {cbc_result} );
110 if( {cbc_result} == 0 )
111 {
112 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000113
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000114 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
115 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000116}
117END_CASE
118
119BEGIN_CASE
120des3_encrypt_ecb:key_count:hex_key_string:hex_src_string:hex_dst_string
121{
122 unsigned char key_str[100];
123 unsigned char src_str[100];
124 unsigned char dst_str[100];
125 unsigned char output[100];
126 des3_context ctx;
127
128 memset(key_str, 0x00, 100);
129 memset(src_str, 0x00, 100);
130 memset(dst_str, 0x00, 100);
131 memset(output, 0x00, 100);
132
133 unhexify( key_str, {hex_key_string} );
134 unhexify( src_str, {hex_src_string} );
135
136 if( {key_count} == 2 )
137 des3_set2key_enc( &ctx, key_str );
138 else if( {key_count} == 3 )
139 des3_set3key_enc( &ctx, key_str );
140 else
141 TEST_ASSERT( 0 );
142
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000143 TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000144 hexify( dst_str, output, 8 );
145
146 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
147}
148END_CASE
149
150BEGIN_CASE
151des3_decrypt_ecb:key_count:hex_key_string:hex_src_string:hex_dst_string
152{
153 unsigned char key_str[100];
154 unsigned char src_str[100];
155 unsigned char dst_str[100];
156 unsigned char output[100];
157 des3_context ctx;
158
159 memset(key_str, 0x00, 100);
160 memset(src_str, 0x00, 100);
161 memset(dst_str, 0x00, 100);
162 memset(output, 0x00, 100);
163
164 unhexify( key_str, {hex_key_string} );
165 unhexify( src_str, {hex_src_string} );
166
167 if( {key_count} == 2 )
168 des3_set2key_dec( &ctx, key_str );
169 else if( {key_count} == 3 )
170 des3_set3key_dec( &ctx, key_str );
171 else
172 TEST_ASSERT( 0 );
173
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000174 TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000175 hexify( dst_str, output, 8 );
176
177 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
178}
179END_CASE
180
181BEGIN_CASE
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000182des3_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 +0000183{
184 unsigned char key_str[100];
185 unsigned char iv_str[100];
186 unsigned char src_str[100];
187 unsigned char dst_str[100];
188 unsigned char output[100];
189 des3_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000190 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000191
192 memset(key_str, 0x00, 100);
193 memset(iv_str, 0x00, 100);
194 memset(src_str, 0x00, 100);
195 memset(dst_str, 0x00, 100);
196 memset(output, 0x00, 100);
197
198 unhexify( key_str, {hex_key_string} );
199 unhexify( iv_str, {hex_iv_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000200 src_len = unhexify( src_str, {hex_src_string} );
Paul Bakkere896fea2009-07-06 06:40:23 +0000201
202 if( {key_count} == 2 )
203 des3_set2key_enc( &ctx, key_str );
204 else if( {key_count} == 3 )
205 des3_set3key_enc( &ctx, key_str );
206 else
207 TEST_ASSERT( 0 );
208
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000209 TEST_ASSERT( des3_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == {cbc_result} );
Paul Bakker02722ea2011-05-25 11:34:44 +0000210
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000211 if( {cbc_result} == 0 )
212 {
213 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000214
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000215 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
216 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000217}
218END_CASE
219
220BEGIN_CASE
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000221des3_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 +0000222{
223 unsigned char key_str[100];
224 unsigned char iv_str[100];
225 unsigned char src_str[100];
226 unsigned char dst_str[100];
227 unsigned char output[100];
228 des3_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000229 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000230
231 memset(key_str, 0x00, 100);
232 memset(iv_str, 0x00, 100);
233 memset(src_str, 0x00, 100);
234 memset(dst_str, 0x00, 100);
235 memset(output, 0x00, 100);
236
237 unhexify( key_str, {hex_key_string} );
238 unhexify( iv_str, {hex_iv_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000239 src_len = unhexify( src_str, {hex_src_string} );
Paul Bakkere896fea2009-07-06 06:40:23 +0000240
241 if( {key_count} == 2 )
242 des3_set2key_dec( &ctx, key_str );
243 else if( {key_count} == 3 )
244 des3_set3key_dec( &ctx, key_str );
245 else
246 TEST_ASSERT( 0 );
247
Paul Bakker02722ea2011-05-25 11:34:44 +0000248 TEST_ASSERT( des3_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == {cbc_result} );
249
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000250 if( {cbc_result} == 0 )
251 {
252 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000253
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000254 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
255 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000256}
257END_CASE
258
259BEGIN_CASE
Paul Bakker1f87fb62011-01-15 17:32:24 +0000260des_key_parity_run:
261{
262 int i, j, cnt;
263 unsigned char key[DES_KEY_SIZE];
264 unsigned int parity;
265
266 memset( key, 0, DES_KEY_SIZE );
267 cnt = 0;
268
269 // Iterate through all possible byte values
270 //
271 for( i = 0; i < 32; i++ )
272 {
273 for( j = 0; j < 8; j++ )
274 key[j] = cnt++;
275
276 // Set the key parity according to the table
277 //
278 des_key_set_parity( key );
279
280 // Check the parity with a function
281 //
282 for( j = 0; j < 8; j++ )
283 {
284 parity = key[j] ^ ( key[j] >> 4 );
285 parity = parity ^
286 ( parity >> 1 ) ^
287 ( parity >> 2 ) ^
288 ( parity >> 3 );
289 parity &= 1;
290
291 if( parity != 1 )
292 TEST_ASSERT( 0 );
293 }
294
295 // Check the parity with the table
296 //
297 TEST_ASSERT( des_key_check_key_parity( key ) == 0 );
298 }
299}
300END_CASE
301
302BEGIN_CASE
Paul Bakkere896fea2009-07-06 06:40:23 +0000303des_selftest:
304{
305 TEST_ASSERT( des_self_test( 0 ) == 0 );
306}
307END_CASE