summaryrefslogtreecommitdiffhomepage
path: root/src/prngs/yarrow.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/prngs/yarrow.c')
-rw-r--r--src/prngs/yarrow.c59
1 files changed, 52 insertions, 7 deletions
diff --git a/src/prngs/yarrow.c b/src/prngs/yarrow.c
index 0e59c22..9fbd4f6 100644
--- a/src/prngs/yarrow.c
+++ b/src/prngs/yarrow.c
@@ -6,7 +6,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
*/
#include "tomcrypt.h"
@@ -66,6 +66,12 @@ int yarrow_start(prng_state *prng)
prng->yarrow.cipher = register_cipher(&rc2_desc);
#elif defined(NOEKEON)
prng->yarrow.cipher = register_cipher(&noekeon_desc);
+#elif defined(ANUBIS)
+ prng->yarrow.cipher = register_cipher(&anubis_desc);
+#elif defined(KSEED)
+ prng->yarrow.cipher = register_cipher(&kseed_desc);
+#elif defined(KHAZAD)
+ prng->yarrow.cipher = register_cipher(&khazad_desc);
#elif defined(CAST5)
prng->yarrow.cipher = register_cipher(&cast5_desc);
#elif defined(XTEA)
@@ -89,6 +95,10 @@ int yarrow_start(prng_state *prng)
prng->yarrow.hash = register_hash(&tiger_desc);
#elif defined(SHA1)
prng->yarrow.hash = register_hash(&sha1_desc);
+#elif defined(RIPEMD320)
+ prng->yarrow.hash = register_hash(&rmd320_desc);
+#elif defined(RIPEMD256)
+ prng->yarrow.hash = register_hash(&rmd256_desc);
#elif defined(RIPEMD160)
prng->yarrow.hash = register_hash(&rmd160_desc);
#elif defined(RIPEMD128)
@@ -110,6 +120,7 @@ int yarrow_start(prng_state *prng)
/* zero the memory used */
zeromem(prng->yarrow.pool, sizeof(prng->yarrow.pool));
+ LTC_MUTEX_INIT(&prng->yarrow.prng_lock)
return CRYPT_OK;
}
@@ -128,32 +139,40 @@ int yarrow_add_entropy(const unsigned char *in, unsigned long inlen, prng_state
LTC_ARGCHK(in != NULL);
LTC_ARGCHK(prng != NULL);
-
+
+ LTC_MUTEX_LOCK(&prng->yarrow.prng_lock);
+
if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
return err;
}
/* start the hash */
if ((err = hash_descriptor[prng->yarrow.hash].init(&md)) != CRYPT_OK) {
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
return err;
}
/* hash the current pool */
if ((err = hash_descriptor[prng->yarrow.hash].process(&md, prng->yarrow.pool,
hash_descriptor[prng->yarrow.hash].hashsize)) != CRYPT_OK) {
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
return err;
}
/* add the new entropy */
if ((err = hash_descriptor[prng->yarrow.hash].process(&md, in, inlen)) != CRYPT_OK) {
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
return err;
}
/* store result */
if ((err = hash_descriptor[prng->yarrow.hash].done(&md, prng->yarrow.pool)) != CRYPT_OK) {
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
return err;
}
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
return CRYPT_OK;
}
@@ -167,18 +186,22 @@ int yarrow_ready(prng_state *prng)
int ks, err;
LTC_ARGCHK(prng != NULL);
+ LTC_MUTEX_LOCK(&prng->yarrow.prng_lock);
if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
return err;
}
if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) {
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
return err;
}
/* setup CTR mode using the "pool" as the key */
ks = (int)hash_descriptor[prng->yarrow.hash].hashsize;
if ((err = cipher_descriptor[prng->yarrow.cipher].keysize(&ks)) != CRYPT_OK) {
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
return err;
}
@@ -188,8 +211,10 @@ int yarrow_ready(prng_state *prng)
0, /* number of rounds */
CTR_COUNTER_LITTLE_ENDIAN, /* little endian counter */
&prng->yarrow.ctr)) != CRYPT_OK) {
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
return err;
}
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
return CRYPT_OK;
}
@@ -205,13 +230,17 @@ unsigned long yarrow_read(unsigned char *out, unsigned long outlen, prng_state *
LTC_ARGCHK(out != NULL);
LTC_ARGCHK(prng != NULL);
+ LTC_MUTEX_LOCK(&prng->yarrow.prng_lock);
+
/* put out in predictable state first */
zeromem(out, outlen);
/* now randomize it */
if (ctr_encrypt(out, out, outlen, &prng->yarrow.ctr) != CRYPT_OK) {
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
return 0;
}
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
return outlen;
}
@@ -222,12 +251,18 @@ unsigned long yarrow_read(unsigned char *out, unsigned long outlen, prng_state *
*/
int yarrow_done(prng_state *prng)
{
+ int err;
LTC_ARGCHK(prng != NULL);
+ LTC_MUTEX_LOCK(&prng->yarrow.prng_lock);
+
/* call cipher done when we invent one ;-) */
/* we invented one */
- return ctr_done(&prng->yarrow.ctr);
+ err = ctr_done(&prng->yarrow.ctr);
+
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
+ return err;
}
/**
@@ -243,12 +278,17 @@ int yarrow_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
LTC_ARGCHK(outlen != NULL);
LTC_ARGCHK(prng != NULL);
+ LTC_MUTEX_LOCK(&prng->yarrow.prng_lock);
+
/* we'll write 64 bytes for s&g's */
if (*outlen < 64) {
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
+ *outlen = 64;
return CRYPT_BUFFER_OVERFLOW;
}
if (yarrow_read(out, 64, prng) != 64) {
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
return CRYPT_ERROR_READPRNG;
}
*outlen = 64;
@@ -269,15 +309,21 @@ int yarrow_import(const unsigned char *in, unsigned long inlen, prng_state *prng
LTC_ARGCHK(in != NULL);
LTC_ARGCHK(prng != NULL);
+
+ LTC_MUTEX_LOCK(&prng->yarrow.prng_lock);
if (inlen != 64) {
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
return CRYPT_INVALID_ARG;
}
if ((err = yarrow_start(prng)) != CRYPT_OK) {
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
return err;
}
- return yarrow_add_entropy(in, 64, prng);
+ err = yarrow_add_entropy(in, 64, prng);
+ LTC_MUTEX_UNLOCK(&prng->yarrow.prng_lock);
+ return err;
}
/**
@@ -304,7 +350,6 @@ int yarrow_test(void)
return err;
}
- yarrow_done(&prng);
return CRYPT_OK;
#endif
}
@@ -313,5 +358,5 @@ int yarrow_test(void)
/* $Source: /cvs/libtom/libtomcrypt/src/prngs/yarrow.c,v $ */
-/* $Revision: 1.5 $ */
-/* $Date: 2005/05/05 14:35:59 $ */
+/* $Revision: 1.10 $ */
+/* $Date: 2006/11/14 04:21:17 $ */