示例#1
0
        public virtual int DoFinal(
            byte[]  outBytes,
            int outOff)
        {
            int extra = bufOff;

            byte[] tmp = new byte[bufBlock.Length];

            bufOff = 0;

            if (forEncryption)
            {
                cipher.ProcessBlock(bufBlock, 0, tmp, 0);
                cipher.ProcessBlock(bufBlock, blockSize, tmp, blockSize);

                Array.Copy(tmp, 0, outBytes, outOff, extra);

                mac.BlockUpdate(tmp, 0, extra);

                calculateMac();

                Array.Copy(macBlock, 0, outBytes, outOff + extra, macSize);

                Reset(false);

                return(extra + macSize);
            }
            else
            {
                if (extra > macSize)
                {
                    mac.BlockUpdate(bufBlock, 0, extra - macSize);

                    cipher.ProcessBlock(bufBlock, 0, tmp, 0);
                    cipher.ProcessBlock(bufBlock, blockSize, tmp, blockSize);

                    Array.Copy(tmp, 0, outBytes, outOff, extra - macSize);
                }

                calculateMac();

                if (!verifyMac(bufBlock, extra - macSize))
                {
                    throw new InvalidCipherTextException("mac check in EAX failed");
                }

                Reset(false);

                return(extra - macSize);
            }
        }
		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;
        }
示例#3
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);
        }