/*
- engineTester.c
- Created on: Sep 29, 2015
-
Author: oezgan
*/
/openssl/
#include <openssl/engine.h>
#include <openssl/crypto.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <stdio.h>
#include <string.h>
int main(int argc, const char* argv[] ) {
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
ENGINE_load_dynamic();
ENGINE *oezgan_engine = ENGINE_by_id("oezgan");
if( oezgan_engine == NULL )
{
printf("Could not Load Oezgan Engine!\n");
exit(1);
}
printf("Oezgan Engine successfully loaded\n");
int init_res = ENGINE_init(oezgan_engine);
printf("Engine name: %s init result : %d \n",ENGINE_get_name(oezgan_engine), init_res);
ENGINE_set_default_RAND(oezgan_engine);
unsigned char * rand_buf= new unsigned char[5];
int err = RAND_bytes(rand_buf,5);
for(int i= 0; i < 5; i++) {
printf("%x",rand_buf[i]);
}
printf("\n");
char * str = "Fraunhofer FKIE Wachtberg!";
int str_len = 26;
int er = ENGINE_set_default_digests(oezgan_engine);
printf("ENGINE SETTING DEFAULT DIGESTS %d\n",er);
unsigned char * digest = new unsigned char[32];
unsigned int digestSize = -1;
EVP_MD_CTX *evp_ctx;
evp_ctx = EVP_MD_CTX_create();
er = EVP_DigestInit_ex(evp_ctx, EVP_sha256(),oezgan_engine);
printf("Digest INIT %d\n",er);
er = EVP_DigestUpdate(evp_ctx, (unsigned char*)str, str_len);
printf("Digest Update %d\n",er);
er = EVP_DigestFinal(evp_ctx, digest, &digestSize);
printf("Digest Final %d Digest size:%d\n",er,digestSize);
for(int i= 0; i< digestSize; i++) {
printf("%x", digest[i]);
}
printf("\n");
EVP_MD_CTX_destroy(evp_ctx);
er = ENGINE_set_default_ECDH(oezgan_engine);
printf("ENGINE SETTING DEFAULT ECDH %d\N ",er);
FILE* fp = fopen("./ownPrivkey.pem", "r");
if (fp == NULL) {
printf( "Could not open private key file\N");
exit(1);
}
EVP_PKEY *privateKey;
privateKey= PEM_read_PrivateKey(fp, NULL, 0, NULL);
if ((privateKey) == NULL) {
printf("Could not extract private key from file\N");
exit(1);
}
fclose(fp);
EC_KEY *eckey;
eckey = EC_KEY_new();
ECDSA_SIG * ecdsa_sig;
ecdsa_sig = ECDSA_SIG_new();
eckey = EVP_PKEY_get1_EC_KEY(privateKey);
EC_GROUP *ec_group;
ec_group = EC_GROUP_new_by_curve_name(NID_brainpoolP384r1);
const EC_POINT* pub_key;
pub_key = EC_KEY_get0_public_key(eckey);
unsigned char agreed_value[200];
EC_KEY *ecdh;
ecdh = EC_KEY_new();
EC_KEY_set_group(ecdh, ec_group);
er = EC_KEY_set_private_key(ecdh, EC_KEY_get0_private_key(eckey));
int agreed_value_len = ECDH_compute_key(agreed_value, 200,pub_key, ecdh, NULL);
printf("Oezgan engine Agreed Value: %d\n",agreed_value_len);
for(int i= 0; i < agreed_value_len; i++) {
printf("%x", agreed_value[i]);
}
printf("\n");
er = ENGINE_set_default_ECDSA(oezgan_engine);
printf("\nENGINE SETTING DEFAULT ECDSA:%d\n",er);
unsigned char *sig = new unsigned char[256];
unsigned int sigsize;
ECDSA_sign(0,digest,digestSize,sig,&sigsize,eckey);
printf("Signature size:%d \n",sigsize);
for(int i=0; i <sigsize;i++) {
printf("%x",sig[i]);
}
printf("\n");
printf("Now verifying!\n");
BIO* bio_in;
bio_in = BIO_new_file("./ownCert.pem", "r");
if (bio_in == NULL) {
printf("could not read public key file\n");
exit(1);
}
X509 *certificate;
certificate = X509_new();
if (PEM_read_bio_X509(bio_in, &certificate, 0, NULL) == NULL) {
printf("could not read certificate from public key file\n");
exit(1);
}
EVP_PKEY *pubKey;
pubKey = X509_get_pubkey(certificate);
EC_KEY* eckey_pub;
eckey_pub = EVP_PKEY_get1_EC_KEY(pubKey);
int result = ECDSA_do_verify(digest, digestSize,
ecdsa_sig, eckey_pub);
printf("Verify result %d\n", result);
EC_KEY_free(eckey);
EC_GROUP_free(ec_group);
EC_KEY_free(ecdh);
X509_free(certificate);
BIO_free_all(bio_in);
free(rand_buf);
return 0;
}