blob: d5d0f11ed2a5b00df75097ba637252fb06fae21b [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);
37
Paul Bakker33b43f12013-08-20 11:48:36 +020038 unhexify( key_str, hex_key_string );
39 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +000040
41 des_setkey_enc( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000042 TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000043 hexify( dst_str, output, 8 );
44
Paul Bakker33b43f12013-08-20 11:48:36 +020045 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000046}
Paul Bakker33b43f12013-08-20 11:48:36 +020047/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000048
Paul Bakker33b43f12013-08-20 11:48:36 +020049/* BEGIN_CASE */
50void des_decrypt_ecb( char *hex_key_string, char *hex_src_string,
51 char *hex_dst_string )
Paul Bakkere896fea2009-07-06 06:40:23 +000052{
53 unsigned char key_str[100];
54 unsigned char src_str[100];
55 unsigned char dst_str[100];
56 unsigned char output[100];
57 des_context ctx;
58
59 memset(key_str, 0x00, 100);
60 memset(src_str, 0x00, 100);
61 memset(dst_str, 0x00, 100);
62 memset(output, 0x00, 100);
63
Paul Bakker33b43f12013-08-20 11:48:36 +020064 unhexify( key_str, hex_key_string );
65 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +000066
67 des_setkey_dec( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000068 TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000069 hexify( dst_str, output, 8 );
70
Paul Bakker33b43f12013-08-20 11:48:36 +020071 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000072}
Paul Bakker33b43f12013-08-20 11:48:36 +020073/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000074
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +020075/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +020076void des_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
77 char *hex_src_string, char *hex_dst_string, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +000078{
79 unsigned char key_str[100];
80 unsigned char iv_str[100];
81 unsigned char src_str[100];
82 unsigned char dst_str[100];
83 unsigned char output[100];
84 des_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000085 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +000086
87 memset(key_str, 0x00, 100);
88 memset(iv_str, 0x00, 100);
89 memset(src_str, 0x00, 100);
90 memset(dst_str, 0x00, 100);
91 memset(output, 0x00, 100);
92
Paul Bakker33b43f12013-08-20 11:48:36 +020093 unhexify( key_str, hex_key_string );
94 unhexify( iv_str, hex_iv_string );
95 src_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +000096
97 des_setkey_enc( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +020098 TEST_ASSERT( des_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
99 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000100 {
101 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000102
Paul Bakker33b43f12013-08-20 11:48:36 +0200103 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000104 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000105}
Paul Bakker33b43f12013-08-20 11:48:36 +0200106/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000107
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200108/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200109void des_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
110 char *hex_src_string, char *hex_dst_string, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000111{
112 unsigned char key_str[100];
113 unsigned char iv_str[100];
114 unsigned char src_str[100];
115 unsigned char dst_str[100];
116 unsigned char output[100];
117 des_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000118 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000119
120 memset(key_str, 0x00, 100);
121 memset(iv_str, 0x00, 100);
122 memset(src_str, 0x00, 100);
123 memset(dst_str, 0x00, 100);
124 memset(output, 0x00, 100);
125
Paul Bakker33b43f12013-08-20 11:48:36 +0200126 unhexify( key_str, hex_key_string );
127 unhexify( iv_str, hex_iv_string );
128 src_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000129
130 des_setkey_dec( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200131 TEST_ASSERT( des_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
132 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000133 {
134 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000135
Paul Bakker33b43f12013-08-20 11:48:36 +0200136 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000137 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000138}
Paul Bakker33b43f12013-08-20 11:48:36 +0200139/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000140
Paul Bakker33b43f12013-08-20 11:48:36 +0200141/* BEGIN_CASE */
142void des3_encrypt_ecb( int key_count, char *hex_key_string,
143 char *hex_src_string, char *hex_dst_string )
Paul Bakkere896fea2009-07-06 06:40:23 +0000144{
145 unsigned char key_str[100];
146 unsigned char src_str[100];
147 unsigned char dst_str[100];
148 unsigned char output[100];
149 des3_context ctx;
150
151 memset(key_str, 0x00, 100);
152 memset(src_str, 0x00, 100);
153 memset(dst_str, 0x00, 100);
154 memset(output, 0x00, 100);
155
Paul Bakker33b43f12013-08-20 11:48:36 +0200156 unhexify( key_str, hex_key_string );
157 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000158
Paul Bakker33b43f12013-08-20 11:48:36 +0200159 if( key_count == 2 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000160 des3_set2key_enc( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200161 else if( key_count == 3 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000162 des3_set3key_enc( &ctx, key_str );
163 else
164 TEST_ASSERT( 0 );
165
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000166 TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000167 hexify( dst_str, output, 8 );
168
Paul Bakker33b43f12013-08-20 11:48:36 +0200169 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000170}
Paul Bakker33b43f12013-08-20 11:48:36 +0200171/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000172
Paul Bakker33b43f12013-08-20 11:48:36 +0200173/* BEGIN_CASE */
174void des3_decrypt_ecb( int key_count, char *hex_key_string,
175 char *hex_src_string, char *hex_dst_string )
Paul Bakkere896fea2009-07-06 06:40:23 +0000176{
177 unsigned char key_str[100];
178 unsigned char src_str[100];
179 unsigned char dst_str[100];
180 unsigned char output[100];
181 des3_context ctx;
182
183 memset(key_str, 0x00, 100);
184 memset(src_str, 0x00, 100);
185 memset(dst_str, 0x00, 100);
186 memset(output, 0x00, 100);
187
Paul Bakker33b43f12013-08-20 11:48:36 +0200188 unhexify( key_str, hex_key_string );
189 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000190
Paul Bakker33b43f12013-08-20 11:48:36 +0200191 if( key_count == 2 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000192 des3_set2key_dec( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200193 else if( key_count == 3 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000194 des3_set3key_dec( &ctx, key_str );
195 else
196 TEST_ASSERT( 0 );
197
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000198 TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000199 hexify( dst_str, output, 8 );
200
Paul Bakker33b43f12013-08-20 11:48:36 +0200201 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000202}
Paul Bakker33b43f12013-08-20 11:48:36 +0200203/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000204
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200205/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200206void des3_encrypt_cbc( int key_count, char *hex_key_string,
207 char *hex_iv_string, char *hex_src_string,
208 char *hex_dst_string, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000209{
210 unsigned char key_str[100];
211 unsigned char iv_str[100];
212 unsigned char src_str[100];
213 unsigned char dst_str[100];
214 unsigned char output[100];
215 des3_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000216 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000217
218 memset(key_str, 0x00, 100);
219 memset(iv_str, 0x00, 100);
220 memset(src_str, 0x00, 100);
221 memset(dst_str, 0x00, 100);
222 memset(output, 0x00, 100);
223
Paul Bakker33b43f12013-08-20 11:48:36 +0200224 unhexify( key_str, hex_key_string );
225 unhexify( iv_str, hex_iv_string );
226 src_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000227
Paul Bakker33b43f12013-08-20 11:48:36 +0200228 if( key_count == 2 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000229 des3_set2key_enc( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200230 else if( key_count == 3 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000231 des3_set3key_enc( &ctx, key_str );
232 else
233 TEST_ASSERT( 0 );
234
Paul Bakker33b43f12013-08-20 11:48:36 +0200235 TEST_ASSERT( des3_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
Paul Bakker02722ea2011-05-25 11:34:44 +0000236
Paul Bakker33b43f12013-08-20 11:48:36 +0200237 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000238 {
239 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000240
Paul Bakker33b43f12013-08-20 11:48:36 +0200241 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000242 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000243}
Paul Bakker33b43f12013-08-20 11:48:36 +0200244/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000245
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200246/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200247void des3_decrypt_cbc( int key_count, char *hex_key_string,
248 char *hex_iv_string, char *hex_src_string,
249 char *hex_dst_string, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000250{
251 unsigned char key_str[100];
252 unsigned char iv_str[100];
253 unsigned char src_str[100];
254 unsigned char dst_str[100];
255 unsigned char output[100];
256 des3_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000257 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000258
259 memset(key_str, 0x00, 100);
260 memset(iv_str, 0x00, 100);
261 memset(src_str, 0x00, 100);
262 memset(dst_str, 0x00, 100);
263 memset(output, 0x00, 100);
264
Paul Bakker33b43f12013-08-20 11:48:36 +0200265 unhexify( key_str, hex_key_string );
266 unhexify( iv_str, hex_iv_string );
267 src_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000268
Paul Bakker33b43f12013-08-20 11:48:36 +0200269 if( key_count == 2 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000270 des3_set2key_dec( &ctx, key_str );
Paul Bakker33b43f12013-08-20 11:48:36 +0200271 else if( key_count == 3 )
Paul Bakkere896fea2009-07-06 06:40:23 +0000272 des3_set3key_dec( &ctx, key_str );
273 else
274 TEST_ASSERT( 0 );
275
Paul Bakker33b43f12013-08-20 11:48:36 +0200276 TEST_ASSERT( des3_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
Paul Bakker02722ea2011-05-25 11:34:44 +0000277
Paul Bakker33b43f12013-08-20 11:48:36 +0200278 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000279 {
280 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000281
Paul Bakker33b43f12013-08-20 11:48:36 +0200282 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000283 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000284}
Paul Bakker33b43f12013-08-20 11:48:36 +0200285/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000286
Paul Bakker33b43f12013-08-20 11:48:36 +0200287/* BEGIN_CASE */
288void des_key_parity_run()
Paul Bakker1f87fb62011-01-15 17:32:24 +0000289{
290 int i, j, cnt;
291 unsigned char key[DES_KEY_SIZE];
292 unsigned int parity;
293
294 memset( key, 0, DES_KEY_SIZE );
295 cnt = 0;
296
297 // Iterate through all possible byte values
298 //
299 for( i = 0; i < 32; i++ )
300 {
301 for( j = 0; j < 8; j++ )
302 key[j] = cnt++;
303
304 // Set the key parity according to the table
305 //
306 des_key_set_parity( key );
307
308 // Check the parity with a function
309 //
310 for( j = 0; j < 8; j++ )
311 {
312 parity = key[j] ^ ( key[j] >> 4 );
313 parity = parity ^
314 ( parity >> 1 ) ^
315 ( parity >> 2 ) ^
316 ( parity >> 3 );
317 parity &= 1;
318
319 if( parity != 1 )
320 TEST_ASSERT( 0 );
321 }
322
323 // Check the parity with the table
324 //
325 TEST_ASSERT( des_key_check_key_parity( key ) == 0 );
326 }
327}
Paul Bakker33b43f12013-08-20 11:48:36 +0200328/* END_CASE */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000329
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200330/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200331void des_selftest()
Paul Bakkere896fea2009-07-06 06:40:23 +0000332{
333 TEST_ASSERT( des_self_test( 0 ) == 0 );
334}
Paul Bakker33b43f12013-08-20 11:48:36 +0200335/* END_CASE */