2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2009-07-06 08:40:23 +02:00
|
|
|
#include <polarssl/des.h>
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:POLARSSL_DES_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 15:16:06 +02:00
|
|
|
|
2014-03-29 17:06:43 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void des_check_weak( char *key_hex, int ret )
|
|
|
|
{
|
|
|
|
unsigned char key[DES_KEY_SIZE];
|
|
|
|
|
|
|
|
memset( key, 0, sizeof key );
|
|
|
|
|
|
|
|
unhexify( key, key_hex );
|
|
|
|
|
|
|
|
TEST_ASSERT( des_key_check_weak( key ) == ret );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void des_encrypt_ecb( char *hex_key_string, char *hex_src_string,
|
|
|
|
char *hex_dst_string )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
|
|
|
unsigned char key_str[100];
|
|
|
|
unsigned char src_str[100];
|
|
|
|
unsigned char dst_str[100];
|
|
|
|
unsigned char output[100];
|
|
|
|
des_context ctx;
|
|
|
|
|
|
|
|
memset(key_str, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 100);
|
|
|
|
memset(dst_str, 0x00, 100);
|
|
|
|
memset(output, 0x00, 100);
|
2014-06-18 11:16:11 +02:00
|
|
|
des_init( &ctx );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
unhexify( key_str, hex_key_string );
|
|
|
|
unhexify( src_str, hex_src_string );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
|
|
|
des_setkey_enc( &ctx, key_str );
|
2010-03-18 22:21:02 +01:00
|
|
|
TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
|
2009-07-06 08:40:23 +02:00
|
|
|
hexify( dst_str, output, 8 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
|
2014-06-18 11:16:11 +02:00
|
|
|
|
|
|
|
des_free( &ctx );
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void des_decrypt_ecb( char *hex_key_string, char *hex_src_string,
|
|
|
|
char *hex_dst_string )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
|
|
|
unsigned char key_str[100];
|
|
|
|
unsigned char src_str[100];
|
|
|
|
unsigned char dst_str[100];
|
|
|
|
unsigned char output[100];
|
|
|
|
des_context ctx;
|
|
|
|
|
|
|
|
memset(key_str, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 100);
|
|
|
|
memset(dst_str, 0x00, 100);
|
|
|
|
memset(output, 0x00, 100);
|
2014-06-18 11:16:11 +02:00
|
|
|
des_init( &ctx );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
unhexify( key_str, hex_key_string );
|
|
|
|
unhexify( src_str, hex_src_string );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
|
|
|
des_setkey_dec( &ctx, key_str );
|
2010-03-18 22:21:02 +01:00
|
|
|
TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
|
2009-07-06 08:40:23 +02:00
|
|
|
hexify( dst_str, output, 8 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
|
2014-06-18 11:16:11 +02:00
|
|
|
|
|
|
|
des_free( &ctx );
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-09-13 16:24:20 +02:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
|
2013-08-20 11:48:36 +02:00
|
|
|
void des_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
|
|
|
|
char *hex_src_string, char *hex_dst_string, int cbc_result )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
|
|
|
unsigned char key_str[100];
|
|
|
|
unsigned char iv_str[100];
|
|
|
|
unsigned char src_str[100];
|
|
|
|
unsigned char dst_str[100];
|
|
|
|
unsigned char output[100];
|
|
|
|
des_context ctx;
|
2009-07-11 21:15:20 +02:00
|
|
|
int src_len;
|
2009-07-06 08:40:23 +02:00
|
|
|
|
|
|
|
memset(key_str, 0x00, 100);
|
|
|
|
memset(iv_str, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 100);
|
|
|
|
memset(dst_str, 0x00, 100);
|
|
|
|
memset(output, 0x00, 100);
|
2014-06-18 11:16:11 +02:00
|
|
|
des_init( &ctx );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
unhexify( key_str, hex_key_string );
|
|
|
|
unhexify( iv_str, hex_iv_string );
|
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
|
|
|
des_setkey_enc( &ctx, key_str );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( des_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
|
|
|
|
if( cbc_result == 0 )
|
2010-03-18 22:21:02 +01:00
|
|
|
{
|
|
|
|
hexify( dst_str, output, src_len );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
|
2010-03-18 22:21:02 +01:00
|
|
|
}
|
2014-06-18 11:16:11 +02:00
|
|
|
|
|
|
|
des_free( &ctx );
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-09-13 16:24:20 +02:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
|
2013-08-20 11:48:36 +02:00
|
|
|
void des_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
|
|
|
|
char *hex_src_string, char *hex_dst_string, int cbc_result )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
|
|
|
unsigned char key_str[100];
|
|
|
|
unsigned char iv_str[100];
|
|
|
|
unsigned char src_str[100];
|
|
|
|
unsigned char dst_str[100];
|
|
|
|
unsigned char output[100];
|
|
|
|
des_context ctx;
|
2009-07-11 21:15:20 +02:00
|
|
|
int src_len;
|
2009-07-06 08:40:23 +02:00
|
|
|
|
|
|
|
memset(key_str, 0x00, 100);
|
|
|
|
memset(iv_str, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 100);
|
|
|
|
memset(dst_str, 0x00, 100);
|
|
|
|
memset(output, 0x00, 100);
|
2014-06-18 11:16:11 +02:00
|
|
|
des_init( &ctx );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
unhexify( key_str, hex_key_string );
|
|
|
|
unhexify( iv_str, hex_iv_string );
|
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
|
|
|
des_setkey_dec( &ctx, key_str );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( des_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
|
|
|
|
if( cbc_result == 0 )
|
2010-03-18 22:21:02 +01:00
|
|
|
{
|
|
|
|
hexify( dst_str, output, src_len );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
|
2010-03-18 22:21:02 +01:00
|
|
|
}
|
2014-06-18 11:16:11 +02:00
|
|
|
|
|
|
|
des_free( &ctx );
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void des3_encrypt_ecb( int key_count, char *hex_key_string,
|
|
|
|
char *hex_src_string, char *hex_dst_string )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
|
|
|
unsigned char key_str[100];
|
|
|
|
unsigned char src_str[100];
|
|
|
|
unsigned char dst_str[100];
|
|
|
|
unsigned char output[100];
|
|
|
|
des3_context ctx;
|
|
|
|
|
|
|
|
memset(key_str, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 100);
|
|
|
|
memset(dst_str, 0x00, 100);
|
|
|
|
memset(output, 0x00, 100);
|
2014-06-18 11:16:11 +02:00
|
|
|
des3_init( &ctx );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
unhexify( key_str, hex_key_string );
|
|
|
|
unhexify( src_str, hex_src_string );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( key_count == 2 )
|
2009-07-06 08:40:23 +02:00
|
|
|
des3_set2key_enc( &ctx, key_str );
|
2013-08-20 11:48:36 +02:00
|
|
|
else if( key_count == 3 )
|
2009-07-06 08:40:23 +02:00
|
|
|
des3_set3key_enc( &ctx, key_str );
|
|
|
|
else
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
|
2010-03-18 22:21:02 +01:00
|
|
|
TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
|
2009-07-06 08:40:23 +02:00
|
|
|
hexify( dst_str, output, 8 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
|
2014-06-18 11:16:11 +02:00
|
|
|
|
|
|
|
des3_free( &ctx );
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void des3_decrypt_ecb( int key_count, char *hex_key_string,
|
|
|
|
char *hex_src_string, char *hex_dst_string )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
|
|
|
unsigned char key_str[100];
|
|
|
|
unsigned char src_str[100];
|
|
|
|
unsigned char dst_str[100];
|
|
|
|
unsigned char output[100];
|
|
|
|
des3_context ctx;
|
|
|
|
|
|
|
|
memset(key_str, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 100);
|
|
|
|
memset(dst_str, 0x00, 100);
|
|
|
|
memset(output, 0x00, 100);
|
2014-06-18 11:16:11 +02:00
|
|
|
des3_init( &ctx );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
unhexify( key_str, hex_key_string );
|
|
|
|
unhexify( src_str, hex_src_string );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( key_count == 2 )
|
2009-07-06 08:40:23 +02:00
|
|
|
des3_set2key_dec( &ctx, key_str );
|
2013-08-20 11:48:36 +02:00
|
|
|
else if( key_count == 3 )
|
2009-07-06 08:40:23 +02:00
|
|
|
des3_set3key_dec( &ctx, key_str );
|
|
|
|
else
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
|
2010-03-18 22:21:02 +01:00
|
|
|
TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
|
2009-07-06 08:40:23 +02:00
|
|
|
hexify( dst_str, output, 8 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
|
2014-06-18 11:16:11 +02:00
|
|
|
|
|
|
|
des3_free( &ctx );
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-09-13 16:24:20 +02:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
|
2013-08-20 11:48:36 +02:00
|
|
|
void des3_encrypt_cbc( int key_count, char *hex_key_string,
|
|
|
|
char *hex_iv_string, char *hex_src_string,
|
|
|
|
char *hex_dst_string, int cbc_result )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
|
|
|
unsigned char key_str[100];
|
|
|
|
unsigned char iv_str[100];
|
|
|
|
unsigned char src_str[100];
|
|
|
|
unsigned char dst_str[100];
|
|
|
|
unsigned char output[100];
|
|
|
|
des3_context ctx;
|
2009-07-11 21:15:20 +02:00
|
|
|
int src_len;
|
2009-07-06 08:40:23 +02:00
|
|
|
|
|
|
|
memset(key_str, 0x00, 100);
|
|
|
|
memset(iv_str, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 100);
|
|
|
|
memset(dst_str, 0x00, 100);
|
|
|
|
memset(output, 0x00, 100);
|
2014-06-18 11:16:11 +02:00
|
|
|
des3_init( &ctx );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
unhexify( key_str, hex_key_string );
|
|
|
|
unhexify( iv_str, hex_iv_string );
|
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( key_count == 2 )
|
2009-07-06 08:40:23 +02:00
|
|
|
des3_set2key_enc( &ctx, key_str );
|
2013-08-20 11:48:36 +02:00
|
|
|
else if( key_count == 3 )
|
2009-07-06 08:40:23 +02:00
|
|
|
des3_set3key_enc( &ctx, key_str );
|
|
|
|
else
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( des3_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );
|
2011-05-25 13:34:44 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( cbc_result == 0 )
|
2010-03-18 22:21:02 +01:00
|
|
|
{
|
|
|
|
hexify( dst_str, output, src_len );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
|
2010-03-18 22:21:02 +01:00
|
|
|
}
|
2014-06-18 11:16:11 +02:00
|
|
|
|
|
|
|
des3_free( &ctx );
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-09-13 16:24:20 +02:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
|
2013-08-20 11:48:36 +02:00
|
|
|
void des3_decrypt_cbc( int key_count, char *hex_key_string,
|
|
|
|
char *hex_iv_string, char *hex_src_string,
|
|
|
|
char *hex_dst_string, int cbc_result )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
|
|
|
unsigned char key_str[100];
|
|
|
|
unsigned char iv_str[100];
|
|
|
|
unsigned char src_str[100];
|
|
|
|
unsigned char dst_str[100];
|
|
|
|
unsigned char output[100];
|
|
|
|
des3_context ctx;
|
2009-07-11 21:15:20 +02:00
|
|
|
int src_len;
|
2009-07-06 08:40:23 +02:00
|
|
|
|
|
|
|
memset(key_str, 0x00, 100);
|
|
|
|
memset(iv_str, 0x00, 100);
|
|
|
|
memset(src_str, 0x00, 100);
|
|
|
|
memset(dst_str, 0x00, 100);
|
|
|
|
memset(output, 0x00, 100);
|
2014-06-18 11:16:11 +02:00
|
|
|
des3_init( &ctx );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
unhexify( key_str, hex_key_string );
|
|
|
|
unhexify( iv_str, hex_iv_string );
|
|
|
|
src_len = unhexify( src_str, hex_src_string );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( key_count == 2 )
|
2009-07-06 08:40:23 +02:00
|
|
|
des3_set2key_dec( &ctx, key_str );
|
2013-08-20 11:48:36 +02:00
|
|
|
else if( key_count == 3 )
|
2009-07-06 08:40:23 +02:00
|
|
|
des3_set3key_dec( &ctx, key_str );
|
|
|
|
else
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( des3_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );
|
2011-05-25 13:34:44 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( cbc_result == 0 )
|
2010-03-18 22:21:02 +01:00
|
|
|
{
|
|
|
|
hexify( dst_str, output, src_len );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
|
2010-03-18 22:21:02 +01:00
|
|
|
}
|
2014-06-18 11:16:11 +02:00
|
|
|
|
|
|
|
des3_free( &ctx );
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void des_key_parity_run()
|
2011-01-15 18:32:24 +01:00
|
|
|
{
|
|
|
|
int i, j, cnt;
|
|
|
|
unsigned char key[DES_KEY_SIZE];
|
|
|
|
unsigned int parity;
|
|
|
|
|
|
|
|
memset( key, 0, DES_KEY_SIZE );
|
|
|
|
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
|
|
|
|
//
|
|
|
|
des_key_set_parity( key );
|
|
|
|
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
TEST_ASSERT( des_key_check_key_parity( key ) == 0 );
|
|
|
|
}
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-01-15 18:32:24 +01:00
|
|
|
|
2013-10-10 12:48:03 +02:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
|
2013-08-20 11:48:36 +02:00
|
|
|
void des_selftest()
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
|
|
|
TEST_ASSERT( des_self_test( 0 ) == 0 );
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|