blob: 4b5d53d06151c426372349b6380435626194df9b [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
Paul Bakkerbd51b262014-07-10 15:26:12 +020048exit:
Paul Bakker8cfd9d82014-06-18 11:16:11 +020049 des_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000050}
Paul Bakker33b43f12013-08-20 11:48:36 +020051/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000052
Paul Bakker33b43f12013-08-20 11:48:36 +020053/* BEGIN_CASE */
54void des_decrypt_ecb( char *hex_key_string, char *hex_src_string,
55 char *hex_dst_string )
Paul Bakkere896fea2009-07-06 06:40:23 +000056{
57 unsigned char key_str[100];
58 unsigned char src_str[100];
59 unsigned char dst_str[100];
60 unsigned char output[100];
61 des_context ctx;
62
63 memset(key_str, 0x00, 100);
64 memset(src_str, 0x00, 100);
65 memset(dst_str, 0x00, 100);
66 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +020067 des_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000068
Paul Bakker33b43f12013-08-20 11:48:36 +020069 unhexify( key_str, hex_key_string );
70 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +000071
72 des_setkey_dec( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000073 TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000074 hexify( dst_str, output, 8 );
75
Paul Bakker33b43f12013-08-20 11:48:36 +020076 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +020077
Paul Bakkerbd51b262014-07-10 15:26:12 +020078exit:
Paul Bakker8cfd9d82014-06-18 11:16:11 +020079 des_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000080}
Paul Bakker33b43f12013-08-20 11:48:36 +020081/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000082
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +020083/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +020084void des_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
85 char *hex_src_string, char *hex_dst_string, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +000086{
87 unsigned char key_str[100];
88 unsigned char iv_str[100];
89 unsigned char src_str[100];
90 unsigned char dst_str[100];
91 unsigned char output[100];
92 des_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000093 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +000094
95 memset(key_str, 0x00, 100);
96 memset(iv_str, 0x00, 100);
97 memset(src_str, 0x00, 100);
98 memset(dst_str, 0x00, 100);
99 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200100 des_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000101
Paul Bakker33b43f12013-08-20 11:48:36 +0200102 unhexify( key_str, hex_key_string );
103 unhexify( iv_str, hex_iv_string );
104 src_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000105
106 des_setkey_enc( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200107 TEST_ASSERT( des_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
108 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000109 {
110 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000111
Paul Bakker33b43f12013-08-20 11:48:36 +0200112 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000113 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200114
Paul Bakkerbd51b262014-07-10 15:26:12 +0200115exit:
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200116 des_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000117}
Paul Bakker33b43f12013-08-20 11:48:36 +0200118/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000119
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200120/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200121void des_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
122 char *hex_src_string, char *hex_dst_string, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000123{
124 unsigned char key_str[100];
125 unsigned char iv_str[100];
126 unsigned char src_str[100];
127 unsigned char dst_str[100];
128 unsigned char output[100];
129 des_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000130 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000131
132 memset(key_str, 0x00, 100);
133 memset(iv_str, 0x00, 100);
134 memset(src_str, 0x00, 100);
135 memset(dst_str, 0x00, 100);
136 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200137 des_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000138
Paul Bakker33b43f12013-08-20 11:48:36 +0200139 unhexify( key_str, hex_key_string );
140 unhexify( iv_str, hex_iv_string );
141 src_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000142
143 des_setkey_dec( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200144 TEST_ASSERT( des_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
145 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000146 {
147 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000148
Paul Bakker33b43f12013-08-20 11:48:36 +0200149 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000150 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200151
Paul Bakkerbd51b262014-07-10 15:26:12 +0200152exit:
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200153 des_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000154}
Paul Bakker33b43f12013-08-20 11:48:36 +0200155/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000156
Paul Bakker33b43f12013-08-20 11:48:36 +0200157/* BEGIN_CASE */
158void des3_encrypt_ecb( int key_count, char *hex_key_string,
159 char *hex_src_string, char *hex_dst_string )
Paul Bakkere896fea2009-07-06 06:40:23 +0000160{
161 unsigned char key_str[100];
162 unsigned char src_str[100];
163 unsigned char dst_str[100];
164 unsigned char output[100];
165 des3_context ctx;
166
167 memset(key_str, 0x00, 100);
168 memset(src_str, 0x00, 100);
169 memset(dst_str, 0x00, 100);
170 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200171 des3_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000172
Paul Bakker33b43f12013-08-20 11:48:36 +0200173 unhexify( key_str, hex_key_string );
174 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000175
Paul Bakker33b43f12013-08-20 11:48:36 +0200176 if( key_count == 2 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000177 des3_set2key_enc( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200178 else if( key_count == 3 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000179 des3_set3key_enc( &ctx, key_str );
180 else
181 TEST_ASSERT( 0 );
182
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000183 TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000184 hexify( dst_str, output, 8 );
185
Paul Bakker33b43f12013-08-20 11:48:36 +0200186 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200187
Paul Bakkerbd51b262014-07-10 15:26:12 +0200188exit:
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200189 des3_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000190}
Paul Bakker33b43f12013-08-20 11:48:36 +0200191/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000192
Paul Bakker33b43f12013-08-20 11:48:36 +0200193/* BEGIN_CASE */
194void des3_decrypt_ecb( int key_count, char *hex_key_string,
195 char *hex_src_string, char *hex_dst_string )
Paul Bakkere896fea2009-07-06 06:40:23 +0000196{
197 unsigned char key_str[100];
198 unsigned char src_str[100];
199 unsigned char dst_str[100];
200 unsigned char output[100];
201 des3_context ctx;
202
203 memset(key_str, 0x00, 100);
204 memset(src_str, 0x00, 100);
205 memset(dst_str, 0x00, 100);
206 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200207 des3_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000208
Paul Bakker33b43f12013-08-20 11:48:36 +0200209 unhexify( key_str, hex_key_string );
210 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000211
Paul Bakker33b43f12013-08-20 11:48:36 +0200212 if( key_count == 2 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000213 des3_set2key_dec( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200214 else if( key_count == 3 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000215 des3_set3key_dec( &ctx, key_str );
216 else
217 TEST_ASSERT( 0 );
218
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000219 TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000220 hexify( dst_str, output, 8 );
221
Paul Bakker33b43f12013-08-20 11:48:36 +0200222 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200223
Paul Bakkerbd51b262014-07-10 15:26:12 +0200224exit:
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200225 des3_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000226}
Paul Bakker33b43f12013-08-20 11:48:36 +0200227/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000228
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200229/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200230void des3_encrypt_cbc( int key_count, char *hex_key_string,
231 char *hex_iv_string, char *hex_src_string,
232 char *hex_dst_string, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000233{
234 unsigned char key_str[100];
235 unsigned char iv_str[100];
236 unsigned char src_str[100];
237 unsigned char dst_str[100];
238 unsigned char output[100];
239 des3_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000240 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000241
242 memset(key_str, 0x00, 100);
243 memset(iv_str, 0x00, 100);
244 memset(src_str, 0x00, 100);
245 memset(dst_str, 0x00, 100);
246 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200247 des3_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000248
Paul Bakker33b43f12013-08-20 11:48:36 +0200249 unhexify( key_str, hex_key_string );
250 unhexify( iv_str, hex_iv_string );
251 src_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000252
Paul Bakker33b43f12013-08-20 11:48:36 +0200253 if( key_count == 2 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000254 des3_set2key_enc( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200255 else if( key_count == 3 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000256 des3_set3key_enc( &ctx, key_str );
257 else
258 TEST_ASSERT( 0 );
259
Paul Bakker33b43f12013-08-20 11:48:36 +0200260 TEST_ASSERT( des3_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
Paul Bakker02722ea2011-05-25 11:34:44 +0000261
Paul Bakker33b43f12013-08-20 11:48:36 +0200262 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000263 {
264 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000265
Paul Bakker33b43f12013-08-20 11:48:36 +0200266 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000267 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200268
Paul Bakkerbd51b262014-07-10 15:26:12 +0200269exit:
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200270 des3_free( &ctx );
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
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200274/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200275void des3_decrypt_cbc( int key_count, char *hex_key_string,
276 char *hex_iv_string, char *hex_src_string,
277 char *hex_dst_string, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000278{
279 unsigned char key_str[100];
280 unsigned char iv_str[100];
281 unsigned char src_str[100];
282 unsigned char dst_str[100];
283 unsigned char output[100];
284 des3_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000285 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000286
287 memset(key_str, 0x00, 100);
288 memset(iv_str, 0x00, 100);
289 memset(src_str, 0x00, 100);
290 memset(dst_str, 0x00, 100);
291 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200292 des3_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000293
Paul Bakker33b43f12013-08-20 11:48:36 +0200294 unhexify( key_str, hex_key_string );
295 unhexify( iv_str, hex_iv_string );
296 src_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000297
Paul Bakker33b43f12013-08-20 11:48:36 +0200298 if( key_count == 2 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000299 des3_set2key_dec( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200300 else if( key_count == 3 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000301 des3_set3key_dec( &ctx, key_str );
302 else
303 TEST_ASSERT( 0 );
304
Paul Bakker33b43f12013-08-20 11:48:36 +0200305 TEST_ASSERT( des3_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
Paul Bakker02722ea2011-05-25 11:34:44 +0000306
Paul Bakker33b43f12013-08-20 11:48:36 +0200307 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000308 {
309 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000310
Paul Bakker33b43f12013-08-20 11:48:36 +0200311 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000312 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200313
Paul Bakkerbd51b262014-07-10 15:26:12 +0200314exit:
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200315 des3_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000316}
Paul Bakker33b43f12013-08-20 11:48:36 +0200317/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000318
Paul Bakker33b43f12013-08-20 11:48:36 +0200319/* BEGIN_CASE */
320void des_key_parity_run()
Paul Bakker1f87fb62011-01-15 17:32:24 +0000321{
322 int i, j, cnt;
323 unsigned char key[DES_KEY_SIZE];
324 unsigned int parity;
325
326 memset( key, 0, DES_KEY_SIZE );
327 cnt = 0;
328
329 // Iterate through all possible byte values
330 //
331 for( i = 0; i < 32; i++ )
332 {
333 for( j = 0; j < 8; j++ )
334 key[j] = cnt++;
335
336 // Set the key parity according to the table
337 //
338 des_key_set_parity( key );
339
340 // Check the parity with a function
341 //
342 for( j = 0; j < 8; j++ )
343 {
344 parity = key[j] ^ ( key[j] >> 4 );
345 parity = parity ^
346 ( parity >> 1 ) ^
347 ( parity >> 2 ) ^
348 ( parity >> 3 );
349 parity &= 1;
350
351 if( parity != 1 )
352 TEST_ASSERT( 0 );
353 }
354
355 // Check the parity with the table
356 //
357 TEST_ASSERT( des_key_check_key_parity( key ) == 0 );
358 }
359}
Paul Bakker33b43f12013-08-20 11:48:36 +0200360/* END_CASE */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000361
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200362/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200363void des_selftest()
Paul Bakkere896fea2009-07-06 06:40:23 +0000364{
365 TEST_ASSERT( des_self_test( 0 ) == 0 );
366}
Paul Bakker33b43f12013-08-20 11:48:36 +0200367/* END_CASE */