src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/PRNG.java
author aefimov
Thu, 08 Aug 2019 21:58:11 +0100
changeset 57686 70f5cbb711a9
parent 53006 4debb3321e65
permissions -rw-r--r--
8225430: Replace wildcard address with loopback or local host in tests - part 14 Reviewed-by: dfuchs, chegar, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
     2
 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.mscapi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.ProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.SecureRandomSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Native PRNG implementation for Windows using the Microsoft Crypto API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public final class PRNG extends SecureRandomSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * The CryptGenRandom function fills a buffer with cryptographically random
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Creates a random number generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Reseeds this random object. The given seed supplements, rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * replaces, the existing seed. Thus, repeated calls are guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * never to reduce randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param seed the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Generates a user-specified number of random bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param bytes the array to be filled in with random bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                throw new ProviderException("Error generating random bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Returns the given number of seed bytes.  This call may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * seed other random number generators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param numBytes the number of seed bytes to generate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @return the seed bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (seed == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new ProviderException("Error generating seed bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return seed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
}