Commit daa2d547 authored by Pascal S. de Kloe's avatar Pascal S. de Kloe Committed by Brad Fitzpatrick

crypto/rsa: add PublicKey.Size accessor

Provide the fixed size from the key pair.

Change-Id: I365c8d0f7d915229ef089e46458d4c83273fc648
Reviewed-on: https://go-review.googlesource.com/103876Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 01237b13
......@@ -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
}
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment