github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/sm4/gen_arm64_ni.go (about)

     1  // Not used yet!!!
     2  // go run gen_arm64_ni.go
     3  
     4  //go:build ignore
     5  // +build ignore
     6  
     7  package main
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"log"
    13  	"math/bits"
    14  	"os"
    15  )
    16  
    17  //SM4E <Vd>.4S, <Vn>.4S
    18  func sm4e(Vd, Vn byte) uint32 {
    19  	inst := uint32(0xcec08400) | uint32(Vd&0x1f) | uint32(Vn&0x1f)<<5
    20  	return bits.ReverseBytes32(inst)
    21  }
    22  
    23  //SM4EKEY <Vd>.4S, <Vn>.4S, <Vm>.4S
    24  func sm4ekey(Vd, Vn, Vm byte) uint32 {
    25  	inst := uint32(0xce60c800) | uint32(Vd&0x1f) | uint32(Vn&0x1f)<<5 | (uint32(Vm&0x1f) << 16)
    26  	return bits.ReverseBytes32(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  	VREV32 V8.B16, V8.B16
    97  	VST1	[V8.B16], (R9)
    98  	RET
    99  `[1:])
   100  
   101  	fmt.Fprint(buf, `
   102  
   103  // func encryptBlocksSM4E(xk *uint32, dst, src *byte)
   104  TEXT ·encryptBlocksSM4E(SB),NOSPLIT,$0
   105  	MOVD xk+0(FP), R8
   106  	MOVD dst+8(FP), R9
   107  	MOVD src+16(FP), R10
   108  
   109  	VLD1.P	64(R8), [V0.S4, V1.S4, V2.S4, V3.S4]
   110  	VLD1.P	64(R8), [V4.S4, V5.S4, V6.S4, V7.S4]
   111  
   112  `[1:])
   113  	for i := 0; i < 4; i++ {
   114  		fmt.Fprintf(buf, "\tVLD1.P 16(R10), [V8.B16]\n")
   115  		fmt.Fprintf(buf, "\tVREV32 V8.B16, V8.B16\n")
   116  		sm4eRound(buf, 8, 0)
   117  		sm4eRound(buf, 8, 1)
   118  		sm4eRound(buf, 8, 2)
   119  		sm4eRound(buf, 8, 3)
   120  		sm4eRound(buf, 8, 4)
   121  		sm4eRound(buf, 8, 5)
   122  		sm4eRound(buf, 8, 6)
   123  		sm4eRound(buf, 8, 7)
   124  		fmt.Fprintf(buf, "\tVREV32 V8.B16, V8.B16\n")
   125  		fmt.Fprintf(buf, "\tVST1.P	[V8.B16], 16(R9)\n\n")
   126  	}
   127  	fmt.Fprintf(buf, `
   128  	RET
   129  `[1:])
   130  
   131  	src := buf.Bytes()
   132  	// fmt.Println(string(src))
   133  	err := os.WriteFile("sm4e_arm64.s", src, 0644)
   134  	if err != nil {
   135  		log.Fatal(err)
   136  	}
   137  }