Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/crypto/provider/ARCFOURCipher.java
38922 views
/*1* Copyright (c) 2003, 2017, 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;2930import javax.crypto.*;3132/**33* Implementation of the ARCFOUR cipher, an algorithm apparently compatible34* with RSA Security's RC4(tm) cipher. The description of this algorithm was35* taken from Bruce Schneier's book Applied Cryptography, 2nd ed.,36* section 17.1.37*38* We support keys from 40 to 1024 bits. ARCFOUR would allow for keys shorter39* than 40 bits, but that is too insecure for us to permit.40*41* Note that we subclass CipherSpi directly and do not use the CipherCore42* framework. That was designed to simplify implementation of block ciphers43* and does not offer any advantages for stream ciphers such as ARCFOUR.44*45* @since 1.546* @author Andreas Sterbenz47*/48public final class ARCFOURCipher extends CipherSpi {4950// state array S, 256 entries. The entries are 8-bit, but we use an int[]51// because int arithmetic is much faster than in Java than bytes.52private final int[] S;5354// state indices i and j. Called is and js to avoid collision with55// local variables. 'is' is set to -1 after a call to doFinal()56private int is, js;5758// the bytes of the last key used (if any)59// we need this to re-initialize after a call to doFinal()60private byte[] lastKey;6162// called by the JCE framework63public ARCFOURCipher() {64S = new int[256];65}6667// core key setup code. initializes S, is, and js68// assumes key is non-null and between 40 and 1024 bit69private void init(byte[] key) {70// initialize S[i] to i71for (int i = 0; i < 256; i++) {72S[i] = i;73}7475// we avoid expanding key to 256 bytes and instead keep a separate76// counter ki = i mod key.length.77for (int i = 0, j = 0, ki = 0; i < 256; i++) {78int Si = S[i];79j = (j + Si + key[ki]) & 0xff;80S[i] = S[j];81S[j] = Si;82ki++;83if (ki == key.length) {84ki = 0;85}86}8788// set indices to 089is = 0;90js = 0;91}9293// core crypt code. OFB style, so works for both encryption and decryption94private void crypt(byte[] in, int inOfs, int inLen, byte[] out,95int outOfs) {96if (is < 0) {97// doFinal() was called, need to reset the cipher to initial state98init(lastKey);99}100while (inLen-- > 0) {101is = (is + 1) & 0xff;102int Si = S[is];103js = (js + Si) & 0xff;104int Sj = S[js];105S[is] = Sj;106S[js] = Si;107out[outOfs++] = (byte)(in[inOfs++] ^ S[(Si + Sj) & 0xff]);108}109}110111// Modes do not make sense with stream ciphers, but allow ECB112// see JCE spec.113protected void engineSetMode(String mode) throws NoSuchAlgorithmException {114if (mode.equalsIgnoreCase("ECB") == false) {115throw new NoSuchAlgorithmException("Unsupported mode " + mode);116}117}118119// Padding does not make sense with stream ciphers, but allow NoPadding120// see JCE spec.121protected void engineSetPadding(String padding)122throws NoSuchPaddingException {123if (padding.equalsIgnoreCase("NoPadding") == false) {124throw new NoSuchPaddingException("Padding must be NoPadding");125}126}127128// Return 0 to indicate stream cipher129// see JCE spec.130protected int engineGetBlockSize() {131return 0;132}133134// output length is always the same as input length135// see JCE spec136protected int engineGetOutputSize(int inputLen) {137return inputLen;138}139140// no IV, return null141// see JCE spec142protected byte[] engineGetIV() {143return null;144}145146// no parameters147// see JCE spec148protected AlgorithmParameters engineGetParameters() {149return null;150}151152// see JCE spec153protected void engineInit(int opmode, Key key, SecureRandom random)154throws InvalidKeyException {155init(opmode, key);156}157158// see JCE spec159protected void engineInit(int opmode, Key key,160AlgorithmParameterSpec params, SecureRandom random)161throws InvalidKeyException, InvalidAlgorithmParameterException {162if (params != null) {163throw new InvalidAlgorithmParameterException164("Parameters not supported");165}166init(opmode, key);167}168169// see JCE spec170protected void engineInit(int opmode, Key key,171AlgorithmParameters params, SecureRandom random)172throws InvalidKeyException, InvalidAlgorithmParameterException {173if (params != null) {174throw new InvalidAlgorithmParameterException175("Parameters not supported");176}177init(opmode, key);178}179180// init method. Check opmode and key, then call init(byte[]).181private void init(int opmode, Key key) throws InvalidKeyException {182if ((opmode < Cipher.ENCRYPT_MODE) || (opmode > Cipher.UNWRAP_MODE)) {183throw new InvalidKeyException("Unknown opmode: " + opmode);184}185lastKey = getEncodedKey(key);186init(lastKey);187}188189// return the encoding of key if key is a valid ARCFOUR key.190// otherwise, throw an InvalidKeyException191private static byte[] getEncodedKey(Key key) throws InvalidKeyException {192String keyAlg = key.getAlgorithm();193if (!keyAlg.equals("RC4") && !keyAlg.equals("ARCFOUR")) {194throw new InvalidKeyException("Not an ARCFOUR key: " + keyAlg);195}196if ("RAW".equals(key.getFormat()) == false) {197throw new InvalidKeyException("Key encoding format must be RAW");198}199byte[] encodedKey = key.getEncoded();200if ((encodedKey.length < 5) || (encodedKey.length > 128)) {201throw new InvalidKeyException202("Key length must be between 40 and 1024 bit");203}204return encodedKey;205}206207// see JCE spec208protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) {209byte[] out = new byte[inLen];210crypt(in, inOfs, inLen, out, 0);211return out;212}213214// see JCE spec215protected int engineUpdate(byte[] in, int inOfs, int inLen,216byte[] out, int outOfs) throws ShortBufferException {217if (out.length - outOfs < inLen) {218throw new ShortBufferException("Output buffer too small");219}220crypt(in, inOfs, inLen, out, outOfs);221return inLen;222}223224// see JCE spec225protected byte[] engineDoFinal(byte[] in, int inOfs, int inLen) {226byte[] out = engineUpdate(in, inOfs, inLen);227is = -1;228return out;229}230231// see JCE spec232protected int engineDoFinal(byte[] in, int inOfs, int inLen,233byte[] out, int outOfs) throws ShortBufferException {234int outLen = engineUpdate(in, inOfs, inLen, out, outOfs);235is = -1;236return outLen;237}238239// see JCE spec240protected byte[] engineWrap(Key key) throws IllegalBlockSizeException,241InvalidKeyException {242byte[] encoded = key.getEncoded();243if ((encoded == null) || (encoded.length == 0)) {244throw new InvalidKeyException("Could not obtain encoded key");245}246return engineDoFinal(encoded, 0, encoded.length);247}248249// see JCE spec250protected Key engineUnwrap(byte[] wrappedKey, String algorithm,251int type) throws InvalidKeyException, NoSuchAlgorithmException {252byte[] encoded = engineDoFinal(wrappedKey, 0, wrappedKey.length);253return ConstructKeys.constructKey(encoded, algorithm, type);254}255256// see JCE spec257protected int engineGetKeySize(Key key) throws InvalidKeyException {258byte[] encodedKey = getEncodedKey(key);259return Math.multiplyExact(encodedKey.length, 8);260}261262}263264265