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 /**
  33  * This class represents ciphers in cipher block chaining (CBC) 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 be
  37  * <i>plugged-in</i> using the constructor.
  38  *
  39  * <p>NOTE: This class does not deal with buffering or padding.
  40  *
  41  * @author Gigi Ankeny
  42  */
  43 
  44 class CipherBlockChaining extends FeedbackCipher  {
  45 
  46     /*
  47      * random bytes that are initialized with iv
  48      */
  49     protected byte[] r;
  50 
  51     /*
  52      * output buffer
  53      */
  54     private byte[] k;
  55 
  56     // variables for save/restore calls
  57     private byte[] rSave = null;
  58 
  59     CipherBlockChaining(SymmetricCipher embeddedCipher) {
  60         super(embeddedCipher);
  61         k = new byte[blockSize];
  62         r = new byte[blockSize];
  63     }
  64 
  65     /**
  66      * Gets the name of this feedback mode.
  67      *
  68      * @return the string <code>CBC</code>
  69      */
  70     String getFeedback() {
  71         return "CBC";
  72     }
  73 
  74     /**
  75      * Initializes the cipher in the specified mode with the given key
  76      * and iv.
  77      *
  78      * @param decrypting flag indicating encryption or decryption
  79      * @param algorithm the algorithm name
  80      * @param key the key
  81      * @param iv the iv
  82      *
  83      * @exception InvalidKeyException if the given key is inappropriate for
  84      * initializing this cipher
  85      */
  86     void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)
  87             throws InvalidKeyException {
  88         if ((key == null) || (iv == null) || (iv.length != blockSize)) {
  89             throw new InvalidKeyException("Internal error");
  90         }
  91         this.iv = iv;
  92         reset();
  93         embeddedCipher.init(decrypting, algorithm, key);
  94     }
  95 
  96     /**
  97      * Resets the iv to its original value.
  98      * This is used when doFinal is called in the Cipher class, so that the
  99      * cipher can be reused (with its original iv).
 100      */
 101     void reset() {
 102         System.arraycopy(iv, 0, r, 0, blockSize);
 103     }
 104 
 105     /**
 106      * Save the current content of this cipher.
 107      */
 108     void save() {
 109         if (rSave == null) {
 110             rSave = new byte[blockSize];
 111         }
 112         System.arraycopy(r, 0, rSave, 0, blockSize);
 113     }
 114 
 115     /**
 116      * Restores the content of this cipher to the previous saved one.
 117      */
 118     void restore() {
 119         System.arraycopy(rSave, 0, r, 0, blockSize);
 120     }
 121 
 122     /**
 123      * Performs encryption operation.
 124      *
 125      * <p>The input plain text <code>plain</code>, starting at
 126      * <code>plainOffset</code> and ending at
 127      * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
 128      * The result is stored in <code>cipher</code>, starting at
 129      * <code>cipherOffset</code>.
 130      *
 131      * @param plain the buffer with the input 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 result
 135      * @param cipherOffset the offset in <code>cipher</code>
 136      * @exception ProviderException if <code>len</code> is not
 137      * a multiple of the block size
 138      * @return the length of the encrypted data
 139      */
 140     int encrypt(byte[] plain, int plainOffset, int plainLen,
 141                 byte[] cipher, int cipherOffset)
 142     {
 143         if ((plainLen % blockSize) != 0) {
 144             throw new ProviderException("Internal error in input buffering");
 145         }
 146         int endIndex = plainOffset + plainLen;
 147 
 148         for (; plainOffset < endIndex;
 149              plainOffset+=blockSize, cipherOffset += blockSize) {
 150             for (int i = 0; i < blockSize; i++) {
 151                 k[i] = (byte)(plain[i + plainOffset] ^ r[i]);
 152             }
 153             embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset);
 154             System.arraycopy(cipher, cipherOffset, r, 0, blockSize);
 155         }
 156         return plainLen;
 157     }
 158 
 159     /**
 160      * Performs decryption operation.
 161      *
 162      * <p>The input cipher text <code>cipher</code>, starting at
 163      * <code>cipherOffset</code> and ending at
 164      * <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.
 165      * The result is stored in <code>plain</code>, starting at
 166      * <code>plainOffset</code>.
 167      *
 168      * <p>It is also the application's responsibility to make sure that
 169      * <code>init</code> has been called before this method is called.
 170      * (This check is omitted here, to avoid double checking.)
 171      *
 172      * @param cipher the buffer with the input data to be decrypted
 173      * @param cipherOffset the offset in <code>cipherOffset</code>
 174      * @param cipherLen the length of the input data
 175      * @param plain the buffer for the result
 176      * @param plainOffset the offset in <code>plain</code>
 177      * @exception ProviderException if <code>len</code> is not
 178      * a multiple of the block size
 179      * @return the length of the decrypted data
 180      */
 181     int decrypt(byte[] cipher, int cipherOffset, int cipherLen,
 182                 byte[] plain, int plainOffset)
 183     {
 184         if ((cipherLen % blockSize) != 0) {
 185             throw new ProviderException("Internal error in input buffering");
 186         }
 187         int endIndex = cipherOffset + cipherLen;
 188 
 189         for (; cipherOffset < endIndex;
 190              cipherOffset += blockSize, plainOffset += blockSize) {
 191             embeddedCipher.decryptBlock(cipher, cipherOffset, k, 0);
 192             for (int i = 0; i < blockSize; i++) {
 193                 plain[i + plainOffset] = (byte)(k[i] ^ r[i]);
 194             }
 195             System.arraycopy(cipher, cipherOffset, r, 0, blockSize);
 196         }
 197         return cipherLen;
 198     }
 199 }