From a950557a2f197f283d5f3b54c00612b443934082 Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Fri, 11 Jan 2019 09:36:00 +0000 Subject: [PATCH] Fix byte ordering: encode state words in little-endian; don't memcpy! How did I miss this earlier!? --- src/microcode/md5.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/microcode/md5.c b/src/microcode/md5.c index 2f797b4db..a9a0fe5be 100644 --- a/src/microcode/md5.c +++ b/src/microcode/md5.c @@ -57,6 +57,17 @@ le32dec(const void *buf) return v; } +static inline void +le32enc(void *buf, uint32_t v) +{ + uint8_t *p = buf; + + p[0] = (v >> 0) & 0xff; + p[1] = (v >> 8) & 0xff; + p[2] = (v >> 16) & 0xff; + p[3] = (v >> 24) & 0xff; +} + static inline void le64enc(void *buf, uint64_t v) { @@ -250,6 +261,7 @@ md5_update(struct md5 *M, const void *buf, size_t len) void md5_final(struct md5 *M, uint8_t h[16]) { + unsigned n; assert(M->i < sizeof(M->block)); assert(M->i == M->b % 64); @@ -281,7 +293,8 @@ md5_final(struct md5 *M, uint8_t h[16]) md5_compress(M->state, M->block); /* Reveal the complete state. (Sorry, length-extension!) */ - (void)memcpy(h, M->state, sizeof(M->state)); + for (n = 0; n < sizeof(M->state)/sizeof(M->state[0]); n++) + le32enc(&h[4*n], M->state[n]); /* Zero it all. */ (void)explicit_memset(M, 0, sizeof M); -- 2.25.1