jdk/src/share/classes/com/sun/crypto/provider/PKCS12PBECipherCore.java
author vinnie
Thu, 11 Apr 2013 17:57:08 +0100
changeset 16909 78a1749a43e2
parent 15010 ec6b49ce42b1
permissions -rw-r--r--
7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider Reviewed-by: vinnie, wetmore Contributed-by: Tony Scarpino <anthony.scarpino@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 14405
diff changeset
     2
 * Copyright (c) 2003, 2013, 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.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This class implements password-base encryption algorithm with
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    37
 * SHA1 digest and the following Ciphers (in CBC mode, where applicable):
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * - DESede cipher and
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    39
 * - RC2 Cipher with 40-bit or 128-bit effective key length and
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    40
 * - RC4 Cipher with 40-bit or 128-bit effective key length
2
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 {
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    47
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    48
    // TBD: replace CipherCore with a CipherSpi object to simplify maintenance
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    49
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private CipherCore cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private int blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int keySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private String algo = null;
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    54
    private String pbeAlgo = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private byte[] salt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private int iCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static final int DEFAULT_SALT_LENGTH = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final int DEFAULT_COUNT = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static final int CIPHER_KEY = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static final int CIPHER_IV = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static final int MAC_KEY = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    65
    // Uses default hash algorithm (SHA-1)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static byte[] derive(char[] chars, byte[] salt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                         int ic, int n, int type) {
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    68
        return derive(chars, salt, ic, n, type, "SHA-1", 64);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    69
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    70
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    71
    // Uses supplied hash algorithm
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    72
    static byte[] derive(char[] chars, byte[] salt, int ic, int n, int type,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    73
        String hashAlgo, int blockLength) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    74
11835
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
    75
        // Add in trailing NULL terminator.  Special case:
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
    76
        // no terminator if password is "\0".
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        int length = chars.length*2;
11835
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
    78
        if (length == 2 && chars[0] == 0) {
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
    79
            chars = new char[0];
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
    80
            length = 0;
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
    81
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            length += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
11835
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
    84
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        byte[] passwd = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        for (int i = 0, j = 0; i < chars.length; i++, j+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            passwd[j] = (byte) ((chars[i] >>> 8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            passwd[j+1] = (byte) (chars[i] & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        byte[] key = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    92
        try {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    93
            MessageDigest sha = MessageDigest.getInstance(hashAlgo);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    95
            int v = blockLength;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    96
            int u = sha.getDigestLength();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    97
            int c = roundup(n, u) / u;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    98
            byte[] D = new byte[v];
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
    99
            int s = roundup(salt.length, v);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   100
            int p = roundup(passwd.length, v);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   101
            byte[] I = new byte[s + p];
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   102
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   103
            Arrays.fill(D, (byte)type);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   104
            concat(salt, I, 0, s);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   105
            concat(passwd, I, s, p);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   106
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            byte[] Ai;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            byte[] B = new byte[v];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            byte[] tmp = new byte[v];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            for (; ; i++, n -= u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                sha.update(D);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                sha.update(I);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                Ai = sha.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                for (int r = 1; r < ic; r++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    Ai = sha.digest(Ai);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                System.arraycopy(Ai, 0, key, u * i, Math.min(n, u));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                if (i + 1 == c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                concat(Ai, B, 0, B.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                BigInteger B1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                B1 = new BigInteger(1, B).add(BigInteger.ONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                for (int j = 0; j < I.length; j += v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    BigInteger Ij;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    int trunc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    if (tmp.length != v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        tmp = new byte[v];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    System.arraycopy(I, j, tmp, 0, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    Ij = new BigInteger(1, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    Ij = Ij.add(B1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    tmp = Ij.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    trunc = tmp.length - v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    if (trunc >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        System.arraycopy(tmp, trunc, I, j, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    } else if (trunc < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        Arrays.fill(I, j, j + (-trunc), (byte)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        System.arraycopy(tmp, 0, I, j + (-trunc), tmp.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new RuntimeException("internal error: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private static int roundup(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return ((x + (y - 1)) / y) * y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    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
   155
        if (src.length == 0) {
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
   156
            return;
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 7043
diff changeset
   157
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        int loop = len / src.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        int off, i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        for (i = 0, off = 0; i < loop; i++, off += src.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            System.arraycopy(src, 0, dst, off + start, src.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        System.arraycopy(src, 0, dst, off + start, len - off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    PKCS12PBECipherCore(String symmCipherAlg, int defKeySize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        throws NoSuchAlgorithmException {
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   167
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        algo = symmCipherAlg;
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   169
        if (algo.equals("RC4")) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   170
            pbeAlgo = "PBEWithSHA1AndRC4_" + defKeySize * 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } else {
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   172
            SymmetricCipher symmCipher = null;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   173
            if (algo.equals("DESede")) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   174
                symmCipher = new DESedeCrypt();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   175
                pbeAlgo = "PBEWithSHA1AndDESede";
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   176
            } else if (algo.equals("RC2")) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   177
                symmCipher = new RC2Crypt();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   178
                pbeAlgo = "PBEWithSHA1AndRC2_" + defKeySize * 8;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   179
            } else {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   180
                throw new NoSuchAlgorithmException("No Cipher implementation " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                       "for PBEWithSHA1And" + algo);
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   182
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   183
            blockSize = symmCipher.getBlockSize();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   184
            cipher = new CipherCore(symmCipher, blockSize);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   185
            cipher.setMode("CBC");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   186
            try {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   187
                cipher.setPadding("PKCS5Padding");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   188
            } catch (NoSuchPaddingException nspe) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   189
                // should not happen
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   190
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        keySize = defKeySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    void implSetMode(String mode) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if ((mode != null) && (!mode.equalsIgnoreCase("CBC"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            throw new NoSuchAlgorithmException("Invalid cipher mode: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                               + mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    void implSetPadding(String padding) throws NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if ((padding != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            (!padding.equalsIgnoreCase("PKCS5Padding"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throw new NoSuchPaddingException("Invalid padding scheme: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                             padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    int implGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    int implGetOutputSize(int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return cipher.getOutputSize(inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    byte[] implGetIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return cipher.getIV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    AlgorithmParameters implGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        AlgorithmParameters params = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (salt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // Cipher is not initialized with parameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            // follow the recommendation in PKCS12 v1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            // section B.4 to generate salt and iCount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            salt = new byte[DEFAULT_SALT_LENGTH];
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 14405
diff changeset
   229
            SunJCE.getRandom().nextBytes(salt);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            iCount = DEFAULT_COUNT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        try {
16909
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   234
            params = AlgorithmParameters.getInstance(pbeAlgo,
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   235
                SunJCE.getInstance());
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   236
            params.init(pbeSpec);
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   237
        } catch (NoSuchAlgorithmException nsae) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            // should never happen
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   239
            throw new RuntimeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   240
                "SunJCE provider is not configured properly");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        } catch (InvalidParameterSpecException ipse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            throw new RuntimeException("PBEParameterSpec not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    void implInit(int opmode, Key key, AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                  SecureRandom random) throws InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        InvalidAlgorithmParameterException {
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   251
        implInit(opmode, key, params, random, null);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   252
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   253
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   254
    void implInit(int opmode, Key key, AlgorithmParameterSpec params,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   255
                  SecureRandom random, CipherSpi cipherImpl)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   256
                      throws InvalidKeyException,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   257
        InvalidAlgorithmParameterException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        char[] passwdChars = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        salt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        iCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (key instanceof javax.crypto.interfaces.PBEKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            javax.crypto.interfaces.PBEKey pbeKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                (javax.crypto.interfaces.PBEKey) key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            passwdChars = pbeKey.getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            salt = pbeKey.getSalt(); // maybe null if unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            iCount = pbeKey.getIterationCount(); // maybe 0 if unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } else if (key instanceof SecretKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            byte[] passwdBytes = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if ((passwdBytes == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                !(key.getAlgorithm().regionMatches(true, 0, "PBE", 0, 3))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                throw new InvalidKeyException("Missing password");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            passwdChars = new char[passwdBytes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            for (int i=0; i<passwdChars.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                passwdChars[i] = (char) (passwdBytes[i] & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            throw new InvalidKeyException("SecretKey of PBE type required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (((opmode == Cipher.DECRYPT_MODE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
             (opmode == Cipher.UNWRAP_MODE)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            ((params == null) && ((salt == null) || (iCount == 0)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                ("Parameters missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // generate default for salt and iteration count if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (salt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                salt = new byte[DEFAULT_SALT_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                if (random != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    random.nextBytes(salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                } else {
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 14405
diff changeset
   295
                    SunJCE.getRandom().nextBytes(salt);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (iCount == 0) iCount = DEFAULT_COUNT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        } else if (!(params instanceof PBEParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                ("PBEParameterSpec type required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            PBEParameterSpec pbeParams = (PBEParameterSpec) params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            // make sure the parameter values are consistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if (salt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                if (!Arrays.equals(salt, pbeParams.getSalt())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                        ("Inconsistent value of salt between key and params");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                salt = pbeParams.getSalt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (iCount != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                if (iCount != pbeParams.getIterationCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                        ("Different iteration count between key and params");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                iCount = pbeParams.getIterationCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        // salt is recommended to be ideally as long as the output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        // of the hash function. However, it may be too strict to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // force this; so instead, we'll just require the minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        // salt length to be 8-byte which is what PKCS#5 recommends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // and openssl does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (salt.length < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                ("Salt must be at least 8 bytes long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (iCount <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                ("IterationCount must be a positive number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        byte[] derivedKey = derive(passwdChars, salt, iCount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                   keySize, CIPHER_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        SecretKey cipherKey = new SecretKeySpec(derivedKey, algo);
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   338
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   339
        if (cipherImpl != null && cipherImpl instanceof ARCFOURCipher) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   340
            ((ARCFOURCipher)cipherImpl).engineInit(opmode, cipherKey, random);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   341
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   342
        } else {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   343
            byte[] derivedIv = derive(passwdChars, salt, iCount, 8,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                  CIPHER_IV);
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   345
            IvParameterSpec ivSpec = new IvParameterSpec(derivedIv, 0, 8);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   347
            // initialize the underlying cipher
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   348
            cipher.init(opmode, cipherKey, ivSpec, random);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   349
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    void implInit(int opmode, Key key, AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                  SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        throws InvalidKeyException, InvalidAlgorithmParameterException {
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   355
        implInit(opmode, key, params, random, null);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   356
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   357
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   358
    void implInit(int opmode, Key key, AlgorithmParameters params,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   359
                  SecureRandom random, CipherSpi cipherImpl)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   360
        throws InvalidKeyException, InvalidAlgorithmParameterException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        AlgorithmParameterSpec paramSpec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                paramSpec = params.getParameterSpec(PBEParameterSpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            } catch (InvalidParameterSpecException ipse) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   366
                throw new InvalidAlgorithmParameterException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   367
                    "requires PBE parameters");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   370
        implInit(opmode, key, paramSpec, random, cipherImpl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    void implInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        throws InvalidKeyException {
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   375
        implInit(opmode, key, random, null);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   376
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   377
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   378
    void implInit(int opmode, Key key, SecureRandom random,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   379
        CipherSpi cipherImpl) throws InvalidKeyException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        try {
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   381
            implInit(opmode, key, (AlgorithmParameterSpec) null, random,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   382
                cipherImpl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            throw new InvalidKeyException("requires PBE parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    byte[] implUpdate(byte[] in, int inOff, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return cipher.update(in, inOff, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    int implUpdate(byte[] in, int inOff, int inLen, byte[] out, int outOff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return cipher.update(in, inOff, inLen, out, outOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    byte[] implDoFinal(byte[] in, int inOff, int inLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        return cipher.doFinal(in, inOff, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    int implDoFinal(byte[] in, int inOff, int inLen, byte[] out, int outOff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        throws ShortBufferException, IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
               BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return cipher.doFinal(in, inOff, inLen, out, outOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    int implGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return keySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    byte[] implWrap(Key key) throws IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return cipher.wrap(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    Key implUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                   int wrappedKeyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        return cipher.unwrap(wrappedKey, wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                             wrappedKeyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    public static final class PBEWithSHA1AndDESede extends CipherSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        private final PKCS12PBECipherCore core;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        public PBEWithSHA1AndDESede() throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            core = new PKCS12PBECipherCore("DESede", 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        protected byte[] engineDoFinal(byte[] in, int inOff, int inLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            return core.implDoFinal(in, inOff, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        protected int engineDoFinal(byte[] in, int inOff, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                                    byte[] out, int outOff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            throws ShortBufferException, IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                   BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return core.implDoFinal(in, inOff, inLen, out, outOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        protected int engineGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            return core.implGetBlockSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        protected byte[] engineGetIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            return core.implGetIV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        protected int engineGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            return core.implGetKeySize(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        protected int engineGetOutputSize(int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            return core.implGetOutputSize(inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        protected AlgorithmParameters engineGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            return core.implGetParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                                  AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                                  SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            core.implInit(opmode, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                                  AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                                  SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            core.implInit(opmode, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        protected void engineInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            core.implInit(opmode, key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        protected void engineSetMode(String mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            core.implSetMode(mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        protected void engineSetPadding(String paddingScheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            throws NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            core.implSetPadding(paddingScheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        protected Key engineUnwrap(byte[] wrappedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                                   String wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                   int wrappedKeyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            return core.implUnwrap(wrappedKey, wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                                   wrappedKeyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        protected byte[] engineUpdate(byte[] in, int inOff, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            return core.implUpdate(in, inOff, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        protected int engineUpdate(byte[] in, int inOff, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                                   byte[] out, int outOff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            return core.implUpdate(in, inOff, inLen, out, outOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        protected byte[] engineWrap(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            throws IllegalBlockSizeException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            return core.implWrap(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    public static final class PBEWithSHA1AndRC2_40 extends CipherSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        private final PKCS12PBECipherCore core;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        public PBEWithSHA1AndRC2_40() throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            core = new PKCS12PBECipherCore("RC2", 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        protected byte[] engineDoFinal(byte[] in, int inOff, int inLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            return core.implDoFinal(in, inOff, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        protected int engineDoFinal(byte[] in, int inOff, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                                    byte[] out, int outOff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            throws ShortBufferException, IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                   BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            return core.implDoFinal(in, inOff, inLen, out, outOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        protected int engineGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            return core.implGetBlockSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        protected byte[] engineGetIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            return core.implGetIV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        protected int engineGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            return core.implGetKeySize(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        protected int engineGetOutputSize(int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            return core.implGetOutputSize(inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        protected AlgorithmParameters engineGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            return core.implGetParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                                  AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                                  SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            core.implInit(opmode, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                                  AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                                  SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            core.implInit(opmode, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        protected void engineInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            core.implInit(opmode, key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        protected void engineSetMode(String mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            core.implSetMode(mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        protected void engineSetPadding(String paddingScheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            throws NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            core.implSetPadding(paddingScheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        protected Key engineUnwrap(byte[] wrappedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                                   String wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                                   int wrappedKeyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            return core.implUnwrap(wrappedKey, wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                                   wrappedKeyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        protected byte[] engineUpdate(byte[] in, int inOff, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            return core.implUpdate(in, inOff, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        protected int engineUpdate(byte[] in, int inOff, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                                   byte[] out, int outOff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            return core.implUpdate(in, inOff, inLen, out, outOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        protected byte[] engineWrap(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            throws IllegalBlockSizeException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            return core.implWrap(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   573
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   574
    public static final class PBEWithSHA1AndRC2_128 extends CipherSpi {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   575
        private final PKCS12PBECipherCore core;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   576
        public PBEWithSHA1AndRC2_128() throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   577
            core = new PKCS12PBECipherCore("RC2", 16);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   578
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   579
        protected byte[] engineDoFinal(byte[] in, int inOff, int inLen)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   580
            throws IllegalBlockSizeException, BadPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   581
            return core.implDoFinal(in, inOff, inLen);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   582
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   583
        protected int engineDoFinal(byte[] in, int inOff, int inLen,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   584
                                    byte[] out, int outOff)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   585
            throws ShortBufferException, IllegalBlockSizeException,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   586
                   BadPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   587
            return core.implDoFinal(in, inOff, inLen, out, outOff);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   588
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   589
        protected int engineGetBlockSize() {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   590
            return core.implGetBlockSize();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   591
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   592
        protected byte[] engineGetIV() {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   593
            return core.implGetIV();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   594
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   595
        protected int engineGetKeySize(Key key) throws InvalidKeyException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   596
            return core.implGetKeySize(key);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   597
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   598
        protected int engineGetOutputSize(int inLen) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   599
            return core.implGetOutputSize(inLen);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   600
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   601
        protected AlgorithmParameters engineGetParameters() {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   602
            return core.implGetParameters();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   603
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   604
        protected void engineInit(int opmode, Key key,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   605
                                  AlgorithmParameterSpec params,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   606
                                  SecureRandom random)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   607
            throws InvalidKeyException, InvalidAlgorithmParameterException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   608
            core.implInit(opmode, key, params, random);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   609
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   610
        protected void engineInit(int opmode, Key key,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   611
                                  AlgorithmParameters params,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   612
                                  SecureRandom random)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   613
            throws InvalidKeyException, InvalidAlgorithmParameterException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   614
            core.implInit(opmode, key, params, random);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   615
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   616
        protected void engineInit(int opmode, Key key, SecureRandom random)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   617
            throws InvalidKeyException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   618
            core.implInit(opmode, key, random);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   619
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   620
        protected void engineSetMode(String mode)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   621
            throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   622
            core.implSetMode(mode);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   623
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   624
        protected void engineSetPadding(String paddingScheme)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   625
            throws NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   626
            core.implSetPadding(paddingScheme);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   627
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   628
        protected Key engineUnwrap(byte[] wrappedKey,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   629
                                   String wrappedKeyAlgorithm,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   630
                                   int wrappedKeyType)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   631
            throws InvalidKeyException, NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   632
            return core.implUnwrap(wrappedKey, wrappedKeyAlgorithm,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   633
                                   wrappedKeyType);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   634
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   635
        protected byte[] engineUpdate(byte[] in, int inOff, int inLen) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   636
            return core.implUpdate(in, inOff, inLen);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   637
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   638
        protected int engineUpdate(byte[] in, int inOff, int inLen,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   639
                                   byte[] out, int outOff)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   640
            throws ShortBufferException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   641
            return core.implUpdate(in, inOff, inLen, out, outOff);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   642
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   643
        protected byte[] engineWrap(Key key)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   644
            throws IllegalBlockSizeException, InvalidKeyException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   645
            return core.implWrap(key);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   646
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   647
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   648
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   649
    public static final class PBEWithSHA1AndRC4_40 extends CipherSpi {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   650
        private static final int RC4_KEYSIZE = 5;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   651
        private final PKCS12PBECipherCore core;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   652
        private final ARCFOURCipher cipher;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   653
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   654
        public PBEWithSHA1AndRC4_40() throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   655
            core = new PKCS12PBECipherCore("RC4", RC4_KEYSIZE);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   656
            cipher = new ARCFOURCipher();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   657
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   658
        protected byte[] engineDoFinal(byte[] in, int inOff, int inLen)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   659
            throws IllegalBlockSizeException, BadPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   660
            return cipher.engineDoFinal(in, inOff, inLen);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   661
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   662
        protected int engineDoFinal(byte[] in, int inOff, int inLen,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   663
                                    byte[] out, int outOff)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   664
            throws ShortBufferException, IllegalBlockSizeException,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   665
                   BadPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   666
            return cipher.engineDoFinal(in, inOff, inLen, out, outOff);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   667
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   668
        protected int engineGetBlockSize() {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   669
            return cipher.engineGetBlockSize();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   670
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   671
        protected byte[] engineGetIV() {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   672
            return cipher.engineGetIV();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   673
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   674
        protected int engineGetKeySize(Key key) throws InvalidKeyException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   675
            return RC4_KEYSIZE;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   676
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   677
        protected int engineGetOutputSize(int inLen) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   678
            return cipher.engineGetOutputSize(inLen);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   679
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   680
        protected AlgorithmParameters engineGetParameters() {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   681
            return core.implGetParameters();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   682
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   683
        protected void engineInit(int opmode, Key key,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   684
                                  AlgorithmParameterSpec params,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   685
                                  SecureRandom random)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   686
            throws InvalidKeyException, InvalidAlgorithmParameterException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   687
            core.implInit(opmode, key, params, random, cipher);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   688
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   689
        protected void engineInit(int opmode, Key key,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   690
                                  AlgorithmParameters params,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   691
                                  SecureRandom random)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   692
            throws InvalidKeyException, InvalidAlgorithmParameterException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   693
            core.implInit(opmode, key, params, random, cipher);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   694
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   695
        protected void engineInit(int opmode, Key key, SecureRandom random)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   696
            throws InvalidKeyException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   697
            core.implInit(opmode, key, random, cipher);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   698
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   699
        protected void engineSetMode(String mode)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   700
            throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   701
            if (mode.equalsIgnoreCase("ECB") == false) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   702
                throw new NoSuchAlgorithmException("Unsupported mode " + mode);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   703
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   704
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   705
        protected void engineSetPadding(String paddingScheme)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   706
            throws NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   707
            if (paddingScheme.equalsIgnoreCase("NoPadding") == false) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   708
                throw new NoSuchPaddingException("Padding must be NoPadding");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   709
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   710
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   711
        protected Key engineUnwrap(byte[] wrappedKey,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   712
                                   String wrappedKeyAlgorithm,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   713
                                   int wrappedKeyType)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   714
            throws InvalidKeyException, NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   715
            return cipher.engineUnwrap(wrappedKey, wrappedKeyAlgorithm,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   716
                                   wrappedKeyType);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   717
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   718
        protected byte[] engineUpdate(byte[] in, int inOff, int inLen) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   719
            return cipher.engineUpdate(in, inOff, inLen);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   720
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   721
        protected int engineUpdate(byte[] in, int inOff, int inLen,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   722
                                   byte[] out, int outOff)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   723
            throws ShortBufferException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   724
            return cipher.engineUpdate(in, inOff, inLen, out, outOff);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   725
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   726
        protected byte[] engineWrap(Key key)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   727
            throws IllegalBlockSizeException, InvalidKeyException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   728
            return cipher.engineWrap(key);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   729
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   730
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   731
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   732
    public static final class PBEWithSHA1AndRC4_128 extends CipherSpi {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   733
        private static final int RC4_KEYSIZE = 16;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   734
        private final PKCS12PBECipherCore core;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   735
        private final ARCFOURCipher cipher;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   736
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   737
        public PBEWithSHA1AndRC4_128() throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   738
            core = new PKCS12PBECipherCore("RC4", RC4_KEYSIZE);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   739
            cipher = new ARCFOURCipher();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   740
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   741
        protected byte[] engineDoFinal(byte[] in, int inOff, int inLen)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   742
            throws IllegalBlockSizeException, BadPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   743
            return cipher.engineDoFinal(in, inOff, inLen);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   744
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   745
        protected int engineDoFinal(byte[] in, int inOff, int inLen,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   746
                                    byte[] out, int outOff)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   747
            throws ShortBufferException, IllegalBlockSizeException,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   748
                   BadPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   749
            return cipher.engineDoFinal(in, inOff, inLen, out, outOff);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   750
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   751
        protected int engineGetBlockSize() {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   752
            return cipher.engineGetBlockSize();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   753
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   754
        protected byte[] engineGetIV() {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   755
            return cipher.engineGetIV();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   756
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   757
        protected int engineGetKeySize(Key key) throws InvalidKeyException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   758
            return RC4_KEYSIZE;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   759
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   760
        protected int engineGetOutputSize(int inLen) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   761
            return cipher.engineGetOutputSize(inLen);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   762
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   763
        protected AlgorithmParameters engineGetParameters() {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   764
            return core.implGetParameters();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   765
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   766
        protected void engineInit(int opmode, Key key,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   767
                                  AlgorithmParameterSpec params,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   768
                                  SecureRandom random)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   769
            throws InvalidKeyException, InvalidAlgorithmParameterException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   770
            core.implInit(opmode, key, params, random, cipher);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   771
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   772
        protected void engineInit(int opmode, Key key,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   773
                                  AlgorithmParameters params,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   774
                                  SecureRandom random)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   775
            throws InvalidKeyException, InvalidAlgorithmParameterException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   776
            core.implInit(opmode, key, params, random, cipher);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   777
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   778
        protected void engineInit(int opmode, Key key, SecureRandom random)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   779
            throws InvalidKeyException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   780
            core.implInit(opmode, key, random, cipher);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   781
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   782
        protected void engineSetMode(String mode)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   783
            throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   784
            if (mode.equalsIgnoreCase("ECB") == false) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   785
                throw new NoSuchAlgorithmException("Unsupported mode " + mode);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   786
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   787
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   788
        protected void engineSetPadding(String paddingScheme)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   789
            throws NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   790
            if (paddingScheme.equalsIgnoreCase("NoPadding") == false) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   791
                throw new NoSuchPaddingException("Padding must be NoPadding");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   792
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   793
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   794
        protected Key engineUnwrap(byte[] wrappedKey,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   795
                                   String wrappedKeyAlgorithm,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   796
                                   int wrappedKeyType)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   797
            throws InvalidKeyException, NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   798
            return cipher.engineUnwrap(wrappedKey, wrappedKeyAlgorithm,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   799
                                   wrappedKeyType);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   800
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   801
        protected byte[] engineUpdate(byte[] in, int inOff, int inLen) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   802
            return cipher.engineUpdate(in, inOff, inLen);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   803
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   804
        protected int engineUpdate(byte[] in, int inOff, int inLen,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   805
                                   byte[] out, int outOff)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   806
            throws ShortBufferException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   807
            return cipher.engineUpdate(in, inOff, inLen, out, outOff);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   808
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   809
        protected byte[] engineWrap(Key key)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   810
            throws IllegalBlockSizeException, InvalidKeyException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   811
            return cipher.engineWrap(key);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   812
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 14342
diff changeset
   813
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
}