prng.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 file implements a random data pool based on the use of an external
  25. entropy function. It is based on the ideas advocated by Peter Gutmann in
  26. his work on pseudo random sequence generators. It is not a 'paranoid'
  27. random sequence generator and no attempt is made to protect the pool
  28. from prying eyes either by memory locking or by techniques to obscure
  29. its location in memory.
  30. */
  31. #include <memory.h>
  32. #include "prng.h"
  33. #if defined(__cplusplus)
  34. extern "C"
  35. {
  36. #endif
  37. /* mix a random data pool using the SHA1 compression function (as */
  38. /* suggested by Peter Gutmann in his paper on random pools) */
  39. static void prng_mix(unsigned char buf[])
  40. { unsigned int i, len;
  41. sha1_ctx ctx[1];
  42. /*lint -e{663} unusual array to pointer conversion */
  43. for(i = 0; i < PRNG_POOL_SIZE; i += SHA1_DIGEST_SIZE)
  44. {
  45. /* copy digest size pool block into SHA1 hash block */
  46. memcpy(ctx->hash, buf + (i ? i : PRNG_POOL_SIZE)
  47. - SHA1_DIGEST_SIZE, SHA1_DIGEST_SIZE);
  48. /* copy data from pool into the SHA1 data buffer */
  49. len = PRNG_POOL_SIZE - i;
  50. memcpy(ctx->wbuf, buf + i, (len > SHA1_BLOCK_SIZE ? SHA1_BLOCK_SIZE : len));
  51. if(len < SHA1_BLOCK_SIZE)
  52. memcpy(((char*)ctx->wbuf) + len, buf, SHA1_BLOCK_SIZE - len);
  53. /* compress using the SHA1 compression function */
  54. sha1_compile(ctx);
  55. /* put digest size block back into the random pool */
  56. memcpy(buf + i, ctx->hash, SHA1_DIGEST_SIZE);
  57. }
  58. }
  59. /* refresh the output buffer and update the random pool by adding */
  60. /* entropy and remixing */
  61. static void update_pool(prng_ctx ctx[1])
  62. { unsigned int i = 0;
  63. /* transfer random pool data to the output buffer */
  64. memcpy(ctx->obuf, ctx->rbuf, PRNG_POOL_SIZE);
  65. /* enter entropy data into the pool */
  66. while(i < PRNG_POOL_SIZE)
  67. i += ctx->entropy(ctx->rbuf + i, PRNG_POOL_SIZE - i);
  68. /* invert and xor the original pool data into the pool */
  69. for(i = 0; i < PRNG_POOL_SIZE; ++i)
  70. ctx->rbuf[i] ^= ~ctx->obuf[i];
  71. /* mix the pool and the output buffer */
  72. prng_mix(ctx->rbuf);
  73. prng_mix(ctx->obuf);
  74. }
  75. void prng_init(prng_entropy_fn fun, prng_ctx ctx[1])
  76. { int i;
  77. /* clear the buffers and the counter in the context */
  78. memset(ctx, 0, sizeof(prng_ctx));
  79. /* set the pointer to the entropy collection function */
  80. ctx->entropy = fun;
  81. /* initialise the random data pool */
  82. update_pool(ctx);
  83. /* mix the pool a minimum number of times */
  84. for(i = 0; i < PRNG_MIN_MIX; ++i)
  85. prng_mix(ctx->rbuf);
  86. /* update the pool to prime the pool output buffer */
  87. update_pool(ctx);
  88. }
  89. /* provide random bytes from the random data pool */
  90. void prng_rand(unsigned char data[], unsigned int data_len, prng_ctx ctx[1])
  91. { unsigned char *rp = data;
  92. unsigned int len, pos = ctx->pos;
  93. while(data_len)
  94. {
  95. /* transfer 'data_len' bytes (or the number of bytes remaining */
  96. /* the pool output buffer if less) into the output */
  97. len = (data_len < PRNG_POOL_SIZE - pos ? data_len : PRNG_POOL_SIZE - pos);
  98. memcpy(rp, ctx->obuf + pos, len);
  99. rp += len; /* update ouput buffer position pointer */
  100. pos += len; /* update pool output buffer pointer */
  101. data_len -= len; /* update the remaining data count */
  102. /* refresh the random pool if necessary */
  103. if(pos == PRNG_POOL_SIZE)
  104. {
  105. update_pool(ctx); pos = 0;
  106. }
  107. }
  108. ctx->pos = pos;
  109. }
  110. void prng_end(prng_ctx ctx[1])
  111. {
  112. /* ensure the data in the context is destroyed */
  113. memset(ctx, 0, sizeof(prng_ctx));
  114. }
  115. #if defined(__cplusplus)
  116. }
  117. #endif