diff --git a/.gitignore b/.gitignore index f8fdab27..68ce0b65 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ config/saved*.cfg config/servers.cfg config/configtemplates.zip config/mapmodelattributes.cfg +config/entropy.dat packages/maps/*.cfg !packages/maps/new_content.cfg packages/maps/*.cgz diff --git a/source/src/crypto.cpp b/source/src/crypto.cpp index adc31cc7..7ef444eb 100644 --- a/source/src/crypto.cpp +++ b/source/src/crypto.cpp @@ -155,7 +155,87 @@ namespace tiger #undef sb4 #undef tround +/////////////////////////////////////////// SHA512 //////////////////////////////////////////////////////////////////// +// adapted from pseudocode and various public domain sources (probably all originating in LibTomCrypt) +// (consider this code public domain, too) + +#define RR64(x, n) (((x) >> (n)) | ((x) << (64 - (n)))) +#define SHA512ROUND(a, b, c, d, e, f, g, h, i) \ + d += h = h + (RR64(e,14) ^ RR64(e,18) ^ RR64(e,41)) + (g ^ (e & (f ^ g))) + K[i] + W[i]; \ + h += (RR64(a,28) ^ RR64(a,34) ^ RR64(a,39)) + (((a | b) & c) | (a & b)); +#define SHA512SIZE (512 / 8) + +void sha512_compress(uint64_t *state, const uchar *inbuf) // sha512 main function: compress one chunk of 128 bytes +{ + static const uint64_t K[80] = + { + 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, + 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, + 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, + 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, + 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, + 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, + 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, + 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, + 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, + 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL + }; + + uint64_t S[8], W[80]; + + loopi(8) S[i] = state[i]; + loopi(16) W[i] = bigswap(((uint64_t*)inbuf)[i]); + loopi(64) W[i + 16] = W[i] + W[i + 9] + (RR64(W[i + 14],19) ^ RR64(W[i + 14],61) ^ (W[i + 14] >> 6)) + (RR64(W[i + 1],1) ^ RR64(W[i + 1], 8) ^ (W[i + 1] >> 7)); + + for(int i = 0; i < 80; i += 8) + { + SHA512ROUND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0); + SHA512ROUND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1); + SHA512ROUND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2); + SHA512ROUND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3); + SHA512ROUND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4); + SHA512ROUND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5); + SHA512ROUND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6); + SHA512ROUND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7); + } + loopi(8) state[i] += S[i]; +} + +void sha512(uchar *hash, const uchar *msg, int msglen) +{ + uint64_t state[8] = { 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, + 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL }; + uint64_t total = msglen * 8ULL; + for(; msglen >= 128; msglen -= 128) + { + sha512_compress(state, msg); + msg += 128; + } + uchar endmsg[128] = { 0 }; + memcpy(endmsg, msg, msglen); + endmsg[msglen] = 0x80; + if(msglen > 111) + { + sha512_compress(state, endmsg); + memset(endmsg, 0, 128); + } + *((uint64_t*)(endmsg + 120)) = bigswap(total); + sha512_compress(state, endmsg); + loopi(8) ((uint64_t*)hash)[i] = bigswap(state[i]); +} +#undef RR64 +#undef SHA512ROUND + #ifndef STANDALONE +COMMANDF(sha512, "s", (char *msg) // SHA512 debug command +{ + uchar hash[SHA512SIZE]; + sha512(hash, (uchar*)msg, strlen(msg)); + defformatstring(erg)("SHA512: "); + loopi(SHA512SIZE) concatformatstring(erg, "%02x", hash[i]); + conoutf("%s", erg); +}); + COMMANDF(tiger, "s", (char *msg) // tiger debug command { tiger::hashval hash; @@ -166,6 +246,146 @@ COMMANDF(tiger, "s", (char *msg) // tiger debug command }); #endif + +////////////////////////// crypto rand (Mersenne twister) //////////////////////////////////////// + +#define MT_N (624) +#define MT_M (397) +#define MT_K (0x9908B0DFU) + +static uint mt_state[2 * MT_N]; +static int mt_next = MT_N; + +void seedMT_do(uint seed, uint *statearray, uint arraysize) +{ + statearray[0] = seed; + for(uint i = 1; i < arraysize; i++) + statearray[i] = seed = 1812433253U * (seed ^ (seed >> 30)) + i; +} + +void seedMT(uint seed) +{ + loopi(MT_N) mt_state[i + MT_N] = mt_state[i]; + mt_state[0] = seed; + seedMT_do(seed, mt_state, MT_N); + mt_next = 0; +} + +uint randomMT() +{ + int cur = mt_next; + if(++mt_next >= MT_N) + { + if(mt_next > MT_N) { seedMT(5489U + time(NULL)); cur = mt_next++; } + else mt_next = 0; + } + uint y = (mt_state[cur] & 0x80000000U) | (mt_state[mt_next] & 0x7FFFFFFFU); + mt_state[cur] = y = mt_state[cur < MT_N - MT_M ? cur + MT_M : cur + MT_M - MT_N] ^ (y >> 1) ^ (-int(y & 1U) & MT_K); + y ^= (y >> 11); + y ^= (y << 7) & 0x9D2C5680U; + y ^= (y << 15) & 0xEFC60000U; + y ^= (y >> 18); + return y; +} + +void popMT() // undo last seedMT() +{ + loopi(MT_N) mt_state[i] = mt_state[i + MT_N]; +} + +/////////////////////////////////////////////// Entropy collector //////////////////////////////////////////////////////////////////////////////// +// * collects entropy (true random data) through mouse moves and keyboard action (and from other sources) +// * used to seed the pseudorandom generator +// * used to generate very good private keys + +#define ENTCHUNKS 32 // pool size (in 128-bytes chunks) +#define ENTPOOLSIZE (ENTCHUNKS * 128) +#define ENTROPYSAVEFILE "config" PATHDIVS "entropy.dat" + +static uchar *entpool = NULL; + +static void xor_block(uchar *d, const uchar *s, int len) +{ + uint64_t *dd = (uint64_t *)d, *ss = (uint64_t *)s; + for( ; len > 7; len -= 8) *dd++ ^= *ss++; + d = (uchar*)dd, s = (uchar*)ss; + while(len-- > 0) *d++ ^= *s++; +} + +static void mix_block(uchar *d, const uchar *s, int len) +{ + static uchar sum = 0; + int t; + while(len-- > 0) + { + t = *d * *s++ + sum; + *d++ += t >> 4; + sum += t >> 7; + } +} + +void entropy_init(uint seed) +{ + DELETEP(entpool); + entpool = new uchar[ENTPOOLSIZE + 128]; + seedMT_do(seed, (uint*)entpool, ENTPOOLSIZE / sizeof(uint)); + int len = 0; + char *buf = loadfile(ENTROPYSAVEFILE, &len); + if(buf && len) entropy_add_block((uchar*)buf, len); + DELETEA(buf); + entropy_get((uchar*)mt_state, MT_N * sizeof(uint)); +} + +void entropy_save() +{ + stream *f = openfile(ENTROPYSAVEFILE, "w"); + if(f) f->write(entpool, ENTPOOLSIZE); + DELETEP(f); +} + +void entropy_add_byte(uchar b) +{ + static int next = 0; + + int r = entpool[next] & 7; + entpool[next++] ^= b << r | b >> (8 - r); + if(next >= ENTPOOLSIZE) next = 0; +} + +void entropy_add_block(const uchar *s, int len) +{ + for( ; len > 0; s += 128, len -= 128) + { + int bl = min(len, 128); + xor_block(entpool + rnd(ENTPOOLSIZE / 8 - 16) * 8, s, bl); + mix_block(entpool + rnd(ENTPOOLSIZE - bl), s, bl); + } +} + +void entropy_get(uchar *buf, int len) +{ + ASSERT(len <= ENTPOOLSIZE); + uchar *tempbuf = new uchar[len + SHA512SIZE]; + memcpy(tempbuf, entpool, len); + memcpy(entpool + ENTPOOLSIZE, entpool, 128); + sha512(tempbuf, entpool, ENTPOOLSIZE); + loopi(len) sha512_compress((uint64_t*)(tempbuf + (i & ~7)), entpool + rnd(ENTPOOLSIZE / 8) * 8); + memcpy(buf, tempbuf, len); + delete[] tempbuf; +} + +#ifndef STANDALONE +COMMANDF(entropy, "", () +{ + uchar buf[32]; + entropy_get(buf, 32); + defformatstring(erg)("entropy: "); + loopi(32) concatformatstring(erg, "%02x", buf[i]); + conoutf("%s", erg); +}); +#endif + + /////////////////// misc helper functions //////////////////////////////////////////////////////////////// static const char *hashchunktoa(tiger::chunk h) // portable solution instead of printf("%llx") @@ -193,46 +413,7 @@ const char *genpwdhash(const char *name, const char *pwd, int salt) return temp; } -////////////////////////// crypto rand //////////////////////////////////////// -#define N (624) -#define M (397) -#define K (0x9908B0DFU) -static uint state[2 * N]; -static int next = N; -void seedMT(uint seed) -{ - loopi(N) state[i + N] = state[i]; - state[0] = seed; - for(uint i = 1; i < N; i++) - state[i] = seed = 1812433253U * (seed ^ (seed >> 30)) + i; - next = 0; -} - -uint randomMT() -{ - int cur = next; - if(++next >= N) - { - if(next > N) { seedMT(5489U + time(NULL)); cur = next++; } - else next = 0; - } - uint y = (state[cur] & 0x80000000U) | (state[next] & 0x7FFFFFFFU); - state[cur] = y = state[cur < N-M ? cur + M : cur + M-N] ^ (y >> 1) ^ (-int(y & 1U) & K); - y ^= (y >> 11); - y ^= (y << 7) & 0x9D2C5680U; - y ^= (y << 15) & 0xEFC60000U; - y ^= (y >> 18); - return y; -} - -void popMT() // undo last seedMT() -{ - loopi(N) state[i] = state[i + N]; -} -#undef N -#undef M -#undef K diff --git a/source/src/main.cpp b/source/src/main.cpp index 7bb0fa68..a43b736f 100644 --- a/source/src/main.cpp +++ b/source/src/main.cpp @@ -43,6 +43,7 @@ void quit() // normal exit else writecfg(); savehistory(); writemapmodelattributes(); + entropy_save(); writeallxmaps(); cleanup(NULL); popscontext(); @@ -188,7 +189,8 @@ void bmp_screenshot(const char *imagepath, bool mapshot = false) int th = mapshot ? ih : ih*screenshotscale; SDL_Surface *image = creatergbsurface(tw, th); if(!image) return; - uchar *tmp = new uchar[iw*ih*3]; + int tmpsize = iw * ih * 3; + uchar *tmp = new uchar[tmpsize]; uchar *dst = (uchar *)image->pixels; if(mapshot) { @@ -222,6 +224,7 @@ void bmp_screenshot(const char *imagepath, bool mapshot = false) memcpy(dst, &tmp[3*tw*(th-i-1)], 3*tw); dst += image->pitch; } + entropy_add_block(tmp, tmpsize); delete[] tmp; const char *filename = screenshotpath(imagepath, "bmp"); stream *file = openfile(filename, "wb"); @@ -247,7 +250,8 @@ void jpeg_screenshot(const char *imagepath, bool mapshot = false) int tw = mapshot ? iw : iw*screenshotscale; int th = mapshot ? ih : ih*screenshotscale; - uchar *pixels = new uchar[3*tw*th]; + int pixelssize = 3 * tw * th; + uchar *pixels = new uchar[pixelssize]; if(mapshot) { @@ -296,6 +300,7 @@ void jpeg_screenshot(const char *imagepath, bool mapshot = false) jpegencoder->encode(filename, (colorRGB *)pixels, tw, th, jpegquality); delete jpegencoder; + entropy_add_block(pixels, pixelssize); delete[] pixels; } @@ -412,7 +417,8 @@ void png_screenshot(const char *imagepath, bool mapshot = false) SDL_Surface *image = creatergbsurface(tw, th); if(!image) return; - uchar *tmp = new uchar[tw*th*3]; + int tmpsize = tw * th * 3; + uchar *tmp = new uchar[tmpsize]; uchar *dst = (uchar *)image->pixels; if(mapshot) @@ -447,6 +453,7 @@ void png_screenshot(const char *imagepath, bool mapshot = false) memcpy(dst, &tmp[3*tw*(th-i-1)], 3*tw); dst += image->pitch; } + entropy_add_block(tmp, tmpsize); delete[] tmp; const char *filename = screenshotpath(imagepath, "png"); @@ -847,6 +854,7 @@ void checkinput() case SDL_KEYDOWN: case SDL_KEYUP: extern bool senst; + entropy_add_byte(event.key.keysym.sym ^ totalmillis); if (event.key.keysym.sym <= SDLK_5 && event.key.keysym.sym >= SDLK_1 && senst) { if (event.key.state==SDL_PRESSED) @@ -876,6 +884,7 @@ void checkinput() if(grabinput && !skipmousemotion(event)) { int dx = event.motion.xrel, dy = event.motion.yrel; + entropy_add_byte(dx ^ (256 - dy)); checkmousemotion(dx, dy); resetmousemotion(); tdx+=dx;tdy+=dy; @@ -891,7 +900,11 @@ void checkinput() break; } } - if(tdx || tdy) mousemove(tdx, tdy); + if(tdx || tdy) + { + entropy_add_byte(tdy + 5 * tdx); + mousemove(tdx, tdy); + } } VARF(gamespeed, 10, 100, 1000, if(multiplayer()) gamespeed = 100); @@ -1096,6 +1109,7 @@ int main(int argc, char **argv) crosshairnames[CROSSHAIR_SCOPE] = "scope"; crosshairnames[CROSSHAIR_EDIT] = "edit"; crosshairnames[CROSSHAIR_NUM] = gunnames[NUMGUNS] = ""; + entropy_init(time(NULL) + (uint)(size_t)&serverport + (uint)(size_t)entropy_init); pushscontext(IEXC_CFG); @@ -1353,6 +1367,7 @@ int main(int argc, char **argv) if(millis