Beispiel #1
0
        public int DecryptFinal(byte[] input, int inputOffset, byte[] output, int outputOffset)
        {
            SecurityBufferAssert.AssertBuffer(input, inputOffset, TagLength);
            SecurityBufferAssert.AssertBuffer(output, outputOffset, bufferOffset);

            var total = 0;
            if (bufferOffset != 0)
            {
                total += bufferOffset;
                inputOffset += bufferOffset;

                DecryptBlock(output, outputOffset);
            }

            var tagCiphertextPaddingLength = (16 - (int)(cSize / 8) % 16) % 16;
            tagHash.Update(new byte[tagCiphertextPaddingLength], 0, tagCiphertextPaddingLength);
            tagHash.Update(EndianBitConverter.Big.GetBytes(aSize), 0, sizeof(long));
            tagHash.Update(EndianBitConverter.Big.GetBytes(cSize), 0, sizeof(long));

            var ctr = new CTRBlockCipher(Cipher);
            ctr.Init(new IVParameter(null, j0));

            var calculatedTag = new byte[16];
            ctr.EncryptBlock(tagHash.Digest(), 0, calculatedTag, 0);

            var tag = new byte[16];
            Array.Copy(input, inputOffset, tag, 0, TagLength);

            SecurityAssert.HashAssert(calculatedTag, tag);

            return total;
        }
Beispiel #2
0
        public int EncryptFinal(byte[] output, int offset)
        {
            var total = 0;

            if (bufferOffset != 0)
            {
                var len = bufferOffset;

                EncryptBlock(output, offset);

                total += len;
                offset += len;
            }

            var tagCiphertextPaddingLength = (16 - (int)(cSize / 8) % 16) % 16;
            tagHash.Update(new byte[tagCiphertextPaddingLength], 0, tagCiphertextPaddingLength);
            tagHash.Update(EndianBitConverter.Big.GetBytes(aSize), 0, sizeof(long));
            tagHash.Update(EndianBitConverter.Big.GetBytes(cSize), 0, sizeof(long));

            var ctr = new CTRBlockCipher(Cipher);
            ctr.Init(new IVParameter(null, j0));

            ctr.EncryptBlock(tagHash.Digest(), 0, output, offset);
            total += BlockLength;

            return total;
        }