Skip to content

Commit

Permalink
sm4: separate cipher_ni from cipher_asm
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Jul 19, 2022
1 parent 6a60fe2 commit af50b13
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 39 deletions.
1 change: 1 addition & 0 deletions .github/codecov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ codecov:
ignore:
- "sm2/gen_p256_table.go"
- "sm4/sm4ni_gcm_asm.go"
- "sm4/cipher_ni.go"
41 changes: 41 additions & 0 deletions pkcs8/pkcs8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"testing"

Expand Down Expand Up @@ -598,3 +600,42 @@ func TestUnknownTypeFailure(t *testing.T) {
t.Fatal("expected error")
}
}

// for encrypted private-key information
type encryptedPrivateKeyInfo struct {
EncryptionAlgorithm pkix.AlgorithmIdentifier
EncryptedData []byte
}

func TestParseInvalidPrivateKey(t *testing.T) {
// test parse pem directly
_, err := pkcs8.ParsePKCS8PrivateKeyECDSA([]byte(encryptedEC256aes), []byte("password"))
if err == nil || err.Error() != "pkcs8: this method just supports DER-encoded key" {
t.Errorf("should be error: pkcs8: this method just supports DER-encoded key")
}
_, err = pkcs8.ParsePKCS8PrivateKeyECDSA(nil, []byte("password"))
if err == nil || err.Error() != "pkcs8: only PKCS #5 v2.0 supported" {
t.Errorf("should be error: pkcs8: only PKCS #5 v2.0 supported")
}

var privKey encryptedPrivateKeyInfo
privKey.EncryptionAlgorithm.Algorithm = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 5, 1}
data, err := asn1.Marshal(privKey)
if err != nil {
t.Fatal(err)
}
_, err = pkcs8.ParsePKCS8PrivateKeyECDSA(data, []byte("password"))
if err == nil || err.Error() != "pkcs8: only PBES2 supported" {
t.Errorf("should be error: pkcs8: only PBES2 supported")
}

privKey.EncryptionAlgorithm.Algorithm = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 5, 13}
data, err = asn1.Marshal(privKey)
if err != nil {
t.Fatal(err)
}
_, err = pkcs8.ParsePKCS8PrivateKeyECDSA(data, []byte("password"))
if err == nil || err.Error() != "pkcs8: invalid PBES2 parameters" {
t.Errorf("should be error: pkcs8: invalid PBES2 parameters")
}
}
39 changes: 0 additions & 39 deletions sm4/cipher_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,6 @@ type sm4CipherAsm struct {
blocksSize int
}

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)
}

func newCipher(key []byte) (cipher.Block, error) {
if supportSM4 {
return newCipherNI(key)
Expand Down
49 changes: 49 additions & 0 deletions sm4/cipher_ni.go
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)
}

0 comments on commit af50b13

Please sign in to comment.