jdk/src/share/classes/com/sun/crypto/provider/RSACipher.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3353 ddbd63234844
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-2007 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 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
        SunJCE.ensureIntegrity(getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        paddingType = PAD_PKCS1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // modes do not make sense for RSA, but allow ECB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (mode.equalsIgnoreCase("ECB") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new NoSuchAlgorithmException("Unsupported mode " + mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    // set the padding type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    protected void engineSetPadding(String paddingName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throws NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (paddingName.equalsIgnoreCase(PAD_NONE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            paddingType = PAD_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } else if (paddingName.equalsIgnoreCase(PAD_PKCS1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            paddingType = PAD_PKCS1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            String lowerPadding = paddingName.toLowerCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (lowerPadding.equals("oaeppadding")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                paddingType = PAD_OAEP_MGF1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            } else if (lowerPadding.startsWith("oaepwith") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                       lowerPadding.endsWith("andmgf1padding")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                paddingType = PAD_OAEP_MGF1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                // "oaepwith".length() == 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                // "andmgf1padding".length() == 14
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                oaepHashAlgorithm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        paddingName.substring(8, paddingName.length() - 14);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                // check if MessageDigest appears to be available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                // avoid getInstance() call here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                if (Providers.getProviderList().getService
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        ("MessageDigest", oaepHashAlgorithm) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    throw new NoSuchPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        ("MessageDigest not available for " + paddingName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                throw new NoSuchPaddingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    ("Padding " + paddingName + " not supported");
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // return 0 as block size, we are not a block cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    protected int engineGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // return the output size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    protected int engineGetOutputSize(int inputLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return outputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // no iv, return null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    protected byte[] engineGetIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    protected AlgorithmParameters engineGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (spec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                AlgorithmParameters params =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    AlgorithmParameters.getInstance("OAEP", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                params.init(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                throw new RuntimeException("Cannot find OAEP " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    " AlgorithmParameters implementation in SunJCE provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            } catch (NoSuchProviderException nspe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                throw new RuntimeException("Cannot find SunJCE provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            } catch (InvalidParameterSpecException ipse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                throw new RuntimeException("OAEPParameterSpec not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    protected void engineInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            init(opmode, key, random, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            // never thrown when null parameters are used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            // but re-throw it just in case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            InvalidKeyException ike =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                new InvalidKeyException("Wrong parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            ike.initCause(iape);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throw ike;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        init(opmode, key, random, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            AlgorithmParameters params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            init(opmode, key, random, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                OAEPParameterSpec spec = (OAEPParameterSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    params.getParameterSpec(OAEPParameterSpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                init(opmode, key, random, spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            } catch (InvalidParameterSpecException ipse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                InvalidAlgorithmParameterException iape =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    new InvalidAlgorithmParameterException("Wrong parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                iape.initCause(ipse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                throw iape;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    // initialize this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private void init(int opmode, Key key, SecureRandom random,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        boolean encrypt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        switch (opmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        case Cipher.ENCRYPT_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        case Cipher.WRAP_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            encrypt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        case Cipher.DECRYPT_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        case Cipher.UNWRAP_MODE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            encrypt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            throw new InvalidKeyException("Unknown mode: " + opmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        RSAKey rsaKey = RSAKeyFactory.toRSAKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (key instanceof RSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            mode = encrypt ? MODE_ENCRYPT : MODE_VERIFY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            publicKey = (RSAPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            privateKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        } else { // must be RSAPrivateKey per check in toRSAKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            mode = encrypt ? MODE_SIGN : MODE_DECRYPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            privateKey = (RSAPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            publicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        int n = RSACore.getByteLength(rsaKey.getModulus());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        outputSize = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        bufOfs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (paddingType == PAD_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                ("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            padding = RSAPadding.getInstance(RSAPadding.PAD_NONE, n, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            buffer = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } else if (paddingType == PAD_PKCS1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                ("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            int blockType = (mode <= MODE_DECRYPT) ? RSAPadding.PAD_BLOCKTYPE_2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                                   : RSAPadding.PAD_BLOCKTYPE_1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            padding = RSAPadding.getInstance(blockType, n, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (encrypt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                int k = padding.getMaxDataSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                buffer = new byte[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                buffer = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        } else { // PAD_OAEP_MGF1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if ((mode == MODE_SIGN) || (mode == MODE_VERIFY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        ("OAEP cannot be used to sign or verify signatures");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            OAEPParameterSpec myParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                if (!(params instanceof OAEPParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                        ("Wrong Parameters for OAEP Padding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                myParams = (OAEPParameterSpec) params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                myParams = new OAEPParameterSpec(oaepHashAlgorithm, "MGF1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            padding = RSAPadding.getInstance(RSAPadding.PAD_OAEP_MGF1, n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                random, myParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (encrypt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                int k = padding.getMaxDataSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                buffer = new byte[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                buffer = new byte[n];
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    // internal update method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    private void update(byte[] in, int inOfs, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if ((inLen == 0) || (in == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (bufOfs + inLen > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            bufOfs = buffer.length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        System.arraycopy(in, inOfs, buffer, bufOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        bufOfs += inLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    // internal doFinal() method. Here we perform the actual RSA operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private byte[] doFinal() throws BadPaddingException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (bufOfs > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            throw new IllegalBlockSizeException("Data must not be longer "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                + "than " + buffer.length + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            byte[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            case MODE_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                data = padding.pad(buffer, 0, bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                return RSACore.rsa(data, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            case MODE_VERIFY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                byte[] verifyBuffer = RSACore.convert(buffer, 0, bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                data = RSACore.rsa(verifyBuffer, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                return padding.unpad(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            case MODE_ENCRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                data = padding.pad(buffer, 0, bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                return RSACore.rsa(data, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            case MODE_DECRYPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                byte[] decryptBuffer = RSACore.convert(buffer, 0, bufOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                data = RSACore.rsa(decryptBuffer, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                return padding.unpad(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                throw new AssertionError("Internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            bufOfs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        update(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        return B0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    protected int engineUpdate(byte[] in, int inOfs, int inLen, byte[] out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            int outOfs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        update(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    protected byte[] engineDoFinal(byte[] in, int inOfs, int inLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            throws BadPaddingException, IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        update(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    protected int engineDoFinal(byte[] in, int inOfs, int inLen, byte[] out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            int outOfs) throws ShortBufferException, BadPaddingException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (outputSize > out.length - outOfs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            throw new ShortBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                ("Need " + outputSize + " bytes for output");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        update(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        byte[] result = doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        int n = result.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        System.arraycopy(result, 0, out, outOfs, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    protected byte[] engineWrap(Key key) throws InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if ((encoded == null) || (encoded.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            throw new InvalidKeyException("Could not obtain encoded key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (encoded.length > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            throw new InvalidKeyException("Key is too long for wrapping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        update(encoded, 0, encoded.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            return doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        } catch (BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            // should not occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            throw new InvalidKeyException("Wrapping failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    protected Key engineUnwrap(byte[] wrappedKey, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            int type) throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (wrappedKey.length > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            throw new InvalidKeyException("Key is too long for unwrapping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        update(wrappedKey, 0, wrappedKey.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            byte[] encoded = doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            return ConstructKeys.constructKey(encoded, algorithm, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        } catch (BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            // should not occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            throw new InvalidKeyException("Unwrapping failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        } catch (IllegalBlockSizeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            // should not occur, handled with length check above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            throw new InvalidKeyException("Unwrapping failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    protected int engineGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        RSAKey rsaKey = RSAKeyFactory.toRSAKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        return rsaKey.getModulus().bitLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
}