ProcessBlock() public method

public ProcessBlock ( byte input, int inOff, byte output, int outOff ) : int
input byte
inOff int
output byte
outOff int
return int
コード例 #1
0
        public virtual int DoFinal(
            byte[]  outBytes,
            int outOff)
        {
            InitCipher();

            int extra = bufOff;

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

            bufOff = 0;

            if (forEncryption)
            {
                Check.OutputLength(outBytes, outOff, extra + macSize, "Output buffer too short");

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

                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)
                {
                    throw new InvalidCipherTextException("data too short");
                }

                Check.OutputLength(outBytes, outOff, extra - macSize, "Output buffer too short");

                if (extra > macSize)
                {
                    mac.BlockUpdate(bufBlock, 0, extra - macSize);

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

                    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);
            }
        }
コード例 #2
0
        public virtual int DoFinal(byte[] outBytes, int outOff)
        {
            InitCipher();
            int num = bufOff;

            byte[] array = new byte[bufBlock.Length];
            bufOff = 0;
            if (forEncryption)
            {
                Check.OutputLength(outBytes, outOff, num + macSize, "Output buffer too short");
                cipher.ProcessBlock(bufBlock, 0, array, 0);
                Array.Copy(array, 0, outBytes, outOff, num);
                mac.BlockUpdate(array, 0, num);
                CalculateMac();
                Array.Copy(macBlock, 0, outBytes, outOff + num, macSize);
                Reset(clearMac: false);
                return(num + macSize);
            }
            if (num < macSize)
            {
                throw new InvalidCipherTextException("data too short");
            }
            Check.OutputLength(outBytes, outOff, num - macSize, "Output buffer too short");
            if (num > macSize)
            {
                mac.BlockUpdate(bufBlock, 0, num - macSize);
                cipher.ProcessBlock(bufBlock, 0, array, 0);
                Array.Copy(array, 0, outBytes, outOff, num - macSize);
            }
            CalculateMac();
            if (!VerifyMac(bufBlock, num - macSize))
            {
                throw new InvalidCipherTextException("mac check in EAX failed");
            }
            Reset(clearMac: false);
            return(num - macSize);
        }
コード例 #3
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);
            }
        }
コード例 #4
0
ファイル: CcmBlockCipher.cs プロジェクト: ubberkid/PeerATT
        /**
         * Process a packet of data for either CCM decryption or encryption.
         *
         * @param in data for processing.
         * @param inOff offset at which data starts in the input array.
         * @param inLen length of the data in the input array.
         * @param output output array.
         * @param outOff offset into output array to start putting processed bytes.
         * @return the number of bytes added to output.
         * @throws IllegalStateException if the cipher is not appropriately set up.
         * @throws InvalidCipherTextException if the input data is truncated or the mac check fails.
         * @throws DataLengthException if output buffer too short.
         */
        public virtual int ProcessPacket(byte[] input, int inOff, int inLen, byte[] output, int outOff)
        {
            // TODO: handle null keyParam (e.g. via RepeatedKeySpec)
            // Need to keep the CTR and CBC Mac parts around and reset
            if (keyParam == null)
                throw new InvalidOperationException("CCM cipher unitialized.");

            int n = nonce.Length;
            int q = 15 - n;
            if (q < 4)
            {
                int limitLen = 1 << (8 * q);
                if (inLen >= limitLen)
                    throw new InvalidOperationException("CCM packet too large for choice of q.");
            }

            byte[] iv = new byte[BlockSize];
            iv[0] = (byte)((q - 1) & 0x7);
            nonce.CopyTo(iv, 1);

            IBlockCipher ctrCipher = new SicBlockCipher(cipher);
            ctrCipher.Init(forEncryption, new ParametersWithIV(keyParam, iv));

            int outputLen;
            int inIndex = inOff;
            int outIndex = outOff;

            if (forEncryption)
            {
                outputLen = inLen + macSize;
                Check.OutputLength(output, outOff, outputLen, "Output buffer too short.");

                calculateMac(input, inOff, inLen, macBlock);

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

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

                byte[] block = new byte[BlockSize];

                Array.Copy(input, inIndex, block, 0, inLen + inOff - inIndex);

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

                Array.Copy(block, 0, output, outIndex, inLen + inOff - inIndex);

                Array.Copy(macBlock, 0, output, outOff + inLen, macSize);
            }
            else
            {
                if (inLen < macSize)
                    throw new InvalidCipherTextException("data too short");

                outputLen = inLen - macSize;
                Check.OutputLength(output, outOff, outputLen, "Output buffer too short.");

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

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

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

                while (inIndex < (inOff + outputLen - BlockSize))
                {
                    ctrCipher.ProcessBlock(input, inIndex, output, outIndex);
                    outIndex += BlockSize;
                    inIndex += BlockSize;
                }

                byte[] block = new byte[BlockSize];

                Array.Copy(input, inIndex, block, 0, outputLen - (inIndex - inOff));

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

                Array.Copy(block, 0, output, outIndex, outputLen - (inIndex - inOff));

                byte[] calculatedMacBlock = new byte[BlockSize];

                calculateMac(output, outOff, outputLen, calculatedMacBlock);

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

            return outputLen;
        }
コード例 #5
0
        /**
         * Process a packet of data for either CCM decryption or encryption.
         *
         * @param in data for processing.
         * @param inOff offset at which data starts in the input array.
         * @param inLen length of the data in the input array.
         * @param output output array.
         * @param outOff offset into output array to start putting processed bytes.
         * @return the number of bytes added to output.
         * @throws IllegalStateException if the cipher is not appropriately set up.
         * @throws InvalidCipherTextException if the input data is truncated or the mac check fails.
         * @throws DataLengthException if output buffer too short.
         */
        public virtual int ProcessPacket(byte[] input, int inOff, int inLen, byte[] output, int outOff)
        {
            // TODO: handle null keyParam (e.g. via RepeatedKeySpec)
            // Need to keep the CTR and CBC Mac parts around and reset
            if (keyParam == null)
            {
                throw new InvalidOperationException("CCM cipher unitialized.");
            }

            int n = nonce.Length;
            int q = 15 - n;

            if (q < 4)
            {
                int limitLen = 1 << (8 * q);
                if (inLen >= limitLen)
                {
                    throw new InvalidOperationException("CCM packet too large for choice of q.");
                }
            }

            byte[] iv = new byte[BlockSize];
            iv[0] = (byte)((q - 1) & 0x7);
            nonce.CopyTo(iv, 1);

            IBlockCipher ctrCipher = new SicBlockCipher(cipher);

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

            int outputLen;
            int inIndex  = inOff;
            int outIndex = outOff;

            if (forEncryption)
            {
                outputLen = inLen + macSize;
                Check.OutputLength(output, outOff, outputLen, "Output buffer too short.");

                calculateMac(input, inOff, inLen, macBlock);

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

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

                byte[] block = new byte[BlockSize];

                Array.Copy(input, inIndex, block, 0, inLen + inOff - inIndex);

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

                Array.Copy(block, 0, output, outIndex, inLen + inOff - inIndex);

                Array.Copy(macBlock, 0, output, outOff + inLen, macSize);
            }
            else
            {
                if (inLen < macSize)
                {
                    throw new InvalidCipherTextException("data too short");
                }

                outputLen = inLen - macSize;
                Check.OutputLength(output, outOff, outputLen, "Output buffer too short.");

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

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

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

                while (inIndex < (inOff + outputLen - BlockSize))
                {
                    ctrCipher.ProcessBlock(input, inIndex, output, outIndex);
                    outIndex += BlockSize;
                    inIndex  += BlockSize;
                }

                byte[] block = new byte[BlockSize];

                Array.Copy(input, inIndex, block, 0, outputLen - (inIndex - inOff));

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

                Array.Copy(block, 0, output, outIndex, outputLen - (inIndex - inOff));

                byte[] calculatedMacBlock = new byte[BlockSize];

                calculateMac(output, outOff, outputLen, calculatedMacBlock);

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

            return(outputLen);
        }
コード例 #6
0
ファイル: CcmBlockCipher.cs プロジェクト: aata/flashcards-wp7
        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);
        }
コード例 #7
0
        public virtual int ProcessPacket(byte[] input, int inOff, int inLen, byte[] output, int outOff)
        {
            if (this.keyParam == null)
            {
                throw new InvalidOperationException("CCM cipher unitialized.");
            }
            int num  = this.nonce.Length;
            int num2 = 15 - num;

            if (num2 < 4)
            {
                int num3 = 1 << 8 * num2;
                if (inLen >= num3)
                {
                    throw new InvalidOperationException("CCM packet too large for choice of q.");
                }
            }
            byte[] array = new byte[CcmBlockCipher.BlockSize];
            array[0] = (byte)(num2 - 1 & 7);
            this.nonce.CopyTo(array, 1);
            IBlockCipher blockCipher = new SicBlockCipher(this.cipher);

            blockCipher.Init(this.forEncryption, new ParametersWithIV(this.keyParam, array));
            int i    = inOff;
            int num4 = outOff;
            int num5;

            if (this.forEncryption)
            {
                num5 = inLen + this.macSize;
                Check.OutputLength(output, outOff, num5, "Output buffer too short.");
                this.calculateMac(input, inOff, inLen, this.macBlock);
                blockCipher.ProcessBlock(this.macBlock, 0, this.macBlock, 0);
                while (i < inOff + inLen - CcmBlockCipher.BlockSize)
                {
                    blockCipher.ProcessBlock(input, i, output, num4);
                    num4 += CcmBlockCipher.BlockSize;
                    i    += CcmBlockCipher.BlockSize;
                }
                byte[] array2 = new byte[CcmBlockCipher.BlockSize];
                Array.Copy(input, i, array2, 0, inLen + inOff - i);
                blockCipher.ProcessBlock(array2, 0, array2, 0);
                Array.Copy(array2, 0, output, num4, inLen + inOff - i);
                Array.Copy(this.macBlock, 0, output, outOff + inLen, this.macSize);
            }
            else
            {
                if (inLen < this.macSize)
                {
                    throw new InvalidCipherTextException("data too short");
                }
                num5 = inLen - this.macSize;
                Check.OutputLength(output, outOff, num5, "Output buffer too short.");
                Array.Copy(input, inOff + num5, this.macBlock, 0, this.macSize);
                blockCipher.ProcessBlock(this.macBlock, 0, this.macBlock, 0);
                for (int num6 = this.macSize; num6 != this.macBlock.Length; num6++)
                {
                    this.macBlock[num6] = 0;
                }
                while (i < inOff + num5 - CcmBlockCipher.BlockSize)
                {
                    blockCipher.ProcessBlock(input, i, output, num4);
                    num4 += CcmBlockCipher.BlockSize;
                    i    += CcmBlockCipher.BlockSize;
                }
                byte[] array3 = new byte[CcmBlockCipher.BlockSize];
                Array.Copy(input, i, array3, 0, num5 - (i - inOff));
                blockCipher.ProcessBlock(array3, 0, array3, 0);
                Array.Copy(array3, 0, output, num4, num5 - (i - inOff));
                byte[] b = new byte[CcmBlockCipher.BlockSize];
                this.calculateMac(output, outOff, num5, b);
                if (!Arrays.ConstantTimeAreEqual(this.macBlock, b))
                {
                    throw new InvalidCipherTextException("mac check in CCM failed");
                }
            }
            return(num5);
        }
コード例 #8
0
ファイル: CcmBlockCipher.cs プロジェクト: hjgode/iTextSharpCF
        public byte[] ProcessPacket(byte[] input, int inOff, int inLen)
        {
            if (parameters == null)
            {
                throw new InvalidOperationException("CCM cipher unitialized.");
            }

            IBlockCipher ctrCipher = new SicBlockCipher(cipher);
            byte[] iv = new byte[blockSize];
            byte[] nonce = parameters.GetNonce();
            int    macSize = parameters.MacSize / 8;
            byte[] output;

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

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

            ctrCipher.Init(forEncryption, new ParametersWithIV(parameters.Key, 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 (!areEqual(macBlock, calculatedMacBlock))
                {
                    throw new InvalidCipherTextException("mac check in CCM failed");
                }
            }

            return output;
        }
コード例 #9
0
        public virtual int ProcessPacket(byte[] input, int inOff, int inLen, byte[] output, int outOff)
        {
            int num4;

            if (this.keyParam == null)
            {
                throw new InvalidOperationException("CCM cipher unitialized.");
            }
            int length = this.nonce.Length;
            int num2   = 15 - length;

            if (num2 < 4)
            {
                int num3 = ((int)1) << (8 * num2);
                if (inLen >= num3)
                {
                    throw new InvalidOperationException("CCM packet too large for choice of q.");
                }
            }
            byte[] array = new byte[BlockSize];
            array[0] = (byte)((num2 - 1) & 7);
            this.nonce.CopyTo(array, 1);
            IBlockCipher cipher = new SicBlockCipher(this.cipher);

            cipher.Init(this.forEncryption, new ParametersWithIV(this.keyParam, array));
            int num5 = inOff;
            int num6 = outOff;

            if (this.forEncryption)
            {
                num4 = inLen + this.macSize;
                Check.OutputLength(output, outOff, num4, "Output buffer too short.");
                this.CalculateMac(input, inOff, inLen, this.macBlock);
                byte[] outBuf = new byte[BlockSize];
                cipher.ProcessBlock(this.macBlock, 0, outBuf, 0);
                while (num5 < ((inOff + inLen) - BlockSize))
                {
                    cipher.ProcessBlock(input, num5, output, num6);
                    num6 += BlockSize;
                    num5 += BlockSize;
                }
                byte[] buffer3 = new byte[BlockSize];
                Array.Copy(input, num5, buffer3, 0, (inLen + inOff) - num5);
                cipher.ProcessBlock(buffer3, 0, buffer3, 0);
                Array.Copy(buffer3, 0, output, num6, (inLen + inOff) - num5);
                Array.Copy(outBuf, 0, output, outOff + inLen, this.macSize);
                return(num4);
            }
            if (inLen < this.macSize)
            {
                throw new InvalidCipherTextException("data too short");
            }
            num4 = inLen - this.macSize;
            Check.OutputLength(output, outOff, num4, "Output buffer too short.");
            Array.Copy(input, inOff + num4, this.macBlock, 0, this.macSize);
            cipher.ProcessBlock(this.macBlock, 0, this.macBlock, 0);
            for (int i = this.macSize; i != this.macBlock.Length; i++)
            {
                this.macBlock[i] = 0;
            }
            while (num5 < ((inOff + num4) - BlockSize))
            {
                cipher.ProcessBlock(input, num5, output, num6);
                num6 += BlockSize;
                num5 += BlockSize;
            }
            byte[] destinationArray = new byte[BlockSize];
            Array.Copy(input, num5, destinationArray, 0, num4 - (num5 - inOff));
            cipher.ProcessBlock(destinationArray, 0, destinationArray, 0);
            Array.Copy(destinationArray, 0, output, num6, num4 - (num5 - inOff));
            byte[] macBlock = new byte[BlockSize];
            this.CalculateMac(output, outOff, num4, macBlock);
            if (!Arrays.ConstantTimeAreEqual(this.macBlock, macBlock))
            {
                throw new InvalidCipherTextException("mac check in CCM failed");
            }
            return(num4);
        }
コード例 #10
0
        public byte[] ProcessPacket(
			byte[]	input,
			int		inOff,
			int		inLen)
        {
            // TODO: handle null keyParam (e.g. via RepeatedKeySpec)
            // Need to keep the CTR and CBC Mac parts around and reset
            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;
        }
コード例 #11
0
        public virtual int ProcessPacket(byte[] input, int inOff, int inLen, byte[] output, int outOff)
        {
            //IL_000d: Unknown result type (might be due to invalid IL or missing references)
            //IL_0037: Unknown result type (might be due to invalid IL or missing references)
            if (keyParam == null)
            {
                throw new InvalidOperationException("CCM cipher unitialized.");
            }
            int num  = nonce.Length;
            int num2 = 15 - num;

            if (num2 < 4)
            {
                int num3 = 1 << 8 * num2;
                if (inLen >= num3)
                {
                    throw new InvalidOperationException("CCM packet too large for choice of q.");
                }
            }
            byte[] array = new byte[BlockSize];
            array[0] = (byte)((uint)(num2 - 1) & 7u);
            ((global::System.Array)nonce).CopyTo((global::System.Array)array, 1);
            IBlockCipher blockCipher = new SicBlockCipher(cipher);

            blockCipher.Init(forEncryption, new ParametersWithIV(keyParam, array));
            int i    = inOff;
            int num4 = outOff;
            int num5;

            if (forEncryption)
            {
                num5 = inLen + macSize;
                Check.OutputLength(output, outOff, num5, "Output buffer too short.");
                CalculateMac(input, inOff, inLen, macBlock);
                byte[] array2 = new byte[BlockSize];
                blockCipher.ProcessBlock(macBlock, 0, array2, 0);
                for (; i < inOff + inLen - BlockSize; i += BlockSize)
                {
                    blockCipher.ProcessBlock(input, i, output, num4);
                    num4 += BlockSize;
                }
                byte[] array3 = new byte[BlockSize];
                global::System.Array.Copy((global::System.Array)input, i, (global::System.Array)array3, 0, inLen + inOff - i);
                blockCipher.ProcessBlock(array3, 0, array3, 0);
                global::System.Array.Copy((global::System.Array)array3, 0, (global::System.Array)output, num4, inLen + inOff - i);
                global::System.Array.Copy((global::System.Array)array2, 0, (global::System.Array)output, outOff + inLen, macSize);
            }
            else
            {
                if (inLen < macSize)
                {
                    throw new InvalidCipherTextException("data too short");
                }
                num5 = inLen - macSize;
                Check.OutputLength(output, outOff, num5, "Output buffer too short.");
                global::System.Array.Copy((global::System.Array)input, inOff + num5, (global::System.Array)macBlock, 0, macSize);
                blockCipher.ProcessBlock(macBlock, 0, macBlock, 0);
                for (int j = macSize; j != macBlock.Length; j++)
                {
                    macBlock[j] = 0;
                }
                for (; i < inOff + num5 - BlockSize; i += BlockSize)
                {
                    blockCipher.ProcessBlock(input, i, output, num4);
                    num4 += BlockSize;
                }
                byte[] array4 = new byte[BlockSize];
                global::System.Array.Copy((global::System.Array)input, i, (global::System.Array)array4, 0, num5 - (i - inOff));
                blockCipher.ProcessBlock(array4, 0, array4, 0);
                global::System.Array.Copy((global::System.Array)array4, 0, (global::System.Array)output, num4, num5 - (i - inOff));
                byte[] b = new byte[BlockSize];
                CalculateMac(output, outOff, num5, b);
                if (!Arrays.ConstantTimeAreEqual(macBlock, b))
                {
                    throw new InvalidCipherTextException("mac check in CCM failed");
                }
            }
            return(num5);
        }