Patches for testing to allow predictable random number and date generation.
[asdcplib.git] / src / KM_prng.cpp
index fb4286ce386051c845353a06dd50ebdadf75de05..2b34e2e193b73c526fe4c0253b4c2adbd3376c6c 100755 (executable)
@@ -64,6 +64,7 @@ public:
   AES_KEY   m_Context;
   byte_t    m_ctr_buf[RNG_BLOCK_SIZE];
   Mutex     m_Lock;
+  unsigned int m_cth_test_rng_state;
 
   h__RNG()
   {
@@ -97,6 +98,7 @@ public:
     } // end AutoMutex context
 
     set_key(rng_key);
+    reset();
   }
        
   //
@@ -138,7 +140,20 @@ public:
        AES_encrypt(m_ctr_buf, tmp, &m_Context);
        memcpy(buf + gen_count, tmp, len - gen_count);
       }
+
+    if (cth_test)
+      {
+#ifdef __unix__
+       for (unsigned int i = 0; i < len; ++i)
+         buf[i] = rand_r(&m_cth_test_rng_state);
+#endif
+      }
   }
+
+  void reset()
+    {
+      m_cth_test_rng_state = 1;
+    }
 };
 
 
@@ -192,6 +207,12 @@ Kumu::FortunaRNG::FillRandom(Kumu::ByteString& Buffer)
   return Buffer.Data();
 }
 
+void
+Kumu::FortunaRNG::Reset()
+{
+  s_RNG->reset();
+}
+
 //------------------------------------------------------------------------------------------
 
 //