Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / net / ssl / provider / SSLRandom.java
blob0b28f1044a7b52e90d02ae2d866c6749ef6ba468
1 /* SSLRandom.java -- SSLv3 pseudo-random function.
2 Copyright (C) 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.net.ssl.provider;
41 import java.util.Map;
42 import gnu.java.security.hash.HashFactory;
43 import gnu.java.security.hash.IMessageDigest;
44 import gnu.java.security.prng.IRandom;
45 import gnu.java.security.prng.LimitReachedException;
47 class SSLRandom implements IRandom
50 // Fields.
51 // -------------------------------------------------------------------------
53 static final String SECRET = "jessie.sslprng.secret";
54 static final String SEED = "jessie.sslprng.seed";
56 private final IMessageDigest md5, sha;
57 private byte[] secret;
58 private byte[] buffer;
59 private byte pad;
60 private byte[] seed;
61 private int idx;
63 // Constructor.
64 // -------------------------------------------------------------------------
66 SSLRandom()
68 md5 = HashFactory.getInstance("MD5");
69 sha = HashFactory.getInstance("SHA-1");
72 // Instance methods.
73 // -------------------------------------------------------------------------
75 public void init(Map attrib)
77 secret = (byte[]) attrib.get(SECRET);
78 seed = (byte[]) attrib.get(SEED);
80 if (secret == null || seed == null)
81 throw new NullPointerException();
83 pad = (byte) 'A';
84 try { buffer = nextBlock(); }
85 catch (LimitReachedException cantHappen) { }
88 public String name()
90 return "SSLRandom";
93 public Object clone()
95 throw new UnsupportedOperationException();
98 public byte nextByte() throws LimitReachedException
100 if (buffer == null)
101 throw new IllegalStateException();
102 if (idx >= buffer.length)
103 buffer = nextBlock();
104 return buffer[idx++];
107 public void nextBytes(byte[] buf, int off, int len)
108 throws LimitReachedException
110 if (buffer == null)
111 throw new IllegalStateException();
112 if (buf == null)
113 throw new NullPointerException();
114 if (off < 0 || len < 0 || off+len > buf.length)
115 throw new IndexOutOfBoundsException();
116 int count = 0;
117 while (count < len)
119 if (idx >= buffer.length)
120 buffer = nextBlock();
121 int l = Math.min(buffer.length-idx, len-count);
122 System.arraycopy(buffer, idx, buf, off+count, l);
123 count += l;
124 idx += l;
128 public boolean selfTest()
130 return true; // XXX
133 // For future versions of GNU Crypto. No-ops.
134 public void addRandomByte (byte b)
138 public void addRandomBytes(byte[] buffer) {
139 addRandomBytes(buffer, 0, buffer.length);
142 public void addRandomBytes (byte[] b, int i, int j)
146 // Own methods.
147 // -------------------------------------------------------------------------
149 private byte[] nextBlock() throws LimitReachedException
151 int count = pad - 'A' + 1;
152 if (count > 26)
153 throw new LimitReachedException();
154 for (int i = 0; i < count; i++)
155 sha.update(pad);
156 sha.update(secret, 0, secret.length);
157 sha.update(seed, 0, seed.length);
158 byte[] b = sha.digest();
159 md5.update(secret, 0, secret.length);
160 md5.update(b, 0, b.length);
161 idx = 0;
162 pad++;
163 return md5.digest();