aboutsummaryrefslogtreecommitdiff
path: root/src/ARMeilleure/Instructions/InstEmitSimdCrypto.cs
blob: 6226e35ae5505428b4215097b9e66cc3b4d841eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using ARMeilleure.Decoders;
using ARMeilleure.IntermediateRepresentation;
using ARMeilleure.Translation;

using static ARMeilleure.Instructions.InstEmitHelper;

namespace ARMeilleure.Instructions
{
    static partial class InstEmit
    {
        public static void Aesd_V(ArmEmitterContext context)
        {
            OpCodeSimd op = (OpCodeSimd)context.CurrOp;

            Operand d = GetVec(op.Rd);
            Operand n = GetVec(op.Rn);

            Operand res;

            if (Optimizations.UseArm64Aes)
            {
                res = context.AddIntrinsic(Intrinsic.Arm64AesdV, d, n);
            }
            else if (Optimizations.UseAesni)
            {
                res = context.AddIntrinsic(Intrinsic.X86Aesdeclast, context.AddIntrinsic(Intrinsic.X86Xorpd, d, n), context.VectorZero());
            }
            else
            {
                res = context.Call(typeof(SoftFallback).GetMethod(nameof(SoftFallback.Decrypt)), d, n);
            }

            context.Copy(d, res);
        }

        public static void Aese_V(ArmEmitterContext context)
        {
            OpCodeSimd op = (OpCodeSimd)context.CurrOp;

            Operand d = GetVec(op.Rd);
            Operand n = GetVec(op.Rn);

            Operand res;

            if (Optimizations.UseArm64Aes)
            {
                res = context.AddIntrinsic(Intrinsic.Arm64AeseV, d, n);
            }
            else if (Optimizations.UseAesni)
            {
                res = context.AddIntrinsic(Intrinsic.X86Aesenclast, context.AddIntrinsic(Intrinsic.X86Xorpd, d, n), context.VectorZero());
            }
            else
            {
                res = context.Call(typeof(SoftFallback).GetMethod(nameof(SoftFallback.Encrypt)), d, n);
            }

            context.Copy(d, res);
        }

        public static void Aesimc_V(ArmEmitterContext context)
        {
            OpCodeSimd op = (OpCodeSimd)context.CurrOp;

            Operand n = GetVec(op.Rn);

            Operand res;

            if (Optimizations.UseArm64Aes)
            {
                res = context.AddIntrinsic(Intrinsic.Arm64AesimcV, n);
            }
            else if (Optimizations.UseAesni)
            {
                res = context.AddIntrinsic(Intrinsic.X86Aesimc, n);
            }
            else
            {
                res = context.Call(typeof(SoftFallback).GetMethod(nameof(SoftFallback.InverseMixColumns)), n);
            }

            context.Copy(GetVec(op.Rd), res);
        }

        public static void Aesmc_V(ArmEmitterContext context)
        {
            OpCodeSimd op = (OpCodeSimd)context.CurrOp;

            Operand n = GetVec(op.Rn);

            Operand res;

            if (Optimizations.UseArm64Aes)
            {
                res = context.AddIntrinsic(Intrinsic.Arm64AesmcV, n);
            }
            else if (Optimizations.UseAesni)
            {
                Operand roundKey = context.VectorZero();

                // Inverse Shift Rows, Inverse Sub Bytes, xor 0 so nothing happens
                res = context.AddIntrinsic(Intrinsic.X86Aesdeclast, n, roundKey);

                // Shift Rows, Sub Bytes, Mix Columns (!), xor 0 so nothing happens
                res = context.AddIntrinsic(Intrinsic.X86Aesenc, res, roundKey);
            }
            else
            {
                res = context.Call(typeof(SoftFallback).GetMethod(nameof(SoftFallback.MixColumns)), n);
            }

            context.Copy(GetVec(op.Rd), res);
        }
    }
}