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 );
237 if ( (
l=
c1->n_len) !=
c2->n_len)
238 return(
l -
c2->n_len);
255 memcpy(
d->n_part,
s->n_part,
sizeof(
rsa_INT)*
l);
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--) {
408 d->n_len =
n_mult(
n->n_part,
m,
d->n_part,
n->n_len );
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])
562#if rsa_MAXBIT == rsa_LOWBITS
588 if ( (i=
n->n_len) &&
n->n_part[i-1] == 0 )
656 while ( ! (nt.
n_part[0] & 1)) {
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 );
803 ret = ((
f == 1) || (
f == 7)) ? 1 : -1;
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] );
900 for (; w &&
m;
m--) {
903 for (i=
n->n_len-1, p=
a.n_part; i; i--)
929 || (
a_cmp( &
a, &n1 ) == 0 && j == -1) )
972 i0=k%3; i1=(k+2)%3; i2=(k+1)%3;
975 m_add( &p[i0], &p[i2], &p[i0] );
976 }
while (
r[i0].n_len);
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;
static constexpr double s
static constexpr double m2
int n_cmp(rsa_INT *i1, rsa_INT *i2, int l)
static rsa_NUMBER g_mod_z2[rsa_MAXBIT]
void a_imult(rsa_NUMBER *n, rsa_INT m, rsa_NUMBER *d)
void m_exp(rsa_NUMBER *x, rsa_NUMBER *n, rsa_NUMBER *z)
void a_mult(rsa_NUMBER *m1, rsa_NUMBER *m2, rsa_NUMBER *d)
void a_div2(rsa_NUMBER *n)
int n_sub(rsa_INT *p1, rsa_INT *p2, rsa_INT *p3, int l, int lo)
void m_mult(rsa_NUMBER *m1, rsa_NUMBER *m2, rsa_NUMBER *d)
int a_cmp(rsa_NUMBER *c1, rsa_NUMBER *c2)
void n_div(rsa_NUMBER *d1, rsa_NUMBER *z2, rsa_NUMBER *q, rsa_NUMBER *r)
void a_assign(rsa_NUMBER *d, rsa_NUMBER *s)
static int jak_g(rsa_NUMBER *, rsa_NUMBER *)
void a_ggt(rsa_NUMBER *a, rsa_NUMBER *b, rsa_NUMBER *f)
static int jakobi(rsa_NUMBER *, rsa_NUMBER *)
void gen_number(int len, rsa_NUMBER *n)
void a_sub(rsa_NUMBER *s1, rsa_NUMBER *s2, rsa_NUMBER *d)
void a_div(rsa_NUMBER *d1, rsa_NUMBER *d2, rsa_NUMBER *q, rsa_NUMBER *r)
void do_crypt(char *s, char *d, int len, rsa_NUMBER *e)
void a_add(rsa_NUMBER *s1, rsa_NUMBER *s2, rsa_NUMBER *d)
int n_mult(rsa_INT *n, rsa_INT m, rsa_INT *d, int l)
void m_add(rsa_NUMBER *s1, rsa_NUMBER *s2, rsa_NUMBER *d)
static int aux_rand()
rand() implementation using /udev/random or /dev/random, if available
static int jak_f(rsa_NUMBER *)
int n_bits(rsa_NUMBER *n, int b)
void m_init(rsa_NUMBER *n, rsa_NUMBER *o)
void inv(rsa_NUMBER *d, rsa_NUMBER *phi, rsa_NUMBER *e)
int n_bitlen(rsa_NUMBER *n)
int p_prim(rsa_NUMBER *n, int m)
int rsa_num_sget(rsa_NUMBER *, char *)
int rsa_num_sput(rsa_NUMBER *, char *, int)
rsa_INT n_part[rsa_MAXLEN]
static long int sum(long int i)