blob: 5c5c1c9071a591039e3b000218b3b7b4cbbe9ca0 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Paul Bakkere896fea2009-07-06 06:40:23 +00002#include <polarssl/des.h>
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakkere896fea2009-07-06 06:40:23 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
6 * depends_on:POLARSSL_DES_C
7 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
11void des_encrypt_ecb( char *hex_key_string, char *hex_src_string,
12 char *hex_dst_string )
Paul Bakkere896fea2009-07-06 06:40:23 +000013{
14 unsigned char key_str[100];
15 unsigned char src_str[100];
16 unsigned char dst_str[100];
17 unsigned char output[100];
18 des_context ctx;
19
20 memset(key_str, 0x00, 100);
21 memset(src_str, 0x00, 100);
22 memset(dst_str, 0x00, 100);
23 memset(output, 0x00, 100);
24
Paul Bakker33b43f12013-08-20 11:48:36 +020025 unhexify( key_str, hex_key_string );
26 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +000027
28 des_setkey_enc( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000029 TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000030 hexify( dst_str, output, 8 );
31
Paul Bakker33b43f12013-08-20 11:48:36 +020032 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000033}
Paul Bakker33b43f12013-08-20 11:48:36 +020034/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000035
Paul Bakker33b43f12013-08-20 11:48:36 +020036/* BEGIN_CASE */
37void des_decrypt_ecb( char *hex_key_string, char *hex_src_string,
38 char *hex_dst_string )
Paul Bakkere896fea2009-07-06 06:40:23 +000039{
40 unsigned char key_str[100];
41 unsigned char src_str[100];
42 unsigned char dst_str[100];
43 unsigned char output[100];
44 des_context ctx;
45
46 memset(key_str, 0x00, 100);
47 memset(src_str, 0x00, 100);
48 memset(dst_str, 0x00, 100);
49 memset(output, 0x00, 100);
50
Paul Bakker33b43f12013-08-20 11:48:36 +020051 unhexify( key_str, hex_key_string );
52 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +000053
54 des_setkey_dec( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000055 TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000056 hexify( dst_str, output, 8 );
57
Paul Bakker33b43f12013-08-20 11:48:36 +020058 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000059}
Paul Bakker33b43f12013-08-20 11:48:36 +020060/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000061
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +020062/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +020063void des_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
64 char *hex_src_string, char *hex_dst_string, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +000065{
66 unsigned char key_str[100];
67 unsigned char iv_str[100];
68 unsigned char src_str[100];
69 unsigned char dst_str[100];
70 unsigned char output[100];
71 des_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000072 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +000073
74 memset(key_str, 0x00, 100);
75 memset(iv_str, 0x00, 100);
76 memset(src_str, 0x00, 100);
77 memset(dst_str, 0x00, 100);
78 memset(output, 0x00, 100);
79
Paul Bakker33b43f12013-08-20 11:48:36 +020080 unhexify( key_str, hex_key_string );
81 unhexify( iv_str, hex_iv_string );
82 src_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +000083
84 des_setkey_enc( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +020085 TEST_ASSERT( des_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
86 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +000087 {
88 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +000089
Paul Bakker33b43f12013-08-20 11:48:36 +020090 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000091 }
Paul Bakkere896fea2009-07-06 06:40:23 +000092}
Paul Bakker33b43f12013-08-20 11:48:36 +020093/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000094
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +020095/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +020096void des_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
97 char *hex_src_string, char *hex_dst_string, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +000098{
99 unsigned char key_str[100];
100 unsigned char iv_str[100];
101 unsigned char src_str[100];
102 unsigned char dst_str[100];
103 unsigned char output[100];
104 des_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000105 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000106
107 memset(key_str, 0x00, 100);
108 memset(iv_str, 0x00, 100);
109 memset(src_str, 0x00, 100);
110 memset(dst_str, 0x00, 100);
111 memset(output, 0x00, 100);
112
Paul Bakker33b43f12013-08-20 11:48:36 +0200113 unhexify( key_str, hex_key_string );
114 unhexify( iv_str, hex_iv_string );
115 src_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000116
117 des_setkey_dec( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200118 TEST_ASSERT( des_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
119 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000120 {
121 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000122
Paul Bakker33b43f12013-08-20 11:48:36 +0200123 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000124 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000125}
Paul Bakker33b43f12013-08-20 11:48:36 +0200126/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000127
Paul Bakker33b43f12013-08-20 11:48:36 +0200128/* BEGIN_CASE */
129void des3_encrypt_ecb( int key_count, char *hex_key_string,
130 char *hex_src_string, char *hex_dst_string )
Paul Bakkere896fea2009-07-06 06:40:23 +0000131{
132 unsigned char key_str[100];
133 unsigned char src_str[100];
134 unsigned char dst_str[100];
135 unsigned char output[100];
136 des3_context ctx;
137
138 memset(key_str, 0x00, 100);
139 memset(src_str, 0x00, 100);
140 memset(dst_str, 0x00, 100);
141 memset(output, 0x00, 100);
142
Paul Bakker33b43f12013-08-20 11:48:36 +0200143 unhexify( key_str, hex_key_string );
144 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000145
Paul Bakker33b43f12013-08-20 11:48:36 +0200146 if( key_count == 2 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000147 des3_set2key_enc( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200148 else if( key_count == 3 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000149 des3_set3key_enc( &ctx, key_str );
150 else
151 TEST_ASSERT( 0 );
152
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000153 TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000154 hexify( dst_str, output, 8 );
155
Paul Bakker33b43f12013-08-20 11:48:36 +0200156 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000157}
Paul Bakker33b43f12013-08-20 11:48:36 +0200158/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000159
Paul Bakker33b43f12013-08-20 11:48:36 +0200160/* BEGIN_CASE */
161void des3_decrypt_ecb( int key_count, char *hex_key_string,
162 char *hex_src_string, char *hex_dst_string )
Paul Bakkere896fea2009-07-06 06:40:23 +0000163{
164 unsigned char key_str[100];
165 unsigned char src_str[100];
166 unsigned char dst_str[100];
167 unsigned char output[100];
168 des3_context ctx;
169
170 memset(key_str, 0x00, 100);
171 memset(src_str, 0x00, 100);
172 memset(dst_str, 0x00, 100);
173 memset(output, 0x00, 100);
174
Paul Bakker33b43f12013-08-20 11:48:36 +0200175 unhexify( key_str, hex_key_string );
176 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000177
Paul Bakker33b43f12013-08-20 11:48:36 +0200178 if( key_count == 2 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000179 des3_set2key_dec( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200180 else if( key_count == 3 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000181 des3_set3key_dec( &ctx, key_str );
182 else
183 TEST_ASSERT( 0 );
184
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000185 TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000186 hexify( dst_str, output, 8 );
187
Paul Bakker33b43f12013-08-20 11:48:36 +0200188 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000189}
Paul Bakker33b43f12013-08-20 11:48:36 +0200190/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000191
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200192/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200193void des3_encrypt_cbc( int key_count, char *hex_key_string,
194 char *hex_iv_string, char *hex_src_string,
195 char *hex_dst_string, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000196{
197 unsigned char key_str[100];
198 unsigned char iv_str[100];
199 unsigned char src_str[100];
200 unsigned char dst_str[100];
201 unsigned char output[100];
202 des3_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000203 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000204
205 memset(key_str, 0x00, 100);
206 memset(iv_str, 0x00, 100);
207 memset(src_str, 0x00, 100);
208 memset(dst_str, 0x00, 100);
209 memset(output, 0x00, 100);
210
Paul Bakker33b43f12013-08-20 11:48:36 +0200211 unhexify( key_str, hex_key_string );
212 unhexify( iv_str, hex_iv_string );
213 src_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000214
Paul Bakker33b43f12013-08-20 11:48:36 +0200215 if( key_count == 2 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000216 des3_set2key_enc( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200217 else if( key_count == 3 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000218 des3_set3key_enc( &ctx, key_str );
219 else
220 TEST_ASSERT( 0 );
221
Paul Bakker33b43f12013-08-20 11:48:36 +0200222 TEST_ASSERT( des3_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
Paul Bakker02722ea2011-05-25 11:34:44 +0000223
Paul Bakker33b43f12013-08-20 11:48:36 +0200224 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000225 {
226 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000227
Paul Bakker33b43f12013-08-20 11:48:36 +0200228 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000229 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000230}
Paul Bakker33b43f12013-08-20 11:48:36 +0200231/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000232
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200233/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200234void des3_decrypt_cbc( int key_count, char *hex_key_string,
235 char *hex_iv_string, char *hex_src_string,
236 char *hex_dst_string, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000237{
238 unsigned char key_str[100];
239 unsigned char iv_str[100];
240 unsigned char src_str[100];
241 unsigned char dst_str[100];
242 unsigned char output[100];
243 des3_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000244 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000245
246 memset(key_str, 0x00, 100);
247 memset(iv_str, 0x00, 100);
248 memset(src_str, 0x00, 100);
249 memset(dst_str, 0x00, 100);
250 memset(output, 0x00, 100);
251
Paul Bakker33b43f12013-08-20 11:48:36 +0200252 unhexify( key_str, hex_key_string );
253 unhexify( iv_str, hex_iv_string );
254 src_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000255
Paul Bakker33b43f12013-08-20 11:48:36 +0200256 if( key_count == 2 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000257 des3_set2key_dec( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200258 else if( key_count == 3 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000259 des3_set3key_dec( &ctx, key_str );
260 else
261 TEST_ASSERT( 0 );
262
Paul Bakker33b43f12013-08-20 11:48:36 +0200263 TEST_ASSERT( des3_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
Paul Bakker02722ea2011-05-25 11:34:44 +0000264
Paul Bakker33b43f12013-08-20 11:48:36 +0200265 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000266 {
267 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000268
Paul Bakker33b43f12013-08-20 11:48:36 +0200269 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000270 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000271}
Paul Bakker33b43f12013-08-20 11:48:36 +0200272/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000273
Paul Bakker33b43f12013-08-20 11:48:36 +0200274/* BEGIN_CASE */
275void des_key_parity_run()
Paul Bakker1f87fb62011-01-15 17:32:24 +0000276{
277 int i, j, cnt;
278 unsigned char key[DES_KEY_SIZE];
279 unsigned int parity;
280
281 memset( key, 0, DES_KEY_SIZE );
282 cnt = 0;
283
284 // Iterate through all possible byte values
285 //
286 for( i = 0; i < 32; i++ )
287 {
288 for( j = 0; j < 8; j++ )
289 key[j] = cnt++;
290
291 // Set the key parity according to the table
292 //
293 des_key_set_parity( key );
294
295 // Check the parity with a function
296 //
297 for( j = 0; j < 8; j++ )
298 {
299 parity = key[j] ^ ( key[j] >> 4 );
300 parity = parity ^
301 ( parity >> 1 ) ^
302 ( parity >> 2 ) ^
303 ( parity >> 3 );
304 parity &= 1;
305
306 if( parity != 1 )
307 TEST_ASSERT( 0 );
308 }
309
310 // Check the parity with the table
311 //
312 TEST_ASSERT( des_key_check_key_parity( key ) == 0 );
313 }
314}
Paul Bakker33b43f12013-08-20 11:48:36 +0200315/* END_CASE */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000316
Manuel Pégourié-Gonnard4fee79b2013-09-19 18:09:14 +0200317/* BEGIN_CASE depends_on:POLARSSL_SELFTEST_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200318void des_selftest()
Paul Bakkere896fea2009-07-06 06:40:23 +0000319{
320 TEST_ASSERT( des_self_test( 0 ) == 0 );
321}
Paul Bakker33b43f12013-08-20 11:48:36 +0200322/* END_CASE */