Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / jce / prng / UMacRandomSpi.java
blob2faebe8eb04c2077ba8777bbf6975b8560b80f70
1 /* UMacRandomSpi.java --
2 Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 USA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.crypto.jce.prng;
41 import gnu.java.security.Configuration;
42 import gnu.java.security.Registry;
43 import gnu.java.security.prng.LimitReachedException;
44 import gnu.java.security.jce.prng.SecureRandomAdapter;
45 import gnu.javax.crypto.cipher.IBlockCipher;
46 import gnu.javax.crypto.prng.UMacGenerator;
48 import java.security.SecureRandomSpi;
49 import java.util.HashMap;
50 import java.util.Random;
51 import java.util.logging.Logger;
53 /**
54 * An <em>Adapter</em> class around {@link UMacGenerator} to allow using this
55 * algorithm as a JCE {@link java.security.SecureRandom}.
57 public class UMacRandomSpi
58 extends SecureRandomSpi
60 private static final Logger log = Logger.getLogger(UMacRandomSpi.class.getName());
62 /** Class-wide prng to generate random material for the underlying prng. */
63 private static final UMacGenerator prng; // blank final
64 static
66 prng = new UMacGenerator();
67 resetLocalPRNG();
69 // error messages
70 private static final String MSG = "Exception while setting up a "
71 + Registry.UMAC_PRNG + " SPI: ";
72 private static final String RETRY = "Retry...";
73 /** Our underlying prng instance. */
74 private UMacGenerator adaptee = new UMacGenerator();
76 // default 0-arguments constructor
78 private static void resetLocalPRNG()
80 HashMap attributes = new HashMap();
81 attributes.put(UMacGenerator.CIPHER, Registry.AES_CIPHER);
82 byte[] key = new byte[128 / 8]; // AES default key size
83 Random rand = new Random(System.currentTimeMillis());
84 rand.nextBytes(key);
85 attributes.put(IBlockCipher.KEY_MATERIAL, key);
86 int index = rand.nextInt() & 0xFF;
87 attributes.put(UMacGenerator.INDEX, Integer.valueOf(index));
88 prng.setup(attributes);
91 public byte[] engineGenerateSeed(int numBytes)
93 return SecureRandomAdapter.getSeed(numBytes);
96 public void engineNextBytes(byte[] bytes)
98 if (! adaptee.isInitialised())
99 engineSetSeed(engineGenerateSeed(32));
100 while (true)
104 adaptee.nextBytes(bytes, 0, bytes.length);
105 break;
107 catch (LimitReachedException x)
108 { // reseed the generator
109 resetLocalPRNG();
114 public void engineSetSeed(byte[] seed)
116 // compute the total number of random bytes required to setup adaptee
117 int materialLength = 0;
118 materialLength += 16; // key material size
119 materialLength++; // index size
120 byte[] material = new byte[materialLength];
121 // use as much as possible bytes from the seed
122 int materialOffset = 0;
123 int materialLeft = material.length;
124 if (seed.length > 0)
125 { // copy some bytes into key and update indices
126 int lenToCopy = Math.min(materialLength, seed.length);
127 System.arraycopy(seed, 0, material, 0, lenToCopy);
128 materialOffset += lenToCopy;
129 materialLeft -= lenToCopy;
131 if (materialOffset > 0) // generate the rest
133 while (true)
137 prng.nextBytes(material, materialOffset, materialLeft);
138 break;
140 catch (IllegalStateException x) // should not happen
142 throw new InternalError(MSG + String.valueOf(x));
144 catch (LimitReachedException x)
146 if (Configuration.DEBUG)
148 log.fine(MSG + String.valueOf(x));
149 log.fine(RETRY);
154 // setup the underlying adaptee instance
155 HashMap attributes = new HashMap();
156 // use AES cipher with 128-bit block size
157 attributes.put(UMacGenerator.CIPHER, Registry.AES_CIPHER);
158 // specify the key
159 byte[] key = new byte[16];
160 System.arraycopy(material, 0, key, 0, 16);
161 attributes.put(IBlockCipher.KEY_MATERIAL, key);
162 // use a 1-byte index
163 attributes.put(UMacGenerator.INDEX, Integer.valueOf(material[16] & 0xFF));
164 adaptee.init(attributes);