Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/crypto/provider/ElectronicCodeBook.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 electronic codebook (ECB) 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 ElectronicCodeBook extends FeedbackCipher {4344ElectronicCodeBook(SymmetricCipher embeddedCipher) {45super(embeddedCipher);46}4748/**49* Gets the name of the feedback mechanism50*51* @return the name of the feedback mechanism52*/53String getFeedback() {54return "ECB";55}5657/**58* Resets the iv to its original value.59* This is used when doFinal is called in the Cipher class, so that the60* cipher can be reused (with its original iv).61*/62void reset() {63// empty64}6566/**67* Save the current content of this cipher.68*/69void save() {}7071/**72* Restores the content of this cipher to the previous saved one.73*/74void restore() {}7576/**77* Initializes the cipher in the specified mode with the given key78* and iv.79*80* @param decrypting flag indicating encryption or decryption81* @param algorithm the algorithm name82* @param key the key83* @param iv the iv84*85* @exception InvalidKeyException if the given key is inappropriate for86* initializing this cipher87*/88void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)89throws InvalidKeyException {90if ((key == null) || (iv != null)) {91throw new InvalidKeyException("Internal error");92}93embeddedCipher.init(decrypting, algorithm, key);94}9596/**97* Performs encryption operation.98*99* <p>The input plain text <code>in</code>, starting at100* <code>inOff</code> and ending at * <code>(inOff + len - 1)</code>,101* is encrypted. The result is stored in <code>out</code>, starting at102* <code>outOff</code>.103*104* @param in the buffer with the input data to be encrypted105* @param inOff the offset in <code>plain</code>106* @param len the length of the input data107* @param out the buffer for the result108* @param outOff the offset in <code>cipher</code>109* @exception ProviderException if <code>len</code> is not110* a multiple of the block size111* @return the length of the encrypted data112*/113int encrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {114RangeUtil.blockSizeCheck(len, blockSize);115RangeUtil.nullAndBoundsCheck(in, inOff, len);116RangeUtil.nullAndBoundsCheck(out, outOff, len);117118for (int i = len; i >= blockSize; i -= blockSize) {119embeddedCipher.encryptBlock(in, inOff, out, outOff);120inOff += blockSize;121outOff += blockSize;122}123return len;124}125126/**127* Performs decryption operation.128*129* <p>The input cipher text <code>in</code>, starting at130* <code>inOff</code> and ending at * <code>(inOff + len - 1)</code>,131* is decrypted.The result is stored in <code>out</code>, starting at132* <code>outOff</code>.133*134* @param in the buffer with the input data to be decrypted135* @param inOff the offset in <code>cipherOffset</code>136* @param len the length of the input data137* @param out the buffer for the result138* @param outOff the offset in <code>plain</code>139* @exception ProviderException if <code>len</code> is not140* a multiple of the block size141* @return the length of the decrypted data142*/143int decrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {144RangeUtil.blockSizeCheck(len, blockSize);145RangeUtil.nullAndBoundsCheck(in, inOff, len);146RangeUtil.nullAndBoundsCheck(out, outOff, len);147148for (int i = len; i >= blockSize; i -= blockSize) {149embeddedCipher.decryptBlock(in, inOff, out, outOff);150inOff += blockSize;151outOff += blockSize;152}153return len;154}155}156157158