Commit graph

21 commits

Author SHA1 Message Date
Legrandin
ec7f3c8662 Remove _fastmath and _slowmath from code base 2014-12-06 12:04:12 +01:00
Legrandin
d83380a048 Removed support for Python<2.4 2014-06-16 20:36:35 +02:00
Legrandin
3755ff63fe Merge branch 'scrypt' of https://github.com/Legrandin/pycrypto
Conflicts:
	lib/Crypto/Protocol/KDF.py
	lib/Crypto/SelfTest/Cipher/common.py
	lib/Crypto/SelfTest/Hash/test_HMAC.py
	lib/Crypto/SelfTest/Protocol/test_KDF.py
	src/hash_template.c
2014-05-11 15:42:33 +02:00
Legrandin
1d093a16db Merge branch 'patch-1' of https://github.com/iwonbigbro/pycrypto 2014-05-11 15:36:51 +02:00
Legrandin
feeaeffc00 Simplify _fastmath (remove import)
The randfunc parameter of getRandomInteger() can
be a callable object or None.

If it is None, C code will import the Random module
and create a new RNG object.

Apart from breaking pypy, the C code is ugly and unnecessary:
it is much easier to ensure that getRandomInteger()
is always called with a valid object from the Python side.
2014-03-04 22:31:51 +01:00
Legrandin
aa32e3d662 Optimize scrypt (~50%) and support for Python 2.1 2013-12-24 23:00:35 +01:00
Legrandin
102cd21c8d Add support for scrypt
scrypt is a robust password-based key derivation function.
These set of changes implements it according to the RFC draft:

http://tools.ietf.org/html/draft-josefsson-scrypt-kdf-01

scrypt is also added to the algorithms understood by PKCS#8
(so that one can protect private keys at rest with it).

Additionally, this patch adds tests cases for PBES functions.
2013-12-24 22:56:21 +01:00
iwonbigbro
da8eb46da8 Fix: AttributeError: 'module' object has no attribute 'HAVE_DECL_MPZ_POWM_SEC'
Fixing run time error caused by invalid attribute reference on object:

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 getattr(_fastmath, "HAVE_DECL_MPZ_POWM_SEC"):
AttributeError: 'module' object has no attribute 'HAVE_DECL_MPZ_POWM_SEC'
2013-08-16 16:22:29 +01:00
Dwayne C. Litzenberger
bf38995ffa Merge branch 'master' into py3k
Conflicts:
	setup.py
	src/_fastmath.c
2011-10-10 14:51:07 -04:00
Dwayne C. Litzenberger
9cfb332b22 autoconf: only use side-channel secured mpz_powm_sec if it's available (libgmp 5 or later) 2011-10-10 14:49:26 -04:00
Thorsten Behrens
cb48387f66 PY3K _fastmath support
o _fastmath now builds and runs on PY3K
o Changes to setup.py to allow /usr/include for gmp.h
o Changes to setup.py to allow linking fastmath w/ static mpir
  on Windows without warning messages
o Changes to test_DSA/test_RSA to throw an exception if _fastmath
  is present but cannot be imported (due to an issue building
  _fastmath or the shared gmp/mpir libraries not being reachable)
o number.py has the code to flag a failing _fastmath, but that
  code is commented out for a better runtime experience
o Clean up the if for py21compat import - should have been == not is
o Clean up some '== None' occurences, now 'is None' instead
2010-12-29 13:21:05 -05:00
Thorsten Behrens
ca701f92f5 PY3K support for _fastmath.c; removed floordiv(a,b) hack and replaced with divmod(a,b)[0]; move to assertEqual throughout the test suite to prep for assert_ and failIf being removed in 3.3/3.4 2010-12-29 05:29:08 -05:00
Thorsten Behrens
295ce314d9 Changes to allow pycrpyto to work on Python 3.x as well as 2.1 through 2.7 2010-12-28 16:26:52 -05:00
Janne Snabb
901254f974 Fix NameError: 'GetRandomNumber_DeprecationWarning' is not defined 2010-08-26 23:33:53 -04:00
Dwayne C. Litzenberger
79f6c64c58 getRandomNumber API compatibility:
Legrandin's getStrongPrime() patch changed the behaviour of
Crypto.Util.number.getRandomNumber() to something that is more like what
people would expect, but different from what we did before.  This change
modifies Crypto.Util.number in the following ways:

- Rename getRandomNBitNumber -> getRandomNBitInteger
  and getRandomNumber -> getRandomInteger
- Preserve old behaviour by making getRandomNumber work the same as
  getRandomNBitInteger.
- Emit a DeprecationWarning when the old getRandomNumber is used.
2010-08-02 17:03:29 -04:00
Dwayne C. Litzenberger
a4cdab130e Fix backward compatibility with PyCrypto 2.1 through 2.5:
- Replaced things like (1 << bits) with (1L << bits). See PEP 237:
    - In Python < 2.4, (1<<31) evaluates as -2147483648
    - In Python >= 2.4, it becomes 2147483648L

- Replaced things like (bits/2) with the equivalent (bits>>1).  This makes
  PyCrypto work when floating-point division is enabled (e.g. in Python 2.6
  with -Qnew)

- In Python < 2.2, expressions like 2**1279, 1007119*2014237, and
  3153640933 raise OverflowError.  Replaced them with it with 2L**1279,
  1007119L*2014237L, and 3153640933, respectively.

- The "//" and "//=" integer division operators are a syntax error in Python
  2.1 and below.  Replaced things like (m //= 2) with the equivalent
  (m >>= 1).

- Where integer division can't be replaced by bit shifting, replace (a/b) with
  (divmod(a, b)[0]).

- math.log takes exactly 1 argument in Python < 2.3, so replaced things like
  "-math.log(false_positive_prob, 4)" with
  "-math.log(false_positive_prob)/math.log(4)".
2010-06-10 23:47:16 -04:00
Lorenz Quack
c575de4f18 getStrongPrime() implementation
From http://lists.dlitz.net/pipermail/pycrypto/2009q4/000167.html, with the
following explanation included in the email:

=== snip ===
Hi there!

Here comes my monster patch.
It includes a python and C version of getStrongPrime, rabinMillerTest and isPrime.
there are also two small unit tests and some helper functions.
They all take a randfunc and propagate them (or so I hope).
The Rabin-Miller-Test uses random bases (non-deterministic).
getStrongPrime and isPrime take an optional parameter "false_positive_prob"
where one can specify the maximum probability that the prime is actually
composite. Internally the functions calculate the Rabin-Miller rounds from
this. It defaults to 1e-6 (1:1000000) which results in 10 rounds of Rabin-Miller
testing.

Please review this carefully. Even though I tried hard to get things right some
bugs always slip through.
maybe you could also review the way I acquire and release the GIL. It felt kind
of ugly the way I did it but I don't see a better way just now.

Concerning the public exponent e:
I now know why it needs to be coprime to p-1 and q-1. The private exponent d is
the inverse of e mod ((p-1)(q-1)).
If e is not coprime to ((p-1)(q-1)) then the inverse does not exist [1].

The getStrongPrime take an optional argument e. if provided the function will
make sure p-1 and e are coprime. if e is even (p-1)/2 will be coprime.
if e is even then there is a additional constraint: p =/= q mod 8.
I can't check for that in getStrongPrime of course but since we hardcoded e to
be odd in _RSA.py this should pose no problem.

The Baillie-PSW-Test is not included.

I tried hard not to use any functionality new than 2.1 but if you find anything
feel free to criticize. Also if I didn't get the coding style right either tell
me or feel free to correct it yourself.

have fun.
//Lorenz

[1] http://mathworld.wolfram.com/ModularInverse.html
=== snip ===
2010-06-10 21:02:07 -04:00
Dwayne C. Litzenberger
8541f7a63d Fix PyCrypto when floor division (python -Qnew) is enabled 2010-05-29 08:11:16 -04:00
Dwayne C. Litzenberger
f0581ba158 number.py: Update the legal notice
I have permission to do this.  See the LEGAL directory.
2009-08-03 19:06:05 -04:00
Dwayne C. Litzenberger
14a3f0b644 Clarify documentation for Crypto.Util.number.getRandomNumber()
Thanks to Sam Phippen for noticing this confusing behaviour.
2009-03-13 18:51:52 -04:00
Dwayne C. Litzenberger
ff8a657a8d cleanup: Move modules to "lib/Crypto" subdirectory.
This will avoid the previous situation where scripts like the old "test.py"
get included accidentally in a release.  It also frees us to put additional
build scripts in the top-level directory of the source tree.
2009-02-28 13:14:53 -05:00
Renamed from Util/number.py (Browse further)