Skip to content
This repository was archived by the owner on Dec 7, 2019. It is now read-only.

Commit 4b040bd

Browse files
committed
forbid RSA keys smaller than 512 bits
We do use small keys for testing but keys smaller than this are entirely useless as we need to be able to sign 256bit hashes. fixes #42
1 parent 0f79fbe commit 4b040bd

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

rsa.go

+14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import (
1313
sha256 "github.com/minio/sha256-simd"
1414
)
1515

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+
1621
// RsaPrivateKey is an rsa private key
1722
type RsaPrivateKey struct {
1823
sk *rsa.PrivateKey
@@ -26,6 +31,9 @@ type RsaPublicKey struct {
2631

2732
// GenerateRSAKeyPair generates a new rsa private and public key
2833
func GenerateRSAKeyPair(bits int, src io.Reader) (PrivKey, PubKey, error) {
34+
if bits < 512 {
35+
return nil, nil, ErrRsaKeyTooSmall
36+
}
2937
priv, err := rsa.GenerateKey(src, bits)
3038
if err != nil {
3139
return nil, nil, err
@@ -111,6 +119,9 @@ func UnmarshalRsaPrivateKey(b []byte) (PrivKey, error) {
111119
if err != nil {
112120
return nil, err
113121
}
122+
if sk.N.BitLen() < 512 {
123+
return nil, ErrRsaKeyTooSmall
124+
}
114125
return &RsaPrivateKey{sk: sk}, nil
115126
}
116127

@@ -129,6 +140,9 @@ func UnmarshalRsaPublicKey(b []byte) (PubKey, error) {
129140
if !ok {
130141
return nil, errors.New("not actually an rsa public key")
131142
}
143+
if pk.N.BitLen() < 512 {
144+
return nil, ErrRsaKeyTooSmall
145+
}
132146
return &RsaPublicKey{pk}, nil
133147
}
134148

rsa_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ func TestRSABasicSignAndVerify(t *testing.T) {
3939
}
4040
}
4141

42+
func TestRSASmallKey(t *testing.T) {
43+
_, _, err := GenerateRSAKeyPair(384, rand.Reader)
44+
if err != ErrRsaKeyTooSmall {
45+
t.Fatal("should have refused to create small RSA key")
46+
}
47+
}
48+
4249
func TestRSASignZero(t *testing.T) {
4350
priv, pub, err := GenerateRSAKeyPair(512, rand.Reader)
4451
if err != nil {

0 commit comments

Comments
 (0)