From 263c1c74882c45b9d17ddd6ff5ca80a5f8db777c Mon Sep 17 00:00:00 2001 From: divverent Date: Wed, 4 Mar 2015 17:30:08 +0000 Subject: [PATCH] Make mdfour thread-safe. git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@12184 d7cf8633-e32d-0410-b094-e92efae38249 --- mdfour.c | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/mdfour.c b/mdfour.c index e057a94b..85f6c9e8 100644 --- a/mdfour.c +++ b/mdfour.c @@ -37,8 +37,6 @@ It assumes that a int is at least 32 bits long */ -static struct mdfour *m; - #define F(X,Y,Z) (((X)&(Y)) | ((~(X))&(Z))) #define G(X,Y,Z) (((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z))) #define H(X,Y,Z) ((X)^(Y)^(Z)) @@ -53,7 +51,7 @@ static struct mdfour *m; #define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + 0x6ED9EBA1,s) /* this applies md4 to 64 byte chunks */ -static void mdfour64(uint32 *M) +static void mdfour64(struct mdfour *md, uint32 *M) { int j; uint32 AA, BB, CC, DD; @@ -63,7 +61,7 @@ static void mdfour64(uint32 *M) for (j=0;j<16;j++) X[j] = M[j]; - A = m->A; B = m->B; C = m->C; D = m->D; + A = md->A; B = md->B; C = md->C; D = md->D; AA = A; BB = B; CC = C; DD = D; ROUND1(A,B,C,D, 0, 3); ROUND1(D,A,B,C, 1, 7); @@ -103,7 +101,7 @@ static void mdfour64(uint32 *M) for (j=0;j<16;j++) X[j] = 0; - m->A = A; m->B = B; m->C = C; m->D = D; + md->A = A; md->B = B; md->C = C; md->D = D; } static void copy64(uint32 *M, const unsigned char *in) @@ -133,15 +131,15 @@ void mdfour_begin(struct mdfour *md) } -static void mdfour_tail(const unsigned char *in, int n) +static void mdfour_tail(struct mdfour *md, const unsigned char *in, int n) { unsigned char buf[128]; uint32 M[16]; uint32 b; - m->totalN += n; + md->totalN += n; - b = m->totalN * 8; + b = md->totalN * 8; memset(buf, 0, 128); if (n) memcpy(buf, in, n); @@ -169,28 +167,24 @@ void mdfour_update(struct mdfour *md, const unsigned char *in, int n) // if (n == 0) mdfour_tail(in, n); // end of edit by Forest 'LordHavoc' Hale - m = md; - while (n >= 64) { copy64(M, in); - mdfour64(M); + mdfour64(md, M); in += 64; n -= 64; - m->totalN += 64; + md->totalN += 64; } - mdfour_tail(in, n); + mdfour_tail(md, in, n); } void mdfour_result(struct mdfour *md, unsigned char *out) { - m = md; - - copy4(out, m->A); - copy4(out+4, m->B); - copy4(out+8, m->C); - copy4(out+12, m->D); + copy4(out, md->A); + copy4(out+4, md->B); + copy4(out+8, md->C); + copy4(out+12, md->D); } -- 2.39.2