arc4random.c 4.6 KB

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