Save This Page
Home » openjdk-7 » com.sun.crypto » provider » [javadoc | source]
    1   /*
    2    * Copyright (c) 1997, 2007, 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.IllegalBlockSizeException;
   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        */
  154        void encryptFinal(byte[] plain, int plainOffset, int plainLen,
  155                          byte[] cipher, int cipherOffset)
  156            throws IllegalBlockSizeException {
  157            encrypt(plain, plainOffset, plainLen, cipher, cipherOffset);
  158        }
  159       /**
  160        * Performs decryption operation.
  161        *
  162        * <p>The input <code>cipher</code>, starting at <code>cipherOffset</code>
  163        * and ending at <code>(cipherOffset+cipherLen-1)</code>, is decrypted.
  164        * The result is stored in <code>plain</code>, starting at
  165        * <code>plainOffset</code>.
  166        *
  167        * <p>The subclass that implements Cipher should ensure that
  168        * <code>init</code> has been called before this method is called.
  169        *
  170        * @param cipher the input buffer with the data to be decrypted
  171        * @param cipherOffset the offset in <code>cipher</code>
  172        * @param cipherLen the length of the input data
  173        * @param plain the buffer for the decryption result
  174        * @param plainOffset the offset in <code>plain</code>
  175        */
  176       abstract void decrypt(byte[] cipher, int cipherOffset, int cipherLen,
  177                             byte[] plain, int plainOffset);
  178   
  179       /**
  180        * Performs decryption operation for the last time.
  181        *
  182        * <p>NOTE: For cipher feedback modes which does not perform
  183        * special handling for the last few blocks, this is essentially
  184        * the same as <code>encrypt(...)</code>. Given most modes do
  185        * not do special handling, the default impl for this method is
  186        * to simply call <code>decrypt(...)</code>.
  187        *
  188        * @param cipher the input buffer with the data to be decrypted
  189        * @param cipherOffset the offset in <code>cipher</code>
  190        * @param cipherLen the length of the input data
  191        * @param plain the buffer for the decryption result
  192        * @param plainOffset the offset in <code>plain</code>
  193        */
  194        void decryptFinal(byte[] cipher, int cipherOffset, int cipherLen,
  195                          byte[] plain, int plainOffset)
  196            throws IllegalBlockSizeException {
  197            decrypt(cipher, cipherOffset, cipherLen, plain, plainOffset);
  198        }
  199   }

Save This Page
Home » openjdk-7 » com.sun.crypto » provider » [javadoc | source]