src/java.base/share/classes/com/sun/crypto/provider/PBES2Core.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:
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     1
/*
35285
c8e399b7825b 8138589: Correct limits on unlimited cryptography
vinnie
parents: 25859
diff changeset
     2
 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     4
 *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    10
 *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    15
 * accompanied this code).
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    16
 *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    20
 *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    23
 * questions.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    24
 */
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    25
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    26
package com.sun.crypto.provider;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    27
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    28
import java.security.*;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    29
import java.security.spec.*;
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
    30
import java.util.Arrays;
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    31
import javax.crypto.*;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    32
import javax.crypto.spec.*;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    33
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    34
/**
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    35
 * This class represents password-based encryption as defined by the PKCS #5
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    36
 * standard.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    37
 * These algorithms implement PBE with HmacSHA1/HmacSHA2-family and AES-CBC.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    38
 * Padding is done as described in PKCS #5.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    39
 *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    40
 * @author Jan Luehe
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    41
 *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    42
 *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    43
 * @see javax.crypto.Cipher
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    44
 */
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    45
abstract class PBES2Core extends CipherSpi {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    46
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    47
    private static final int DEFAULT_SALT_LENGTH = 20;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    48
    private static final int DEFAULT_COUNT = 4096;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    49
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    50
    // the encapsulated cipher
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    51
    private final CipherCore cipher;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    52
    private final int keyLength; // in bits
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    53
    private final int blkSize; // in bits
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    54
    private final PBKDF2Core kdf;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    55
    private final String pbeAlgo;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    56
    private final String cipherAlgo;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    57
    private int iCount = DEFAULT_COUNT;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    58
    private byte[] salt = null;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    59
    private IvParameterSpec ivSpec = null;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    60
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    61
    /**
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    62
     * Creates an instance of PBE Scheme 2 according to the selected
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    63
     * password-based key derivation function and encryption scheme.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    64
     */
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    65
    PBES2Core(String kdfAlgo, String cipherAlgo, int keySize)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    66
        throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    67
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    68
        this.cipherAlgo = cipherAlgo;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    69
        keyLength = keySize * 8;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    70
        pbeAlgo = "PBEWith" + kdfAlgo + "And" + cipherAlgo + "_" + keyLength;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    71
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    72
        if (cipherAlgo.equals("AES")) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    73
            blkSize = AESConstants.AES_BLOCK_SIZE;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    74
            cipher = new CipherCore(new AESCrypt(), blkSize);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    75
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    76
            switch(kdfAlgo) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    77
            case "HmacSHA1":
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    78
                kdf = new PBKDF2Core.HmacSHA1();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    79
                break;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    80
            case "HmacSHA224":
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    81
                kdf = new PBKDF2Core.HmacSHA224();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    82
                break;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    83
            case "HmacSHA256":
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    84
                kdf = new PBKDF2Core.HmacSHA256();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    85
                break;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    86
            case "HmacSHA384":
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    87
                kdf = new PBKDF2Core.HmacSHA384();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    88
                break;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    89
            case "HmacSHA512":
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    90
                kdf = new PBKDF2Core.HmacSHA512();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    91
                break;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    92
            default:
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    93
                throw new NoSuchAlgorithmException(
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    94
                    "No Cipher implementation for " + kdfAlgo);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    95
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    96
        } else {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    97
            throw new NoSuchAlgorithmException("No Cipher implementation for " +
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    98
                                               pbeAlgo);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    99
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   100
        cipher.setMode("CBC");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   101
        cipher.setPadding("PKCS5Padding");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   102
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   103
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   104
    protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   105
        if ((mode != null) && (!mode.equalsIgnoreCase("CBC"))) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   106
            throw new NoSuchAlgorithmException("Invalid cipher mode: " + mode);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   107
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   108
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   109
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   110
    protected void engineSetPadding(String paddingScheme)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   111
        throws NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   112
        if ((paddingScheme != null) &&
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   113
            (!paddingScheme.equalsIgnoreCase("PKCS5Padding"))) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   114
            throw new NoSuchPaddingException("Invalid padding scheme: " +
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   115
                                             paddingScheme);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   116
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   117
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   118
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   119
    protected int engineGetBlockSize() {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   120
        return blkSize;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   121
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   122
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   123
    protected int engineGetOutputSize(int inputLen) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   124
        return cipher.getOutputSize(inputLen);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   125
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   126
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   127
    protected byte[] engineGetIV() {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   128
        return cipher.getIV();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   129
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   130
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   131
    protected AlgorithmParameters engineGetParameters() {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   132
        AlgorithmParameters params = null;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   133
        if (salt == null) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   134
            // generate random salt and use default iteration count
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   135
            salt = new byte[DEFAULT_SALT_LENGTH];
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 14405
diff changeset
   136
            SunJCE.getRandom().nextBytes(salt);
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   137
            iCount = DEFAULT_COUNT;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   138
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   139
        if (ivSpec == null) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   140
            // generate random IV
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   141
            byte[] ivBytes = new byte[blkSize];
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 14405
diff changeset
   142
            SunJCE.getRandom().nextBytes(ivBytes);
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   143
            ivSpec = new IvParameterSpec(ivBytes);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   144
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   145
        PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount, ivSpec);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   146
        try {
16909
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   147
            params = AlgorithmParameters.getInstance(pbeAlgo,
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   148
                SunJCE.getInstance());
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   149
            params.init(pbeSpec);
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   150
        } catch (NoSuchAlgorithmException nsae) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   151
            // should never happen
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   152
            throw new RuntimeException("SunJCE called, but not configured");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   153
        } catch (InvalidParameterSpecException ipse) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   154
            // should never happen
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   155
            throw new RuntimeException("PBEParameterSpec not supported");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   156
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   157
        return params;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   158
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   159
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   160
    protected void engineInit(int opmode, Key key, SecureRandom random)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   161
        throws InvalidKeyException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   162
        try {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   163
            engineInit(opmode, key, (AlgorithmParameterSpec) null, random);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   164
        } catch (InvalidAlgorithmParameterException ie) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   165
            InvalidKeyException ike =
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   166
                new InvalidKeyException("requires PBE parameters");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   167
            ike.initCause(ie);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   168
            throw ike;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   169
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   170
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   171
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   172
    protected void engineInit(int opmode, Key key,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   173
                              AlgorithmParameterSpec params,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   174
                              SecureRandom random)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   175
        throws InvalidKeyException, InvalidAlgorithmParameterException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   176
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   177
        if (key == null) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   178
            throw new InvalidKeyException("Null key");
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   179
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   180
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   181
        byte[] passwdBytes = key.getEncoded();
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   182
        char[] passwdChars = null;
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   183
        PBEKeySpec pbeSpec;
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   184
        try {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   185
            if ((passwdBytes == null) ||
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   186
                    !(key.getAlgorithm().regionMatches(true, 0, "PBE", 0, 3))) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   187
                throw new InvalidKeyException("Missing password");
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   188
            }
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   189
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   190
            // TBD: consolidate the salt, ic and IV parameter checks below
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   191
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   192
            // Extract salt and iteration count from the key, if present
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   193
            if (key instanceof javax.crypto.interfaces.PBEKey) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   194
                salt = ((javax.crypto.interfaces.PBEKey)key).getSalt();
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   195
                if (salt != null && salt.length < 8) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   196
                    throw new InvalidAlgorithmParameterException(
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   197
                            "Salt must be at least 8 bytes long");
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   198
                }
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   199
                iCount = ((javax.crypto.interfaces.PBEKey)key).getIterationCount();
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   200
                if (iCount == 0) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   201
                    iCount = DEFAULT_COUNT;
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   202
                } else if (iCount < 0) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   203
                    throw new InvalidAlgorithmParameterException(
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   204
                            "Iteration count must be a positive number");
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   205
                }
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   206
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   207
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   208
            // Extract salt, iteration count and IV from the params, if present
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   209
            if (params == null) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   210
                if (salt == null) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   211
                    // generate random salt and use default iteration count
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   212
                    salt = new byte[DEFAULT_SALT_LENGTH];
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   213
                    random.nextBytes(salt);
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   214
                    iCount = DEFAULT_COUNT;
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   215
                }
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   216
                if ((opmode == Cipher.ENCRYPT_MODE) ||
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   217
                        (opmode == Cipher.WRAP_MODE)) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   218
                    // generate random IV
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   219
                    byte[] ivBytes = new byte[blkSize];
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   220
                    random.nextBytes(ivBytes);
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   221
                    ivSpec = new IvParameterSpec(ivBytes);
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   222
                }
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   223
            } else {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   224
                if (!(params instanceof PBEParameterSpec)) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   225
                    throw new InvalidAlgorithmParameterException
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   226
                            ("Wrong parameter type: PBE expected");
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   227
                }
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   228
                // salt and iteration count from the params take precedence
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   229
                byte[] specSalt = ((PBEParameterSpec) params).getSalt();
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   230
                if (specSalt != null && specSalt.length < 8) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   231
                    throw new InvalidAlgorithmParameterException(
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   232
                            "Salt must be at least 8 bytes long");
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   233
                }
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   234
                salt = specSalt;
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   235
                int specICount = ((PBEParameterSpec) params).getIterationCount();
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   236
                if (specICount == 0) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   237
                    specICount = DEFAULT_COUNT;
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   238
                } else if (specICount < 0) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   239
                    throw new InvalidAlgorithmParameterException(
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   240
                            "Iteration count must be a positive number");
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   241
                }
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   242
                iCount = specICount;
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   243
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   244
                AlgorithmParameterSpec specParams =
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   245
                        ((PBEParameterSpec) params).getParameterSpec();
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   246
                if (specParams != null) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   247
                    if (specParams instanceof IvParameterSpec) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   248
                        ivSpec = (IvParameterSpec)specParams;
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   249
                    } else {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   250
                        throw new InvalidAlgorithmParameterException(
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   251
                                "Wrong parameter type: IV expected");
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   252
                    }
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   253
                } else if ((opmode == Cipher.ENCRYPT_MODE) ||
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   254
                        (opmode == Cipher.WRAP_MODE)) {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   255
                    // generate random IV
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   256
                    byte[] ivBytes = new byte[blkSize];
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   257
                    random.nextBytes(ivBytes);
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   258
                    ivSpec = new IvParameterSpec(ivBytes);
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   259
                } else {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   260
                    throw new InvalidAlgorithmParameterException(
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   261
                            "Missing parameter type: IV expected");
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   262
                }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   263
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   264
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   265
            passwdChars = new char[passwdBytes.length];
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   266
            for (int i = 0; i < passwdChars.length; i++)
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   267
                passwdChars[i] = (char) (passwdBytes[i] & 0x7f);
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   268
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   269
            pbeSpec = new PBEKeySpec(passwdChars, salt, iCount, keyLength);
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   270
            // password char[] was cloned in PBEKeySpec constructor,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   271
            // so we can zero it out here
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   272
        } finally {
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   273
            if (passwdChars != null) Arrays.fill(passwdChars, '\0');
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   274
            if (passwdBytes != null) Arrays.fill(passwdBytes, (byte)0x00);
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   275
        }
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   276
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   277
        SecretKey s = null;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   278
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   279
        try {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   280
            s = kdf.engineGenerateSecret(pbeSpec);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   281
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   282
        } catch (InvalidKeySpecException ikse) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   283
            InvalidKeyException ike =
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   284
                new InvalidKeyException("Cannot construct PBE key");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   285
            ike.initCause(ikse);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   286
            throw ike;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   287
        }
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   288
        byte[] derivedKey = s.getEncoded();
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   289
        SecretKeySpec cipherKey = new SecretKeySpec(derivedKey, cipherAlgo);
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   290
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   291
        // initialize the underlying cipher
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   292
        cipher.init(opmode, cipherKey, ivSpec, random);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   293
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   294
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   295
    protected void engineInit(int opmode, Key key, AlgorithmParameters params,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   296
                              SecureRandom random)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   297
        throws InvalidKeyException, InvalidAlgorithmParameterException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   298
        AlgorithmParameterSpec pbeSpec = null;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   299
        if (params != null) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   300
            try {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   301
                pbeSpec = params.getParameterSpec(PBEParameterSpec.class);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   302
            } catch (InvalidParameterSpecException ipse) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   303
                throw new InvalidAlgorithmParameterException(
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   304
                    "Wrong parameter type: PBE expected");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   305
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   306
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   307
        engineInit(opmode, key, pbeSpec, random);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   308
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   309
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   310
    protected byte[] engineUpdate(byte[] input, int inputOffset, int inputLen) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   311
        return cipher.update(input, inputOffset, inputLen);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   312
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   313
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   314
    protected int engineUpdate(byte[] input, int inputOffset, int inputLen,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   315
                               byte[] output, int outputOffset)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   316
        throws ShortBufferException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   317
        return cipher.update(input, inputOffset, inputLen,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   318
                             output, outputOffset);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   319
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   320
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   321
    protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   322
        throws IllegalBlockSizeException, BadPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   323
        return cipher.doFinal(input, inputOffset, inputLen);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   324
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   325
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   326
    protected int engineDoFinal(byte[] input, int inputOffset, int inputLen,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   327
                                byte[] output, int outputOffset)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   328
        throws ShortBufferException, IllegalBlockSizeException,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   329
               BadPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   330
        return cipher.doFinal(input, inputOffset, inputLen,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   331
                              output, outputOffset);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   332
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   333
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   334
    protected int engineGetKeySize(Key key) throws InvalidKeyException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   335
        return keyLength;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   336
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   337
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   338
    protected byte[] engineWrap(Key key)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   339
        throws IllegalBlockSizeException, InvalidKeyException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   340
        return cipher.wrap(key);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   341
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   342
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   343
    protected Key engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   344
                               int wrappedKeyType)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   345
        throws InvalidKeyException, NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   346
        byte[] encodedKey;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   347
        return cipher.unwrap(wrappedKey, wrappedKeyAlgorithm,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   348
                             wrappedKeyType);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   349
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   350
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   351
    public static final class HmacSHA1AndAES_128 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   352
        public HmacSHA1AndAES_128()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   353
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   354
            super("HmacSHA1", "AES", 16);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   355
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   356
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   357
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   358
    public static final class HmacSHA224AndAES_128 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   359
        public HmacSHA224AndAES_128()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   360
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   361
            super("HmacSHA224", "AES", 16);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   362
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   363
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   364
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   365
    public static final class HmacSHA256AndAES_128 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   366
        public HmacSHA256AndAES_128()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   367
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   368
            super("HmacSHA256", "AES", 16);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   369
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   370
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   371
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   372
    public static final class HmacSHA384AndAES_128 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   373
        public HmacSHA384AndAES_128()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   374
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   375
            super("HmacSHA384", "AES", 16);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   376
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   377
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   378
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   379
    public static final class HmacSHA512AndAES_128 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   380
        public HmacSHA512AndAES_128()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   381
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   382
            super("HmacSHA512", "AES", 16);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   383
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   384
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   385
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   386
    public static final class HmacSHA1AndAES_256 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   387
        public HmacSHA1AndAES_256()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   388
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   389
            super("HmacSHA1", "AES", 32);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   390
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   391
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   392
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   393
    public static final class HmacSHA224AndAES_256 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   394
        public HmacSHA224AndAES_256()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   395
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   396
            super("HmacSHA224", "AES", 32);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   397
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   398
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   399
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   400
    public static final class HmacSHA256AndAES_256 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   401
        public HmacSHA256AndAES_256()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   402
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   403
            super("HmacSHA256", "AES", 32);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   404
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   405
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   406
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   407
    public static final class HmacSHA384AndAES_256 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   408
        public HmacSHA384AndAES_256()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   409
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   410
            super("HmacSHA384", "AES", 32);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   411
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   412
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   413
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   414
    public static final class HmacSHA512AndAES_256 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   415
        public HmacSHA512AndAES_256()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   416
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   417
            super("HmacSHA512", "AES", 32);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   418
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   419
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   420
}