QuickCrypt Library API

The following sample demonstrates how to use the GOST and CTR (Counter mode) API to perform encryption/decryption of a part of memory buffer in Counter mode of operation.

#include <stdio.h>
#include <QuickCrypt.h>

/* performs GOST encryption/decryption of of a memory buffer portion in Counter mode  */
void EncryptDecryptBufferPortion( unsigned char* buff, const unsigned int buffsize, const unsigned char* iv, const unsigned char* key )
{
    unsigned char context[ SLC_GOST_CONTEXTSIZE ];
    unsigned int pos = buffsize / 2;

    /***** Initialize context ***************************************/

    SL_GOST_Init( context, SLC_ENCRYPT, key, SLC_GOST_DEFAULTKEYSIZE );
    SL_CTR_Init( context, iv );

    /***** Encrypt/Decrypt the second half of the memory buffer *****/

    SL_CTR_Seek( context, pos );
    SL_CTR_Process( context, buff + pos, buff + pos, buffsize - pos ); /* in-place encryption/decryption */
}

void ShowBuffer( const unsigned char* buff, const unsigned int buffsize );

int main()
{
    /***** Define key & iv *******************/

    char key[SLC_GOST_DEFAULTKEYSIZE] = 
    {
        's', 'e', 'c', 'r', 'e', 't', 'l', 'y',
        '9', '6', '7', '1', '2', '9', '2', '7',
        '0', '1', '2', '2', '0', '0', '5', '7',
        '*', '-', '*', '-', '*', '-', '*', '-'
    };

    unsigned char iv[SLC_GOST_BLOCKSIZE] = 
    {
        0x41, 0x3E, 0xF0, 0xA1, 0xC6, 0x11, 0xE5, 0x50
    };

    unsigned char buff[] = 
    {
        0xED, 0x39, 0x08, 0x1B, 0xCA, 0xBE, 0x94, 0xCC, 
        0xA2, 0x04, 0x5F, 0x0C, 0x94, 0xCC, 0xAD, 0xE6, 
        0x24, 0xCA, 0xFE, 0x33, 0xF9, 0x8B, 0x29, 0x1F, 
        0x8B, 0xE9, 0x63, 0x1A, 0x96, 0xD9, 0x75, 0xDA
    };

    unsigned int buffsize = sizeof( buff ) / sizeof( unsigned char );

    printf( "Initial memory buffer:\n" );
    ShowBuffer( buff, buffsize );

    /***** Encrypt memory buffer *************/
    
    EncryptDecryptBufferPortion( buff, buffsize, iv, (const unsigned char*)key );

    printf( "Encrypted memory buffer:\n" );
    ShowBuffer( buff, buffsize );

    /***** Decrypt memory buffer *************/

    EncryptDecryptBufferPortion( buff, buffsize, iv, (const unsigned char*)key );

    printf( "Decrypted memory buffer:\n" );
    ShowBuffer( buff, buffsize );

    return 0;
}

void ShowBuffer( const unsigned char* buff, const unsigned int buffsize )
{
    unsigned int i = 0;

    for( ; i < buffsize; ++i )
    {
        printf( "%.2x ", (int)buff[ i ] );

        if( ( i + 1 ) % SLC_GOST_BLOCKSIZE == 0 )
            printf( "\n" );
    }

    printf( "\n" );
}

 


 

 

Send Feedback to SlavaSoft Inc. Tell a friend about QuickCrypt Library