1
0
mirror of https://github.com/golang/go synced 2024-09-29 13:34:30 -06:00

crypto/rsa: add PublicKey.Size accessor

Provide the fixed size from the key pair.

Change-Id: I365c8d0f7d915229ef089e46458d4c83273fc648
Reviewed-on: https://go-review.googlesource.com/103876
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Pascal S. de Kloe 2018-03-31 15:45:35 +02:00 committed by Brad Fitzpatrick
parent 01237b1362
commit daa2d54773
2 changed files with 12 additions and 7 deletions

View File

@ -38,7 +38,7 @@ func EncryptPKCS1v15(rand io.Reader, pub *PublicKey, msg []byte) ([]byte, error)
if err := checkPub(pub); err != nil {
return nil, err
}
k := (pub.N.BitLen() + 7) / 8
k := pub.Size()
if len(msg) > k-11 {
return nil, ErrMessageTooLong
}
@ -106,7 +106,7 @@ func DecryptPKCS1v15SessionKey(rand io.Reader, priv *PrivateKey, ciphertext []by
if err := checkPub(&priv.PublicKey); err != nil {
return err
}
k := (priv.N.BitLen() + 7) / 8
k := priv.Size()
if k-(len(key)+3+8) < 0 {
return ErrDecryption
}
@ -134,7 +134,7 @@ func DecryptPKCS1v15SessionKey(rand io.Reader, priv *PrivateKey, ciphertext []by
// in order to maintain constant memory access patterns. If the plaintext was
// valid then index contains the index of the original message in em.
func decryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) (valid int, em []byte, index int, err error) {
k := (priv.N.BitLen() + 7) / 8
k := priv.Size()
if k < 11 {
err = ErrDecryption
return
@ -232,7 +232,7 @@ func SignPKCS1v15(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []b
}
tLen := len(prefix) + hashLen
k := (priv.N.BitLen() + 7) / 8
k := priv.Size()
if k < tLen+11 {
return nil, ErrMessageTooLong
}
@ -268,7 +268,7 @@ func VerifyPKCS1v15(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte)
}
tLen := len(prefix) + hashLen
k := (pub.N.BitLen() + 7) / 8
k := pub.Size()
if k < tLen+11 {
return ErrVerification
}

View File

@ -42,6 +42,11 @@ type PublicKey struct {
E int // public exponent
}
// Size returns the number of bytes for signatures from this key.
func (pub *PublicKey) Size() int {
return (pub.N.BitLen() + 7) / 8
}
// OAEPOptions is an interface for passing options to OAEP decryption using the
// crypto.Decrypter interface.
type OAEPOptions struct {
@ -373,7 +378,7 @@ func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, l
return nil, err
}
hash.Reset()
k := (pub.N.BitLen() + 7) / 8
k := pub.Size()
if len(msg) > k-2*hash.Size()-2 {
return nil, ErrMessageTooLong
}
@ -587,7 +592,7 @@ func DecryptOAEP(hash hash.Hash, random io.Reader, priv *PrivateKey, ciphertext
if err := checkPub(&priv.PublicKey); err != nil {
return nil, err
}
k := (priv.N.BitLen() + 7) / 8
k := priv.Size()
if len(ciphertext) > k ||
k < hash.Size()*2+2 {
return nil, ErrDecryption