From 2033e2af0461bc98d09af36dcb601f1d308f2c04 Mon Sep 17 00:00:00 2001 From: Robert Vollmert Date: Mon, 14 Dec 2009 20:05:31 +0100 Subject: Implement rng pushing and popping for sha256 hardened prng (Adeon) the rng.cc used to call just push_mt_state which bypassed the hashing process and didn't return the state as it was after popping the state Fixes bug #52. --- crawl-ref/source/sha256.cc | 63 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 5 deletions(-) (limited to 'crawl-ref/source/sha256.cc') diff --git a/crawl-ref/source/sha256.cc b/crawl-ref/source/sha256.cc index a36d316d92..96443587a0 100644 --- a/crawl-ref/source/sha256.cc +++ b/crawl-ref/source/sha256.cc @@ -11,12 +11,13 @@ #include typedef uint32_t u32; -typedef uint64_t u64; #include "mt19937ar.h" #ifdef MORE_HARDENED_PRNG +#include + #include #include #include @@ -137,12 +138,50 @@ void sha256chunk(const char* chunk, sha256state* ss) } } -// 256 bits -u32 mt_sha256_block[8], mt_block[8]; -u32 mt_block_index = 0; +struct sha256mt_state +{ + // 256 bits + u32 mt_sha256_block[8], mt_block[8]; + u32 mt_block_index; + + sha256mt_state() + { + mt_block_index = 0; + } +}; + +sha256mt_state effective_state; + +std::stack sha256mt_state_stack; + +void reset_sha256_state() +{ + effective_state.mt_block_index = 0; +} + +void push_sha256_state() +{ + sha256mt_state_stack.push(effective_state); + push_mt_state(); +} + +void pop_sha256_state() +{ + if (sha256mt_state_stack.empty()) + return; + + effective_state = sha256mt_state_stack.top(); + + sha256mt_state_stack.pop(); + pop_mt_state(); +} unsigned long sha256_genrand() { + u32 &mt_block_index = effective_state.mt_block_index; + u32 *mt_sha256_block = effective_state.mt_sha256_block; + u32 *mt_block = effective_state.mt_block; + // Needs some hashing if (!(mt_block_index % 8)) { @@ -166,7 +205,21 @@ unsigned long sha256_genrand() return mt_sha256_block[mt_block_index++]; } #else // MORE_HARDENED_PRNG -// Stub this to MT function +// Stub these to MT functions +void push_sha256_state() +{ + push_mt_state(); +} + +void pop_sha256_state() +{ + pop_mt_state(); +} + +void reset_sha256_state() +{ +} + unsigned long sha256_genrand() { return genrand_int32(); -- cgit v1.2.3-54-g00ecf