Question

I am running python 2.6 on Red Hat 6.4

I had a script running on one machine using paramiko sftp. When I transferred it to another machine I got an exception: SFTP file has no attribute __exit__ (something along those lines).

I decided to upgrade paramiko (thought it would solve the problem) with pip. Now I am getting this the moment I run a python script:

  ...
  File "/usr/lib/python2.6/site-packages/paramiko/__init__.py", line 64, in <module>
    from transport import SecurityOptions, Transport
  File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line 33, in <module>
    from paramiko import util
  File "/usr/lib/python2.6/site-packages/paramiko/util.py", line 33, in <module>
    from paramiko.common import *
  File "/usr/lib/python2.6/site-packages/paramiko/common.py", line 98, in <module>
    from Crypto import Random
  File "/usr/lib64/python2.6/site-packages/Crypto/Random/__init__.py", line 29, in <module>
    from Crypto.Random import _UserFriendlyRNG
  File "/usr/lib64/python2.6/site-packages/Crypto/Random/_UserFriendlyRNG.py", line 38, in <module>
    from Crypto.Random.Fortuna import FortunaAccumulator
  File "/usr/lib64/python2.6/site-packages/Crypto/Random/Fortuna/FortunaAccumulator.py", line 39, in <module>
    import FortunaGenerator
  File "/usr/lib64/python2.6/site-packages/Crypto/Random/Fortuna/FortunaGenerator.py", line 34, in <module>
    from Crypto.Util.number import ceil_shift, exact_log2, exact_div
  File "/usr/lib64/python2.6/site-packages/Crypto/Util/number.py", line 56, in <module>
    if _fastmath is not None and not _fastmath.HAVE_DECL_MPZ_POWM_SEC:
AttributeError: 'module' object has no attribute 'HAVE_DECL_MPZ_POWM_SEC'

So help!!

Was it helpful?

Solution

It seems you have a mix of PyCrypto libraries installed in your system. This bug report on the PyCrypto trackiong system seems related:

My guess is that you have the python-crypto-2.0.1 RPM installed system-wide, and you didn't have gmp-devel installed when you built & installed a later version of PyCrypto, so you'reusing a newer version of PyCrypto with an older version of _fastmath.

You may try to do as suggested: install the gmp-devel package and then PyCrypto with pip.

OTHER TIPS

For me, the issue was that I had the python-crypto package installed via yum, and then had also installed the pycrypto module via pip.

yum remove python-crypto

worked for me.

This one gives better insight into the error itself.

Some developer thought it a good idea to provide a user-friendly warning about a potential security threat, but botched up the code that prints that warning.

I just replaced this:

# You need libgmp v5 or later to get mpz_powm_sec.  Warn if it's not available.
if _fastmath is not None and not _fastmath.HAVE_DECL_MPZ_POWM_SEC:
    _warn("Not using mpz_powm_sec.  You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.", PowmInsecureWarning)

with this:

# You need libgmp v5 or later to get mpz_powm_sec.  Warn if it's not available.
if _fastmath is not None and not hasattr(_fastmath, 'HAVE_DECL_MPZ_POWM_SEC'):
    _warn("Not using mpz_powm_sec.  You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.", PowmInsecureWarning)

in my /usr/lib64/python2.6/site-packages/Crypto/Util/number.py

It still can be done better, but if you don't care for the security threat, this will get you going.

If (like me) you don't have access to number.py, the following may help:

import Crypto.PublicKey._fastmath
Crypto.PublicKey._fastmath.HAVE_DECL_MPZ_POWM_SEC=False

add before to ignore the warning:

import warnings
from Crypto.pct_warnings import PowmInsecureWarning
warnings.simplefilter("ignore", PowmInsecureWarning)

you can find file: /usr/lib/python2.7/site-packages/Crypto/Util/number.py with content:

if _fastmath is not None and not _fastmath.HAVE_DECL_MPZ_POWM_SEC:

Then, you can annotate this line like

#if _fastmath is not None and not _fastmath.HAVE_DECL_MPZ_POWM_SEC:

and you can import paramiko

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top