| 1 |
package sigstore |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/ecdsa" |
| 5 |
"crypto/elliptic" |
| 6 |
"crypto/rand" |
| 7 |
) |
| 8 |
|
| 9 |
// GenerateKeyPairResult is a struct to ensure the private and public parts can not be confused by the caller. |
| 10 |
type GenerateKeyPairResult struct { |
| 11 |
PublicKey []byte |
| 12 |
PrivateKey []byte |
| 13 |
} |
| 14 |
|
| 15 |
// GenerateKeyPair generates a public/private key pair usable for signing images using the sigstore format, |
| 16 |
// and returns key representations suitable for storing in long-term files (with the private key encrypted using the provided passphrase). |
| 17 |
// The specific key kind (e.g. algorithm, size), as well as the file format, are unspecified by this API, |
| 18 |
// and can change with best practices over time. |
| 19 |
func GenerateKeyPair(passphrase []byte) (*GenerateKeyPairResult, error) { |
| 20 |
// https://github.com/sigstore/cosign/blob/main/specs/SIGNATURE_SPEC.md#signature-schemes |
| 21 |
// only requires ECDSA-P256 to be supported, so that’s what we must use. |
| 22 |
rawKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 23 |
if err != nil { |
| 24 |
// Coverage: This can fail only if the randomness source fails |
| 25 |
return nil, err |
| 26 |
} |
| 27 |
private, public, err := marshalKeyPair(rawKey, rawKey.Public(), passphrase) |
| 28 |
if err != nil { |
| 29 |
return nil, err |
| 30 |
} |
| 31 |
return &GenerateKeyPairResult{ |
| 32 |
PublicKey: public, |
| 33 |
PrivateKey: private, |
| 34 |
}, nil |
| 35 |
} |