summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/sha256.cc
diff options
context:
space:
mode:
authorRobert Vollmert <rvollmert@gmx.net>2009-12-14 20:05:31 +0100
committerRobert Vollmert <rvollmert@gmx.net>2009-12-14 20:05:31 +0100
commit2033e2af0461bc98d09af36dcb601f1d308f2c04 (patch)
tree50bbbc8b8be2d2c0e96c04f87e0d04951e5285af /crawl-ref/source/sha256.cc
parent09636d72edc4fc3ab748bc75daee8035ac89dacd (diff)
downloadcrawl-ref-2033e2af0461bc98d09af36dcb601f1d308f2c04.tar.gz
crawl-ref-2033e2af0461bc98d09af36dcb601f1d308f2c04.zip
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.
Diffstat (limited to 'crawl-ref/source/sha256.cc')
-rw-r--r--crawl-ref/source/sha256.cc63
1 files changed, 58 insertions, 5 deletions
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 <stdint.h>
typedef uint32_t u32;
-typedef uint64_t u64;
#include "mt19937ar.h"
#ifdef MORE_HARDENED_PRNG
+#include <stack>
+
#include <cstring>
#include <cstdio>
#include <cstdlib>
@@ -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> 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();