blob: 0231757c1d0987f4c7e61f1b71bb79cb67569c9b [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 */
Manuel Pégourié-Gonnard9ce7e842014-03-29 17:06:43 +010011void des_check_weak( char *key_hex, int ret )
12{
13 unsigned char key[DES_KEY_SIZE];
14
15 memset( key, 0, sizeof key );
16
17 unhexify( key, key_hex );
18
19 TEST_ASSERT( des_key_check_weak( key ) == ret );
20}
21/* END_CASE */
22
23/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +020024void des_encrypt_ecb( char *hex_key_string, char *hex_src_string,
25 char *hex_dst_string )
Paul Bakkere896fea2009-07-06 06:40:23 +000026{
27 unsigned char key_str[100];
28 unsigned char src_str[100];
29 unsigned char dst_str[100];
30 unsigned char output[100];
31 des_context ctx;
32
33 memset(key_str, 0x00, 100);
34 memset(src_str, 0x00, 100);
35 memset(dst_str, 0x00, 100);
36 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +020037 des_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000038
Paul Bakker33b43f12013-08-20 11:48:36 +020039 unhexify( key_str, hex_key_string );
40 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +000041
42 des_setkey_enc( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000043 TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000044 hexify( dst_str, output, 8 );
45
Paul Bakker33b43f12013-08-20 11:48:36 +020046 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +020047
48 des_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000049}
Paul Bakker33b43f12013-08-20 11:48:36 +020050/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000051
Paul Bakker33b43f12013-08-20 11:48:36 +020052/* BEGIN_CASE */
53void des_decrypt_ecb( char *hex_key_string, char *hex_src_string,
54 char *hex_dst_string )
Paul Bakkere896fea2009-07-06 06:40:23 +000055{
56 unsigned char key_str[100];
57 unsigned char src_str[100];
58 unsigned char dst_str[100];
59 unsigned char output[100];
60 des_context ctx;
61
62 memset(key_str, 0x00, 100);
63 memset(src_str, 0x00, 100);
64 memset(dst_str, 0x00, 100);
65 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +020066 des_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000067
Paul Bakker33b43f12013-08-20 11:48:36 +020068 unhexify( key_str, hex_key_string );
69 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +000070
71 des_setkey_dec( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000072 TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000073 hexify( dst_str, output, 8 );
74
Paul Bakker33b43f12013-08-20 11:48:36 +020075 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +020076
77 des_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000078}
Paul Bakker33b43f12013-08-20 11:48:36 +020079/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000080
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +020081/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +020082void des_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
83 char *hex_src_string, char *hex_dst_string, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +000084{
85 unsigned char key_str[100];
86 unsigned char iv_str[100];
87 unsigned char src_str[100];
88 unsigned char dst_str[100];
89 unsigned char output[100];
90 des_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000091 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +000092
93 memset(key_str, 0x00, 100);
94 memset(iv_str, 0x00, 100);
95 memset(src_str, 0x00, 100);
96 memset(dst_str, 0x00, 100);
97 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +020098 des_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000099
Paul Bakker33b43f12013-08-20 11:48:36 +0200100 unhexify( key_str, hex_key_string );
101 unhexify( iv_str, hex_iv_string );
102 src_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000103
104 des_setkey_enc( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200105 TEST_ASSERT( des_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
106 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000107 {
108 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000109
Paul Bakker33b43f12013-08-20 11:48:36 +0200110 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000111 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200112
113 des_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000114}
Paul Bakker33b43f12013-08-20 11:48:36 +0200115/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000116
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200117/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200118void des_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
119 char *hex_src_string, char *hex_dst_string, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000120{
121 unsigned char key_str[100];
122 unsigned char iv_str[100];
123 unsigned char src_str[100];
124 unsigned char dst_str[100];
125 unsigned char output[100];
126 des_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000127 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000128
129 memset(key_str, 0x00, 100);
130 memset(iv_str, 0x00, 100);
131 memset(src_str, 0x00, 100);
132 memset(dst_str, 0x00, 100);
133 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200134 des_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000135
Paul Bakker33b43f12013-08-20 11:48:36 +0200136 unhexify( key_str, hex_key_string );
137 unhexify( iv_str, hex_iv_string );
138 src_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000139
140 des_setkey_dec( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200141 TEST_ASSERT( des_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
142 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000143 {
144 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000145
Paul Bakker33b43f12013-08-20 11:48:36 +0200146 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000147 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200148
149 des_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000150}
Paul Bakker33b43f12013-08-20 11:48:36 +0200151/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000152
Paul Bakker33b43f12013-08-20 11:48:36 +0200153/* BEGIN_CASE */
154void des3_encrypt_ecb( int key_count, char *hex_key_string,
155 char *hex_src_string, char *hex_dst_string )
Paul Bakkere896fea2009-07-06 06:40:23 +0000156{
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);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200167 des3_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000168
Paul Bakker33b43f12013-08-20 11:48:36 +0200169 unhexify( key_str, hex_key_string );
170 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000171
Paul Bakker33b43f12013-08-20 11:48:36 +0200172 if( key_count == 2 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000173 des3_set2key_enc( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200174 else if( key_count == 3 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000175 des3_set3key_enc( &ctx, key_str );
176 else
177 TEST_ASSERT( 0 );
178
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000179 TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000180 hexify( dst_str, output, 8 );
181
Paul Bakker33b43f12013-08-20 11:48:36 +0200182 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200183
184 des3_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000185}
Paul Bakker33b43f12013-08-20 11:48:36 +0200186/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000187
Paul Bakker33b43f12013-08-20 11:48:36 +0200188/* BEGIN_CASE */
189void des3_decrypt_ecb( int key_count, char *hex_key_string,
190 char *hex_src_string, char *hex_dst_string )
Paul Bakkere896fea2009-07-06 06:40:23 +0000191{
192 unsigned char key_str[100];
193 unsigned char src_str[100];
194 unsigned char dst_str[100];
195 unsigned char output[100];
196 des3_context ctx;
197
198 memset(key_str, 0x00, 100);
199 memset(src_str, 0x00, 100);
200 memset(dst_str, 0x00, 100);
201 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200202 des3_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000203
Paul Bakker33b43f12013-08-20 11:48:36 +0200204 unhexify( key_str, hex_key_string );
205 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000206
Paul Bakker33b43f12013-08-20 11:48:36 +0200207 if( key_count == 2 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000208 des3_set2key_dec( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200209 else if( key_count == 3 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000210 des3_set3key_dec( &ctx, key_str );
211 else
212 TEST_ASSERT( 0 );
213
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000214 TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000215 hexify( dst_str, output, 8 );
216
Paul Bakker33b43f12013-08-20 11:48:36 +0200217 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200218
219 des3_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000220}
Paul Bakker33b43f12013-08-20 11:48:36 +0200221/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000222
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200223/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200224void des3_encrypt_cbc( int key_count, char *hex_key_string,
225 char *hex_iv_string, char *hex_src_string,
226 char *hex_dst_string, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000227{
228 unsigned char key_str[100];
229 unsigned char iv_str[100];
230 unsigned char src_str[100];
231 unsigned char dst_str[100];
232 unsigned char output[100];
233 des3_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000234 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000235
236 memset(key_str, 0x00, 100);
237 memset(iv_str, 0x00, 100);
238 memset(src_str, 0x00, 100);
239 memset(dst_str, 0x00, 100);
240 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200241 des3_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000242
Paul Bakker33b43f12013-08-20 11:48:36 +0200243 unhexify( key_str, hex_key_string );
244 unhexify( iv_str, hex_iv_string );
245 src_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000246
Paul Bakker33b43f12013-08-20 11:48:36 +0200247 if( key_count == 2 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000248 des3_set2key_enc( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200249 else if( key_count == 3 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000250 des3_set3key_enc( &ctx, key_str );
251 else
252 TEST_ASSERT( 0 );
253
Paul Bakker33b43f12013-08-20 11:48:36 +0200254 TEST_ASSERT( des3_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
Paul Bakker02722ea2011-05-25 11:34:44 +0000255
Paul Bakker33b43f12013-08-20 11:48:36 +0200256 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000257 {
258 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000259
Paul Bakker33b43f12013-08-20 11:48:36 +0200260 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000261 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200262
263 des3_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000264}
Paul Bakker33b43f12013-08-20 11:48:36 +0200265/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000266
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200267/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200268void des3_decrypt_cbc( int key_count, char *hex_key_string,
269 char *hex_iv_string, char *hex_src_string,
270 char *hex_dst_string, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000271{
272 unsigned char key_str[100];
273 unsigned char iv_str[100];
274 unsigned char src_str[100];
275 unsigned char dst_str[100];
276 unsigned char output[100];
277 des3_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000278 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000279
280 memset(key_str, 0x00, 100);
281 memset(iv_str, 0x00, 100);
282 memset(src_str, 0x00, 100);
283 memset(dst_str, 0x00, 100);
284 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200285 des3_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000286
Paul Bakker33b43f12013-08-20 11:48:36 +0200287 unhexify( key_str, hex_key_string );
288 unhexify( iv_str, hex_iv_string );
289 src_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000290
Paul Bakker33b43f12013-08-20 11:48:36 +0200291 if( key_count == 2 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000292 des3_set2key_dec( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200293 else if( key_count == 3 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000294 des3_set3key_dec( &ctx, key_str );
295 else
296 TEST_ASSERT( 0 );
297
Paul Bakker33b43f12013-08-20 11:48:36 +0200298 TEST_ASSERT( des3_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
Paul Bakker02722ea2011-05-25 11:34:44 +0000299
Paul Bakker33b43f12013-08-20 11:48:36 +0200300 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000301 {
302 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000303
Paul Bakker33b43f12013-08-20 11:48:36 +0200304 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000305 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200306
307 des3_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000308}
Paul Bakker33b43f12013-08-20 11:48:36 +0200309/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000310
Paul Bakker33b43f12013-08-20 11:48:36 +0200311/* BEGIN_CASE */
312void des_key_parity_run()
Paul Bakker1f87fb62011-01-15 17:32:24 +0000313{
314 int i, j, cnt;
315 unsigned char key[DES_KEY_SIZE];
316 unsigned int parity;
317
318 memset( key, 0, DES_KEY_SIZE );
319 cnt = 0;
320
321 // Iterate through all possible byte values
322 //
323 for( i = 0; i < 32; i++ )
324 {
325 for( j = 0; j < 8; j++ )
326 key[j] = cnt++;
327
328 // Set the key parity according to the table
329 //
330 des_key_set_parity( key );
331
332 // Check the parity with a function
333 //
334 for( j = 0; j < 8; j++ )
335 {
336 parity = key[j] ^ ( key[j] >> 4 );
337 parity = parity ^
338 ( parity >> 1 ) ^
339 ( parity >> 2 ) ^
340 ( parity >> 3 );
341 parity &= 1;
342
343 if( parity != 1 )
344 TEST_ASSERT( 0 );
345 }
346
347 // Check the parity with the table
348 //
349 TEST_ASSERT( des_key_check_key_parity( key ) == 0 );
350 }
351}
Paul Bakker33b43f12013-08-20 11:48:36 +0200352/* END_CASE */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000353
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200354/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200355void des_selftest()
Paul Bakkere896fea2009-07-06 06:40:23 +0000356{
357 TEST_ASSERT( des_self_test( 0 ) == 0 );
358}
Paul Bakker33b43f12013-08-20 11:48:36 +0200359/* END_CASE */