aboutsummaryrefslogtreecommitdiffhomepage
path: root/externals/biscuit/src/assembler_crypto.cpp
blob: 8d83bc41ec8e2ca45d447de87fe248a4c08f3db7 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <biscuit/assert.hpp>
#include <biscuit/assembler.hpp>

#include "assembler_util.hpp"

namespace biscuit {
namespace {
void EmitAES32Instruction(CodeBuffer& buffer, uint32_t op, GPR rd, GPR rs1, GPR rs2, uint32_t bs) noexcept {
    BISCUIT_ASSERT(bs <= 0b11);
    buffer.Emit32(op | (bs << 30) | (rs2.Index() << 20) |
                  (rs1.Index() << 15) | (rd.Index() << 7));
}

void EmitSM4Instruction(CodeBuffer& buffer, uint32_t op, GPR rd, GPR rs1, GPR rs2, uint32_t bs) noexcept {
    // Same behavior, function exists for a better contextual name.
    EmitAES32Instruction(buffer, op, rd, rs1, rs2, bs);
}

void EmitAES64Instruction(CodeBuffer& buffer, uint32_t op, GPR rd, GPR rs1, GPR rs2) noexcept {
    buffer.Emit32(op | (rs2.Index() << 20) | (rs1.Index() << 15) | (rd.Index() << 7));
}

void EmitSHAInstruction(CodeBuffer& buffer, uint32_t op, GPR rd, GPR rs1, GPR rs2) noexcept {
    // Same behavior, function exists for a better contextual name.
    EmitAES64Instruction(buffer, op, rd, rs1, rs2);
}

void EmitSM3Instruction(CodeBuffer& buffer, uint32_t op, GPR rd, GPR rs) noexcept {
    // Same behavior, function exists for a better contextual name.
    EmitAES64Instruction(buffer, op, rd, rs, x0);
}
} // Anonymous namespace

void Assembler::AES32DSI(GPR rd, GPR rs1, GPR rs2, uint32_t bs) noexcept {
    BISCUIT_ASSERT(IsRV32(m_features));
    EmitAES32Instruction(m_buffer, 0x2A000033, rd, rs1, rs2, bs);
}

void Assembler::AES32DSMI(GPR rd, GPR rs1, GPR rs2, uint32_t bs) noexcept {
    BISCUIT_ASSERT(IsRV32(m_features));
    EmitAES32Instruction(m_buffer, 0x2E000033, rd, rs1, rs2, bs);
}

void Assembler::AES32ESI(GPR rd, GPR rs1, GPR rs2, uint32_t bs) noexcept {
    BISCUIT_ASSERT(IsRV32(m_features));
    EmitAES32Instruction(m_buffer, 0x22000033, rd, rs1, rs2, bs);
}

void Assembler::AES32ESMI(GPR rd, GPR rs1, GPR rs2, uint32_t bs) noexcept {
    BISCUIT_ASSERT(IsRV32(m_features));
    EmitAES32Instruction(m_buffer, 0x26000033, rd, rs1, rs2, bs);
}

void Assembler::AES64DS(GPR rd, GPR rs1, GPR rs2) noexcept {
    BISCUIT_ASSERT(IsRV64(m_features));
    EmitAES64Instruction(m_buffer, 0x3A000033, rd, rs1, rs2);
}

void Assembler::AES64DSM(GPR rd, GPR rs1, GPR rs2) noexcept {
    BISCUIT_ASSERT(IsRV64(m_features));
    EmitAES64Instruction(m_buffer, 0x3E000033, rd, rs1, rs2);
}

void Assembler::AES64ES(GPR rd, GPR rs1, GPR rs2) noexcept {
    BISCUIT_ASSERT(IsRV64(m_features));
    EmitAES64Instruction(m_buffer, 0x32000033, rd, rs1, rs2);
}

void Assembler::AES64ESM(GPR rd, GPR rs1, GPR rs2) noexcept {
    BISCUIT_ASSERT(IsRV64(m_features));
    EmitAES64Instruction(m_buffer, 0x36000033, rd, rs1, rs2);
}

void Assembler::AES64IM(GPR rd, GPR rs) noexcept {
    BISCUIT_ASSERT(IsRV64(m_features));
    EmitAES64Instruction(m_buffer, 0x30001013, rd, rs, x0);
}

void Assembler::AES64KS1I(GPR rd, GPR rs, uint32_t rnum) noexcept {
    // RVK spec states that rnums 0xB to 0xF are reserved.
    BISCUIT_ASSERT(IsRV64(m_features));
    BISCUIT_ASSERT(rnum <= 0xA);
    EmitAES64Instruction(m_buffer, 0x31001013, rd, rs, GPR{rnum});
}

void Assembler::AES64KS2(GPR rd, GPR rs1, GPR rs2) noexcept {
    BISCUIT_ASSERT(IsRV64(m_features));
    EmitAES64Instruction(m_buffer, 0x7E000033, rd, rs1, rs2);
}

void Assembler::SHA256SIG0(GPR rd, GPR rs) noexcept {
    EmitSHAInstruction(m_buffer, 0x10201013, rd, rs, x0);
}

void Assembler::SHA256SIG1(GPR rd, GPR rs) noexcept {
    EmitSHAInstruction(m_buffer, 0x10301013, rd, rs, x0);
}

void Assembler::SHA256SUM0(GPR rd, GPR rs) noexcept {
    EmitSHAInstruction(m_buffer, 0x10001013, rd, rs, x0);
}

void Assembler::SHA256SUM1(GPR rd, GPR rs) noexcept {
    EmitSHAInstruction(m_buffer, 0x10101013, rd, rs, x0);
}

void Assembler::SHA512SIG0(GPR rd, GPR rs) noexcept {
    BISCUIT_ASSERT(IsRV64(m_features));
    EmitSHAInstruction(m_buffer, 0x10601013, rd, rs, x0);
}

void Assembler::SHA512SIG0H(GPR rd, GPR rs1, GPR rs2) noexcept {
    BISCUIT_ASSERT(IsRV32(m_features));
    EmitSHAInstruction(m_buffer, 0x5C000033, rd, rs1, rs2);
}

void Assembler::SHA512SIG0L(GPR rd, GPR rs1, GPR rs2) noexcept {
    BISCUIT_ASSERT(IsRV32(m_features));
    EmitSHAInstruction(m_buffer, 0x54000033, rd, rs1, rs2);
}

void Assembler::SHA512SIG1(GPR rd, GPR rs) noexcept {
    BISCUIT_ASSERT(IsRV64(m_features));
    EmitSHAInstruction(m_buffer, 0x10701013, rd, rs, x0);
}

void Assembler::SHA512SIG1H(GPR rd, GPR rs1, GPR rs2) noexcept {
    BISCUIT_ASSERT(IsRV32(m_features));
    EmitSHAInstruction(m_buffer, 0x5E000033, rd, rs1, rs2);
}

void Assembler::SHA512SIG1L(GPR rd, GPR rs1, GPR rs2) noexcept {
    BISCUIT_ASSERT(IsRV32(m_features));
    EmitSHAInstruction(m_buffer, 0x56000033, rd, rs1, rs2);
}

void Assembler::SHA512SUM0(GPR rd, GPR rs) noexcept {
    BISCUIT_ASSERT(IsRV64(m_features));
    EmitSHAInstruction(m_buffer, 0x10401013, rd, rs, x0);
}

void Assembler::SHA512SUM0R(GPR rd, GPR rs1, GPR rs2) noexcept {
    BISCUIT_ASSERT(IsRV32(m_features));
    EmitSHAInstruction(m_buffer, 0x50000033, rd, rs1, rs2);
}

void Assembler::SHA512SUM1(GPR rd, GPR rs) noexcept {
    BISCUIT_ASSERT(IsRV64(m_features));
    EmitSHAInstruction(m_buffer, 0x10501013, rd, rs, x0);
}

void Assembler::SHA512SUM1R(GPR rd, GPR rs1, GPR rs2) noexcept {
    BISCUIT_ASSERT(IsRV32(m_features));
    EmitSHAInstruction(m_buffer, 0x52000033, rd, rs1, rs2);
}

void Assembler::SM3P0(GPR rd, GPR rs) noexcept {
    EmitSM3Instruction(m_buffer, 0x10801013, rd, rs);
}

void Assembler::SM3P1(GPR rd, GPR rs) noexcept {
    EmitSM3Instruction(m_buffer, 0x10901013, rd, rs);
}

void Assembler::SM4ED(GPR rd, GPR rs1, GPR rs2, uint32_t bs) noexcept {
    EmitSM4Instruction(m_buffer, 0x30000033, rd, rs1, rs2, bs);
}

void Assembler::SM4KS(GPR rd, GPR rs1, GPR rs2, uint32_t bs) noexcept {
    EmitSM4Instruction(m_buffer, 0x34000033, rd, rs1, rs2, bs);
}
} // namespace biscuit