예제 #1
0
        public void Encrypt(ReadOnlySpan <byte> input, Span <byte> output)
        {
            int length     = Math.Min(input.Length, output.Length);
            int blockCount = length >> 4;
            int leftover   = length & 0xF;

            // Data units must be at least 1 block long.
            if (length < Aes.BlockSize)
            {
                throw new ArgumentException();
            }

            var tweak = new Buffer16();

            _tweakAesCore.Encrypt(Iv, tweak);

            using var tweakBuffer = new RentedArray <byte>(blockCount * Aes.BlockSize);
            tweak = FillTweakBuffer(tweak, MemoryMarshal.Cast <byte, Buffer16>(tweakBuffer.Span));

            Utilities.XorArrays(output, input, tweakBuffer.Span);
            _dataAesCore.Encrypt(output.Slice(0, blockCount * Aes.BlockSize), output);
            Utilities.XorArrays(output, output, tweakBuffer.Array);

            if (leftover != 0)
            {
                Buffer16 inBlock =
                    Unsafe.Add(ref Unsafe.As <byte, Buffer16>(ref MemoryMarshal.GetReference(input)), blockCount);

                ref Buffer16 outBlock =
                    ref Unsafe.Add(ref Unsafe.As <byte, Buffer16>(ref MemoryMarshal.GetReference(output)), blockCount);

                ref Buffer16 prevOutBlock = ref Unsafe.Subtract(ref outBlock, 1);
예제 #2
0
        public void Transform(ReadOnlySpan<byte> input, Span<byte> output)
        {
            int blockCount = BitUtil.DivideUp(input.Length, Aes.BlockSize);
            int length = blockCount * Aes.BlockSize;

            using var counterBuffer = new RentedArray<byte>(length);
            FillDecryptedCounter(Iv, counterBuffer.Span);

            _aesCore.Encrypt(counterBuffer.Array, counterBuffer.Array, length);
            Utilities.XorArrays(output, input, counterBuffer.Span);
        }
예제 #3
0
 public void Encrypt(ReadOnlySpan <byte> input, Span <byte> output)
 {
     _aesCore.Encrypt(input, output);
 }