mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-21 17:56:19 +08:00
sm2,smx509: add encoding paths for SM2 ecdh keys
This commit is contained in:
parent
984913e228
commit
f1993bc41a
@ -424,6 +424,20 @@ func TestCipherASN1WithInvalidBytes(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPublicKeyToECDH(t *testing.T) {
|
||||||
|
priv, _ := GenerateKey(rand.Reader)
|
||||||
|
_, err := PublicKeyToECDH(&priv.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
p256, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||||
|
_, err = PublicKeyToECDH(&p256.PublicKey)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("should be error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkGenerateKey_SM2(b *testing.B) {
|
func BenchmarkGenerateKey_SM2(b *testing.B) {
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/emmansun/gmsm/ecdh"
|
||||||
"github.com/emmansun/gmsm/sm2"
|
"github.com/emmansun/gmsm/sm2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -186,3 +187,29 @@ func parseAndCheckCsr(csrPem []byte) error {
|
|||||||
}
|
}
|
||||||
return csr.CheckSignature()
|
return csr.CheckSignature()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMarshalECDHPKIXPublicKey(t *testing.T) {
|
||||||
|
privKey, err := ecdh.P256().GenerateKey(rand.Reader)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
result1, err := MarshalPKIXPublicKey(privKey.Public())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
pubKey, err := ParsePKIXPublicKey(result1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
sm2PubKey, ok := pubKey.(*ecdsa.PublicKey)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("should be valid sm2 public key")
|
||||||
|
}
|
||||||
|
sm2ecdhPub, err := sm2.PublicKeyToECDH(sm2PubKey)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !privKey.PublicKey().Equal(sm2ecdhPub) {
|
||||||
|
t.Fatal("should be same")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user