Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/crypto/provider/CounterMode.java
38922 views
/*1* Copyright (c) 2002, 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;2829/**30* This class represents ciphers in counter (CTR) mode.31*32* <p>This mode is implemented independently of a particular cipher.33* Ciphers to which this mode should apply (e.g., DES) must be34* <i>plugged-in</i> using the constructor.35*36* <p>NOTE: This class does not deal with buffering or padding.37*38* @author Andreas Sterbenz39* @since 1.4.240*/41final class CounterMode extends FeedbackCipher {4243// current counter value44private final byte[] counter;4546// encrypted bytes of the previous counter value47private final byte[] encryptedCounter;4849// number of bytes in encryptedCounter already used up50private int used;5152// variables for save/restore calls53private byte[] counterSave = null;54private byte[] encryptedCounterSave = null;55private int usedSave = 0;5657CounterMode(SymmetricCipher embeddedCipher) {58super(embeddedCipher);59counter = new byte[blockSize];60encryptedCounter = new byte[blockSize];61}6263/**64* Gets the name of the feedback mechanism65*66* @return the name of the feedback mechanism67*/68String getFeedback() {69return "CTR";70}7172/**73* Resets the iv to its original value.74* This is used when doFinal is called in the Cipher class, so that the75* cipher can be reused (with its original iv).76*/77void reset() {78System.arraycopy(iv, 0, counter, 0, blockSize);79used = blockSize;80}8182/**83* Save the current content of this cipher.84*/85void save() {86if (counterSave == null) {87counterSave = new byte[blockSize];88encryptedCounterSave = new byte[blockSize];89}90System.arraycopy(counter, 0, counterSave, 0, blockSize);91System.arraycopy(encryptedCounter, 0, encryptedCounterSave, 0,92blockSize);93usedSave = used;94}9596/**97* Restores the content of this cipher to the previous saved one.98*/99void restore() {100System.arraycopy(counterSave, 0, counter, 0, blockSize);101System.arraycopy(encryptedCounterSave, 0, encryptedCounter, 0,102blockSize);103used = usedSave;104}105106/**107* Initializes the cipher in the specified mode with the given key108* and iv.109*110* @param decrypting flag indicating encryption or decryption111* @param algorithm the algorithm name112* @param key the key113* @param iv the iv114*115* @exception InvalidKeyException if the given key is inappropriate for116* initializing this cipher117*/118void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)119throws InvalidKeyException {120if ((key == null) || (iv == null) || (iv.length != blockSize)) {121throw new InvalidKeyException("Internal error");122}123this.iv = iv;124reset();125// always encrypt mode for embedded cipher126embeddedCipher.init(false, algorithm, key);127}128129/**130* Performs encryption operation.131*132* <p>The input plain text <code>plain</code>, starting at133* <code>plainOffset</code> and ending at134* <code>(plainOffset + len - 1)</code>, is encrypted.135* The result is stored in <code>cipher</code>, starting at136* <code>cipherOffset</code>.137*138* @param in the buffer with the input data to be encrypted139* @param inOffset the offset in <code>plain</code>140* @param len the length of the input data141* @param out the buffer for the result142* @param outOff the offset in <code>cipher</code>143* @return the length of the encrypted data144*/145int encrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {146return crypt(in, inOff, len, out, outOff);147}148149// CTR encrypt and decrypt are identical150int decrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {151return crypt(in, inOff, len, out, outOff);152}153154/**155* Increment the counter value.156*/157private static void increment(byte[] b) {158int n = b.length - 1;159while ((n >= 0) && (++b[n] == 0)) {160n--;161}162}163164/**165* Do the actual encryption/decryption operation.166* Essentially we XOR the input plaintext/ciphertext stream with a167* keystream generated by encrypting the counter values. Counter values168* are encrypted on demand.169*/170private int crypt(byte[] in, int inOff, int len, byte[] out, int outOff) {171if (len == 0) {172return 0;173}174175RangeUtil.nullAndBoundsCheck(in, inOff, len);176RangeUtil.nullAndBoundsCheck(out, outOff, len);177178int result = len;179while (len-- > 0) {180if (used >= blockSize) {181embeddedCipher.encryptBlock(counter, 0, encryptedCounter, 0);182increment(counter);183used = 0;184}185out[outOff++] = (byte)(in[inOff++] ^ encryptedCounter[used++]);186}187return result;188}189}190191192