update new openssl

parent 97b2f034
......@@ -29,6 +29,7 @@
#include <time.h>
#include <openssl/rsa.h>
#include <openssl/bn.h>
#include <openssl/sha.h>
#include <math.h>
#include "conf.h"
......@@ -38,6 +39,6 @@
#define maxprivlen 11+4*Tbytes+maxpublen
void error(char* smsg);
int get_pub_packet(unsigned char * str, RSA * prsa, int complet);
int get_priv_packet( unsigned char * str, RSA * prsa, BIGNUM * p, BIGNUM * q, BIGNUM * u );
void write_keys( RSA * prsa, BIGNUM * p, BIGNUM * q, BIGNUM * u, char * prefix, unsigned char * hash, double score);
int get_pub_packet(unsigned char * str, BIGNUM * n, BIGNUM * e, BIGNUM * d, int complet);
int get_priv_packet( unsigned char * str, BIGNUM * n, BIGNUM * e, BIGNUM * d, BIGNUM * p, BIGNUM * q, BIGNUM * u );
void write_keys( BIGNUM * n, BIGNUM * e, BIGNUM * d, BIGNUM * p, BIGNUM * q, BIGNUM * u, char * prefix, unsigned char * hash, double score);
......@@ -70,7 +70,6 @@ int main(int argc, char ** argv)
time_t time_now;
int i;
int k;
fprintf(stderr,"Target: ");
for(i=0;i<16;i+=2)
......@@ -84,40 +83,52 @@ int main(int argc, char ** argv)
while(1)
{
RSA *prsa;
prsa = RSA_generate_key(Tbits, RSA_F4, NULL, NULL);
BN_CTX * ctx = BN_CTX_new();
BN_CTX_start(ctx);
RSA *prsa = RSA_new();
BIGNUM * n = BN_CTX_get(ctx);
BIGNUM * e = BN_CTX_get(ctx);
BIGNUM * d = BN_CTX_get(ctx);
BIGNUM * p = BN_CTX_get(ctx);
BIGNUM * q = BN_CTX_get(ctx);
BIGNUM * tmp = BN_CTX_get(ctx);
BN_set_word(e, RSA_F4);
if(!prsa)
if(!RSA_generate_key_ex(prsa, Tbits, e, NULL))
error("OpenSSL ne veut pas generer de clef");
BN_CTX * ctx = BN_CTX_new();
RSA_get0_key(prsa, (const BIGNUM **)&n, (const BIGNUM **)&e, (const BIGNUM **)&d);
RSA_get0_factors(prsa, (const BIGNUM **)&p, (const BIGNUM **)&q);
BN_CTX_start(ctx);
BIGNUM * p1 = BN_CTX_get(ctx);
BIGNUM * q1 = BN_CTX_get(ctx);
BIGNUM * p1q1 = BN_CTX_get(ctx);
BN_sub(p1, prsa->p, BN_value_one());
BN_sub(q1, prsa->q, BN_value_one());
BN_sub(p1, p, BN_value_one());
BN_sub(q1, q, BN_value_one());
BN_mul(p1q1,p1,q1,ctx);
BIGNUM * p = BN_CTX_get(ctx);
BIGNUM * q = BN_CTX_get(ctx);
BIGNUM * u = BN_CTX_get(ctx);
if(BN_cmp(prsa->p,prsa->q)<0)
{
BN_copy(p,prsa->p);
BN_copy(q,prsa->q);
}
else
if(BN_cmp(p,q)>0)
{
BN_copy(q,prsa->p);
BN_copy(p,prsa->q);
BN_copy(tmp,p);
BN_copy(p,q);
BN_copy(q,tmp);
}
BN_mod_inverse(u,p,q,ctx);
printf("n %d : ",BN_num_bits(n));
BN_print_fp(stdout,n);
printf("\n");
printf("e %d : ",BN_num_bits(e));
BN_print_fp(stdout,e);
printf("\n");
printf("p %d : ",BN_num_bits(p));
BN_print_fp(stdout,p);
printf("\n");
......@@ -134,7 +145,7 @@ int main(int argc, char ** argv)
publen = get_pub_packet(pub,prsa,1);
publen = get_pub_packet(pub,n,e,d,1);
BIGNUM * test = BN_CTX_get(ctx);
......@@ -143,7 +154,7 @@ int main(int argc, char ** argv)
{
publen = get_pub_packet(pub,prsa,0);
publen = get_pub_packet(pub,n,e,d,0);
SHA1(pub,publen,hash);
nb++;
......@@ -160,7 +171,7 @@ int main(int argc, char ** argv)
)
{
/* Yeah, one */
BN_gcd(test, p1q1, prsa->e,ctx);
BN_gcd(test, p1q1, e,ctx);
if(BN_is_one(test))
{
/* This key is valid */
......@@ -170,7 +181,7 @@ int main(int argc, char ** argv)
for(i=0;i<32;i++)
{
char num=0;
unsigned int num=0;
if('0'<=str_hash[i] && str_hash[i]<='9')
{
num=str_hash[i]-'0';
......@@ -180,7 +191,7 @@ int main(int argc, char ** argv)
num=str_hash[i]-'A'+10;
}
char num_ref=0;
unsigned int num_ref=0;
if('0'<=str_hash_ref[i] && str_hash_ref[i]<='9')
{
num_ref=str_hash_ref[i]-'0';
......@@ -202,8 +213,8 @@ int main(int argc, char ** argv)
}
}
BN_mod_inverse(prsa->d,prsa->e,p1q1,ctx);
write_keys(prsa,p,q,u,argv[1],hash,score);
BN_mod_inverse(d,e,p1q1,ctx);
write_keys(n,e,d,p,q,u,argv[1],hash,score);
}
/* else FUCK ! */
......@@ -220,9 +231,9 @@ int main(int argc, char ** argv)
for(i=0;i<20;i+=2)
fprintf(stderr," %02X%02X",hash_best[i],hash_best[i+1]);
fprintf(stderr,", Score: %g",score_best);
fprintf(stderr,", Hashs: %luM",nb/((unsigned long long)1e6));
fprintf(stderr,", Hashs: %lluM",nb/((unsigned long long)1e6));
unsigned int tps = (time_now-time_start)>0 ? (time_now-time_start) : 1;
fprintf(stderr,", Rate: %luk/s",nb/((unsigned long long)(1e3*tps)));
fprintf(stderr,", Rate: %lluk/s",nb/((unsigned long long)(1e3*tps)));
fprintf(stderr,", Time: %lus ",time_now-time_start);
......@@ -235,7 +246,7 @@ int main(int argc, char ** argv)
BN_add_word(prsa->e, 2L);
BN_add_word(e, 2L);
}
......
......@@ -26,11 +26,11 @@
#include "all.h"
int get_pub_packet(unsigned char * str, RSA * prsa, int complet)
int get_pub_packet(unsigned char * str, BIGNUM* n, BIGNUM* e, BIGNUM* d, int complet)
{
unsigned int lBe=(unsigned int)BN_num_bytes(prsa->e);
unsigned int lbe=(unsigned int)BN_num_bits(prsa->e);
unsigned int lBe=(unsigned int)BN_num_bytes(e);
unsigned int lbe=(unsigned int)BN_num_bits(e);
//header
str[1]=((10+Tbytes+lBe)>>8);
......@@ -54,13 +54,13 @@ int get_pub_packet(unsigned char * str, RSA * prsa, int complet)
//MPI n
str[9]=(unsigned char)(Tbits>>8);
str[10]=(unsigned char)(Tbits);
BN_bn2bin(prsa->n,str+11);
BN_bn2bin(n,str+11);
}
//MPI e
str[11+Tbytes]=lbe>>8;
str[12+Tbytes]=lbe;
BN_bn2bin(prsa->e,str+13+Tbytes);
BN_bn2bin(e,str+13+Tbytes);
return(13+Tbytes+lBe);
}
......@@ -68,7 +68,9 @@ int get_pub_packet(unsigned char * str, RSA * prsa, int complet)
int get_priv_packet(
unsigned char * str,
RSA * prsa,
BIGNUM * n,
BIGNUM * e,
BIGNUM * d,
BIGNUM * p,
BIGNUM * q,
BIGNUM * u
......@@ -76,8 +78,8 @@ int get_priv_packet(
{
//calculated values
unsigned int lBd=(unsigned int)BN_num_bytes(prsa->d);
unsigned int lbd=(unsigned int)BN_num_bits(prsa->d);
unsigned int lBd=(unsigned int)BN_num_bytes(d);
unsigned int lbd=(unsigned int)BN_num_bits(d);
unsigned int lBp=(unsigned int)BN_num_bytes(p);
unsigned int lbp=(unsigned int)BN_num_bits(p);
......@@ -89,7 +91,7 @@ int get_priv_packet(
unsigned int lbu=(unsigned int)BN_num_bits(u);
//public key
int publen = get_pub_packet(str,prsa,1);
int publen = get_pub_packet(str,n, e, d,1);
//header (rewrite public key header)
str[0]=0x95;
......@@ -103,7 +105,7 @@ int get_priv_packet(
//MPI d
str[1+publen]=lbd>>8;
str[2+publen]=lbd;
BN_bn2bin(prsa->d,str+3+publen);
BN_bn2bin(d,str+3+publen);
// algo specific fields p (p<q)
//MPI p
......@@ -144,7 +146,9 @@ int get_priv_packet(
void write_keys(
RSA * prsa,
BIGNUM * n,
BIGNUM * e,
BIGNUM * d,
BIGNUM * p,
BIGNUM * q,
BIGNUM * u,
......@@ -155,11 +159,11 @@ void write_keys(
{
//public key
unsigned char pub[maxpublen];
int publen = get_pub_packet(pub,prsa,1);
int publen = get_pub_packet(pub,n,e,d,1);
//priv
unsigned char priv[maxprivlen];
int privlen = get_priv_packet(priv,prsa,p,q,u);
int privlen = get_priv_packet(priv,n,e,d,p,q,u);
// write
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment