jdk/src/share/classes/com/sun/crypto/provider/RSACipher.java
author valeriep
Tue, 08 May 2012 17:57:48 -0700
changeset 12685 8a448b5b9006
parent 10336 0bb1999251f8
child 16909 78a1749a43e2
permissions -rw-r--r--
4963723: Implement SHA-224 Summary: Add support for SHA-224, SHA224withRSA, SHA224withECDSA, HmacSHA224 and OAEPwithSHA-224AndMGF1Padding. Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
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: 3353
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: 3353
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: 3353
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
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 com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Locale;
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
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.spec.InvalidParameterSpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.spec.MGF1ParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.crypto.spec.PSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.crypto.spec.OAEPParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.rsa.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.jca.Providers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * RSA cipher implementation. Supports RSA en/decryption and signing/verifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * using PKCS#1 v1.5 padding and without padding (raw RSA). Note that raw RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * is supported mostly for completeness and should only be used in rare cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Objects should be instantiated by calling Cipher.getInstance() using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * following algorithm names:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *  . "RSA/ECB/PKCS1Padding" (or "RSA") for PKCS#1 padding. The mode (blocktype)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *    is selected based on the en/decryption mode and public/private key used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *  . "RSA/ECB/NoPadding" for rsa RSA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * We only do one RSA operation per doFinal() call. If the application passes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * more data via calls to update() or doFinal(), we throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * IllegalBlockSizeException when doFinal() is called (see JCE API spec).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Bulk encryption using RSA does not make sense and is not standardized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Note: RSA keys should be at least 512 bits long
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public final class RSACipher extends CipherSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // constant for an empty byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final static byte[] B0 = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // mode constant for public key encryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private final static int MODE_ENCRYPT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // mode constant for private key decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private final static int MODE_DECRYPT = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // mode constant for private key encryption (signing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private final static int MODE_SIGN    = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // mode constant for public key decryption (verifying)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private final static int MODE_VERIFY  = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // constant for raw RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private final static String PAD_NONE  = "NoPadding";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // constant for PKCS#1 v1.5 RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private final static String PAD_PKCS1 = "PKCS1Padding";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // constant for PKCS#2 v2.0 OAEP with MGF1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private final static String PAD_OAEP_MGF1  = "OAEP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // current mode, one of MODE_* above. Set when init() is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private int mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // active padding type, one of PAD_* above. Set by setPadding()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private String paddingType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    // padding object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private RSAPadding padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // cipher parameter for OAEP padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private OAEPParameterSpec spec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // buffer for the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private byte[] buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // offset into the buffer (number of bytes buffered)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private int bufOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    // size of the output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private int outputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // the public key, if we were initialized using a public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private RSAPublicKey publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // the private key, if we were initialized using a private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private RSAPrivateKey privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // hash algorithm for OAEP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private String oaepHashAlgorithm = "SHA-1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public RSACipher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        paddingType = PAD_PKCS1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // modes do not make sense for RSA, but allow ECB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (mode.equalsIgnoreCase("ECB") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new NoSuchAlgorithmException("Unsupported mode " + mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    // set the padding type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    protected void engineSetPadding(String paddingName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throws NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (paddingName.equalsIgnoreCase(PAD_NONE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            paddingType = PAD_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } else if (paddingName.equalsIgnoreCase(PAD_PKCS1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            paddingType = PAD_PKCS1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            String lowerPadding = paddingName.toLowerCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (lowerPadding.equals("oaeppadding")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                paddingType = PAD_OAEP_MGF1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } else if (lowerPadding.startsWith("oaepwith") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                       lowerPadding.endsWith("andmgf1padding")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                paddingType = PAD_OAEP_MGF1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                // "oaepwith".length() == 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                // "andmgf1padding".length() == 14
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                oaepHashAlgorithm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        paddingName.substring(8, paddingName.length() - 14);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                // check if MessageDigest appears to be available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                // avoid getInstance() call here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if (Providers.getProviderList().getService
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        ("MessageDigest", oaepHashAlgorithm) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    throw new NoSuchPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        ("MessageDigest not available for " + paddingName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                throw new NoSuchPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    ("Padding " + paddingName + " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // return 0 as block size, we are not a block cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    protected int engineGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    // return the output size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    protected int engineGetOutputSize(int inputLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return outputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    // no iv, return null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    protected byte[] engineGetIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    protected AlgorithmParameters engineGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (spec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                AlgorithmParameters params =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    AlgorithmParameters.getInstance("OAEP", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                params.init(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                throw new RuntimeException("Cannot find OAEP " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    " AlgorithmParameters implementation in SunJCE provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            } catch (NoSuchProviderException nspe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                throw new RuntimeException("Cannot find SunJCE provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            } catch (InvalidParameterSpecException ipse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                throw new RuntimeException("OAEPParameterSpec not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    protected void engineInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            init(opmode, key, random, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // never thrown when null parameters are used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            // but re-throw it just in case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            InvalidKeyException ike =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                new InvalidKeyException("Wrong parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            ike.initCause(iape);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            throw ike;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        init(opmode, key, random, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            AlgorithmParameters params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            init(opmode, key, random, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   230
                OAEPParameterSpec spec =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   231
                        params.getParameterSpec(OAEPParameterSpec.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                init(opmode, key, random, spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            } catch (InvalidParameterSpecException ipse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                InvalidAlgorithmParameterException iape =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    new InvalidAlgorithmParameterException("Wrong parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                iape.initCause(ipse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                throw iape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    // initialize this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private void init(int opmode, Key key, SecureRandom random,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        boolean encrypt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        switch (opmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        case Cipher.ENCRYPT_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        case Cipher.WRAP_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            encrypt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        case Cipher.DECRYPT_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        case Cipher.UNWRAP_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            encrypt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            throw new InvalidKeyException("Unknown mode: " + opmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        RSAKey rsaKey = RSAKeyFactory.toRSAKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (key instanceof RSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            mode = encrypt ? MODE_ENCRYPT : MODE_VERIFY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            publicKey = (RSAPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            privateKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        } else { // must be RSAPrivateKey per check in toRSAKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            mode = encrypt ? MODE_SIGN : MODE_DECRYPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            privateKey = (RSAPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            publicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        int n = RSACore.getByteLength(rsaKey.getModulus());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        outputSize = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        bufOfs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (paddingType == PAD_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                ("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            padding = RSAPadding.getInstance(RSAPadding.PAD_NONE, n, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            buffer = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } else if (paddingType == PAD_PKCS1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                ("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            int blockType = (mode <= MODE_DECRYPT) ? RSAPadding.PAD_BLOCKTYPE_2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                                   : RSAPadding.PAD_BLOCKTYPE_1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            padding = RSAPadding.getInstance(blockType, n, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (encrypt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                int k = padding.getMaxDataSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                buffer = new byte[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                buffer = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        } else { // PAD_OAEP_MGF1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if ((mode == MODE_SIGN) || (mode == MODE_VERIFY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        ("OAEP cannot be used to sign or verify signatures");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            OAEPParameterSpec myParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                if (!(params instanceof OAEPParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        ("Wrong Parameters for OAEP Padding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                myParams = (OAEPParameterSpec) params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                myParams = new OAEPParameterSpec(oaepHashAlgorithm, "MGF1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            padding = RSAPadding.getInstance(RSAPadding.PAD_OAEP_MGF1, n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                random, myParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if (encrypt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                int k = padding.getMaxDataSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                buffer = new byte[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                buffer = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    // internal update method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    private void update(byte[] in, int inOfs, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if ((inLen == 0) || (in == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (bufOfs + inLen > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            bufOfs = buffer.length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        System.arraycopy(in, inOfs, buffer, bufOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        bufOfs += inLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    // internal doFinal() method. Here we perform the actual RSA operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    private byte[] doFinal() throws BadPaddingException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (bufOfs > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            throw new IllegalBlockSizeException("Data must not be longer "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                + "than " + buffer.length + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            byte[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            case MODE_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                data = padding.pad(buffer, 0, bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                return RSACore.rsa(data, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            case MODE_VERIFY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                byte[] verifyBuffer = RSACore.convert(buffer, 0, bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                data = RSACore.rsa(verifyBuffer, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                return padding.unpad(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            case MODE_ENCRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                data = padding.pad(buffer, 0, bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                return RSACore.rsa(data, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            case MODE_DECRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                byte[] decryptBuffer = RSACore.convert(buffer, 0, bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                data = RSACore.rsa(decryptBuffer, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                return padding.unpad(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                throw new AssertionError("Internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            bufOfs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        update(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return B0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    protected int engineUpdate(byte[] in, int inOfs, int inLen, byte[] out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            int outOfs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        update(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    protected byte[] engineDoFinal(byte[] in, int inOfs, int inLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            throws BadPaddingException, IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        update(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    protected int engineDoFinal(byte[] in, int inOfs, int inLen, byte[] out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            int outOfs) throws ShortBufferException, BadPaddingException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (outputSize > out.length - outOfs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            throw new ShortBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                ("Need " + outputSize + " bytes for output");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        update(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        byte[] result = doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        int n = result.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        System.arraycopy(result, 0, out, outOfs, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    protected byte[] engineWrap(Key key) throws InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        if ((encoded == null) || (encoded.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            throw new InvalidKeyException("Could not obtain encoded key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (encoded.length > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            throw new InvalidKeyException("Key is too long for wrapping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        update(encoded, 0, encoded.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            return doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        } catch (BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            // should not occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            throw new InvalidKeyException("Wrapping failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    protected Key engineUnwrap(byte[] wrappedKey, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            int type) throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (wrappedKey.length > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            throw new InvalidKeyException("Key is too long for unwrapping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        update(wrappedKey, 0, wrappedKey.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            byte[] encoded = doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            return ConstructKeys.constructKey(encoded, algorithm, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        } catch (BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            // should not occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            throw new InvalidKeyException("Unwrapping failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        } catch (IllegalBlockSizeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            // should not occur, handled with length check above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            throw new InvalidKeyException("Unwrapping failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    protected int engineGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        RSAKey rsaKey = RSAKeyFactory.toRSAKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        return rsaKey.getModulus().bitLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
}