CVS: crypto/PublicKey RSA.py,1.17,1.18
"A.M. Kuchling" <[email protected]> Fri, 04 Apr 2003 11:03:19 -0800
| Newsgroups | gmane.comp.python.cryptography.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/pycrypto/crypto/PublicKey
In directory sc8-pr-cvs1:/tmp/cvs-serv8136
Modified Files:
RSA.py
Log Message:
Hard-wire encryption exponent to 65537
Simplify key generation by generating two equal-sized primes of size
N/2+1.
Index: RSA.py
===================================================================
RCS file: /cvsroot/pycrypto/crypto/PublicKey/RSA.py,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- RSA.py 4 Apr 2003 15:13:35 -0000 1.17
+++ RSA.py 4 Apr 2003 19:03:13 -0000 1.18
@@ -13,6 +13,7 @@
__revision__ = "$Id$"
from Crypto.PublicKey import pubkey
+from Crypto.Util import number
try:
from Crypto.PublicKey import _fastmath
@@ -30,31 +31,29 @@
the progress of the key generation.
"""
obj=RSAobj()
- # Generate random number from 0 to 7
- difference=ord(randfunc(1)) & 7
-
# Generate the prime factors of n
if progress_func:
progress_func('p\n')
- obj.p=pubkey.getPrime(bits/2, randfunc)
+ obj.p = pubkey.getPrime(bits/2+1, randfunc)
if progress_func:
progress_func('q\n')
- obj.q=pubkey.getPrime((bits/2)+difference, randfunc)
+ obj.q = pubkey.getPrime(bits/2+1, randfunc)
+
# p shall be smaller than q (for calc of u)
- if obj.p>obj.q:
+ 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
+ obj.u = pubkey.inverse(obj.p, obj.q)
+ obj.n = obj.p*obj.q
- # Generate encryption exponent
- if progress_func:
- progress_func('e\n')
- obj.e=pubkey.getPrime(17, randfunc)
+ obj.e = 65537L
if progress_func:
progress_func('d\n')
obj.d=pubkey.inverse(obj.e, (obj.p-1)*(obj.q-1))
+
+ assert obj.size() >= bits, "Generated key is too small"
+
return obj
def construct(tuple):
@@ -123,10 +122,7 @@
"""size() : int
Return the maximum number of bits that can be handled by this key.
"""
- bits, power = 0,1L
- while (power<self.n):
- bits, power = bits+1, power<<1
- return bits-1
+ return number.size(self.n) - 1
def has_private(self):
"""has_private() : bool
@@ -208,32 +204,35 @@
return construct_c((self.key.n, self.key.e))
def generate_c(bits, randfunc, progress_func = None):
- difference=ord(randfunc(1)) & 7
-
# Generate the prime factors of n
if progress_func:
progress_func('p\n')
- p=pubkey.getPrime(bits/2, randfunc)
+ p=pubkey.getPrime(bits/2 + 1, randfunc)
if progress_func:
progress_func('q\n')
- q=pubkey.getPrime((bits/2)+difference, randfunc)
+ q=pubkey.getPrime(bits/2 + 1, randfunc)
# p shall be smaller than q (for calc of u)
- if p>q:
+ 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
- if progress_func:
- progress_func('e\n')
- e=pubkey.getPrime(17, randfunc)
+ e = 65537L
if progress_func:
progress_func('d\n')
d=pubkey.inverse(e, (p-1)*(q-1))
key = _fastmath.rsa_construct(n,e,d,p,q,u)
- return RSAobj_c(key)
+ obj = RSAobj_c(key)
+
+## print p
+## print q
+## print number.size(p), number.size(q), number.size(q*p),
+## print obj.size(), bits
+ assert obj.size() >= bits, "Generated key is too small"
+ return obj
+
def construct_c(tuple):
key = apply(_fastmath.rsa_construct, 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/