X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=mdfour.c;h=36180c6e74b957392a69391c938f20924da9599c;hb=7e8a101c81015e0bdab9fd78704f38f39d5a8ffe;hp=cd0ce05a65c4ebdf16bf1a74396187af9b199a2a;hpb=9e279d8ea4a631713d98132cd1b01338e81f5c0b;p=xonotic%2Fdarkplaces.git diff --git a/mdfour.c b/mdfour.c index cd0ce05a..36180c6e 100644 --- a/mdfour.c +++ b/mdfour.c @@ -27,6 +27,8 @@ $Id$ */ +#include "quakedef.h" + #include /* XoXus: needed for memset call */ #include "mdfour.h" @@ -35,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)) @@ -51,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; @@ -61,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); @@ -101,10 +101,10 @@ 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, unsigned char *in) +static void copy64(uint32 *M, const unsigned char *in) { int i; @@ -131,15 +131,15 @@ void mdfour_begin(struct mdfour *md) } -static void mdfour_tail(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); @@ -148,48 +148,47 @@ static void mdfour_tail(unsigned char *in, int n) if (n <= 55) { copy4(buf+56, b); copy64(M, buf); - mdfour64(M); + mdfour64(md, M); } else { copy4(buf+120, b); copy64(M, buf); - mdfour64(M); + mdfour64(md, M); copy64(M, buf+64); - mdfour64(M); + mdfour64(md, M); } } -void mdfour_update(struct mdfour *md, unsigned char *in, int n) +void mdfour_update(struct mdfour *md, const unsigned char *in, int n) { uint32 M[16]; - if (n == 0) mdfour_tail(in, n); - - m = md; +// start of edit by Forest 'LordHavoc' Hale +// commented out to prevent crashing when length is 0 +// if (n == 0) mdfour_tail(in, n); +// end of edit by Forest 'LordHavoc' Hale 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); } -void mdfour(unsigned char *out, unsigned char *in, int n) +void mdfour(unsigned char *out, const unsigned char *in, int n) { struct mdfour md; mdfour_begin(&md);