author | aefimov |
Thu, 08 Aug 2019 21:58:11 +0100 | |
changeset 57686 | 70f5cbb711a9 |
parent 53006 | 4debb3321e65 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
53006 | 2 |
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.mscapi; |
|
27 |
||
52801
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
28 |
import java.lang.ref.Cleaner; |
2 | 29 |
import java.security.ProviderException; |
30 |
import java.security.SecureRandomSpi; |
|
31 |
||
32 |
/** |
|
33 |
* Native PRNG implementation for Windows using the Microsoft Crypto API. |
|
34 |
* |
|
35 |
* @since 1.6 |
|
36 |
*/ |
|
37 |
||
38 |
public final class PRNG extends SecureRandomSpi |
|
39 |
implements java.io.Serializable { |
|
40 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
41 |
private static final long serialVersionUID = 4129268715132691532L; |
2 | 42 |
|
43 |
/* |
|
44 |
* The CryptGenRandom function fills a buffer with cryptographically random |
|
45 |
* bytes. |
|
46 |
*/ |
|
52801
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
47 |
private static native byte[] generateSeed(long ctxt, int length, byte[] seed); |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
48 |
private static native long getContext(); |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
49 |
private static native void releaseContext(long ctxt); |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
50 |
private static final Cleaner CLEANER = Cleaner.create(); |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
51 |
|
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
52 |
private transient long ctxt; |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
53 |
|
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
54 |
private static class State implements Runnable { |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
55 |
private final long ctxt; |
2 | 56 |
|
52801
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
57 |
State(long ctxt) { |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
58 |
this.ctxt = ctxt; |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
59 |
} |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
60 |
|
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
61 |
@Override |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
62 |
public void run() { |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
63 |
releaseContext(ctxt); |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
64 |
} |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
65 |
} |
2 | 66 |
/** |
67 |
* Creates a random number generator. |
|
68 |
*/ |
|
69 |
public PRNG() { |
|
52801
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
70 |
ctxt = getContext(); |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
71 |
CLEANER.register(this, new State(ctxt)); |
2 | 72 |
} |
73 |
||
74 |
/** |
|
75 |
* Reseeds this random object. The given seed supplements, rather than |
|
76 |
* replaces, the existing seed. Thus, repeated calls are guaranteed |
|
77 |
* never to reduce randomness. |
|
78 |
* |
|
79 |
* @param seed the seed. |
|
80 |
*/ |
|
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
10336
diff
changeset
|
81 |
@Override |
52801
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
82 |
protected synchronized void engineSetSeed(byte[] seed) { |
2 | 83 |
if (seed != null) { |
52801
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
84 |
generateSeed(ctxt, -1, seed); |
2 | 85 |
} |
86 |
} |
|
87 |
||
88 |
/** |
|
89 |
* Generates a user-specified number of random bytes. |
|
90 |
* |
|
91 |
* @param bytes the array to be filled in with random bytes. |
|
92 |
*/ |
|
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
10336
diff
changeset
|
93 |
@Override |
52801
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
94 |
protected synchronized void engineNextBytes(byte[] bytes) { |
2 | 95 |
if (bytes != null) { |
52801
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
96 |
if (generateSeed(ctxt, 0, bytes) == null) { |
2 | 97 |
throw new ProviderException("Error generating random bytes"); |
98 |
} |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* Returns the given number of seed bytes. This call may be used to |
|
104 |
* seed other random number generators. |
|
105 |
* |
|
106 |
* @param numBytes the number of seed bytes to generate. |
|
107 |
* |
|
108 |
* @return the seed bytes. |
|
109 |
*/ |
|
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
10336
diff
changeset
|
110 |
@Override |
52801
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
111 |
protected synchronized byte[] engineGenerateSeed(int numBytes) { |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
112 |
byte[] seed = generateSeed(ctxt, numBytes, null); |
2 | 113 |
|
114 |
if (seed == null) { |
|
115 |
throw new ProviderException("Error generating seed bytes"); |
|
116 |
} |
|
117 |
return seed; |
|
118 |
} |
|
52801
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
119 |
|
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
120 |
private void readObject(java.io.ObjectInputStream s) |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
121 |
throws java.io.IOException, ClassNotFoundException |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
122 |
{ |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
123 |
s.defaultReadObject(); |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
124 |
ctxt = getContext(); |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
125 |
CLEANER.register(this, new State(ctxt)); |
0baf34792a27
8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents:
47216
diff
changeset
|
126 |
} |
2 | 127 |
} |