CVS: crypto/PublicKey RSA.py,1.15,1.16

"A.M. Kuchling" <[email protected]> Fri, 04 Apr 2003 06:59:22 -0800
Newsgroups gmane.comp.python.cryptography.cvs
Message-ID <[email protected]>
Update of /cvsroot/pycrypto/crypto/PublicKey
In directory sc8-pr-cvs1:/tmp/cvs-serv6595/PublicKey

Modified Files:
	RSA.py 
Log Message:
Add support for faster RSA decryption

Index: RSA.py
===================================================================
RCS file: /cvsroot/pycrypto/crypto/PublicKey/RSA.py,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- RSA.py	3 Apr 2003 20:36:13 -0000	1.15
+++ RSA.py	4 Apr 2003 14:59:19 -0000	1.16
@@ -38,6 +38,11 @@
     obj.p=pubkey.getPrime(bits/2, randfunc)
     if progress_func: progress_func('q\n')
     obj.q=pubkey.getPrime((bits/2)+difference, randfunc)
+    # p shall be smaller than q (for calc of u)
+    if obj.p>obj.q:
+        (obj.p, obj.q)=(obj.q, obj.p)
+    if progress_func: progress_func('u\n')
+    obj.u=pubkey.inverse(obj.p, obj.q)
     obj.n=obj.p*obj.q
 
     # Generate encryption exponent
@@ -48,21 +53,29 @@
     return obj
 
 def construct(tuple):
-    """construct(tuple:(long,long)|(long,long,long)|(long,long,long,long,long))
-             : RSAobj
-    Construct an RSA object from a 2-, 3-, or 5-tuple of numbers.
+    """construct(tuple:(long,) : RSAobj
+    Construct an RSA object from a 2-, 3-, 5-, or 6-tuple of numbers.
     """
 
     obj=RSAobj()
-    if len(tuple) not in [2,3,5]:
+    if len(tuple) not in [2,3,5,6]:
         raise error, 'argument for construct() wrong length'
     for i in range(len(tuple)):
         field = obj.keydata[i]
         setattr(obj, field, tuple[i])
+    if len(tuple) >= 5:
+        # Ensure p is smaller than q 
+        if obj.p>obj.q:
+            (obj.p, obj.q)=(obj.q, obj.p)
+
+    if len(tuple) == 5:
+        # u not supplied, so we're going to have to compute it.
+        obj.u=pubkey.inverse(obj.p, obj.q)
+
     return obj
 
 class RSAobj(pubkey.pubkey):
-    keydata=['n', 'e', 'd', 'p','q']
+    keydata = ['n', 'e', 'd', 'p', 'q', 'u']
     def _encrypt(self, plaintext, K=''):
         if self.n<=plaintext:
             raise error, 'Plaintext too large'
@@ -123,7 +136,7 @@
         return construct((self.n, self.e))
 
 class RSAobj_c(pubkey.pubkey):
-    keydata = ['n', 'e', 'd', 'p', 'q']
+    keydata = ['n', 'e', 'd', 'p', 'q', 'u']
 
     def __init__(self, key):
         self.key = key
@@ -153,8 +166,8 @@
             if 'q' not in state:
                 self.key = _fastmath.rsa_construct(n,e,d)
             else:
-                p, q = state['p'], state['q']
-                self.key = _fastmath.rsa_construct(n,e,d,p,q)
+                p, q, u = state['p'], state['q'], state['u']
+                self.key = _fastmath.rsa_construct(n,e,d,p,q,u)
 
     def _encrypt(self, plain, K):
         return (self.key._encrypt(plain),)
@@ -194,6 +207,11 @@
     p=pubkey.getPrime(bits/2, randfunc)
     if progress_func: progress_func('q\n')
     q=pubkey.getPrime((bits/2)+difference, randfunc)
+    # p shall be smaller than q (for calc of u)
+    if p>q:
+        (p, q)=(q, p)
+    if progress_func: progress_func('u\n')
+    u=pubkey.inverse(p, q)
     n=p*q
 
     # Generate encryption exponent
@@ -201,7 +219,7 @@
     e=pubkey.getPrime(17, randfunc)
     if progress_func: progress_func('d\n')
     d=pubkey.inverse(e, (p-1)*(q-1))
-    key = _fastmath.rsa_construct(n,e,d,p,q)
+    key = _fastmath.rsa_construct(n,e,d,p,q,u)
     return RSAobj_c(key)
 
 def construct_c(tuple):



-------------------------------------------------------
This SF.net email is sponsored by: ValueWeb: 
Dedicated Hosting for just $79/mo with 500 GB of bandwidth! 
No other company gives more support or power for your dedicated server
http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/