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