/* 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/err.h>

#include <openssl/rsa.h>
#include <openssl/evp.h>

/* encrypt: binary plain + binary key -> binary cipher */
/* decrypt: binary cipher + binary key -> binary plain */
void encrypt(const unsigned char *inbuf, int inbuflen, RSA *key, unsigned char *outbuf, int *outbuflen)
{
    *outbuflen = RSA_private_encrypt(inbuflen, inbuf, outbuf, key, RSA_PKCS1_PADDING);
}

void decrypt(const unsigned char *inbuf, int inbuflen, RSA *key, unsigned char *outbuf, int *outbuflen)
{
    *outbuflen = RSA_public_decrypt(inbuflen, inbuf, outbuf, key, RSA_PKCS1_PADDING);
}

/* 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;
}

/* encrypt: text plain + text key -> Base64 text cipher */
/* decrypt: Base64 text cipher + text key -> text plain */
void encrypt_encode(const char *plain, const char *privkey, int privkeylen, char *cipher)
{
    RSA *key;
    unsigned char *temp;
    int templen, cipherlen;
    key = d2i_RSAPrivateKey(NULL, (const unsigned char **)&privkey, privkeylen);
    temp = malloc(RSA_size(key));
    encrypt((unsigned char *)plain, strlen(plain), key, temp, &templen);
    encode(temp, templen, (unsigned char *)cipher, &cipherlen);
    free(temp);
}

void decode_decrypt(const char *cipher, const char *pubkey, int pubkeylen, char *plain)
{
    RSA *key;
    unsigned char *temp;
    int templen, plainlen;
    key = d2i_RSAPublicKey(NULL, (const unsigned char **)&pubkey, pubkeylen);
    temp = malloc(strlen(cipher));
    decode((unsigned char *)cipher, strlen(cipher), temp, &templen);
    decrypt(temp, templen, key, (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.";
 
void test(char *label, int bits)
{
    RSA *keypair;
    char *privkey;
    char *pubkey;
    char *temp;
    int privkeylen;
    int pubkeylen;
    char cipher[10000];
    char plain[10000];
    printf("%s:\n", label);
    /* generate keys */
    keypair = RSA_generate_key(bits, RSA_F4, NULL, NULL);
    temp = malloc(10000);
    privkey = temp;
    privkeylen = i2d_RSAPrivateKey(keypair, (unsigned char **)&temp);
    temp = malloc(10000);
    pubkey = temp;
    pubkeylen = i2d_RSAPublicKey(keypair, (unsigned char **)&temp);
    /* real test */
    printf("  %s\n", TEXT);
    encrypt_encode(TEXT, privkey, privkeylen, cipher);
    printf("  %s\n", cipher);
    decode_decrypt(cipher, pubkey, pubkeylen, plain);
    printf("  %s\n", plain);
    free(privkey);
    free(pubkey);
}

int main()
{
    test("RSA 2048 bit", 2048);
    test("RSA 3072 bit", 3072);
    return 0;
}
