github.com/emmansun/gmsm@v0.29.1/sm4/gen_arm64_ni.go (about)

     1  // Not used yet!!!
     2  // go run gen_arm64_ni.go
     3  
     4  //go:build ignore
     5  
     6  package main
     7  
     8  import (
     9  	"bytes"
    10  	"fmt"
    11  	"log"
    12  	"os"
    13  )
    14  
    15  //SM4E <Vd>.4S, <Vn>.4S
    16  func sm4e(Vd, Vn byte) uint32 {
    17  	inst := uint32(0xcec08400) | uint32(Vd&0x1f) | uint32(Vn&0x1f)<<5
    18  	// return bits.ReverseBytes32(inst)
    19  	return inst
    20  }
    21  
    22  //SM4EKEY <Vd>.4S, <Vn>.4S, <Vm>.4S
    23  func sm4ekey(Vd, Vn, Vm byte) uint32 {
    24  	inst := uint32(0xce60c800) | uint32(Vd&0x1f) | uint32(Vn&0x1f)<<5 | (uint32(Vm&0x1f) << 16)
    25  	// return bits.ReverseBytes32(inst)
    26  	return inst
    27  }
    28  
    29  func sm4ekeyRound(buf *bytes.Buffer, d, n, m byte) {
    30  	fmt.Fprintf(buf, "\tWORD $0x%08x          //SM4EKEY V%d.4S, V%d.4S, V%d.4S\n", sm4ekey(d, n, m), d, n, m)
    31  }
    32  
    33  func sm4eRound(buf *bytes.Buffer, d, n byte) {
    34  	fmt.Fprintf(buf, "\tWORD $0x%08x          //SM4E V%d.4S, V%d.4S\n", sm4e(d, n), d, n)
    35  }
    36  
    37  func main() {
    38  	buf := new(bytes.Buffer)
    39  	fmt.Fprint(buf, `
    40  // Generated by gen_arm64_ni.go. DO NOT EDIT.
    41  
    42  #include "textflag.h"
    43  
    44  // func expandKeySM4E(key *byte, fk, ck, enc *uint32)
    45  TEXT ·expandKeySM4E(SB),NOSPLIT,$0
    46  	MOVD key+0(FP), R8
    47  	MOVD fk+8(FP), R9
    48  	MOVD ck+16(FP), R10
    49  	MOVD enc+24(FP), R11
    50  
    51  	VLD1 (R8), [V9.B16]
    52  	VREV32 V9.B16, V9.B16
    53  	VLD1 (R9), [V8.S4]
    54  	VEOR V9, V8, V9
    55  	VLD1.P	64(R10), [V0.S4, V1.S4, V2.S4, V3.S4]
    56  `[1:])
    57  
    58  	sm4ekeyRound(buf, 8, 9, 0)
    59  	sm4ekeyRound(buf, 9, 8, 1)
    60  	fmt.Fprintf(buf, "\tVST1.P	[V8.S4, V9.S4], 32(R11)\n")
    61  	sm4ekeyRound(buf, 8, 9, 2)
    62  	sm4ekeyRound(buf, 9, 8, 3)
    63  	fmt.Fprintf(buf, "\tVST1.P	[V8.S4, V9.S4], 32(R11)\n")
    64  	fmt.Fprintf(buf, "\tVLD1.P	64(R10), [V0.S4, V1.S4, V2.S4, V3.S4]\n")
    65  	sm4ekeyRound(buf, 8, 9, 0)
    66  	sm4ekeyRound(buf, 9, 8, 1)
    67  	fmt.Fprintf(buf, "\tVST1.P	[V8.S4, V9.S4], 32(R11)\n")
    68  	sm4ekeyRound(buf, 8, 9, 2)
    69  	sm4ekeyRound(buf, 9, 8, 3)
    70  	fmt.Fprintf(buf, `
    71  	VST1.P	[V8.S4, V9.S4], 32(R11)
    72  	RET
    73  `[1:])
    74  	fmt.Fprint(buf, `
    75  
    76  // func encryptBlockSM4E(xk *uint32, dst, src *byte)
    77  TEXT ·encryptBlockSM4E(SB),NOSPLIT,$0
    78  	MOVD xk+0(FP), R8
    79  	MOVD dst+8(FP), R9
    80  	MOVD src+16(FP), R10
    81  
    82  	VLD1 (R10), [V8.B16]
    83  	VREV32 V8.B16, V8.B16
    84  	VLD1.P	64(R8), [V0.S4, V1.S4, V2.S4, V3.S4]
    85  `[1:])
    86  	sm4eRound(buf, 8, 0)
    87  	sm4eRound(buf, 8, 1)
    88  	sm4eRound(buf, 8, 2)
    89  	sm4eRound(buf, 8, 3)
    90  	fmt.Fprintf(buf, "\tVLD1.P	64(R8), [V0.S4, V1.S4, V2.S4, V3.S4]\n")
    91  	sm4eRound(buf, 8, 0)
    92  	sm4eRound(buf, 8, 1)
    93  	sm4eRound(buf, 8, 2)
    94  	sm4eRound(buf, 8, 3)
    95  	fmt.Fprintf(buf, `
    96  	VREV64	V8.B16, V8.B16
    97  	VEXT $8, V8.B16, V8.B16, V8.B16
    98  	VST1	[V8.B16], (R9)
    99  	RET
   100  `[1:])
   101  
   102  	fmt.Fprint(buf, `
   103  
   104  // func encryptBlocksSM4E(xk *uint32, dst, src *byte)
   105  TEXT ·encryptBlocksSM4E(SB),NOSPLIT,$0
   106  	MOVD xk+0(FP), R8
   107  	MOVD dst+8(FP), R9
   108  	MOVD src+16(FP), R10
   109  
   110  	VLD1.P	64(R8), [V0.S4, V1.S4, V2.S4, V3.S4]
   111  	VLD1.P	64(R8), [V4.S4, V5.S4, V6.S4, V7.S4]
   112  
   113  `[1:])
   114  	for i := 0; i < 4; i++ {
   115  		fmt.Fprintf(buf, "\tVLD1.P 16(R10), [V8.B16]\n")
   116  		fmt.Fprintf(buf, "\tVREV32 V8.B16, V8.B16\n")
   117  		sm4eRound(buf, 8, 0)
   118  		sm4eRound(buf, 8, 1)
   119  		sm4eRound(buf, 8, 2)
   120  		sm4eRound(buf, 8, 3)
   121  		sm4eRound(buf, 8, 4)
   122  		sm4eRound(buf, 8, 5)
   123  		sm4eRound(buf, 8, 6)
   124  		sm4eRound(buf, 8, 7)
   125  		fmt.Fprintf(buf, "\tVREV64	V8.B16, V8.B16\n")
   126  		fmt.Fprintf(buf, "\tVEXT $8, V8.B16, V8.B16, V8.B16\n")
   127  		fmt.Fprintf(buf, "\tVST1.P	[V8.B16], 16(R9)\n\n")
   128  	}
   129  	fmt.Fprintf(buf, `
   130  	RET
   131  `[1:])
   132  
   133  	src := buf.Bytes()
   134  	// fmt.Println(string(src))
   135  	err := os.WriteFile("sm4e_arm64.s", src, 0644)
   136  	if err != nil {
   137  		log.Fatal(err)
   138  	}
   139  }