jdk/src/share/classes/com/sun/crypto/provider/PBES2Core.java
author valeriep
Tue, 08 Jan 2013 11:55:21 -0800
changeset 15010 ec6b49ce42b1
parent 14405 e7fff80005c1
child 16909 78a1749a43e2
permissions -rw-r--r--
8004044: Lazily instantiate SunJCE.RANDOM Summary: Replace the static initialization of SunJCE.RANDOM object w/ lazy initialization Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
     1
/*
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 14405
diff changeset
     2
 * Copyright (c) 2012, 2013, 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.io.UnsupportedEncodingException;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    29
import java.security.*;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    30
import java.security.spec.*;
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.interfaces.*;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    33
import javax.crypto.spec.*;
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
/**
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    36
 * 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
    37
 * standard.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    38
 * 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
    39
 * Padding is done as described in PKCS #5.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    40
 *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    41
 * @author Jan Luehe
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
 *
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    44
 * @see javax.crypto.Cipher
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    45
 */
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    46
abstract class PBES2Core extends CipherSpi {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    47
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    48
    private static final int DEFAULT_SALT_LENGTH = 20;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    49
    private static final int DEFAULT_COUNT = 4096;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    50
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    51
    // the encapsulated cipher
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    52
    private final CipherCore cipher;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    53
    private final int keyLength; // in bits
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    54
    private final int blkSize; // in bits
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    55
    private final PBKDF2Core kdf;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    56
    private final String pbeAlgo;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    57
    private final String cipherAlgo;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    58
    private int iCount = DEFAULT_COUNT;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    59
    private byte[] salt = null;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    60
    private IvParameterSpec ivSpec = null;
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
    /**
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    63
     * 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
    64
     * password-based key derivation function and encryption scheme.
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    65
     */
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    66
    PBES2Core(String kdfAlgo, String cipherAlgo, int keySize)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    67
        throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    68
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    69
        this.cipherAlgo = cipherAlgo;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    70
        keyLength = keySize * 8;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    71
        pbeAlgo = "PBEWith" + kdfAlgo + "And" + cipherAlgo + "_" + keyLength;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    72
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    73
        if (cipherAlgo.equals("AES")) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    74
            blkSize = AESConstants.AES_BLOCK_SIZE;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    75
            cipher = new CipherCore(new AESCrypt(), blkSize);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    76
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    77
            switch(kdfAlgo) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    78
            case "HmacSHA1":
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    79
                kdf = new PBKDF2Core.HmacSHA1();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    80
                break;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    81
            case "HmacSHA224":
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    82
                kdf = new PBKDF2Core.HmacSHA224();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    83
                break;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    84
            case "HmacSHA256":
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    85
                kdf = new PBKDF2Core.HmacSHA256();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    86
                break;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    87
            case "HmacSHA384":
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    88
                kdf = new PBKDF2Core.HmacSHA384();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    89
                break;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    90
            case "HmacSHA512":
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    91
                kdf = new PBKDF2Core.HmacSHA512();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    92
                break;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    93
            default:
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    94
                throw new NoSuchAlgorithmException(
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    95
                    "No Cipher implementation for " + kdfAlgo);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    96
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    97
        } else {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    98
            throw new NoSuchAlgorithmException("No Cipher implementation for " +
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
    99
                                               pbeAlgo);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   100
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   101
        cipher.setMode("CBC");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   102
        cipher.setPadding("PKCS5Padding");
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   105
    protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   106
        if ((mode != null) && (!mode.equalsIgnoreCase("CBC"))) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   107
            throw new NoSuchAlgorithmException("Invalid cipher mode: " + mode);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   111
    protected void engineSetPadding(String paddingScheme)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   112
        throws NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   113
        if ((paddingScheme != null) &&
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   114
            (!paddingScheme.equalsIgnoreCase("PKCS5Padding"))) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   115
            throw new NoSuchPaddingException("Invalid padding scheme: " +
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   116
                                             paddingScheme);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   120
    protected int engineGetBlockSize() {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   121
        return blkSize;
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   124
    protected int engineGetOutputSize(int inputLen) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   125
        return cipher.getOutputSize(inputLen);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   128
    protected byte[] engineGetIV() {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   129
        return cipher.getIV();
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   132
    protected AlgorithmParameters engineGetParameters() {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   133
        AlgorithmParameters params = null;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   134
        if (salt == null) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   135
            // generate random salt and use default iteration count
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   136
            salt = new byte[DEFAULT_SALT_LENGTH];
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 14405
diff changeset
   137
            SunJCE.getRandom().nextBytes(salt);
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   138
            iCount = DEFAULT_COUNT;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   139
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   140
        if (ivSpec == null) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   141
            // generate random IV
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   142
            byte[] ivBytes = new byte[blkSize];
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 14405
diff changeset
   143
            SunJCE.getRandom().nextBytes(ivBytes);
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   144
            ivSpec = new IvParameterSpec(ivBytes);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   145
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   146
        PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount, ivSpec);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   147
        try {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   148
            params = AlgorithmParameters.getInstance(pbeAlgo, "SunJCE");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   149
        } catch (NoSuchAlgorithmException nsae) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   150
            // should never happen
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   151
            throw new RuntimeException("SunJCE called, but not configured");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   152
        } catch (NoSuchProviderException nspe) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   153
            // should never happen
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   154
            throw new RuntimeException("SunJCE called, but not configured");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   155
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   156
        try {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   157
            params.init(pbeSpec);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   158
        } catch (InvalidParameterSpecException ipse) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   159
            // should never happen
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   160
            throw new RuntimeException("PBEParameterSpec not supported");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   161
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   162
        return params;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   163
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   164
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   165
    protected void engineInit(int opmode, Key key, SecureRandom random)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   166
        throws InvalidKeyException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   167
        try {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   168
            engineInit(opmode, key, (AlgorithmParameterSpec) null, random);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   169
        } catch (InvalidAlgorithmParameterException ie) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   170
            InvalidKeyException ike =
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   171
                new InvalidKeyException("requires PBE parameters");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   172
            ike.initCause(ie);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   173
            throw ike;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   174
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   175
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   176
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   177
    protected void engineInit(int opmode, Key key,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   178
                              AlgorithmParameterSpec params,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   179
                              SecureRandom random)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   180
        throws InvalidKeyException, InvalidAlgorithmParameterException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   181
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   182
        if ((key == null) ||
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   183
            (key.getEncoded() == null) ||
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   184
            !(key.getAlgorithm().regionMatches(true, 0, "PBE", 0, 3))) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   185
            throw new InvalidKeyException("Missing password");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   186
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   187
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   188
        // TBD: consolidate the salt, ic and IV parameter checks below
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   189
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   190
        // Extract salt and iteration count from the key, if present
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   191
        if (key instanceof javax.crypto.interfaces.PBEKey) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   192
            salt = ((javax.crypto.interfaces.PBEKey)key).getSalt();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   193
            if (salt != null && salt.length < 8) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   194
                throw new InvalidAlgorithmParameterException(
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   195
                    "Salt must be at least 8 bytes long");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   196
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   197
            iCount = ((javax.crypto.interfaces.PBEKey)key).getIterationCount();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   198
            if (iCount == 0) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   199
                iCount = DEFAULT_COUNT;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   200
            } else if (iCount < 0) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   201
                throw new InvalidAlgorithmParameterException(
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   202
                    "Iteration count must be a positive number");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   203
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   204
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   205
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   206
        // Extract salt, iteration count and IV from the params, if present
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   207
        if (params == null) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   208
            if (salt == null) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   209
                // generate random salt and use default iteration count
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   210
                salt = new byte[DEFAULT_SALT_LENGTH];
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   211
                random.nextBytes(salt);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   212
                iCount = DEFAULT_COUNT;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   213
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   214
            if ((opmode == Cipher.ENCRYPT_MODE) ||
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   215
                        (opmode == Cipher.WRAP_MODE)) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   216
                // generate random IV
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   217
                byte[] ivBytes = new byte[blkSize];
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   218
                random.nextBytes(ivBytes);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   219
                ivSpec = new IvParameterSpec(ivBytes);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   220
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   221
        } else {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   222
            if (!(params instanceof PBEParameterSpec)) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   223
                throw new InvalidAlgorithmParameterException
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   224
                    ("Wrong parameter type: PBE expected");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   225
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   226
            // salt and iteration count from the params take precedence
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   227
            byte[] specSalt = ((PBEParameterSpec) params).getSalt();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   228
            if (specSalt != null && specSalt.length < 8) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   229
                throw new InvalidAlgorithmParameterException(
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   230
                    "Salt must be at least 8 bytes long");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   231
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   232
            salt = specSalt;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   233
            int specICount = ((PBEParameterSpec) params).getIterationCount();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   234
            if (specICount == 0) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   235
                specICount = DEFAULT_COUNT;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   236
            } else if (specICount < 0) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   237
                throw new InvalidAlgorithmParameterException(
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   238
                    "Iteration count must be a positive number");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   239
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   240
            iCount = specICount;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   241
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   242
            AlgorithmParameterSpec specParams =
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   243
                ((PBEParameterSpec) params).getParameterSpec();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   244
            if (specParams != null) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   245
                if (specParams instanceof IvParameterSpec) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   246
                    ivSpec = (IvParameterSpec)specParams;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   247
                } else {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   248
                    throw new InvalidAlgorithmParameterException(
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   249
                        "Wrong parameter type: IV expected");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   250
                }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   251
            } else if ((opmode == Cipher.ENCRYPT_MODE) ||
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   252
                        (opmode == Cipher.WRAP_MODE)) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   253
                // generate random IV
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   254
                byte[] ivBytes = new byte[blkSize];
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   255
                random.nextBytes(ivBytes);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   256
                ivSpec = new IvParameterSpec(ivBytes);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   257
            } else {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   258
                throw new InvalidAlgorithmParameterException(
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   259
                    "Missing parameter type: IV expected");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   260
            }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   261
        }
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
        SecretKeySpec cipherKey = null;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   264
        byte[] derivedKey = null;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   265
        byte[] passwdBytes = key.getEncoded();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   266
        char[] passwdChars = new char[passwdBytes.length];
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   267
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   268
        for (int i=0; i<passwdChars.length; i++)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   269
            passwdChars[i] = (char) (passwdBytes[i] & 0x7f);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   270
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   271
        PBEKeySpec pbeSpec =
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   272
            new PBEKeySpec(passwdChars, salt, iCount, blkSize * 8);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   273
            // password char[] was cloned in PBEKeySpec constructor,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   274
            // so we can zero it out here
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   275
        java.util.Arrays.fill(passwdChars, ' ');
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   276
        java.util.Arrays.fill(passwdBytes, (byte)0x00);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   277
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   278
        SecretKey s = null;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   279
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   280
        try {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   281
            s = kdf.engineGenerateSecret(pbeSpec);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   282
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   283
        } catch (InvalidKeySpecException ikse) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   284
            InvalidKeyException ike =
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   285
                new InvalidKeyException("Cannot construct PBE key");
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   286
            ike.initCause(ikse);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   287
            throw ike;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   288
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   289
        derivedKey = s.getEncoded();
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   290
        cipherKey = new SecretKeySpec(derivedKey, cipherAlgo);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   291
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   292
        // initialize the underlying cipher
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   293
        cipher.init(opmode, cipherKey, ivSpec, random);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   296
    protected void engineInit(int opmode, Key key, AlgorithmParameters params,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   297
                              SecureRandom random)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   298
        throws InvalidKeyException, InvalidAlgorithmParameterException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   299
        AlgorithmParameterSpec pbeSpec = null;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   300
        if (params != null) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   301
            try {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   302
                pbeSpec = params.getParameterSpec(PBEParameterSpec.class);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   303
            } catch (InvalidParameterSpecException ipse) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   304
                throw new InvalidAlgorithmParameterException(
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   305
                    "Wrong parameter type: PBE expected");
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
        }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   308
        engineInit(opmode, key, pbeSpec, random);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   311
    protected byte[] engineUpdate(byte[] input, int inputOffset, int inputLen) {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   312
        return cipher.update(input, inputOffset, inputLen);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   315
    protected int engineUpdate(byte[] input, int inputOffset, int inputLen,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   316
                               byte[] output, int outputOffset)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   317
        throws ShortBufferException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   318
        return cipher.update(input, inputOffset, inputLen,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   319
                             output, outputOffset);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   322
    protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   323
        throws IllegalBlockSizeException, BadPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   324
        return cipher.doFinal(input, inputOffset, inputLen);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   327
    protected int engineDoFinal(byte[] input, int inputOffset, int inputLen,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   328
                                byte[] output, int outputOffset)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   329
        throws ShortBufferException, IllegalBlockSizeException,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   330
               BadPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   331
        return cipher.doFinal(input, inputOffset, inputLen,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   332
                              output, outputOffset);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   335
    protected int engineGetKeySize(Key key) throws InvalidKeyException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   336
        return keyLength;
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   339
    protected byte[] engineWrap(Key key)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   340
        throws IllegalBlockSizeException, InvalidKeyException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   341
        return cipher.wrap(key);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   344
    protected Key engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   345
                               int wrappedKeyType)
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   346
        throws InvalidKeyException, NoSuchAlgorithmException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   347
        byte[] encodedKey;
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   348
        return cipher.unwrap(wrappedKey, wrappedKeyAlgorithm,
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   349
                             wrappedKeyType);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   352
    public static final class HmacSHA1AndAES_128 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   353
        public HmacSHA1AndAES_128()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   354
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   355
            super("HmacSHA1", "AES", 16);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   359
    public static final class HmacSHA224AndAES_128 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   360
        public HmacSHA224AndAES_128()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   361
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   362
            super("HmacSHA224", "AES", 16);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   366
    public static final class HmacSHA256AndAES_128 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   367
        public HmacSHA256AndAES_128()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   368
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   369
            super("HmacSHA256", "AES", 16);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   373
    public static final class HmacSHA384AndAES_128 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   374
        public HmacSHA384AndAES_128()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   375
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   376
            super("HmacSHA384", "AES", 16);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   380
    public static final class HmacSHA512AndAES_128 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   381
        public HmacSHA512AndAES_128()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   382
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   383
            super("HmacSHA512", "AES", 16);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   387
    public static final class HmacSHA1AndAES_256 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   388
        public HmacSHA1AndAES_256()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   389
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   390
            super("HmacSHA1", "AES", 32);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   394
    public static final class HmacSHA224AndAES_256 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   395
        public HmacSHA224AndAES_256()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   396
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   397
            super("HmacSHA224", "AES", 32);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   401
    public static final class HmacSHA256AndAES_256 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   402
        public HmacSHA256AndAES_256()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   403
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   404
            super("HmacSHA256", "AES", 32);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   408
    public static final class HmacSHA384AndAES_256 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   409
        public HmacSHA384AndAES_256()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   410
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   411
            super("HmacSHA384", "AES", 32);
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
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   415
    public static final class HmacSHA512AndAES_256 extends PBES2Core {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   416
        public HmacSHA512AndAES_256()
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   417
            throws NoSuchAlgorithmException, NoSuchPaddingException {
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   418
            super("HmacSHA512", "AES", 32);
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
    }
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
diff changeset
   421
}