1/* BEGIN_HEADER */
2#include "mbedtls/des.h"
3/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
6 * depends_on:MBEDTLS_DES_C
7 * END_DEPENDENCIES
8 */
9
10/* BEGIN_CASE */
11void des_check_weak( char *key_hex, int ret )
12{
13    unsigned char key[MBEDTLS_DES_KEY_SIZE];
14
15    memset( key, 0, sizeof key );
16
17    unhexify( key, key_hex );
18
19    TEST_ASSERT( mbedtls_des_key_check_weak( key ) == ret );
20}
21/* END_CASE */
22
23/* BEGIN_CASE */
24void des_encrypt_ecb( char *hex_key_string, char *hex_src_string,
25                      char *hex_dst_string )
26{
27    unsigned char key_str[100];
28    unsigned char src_str[100];
29    unsigned char dst_str[100];
30    unsigned char output[100];
31    mbedtls_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    mbedtls_des_init( &ctx );
38
39    unhexify( key_str, hex_key_string );
40    unhexify( src_str, hex_src_string );
41
42    mbedtls_des_setkey_enc( &ctx, key_str );
43    TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str, output ) == 0 );
44    hexify( dst_str, output, 8 );
45
46    TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
47
48exit:
49    mbedtls_des_free( &ctx );
50}
51/* END_CASE */
52
53/* BEGIN_CASE */
54void des_decrypt_ecb( char *hex_key_string, char *hex_src_string,
55                      char *hex_dst_string )
56{
57    unsigned char key_str[100];
58    unsigned char src_str[100];
59    unsigned char dst_str[100];
60    unsigned char output[100];
61    mbedtls_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);
67    mbedtls_des_init( &ctx );
68
69    unhexify( key_str, hex_key_string );
70    unhexify( src_str, hex_src_string );
71
72    mbedtls_des_setkey_dec( &ctx, key_str );
73    TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str, output ) == 0 );
74    hexify( dst_str, output, 8 );
75
76    TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
77
78exit:
79    mbedtls_des_free( &ctx );
80}
81/* END_CASE */
82
83/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
84void des_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
85                      char *hex_src_string, char *hex_dst_string, int cbc_result )
86{
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    mbedtls_des_context ctx;
93    int src_len;
94
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);
100    mbedtls_des_init( &ctx );
101
102    unhexify( key_str, hex_key_string );
103    unhexify( iv_str, hex_iv_string );
104    src_len = unhexify( src_str, hex_src_string );
105
106    mbedtls_des_setkey_enc( &ctx, key_str );
107    TEST_ASSERT( mbedtls_des_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
108    if( cbc_result == 0 )
109    {
110        hexify( dst_str, output, src_len );
111
112        TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
113    }
114
115exit:
116    mbedtls_des_free( &ctx );
117}
118/* END_CASE */
119
120/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
121void des_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
122                      char *hex_src_string, char *hex_dst_string, int cbc_result )
123{
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    mbedtls_des_context ctx;
130    int src_len;
131
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);
137    mbedtls_des_init( &ctx );
138
139    unhexify( key_str, hex_key_string );
140    unhexify( iv_str, hex_iv_string );
141    src_len = unhexify( src_str, hex_src_string );
142
143    mbedtls_des_setkey_dec( &ctx, key_str );
144    TEST_ASSERT( mbedtls_des_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
145    if( cbc_result == 0 )
146    {
147        hexify( dst_str, output, src_len );
148
149        TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
150    }
151
152exit:
153    mbedtls_des_free( &ctx );
154}
155/* END_CASE */
156
157/* BEGIN_CASE */
158void des3_encrypt_ecb( int key_count, char *hex_key_string,
159                       char *hex_src_string, char *hex_dst_string )
160{
161    unsigned char key_str[100];
162    unsigned char src_str[100];
163    unsigned char dst_str[100];
164    unsigned char output[100];
165    mbedtls_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);
171    mbedtls_des3_init( &ctx );
172
173    unhexify( key_str, hex_key_string );
174    unhexify( src_str, hex_src_string );
175
176    if( key_count == 2 )
177        mbedtls_des3_set2key_enc( &ctx, key_str );
178    else if( key_count == 3 )
179        mbedtls_des3_set3key_enc( &ctx, key_str );
180    else
181        TEST_ASSERT( 0 );
182
183    TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str, output ) == 0 );
184    hexify( dst_str, output, 8 );
185
186    TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
187
188exit:
189    mbedtls_des3_free( &ctx );
190}
191/* END_CASE */
192
193/* BEGIN_CASE */
194void des3_decrypt_ecb( int key_count, char *hex_key_string,
195                       char *hex_src_string, char *hex_dst_string )
196{
197    unsigned char key_str[100];
198    unsigned char src_str[100];
199    unsigned char dst_str[100];
200    unsigned char output[100];
201    mbedtls_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);
207    mbedtls_des3_init( &ctx );
208
209    unhexify( key_str, hex_key_string );
210    unhexify( src_str, hex_src_string );
211
212    if( key_count == 2 )
213        mbedtls_des3_set2key_dec( &ctx, key_str );
214    else if( key_count == 3 )
215        mbedtls_des3_set3key_dec( &ctx, key_str );
216    else
217        TEST_ASSERT( 0 );
218
219    TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str, output ) == 0 );
220    hexify( dst_str, output, 8 );
221
222    TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
223
224exit:
225    mbedtls_des3_free( &ctx );
226}
227/* END_CASE */
228
229/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
230void 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 )
233{
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    mbedtls_des3_context ctx;
240    int src_len;
241
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);
247    mbedtls_des3_init( &ctx );
248
249    unhexify( key_str, hex_key_string );
250    unhexify( iv_str, hex_iv_string );
251    src_len = unhexify( src_str, hex_src_string );
252
253    if( key_count == 2 )
254        mbedtls_des3_set2key_enc( &ctx, key_str );
255    else if( key_count == 3 )
256        mbedtls_des3_set3key_enc( &ctx, key_str );
257    else
258        TEST_ASSERT( 0 );
259
260    TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
261
262    if( cbc_result == 0 )
263    {
264        hexify( dst_str, output, src_len );
265
266        TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
267    }
268
269exit:
270    mbedtls_des3_free( &ctx );
271}
272/* END_CASE */
273
274/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
275void 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 )
278{
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    mbedtls_des3_context ctx;
285    int src_len;
286
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);
292    mbedtls_des3_init( &ctx );
293
294    unhexify( key_str, hex_key_string );
295    unhexify( iv_str, hex_iv_string );
296    src_len = unhexify( src_str, hex_src_string );
297
298    if( key_count == 2 )
299        mbedtls_des3_set2key_dec( &ctx, key_str );
300    else if( key_count == 3 )
301        mbedtls_des3_set3key_dec( &ctx, key_str );
302    else
303        TEST_ASSERT( 0 );
304
305    TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
306
307    if( cbc_result == 0 )
308    {
309        hexify( dst_str, output, src_len );
310
311        TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
312    }
313
314exit:
315    mbedtls_des3_free( &ctx );
316}
317/* END_CASE */
318
319/* BEGIN_CASE */
320void des_key_parity_run()
321{
322    int i, j, cnt;
323    unsigned char key[MBEDTLS_DES_KEY_SIZE];
324    unsigned int parity;
325
326    memset( key, 0, MBEDTLS_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        mbedtls_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( mbedtls_des_key_check_key_parity( key ) == 0 );
358    }
359}
360/* END_CASE */
361
362/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
363void des_selftest()
364{
365    TEST_ASSERT( mbedtls_des_self_test( 1 ) == 0 );
366}
367/* END_CASE */
368