Exemple #1
0
            public void RunStructFldScenario(AesBinaryOpTest__DecryptLastByte testClass)
            {
                var result = Aes.DecryptLast(_fld1, _fld2);

                Unsafe.Write(testClass._dataTable.outArrayPtr, result);
                testClass.ValidateResult(testClass._dataTable.outArrayPtr);
            }
Exemple #2
0
        protected Vector128 <byte> DecryptAes(Vector128 <byte> data)
        {
            if (DecryptionKey == null)
            {
                DecryptionKey = GenerateWorkingKey(Key, false);
            }

            data = Sse2.Xor(data, DecryptionKey[Rounds]);
            // unrolled for performance
            if (Rounds > 12)
            {
                data = Aes.Decrypt(data, DecryptionKey[13]);
                data = Aes.Decrypt(data, DecryptionKey[12]);
            }

            if (Rounds > 10)
            {
                data = Aes.Decrypt(data, DecryptionKey[11]);
                data = Aes.Decrypt(data, DecryptionKey[10]);
            }

            data = Aes.Decrypt(data, DecryptionKey[9]);
            data = Aes.Decrypt(data, DecryptionKey[8]);
            data = Aes.Decrypt(data, DecryptionKey[7]);
            data = Aes.Decrypt(data, DecryptionKey[6]);
            data = Aes.Decrypt(data, DecryptionKey[5]);
            data = Aes.Decrypt(data, DecryptionKey[4]);
            data = Aes.Decrypt(data, DecryptionKey[3]);
            data = Aes.Decrypt(data, DecryptionKey[2]);
            data = Aes.Decrypt(data, DecryptionKey[1]);
            return(Aes.DecryptLast(data, DecryptionKey[0]));
        }
Exemple #3
0
        public override unsafe void Decrypt(ReadOnlySpan <byte> source, Span <byte> destination)
        {
            base.Decrypt(source, destination);

            Vector128 <byte> t;

            fixed(byte *s = source)
            {
                t = Sse2.LoadVector128(s);
            }

            t = Sse2.Xor(t, _k14);
            t = Aes.Decrypt(t, _k15);
            t = Aes.Decrypt(t, _k16);
            t = Aes.Decrypt(t, _k17);
            t = Aes.Decrypt(t, _k18);
            t = Aes.Decrypt(t, _k19);
            t = Aes.Decrypt(t, _k20);
            t = Aes.Decrypt(t, _k21);
            t = Aes.Decrypt(t, _k22);
            t = Aes.Decrypt(t, _k23);
            t = Aes.Decrypt(t, _k24);
            t = Aes.Decrypt(t, _k25);
            t = Aes.Decrypt(t, _k26);
            t = Aes.Decrypt(t, _k27);
            t = Aes.DecryptLast(t, _k0);

            fixed(byte *d = destination)
            {
                Sse2.Store(d, t);
            }
        }
Exemple #4
0
        public void RunClassFldScenario()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunClassFldScenario));

            var result = Aes.DecryptLast(_fld1, _fld2);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(_dataTable.outArrayPtr);
        }
Exemple #5
0
        public void RunClassLclFldScenario()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunClassLclFldScenario));

            var test   = new AesBinaryOpTest__DecryptLastByte();
            var result = Aes.DecryptLast(test._fld1, test._fld2);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(_dataTable.outArrayPtr);
        }
Exemple #6
0
        private Vector128 <byte> DecryptBlock(Vector128 <byte> data)
        {
            data = Sse2.Xor(data, _roundKeys.Last());
            for (int i = _roundKeys.Length - 2; i > 0; i--)
            {
                data = Aes.Decrypt(data, _roundKeys[i]);
            }

            return(Aes.DecryptLast(data, _roundKeys[0]));
        }
Exemple #7
0
        public void RunStructLclFldScenario()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunStructLclFldScenario));

            var test   = TestStruct.Create();
            var result = Aes.DecryptLast(test._fld1, test._fld2);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(_dataTable.outArrayPtr);
        }
Exemple #8
0
        public void RunLclVarScenario_LoadAligned()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunLclVarScenario_LoadAligned));

            var left   = Aes.LoadAlignedVector128((Byte *)(_dataTable.inArray1Ptr));
            var right  = Aes.LoadAlignedVector128((Byte *)(_dataTable.inArray2Ptr));
            var result = Aes.DecryptLast(left, right);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(_dataTable.outArrayPtr);
        }
Exemple #9
0
        public void RunLclVarScenario_UnsafeRead()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunLclVarScenario_UnsafeRead));

            var left   = Unsafe.Read <Vector128 <Byte> >(_dataTable.inArray1Ptr);
            var right  = Unsafe.Read <Vector128 <Byte> >(_dataTable.inArray2Ptr);
            var result = Aes.DecryptLast(left, right);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(_dataTable.outArrayPtr);
        }
Exemple #10
0
        public void RunBasicScenario_Load()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunBasicScenario_Load));

            var result = Aes.DecryptLast(
                Aes.LoadVector128((Byte *)(_dataTable.inArray1Ptr)),
                Aes.LoadVector128((Byte *)(_dataTable.inArray2Ptr))
                );

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(_dataTable.outArrayPtr);
        }
Exemple #11
0
        public void RunStructLclFldScenario_Load()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunStructLclFldScenario_Load));

            var test   = TestStruct.Create();
            var result = Aes.DecryptLast(
                Aes.LoadVector128((Byte *)(&test._fld1)),
                Aes.LoadVector128((Byte *)(&test._fld2))
                );

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(_dataTable.outArrayPtr);
        }
Exemple #12
0
            public void RunStructFldScenario_Load(AesBinaryOpTest__DecryptLastByte testClass)
            {
                fixed(Vector128 <Byte> *pFld1 = &_fld1)
                fixed(Vector128 <Byte> *pFld2 = &_fld2)
                {
                    var result = Aes.DecryptLast(
                        Aes.LoadVector128((Byte *)(pFld1)),
                        Aes.LoadVector128((Byte *)(pFld2))
                        );

                    Unsafe.Write(testClass._dataTable.outArrayPtr, result);
                    testClass.ValidateResult(testClass._dataTable.outArrayPtr);
                }
            }
Exemple #13
0
        public void RunClassFldScenario_Load()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunClassFldScenario_Load));

            fixed(Vector128 <Byte> *pFld1 = &_fld1)
            fixed(Vector128 <Byte> *pFld2 = &_fld2)
            {
                var result = Aes.DecryptLast(
                    Aes.LoadVector128((Byte *)(pFld1)),
                    Aes.LoadVector128((Byte *)(pFld2))
                    );

                Unsafe.Write(_dataTable.outArrayPtr, result);
                ValidateResult(_dataTable.outArrayPtr);
            }
        }
Exemple #14
0
        public static AesVector RawDecryptBlock(AesVector block, Span <AesVector> keySchedule)
        {
            block = Sse2.Xor(block, keySchedule[10 + 0]);
            block = Aes.Decrypt(block, keySchedule[10 + 1]);
            block = Aes.Decrypt(block, keySchedule[10 + 2]);
            block = Aes.Decrypt(block, keySchedule[10 + 3]);
            block = Aes.Decrypt(block, keySchedule[10 + 4]);
            block = Aes.Decrypt(block, keySchedule[10 + 5]);
            block = Aes.Decrypt(block, keySchedule[10 + 6]);
            block = Aes.Decrypt(block, keySchedule[10 + 7]);
            block = Aes.Decrypt(block, keySchedule[10 + 8]);
            block = Aes.Decrypt(block, keySchedule[10 + 9]);
            block = Aes.DecryptLast(block, keySchedule[0]);

            return(block);
        }
        public override void DecryptBlock(ref byte ciphertext, ref byte plaintext)
        {
            var block = Unsafe.ReadUnaligned <Vector128 <byte> >(ref ciphertext);

            block = Sse2.Xor(block, _keys.Key0);
            block = Aes.Decrypt(block, _keys.Key1);
            block = Aes.Decrypt(block, _keys.Key2);
            block = Aes.Decrypt(block, _keys.Key3);
            block = Aes.Decrypt(block, _keys.Key4);
            block = Aes.Decrypt(block, _keys.Key5);
            block = Aes.Decrypt(block, _keys.Key6);
            block = Aes.Decrypt(block, _keys.Key7);
            block = Aes.Decrypt(block, _keys.Key8);
            block = Aes.Decrypt(block, _keys.Key9);
            block = Aes.DecryptLast(block, _keys.Key10);
            Unsafe.WriteUnaligned(ref plaintext, block);
        }
        public override void DecryptBlock(ReadOnlySpan <byte> ciphertext, Span <byte> plaintext)
        {
            var block = Unsafe.ReadUnaligned <Vector128 <byte> >(ref MemoryMarshal.GetReference(ciphertext));

            block = Sse2.Xor(block, _keys.Key0);
            block = Aes.Decrypt(block, _keys.Key1);
            block = Aes.Decrypt(block, _keys.Key2);
            block = Aes.Decrypt(block, _keys.Key3);
            block = Aes.Decrypt(block, _keys.Key4);
            block = Aes.Decrypt(block, _keys.Key5);
            block = Aes.Decrypt(block, _keys.Key6);
            block = Aes.Decrypt(block, _keys.Key7);
            block = Aes.Decrypt(block, _keys.Key8);
            block = Aes.Decrypt(block, _keys.Key9);
            block = Aes.DecryptLast(block, _keys.Key10);
            Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(plaintext), block);
        }
Exemple #17
0
        public override ulong Run(CancellationToken cancellationToken)
        {
            if (!Aes.IsSupported)
            {
                return(0uL);
            }

            var iterations     = 0uL;
            var cipherTextSpan = new Span <int>(cipherText);
            var plainTextSpan  = new Span <int>(Enumerable.Repeat(0, plainText.Length).ToArray());

            unsafe
            {
                fixed(int *cipherTextPointer = cipherTextSpan)
                fixed(int *plainTextPointer = plainTextSpan)
                {
                    while (!cancellationToken.IsCancellationRequested)
                    {
                        for (var j = 0; j < LENGTH; j++)
                        {
                            var cipherTextVector = Sse2.LoadVector128(cipherTextPointer);
                            var workingCopy      = Sse2.Xor(cipherTextVector, loadedEncryptionKey[10]).As <int, byte>();
                            workingCopy = Aes.Decrypt(workingCopy, loadedEncryptionKey[11].As <int, byte>());
                            workingCopy = Aes.Decrypt(workingCopy, loadedEncryptionKey[12].As <int, byte>());
                            workingCopy = Aes.Decrypt(workingCopy, loadedEncryptionKey[13].As <int, byte>());
                            workingCopy = Aes.Decrypt(workingCopy, loadedEncryptionKey[14].As <int, byte>());
                            workingCopy = Aes.Decrypt(workingCopy, loadedEncryptionKey[15].As <int, byte>());
                            workingCopy = Aes.Decrypt(workingCopy, loadedEncryptionKey[16].As <int, byte>());
                            workingCopy = Aes.Decrypt(workingCopy, loadedEncryptionKey[17].As <int, byte>());
                            workingCopy = Aes.Decrypt(workingCopy, loadedEncryptionKey[18].As <int, byte>());
                            workingCopy = Aes.Decrypt(workingCopy, loadedEncryptionKey[19].As <int, byte>());
                            workingCopy = Aes.DecryptLast(workingCopy, loadedEncryptionKey[0].As <int, byte>());

                            Sse2.Store(plainTextPointer, workingCopy.As <byte, int>());
                        }

                        iterations++;
                    }
                }
            }

            return(iterations);
        }
Exemple #18
0
        private void Decrypt(Span <byte> data)
        {
            Vector128 <byte>[]       keys   = roundKeys;
            Span <Vector128 <byte> > blocks = MemoryMarshal.Cast <byte, Vector128 <byte> >(data);

            for (int i = 0; i < blocks.Length; i++)
            {
                Vector128 <byte> b = blocks[i];

                b = Sse2.Xor(b, keys[10]);
                b = Aes.Decrypt(b, keys[19]);
                b = Aes.Decrypt(b, keys[18]);
                b = Aes.Decrypt(b, keys[17]);
                b = Aes.Decrypt(b, keys[16]);
                b = Aes.Decrypt(b, keys[15]);
                b = Aes.Decrypt(b, keys[14]);
                b = Aes.Decrypt(b, keys[13]);
                b = Aes.Decrypt(b, keys[12]);
                b = Aes.Decrypt(b, keys[11]);
                b = Aes.DecryptLast(b, keys[0]);

                blocks[i] = b;
            }
        }