-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,028 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package bip0340 | ||
|
||
import ( | ||
"encoding/asn1" | ||
"crypto/elliptic" | ||
) | ||
|
||
type namedCurveInfo struct { | ||
namedCurve elliptic.Curve | ||
oid asn1.ObjectIdentifier | ||
} | ||
|
||
var namedCurves = make([]namedCurveInfo, 0) | ||
|
||
func AddNamedCurve(curve elliptic.Curve, oid asn1.ObjectIdentifier) { | ||
namedCurves = append(namedCurves, namedCurveInfo{ | ||
namedCurve: curve, | ||
oid: oid, | ||
}) | ||
} | ||
|
||
func NamedCurveFromOid(oid asn1.ObjectIdentifier) elliptic.Curve { | ||
for i := range namedCurves { | ||
cur := &namedCurves[i] | ||
if cur.oid.Equal(oid) { | ||
return cur.namedCurve | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func OidFromNamedCurve(curve elliptic.Curve) (asn1.ObjectIdentifier, bool) { | ||
for i := range namedCurves { | ||
cur := &namedCurves[i] | ||
if cur.namedCurve == curve { | ||
return cur.oid, true | ||
} | ||
} | ||
|
||
return asn1.ObjectIdentifier{}, false | ||
} |
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,147 @@ | ||
package bip0340 | ||
|
||
import ( | ||
"testing" | ||
"crypto/rand" | ||
"crypto/elliptic" | ||
|
||
cryptobin_test "github.com/deatil/go-cryptobin/tool/test" | ||
) | ||
|
||
func Test_MarshalPKCS1(t *testing.T) { | ||
private, err := GenerateKey(rand.Reader, elliptic.P224()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
prikey, err := MarshalECPrivateKey(private) | ||
if err != nil { | ||
t.Errorf("MarshalECPrivateKey error: %s", err) | ||
} | ||
|
||
parsedPri, err := ParseECPrivateKey(prikey) | ||
if err != nil { | ||
t.Errorf("ParseECPrivateKey error: %s", err) | ||
} | ||
|
||
if !private.Equal(parsedPri) { | ||
t.Errorf("parsedPri error") | ||
} | ||
|
||
// t.Errorf("%s \n", encodePEM(prikey, "EC PRIVATE KEY")) | ||
} | ||
|
||
func Test_MarshalPKCS8(t *testing.T) { | ||
private, err := GenerateKey(rand.Reader, elliptic.P224()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
public := &private.PublicKey | ||
|
||
pubkey, err := MarshalPublicKey(public) | ||
if err != nil { | ||
t.Errorf("MarshalPublicKey error: %s", err) | ||
} | ||
|
||
parsedPub, err := ParsePublicKey(pubkey) | ||
if err != nil { | ||
t.Errorf("ParsePublicKey error: %s", err) | ||
} | ||
|
||
prikey, err := MarshalPrivateKey(private) | ||
if err != nil { | ||
t.Errorf("MarshalPrivateKey error: %s", err) | ||
} | ||
|
||
parsedPri, err := ParsePrivateKey(prikey) | ||
if err != nil { | ||
t.Errorf("ParsePrivateKey error: %s", err) | ||
} | ||
|
||
if !public.Equal(parsedPub) { | ||
t.Errorf("parsedPub error") | ||
} | ||
if !private.Equal(parsedPri) { | ||
t.Errorf("parsedPri error") | ||
} | ||
|
||
// t.Errorf("%s, %s \n", encodePEM(pubkey, "PUBLIC KEY"), encodePEM(prikey, "PRIVATE KEY")) | ||
} | ||
|
||
var privPEM = `-----BEGIN PRIVATE KEY----- | ||
MHcCAQAwDwYGKPQoAwAOBgUrgQQAIQRhMF8CAQEEHFGSCJFnOD5T22X6eRTXaj8U | ||
Ou20LRoU3WWq1IKhPAM6AARjo39iPyIBcqR9HR2B62MCVQaolujz78DnIIC6uGFN | ||
ILl2BWUb0iZSoVYch5cB1ztXQnkKw8NIfw== | ||
-----END PRIVATE KEY----- | ||
` | ||
|
||
var pubPEM = `-----BEGIN PUBLIC KEY----- | ||
ME0wDwYGKPQoAwAOBgUrgQQAIQM6AARjo39iPyIBcqR9HR2B62MCVQaolujz78Dn | ||
IIC6uGFNILl2BWUb0iZSoVYch5cB1ztXQnkKw8NIfw== | ||
-----END PUBLIC KEY----- | ||
` | ||
|
||
func Test_PKCS8_Check(t *testing.T) { | ||
test_PKCS8_Check(t, privPEM, pubPEM) | ||
} | ||
|
||
func test_PKCS8_Check(t *testing.T, priv, pub string) { | ||
assertEqual := cryptobin_test.AssertEqualT(t) | ||
|
||
parsedPub, err := ParsePublicKey(decodePEM(pub)) | ||
if err != nil { | ||
t.Errorf("ParsePublicKey error: %s", err) | ||
} | ||
|
||
pubkey, err := MarshalPublicKey(parsedPub) | ||
if err != nil { | ||
t.Errorf("MarshalPublicKey error: %s", err) | ||
} | ||
|
||
pubPemCheck := encodePEM(pubkey, "PUBLIC KEY") | ||
assertEqual(pubPemCheck, pub, "test_Marshal_Check pubkey") | ||
|
||
// =========== | ||
|
||
parsedPriv, err := ParsePrivateKey(decodePEM(priv)) | ||
if err != nil { | ||
t.Errorf("ParsePrivateKey error: %s", err) | ||
} | ||
|
||
privkey, err := MarshalPrivateKey(parsedPriv) | ||
if err != nil { | ||
t.Errorf("MarshalPrivateKey error: %s", err) | ||
} | ||
|
||
privPemCheck := encodePEM(privkey, "PRIVATE KEY") | ||
assertEqual(privPemCheck, priv, "test_Marshal_Check privkey") | ||
} | ||
|
||
var privPKCS1PEM = `-----BEGIN EC PRIVATE KEY----- | ||
MGgCAQEEHAFV6ntMcu9C7KFgNKsxnzxGDVbkXzEEI09byZWgBwYFK4EEACGhPAM6 | ||
AATt8qEbx2NjhCYDsWcavsZB2IyacOUpXJq4jshIULyP0bGmH+RbE7i9RGiLdXn4 | ||
He58X2nZkUkS5A== | ||
-----END EC PRIVATE KEY----- | ||
` | ||
|
||
func Test_PKCS1_Check(t *testing.T) { | ||
test_PKCS1_Check(t, privPKCS1PEM) | ||
} | ||
|
||
func test_PKCS1_Check(t *testing.T, priv string) { | ||
assertEqual := cryptobin_test.AssertEqualT(t) | ||
|
||
parsedPriv, err := ParseECPrivateKey(decodePEM(priv)) | ||
if err != nil { | ||
t.Errorf("ParseECPrivateKey error: %s", err) | ||
} | ||
|
||
privkey, err := MarshalECPrivateKey(parsedPriv) | ||
if err != nil { | ||
t.Errorf("MarshalECPrivateKey error: %s", err) | ||
} | ||
|
||
privPemCheck := encodePEM(privkey, "EC PRIVATE KEY") | ||
assertEqual(privPemCheck, priv, "test_Marshal_Check privkey") | ||
} |
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,26 @@ | ||
package bip0340 | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
// ParseECPrivateKey parses an EC private key in SEC 1, ASN.1 DER form. | ||
// | ||
// This kind of key is commonly encoded in PEM blocks of type "EC PRIVATE KEY". | ||
func ParseECPrivateKey(der []byte) (*PrivateKey, error) { | ||
return parseECPrivateKey(nil, der) | ||
} | ||
|
||
// MarshalECPrivateKey converts an EC private key to SEC 1, ASN.1 DER form. | ||
// | ||
// This kind of key is commonly encoded in PEM blocks of type "EC PRIVATE KEY". | ||
// For a more flexible key format which is not EC specific, use | ||
// MarshalPKCS8PrivateKey. | ||
func MarshalECPrivateKey(key *PrivateKey) ([]byte, error) { | ||
oid, ok := OidFromNamedCurve(key.Curve) | ||
if !ok { | ||
return nil, errors.New("ecgdsa: unknown elliptic curve") | ||
} | ||
|
||
return marshalECPrivateKeyWithOID(key, oid) | ||
} |
Oops, something went wrong.