CVS: crypto/PublicKey DSA.py,1.13,1.14 ElGamal.py,1.7,1.8 RSA.py,1.16,1.17 qNEW.py,1.7,1.8
"A.M. Kuchling" <[email protected]> Fri, 04 Apr 2003 07:13:43 -0800
| Newsgroups | gmane.comp.python.cryptography.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/pycrypto/crypto/PublicKey
In directory sc8-pr-cvs1:/tmp/cvs-serv12181
Modified Files:
DSA.py ElGamal.py RSA.py qNEW.py
Log Message:
Code formatting improvements; shouldn't be any semantic changes
Index: DSA.py
===================================================================
RCS file: /cvsroot/pycrypto/crypto/PublicKey/DSA.py,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- DSA.py 3 Apr 2003 20:36:09 -0000 1.13
+++ DSA.py 4 Apr 2003 15:13:34 -0000 1.14
@@ -32,12 +32,15 @@
q = bignum(0)
for i in range(0,20):
c=ord(hash1[i])^ord(hash2[i])
- if i==0: c=c | 128
- if i==19: c= c | 1
+ if i==0:
+ c=c | 128
+ if i==19:
+ c= c | 1
q=q*256+c
while (not isPrime(q)):
q=q+2
- if pow(2,159L)<q<pow(2,160L): return S, q
+ if pow(2,159L) < q < pow(2,160L):
+ return S, q
raise error, 'Bad q value generated'
def generate(bits, randfunc, progress_func=None):
@@ -48,10 +51,12 @@
the progress of the key generation.
"""
- if bits<160: raise error, 'Key length <160 bits'
+ if bits<160:
+ raise error, 'Key length <160 bits'
obj=DSAobj()
# Generate string S and prime q
- if progress_func: progress_func('p,q\n')
+ if progress_func:
+ progress_func('p,q\n')
while (1):
S, obj.q = generateQ(randfunc)
n=(bits-1)/160
@@ -63,26 +68,35 @@
for k in range(0, n+1):
V[k]=bytes_to_long(SHA.new(S+str(N)+str(k)).digest())
W=V[n] % powb
- for k in range(n-1, -1, -1): W=(W<<160L)+V[k]
+ for k in range(n-1, -1, -1):
+ W=(W<<160L)+V[k]
X=W+powL1
p=X-(X%(2*obj.q)-1)
- if powL1<=p and isPrime(p): break
+ if powL1<=p and isPrime(p):
+ break
C, N = C+1, N+n+1
- if C<4096: break
- if progress_func: progress_func('4096 multiples failed\n')
+ if C<4096:
+ break
+ if progress_func:
+ progress_func('4096 multiples failed\n')
+
obj.p = p
power=(p-1)/obj.q
- if progress_func: progress_func('h,g\n')
+ if progress_func:
+ progress_func('h,g\n')
while (1):
h=bytes_to_long(randfunc(bits)) % (p-1)
g=pow(h, power, p)
- if 1<h<p-1 and g>1: break
+ if 1<h<p-1 and g>1:
+ break
obj.g=g
- if progress_func: progress_func('x,y\n')
+ if progress_func:
+ progress_func('x,y\n')
while (1):
x=bytes_to_long(randfunc(20))
- if 0<x<obj.q: break
- obj.x, obj.y=x, pow(g, x, p)
+ if 0 < x < obj.q:
+ break
+ obj.x, obj.y = x, pow(g, x, p)
return obj
def construct(tuple):
@@ -107,34 +121,40 @@
raise error, 'DSA algorithm cannot decrypt data'
def _sign(self, M, K):
- if (K<2 or self.q<=K): raise error, 'K is not between 2 and q'
+ if (K<2 or self.q<=K):
+ raise error, 'K is not between 2 and q'
r=pow(self.g, K, self.p) % self.q
s=(inverse(K, self.q)*(M+self.x*r)) % self.q
return (r,s)
def _verify(self, M, sig):
r, s = sig
- if r<=0 or r>=self.q or s<=0 or s>=self.q: return 0
+ if r<=0 or r>=self.q or s<=0 or s>=self.q:
+ return 0
w=inverse(s, self.q)
u1, u2 = (M*w) % self.q, (r*w) % self.q
- v1=pow(self.g, u1, self.p)
- v2=pow(self.y, u2, self.p)
- v=((v1*v2) % self.p)
- v=v % self.q
- if v==r: return 1
+ v1 = pow(self.g, u1, self.p)
+ v2 = pow(self.y, u2, self.p)
+ v = ((v1*v2) % self.p)
+ v = v % self.q
+ if v==r:
+ return 1
return 0
def size(self):
"Return the maximum number of bits that can be handled by this key."
bits, power = 0,1L
- while (power<self.p): bits, power = bits+1, power<<1
+ while (power<self.p):
+ bits, power = bits+1, power<<1
return bits-1
def has_private(self):
"""Return a Boolean denoting whether the object contains
private components."""
- if hasattr(self, 'x'): return 1
- else: return 0
+ if hasattr(self, 'x'):
+ return 1
+ else:
+ return 0
def can_sign(self):
"""Return a Boolean value recording whether this algorithm can generate signatures."""
Index: ElGamal.py
===================================================================
RCS file: /cvsroot/pycrypto/crypto/PublicKey/ElGamal.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- ElGamal.py 3 Apr 2003 20:36:12 -0000 1.7
+++ ElGamal.py 4 Apr 2003 15:13:35 -0000 1.8
@@ -27,29 +27,39 @@
"""
obj=ElGamalobj()
# Generate prime p
- if progress_func: progress_func('p\n')
+ if progress_func:
+ progress_func('p\n')
obj.p=bignum(getPrime(bits, randfunc))
# Generate random number g
- if progress_func: progress_func('g\n')
+ if progress_func:
+ progress_func('g\n')
size=bits-1-(ord(randfunc(1)) & 63) # g will be from 1--64 bits smaller than p
- if size<1: size=bits-1
+ if size<1:
+ size=bits-1
while (1):
obj.g=bignum(getPrime(size, randfunc))
- if obj.g<obj.p: break
+ if obj.g < obj.p:
+ break
size=(size+1) % bits
- if size==0: size=4
+ if size==0:
+ size=4
# Generate random number x
- if progress_func: progress_func('x\n')
+ if progress_func:
+ progress_func('x\n')
while (1):
size=bits-1-ord(randfunc(1)) # x will be from 1 to 256 bits smaller than p
- if size>2: break
+ if size>2:
+ break
while (1):
obj.x=bignum(getPrime(size, randfunc))
- if obj.x<obj.p: break
- size=(size+1) % bits
- if size==0: size=4
- if progress_func: progress_func('y\n')
- obj.y=pow(obj.g, obj.x, obj.p)
+ if obj.x < obj.p:
+ break
+ size = (size+1) % bits
+ if size==0:
+ size=4
+ if progress_func:
+ progress_func('y\n')
+ obj.y = pow(obj.g, obj.x, obj.p)
return obj
def construct(tuple):
@@ -97,7 +107,8 @@
v1=pow(self.y, sig[0], self.p)
v1=(v1*pow(sig[0], sig[1], self.p)) % self.p
v2=pow(self.g, M, self.p)
- if v1==v2: return 1
+ if v1==v2:
+ return 1
return 0
def size(self):
@@ -109,8 +120,10 @@
def has_private(self):
"""Return a Boolean denoting whether the object contains
private components."""
- if hasattr(self, 'x'): return 1
- else: return 0
+ if hasattr(self, 'x'):
+ return 1
+ else:
+ return 0
def publickey(self):
"""Return a new key object containing only the public information."""
Index: RSA.py
===================================================================
RCS file: /cvsroot/pycrypto/crypto/PublicKey/RSA.py,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- RSA.py 4 Apr 2003 14:59:19 -0000 1.16
+++ RSA.py 4 Apr 2003 15:13:35 -0000 1.17
@@ -34,21 +34,26 @@
difference=ord(randfunc(1)) & 7
# Generate the prime factors of n
- if progress_func: progress_func('p\n')
+ if progress_func:
+ progress_func('p\n')
obj.p=pubkey.getPrime(bits/2, randfunc)
- if progress_func: progress_func('q\n')
+ 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')
+ if progress_func:
+ progress_func('u\n')
obj.u=pubkey.inverse(obj.p, obj.q)
obj.n=obj.p*obj.q
# Generate encryption exponent
- if progress_func: progress_func('e\n')
+ if progress_func:
+ progress_func('e\n')
obj.e=pubkey.getPrime(17, randfunc)
- if progress_func: progress_func('d\n')
+ if progress_func:
+ progress_func('d\n')
obj.d=pubkey.inverse(obj.e, (obj.p-1)*(obj.q-1))
return obj
@@ -93,7 +98,8 @@
def _verify(self, M, sig):
m2=self._encrypt(sig[0])
- if m2[0]==M: return 1
+ if m2[0]==M:
+ return 1
else: return 0
def _blind(self, M, B):
@@ -118,7 +124,8 @@
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
+ while (power<self.n):
+ bits, power = bits+1, power<<1
return bits-1
def has_private(self):
@@ -126,7 +133,8 @@
Return a Boolean denoting whether the object contains
private components.
"""
- if hasattr(self, 'd'): return 1
+ if hasattr(self, 'd'):
+ return 1
else: return 0
def publickey(self):
@@ -203,21 +211,26 @@
difference=ord(randfunc(1)) & 7
# Generate the prime factors of n
- if progress_func: progress_func('p\n')
+ if progress_func:
+ progress_func('p\n')
p=pubkey.getPrime(bits/2, randfunc)
- if progress_func: progress_func('q\n')
+ 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')
+ 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')
+ if progress_func:
+ progress_func('e\n')
e=pubkey.getPrime(17, randfunc)
- if progress_func: progress_func('d\n')
+ 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)
Index: qNEW.py
===================================================================
RCS file: /cvsroot/pycrypto/crypto/PublicKey/qNEW.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- qNEW.py 3 Apr 2003 20:36:14 -0000 1.7
+++ qNEW.py 4 Apr 2003 15:13:35 -0000 1.8
@@ -40,7 +40,8 @@
# use the seed to duplicate the key generation. This can
# protect you from someone generating values of p,q that have
# some special form that's easy to break.
- if progress_func: progress_func('p,q\n')
+ if progress_func:
+ progress_func('p,q\n')
while (1):
obj.q = getPrime(160, randfunc)
# assert pow(2, 159L)<obj.q<pow(2, 160L)
@@ -65,32 +66,39 @@
p = p - (p % (2*obj.q)-1)
# If p is still the right size, and it's prime, we're done!
- if powL1<=p and isPrime(p): break
+ if powL1<=p and isPrime(p):
+ break
# Otherwise, increment the counter and try again
C, N = C+1, N+n+1
- if C<4096: break # Ended early, so exit the while loop
- if progress_func: progress_func('4096 values of p tried\n')
+ if C<4096:
+ break # Ended early, so exit the while loop
+ if progress_func:
+ progress_func('4096 values of p tried\n')
obj.p = p
power=(p-1)/obj.q
# Next parameter: g = h**((p-1)/q) mod p, such that h is any
# number <p-1, and g>1. g is kept; h can be discarded.
- if progress_func: progress_func('h,g\n')
+ if progress_func:
+ progress_func('h,g\n')
while (1):
h=bytes_to_long(randfunc(bits)) % (p-1)
g=pow(h, power, p)
- if 1<h<p-1 and g>1: break
+ if 1<h<p-1 and g>1:
+ break
obj.g=g
# x is the private key information, and is
# just a random number between 0 and q.
# y=g**x mod p, and is part of the public information.
- if progress_func: progress_func('x,y\n')
+ if progress_func:
+ progress_func('x,y\n')
while (1):
x=bytes_to_long(randfunc(20))
- if 0<x<obj.q: break
+ if 0 < x < obj.q:
+ break
obj.x, obj.y=x, pow(g, x, p)
return obj
@@ -123,15 +131,18 @@
return (r,s)
def _verify(self, M, sig):
r, s = sig
- if r<=0 or r>=self.q or s<=0 or s>=self.q: return 0
+ if r<=0 or r>=self.q or s<=0 or s>=self.q:
+ return 0
if M<0:
raise error, 'Illegal value of M (<0)'
- if M<=0 or M>=pow(2,161L): return 0
- v1=pow(self.g, s, self.p)
- v2=pow(self.y, M*r, self.p)
- v=((v1*v2) % self.p)
- v=v % self.q
- if v==r: return 1
+ if M<=0 or M>=pow(2,161L):
+ return 0
+ v1 = pow(self.g, s, self.p)
+ v2 = pow(self.y, M*r, self.p)
+ v = ((v1*v2) % self.p)
+ v = v % self.q
+ if v==r:
+ return 1
return 0
def size(self):
@@ -146,6 +157,7 @@
def can_sign(self):
"""Return a Boolean value recording whether this algorithm can generate signatures."""
return 1
+
def can_encrypt(self):
"""Return a Boolean value recording whether this algorithm can encrypt data."""
return 0
-------------------------------------------------------
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/