-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
93 lines (78 loc) · 2.02 KB
/
key.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package pcert
import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"fmt"
)
var (
defaultAlgorithm = x509.ECDSA
defaultRSAKeySize = 2048
defaultECDSAKeySize = 256
)
// KeyOptions specifies a key algorithm and a size
type KeyOptions struct {
Algorithm x509.PublicKeyAlgorithm
Size int
}
// PublicKeyAlgorithms which are supported to create x509 certificates
var PublicKeyAlgorithms = []x509.PublicKeyAlgorithm{
x509.RSA,
x509.ECDSA,
x509.Ed25519,
}
// GenerateKey returns a private and a public key based on the options.
// If no PublicKeyAlgorithm is set in the options ECDSA is used. If no key size
// is set in the options 256 bit is used for ECDSA and 2048 bit for RSA.
// For ECDSA the following sizes are valid: 224, 256, 384 and 521.
// For the x509.Ed25519 algorithm the size in the KeyOptions is ignored.
func GenerateKey(opts KeyOptions) (crypto.PrivateKey, crypto.PublicKey, error) {
if opts.Algorithm == x509.UnknownPublicKeyAlgorithm {
opts.Algorithm = defaultAlgorithm
}
if opts.Size == 0 {
if opts.Algorithm == x509.RSA {
opts.Size = defaultRSAKeySize
} else if opts.Algorithm == x509.ECDSA {
opts.Size = defaultECDSAKeySize
}
}
switch opts.Algorithm {
case x509.RSA:
priv, err := rsa.GenerateKey(rand.Reader, opts.Size)
if err != nil {
return nil, nil, err
}
pub := priv.Public()
return priv, pub, err
case x509.ECDSA:
var curve elliptic.Curve
switch opts.Size {
case 224:
curve = elliptic.P224()
case 256:
curve = elliptic.P256()
case 384:
curve = elliptic.P384()
case 521:
curve = elliptic.P521()
default:
return nil, nil, fmt.Errorf("invalid size for ecdsa")
}
priv, err := ecdsa.GenerateKey(curve, rand.Reader)
if err != nil {
return nil, nil, err
}
pub := priv.Public()
return priv, pub, nil
case x509.Ed25519:
pub, priv, err := ed25519.GenerateKey(rand.Reader)
return priv, pub, err
default:
return nil, nil, fmt.Errorf("unknown key algorithm: %s", opts.Algorithm)
}
}