Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/CipherFeedback.java
41161 views
/*1* Copyright (c) 1997, 2018, 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.InvalidKeyException;28import java.security.ProviderException;29import sun.security.util.ArrayUtil;3031/**32* This class represents ciphers in cipher-feedback (CFB) mode.33*34* <p>This mode is implemented independently of a particular cipher.35* Ciphers to which this mode should apply (e.g., DES) must be36* <i>plugged-in</i> using the constructor.37*38* <p>NOTE: This class does not deal with buffering or padding.39*40* @author Gigi Ankeny41*/4243final class CipherFeedback extends FeedbackCipher {4445/*46* encrypt/decrypt output buffer47*/48private final byte[] k;4950/*51* register value, initialized with iv52*/53private final byte[] register;5455/*56* number of bytes for each stream unit, defaults to the blocksize57* of the embedded cipher58*/59private int numBytes;6061// variables for save/restore calls62private byte[] registerSave = null;6364CipherFeedback(SymmetricCipher embeddedCipher, int numBytes) {65super(embeddedCipher);66if (numBytes > blockSize) {67numBytes = blockSize;68}69this.numBytes = numBytes;70k = new byte[blockSize];71register = new byte[blockSize];72}7374/**75* Gets the name of this feedback mode.76*77* @return the string <code>CFB</code>78*/79String getFeedback() {80return "CFB";81}8283/**84* Initializes the cipher in the specified mode with the given key85* and iv.86*87* @param decrypting flag indicating encryption or decryption88* @param algorithm the algorithm name89* @param key the key90* @param iv the iv91*92* @exception InvalidKeyException if the given key is inappropriate for93* initializing this cipher94*/95void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)96throws InvalidKeyException {97if ((key == null) || (iv == null) || (iv.length != blockSize)) {98throw new InvalidKeyException("Internal error");99}100this.iv = iv;101reset();102// always encrypt mode for embedded cipher103embeddedCipher.init(false, algorithm, key);104}105106/**107* Resets the iv to its original value.108* This is used when doFinal is called in the Cipher class, so that the109* cipher can be reused (with its original iv).110*/111void reset() {112System.arraycopy(iv, 0, register, 0, blockSize);113}114115/**116* Save the current content of this cipher.117*/118void save() {119if (registerSave == null) {120registerSave = new byte[blockSize];121}122System.arraycopy(register, 0, registerSave, 0, blockSize);123}124125/**126* Restores the content of this cipher to the previous saved one.127*/128void restore() {129System.arraycopy(registerSave, 0, register, 0, blockSize);130}131132/**133* Performs encryption operation.134*135* <p>The input plain text <code>plain</code>, starting at136* <code>plainOffset</code> and ending at137* <code>(plainOffset + plainLen - 1)</code>, is encrypted.138* The result is stored in <code>cipher</code>, starting at139* <code>cipherOffset</code>.140*141* @param plain the buffer with the input data to be encrypted142* @param plainOffset the offset in <code>plain</code>143* @param plainLen the length of the input data144* @param cipher the buffer for the result145* @param cipherOffset the offset in <code>cipher</code>146* @exception ProviderException if <code>plainLen</code> is not147* a multiple of the <code>numBytes</code>148* @return the length of the encrypted data149*/150int encrypt(byte[] plain, int plainOffset, int plainLen,151byte[] cipher, int cipherOffset) {152ArrayUtil.blockSizeCheck(plainLen, numBytes);153ArrayUtil.nullAndBoundsCheck(plain, plainOffset, plainLen);154ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, plainLen);155156int nShift = blockSize - numBytes;157int loopCount = plainLen / numBytes;158159for (; loopCount > 0 ;160plainOffset += numBytes, cipherOffset += numBytes,161loopCount--) {162embeddedCipher.encryptBlock(register, 0, k, 0);163if (nShift != 0) {164System.arraycopy(register, numBytes, register, 0, nShift);165}166for (int i = 0; i < numBytes; i++) {167register[nShift + i] = cipher[i + cipherOffset] =168(byte)(k[i] ^ plain[i + plainOffset]);169}170}171return plainLen;172}173174/**175* Performs the last encryption operation.176*177* <p>The input plain text <code>plain</code>, starting at178* <code>plainOffset</code> and ending at179* <code>(plainOffset + plainLen - 1)</code>, is encrypted.180* The result is stored in <code>cipher</code>, starting at181* <code>cipherOffset</code>.182*183* @param plain the buffer with the input data to be encrypted184* @param plainOffset the offset in <code>plain</code>185* @param plainLen the length of the input data186* @param cipher the buffer for the result187* @param cipherOffset the offset in <code>cipher</code>188* @return the number of bytes placed into <code>cipher</code>189*/190int encryptFinal(byte[] plain, int plainOffset, int plainLen,191byte[] cipher, int cipherOffset) {192193int oddBytes = plainLen % numBytes;194int len = encrypt(plain, plainOffset, (plainLen - oddBytes),195cipher, cipherOffset);196plainOffset += len;197cipherOffset += len;198if (oddBytes != 0) {199embeddedCipher.encryptBlock(register, 0, k, 0);200for (int i = 0; i < oddBytes; i++) {201cipher[i + cipherOffset] =202(byte)(k[i] ^ plain[i + plainOffset]);203}204}205return plainLen;206}207208/**209* Performs decryption operation.210*211* <p>The input cipher text <code>cipher</code>, starting at212* <code>cipherOffset</code> and ending at213* <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.214* The result is stored in <code>plain</code>, starting at215* <code>plainOffset</code>.216*217* @param cipher the buffer with the input data to be decrypted218* @param cipherOffset the offset in <code>cipherOffset</code>219* @param cipherLen the length of the input data220* @param plain the buffer for the result221* @param plainOffset the offset in <code>plain</code>222* @exception ProviderException if <code>cipherLen</code> is not223* a multiple of the <code>numBytes</code>224* @return the length of the decrypted data225*/226int decrypt(byte[] cipher, int cipherOffset, int cipherLen,227byte[] plain, int plainOffset) {228229ArrayUtil.blockSizeCheck(cipherLen, numBytes);230ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, cipherLen);231ArrayUtil.nullAndBoundsCheck(plain, plainOffset, cipherLen);232233int nShift = blockSize - numBytes;234int loopCount = cipherLen / numBytes;235236for (; loopCount > 0;237plainOffset += numBytes, cipherOffset += numBytes,238loopCount--) {239embeddedCipher.encryptBlock(register, 0, k, 0);240if (nShift != 0) {241System.arraycopy(register, numBytes, register, 0, nShift);242}243for (int i = 0; i < numBytes; i++) {244register[i + nShift] = cipher[i + cipherOffset];245plain[i + plainOffset]246= (byte)(cipher[i + cipherOffset] ^ k[i]);247}248}249return cipherLen;250}251252/**253* Performs the last decryption operation.254*255* <p>The input cipher text <code>cipher</code>, starting at256* <code>cipherOffset</code> and ending at257* <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.258* The result is stored in <code>plain</code>, starting at259* <code>plainOffset</code>.260*261* @param cipher the buffer with the input data to be decrypted262* @param cipherOffset the offset in <code>cipherOffset</code>263* @param cipherLen the length of the input data264* @param plain the buffer for the result265* @param plainOffset the offset in <code>plain</code>266* @return the length of the decrypted data267*/268int decryptFinal(byte[] cipher, int cipherOffset, int cipherLen,269byte[] plain, int plainOffset) {270271int oddBytes = cipherLen % numBytes;272int len = decrypt(cipher, cipherOffset, (cipherLen - oddBytes),273plain, plainOffset);274cipherOffset += len;275plainOffset += len;276if (oddBytes != 0) {277embeddedCipher.encryptBlock(register, 0, k, 0);278for (int i = 0; i < oddBytes; i++) {279plain[i + plainOffset]280= (byte)(cipher[i + cipherOffset] ^ k[i]);281}282}283return cipherLen;284}285}286287288