2 #include "mbedtls/des.h"
6 * depends_on:MBEDTLS_DES_C
11 void des_check_weak( data_t * key, int ret )
13 TEST_ASSERT( mbedtls_des_key_check_weak( key->x ) == ret );
18 void des_encrypt_ecb( data_t * key_str, data_t * src_str,
19 data_t * hex_dst_string )
21 unsigned char output[100];
22 mbedtls_des_context ctx;
24 memset(output, 0x00, 100);
25 mbedtls_des_init( &ctx );
28 mbedtls_des_setkey_enc( &ctx, key_str->x );
29 TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str->x, output ) == 0 );
31 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 8, hex_dst_string->len ) == 0 );
34 mbedtls_des_free( &ctx );
39 void des_decrypt_ecb( data_t * key_str, data_t * src_str,
40 data_t * hex_dst_string )
42 unsigned char output[100];
43 mbedtls_des_context ctx;
45 memset(output, 0x00, 100);
46 mbedtls_des_init( &ctx );
49 mbedtls_des_setkey_dec( &ctx, key_str->x );
50 TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str->x, output ) == 0 );
52 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 8, hex_dst_string->len ) == 0 );
55 mbedtls_des_free( &ctx );
59 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
60 void des_encrypt_cbc( data_t * key_str, data_t * iv_str,
61 data_t * src_str, data_t * hex_dst_string,
64 unsigned char output[100];
65 mbedtls_des_context ctx;
67 memset(output, 0x00, 100);
68 mbedtls_des_init( &ctx );
71 mbedtls_des_setkey_enc( &ctx, key_str->x );
72 TEST_ASSERT( mbedtls_des_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
76 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
80 mbedtls_des_free( &ctx );
84 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
85 void des_decrypt_cbc( data_t * key_str, data_t * iv_str,
86 data_t * src_str, data_t * hex_dst_string,
89 unsigned char output[100];
90 mbedtls_des_context ctx;
92 memset(output, 0x00, 100);
93 mbedtls_des_init( &ctx );
96 mbedtls_des_setkey_dec( &ctx, key_str->x );
97 TEST_ASSERT( mbedtls_des_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
101 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
105 mbedtls_des_free( &ctx );
110 void des3_encrypt_ecb( int key_count, data_t * key_str,
111 data_t * src_str, data_t * hex_dst_string )
113 unsigned char output[100];
114 mbedtls_des3_context ctx;
116 memset(output, 0x00, 100);
117 mbedtls_des3_init( &ctx );
121 mbedtls_des3_set2key_enc( &ctx, key_str->x );
122 else if( key_count == 3 )
123 mbedtls_des3_set3key_enc( &ctx, key_str->x );
127 TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str->x, output ) == 0 );
129 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 8, hex_dst_string->len ) == 0 );
132 mbedtls_des3_free( &ctx );
137 void des3_decrypt_ecb( int key_count, data_t * key_str,
138 data_t * src_str, data_t * hex_dst_string )
140 unsigned char output[100];
141 mbedtls_des3_context ctx;
143 memset(output, 0x00, 100);
144 mbedtls_des3_init( &ctx );
148 mbedtls_des3_set2key_dec( &ctx, key_str->x );
149 else if( key_count == 3 )
150 mbedtls_des3_set3key_dec( &ctx, key_str->x );
154 TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str->x, output ) == 0 );
156 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 8, hex_dst_string->len ) == 0 );
159 mbedtls_des3_free( &ctx );
163 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
164 void des3_encrypt_cbc( int key_count, data_t * key_str,
165 data_t * iv_str, data_t * src_str,
166 data_t * hex_dst_string, int cbc_result )
168 unsigned char output[100];
169 mbedtls_des3_context ctx;
171 memset(output, 0x00, 100);
172 mbedtls_des3_init( &ctx );
176 mbedtls_des3_set2key_enc( &ctx, key_str->x );
177 else if( key_count == 3 )
178 mbedtls_des3_set3key_enc( &ctx, key_str->x );
182 TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
184 if( cbc_result == 0 )
187 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
191 mbedtls_des3_free( &ctx );
195 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
196 void des3_decrypt_cbc( int key_count, data_t * key_str,
197 data_t * iv_str, data_t * src_str,
198 data_t * hex_dst_string, int cbc_result )
200 unsigned char output[100];
201 mbedtls_des3_context ctx;
203 memset(output, 0x00, 100);
204 mbedtls_des3_init( &ctx );
208 mbedtls_des3_set2key_dec( &ctx, key_str->x );
209 else if( key_count == 3 )
210 mbedtls_des3_set3key_dec( &ctx, key_str->x );
214 TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
216 if( cbc_result == 0 )
219 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
223 mbedtls_des3_free( &ctx );
228 void des_key_parity_run( )
231 unsigned char key[MBEDTLS_DES_KEY_SIZE];
234 memset( key, 0, MBEDTLS_DES_KEY_SIZE );
237 // Iterate through all possible byte values
239 for( i = 0; i < 32; i++ )
241 for( j = 0; j < 8; j++ )
244 // Set the key parity according to the table
246 mbedtls_des_key_set_parity( key );
248 // Check the parity with a function
250 for( j = 0; j < 8; j++ )
252 parity = key[j] ^ ( key[j] >> 4 );
263 // Check the parity with the table
265 TEST_ASSERT( mbedtls_des_key_check_key_parity( key ) == 0 );
270 /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
273 TEST_ASSERT( mbedtls_des_self_test( 1 ) == 0 );