-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #160 from smlx/fix-invalid-pubkey
fix: correctly convert to openpgp ecdsa key representation
- Loading branch information
Showing
5 changed files
with
75 additions
and
47 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
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,57 @@ | ||
package gpg | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"fmt" | ||
|
||
openpgpecdsa "github.com/ProtonMail/go-crypto/openpgp/ecdsa" | ||
) | ||
|
||
// nameToCurve takes a given curve name and returns the associated | ||
// elliptic.Curve. | ||
func nameToCurve(name string) (elliptic.Curve, error) { | ||
switch name { | ||
case elliptic.P224().Params().Name: | ||
return elliptic.P224(), nil | ||
case elliptic.P256().Params().Name: | ||
return elliptic.P256(), nil | ||
case elliptic.P384().Params().Name: | ||
return elliptic.P384(), nil | ||
case elliptic.P521().Params().Name: | ||
return elliptic.P521(), nil | ||
default: | ||
return nil, fmt.Errorf("unknown curve name: %s", name) | ||
} | ||
} | ||
|
||
// ecdsaPublicKey converts the given ECDSA Key in go-crypto/openpgp | ||
// representation, to standard library crypto/ecdsa representation. | ||
func ecdsaPublicKey(k *openpgpecdsa.PublicKey) (*ecdsa.PublicKey, error) { | ||
curve, err := nameToCurve(k.GetCurve().GetCurveName()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ecdsa.PublicKey{ | ||
Curve: curve, | ||
X: k.X, | ||
Y: k.Y, | ||
}, nil | ||
} | ||
|
||
// ecdsaPrivateKey converts the given ECDSA Key in go-crypto/openpgp | ||
// representation, to standard library crypto/ecdsa representation. | ||
func ecdsaPrivateKey(k *openpgpecdsa.PrivateKey) (*ecdsa.PrivateKey, error) { | ||
curve, err := nameToCurve(k.GetCurve().GetCurveName()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ecdsa.PrivateKey{ | ||
D: k.D, | ||
PublicKey: ecdsa.PublicKey{ | ||
Curve: curve, | ||
X: k.X, | ||
Y: k.Y, | ||
}, | ||
}, nil | ||
} |
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,16 @@ | ||
package securitykey | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
|
||
openpgpecdsa "github.com/ProtonMail/go-crypto/openpgp/ecdsa" | ||
) | ||
|
||
// openpgpECDSAPublicKey converts the given ECDSA Key in crypto/ecdsa | ||
// representation, to go-crypto/openpgp representation. | ||
func openpgpECDSAPublicKey(k *ecdsa.PublicKey) *openpgpecdsa.PublicKey { | ||
openpgpPubKey := openpgpecdsa.NewPublicKeyFromCurve(k.Curve) | ||
openpgpPubKey.X = k.X | ||
openpgpPubKey.Y = k.Y | ||
return openpgpPubKey | ||
} |
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