[SM4] fallback arm64 first

This commit is contained in:
Emman 2022-01-12 17:14:37 +08:00
parent ac50f11ef8
commit 46fb08c038
3 changed files with 13 additions and 29 deletions

View File

@ -179,14 +179,14 @@ TEXT ·gcmSm4Finish(SB),NOSPLIT,$0
SM4_TAO_L1(x, y, z, z1, z2); \ SM4_TAO_L1(x, y, z, z1, z2); \
VEOR x.B16, t0.B16, t0.B16 VEOR x.B16, t0.B16, t0.B16
// func gcmSm4Init(productTable *[256]byte, rk []uint32) // func precomputeTableAsm(productTable *[256]byte, src *[16]byte)
TEXT ·gcmSm4Init(SB),NOSPLIT,$0 TEXT ·precomputeTableAsm(SB),NOSPLIT,$0
#define pTbl R0 #define pTbl R0
#define RK R1 #define SRC R1
#define I R2 #define I R2
MOVD productTable+0(FP), pTbl MOVD productTable+0(FP), pTbl
MOVD rk+8(FP), RK MOVD src+8(FP), SRC
MOVD $0xC2, I MOVD $0xC2, I
LSL $56, I LSL $56, I
@ -195,26 +195,8 @@ TEXT ·gcmSm4Init(SB),NOSPLIT,$0
VMOV I, POLY.D[1] VMOV I, POLY.D[1]
VEOR ZERO.B16, ZERO.B16, ZERO.B16 VEOR ZERO.B16, ZERO.B16, ZERO.B16
// Encrypt block 0 with the SM4 keys to generate the hash key H VLD1 (SRC), [B0.B16]
VEOR B0.B16, B0.B16, B0.B16 VREV64 B0.B16, B0.B16
VEOR B1.B16, B1.B16, B1.B16
VEOR B2.B16, B2.B16, B2.B16
VEOR B3.B16, B3.B16, B3.B16
EOR R3, R3
sm4InitEncLoop:
SM4_ROUND(RK, K0, K1, K2, K3, K4, B0, B1, B2, B3)
SM4_ROUND(RK, K0, K1, K2, K3, K4, B1, B2, B3, B0)
SM4_ROUND(RK, K0, K1, K2, K3, K4, B2, B3, B0, B1)
SM4_ROUND(RK, K0, K1, K2, K3, K4, B3, B0, B1, B2)
ADD $16, R3
CMP $128, R3
BNE sm4InitEncLoop
VMOV B1.S[0], B0.S[1]
VMOV B2.S[0], B0.S[2]
VMOV B3.S[0], B0.S[3]
// Multiply by 2 modulo P // Multiply by 2 modulo P
VMOV B0.D[0], I VMOV B0.D[0], I
@ -272,7 +254,7 @@ initLoop:
BNE initLoop BNE initLoop
RET RET
#undef I #undef I
#undef RK #undef SRC
#undef pTbl #undef pTbl
// func gcmSm4Data(productTable *[256]byte, data []byte, T *[16]byte) // func gcmSm4Data(productTable *[256]byte, data []byte, T *[16]byte)

View File

@ -21,7 +21,7 @@ type sm4CipherGCM struct {
var _ gcmAble = (*sm4CipherGCM)(nil) var _ gcmAble = (*sm4CipherGCM)(nil)
//go:noescape //go:noescape
func gcmSm4Init(productTable *[256]byte, rk []uint32) func precomputeTableAsm(productTable *[256]byte, src *[16]byte)
//go:noescape //go:noescape
func gcmSm4Data(productTable *[256]byte, data []byte, T *[16]byte) func gcmSm4Data(productTable *[256]byte, data []byte, T *[16]byte)
@ -41,7 +41,9 @@ func (c *sm4CipherGCM) NewGCM(nonceSize, tagSize int) (cipher.AEAD, error) {
g.cipher = &c.sm4CipherAsm g.cipher = &c.sm4CipherAsm
g.nonceSize = nonceSize g.nonceSize = nonceSize
g.tagSize = tagSize g.tagSize = tagSize
gcmSm4Init(&g.bytesProductTable, g.cipher.enc) var key [gcmBlockSize]byte
c.Encrypt(key[:], key[:])
precomputeTableAsm(&g.bytesProductTable, &key)
return g, nil return g, nil
} }

View File

@ -1,5 +1,5 @@
//go:build amd64 || arm64 //go:build amd64
// +build amd64 arm64 // +build amd64
package sm4 package sm4