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( data_t * key, int ret ) 12{ 13 TEST_ASSERT( mbedtls_des_key_check_weak( key->x ) == ret ); 14} 15/* END_CASE */ 16 17/* BEGIN_CASE */ 18void des_encrypt_ecb( data_t * key_str, data_t * src_str, data_t * dst ) 19{ 20 unsigned char output[100]; 21 mbedtls_des_context ctx; 22 23 memset(output, 0x00, 100); 24 mbedtls_des_init( &ctx ); 25 26 27 TEST_ASSERT( mbedtls_des_setkey_enc( &ctx, key_str->x ) == 0 ); 28 TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str->x, output ) == 0 ); 29 30 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 8, dst->len ) == 0 ); 31 32exit: 33 mbedtls_des_free( &ctx ); 34} 35/* END_CASE */ 36 37/* BEGIN_CASE */ 38void des_decrypt_ecb( data_t * key_str, data_t * src_str, data_t * dst ) 39{ 40 unsigned char output[100]; 41 mbedtls_des_context ctx; 42 43 memset(output, 0x00, 100); 44 mbedtls_des_init( &ctx ); 45 46 47 TEST_ASSERT( mbedtls_des_setkey_dec( &ctx, key_str->x ) == 0 ); 48 TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str->x, output ) == 0 ); 49 50 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 8, dst->len ) == 0 ); 51 52exit: 53 mbedtls_des_free( &ctx ); 54} 55/* END_CASE */ 56 57/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ 58void des_encrypt_cbc( data_t * key_str, data_t * iv_str, 59 data_t * src_str, data_t * dst, int cbc_result ) 60{ 61 unsigned char output[100]; 62 mbedtls_des_context ctx; 63 64 memset(output, 0x00, 100); 65 mbedtls_des_init( &ctx ); 66 67 68 TEST_ASSERT( mbedtls_des_setkey_enc( &ctx, key_str->x ) == 0 ); 69 TEST_ASSERT( mbedtls_des_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result ); 70 if( cbc_result == 0 ) 71 { 72 73 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len, 74 dst->len ) == 0 ); 75 } 76 77exit: 78 mbedtls_des_free( &ctx ); 79} 80/* END_CASE */ 81 82/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ 83void des_decrypt_cbc( data_t * key_str, data_t * iv_str, 84 data_t * src_str, data_t * dst, 85 int cbc_result ) 86{ 87 unsigned char output[100]; 88 mbedtls_des_context ctx; 89 90 memset(output, 0x00, 100); 91 mbedtls_des_init( &ctx ); 92 93 94 TEST_ASSERT( mbedtls_des_setkey_dec( &ctx, key_str->x ) == 0 ); 95 TEST_ASSERT( mbedtls_des_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result ); 96 if( cbc_result == 0 ) 97 { 98 99 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len, 100 dst->len ) == 0 ); 101 } 102 103exit: 104 mbedtls_des_free( &ctx ); 105} 106/* END_CASE */ 107 108/* BEGIN_CASE */ 109void des3_encrypt_ecb( int key_count, data_t * key_str, 110 data_t * src_str, data_t * dst ) 111{ 112 unsigned char output[100]; 113 mbedtls_des3_context ctx; 114 115 memset(output, 0x00, 100); 116 mbedtls_des3_init( &ctx ); 117 118 119 if( key_count == 2 ) 120 TEST_ASSERT( mbedtls_des3_set2key_enc( &ctx, key_str->x ) == 0 ); 121 else if( key_count == 3 ) 122 TEST_ASSERT( mbedtls_des3_set3key_enc( &ctx, key_str->x ) == 0 ); 123 else 124 TEST_ASSERT( 0 ); 125 126 TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str->x, output ) == 0 ); 127 128 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 8, dst->len ) == 0 ); 129 130exit: 131 mbedtls_des3_free( &ctx ); 132} 133/* END_CASE */ 134 135/* BEGIN_CASE */ 136void des3_decrypt_ecb( int key_count, data_t * key_str, 137 data_t * src_str, data_t * dst ) 138{ 139 unsigned char output[100]; 140 mbedtls_des3_context ctx; 141 142 memset(output, 0x00, 100); 143 mbedtls_des3_init( &ctx ); 144 145 146 if( key_count == 2 ) 147 TEST_ASSERT( mbedtls_des3_set2key_dec( &ctx, key_str->x ) == 0 ); 148 else if( key_count == 3 ) 149 TEST_ASSERT( mbedtls_des3_set3key_dec( &ctx, key_str->x ) == 0 ); 150 else 151 TEST_ASSERT( 0 ); 152 153 TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str->x, output ) == 0 ); 154 155 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 8, dst->len ) == 0 ); 156 157exit: 158 mbedtls_des3_free( &ctx ); 159} 160/* END_CASE */ 161 162/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ 163void des3_encrypt_cbc( int key_count, data_t * key_str, 164 data_t * iv_str, data_t * src_str, 165 data_t * dst, int cbc_result ) 166{ 167 unsigned char output[100]; 168 mbedtls_des3_context ctx; 169 170 memset(output, 0x00, 100); 171 mbedtls_des3_init( &ctx ); 172 173 174 if( key_count == 2 ) 175 TEST_ASSERT( mbedtls_des3_set2key_enc( &ctx, key_str->x ) == 0 ); 176 else if( key_count == 3 ) 177 TEST_ASSERT( mbedtls_des3_set3key_enc( &ctx, key_str->x ) == 0 ); 178 else 179 TEST_ASSERT( 0 ); 180 181 TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result ); 182 183 if( cbc_result == 0 ) 184 { 185 186 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 187 src_str->len, dst->len ) == 0 ); 188 } 189 190exit: 191 mbedtls_des3_free( &ctx ); 192} 193/* END_CASE */ 194 195/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ 196void des3_decrypt_cbc( int key_count, data_t * key_str, 197 data_t * iv_str, data_t * src_str, 198 data_t * dst, int cbc_result ) 199{ 200 unsigned char output[100]; 201 mbedtls_des3_context ctx; 202 203 memset(output, 0x00, 100); 204 mbedtls_des3_init( &ctx ); 205 206 207 if( key_count == 2 ) 208 TEST_ASSERT( mbedtls_des3_set2key_dec( &ctx, key_str->x ) == 0 ); 209 else if( key_count == 3 ) 210 TEST_ASSERT( mbedtls_des3_set3key_dec( &ctx, key_str->x ) == 0 ); 211 else 212 TEST_ASSERT( 0 ); 213 214 TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result ); 215 216 if( cbc_result == 0 ) 217 { 218 219 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len, 220 dst->len ) == 0 ); 221 } 222 223exit: 224 mbedtls_des3_free( &ctx ); 225} 226/* END_CASE */ 227 228/* BEGIN_CASE */ 229void des_key_parity_run( ) 230{ 231 int i, j, cnt; 232 unsigned char key[MBEDTLS_DES_KEY_SIZE]; 233 unsigned int parity; 234 235 memset( key, 0, MBEDTLS_DES_KEY_SIZE ); 236 cnt = 0; 237 238 // Iterate through all possible byte values 239 // 240 for( i = 0; i < 32; i++ ) 241 { 242 for( j = 0; j < 8; j++ ) 243 key[j] = cnt++; 244 245 // Set the key parity according to the table 246 // 247 mbedtls_des_key_set_parity( key ); 248 249 // Check the parity with a function 250 // 251 for( j = 0; j < 8; j++ ) 252 { 253 parity = key[j] ^ ( key[j] >> 4 ); 254 parity = parity ^ 255 ( parity >> 1 ) ^ 256 ( parity >> 2 ) ^ 257 ( parity >> 3 ); 258 parity &= 1; 259 260 if( parity != 1 ) 261 TEST_ASSERT( 0 ); 262 } 263 264 // Check the parity with the table 265 // 266 TEST_ASSERT( mbedtls_des_key_check_key_parity( key ) == 0 ); 267 } 268} 269/* END_CASE */ 270 271/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ 272void des_selftest( ) 273{ 274 TEST_ASSERT( mbedtls_des_self_test( 1 ) == 0 ); 275} 276/* END_CASE */ 277