-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sm4: separate cipher_ni from cipher_asm
- Loading branch information
Showing
4 changed files
with
91 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ codecov: | |
ignore: | ||
- "sm2/gen_p256_table.go" | ||
- "sm4/sm4ni_gcm_asm.go" | ||
- "sm4/cipher_ni.go" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//go:build (amd64 && !generic) || (arm64 && !generic) | ||
// +build amd64,!generic arm64,!generic | ||
|
||
package sm4 | ||
|
||
import ( | ||
"crypto/cipher" | ||
|
||
"github.com/emmansun/gmsm/internal/subtle" | ||
) | ||
|
||
type sm4CipherNI struct { | ||
sm4Cipher | ||
} | ||
|
||
func newCipherNI(key []byte) (cipher.Block, error) { | ||
c := &sm4CipherNI{sm4Cipher{make([]uint32, rounds), make([]uint32, rounds)}} | ||
expandKeyAsm(&key[0], &ck[0], &c.enc[0], &c.dec[0], INST_SM4) | ||
if supportsGFMUL { | ||
return &sm4CipherNIGCM{c}, nil | ||
} | ||
return c, nil | ||
} | ||
|
||
func (c *sm4CipherNI) Encrypt(dst, src []byte) { | ||
if len(src) < BlockSize { | ||
panic("sm4: input not full block") | ||
} | ||
if len(dst) < BlockSize { | ||
panic("sm4: output not full block") | ||
} | ||
if subtle.InexactOverlap(dst[:BlockSize], src[:BlockSize]) { | ||
panic("sm4: invalid buffer overlap") | ||
} | ||
encryptBlockAsm(&c.enc[0], &dst[0], &src[0], INST_SM4) | ||
} | ||
|
||
func (c *sm4CipherNI) Decrypt(dst, src []byte) { | ||
if len(src) < BlockSize { | ||
panic("sm4: input not full block") | ||
} | ||
if len(dst) < BlockSize { | ||
panic("sm4: output not full block") | ||
} | ||
if subtle.InexactOverlap(dst[:BlockSize], src[:BlockSize]) { | ||
panic("sm4: invalid buffer overlap") | ||
} | ||
encryptBlockAsm(&c.dec[0], &dst[0], &src[0], INST_SM4) | ||
} |