summaryrefslogtreecommitdiff
path: root/asdcplib/src/KM_prng.cpp
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2016-01-04 16:19:04 +0000
committerCarl Hetherington <cth@carlh.net>2016-01-04 16:19:04 +0000
commitf4802823b481b47a240002d086a5caefee3e4b61 (patch)
tree06f7eedc862fd9cc1b083da91b753bded5fd8914 /asdcplib/src/KM_prng.cpp
parentfba3fceee2203833b74631a951ec1364521630be (diff)
Fix new asdcplib to build.
Diffstat (limited to 'asdcplib/src/KM_prng.cpp')
-rwxr-xr-xasdcplib/src/KM_prng.cpp48
1 files changed, 41 insertions, 7 deletions
diff --git a/asdcplib/src/KM_prng.cpp b/asdcplib/src/KM_prng.cpp
index 5212595f..10a97eb8 100755
--- a/asdcplib/src/KM_prng.cpp
+++ b/asdcplib/src/KM_prng.cpp
@@ -64,6 +64,7 @@ public:
AES_KEY m_Context;
byte_t m_ctr_buf[RNG_BLOCK_SIZE];
Mutex m_Lock;
+ unsigned int m_libdcp_test_rng_state;
h__RNG()
{
@@ -97,8 +98,12 @@ public:
} // end AutoMutex context
set_key(rng_key);
+
+#ifdef LIBDCP_POSIX
+ reset();
+#endif
}
-
+
//
void
set_key(const byte_t* key_fodder)
@@ -114,9 +119,10 @@ public:
AutoMutex Lock(m_Lock);
AES_set_encrypt_key(sha_buf, RNG_KEY_SIZE_BITS, &m_Context);
- *(ui32_t*)(m_ctr_buf + 12) = 1;
+ ui32_t* p = (ui32_t*) (m_ctr_buf + 12);
+ *p = 1;
}
-
+
//
void
fill_rand(byte_t* buf, ui32_t len)
@@ -128,17 +134,37 @@ public:
while ( gen_count + RNG_BLOCK_SIZE <= len )
{
AES_encrypt(m_ctr_buf, buf + gen_count, &m_Context);
- *(ui32_t*)(m_ctr_buf + 12) += 1;
+ ui32_t* p = (ui32_t*) (m_ctr_buf + 12);
+ *p += 1;
gen_count += RNG_BLOCK_SIZE;
}
-
+
if ( len != gen_count ) // partial count needed?
{
byte_t tmp[RNG_BLOCK_SIZE];
AES_encrypt(m_ctr_buf, tmp, &m_Context);
memcpy(buf + gen_count, tmp, len - gen_count);
}
+
+#ifdef LIBDCP_POSIX
+ if (libdcp_test)
+ {
+ for (unsigned int i = 0; i < len; ++i)
+ buf[i] = rand_r(&m_libdcp_test_rng_state);
+ }
+#endif
+
+#ifdef LIBDCP_WINDOWS
+ /* XXX */
+#endif
}
+
+#ifdef LIBDCP_POSIX
+ void reset ()
+ {
+ m_libdcp_test_rng_state = 1;
+ }
+#endif
};
@@ -173,13 +199,13 @@ Kumu::FortunaRNG::FillRandom(byte_t* buf, ui32_t len)
s_RNG->fill_rand(buf, gen_size);
buf += gen_size;
len -= gen_size;
-
+
// re-seed the generator
byte_t rng_key[RNG_KEY_SIZE];
s_RNG->fill_rand(rng_key, RNG_KEY_SIZE);
s_RNG->set_key(rng_key);
}
-
+
return front_of_buffer;
}
@@ -192,6 +218,14 @@ Kumu::FortunaRNG::FillRandom(Kumu::ByteString& Buffer)
return Buffer.Data();
}
+#ifdef LIBDCP_POSIX
+void
+Kumu::FortunaRNG::Reset()
+{
+ s_RNG->reset();
+}
+#endif
+
//------------------------------------------------------------------------------------------
//