mycrypt_prng.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* ---- PRNG Stuff ---- */
  2. struct yarrow_prng {
  3. int cipher, hash;
  4. unsigned char pool[MAXBLOCKSIZE];
  5. symmetric_CTR ctr;
  6. };
  7. struct rc4_prng {
  8. int x, y;
  9. unsigned char buf[256];
  10. };
  11. struct fortuna_prng {
  12. hash_state pool[32]; /* the 32 pools */
  13. symmetric_key skey;
  14. unsigned char K[32], /* the current key */
  15. IV[16]; /* IV for CTR mode */
  16. unsigned long pool_idx, /* current pool we will add to */
  17. pool0_len, /* length of 0'th pool */
  18. wd;
  19. ulong64 reset_cnt; /* number of times we have reset */
  20. };
  21. typedef union Prng_state {
  22. struct yarrow_prng yarrow;
  23. struct rc4_prng rc4;
  24. struct fortuna_prng fortuna;
  25. } prng_state;
  26. extern struct _prng_descriptor {
  27. char *name;
  28. int (*start)(prng_state *);
  29. int (*add_entropy)(const unsigned char *, unsigned long, prng_state *);
  30. int (*ready)(prng_state *);
  31. unsigned long (*read)(unsigned char *, unsigned long, prng_state *);
  32. void (*done)(prng_state *);
  33. int (*export)(unsigned char *, unsigned long *, prng_state *);
  34. int (*import)(const unsigned char *, unsigned long, prng_state *);
  35. } prng_descriptor[];
  36. #ifdef YARROW
  37. int yarrow_start(prng_state *prng);
  38. int yarrow_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng);
  39. int yarrow_ready(prng_state *prng);
  40. unsigned long yarrow_read(unsigned char *buf, unsigned long len, prng_state *prng);
  41. void yarrow_done(prng_state *prng);
  42. int yarrow_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
  43. int yarrow_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
  44. extern const struct _prng_descriptor yarrow_desc;
  45. #endif
  46. #ifdef FORTUNA
  47. int fortuna_start(prng_state *prng);
  48. int fortuna_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng);
  49. int fortuna_ready(prng_state *prng);
  50. unsigned long fortuna_read(unsigned char *buf, unsigned long len, prng_state *prng);
  51. void fortuna_done(prng_state *prng);
  52. int fortuna_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
  53. int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
  54. extern const struct _prng_descriptor fortuna_desc;
  55. #endif
  56. #ifdef RC4
  57. int rc4_start(prng_state *prng);
  58. int rc4_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng);
  59. int rc4_ready(prng_state *prng);
  60. unsigned long rc4_read(unsigned char *buf, unsigned long len, prng_state *prng);
  61. void rc4_done(prng_state *prng);
  62. int rc4_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
  63. int rc4_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
  64. extern const struct _prng_descriptor rc4_desc;
  65. #endif
  66. #ifdef SPRNG
  67. int sprng_start(prng_state *prng);
  68. int sprng_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng);
  69. int sprng_ready(prng_state *prng);
  70. unsigned long sprng_read(unsigned char *buf, unsigned long len, prng_state *prng);
  71. void sprng_done(prng_state *prng);
  72. int sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
  73. int sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
  74. extern const struct _prng_descriptor sprng_desc;
  75. #endif
  76. int find_prng(const char *name);
  77. int register_prng(const struct _prng_descriptor *prng);
  78. int unregister_prng(const struct _prng_descriptor *prng);
  79. int prng_is_valid(int idx);
  80. /* Slow RNG you **might** be able to use to seed a PRNG with. Be careful as this
  81. * might not work on all platforms as planned
  82. */
  83. /* ch2-02-1 */
  84. unsigned long rng_get_bytes(unsigned char *buf,
  85. unsigned long len,
  86. void (*callback)(void));
  87. /* ch2-02-1 */
  88. int rng_make_prng(int bits, int wprng, prng_state *prng, void (*callback)(void));