exception raised when decrypt with private key re-constructed by only n, d

Bug #1664861 reported by chamos lee
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Python-Crypto
New
Undecided
Unassigned

Bug Description

the following code would raise the exception
python 3.5 64bit on windows 10 64bit

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as cipher_pkcs1

    public_key = """-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCIk50gdmuHTUqE1Iet3f3/uDg3/vLNpQlCoFTs
G+NYvQAToihP+HSxAg5lnzYvfruNV0QnIPRwSKFbPmNeHnMmPXogmjyJB1hjQcKEc/SvErMoMFuA
2phryr+gT1kG0s6MtLN1Y4LVr2tNiqFLDYr7WCn52Zj842PWSiGu/elx4QIDAQAB
-----END PUBLIC KEY-----"""

# original private key which contains n,d,e,p,q
# private_key = """-----BEGIN PRIVATE KEY-----
# MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIiTnSB2a4dNSoTUh63d/f+4ODf+
# 8s2lCUKgVOwb41i9ABOiKE/4dLECDmWfNi9+u41XRCcg9HBIoVs+Y14ecyY9eiCaPIkHWGNBwoRz
# 9K8SsygwW4DamGvKv6BPWQbSzoy0s3VjgtWva02KoUsNivtYKfnZmPzjY9ZKIa796XHhAgMBAAEC
# gYBq7qhnclBebzWZKo3Qo67rOOFLdmHEgSLu93mGI4+tOs2EkcHeWzmCHYoIJs/V//P4hgrmV/cK
# stMjDYXfexfL+mzRUxQg5XBfiuqEahRTxOBxvXYnqJlyeNVtsxatF+bjA8m3ZZ5nuG+41ETHzkfS
# LAQsCzkS+IQP/Kr4POVVwQJBAPHQBfB0XsT8mtZrk6GtqSsDli/+/GqRgPP6jBvIY0yG6lwtBFW4
# O/NDmKrtSvATzuKF+3tWlmP5cRUj+xNiBE8CQQCQlvee0ZCMMM/d1r6WmrDYVpyg7kDAfEkFghKk
# ao+ntPDCyf93NjfqLV4p33HuSmOt9zYW3rUeJzhC/FJAHSrPAkAi+24wiBVBuC2KQYvdAuuLPnyW
# uvO3Uw9tvFxSlJ3Q71+uMBEFeZ6qpafLaoLB0ovGd42XHUKqeeajIU5sUmy/AkAWCk04sm08UXx+
# Lmvi/TWT5K//gBHyxX9EDa3VzkCTqetBZhVBEMy5L6046MyvLDvBmvttxQkKzaOv4lKPX2LVAkEA
# 64qQuwJD+dLLMOpL34fRZk1NXzqi95S0G2aseGphJ8xI37QXHV/ogZ6y0N1WHNcnc6L/hXVlYrH+
# OOLi6VEyjw==
# -----END PRIVATE KEY-----"""

    #private key re-constructed by n d and p q not present
    private_key = """-----BEGIN PRIVATE KEY-----
MIIBNgIBADANBgkqhkiG9w0BAQEFAASCASAwggEcAgEAAoGBAIiTnSB2a4dNSoTUh63d/f+4ODf+
8s2lCUKgVOwb41i9ABOiKE/4dLECDmWfNi9+u41XRCcg9HBIoVs+Y14ecyY9eiCaPIkHWGNBwoRz
9K8SsygwW4DamGvKv6BPWQbSzoy0s3VjgtWva02KoUsNivtYKfnZmPzjY9ZKIa796XHhAgEAAoGA
au6oZ3JQXm81mSqN0KOu6zjhS3ZhxIEi7vd5hiOPrTrNhJHB3ls5gh2KCCbP1f/z+IYK5lf3CrLT
Iw2F33sXy/ps0VMUIOVwX4rqhGoUU8Tgcb12J6iZcnjVbbMWrRfm4wPJt2WeZ7hvuNREx85H0iwE
LAs5EviED/yq+DzlVcECAQACAQACAQACAQACAQA=
-----END PRIVATE KEY-----"""

    message = 'abcdef'.encode()
    pub_key = RSA.importKey(public_key)
    cipher = cipher_pkcs1.new(pub_key)
    encrypted = cipher.encrypt(message)
    print("{} encrypted as {}".format(message, encrypted))

    pri_key = RSA.importKey(private_key)
    cipher = cipher_pkcs1.new(pri_key)
    sentinel = None
    decrypted = cipher.decrypt(encrypted, sentinel)
    print("{} decrypted as {}".format(encrypted, decrypted))

the error looks like:
File "C:\Anaconda3\lib\site-packages\Crypto\Cipher\PKCS1_v1_5.py", line 206, in decrypt
    m = self._key.decrypt(ct)
  File "C:\Anaconda3\lib\site-packages\Crypto\PublicKey\RSA.py", line 174, in decrypt
    return pubkey.pubkey.decrypt(self, ciphertext)
  File "C:\Anaconda3\lib\site-packages\Crypto\PublicKey\pubkey.py", line 93, in decrypt
    plaintext=self._decrypt(ciphertext)
  File "C:\Anaconda3\lib\site-packages\Crypto\PublicKey\RSA.py", line 240, in _decrypt
    mp = self.key._decrypt(cp)
  File "C:\Anaconda3\lib\site-packages\Crypto\PublicKey\_slowmath.py", line 54, in _decrypt
    m1 = pow(c, self.d % (self.p-1), self.p)
ValueError: pow() 3rd argument cannot be 0

using the original private key instead of the re-constructed one, everything is fine.
with the following modifications, the decryption would work:
1. _slowmath.py line 53
-if (hasattr(self,'p') and hasattr(self,'q') and hasattr(self,'u')):
+if (hasattr(self,'p') and hasattr(self,'q') and hasattr(self,'u') and self.p > 0 and self.q > 0):
2. RSA.py line 235
-r = getRandomRange(1, self.key.n-1, randfunc=self._randfunc)
+r = 1

have no idea why the r has to be 1, 1 just works.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.