@@ -13,6 +13,11 @@ import (
13
13
sha256 "github.com/minio/sha256-simd"
14
14
)
15
15
16
+ // ErrRsaKeyTooSmall is returned when trying to generate or parse an RSA key
17
+ // that's smaller than 512 bits. Keys need to be larger enough to sign a 256bit
18
+ // hash so this is a reasonable absolute minimum.
19
+ var ErrRsaKeyTooSmall = errors .New ("rsa keys must be >= 512 bits to be useful" )
20
+
16
21
// RsaPrivateKey is an rsa private key
17
22
type RsaPrivateKey struct {
18
23
sk * rsa.PrivateKey
@@ -26,6 +31,9 @@ type RsaPublicKey struct {
26
31
27
32
// GenerateRSAKeyPair generates a new rsa private and public key
28
33
func GenerateRSAKeyPair (bits int , src io.Reader ) (PrivKey , PubKey , error ) {
34
+ if bits < 512 {
35
+ return nil , nil , ErrRsaKeyTooSmall
36
+ }
29
37
priv , err := rsa .GenerateKey (src , bits )
30
38
if err != nil {
31
39
return nil , nil , err
@@ -111,6 +119,9 @@ func UnmarshalRsaPrivateKey(b []byte) (PrivKey, error) {
111
119
if err != nil {
112
120
return nil , err
113
121
}
122
+ if sk .N .BitLen () < 512 {
123
+ return nil , ErrRsaKeyTooSmall
124
+ }
114
125
return & RsaPrivateKey {sk : sk }, nil
115
126
}
116
127
@@ -129,6 +140,9 @@ func UnmarshalRsaPublicKey(b []byte) (PubKey, error) {
129
140
if ! ok {
130
141
return nil , errors .New ("not actually an rsa public key" )
131
142
}
143
+ if pk .N .BitLen () < 512 {
144
+ return nil , ErrRsaKeyTooSmall
145
+ }
132
146
return & RsaPublicKey {pk }, nil
133
147
}
134
148
0 commit comments