yarrow.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. *
  9. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. #include "mycrypt.h"
  12. #ifdef YARROW
  13. const struct _prng_descriptor yarrow_desc =
  14. {
  15. "yarrow",
  16. &yarrow_start,
  17. &yarrow_add_entropy,
  18. &yarrow_ready,
  19. &yarrow_read
  20. };
  21. int yarrow_start(prng_state *prng)
  22. {
  23. int err;
  24. _ARGCHK(prng != NULL);
  25. /* these are the default hash/cipher combo used */
  26. #ifdef RIJNDAEL
  27. #if YARROW_AES==0
  28. prng->yarrow.cipher = register_cipher(&rijndael_enc_desc);
  29. #elif YARROW_AES==1
  30. prng->yarrow.cipher = register_cipher(&aes_enc_desc);
  31. #elif YARROW_AES==2
  32. prng->yarrow.cipher = register_cipher(&rijndael_desc);
  33. #elif YARROW_AES==3
  34. prng->yarrow.cipher = register_cipher(&aes_desc);
  35. #endif
  36. #elif defined(BLOWFISH)
  37. prng->yarrow.cipher = register_cipher(&blowfish_desc);
  38. #elif defined(TWOFISH)
  39. prng->yarrow.cipher = register_cipher(&twofish_desc);
  40. #elif defined(RC6)
  41. prng->yarrow.cipher = register_cipher(&rc6_desc);
  42. #elif defined(RC5)
  43. prng->yarrow.cipher = register_cipher(&rc5_desc);
  44. #elif defined(SAFERP)
  45. prng->yarrow.cipher = register_cipher(&saferp_desc);
  46. #elif defined(RC2)
  47. prng->yarrow.cipher = register_cipher(&rc2_desc);
  48. #elif defined(NOEKEON)
  49. prng->yarrow.cipher = register_cipher(&noekeon_desc);
  50. #elif defined(CAST5)
  51. prng->yarrow.cipher = register_cipher(&cast5_desc);
  52. #elif defined(XTEA)
  53. prng->yarrow.cipher = register_cipher(&xtea_desc);
  54. #elif defined(SAFER)
  55. prng->yarrow.cipher = register_cipher(&safer_sk128_desc);
  56. #elif defined(DES)
  57. prng->yarrow.cipher = register_cipher(&des3_desc);
  58. #elif
  59. #error YARROW needs at least one CIPHER
  60. #endif
  61. if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) {
  62. return err;
  63. }
  64. #ifdef SHA256
  65. prng->yarrow.hash = register_hash(&sha256_desc);
  66. #elif defined(SHA512)
  67. prng->yarrow.hash = register_hash(&sha512_desc);
  68. #elif defined(TIGER)
  69. prng->yarrow.hash = register_hash(&tiger_desc);
  70. #elif defined(SHA1)
  71. prng->yarrow.hash = register_hash(&sha1_desc);
  72. #elif defined(RIPEMD160)
  73. prng->yarrow.hash = register_hash(&rmd160_desc);
  74. #elif defined(RIPEMD128)
  75. prng->yarrow.hash = register_hash(&rmd128_desc);
  76. #elif defined(MD5)
  77. prng->yarrow.hash = register_hash(&md5_desc);
  78. #elif defined(MD4)
  79. prng->yarrow.hash = register_hash(&md4_desc);
  80. #elif defined(MD2)
  81. prng->yarrow.hash = register_hash(&md2_desc);
  82. #elif defined(WHIRLPOOL)
  83. prng->yarrow.hash = register_hash(&whirlpool_desc);
  84. #else
  85. #error YARROW needs at least one HASH
  86. #endif
  87. if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
  88. return err;
  89. }
  90. /* zero the memory used */
  91. zeromem(prng->yarrow.pool, sizeof(prng->yarrow.pool));
  92. return CRYPT_OK;
  93. }
  94. int yarrow_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng)
  95. {
  96. hash_state md;
  97. int err;
  98. _ARGCHK(buf != NULL);
  99. _ARGCHK(prng != NULL);
  100. if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
  101. return err;
  102. }
  103. /* start the hash */
  104. hash_descriptor[prng->yarrow.hash].init(&md);
  105. /* hash the current pool */
  106. if ((err = hash_descriptor[prng->yarrow.hash].process(&md, prng->yarrow.pool,
  107. hash_descriptor[prng->yarrow.hash].hashsize)) != CRYPT_OK) {
  108. return err;
  109. }
  110. /* add the new entropy */
  111. if ((err = hash_descriptor[prng->yarrow.hash].process(&md, buf, len)) != CRYPT_OK) {
  112. return err;
  113. }
  114. /* store result */
  115. if ((err = hash_descriptor[prng->yarrow.hash].done(&md, prng->yarrow.pool)) != CRYPT_OK) {
  116. return err;
  117. }
  118. return CRYPT_OK;
  119. }
  120. int yarrow_ready(prng_state *prng)
  121. {
  122. int ks, err;
  123. _ARGCHK(prng != NULL);
  124. if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
  125. return err;
  126. }
  127. if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) {
  128. return err;
  129. }
  130. /* setup CTR mode using the "pool" as the key */
  131. ks = (int)hash_descriptor[prng->yarrow.hash].hashsize;
  132. if ((err = cipher_descriptor[prng->yarrow.cipher].keysize(&ks)) != CRYPT_OK) {
  133. return err;
  134. }
  135. if ((err = ctr_start(prng->yarrow.cipher, /* what cipher to use */
  136. prng->yarrow.pool, /* IV */
  137. prng->yarrow.pool, ks, /* KEY and key size */
  138. 0, /* number of rounds */
  139. &prng->yarrow.ctr)) != CRYPT_OK) {
  140. return err;
  141. }
  142. return CRYPT_OK;
  143. }
  144. unsigned long yarrow_read(unsigned char *buf, unsigned long len, prng_state *prng)
  145. {
  146. _ARGCHK(buf != NULL);
  147. _ARGCHK(prng != NULL);
  148. /* put buf in predictable state first */
  149. zeromem(buf, len);
  150. /* now randomize it */
  151. if (ctr_encrypt(buf, buf, len, &prng->yarrow.ctr) != CRYPT_OK) {
  152. return 0;
  153. }
  154. return len;
  155. }
  156. #endif