fspr_random.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 "???" by Ben Laurie for an explanation of this PRNG.
  18. */
  19. #include "fspr.h"
  20. #include "fspr_pools.h"
  21. #include "fspr_random.h"
  22. #include "fspr_thread_proc.h"
  23. #include <assert.h>
  24. #ifdef min
  25. #undef min
  26. #endif
  27. #define min(a,b) ((a) < (b) ? (a) : (b))
  28. #define APR_RANDOM_DEFAULT_POOLS 32
  29. #define APR_RANDOM_DEFAULT_REHASH_SIZE 1024
  30. #define APR_RANDOM_DEFAULT_RESEED_SIZE 32
  31. #define APR_RANDOM_DEFAULT_HASH_SECRET_SIZE 32
  32. #define APR_RANDOM_DEFAULT_G_FOR_INSECURE 32
  33. #define APR_RANDOM_DEFAULT_G_FOR_SECURE 320
  34. typedef struct fspr_random_pool_t {
  35. unsigned char *pool;
  36. unsigned int bytes;
  37. unsigned int pool_size;
  38. } fspr_random_pool_t;
  39. #define hash_init(h) (h)->init(h)
  40. #define hash_add(h,b,n) (h)->add(h,b,n)
  41. #define hash_finish(h,r) (h)->finish(h,r)
  42. #define hash(h,r,b,n) hash_init(h),hash_add(h,b,n),hash_finish(h,r)
  43. #define crypt_setkey(c,k) (c)->set_key((c)->data,k)
  44. #define crypt_crypt(c,out,in) (c)->crypt((c)->date,out,in)
  45. struct fspr_random_t {
  46. fspr_pool_t *fspr_pool;
  47. fspr_crypto_hash_t *pool_hash;
  48. unsigned int npools;
  49. fspr_random_pool_t *pools;
  50. unsigned int next_pool;
  51. unsigned int generation;
  52. fspr_size_t rehash_size;
  53. fspr_size_t reseed_size;
  54. fspr_crypto_hash_t *key_hash;
  55. #define K_size(g) ((g)->key_hash->size)
  56. fspr_crypto_hash_t *prng_hash;
  57. #define B_size(g) ((g)->prng_hash->size)
  58. unsigned char *H;
  59. unsigned char *H_waiting;
  60. #define H_size(g) (B_size(g)+K_size(g))
  61. #define H_current(g) (((g)->insecure_started && !(g)->secure_started) \
  62. ? (g)->H_waiting : (g)->H)
  63. unsigned char *randomness;
  64. fspr_size_t random_bytes;
  65. unsigned int g_for_insecure;
  66. unsigned int g_for_secure;
  67. unsigned int secure_base;
  68. unsigned int insecure_started:1;
  69. unsigned int secure_started:1;
  70. fspr_random_t *next;
  71. };
  72. static fspr_random_t *all_random;
  73. APR_DECLARE(void) fspr_random_init(fspr_random_t *g,fspr_pool_t *p,
  74. fspr_crypto_hash_t *pool_hash,
  75. fspr_crypto_hash_t *key_hash,
  76. fspr_crypto_hash_t *prng_hash)
  77. {
  78. unsigned int n;
  79. g->fspr_pool = p;
  80. g->pool_hash = pool_hash;
  81. g->key_hash = key_hash;
  82. g->prng_hash = prng_hash;
  83. g->npools = APR_RANDOM_DEFAULT_POOLS;
  84. g->pools = fspr_palloc(p,g->npools*sizeof *g->pools);
  85. for (n = 0; n < g->npools; ++n) {
  86. g->pools[n].bytes = g->pools[n].pool_size = 0;
  87. g->pools[n].pool = NULL;
  88. }
  89. g->next_pool = 0;
  90. g->generation = 0;
  91. g->rehash_size = APR_RANDOM_DEFAULT_REHASH_SIZE;
  92. /* Ensure that the rehash size is twice the size of the pool hasher */
  93. g->rehash_size = ((g->rehash_size+2*g->pool_hash->size-1)/g->pool_hash->size
  94. /2)*g->pool_hash->size*2;
  95. g->reseed_size = APR_RANDOM_DEFAULT_RESEED_SIZE;
  96. g->H = fspr_pcalloc(p,H_size(g));
  97. g->H_waiting = fspr_pcalloc(p,H_size(g));
  98. g->randomness = fspr_palloc(p,B_size(g));
  99. g->random_bytes = 0;
  100. g->g_for_insecure = APR_RANDOM_DEFAULT_G_FOR_INSECURE;
  101. g->secure_base = 0;
  102. g->g_for_secure = APR_RANDOM_DEFAULT_G_FOR_SECURE;
  103. g->secure_started = g->insecure_started = 0;
  104. g->next = all_random;
  105. all_random = g;
  106. }
  107. static void mix_pid(fspr_random_t *g,unsigned char *H,pid_t pid)
  108. {
  109. hash_init(g->key_hash);
  110. hash_add(g->key_hash,H,H_size(g));
  111. hash_add(g->key_hash,&pid,sizeof pid);
  112. hash_finish(g->key_hash,H);
  113. }
  114. static void mixer(fspr_random_t *g,pid_t pid)
  115. {
  116. unsigned char *H = H_current(g);
  117. /* mix the PID into the current H */
  118. mix_pid(g,H,pid);
  119. /* if we are in waiting, then also mix into main H */
  120. if (H != g->H)
  121. mix_pid(g,g->H,pid);
  122. /* change order of pool mixing for good measure - note that going
  123. backwards is much better than going forwards */
  124. --g->generation;
  125. /* blow away any lingering randomness */
  126. g->random_bytes = 0;
  127. }
  128. APR_DECLARE(void) fspr_random_after_fork(fspr_proc_t *proc)
  129. {
  130. fspr_random_t *r;
  131. for (r = all_random; r; r = r->next)
  132. mixer(r,proc->pid);
  133. }
  134. APR_DECLARE(fspr_random_t *) fspr_random_standard_new(fspr_pool_t *p)
  135. {
  136. fspr_random_t *r = fspr_palloc(p,sizeof *r);
  137. fspr_random_init(r,p,fspr_crypto_sha256_new(p),fspr_crypto_sha256_new(p),
  138. fspr_crypto_sha256_new(p));
  139. return r;
  140. }
  141. static void rekey(fspr_random_t *g)
  142. {
  143. unsigned int n;
  144. unsigned char *H = H_current(g);
  145. hash_init(g->key_hash);
  146. hash_add(g->key_hash,H,H_size(g));
  147. for (n = 0 ; n < g->npools && (n == 0 || g->generation&(1 << (n-1)))
  148. ; ++n) {
  149. hash_add(g->key_hash,g->pools[n].pool,g->pools[n].bytes);
  150. g->pools[n].bytes = 0;
  151. }
  152. hash_finish(g->key_hash,H+B_size(g));
  153. ++g->generation;
  154. if (!g->insecure_started && g->generation > g->g_for_insecure) {
  155. g->insecure_started = 1;
  156. if (!g->secure_started) {
  157. memcpy(g->H_waiting,g->H,H_size(g));
  158. g->secure_base = g->generation;
  159. }
  160. }
  161. if (!g->secure_started && g->generation > g->secure_base+g->g_for_secure) {
  162. g->secure_started = 1;
  163. memcpy(g->H,g->H_waiting,H_size(g));
  164. }
  165. }
  166. APR_DECLARE(void) fspr_random_add_entropy(fspr_random_t *g,const void *entropy_,
  167. fspr_size_t bytes)
  168. {
  169. unsigned int n;
  170. const unsigned char *entropy = entropy_;
  171. for (n = 0; n < bytes; ++n) {
  172. fspr_random_pool_t *p = &g->pools[g->next_pool];
  173. if (++g->next_pool == g->npools)
  174. g->next_pool = 0;
  175. if (p->pool_size < p->bytes+1) {
  176. unsigned char *np = fspr_palloc(g->fspr_pool,(p->bytes+1)*2);
  177. memcpy(np,p->pool,p->bytes);
  178. p->pool = np;
  179. p->pool_size = (p->bytes+1)*2;
  180. }
  181. p->pool[p->bytes++] = entropy[n];
  182. if (p->bytes == g->rehash_size) {
  183. unsigned int r;
  184. for (r = 0; r < p->bytes/2; r+=g->pool_hash->size)
  185. hash(g->pool_hash,p->pool+r,p->pool+r*2,g->pool_hash->size*2);
  186. p->bytes/=2;
  187. }
  188. assert(p->bytes < g->rehash_size);
  189. }
  190. if (g->pools[0].bytes >= g->reseed_size)
  191. rekey(g);
  192. }
  193. /* This will give g->B_size bytes of randomness */
  194. static void fspr_random_block(fspr_random_t *g,unsigned char *random)
  195. {
  196. /* FIXME: in principle, these are different hashes */
  197. hash(g->prng_hash,g->H,g->H,H_size(g));
  198. hash(g->prng_hash,random,g->H,B_size(g));
  199. }
  200. static void fspr_random_bytes(fspr_random_t *g,unsigned char *random,
  201. fspr_size_t bytes)
  202. {
  203. fspr_size_t n;
  204. for (n = 0; n < bytes; ) {
  205. int l;
  206. if (g->random_bytes == 0) {
  207. fspr_random_block(g,g->randomness);
  208. g->random_bytes = B_size(g);
  209. }
  210. l = min(bytes-n,g->random_bytes);
  211. memcpy(&random[n],g->randomness+B_size(g)-g->random_bytes,l);
  212. g->random_bytes-=l;
  213. n+=l;
  214. }
  215. }
  216. APR_DECLARE(fspr_status_t) fspr_random_secure_bytes(fspr_random_t *g,
  217. void *random,
  218. fspr_size_t bytes)
  219. {
  220. if (!g->secure_started)
  221. return APR_ENOTENOUGHENTROPY;
  222. fspr_random_bytes(g,random,bytes);
  223. return APR_SUCCESS;
  224. }
  225. APR_DECLARE(fspr_status_t) fspr_random_insecure_bytes(fspr_random_t *g,
  226. void *random,
  227. fspr_size_t bytes)
  228. {
  229. if (!g->insecure_started)
  230. return APR_ENOTENOUGHENTROPY;
  231. fspr_random_bytes(g,random,bytes);
  232. return APR_SUCCESS;
  233. }
  234. APR_DECLARE(void) fspr_random_barrier(fspr_random_t *g)
  235. {
  236. g->secure_started = 0;
  237. g->secure_base = g->generation;
  238. }
  239. APR_DECLARE(fspr_status_t) fspr_random_secure_ready(fspr_random_t *r)
  240. {
  241. if (!r->secure_started)
  242. return APR_ENOTENOUGHENTROPY;
  243. return APR_SUCCESS;
  244. }
  245. APR_DECLARE(fspr_status_t) fspr_random_insecure_ready(fspr_random_t *r)
  246. {
  247. if (!r->insecure_started)
  248. return APR_ENOTENOUGHENTROPY;
  249. return APR_SUCCESS;
  250. }