| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 | /* BEGIN_HEADER */#include "mbedtls/des.h"/* END_HEADER *//* BEGIN_DEPENDENCIES * depends_on:MBEDTLS_DES_C * END_DEPENDENCIES *//* BEGIN_CASE */void des_check_weak( char *key_hex, int ret ){    unsigned char key[MBEDTLS_DES_KEY_SIZE];    memset( key, 0, sizeof key );    unhexify( key, key_hex );    TEST_ASSERT( mbedtls_des_key_check_weak( key ) == ret );}/* END_CASE *//* BEGIN_CASE */void des_encrypt_ecb( char *hex_key_string, char *hex_src_string,                      char *hex_dst_string ){    unsigned char key_str[100];    unsigned char src_str[100];    unsigned char dst_str[100];    unsigned char output[100];    mbedtls_des_context ctx;    memset(key_str, 0x00, 100);    memset(src_str, 0x00, 100);    memset(dst_str, 0x00, 100);    memset(output, 0x00, 100);    mbedtls_des_init( &ctx );    unhexify( key_str, hex_key_string );    unhexify( src_str, hex_src_string );    mbedtls_des_setkey_enc( &ctx, key_str );    TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str, output ) == 0 );    hexify( dst_str, output, 8 );    TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );exit:    mbedtls_des_free( &ctx );}/* END_CASE *//* BEGIN_CASE */void des_decrypt_ecb( char *hex_key_string, char *hex_src_string,                      char *hex_dst_string ){    unsigned char key_str[100];    unsigned char src_str[100];    unsigned char dst_str[100];    unsigned char output[100];    mbedtls_des_context ctx;    memset(key_str, 0x00, 100);    memset(src_str, 0x00, 100);    memset(dst_str, 0x00, 100);    memset(output, 0x00, 100);    mbedtls_des_init( &ctx );    unhexify( key_str, hex_key_string );    unhexify( src_str, hex_src_string );    mbedtls_des_setkey_dec( &ctx, key_str );    TEST_ASSERT( mbedtls_des_crypt_ecb( &ctx, src_str, output ) == 0 );    hexify( dst_str, output, 8 );    TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );exit:    mbedtls_des_free( &ctx );}/* END_CASE *//* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */void des_encrypt_cbc( char *hex_key_string, char *hex_iv_string,                      char *hex_src_string, char *hex_dst_string, int cbc_result ){    unsigned char key_str[100];    unsigned char iv_str[100];    unsigned char src_str[100];    unsigned char dst_str[100];    unsigned char output[100];    mbedtls_des_context ctx;    int src_len;    memset(key_str, 0x00, 100);    memset(iv_str, 0x00, 100);    memset(src_str, 0x00, 100);    memset(dst_str, 0x00, 100);    memset(output, 0x00, 100);    mbedtls_des_init( &ctx );    unhexify( key_str, hex_key_string );    unhexify( iv_str, hex_iv_string );    src_len = unhexify( src_str, hex_src_string );    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 );    if( cbc_result == 0 )    {        hexify( dst_str, output, src_len );        TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );    }exit:    mbedtls_des_free( &ctx );}/* END_CASE *//* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */void des_decrypt_cbc( char *hex_key_string, char *hex_iv_string,                      char *hex_src_string, char *hex_dst_string, int cbc_result ){    unsigned char key_str[100];    unsigned char iv_str[100];    unsigned char src_str[100];    unsigned char dst_str[100];    unsigned char output[100];    mbedtls_des_context ctx;    int src_len;    memset(key_str, 0x00, 100);    memset(iv_str, 0x00, 100);    memset(src_str, 0x00, 100);    memset(dst_str, 0x00, 100);    memset(output, 0x00, 100);    mbedtls_des_init( &ctx );    unhexify( key_str, hex_key_string );    unhexify( iv_str, hex_iv_string );    src_len = unhexify( src_str, hex_src_string );    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 );    if( cbc_result == 0 )    {        hexify( dst_str, output, src_len );        TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );    }exit:    mbedtls_des_free( &ctx );}/* END_CASE *//* BEGIN_CASE */void des3_encrypt_ecb( int key_count, char *hex_key_string,                       char *hex_src_string, char *hex_dst_string ){    unsigned char key_str[100];    unsigned char src_str[100];    unsigned char dst_str[100];    unsigned char output[100];    mbedtls_des3_context ctx;    memset(key_str, 0x00, 100);    memset(src_str, 0x00, 100);    memset(dst_str, 0x00, 100);    memset(output, 0x00, 100);    mbedtls_des3_init( &ctx );    unhexify( key_str, hex_key_string );    unhexify( src_str, hex_src_string );    if( key_count == 2 )        mbedtls_des3_set2key_enc( &ctx, key_str );    else if( key_count == 3 )        mbedtls_des3_set3key_enc( &ctx, key_str );    else        TEST_ASSERT( 0 );    TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str, output ) == 0 );    hexify( dst_str, output, 8 );    TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );exit:    mbedtls_des3_free( &ctx );}/* END_CASE *//* BEGIN_CASE */void des3_decrypt_ecb( int key_count, char *hex_key_string,                       char *hex_src_string, char *hex_dst_string ){    unsigned char key_str[100];    unsigned char src_str[100];    unsigned char dst_str[100];    unsigned char output[100];    mbedtls_des3_context ctx;    memset(key_str, 0x00, 100);    memset(src_str, 0x00, 100);    memset(dst_str, 0x00, 100);    memset(output, 0x00, 100);    mbedtls_des3_init( &ctx );    unhexify( key_str, hex_key_string );    unhexify( src_str, hex_src_string );    if( key_count == 2 )        mbedtls_des3_set2key_dec( &ctx, key_str );    else if( key_count == 3 )        mbedtls_des3_set3key_dec( &ctx, key_str );    else        TEST_ASSERT( 0 );    TEST_ASSERT( mbedtls_des3_crypt_ecb( &ctx, src_str, output ) == 0 );    hexify( dst_str, output, 8 );    TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );exit:    mbedtls_des3_free( &ctx );}/* END_CASE *//* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */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 ){    unsigned char key_str[100];    unsigned char iv_str[100];    unsigned char src_str[100];    unsigned char dst_str[100];    unsigned char output[100];    mbedtls_des3_context ctx;    int src_len;    memset(key_str, 0x00, 100);    memset(iv_str, 0x00, 100);    memset(src_str, 0x00, 100);    memset(dst_str, 0x00, 100);    memset(output, 0x00, 100);    mbedtls_des3_init( &ctx );    unhexify( key_str, hex_key_string );    unhexify( iv_str, hex_iv_string );    src_len = unhexify( src_str, hex_src_string );    if( key_count == 2 )        mbedtls_des3_set2key_enc( &ctx, key_str );    else if( key_count == 3 )        mbedtls_des3_set3key_enc( &ctx, key_str );    else        TEST_ASSERT( 0 );    TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_ENCRYPT, src_len, iv_str, src_str, output ) == cbc_result );    if( cbc_result == 0 )    {        hexify( dst_str, output, src_len );        TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );    }exit:    mbedtls_des3_free( &ctx );}/* END_CASE *//* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */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 ){    unsigned char key_str[100];    unsigned char iv_str[100];    unsigned char src_str[100];    unsigned char dst_str[100];    unsigned char output[100];    mbedtls_des3_context ctx;    int src_len;    memset(key_str, 0x00, 100);    memset(iv_str, 0x00, 100);    memset(src_str, 0x00, 100);    memset(dst_str, 0x00, 100);    memset(output, 0x00, 100);    mbedtls_des3_init( &ctx );    unhexify( key_str, hex_key_string );    unhexify( iv_str, hex_iv_string );    src_len = unhexify( src_str, hex_src_string );    if( key_count == 2 )        mbedtls_des3_set2key_dec( &ctx, key_str );    else if( key_count == 3 )        mbedtls_des3_set3key_dec( &ctx, key_str );    else        TEST_ASSERT( 0 );    TEST_ASSERT( mbedtls_des3_crypt_cbc( &ctx, MBEDTLS_DES_DECRYPT, src_len, iv_str, src_str, output ) == cbc_result );    if( cbc_result == 0 )    {        hexify( dst_str, output, src_len );        TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );    }exit:    mbedtls_des3_free( &ctx );}/* END_CASE *//* BEGIN_CASE */void des_key_parity_run(){    int i, j, cnt;    unsigned char key[MBEDTLS_DES_KEY_SIZE];    unsigned int parity;    memset( key, 0, MBEDTLS_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        //        mbedtls_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( mbedtls_des_key_check_key_parity( key ) == 0 );    }}/* END_CASE *//* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */void des_selftest(){    TEST_ASSERT( mbedtls_des_self_test( 1 ) == 0 );}/* END_CASE */
 |