Browse Source

put PRNG descriptor into state

Steffen Jaeckel 6 years ago
parent
commit
36e529420e

+ 0 - 1
helper.pl

@@ -133,7 +133,6 @@ sub check_descriptors {
   my $fails = 0;
   $fails = $fails + check_descriptor("ciphers", "cipher");
   $fails = $fails + check_descriptor("hashes", "hash");
-  $fails = $fails + check_descriptor("prngs", "prng");
   return $fails;
 }
 

+ 28 - 38
src/headers/tomcrypt_prng.h

@@ -48,31 +48,10 @@ struct sober128_prng {
 };
 #endif
 
-typedef struct {
-   union {
-      char dummy[1];
-#ifdef LTC_YARROW
-      struct yarrow_prng    yarrow;
-#endif
-#ifdef LTC_RC4
-      struct rc4_prng       rc4;
-#endif
-#ifdef LTC_CHACHA20_PRNG
-      struct chacha20_prng  chacha;
-#endif
-#ifdef LTC_FORTUNA
-      struct fortuna_prng   fortuna;
-#endif
-#ifdef LTC_SOBER128
-      struct sober128_prng  sober128;
-#endif
-   } u;
-   short ready;            /* ready flag 0-1 */
-   LTC_MUTEX_TYPE(lock)    /* lock */
-} prng_state;
+typedef struct ltc_prng_state prng_state;
 
 /** PRNG descriptor */
-extern struct ltc_prng_descriptor {
+struct ltc_prng_descriptor {
     /** Name of the PRNG */
     const char *name;
     /** size in bytes of exported state */
@@ -124,7 +103,31 @@ extern struct ltc_prng_descriptor {
         @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
     */
     int (*test)(void);
-} prng_descriptor[];
+};
+
+struct ltc_prng_state {
+   union {
+      char dummy[1];
+#ifdef LTC_YARROW
+      struct yarrow_prng    yarrow;
+#endif
+#ifdef LTC_RC4
+      struct rc4_prng       rc4;
+#endif
+#ifdef LTC_CHACHA20_PRNG
+      struct chacha20_prng  chacha;
+#endif
+#ifdef LTC_FORTUNA
+      struct fortuna_prng   fortuna;
+#endif
+#ifdef LTC_SOBER128
+      struct sober128_prng  sober128;
+#endif
+   } u;
+   short ready;            /* ready flag 0-1 */
+   struct ltc_prng_descriptor desc;
+   LTC_MUTEX_TYPE(lock)    /* lock */
+};
 
 #ifdef LTC_YARROW
 int yarrow_start(prng_state *prng);
@@ -135,7 +138,6 @@ int yarrow_done(prng_state *prng);
 int  yarrow_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
 int  yarrow_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
 int  yarrow_test(void);
-extern const struct ltc_prng_descriptor yarrow_desc;
 #endif
 
 #ifdef LTC_FORTUNA
@@ -149,7 +151,6 @@ int fortuna_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
 int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
 int fortuna_update_seed(const unsigned char *in, unsigned long inlen, prng_state *prng);
 int fortuna_test(void);
-extern const struct ltc_prng_descriptor fortuna_desc;
 #endif
 
 #ifdef LTC_RC4
@@ -161,7 +162,6 @@ int  rc4_done(prng_state *prng);
 int  rc4_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
 int  rc4_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
 int  rc4_test(void);
-extern const struct ltc_prng_descriptor rc4_desc;
 #endif
 
 #ifdef LTC_CHACHA20_PRNG
@@ -173,7 +173,6 @@ int  chacha20_prng_done(prng_state *prng);
 int  chacha20_prng_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
 int  chacha20_prng_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
 int  chacha20_prng_test(void);
-extern const struct ltc_prng_descriptor chacha20_prng_desc;
 #endif
 
 #ifdef LTC_SPRNG
@@ -185,7 +184,6 @@ int sprng_done(prng_state *prng);
 int  sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
 int  sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
 int  sprng_test(void);
-extern const struct ltc_prng_descriptor sprng_desc;
 #endif
 
 #ifdef LTC_SOBER128
@@ -197,16 +195,8 @@ int sober128_done(prng_state *prng);
 int  sober128_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
 int  sober128_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
 int  sober128_test(void);
-extern const struct ltc_prng_descriptor sober128_desc;
 #endif
 
-int find_prng(const char *name);
-int register_prng(const struct ltc_prng_descriptor *prng);
-int unregister_prng(const struct ltc_prng_descriptor *prng);
-int register_all_prngs(void);
-int prng_is_valid(int idx);
-LTC_MUTEX_PROTO(ltc_prng_mutex)
-
 /* Slow RNG you **might** be able to use to seed a PRNG with.  Be careful as this
  * might not work on all platforms as planned
  */
@@ -214,7 +204,7 @@ unsigned long rng_get_bytes(unsigned char *out,
                             unsigned long outlen,
                             void (*callback)(void));
 
-int rng_make_prng(int bits, int wprng, prng_state *prng, void (*callback)(void));
+int rng_make_prng(int bits, prng_state *prng, void (*callback)(void));
 
 #ifdef LTC_PRNG_ENABLE_LTC_RNG
 extern unsigned long (*ltc_rng)(unsigned char *out, unsigned long outlen,

+ 0 - 29
src/misc/crypt/crypt_find_prng.c

@@ -1,29 +0,0 @@
-/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-#include "tomcrypt_private.h"
-
-/**
-  @file crypt_find_prng.c
-  Find a PRNG, Tom St Denis
-*/
-
-/**
-   Find a registered PRNG by name
-   @param name   The name of the PRNG to look for
-   @return >= 0 if found, -1 if not present
-*/
-int find_prng(const char *name)
-{
-   int x;
-   LTC_ARGCHK(name != NULL);
-   LTC_MUTEX_LOCK(&ltc_prng_mutex);
-   for (x = 0; x < TAB_SIZE; x++) {
-       if ((prng_descriptor[x].name != NULL) && XSTRCMP(prng_descriptor[x].name, name) == 0) {
-          LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
-          return x;
-       }
-   }
-   LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
-   return -1;
-}
-

+ 0 - 14
src/misc/crypt/crypt_prng_descriptor.c

@@ -1,14 +0,0 @@
-/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-#include "tomcrypt_private.h"
-
-/**
-  @file crypt_prng_descriptor.c
-  Stores the PRNG descriptors, Tom St Denis
-*/
-struct ltc_prng_descriptor prng_descriptor[TAB_SIZE] = {
-{ NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
-};
-
-LTC_MUTEX_GLOBAL(ltc_prng_mutex)
-

+ 0 - 24
src/misc/crypt/crypt_prng_is_valid.c

@@ -1,24 +0,0 @@
-/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-#include "tomcrypt_private.h"
-
-/**
-  @file crypt_prng_is_valid.c
-  Determine if PRNG is valid, Tom St Denis
-*/
-
-/*
-   Test if a PRNG index is valid
-   @param idx   The index of the PRNG to search for
-   @return CRYPT_OK if valid
-*/
-int prng_is_valid(int idx)
-{
-   LTC_MUTEX_LOCK(&ltc_prng_mutex);
-   if (idx < 0 || idx >= TAB_SIZE || prng_descriptor[idx].name == NULL) {
-      LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
-      return CRYPT_INVALID_PRNG;
-   }
-   LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
-   return CRYPT_OK;
-}

+ 0 - 7
src/misc/crypt/crypt_prng_rng_descriptor.c

@@ -1,7 +0,0 @@
-/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-#include "tomcrypt_private.h"
-
-#ifdef LTC_PRNG_ENABLE_LTC_RNG
-unsigned long (*ltc_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
-#endif

+ 0 - 38
src/misc/crypt/crypt_register_all_prngs.c

@@ -1,38 +0,0 @@
-/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-#include "tomcrypt_private.h"
-
-/**
-  @file crypt_register_all_prngs.c
-
-  Steffen Jaeckel
-*/
-
-#define REGISTER_PRNG(h) do {\
-   LTC_ARGCHK(register_prng(h) != -1); \
-} while(0)
-
-int register_all_prngs(void)
-{
-#ifdef LTC_YARROW
-   REGISTER_PRNG(&yarrow_desc);
-#endif
-#ifdef LTC_FORTUNA
-   REGISTER_PRNG(&fortuna_desc);
-#endif
-#ifdef LTC_RC4
-   REGISTER_PRNG(&rc4_desc);
-#endif
-#ifdef LTC_CHACHA20_PRNG
-   REGISTER_PRNG(&chacha20_prng_desc);
-#endif
-#ifdef LTC_SOBER128
-   REGISTER_PRNG(&sober128_desc);
-#endif
-#ifdef LTC_SPRNG
-   REGISTER_PRNG(&sprng_desc);
-#endif
-
-   return CRYPT_OK;
-}

+ 0 - 42
src/misc/crypt/crypt_register_prng.c

@@ -1,42 +0,0 @@
-/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-#include "tomcrypt_private.h"
-
-/**
-  @file crypt_register_prng.c
-  Register a PRNG, Tom St Denis
-*/
-
-/**
-   Register a PRNG with the descriptor table
-   @param prng   The PRNG you wish to register
-   @return value >= 0 if successfully added (or already present), -1 if unsuccessful
-*/
-int register_prng(const struct ltc_prng_descriptor *prng)
-{
-   int x;
-
-   LTC_ARGCHK(prng != NULL);
-
-   /* is it already registered? */
-   LTC_MUTEX_LOCK(&ltc_prng_mutex);
-   for (x = 0; x < TAB_SIZE; x++) {
-       if (XMEMCMP(&prng_descriptor[x], prng, sizeof(struct ltc_prng_descriptor)) == 0) {
-          LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
-          return x;
-       }
-   }
-
-   /* find a blank spot */
-   for (x = 0; x < TAB_SIZE; x++) {
-       if (prng_descriptor[x].name == NULL) {
-          XMEMCPY(&prng_descriptor[x], prng, sizeof(struct ltc_prng_descriptor));
-          LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
-          return x;
-       }
-   }
-
-   /* no spot */
-   LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
-   return -1;
-}

+ 0 - 32
src/misc/crypt/crypt_unregister_prng.c

@@ -1,32 +0,0 @@
-/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-#include "tomcrypt_private.h"
-
-/**
-  @file crypt_unregister_prng.c
-  Unregister a PRNG, Tom St Denis
-*/
-
-/**
-  Unregister a PRNG from the descriptor table
-  @param prng   The PRNG descriptor to remove
-  @return CRYPT_OK on success
-*/
-int unregister_prng(const struct ltc_prng_descriptor *prng)
-{
-   int x;
-
-   LTC_ARGCHK(prng != NULL);
-
-   /* is it already registered? */
-   LTC_MUTEX_LOCK(&ltc_prng_mutex);
-   for (x = 0; x < TAB_SIZE; x++) {
-       if (XMEMCMP(&prng_descriptor[x], prng, sizeof(struct ltc_prng_descriptor)) == 0) {
-          prng_descriptor[x].name = NULL;
-          LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
-          return CRYPT_OK;
-       }
-   }
-   LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
-   return CRYPT_ERROR;
-}

+ 2 - 1
src/prngs/chacha20.c

@@ -9,7 +9,7 @@
 
 #ifdef LTC_CHACHA20_PRNG
 
-const struct ltc_prng_descriptor chacha20_prng_desc =
+static const struct ltc_prng_descriptor chacha20_prng_desc =
 {
    "chacha20",
    40,
@@ -34,6 +34,7 @@ int chacha20_prng_start(prng_state *prng)
    prng->ready = 0;
    XMEMSET(&prng->u.chacha.ent, 0, sizeof(prng->u.chacha.ent));
    prng->u.chacha.idx = 0;
+   prng->desc = chacha20_prng_desc;
    LTC_MUTEX_INIT(&prng->lock)
    return CRYPT_OK;
 }

+ 3 - 1
src/prngs/fortuna.c

@@ -51,7 +51,7 @@ we reseed automatically when len(pool0) >= 64 or every LTC_FORTUNA_WD calls to t
 #define AES_TEST  aes_test
 #endif
 
-const struct ltc_prng_descriptor fortuna_desc = {
+static const struct ltc_prng_descriptor fortuna_desc = {
     "fortuna",
     64,
     &fortuna_start,
@@ -256,6 +256,8 @@ int fortuna_start(prng_state *prng)
    }
    zeromem(prng->u.fortuna.IV, 16);
 
+   prng->desc = fortuna_desc;
+
    LTC_MUTEX_INIT(&prng->lock)
 
    return CRYPT_OK;

+ 2 - 1
src/prngs/rc4.c

@@ -9,7 +9,7 @@
 
 #ifdef LTC_RC4
 
-const struct ltc_prng_descriptor rc4_desc =
+static const struct ltc_prng_descriptor rc4_desc =
 {
    "rc4",
    32,
@@ -36,6 +36,7 @@ int rc4_start(prng_state *prng)
    prng->u.rc4.s.x = 0;
    /* clear entropy (key) buffer */
    XMEMSET(&prng->u.rc4.s.buf, 0, sizeof(prng->u.rc4.s.buf));
+   prng->desc = rc4_desc;
    LTC_MUTEX_INIT(&prng->lock)
    return CRYPT_OK;
 }

+ 6 - 11
src/prngs/rng_make_prng.c

@@ -21,7 +21,7 @@
   @param callback A pointer to a void function for when the RNG is slow, this can be NULL
   @return CRYPT_OK if successful
 */
-int rng_make_prng(int bits, int wprng, prng_state *prng,
+int rng_make_prng(int bits, prng_state *prng,
                   void (*callback)(void))
 {
    unsigned char* buf;
@@ -30,20 +30,15 @@ int rng_make_prng(int bits, int wprng, prng_state *prng,
 
    LTC_ARGCHK(prng != NULL);
 
-   /* check parameter */
-   if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
-      return err;
-   }
-
    if (bits == -1) {
-      bytes = prng_descriptor[wprng].export_size;
+      bytes = prng->desc.export_size;
    } else if (bits < 64 || bits > 1024) {
       return CRYPT_INVALID_PRNGSIZE;
    } else {
       bytes = (unsigned long)((bits+7)/8) * 2;
    }
 
-   if ((err = prng_descriptor[wprng].start(prng)) != CRYPT_OK) {
+   if ((err = prng->desc.start(prng)) != CRYPT_OK) {
       return err;
    }
 
@@ -58,15 +53,15 @@ int rng_make_prng(int bits, int wprng, prng_state *prng,
    }
 
    if (bits == -1) {
-      if ((err = prng_descriptor[wprng].pimport(buf, bytes, prng)) != CRYPT_OK) {
+      if ((err = prng->desc.pimport(buf, bytes, prng)) != CRYPT_OK) {
          goto LBL_ERR;
       }
    } else {
-      if ((err = prng_descriptor[wprng].add_entropy(buf, bytes, prng)) != CRYPT_OK) {
+      if ((err = prng->desc.add_entropy(buf, bytes, prng)) != CRYPT_OK) {
          goto LBL_ERR;
       }
    }
-   if ((err = prng_descriptor[wprng].ready(prng)) != CRYPT_OK) {
+   if ((err = prng->desc.ready(prng)) != CRYPT_OK) {
       goto LBL_ERR;
    }
 

+ 2 - 1
src/prngs/sober128.c

@@ -11,7 +11,7 @@
 
 #ifdef LTC_SOBER128
 
-const struct ltc_prng_descriptor sober128_desc =
+static const struct ltc_prng_descriptor sober128_desc =
 {
    "sober128",
    40,
@@ -36,6 +36,7 @@ int sober128_start(prng_state *prng)
    prng->ready = 0;
    XMEMSET(&prng->u.sober128.ent, 0, sizeof(prng->u.sober128.ent));
    prng->u.sober128.idx = 0;
+   prng->desc = sober128_desc;
    LTC_MUTEX_INIT(&prng->lock)
    return CRYPT_OK;
 }

+ 3 - 2
src/prngs/sprng.c

@@ -14,7 +14,7 @@
 
 #ifdef LTC_SPRNG
 
-const struct ltc_prng_descriptor sprng_desc =
+static const struct ltc_prng_descriptor sprng_desc =
 {
     "sprng", 0,
     &sprng_start,
@@ -34,7 +34,8 @@ const struct ltc_prng_descriptor sprng_desc =
 */
 int sprng_start(prng_state *prng)
 {
-   LTC_UNUSED_PARAM(prng);
+   LTC_ARGCHK(prng != NULL);
+   prng->desc = sprng_desc;
    return CRYPT_OK;
 }
 

+ 3 - 1
src/prngs/yarrow.c

@@ -9,7 +9,7 @@
 
 #ifdef LTC_YARROW
 
-const struct ltc_prng_descriptor yarrow_desc =
+static const struct ltc_prng_descriptor yarrow_desc =
 {
     "yarrow", 64,
     &yarrow_start,
@@ -111,6 +111,8 @@ int yarrow_start(prng_state *prng)
       return err;
    }
 
+   prng->desc = yarrow_desc;
+
    /* zero the memory used */
    zeromem(prng->u.yarrow.pool, sizeof(prng->u.yarrow.pool));
    LTC_MUTEX_INIT(&prng->lock)