Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/crypto/provider/CipherFeedback.java
38922 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;2930/**31* This class represents ciphers in cipher-feedback (CFB) mode.32*33* <p>This mode is implemented independently of a particular cipher.34* Ciphers to which this mode should apply (e.g., DES) must be35* <i>plugged-in</i> using the constructor.36*37* <p>NOTE: This class does not deal with buffering or padding.38*39* @author Gigi Ankeny40*/4142final class CipherFeedback extends FeedbackCipher {4344/*45* encrypt/decrypt output buffer46*/47private final byte[] k;4849/*50* register value, initialized with iv51*/52private final byte[] register;5354/*55* number of bytes for each stream unit, defaults to the blocksize56* of the embedded cipher57*/58private int numBytes;5960// variables for save/restore calls61private byte[] registerSave = null;6263CipherFeedback(SymmetricCipher embeddedCipher, int numBytes) {64super(embeddedCipher);65if (numBytes > blockSize) {66numBytes = blockSize;67}68this.numBytes = numBytes;69k = new byte[blockSize];70register = new byte[blockSize];71}7273/**74* Gets the name of this feedback mode.75*76* @return the string <code>CFB</code>77*/78String getFeedback() {79return "CFB";80}8182/**83* Initializes the cipher in the specified mode with the given key84* and iv.85*86* @param decrypting flag indicating encryption or decryption87* @param algorithm the algorithm name88* @param key the key89* @param iv the iv90*91* @exception InvalidKeyException if the given key is inappropriate for92* initializing this cipher93*/94void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)95throws InvalidKeyException {96if ((key == null) || (iv == null) || (iv.length != blockSize)) {97throw new InvalidKeyException("Internal error");98}99this.iv = iv;100reset();101// always encrypt mode for embedded cipher102embeddedCipher.init(false, algorithm, key);103}104105/**106* Resets the iv to its original value.107* This is used when doFinal is called in the Cipher class, so that the108* cipher can be reused (with its original iv).109*/110void reset() {111System.arraycopy(iv, 0, register, 0, blockSize);112}113114/**115* Save the current content of this cipher.116*/117void save() {118if (registerSave == null) {119registerSave = new byte[blockSize];120}121System.arraycopy(register, 0, registerSave, 0, blockSize);122}123124/**125* Restores the content of this cipher to the previous saved one.126*/127void restore() {128System.arraycopy(registerSave, 0, register, 0, blockSize);129}130131/**132* Performs encryption operation.133*134* <p>The input plain text <code>plain</code>, starting at135* <code>plainOffset</code> and ending at136* <code>(plainOffset + plainLen - 1)</code>, is encrypted.137* The result is stored in <code>cipher</code>, starting at138* <code>cipherOffset</code>.139*140* @param plain the buffer with the input data to be encrypted141* @param plainOffset the offset in <code>plain</code>142* @param plainLen the length of the input data143* @param cipher the buffer for the result144* @param cipherOffset the offset in <code>cipher</code>145* @exception ProviderException if <code>plainLen</code> is not146* a multiple of the <code>numBytes</code>147* @return the length of the encrypted data148*/149int encrypt(byte[] plain, int plainOffset, int plainLen,150byte[] cipher, int cipherOffset) {151RangeUtil.blockSizeCheck(plainLen, numBytes);152RangeUtil.nullAndBoundsCheck(plain, plainOffset, plainLen);153RangeUtil.nullAndBoundsCheck(cipher, cipherOffset, plainLen);154155int nShift = blockSize - numBytes;156int loopCount = plainLen / numBytes;157158for (; loopCount > 0 ;159plainOffset += numBytes, cipherOffset += numBytes,160loopCount--) {161embeddedCipher.encryptBlock(register, 0, k, 0);162if (nShift != 0) {163System.arraycopy(register, numBytes, register, 0, nShift);164}165for (int i = 0; i < numBytes; i++) {166register[nShift + i] = cipher[i + cipherOffset] =167(byte)(k[i] ^ plain[i + plainOffset]);168}169}170return plainLen;171}172173/**174* Performs the last encryption operation.175*176* <p>The input plain text <code>plain</code>, starting at177* <code>plainOffset</code> and ending at178* <code>(plainOffset + plainLen - 1)</code>, is encrypted.179* The result is stored in <code>cipher</code>, starting at180* <code>cipherOffset</code>.181*182* @param plain the buffer with the input data to be encrypted183* @param plainOffset the offset in <code>plain</code>184* @param plainLen the length of the input data185* @param cipher the buffer for the result186* @param cipherOffset the offset in <code>cipher</code>187* @return the number of bytes placed into <code>cipher</code>188*/189int encryptFinal(byte[] plain, int plainOffset, int plainLen,190byte[] cipher, int cipherOffset) {191192int oddBytes = plainLen % numBytes;193int len = encrypt(plain, plainOffset, (plainLen - oddBytes),194cipher, cipherOffset);195plainOffset += len;196cipherOffset += len;197if (oddBytes != 0) {198embeddedCipher.encryptBlock(register, 0, k, 0);199for (int i = 0; i < oddBytes; i++) {200cipher[i + cipherOffset] =201(byte)(k[i] ^ plain[i + plainOffset]);202}203}204return plainLen;205}206207/**208* Performs decryption operation.209*210* <p>The input cipher text <code>cipher</code>, starting at211* <code>cipherOffset</code> and ending at212* <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.213* The result is stored in <code>plain</code>, starting at214* <code>plainOffset</code>.215*216* @param cipher the buffer with the input data to be decrypted217* @param cipherOffset the offset in <code>cipherOffset</code>218* @param cipherLen the length of the input data219* @param plain the buffer for the result220* @param plainOffset the offset in <code>plain</code>221* @exception ProviderException if <code>cipherLen</code> is not222* a multiple of the <code>numBytes</code>223* @return the length of the decrypted data224*/225int decrypt(byte[] cipher, int cipherOffset, int cipherLen,226byte[] plain, int plainOffset) {227228RangeUtil.blockSizeCheck(cipherLen, numBytes);229RangeUtil.nullAndBoundsCheck(cipher, cipherOffset, cipherLen);230RangeUtil.nullAndBoundsCheck(plain, plainOffset, cipherLen);231232int nShift = blockSize - numBytes;233int loopCount = cipherLen / numBytes;234235for (; loopCount > 0;236plainOffset += numBytes, cipherOffset += numBytes,237loopCount--) {238embeddedCipher.encryptBlock(register, 0, k, 0);239if (nShift != 0) {240System.arraycopy(register, numBytes, register, 0, nShift);241}242for (int i = 0; i < numBytes; i++) {243register[i + nShift] = cipher[i + cipherOffset];244plain[i + plainOffset]245= (byte)(cipher[i + cipherOffset] ^ k[i]);246}247}248return cipherLen;249}250251/**252* Performs the last decryption operation.253*254* <p>The input cipher text <code>cipher</code>, starting at255* <code>cipherOffset</code> and ending at256* <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.257* The result is stored in <code>plain</code>, starting at258* <code>plainOffset</code>.259*260* @param cipher the buffer with the input data to be decrypted261* @param cipherOffset the offset in <code>cipherOffset</code>262* @param cipherLen the length of the input data263* @param plain the buffer for the result264* @param plainOffset the offset in <code>plain</code>265* @return the length of the decrypted data266*/267int decryptFinal(byte[] cipher, int cipherOffset, int cipherLen,268byte[] plain, int plainOffset) {269270int oddBytes = cipherLen % numBytes;271int len = decrypt(cipher, cipherOffset, (cipherLen - oddBytes),272plain, plainOffset);273cipherOffset += len;274plainOffset += len;275if (oddBytes != 0) {276embeddedCipher.encryptBlock(register, 0, k, 0);277for (int i = 0; i < oddBytes; i++) {278plain[i + plainOffset]279= (byte)(cipher[i + cipherOffset] ^ k[i]);280}281}282return cipherLen;283}284}285286287