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