0N/A/*
3002N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.crypto.provider;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.spec.AlgorithmParameterSpec;
0N/A
0N/Aimport javax.crypto.*;
0N/A
0N/A/**
0N/A * Implementation of the ARCFOUR cipher, an algorithm apparently compatible
0N/A * with RSA Security's RC4(tm) cipher. The description of this algorithm was
0N/A * taken from Bruce Schneier's book Applied Cryptography, 2nd ed.,
0N/A * section 17.1.
0N/A *
0N/A * We support keys from 40 to 1024 bits. ARCFOUR would allow for keys shorter
0N/A * than 40 bits, but that is too insecure for us to permit.
0N/A *
0N/A * Note that we subclass CipherSpi directly and do not use the CipherCore
0N/A * framework. That was designed to simplify implementation of block ciphers
0N/A * and does not offer any advantages for stream ciphers such as ARCFOUR.
0N/A *
0N/A * @since 1.5
0N/A * @author Andreas Sterbenz
0N/A */
0N/Apublic final class ARCFOURCipher extends CipherSpi {
0N/A
0N/A // state array S, 256 entries. The entries are 8-bit, but we use an int[]
0N/A // because int arithmetic is much faster than in Java than bytes.
0N/A private final int[] S;
0N/A
0N/A // state indices i and j. Called is and js to avoid collision with
0N/A // local variables. 'is' is set to -1 after a call to doFinal()
0N/A private int is, js;
0N/A
0N/A // the bytes of the last key used (if any)
0N/A // we need this to re-initialize after a call to doFinal()
0N/A private byte[] lastKey;
0N/A
0N/A // called by the JCE framework
0N/A public ARCFOURCipher() {
0N/A S = new int[256];
0N/A }
0N/A
0N/A // core key setup code. initializes S, is, and js
0N/A // assumes key is non-null and between 40 and 1024 bit
0N/A private void init(byte[] key) {
0N/A // initialize S[i] to i
0N/A for (int i = 0; i < 256; i++) {
0N/A S[i] = i;
0N/A }
0N/A
0N/A // we avoid expanding key to 256 bytes and instead keep a separate
0N/A // counter ki = i mod key.length.
0N/A for (int i = 0, j = 0, ki = 0; i < 256; i++) {
0N/A int Si = S[i];
0N/A j = (j + Si + key[ki]) & 0xff;
0N/A S[i] = S[j];
0N/A S[j] = Si;
0N/A ki++;
0N/A if (ki == key.length) {
0N/A ki = 0;
0N/A }
0N/A }
0N/A
0N/A // set indices to 0
0N/A is = 0;
0N/A js = 0;
0N/A }
0N/A
0N/A // core crypt code. OFB style, so works for both encryption and decryption
3002N/A private void crypt(byte[] in, int inOfs, int inLen, byte[] out,
3002N/A int outOfs) {
0N/A if (is < 0) {
0N/A // doFinal() was called, need to reset the cipher to initial state
0N/A init(lastKey);
0N/A }
0N/A while (inLen-- > 0) {
0N/A is = (is + 1) & 0xff;
0N/A int Si = S[is];
0N/A js = (js + Si) & 0xff;
0N/A int Sj = S[js];
0N/A S[is] = Sj;
0N/A S[js] = Si;
0N/A out[outOfs++] = (byte)(in[inOfs++] ^ S[(Si + Sj) & 0xff]);
0N/A }
0N/A }
0N/A
0N/A // Modes do not make sense with stream ciphers, but allow ECB
0N/A // see JCE spec.
0N/A protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
0N/A if (mode.equalsIgnoreCase("ECB") == false) {
0N/A throw new NoSuchAlgorithmException("Unsupported mode " + mode);
0N/A }
0N/A }
0N/A
0N/A // Padding does not make sense with stream ciphers, but allow NoPadding
0N/A // see JCE spec.
0N/A protected void engineSetPadding(String padding)
0N/A throws NoSuchPaddingException {
0N/A if (padding.equalsIgnoreCase("NoPadding") == false) {
0N/A throw new NoSuchPaddingException("Padding must be NoPadding");
0N/A }
0N/A }
0N/A
0N/A // Return 0 to indicate stream cipher
0N/A // see JCE spec.
0N/A protected int engineGetBlockSize() {
0N/A return 0;
0N/A }
0N/A
0N/A // output length is always the same as input length
0N/A // see JCE spec
0N/A protected int engineGetOutputSize(int inputLen) {
0N/A return inputLen;
0N/A }
0N/A
0N/A // no IV, return null
0N/A // see JCE spec
0N/A protected byte[] engineGetIV() {
0N/A return null;
0N/A }
0N/A
0N/A // no parameters
0N/A // see JCE spec
0N/A protected AlgorithmParameters engineGetParameters() {
0N/A return null;
0N/A }
0N/A
0N/A // see JCE spec
0N/A protected void engineInit(int opmode, Key key, SecureRandom random)
0N/A throws InvalidKeyException {
0N/A init(opmode, key);
0N/A }
0N/A
0N/A // see JCE spec
0N/A protected void engineInit(int opmode, Key key,
0N/A AlgorithmParameterSpec params, SecureRandom random)
0N/A throws InvalidKeyException, InvalidAlgorithmParameterException {
0N/A if (params != null) {
0N/A throw new InvalidAlgorithmParameterException
0N/A ("Parameters not supported");
0N/A }
0N/A init(opmode, key);
0N/A }
0N/A
0N/A // see JCE spec
0N/A protected void engineInit(int opmode, Key key,
0N/A AlgorithmParameters params, SecureRandom random)
0N/A throws InvalidKeyException, InvalidAlgorithmParameterException {
0N/A if (params != null) {
0N/A throw new InvalidAlgorithmParameterException
0N/A ("Parameters not supported");
0N/A }
0N/A init(opmode, key);
0N/A }
0N/A
0N/A // init method. Check opmode and key, then call init(byte[]).
0N/A private void init(int opmode, Key key) throws InvalidKeyException {
0N/A if ((opmode < Cipher.ENCRYPT_MODE) || (opmode > Cipher.UNWRAP_MODE)) {
0N/A throw new InvalidKeyException("Unknown opmode: " + opmode);
0N/A }
0N/A lastKey = getEncodedKey(key);
0N/A init(lastKey);
0N/A }
0N/A
0N/A // return the encoding of key if key is a valid ARCFOUR key.
0N/A // otherwise, throw an InvalidKeyException
0N/A private static byte[] getEncodedKey(Key key) throws InvalidKeyException {
0N/A String keyAlg = key.getAlgorithm();
0N/A if (!keyAlg.equals("RC4") && !keyAlg.equals("ARCFOUR")) {
0N/A throw new InvalidKeyException("Not an ARCFOUR key: " + keyAlg);
0N/A }
0N/A if ("RAW".equals(key.getFormat()) == false) {
0N/A throw new InvalidKeyException("Key encoding format must be RAW");
0N/A }
0N/A byte[] encodedKey = key.getEncoded();
0N/A if ((encodedKey.length < 5) || (encodedKey.length > 128)) {
0N/A throw new InvalidKeyException
0N/A ("Key length must be between 40 and 1024 bit");
0N/A }
0N/A return encodedKey;
0N/A }
0N/A
0N/A // see JCE spec
0N/A protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
0N/A byte[] out = new byte[inLen];
0N/A crypt(in, inOfs, inLen, out, 0);
0N/A return out;
0N/A }
0N/A
0N/A // see JCE spec
0N/A protected int engineUpdate(byte[] in, int inOfs, int inLen,
0N/A byte[] out, int outOfs) throws ShortBufferException {
0N/A if (out.length - outOfs < inLen) {
0N/A throw new ShortBufferException("Output buffer too small");
0N/A }
0N/A crypt(in, inOfs, inLen, out, outOfs);
0N/A return inLen;
0N/A }
0N/A
0N/A // see JCE spec
0N/A protected byte[] engineDoFinal(byte[] in, int inOfs, int inLen) {
0N/A byte[] out = engineUpdate(in, inOfs, inLen);
0N/A is = -1;
0N/A return out;
0N/A }
0N/A
0N/A // see JCE spec
0N/A protected int engineDoFinal(byte[] in, int inOfs, int inLen,
0N/A byte[] out, int outOfs) throws ShortBufferException {
0N/A int outLen = engineUpdate(in, inOfs, inLen, out, outOfs);
0N/A is = -1;
0N/A return outLen;
0N/A }
0N/A
0N/A // see JCE spec
0N/A protected byte[] engineWrap(Key key) throws IllegalBlockSizeException,
0N/A InvalidKeyException {
0N/A byte[] encoded = key.getEncoded();
0N/A if ((encoded == null) || (encoded.length == 0)) {
0N/A throw new InvalidKeyException("Could not obtain encoded key");
0N/A }
0N/A return engineDoFinal(encoded, 0, encoded.length);
0N/A }
0N/A
0N/A // see JCE spec
0N/A protected Key engineUnwrap(byte[] wrappedKey, String algorithm,
0N/A int type) throws InvalidKeyException, NoSuchAlgorithmException {
0N/A byte[] encoded = engineDoFinal(wrappedKey, 0, wrappedKey.length);
0N/A return ConstructKeys.constructKey(encoded, algorithm, type);
0N/A }
0N/A
0N/A // see JCE spec
0N/A protected int engineGetKeySize(Key key) throws InvalidKeyException {
0N/A byte[] encodedKey = getEncodedKey(key);
0N/A return encodedKey.length << 3;
0N/A }
0N/A
0N/A}