apr_random.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * See the paper "On Randomness" by Ben Laurie for an explanation of this PRNG.
  18. * http://www.apache-ssl.org/randomness.pdf
  19. * XXX: Is there a formal proof of this PRNG? Couldn't we use the more popular
  20. * Mersenne Twister PRNG (and BSD licensed)?
  21. */
  22. #include "apr.h"
  23. #include "apr_pools.h"
  24. #include "apr_random.h"
  25. #include "apr_thread_proc.h"
  26. #include <assert.h>
  27. #ifdef min
  28. #undef min
  29. #endif
  30. #define min(a,b) ((a) < (b) ? (a) : (b))
  31. #define APR_RANDOM_DEFAULT_POOLS 32
  32. #define APR_RANDOM_DEFAULT_REHASH_SIZE 1024
  33. #define APR_RANDOM_DEFAULT_RESEED_SIZE 32
  34. #define APR_RANDOM_DEFAULT_HASH_SECRET_SIZE 32
  35. #define APR_RANDOM_DEFAULT_G_FOR_INSECURE 32
  36. #define APR_RANDOM_DEFAULT_G_FOR_SECURE 320
  37. typedef struct apr_random_pool_t {
  38. unsigned char *pool;
  39. unsigned int bytes;
  40. unsigned int pool_size;
  41. } apr_random_pool_t;
  42. #define hash_init(h) (h)->init(h)
  43. #define hash_add(h,b,n) (h)->add(h,b,n)
  44. #define hash_finish(h,r) (h)->finish(h,r)
  45. #define hash(h,r,b,n) hash_init(h),hash_add(h,b,n),hash_finish(h,r)
  46. #define crypt_setkey(c,k) (c)->set_key((c)->data,k)
  47. #define crypt_crypt(c,out,in) (c)->crypt((c)->date,out,in)
  48. struct apr_random_t {
  49. apr_pool_t *apr_pool;
  50. apr_crypto_hash_t *pool_hash;
  51. unsigned int npools;
  52. apr_random_pool_t *pools;
  53. unsigned int next_pool;
  54. unsigned int generation;
  55. apr_size_t rehash_size;
  56. apr_size_t reseed_size;
  57. apr_crypto_hash_t *key_hash;
  58. #define K_size(g) ((g)->key_hash->size)
  59. apr_crypto_hash_t *prng_hash;
  60. #define B_size(g) ((g)->prng_hash->size)
  61. unsigned char *H;
  62. unsigned char *H_waiting;
  63. #define H_size(g) (B_size(g)+K_size(g))
  64. #define H_current(g) (((g)->insecure_started && !(g)->secure_started) \
  65. ? (g)->H_waiting : (g)->H)
  66. unsigned char *randomness;
  67. apr_size_t random_bytes;
  68. unsigned int g_for_insecure;
  69. unsigned int g_for_secure;
  70. unsigned int secure_base;
  71. unsigned int insecure_started:1;
  72. unsigned int secure_started:1;
  73. apr_random_t *next;
  74. };
  75. static apr_random_t *all_random;
  76. static apr_status_t random_cleanup(void *data)
  77. {
  78. apr_random_t *remove_this = data,
  79. *cur = all_random,
  80. **prev_ptr = &all_random;
  81. while (cur) {
  82. if (cur == remove_this) {
  83. *prev_ptr = cur->next;
  84. break;
  85. }
  86. prev_ptr = &cur->next;
  87. cur = cur->next;
  88. }
  89. return APR_SUCCESS;
  90. }
  91. APR_DECLARE(void) apr_random_init(apr_random_t *g,apr_pool_t *p,
  92. apr_crypto_hash_t *pool_hash,
  93. apr_crypto_hash_t *key_hash,
  94. apr_crypto_hash_t *prng_hash)
  95. {
  96. unsigned int n;
  97. g->apr_pool = p;
  98. g->pool_hash = pool_hash;
  99. g->key_hash = key_hash;
  100. g->prng_hash = prng_hash;
  101. g->npools = APR_RANDOM_DEFAULT_POOLS;
  102. g->pools = apr_palloc(p,g->npools*sizeof *g->pools);
  103. for (n = 0; n < g->npools; ++n) {
  104. g->pools[n].bytes = g->pools[n].pool_size = 0;
  105. g->pools[n].pool = NULL;
  106. }
  107. g->next_pool = 0;
  108. g->generation = 0;
  109. g->rehash_size = APR_RANDOM_DEFAULT_REHASH_SIZE;
  110. /* Ensure that the rehash size is twice the size of the pool hasher */
  111. g->rehash_size = ((g->rehash_size+2*g->pool_hash->size-1)/g->pool_hash->size
  112. /2)*g->pool_hash->size*2;
  113. g->reseed_size = APR_RANDOM_DEFAULT_RESEED_SIZE;
  114. g->H = apr_pcalloc(p,H_size(g));
  115. g->H_waiting = apr_pcalloc(p,H_size(g));
  116. g->randomness = apr_palloc(p,B_size(g));
  117. g->random_bytes = 0;
  118. g->g_for_insecure = APR_RANDOM_DEFAULT_G_FOR_INSECURE;
  119. g->secure_base = 0;
  120. g->g_for_secure = APR_RANDOM_DEFAULT_G_FOR_SECURE;
  121. g->secure_started = g->insecure_started = 0;
  122. g->next = all_random;
  123. all_random = g;
  124. apr_pool_cleanup_register(p, g, random_cleanup, apr_pool_cleanup_null);
  125. }
  126. static void mix_pid(apr_random_t *g,unsigned char *H,pid_t pid)
  127. {
  128. hash_init(g->key_hash);
  129. hash_add(g->key_hash,H,H_size(g));
  130. hash_add(g->key_hash,&pid,sizeof pid);
  131. hash_finish(g->key_hash,H);
  132. }
  133. static void mixer(apr_random_t *g,pid_t pid)
  134. {
  135. unsigned char *H = H_current(g);
  136. /* mix the PID into the current H */
  137. mix_pid(g,H,pid);
  138. /* if we are in waiting, then also mix into main H */
  139. if (H != g->H)
  140. mix_pid(g,g->H,pid);
  141. /* change order of pool mixing for good measure - note that going
  142. backwards is much better than going forwards */
  143. --g->generation;
  144. /* blow away any lingering randomness */
  145. g->random_bytes = 0;
  146. }
  147. APR_DECLARE(void) apr_random_after_fork(apr_proc_t *proc)
  148. {
  149. apr_random_t *r;
  150. for (r = all_random; r; r = r->next)
  151. /*
  152. * XXX Note: the pid does not provide sufficient entropy to
  153. * actually call this secure. See Ben's paper referenced at
  154. * the top of this file.
  155. */
  156. mixer(r,proc->pid);
  157. }
  158. APR_DECLARE(apr_random_t *) apr_random_standard_new(apr_pool_t *p)
  159. {
  160. apr_random_t *r = apr_palloc(p,sizeof *r);
  161. apr_random_init(r,p,apr_crypto_sha256_new(p),apr_crypto_sha256_new(p),
  162. apr_crypto_sha256_new(p));
  163. return r;
  164. }
  165. static void rekey(apr_random_t *g)
  166. {
  167. unsigned int n;
  168. unsigned char *H = H_current(g);
  169. hash_init(g->key_hash);
  170. hash_add(g->key_hash,H,H_size(g));
  171. for (n = 0 ; n < g->npools && (n == 0 || g->generation&(1 << (n-1)))
  172. ; ++n) {
  173. hash_add(g->key_hash,g->pools[n].pool,g->pools[n].bytes);
  174. g->pools[n].bytes = 0;
  175. }
  176. hash_finish(g->key_hash,H+B_size(g));
  177. ++g->generation;
  178. if (!g->insecure_started && g->generation > g->g_for_insecure) {
  179. g->insecure_started = 1;
  180. if (!g->secure_started) {
  181. memcpy(g->H_waiting,g->H,H_size(g));
  182. g->secure_base = g->generation;
  183. }
  184. }
  185. if (!g->secure_started && g->generation > g->secure_base+g->g_for_secure) {
  186. g->secure_started = 1;
  187. memcpy(g->H,g->H_waiting,H_size(g));
  188. }
  189. }
  190. APR_DECLARE(void) apr_random_add_entropy(apr_random_t *g,const void *entropy_,
  191. apr_size_t bytes)
  192. {
  193. unsigned int n;
  194. const unsigned char *entropy = entropy_;
  195. for (n = 0; n < bytes; ++n) {
  196. apr_random_pool_t *p = &g->pools[g->next_pool];
  197. if (++g->next_pool == g->npools)
  198. g->next_pool = 0;
  199. if (p->pool_size < p->bytes+1) {
  200. unsigned char *np = apr_palloc(g->apr_pool,(p->bytes+1)*2);
  201. memcpy(np,p->pool,p->bytes);
  202. p->pool = np;
  203. p->pool_size = (p->bytes+1)*2;
  204. }
  205. p->pool[p->bytes++] = entropy[n];
  206. if (p->bytes == g->rehash_size) {
  207. apr_size_t r;
  208. for (r = 0; r < p->bytes/2; r+=g->pool_hash->size)
  209. hash(g->pool_hash,p->pool+r,p->pool+r*2,g->pool_hash->size*2);
  210. p->bytes/=2;
  211. }
  212. assert(p->bytes < g->rehash_size);
  213. }
  214. if (g->pools[0].bytes >= g->reseed_size)
  215. rekey(g);
  216. }
  217. /* This will give g->B_size bytes of randomness */
  218. static void apr_random_block(apr_random_t *g,unsigned char *random)
  219. {
  220. /* FIXME: in principle, these are different hashes */
  221. hash(g->prng_hash,g->H,g->H,H_size(g));
  222. hash(g->prng_hash,random,g->H,B_size(g));
  223. }
  224. static void apr_random_bytes(apr_random_t *g,unsigned char *random,
  225. apr_size_t bytes)
  226. {
  227. apr_size_t n;
  228. for (n = 0; n < bytes; ) {
  229. apr_size_t l;
  230. if (g->random_bytes == 0) {
  231. apr_random_block(g,g->randomness);
  232. g->random_bytes = B_size(g);
  233. }
  234. l = min(bytes-n,g->random_bytes);
  235. memcpy(&random[n],g->randomness+B_size(g)-g->random_bytes,l);
  236. g->random_bytes-=l;
  237. n+=l;
  238. }
  239. }
  240. APR_DECLARE(apr_status_t) apr_random_secure_bytes(apr_random_t *g,
  241. void *random,
  242. apr_size_t bytes)
  243. {
  244. if (!g->secure_started)
  245. return APR_ENOTENOUGHENTROPY;
  246. apr_random_bytes(g,random,bytes);
  247. return APR_SUCCESS;
  248. }
  249. APR_DECLARE(apr_status_t) apr_random_insecure_bytes(apr_random_t *g,
  250. void *random,
  251. apr_size_t bytes)
  252. {
  253. if (!g->insecure_started)
  254. return APR_ENOTENOUGHENTROPY;
  255. apr_random_bytes(g,random,bytes);
  256. return APR_SUCCESS;
  257. }
  258. APR_DECLARE(void) apr_random_barrier(apr_random_t *g)
  259. {
  260. g->secure_started = 0;
  261. g->secure_base = g->generation;
  262. }
  263. APR_DECLARE(apr_status_t) apr_random_secure_ready(apr_random_t *r)
  264. {
  265. if (!r->secure_started)
  266. return APR_ENOTENOUGHENTROPY;
  267. return APR_SUCCESS;
  268. }
  269. APR_DECLARE(apr_status_t) apr_random_insecure_ready(apr_random_t *r)
  270. {
  271. if (!r->insecure_started)
  272. return APR_ENOTENOUGHENTROPY;
  273. return APR_SUCCESS;
  274. }