Save This Page
Home » crypto-143 » org.bouncycastle.crypto » [javadoc | source]
    1   package org.bouncycastle.crypto;
    2   
    3   /**
    4    * a buffer wrapper for an asymmetric block cipher, allowing input
    5    * to be accumulated in a piecemeal fashion until final processing.
    6    */
    7   public class BufferedAsymmetricBlockCipher
    8   {
    9       protected byte[]        buf;
   10       protected int           bufOff;
   11   
   12       private final AsymmetricBlockCipher   cipher;
   13   
   14       /**
   15        * base constructor.
   16        *
   17        * @param cipher the cipher this buffering object wraps.
   18        */
   19       public BufferedAsymmetricBlockCipher(
   20           AsymmetricBlockCipher     cipher)
   21       {
   22           this.cipher = cipher;
   23       }
   24   
   25       /**
   26        * return the underlying cipher for the buffer.
   27        *
   28        * @return the underlying cipher for the buffer.
   29        */
   30       public AsymmetricBlockCipher getUnderlyingCipher()
   31       {
   32           return cipher;
   33       }
   34   
   35       /**
   36        * return the amount of data sitting in the buffer.
   37        *
   38        * @return the amount of data sitting in the buffer.
   39        */
   40       public int getBufferPosition()
   41       {
   42           return bufOff;
   43       }
   44   
   45       /**
   46        * initialise the buffer and the underlying cipher.
   47        *
   48        * @param forEncryption if true the cipher is initialised for
   49        *  encryption, if false for decryption.
   50        * @param params the key and other data required by the cipher.
   51        */
   52       public void init(
   53           boolean             forEncryption,
   54           CipherParameters    params)
   55       {
   56           reset();
   57   
   58           cipher.init(forEncryption, params);
   59   
   60           //
   61           // we allow for an extra byte where people are using their own padding
   62           // mechanisms on a raw cipher.
   63           //
   64           buf = new byte[cipher.getInputBlockSize() + (forEncryption ? 1 : 0)];
   65           bufOff = 0;
   66       }
   67   
   68       /**
   69        * returns the largest size an input block can be.
   70        *
   71        * @return maximum size for an input block.
   72        */
   73       public int getInputBlockSize()
   74       {
   75           return cipher.getInputBlockSize();
   76       }
   77   
   78       /**
   79        * returns the maximum size of the block produced by this cipher.
   80        *
   81        * @return maximum size of the output block produced by the cipher.
   82        */
   83       public int getOutputBlockSize()
   84       {
   85           return cipher.getOutputBlockSize();
   86       }
   87   
   88       /**
   89        * add another byte for processing.
   90        * 
   91        * @param in the input byte.
   92        */
   93       public void processByte(
   94           byte        in)
   95       {
   96           if (bufOff >= buf.length)
   97           {
   98               throw new DataLengthException("attempt to process message too long for cipher");
   99           }
  100   
  101           buf[bufOff++] = in;
  102       }
  103   
  104       /**
  105        * add len bytes to the buffer for processing.
  106        *
  107        * @param in the input data
  108        * @param inOff offset into the in array where the data starts
  109        * @param len the length of the block to be processed.
  110        */
  111       public void processBytes(
  112           byte[]      in,
  113           int         inOff,
  114           int         len)
  115       {
  116           if (len == 0)
  117           {
  118               return;
  119           }
  120   
  121           if (len < 0)
  122           {
  123               throw new IllegalArgumentException("Can't have a negative input length!");
  124           }
  125   
  126           if (bufOff + len > buf.length)
  127           {
  128               throw new DataLengthException("attempt to process message too long for cipher");
  129           }
  130   
  131           System.arraycopy(in, inOff, buf, bufOff, len);
  132           bufOff += len;
  133       }
  134   
  135       /**
  136        * process the contents of the buffer using the underlying
  137        * cipher.
  138        *
  139        * @return the result of the encryption/decryption process on the
  140        * buffer.
  141        * @exception InvalidCipherTextException if we are given a garbage block.
  142        */
  143       public byte[] doFinal()
  144           throws InvalidCipherTextException
  145       {
  146           byte[] out = cipher.processBlock(buf, 0, bufOff);
  147   
  148           reset();
  149   
  150           return out;
  151       }
  152   
  153       /**
  154        * Reset the buffer and the underlying cipher.
  155        */
  156       public void reset()
  157       {
  158           /*
  159            * clean the buffer.
  160            */
  161           if (buf != null)
  162           {
  163               for (int i = 0; i < buf.length; i++)
  164               {
  165                   buf[0] = 0;
  166               }
  167           }
  168   
  169           bufOff = 0;
  170       }
  171   }

Save This Page
Home » crypto-143 » org.bouncycastle.crypto » [javadoc | source]