arc4random.c 4.4 KB

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