1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

Make the frankly ridiculous prototypes for modpow() and modmul() more sane

[originally from svn r752]
This commit is contained in:
Simon Tatham 2000-10-23 16:11:31 +00:00
parent bf2744aabf
commit e51b4da9f7
6 changed files with 33 additions and 34 deletions

4
ssh.h
View File

@ -153,8 +153,8 @@ Bignum newbn(int length);
Bignum copybn(Bignum b);
Bignum bignum_from_short(unsigned short n);
void freebn(Bignum b);
void modpow(Bignum base, Bignum exp, Bignum mod, Bignum result);
void modmul(Bignum a, Bignum b, Bignum mod, Bignum result);
Bignum modpow(Bignum base, Bignum exp, Bignum mod);
Bignum modmul(Bignum a, Bignum b, Bignum mod);
void decbn(Bignum n);
extern Bignum Zero, One;
int ssh1_read_bignum(unsigned char *data, Bignum *result);

14
sshbn.c
View File

@ -184,11 +184,12 @@ static void internal_mod(unsigned short *a, int alen,
* The most significant word of mod MUST be non-zero.
* We assume that the result array is the same size as the mod array.
*/
void modpow(Bignum base, Bignum exp, Bignum mod, Bignum result)
Bignum modpow(Bignum base, Bignum exp, Bignum mod)
{
unsigned short *a, *b, *n, *m;
int mshift;
int mlen, i, j;
Bignum result;
/* Allocate m of size mlen, copy mod to m */
/* We use big endian internally */
@ -252,14 +253,18 @@ void modpow(Bignum base, Bignum exp, Bignum mod, Bignum result)
}
/* Copy result to buffer */
result = newbn(mod[0]);
for (i = 0; i < mlen; i++)
result[result[0] - i] = a[i+mlen];
while (result[0] > 1 && result[result[0]] == 0) result[0]--;
/* Free temporary arrays */
for (i = 0; i < 2*mlen; i++) a[i] = 0; free(a);
for (i = 0; i < 2*mlen; i++) b[i] = 0; free(b);
for (i = 0; i < mlen; i++) m[i] = 0; free(m);
for (i = 0; i < mlen; i++) n[i] = 0; free(n);
return result;
}
/*
@ -267,11 +272,12 @@ void modpow(Bignum base, Bignum exp, Bignum mod, Bignum result)
* The most significant word of mod MUST be non-zero.
* We assume that the result array is the same size as the mod array.
*/
void modmul(Bignum p, Bignum q, Bignum mod, Bignum result)
Bignum modmul(Bignum p, Bignum q, Bignum mod)
{
unsigned short *a, *n, *m, *o;
int mshift;
int pqlen, mlen, i, j;
Bignum result;
/* Allocate m of size mlen, copy mod to m */
/* We use big endian internally */
@ -320,14 +326,18 @@ void modmul(Bignum p, Bignum q, Bignum mod, Bignum result)
}
/* Copy result to buffer */
result = newbn(mod[0]);
for (i = 0; i < mlen; i++)
result[result[0] - i] = a[i+2*pqlen-mlen];
while (result[0] > 1 && result[result[0]] == 0) result[0]--;
/* Free temporary arrays */
for (i = 0; i < 2*pqlen; i++) a[i] = 0; free(a);
for (i = 0; i < mlen; i++) m[i] = 0; free(m);
for (i = 0; i < pqlen; i++) n[i] = 0; free(n);
for (i = 0; i < pqlen; i++) o[i] = 0; free(o);
return result;
}
/*

View File

@ -99,8 +99,7 @@ Bignum dh_create_e(void) {
/*
* Done. Now compute e = g^x mod p.
*/
e = newbn(P[0]);
modpow(G, x, P, e);
e = modpow(G, x, P);
return e;
}
@ -110,6 +109,6 @@ Bignum dh_create_e(void) {
*/
Bignum dh_find_K(Bignum f) {
Bignum K = newbn(P[0]);
modpow(f, x, P, K);
K = modpow(f, x, P);
return K;
}

View File

@ -190,7 +190,7 @@ static int dss_verifysig(char *sig, int siglen, char *data, int datalen) {
char *p;
int slen;
char hash[20];
Bignum r, s, w, i1, i2, i3, u1, u2, sha, v;
Bignum r, s, w, gu1p, yu2p, gu1yu2p, u1, u2, sha, v;
int ret;
if (!dss_p)
@ -243,34 +243,28 @@ static int dss_verifysig(char *sig, int siglen, char *data, int datalen) {
/*
* Step 2. u1 <- SHA(message) * w mod q.
*/
u1 = newbn(dss_q[0]);
SHA_Simple(data, datalen, hash);
p = hash; slen = 20; sha = get160(&p, &slen);
diagbn("sha=", sha);
modmul(sha, w, dss_q, u1);
u1 = modmul(sha, w, dss_q);
diagbn("u1=", u1);
/*
* Step 3. u2 <- r * w mod q.
*/
u2 = newbn(dss_q[0]);
modmul(r, w, dss_q, u2);
u2 = modmul(r, w, dss_q);
diagbn("u2=", u2);
/*
* Step 4. v <- (g^u1 * y^u2 mod p) mod q.
*/
i1 = newbn(dss_p[0]);
i2 = newbn(dss_p[0]);
i3 = newbn(dss_p[0]);
v = newbn(dss_q[0]);
modpow(dss_g, u1, dss_p, i1);
diagbn("gu1p=", i1);
modpow(dss_y, u2, dss_p, i2);
diagbn("yu2p=", i2);
modmul(i1, i2, dss_p, i3);
diagbn("gu1yu2p=", i3);
modmul(i3, One, dss_q, v);
gu1p = modpow(dss_g, u1, dss_p);
diagbn("gu1p=", gu1p);
yu2p = modpow(dss_y, u2, dss_p);
diagbn("yu2p=", yu2p);
gu1yu2p = modmul(gu1p, yu2p, dss_p);
diagbn("gu1yu2p=", gu1yu2p);
v = modmul(gu1yu2p, One, dss_q);
diagbn("gu1yu2q=v=", v);
diagbn("r=", r);
@ -282,9 +276,9 @@ static int dss_verifysig(char *sig, int siglen, char *data, int datalen) {
freebn(w);
freebn(sha);
freebn(i1);
freebn(i2);
freebn(i3);
freebn(gu1p);
freebn(yu2p);
freebn(gu1yu2p);
freebn(v);
freebn(r);
freebn(s);

View File

@ -670,8 +670,7 @@ Bignum primegen(int bits, int modulus, int residue,
/*
* Compute w^q mod p.
*/
wqp = newbn(p[0]);
modpow(w, q, p, wqp);
wqp = modpow(w, q, p);
freebn(w);
/*
@ -683,8 +682,7 @@ Bignum primegen(int bits, int modulus, int residue,
continue;
}
for (i = 0; i < k; i++) {
wqp2 = newbn(p[0]);
modmul(wqp, wqp, p, wqp2);
wqp2 = modmul(wqp, wqp, p);
freebn(wqp);
wqp = wqp2;
if (bignum_cmp(wqp, One) == 0)

View File

@ -65,7 +65,6 @@ void rsaencrypt(unsigned char *data, int length, struct RSAKey *key) {
w = (key->bytes+1)/2;
b1 = newbn(w);
b2 = newbn(w);
p = data;
for (i=1; i<=w; i++)
@ -78,7 +77,7 @@ void rsaencrypt(unsigned char *data, int length, struct RSAKey *key) {
b1[1+i/2] |= byte;
}
modpow(b1, key->exponent, key->modulus, b2);
b2 = modpow(b1, key->exponent, key->modulus);
p = data;
for (i=key->bytes; i-- ;) {
@ -96,8 +95,7 @@ void rsaencrypt(unsigned char *data, int length, struct RSAKey *key) {
Bignum rsadecrypt(Bignum input, struct RSAKey *key) {
Bignum ret;
ret = newbn(key->modulus[0]);
modpow(input, key->private_exponent, key->modulus, ret);
ret = modpow(input, key->private_exponent, key->modulus);
return ret;
}