prng.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.
  4. All rights reserved.
  5. LICENSE TERMS
  6. The free distribution and use of this software in both source and binary
  7. form is allowed (with or without changes) provided that:
  8. 1. distributions of this source code include the above copyright
  9. notice, this list of conditions and the following disclaimer;
  10. 2. distributions in binary form include the above copyright
  11. notice, this list of conditions and the following disclaimer
  12. in the documentation and/or other associated materials;
  13. 3. the copyright holder's name is not used to endorse products
  14. built using this software without specific written permission.
  15. ALTERNATIVELY, provided that this notice is retained in full, this product
  16. may be distributed under the terms of the GNU General Public License (GPL),
  17. in which case the provisions of the GPL apply INSTEAD OF those given above.
  18. DISCLAIMER
  19. This software is provided 'as is' with no explicit or implied warranties
  20. in respect of its properties, including, but not limited to, correctness
  21. and/or fitness for purpose.
  22. ---------------------------------------------------------------------------
  23. Issue Date: 24/01/2003
  24. This is the header file for an implementation of a random data pool based on
  25. the use of an external entropy function (inspired by Peter Gutmann's work).
  26. */
  27. #ifndef _PRNG_H
  28. #define _PRNG_H
  29. #include "sha1.h"
  30. #define PRNG_POOL_LEN 256 /* minimum random pool size */
  31. #define PRNG_MIN_MIX 20 /* min initial pool mixing iterations */
  32. /* ensure that pool length is a multiple of the SHA1 digest size */
  33. #define PRNG_POOL_SIZE (SHA1_DIGEST_SIZE * (1 + (PRNG_POOL_LEN - 1) / SHA1_DIGEST_SIZE))
  34. #if defined(__cplusplus)
  35. extern "C"
  36. {
  37. #endif
  38. /* A function for providing entropy is a parameter in the prng_init() */
  39. /* call. This function has the following form and returns a maximum */
  40. /* of 'len' bytes of pseudo random data in the buffer 'buf'. It can */
  41. /* return less than 'len' bytes but will be repeatedly called for more */
  42. /* data in this case. */
  43. typedef int (*prng_entropy_fn)(unsigned char buf[], unsigned int len);
  44. typedef struct
  45. { unsigned char rbuf[PRNG_POOL_SIZE]; /* the random pool */
  46. unsigned char obuf[PRNG_POOL_SIZE]; /* pool output buffer */
  47. unsigned int pos; /* output buffer position */
  48. prng_entropy_fn entropy; /* entropy function pointer */
  49. } prng_ctx;
  50. /* initialise the random stream generator */
  51. void prng_init(prng_entropy_fn fun, prng_ctx ctx[1]);
  52. /* obtain random bytes from the generator */
  53. void prng_rand(unsigned char data[], unsigned int data_len, prng_ctx ctx[1]);
  54. /* close the random stream generator */
  55. void prng_end(prng_ctx ctx[1]);
  56. #if defined(__cplusplus)
  57. }
  58. #endif
  59. #endif