1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.crypto.provider;
  27 
  28 import java.security.InvalidKeyException;
  29 import java.security.ProviderException;
  30 
  31 /**
  32  * This class represents ciphers in electronic codebook (ECB) mode.
  33  *
  34  * <p>This mode is implemented independently of a particular cipher.
  35  * Ciphers to which this mode should apply (e.g., DES) must be
  36  * <i>plugged-in</i> using the constructor.
  37  *
  38  * <p>NOTE: This class does not deal with buffering or padding.
  39  *
  40  * @author Gigi Ankeny
  41  */
  42 
  43 final class ElectronicCodeBook extends FeedbackCipher {
  44 
  45     ElectronicCodeBook(SymmetricCipher embeddedCipher) {
  46         super(embeddedCipher);
  47     }
  48 
  49     /**
  50      * Gets the name of the feedback mechanism
  51      *
  52      * @return the name of the feedback mechanism
  53      */
  54     String getFeedback() {
  55         return "ECB";
  56     }
  57 
  58     /**
  59      * Resets the iv to its original value.
  60      * This is used when doFinal is called in the Cipher class, so that the
  61      * cipher can be reused (with its original iv).
  62      */
  63     void reset() {
  64         // empty
  65     }
  66 
  67     /**
  68      * Save the current content of this cipher.
  69      */
  70     void save() {}
  71 
  72     /**
  73      * Restores the content of this cipher to the previous saved one.
  74      */
  75     void restore() {}
  76 
  77     /**
  78      * Initializes the cipher in the specified mode with the given key
  79      * and iv.
  80      *
  81      * @param decrypting flag indicating encryption or decryption
  82      * @param algorithm the algorithm name
  83      * @param key the key
  84      * @param iv the iv
  85      *
  86      * @exception InvalidKeyException if the given key is inappropriate for
  87      * initializing this cipher
  88      */
  89     void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)
  90             throws InvalidKeyException {
  91         if ((key == null) || (iv != null)) {
  92             throw new InvalidKeyException("Internal error");
  93         }
  94         embeddedCipher.init(decrypting, algorithm, key);
  95     }
  96 
  97     /**
  98      * Performs encryption operation.
  99      *
 100      * <p>The input plain text <code>in</code>, starting at
 101      * <code>inOff</code> and ending at * <code>(inOff + len - 1)</code>,
 102      * is encrypted. The result is stored in <code>out</code>, starting at
 103      * <code>outOff</code>.
 104      *
 105      * @param in the buffer with the input data to be encrypted
 106      * @param inOff the offset in <code>plain</code>
 107      * @param len the length of the input data
 108      * @param out the buffer for the result
 109      * @param outOff the offset in <code>cipher</code>
 110      * @exception ProviderException if <code>len</code> is not
 111      * a multiple of the block size
 112      * @return the length of the encrypted data
 113      */
 114     int encrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
 115         embeddedCipher.encryptBlocks(in, inOff, out, outOff, len);
 116         return len;
 117     }
 118 
 119     /**
 120      * Performs decryption operation.
 121      *
 122      * <p>The input cipher text <code>in</code>, starting at
 123      * <code>inOff</code> and ending at * <code>(inOff + len - 1)</code>,
 124      * is decrypted.The result is stored in <code>out</code>, starting at
 125      * <code>outOff</code>.
 126      *
 127      * @param in the buffer with the input data to be decrypted
 128      * @param inOff the offset in <code>cipherOffset</code>
 129      * @param len the length of the input data
 130      * @param out the buffer for the result
 131      * @param outOff the offset in <code>plain</code>
 132      * @exception ProviderException if <code>len</code> is not
 133      * a multiple of the block size
 134      * @return the length of the decrypted data
 135      */
 136     int decrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
 137         embeddedCipher.decryptBlocks(in, inOff, out, outOff, len);
 138         return len;
 139     }
 140 }