Example #1
0
        /**
         * Basic constructor.
         *
         * @param cipher the block cipher to be used as the basis of the
         * feedback mode.
         * @param blockSize the block size in bits (note: a multiple of 8)
         */
        public CFBBlockCipher(
            BlockCipher cipher,
            int bitBlockSize)
        {
            this.cipher    = cipher;
            this.blockSize = bitBlockSize / 8;

            this.IV      = new byte[cipher.getBlockSize()];
            this.cfbV    = new byte[cipher.getBlockSize()];
            this.cfbOutV = new byte[cipher.getBlockSize()];
        }
Example #2
0
        /**
         * Create a buffered block cipher without padding.
         *
         * @param cipher the underlying block cipher this buffering object wraps.
         * @param padded true if the buffer should add, or remove, pad bytes,
         * false otherwise.
         */
        public BufferedBlockCipher(
            BlockCipher cipher)
        {
            this.cipher = cipher;

            buf    = new byte[cipher.getBlockSize()];
            bufOff = 0;

            //
            // check if we can handle partial blocks on doFinal.
            //
            String name = cipher.getAlgorithmName();
            int    idx  = name.IndexOf('/') + 1;

            pgpCFB = (idx > 0 && name.Substring(idx).StartsWith("PGP"));

            if (pgpCFB)
            {
                partialBlockOkay = true;
            }
            else
            {
                partialBlockOkay = (idx > 0 && (name.Substring(idx).StartsWith("CFB") ||
                                                name.Substring(idx).StartsWith("OFB")));
            }
        }
Example #3
0
 /**
  * Basic constructor.
  *
  * @param c the block cipher to be used.
  */
 public SICBlockCipher(BlockCipher c)
 {
     this.cipher     = c;
     this.blockSize  = cipher.getBlockSize();
     this.IV         = new byte[blockSize];
     this.counter    = new byte[blockSize];
     this.counterOut = new byte[blockSize];
 }
Example #4
0
        /**
         * Basic constructor.
         *
         * @param cipher the block cipher to be used as the basis of chaining.
         */
        public CBCBlockCipher(
            BlockCipher cipher)
        {
            this.cipher    = cipher;
            this.blockSize = cipher.getBlockSize();

            this.IV       = new byte[blockSize];
            this.cbcV     = new byte[blockSize];
            this.cbcNextV = new byte[blockSize];
        }
Example #5
0
        /**
         * Create a buffered block cipher with the desired padding.
         *
         * @param cipher the underlying block cipher this buffering object wraps.
         * @param padding the padding type.
         */
        public PaddedBufferedBlockCipher(
            BlockCipher cipher,
            BlockCipherPadding padding)
        {
            this.cipher  = cipher;
            this.padding = padding;

            buf    = new byte[cipher.getBlockSize()];
            bufOff = 0;
        }
Example #6
0
        /**
         * basic constructor.
         *
         * @param cipher the block cipher to be wrapped.
         * @exception IllegalArgumentException if the cipher has a block size other than
         * one.
         */
        public StreamBlockCipher(
            BlockCipher cipher)
        {
            if (cipher.getBlockSize() != 1)
            {
                throw new ArgumentException("block cipher block size != 1.");
            }

            this.cipher = cipher;
        }
Example #7
0
        /**
         * create a standard MAC based on a block cipher with the size of the
         * MAC been given in bits. This class uses CBC mode as the basis for the
         * MAC generation.
         * <p>
         * Note: the size of the MAC must be at least 24 bits (FIPS Publication 81),
         * or 16 bits if being used as a data authenticator (FIPS Publication 113),
         * and in general should be less than the size of the block cipher as it reduces
         * the chance of an exhaustive attack (see Handbook of Applied Cryptography).
         *
         * @param cipher the cipher to be used as the basis of the MAC generation.
         * @param macSizeInBits the size of the MAC in bits, must be a multiple of 8.
         * @param padding the padding to be used to complete the last block.
         */
        public CBCBlockCipherMac(
            BlockCipher cipher,
            int macSizeInBits,
            BlockCipherPadding padding)
        {
            if ((macSizeInBits % 8) != 0)
            {
                throw new ArgumentException("MAC size must be multiple of 8");
            }

            this.cipher  = new CBCBlockCipher(cipher);
            this.padding = padding;
            this.macSize = macSizeInBits / 8;

            mac = new byte[cipher.getBlockSize()];

            buf    = new byte[cipher.getBlockSize()];
            bufOff = 0;
        }
Example #8
0
        public void update(
            byte[]      inBytes,
            int inOff,
            int len)
        {
            if (len < 0)
            {
                throw new ArgumentException("Can't have a negative input length!");
            }

            int blockSize = cipher.getBlockSize();
            int resultLen = 0;
            int gapLen    = blockSize - bufOff;

            if (len > gapLen)
            {
                Array.Copy(inBytes, inOff, buf, bufOff, gapLen);

                resultLen += cipher.processBlock(buf, 0, mac, 0);

                bufOff = 0;
                len   -= gapLen;
                inOff += gapLen;

                while (len > blockSize)
                {
                    resultLen += cipher.processBlock(inBytes, inOff, mac, 0);

                    len   -= blockSize;
                    inOff += blockSize;
                }
            }

            Array.Copy(inBytes, inOff, buf, bufOff, len);

            bufOff += len;
        }
Example #9
0
 /**
  * return the block size of the underlying cipher.
  *
  * @return the block size of the underlying cipher.
  */
 public int getBlockSize()
 {
     return(cipher.getBlockSize());
 }
Example #10
0
 /**
  * create a standard MAC based on a CBC block cipher. This will produce an
  * authentication code half the length of the block size of the cipher.
  *
  * @param cipher the cipher to be used as the basis of the MAC generation.
  * @param padding the padding to be used to complete the last block.
  */
 public CBCBlockCipherMac(
     BlockCipher cipher,
     BlockCipherPadding padding)
     : this(cipher, (cipher.getBlockSize() * 8) / 2, padding)
 {
 }
Example #11
0
 /**
  * create a standard MAC based on a CBC block cipher. This will produce an
  * authentication code half the length of the block size of the cipher.
  *
  * @param cipher the cipher to be used as the basis of the MAC generation.
  */
 public CBCBlockCipherMac(BlockCipher cipher)
     : this(cipher, (cipher.getBlockSize() * 8) / 2, null)
 {
 }
Example #12
0
 /**
  * return the blocksize for the underlying cipher.
  *
  * @return the blocksize for the underlying cipher.
  */
 public virtual int getBlockSize()
 {
     return(cipher.getBlockSize());
 }