How to implement Triple DES crypto in C using OpenSSL library

anonim picture anonim · Sep 27, 2013 · Viewed 9.4k times · Source

I'm trying to implement Triple DES encryption in C using OpenSSL library but I am not such professional in cryptography. I've found a useful sample code here for DES ECB crypto but I could not find an example code on how to implement 3DES and most of web resources just describe how to use OpenSSL as a tool.

I've implemented DES ECB for a specific purpose as follows

typedef struct
{
    size_t size;
    unsigned char*  data;
} byte_array, *byte_array_ptr;

for encryption

byte_array_ptr des_encrypt_to_pin_block(byte_array_ptr key_bytes, byte_array_ptr xor_bytes)
{
    if ((key_bytes->size != 8) || (xor_bytes->size != 8))
        return NULL;

    DES_key_schedule    schedule;
    const_DES_cblock    key_data;
    const_DES_cblock    xor_data;
    DES_cblock      buffer;

    memcpy(&key_data, key_bytes->data, key_bytes->size);
    memcpy(&xor_data, xor_bytes->data, xor_bytes->size);

    if (DES_set_key(&key_data, &schedule) < 0)
    {
        printf("ERROR: %s", "DES Set Key Error!");
        return NULL;
    }

    DES_ecb_encrypt(&xor_data, &buffer, &schedule, DES_ENCRYPT);

    byte_array_ptr pin_block;
    pin_block = (byte_array_ptr)malloc(sizeof(size_t) + 8);
    pin_block->size = 8;
    pin_block->data = (unsigned char *)malloc(pin_block->size);

    memcpy(pin_block->data, &buffer, pin_block->size);

    return pin_block;
}

and the decryption

byte_array_ptr des_decrypt_to_xor_bytes(byte_array_ptr key_bytes, byte_array_ptr pin_block)
{
    if ((key_bytes->size != 8) || (pin_block->size != 8))
        return NULL;

    DES_key_schedule    schedule;
    const_DES_cblock    key_data;
    const_DES_cblock    pin_data;
    DES_cblock      buffer;

    memcpy(&key_data, key_bytes->data, key_bytes->size);
    memcpy(&pin_data, pin_block->data, pin_block->size);

    if (DES_set_key(&key_data, &schedule) < 0)
    {
        printf("ERROR: %s", "DES Set Key Error!");
        return NULL;
    }

    DES_ecb_encrypt(&pin_data, &buffer, &schedule, DES_DECRYPT);

    byte_array_ptr xor_bytes;
    xor_bytes = (byte_array_ptr)malloc(sizeof(size_t) + 8);
    xor_bytes->size = 8;
    xor_bytes->data = (unsigned char *)malloc(xor_bytes->size);

    memcpy(xor_bytes->data, &buffer, xor_bytes->size);

    return xor_bytes;
}

but I have no idea how to do it for 3DES.

Any idea?

Answer

SkySurfer picture SkySurfer · Sep 30, 2013

OpenSSL provides a set of functions for Triple DES in EDE mode(Encrypt using key #1, Decrypt using key #2, Encrypt using key #3) for all encryption schemes. Also its common situation when key #1 equals key #3, there are functions for this case either.

So if you have all three keys the functions are:

DES_ecb3_encrypt()
DES_ede3_cbc_encrypt()
DES_ede3_ofb_encrypt()
DES_ede3_cfb_encrypt()

If you have only two keys the functions are:

DES_ecb2_encrypt()
DES_ede2_cbc_encrypt()
DES_ede2_ofb_encrypt()
DES_ede2_cfb_encrypt()