mycrypt_prng.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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[FORTUNA_POOLS]; /* the 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. struct sober128_prng {
  22. ulong32 R[17], /* Working storage for the shift register */
  23. initR[17], /* saved register contents */
  24. konst, /* key dependent constant */
  25. sbuf; /* partial word encryption buffer */
  26. int nbuf, /* number of part-word stream bits buffered */
  27. flag, /* first add_entropy call or not? */
  28. set; /* did we call add_entropy to set key? */
  29. };
  30. typedef union Prng_state {
  31. #ifdef YARROW
  32. struct yarrow_prng yarrow;
  33. #endif
  34. #ifdef RC4
  35. struct rc4_prng rc4;
  36. #endif
  37. #ifdef FORTUNA
  38. struct fortuna_prng fortuna;
  39. #endif
  40. #ifdef SOBER128
  41. struct sober128_prng sober128;
  42. #endif
  43. } prng_state;
  44. extern struct _prng_descriptor {
  45. char *name;
  46. int export_size; /* size in bytes of exported state */
  47. int (*start)(prng_state *);
  48. int (*add_entropy)(const unsigned char *, unsigned long, prng_state *);
  49. int (*ready)(prng_state *);
  50. unsigned long (*read)(unsigned char *, unsigned long, prng_state *);
  51. int (*done)(prng_state *);
  52. int (*export)(unsigned char *, unsigned long *, prng_state *);
  53. int (*import)(const unsigned char *, unsigned long, prng_state *);
  54. int (*test)(void);
  55. } prng_descriptor[];
  56. #ifdef YARROW
  57. int yarrow_start(prng_state *prng);
  58. int yarrow_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng);
  59. int yarrow_ready(prng_state *prng);
  60. unsigned long yarrow_read(unsigned char *buf, unsigned long len, prng_state *prng);
  61. int yarrow_done(prng_state *prng);
  62. int yarrow_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
  63. int yarrow_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
  64. int yarrow_test(void);
  65. extern const struct _prng_descriptor yarrow_desc;
  66. #endif
  67. #ifdef FORTUNA
  68. int fortuna_start(prng_state *prng);
  69. int fortuna_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng);
  70. int fortuna_ready(prng_state *prng);
  71. unsigned long fortuna_read(unsigned char *buf, unsigned long len, prng_state *prng);
  72. int fortuna_done(prng_state *prng);
  73. int fortuna_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
  74. int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
  75. int fortuna_test(void);
  76. extern const struct _prng_descriptor fortuna_desc;
  77. #endif
  78. #ifdef RC4
  79. int rc4_start(prng_state *prng);
  80. int rc4_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng);
  81. int rc4_ready(prng_state *prng);
  82. unsigned long rc4_read(unsigned char *buf, unsigned long len, prng_state *prng);
  83. int rc4_done(prng_state *prng);
  84. int rc4_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
  85. int rc4_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
  86. int rc4_test(void);
  87. extern const struct _prng_descriptor rc4_desc;
  88. #endif
  89. #ifdef SPRNG
  90. int sprng_start(prng_state *prng);
  91. int sprng_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng);
  92. int sprng_ready(prng_state *prng);
  93. unsigned long sprng_read(unsigned char *buf, unsigned long len, prng_state *prng);
  94. int sprng_done(prng_state *prng);
  95. int sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
  96. int sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
  97. int sprng_test(void);
  98. extern const struct _prng_descriptor sprng_desc;
  99. #endif
  100. #ifdef SOBER128
  101. int sober128_start(prng_state *prng);
  102. int sober128_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng);
  103. int sober128_ready(prng_state *prng);
  104. unsigned long sober128_read(unsigned char *buf, unsigned long len, prng_state *prng);
  105. int sober128_done(prng_state *prng);
  106. int sober128_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
  107. int sober128_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
  108. int sober128_test(void);
  109. extern const struct _prng_descriptor sober128_desc;
  110. #endif
  111. int find_prng(const char *name);
  112. int register_prng(const struct _prng_descriptor *prng);
  113. int unregister_prng(const struct _prng_descriptor *prng);
  114. int prng_is_valid(int idx);
  115. /* Slow RNG you **might** be able to use to seed a PRNG with. Be careful as this
  116. * might not work on all platforms as planned
  117. */
  118. unsigned long rng_get_bytes(unsigned char *buf,
  119. unsigned long len,
  120. void (*callback)(void));
  121. int rng_make_prng(int bits, int wprng, prng_state *prng, void (*callback)(void));