/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>
#include <string.h>

#include <openssl/sha.h>
#include <openssl/evp.h>
#include <openssl/rand.h>

/* generate random IV */
static void gen_iv(int size, unsigned char *iv)
{
    RAND_bytes(iv, size);
}

/* encrypt: binary plain + binary key -> binary cipher */
/* decrypt: binary cipher + binary key -> binary plain */
void encrypt(const EVP_CIPHER *alg, const unsigned char *inbuf, int inbuflen, unsigned char *realkey, int keysiz, int blksiz, unsigned char *outbuf, int *outbuflen)
{
    EVP_CIPHER_CTX ctx;
    unsigned char iv[blksiz];
    int len;
    gen_iv(blksiz, iv);
    memcpy(outbuf, iv, blksiz);
    EVP_EncryptInit(&ctx, alg, realkey, iv);
    EVP_EncryptUpdate(&ctx, outbuf + blksiz, &len, inbuf, inbuflen);
    *outbuflen = blksiz + len;
    EVP_EncryptFinal(&ctx, outbuf + blksiz + len, &len);
    *outbuflen += len;
    EVP_CIPHER_CTX_free(&ctx);
}

void decrypt(const EVP_CIPHER *alg, const unsigned char *inbuf, int inbuflen, unsigned char *realkey, int keysiz, int blksiz, unsigned char *outbuf, int *outbuflen)
{
    EVP_CIPHER_CTX ctx;
    unsigned char iv[blksiz];
    int len;
    memcpy(iv, inbuf, blksiz);
    EVP_DecryptInit(&ctx, alg, realkey, iv);
    EVP_DecryptUpdate(&ctx, outbuf, &len, inbuf + blksiz, inbuflen - blksiz);
    *outbuflen = len;
    EVP_DecryptFinal(&ctx, outbuf + len, &len);
    *outbuflen += len;
    EVP_CIPHER_CTX_free(&ctx);
}

/* Base64 encode: binary -> text */
/* Base64 decode: text -> binary */
void encode(const unsigned char *inbuf, int inbuflen, unsigned char *outbuf, int *outbuflen)
{
    EVP_ENCODE_CTX ctx;
    int len;
    EVP_EncodeInit(&ctx);
    EVP_EncodeUpdate(&ctx, outbuf, &len, inbuf, inbuflen);
    *outbuflen = len;
    EVP_EncodeFinal(&ctx, outbuf + len, &len);
    *outbuflen += len;
    if(outbuf[*outbuflen - 1] == '\n') *outbuflen -= 1;
    outbuf[*outbuflen] = '\0'; 
}

void decode(const unsigned char *inbuf, int inbuflen, unsigned char *outbuf, int *outbuflen)
{
    EVP_ENCODE_CTX ctx;
    int len;
    EVP_DecodeInit(&ctx);
    EVP_DecodeUpdate(&ctx, outbuf, &len, inbuf, inbuflen);
    *outbuflen = len;
    EVP_DecodeFinal(&ctx, outbuf + len, &len);
    *outbuflen += len;
}

/* convert text key to binary key */
static void gen_key(const char *key, unsigned char *realkey) 
{
    SHA256_CTX ctx;
    SHA256_Init(&ctx);
    SHA256_Update(&ctx, key, strlen(key));
    SHA256_Final(realkey, &ctx);
}

/* encrypt: text plain + text key -> Base64 text cipher */
/* decrypt: Base64 text cipher + text key -> text plain */
void encrypt_encode(const EVP_CIPHER *alg, const char *plain, const char *key, int keysiz, int blksiz, char *cipher)
{
    unsigned char realkey[keysiz];
    unsigned char *temp;
    int templen, cipherlen;
    gen_key(key, realkey);
    temp = malloc(blksiz + strlen(plain) + blksiz);
    encrypt(alg, (unsigned char *)plain, strlen(plain), realkey, keysiz, blksiz, temp, &templen);
    encode(temp, templen, (unsigned char *)cipher, &cipherlen);
    free(temp);
}

void decode_decrypt(const EVP_CIPHER *alg, const char *cipher, const char *key, int keysiz, int blksiz, char *plain)
{
    unsigned char realkey[keysiz];
    unsigned char *temp;
    int templen, plainlen;
    gen_key(key, realkey);
    temp = malloc(strlen(cipher));
    decode((unsigned char *)cipher, strlen(cipher), temp, &templen);
    decrypt(alg, temp, templen, realkey, keysiz, blksiz, (unsigned char *)plain, &plainlen);
    plain[plainlen] = '\0';
    free(temp);
}

/* test */
static const char *TEXT = "This is some random text to be used to test encryption.";
static const char *KEY = "This is a very long and super secret key that should really be like 4 times as long as keysize";
 
void test(char *label, const EVP_CIPHER *alg, int keysize, int blksiz)
{
    char cipher[1000];
    char plain[1000];
    printf("%s:\n", label);
    printf("  %s\n", TEXT);
    encrypt_encode(alg, TEXT, KEY, keysize, blksiz, cipher);
    printf("  %s\n", cipher);
    decode_decrypt(alg, cipher, KEY, keysize, blksiz, plain);
    printf("  %s\n", plain);
}

void test2()
{
    char temp[1000];
    decode_decrypt(EVP_aes_128_cbc(), "Xc252As4sZuj12tLJtDk2l60tHxu1hk4FTB0l9IK/q96cWd+Uop4lXcId1hGEmm0SN1Xjua4YqVlr3rvT+Mw4hStrnz3h8TEfdENfUMqLMY=", KEY, 128, 16, temp);
    if(strcmp(temp, TEXT) != 0)
    {
        printf("Ooops\n");
    }
}

int main()
{
    test("AES 128 bit", EVP_aes_128_cbc(), 16, 16);
    test("AES 192 bit", EVP_aes_192_cbc(), 24, 16);
    test("AES 256 bit", EVP_aes_256_cbc(), 32, 16);
    test("3DES 168 bit", EVP_des_ede3_cbc(), 24, 8);
    test("IDEA 128 bit", EVP_idea_cbc(), 16, 8);
    test("Blowfish 256 bit", EVP_bf_cbc(), 32, 8);
    test("CAST5 128 bit", EVP_cast5_cbc(), 16, 8);
    test2();
    return 0;
}
