Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/ARCFOURCipher.java
41161 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.crypto.provider;2627import java.security.*;28import java.security.spec.AlgorithmParameterSpec;29import java.util.Arrays;3031import javax.crypto.*;3233/**34* Implementation of the ARCFOUR cipher, an algorithm apparently compatible35* with RSA Security's RC4(tm) cipher. The description of this algorithm was36* taken from Bruce Schneier's book Applied Cryptography, 2nd ed.,37* section 17.1.38*39* We support keys from 40 to 1024 bits. ARCFOUR would allow for keys shorter40* than 40 bits, but that is too insecure for us to permit.41*42* Note that we subclass CipherSpi directly and do not use the CipherCore43* framework. That was designed to simplify implementation of block ciphers44* and does not offer any advantages for stream ciphers such as ARCFOUR.45*46* @since 1.547* @author Andreas Sterbenz48*/49public final class ARCFOURCipher extends CipherSpi {5051// state array S, 256 entries. The entries are 8-bit, but we use an int[]52// because int arithmetic is much faster than in Java than bytes.53private final int[] S;5455// state indices i and j. Called is and js to avoid collision with56// local variables. 'is' is set to -1 after a call to doFinal()57private int is, js;5859// the bytes of the last key used (if any)60// we need this to re-initialize after a call to doFinal()61private byte[] lastKey;6263// called by the JCE framework64public ARCFOURCipher() {65S = new int[256];66}6768// core key setup code. initializes S, is, and js69// assumes key is non-null and between 40 and 1024 bit70private void init(byte[] key) {71// initialize S[i] to i72for (int i = 0; i < 256; i++) {73S[i] = i;74}7576// we avoid expanding key to 256 bytes and instead keep a separate77// counter ki = i mod key.length.78for (int i = 0, j = 0, ki = 0; i < 256; i++) {79int Si = S[i];80j = (j + Si + key[ki]) & 0xff;81S[i] = S[j];82S[j] = Si;83ki++;84if (ki == key.length) {85ki = 0;86}87}8889// set indices to 090is = 0;91js = 0;92}9394// core crypt code. OFB style, so works for both encryption and decryption95private void crypt(byte[] in, int inOfs, int inLen, byte[] out,96int outOfs) {97if (is < 0) {98// doFinal() was called, need to reset the cipher to initial state99init(lastKey);100}101while (inLen-- > 0) {102is = (is + 1) & 0xff;103int Si = S[is];104js = (js + Si) & 0xff;105int Sj = S[js];106S[is] = Sj;107S[js] = Si;108out[outOfs++] = (byte)(in[inOfs++] ^ S[(Si + Sj) & 0xff]);109}110}111112// Modes do not make sense with stream ciphers, but allow ECB113// see JCE spec.114protected void engineSetMode(String mode) throws NoSuchAlgorithmException {115if (mode.equalsIgnoreCase("ECB") == false) {116throw new NoSuchAlgorithmException("Unsupported mode " + mode);117}118}119120// Padding does not make sense with stream ciphers, but allow NoPadding121// see JCE spec.122protected void engineSetPadding(String padding)123throws NoSuchPaddingException {124if (padding.equalsIgnoreCase("NoPadding") == false) {125throw new NoSuchPaddingException("Padding must be NoPadding");126}127}128129// Return 0 to indicate stream cipher130// see JCE spec.131protected int engineGetBlockSize() {132return 0;133}134135// output length is always the same as input length136// see JCE spec137protected int engineGetOutputSize(int inputLen) {138return inputLen;139}140141// no IV, return null142// see JCE spec143protected byte[] engineGetIV() {144return null;145}146147// no parameters148// see JCE spec149protected AlgorithmParameters engineGetParameters() {150return null;151}152153// see JCE spec154protected void engineInit(int opmode, Key key, SecureRandom random)155throws InvalidKeyException {156init(opmode, key);157}158159// see JCE spec160protected void engineInit(int opmode, Key key,161AlgorithmParameterSpec params, SecureRandom random)162throws InvalidKeyException, InvalidAlgorithmParameterException {163if (params != null) {164throw new InvalidAlgorithmParameterException165("Parameters not supported");166}167init(opmode, key);168}169170// see JCE spec171protected void engineInit(int opmode, Key key,172AlgorithmParameters params, SecureRandom random)173throws InvalidKeyException, InvalidAlgorithmParameterException {174if (params != null) {175throw new InvalidAlgorithmParameterException176("Parameters not supported");177}178init(opmode, key);179}180181// init method. Check opmode and key, then call init(byte[]).182private void init(int opmode, Key key) throws InvalidKeyException {183if (lastKey != null) {184Arrays.fill(lastKey, (byte)0);185}186if ((opmode < Cipher.ENCRYPT_MODE) || (opmode > Cipher.UNWRAP_MODE)) {187throw new InvalidKeyException("Unknown opmode: " + opmode);188}189lastKey = getEncodedKey(key);190init(lastKey);191}192193// return the encoding of key if key is a valid ARCFOUR key.194// otherwise, throw an InvalidKeyException195private static byte[] getEncodedKey(Key key) throws InvalidKeyException {196String keyAlg = key.getAlgorithm();197if (!keyAlg.equals("RC4") && !keyAlg.equals("ARCFOUR")) {198throw new InvalidKeyException("Not an ARCFOUR key: " + keyAlg);199}200if ("RAW".equals(key.getFormat()) == false) {201throw new InvalidKeyException("Key encoding format must be RAW");202}203byte[] encodedKey = key.getEncoded();204if ((encodedKey.length < 5) || (encodedKey.length > 128)) {205Arrays.fill(encodedKey, (byte)0);206throw new InvalidKeyException207("Key length must be between 40 and 1024 bit");208}209return encodedKey;210}211212// see JCE spec213protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) {214byte[] out = new byte[inLen];215crypt(in, inOfs, inLen, out, 0);216return out;217}218219// see JCE spec220protected int engineUpdate(byte[] in, int inOfs, int inLen,221byte[] out, int outOfs) throws ShortBufferException {222if (out.length - outOfs < inLen) {223throw new ShortBufferException("Output buffer too small");224}225crypt(in, inOfs, inLen, out, outOfs);226return inLen;227}228229// see JCE spec230protected byte[] engineDoFinal(byte[] in, int inOfs, int inLen) {231byte[] out = engineUpdate(in, inOfs, inLen);232is = -1;233return out;234}235236// see JCE spec237protected int engineDoFinal(byte[] in, int inOfs, int inLen,238byte[] out, int outOfs) throws ShortBufferException {239int outLen = engineUpdate(in, inOfs, inLen, out, outOfs);240is = -1;241return outLen;242}243244// see JCE spec245protected byte[] engineWrap(Key key) throws IllegalBlockSizeException,246InvalidKeyException {247byte[] encoded = key.getEncoded();248if ((encoded == null) || (encoded.length == 0)) {249throw new InvalidKeyException("Could not obtain encoded key");250}251try {252return engineDoFinal(encoded, 0, encoded.length);253} finally {254Arrays.fill(encoded, (byte)0);255}256}257258// see JCE spec259protected Key engineUnwrap(byte[] wrappedKey, String algorithm,260int type) throws InvalidKeyException, NoSuchAlgorithmException {261byte[] encoded = null;262try {263encoded = engineDoFinal(wrappedKey, 0, wrappedKey.length);264return ConstructKeys.constructKey(encoded, algorithm, type);265} finally {266if (encoded != null) {267Arrays.fill(encoded, (byte) 0);268}269}270}271272// see JCE spec273protected int engineGetKeySize(Key key) throws InvalidKeyException {274byte[] encodedKey = getEncodedKey(key);275Arrays.fill(encodedKey, (byte)0);276return Math.multiplyExact(encodedKey.length, 8);277}278279}280281282