| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
""" |
|---|
| 7 |
Common functions for the SSH classes. |
|---|
| 8 |
|
|---|
| 9 |
Maintainer: Paul Swartz |
|---|
| 10 |
""" |
|---|
| 11 |
|
|---|
| 12 |
import struct, warnings |
|---|
| 13 |
|
|---|
| 14 |
try: |
|---|
| 15 |
from Crypto import Util |
|---|
| 16 |
except ImportError: |
|---|
| 17 |
warnings.warn("PyCrypto not installed, but continuing anyways!", |
|---|
| 18 |
RuntimeWarning) |
|---|
| 19 |
|
|---|
| 20 |
from twisted.python import randbytes |
|---|
| 21 |
|
|---|
| 22 |
class Entropy(object): |
|---|
| 23 |
""" |
|---|
| 24 |
A Crypto.Util.randpool.RandomPool mock for compatibility. |
|---|
| 25 |
""" |
|---|
| 26 |
def get_bytes(self, numBytes): |
|---|
| 27 |
""" |
|---|
| 28 |
Get a number of random bytes. |
|---|
| 29 |
""" |
|---|
| 30 |
warnings.warn("entropy.get_bytes is deprecated, please use " |
|---|
| 31 |
"twisted.python.randbytes.secureRandom instead.", |
|---|
| 32 |
category=DeprecationWarning, stacklevel=2) |
|---|
| 33 |
return randbytes.secureRandom(numBytes) |
|---|
| 34 |
|
|---|
| 35 |
entropy = Entropy() |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
def NS(t): |
|---|
| 39 |
""" |
|---|
| 40 |
net string |
|---|
| 41 |
""" |
|---|
| 42 |
return struct.pack('!L',len(t)) + t |
|---|
| 43 |
|
|---|
| 44 |
def getNS(s, count=1): |
|---|
| 45 |
""" |
|---|
| 46 |
get net string |
|---|
| 47 |
""" |
|---|
| 48 |
ns = [] |
|---|
| 49 |
c = 0 |
|---|
| 50 |
for i in range(count): |
|---|
| 51 |
l, = struct.unpack('!L',s[c:c+4]) |
|---|
| 52 |
ns.append(s[c+4:4+l+c]) |
|---|
| 53 |
c += 4 + l |
|---|
| 54 |
return tuple(ns) + (s[c:],) |
|---|
| 55 |
|
|---|
| 56 |
def MP(number): |
|---|
| 57 |
if number==0: return '\000'*4 |
|---|
| 58 |
assert number>0 |
|---|
| 59 |
bn = Util.number.long_to_bytes(number) |
|---|
| 60 |
if ord(bn[0])&128: |
|---|
| 61 |
bn = '\000' + bn |
|---|
| 62 |
return struct.pack('>L',len(bn)) + bn |
|---|
| 63 |
|
|---|
| 64 |
def getMP(data, count=1): |
|---|
| 65 |
""" |
|---|
| 66 |
Get multiple precision integer out of the string. A multiple precision |
|---|
| 67 |
integer is stored as a 4-byte length followed by length bytes of the |
|---|
| 68 |
integer. If count is specified, get count integers out of the string. |
|---|
| 69 |
The return value is a tuple of count integers followed by the rest of |
|---|
| 70 |
the data. |
|---|
| 71 |
""" |
|---|
| 72 |
mp = [] |
|---|
| 73 |
c = 0 |
|---|
| 74 |
for i in range(count): |
|---|
| 75 |
length, = struct.unpack('>L',data[c:c+4]) |
|---|
| 76 |
mp.append(Util.number.bytes_to_long(data[c+4:c+4+length])) |
|---|
| 77 |
c += 4 + length |
|---|
| 78 |
return tuple(mp) + (data[c:],) |
|---|
| 79 |
|
|---|
| 80 |
def _MPpow(x, y, z): |
|---|
| 81 |
"""return the MP version of (x**y)%z |
|---|
| 82 |
""" |
|---|
| 83 |
return MP(pow(x,y,z)) |
|---|
| 84 |
|
|---|
| 85 |
def ffs(c, s): |
|---|
| 86 |
""" |
|---|
| 87 |
first from second |
|---|
| 88 |
goes through the first list, looking for items in the second, returns the first one |
|---|
| 89 |
""" |
|---|
| 90 |
for i in c: |
|---|
| 91 |
if i in s: return i |
|---|
| 92 |
|
|---|
| 93 |
getMP_py = getMP |
|---|
| 94 |
MP_py = MP |
|---|
| 95 |
_MPpow_py = _MPpow |
|---|
| 96 |
pyPow = pow |
|---|
| 97 |
|
|---|
| 98 |
def _fastgetMP(data, count=1): |
|---|
| 99 |
mp = [] |
|---|
| 100 |
c = 0 |
|---|
| 101 |
for i in range(count): |
|---|
| 102 |
length = struct.unpack('!L', data[c:c+4])[0] |
|---|
| 103 |
mp.append(long(gmpy.mpz(data[c + 4:c + 4 + length][::-1] + '\x00', 256))) |
|---|
| 104 |
c += length + 4 |
|---|
| 105 |
return tuple(mp) + (data[c:],) |
|---|
| 106 |
|
|---|
| 107 |
def _fastMP(i): |
|---|
| 108 |
i2 = gmpy.mpz(i).binary()[::-1] |
|---|
| 109 |
return struct.pack('!L', len(i2)) + i2 |
|---|
| 110 |
|
|---|
| 111 |
def _fastMPpow(x, y, z=None): |
|---|
| 112 |
r = pyPow(gmpy.mpz(x),y,z).binary()[::-1] |
|---|
| 113 |
return struct.pack('!L', len(r)) + r |
|---|
| 114 |
|
|---|
| 115 |
def _fastpow(x, y, z=None): |
|---|
| 116 |
return pyPow(gmpy.mpz(x), y, z) |
|---|
| 117 |
|
|---|
| 118 |
def install(): |
|---|
| 119 |
global getMP, MP, _MPpow |
|---|
| 120 |
getMP = _fastgetMP |
|---|
| 121 |
MP = _fastMP |
|---|
| 122 |
_MPpow = _fastMPpow |
|---|
| 123 |
__builtins__['pow'] = _fastpow |
|---|
| 124 |
|
|---|
| 125 |
try: |
|---|
| 126 |
import gmpy |
|---|
| 127 |
install() |
|---|
| 128 |
except ImportError: |
|---|
| 129 |
pass |
|---|
| 130 |
|
|---|