jdk/src/share/classes/sun/security/rsa/RSACore.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7973 dffe8439eb20
child 25541 df83fb1a542e
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7973
diff changeset
     2
 * Copyright (c) 2003, 2011, 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.rsa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.SecureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.BadPaddingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.jca.JCAUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Core of the RSA implementation. Has code to perform public and private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * RSA operations (with and without CRT for private key ops). Private CRT ops
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * also support blinding to twart timing attacks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The code in this class only does the core RSA operation. Padding and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * unpadding must be done externally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Note: RSA keys should be at least 512 bits long
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public final class RSACore {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private RSACore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Return the number of bytes required to store the magnitude byte[] of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * this BigInteger. Do not count a 0x00 byte toByteArray() would
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * prefix for 2's complement form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static int getByteLength(BigInteger b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        int n = b.bitLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return (n + 7) >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Return the number of bytes required to store the modulus of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * RSA key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public static int getByteLength(RSAKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return getByteLength(key.getModulus());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // temporary, used by RSACipher and RSAPadding. Move this somewhere else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static byte[] convert(byte[] b, int ofs, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if ((ofs == 0) && (len == b.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            byte[] t = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            System.arraycopy(b, ofs, t, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Perform an RSA public key operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static byte[] rsa(byte[] msg, RSAPublicKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return crypt(msg, key.getModulus(), key.getPublicExponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Perform an RSA private key operation. Uses CRT if the key is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * CRT key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public static byte[] rsa(byte[] msg, RSAPrivateKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (key instanceof RSAPrivateCrtKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return crtCrypt(msg, (RSAPrivateCrtKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return crypt(msg, key.getModulus(), key.getPrivateExponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * RSA public key ops and non-CRT private key ops. Simple modPow().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static byte[] crypt(byte[] msg, BigInteger n, BigInteger exp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        BigInteger m = parseMsg(msg, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        BigInteger c = m.modPow(exp, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return toByteArray(c, getByteLength(n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * RSA private key operations with CRT. Algorithm and variable naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * are taken from PKCS#1 v2.1, section 5.1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * The only difference is the addition of blinding to twart timing attacks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * This is described in the RSA Bulletin#2 (Jan 96) among other places.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * This means instead of implementing RSA as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *   m = c ^ d mod n (or RSA in CRT variant)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * we do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *   r  = random(0, n-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *   c' = c  * r^e  mod n
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *   m' = c' ^ d    mod n (or RSA in CRT variant)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *   m  = m' * r^-1 mod n (where r^-1 is the modular inverse of r mod n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * This works because r^(e*d) * r^-1 = r * r^-1 = 1 (all mod n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * We do not generate new blinding parameters for each operation but reuse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * them BLINDING_MAX_REUSE times (see definition below).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static byte[] crtCrypt(byte[] msg, RSAPrivateCrtKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        BigInteger n = key.getModulus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        BigInteger c = parseMsg(msg, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        BigInteger p = key.getPrimeP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        BigInteger q = key.getPrimeQ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        BigInteger dP = key.getPrimeExponentP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        BigInteger dQ = key.getPrimeExponentQ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        BigInteger qInv = key.getCrtCoefficient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        BlindingParameters params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (ENABLE_BLINDING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            params = getBlindingParameters(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            c = c.multiply(params.re).mod(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            params = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // m1 = c ^ dP mod p
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        BigInteger m1 = c.modPow(dP, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // m2 = c ^ dQ mod q
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        BigInteger m2 = c.modPow(dQ, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // h = (m1 - m2) * qInv mod p
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        BigInteger mtmp = m1.subtract(m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (mtmp.signum() < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            mtmp = mtmp.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        BigInteger h = mtmp.multiply(qInv).mod(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // m = m2 + q * h
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        BigInteger m = h.multiply(q).add(m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            m = m.multiply(params.rInv).mod(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return toByteArray(m, getByteLength(n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Parse the msg into a BigInteger and check against the modulus n.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private static BigInteger parseMsg(byte[] msg, BigInteger n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        BigInteger m = new BigInteger(1, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (m.compareTo(n) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new BadPaddingException("Message is larger than modulus");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Return the encoding of this BigInteger that is exactly len bytes long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Prefix/strip off leading 0x00 bytes if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Precondition: bi must fit into len bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private static byte[] toByteArray(BigInteger bi, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        byte[] b = bi.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        int n = b.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (n == len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // BigInteger prefixed a 0x00 byte for 2's complement form, remove it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if ((n == len + 1) && (b[0] == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            byte[] t = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            System.arraycopy(b, 1, t, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        // must be smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        assert (n < len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        byte[] t = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        System.arraycopy(b, 0, t, (len - n), n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    // globally enable/disable use of blinding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private final static boolean ENABLE_BLINDING = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    // maximum number of times that we will use a set of blinding parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    // value suggested by Paul Kocher (quoted by NSS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private final static int BLINDING_MAX_REUSE = 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
7973
dffe8439eb20 7005608: diamond conversion of JCA and crypto providers
smarks
parents: 5506
diff changeset
   218
    // cache for blinding parameters. Map<BigInteger, BlindingParameters>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    // use a weak hashmap so that cached values are automatically cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    // when the modulus is GC'ed
7973
dffe8439eb20 7005608: diamond conversion of JCA and crypto providers
smarks
parents: 5506
diff changeset
   221
    private final static Map<BigInteger, BlindingParameters> blindingCache =
dffe8439eb20 7005608: diamond conversion of JCA and crypto providers
smarks
parents: 5506
diff changeset
   222
                new WeakHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Set of blinding parameters for a given RSA key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * The RSA modulus is usually unique, so we index by modulus in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * blindingCache. However, to protect against the unlikely case of two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * keys sharing the same modulus, we also store the public exponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * This means we cannot cache blinding parameters for multiple keys that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * share the same modulus, but since sharing moduli is fundamentally broken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * an insecure, this does not matter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private static final class BlindingParameters {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // e (RSA public exponent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        final BigInteger e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        // r ^ e mod n
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        final BigInteger re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // inverse of r mod n
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        final BigInteger rInv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // how many more times this parameter object can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        private volatile int remainingUses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        BlindingParameters(BigInteger e, BigInteger re, BigInteger rInv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            this.e = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            this.re = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            this.rInv = rInv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            // initialize remaining uses, subtract current use now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            remainingUses = BLINDING_MAX_REUSE - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        boolean valid(BigInteger e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            int k = remainingUses--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return (k > 0) && this.e.equals(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Return valid RSA blinding parameters for the given private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Use cached parameters if available. If not, generate new parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * and cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private static BlindingParameters getBlindingParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            (RSAPrivateCrtKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        BigInteger modulus = key.getModulus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        BigInteger e = key.getPublicExponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        BlindingParameters params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        // we release the lock between get() and put()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // that means threads might concurrently generate new blinding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // parameters for the same modulus. this is only a slight waste
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // of cycles and seems preferable in terms of scalability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // to locking out all threads while generating new parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        synchronized (blindingCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            params = blindingCache.get(modulus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if ((params != null) && params.valid(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        int len = modulus.bitLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        SecureRandom random = JCAUtil.getSecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        BigInteger r = new BigInteger(len, random).mod(modulus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        BigInteger re = r.modPow(e, modulus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        BigInteger rInv = r.modInverse(modulus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        params = new BlindingParameters(e, re, rInv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        synchronized (blindingCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            blindingCache.put(modulus, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
}