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
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
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
;
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
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
;
64 // -------------------------------------------------------------------------
68 md5
= HashFactory
.getInstance("MD5");
69 sha
= HashFactory
.getInstance("SHA-1");
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();
84 try { buffer
= nextBlock(); }
85 catch (LimitReachedException cantHappen
) { }
95 throw new UnsupportedOperationException();
98 public byte nextByte() throws LimitReachedException
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
111 throw new IllegalStateException();
113 throw new NullPointerException();
114 if (off
< 0 || len
< 0 || off
+len
> buf
.length
)
115 throw new IndexOutOfBoundsException();
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
);
128 public boolean selfTest()
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
)
147 // -------------------------------------------------------------------------
149 private byte[] nextBlock() throws LimitReachedException
151 int count
= pad
- 'A' + 1;
153 throw new LimitReachedException();
154 for (int i
= 0; i
< count
; i
++)
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
);