random.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. static QLock rl;
  15. /*
  16. * Add entropy
  17. */
  18. void
  19. random_add(void *xp){
  20. Proc *up = externup();
  21. if(waserror()){
  22. qunlock(&rl);
  23. nexterror();
  24. }
  25. qlock(&rl);
  26. fortuna_add_entropy(xp, sizeof(xp));
  27. qunlock(&rl);
  28. poperror();
  29. }
  30. /*
  31. * consume random bytes
  32. */
  33. uint32_t
  34. randomread(void *xp, uint32_t n){
  35. Proc *up = externup();
  36. if(waserror()){
  37. qunlock(&rl);
  38. nexterror();
  39. }
  40. qlock(&rl);
  41. fortuna_get_bytes(n, xp);
  42. qunlock(&rl);
  43. poperror();
  44. return n;
  45. }
  46. /**
  47. * Fast random generator
  48. **/
  49. uint32_t
  50. urandomread(void *xp, uint32_t n){
  51. Proc *up = externup();
  52. uint64_t seed[16];
  53. uint8_t *e, *p;
  54. uint32_t x=0;
  55. uint64_t s0;
  56. uint64_t s1;
  57. if(waserror()){
  58. nexterror();
  59. }
  60. //The initial seed is from a good random pool.
  61. randomread(seed, sizeof(seed));
  62. p = xp;
  63. for(e = p + n; p < e; ){
  64. s0 = seed[ x ];
  65. s1 = seed[ x = (x+1) & 15 ];
  66. s1 ^= s1 << 31;
  67. s1 ^= s1 >> 11;
  68. s0 ^= s0 >> 30;
  69. *p++=( seed[x] = s0 ^ s1 ) * 1181783497276652981LL;
  70. }
  71. poperror();
  72. return n;
  73. }