summaryrefslogtreecommitdiff
path: root/libjava/classpath/gnu/javax/crypto/prng/CSPRNG.java
blob: ecea2f469199a81d1f15e73416d2faefbdfef1b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
/* CSPRNG.java -- continuously-seeded pseudo-random number generator.
   Copyright (C) 2004, 2006 Free Software Foundation, Inc.

This file is a part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.  */


package gnu.javax.crypto.prng;

import gnu.java.security.Configuration;
import gnu.java.security.Properties;
import gnu.java.security.Registry;
import gnu.java.security.hash.HashFactory;
import gnu.java.security.hash.IMessageDigest;
import gnu.java.security.prng.BasePRNG;
import gnu.java.security.prng.EntropySource;
import gnu.java.security.prng.IRandom;
import gnu.java.security.prng.LimitReachedException;
import gnu.java.security.util.SimpleList;
import gnu.java.security.util.Util;
import gnu.javax.crypto.cipher.CipherFactory;
import gnu.javax.crypto.cipher.IBlockCipher;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.AccessController;
import java.security.InvalidKeyException;
import java.security.PrivilegedAction;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * An entropy pool-based pseudo-random number generator based on the PRNG in
 * Peter Gutmann's cryptlib (<a
 * href="http://www.cs.auckland.ac.nz/~pgut001/cryptlib/">http://www.cs.auckland.ac.nz/~pgut001/cryptlib/</a>).
 * <p>
 * The basic properties of this generator are:
 * <ol>
 * <li>The internal state cannot be determined by knowledge of the input.</li>
 * <li>It is resistant to bias introduced by specific inputs.</li>
 * <li>The output does not reveal the state of the generator.</li>
 * </ol>
 */
public class CSPRNG
    extends BasePRNG
{
  private static final Logger log = Logger.getLogger(CSPRNG.class.getName());
  /**
   * Property name for the list of files to read for random values. The mapped
   * value is a list with the following values:
   * <ol>
   * <li>A {@link Double}, indicating the suggested <i>quality</i> of this
   * source. This value must be between 0 and 100.</li>
   * <li>An {@link Integer}, indicating the number of bytes to skip in the
   * file before reading bytes. This can be any nonnegative value.</li>
   * <li>An {@link Integer}, indicating the number of bytes to read.</li>
   * <li>A {@link String}, indicating the path to the file.</li>
   * </ol>
   *
   * @see gnu.java.security.util.SimpleList
   */
  public static final String FILE_SOURCES = "gnu.crypto.prng.pool.files";
  /**
   * Property name for the list of URLs to poll for random values. The mapped
   * value is a list formatted similarly as in {@link #FILE_SOURCES}, but the
   * fourth member is a {@link URL}.
   */
  public static final String URL_SOURCES = "gnu.crypto.prng.pool.urls";
  /**
   * Property name for the list of programs to execute, and use the output as
   * new random bytes. The mapped property is formatted similarly an in
   * {@link #FILE_SOURCES} and {@link #URL_SOURCES}, except the fourth member
   * is a {@link String} of the program to execute.
   */
  public static final String PROGRAM_SOURCES = "gnu.crypto.prng.pool.programs";
  /**
   * Property name for a list of other sources of entropy. The mapped value must
   * be a list of {@link EntropySource} objects.
   */
  public static final String OTHER_SOURCES = "gnu.crypto.prng.pool.other";
  /**
   * Property name for whether or not to wait for the slow poll to complete,
   * passed as a {@link Boolean}. The default value is true.
   */
  public static final String BLOCKING = "gnu.crypto.prng.pool.blocking";
  private static final String FILES = "gnu.crypto.csprng.file.";
  private static final String URLS = "gnu.crypto.csprng.url.";
  private static final String PROGS = "gnu.crypto.csprng.program.";
  private static final String OTHER = "gnu.crypto.csprng.other.";
  private static final String BLOCK = "gnu.crypto.csprng.blocking";
  private static final int POOL_SIZE = 256;
  private static final int ALLOC_SIZE = 260;
  private static final int OUTPUT_SIZE = POOL_SIZE / 2;
  private static final int X917_POOL_SIZE = 16;
  private static final String HASH_FUNCTION = Registry.SHA160_HASH;
  private static final String CIPHER = Registry.AES_CIPHER;
  private static final int MIX_COUNT = 10;
  private static final int X917_LIFETIME = 8192;
  // FIXME this should be configurable.
  private static final int SPINNER_COUNT = 8;
  /**
   * The spinner group singleton. We use this to add a small amount of
   * randomness (in addition to the current time and the amount of free memory)
   * based on the randomness (if any) present due to system load and thread
   * scheduling.
   */
  private static final Spinner[] SPINNERS = new Spinner[SPINNER_COUNT];
  private static final Thread[] SPINNER_THREADS = new Thread[SPINNER_COUNT];
  static
    {
      for (int i = 0; i < SPINNER_COUNT; i++)
        {
          SPINNER_THREADS[i] = new Thread(SPINNERS[i] = new Spinner(),
                                          "spinner-" + i);
          SPINNER_THREADS[i].setDaemon(true);
          SPINNER_THREADS[i].setPriority(Thread.MIN_PRIORITY);
          SPINNER_THREADS[i].start();
        }
    }
  /** The message digest (SHA-1) used in the mixing function. */
  private final IMessageDigest hash;
  /** The cipher (AES) used in the output masking function. */
  private final IBlockCipher cipher;
  /** The number of times the pool has been mixed. */
  private int mixCount;
  /** The entropy pool. */
  private final byte[] pool;
  /** The quality of the random pool (percentage). */
  private double quality;
  /** The index of the next byte in the entropy pool. */
  private int index;
  /** The pool for the X9.17-like generator. */
  private byte[] x917pool;
  /** The number of iterations of the X9.17-like generators. */
  private int x917count;
  /** Whether or not the X9.17-like generator is initialized. */
  private boolean x917init;
  /** The list of file soures. */
  private final List files;
  /** The list of URL sources. */
  private final List urls;
  /** The list of program sources. */
  private final List progs;
  /** The list of other sources. */
  private final List other;
  /** Whether or not to wait for the slow poll to complete. */
  private boolean blocking;
  /** The thread that polls for random data. */
  private Poller poller;
  private Thread pollerThread;

  public CSPRNG()
  {
    super("CSPRNG");
    pool = new byte[ALLOC_SIZE];
    x917pool = new byte[X917_POOL_SIZE];
    x917count = 0;
    x917init = false;
    quality = 0.0;
    hash = HashFactory.getInstance(HASH_FUNCTION);
    cipher = CipherFactory.getInstance(CIPHER);
    buffer = new byte[OUTPUT_SIZE];
    ndx = 0;
    initialised = false;
    files = new LinkedList();
    urls = new LinkedList();
    progs = new LinkedList();
    other = new LinkedList();
  }

  /**
   * Create and initialize a CSPRNG instance with the "system" parameters; the
   * files, URLs, programs, and {@link EntropySource} sources used by the
   * instance are derived from properties set in the system {@link Properties}.
   * <p>
   * All properties are of the from <i>name</i>.</i>N</i>, where <i>name</i>
   * is the name of the source, and <i>N</i> is an integer (staring at 1) that
   * indicates the preference number for that source.
   * <p>
   * The following vales for <i>name</i> are used here:
   * <dl>
   * <dt>gnu.crypto.csprng.file</dt>
   * <dd>
   * <p>
   * These properties are file sources, passed as the {@link #FILE_SOURCES}
   * parameter of the instance. The property value is a 4-tuple formatted as:
   * </p>
   * <blockquote><i>quality</i> ; <i>offset</i> ; <i>count</i> ; <i>path</i></blockquote>
   * <p>
   * The parameters are mapped to the parameters defined for {@link
   * #FILE_SOURCES}. Leading or trailing spaces on any item are trimmed off.
   * </p>
   * </dd>
   * <dt>gnu.crypto.csprng.url</dt>
   * <dd>
   * <p>
   * These properties are URL sources, passed as the {@link #URL_SOURCES}
   * parameter of the instance. The property is formatted the same way as file
   * sources, but the <i>path</i> argument must be a valid URL.
   * </p>
   * </dd>
   * <dt>gnu.crypto.csprng.program</dt>
   * <dd>
   * <p>
   * These properties are program sources, passed as the {@link
   * #PROGRAM_SOURCES} parameter of the instance. This property is formatted the
   * same way as file and URL sources, but the last argument is a program and
   * its arguments.
   * </p>
   * </dd>
   * <dt>gnu.crypto.cspring.other</dt>
   * <dd>
   * <p>
   * These properties are other sources, passed as the {@link #OTHER_SOURCES}
   * parameter of the instance. The property value must be the full name of a
   * class that implements the {@link EntropySource} interface and has a public
   * no-argument constructor.
   * </p>
   * </dd>
   * </dl>
   * <p>
   * Finally, a boolean property "gnu.crypto.csprng.blocking" can be set to the
   * desired value of {@link #BLOCKING}.
   * <p>
   * An example of valid properties would be:
   * <pre>
   *  gnu.crypto.csprng.blocking=true
   *
   *  gnu.crypto.csprng.file.1=75.0;0;256;/dev/random
   *  gnu.crypto.csprng.file.2=10.0;0;100;/home/user/file
   *
   *  gnu.crypto.csprng.url.1=5.0;0;256;http://www.random.org/cgi-bin/randbyte?nbytes=256
   *  gnu.crypto.csprng.url.2=0;256;256;http://slashdot.org/
   *
   *  gnu.crypto.csprng.program.1=0.5;0;10;last -n 50
   *  gnu.crypto.csprng.program.2=0.5;0;10;tcpdump -c 5
   *
   *  gnu.crypto.csprng.other.1=foo.bar.MyEntropySource
   *  gnu.crypto.csprng.other.2=com.company.OtherEntropySource
   * </pre>
   */
  public static IRandom getSystemInstance() throws ClassNotFoundException,
      MalformedURLException, NumberFormatException
  {
    CSPRNG instance = new CSPRNG();
    HashMap attrib = new HashMap();
    attrib.put(BLOCKING, Boolean.valueOf(getProperty(BLOCK)));
    String s = null;
    // Get each file source "gnu.crypto.csprng.file.N".
    List l = new LinkedList();
    for (int i = 0; (s = getProperty(FILES + i)) != null; i++)
      try
        {
          l.add(parseString(s.trim()));
        }
      catch (NumberFormatException nfe)
        {
        }
    attrib.put(FILE_SOURCES, l);
    l = new LinkedList();
    for (int i = 0; (s = getProperty(URLS + i)) != null; i++)
      try
        {
          l.add(parseURL(s.trim()));
        }
      catch (NumberFormatException nfe)
        {
        }
      catch (MalformedURLException mue)
        {
        }
    attrib.put(URL_SOURCES, l);
    l = new LinkedList();
    for (int i = 0; (s = getProperty(PROGS + i)) != null; i++)
      try
        {
          l.add(parseString(s.trim()));
        }
      catch (NumberFormatException nfe)
        {
        }
    attrib.put(PROGRAM_SOURCES, l);
    l = new LinkedList();
    for (int i = 0; (s = getProperty(OTHER + i)) != null; i++)
      try
        {
          Class c = Class.forName(s.trim());
          l.add(c.newInstance());
        }
      catch (ClassNotFoundException cnfe)
        {
        }
      catch (InstantiationException ie)
        {
        }
      catch (IllegalAccessException iae)
        {
        }
    attrib.put(OTHER_SOURCES, l);
    instance.init(attrib);
    return instance;
  }

  private static String getProperty(final String name)
  {
    return (String) AccessController.doPrivileged(new PrivilegedAction()
    {
      public Object run()
      {
        return Properties.getProperty(name);
      }
    });
  }

  private static List parseString(String s) throws NumberFormatException
  {
    StringTokenizer tok = new StringTokenizer(s, ";");
    if (tok.countTokens() != 4)
      throw new IllegalArgumentException("malformed property");
    Double quality = new Double(tok.nextToken());
    Integer offset = new Integer(tok.nextToken());
    Integer length = new Integer(tok.nextToken());
    String str = tok.nextToken();
    return new SimpleList(quality, offset, length, str);
  }

  private static List parseURL(String s) throws MalformedURLException,
      NumberFormatException
  {
    StringTokenizer tok = new StringTokenizer(s, ";");
    if (tok.countTokens() != 4)
      throw new IllegalArgumentException("malformed property");
    Double quality = new Double(tok.nextToken());
    Integer offset = new Integer(tok.nextToken());
    Integer length = new Integer(tok.nextToken());
    URL url = new URL(tok.nextToken());
    return new SimpleList(quality, offset, length, url);
  }

  public Object clone()
  {
    return new CSPRNG();
  }

  public void setup(Map attrib)
  {
    List list = null;
    if (Configuration.DEBUG)
      log.fine("attrib=" + String.valueOf(attrib));
    try
      {
        list = (List) attrib.get(FILE_SOURCES);
        if (Configuration.DEBUG)
          log.fine("list=" + String.valueOf(list));
        if (list != null)
          {
            files.clear();
            for (Iterator it = list.iterator(); it.hasNext();)
              {
                List l = (List) it.next();
                if (Configuration.DEBUG)
                  log.fine("l=" + l);
                if (l.size() != 4)
                  {
                    if (Configuration.DEBUG)
                      log.fine("file list too small: " + l.size());
                    throw new IllegalArgumentException("invalid file list");
                  }
                Double quality = (Double) l.get(0);
                Integer offset = (Integer) l.get(1);
                Integer length = (Integer) l.get(2);
                String source = (String) l.get(3);
                files.add(new SimpleList(quality, offset, length, source));
              }
          }
      }
    catch (ClassCastException cce)
      {
        if (Configuration.DEBUG)
          log.log(Level.FINE, "bad file list", cce);
        throw new IllegalArgumentException("invalid file list");
      }
    try
      {
        list = (List) attrib.get(URL_SOURCES);
        if (Configuration.DEBUG)
          log.fine("list=" + String.valueOf(list));
        if (list != null)
          {
            urls.clear();
            for (Iterator it = list.iterator(); it.hasNext();)
              {
                List l = (List) it.next();
                if (Configuration.DEBUG)
                  log.fine("l=" + l);
                if (l.size() != 4)
                  {
                    if (Configuration.DEBUG)
                      log.fine("URL list too small: " + l.size());
                    throw new IllegalArgumentException("invalid URL list");
                  }
                Double quality = (Double) l.get(0);
                Integer offset = (Integer) l.get(1);
                Integer length = (Integer) l.get(2);
                URL source = (URL) l.get(3);
                urls.add(new SimpleList(quality, offset, length, source));
              }
          }
      }
    catch (ClassCastException cce)
      {
        if (Configuration.DEBUG)
          log.log(Level.FINE, "bad URL list", cce);
        throw new IllegalArgumentException("invalid URL list");
      }
    try
      {
        list = (List) attrib.get(PROGRAM_SOURCES);
        if (Configuration.DEBUG)
          log.fine("list=" + String.valueOf(list));
        if (list != null)
          {
            progs.clear();
            for (Iterator it = list.iterator(); it.hasNext();)
              {
                List l = (List) it.next();
                if (Configuration.DEBUG)
                  log.fine("l=" + l);
                if (l.size() != 4)
                  {
                    if (Configuration.DEBUG)
                      log.fine("program list too small: " + l.size());
                    throw new IllegalArgumentException("invalid program list");
                  }
                Double quality = (Double) l.get(0);
                Integer offset = (Integer) l.get(1);
                Integer length = (Integer) l.get(2);
                String source = (String) l.get(3);
                progs.add(new SimpleList(quality, offset, length, source));
              }
          }
      }
    catch (ClassCastException cce)
      {
        if (Configuration.DEBUG)
          log.log(Level.FINE, "bad program list", cce);
        throw new IllegalArgumentException("invalid program list");
      }
    try
      {
        list = (List) attrib.get(OTHER_SOURCES);
        if (Configuration.DEBUG)
          log.fine("list=" + String.valueOf(list));
        if (list != null)
          {
            other.clear();
            for (Iterator it = list.iterator(); it.hasNext();)
              {
                EntropySource src = (EntropySource) it.next();
                if (Configuration.DEBUG)
                  log.fine("src=" + src);
                if (src == null)
                  throw new NullPointerException("null source in source list");
                other.add(src);
              }
          }
      }
    catch (ClassCastException cce)
      {
        throw new IllegalArgumentException("invalid source list");
      }

    try
      {
        Boolean block = (Boolean) attrib.get(BLOCKING);
        if (block != null)
          blocking = block.booleanValue();
        else
          blocking = true;
      }
    catch (ClassCastException cce)
      {
        throw new IllegalArgumentException("invalid blocking parameter");
      }
    poller = new Poller(files, urls, progs, other, this);
    try
      {
        fillBlock();
      }
    catch (LimitReachedException lre)
      {
        throw new RuntimeException("bootstrapping CSPRNG failed");
      }
  }

  public void fillBlock() throws LimitReachedException
  {
    if (Configuration.DEBUG)
      log.fine("fillBlock");
    if (getQuality() < 100.0)
      {
        if (Configuration.DEBUG)
          log.fine("doing slow poll");
        slowPoll();
      }
    do
      {
        fastPoll();
        mixRandomPool();
      }
    while (mixCount < MIX_COUNT);
    if (! x917init || x917count >= X917_LIFETIME)
      {
        mixRandomPool(pool);
        Map attr = new HashMap();
        byte[] key = new byte[32];
        System.arraycopy(pool, 0, key, 0, 32);
        cipher.reset();
        attr.put(IBlockCipher.KEY_MATERIAL, key);
        try
          {
            cipher.init(attr);
          }
        catch (InvalidKeyException ike)
          {
            throw new Error(ike.toString());
          }
        mixRandomPool(pool);
        generateX917(pool);
        mixRandomPool(pool);
        generateX917(pool);
        if (x917init)
          quality = 0.0;
        x917init = true;
        x917count = 0;
      }
    byte[] export = new byte[ALLOC_SIZE];
    for (int i = 0; i < ALLOC_SIZE; i++)
      export[i] = (byte)(pool[i] ^ 0xFF);
    mixRandomPool();
    mixRandomPool(export);
    generateX917(export);
    for (int i = 0; i < OUTPUT_SIZE; i++)
      buffer[i] = (byte)(export[i] ^ export[i + OUTPUT_SIZE]);
    Arrays.fill(export, (byte) 0);
  }

  /**
   * Add an array of bytes into the randomness pool. Note that this method will
   * <i>not</i> increment the pool's quality counter (this can only be done via
   * a source provided to the setup method).
   *
   * @param buf The byte array.
   * @param off The offset from whence to start reading bytes.
   * @param len The number of bytes to add.
   * @throws ArrayIndexOutOfBoundsException If <i>off</i> or <i>len</i> are
   *           out of the range of <i>buf</i>.
   */
  public synchronized void addRandomBytes(byte[] buf, int off, int len)
  {
    if (off < 0 || len < 0 || off + len > buf.length)
      throw new ArrayIndexOutOfBoundsException();
    if (Configuration.DEBUG)
      {
        log.fine("adding random bytes:");
        log.fine(Util.toString(buf, off, len));
      }
    final int count = off + len;
    for (int i = off; i < count; i++)
      {
        pool[index++] ^= buf[i];
        if (index == pool.length)
          {
            mixRandomPool();
            index = 0;
          }
      }
  }

  /**
   * Add a single random byte to the randomness pool. Note that this method will
   * <i>not</i> increment the pool's quality counter (this can only be done via
   * a source provided to the setup method).
   *
   * @param b The byte to add.
   */
  public synchronized void addRandomByte(byte b)
  {
    if (Configuration.DEBUG)
      log.fine("adding byte " + Integer.toHexString(b));
    pool[index++] ^= b;
    if (index >= pool.length)
      {
        mixRandomPool();
        index = 0;
      }
  }

  synchronized void addQuality(double quality)
  {
    if (Configuration.DEBUG)
      log.fine("adding quality " + quality);
    if (this.quality < 100)
      this.quality += quality;
    if (Configuration.DEBUG)
      log.fine("quality now " + this.quality);
  }

  synchronized double getQuality()
  {
    return quality;
  }

  /**
   * The mix operation. This method will, for every 20-byte block in the random
   * pool, hash that block, the previous 20 bytes, and the next 44 bytes with
   * SHA-1, writing the result back into that block.
   */
  private void mixRandomPool(byte[] buf)
  {
    int hashSize = hash.hashSize();
    for (int i = 0; i < buf.length; i += hashSize)
      {
        // First update the bytes [p-19..p-1].
        if (i == 0)
          hash.update(buf, buf.length - hashSize, hashSize);
        else
          hash.update(buf, i - hashSize, hashSize);
        // Now the next 64 bytes.
        if (i + 64 < buf.length)
          hash.update(buf, i, 64);
        else
          {
            hash.update(buf, i, buf.length - i);
            hash.update(buf, 0, 64 - (buf.length - i));
          }
        byte[] digest = hash.digest();
        System.arraycopy(digest, 0, buf, i, hashSize);
      }
  }

  private void mixRandomPool()
  {
    mixRandomPool(pool);
    mixCount++;
  }

  private void generateX917(byte[] buf)
  {
    int off = 0;
    for (int i = 0; i < buf.length; i += X917_POOL_SIZE)
      {
        int copy = Math.min(buf.length - i, X917_POOL_SIZE);
        for (int j = 0; j < copy; j++)
          x917pool[j] ^= pool[off + j];
        cipher.encryptBlock(x917pool, 0, x917pool, 0);
        System.arraycopy(x917pool, 0, buf, off, copy);
        cipher.encryptBlock(x917pool, 0, x917pool, 0);
        off += copy;
        x917count++;
      }
  }

  /**
   * Add random data always immediately available into the random pool, such as
   * the values of the eight asynchronous counters, the current time, the
   * current memory usage, the calling thread name, and the current stack trace.
   * <p>
   * This method does not alter the quality counter, and is provided more to
   * maintain randomness, not to seriously improve the current random state.
   */
  private void fastPoll()
  {
    byte b = 0;
    for (int i = 0; i < SPINNER_COUNT; i++)
      b ^= SPINNERS[i].counter;
    addRandomByte(b);
    addRandomByte((byte) System.currentTimeMillis());
    addRandomByte((byte) Runtime.getRuntime().freeMemory());
    String s = Thread.currentThread().getName();
    if (s != null)
      {
        byte[] buf = s.getBytes();
        addRandomBytes(buf, 0, buf.length);
      }
    ByteArrayOutputStream bout = new ByteArrayOutputStream(1024);
    PrintStream pout = new PrintStream(bout);
    Throwable t = new Throwable();
    t.printStackTrace(pout);
    pout.flush();
    byte[] buf = bout.toByteArray();
    addRandomBytes(buf, 0, buf.length);
  }

  private void slowPoll() throws LimitReachedException
  {
    if (Configuration.DEBUG)
      log.fine("poller is alive? "
               + (pollerThread == null ? false : pollerThread.isAlive()));
    if (pollerThread == null || ! pollerThread.isAlive())
      {
        boolean interrupted = false;
        pollerThread = new Thread(poller);
        pollerThread.setDaemon(true);
        pollerThread.setPriority(Thread.NORM_PRIORITY - 1);
        pollerThread.start();
        if (blocking)
          try
            {
              pollerThread.join();
            }
          catch (InterruptedException ie)
            {
              interrupted = true;
            }
        // If the full slow poll has completed after we waited for it,
        // and there in insufficient randomness, throw an exception.
        if (! interrupted && blocking && quality < 100.0)
          {
            if (Configuration.DEBUG)
              log.fine("insufficient quality: " + quality);
            throw new LimitReachedException("insufficient randomness was polled");
          }
      }
  }

  protected void finalize() throws Throwable
  {
    if (poller != null && pollerThread != null && pollerThread.isAlive())
      {
        pollerThread.interrupt();
        poller.stopUpdating();
        pollerThread.interrupt();
      }
    Arrays.fill(pool, (byte) 0);
    Arrays.fill(x917pool, (byte) 0);
    Arrays.fill(buffer, (byte) 0);
  }

  /**
   * A simple thread that constantly updates a byte counter. This class is used
   * in a group of lowest-priority threads and the values of their counters
   * (updated in competition with all other threads) is used as a source of
   * entropy bits.
   */
  private static class Spinner
      implements Runnable
  {
    protected byte counter;

    private Spinner()
    {
    }

    public void run()
    {
      while (true)
        {
          counter++;
          try
            {
              Thread.sleep(100);
            }
          catch (InterruptedException ie)
            {
            }
        }
    }
  }

  private final class Poller
      implements Runnable
  {
    private final List files;
    private final List urls;
    private final List progs;
    private final List other;
    private final CSPRNG pool;
    private boolean running;

    Poller(List files, List urls, List progs, List other, CSPRNG pool)
    {
      super();
      this.files = Collections.unmodifiableList(files);
      this.urls = Collections.unmodifiableList(urls);
      this.progs = Collections.unmodifiableList(progs);
      this.other = Collections.unmodifiableList(other);
      this.pool = pool;
    }

    public void run()
    {
      running = true;
      if (Configuration.DEBUG)
        {
          log.fine("files: " + files);
          log.fine("URLs: " + urls);
          log.fine("progs: " + progs);
        }
      Iterator files_it = files.iterator();
      Iterator urls_it = urls.iterator();
      Iterator prog_it = progs.iterator();
      Iterator other_it = other.iterator();

      while (files_it.hasNext() || urls_it.hasNext() || prog_it.hasNext()
             || other_it.hasNext())
        {
          // There is enough random data. Go away.
          if (pool.getQuality() >= 100.0 || ! running)
            return;
          if (files_it.hasNext())
            try
              {
                List l = (List) files_it.next();
                if (Configuration.DEBUG)
                  log.fine(l.toString());
                double qual = ((Double) l.get(0)).doubleValue();
                int offset = ((Integer) l.get(1)).intValue();
                int count = ((Integer) l.get(2)).intValue();
                String src = (String) l.get(3);
                InputStream in = new FileInputStream(src);
                byte[] buf = new byte[count];
                if (offset > 0)
                  in.skip(offset);
                int len = in.read(buf);
                if (len >= 0)
                  {
                    pool.addRandomBytes(buf, 0, len);
                    pool.addQuality(qual * ((double) len / (double) count));
                  }
                if (Configuration.DEBUG)
                  log.fine("got " + len + " bytes from " + src);
              }
            catch (Exception x)
              {
                if (Configuration.DEBUG)
                  log.throwing(this.getClass().getName(), "run", x);
              }
          if (pool.getQuality() >= 100.0 || ! running)
            return;
          if (urls_it.hasNext())
            try
              {
                List l = (List) urls_it.next();
                if (Configuration.DEBUG)
                  log.fine(l.toString());
                double qual = ((Double) l.get(0)).doubleValue();
                int offset = ((Integer) l.get(1)).intValue();
                int count = ((Integer) l.get(2)).intValue();
                URL src = (URL) l.get(3);
                InputStream in = src.openStream();
                byte[] buf = new byte[count];
                if (offset > 0)
                  in.skip(offset);
                int len = in.read(buf);
                if (len >= 0)
                  {
                    pool.addRandomBytes(buf, 0, len);
                    pool.addQuality(qual * ((double) len / (double) count));
                  }
                if (Configuration.DEBUG)
                  log.fine("got " + len + " bytes from " + src);
              }
            catch (Exception x)
              {
                if (Configuration.DEBUG)
                  log.throwing(this.getClass().getName(), "run", x);
              }
          if (pool.getQuality() >= 100.0 || ! running)
            return;
          Process proc = null;
          if (prog_it.hasNext())
            try
              {
                List l = (List) prog_it.next();
                if (Configuration.DEBUG)
                  log.finer(l.toString());
                double qual = ((Double) l.get(0)).doubleValue();
                int offset = ((Integer) l.get(1)).intValue();
                int count = ((Integer) l.get(2)).intValue();
                String src = (String) l.get(3);
                proc = null;
                proc = Runtime.getRuntime().exec(src);
                InputStream in = proc.getInputStream();
                byte[] buf = new byte[count];
                if (offset > 0)
                  in.skip(offset);
                int len = in.read(buf);
                if (len >= 0)
                  {
                    pool.addRandomBytes(buf, 0, len);
                    pool.addQuality(qual * ((double) len / (double) count));
                  }
                proc.destroy();
                proc.waitFor();
                if (Configuration.DEBUG)
                  log.fine("got " + len + " bytes from " + src);
              }
            catch (Exception x)
              {
                if (Configuration.DEBUG)
                  log.throwing(this.getClass().getName(), "run", x);
                try
                  {
                    if (proc != null)
                      {
                        proc.destroy();
                        proc.waitFor();
                      }
                  }
                catch (Exception ignored)
                  {
                  }
              }
          if (pool.getQuality() >= 100.0 || ! running)
            return;
          if (other_it.hasNext())
            try
              {
                EntropySource src = (EntropySource) other_it.next();
                byte[] buf = src.nextBytes();
                if (pool == null)
                  return;
                pool.addRandomBytes(buf, 0, buf.length);
                pool.addQuality(src.quality());
                if (Configuration.DEBUG)
                  log.fine("got " + buf.length + " bytes from " + src);
              }
            catch (Exception x)
              {
                if (Configuration.DEBUG)
                  log.throwing(this.getClass().getName(), "run", x);
              }
        }
    }

    public void stopUpdating()
    {
      running = false;
    }
  }
}