Beispiel #1
0
 /**
  * Constructor that accepts an instance of a block cipher engine.
  *
  * @param cipher the engine to use
  */
 public EaxBlockCipher(
     IBlockCipher cipher)
 {
     blockSize         = cipher.GetBlockSize();
     mac               = new CMac(cipher);
     macBlock          = new byte[blockSize];
     bufBlock          = new byte[blockSize * 2];
     associatedTextMac = new byte[mac.GetMacSize()];
     nonceMac          = new byte[mac.GetMacSize()];
     this.cipher       = new SicBlockCipher(cipher);
 }
Beispiel #2
0
        public byte[] ProcessPacket(
            byte[]  input,
            int inOff,
            int inLen)
        {
            if (keyParam == null)
            {
                throw new InvalidOperationException("CCM cipher unitialized.");
            }

            IBlockCipher ctrCipher = new SicBlockCipher(cipher);

            byte[] iv = new byte[BlockSize];
            byte[] output;

            iv[0] = (byte)(((15 - nonce.Length) - 1) & 0x7);

            Array.Copy(nonce, 0, iv, 1, nonce.Length);

            ctrCipher.Init(forEncryption, new ParametersWithIV(keyParam, iv));

            if (forEncryption)
            {
                int index  = inOff;
                int outOff = 0;

                output = new byte[inLen + macSize];

                calculateMac(input, inOff, inLen, macBlock);

                ctrCipher.ProcessBlock(macBlock, 0, macBlock, 0);   // S0

                while (index < inLen - BlockSize)                   // S1...
                {
                    ctrCipher.ProcessBlock(input, index, output, outOff);
                    outOff += BlockSize;
                    index  += BlockSize;
                }

                byte[] block = new byte[BlockSize];

                Array.Copy(input, index, block, 0, inLen - index);

                ctrCipher.ProcessBlock(block, 0, block, 0);

                Array.Copy(block, 0, output, outOff, inLen - index);

                outOff += inLen - index;

                Array.Copy(macBlock, 0, output, outOff, output.Length - outOff);
            }
            else
            {
                int index  = inOff;
                int outOff = 0;

                output = new byte[inLen - macSize];

                Array.Copy(input, inOff + inLen - macSize, macBlock, 0, macSize);

                ctrCipher.ProcessBlock(macBlock, 0, macBlock, 0);

                for (int i = macSize; i != macBlock.Length; i++)
                {
                    macBlock[i] = 0;
                }

                while (outOff < output.Length - BlockSize)
                {
                    ctrCipher.ProcessBlock(input, index, output, outOff);
                    outOff += BlockSize;
                    index  += BlockSize;
                }

                byte[] block = new byte[BlockSize];

                Array.Copy(input, index, block, 0, output.Length - outOff);

                ctrCipher.ProcessBlock(block, 0, block, 0);

                Array.Copy(block, 0, output, outOff, output.Length - outOff);

                byte[] calculatedMacBlock = new byte[BlockSize];

                calculateMac(output, 0, output.Length, calculatedMacBlock);

                if (!Arrays.ConstantTimeAreEqual(macBlock, calculatedMacBlock))
                {
                    throw new InvalidCipherTextException("mac check in CCM failed");
                }
            }

            return(output);
        }