arc4random.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #endif
  36. struct arc4_stream {
  37. uint8_t i;
  38. uint8_t j;
  39. uint8_t s[256];
  40. };
  41. static int rs_initialized;
  42. static struct arc4_stream rs;
  43. static __inline__ void arc4_init(struct arc4_stream *);
  44. static __inline__ void arc4_addrandom(struct arc4_stream *, u_char *, int);
  45. static void arc4_stir(struct arc4_stream *);
  46. static __inline__ uint8_t arc4_getbyte(struct arc4_stream *);
  47. static __inline__ uint32_t arc4_getword(struct arc4_stream *);
  48. static __inline__ void
  49. arc4_init(as)
  50. struct arc4_stream *as;
  51. {
  52. int n;
  53. for (n = 0; n < 256; n++)
  54. as->s[n] = n;
  55. as->i = 0;
  56. as->j = 0;
  57. }
  58. static __inline__ void
  59. arc4_addrandom(as, dat, datlen)
  60. struct arc4_stream *as;
  61. u_char *dat;
  62. int datlen;
  63. {
  64. int n;
  65. uint8_t si;
  66. as->i--;
  67. for (n = 0; n < 256; n++) {
  68. as->i = (as->i + 1);
  69. si = as->s[as->i];
  70. as->j = (as->j + si + dat[n % datlen]);
  71. as->s[as->i] = as->s[as->j];
  72. as->s[as->j] = si;
  73. }
  74. as->j = as->i;
  75. }
  76. static void
  77. arc4_stir(as)
  78. struct arc4_stream *as;
  79. {
  80. int fd;
  81. struct {
  82. struct timeval tv;
  83. uint rnd[(128 - sizeof(struct timeval)) / sizeof(uint)];
  84. } rdat;
  85. int n;
  86. gettimeofday(&rdat.tv, NULL);
  87. fd = open("/dev/urandom", O_RDONLY);
  88. if (fd != -1) {
  89. read(fd, rdat.rnd, sizeof(rdat.rnd));
  90. close(fd);
  91. }
  92. #ifdef __ARC4RANDOM_USE_ERANDOM__
  93. else {
  94. int mib[3];
  95. uint i;
  96. size_t len;
  97. /* Device could not be opened, we might be chrooted, take
  98. * randomness from sysctl. */
  99. mib[0] = CTL_KERN;
  100. mib[1] = KERN_RANDOM;
  101. mib[2] = RANDOM_ERANDOM;
  102. for (i = 0; i < sizeof(rdat.rnd) / sizeof(uint); i++) {
  103. len = sizeof(uint);
  104. if (sysctl(mib, 3, &rdat.rnd[i], &len, NULL, 0) == -1)
  105. break;
  106. }
  107. }
  108. #endif
  109. arc4_addrandom(as, (void *) &rdat, sizeof(rdat));
  110. /*
  111. * Throw away the first N words of output, as suggested in the
  112. * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
  113. * by Fluher, Mantin, and Shamir.
  114. * http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps
  115. * N = 256 in our case.
  116. */
  117. for (n = 0; n < 256 * 4; n++)
  118. arc4_getbyte(as);
  119. }
  120. static __inline__ uint8_t
  121. arc4_getbyte(as)
  122. struct arc4_stream *as;
  123. {
  124. uint8_t si, sj;
  125. as->i = (as->i + 1);
  126. si = as->s[as->i];
  127. as->j = (as->j + si);
  128. sj = as->s[as->j];
  129. as->s[as->i] = sj;
  130. as->s[as->j] = si;
  131. return (as->s[(si + sj) & 0xff]);
  132. }
  133. static __inline__ uint32_t
  134. arc4_getword(as)
  135. struct arc4_stream *as;
  136. {
  137. uint32_t val;
  138. val = arc4_getbyte(as) << 24;
  139. val |= arc4_getbyte(as) << 16;
  140. val |= arc4_getbyte(as) << 8;
  141. val |= arc4_getbyte(as);
  142. return val;
  143. }
  144. void
  145. arc4random_stir(void)
  146. {
  147. if (!rs_initialized) {
  148. arc4_init(&rs);
  149. rs_initialized = 1;
  150. }
  151. arc4_stir(&rs);
  152. }
  153. libc_hidden_def(arc4random_stir)
  154. void
  155. arc4random_addrandom(u_char *dat, int datlen)
  156. {
  157. if (!rs_initialized)
  158. arc4random_stir();
  159. arc4_addrandom(&rs, dat, datlen);
  160. }
  161. uint32_t
  162. arc4random(void)
  163. {
  164. if (!rs_initialized)
  165. arc4random_stir();
  166. return arc4_getword(&rs);
  167. }
  168. #if 0
  169. /*-------- Test code --------*/
  170. #include <stdlib.h>
  171. #include <stdio.h>
  172. int main(void) {
  173. int random_number;
  174. random_number = arc4random() % 65536;
  175. printf("%d\n", random_number);
  176. return 0;
  177. }
  178. #endif