bigpoint_crypt.c 3.46 KB
#include <stdarg.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <mcrypt.h>
#include <mhash.h>

#include "bigpoint_crypt.h"


static
void
__construct(struct BIGPOINT_CRYPT * _this, va_list * params)
{
    _this->algorithm = va_arg(* params, const char * const);
    _this->mode      = va_arg(* params, const char * const);

    _this->mcrypt = mcrypt_module_open(
            (char *)_this->algorithm,
            NULL,
            (char *)_this->mode,
            NULL);

    _this->ivsize  = mcrypt_enc_get_iv_size(_this->mcrypt);
    _this->keysize = mcrypt_enc_get_key_size(_this->mcrypt);
}

static
void
__destruct(struct BIGPOINT_CRYPT * _this)
{
    if (_this->iv) {
        free(_this->iv);
    }

    mcrypt_module_close(_this->mcrypt);
}

static const
struct BIGPOINT_CCLASS _bigpoint_crypt = {
    sizeof(struct BIGPOINT_CRYPT),
    (ctor)__construct,
    NULL,
    (dtor)__destruct,
    NULL
};

const struct BIGPOINT_CCLASS * const BIGPOINT_CRYPT = &_bigpoint_crypt;

static
void
mcrypt_close(MCRYPT * mcrypt)
{
    mcrypt_free(*mcrypt);
    *mcrypt = NULL;
}

void *
bigpoint_crypt_createIv(struct BIGPOINT_CRYPT * _this)
{
    int    urandom;
    size_t rsize  = 0;
    void * iv = NULL;

    iv = calloc(_this->ivsize, sizeof(char));

    urandom = open("/dev/urandom", O_RDONLY);
    rsize   = read(urandom, iv, _this->ivsize);

    if (_this->ivsize != rsize) {
        free(iv);
        iv = NULL;
    }

    return iv;
}

static
void *
createKey(struct BIGPOINT_CRYPT * _this, const char * const password)
{
    void * key = NULL;

    key = calloc(_this->keysize, sizeof(char));

    mhash_keygen(
            KEYGEN_MCRYPT,
            MHASH_SHA256,
            mhash_keygen_count(),
            key,
            _this->keysize,
            NULL,
            0,
            (char *)password, // @TODO: bad karma...now this might change password.
            strlen(password));

    return key;
}

void *
bigpoint_crypt_encrypt(
        struct BIGPOINT_CRYPT * _this,
        const void * const data,
        const char * const password,
        size_t * length)
{
    char * encrypted;
    void * iv;
    void * key;

    key = createKey(_this, password);
    if(_this->iv) {
        iv = _this->iv;
    } else {
        iv = bigpoint_crypt_createIv(_this);
    }

    mcrypt_generic_init(_this->mcrypt, key, _this->keysize, iv);

    encrypted = calloc(_this->ivsize + *length, sizeof(char));
    memcpy(encrypted, iv, _this->ivsize);
    memcpy(encrypted + _this->ivsize, data, *length);

    mcrypt_generic(_this->mcrypt, encrypted + _this->ivsize, *length);
    mcrypt_generic_deinit(_this->mcrypt);
    *length += _this->ivsize;

    free(key);
    if (_this->iv != iv) {
        free(iv);
    }

    return encrypted;
}

void *
bigpoint_crypt_decrypt(
        struct BIGPOINT_CRYPT * _this,
        const void * const data,
        const char * const password,
        size_t * length)
{
    char * decrypted;
    void * iv;
    void * key;

    key = createKey(_this, password);
    iv  = calloc(_this->ivsize, sizeof(char));
    memcpy(iv, data, _this->ivsize);

    mcrypt_generic_init(_this->mcrypt, key, _this->keysize, iv);

    *length -= _this->ivsize;
    decrypted = calloc(*length, sizeof(char));
    memcpy(decrypted, data + _this->ivsize, *length);

    mdecrypt_generic(_this->mcrypt, decrypted, *length);
    mcrypt_generic_deinit(_this->mcrypt);

    free(key);
    free(iv);

    return decrypted;
}

// vim: set et ts=4 sw=4: