diff --git a/rsa/Makefile b/rsa/Makefile new file mode 100644 index 0000000..c7203cf --- /dev/null +++ b/rsa/Makefile @@ -0,0 +1,13 @@ +all: + gcc genrsa.c -L/opt/openssl/lib -lcrypto -o genrsa + gcc rsa.c -L/opt/openssl/lib -lcrypto -o rsa + gcc rsasign.c -L/opt/openssl/lib -lcrypto -o rsasign + gcc rsaverify.c -L/opt/openssl/lib -lcrypto -o rsaverify + + +clean: + rm genrsa -rf + rm rsa -rf + rm rsasign -rf + rm rsaverify -rf + diff --git a/rsa/genrsa.c b/rsa/genrsa.c new file mode 100644 index 0000000..177060f --- /dev/null +++ b/rsa/genrsa.c @@ -0,0 +1,124 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +char *prtkeyfile=NULL; +BIO *bio_log=NULL; +unsigned int rsabits = 2048; + +void usage(void){ + printf("\n rsagen -s -o "); + printf("\n rsagen -h -- For help"); + printf("\n"); +} + + +void generate_rsa(void){ + BIGNUM *bn=NULL; + RSA *rsa=NULL; + EVP_PKEY *pkey=NULL; + BIO *prtbio; + + if((bn = BN_new()) == NULL) { + BIO_printf(bio_log, "Failed to allocate BN\n"); + goto end; + } + BN_set_word(bn, RSA_F4); + + rsa = RSA_new(); + RSA_generate_key_ex(rsa, rsabits, bn, NULL); + + if((pkey = EVP_PKEY_new()) == NULL) { + BIO_printf(bio_log, "EVP_PKEY_New failed\n"); + goto end; + } + EVP_PKEY_assign_RSA(pkey, rsa); + + if (prtkeyfile) { + if ((prtbio = BIO_new_file(prtkeyfile, "w")) == NULL) { + BIO_printf(bio_log, "\n BIO_new_file %s failed\n", prtkeyfile); + goto end; + } + } + else { + if ((prtbio = BIO_new_fp(stdout, BIO_NOCLOSE)) == NULL) { + BIO_printf(bio_log, "\n BIO_new_file failed\n"); + goto end; + } + } + + if (!PEM_write_bio_PrivateKey(prtbio, pkey, NULL, NULL, 0, NULL, NULL)) { + BIO_printf(bio_log, "PEM_write_bio_PrivateKey failed\n"); + goto end; + } + + end: + if (bn) { + BN_free(bn); + } + + if(pkey) { + EVP_PKEY_free(pkey); + } + + + return; +} + +int main(int argc, char **argv) { + + unsigned int opt; + + while( (opt=getopt(argc, argv, "o:s:h")) != -1) { + switch(opt) { + case 's': + rsabits = atoi(optarg); + break; + + case 'o': + if((prtkeyfile = (char *) OPENSSL_malloc(strlen(optarg)+1)) == NULL) { + printf("\n %s: OPENSSL_malloc failed. \n", optarg); + } + + memset(prtkeyfile, 0, strlen(optarg)+1); + memcpy(prtkeyfile, optarg, strlen(optarg)); + break; + + case 'h': + usage(); + goto end; + break; + + case '?': + printf("\n Invalid parameter passed\n"); + break; + } + } + + bio_log = BIO_new_fp(stdout, BIO_NOCLOSE); + if(bio_log == NULL) { + printf("BIO_new_fp failed\n"); + goto end; + } + + generate_rsa(); + + +end: + + if(prtkeyfile) { + OPENSSL_free(prtkeyfile); + } + + if(bio_log) { + BIO_free_all(bio_log); + } + + return 0; +} diff --git a/rsa/rsa.c b/rsa/rsa.c new file mode 100644 index 0000000..f7a4318 --- /dev/null +++ b/rsa/rsa.c @@ -0,0 +1,141 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +char *pubkeyfile=NULL; +char *prtkeyfile=NULL; +BIO *bio_log=NULL; +unsigned int print; + +void usage(void){ + printf("\n rsa -i -o -p "); + printf("\n rsa -h -- For help"); + printf("\n"); +} + + +void getrsa(void){ + + BIO *prtbio, *pubbio; + EVP_PKEY *pkey; + + if (prtkeyfile) { + if ((prtbio = BIO_new_file(prtkeyfile, "r")) == NULL) { + BIO_printf(bio_log, "\n BIO_new_file %s failed\n", prtkeyfile); + goto end; + } + } + else { + BIO_printf(bio_log, "\n Private key file not provided\n"); + goto end; + } + + if (pubkeyfile) { + if ((pubbio = BIO_new_file(pubkeyfile, "w")) == NULL) { + BIO_printf(bio_log, "\n BIO_new_file %s failed\n", pubkeyfile); + goto end; + } + } + else { + pubbio = bio_log; + } + + if ((pkey = PEM_read_bio_PrivateKey(prtbio, NULL, NULL, NULL)) == NULL) { + BIO_printf(bio_log, "\n Unable to load\n"); + goto end; + } + + if( !PEM_write_bio_PUBKEY(pubbio, pkey)) { + BIO_printf(bio_log, "\n Public key extract failed\n"); + goto end; + } + + if (print) { + EVP_PKEY_print_private(pubbio, pkey, 0, NULL); + } + + end: + if(prtbio) { + BIO_free(prtbio); + } + + if(pubkeyfile && pubbio) { + BIO_free(pubbio); + } + + if(pkey) { + EVP_PKEY_free(pkey); + } + + return; +} + +int main(int argc, char **argv) { + + unsigned int opt; + + while( (opt=getopt(argc, argv, "i:o:hp")) != -1) { + switch(opt) { + case 'i': + if((prtkeyfile = (char *) OPENSSL_malloc(strlen(optarg)+1)) == NULL) { + printf("\n %s: OPENSSL_malloc failed. \n", optarg); + } + + memset(prtkeyfile, 0, strlen(optarg)+1); + memcpy(prtkeyfile, optarg, strlen(optarg)); + break; + + case 'o': + if((pubkeyfile = (char *) OPENSSL_malloc(strlen(optarg)+1)) == NULL) { + printf("\n %s: OPENSSL_malloc failed. \n", optarg); + } + + memset(pubkeyfile, 0, strlen(optarg)+1); + memcpy(pubkeyfile, optarg, strlen(optarg)); + break; + + case 'p': + print = 1; + break; + case 'h': + usage(); + goto end; + break; + + case '?': + printf("\n Invalid parameter passed\n"); + break; + } + } + + bio_log = BIO_new_fp(stdout, BIO_NOCLOSE); + if(bio_log == NULL) { + printf("BIO_new_fp failed\n"); + goto end; + } + + getrsa(); + + +end: + + if(pubkeyfile) { + OPENSSL_free(pubkeyfile); + } + + if(prtkeyfile) { + OPENSSL_free(prtkeyfile); + } + + if(bio_log) { + BIO_free_all(bio_log); + } + + return 0; +} diff --git a/rsa/rsasign.c b/rsa/rsasign.c new file mode 100644 index 0000000..15bcd71 --- /dev/null +++ b/rsa/rsasign.c @@ -0,0 +1,223 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +char *prtkeyfile=NULL; +char *signfile=NULL; +char *msgfile=NULL; +BIO *bio_log=NULL; +char *message = "RSA signing message"; + +void usage(void){ + printf("\n rsasign -i -m -d -s "); + printf("\n rsasign -h -- For help"); + printf("\n"); +} + + +void rsa_sign(void){ + + EVP_MD_CTX *mdctx = NULL; + EVP_PKEY *pkey=NULL; + BIO *prtbio = NULL, *signbio=NULL; + char *msg = NULL, *signbuf = NULL; + unsigned int msglen; + size_t signlen; + int ret; + + + if( (mdctx = EVP_MD_CTX_new()) == NULL) { + BIO_printf(bio_log, "\n MD ctx create failed\n"); + return ; + + } + + if (prtkeyfile) { + if ((prtbio = BIO_new_file(prtkeyfile, "r")) == NULL) { + BIO_printf(bio_log, "\n BIO_new_file %s failed\n", prtkeyfile); + goto end; + } + } + else { + BIO_printf(bio_log, "\n Private key file not provided\n"); + goto end; + } + + if (signfile) { + if ((signbio = BIO_new_file(signfile, "w")) == NULL) { + BIO_printf(bio_log, "\n BIO_new_file %s failed\n", signfile); + goto end; + } + } + else { + signbio = bio_log; + } + + if ((pkey = PEM_read_bio_PrivateKey(prtbio, NULL, NULL, NULL)) == NULL) { + BIO_printf(bio_log, "\n Unable to load\n"); + goto end; + } + + ret = EVP_DigestSignInit(mdctx, NULL, EVP_sha256(), NULL, pkey ); + if(!ret) { + BIO_printf(bio_log, "\n MD ctx sign init failed\n"); + goto end; + + } + + if(msgfile){ + FILE *fp = fopen(msgfile, "r"); + if (fp) { + fseek(fp, 0L, SEEK_END); + msglen = ftell(fp); + fseek(fp, 0L, SEEK_SET); + if((msg = OPENSSL_malloc(msglen)) == NULL) { + BIO_printf(bio_log, "\n Mem alloc failed\n"); + goto end; + + } + msglen = fread(msg, 1, msglen, fp); + } + else{ + BIO_printf(bio_log, "\n Message file open failed\n"); + goto end; + } + } + else { + msg = message; + msglen = strlen(msg); + } + + if(EVP_DigestSignUpdate(mdctx, msg, msglen) <= 0){ + BIO_printf(bio_log, "\n MD ctx sign update failed\n"); + goto end; + } + + if(EVP_DigestSignFinal(mdctx, NULL, &signlen) <= 0){ + BIO_printf(bio_log, "\n Sign len get failed\n"); + goto end; + } + + if( (signbuf = OPENSSL_malloc(signlen)) == NULL) { + BIO_printf(bio_log, "\n Mem alloc failed\n"); + goto end; + } + + if(EVP_DigestSignFinal(mdctx, signbuf, &signlen) <= 0){ + BIO_printf(bio_log, "\n Signature getting failed\n"); + goto end; + } + + BIO_write(signbio, signbuf, signlen); + + + end: + + if(msgfile && msg) { + OPENSSL_free(msg); + } + + if(signbuf) { + OPENSSL_free(signbuf); + } + + if(mdctx) { + EVP_MD_CTX_free(mdctx); + } + + if(prtbio) { + BIO_free(prtbio); + } + + if(signfile && signbio) { + BIO_free(signbio); + } + + if(pkey) { + EVP_PKEY_free(pkey); + } + + return; +} + +int main(int argc, char **argv) { + + unsigned int opt; + + while( (opt=getopt(argc, argv, "i:s:d:m:h")) != -1) { + switch(opt) { + case 'i': + if((prtkeyfile = (char *) OPENSSL_malloc(strlen(optarg)+1)) == NULL) { + printf("\n %s: OPENSSL_malloc failed. \n", optarg); + } + + memset(prtkeyfile, 0, strlen(optarg)+1); + memcpy(prtkeyfile, optarg, strlen(optarg)); + break; + + case 'm': + if((msgfile = (char *) OPENSSL_malloc(strlen(optarg)+1)) == NULL) { + printf("\n %s: OPENSSL_malloc failed. \n", optarg); + } + + memset(msgfile, 0, strlen(optarg)+1); + memcpy(msgfile, optarg, strlen(optarg)); + break; + + case 's': + if((signfile = (char *) OPENSSL_malloc(strlen(optarg)+1)) == NULL) { + printf("\n %s: OPENSSL_malloc failed. \n", optarg); + } + + memset(signfile, 0, strlen(optarg)+1); + memcpy(signfile, optarg, strlen(optarg)); + break; + + case 'd': + break; + + case 'h': + usage(); + goto end; + break; + + case '?': + printf("\n Invalid parameter passed\n"); + break; + } + } + + bio_log = BIO_new_fp(stdout, BIO_NOCLOSE); + if(bio_log == NULL) { + printf("BIO_new_fp failed\n"); + goto end; + } + + rsa_sign(); + + +end: + if(prtkeyfile) { + OPENSSL_free(prtkeyfile); + } + + if(signfile) { + OPENSSL_free(signfile); + } + + if(msgfile) { + OPENSSL_free(msgfile); + } + + if(bio_log) { + BIO_free_all(bio_log); + } + + return 0; +} diff --git a/rsa/rsaverify.c b/rsa/rsaverify.c new file mode 100644 index 0000000..ee9e11e --- /dev/null +++ b/rsa/rsaverify.c @@ -0,0 +1,221 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +char *pubkeyfile=NULL; +char *signfile=NULL; +char *msgfile=NULL; +BIO *bio_log=NULL; +char *message = "RSA signing message"; + +void usage(void){ + printf("\n rsaverify -i -m -d -s "); + printf("\n rsaverify -h -- For help"); + printf("\n"); +} + + +void rsa_verify(void){ + + EVP_MD_CTX *mdctx = NULL; + EVP_PKEY *pkey=NULL; + BIO *pubbio = NULL, *signbio=NULL; + char *msg = NULL, *signbuf = NULL; + unsigned int msglen; + size_t signlen; + int ret; + + + if( (mdctx = EVP_MD_CTX_new()) == NULL) { + BIO_printf(bio_log, "\nMD ctx create failed\n"); + return ; + + } + + if (pubkeyfile) { + if ((pubbio = BIO_new_file(pubkeyfile, "r")) == NULL) { + BIO_printf(bio_log, "\nBIO_new_file %s failed\n", pubkeyfile); + goto end; + } + } + else { + BIO_printf(bio_log, "\nPublic key file not provided\n"); + goto end; + } + + if ((pkey = PEM_read_bio_PUBKEY(pubbio, NULL, NULL, NULL)) == NULL) { + BIO_printf(bio_log, "\nUnable to load PUBKEY\n"); + goto end; + } + + ret = EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pkey ); + if(!ret) { + BIO_printf(bio_log, "\nMD ctx sign init failed\n"); + goto end; + + } + + if(msgfile){ + FILE *fp = fopen(msgfile, "r"); + if (fp) { + fseek(fp, 0L, SEEK_END); + msglen = ftell(fp); + fseek(fp, 0L, SEEK_SET); + if((msg = OPENSSL_malloc(msglen)) == NULL) { + BIO_printf(bio_log, "\nMem alloc failed\n"); + goto end; + + } + msglen = fread(msg, 1, msglen, fp); + } + else{ + BIO_printf(bio_log, "\nMessage file open failed\n"); + goto end; + } + } + else { + msg = message; + msglen = strlen(msg); + } + + if( (signbio = BIO_new_file(signfile, "rb")) == NULL) { + BIO_printf(bio_log, "\nSign file open failed\n"); + goto end; + } + + signlen = EVP_PKEY_size(pkey); + + signbuf = OPENSSL_malloc(signlen); + if (signbuf == NULL) { + BIO_printf(bio_log, "\nMalloc failed\n"); + BIO_free(signbio); + goto end; + } + + signlen = BIO_read(signbio, signbuf, signlen); + BIO_free(signbio); + if (signlen < 0 ) { + BIO_printf(bio_log, "\nError in reading sign file\n"); + goto end; + } + + if(EVP_DigestVerifyUpdate(mdctx, msg, msglen) <= 0){ + BIO_printf(bio_log, "\nMD ctx sign update failed\n"); + goto end; + } + + if(EVP_DigestVerifyFinal(mdctx, signbuf, signlen) == 1){ + BIO_printf(bio_log, "\nSignature verification SUCCESS\n"); + } + else { + BIO_printf(bio_log, "\nSignature verification FAILED\n"); + } + + + + end: + + if(msgfile && msg) { + OPENSSL_free(msg); + } + + if(signbuf) { + OPENSSL_free(signbuf); + } + + if(mdctx) { + EVP_MD_CTX_free(mdctx); + } + + if(pubbio) { + BIO_free(pubbio); + } + + if(pkey) { + EVP_PKEY_free(pkey); + } + + return; +} + +int main(int argc, char **argv) { + + unsigned int opt; + + while( (opt=getopt(argc, argv, "i:s:d:m:h")) != -1) { + switch(opt) { + case 'i': + if((pubkeyfile = (char *) OPENSSL_malloc(strlen(optarg)+1)) == NULL) { + printf("\n %s: OPENSSL_malloc failed. \n", optarg); + } + + memset(pubkeyfile, 0, strlen(optarg)+1); + memcpy(pubkeyfile, optarg, strlen(optarg)); + break; + + case 'm': + if((msgfile = (char *) OPENSSL_malloc(strlen(optarg)+1)) == NULL) { + printf("\n %s: OPENSSL_malloc failed. \n", optarg); + } + + memset(msgfile, 0, strlen(optarg)+1); + memcpy(msgfile, optarg, strlen(optarg)); + break; + + case 's': + if((signfile = (char *) OPENSSL_malloc(strlen(optarg)+1)) == NULL) { + printf("\n %s: OPENSSL_malloc failed. \n", optarg); + } + + memset(signfile, 0, strlen(optarg)+1); + memcpy(signfile, optarg, strlen(optarg)); + break; + + case 'd': + break; + + case 'h': + usage(); + goto end; + break; + + case '?': + printf("\n Invalid parameter passed\n"); + break; + } + } + + bio_log = BIO_new_fp(stdout, BIO_NOCLOSE); + if(bio_log == NULL) { + printf("BIO_new_fp failed\n"); + goto end; + } + + rsa_verify(); + + +end: + if(pubkeyfile) { + OPENSSL_free(pubkeyfile); + } + + if(signfile) { + OPENSSL_free(signfile); + } + + if(msgfile) { + OPENSSL_free(msgfile); + } + + if(bio_log) { + BIO_free_all(bio_log); + } + + return 0; +}