From: Pascal S. de Kloe Date: Sat, 31 Mar 2018 13:45:35 +0000 (+0200) Subject: crypto/rsa: add PublicKey.Size accessor X-Git-Tag: go1.11beta1~995 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=daa2d547734a3e5693b06d7d09e38ff1e617c89f;p=gostls13.git 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 --- diff --git a/src/crypto/rsa/pkcs1v15.go b/src/crypto/rsa/pkcs1v15.go index 3517a8c776..cdd2071ab9 100644 --- a/src/crypto/rsa/pkcs1v15.go +++ b/src/crypto/rsa/pkcs1v15.go @@ -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 } diff --git a/src/crypto/rsa/rsa.go b/src/crypto/rsa/rsa.go index 0faca43e43..c9fa6fbc35 100644 --- a/src/crypto/rsa/rsa.go +++ b/src/crypto/rsa/rsa.go @@ -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