2004-01-25 12:40:34 -05:00
|
|
|
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
|
|
|
*
|
|
|
|
* LibTomCrypt is a library that provides various cryptographic
|
|
|
|
* algorithms in a highly modular and flexible manner.
|
|
|
|
*
|
|
|
|
* The library is free for all purposes without any express
|
2004-05-12 16:42:16 -04:00
|
|
|
* guarantee it works.
|
2004-01-25 12:40:34 -05:00
|
|
|
*
|
|
|
|
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
|
|
|
*/
|
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
#include "mycrypt.h"
|
|
|
|
|
|
|
|
#ifdef YARROW
|
|
|
|
|
|
|
|
const struct _prng_descriptor yarrow_desc =
|
|
|
|
{
|
|
|
|
"yarrow",
|
|
|
|
&yarrow_start,
|
|
|
|
&yarrow_add_entropy,
|
|
|
|
&yarrow_ready,
|
|
|
|
&yarrow_read
|
|
|
|
};
|
|
|
|
|
|
|
|
int yarrow_start(prng_state *prng)
|
|
|
|
{
|
2003-03-02 20:02:42 -05:00
|
|
|
int err;
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
_ARGCHK(prng != NULL);
|
|
|
|
|
|
|
|
/* these are the default hash/cipher combo used */
|
|
|
|
#ifdef RIJNDAEL
|
2004-05-30 22:36:47 -04:00
|
|
|
#if YARROW_AES==0
|
|
|
|
prng->yarrow.cipher = register_cipher(&rijndael_enc_desc);
|
|
|
|
#elif YARROW_AES==1
|
|
|
|
prng->yarrow.cipher = register_cipher(&aes_enc_desc);
|
|
|
|
#elif YARROW_AES==2
|
2003-03-02 19:59:24 -05:00
|
|
|
prng->yarrow.cipher = register_cipher(&rijndael_desc);
|
2004-05-30 22:36:47 -04:00
|
|
|
#elif YARROW_AES==3
|
|
|
|
prng->yarrow.cipher = register_cipher(&aes_desc);
|
|
|
|
#endif
|
2003-03-02 19:59:24 -05:00
|
|
|
#elif defined(BLOWFISH)
|
|
|
|
prng->yarrow.cipher = register_cipher(&blowfish_desc);
|
|
|
|
#elif defined(TWOFISH)
|
|
|
|
prng->yarrow.cipher = register_cipher(&twofish_desc);
|
|
|
|
#elif defined(RC6)
|
|
|
|
prng->yarrow.cipher = register_cipher(&rc6_desc);
|
2003-12-24 13:59:57 -05:00
|
|
|
#elif defined(RC5)
|
|
|
|
prng->yarrow.cipher = register_cipher(&rc5_desc);
|
|
|
|
#elif defined(SAFERP)
|
|
|
|
prng->yarrow.cipher = register_cipher(&saferp_desc);
|
2003-03-02 19:59:24 -05:00
|
|
|
#elif defined(RC2)
|
|
|
|
prng->yarrow.cipher = register_cipher(&rc2_desc);
|
2003-12-24 13:59:57 -05:00
|
|
|
#elif defined(NOEKEON)
|
|
|
|
prng->yarrow.cipher = register_cipher(&noekeon_desc);
|
|
|
|
#elif defined(CAST5)
|
|
|
|
prng->yarrow.cipher = register_cipher(&cast5_desc);
|
|
|
|
#elif defined(XTEA)
|
|
|
|
prng->yarrow.cipher = register_cipher(&xtea_desc);
|
|
|
|
#elif defined(SAFER)
|
|
|
|
prng->yarrow.cipher = register_cipher(&safer_sk128_desc);
|
2003-03-02 19:59:24 -05:00
|
|
|
#elif defined(DES)
|
|
|
|
prng->yarrow.cipher = register_cipher(&des3_desc);
|
|
|
|
#elif
|
|
|
|
#error YARROW needs at least one CIPHER
|
|
|
|
#endif
|
2003-03-02 20:02:42 -05:00
|
|
|
if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) {
|
|
|
|
return err;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SHA256
|
|
|
|
prng->yarrow.hash = register_hash(&sha256_desc);
|
|
|
|
#elif defined(SHA512)
|
|
|
|
prng->yarrow.hash = register_hash(&sha512_desc);
|
|
|
|
#elif defined(TIGER)
|
|
|
|
prng->yarrow.hash = register_hash(&tiger_desc);
|
2003-12-24 13:59:57 -05:00
|
|
|
#elif defined(SHA1)
|
|
|
|
prng->yarrow.hash = register_hash(&sha1_desc);
|
|
|
|
#elif defined(RIPEMD160)
|
|
|
|
prng->yarrow.hash = register_hash(&rmd160_desc);
|
|
|
|
#elif defined(RIPEMD128)
|
|
|
|
prng->yarrow.hash = register_hash(&rmd128_desc);
|
2003-03-02 19:59:24 -05:00
|
|
|
#elif defined(MD5)
|
|
|
|
prng->yarrow.hash = register_hash(&md5_desc);
|
|
|
|
#elif defined(MD4)
|
|
|
|
prng->yarrow.hash = register_hash(&md4_desc);
|
|
|
|
#elif defined(MD2)
|
|
|
|
prng->yarrow.hash = register_hash(&md2_desc);
|
2004-05-30 22:36:47 -04:00
|
|
|
#elif defined(WHIRLPOOL)
|
|
|
|
prng->yarrow.hash = register_hash(&whirlpool_desc);
|
2003-03-02 19:59:24 -05:00
|
|
|
#else
|
|
|
|
#error YARROW needs at least one HASH
|
|
|
|
#endif
|
2003-03-02 20:02:42 -05:00
|
|
|
if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
|
|
|
|
return err;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* zero the memory used */
|
|
|
|
zeromem(prng->yarrow.pool, sizeof(prng->yarrow.pool));
|
|
|
|
|
|
|
|
return CRYPT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int yarrow_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng)
|
|
|
|
{
|
|
|
|
hash_state md;
|
2003-03-02 20:02:42 -05:00
|
|
|
int err;
|
2003-03-02 19:59:24 -05:00
|
|
|
|
2004-05-12 16:42:16 -04:00
|
|
|
_ARGCHK(buf != NULL);
|
2003-03-02 19:59:24 -05:00
|
|
|
_ARGCHK(prng != NULL);
|
|
|
|
|
2003-03-02 20:02:42 -05:00
|
|
|
if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
|
|
|
|
return err;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* start the hash */
|
|
|
|
hash_descriptor[prng->yarrow.hash].init(&md);
|
|
|
|
|
|
|
|
/* hash the current pool */
|
2004-05-30 22:36:47 -04:00
|
|
|
if ((err = hash_descriptor[prng->yarrow.hash].process(&md, prng->yarrow.pool,
|
|
|
|
hash_descriptor[prng->yarrow.hash].hashsize)) != CRYPT_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* add the new entropy */
|
2004-05-30 22:36:47 -04:00
|
|
|
if ((err = hash_descriptor[prng->yarrow.hash].process(&md, buf, len)) != CRYPT_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* store result */
|
2004-05-30 22:36:47 -04:00
|
|
|
if ((err = hash_descriptor[prng->yarrow.hash].done(&md, prng->yarrow.pool)) != CRYPT_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
return CRYPT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int yarrow_ready(prng_state *prng)
|
|
|
|
{
|
2003-03-02 20:02:42 -05:00
|
|
|
int ks, err;
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
_ARGCHK(prng != NULL);
|
|
|
|
|
2003-03-02 20:02:42 -05:00
|
|
|
if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
|
|
|
|
return err;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
2003-03-02 20:02:42 -05:00
|
|
|
if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) {
|
|
|
|
return err;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* setup CTR mode using the "pool" as the key */
|
2003-03-02 20:02:42 -05:00
|
|
|
ks = (int)hash_descriptor[prng->yarrow.hash].hashsize;
|
|
|
|
if ((err = cipher_descriptor[prng->yarrow.cipher].keysize(&ks)) != CRYPT_OK) {
|
|
|
|
return err;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
2003-09-07 21:06:11 -04:00
|
|
|
if ((err = ctr_start(prng->yarrow.cipher, /* what cipher to use */
|
|
|
|
prng->yarrow.pool, /* IV */
|
|
|
|
prng->yarrow.pool, ks, /* KEY and key size */
|
|
|
|
0, /* number of rounds */
|
|
|
|
&prng->yarrow.ctr)) != CRYPT_OK) {
|
2003-03-02 20:02:42 -05:00
|
|
|
return err;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
return CRYPT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long yarrow_read(unsigned char *buf, unsigned long len, prng_state *prng)
|
|
|
|
{
|
2004-05-12 16:42:16 -04:00
|
|
|
_ARGCHK(buf != NULL);
|
2003-03-02 19:59:24 -05:00
|
|
|
_ARGCHK(prng != NULL);
|
|
|
|
|
2003-03-29 13:19:07 -05:00
|
|
|
/* put buf in predictable state first */
|
|
|
|
zeromem(buf, len);
|
|
|
|
|
|
|
|
/* now randomize it */
|
2003-03-02 19:59:24 -05:00
|
|
|
if (ctr_encrypt(buf, buf, len, &prng->yarrow.ctr) != CRYPT_OK) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|