aboutsummaryrefslogtreecommitdiff
path: root/riscv/insns/sm4ed.h
blob: a78c1a8767ea332144a7d85f26a323f73f0e544e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

require_extension(EXT_ZKSED);

#include "sm4_common.h"

uint8_t  bs     = insn.bs();

uint32_t sb_in  = (RS2 >> (8*bs)) & 0xFF;
uint32_t sb_out = (uint32_t)sm4_sbox[sb_in];

uint32_t linear = sb_out ^  (sb_out         <<  8) ^ 
                            (sb_out         <<  2) ^
                            (sb_out         << 18) ^
                           ((sb_out & 0x3f) << 26) ^
                           ((sb_out & 0xC0) << 10) ;

uint32_t rotl   = (linear << (8*bs)) | (linear >> (32-8*bs));

uint32_t result = rotl ^ RS1;

WRITE_RD(sext32(result));