From f1b1b1d260d4762610fe550222f18ae2d9e17b23 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Thu, 24 May 2018 09:42:02 +0100 Subject: [PATCH] Simplify hashing operations in sshrsa.c and sshdss.c. We can now simply call the centralised functions to put uint32s and mpints into hash states, so there's no need to have duplicate local copies doing the same things less type-generically. --- sshdss.c | 36 ++++-------------------------------- sshrsa.c | 22 +++------------------- 2 files changed, 7 insertions(+), 51 deletions(-) diff --git a/sshdss.c b/sshdss.c index 20a5e7fa..ffe7157e 100644 --- a/sshdss.c +++ b/sshdss.c @@ -9,34 +9,6 @@ #include "ssh.h" #include "misc.h" -static void sha_mpint(SHA_State * s, Bignum b) -{ - unsigned char lenbuf[4]; - int len; - len = (bignum_bitcount(b) + 8) / 8; - PUT_32BIT(lenbuf, len); - SHA_Bytes(s, lenbuf, 4); - while (len-- > 0) { - lenbuf[0] = bignum_byte(b, len); - SHA_Bytes(s, lenbuf, 1); - } - smemclr(lenbuf, sizeof(lenbuf)); -} - -static void sha512_mpint(SHA512_State * s, Bignum b) -{ - unsigned char lenbuf[4]; - int len; - len = (bignum_bitcount(b) + 8) / 8; - PUT_32BIT(lenbuf, len); - SHA512_Bytes(s, lenbuf, 4); - while (len-- > 0) { - lenbuf[0] = bignum_byte(b, len); - SHA512_Bytes(s, lenbuf, 1); - } - smemclr(lenbuf, sizeof(lenbuf)); -} - static void getstring(const char **data, int *datalen, const char **p, int *length) { @@ -395,9 +367,9 @@ static void *dss_createkey(const struct ssh_signkey *self, getstring(&pb, &priv_len, &hash, &hashlen); if (hashlen == 20) { SHA_Init(&s); - sha_mpint(&s, dss->p); - sha_mpint(&s, dss->q); - sha_mpint(&s, dss->g); + put_mp_ssh2(&s, dss->p); + put_mp_ssh2(&s, dss->q); + put_mp_ssh2(&s, dss->g); SHA_Final(&s, digest); if (0 != memcmp(hash, digest, 20)) { dss_freekey(dss); @@ -569,7 +541,7 @@ Bignum *dss_gen_k(const char *id_string, Bignum modulus, Bignum private_key, */ SHA512_Init(&ss); SHA512_Bytes(&ss, id_string, strlen(id_string) + 1); - sha512_mpint(&ss, private_key); + put_mp_ssh2(&ss, private_key); SHA512_Final(&ss, digest512); /* diff --git a/sshrsa.c b/sshrsa.c index fada7390..39936bcd 100644 --- a/sshrsa.c +++ b/sshrsa.c @@ -94,20 +94,6 @@ int rsa_ssh1_encrypt(unsigned char *data, int length, struct RSAKey *key) return 1; } -static void sha512_mpint(SHA512_State * s, Bignum b) -{ - unsigned char lenbuf[4]; - int len; - len = (bignum_bitcount(b) + 8) / 8; - PUT_32BIT(lenbuf, len); - SHA512_Bytes(s, lenbuf, 4); - while (len-- > 0) { - lenbuf[0] = bignum_byte(b, len); - SHA512_Bytes(s, lenbuf, 1); - } - smemclr(lenbuf, sizeof(lenbuf)); -} - /* * Compute (base ^ exp) % mod, provided mod == p * q, with p,q * distinct primes, and iqmp is the multiplicative inverse of q mod p. @@ -232,12 +218,10 @@ static Bignum rsa_privkey_op(Bignum input, struct RSAKey *key) * byte = random_byte(); */ if (digestused >= lenof(digest512)) { - unsigned char seqbuf[4]; - PUT_32BIT(seqbuf, hashseq); SHA512_Init(&ss); SHA512_Bytes(&ss, "RSA deterministic blinding", 26); - SHA512_Bytes(&ss, seqbuf, sizeof(seqbuf)); - sha512_mpint(&ss, key->private_exponent); + put_uint32(&ss, hashseq); + put_mp_ssh2(&ss, key->private_exponent); SHA512_Final(&ss, digest512); hashseq++; @@ -247,7 +231,7 @@ static Bignum rsa_privkey_op(Bignum input, struct RSAKey *key) */ SHA512_Init(&ss); SHA512_Bytes(&ss, digest512, sizeof(digest512)); - sha512_mpint(&ss, input); + put_mp_ssh2(&ss, input); SHA512_Final(&ss, digest512); digestused = 0;