X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=sshmd5.c;h=b39dfd3e52dba4a399706c3769193c05ff738168;hb=a1e622523e48125b7322cf4e53d504b9a6614c15;hp=215b81fddcb63fc916c6c66cbae2f91bdacbfee2;hpb=11d5c791acb6fbc3376ab0843d2040aa0e40e2f2;p=PuTTY.git diff --git a/sshmd5.c b/sshmd5.c index 215b81fd..b39dfd3e 100644 --- a/sshmd5.c +++ b/sshmd5.c @@ -210,6 +210,7 @@ void MD5Simple(void const *p, unsigned len, unsigned char output[16]) MD5Init(&s); MD5Update(&s, (unsigned char const *)p, len); MD5Final(output, &s); + smemclr(&s, sizeof(s)); } /* ---------------------------------------------------------------------- @@ -220,13 +221,14 @@ void MD5Simple(void const *p, unsigned len, unsigned char output[16]) * useful elsewhere (SOCKS5 CHAP authentication uses HMAC-MD5). */ -void *hmacmd5_make_context(void) +void *hmacmd5_make_context(void *cipher_ctx) { - return snewn(2, struct MD5Context); + return snewn(3, struct MD5Context); } void hmacmd5_free_context(void *handle) { + smemclr(handle, 3*sizeof(struct MD5Context)); sfree(handle); } @@ -249,7 +251,7 @@ void hmacmd5_key(void *handle, void const *keyv, int len) MD5Init(&keys[1]); MD5Update(&keys[1], foo, 64); - memset(foo, 0, 64); /* burn the evidence */ + smemclr(foo, 64); /* burn the evidence */ } static void hmacmd5_key_16(void *handle, unsigned char *key) @@ -257,24 +259,50 @@ static void hmacmd5_key_16(void *handle, unsigned char *key) hmacmd5_key(handle, key, 16); } -static void hmacmd5_do_hmac_internal(void *handle, - unsigned char const *blk, int len, - unsigned char const *blk2, int len2, - unsigned char *hmac) +static void hmacmd5_start(void *handle) +{ + struct MD5Context *keys = (struct MD5Context *)handle; + + keys[2] = keys[0]; /* structure copy */ +} + +static void hmacmd5_bytes(void *handle, unsigned char const *blk, int len) +{ + struct MD5Context *keys = (struct MD5Context *)handle; + MD5Update(&keys[2], blk, len); +} + +static void hmacmd5_genresult(void *handle, unsigned char *hmac) { struct MD5Context *keys = (struct MD5Context *)handle; struct MD5Context s; unsigned char intermediate[16]; - s = keys[0]; /* structure copy */ - MD5Update(&s, blk, len); - if (blk2) MD5Update(&s, blk2, len2); + s = keys[2]; /* structure copy */ MD5Final(intermediate, &s); s = keys[1]; /* structure copy */ MD5Update(&s, intermediate, 16); MD5Final(hmac, &s); } +static int hmacmd5_verresult(void *handle, unsigned char const *hmac) +{ + unsigned char correct[16]; + hmacmd5_genresult(handle, correct); + return smemeq(correct, hmac, 16); +} + +static void hmacmd5_do_hmac_internal(void *handle, + unsigned char const *blk, int len, + unsigned char const *blk2, int len2, + unsigned char *hmac) +{ + hmacmd5_start(handle); + hmacmd5_bytes(handle, blk, len); + if (blk2) hmacmd5_bytes(handle, blk2, len2); + hmacmd5_genresult(handle, hmac); +} + void hmacmd5_do_hmac(void *handle, unsigned char const *blk, int len, unsigned char *hmac) { @@ -286,11 +314,7 @@ static void hmacmd5_do_hmac_ssh(void *handle, unsigned char const *blk, int len, { unsigned char seqbuf[16]; - seqbuf[0] = (unsigned char) ((seq >> 24) & 0xFF); - seqbuf[1] = (unsigned char) ((seq >> 16) & 0xFF); - seqbuf[2] = (unsigned char) ((seq >> 8) & 0xFF); - seqbuf[3] = (unsigned char) ((seq) & 0xFF); - + PUT_32BIT_MSB_FIRST(seqbuf, seq); hmacmd5_do_hmac_internal(handle, seqbuf, 4, blk, len, hmac); } @@ -305,13 +329,14 @@ static int hmacmd5_verify(void *handle, unsigned char *blk, int len, { unsigned char correct[16]; hmacmd5_do_hmac_ssh(handle, blk, len, seq, correct); - return !memcmp(correct, blk + len, 16); + return smemeq(correct, blk + len, 16); } const struct ssh_mac ssh_hmac_md5 = { hmacmd5_make_context, hmacmd5_free_context, hmacmd5_key_16, hmacmd5_generate, hmacmd5_verify, - "hmac-md5", - 16, + hmacmd5_start, hmacmd5_bytes, hmacmd5_genresult, hmacmd5_verresult, + "hmac-md5", "hmac-md5-etm@openssh.com", + 16, 16, "HMAC-MD5" };