arc4random.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* $$$: arc4random.c 2005/02/08 robert */
  2. /* $NetBSD: arc4random.c,v 1.5.2.1 2004/03/26 22:52:50 jmc Exp $ */
  3. /* $OpenBSD: arc4random.c,v 1.6 2001/06/05 05:05:38 pvalchev Exp $ */
  4. /*
  5. * Arc4 random number generator for OpenBSD.
  6. * Copyright 1996 David Mazieres <dm@lcs.mit.edu>.
  7. *
  8. * Modification and redistribution in source and binary forms is
  9. * permitted provided that due credit is given to the author and the
  10. * OpenBSD project by leaving this copyright notice intact.
  11. */
  12. /*
  13. * This code is derived from section 17.1 of Applied Cryptography,
  14. * second edition, which describes a stream cipher allegedly
  15. * compatible with RSA Labs "RC4" cipher (the actual description of
  16. * which is a trade secret). The same algorithm is used as a stream
  17. * cipher called "arcfour" in Tatu Ylonen's ssh package.
  18. *
  19. * Here the stream cipher has been modified always to include the time
  20. * when initializing the state. That makes it impossible to
  21. * regenerate the same random sequence twice, so this can't be used
  22. * for encryption, but will generate good random numbers.
  23. *
  24. * RC4 is a registered trademark of RSA Laboratories.
  25. */
  26. #include <features.h>
  27. #include <fcntl.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <sys/types.h>
  31. #include <sys/param.h>
  32. #include <sys/time.h>
  33. #ifdef __ARC4RANDOM_USE_ERANDOM__
  34. #include <sys/sysctl.h>
  35. //libc_hidden_proto(sysctl)
  36. #endif
  37. libc_hidden_proto(open)
  38. libc_hidden_proto(read)
  39. libc_hidden_proto(close)
  40. libc_hidden_proto(gettimeofday)
  41. struct arc4_stream {
  42. uint8_t i;
  43. uint8_t j;
  44. uint8_t s[256];
  45. };
  46. static int rs_initialized;
  47. static struct arc4_stream rs;
  48. static __inline__ void arc4_init(struct arc4_stream *);
  49. static __inline__ void arc4_addrandom(struct arc4_stream *, u_char *, int);
  50. static void arc4_stir(struct arc4_stream *);
  51. static __inline__ uint8_t arc4_getbyte(struct arc4_stream *);
  52. static __inline__ uint32_t arc4_getword(struct arc4_stream *);
  53. static __inline__ void
  54. arc4_init(as)
  55. struct arc4_stream *as;
  56. {
  57. int n;
  58. for (n = 0; n < 256; n++)
  59. as->s[n] = n;
  60. as->i = 0;
  61. as->j = 0;
  62. }
  63. static __inline__ void
  64. arc4_addrandom(as, dat, datlen)
  65. struct arc4_stream *as;
  66. u_char *dat;
  67. int datlen;
  68. {
  69. int n;
  70. uint8_t si;
  71. as->i--;
  72. for (n = 0; n < 256; n++) {
  73. as->i = (as->i + 1);
  74. si = as->s[as->i];
  75. as->j = (as->j + si + dat[n % datlen]);
  76. as->s[as->i] = as->s[as->j];
  77. as->s[as->j] = si;
  78. }
  79. as->j = as->i;
  80. }
  81. static void
  82. arc4_stir(as)
  83. struct arc4_stream *as;
  84. {
  85. int fd;
  86. struct {
  87. struct timeval tv;
  88. uint rnd[(128 - sizeof(struct timeval)) / sizeof(uint)];
  89. } rdat;
  90. int n;
  91. gettimeofday(&rdat.tv, NULL);
  92. fd = open("/dev/urandom", O_RDONLY);
  93. if (fd != -1) {
  94. read(fd, rdat.rnd, sizeof(rdat.rnd));
  95. close(fd);
  96. }
  97. #ifdef __ARC4RANDOM_USE_ERANDOM__
  98. else {
  99. int mib[3];
  100. uint i;
  101. size_t len;
  102. /* Device could not be opened, we might be chrooted, take
  103. * randomness from sysctl. */
  104. mib[0] = CTL_KERN;
  105. mib[1] = KERN_RANDOM;
  106. mib[2] = RANDOM_ERANDOM;
  107. for (i = 0; i < sizeof(rdat.rnd) / sizeof(uint); i++) {
  108. len = sizeof(uint);
  109. if (sysctl(mib, 3, &rdat.rnd[i], &len, NULL, 0) == -1)
  110. break;
  111. }
  112. }
  113. #endif
  114. arc4_addrandom(as, (void *) &rdat, sizeof(rdat));
  115. /*
  116. * Throw away the first N words of output, as suggested in the
  117. * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
  118. * by Fluher, Mantin, and Shamir.
  119. * http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps
  120. * N = 256 in our case.
  121. */
  122. for (n = 0; n < 256 * 4; n++)
  123. arc4_getbyte(as);
  124. }
  125. static __inline__ uint8_t
  126. arc4_getbyte(as)
  127. struct arc4_stream *as;
  128. {
  129. uint8_t si, sj;
  130. as->i = (as->i + 1);
  131. si = as->s[as->i];
  132. as->j = (as->j + si);
  133. sj = as->s[as->j];
  134. as->s[as->i] = sj;
  135. as->s[as->j] = si;
  136. return (as->s[(si + sj) & 0xff]);
  137. }
  138. static __inline__ uint32_t
  139. arc4_getword(as)
  140. struct arc4_stream *as;
  141. {
  142. uint32_t val;
  143. val = arc4_getbyte(as) << 24;
  144. val |= arc4_getbyte(as) << 16;
  145. val |= arc4_getbyte(as) << 8;
  146. val |= arc4_getbyte(as);
  147. return val;
  148. }
  149. libc_hidden_proto(arc4random_stir)
  150. void
  151. arc4random_stir(void)
  152. {
  153. if (!rs_initialized) {
  154. arc4_init(&rs);
  155. rs_initialized = 1;
  156. }
  157. arc4_stir(&rs);
  158. }
  159. libc_hidden_def(arc4random_stir)
  160. void
  161. arc4random_addrandom(u_char *dat, int datlen)
  162. {
  163. if (!rs_initialized)
  164. arc4random_stir();
  165. arc4_addrandom(&rs, dat, datlen);
  166. }
  167. uint32_t
  168. arc4random(void)
  169. {
  170. if (!rs_initialized)
  171. arc4random_stir();
  172. return arc4_getword(&rs);
  173. }
  174. #if 0
  175. /*-------- Test code --------*/
  176. #include <stdlib.h>
  177. #include <stdio.h>
  178. int main(void) {
  179. int random_number;
  180. random_number = arc4random() % 65536;
  181. printf("%d\n", random_number);
  182. return 0;
  183. }
  184. #endif