github.com/emmansun/gmsm@v0.29.1/sm4/sm4ni_xts.go (about) 1 //go:build arm64 && !purego 2 3 package sm4 4 5 import ( 6 "crypto/cipher" 7 ) 8 9 // Assert that sm4CipherAsm implements the xtsEncAble and xtsDecAble interfaces. 10 var _ xtsEncAble = (*sm4CipherNI)(nil) 11 var _ xtsDecAble = (*sm4CipherNI)(nil) 12 13 type xtsNI struct { 14 b *sm4CipherNI 15 tweak [BlockSize]byte 16 isGB bool // if true, follows GB/T 17964-2021 17 enc int 18 } 19 20 func (b *sm4CipherNI) NewXTSEncrypter(encryptedTweak *[BlockSize]byte, isGB bool) cipher.BlockMode { 21 var c xtsNI 22 c.b = b 23 c.enc = xtsEncrypt 24 c.isGB = isGB 25 copy(c.tweak[:], encryptedTweak[:]) 26 return &c 27 } 28 29 func (b *sm4CipherNI) NewXTSDecrypter(encryptedTweak *[BlockSize]byte, isGB bool) cipher.BlockMode { 30 var c xtsNI 31 c.b = b 32 c.enc = xtsDecrypt 33 c.isGB = isGB 34 copy(c.tweak[:], encryptedTweak[:]) 35 return &c 36 } 37 38 func (x *xtsNI) BlockSize() int { return BlockSize } 39 40 //go:noescape 41 func encryptSm4NiXts(xk *uint32, tweak *[BlockSize]byte, dst, src []byte) 42 43 //go:noescape 44 func encryptSm4NiXtsGB(xk *uint32, tweak *[BlockSize]byte, dst, src []byte) 45 46 //go:noescape 47 func decryptSm4NiXts(xk *uint32, tweak *[BlockSize]byte, dst, src []byte) 48 49 //go:noescape 50 func decryptSm4NiXtsGB(xk *uint32, tweak *[BlockSize]byte, dst, src []byte) 51 52 func (x *xtsNI) CryptBlocks(dst, src []byte) { 53 validateXtsInput(dst, src) 54 if x.enc == xtsEncrypt { 55 if x.isGB { 56 encryptSm4NiXtsGB(&x.b.enc[0], &x.tweak, dst, src) 57 } else { 58 encryptSm4NiXts(&x.b.enc[0], &x.tweak, dst, src) 59 } 60 } else { 61 if x.isGB { 62 decryptSm4NiXtsGB(&x.b.dec[0], &x.tweak, dst, src) 63 } else { 64 decryptSm4NiXts(&x.b.dec[0], &x.tweak, dst, src) 65 } 66 } 67 }