1
2
3
4 """cryptomath module
5
6 This module has basic math/crypto code."""
7
8 import os
9 import math
10 import base64
11 import binascii
12
13 from .compat import *
14
15
16
17
18
19
20
21 try:
22 from M2Crypto import m2
23 m2cryptoLoaded = True
24
25 except ImportError:
26 m2cryptoLoaded = False
27
28
29 try:
30 import gmpy
31 gmpyLoaded = True
32 except ImportError:
33 gmpyLoaded = False
34
35
36 try:
37 import Crypto.Cipher.AES
38 pycryptoLoaded = True
39 except ImportError:
40 pycryptoLoaded = False
41
42
43
44
45
46
47
48 import zlib
49 length = len(zlib.compress(os.urandom(1000)))
50 assert(length > 900)
51
53 s = os.urandom(howMany)
54 assert(len(s) == howMany)
55 return stringToBytes(s)
56 prngName = "os.urandom"
57
58
59
60
61
62
64 total = 0L
65 multiplier = 1L
66 for count in range(len(bytes)-1, -1, -1):
67 byte = bytes[count]
68 total += multiplier * byte
69 multiplier *= 256
70 return total
71
73 """Convert an integer into a bytearray, zero-pad to howManyBytes.
74
75 The returned bytearray may be smaller than howManyBytes, but will
76 not be larger. The returned bytearray will contain a big-endian
77 encoding of the input integer (n).
78 """
79 if howManyBytes == None:
80 howManyBytes = numBytes(n)
81 bytes = createByteArrayZeros(howManyBytes)
82 for count in range(howManyBytes-1, -1, -1):
83 bytes[count] = int(n % 256)
84 n >>= 8
85 return bytes
86
90
94
98
102
106
110
112 try:
113 return base64.decodestring(s)
114 except binascii.Error, e:
115 raise SyntaxError(e)
116 except binascii.Incomplete, e:
117 raise SyntaxError(e)
118
120 return base64.encodestring(s).replace("\n", "")
121
123 if (ord(mpi[4]) & 0x80) !=0:
124 raise AssertionError()
125 bytes = stringToBytes(mpi[4:])
126 return bytesToNumber(bytes)
127
142
143
144
145
146
147
148
150 if n==0:
151 return 0
152 bits = numBits(n)
153 return int(math.ceil(bits / 8.0))
154
155
156
157
158
160 if low >= high:
161 raise AssertionError()
162 howManyBits = numBits(high)
163 howManyBytes = numBytes(high)
164 lastBits = howManyBits % 8
165 while 1:
166 bytes = getRandomBytes(howManyBytes)
167 if lastBits:
168 bytes[0] = bytes[0] % (1 << lastBits)
169 n = bytesToNumber(bytes)
170 if n >= low and n < high:
171 return n
172
174 a, b = max(a,b), min(a,b)
175 while b:
176 a, b = b, a % b
177 return a
178
180 return (a * b) // gcd(a, b)
181
182
183
185 c, d = a, b
186 uc, ud = 1, 0
187 while c != 0:
188 q = d // c
189 c, d = d-(q*c), c
190 uc, ud = ud - (q * uc), uc
191 if d == 1:
192 return ud % b
193 return 0
194
195
196 if gmpyLoaded:
197 - def powMod(base, power, modulus):
198 base = gmpy.mpz(base)
199 power = gmpy.mpz(power)
200 modulus = gmpy.mpz(modulus)
201 result = pow(base, power, modulus)
202 return long(result)
203
204 else:
205 - def powMod(base, power, modulus):
206 if power < 0:
207 result = pow(base, power*-1, modulus)
208 result = invMod(result, modulus)
209 return result
210 else:
211 return pow(base, power, modulus)
212
213
215 sieve = range(n)
216 for count in range(2, int(math.sqrt(n))):
217 if sieve[count] == 0:
218 continue
219 x = sieve[count] * 2
220 while x < len(sieve):
221 sieve[x] = 0
222 x += sieve[count]
223 sieve = [x for x in sieve[2:] if x]
224 return sieve
225
226 sieve = makeSieve(1000)
227
228 -def isPrime(n, iterations=5, display=False):
229
230 for x in sieve:
231 if x >= n: return True
232 if n % x == 0: return False
233
234
235
236 if display: print "*",
237 s, t = n-1, 0
238 while s % 2 == 0:
239 s, t = s//2, t+1
240
241 a = 2
242 for count in range(iterations):
243 v = powMod(a, s, n)
244 if v==1:
245 continue
246 i = 0
247 while v != n-1:
248 if i == t-1:
249 return False
250 else:
251 v, i = powMod(v, 2, n), i+1
252 a = getRandomNumber(2, n)
253 return True
254
256 if bits < 10:
257 raise AssertionError()
258
259
260
261
262
263 low = ((2L ** (bits-1)) * 3) // 2
264 high = 2L ** bits - 30
265 p = getRandomNumber(low, high)
266 p += 29 - (p % 30)
267 while 1:
268 if display: print ".",
269 p += 30
270 if p >= high:
271 p = getRandomNumber(low, high)
272 p += 29 - (p % 30)
273 if isPrime(p, display=display):
274 return p
275
276
278 if bits < 10:
279 raise AssertionError()
280
281
282
283
284
285 low = (2 ** (bits-2)) * 3//2
286 high = (2 ** (bits-1)) - 30
287 q = getRandomNumber(low, high)
288 q += 29 - (q % 30)
289 while 1:
290 if display: print ".",
291 q += 30
292 if (q >= high):
293 q = getRandomNumber(low, high)
294 q += 29 - (q % 30)
295
296
297 if isPrime(q, 0, display=display):
298 p = (2 * q) + 1
299 if isPrime(p, display=display):
300 if isPrime(q, display=display):
301 return p
302