src/java.base/share/classes/com/sun/crypto/provider/ARCFOURCipher.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/java.base/share/classes/com/sun/crypto/provider/ARCFOURCipher.java@3317bb8137f4
child 47407 2f79180e86e9
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Implementation of the ARCFOUR cipher, an algorithm apparently compatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * with RSA Security's RC4(tm) cipher. The description of this algorithm was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * taken from Bruce Schneier's book Applied Cryptography, 2nd ed.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * section 17.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * We support keys from 40 to 1024 bits. ARCFOUR would allow for keys shorter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * than 40 bits, but that is too insecure for us to permit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Note that we subclass CipherSpi directly and do not use the CipherCore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * framework. That was designed to simplify implementation of block ciphers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * and does not offer any advantages for stream ciphers such as ARCFOUR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public final class ARCFOURCipher extends CipherSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // state array S, 256 entries. The entries are 8-bit, but we use an int[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // because int arithmetic is much faster than in Java than bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private final int[] S;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // state indices i and j. Called is and js to avoid collision with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // local variables. 'is' is set to -1 after a call to doFinal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private int is, js;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // the bytes of the last key used (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // we need this to re-initialize after a call to doFinal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private byte[] lastKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // called by the JCE framework
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public ARCFOURCipher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        S = new int[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // core key setup code. initializes S, is, and js
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // assumes key is non-null and between 40 and 1024 bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private void init(byte[] key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // initialize S[i] to i
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        for (int i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            S[i] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // we avoid expanding key to 256 bytes and instead keep a separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        // counter ki = i mod key.length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        for (int i = 0, j = 0, ki = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            int Si = S[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            j = (j + Si + key[ki]) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            S[i] = S[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            S[j] = Si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            ki++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (ki == key.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                ki = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // set indices to 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        is = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        js = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // core crypt code. OFB style, so works for both encryption and decryption
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    95
    private void crypt(byte[] in, int inOfs, int inLen, byte[] out,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    96
            int outOfs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (is < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            // doFinal() was called, need to reset the cipher to initial state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            init(lastKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        while (inLen-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            is = (is + 1) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            int Si = S[is];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            js = (js + Si) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            int Sj = S[js];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            S[is] = Sj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            S[js] = Si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            out[outOfs++] = (byte)(in[inOfs++] ^ S[(Si + Sj) & 0xff]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // Modes do not make sense with stream ciphers, but allow ECB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // see JCE spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (mode.equalsIgnoreCase("ECB") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new NoSuchAlgorithmException("Unsupported mode " + mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // Padding does not make sense with stream ciphers, but allow NoPadding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    // see JCE spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    protected void engineSetPadding(String padding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throws NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (padding.equalsIgnoreCase("NoPadding") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new NoSuchPaddingException("Padding must be NoPadding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    // Return 0 to indicate stream cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    // see JCE spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    protected int engineGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // output length is always the same as input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    protected int engineGetOutputSize(int inputLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return inputLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // no IV, return null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    protected byte[] engineGetIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    // no parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    protected AlgorithmParameters engineGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    protected void engineInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        init(opmode, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                ("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        init(opmode, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            AlgorithmParameters params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                ("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        init(opmode, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    // init method. Check opmode and key, then call init(byte[]).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private void init(int opmode, Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if ((opmode < Cipher.ENCRYPT_MODE) || (opmode > Cipher.UNWRAP_MODE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw new InvalidKeyException("Unknown opmode: " + opmode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        lastKey = getEncodedKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        init(lastKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    // return the encoding of key if key is a valid ARCFOUR key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    // otherwise, throw an InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private static byte[] getEncodedKey(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        String keyAlg = key.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (!keyAlg.equals("RC4") && !keyAlg.equals("ARCFOUR")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            throw new InvalidKeyException("Not an ARCFOUR key: " + keyAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if ("RAW".equals(key.getFormat()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throw new InvalidKeyException("Key encoding format must be RAW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        byte[] encodedKey = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if ((encodedKey.length < 5) || (encodedKey.length > 128)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                ("Key length must be between 40 and 1024 bit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return encodedKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        byte[] out = new byte[inLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        crypt(in, inOfs, inLen, out, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    protected int engineUpdate(byte[] in, int inOfs, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            byte[] out, int outOfs) throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (out.length - outOfs < inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new ShortBufferException("Output buffer too small");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        crypt(in, inOfs, inLen, out, outOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return inLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    protected byte[] engineDoFinal(byte[] in, int inOfs, int inLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        byte[] out = engineUpdate(in, inOfs, inLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        is = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    protected int engineDoFinal(byte[] in, int inOfs, int inLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            byte[] out, int outOfs) throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        int outLen = engineUpdate(in, inOfs, inLen, out, outOfs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        is = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return outLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    protected byte[] engineWrap(Key key) throws IllegalBlockSizeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if ((encoded == null) || (encoded.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throw new InvalidKeyException("Could not obtain encoded key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return engineDoFinal(encoded, 0, encoded.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    protected Key engineUnwrap(byte[] wrappedKey, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            int type) throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        byte[] encoded = engineDoFinal(wrappedKey, 0, wrappedKey.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return ConstructKeys.constructKey(encoded, algorithm, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    protected int engineGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        byte[] encodedKey = getEncodedKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return encodedKey.length << 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
}