Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/ElectronicCodeBook.java
41161 views
/*1* Copyright (c) 1997, 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;28import java.security.ProviderException;29import sun.security.util.ArrayUtil;30import java.util.Objects;31import jdk.internal.vm.annotation.IntrinsicCandidate;3233/**34* This class represents ciphers in electronic codebook (ECB) mode.35*36* <p>This mode is implemented independently of a particular cipher.37* Ciphers to which this mode should apply (e.g., DES) must be38* <i>plugged-in</i> using the constructor.39*40* <p>NOTE: This class does not deal with buffering or padding.41*42* @author Gigi Ankeny43*/4445final class ElectronicCodeBook extends FeedbackCipher {4647ElectronicCodeBook(SymmetricCipher embeddedCipher) {48super(embeddedCipher);49}5051/**52* Gets the name of the feedback mechanism53*54* @return the name of the feedback mechanism55*/56String getFeedback() {57return "ECB";58}5960/**61* Resets the iv to its original value.62* This is used when doFinal is called in the Cipher class, so that the63* cipher can be reused (with its original iv).64*/65void reset() {66// empty67}6869/**70* Save the current content of this cipher.71*/72void save() {}7374/**75* Restores the content of this cipher to the previous saved one.76*/77void restore() {}7879/**80* Initializes the cipher in the specified mode with the given key81* and iv.82*83* @param decrypting flag indicating encryption or decryption84* @param algorithm the algorithm name85* @param key the key86* @param iv the iv87*88* @exception InvalidKeyException if the given key is inappropriate for89* initializing this cipher90*/91void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)92throws InvalidKeyException {93if ((key == null) || (iv != null)) {94throw new InvalidKeyException("Internal error");95}96embeddedCipher.init(decrypting, algorithm, key);97}9899@IntrinsicCandidate100private int implECBEncrypt(byte [] in, int inOff, int len, byte[] out, int outOff) {101for (int i = len; i >= blockSize; i -= blockSize) {102embeddedCipher.encryptBlock(in, inOff, out, outOff);103inOff += blockSize;104outOff += blockSize;105}106return len;107}108109/**110* Performs encryption operation.111*112* <p>The input plain text <code>in</code>, starting at113* <code>inOff</code> and ending at * <code>(inOff + len - 1)</code>,114* is encrypted. The result is stored in <code>out</code>, starting at115* <code>outOff</code>.116*117* @param in the buffer with the input data to be encrypted118* @param inOff the offset in <code>plain</code>119* @param len the length of the input data120* @param out the buffer for the result121* @param outOff the offset in <code>cipher</code>122* @exception ProviderException if <code>len</code> is not123* a multiple of the block size124* @return the length of the encrypted data125*/126int encrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {127ArrayUtil.blockSizeCheck(len, blockSize);128ArrayUtil.nullAndBoundsCheck(in, inOff, len);129ArrayUtil.nullAndBoundsCheck(out, outOff, len);130return implECBEncrypt(in, inOff, len, out, outOff);131}132133@IntrinsicCandidate134private int implECBDecrypt(byte [] in, int inOff, int len, byte[] out, int outOff) {135for (int i = len; i >= blockSize; i -= blockSize) {136embeddedCipher.decryptBlock(in, inOff, out, outOff);137inOff += blockSize;138outOff += blockSize;139}140return len;141}142143/**144* Performs decryption operation.145*146* <p>The input cipher text <code>in</code>, starting at147* <code>inOff</code> and ending at * <code>(inOff + len - 1)</code>,148* is decrypted.The result is stored in <code>out</code>, starting at149* <code>outOff</code>.150*151* @param in the buffer with the input data to be decrypted152* @param inOff the offset in <code>cipherOffset</code>153* @param len the length of the input data154* @param out the buffer for the result155* @param outOff the offset in <code>plain</code>156* @exception ProviderException if <code>len</code> is not157* a multiple of the block size158* @return the length of the decrypted data159*/160int decrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {161ArrayUtil.blockSizeCheck(len, blockSize);162ArrayUtil.nullAndBoundsCheck(in, inOff, len);163ArrayUtil.nullAndBoundsCheck(out, outOff, len);164return implECBDecrypt(in, inOff, len, out, outOff);165}166}167168169