37 #include <sys/types.h> 46 # include <sys/time.h> 170 int frnd =
open(
"/dev/urandom", O_RDONLY);
171 if (frnd < 0) frnd =
open(
"/dev/random", O_RDONLY);
174 ssize_t rs =
read(frnd, (
void *) &r,
sizeof(
int));
177 if (rs ==
sizeof(
int))
return r;
179 printf(
"+++ERROR+++ : aux_rand: neither /dev/urandom nor /dev/random are available or readable!\n");
181 if (gettimeofday(&tv,0) == 0) {
183 memcpy((
void *)&t1, (
void *)&tv.tv_sec,
sizeof(
int));
184 memcpy((
void *)&t2, (
void *)&tv.tv_usec,
sizeof(
int));
224 if ( *i1-- != *i2-- )
225 return( i1[1] > i2[1] ? 1 : -1 );
238 return( l - c2->
n_len);
306 if (!lo && same && !sum)
332 for (lc=1, ld=0; l--; lc++) {
356 if (!lo && same && !over) {
384 for (i=l,mul=0; i; i--) {
422 int l1,l2,ld,lc,
l,i,j;
430 for (i=l, vp=
id; i--;)
434 for ( p1 = m1->
n_part, i=0; i < l1 ; i++, p1++) {
439 for ( p2 = m2->
n_part, j = l2; j--;) {
449 for (lc=0, vp=
id, p1=d->
n_part; lc++ < l;) {
450 if ( (*p1++ = *vp++))
495 for (; l >= 0; ld++, i1--, i1e--, l--, i3--) {
498 if (ld == l2 && ! *i1e) {
503 if ( ld > l2 || (ld == l2 &&
n_cmp( i1, z2->
n_part, l2) >= 0) ) {
507 if ( ld > (l2t= z2[pw].n_len)
509 &&
n_cmp( i1, z2[pw].n_part, ld) >= 0)) {
510 ld =
n_sub( i1, z2[pw].n_part, i1, ld, l2t );
527 if (lq>0 && !q->
n_part[lq -1])
554 n_div( d1, d2, q, r );
562 #if rsa_MAXBIT == rsa_LOWBITS 624 if (!
a_cmp( n, &g_mod_z2[0]) )
634 if (
a_cmp( d, g_mod_z2) >= 0)
635 a_sub( d, g_mod_z2, d );
656 while ( ! (nt.
n_part[0] & 1)) {
661 a_sub( &nt, &a_one, &nt );
676 if (
a_cmp( &t[at], &t[bt]) < 0) {
677 tmp= at; at= bt; bt= tmp;
680 while ( t[bt].n_len) {
682 tmp= at; at= bt; bt= tmp;
703 return( n->
n_part[0] & m );
728 for (i=0;
a_cmp( &b, n) <= 0;
a_mult( &b, &a_two, &b ), i++)
803 ret = ((f == 1) || (f == 7)) ? 1 : -1;
815 if (
n_bits( n, 2) == 1 ||
848 if (!
a_cmp(&t[at],&a_one)) {
851 if (!
a_cmp(&t[at],&a_two)) {
852 ret *=
jak_f( &t[nt] );
857 if ( t[at].n_part[0] & 1) {
860 ret *=
jak_g( &t[at], &t[nt] );
862 tmp = at; at = nt; nt = tmp;
865 ret *=
jak_f( &t[nt] );
890 if (
a_cmp(n,&a_two) <= 0 || m <= 0)
893 a_sub( n, &a_one, &n1 );
900 for (; w &&
m; m--) {
910 }
while (
a_cmp( &a, n) >= 0 ||
a_cmp( &a, &a_two) < 0 );
923 if (
a_cmp( >, &a_one) == 0) {
926 m_exp( &a, &n2, &a );
928 if ( (
a_cmp( &a, &a_one) == 0 && j == 1 )
929 || (
a_cmp( &a, &n1 ) == 0 && j == -1) )
959 if (
a_cmp(phi,d) <= 0)
972 i0=k%3; i1=(k+2)%3; i2=(k+1)%3;
973 a_div( &r[i2], &r[i1], &c, &r[i0] );
974 m_mult( &c, &p[i1], &p[i0] );
975 m_add( &p[i0], &p[i2], &p[i0] );
976 }
while (r[i0].n_len);
978 if (
a_cmp( &r[i1], &a_one) )
982 a_sub( phi, &p[i1], e );
996 const char *hex =
"0123456789ABCDEF" ;
1001 p=&num[
sizeof(num) -1];
1004 for (l=len; l--; p--) {
1010 while (len-- && *p ==
'0')
1018 const char *randdev =
"/dev/urandom";
1022 if ((fd =
open(randdev, O_RDONLY)) != -1) {
1023 if (
read(fd, &seed,
sizeof(seed))) {;}
1026 seed = (
unsigned int)time(0);
1094 static char hex[] =
"0123456789ABCDEF";
1103 for (i=len; i; i--) {
1105 *ph-- = hex[ (c >> 4) & 0xF ];
1106 *ph-- = hex[ c & 0xF ];
1116 ph = buf + (i=strlen(buf)) -1;
1118 for (; len; len--) {
1120 c = (strchr( hex, *ph) - hex) << 4;
1126 c |= strchr( hex, *ph) - hex;
double read(const std::string &file_name)
reading
void inv(rsa_NUMBER *d, rsa_NUMBER *phi, rsa_NUMBER *e)
static long int sum(long int i)
static double p3(double t, double a, double b, double c, double d)
void a_imult(rsa_NUMBER *n, rsa_INT m, rsa_NUMBER *d)
int n_cmp(rsa_INT *i1, rsa_INT *i2, int l)
int p_prim(rsa_NUMBER *n, int m)
static int aux_rand()
rand() implementation using /udev/random or /dev/random, if available
void a_div(rsa_NUMBER *d1, rsa_NUMBER *d2, rsa_NUMBER *q, rsa_NUMBER *r)
int a_cmp(rsa_NUMBER *c1, rsa_NUMBER *c2)
void m_init(rsa_NUMBER *n, rsa_NUMBER *o)
void m_add(rsa_NUMBER *s1, rsa_NUMBER *s2, rsa_NUMBER *d)
void a_assign(rsa_NUMBER *d, rsa_NUMBER *s)
void a_ggt(rsa_NUMBER *a, rsa_NUMBER *b, rsa_NUMBER *f)
static double p2(double t, double a, double b, double c)
void m_exp(rsa_NUMBER *x, rsa_NUMBER *n, rsa_NUMBER *z)
void a_sub(rsa_NUMBER *s1, rsa_NUMBER *s2, rsa_NUMBER *d)
rsa_INT n_part[rsa_MAXLEN]
static int jakobi(rsa_NUMBER *, rsa_NUMBER *)
void do_crypt(char *s, char *d, int len, rsa_NUMBER *e)
int rsa_num_sget(rsa_NUMBER *, char *)
void n_div(rsa_NUMBER *d1, rsa_NUMBER *z2, rsa_NUMBER *q, rsa_NUMBER *r)
void m_mult(rsa_NUMBER *m1, rsa_NUMBER *m2, rsa_NUMBER *d)
void a_add(rsa_NUMBER *s1, rsa_NUMBER *s2, rsa_NUMBER *d)
static double p1(double t, double a, double b)
int rsa_num_sput(rsa_NUMBER *, char *, int)
void gen_number(int len, rsa_NUMBER *n)
void a_div2(rsa_NUMBER *n)
int n_sub(rsa_INT *p1, rsa_INT *p2, rsa_INT *p3, int l, int lo)
int n_bits(rsa_NUMBER *n, int b)
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
static rsa_NUMBER g_mod_z2[rsa_MAXBIT]
you should not use this method at all Int_t Int_t z
static int jak_g(rsa_NUMBER *, rsa_NUMBER *)
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
static int jak_f(rsa_NUMBER *)
int n_bitlen(rsa_NUMBER *n)
int n_mult(rsa_INT *n, rsa_INT m, rsa_INT *d, int l)
if(line.BeginsWith("/*"))
void a_mult(rsa_NUMBER *m1, rsa_NUMBER *m2, rsa_NUMBER *d)