Browse Source

Added code to detect already initialized random number generator

mingodad 8 years ago
parent
commit
e6ed145ec7
2 changed files with 8 additions and 0 deletions
  1. 1 0
      myaxtls/crypto.h
  2. 7 0
      myaxtls/crypto_misc.c

+ 1 - 0
myaxtls/crypto.h

@@ -255,6 +255,7 @@ void RSA_print(const RSA_CTX *ctx);
 /**************************************************************************
  * RNG declarations 
  **************************************************************************/
+EXP_FUNC int STDCALL RNG_is_initialized(void);;
 EXP_FUNC void STDCALL RNG_initialize(void);
 EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size);
 EXP_FUNC void STDCALL RNG_terminate(void);

+ 7 - 0
myaxtls/crypto_misc.c

@@ -42,6 +42,9 @@
 #include "wincrypt.h"
 #endif
 
+static int rng_started = 0;
+EXP_FUNC int STDCALL RNG_is_initialized() {return rng_started;}
+
 #ifndef WIN32
 static int rng_fd = -1;
 #elif defined(CONFIG_WIN32_USE_CRYPTO_LIB)
@@ -103,6 +106,7 @@ int get_file(const char *filename, uint8_t **buf)
  */
 EXP_FUNC void STDCALL RNG_initialize()
 {
+    if(rng_started) return;
 #if !defined(WIN32) && defined(CONFIG_USE_DEV_URANDOM)
     rng_fd = ax_open("/dev/urandom", O_RDONLY);
 #elif defined(WIN32) && defined(CONFIG_WIN32_USE_CRYPTO_LIB)
@@ -130,6 +134,7 @@ EXP_FUNC void STDCALL RNG_initialize()
     srand((unsigned int)&i); 
 #endif
 #endif
+    rng_started = 1;
 }
 
 /**
@@ -150,11 +155,13 @@ EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size)
  */
 EXP_FUNC void STDCALL RNG_terminate(void)
 {
+    if(!rng_started) return;
 #ifndef WIN32
     close(rng_fd);
 #elif defined(CONFIG_WIN32_USE_CRYPTO_LIB)
     CryptReleaseContext(gCryptProv, 0);
 #endif
+	rng_started = 0;
 }
 
 /**