| 1 | //package ecc provides helpers for creating elliptic curve leys |
| 2 | package ecc |
| 3 | |
| 4 | import ( |
| 5 | "crypto/ecdsa" |
| 6 | "crypto/elliptic" |
| 7 | "crypto/x509" |
| 8 | "encoding/pem" |
| 9 | "errors" |
| 10 | "math/big" |
| 11 | ) |
| 12 | |
| 13 | // ReadPublic loads ecdsa.PublicKey from given PKCS1 X509 or PKIX blobs |
| 14 | func ReadPublic(raw []byte) (key *ecdsa.PublicKey, err error) { |
| 15 | var encoded *pem.Block |
| 16 | |
| 17 | if encoded, _ = pem.Decode(raw); encoded == nil { |
| 18 | return nil, errors.New("Ecc.ReadPublic(): Key must be PEM encoded PKCS1 X509 certificate or PKIX EC public key") |
| 19 | } |
| 20 | |
| 21 | var parsedKey interface{} |
| 22 | var cert *x509.Certificate |
| 23 | |
| 24 | if parsedKey, err = x509.ParsePKIXPublicKey(encoded.Bytes); err != nil { |
| 25 | if cert, err = x509.ParseCertificate(encoded.Bytes); err != nil { |
| 26 | return nil, err |
| 27 | } |
| 28 | |
| 29 | parsedKey = cert.PublicKey |
| 30 | } |
| 31 | |
| 32 | var ok bool |
| 33 | |
| 34 | if key, ok = parsedKey.(*ecdsa.PublicKey); !ok { |
| 35 | return nil, errors.New("Ecc.ReadPublic(): Key is not a valid *ecdsa.PublicKey") |
| 36 | } |
| 37 | |
| 38 | return key, nil |
| 39 | } |
| 40 | |
| 41 | // ReadPrivate loads ecdsa.PrivateKey from given PKCS1 or PKCS8 blobs |
| 42 | func ReadPrivate(raw []byte) (key *ecdsa.PrivateKey, err error) { |
| 43 | var encoded *pem.Block |
| 44 | |
| 45 | if encoded, _ = pem.Decode(raw); encoded == nil { |
| 46 | return nil, errors.New("Ecc.ReadPrivate(): Key must be PEM encoded PKCS1 or PKCS8 EC private key") |
| 47 | } |
| 48 | |
| 49 | var parsedKey interface{} |
| 50 | |
| 51 | if parsedKey, err = x509.ParseECPrivateKey(encoded.Bytes); err != nil { |
| 52 | if parsedKey, err = x509.ParsePKCS8PrivateKey(encoded.Bytes); err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | var ok bool |
| 58 | |
| 59 | if key, ok = parsedKey.(*ecdsa.PrivateKey); !ok { |
| 60 | return nil, errors.New("Ecc.ReadPrivate(): Key is not valid *ecdsa.PrivateKey") |
| 61 | } |
| 62 | |
| 63 | return key, nil |
| 64 | } |
| 65 | |
| 66 | // NewPublic constructs ecdsa.PublicKey from given (X,Y) |
| 67 | func NewPublic(x, y []byte) *ecdsa.PublicKey { |
| 68 | return &ecdsa.PublicKey{Curve: curve(len(x)), |
| 69 | X: new(big.Int).SetBytes(x), |
| 70 | Y: new(big.Int).SetBytes(y)} |
| 71 | } |
| 72 | |
| 73 | // NewPrivate constructs ecdsa.PrivateKey from given (X,Y) and D |
| 74 | func NewPrivate(x, y, d []byte) *ecdsa.PrivateKey { |
| 75 | return &ecdsa.PrivateKey{D: new(big.Int).SetBytes(d), |
| 76 | PublicKey: ecdsa.PublicKey{Curve: curve(len(x)), |
| 77 | X: new(big.Int).SetBytes(x), |
| 78 | Y: new(big.Int).SetBytes(y)}} |
| 79 | } |
| 80 | |
| 81 | func curve(size int) elliptic.Curve { |
| 82 | switch size { |
| 83 | case 31, 32: |
| 84 | return elliptic.P256() |
| 85 | case 48: |
| 86 | return elliptic.P384() |
| 87 | case 65, 66: |
| 88 | return elliptic.P521() //adjust for P-521 curve, which can be 65 or 66 bytes |
| 89 | default: |
| 90 | return nil //unsupported curve |
| 91 | } |
| 92 | } |