/ externals / biscuit / src / assembler_crypto.cpp
assembler_crypto.cpp
  1  #include <biscuit/assert.hpp>
  2  #include <biscuit/assembler.hpp>
  3  
  4  #include "assembler_util.hpp"
  5  
  6  namespace biscuit {
  7  namespace {
  8  void EmitAES32Instruction(CodeBuffer& buffer, uint32_t op, GPR rd, GPR rs1, GPR rs2, uint32_t bs) noexcept {
  9      BISCUIT_ASSERT(bs <= 0b11);
 10      buffer.Emit32(op | (bs << 30) | (rs2.Index() << 20) |
 11                    (rs1.Index() << 15) | (rd.Index() << 7));
 12  }
 13  
 14  void EmitSM4Instruction(CodeBuffer& buffer, uint32_t op, GPR rd, GPR rs1, GPR rs2, uint32_t bs) noexcept {
 15      // Same behavior, function exists for a better contextual name.
 16      EmitAES32Instruction(buffer, op, rd, rs1, rs2, bs);
 17  }
 18  
 19  void EmitAES64Instruction(CodeBuffer& buffer, uint32_t op, GPR rd, GPR rs1, GPR rs2) noexcept {
 20      buffer.Emit32(op | (rs2.Index() << 20) | (rs1.Index() << 15) | (rd.Index() << 7));
 21  }
 22  
 23  void EmitSHAInstruction(CodeBuffer& buffer, uint32_t op, GPR rd, GPR rs1, GPR rs2) noexcept {
 24      // Same behavior, function exists for a better contextual name.
 25      EmitAES64Instruction(buffer, op, rd, rs1, rs2);
 26  }
 27  
 28  void EmitSM3Instruction(CodeBuffer& buffer, uint32_t op, GPR rd, GPR rs) noexcept {
 29      // Same behavior, function exists for a better contextual name.
 30      EmitAES64Instruction(buffer, op, rd, rs, x0);
 31  }
 32  } // Anonymous namespace
 33  
 34  void Assembler::AES32DSI(GPR rd, GPR rs1, GPR rs2, uint32_t bs) noexcept {
 35      BISCUIT_ASSERT(IsRV32(m_features));
 36      EmitAES32Instruction(m_buffer, 0x2A000033, rd, rs1, rs2, bs);
 37  }
 38  
 39  void Assembler::AES32DSMI(GPR rd, GPR rs1, GPR rs2, uint32_t bs) noexcept {
 40      BISCUIT_ASSERT(IsRV32(m_features));
 41      EmitAES32Instruction(m_buffer, 0x2E000033, rd, rs1, rs2, bs);
 42  }
 43  
 44  void Assembler::AES32ESI(GPR rd, GPR rs1, GPR rs2, uint32_t bs) noexcept {
 45      BISCUIT_ASSERT(IsRV32(m_features));
 46      EmitAES32Instruction(m_buffer, 0x22000033, rd, rs1, rs2, bs);
 47  }
 48  
 49  void Assembler::AES32ESMI(GPR rd, GPR rs1, GPR rs2, uint32_t bs) noexcept {
 50      BISCUIT_ASSERT(IsRV32(m_features));
 51      EmitAES32Instruction(m_buffer, 0x26000033, rd, rs1, rs2, bs);
 52  }
 53  
 54  void Assembler::AES64DS(GPR rd, GPR rs1, GPR rs2) noexcept {
 55      BISCUIT_ASSERT(IsRV64(m_features));
 56      EmitAES64Instruction(m_buffer, 0x3A000033, rd, rs1, rs2);
 57  }
 58  
 59  void Assembler::AES64DSM(GPR rd, GPR rs1, GPR rs2) noexcept {
 60      BISCUIT_ASSERT(IsRV64(m_features));
 61      EmitAES64Instruction(m_buffer, 0x3E000033, rd, rs1, rs2);
 62  }
 63  
 64  void Assembler::AES64ES(GPR rd, GPR rs1, GPR rs2) noexcept {
 65      BISCUIT_ASSERT(IsRV64(m_features));
 66      EmitAES64Instruction(m_buffer, 0x32000033, rd, rs1, rs2);
 67  }
 68  
 69  void Assembler::AES64ESM(GPR rd, GPR rs1, GPR rs2) noexcept {
 70      BISCUIT_ASSERT(IsRV64(m_features));
 71      EmitAES64Instruction(m_buffer, 0x36000033, rd, rs1, rs2);
 72  }
 73  
 74  void Assembler::AES64IM(GPR rd, GPR rs) noexcept {
 75      BISCUIT_ASSERT(IsRV64(m_features));
 76      EmitAES64Instruction(m_buffer, 0x30001013, rd, rs, x0);
 77  }
 78  
 79  void Assembler::AES64KS1I(GPR rd, GPR rs, uint32_t rnum) noexcept {
 80      // RVK spec states that rnums 0xB to 0xF are reserved.
 81      BISCUIT_ASSERT(IsRV64(m_features));
 82      BISCUIT_ASSERT(rnum <= 0xA);
 83      EmitAES64Instruction(m_buffer, 0x31001013, rd, rs, GPR{rnum});
 84  }
 85  
 86  void Assembler::AES64KS2(GPR rd, GPR rs1, GPR rs2) noexcept {
 87      BISCUIT_ASSERT(IsRV64(m_features));
 88      EmitAES64Instruction(m_buffer, 0x7E000033, rd, rs1, rs2);
 89  }
 90  
 91  void Assembler::SHA256SIG0(GPR rd, GPR rs) noexcept {
 92      EmitSHAInstruction(m_buffer, 0x10201013, rd, rs, x0);
 93  }
 94  
 95  void Assembler::SHA256SIG1(GPR rd, GPR rs) noexcept {
 96      EmitSHAInstruction(m_buffer, 0x10301013, rd, rs, x0);
 97  }
 98  
 99  void Assembler::SHA256SUM0(GPR rd, GPR rs) noexcept {
100      EmitSHAInstruction(m_buffer, 0x10001013, rd, rs, x0);
101  }
102  
103  void Assembler::SHA256SUM1(GPR rd, GPR rs) noexcept {
104      EmitSHAInstruction(m_buffer, 0x10101013, rd, rs, x0);
105  }
106  
107  void Assembler::SHA512SIG0(GPR rd, GPR rs) noexcept {
108      BISCUIT_ASSERT(IsRV64(m_features));
109      EmitSHAInstruction(m_buffer, 0x10601013, rd, rs, x0);
110  }
111  
112  void Assembler::SHA512SIG0H(GPR rd, GPR rs1, GPR rs2) noexcept {
113      BISCUIT_ASSERT(IsRV32(m_features));
114      EmitSHAInstruction(m_buffer, 0x5C000033, rd, rs1, rs2);
115  }
116  
117  void Assembler::SHA512SIG0L(GPR rd, GPR rs1, GPR rs2) noexcept {
118      BISCUIT_ASSERT(IsRV32(m_features));
119      EmitSHAInstruction(m_buffer, 0x54000033, rd, rs1, rs2);
120  }
121  
122  void Assembler::SHA512SIG1(GPR rd, GPR rs) noexcept {
123      BISCUIT_ASSERT(IsRV64(m_features));
124      EmitSHAInstruction(m_buffer, 0x10701013, rd, rs, x0);
125  }
126  
127  void Assembler::SHA512SIG1H(GPR rd, GPR rs1, GPR rs2) noexcept {
128      BISCUIT_ASSERT(IsRV32(m_features));
129      EmitSHAInstruction(m_buffer, 0x5E000033, rd, rs1, rs2);
130  }
131  
132  void Assembler::SHA512SIG1L(GPR rd, GPR rs1, GPR rs2) noexcept {
133      BISCUIT_ASSERT(IsRV32(m_features));
134      EmitSHAInstruction(m_buffer, 0x56000033, rd, rs1, rs2);
135  }
136  
137  void Assembler::SHA512SUM0(GPR rd, GPR rs) noexcept {
138      BISCUIT_ASSERT(IsRV64(m_features));
139      EmitSHAInstruction(m_buffer, 0x10401013, rd, rs, x0);
140  }
141  
142  void Assembler::SHA512SUM0R(GPR rd, GPR rs1, GPR rs2) noexcept {
143      BISCUIT_ASSERT(IsRV32(m_features));
144      EmitSHAInstruction(m_buffer, 0x50000033, rd, rs1, rs2);
145  }
146  
147  void Assembler::SHA512SUM1(GPR rd, GPR rs) noexcept {
148      BISCUIT_ASSERT(IsRV64(m_features));
149      EmitSHAInstruction(m_buffer, 0x10501013, rd, rs, x0);
150  }
151  
152  void Assembler::SHA512SUM1R(GPR rd, GPR rs1, GPR rs2) noexcept {
153      BISCUIT_ASSERT(IsRV32(m_features));
154      EmitSHAInstruction(m_buffer, 0x52000033, rd, rs1, rs2);
155  }
156  
157  void Assembler::SM3P0(GPR rd, GPR rs) noexcept {
158      EmitSM3Instruction(m_buffer, 0x10801013, rd, rs);
159  }
160  
161  void Assembler::SM3P1(GPR rd, GPR rs) noexcept {
162      EmitSM3Instruction(m_buffer, 0x10901013, rd, rs);
163  }
164  
165  void Assembler::SM4ED(GPR rd, GPR rs1, GPR rs2, uint32_t bs) noexcept {
166      EmitSM4Instruction(m_buffer, 0x30000033, rd, rs1, rs2, bs);
167  }
168  
169  void Assembler::SM4KS(GPR rd, GPR rs1, GPR rs2, uint32_t bs) noexcept {
170      EmitSM4Instruction(m_buffer, 0x34000033, rd, rs1, rs2, bs);
171  }
172  } // namespace biscuit