github.com/emmansun/gmsm@v0.29.1/sm4/cipher_ni.go (about) 1 //go:build (amd64 || arm64 || ppc64 || ppc64le) && !purego 2 3 package sm4 4 5 import ( 6 "crypto/cipher" 7 8 "github.com/emmansun/gmsm/internal/alias" 9 ) 10 11 type sm4CipherNI struct { 12 sm4Cipher 13 } 14 15 // sm4CipherNIGCM implements crypto/cipher.gcmAble so that crypto/cipher.NewGCM 16 // will use the optimised implementation in this file when possible. Instances 17 // of this type only exist when hasGCMAsm and hasSM4 returns true. 18 type sm4CipherNIGCM struct { 19 sm4CipherNI 20 } 21 22 func newCipherNI(key []byte) (cipher.Block, error) { 23 c := &sm4CipherNIGCM{sm4CipherNI{sm4Cipher{}}} 24 expandKeyAsm(&key[0], &ck[0], &c.enc[0], &c.dec[0], INST_SM4) 25 if supportsGFMUL { 26 return c, nil 27 } 28 return &c.sm4CipherNI, nil 29 } 30 31 func (c *sm4CipherNI) Encrypt(dst, src []byte) { 32 if len(src) < BlockSize { 33 panic("sm4: input not full block") 34 } 35 if len(dst) < BlockSize { 36 panic("sm4: output not full block") 37 } 38 if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) { 39 panic("sm4: invalid buffer overlap") 40 } 41 encryptBlockAsm(&c.enc[0], &dst[0], &src[0], INST_SM4) 42 } 43 44 func (c *sm4CipherNI) Decrypt(dst, src []byte) { 45 if len(src) < BlockSize { 46 panic("sm4: input not full block") 47 } 48 if len(dst) < BlockSize { 49 panic("sm4: output not full block") 50 } 51 if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) { 52 panic("sm4: invalid buffer overlap") 53 } 54 encryptBlockAsm(&c.dec[0], &dst[0], &src[0], INST_SM4) 55 }