]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/rsa: add PublicKey.Size accessor
authorPascal S. de Kloe <pascal@quies.net>
Sat, 31 Mar 2018 13:45:35 +0000 (15:45 +0200)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 3 Apr 2018 15:55:48 +0000 (15:55 +0000)
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>
src/crypto/rsa/pkcs1v15.go
src/crypto/rsa/rsa.go

index 3517a8c776c668d31299cf2f3d0beceaeceab381..cdd2071ab90b79b0228665854cf202ede9861e33 100644 (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
        }
index 0faca43e430766dac34d87a6c4b5e864866bb0f4..c9fa6fbc35d1a9a51b515dd1feda6402727df223 100644 (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