Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/CounterMode.java
41161 views
/*1* Copyright (c) 2002, 2020, 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;2829import jdk.internal.vm.annotation.IntrinsicCandidate;30import sun.security.util.ArrayUtil;3132/**33* This class represents ciphers in counter (CTR) mode.34*35* <p>This mode is implemented independently of a particular cipher.36* Ciphers to which this mode should apply (e.g., DES) must be37* <i>plugged-in</i> using the constructor.38*39* <p>NOTE: This class does not deal with buffering or padding.40*41* @author Andreas Sterbenz42* @since 1.4.243*/44class CounterMode extends FeedbackCipher {4546// current counter value47final byte[] counter;4849// encrypted bytes of the previous counter value50private final byte[] encryptedCounter;5152// number of bytes in encryptedCounter already used up53private int used;5455// variables for save/restore calls56private byte[] counterSave = null;57private byte[] encryptedCounterSave = null;58private int usedSave = 0;5960CounterMode(SymmetricCipher embeddedCipher) {61super(embeddedCipher);62counter = new byte[blockSize];63encryptedCounter = new byte[blockSize];64}6566/**67* Gets the name of the feedback mechanism68*69* @return the name of the feedback mechanism70*/71String getFeedback() {72return "CTR";73}7475/**76* Resets the iv to its original value.77* This is used when doFinal is called in the Cipher class, so that the78* cipher can be reused (with its original iv).79*/80void reset() {81System.arraycopy(iv, 0, counter, 0, blockSize);82used = blockSize;83}8485/**86* Save the current content of this cipher.87*/88void save() {89if (counterSave == null) {90counterSave = new byte[blockSize];91encryptedCounterSave = new byte[blockSize];92}93System.arraycopy(counter, 0, counterSave, 0, blockSize);94System.arraycopy(encryptedCounter, 0, encryptedCounterSave, 0,95blockSize);96usedSave = used;97}9899/**100* Restores the content of this cipher to the previous saved one.101*/102void restore() {103System.arraycopy(counterSave, 0, counter, 0, blockSize);104System.arraycopy(encryptedCounterSave, 0, encryptedCounter, 0,105blockSize);106used = usedSave;107}108109/**110* Initializes the cipher in the specified mode with the given key111* and iv.112*113* @param decrypting flag indicating encryption or decryption114* @param algorithm the algorithm name115* @param key the key116* @param iv the iv117*118* @exception InvalidKeyException if the given key is inappropriate for119* initializing this cipher120*/121void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)122throws InvalidKeyException {123if ((key == null) || (iv == null) || (iv.length != blockSize)) {124throw new InvalidKeyException("Internal error");125}126this.iv = iv;127reset();128// always encrypt mode for embedded cipher129embeddedCipher.init(false, algorithm, key);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 + len - 1)</code>, is encrypted.138* The result is stored in <code>cipher</code>, starting at139* <code>cipherOffset</code>.140*141* @param in the buffer with the input data to be encrypted142* @param inOff the offset in <code>plain</code>143* @param len the length of the input data144* @param out the buffer for the result145* @param outOff the offset in <code>cipher</code>146* @return the length of the encrypted data147*/148int encrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {149return crypt(in, inOff, len, out, outOff);150}151152// CTR encrypt and decrypt are identical153int decrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {154return crypt(in, inOff, len, out, outOff);155}156157/**158* Increment the counter value.159*/160private static void increment(byte[] b) {161int n = b.length - 1;162while ((n >= 0) && (++b[n] == 0)) {163n--;164}165}166167/**168* Do the actual encryption/decryption operation.169* Essentially we XOR the input plaintext/ciphertext stream with a170* keystream generated by encrypting the counter values. Counter values171* are encrypted on demand.172*/173private int crypt(byte[] in, int inOff, int len, byte[] out, int outOff) {174if (len == 0) {175return 0;176}177178ArrayUtil.nullAndBoundsCheck(in, inOff, len);179ArrayUtil.nullAndBoundsCheck(out, outOff, len);180return implCrypt(in, inOff, len, out, outOff);181}182183// Implementation of crpyt() method. Possibly replaced with a compiler intrinsic.184@IntrinsicCandidate185private int implCrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {186int result = len;187while (len-- > 0) {188if (used >= blockSize) {189embeddedCipher.encryptBlock(counter, 0, encryptedCounter, 0);190increment(counter);191used = 0;192}193out[outOff++] = (byte)(in[inOff++] ^ encryptedCounter[used++]);194}195return result;196}197198}199200201