diff options
| author | Charles.Forsyth <devnull@localhost> | 2008-06-11 12:31:51 +0000 |
|---|---|---|
| committer | Charles.Forsyth <devnull@localhost> | 2008-06-11 12:31:51 +0000 |
| commit | 31a18a6996a6b5927e39cc553696c167e6c88e3d (patch) | |
| tree | 40433c168b91d5c224cb8c338054eb9fb2ecbd37 /libinterp | |
| parent | ac4d5a82181d3f4d63469e7745ccd38126c04b24 (diff) | |
20080611-1331
Diffstat (limited to 'libinterp')
| -rw-r--r-- | libinterp/ipint.c | 136 | ||||
| -rw-r--r-- | libinterp/keyring.c | 94 |
2 files changed, 80 insertions, 150 deletions
diff --git a/libinterp/ipint.c b/libinterp/ipint.c index 94f4acc0..968540af 100644 --- a/libinterp/ipint.c +++ b/libinterp/ipint.c @@ -9,30 +9,36 @@ #include "../libkeyring/keys.h" #include "raise.h" -enum { - PSEUDO=0, - REALLY, -}; - -void getRandBetween(BigInt p, BigInt q, BigInt result, int type); - extern Type *TIPint; -#define MP(x) (((IPint*)(x))->b) +#define MP(x) checkIPint((x)) Keyring_IPint* -newIPint(BigInt b) +newIPint(mpint* b) { Heap *h; IPint *ip; if(b == nil) error(exHeap); - h = heap(TIPint); /* TO DO: loss if heap fails */ + h = heap(TIPint); /* TO DO: caller might lose other values if heap raises error here */ ip = H2D(IPint*, h); ip->b = b; return (Keyring_IPint*)ip; } +mpint* +checkIPint(Keyring_IPint *v) +{ + IPint *ip; + + ip = (IPint*)v; + if(ip == H || ip == nil) + error(exNilref); + if(D2H(ip)->t != TIPint) + error(exType); + return ip->b; +} + void freeIPint(Heap *h, int swept) { @@ -49,7 +55,7 @@ void IPint_iptob64z(void *fp) { F_IPint_iptob64 *f; - BigInt b; + mpint *b; char buf[MaxBigBytes]; /* TO DO: should allocate these */ uchar *p; int n, o; @@ -154,7 +160,7 @@ static Keyring_IPint* strtoipint(String *s, int base) { char *p, *q; - BigInt b; + mpint *b; p = string2c(s); b = strtomp(p, &q, base, nil); @@ -185,7 +191,7 @@ void IPint_bytestoip(void *fp) { F_IPint_bytestoip *f; - BigInt b; + mpint *b; f = fp; destroy(*f->ret); @@ -202,7 +208,7 @@ void IPint_bebytestoip(void *fp) { F_IPint_bebytestoip *f; - BigInt b; + mpint *b; f = fp; destroy(*f->ret); @@ -232,24 +238,17 @@ void IPint_random(void *fp) { F_IPint_random *f; - BigInt b, min, max; + mpint *b; + void *v; f = fp; - destroy(*f->ret); + v = *f->ret; *f->ret = H; + destroy(v); - b = itomp(1, nil); - min = mpnew(0); - max = mpnew(0); - mpleft(b, f->minbits, min); - mpleft(b, f->maxbits, max); - release(); - getRandBetween(min, max, b, PSEUDO); /* TO DO */ + b = mprand(f->maxbits, genrandom, nil); acquire(); - - mpfree(min); - mpfree(max); *f->ret = newIPint(b); } @@ -301,7 +300,7 @@ void IPint_expmod(void *fp) { F_IPint_expmod *f; - BigInt ret, mod; + mpint *ret, *mod; f = fp; destroy(*f->ret); @@ -324,7 +323,7 @@ void IPint_invert(void *fp) { F_IPint_invert *f; - BigInt ret; + mpint *ret; f = fp; destroy(*f->ret); @@ -341,7 +340,7 @@ void IPint_add(void *fp) { F_IPint_add *f; - BigInt i1, i2, ret; + mpint *i1, *i2, *ret; f = fp; destroy(*f->ret); @@ -362,7 +361,7 @@ void IPint_sub(void *fp) { F_IPint_sub *f; - BigInt i1, i2, ret; + mpint *i1, *i2, *ret; f = fp; destroy(*f->ret); @@ -383,7 +382,7 @@ void IPint_mul(void *fp) { F_IPint_mul *f; - BigInt i1, i2, ret; + mpint *i1, *i2, *ret; f = fp; destroy(*f->ret); @@ -404,7 +403,7 @@ void IPint_div(void *fp) { F_IPint_div *f; - BigInt i1, i2, quo, rem; + mpint *i1, *i2, *quo, *rem; f = fp; destroy(f->ret->t0); @@ -434,7 +433,7 @@ void IPint_mod(void *fp) { F_IPint_mod *f; - BigInt i1, i2, ret; + mpint *i1, *i2, *ret; f = fp; destroy(*f->ret); @@ -455,7 +454,7 @@ void IPint_neg(void *fp) { F_IPint_neg *f; - BigInt i, ret; + mpint *i, *ret; f = fp; destroy(*f->ret); @@ -525,7 +524,7 @@ void IPint_shl(void *fp) { F_IPint_shl *f; - BigInt ret; + mpint *ret; f = fp; destroy(*f->ret); @@ -543,7 +542,7 @@ void IPint_shr(void *fp) { F_IPint_shr *f; - BigInt ret; + mpint *ret; f = fp; destroy(*f->ret); @@ -647,7 +646,7 @@ void IPint_and(void *fp) { F_IPint_and *f; - BigInt ret; + mpint *ret; f = fp; destroy(*f->ret); @@ -665,7 +664,7 @@ void IPint_ori(void *fp) { F_IPint_ori *f; - BigInt ret; + mpint *ret; f = fp; destroy(*f->ret); @@ -683,7 +682,7 @@ void IPint_xor(void *fp) { F_IPint_xor *f; - BigInt ret; + mpint *ret; f = fp; destroy(*f->ret); @@ -701,7 +700,7 @@ void IPint_not(void *fp) { F_IPint_not *f; - BigInt ret; + mpint *ret; f = fp; destroy(*f->ret); @@ -714,60 +713,3 @@ IPint_not(void *fp) mpnot(MP(f->i1), ret); *f->ret = newIPint(ret); } - -/* - * return a random number between a and b - */ -void -getRandBetween(BigInt p, BigInt q, BigInt result, int type) -{ - BigInt T, slop, r, diff, one, two; - int length; - -if(0)print("1"); - diff = mpnew(0); - one = itomp(1, nil); - - /* smaller in p, larger in q */ - if (mpcmp(p, q) > 0) { - T = p; p = q; q = T; - } - - mpsub(q, p, diff); - - two = itomp(2, nil); - if(mpcmp(diff, two) < 0){ - mpfree(one); - mpfree(two); - itomp(0, result); - return; - } - mpfree(two); - - /* generate a random number between 0 and diff */ - T = mpnew(0); - slop = mpnew(0); - mpleft(one, mpsignif(diff), T); - length = mpsignif(T); - - mpmod(T, diff, slop); - mpfree(T); - - r = mpnew(0); - do { -if(0)print("3"); - mprand(length, type == PSEUDO? prng: genrandom, r); -if(0)print("4"); - } while (mpcmp(r, slop) < 0); - mpfree(slop); - - mpmod(r, diff, result); - mpfree(r); - mpfree(diff); - mpfree(one); - - /* add smaller number back in */ - mpadd(result, p, result); - -if(0)print("2"); -} diff --git a/libinterp/keyring.c b/libinterp/keyring.c index 57a2095d..b0f0a0fa 100644 --- a/libinterp/keyring.c +++ b/libinterp/keyring.c @@ -15,7 +15,6 @@ enum { PSEUDO=0, REALLY, }; -void getRandBetween(BigInt p, BigInt q, BigInt result, int type); Type *TSigAlg; Type *TCertificate; @@ -69,7 +68,8 @@ PK* checkPK(Keyring_PK *k); extern void setid(char*, int); extern vlong osusectime(void); -extern Keyring_IPint* newIPint(BigInt); +extern Keyring_IPint* newIPint(mpint*); +extern mpint* checkIPint(Keyring_IPint*); extern void freeIPint(Heap*, int); static char exBadSA[] = "bad signature algorithm"; @@ -92,7 +92,7 @@ struct XBFstate /* convert a Big to base64 ascii */ int -bigtobase64(BigInt b, char *buf, int len) +bigtobase64(mpint* b, char *buf, int len) { uchar *p; int n, rv, o; @@ -129,11 +129,11 @@ Err: int big64conv(Fmt *f) { - BigInt b; + mpint *b; char *buf; int n; - b = va_arg(f->args, BigInt); + b = va_arg(f->args, mpint*); n = (b->top+1)*Dbytes + 1; n = ((n+3)/3)*4 + 1; buf = malloc(n); @@ -143,19 +143,6 @@ big64conv(Fmt *f) return n; } -static BigInt -checkIPint(Keyring_IPint *v) -{ - IPint *ip; - - ip = (IPint*)v; - if(ip == H || ip == nil) - error(exNilref); - if(D2H(ip)->t != TIPint) - error(exType); - return ip->b; -} - static void* newthing(Type *t, int add) { @@ -168,7 +155,7 @@ newthing(Type *t, int add) } static Keyring_IPint* -ipcopymp(BigInt b) +ipcopymp(mpint* b) { if(b == nil) return H; @@ -176,12 +163,12 @@ ipcopymp(BigInt b) } /* convert a base64 string to a big */ -BigInt +mpint* base64tobig(char *str, char **strp) { int n; char *p; - BigInt b; + mpint *b; uchar hex[(MaxBigBytes*6 + 7)/8]; for(p = str; *p && *p != '\n'; p++) @@ -400,7 +387,7 @@ Keyring_genSKfromPK(void *fp) acquire(); } -/* converts a sequence of newline-separated base64-encoded BigInts to attr=hexval ... in f */ +/* converts a sequence of newline-separated base64-encoded mpints to attr=hexval ... in f */ static char* bigs2attr(Fmt *f, char *bigs, char **names) { @@ -900,7 +887,7 @@ static Certificate* sign(SK *sk, char *ha, ulong exp, uchar *a, int len) { Certificate *c; - BigInt b; + mpint *b; int n; SigAlg *sa; DigestState *ds; @@ -956,7 +943,7 @@ Keyring_sign(void *fp) { F_Keyring_sign *f; Certificate *c; - BigInt b; + mpint *b; int n; SigAlg *sa; SK *sk; @@ -1015,7 +1002,7 @@ Keyring_signm(void *fp) { F_Keyring_signm *f; Certificate *c; - BigInt b; + mpint *b; SigAlg *sa; SK *sk; void *v; @@ -1043,7 +1030,7 @@ Keyring_signm(void *fp) static int verify(PK *pk, Certificate *c, char *a, int len) { - BigInt b; + mpint *b; int n; SigAlg *sa, *pksa; DigestState *ds; @@ -1096,7 +1083,7 @@ Keyring_verify(void *fp) { F_Keyring_verify *f; Certificate *c; - BigInt b; + mpint *b; int n; SigAlg *sa, *pksa; PK *pk; @@ -1352,21 +1339,25 @@ void Keyring_dhparams(void *fp) { F_Keyring_dhparams *f; - EGpriv *egp; - BigInt p, alpha; + mpint *p, *alpha; + void *v; f = fp; - destroy(f->ret->t0); + v = f->ret->t0; f->ret->t0 = H; - destroy(f->ret->t1); + destroy(v); + v = f->ret->t1; f->ret->t1 = H; + destroy(v); + p = mpnew(0); + alpha = mpnew(0); release(); - egp = eggen(f->nbits, 0); + if(f->nbits == 1024) + DSAprimes(alpha, p, nil); + else + gensafeprime(p, alpha, f->nbits, 0); acquire(); - p = mpcopy(egp->pub.p); - alpha = mpcopy(egp->pub.alpha); - egprivfree(egp); f->ret->t0 = newIPint(alpha); f->ret->t1 = newIPint(p); } @@ -1548,7 +1539,7 @@ void Keyring_auth(void *fp) { F_Keyring_auth *f; - BigInt r0, r1, p, alpha, alphar0, alphar1, alphar0r1, low; + mpint *r0, *r1, *p, *alpha, *alphar0, *alphar1, *alphar0r1; SK *mysk; PK *mypk, *spk, *hispk; Certificate *cert, *hiscert, *alphacert; @@ -1568,7 +1559,7 @@ Keyring_auth(void *fp) f->ret->t0 = H; destroy(f->ret->t1); f->ret->t1 = H; - low = r0 = r1 = alphar0 = alphar1 = alphar0r1 = nil; + r0 = r1 = alphar0 = alphar1 = alphar0r1 = nil; /* check args */ if(f->fd == H || f->fd->fd < 0){ @@ -1636,15 +1627,14 @@ Keyring_auth(void *fp) } /* get alpha and p */ - p = ((IPint*)f->info->p)->b; - alpha = ((IPint*)f->info->alpha)->b; + p = checkIPint(f->info->p); + alpha = checkIPint(f->info->alpha); if(p->sign == -1) { err = "-ve modulus"; goto out; } - low = mpnew(0); r0 = mpnew(0); r1 = mpnew(0); alphar0 = mpnew(0); @@ -1653,8 +1643,7 @@ Keyring_auth(void *fp) /* generate alpha**r0 */ if(0)print("X"); release(); - mpright(p, mpsignif(p)/4, low); - getRandBetween(low, p, r0, PSEUDO); + mprand(mpsignif(p), genrandom, r0); mpexp(alpha, r0, p, alphar0); acquire(); if(0)print("Y"); @@ -1842,8 +1831,7 @@ out: destroy(alphacert); } free(buf); - if(low != nil){ - mpfree(low); + if(r0 != nil){ mpfree(r0); mpfree(r1); mpfree(alphar0); @@ -1949,7 +1937,7 @@ Keyring_readauthinfo(void *fp) SK *mysk; SigAlg *sa; Keyring_Authinfo *ai; - BigInt b; + mpint *b; f = fp; destroy(*f->ret); @@ -2704,7 +2692,7 @@ DSAsk_sign(void *fp) F_DSAsk_sign *f; Keyring_DSAsig *sig; DSApriv p; - BigInt m; + mpint *m; DSAsig *s; void *v; @@ -2730,7 +2718,7 @@ DSApk_verify(void *fp) F_DSApk_verify *f; DSApub p; DSAsig sig; - BigInt m; + mpint *m; f = fp; *f->ret = 0; @@ -2808,7 +2796,7 @@ EGsk_sign(void *fp) F_EGsk_sign *f; Keyring_EGsig *sig; EGpriv p; - BigInt m; + mpint *m; EGsig *s; void *v; @@ -2834,7 +2822,7 @@ EGpk_verify(void *fp) F_EGpk_verify *f; EGpub p; EGsig sig; - BigInt m; + mpint *m; f = fp; *f->ret = 0; @@ -2894,7 +2882,7 @@ RSApk_encrypt(void *fp) { F_RSApk_encrypt *f; RSApub p; - BigInt m, o; + mpint *m, *o; void *v; f = fp; @@ -2968,7 +2956,7 @@ RSAsk_decrypt(void *fp) { F_RSAsk_decrypt *f; RSApriv p; - BigInt m, o; + mpint *m, *o; void *v; f = fp; @@ -2990,7 +2978,7 @@ RSAsk_sign(void *fp) F_RSAsk_sign *f; Keyring_RSAsig *sig; RSApriv p; - BigInt m, s; + mpint *m, *s; void *v; f = fp; @@ -3012,7 +3000,7 @@ RSApk_verify(void *fp) { F_RSApk_verify *f; RSApub p; - BigInt sig, m, t; + mpint *sig, *m, *t; f = fp; *f->ret = 0; |
