Home | History | Annotate | Line # | Download | only in riscv64
      1  1.1  christos #include <machine/asm.h>
      2  1.1  christos .text
      3  1.1  christos .p2align 3
      4  1.1  christos .globl rv64i_zvksed_sm4_set_encrypt_key
      5  1.1  christos .type rv64i_zvksed_sm4_set_encrypt_key,@function
      6  1.1  christos rv64i_zvksed_sm4_set_encrypt_key:
      7  1.1  christos     .word 0xc1027057
      8  1.1  christos 
      9  1.1  christos     # Load the user key
     10  1.1  christos     .word 33906823
     11  1.1  christos     .word 1242865879
     12  1.1  christos 
     13  1.1  christos     # Load the FK.
     14  1.1  christos     la t0, FK
     15  1.1  christos     .word 33743111
     16  1.1  christos 
     17  1.1  christos     # Generate round keys.
     18  1.1  christos     .word 772866263
     19  1.1  christos     .word 2249204215 # rk[0:3]
     20  1.1  christos     .word 2251334263 # rk[4:7]
     21  1.1  christos     .word 2252415735 # rk[8:11]
     22  1.1  christos     .word 2253497207 # rk[12:15]
     23  1.1  christos     .word 2254578679 # rk[16:19]
     24  1.1  christos     .word 2255660151 # rk[20:23]
     25  1.1  christos     .word 2256741623 # rk[24:27]
     26  1.1  christos     .word 2257823095 # rk[28:31]
     27  1.1  christos 
     28  1.1  christos     # Store round keys
     29  1.1  christos     .word 33939879 # rk[0:3]
     30  1.1  christos     addi a1, a1, 16
     31  1.1  christos     .word 33940007 # rk[4:7]
     32  1.1  christos     addi a1, a1, 16
     33  1.1  christos     .word 33940135 # rk[8:11]
     34  1.1  christos     addi a1, a1, 16
     35  1.1  christos     .word 33940263 # rk[12:15]
     36  1.1  christos     addi a1, a1, 16
     37  1.1  christos     .word 33940391 # rk[16:19]
     38  1.1  christos     addi a1, a1, 16
     39  1.1  christos     .word 33940519 # rk[20:23]
     40  1.1  christos     addi a1, a1, 16
     41  1.1  christos     .word 33940647 # rk[24:27]
     42  1.1  christos     addi a1, a1, 16
     43  1.1  christos     .word 33940775 # rk[28:31]
     44  1.1  christos 
     45  1.1  christos     li a0, 1
     46  1.1  christos     ret
     47  1.1  christos .size rv64i_zvksed_sm4_set_encrypt_key,.-rv64i_zvksed_sm4_set_encrypt_key
     48  1.1  christos .p2align 3
     49  1.1  christos .globl rv64i_zvksed_sm4_set_decrypt_key
     50  1.1  christos .type rv64i_zvksed_sm4_set_decrypt_key,@function
     51  1.1  christos rv64i_zvksed_sm4_set_decrypt_key:
     52  1.1  christos     .word 0xc1027057
     53  1.1  christos 
     54  1.1  christos     # Load the user key
     55  1.1  christos     .word 33906823
     56  1.1  christos     .word 1242865879
     57  1.1  christos 
     58  1.1  christos     # Load the FK.
     59  1.1  christos     la t0, FK
     60  1.1  christos     .word 33743111
     61  1.1  christos 
     62  1.1  christos     # Generate round keys.
     63  1.1  christos     .word 772866263
     64  1.1  christos     .word 2249204215 # rk[0:3]
     65  1.1  christos     .word 2251334263 # rk[4:7]
     66  1.1  christos     .word 2252415735 # rk[8:11]
     67  1.1  christos     .word 2253497207 # rk[12:15]
     68  1.1  christos     .word 2254578679 # rk[16:19]
     69  1.1  christos     .word 2255660151 # rk[20:23]
     70  1.1  christos     .word 2256741623 # rk[24:27]
     71  1.1  christos     .word 2257823095 # rk[28:31]
     72  1.1  christos 
     73  1.1  christos     # Store round keys in reverse order
     74  1.1  christos     addi a1, a1, 12
     75  1.1  christos     li t1, -4
     76  1.1  christos     .word 174449959 # rk[31:28]
     77  1.1  christos     addi a1, a1, 16
     78  1.1  christos     .word 174449831 # rk[27:24]
     79  1.1  christos     addi a1, a1, 16
     80  1.1  christos     .word 174449703 # rk[23:20]
     81  1.1  christos     addi a1, a1, 16
     82  1.1  christos     .word 174449575 # rk[19:16]
     83  1.1  christos     addi a1, a1, 16
     84  1.1  christos     .word 174449447 # rk[15:12]
     85  1.1  christos     addi a1, a1, 16
     86  1.1  christos     .word 174449319 # rk[11:8]
     87  1.1  christos     addi a1, a1, 16
     88  1.1  christos     .word 174449191 # rk[7:4]
     89  1.1  christos     addi a1, a1, 16
     90  1.1  christos     .word 174449063 # rk[3:0]
     91  1.1  christos 
     92  1.1  christos     li a0, 1
     93  1.1  christos     ret
     94  1.1  christos .size rv64i_zvksed_sm4_set_decrypt_key,.-rv64i_zvksed_sm4_set_decrypt_key
     95  1.1  christos .p2align 3
     96  1.1  christos .globl rv64i_zvksed_sm4_encrypt
     97  1.1  christos .type rv64i_zvksed_sm4_encrypt,@function
     98  1.1  christos rv64i_zvksed_sm4_encrypt:
     99  1.1  christos     .word 0xc1027057
    100  1.1  christos 
    101  1.1  christos     # Order of elements was adjusted in set_encrypt_key()
    102  1.1  christos     .word 33972487 # rk[0:3]
    103  1.1  christos     addi a2, a2, 16
    104  1.1  christos     .word 33972615 # rk[4:7]
    105  1.1  christos     addi a2, a2, 16
    106  1.1  christos     .word 33972743 # rk[8:11]
    107  1.1  christos     addi a2, a2, 16
    108  1.1  christos     .word 33972871 # rk[12:15]
    109  1.1  christos     addi a2, a2, 16
    110  1.1  christos     .word 33972999 # rk[16:19]
    111  1.1  christos     addi a2, a2, 16
    112  1.1  christos     .word 33973127 # rk[20:23]
    113  1.1  christos     addi a2, a2, 16
    114  1.1  christos     .word 33973255 # rk[24:27]
    115  1.1  christos     addi a2, a2, 16
    116  1.1  christos     .word 33973383 # rk[28:31]
    117  1.1  christos 
    118  1.1  christos     # Load input data
    119  1.1  christos     .word 33906823
    120  1.1  christos     .word 1242865879
    121  1.1  christos 
    122  1.1  christos     # Encrypt with all keys
    123  1.1  christos     .word 2787647735
    124  1.1  christos     .word 2788696311
    125  1.1  christos     .word 2789744887
    126  1.1  christos     .word 2790793463
    127  1.1  christos     .word 2791842039
    128  1.1  christos     .word 2792890615
    129  1.1  christos     .word 2793939191
    130  1.1  christos     .word 2794987767
    131  1.1  christos 
    132  1.1  christos     # Save the ciphertext (in reverse element order)
    133  1.1  christos     .word 1242865879
    134  1.1  christos     li t0, -4
    135  1.1  christos     addi a1, a1, 12
    136  1.1  christos     .word 173400231
    137  1.1  christos 
    138  1.1  christos     ret
    139  1.1  christos .size rv64i_zvksed_sm4_encrypt,.-rv64i_zvksed_sm4_encrypt
    140  1.1  christos .p2align 3
    141  1.1  christos .globl rv64i_zvksed_sm4_decrypt
    142  1.1  christos .type rv64i_zvksed_sm4_decrypt,@function
    143  1.1  christos rv64i_zvksed_sm4_decrypt:
    144  1.1  christos     .word 0xc1027057
    145  1.1  christos 
    146  1.1  christos     # Order of elements was adjusted in set_decrypt_key()
    147  1.1  christos     .word 33973383 # rk[31:28]
    148  1.1  christos     addi a2, a2, 16
    149  1.1  christos     .word 33973255 # rk[27:24]
    150  1.1  christos     addi a2, a2, 16
    151  1.1  christos     .word 33973127 # rk[23:20]
    152  1.1  christos     addi a2, a2, 16
    153  1.1  christos     .word 33972999 # rk[19:16]
    154  1.1  christos     addi a2, a2, 16
    155  1.1  christos     .word 33972871 # rk[15:11]
    156  1.1  christos     addi a2, a2, 16
    157  1.1  christos     .word 33972743 # rk[11:8]
    158  1.1  christos     addi a2, a2, 16
    159  1.1  christos     .word 33972615 # rk[7:4]
    160  1.1  christos     addi a2, a2, 16
    161  1.1  christos     .word 33972487 # rk[3:0]
    162  1.1  christos 
    163  1.1  christos     # Load input data
    164  1.1  christos     .word 33906823
    165  1.1  christos     .word 1242865879
    166  1.1  christos 
    167  1.1  christos     # Encrypt with all keys
    168  1.1  christos     .word 2794987767
    169  1.1  christos     .word 2793939191
    170  1.1  christos     .word 2792890615
    171  1.1  christos     .word 2791842039
    172  1.1  christos     .word 2790793463
    173  1.1  christos     .word 2789744887
    174  1.1  christos     .word 2788696311
    175  1.1  christos     .word 2787647735
    176  1.1  christos 
    177  1.1  christos     # Save the ciphertext (in reverse element order)
    178  1.1  christos     .word 1242865879
    179  1.1  christos     li t0, -4
    180  1.1  christos     addi a1, a1, 12
    181  1.1  christos     .word 173400231
    182  1.1  christos 
    183  1.1  christos     ret
    184  1.1  christos .size rv64i_zvksed_sm4_decrypt,.-rv64i_zvksed_sm4_decrypt
    185  1.1  christos # Family Key (little-endian 32-bit chunks)
    186  1.1  christos .p2align 3
    187  1.1  christos FK:
    188  1.1  christos     .word 0xA3B1BAC6, 0x56AA3350, 0x677D9197, 0xB27022DC
    189  1.1  christos .size FK,.-FK
    190