jdk/src/share/classes/com/sun/crypto/provider/PKCS12PBECipherCore.java
author valeriep
Tue, 08 May 2012 17:57:48 -0700
changeset 12685 8a448b5b9006
parent 11835 c9e7cfc908b3
child 14342 8435a30053c1
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
/*
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2010, 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.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class implements password-base encryption algorithm with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * SHA1 digest and the following Ciphers in CBC mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * - DESede cipher and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * - RC2 Cipher with 40-bit effective key length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * as defined by PKCS #12 version 1.0 standard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @see javax.crypto.CipherSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
final class PKCS12PBECipherCore {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private CipherCore cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private int blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private int keySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private String algo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private byte[] salt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int iCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final int DEFAULT_SALT_LENGTH = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final int DEFAULT_COUNT = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static final int CIPHER_KEY = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static final int CIPHER_IV = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static final int MAC_KEY = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static byte[] derive(char[] chars, byte[] salt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                         int ic, int n, int type) {
11835
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
    63
        // Add in trailing NULL terminator.  Special case:
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
    64
        // no terminator if password is "\0".
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        int length = chars.length*2;
11835
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
    66
        if (length == 2 && chars[0] == 0) {
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
    67
            chars = new char[0];
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
    68
            length = 0;
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
    69
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            length += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
11835
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
    72
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        byte[] passwd = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        for (int i = 0, j = 0; i < chars.length; i++, j+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            passwd[j] = (byte) ((chars[i] >>> 8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            passwd[j+1] = (byte) (chars[i] & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        int v = 512 / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        int u = 160 / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        int c = roundup(n, u) / u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        byte[] D = new byte[v];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int s = roundup(salt.length, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        int p = roundup(passwd.length, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        byte[] I = new byte[s + p];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        byte[] key = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Arrays.fill(D, (byte)type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        concat(salt, I, 0, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        concat(passwd, I, s, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            MessageDigest sha = MessageDigest.getInstance("SHA1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            byte[] Ai;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            byte[] B = new byte[v];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            byte[] tmp = new byte[v];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            for (; ; i++, n -= u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                sha.update(D);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                sha.update(I);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                Ai = sha.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                for (int r = 1; r < ic; r++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    Ai = sha.digest(Ai);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                System.arraycopy(Ai, 0, key, u * i, Math.min(n, u));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                if (i + 1 == c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                concat(Ai, B, 0, B.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                BigInteger B1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                B1 = new BigInteger(1, B).add(BigInteger.ONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                for (int j = 0; j < I.length; j += v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    BigInteger Ij;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    int trunc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    if (tmp.length != v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        tmp = new byte[v];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    System.arraycopy(I, j, tmp, 0, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    Ij = new BigInteger(1, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    Ij = Ij.add(B1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    tmp = Ij.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    trunc = tmp.length - v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    if (trunc >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        System.arraycopy(tmp, trunc, I, j, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    } else if (trunc < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        Arrays.fill(I, j, j + (-trunc), (byte)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                        System.arraycopy(tmp, 0, I, j + (-trunc), tmp.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throw new RuntimeException("internal error: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private static int roundup(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return ((x + (y - 1)) / y) * y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private static void concat(byte[] src, byte[] dst, int start, int len) {
11835
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
   141
        if (src.length == 0) {
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
   142
            return;
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
   143
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int loop = len / src.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int off, i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        for (i = 0, off = 0; i < loop; i++, off += src.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            System.arraycopy(src, 0, dst, off + start, src.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        System.arraycopy(src, 0, dst, off + start, len - off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    PKCS12PBECipherCore(String symmCipherAlg, int defKeySize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        algo = symmCipherAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        SymmetricCipher symmCipher = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (algo.equals("DESede")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            symmCipher = new DESedeCrypt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } else if (algo.equals("RC2")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            symmCipher = new RC2Crypt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new NoSuchAlgorithmException("No Cipher implementation " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                       "for PBEWithSHA1And" + algo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        blockSize = symmCipher.getBlockSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        cipher = new CipherCore(symmCipher, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        cipher.setMode("CBC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            cipher.setPadding("PKCS5Padding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } catch (NoSuchPaddingException nspe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            // should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        keySize = defKeySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    void implSetMode(String mode) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if ((mode != null) && (!mode.equalsIgnoreCase("CBC"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw new NoSuchAlgorithmException("Invalid cipher mode: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                               + mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    void implSetPadding(String padding) throws NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if ((padding != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            (!padding.equalsIgnoreCase("PKCS5Padding"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw new NoSuchPaddingException("Invalid padding scheme: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                             padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    int implGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    int implGetOutputSize(int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return cipher.getOutputSize(inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    byte[] implGetIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return cipher.getIV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    AlgorithmParameters implGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        AlgorithmParameters params = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (salt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            // Cipher is not initialized with parameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            // follow the recommendation in PKCS12 v1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // section B.4 to generate salt and iCount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            salt = new byte[DEFAULT_SALT_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            SunJCE.RANDOM.nextBytes(salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            iCount = DEFAULT_COUNT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            params = AlgorithmParameters.getInstance("PBEWithSHA1And" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                (algo.equalsIgnoreCase("RC2")?"RC2_40":algo), "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        } catch (GeneralSecurityException gse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            // should never happen
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   217
            throw new RuntimeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   218
                "SunJCE provider is not configured properly");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            params.init(pbeSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } catch (InvalidParameterSpecException ipse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new RuntimeException("PBEParameterSpec not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    void implInit(int opmode, Key key, AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                  SecureRandom random) throws InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        char[] passwdChars = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        salt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        iCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (key instanceof javax.crypto.interfaces.PBEKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            javax.crypto.interfaces.PBEKey pbeKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                (javax.crypto.interfaces.PBEKey) key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            passwdChars = pbeKey.getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            salt = pbeKey.getSalt(); // maybe null if unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            iCount = pbeKey.getIterationCount(); // maybe 0 if unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        } else if (key instanceof SecretKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            byte[] passwdBytes = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if ((passwdBytes == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                !(key.getAlgorithm().regionMatches(true, 0, "PBE", 0, 3))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                throw new InvalidKeyException("Missing password");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            passwdChars = new char[passwdBytes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            for (int i=0; i<passwdChars.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                passwdChars[i] = (char) (passwdBytes[i] & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            throw new InvalidKeyException("SecretKey of PBE type required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (((opmode == Cipher.DECRYPT_MODE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
             (opmode == Cipher.UNWRAP_MODE)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            ((params == null) && ((salt == null) || (iCount == 0)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                ("Parameters missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            // generate default for salt and iteration count if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            if (salt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                salt = new byte[DEFAULT_SALT_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                if (random != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    random.nextBytes(salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    SunJCE.RANDOM.nextBytes(salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (iCount == 0) iCount = DEFAULT_COUNT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        } else if (!(params instanceof PBEParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                ("PBEParameterSpec type required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            PBEParameterSpec pbeParams = (PBEParameterSpec) params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            // make sure the parameter values are consistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (salt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                if (!Arrays.equals(salt, pbeParams.getSalt())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        ("Inconsistent value of salt between key and params");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                salt = pbeParams.getSalt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (iCount != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                if (iCount != pbeParams.getIterationCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        ("Different iteration count between key and params");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                iCount = pbeParams.getIterationCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        // salt is recommended to be ideally as long as the output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        // of the hash function. However, it may be too strict to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        // force this; so instead, we'll just require the minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // salt length to be 8-byte which is what PKCS#5 recommends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        // and openssl does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (salt.length < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                ("Salt must be at least 8 bytes long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (iCount <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                ("IterationCount must be a positive number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        byte[] derivedKey = derive(passwdChars, salt, iCount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                   keySize, CIPHER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        SecretKey cipherKey = new SecretKeySpec(derivedKey, algo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        byte[] derivedIv = derive(passwdChars, salt, iCount, 8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                  CIPHER_IV);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        IvParameterSpec ivSpec = new IvParameterSpec(derivedIv, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        // initialize the underlying cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        cipher.init(opmode, cipherKey, ivSpec, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    void implInit(int opmode, Key key, AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                  SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        AlgorithmParameterSpec paramSpec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                paramSpec = params.getParameterSpec(PBEParameterSpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            } catch (InvalidParameterSpecException ipse) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   328
                throw new InvalidAlgorithmParameterException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   329
                    "requires PBE parameters");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        implInit(opmode, key, paramSpec, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    void implInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            implInit(opmode, key, (AlgorithmParameterSpec) null, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            throw new InvalidKeyException("requires PBE parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    byte[] implUpdate(byte[] in, int inOff, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return cipher.update(in, inOff, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    int implUpdate(byte[] in, int inOff, int inLen, byte[] out, int outOff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return cipher.update(in, inOff, inLen, out, outOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    byte[] implDoFinal(byte[] in, int inOff, int inLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return cipher.doFinal(in, inOff, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    int implDoFinal(byte[] in, int inOff, int inLen, byte[] out, int outOff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        throws ShortBufferException, IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
               BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return cipher.doFinal(in, inOff, inLen, out, outOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    int implGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return keySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    byte[] implWrap(Key key) throws IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        return cipher.wrap(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    Key implUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                   int wrappedKeyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        return cipher.unwrap(wrappedKey, wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                             wrappedKeyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public static final class PBEWithSHA1AndDESede extends CipherSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        private final PKCS12PBECipherCore core;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        public PBEWithSHA1AndDESede() throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            core = new PKCS12PBECipherCore("DESede", 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        protected byte[] engineDoFinal(byte[] in, int inOff, int inLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            return core.implDoFinal(in, inOff, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        protected int engineDoFinal(byte[] in, int inOff, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                                    byte[] out, int outOff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            throws ShortBufferException, IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                   BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            return core.implDoFinal(in, inOff, inLen, out, outOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        protected int engineGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            return core.implGetBlockSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        protected byte[] engineGetIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            return core.implGetIV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        protected int engineGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            return core.implGetKeySize(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        protected int engineGetOutputSize(int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            return core.implGetOutputSize(inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        protected AlgorithmParameters engineGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            return core.implGetParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                                  AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                                  SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            core.implInit(opmode, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                                  AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                                  SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            core.implInit(opmode, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        protected void engineInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            core.implInit(opmode, key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        protected void engineSetMode(String mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            core.implSetMode(mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        protected void engineSetPadding(String paddingScheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            throws NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            core.implSetPadding(paddingScheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        protected Key engineUnwrap(byte[] wrappedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                   String wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                                   int wrappedKeyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            return core.implUnwrap(wrappedKey, wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                                   wrappedKeyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        protected byte[] engineUpdate(byte[] in, int inOff, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            return core.implUpdate(in, inOff, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        protected int engineUpdate(byte[] in, int inOff, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                                   byte[] out, int outOff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            return core.implUpdate(in, inOff, inLen, out, outOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        protected byte[] engineWrap(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            throws IllegalBlockSizeException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            return core.implWrap(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public static final class PBEWithSHA1AndRC2_40 extends CipherSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        private final PKCS12PBECipherCore core;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        public PBEWithSHA1AndRC2_40() throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            core = new PKCS12PBECipherCore("RC2", 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        protected byte[] engineDoFinal(byte[] in, int inOff, int inLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            return core.implDoFinal(in, inOff, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        protected int engineDoFinal(byte[] in, int inOff, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                                    byte[] out, int outOff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            throws ShortBufferException, IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                   BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            return core.implDoFinal(in, inOff, inLen, out, outOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        protected int engineGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            return core.implGetBlockSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        protected byte[] engineGetIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            return core.implGetIV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        protected int engineGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            return core.implGetKeySize(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        protected int engineGetOutputSize(int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            return core.implGetOutputSize(inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        protected AlgorithmParameters engineGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            return core.implGetParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                  AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                                  SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            core.implInit(opmode, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                                  AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                                  SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            core.implInit(opmode, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        protected void engineInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            core.implInit(opmode, key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        protected void engineSetMode(String mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            core.implSetMode(mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        protected void engineSetPadding(String paddingScheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            throws NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            core.implSetPadding(paddingScheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        protected Key engineUnwrap(byte[] wrappedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                                   String wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                                   int wrappedKeyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            return core.implUnwrap(wrappedKey, wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                                   wrappedKeyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        protected byte[] engineUpdate(byte[] in, int inOff, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            return core.implUpdate(in, inOff, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        protected int engineUpdate(byte[] in, int inOff, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                                   byte[] out, int outOff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            return core.implUpdate(in, inOff, inLen, out, outOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        protected byte[] engineWrap(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            throws IllegalBlockSizeException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            return core.implWrap(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
}