/**
 * @brief	RSA test 14.
 * @file	rsa_test14.c
 */

/*
 * Compilation:
 * $ gcc -g -Wall -pedantic -o rsa_test14 rsa_test14.c -lcrypto -lssl
 */

#include <stdio.h>
#include <string.h>

#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/bn.h>

int print_hex(unsigned char *buf, int len)
{
	int i;
	int n;

	printf(" ");

	for (i = 0, n = 0; i < len; i++) {
		if (n > 7) {
			printf("\n ");
			n = 0;
		}
		printf("0x%02x ", buf[i]);
		n++;
	}

	printf("\n");

	return (0);
}

int log_err(void)
{
	char buf[256];
	u_long err;

	while ((err = ERR_get_error()) != 0) {
		ERR_error_string_n(err, buf, sizeof(buf));
		printf("*** %s\n", buf);
	}

	return (0);
}

int main()
{
	RSA *rsa_priv;
	RSA *rsa_pub;

	unsigned char enc_bin[1024];
	int enc_len;
	unsigned char dec_bin[1024];
	int dec_len;

	char N[] = { "CB2DA098676495E3BEE8807573B658A3" };
	char E[] = { "010001" };
	char D[] = { "AE6BB41341A75AF849C19ADD86E4C751" };
	char P[] = { "ECB6D81B9A287E2B" };
	char Q[] = { "DBBB525D666C4B69" };
	char DMP1[] = { "3381857C5C0ABB89" };
	char DMQ1[] = { "C11B735B5A343A61" };
	char IQMP[] = { "DECAD6D4EF77DE64" };

	char msg[] = { "xyz" };

	SSL_load_error_strings();
	SSLeay_add_ssl_algorithms();

	RAND_load_file("/dev/urandom", 1024);

	if ((rsa_priv = RSA_new()) == NULL) {
		goto err;
	}

	if ((rsa_pub = RSA_new()) == NULL) {
		goto err;
	}

	/* load public key */
	printf("public key parameters:\n");

	if (!BN_hex2bn(&rsa_pub->n, N)) {
		goto err;
	}
	printf(" N: %s\n", N);
	printf(" n: %s\n", BN_bn2hex(rsa_pub->n));

	if (!BN_hex2bn(&rsa_pub->e, E)) {
		goto err;
	}
	printf(" E: %s\n", E);
	printf(" e: %s\n", BN_bn2hex(rsa_pub->e));

	printf("public key size : %d bits\n", RSA_size(rsa_pub) * 8);

	/* load private key */
	printf("private key parameters:\n");

	if (!BN_hex2bn(&rsa_priv->n, N)) {
		goto err;
	}
	printf(" N: %s\n", N);
	printf(" n: %s\n", BN_bn2hex(rsa_priv->n));
/*
	if (!BN_hex2bn(&rsa_priv->d, D)) {
		goto err;
	}
	printf(" D: %s\n", D);
	printf(" d: %s\n", BN_bn2hex(rsa_priv->d));

	if (!BN_hex2bn(&rsa_priv->e, E)) {
		goto err;
	}
	printf(" E: %s\n", E);
	printf(" e: %s\n", BN_bn2hex(rsa_priv->e));
*/
	if (!BN_hex2bn(&rsa_priv->p, P)) {
		goto err;
	}
	printf(" P: %s\n", P);
	printf(" p: %s\n", BN_bn2hex(rsa_priv->p));

	if (!BN_hex2bn(&rsa_priv->q, Q)) {
		goto err;
	}
	printf(" Q: %s\n", Q);
	printf(" q: %s\n", BN_bn2hex(rsa_priv->q));

	if (!BN_hex2bn(&rsa_priv->dmp1, DMP1)) {
		goto err;
	}
	printf(" DMP1: %s\n", DMP1);
	printf(" dmp1: %s\n", BN_bn2hex(rsa_priv->dmp1));

	if (!BN_hex2bn(&rsa_priv->dmq1, DMQ1)) {
		goto err;
	}
	printf(" DMQ1: %s\n", DMQ1);
	printf(" dmq1: %s\n", BN_bn2hex(rsa_priv->dmq1));

	if (!BN_hex2bn(&rsa_priv->iqmp, IQMP)) {
		goto err;
	}
	printf(" IQMP: %s\n", IQMP);
	printf(" iqmp: %s\n", BN_bn2hex(rsa_priv->iqmp));

	printf("private key size: %d bits\n", RSA_size(rsa_priv) * 8);

	printf("clear data:\n");
	print_hex((unsigned char *) msg, strlen(msg));

	/* encrypt */
	if ((enc_len =
		 RSA_public_encrypt(strlen(msg), msg, enc_bin, rsa_pub, RSA_PKCS1_PADDING)) < 0) {
		goto err;
	}

	printf("encrypted data:\n");
	print_hex(enc_bin, enc_len);

	/* decrypt */
	RSA_blinding_off(rsa_priv);
	if ((dec_len =
		 RSA_private_decrypt(enc_len, enc_bin, dec_bin, rsa_priv, RSA_PKCS1_PADDING)) < 0) {
		goto err;
	}

	printf("decrypted data:\n");
	print_hex(dec_bin, dec_len);

	return (0);

  err:
	log_err();
	return (1);
}
