Created
February 6, 2016 12:20
-
-
Save ShikChen/457a9c5065116aec6c2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <openssl/bn.h> | |
#include <openssl/rsa.h> | |
#include <openssl/engine.h> | |
#include <openssl/pem.h> | |
const int L = 4 << 20; | |
char sp[L], sq[L]; | |
int main (int argc, char *argv[]) | |
{ | |
freopen("p.txt","r",stdin); | |
gets(sp); | |
freopen("q.txt","r",stdin); | |
gets(sq); | |
argc=3; | |
argv[1]=sp; | |
argv[2]=sq; | |
BIGNUM *n = BN_new (); | |
BIGNUM *d = BN_new (); | |
BIGNUM *e = BN_new (); | |
BIGNUM *p = BN_new (); | |
BIGNUM *q = BN_new (); | |
BIGNUM *p1 = BN_new (); | |
BIGNUM *q1 = BN_new (); | |
BIGNUM *dmp1 = BN_new (); | |
BIGNUM *dmq1 = BN_new (); | |
BIGNUM *iqmp = BN_new (); | |
BIGNUM *phi = BN_new (); | |
BN_CTX *ctx = BN_CTX_new (); | |
RSA *key = RSA_new (); | |
if (argc < 3) | |
{ | |
fprintf (stderr, "usage: %s p q\n", argv[0]); | |
exit (1); | |
} | |
fprintf(stderr,"LINE %d\n",__LINE__); | |
if (!(BN_dec2bn (&p, argv[1])) || !(BN_dec2bn (&q, argv[2]))) { | |
fprintf (stderr, "usage: %s p q\n", argv[0]); | |
exit (1); | |
} | |
fprintf(stderr,"LINE %d\n",__LINE__); | |
/* if (!(BN_is_prime (p, BN_prime_checks, NULL, ctx, NULL)) || */ | |
/* !(BN_is_prime (q, BN_prime_checks, NULL, ctx, NULL))) { */ | |
/* fprintf (stderr, "Arguments must both be prime!\n", argv[0]); */ | |
/* exit (1); */ | |
/* } */ | |
fprintf(stderr,"LINE %d\n",__LINE__); | |
BN_dec2bn (&e, "65537"); | |
fprintf(stderr,"LINE %d\n",__LINE__); | |
/* Calculate RSA private key parameters */ | |
/* n = p*q */ | |
BN_mul (n, p, q, ctx); | |
fprintf(stderr,"LINE %d\n",__LINE__); | |
/* p1 = p-1 */ | |
BN_sub (p1, p, BN_value_one ()); | |
fprintf(stderr,"LINE %d\n",__LINE__); | |
/* q1 = q-1 */ | |
BN_sub (q1, q, BN_value_one ()); | |
fprintf(stderr,"LINE %d\n",__LINE__); | |
/* phi(pq) = (p-1)*(q-1) */ | |
BN_mul (phi, p1, q1, ctx); | |
fprintf(stderr,"LINE %d\n",__LINE__); | |
/* d = e^-1 mod phi */ | |
BN_mod_inverse (d, e, phi, ctx); | |
fprintf(stderr,"LINE %d\n",__LINE__); | |
/* dmp1 = d mod (p-1) */ | |
BN_mod (dmp1, d, p1, ctx); | |
fprintf(stderr,"LINE %d\n",__LINE__); | |
/* dmq1 = d mod (q-1) */ | |
BN_mod (dmq1, d, q1, ctx); | |
fprintf(stderr,"LINE %d\n",__LINE__); | |
/* iqmp = q^-1 mod p */ | |
BN_mod_inverse (iqmp, q, p, ctx); | |
fprintf(stderr,"LINE %d\n",__LINE__); | |
/* Populate key data structure */ | |
key->n = n; | |
key->e = e; | |
key->d = d; | |
key->p = p; | |
key->q = q; | |
key->dmp1 = dmp1; | |
key->dmq1 = dmq1; | |
key->iqmp = iqmp; | |
fprintf(stderr,"LINE %d\n",__LINE__); | |
/* Output the private key in human-readable and PEM forms */ | |
RSA_print_fp (stdout, key, 5); | |
fprintf(stderr,"LINE %d\n",__LINE__); | |
printf("\n"); | |
PEM_write_RSAPrivateKey (stdout, key, NULL, NULL, 0, 0, NULL); | |
fprintf(stderr,"LINE %d\n",__LINE__); | |
/* Release allocated objects */ | |
BN_CTX_free (ctx); | |
RSA_free(key); /* also frees n, e, d, p, q, dmp1, dmq1, iqmp */ | |
BN_clear_free (phi); | |
BN_clear_free (p1); | |
BN_clear_free (q1); | |
fprintf(stderr,"LINE %d\n",__LINE__); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment