OLD | NEW |
1 # Authors: | 1 # Authors: |
2 # Trevor Perrin | 2 # Trevor Perrin |
3 # Martin von Loewis - python 3 port | 3 # Martin von Loewis - python 3 port |
| 4 # Yngve Pettersen (ported by Paul Sokolovsky) - TLS 1.2 |
4 # | 5 # |
5 # See the LICENSE file for legal information regarding use of this file. | 6 # See the LICENSE file for legal information regarding use of this file. |
6 | 7 |
7 """cryptomath module | 8 """cryptomath module |
8 | 9 |
9 This module has basic math/crypto code.""" | 10 This module has basic math/crypto code.""" |
10 from __future__ import print_function | 11 from __future__ import print_function |
11 import os | 12 import os |
12 import math | 13 import math |
13 import base64 | 14 import base64 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 def HMAC_MD5(k, b): | 76 def HMAC_MD5(k, b): |
76 k = compatHMAC(k) | 77 k = compatHMAC(k) |
77 b = compatHMAC(b) | 78 b = compatHMAC(b) |
78 return bytearray(hmac.new(k, b, hashlib.md5).digest()) | 79 return bytearray(hmac.new(k, b, hashlib.md5).digest()) |
79 | 80 |
80 def HMAC_SHA1(k, b): | 81 def HMAC_SHA1(k, b): |
81 k = compatHMAC(k) | 82 k = compatHMAC(k) |
82 b = compatHMAC(b) | 83 b = compatHMAC(b) |
83 return bytearray(hmac.new(k, b, hashlib.sha1).digest()) | 84 return bytearray(hmac.new(k, b, hashlib.sha1).digest()) |
84 | 85 |
| 86 def HMAC_SHA256(k, b): |
| 87 k = compatHMAC(k) |
| 88 b = compatHMAC(b) |
| 89 return bytearray(hmac.new(k, b, hashlib.sha256).digest()) |
85 | 90 |
86 # ************************************************************************** | 91 # ************************************************************************** |
87 # Converter Functions | 92 # Converter Functions |
88 # ************************************************************************** | 93 # ************************************************************************** |
89 | 94 |
90 def bytesToNumber(b): | 95 def bytesToNumber(b): |
91 total = 0 | 96 total = 0 |
92 multiplier = 1 | 97 multiplier = 1 |
93 for count in range(len(b)-1, -1, -1): | 98 for count in range(len(b)-1, -1, -1): |
94 byte = b[count] | 99 byte = b[count] |
95 total += multiplier * byte | 100 total += multiplier * byte |
96 multiplier *= 256 | 101 multiplier *= 256 |
97 # Force-cast to long to appease PyCrypto. | 102 return total |
98 # https://github.com/trevp/tlslite/issues/15 | |
99 return long(total) | |
100 | 103 |
101 def numberToByteArray(n, howManyBytes=None): | 104 def numberToByteArray(n, howManyBytes=None): |
102 """Convert an integer into a bytearray, zero-pad to howManyBytes. | 105 """Convert an integer into a bytearray, zero-pad to howManyBytes. |
103 | 106 |
104 The returned bytearray may be smaller than howManyBytes, but will | 107 The returned bytearray may be smaller than howManyBytes, but will |
105 not be larger. The returned bytearray will contain a big-endian | 108 not be larger. The returned bytearray will contain a big-endian |
106 encoding of the input integer (n). | 109 encoding of the input integer (n). |
107 """ | 110 """ |
108 if howManyBytes == None: | 111 if howManyBytes == None: |
109 howManyBytes = numBytes(n) | 112 howManyBytes = numBytes(n) |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 if power < 0: | 214 if power < 0: |
212 result = pow(base, power*-1, modulus) | 215 result = pow(base, power*-1, modulus) |
213 result = invMod(result, modulus) | 216 result = invMod(result, modulus) |
214 return result | 217 return result |
215 else: | 218 else: |
216 return pow(base, power, modulus) | 219 return pow(base, power, modulus) |
217 | 220 |
218 #Pre-calculate a sieve of the ~100 primes < 1000: | 221 #Pre-calculate a sieve of the ~100 primes < 1000: |
219 def makeSieve(n): | 222 def makeSieve(n): |
220 sieve = list(range(n)) | 223 sieve = list(range(n)) |
221 for count in range(2, int(math.sqrt(n))): | 224 for count in range(2, int(math.sqrt(n))+1): |
222 if sieve[count] == 0: | 225 if sieve[count] == 0: |
223 continue | 226 continue |
224 x = sieve[count] * 2 | 227 x = sieve[count] * 2 |
225 while x < len(sieve): | 228 while x < len(sieve): |
226 sieve[x] = 0 | 229 sieve[x] = 0 |
227 x += sieve[count] | 230 x += sieve[count] |
228 sieve = [x for x in sieve[2:] if x] | 231 sieve = [x for x in sieve[2:] if x] |
229 return sieve | 232 return sieve |
230 | 233 |
231 sieve = makeSieve(1000) | 234 sieve = makeSieve(1000) |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 if (q >= high): | 300 if (q >= high): |
298 q = getRandomNumber(low, high) | 301 q = getRandomNumber(low, high) |
299 q += 29 - (q % 30) | 302 q += 29 - (q % 30) |
300 #Ideas from Tom Wu's SRP code | 303 #Ideas from Tom Wu's SRP code |
301 #Do trial division on p and q before Rabin-Miller | 304 #Do trial division on p and q before Rabin-Miller |
302 if isPrime(q, 0, display=display): | 305 if isPrime(q, 0, display=display): |
303 p = (2 * q) + 1 | 306 p = (2 * q) + 1 |
304 if isPrime(p, display=display): | 307 if isPrime(p, display=display): |
305 if isPrime(q, display=display): | 308 if isPrime(q, display=display): |
306 return p | 309 return p |
OLD | NEW |