Path: blob/master/src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/PRNG.java
41159 views
/*1* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.mscapi;2627import java.lang.ref.Cleaner;28import java.security.ProviderException;29import java.security.SecureRandomSpi;3031/**32* Native PRNG implementation for Windows using the Microsoft Crypto API.33*34* @since 1.635*/3637public final class PRNG extends SecureRandomSpi38implements java.io.Serializable {3940private static final long serialVersionUID = 4129268715132691532L;4142/*43* The CryptGenRandom function fills a buffer with cryptographically random44* bytes.45*/46private static native byte[] generateSeed(long ctxt, int length, byte[] seed);47private static native long getContext();48private static native void releaseContext(long ctxt);49private static final Cleaner CLEANER = Cleaner.create();5051private transient long ctxt;5253private static class State implements Runnable {54private final long ctxt;5556State(long ctxt) {57this.ctxt = ctxt;58}5960@Override61public void run() {62releaseContext(ctxt);63}64}65/**66* Creates a random number generator.67*/68public PRNG() {69ctxt = getContext();70CLEANER.register(this, new State(ctxt));71}7273/**74* Reseeds this random object. The given seed supplements, rather than75* replaces, the existing seed. Thus, repeated calls are guaranteed76* never to reduce randomness.77*78* @param seed the seed.79*/80@Override81protected synchronized void engineSetSeed(byte[] seed) {82if (seed != null) {83generateSeed(ctxt, -1, seed);84}85}8687/**88* Generates a user-specified number of random bytes.89*90* @param bytes the array to be filled in with random bytes.91*/92@Override93protected synchronized void engineNextBytes(byte[] bytes) {94if (bytes != null) {95if (generateSeed(ctxt, 0, bytes) == null) {96throw new ProviderException("Error generating random bytes");97}98}99}100101/**102* Returns the given number of seed bytes. This call may be used to103* seed other random number generators.104*105* @param numBytes the number of seed bytes to generate.106*107* @return the seed bytes.108*/109@Override110protected synchronized byte[] engineGenerateSeed(int numBytes) {111byte[] seed = generateSeed(ctxt, numBytes, null);112113if (seed == null) {114throw new ProviderException("Error generating seed bytes");115}116return seed;117}118119private void readObject(java.io.ObjectInputStream s)120throws java.io.IOException, ClassNotFoundException121{122s.defaultReadObject();123ctxt = getContext();124CLEANER.register(this, new State(ctxt));125}126}127128129