1 /*
   2  * Copyright (c) 1997, 2013, 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 javax.crypto.*;
  30 
  31 /**
  32  * This class represents a block cipher in one of its modes. It wraps
  33  * a SymmetricCipher maintaining the mode state and providing
  34  * the capability to encrypt amounts of data larger than a single block.
  35  *
  36  * @author Jan Luehe
  37  * @see ElectronicCodeBook
  38  * @see CipherBlockChaining
  39  * @see CipherFeedback
  40  * @see OutputFeedback
  41  * @see PCBC
  42  */
  43 abstract class FeedbackCipher {
  44 
  45     // the embedded block cipher
  46     final SymmetricCipher embeddedCipher;
  47 
  48     // the block size of the embedded block cipher
  49     final int blockSize;
  50 
  51     // the initialization vector
  52     byte[] iv;
  53 
  54     FeedbackCipher(SymmetricCipher embeddedCipher) {
  55         this.embeddedCipher = embeddedCipher;
  56         blockSize = embeddedCipher.getBlockSize();
  57     }
  58 
  59     final SymmetricCipher getEmbeddedCipher() {
  60         return embeddedCipher;
  61     }
  62 
  63     /**
  64      * Gets the block size of the embedded cipher.
  65      *
  66      * @return the block size of the embedded cipher
  67      */
  68     final int getBlockSize() {
  69         return blockSize;
  70     }
  71 
  72     /**
  73      * Gets the name of the feedback mechanism
  74      *
  75      * @return the name of the feedback mechanism
  76      */
  77     abstract String getFeedback();
  78 
  79     /**
  80      * Save the current content of this cipher.
  81      */
  82     abstract void save();
  83 
  84     /**
  85      * Restores the content of this cipher to the previous saved one.
  86      */
  87     abstract void restore();
  88 
  89     /**
  90      * Initializes the cipher in the specified mode with the given key
  91      * and iv.
  92      *
  93      * @param decrypting flag indicating encryption or decryption mode
  94      * @param algorithm the algorithm name (never null)
  95      * @param key the key (never null)
  96      * @param iv the iv (either null or blockSize bytes long)
  97      *
  98      * @exception InvalidKeyException if the given key is inappropriate for
  99      * initializing this cipher
 100      */
 101     abstract void init(boolean decrypting, String algorithm, byte[] key,
 102                        byte[] iv) throws InvalidKeyException;
 103 
 104    /**
 105      * Gets the initialization vector.
 106      *
 107      * @return the initialization vector
 108      */
 109     final byte[] getIV() {
 110         return iv;
 111     }
 112 
 113     /**
 114      * Resets the iv to its original value.
 115      * This is used when doFinal is called in the Cipher class, so that the
 116      * cipher can be reused (with its original iv).
 117      */
 118     abstract void reset();
 119 
 120     /**
 121      * Performs encryption operation.
 122      *
 123      * <p>The input <code>plain</code>, starting at <code>plainOffset</code>
 124      * and ending at <code>(plainOffset+plainLen-1)</code>, is encrypted.
 125      * The result is stored in <code>cipher</code>, starting at
 126      * <code>cipherOffset</code>.
 127      *
 128      * <p>The subclass that implements Cipher should ensure that
 129      * <code>init</code> has been called before this method is called.
 130      *
 131      * @param plain the input buffer with the data to be encrypted
 132      * @param plainOffset the offset in <code>plain</code>
 133      * @param plainLen the length of the input data
 134      * @param cipher the buffer for the encryption result
 135      * @param cipherOffset the offset in <code>cipher</code>
 136      */
 137     abstract void encrypt(byte[] plain, int plainOffset, int plainLen,
 138                           byte[] cipher, int cipherOffset);
 139     /**
 140      * Performs encryption operation for the last time.
 141      *
 142      * <p>NOTE: For cipher feedback modes which does not perform
 143      * special handling for the last few blocks, this is essentially
 144      * the same as <code>encrypt(...)</code>. Given most modes do
 145      * not do special handling, the default impl for this method is
 146      * to simply call <code>encrypt(...)</code>.
 147      *
 148      * @param plain the input buffer with the data to be encrypted
 149      * @param plainOffset the offset in <code>plain</code>
 150      * @param plainLen the length of the input data
 151      * @param cipher the buffer for the encryption result
 152      * @param cipherOffset the offset in <code>cipher</code>
 153      * @return the number of bytes placed into <code>cipher</code>
 154      */
 155      int encryptFinal(byte[] plain, int plainOffset, int plainLen,
 156                       byte[] cipher, int cipherOffset)
 157          throws IllegalBlockSizeException {
 158          encrypt(plain, plainOffset, plainLen, cipher, cipherOffset);
 159          return plainLen;
 160      }
 161     /**
 162      * Performs decryption operation.
 163      *
 164      * <p>The input <code>cipher</code>, starting at <code>cipherOffset</code>
 165      * and ending at <code>(cipherOffset+cipherLen-1)</code>, is decrypted.
 166      * The result is stored in <code>plain</code>, starting at
 167      * <code>plainOffset</code>.
 168      *
 169      * <p>The subclass that implements Cipher should ensure that
 170      * <code>init</code> has been called before this method is called.
 171      *
 172      * @param cipher the input buffer with the data to be decrypted
 173      * @param cipherOffset the offset in <code>cipher</code>
 174      * @param cipherLen the length of the input data
 175      * @param plain the buffer for the decryption result
 176      * @param plainOffset the offset in <code>plain</code>
 177      */
 178     abstract void decrypt(byte[] cipher, int cipherOffset, int cipherLen,
 179                           byte[] plain, int plainOffset);
 180 
 181     /**
 182      * Performs decryption operation for the last time.
 183      *
 184      * <p>NOTE: For cipher feedback modes which does not perform
 185      * special handling for the last few blocks, this is essentially
 186      * the same as <code>encrypt(...)</code>. Given most modes do
 187      * not do special handling, the default impl for this method is
 188      * to simply call <code>decrypt(...)</code>.
 189      *
 190      * @param cipher the input buffer with the data to be decrypted
 191      * @param cipherOffset the offset in <code>cipher</code>
 192      * @param cipherLen the length of the input data
 193      * @param plain the buffer for the decryption result
 194      * @param plainOffset the offset in <code>plain</code>
 195      * @return the number of bytes placed into <code>plain</code>
 196      */
 197      int decryptFinal(byte[] cipher, int cipherOffset, int cipherLen,
 198                       byte[] plain, int plainOffset)
 199          throws IllegalBlockSizeException, AEADBadTagException {
 200          decrypt(cipher, cipherOffset, cipherLen, plain, plainOffset);
 201          return cipherLen;
 202      }
 203 
 204     /**
 205      * Continues a multi-part update of the Additional Authentication
 206      * Data (AAD), using a subset of the provided buffer. If this
 207      * cipher is operating in either GCM or CCM mode, all AAD must be
 208      * supplied before beginning operations on the ciphertext (via the
 209      * {@code update} and {@code doFinal} methods).
 210      * <p>
 211      * NOTE: Given most modes do not accept AAD, default impl for this
 212      * method throws IllegalStateException.
 213      *
 214      * @param src the buffer containing the AAD
 215      * @param offset the offset in {@code src} where the AAD input starts
 216      * @param len the number of AAD bytes
 217      *
 218      * @throws IllegalStateException if this cipher is in a wrong state
 219      * (e.g., has not been initialized), does not accept AAD, or if
 220      * operating in either GCM or CCM mode and one of the {@code update}
 221      * methods has already been called for the active
 222      * encryption/decryption operation
 223      * @throws UnsupportedOperationException if this method
 224      * has not been overridden by an implementation
 225      *
 226      * @since 1.8
 227      */
 228     void updateAAD(byte[] src, int offset, int len) {
 229         throw new IllegalStateException("No AAD accepted");
 230     }
 231 }