jdk/src/share/classes/sun/security/pkcs11/P11SecureRandom.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5156 bb63e4d4b1de
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.pkcs11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * SecureRandom implementation class. Some tokens support only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * C_GenerateRandom() and not C_SeedRandom(). In order not to lose an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * application specified seed, we create a SHA1PRNG that we mix with in that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Note that since SecureRandom is thread safe, we only need one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * instance per PKCS#11 token instance. It is created on demand and cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * in the SunPKCS11 class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Also note that we obtain the PKCS#11 session on demand, no need to tie one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
final class P11SecureRandom extends SecureRandomSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final long serialVersionUID = -8939510236124553291L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // token instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // PRNG for mixing, non-null if active (i.e. setSeed() has been called)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private volatile SecureRandom mixRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // buffer, if mixing is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private byte[] mixBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // bytes remaining in buffer, if mixing is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private int buffered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    P11SecureRandom(Token token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    protected synchronized void engineSetSeed(byte[] seed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (seed == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new NullPointerException("seed must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            token.p11.C_SeedRandom(session.id(), seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            // cannot set seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            // let a SHA1PRNG use that seed instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            SecureRandom random = mixRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (random != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                random.setSeed(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    mixBuffer = new byte[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    random = SecureRandom.getInstance("SHA1PRNG");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    // initialize object before assigning to class field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    random.setSeed(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    mixRandom = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                } catch (NoSuchAlgorithmException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    throw new ProviderException(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            token.releaseSession(session);
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
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    protected void engineNextBytes(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if ((bytes == null) || (bytes.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            token.p11.C_GenerateRandom(session.id(), bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            mix(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new ProviderException("nextBytes() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    protected byte[] engineGenerateSeed(int numBytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        byte[] b = new byte[numBytes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        engineNextBytes(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private void mix(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        SecureRandom random = mixRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (random == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            // avoid mixing if setSeed() has never been called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            int ofs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            int len = b.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            while (len-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                if (buffered == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    random.nextBytes(mixBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    buffered = mixBuffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                b[ofs++] ^= mixBuffer[mixBuffer.length - buffered];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                buffered--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}