1
# -*- test-case-name: twisted.conch.test.test_ssh -*-
2
# Copyright (c) 2001-2007 Twisted Matrix Laboratories.
3
# See LICENSE for details.
7
Common functions for the SSH classes.
9
Maintainer: Paul Swartz
12
import struct, warnings
15
from Crypto import Util
17
warnings.warn("PyCrypto not installed, but continuing anyways!",
20
from twisted.python import randbytes
22
class Entropy(object):
24
A Crypto.Util.randpool.RandomPool mock for compatibility.
26
def get_bytes(self, numBytes):
28
Get a number of random bytes.
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)
42
return struct.pack('!L',len(t)) + t
44
def getNS(s, count=1):
50
for i in range(count):
51
l, = struct.unpack('!L',s[c:c+4])
52
ns.append(s[c+4:4+l+c])
54
return tuple(ns) + (s[c:],)
57
if number==0: return '\000'*4
59
bn = Util.number.long_to_bytes(number)
62
return struct.pack('>L',len(bn)) + bn
64
def getMP(data, count=1):
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
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]))
78
return tuple(mp) + (data[c:],)
81
"""return the MP version of (x**y)%z
88
goes through the first list, looking for items in the second, returns the first one
98
def _fastgetMP(data, count=1):
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)))
105
return tuple(mp) + (data[c:],)
108
i2 = gmpy.mpz(i).binary()[::-1]
109
return struct.pack('!L', len(i2)) + i2
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
115
def _fastpow(x, y, z=None):
116
return pyPow(gmpy.mpz(x), y, z)
119
global getMP, MP, _MPpow
123
__builtins__['pow'] = _fastpow # evil evil