2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/des.h"
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_HEADER */
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 10:49:31 +00:00
|
|
|
* depends_on:MBEDTLS_DES_C
|
2013-08-20 09:48:36 +00:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 13:16:06 +00:00
|
|
|
|
2014-03-29 16:06:43 +00:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 13:23:15 +00:00
|
|
|
void des_check_weak( uint8_t * key, uint32_t key_len, int ret )
|
2014-03-29 16:06:43 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_des_key_check_weak( key ) == ret );
|
2014-03-29 16:06:43 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 13:23:15 +00:00
|
|
|
void des_encrypt_ecb( uint8_t * key_str, uint32_t key_str_len,
|
|
|
|
uint8_t * src_str, uint32_t src_str_len,
|
|
|
|
uint8_t * hex_dst_string, uint32_t hex_dst_string_len )
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
|
|
|
unsigned char output[100];
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_context ctx;
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
memset(output, 0x00, 100);
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_init( &ctx );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_setkey_enc( &ctx, key_str );
|
|
|
|
TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str, output ) == 0 );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2017-05-30 13:23:15 +00:00
|
|
|
TEST_ASSERT( hexcmp( output, hex_dst_string, 8, hex_dst_string_len ) == 0 );
|
2014-06-18 09:16:11 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_free( &ctx );
|
2009-07-06 06:40:23 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 13:23:15 +00:00
|
|
|
void des_decrypt_ecb( uint8_t * key_str, uint32_t key_str_len,
|
|
|
|
uint8_t * src_str, uint32_t src_str_len,
|
|
|
|
uint8_t * hex_dst_string, uint32_t hex_dst_string_len )
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
|
|
|
unsigned char output[100];
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_context ctx;
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
memset(output, 0x00, 100);
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_init( &ctx );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_setkey_dec( &ctx, key_str );
|
|
|
|
TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str, output ) == 0 );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2017-05-30 13:23:15 +00:00
|
|
|
TEST_ASSERT( hexcmp( output, hex_dst_string, 8, hex_dst_string_len ) == 0 );
|
2014-06-18 09:16:11 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_free( &ctx );
|
2009-07-06 06:40:23 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
|
2017-05-30 13:23:15 +00:00
|
|
|
void des_encrypt_cbc( uint8_t * key_str, uint32_t key_str_len,
|
|
|
|
uint8_t * iv_str, uint32_t iv_str_len,
|
|
|
|
uint8_t * src_str, uint32_t src_len,
|
|
|
|
uint8_t * hex_dst_string, uint32_t hex_dst_string_len,
|
|
|
|
int cbc_result )
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
|
|
|
unsigned char output[100];
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_context ctx;
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
memset(output, 0x00, 100);
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_init( &ctx );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_setkey_enc( &ctx, key_str );
|
|
|
|
TEST_ASSERT( mbedtls_des_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
|
2013-08-20 09:48:36 +00:00
|
|
|
if( cbc_result == 0 )
|
2010-03-18 21:21:02 +00:00
|
|
|
{
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2017-05-30 13:23:15 +00:00
|
|
|
TEST_ASSERT( hexcmp( output, hex_dst_string, src_len, hex_dst_string_len ) == 0 );
|
2010-03-18 21:21:02 +00:00
|
|
|
}
|
2014-06-18 09:16:11 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_free( &ctx );
|
2009-07-06 06:40:23 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
|
2017-05-30 13:23:15 +00:00
|
|
|
void des_decrypt_cbc( uint8_t * key_str, uint32_t key_str_len,
|
|
|
|
uint8_t * iv_str, uint32_t iv_str_len,
|
|
|
|
uint8_t * src_str, uint32_t src_len,
|
|
|
|
uint8_t * hex_dst_string, uint32_t hex_dst_string_len,
|
|
|
|
int cbc_result )
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
|
|
|
unsigned char output[100];
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_context ctx;
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
memset(output, 0x00, 100);
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_init( &ctx );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_setkey_dec( &ctx, key_str );
|
|
|
|
TEST_ASSERT( mbedtls_des_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
|
2013-08-20 09:48:36 +00:00
|
|
|
if( cbc_result == 0 )
|
2010-03-18 21:21:02 +00:00
|
|
|
{
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2017-05-30 13:23:15 +00:00
|
|
|
TEST_ASSERT( hexcmp( output, hex_dst_string, src_len, hex_dst_string_len ) == 0 );
|
2010-03-18 21:21:02 +00:00
|
|
|
}
|
2014-06-18 09:16:11 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_free( &ctx );
|
2009-07-06 06:40:23 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 13:23:15 +00:00
|
|
|
void des3_encrypt_ecb( int key_count, uint8_t * key_str, uint32_t key_str_len,
|
|
|
|
uint8_t * src_str, uint32_t src_str_len,
|
|
|
|
uint8_t * hex_dst_string, uint32_t hex_dst_string_len )
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
|
|
|
unsigned char output[100];
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_context ctx;
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
memset(output, 0x00, 100);
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_init( &ctx );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
if( key_count == 2 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_set2key_enc( &ctx, key_str );
|
2013-08-20 09:48:36 +00:00
|
|
|
else if( key_count == 3 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_set3key_enc( &ctx, key_str );
|
2009-07-06 06:40:23 +00:00
|
|
|
else
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str, output ) == 0 );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2017-05-30 13:23:15 +00:00
|
|
|
TEST_ASSERT( hexcmp( output, hex_dst_string, 8, hex_dst_string_len ) == 0 );
|
2014-06-18 09:16:11 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_free( &ctx );
|
2009-07-06 06:40:23 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 13:23:15 +00:00
|
|
|
void des3_decrypt_ecb( int key_count, uint8_t * key_str, uint32_t key_str_len,
|
|
|
|
uint8_t * src_str, uint32_t src_str_len,
|
|
|
|
uint8_t * hex_dst_string, uint32_t hex_dst_string_len )
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
|
|
|
unsigned char output[100];
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_context ctx;
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
memset(output, 0x00, 100);
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_init( &ctx );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
if( key_count == 2 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_set2key_dec( &ctx, key_str );
|
2013-08-20 09:48:36 +00:00
|
|
|
else if( key_count == 3 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_set3key_dec( &ctx, key_str );
|
2009-07-06 06:40:23 +00:00
|
|
|
else
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str, output ) == 0 );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2017-05-30 13:23:15 +00:00
|
|
|
TEST_ASSERT( hexcmp( output, hex_dst_string, 8, hex_dst_string_len ) == 0 );
|
2014-06-18 09:16:11 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_free( &ctx );
|
2009-07-06 06:40:23 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
|
2017-05-30 13:23:15 +00:00
|
|
|
void des3_encrypt_cbc( int key_count, uint8_t * key_str, uint32_t key_str_len,
|
|
|
|
uint8_t * iv_str, uint32_t iv_str_len,
|
|
|
|
uint8_t * src_str, uint32_t src_len,
|
|
|
|
uint8_t * hex_dst_string, uint32_t hex_dst_string_len,
|
|
|
|
int cbc_result )
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
|
|
|
unsigned char output[100];
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_context ctx;
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
memset(output, 0x00, 100);
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_init( &ctx );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
if( key_count == 2 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_set2key_enc( &ctx, key_str );
|
2013-08-20 09:48:36 +00:00
|
|
|
else if( key_count == 3 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_set3key_enc( &ctx, key_str );
|
2009-07-06 06:40:23 +00:00
|
|
|
else
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
|
2011-05-25 11:34:44 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
if( cbc_result == 0 )
|
2010-03-18 21:21:02 +00:00
|
|
|
{
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2017-05-30 13:23:15 +00:00
|
|
|
TEST_ASSERT( hexcmp( output, hex_dst_string, src_len, hex_dst_string_len ) == 0 );
|
2010-03-18 21:21:02 +00:00
|
|
|
}
|
2014-06-18 09:16:11 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_free( &ctx );
|
2009-07-06 06:40:23 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
|
2017-05-30 13:23:15 +00:00
|
|
|
void des3_decrypt_cbc( int key_count, uint8_t * key_str, uint32_t key_str_len,
|
|
|
|
uint8_t * iv_str, uint32_t iv_str_len,
|
|
|
|
uint8_t * src_str, uint32_t src_len,
|
|
|
|
uint8_t * hex_dst_string, uint32_t hex_dst_string_len,
|
|
|
|
int cbc_result )
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
|
|
|
unsigned char output[100];
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_context ctx;
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
memset(output, 0x00, 100);
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_init( &ctx );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
if( key_count == 2 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_set2key_dec( &ctx, key_str );
|
2013-08-20 09:48:36 +00:00
|
|
|
else if( key_count == 3 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_set3key_dec( &ctx, key_str );
|
2009-07-06 06:40:23 +00:00
|
|
|
else
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
|
2011-05-25 11:34:44 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
if( cbc_result == 0 )
|
2010-03-18 21:21:02 +00:00
|
|
|
{
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2017-05-30 13:23:15 +00:00
|
|
|
TEST_ASSERT( hexcmp( output, hex_dst_string, src_len, hex_dst_string_len ) == 0 );
|
2010-03-18 21:21:02 +00:00
|
|
|
}
|
2014-06-18 09:16:11 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des3_free( &ctx );
|
2009-07-06 06:40:23 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 13:23:15 +00:00
|
|
|
void des_key_parity_run( )
|
2011-01-15 17:32:24 +00:00
|
|
|
{
|
|
|
|
int i, j, cnt;
|
2015-04-08 10:49:31 +00:00
|
|
|
unsigned char key[MBEDTLS_DES_KEY_SIZE];
|
2011-01-15 17:32:24 +00:00
|
|
|
unsigned int parity;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
memset( key, 0, MBEDTLS_DES_KEY_SIZE );
|
2011-01-15 17:32:24 +00:00
|
|
|
cnt = 0;
|
|
|
|
|
|
|
|
// Iterate through all possible byte values
|
|
|
|
//
|
|
|
|
for( i = 0; i < 32; i++ )
|
|
|
|
{
|
|
|
|
for( j = 0; j < 8; j++ )
|
|
|
|
key[j] = cnt++;
|
|
|
|
|
|
|
|
// Set the key parity according to the table
|
|
|
|
//
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_des_key_set_parity( key );
|
2011-01-15 17:32:24 +00:00
|
|
|
|
|
|
|
// Check the parity with a function
|
|
|
|
//
|
|
|
|
for( j = 0; j < 8; j++ )
|
|
|
|
{
|
|
|
|
parity = key[j] ^ ( key[j] >> 4 );
|
|
|
|
parity = parity ^
|
|
|
|
( parity >> 1 ) ^
|
|
|
|
( parity >> 2 ) ^
|
|
|
|
( parity >> 3 );
|
|
|
|
parity &= 1;
|
|
|
|
|
|
|
|
if( parity != 1 )
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the parity with the table
|
|
|
|
//
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_des_key_check_key_parity( key ) == 0 );
|
2011-01-15 17:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2011-01-15 17:32:24 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
2017-05-30 13:23:15 +00:00
|
|
|
void des_selftest( )
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
2016-09-09 08:10:28 +00:00
|
|
|
TEST_ASSERT( mbedtls_des_self_test( 1 ) == 0 );
|
2009-07-06 06:40:23 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|