├── Crypto ├── Random │ ├── Fortuna │ │ ├── __init__.py │ │ ├── SHAd256.pyc │ │ ├── __init__.pyc │ │ ├── FortunaGenerator.pyc │ │ ├── FortunaAccumulator.pyc │ │ └── SHAd256.py │ ├── OSRNG │ │ ├── nt.pyc │ │ ├── posix.pyc │ │ ├── __init__.pyc │ │ ├── fallback.pyc │ │ ├── rng_base.pyc │ │ ├── __init__.py │ │ ├── fallback.py │ │ ├── nt.py │ │ ├── posix.py │ │ └── rng_base.py │ ├── __init__.pyc │ ├── random.pyc │ ├── _UserFriendlyRNG.pyc │ └── __init__.py ├── Cipher │ ├── AES.pyc │ ├── DES.pyc │ ├── XOR.pyc │ ├── _AES.so │ ├── _DES.so │ ├── _XOR.so │ ├── ARC2.pyc │ ├── ARC4.pyc │ ├── CAST.pyc │ ├── DES3.pyc │ ├── _ARC2.so │ ├── _ARC4.so │ ├── _CAST.so │ ├── _DES3.so │ ├── Blowfish.pyc │ ├── _Blowfish.so │ ├── __init__.pyc │ ├── PKCS1_OAEP.pyc │ ├── PKCS1_v1_5.pyc │ ├── blockalgo.pyc │ ├── XOR.py │ └── __init__.py ├── Hash │ ├── HMAC.pyc │ ├── MD2.pyc │ ├── MD4.pyc │ ├── MD5.pyc │ ├── SHA.pyc │ ├── _MD2.so │ ├── _MD4.so │ ├── RIPEMD.pyc │ ├── SHA224.pyc │ ├── SHA256.pyc │ ├── SHA384.pyc │ ├── SHA512.pyc │ ├── _SHA224.so │ ├── _SHA256.so │ ├── _SHA384.so │ ├── _SHA512.so │ ├── _RIPEMD160.so │ ├── __init__.pyc │ ├── hashalgo.pyc │ ├── __init__.py │ ├── MD4.py │ ├── MD2.py │ ├── SHA224.py │ ├── SHA512.py │ ├── SHA.py │ ├── SHA256.py │ ├── SHA384.py │ ├── MD5.py │ └── RIPEMD.py ├── Util │ ├── asn1.pyc │ ├── strxor.so │ ├── Counter.pyc │ ├── RFC1751.pyc │ ├── _counter.so │ ├── number.pyc │ ├── __init__.pyc │ ├── py21compat.pyc │ ├── py3compat.pyc │ ├── randpool.pyc │ ├── winrandom.pyc │ ├── _number_new.pyc │ ├── winrandom.py │ ├── __init__.py │ ├── randpool.py │ ├── py21compat.py │ └── py3compat.py ├── __init__.pyc ├── Protocol │ ├── KDF.pyc │ ├── Chaffing.pyc │ ├── __init__.pyc │ ├── AllOrNothing.pyc │ └── __init__.py ├── pct_warnings.pyc ├── PublicKey │ ├── DSA.pyc │ ├── RSA.pyc │ ├── _DSA.pyc │ ├── _RSA.pyc │ ├── ElGamal.pyc │ ├── pubkey.pyc │ ├── __init__.pyc │ ├── _slowmath.pyc │ ├── __init__.py │ ├── _RSA.py │ └── _DSA.py ├── SelfTest │ ├── __init__.pyc │ ├── Hash │ │ ├── common.pyc │ │ ├── __init__.pyc │ │ ├── test_MD2.pyc │ │ ├── test_MD4.pyc │ │ ├── test_MD5.pyc │ │ ├── test_SHA.pyc │ │ ├── test_HMAC.pyc │ │ ├── test_RIPEMD.pyc │ │ ├── test_SHA224.pyc │ │ ├── test_SHA256.pyc │ │ ├── test_SHA384.pyc │ │ ├── test_SHA512.pyc │ │ ├── test_SHA.py │ │ ├── test_MD2.py │ │ ├── test_MD4.py │ │ ├── test_MD5.py │ │ ├── __init__.py │ │ ├── test_SHA224.py │ │ ├── test_RIPEMD.py │ │ ├── test_SHA384.py │ │ ├── test_SHA512.py │ │ └── test_SHA256.py │ ├── st_common.pyc │ ├── Cipher │ │ ├── common.pyc │ │ ├── __init__.pyc │ │ ├── test_AES.pyc │ │ ├── test_ARC2.pyc │ │ ├── test_ARC4.pyc │ │ ├── test_CAST.pyc │ │ ├── test_DES.pyc │ │ ├── test_DES3.pyc │ │ ├── test_XOR.pyc │ │ ├── test_Blowfish.pyc │ │ ├── test_pkcs1_15.pyc │ │ ├── test_pkcs1_oaep.pyc │ │ ├── test_CAST.py │ │ ├── __init__.py │ │ ├── test_XOR.py │ │ └── test_ARC4.py │ ├── Util │ │ ├── __init__.pyc │ │ ├── test_asn1.pyc │ │ ├── test_number.pyc │ │ ├── test_Counter.pyc │ │ ├── test_winrandom.pyc │ │ ├── __init__.py │ │ └── test_winrandom.py │ ├── Random │ │ ├── __init__.pyc │ │ ├── test_random.pyc │ │ ├── OSRNG │ │ │ ├── __init__.pyc │ │ │ ├── test_nt.pyc │ │ │ ├── test_posix.pyc │ │ │ ├── test_fallback.pyc │ │ │ ├── test_generic.pyc │ │ │ ├── test_winrandom.pyc │ │ │ ├── test_generic.py │ │ │ ├── test_nt.py │ │ │ ├── test_posix.py │ │ │ ├── test_winrandom.py │ │ │ ├── test_fallback.py │ │ │ └── __init__.py │ │ ├── Fortuna │ │ │ ├── __init__.pyc │ │ │ ├── test_SHAd256.pyc │ │ │ ├── test_FortunaGenerator.pyc │ │ │ ├── test_FortunaAccumulator.pyc │ │ │ ├── __init__.py │ │ │ ├── test_SHAd256.py │ │ │ └── test_FortunaGenerator.py │ │ ├── test_rpoolcompat.pyc │ │ ├── test__UserFriendlyRNG.pyc │ │ ├── __init__.py │ │ └── test_rpoolcompat.py │ ├── Protocol │ │ ├── __init__.pyc │ │ ├── test_KDF.pyc │ │ ├── test_rfc1751.pyc │ │ ├── test_chaffing.pyc │ │ ├── test_AllOrNothing.pyc │ │ ├── __init__.py │ │ ├── test_rfc1751.py │ │ ├── test_chaffing.py │ │ ├── test_AllOrNothing.py │ │ └── test_KDF.py │ ├── PublicKey │ │ ├── __init__.pyc │ │ ├── test_DSA.pyc │ │ ├── test_RSA.pyc │ │ ├── test_ElGamal.pyc │ │ ├── test_importKey.pyc │ │ └── __init__.py │ ├── Signature │ │ ├── __init__.pyc │ │ ├── test_pkcs1_15.pyc │ │ ├── test_pkcs1_pss.pyc │ │ └── __init__.py │ ├── st_common.py │ └── __init__.py ├── Signature │ ├── PKCS1_PSS.pyc │ ├── PKCS1_v1_5.pyc │ ├── __init__.pyc │ └── __init__.py ├── __init__.py └── pct_warnings.py ├── pycrypto-2.6.1.dist-info ├── top_level.txt ├── DESCRIPTION.rst ├── WHEEL ├── metadata.json └── METADATA └── README.md /Crypto/Random/Fortuna/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /pycrypto-2.6.1.dist-info/top_level.txt: -------------------------------------------------------------------------------- 1 | Crypto 2 | -------------------------------------------------------------------------------- /pycrypto-2.6.1.dist-info/DESCRIPTION.rst: -------------------------------------------------------------------------------- 1 | UNKNOWN 2 | 3 | 4 | -------------------------------------------------------------------------------- /Crypto/Cipher/AES.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/AES.pyc -------------------------------------------------------------------------------- /Crypto/Cipher/DES.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/DES.pyc -------------------------------------------------------------------------------- /Crypto/Cipher/XOR.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/XOR.pyc -------------------------------------------------------------------------------- /Crypto/Cipher/_AES.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/_AES.so -------------------------------------------------------------------------------- /Crypto/Cipher/_DES.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/_DES.so -------------------------------------------------------------------------------- /Crypto/Cipher/_XOR.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/_XOR.so -------------------------------------------------------------------------------- /Crypto/Hash/HMAC.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/HMAC.pyc -------------------------------------------------------------------------------- /Crypto/Hash/MD2.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/MD2.pyc -------------------------------------------------------------------------------- /Crypto/Hash/MD4.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/MD4.pyc -------------------------------------------------------------------------------- /Crypto/Hash/MD5.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/MD5.pyc -------------------------------------------------------------------------------- /Crypto/Hash/SHA.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/SHA.pyc -------------------------------------------------------------------------------- /Crypto/Hash/_MD2.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/_MD2.so -------------------------------------------------------------------------------- /Crypto/Hash/_MD4.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/_MD4.so -------------------------------------------------------------------------------- /Crypto/Util/asn1.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Util/asn1.pyc -------------------------------------------------------------------------------- /Crypto/Util/strxor.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Util/strxor.so -------------------------------------------------------------------------------- /Crypto/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/__init__.pyc -------------------------------------------------------------------------------- /Crypto/Cipher/ARC2.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/ARC2.pyc -------------------------------------------------------------------------------- /Crypto/Cipher/ARC4.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/ARC4.pyc -------------------------------------------------------------------------------- /Crypto/Cipher/CAST.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/CAST.pyc -------------------------------------------------------------------------------- /Crypto/Cipher/DES3.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/DES3.pyc -------------------------------------------------------------------------------- /Crypto/Cipher/_ARC2.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/_ARC2.so -------------------------------------------------------------------------------- /Crypto/Cipher/_ARC4.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/_ARC4.so -------------------------------------------------------------------------------- /Crypto/Cipher/_CAST.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/_CAST.so -------------------------------------------------------------------------------- /Crypto/Cipher/_DES3.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/_DES3.so -------------------------------------------------------------------------------- /Crypto/Hash/RIPEMD.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/RIPEMD.pyc -------------------------------------------------------------------------------- /Crypto/Hash/SHA224.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/SHA224.pyc -------------------------------------------------------------------------------- /Crypto/Hash/SHA256.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/SHA256.pyc -------------------------------------------------------------------------------- /Crypto/Hash/SHA384.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/SHA384.pyc -------------------------------------------------------------------------------- /Crypto/Hash/SHA512.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/SHA512.pyc -------------------------------------------------------------------------------- /Crypto/Hash/_SHA224.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/_SHA224.so -------------------------------------------------------------------------------- /Crypto/Hash/_SHA256.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/_SHA256.so -------------------------------------------------------------------------------- /Crypto/Hash/_SHA384.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/_SHA384.so -------------------------------------------------------------------------------- /Crypto/Hash/_SHA512.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/_SHA512.so -------------------------------------------------------------------------------- /Crypto/Protocol/KDF.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Protocol/KDF.pyc -------------------------------------------------------------------------------- /Crypto/Util/Counter.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Util/Counter.pyc -------------------------------------------------------------------------------- /Crypto/Util/RFC1751.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Util/RFC1751.pyc -------------------------------------------------------------------------------- /Crypto/Util/_counter.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Util/_counter.so -------------------------------------------------------------------------------- /Crypto/Util/number.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Util/number.pyc -------------------------------------------------------------------------------- /Crypto/pct_warnings.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/pct_warnings.pyc -------------------------------------------------------------------------------- /Crypto/Cipher/Blowfish.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/Blowfish.pyc -------------------------------------------------------------------------------- /Crypto/Cipher/_Blowfish.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/_Blowfish.so -------------------------------------------------------------------------------- /Crypto/Cipher/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/__init__.pyc -------------------------------------------------------------------------------- /Crypto/Hash/_RIPEMD160.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/_RIPEMD160.so -------------------------------------------------------------------------------- /Crypto/Hash/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/__init__.pyc -------------------------------------------------------------------------------- /Crypto/Hash/hashalgo.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Hash/hashalgo.pyc -------------------------------------------------------------------------------- /Crypto/PublicKey/DSA.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/PublicKey/DSA.pyc -------------------------------------------------------------------------------- /Crypto/PublicKey/RSA.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/PublicKey/RSA.pyc -------------------------------------------------------------------------------- /Crypto/PublicKey/_DSA.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/PublicKey/_DSA.pyc -------------------------------------------------------------------------------- /Crypto/PublicKey/_RSA.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/PublicKey/_RSA.pyc -------------------------------------------------------------------------------- /Crypto/Random/OSRNG/nt.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Random/OSRNG/nt.pyc -------------------------------------------------------------------------------- /Crypto/Random/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Random/__init__.pyc -------------------------------------------------------------------------------- /Crypto/Random/random.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Random/random.pyc -------------------------------------------------------------------------------- /Crypto/Util/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Util/__init__.pyc -------------------------------------------------------------------------------- /Crypto/Util/py21compat.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Util/py21compat.pyc -------------------------------------------------------------------------------- /Crypto/Util/py3compat.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Util/py3compat.pyc -------------------------------------------------------------------------------- /Crypto/Util/randpool.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Util/randpool.pyc -------------------------------------------------------------------------------- /Crypto/Util/winrandom.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Util/winrandom.pyc -------------------------------------------------------------------------------- /Crypto/Cipher/PKCS1_OAEP.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/PKCS1_OAEP.pyc -------------------------------------------------------------------------------- /Crypto/Cipher/PKCS1_v1_5.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/PKCS1_v1_5.pyc -------------------------------------------------------------------------------- /Crypto/Cipher/blockalgo.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Cipher/blockalgo.pyc -------------------------------------------------------------------------------- /Crypto/Protocol/Chaffing.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Protocol/Chaffing.pyc -------------------------------------------------------------------------------- /Crypto/Protocol/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Protocol/__init__.pyc -------------------------------------------------------------------------------- /Crypto/PublicKey/ElGamal.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/PublicKey/ElGamal.pyc -------------------------------------------------------------------------------- /Crypto/PublicKey/pubkey.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/PublicKey/pubkey.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/__init__.pyc -------------------------------------------------------------------------------- /Crypto/Util/_number_new.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Util/_number_new.pyc -------------------------------------------------------------------------------- /Crypto/PublicKey/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/PublicKey/__init__.pyc -------------------------------------------------------------------------------- /Crypto/PublicKey/_slowmath.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/PublicKey/_slowmath.pyc -------------------------------------------------------------------------------- /Crypto/Random/OSRNG/posix.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Random/OSRNG/posix.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/common.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Hash/common.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/st_common.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/st_common.pyc -------------------------------------------------------------------------------- /Crypto/Signature/PKCS1_PSS.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Signature/PKCS1_PSS.pyc -------------------------------------------------------------------------------- /Crypto/Signature/PKCS1_v1_5.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Signature/PKCS1_v1_5.pyc -------------------------------------------------------------------------------- /Crypto/Signature/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Signature/__init__.pyc -------------------------------------------------------------------------------- /Crypto/Protocol/AllOrNothing.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Protocol/AllOrNothing.pyc -------------------------------------------------------------------------------- /Crypto/Random/Fortuna/SHAd256.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Random/Fortuna/SHAd256.pyc -------------------------------------------------------------------------------- /Crypto/Random/OSRNG/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Random/OSRNG/__init__.pyc -------------------------------------------------------------------------------- /Crypto/Random/OSRNG/fallback.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Random/OSRNG/fallback.pyc -------------------------------------------------------------------------------- /Crypto/Random/OSRNG/rng_base.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Random/OSRNG/rng_base.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Cipher/common.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Cipher/common.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Hash/__init__.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_MD2.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Hash/test_MD2.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_MD4.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Hash/test_MD4.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_MD5.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Hash/test_MD5.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_SHA.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Hash/test_SHA.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Util/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Util/__init__.pyc -------------------------------------------------------------------------------- /Crypto/Random/Fortuna/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Random/Fortuna/__init__.pyc -------------------------------------------------------------------------------- /Crypto/Random/_UserFriendlyRNG.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Random/_UserFriendlyRNG.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Cipher/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Cipher/__init__.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Cipher/test_AES.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Cipher/test_AES.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Cipher/test_ARC2.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Cipher/test_ARC2.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Cipher/test_ARC4.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Cipher/test_ARC4.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Cipher/test_CAST.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Cipher/test_CAST.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Cipher/test_DES.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Cipher/test_DES.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Cipher/test_DES3.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Cipher/test_DES3.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Cipher/test_XOR.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Cipher/test_XOR.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_HMAC.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Hash/test_HMAC.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_RIPEMD.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Hash/test_RIPEMD.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_SHA224.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Hash/test_SHA224.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_SHA256.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Hash/test_SHA256.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_SHA384.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Hash/test_SHA384.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_SHA512.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Hash/test_SHA512.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Random/__init__.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Util/test_asn1.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Util/test_asn1.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Util/test_number.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Util/test_number.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Protocol/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Protocol/__init__.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Protocol/test_KDF.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Protocol/test_KDF.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/PublicKey/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/PublicKey/__init__.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/PublicKey/test_DSA.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/PublicKey/test_DSA.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/PublicKey/test_RSA.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/PublicKey/test_RSA.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/test_random.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Random/test_random.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Signature/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Signature/__init__.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Util/test_Counter.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Util/test_Counter.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Cipher/test_Blowfish.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Cipher/test_Blowfish.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Cipher/test_pkcs1_15.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Cipher/test_pkcs1_15.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Protocol/test_rfc1751.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Protocol/test_rfc1751.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/OSRNG/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Random/OSRNG/__init__.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/OSRNG/test_nt.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Random/OSRNG/test_nt.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Util/test_winrandom.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Util/test_winrandom.pyc -------------------------------------------------------------------------------- /Crypto/Random/Fortuna/FortunaGenerator.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Random/Fortuna/FortunaGenerator.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Cipher/test_pkcs1_oaep.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Cipher/test_pkcs1_oaep.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Protocol/test_chaffing.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Protocol/test_chaffing.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/PublicKey/test_ElGamal.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/PublicKey/test_ElGamal.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/Fortuna/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Random/Fortuna/__init__.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/OSRNG/test_posix.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Random/OSRNG/test_posix.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/test_rpoolcompat.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Random/test_rpoolcompat.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Signature/test_pkcs1_15.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Signature/test_pkcs1_15.pyc -------------------------------------------------------------------------------- /Crypto/Random/Fortuna/FortunaAccumulator.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/Random/Fortuna/FortunaAccumulator.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Protocol/test_AllOrNothing.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Protocol/test_AllOrNothing.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/PublicKey/test_importKey.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/PublicKey/test_importKey.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/OSRNG/test_fallback.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Random/OSRNG/test_fallback.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/OSRNG/test_generic.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Random/OSRNG/test_generic.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Signature/test_pkcs1_pss.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Signature/test_pkcs1_pss.pyc -------------------------------------------------------------------------------- /pycrypto-2.6.1.dist-info/WHEEL: -------------------------------------------------------------------------------- 1 | Wheel-Version: 1.0 2 | Generator: bdist_wheel (0.26.0) 3 | Root-Is-Purelib: false 4 | Tag: cp27-none-linux_x86_64 5 | 6 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/Fortuna/test_SHAd256.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Random/Fortuna/test_SHAd256.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/OSRNG/test_winrandom.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Random/OSRNG/test_winrandom.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/test__UserFriendlyRNG.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Random/test__UserFriendlyRNG.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/Fortuna/test_FortunaGenerator.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Random/Fortuna/test_FortunaGenerator.pyc -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/Fortuna/test_FortunaAccumulator.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Doerge/awslambda-pycrypto/HEAD/Crypto/SelfTest/Random/Fortuna/test_FortunaAccumulator.pyc -------------------------------------------------------------------------------- /pycrypto-2.6.1.dist-info/metadata.json: -------------------------------------------------------------------------------- 1 | {"generator": "bdist_wheel (0.26.0)", "summary": "Cryptographic modules for Python.", "classifiers": ["Development Status :: 5 - Production/Stable", "License :: Public Domain", "Intended Audience :: Developers", "Operating System :: Unix", "Operating System :: Microsoft :: Windows", "Operating System :: MacOS :: MacOS X", "Topic :: Security :: Cryptography", "Programming Language :: Python :: 2", "Programming Language :: Python :: 3"], "extensions": {"python.details": {"project_urls": {"Home": "http://www.pycrypto.org/"}, "contacts": [{"email": "dlitz@dlitz.net", "name": "Dwayne C. Litzenberger", "role": "author"}], "document_names": {"description": "DESCRIPTION.rst"}}}, "metadata_version": "2.0", "name": "pycrypto", "version": "2.6.1"} -------------------------------------------------------------------------------- /pycrypto-2.6.1.dist-info/METADATA: -------------------------------------------------------------------------------- 1 | Metadata-Version: 2.0 2 | Name: pycrypto 3 | Version: 2.6.1 4 | Summary: Cryptographic modules for Python. 5 | Home-page: http://www.pycrypto.org/ 6 | Author: Dwayne C. Litzenberger 7 | Author-email: dlitz@dlitz.net 8 | License: UNKNOWN 9 | Platform: UNKNOWN 10 | Classifier: Development Status :: 5 - Production/Stable 11 | Classifier: License :: Public Domain 12 | Classifier: Intended Audience :: Developers 13 | Classifier: Operating System :: Unix 14 | Classifier: Operating System :: Microsoft :: Windows 15 | Classifier: Operating System :: MacOS :: MacOS X 16 | Classifier: Topic :: Security :: Cryptography 17 | Classifier: Programming Language :: Python :: 2 18 | Classifier: Programming Language :: Python :: 3 19 | 20 | UNKNOWN 21 | 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # awslambda-pycrypto 2 | [Python Cryptography Toolkit (pycrypto)](https://github.com/dlitz/pycrypto) compiled and packaged for AWS Lambda. Because that is a pain to do. 3 | 4 | # Setup 5 | Simply download and include the Crypto folder in your project, zip and upload it to Lambda. 6 | 7 | # Lambda Example: 8 | Directory structure: 9 | ``` 10 | * myproject 11 | - Crypto/ 12 | - lambda_handler.py 13 | ``` 14 | `lambda_handler.py`: 15 | ```python 16 | from __future__ import print_function 17 | 18 | from Crypto.Hash import SHA256 19 | 20 | 21 | def lambda_handler(event, context): 22 | hash = SHA256.new() 23 | hash.update('message') 24 | digest = hash.digest() 25 | print(len(digest)) 26 | print(repr(digest)) 27 | return 28 | ``` 29 | 30 | Log output: 31 | ``` 32 | START RequestId: 17e0f272-9b91-11e6-bf2d-f756ae83795f Version: $LATEST 33 | 32 34 | '\xabS\n\x13\xe4Y\x14\x98+y\xf9\xb7\xe3\xfb\xa9\x94\xcf\xd1\xf3\xfb"\xf7\x1c\xea\x1a\xfb\xf0+F\x0cm\x1d' 35 | END RequestId: 17e0f272-9b91-11e6-bf2d-f756ae83795f 36 | REPORT RequestId: 17e0f272-9b91-11e6-bf2d-f756ae83795f Duration: 0.28 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 14 MB 37 | ``` 38 | # Disclaimer 39 | Use at your own risk. I compiled this for usage in a personal project where it worked fine. 40 | -------------------------------------------------------------------------------- /Crypto/Util/winrandom.py: -------------------------------------------------------------------------------- 1 | # 2 | # Util/winrandom.py : Stub for Crypto.Random.OSRNG.winrandom 3 | # 4 | # Written in 2008 by Dwayne C. Litzenberger 5 | # 6 | # =================================================================== 7 | # The contents of this file are dedicated to the public domain. To 8 | # the extent that dedication to the public domain is not available, 9 | # everyone is granted a worldwide, perpetual, royalty-free, 10 | # non-exclusive license to exercise all rights associated with the 11 | # contents of this file for any purpose whatsoever. 12 | # No rights are reserved. 13 | # 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 18 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 19 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | # =================================================================== 23 | 24 | __revision__ = "$Id$" 25 | 26 | from Crypto.Random.OSRNG.winrandom import * 27 | 28 | # vim:set ts=4 sw=4 sts=4 expandtab: 29 | -------------------------------------------------------------------------------- /Crypto/Signature/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # =================================================================== 4 | # The contents of this file are dedicated to the public domain. To 5 | # the extent that dedication to the public domain is not available, 6 | # everyone is granted a worldwide, perpetual, royalty-free, 7 | # non-exclusive license to exercise all rights associated with the 8 | # contents of this file for any purpose whatsoever. 9 | # No rights are reserved. 10 | # 11 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 12 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 13 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 15 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 17 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | # SOFTWARE. 19 | # =================================================================== 20 | 21 | """Digital signature protocols 22 | 23 | A collection of standardized protocols to carry out digital signatures. 24 | 25 | :undocumented: __revision__, __package__ 26 | """ 27 | 28 | __all__ = [ 'PKCS1_v1_5', 'PKCS1_PSS' ] 29 | __revision__ = "$Id$" 30 | 31 | 32 | -------------------------------------------------------------------------------- /Crypto/Util/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # =================================================================== 4 | # The contents of this file are dedicated to the public domain. To 5 | # the extent that dedication to the public domain is not available, 6 | # everyone is granted a worldwide, perpetual, royalty-free, 7 | # non-exclusive license to exercise all rights associated with the 8 | # contents of this file for any purpose whatsoever. 9 | # No rights are reserved. 10 | # 11 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 12 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 13 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 15 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 17 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | # SOFTWARE. 19 | # =================================================================== 20 | 21 | """Miscellaneous modules 22 | 23 | Contains useful modules that don't belong into any of the 24 | other Crypto.* subpackages. 25 | 26 | Crypto.Util.number Number-theoretic functions (primality testing, etc.) 27 | Crypto.Util.randpool Random number generation 28 | Crypto.Util.RFC1751 Converts between 128-bit keys and human-readable 29 | strings of words. 30 | Crypto.Util.asn1 Minimal support for ASN.1 DER encoding 31 | 32 | """ 33 | 34 | __all__ = ['randpool', 'RFC1751', 'number', 'strxor', 'asn1' ] 35 | 36 | __revision__ = "$Id$" 37 | 38 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Signature/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Signature/__init__.py: Self-test for signature modules 4 | # 5 | # =================================================================== 6 | # The contents of this file are dedicated to the public domain. To 7 | # the extent that dedication to the public domain is not available, 8 | # everyone is granted a worldwide, perpetual, royalty-free, 9 | # non-exclusive license to exercise all rights associated with the 10 | # contents of this file for any purpose whatsoever. 11 | # No rights are reserved. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 14 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 15 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 16 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 17 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 18 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | # SOFTWARE. 21 | # =================================================================== 22 | 23 | """Self-test for signature modules""" 24 | 25 | __revision__ = "$Id$" 26 | 27 | import os 28 | 29 | def get_tests(config={}): 30 | tests = [] 31 | import test_pkcs1_15; tests += test_pkcs1_15.get_tests(config=config) 32 | import test_pkcs1_pss; tests += test_pkcs1_pss.get_tests(config=config) 33 | return tests 34 | 35 | if __name__ == '__main__': 36 | import unittest 37 | suite = lambda: unittest.TestSuite(get_tests()) 38 | unittest.main(defaultTest='suite') 39 | 40 | # vim:set ts=4 sw=4 sts=4 expandtab: 41 | -------------------------------------------------------------------------------- /Crypto/Random/OSRNG/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | # Random/OSRNG/__init__.py : Platform-independent OS RNG API 3 | # 4 | # Written in 2008 by Dwayne C. Litzenberger 5 | # 6 | # =================================================================== 7 | # The contents of this file are dedicated to the public domain. To 8 | # the extent that dedication to the public domain is not available, 9 | # everyone is granted a worldwide, perpetual, royalty-free, 10 | # non-exclusive license to exercise all rights associated with the 11 | # contents of this file for any purpose whatsoever. 12 | # No rights are reserved. 13 | # 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 18 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 19 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | # =================================================================== 23 | 24 | """Provides a platform-independent interface to the random number generators 25 | supplied by various operating systems.""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | import os 30 | 31 | if os.name == 'posix': 32 | from Crypto.Random.OSRNG.posix import new 33 | elif os.name == 'nt': 34 | from Crypto.Random.OSRNG.nt import new 35 | elif hasattr(os, 'urandom'): 36 | from Crypto.Random.OSRNG.fallback import new 37 | else: 38 | raise ImportError("Not implemented") 39 | 40 | # vim:set ts=4 sw=4 sts=4 expandtab: 41 | -------------------------------------------------------------------------------- /Crypto/Random/OSRNG/fallback.py: -------------------------------------------------------------------------------- 1 | # 2 | # Random/OSRNG/fallback.py : Fallback entropy source for systems with os.urandom 3 | # 4 | # Written in 2008 by Dwayne C. Litzenberger 5 | # 6 | # =================================================================== 7 | # The contents of this file are dedicated to the public domain. To 8 | # the extent that dedication to the public domain is not available, 9 | # everyone is granted a worldwide, perpetual, royalty-free, 10 | # non-exclusive license to exercise all rights associated with the 11 | # contents of this file for any purpose whatsoever. 12 | # No rights are reserved. 13 | # 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 18 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 19 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | # =================================================================== 23 | 24 | 25 | __revision__ = "$Id$" 26 | __all__ = ['PythonOSURandomRNG'] 27 | 28 | import os 29 | 30 | from rng_base import BaseRNG 31 | 32 | class PythonOSURandomRNG(BaseRNG): 33 | 34 | name = "" 35 | 36 | def __init__(self): 37 | self._read = os.urandom 38 | BaseRNG.__init__(self) 39 | 40 | def _close(self): 41 | self._read = None 42 | 43 | def new(*args, **kwargs): 44 | return PythonOSURandomRNG(*args, **kwargs) 45 | 46 | # vim:set ts=4 sw=4 sts=4 expandtab: 47 | -------------------------------------------------------------------------------- /Crypto/Protocol/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # =================================================================== 4 | # The contents of this file are dedicated to the public domain. To 5 | # the extent that dedication to the public domain is not available, 6 | # everyone is granted a worldwide, perpetual, royalty-free, 7 | # non-exclusive license to exercise all rights associated with the 8 | # contents of this file for any purpose whatsoever. 9 | # No rights are reserved. 10 | # 11 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 12 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 13 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 15 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 17 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | # SOFTWARE. 19 | # =================================================================== 20 | 21 | """Cryptographic protocols 22 | 23 | Implements various cryptographic protocols. (Don't expect to find 24 | network protocols here.) 25 | 26 | Crypto.Protocol.AllOrNothing 27 | Transforms a message into a set of message blocks, such that the blocks 28 | can be recombined to get the message back. 29 | 30 | Crypto.Protocol.Chaffing 31 | Takes a set of authenticated message blocks (the wheat) and adds a number 32 | of randomly generated blocks (the chaff). 33 | 34 | Crypto.Protocol.KDF 35 | A collection of standard key derivation functions. 36 | 37 | :undocumented: __revision__ 38 | """ 39 | 40 | __all__ = ['AllOrNothing', 'Chaffing', 'KDF'] 41 | __revision__ = "$Id$" 42 | -------------------------------------------------------------------------------- /Crypto/Random/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Random/__init__.py : PyCrypto random number generation 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | __revision__ = "$Id$" 26 | __all__ = ['new'] 27 | 28 | from Crypto.Random import OSRNG 29 | from Crypto.Random import _UserFriendlyRNG 30 | 31 | def new(*args, **kwargs): 32 | """Return a file-like object that outputs cryptographically random bytes.""" 33 | return _UserFriendlyRNG.new(*args, **kwargs) 34 | 35 | def atfork(): 36 | """Call this whenever you call os.fork()""" 37 | _UserFriendlyRNG.reinit() 38 | 39 | def get_random_bytes(n): 40 | """Return the specified number of cryptographically-strong random bytes.""" 41 | return _UserFriendlyRNG.get_random_bytes(n) 42 | 43 | # vim:set ts=4 sw=4 sts=4 expandtab: 44 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Protocol/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Protocol/__init__.py: Self-tests for Crypto.Protocol 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test for Crypto.Protocol""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | def get_tests(config={}): 30 | tests = [] 31 | from Crypto.SelfTest.Protocol import test_chaffing; tests += test_chaffing.get_tests(config=config) 32 | from Crypto.SelfTest.Protocol import test_rfc1751; tests += test_rfc1751.get_tests(config=config) 33 | from Crypto.SelfTest.Protocol import test_AllOrNothing; tests += test_AllOrNothing.get_tests(config=config) 34 | return tests 35 | 36 | if __name__ == '__main__': 37 | import unittest 38 | suite = lambda: unittest.TestSuite(get_tests()) 39 | unittest.main(defaultTest='suite') 40 | 41 | # vim:set ts=4 sw=4 sts=4 expandtab: 42 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Util/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/__init__.py: Self-test for utility modules 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test for utility modules""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | import os 30 | 31 | def get_tests(config={}): 32 | tests = [] 33 | if os.name == 'nt': 34 | from Crypto.SelfTest.Util import test_winrandom; tests += test_winrandom.get_tests(config=config) 35 | from Crypto.SelfTest.Util import test_number; tests += test_number.get_tests(config=config) 36 | from Crypto.SelfTest.Util import test_Counter; tests += test_Counter.get_tests(config=config) 37 | return tests 38 | 39 | if __name__ == '__main__': 40 | import unittest 41 | suite = lambda: unittest.TestSuite(get_tests()) 42 | unittest.main(defaultTest='suite') 43 | 44 | # vim:set ts=4 sw=4 sts=4 expandtab: 45 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/OSRNG/test_generic.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/test_generic.py: Self-test for the OSRNG.new() function 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Random.OSRNG""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | import unittest 30 | 31 | class SimpleTest(unittest.TestCase): 32 | def runTest(self): 33 | """Crypto.Random.OSRNG.new()""" 34 | # Import the OSRNG module and try to use it 35 | import Crypto.Random.OSRNG 36 | randobj = Crypto.Random.OSRNG.new() 37 | x = randobj.read(16) 38 | y = randobj.read(16) 39 | self.assertNotEqual(x, y) 40 | 41 | def get_tests(config={}): 42 | return [SimpleTest()] 43 | 44 | if __name__ == '__main__': 45 | suite = lambda: unittest.TestSuite(get_tests()) 46 | unittest.main(defaultTest='suite') 47 | 48 | # vim:set ts=4 sw=4 sts=4 expandtab: 49 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/OSRNG/test_nt.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/test_generic.py: Self-test for the OSRNG.nt.new() function 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Random.OSRNG.nt""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | import unittest 30 | 31 | class SimpleTest(unittest.TestCase): 32 | def runTest(self): 33 | """Crypto.Random.OSRNG.nt.new()""" 34 | # Import the OSRNG.nt module and try to use it 35 | import Crypto.Random.OSRNG.nt 36 | randobj = Crypto.Random.OSRNG.nt.new() 37 | x = randobj.read(16) 38 | y = randobj.read(16) 39 | self.assertNotEqual(x, y) 40 | 41 | def get_tests(config={}): 42 | return [SimpleTest()] 43 | 44 | if __name__ == '__main__': 45 | suite = lambda: unittest.TestSuite(get_tests()) 46 | unittest.main(defaultTest='suite') 47 | 48 | # vim:set ts=4 sw=4 sts=4 expandtab: 49 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Util/test_winrandom.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/test_winrandom.py: Self-test for the winrandom module 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Util.winrandom""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | import unittest 30 | 31 | class WinRandomImportTest(unittest.TestCase): 32 | def runTest(self): 33 | """winrandom: simple test""" 34 | # Import the winrandom module and try to use it 35 | from Crypto.Util import winrandom 36 | randobj = winrandom.new() 37 | x = randobj.get_bytes(16) 38 | y = randobj.get_bytes(16) 39 | self.assertNotEqual(x, y) 40 | 41 | def get_tests(config={}): 42 | return [WinRandomImportTest()] 43 | 44 | if __name__ == '__main__': 45 | suite = lambda: unittest.TestSuite(get_tests()) 46 | unittest.main(defaultTest='suite') 47 | 48 | # vim:set ts=4 sw=4 sts=4 expandtab: 49 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/OSRNG/test_posix.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/test_posix.py: Self-test for the OSRNG.posix.new() function 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Random.OSRNG.posix""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | import unittest 30 | 31 | class SimpleTest(unittest.TestCase): 32 | def runTest(self): 33 | """Crypto.Random.OSRNG.posix.new()""" 34 | # Import the OSRNG.nt module and try to use it 35 | import Crypto.Random.OSRNG.posix 36 | randobj = Crypto.Random.OSRNG.posix.new() 37 | x = randobj.read(16) 38 | y = randobj.read(16) 39 | self.assertNotEqual(x, y) 40 | 41 | def get_tests(config={}): 42 | return [SimpleTest()] 43 | 44 | if __name__ == '__main__': 45 | suite = lambda: unittest.TestSuite(get_tests()) 46 | unittest.main(defaultTest='suite') 47 | 48 | # vim:set ts=4 sw=4 sts=4 expandtab: 49 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/OSRNG/test_winrandom.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/test_winrandom.py: Self-test for the winrandom module 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Random.OSRNG.winrandom""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | import unittest 30 | 31 | class SimpleTest(unittest.TestCase): 32 | def runTest(self): 33 | """Crypto.Random.OSRNG.winrandom""" 34 | # Import the winrandom module and try to use it 35 | from Crypto.Random.OSRNG import winrandom 36 | randobj = winrandom.new() 37 | x = randobj.get_bytes(16) 38 | y = randobj.get_bytes(16) 39 | self.assertNotEqual(x, y) 40 | 41 | def get_tests(config={}): 42 | return [SimpleTest()] 43 | 44 | if __name__ == '__main__': 45 | suite = lambda: unittest.TestSuite(get_tests()) 46 | unittest.main(defaultTest='suite') 47 | 48 | # vim:set ts=4 sw=4 sts=4 expandtab: 49 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/OSRNG/test_fallback.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/test_fallback.py: Self-test for the OSRNG.fallback.new() function 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Random.OSRNG.fallback""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | import unittest 30 | 31 | class SimpleTest(unittest.TestCase): 32 | def runTest(self): 33 | """Crypto.Random.OSRNG.fallback.new()""" 34 | # Import the OSRNG.nt module and try to use it 35 | import Crypto.Random.OSRNG.fallback 36 | randobj = Crypto.Random.OSRNG.fallback.new() 37 | x = randobj.read(16) 38 | y = randobj.read(16) 39 | self.assertNotEqual(x, y) 40 | 41 | def get_tests(config={}): 42 | return [SimpleTest()] 43 | 44 | if __name__ == '__main__': 45 | suite = lambda: unittest.TestSuite(get_tests()) 46 | unittest.main(defaultTest='suite') 47 | 48 | # vim:set ts=4 sw=4 sts=4 expandtab: 49 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/Fortuna/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Random/Fortuna/__init__.py: Self-test for Fortuna modules 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test for the Crypto.Random.Fortuna package""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | import os 30 | 31 | def get_tests(config={}): 32 | tests = [] 33 | from Crypto.SelfTest.Random.Fortuna import test_FortunaAccumulator; tests += test_FortunaAccumulator.get_tests(config=config) 34 | from Crypto.SelfTest.Random.Fortuna import test_FortunaGenerator; tests += test_FortunaGenerator.get_tests(config=config) 35 | from Crypto.SelfTest.Random.Fortuna import test_SHAd256; tests += test_SHAd256.get_tests(config=config) 36 | return tests 37 | 38 | if __name__ == '__main__': 39 | import unittest 40 | suite = lambda: unittest.TestSuite(get_tests()) 41 | unittest.main(defaultTest='suite') 42 | 43 | 44 | # vim:set ts=4 sw=4 sts=4 expandtab: 45 | -------------------------------------------------------------------------------- /Crypto/SelfTest/PublicKey/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/PublicKey/__init__.py: Self-test for public key crypto 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test for public-key crypto""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | import os 30 | 31 | def get_tests(config={}): 32 | tests = [] 33 | from Crypto.SelfTest.PublicKey import test_DSA; tests += test_DSA.get_tests(config=config) 34 | from Crypto.SelfTest.PublicKey import test_RSA; tests += test_RSA.get_tests(config=config) 35 | from Crypto.SelfTest.PublicKey import test_importKey; tests += test_importKey.get_tests(config=config) 36 | from Crypto.SelfTest.PublicKey import test_ElGamal; tests += test_ElGamal.get_tests(config=config) 37 | return tests 38 | 39 | if __name__ == '__main__': 40 | import unittest 41 | suite = lambda: unittest.TestSuite(get_tests()) 42 | unittest.main(defaultTest='suite') 43 | 44 | # vim:set ts=4 sw=4 sts=4 expandtab: 45 | -------------------------------------------------------------------------------- /Crypto/PublicKey/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # =================================================================== 4 | # The contents of this file are dedicated to the public domain. To 5 | # the extent that dedication to the public domain is not available, 6 | # everyone is granted a worldwide, perpetual, royalty-free, 7 | # non-exclusive license to exercise all rights associated with the 8 | # contents of this file for any purpose whatsoever. 9 | # No rights are reserved. 10 | # 11 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 12 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 13 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 15 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 17 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | # SOFTWARE. 19 | # =================================================================== 20 | 21 | """Public-key encryption and signature algorithms. 22 | 23 | Public-key encryption uses two different keys, one for encryption and 24 | one for decryption. The encryption key can be made public, and the 25 | decryption key is kept private. Many public-key algorithms can also 26 | be used to sign messages, and some can *only* be used for signatures. 27 | 28 | ======================== ============================================= 29 | Module Description 30 | ======================== ============================================= 31 | Crypto.PublicKey.DSA Digital Signature Algorithm (Signature only) 32 | Crypto.PublicKey.ElGamal (Signing and encryption) 33 | Crypto.PublicKey.RSA (Signing, encryption, and blinding) 34 | ======================== ============================================= 35 | 36 | :undocumented: _DSA, _RSA, _fastmath, _slowmath, pubkey 37 | """ 38 | 39 | __all__ = ['RSA', 'DSA', 'ElGamal'] 40 | __revision__ = "$Id$" 41 | 42 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Random/__init__.py: Self-test for random number generation modules 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test for random number generators""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | def get_tests(config={}): 30 | tests = [] 31 | from Crypto.SelfTest.Random import Fortuna; tests += Fortuna.get_tests(config=config) 32 | from Crypto.SelfTest.Random import OSRNG; tests += OSRNG.get_tests(config=config) 33 | from Crypto.SelfTest.Random import test_random; tests += test_random.get_tests(config=config) 34 | from Crypto.SelfTest.Random import test_rpoolcompat; tests += test_rpoolcompat.get_tests(config=config) 35 | from Crypto.SelfTest.Random import test__UserFriendlyRNG; tests += test__UserFriendlyRNG.get_tests(config=config) 36 | return tests 37 | 38 | if __name__ == '__main__': 39 | import unittest 40 | suite = lambda: unittest.TestSuite(get_tests()) 41 | unittest.main(defaultTest='suite') 42 | 43 | # vim:set ts=4 sw=4 sts=4 expandtab: 44 | -------------------------------------------------------------------------------- /Crypto/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # =================================================================== 4 | # The contents of this file are dedicated to the public domain. To 5 | # the extent that dedication to the public domain is not available, 6 | # everyone is granted a worldwide, perpetual, royalty-free, 7 | # non-exclusive license to exercise all rights associated with the 8 | # contents of this file for any purpose whatsoever. 9 | # No rights are reserved. 10 | # 11 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 12 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 13 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 15 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 17 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | # SOFTWARE. 19 | # =================================================================== 20 | 21 | """Python Cryptography Toolkit 22 | 23 | A collection of cryptographic modules implementing various algorithms 24 | and protocols. 25 | 26 | Subpackages: 27 | 28 | Crypto.Cipher 29 | Secret-key (AES, DES, ARC4) and public-key encryption (RSA PKCS#1) algorithms 30 | Crypto.Hash 31 | Hashing algorithms (MD5, SHA, HMAC) 32 | Crypto.Protocol 33 | Cryptographic protocols (Chaffing, all-or-nothing transform, key derivation 34 | functions). This package does not contain any network protocols. 35 | Crypto.PublicKey 36 | Public-key encryption and signature algorithms (RSA, DSA) 37 | Crypto.Signature 38 | Public-key signature algorithms (RSA PKCS#1) 39 | Crypto.Util 40 | Various useful modules and functions (long-to-string conversion, random number 41 | generation, number theoretic functions) 42 | """ 43 | 44 | __all__ = ['Cipher', 'Hash', 'Protocol', 'PublicKey', 'Util', 'Signature'] 45 | 46 | __version__ = '2.6.1' # See also below and setup.py 47 | __revision__ = "$Id$" 48 | 49 | # New software should look at this instead of at __version__ above. 50 | version_info = (2, 6, 1, 'final', 0) # See also above and setup.py 51 | 52 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Cipher/test_CAST.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Cipher/CAST.py: Self-test for the CAST-128 (CAST5) cipher 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Cipher.CAST""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | from Crypto.Util.py3compat import * 30 | 31 | # This is a list of (plaintext, ciphertext, key) tuples. 32 | test_data = [ 33 | # Test vectors from RFC 2144, B.1 34 | ('0123456789abcdef', '238b4fe5847e44b2', 35 | '0123456712345678234567893456789a', 36 | '128-bit key'), 37 | 38 | ('0123456789abcdef', 'eb6a711a2c02271b', 39 | '01234567123456782345', 40 | '80-bit key'), 41 | 42 | ('0123456789abcdef', '7ac816d16e9b302e', 43 | '0123456712', 44 | '40-bit key'), 45 | ] 46 | 47 | def get_tests(config={}): 48 | from Crypto.Cipher import CAST 49 | from common import make_block_tests 50 | return make_block_tests(CAST, "CAST", test_data) 51 | 52 | if __name__ == '__main__': 53 | import unittest 54 | suite = lambda: unittest.TestSuite(get_tests()) 55 | unittest.main(defaultTest='suite') 56 | 57 | # vim:set ts=4 sw=4 sts=4 expandtab: 58 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/test_rpoolcompat.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/test_winrandom.py: Self-test for the winrandom module 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test for the Crypto.Util.randpool.RandomPool wrapper class""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | import sys 30 | import unittest 31 | 32 | class SimpleTest(unittest.TestCase): 33 | def runTest(self): 34 | """Crypto.Util.randpool.RandomPool""" 35 | # Import the winrandom module and try to use it 36 | from Crypto.Util.randpool import RandomPool 37 | sys.stderr.write("SelfTest: You can ignore the RandomPool_DeprecationWarning that follows.\n") 38 | rpool = RandomPool() 39 | x = rpool.get_bytes(16) 40 | y = rpool.get_bytes(16) 41 | self.assertNotEqual(x, y) 42 | self.assertNotEqual(rpool.entropy, 0) 43 | 44 | rpool.randomize() 45 | rpool.stir('foo') 46 | rpool.add_event('foo') 47 | 48 | def get_tests(config={}): 49 | return [SimpleTest()] 50 | 51 | if __name__ == '__main__': 52 | suite = lambda: unittest.TestSuite(get_tests()) 53 | unittest.main(defaultTest='suite') 54 | 55 | # vim:set ts=4 sw=4 sts=4 expandtab: 56 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/OSRNG/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Random/OSRNG/__init__.py: Self-test for OSRNG modules 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test for Crypto.Random.OSRNG package""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | import os 30 | 31 | def get_tests(config={}): 32 | tests = [] 33 | if os.name == 'nt': 34 | from Crypto.SelfTest.Random.OSRNG import test_nt; tests += test_nt.get_tests(config=config) 35 | from Crypto.SelfTest.Random.OSRNG import test_winrandom; tests += test_winrandom.get_tests(config=config) 36 | elif os.name == 'posix': 37 | from Crypto.SelfTest.Random.OSRNG import test_posix; tests += test_posix.get_tests(config=config) 38 | if hasattr(os, 'urandom'): 39 | from Crypto.SelfTest.Random.OSRNG import test_fallback; tests += test_fallback.get_tests(config=config) 40 | from Crypto.SelfTest.Random.OSRNG import test_generic; tests += test_generic.get_tests(config=config) 41 | return tests 42 | 43 | if __name__ == '__main__': 44 | import unittest 45 | suite = lambda: unittest.TestSuite(get_tests()) 46 | unittest.main(defaultTest='suite') 47 | 48 | 49 | # vim:set ts=4 sw=4 sts=4 expandtab: 50 | -------------------------------------------------------------------------------- /Crypto/SelfTest/st_common.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/st_common.py: Common functions for SelfTest modules 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Common functions for SelfTest modules""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | import unittest 30 | import binascii 31 | import sys 32 | if sys.version_info[0] == 2 and sys.version_info[1] == 1: 33 | from Crypto.Util.py21compat import * 34 | from Crypto.Util.py3compat import * 35 | 36 | class _list_testloader(unittest.TestLoader): 37 | suiteClass = list 38 | 39 | def list_test_cases(class_): 40 | """Return a list of TestCase instances given a TestCase class 41 | 42 | This is useful when you have defined test* methods on your TestCase class. 43 | """ 44 | return _list_testloader().loadTestsFromTestCase(class_) 45 | 46 | def strip_whitespace(s): 47 | """Remove whitespace from a text or byte string""" 48 | if isinstance(s,str): 49 | return b("".join(s.split())) 50 | else: 51 | return b("").join(s.split()) 52 | 53 | def a2b_hex(s): 54 | """Convert hexadecimal to binary, ignoring whitespace""" 55 | return binascii.a2b_hex(strip_whitespace(s)) 56 | 57 | def b2a_hex(s): 58 | """Convert binary to hexadecimal""" 59 | # For completeness 60 | return binascii.b2a_hex(s) 61 | 62 | # vim:set ts=4 sw=4 sts=4 expandtab: 63 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Protocol/test_rfc1751.py: -------------------------------------------------------------------------------- 1 | # 2 | # Test script for Crypto.Util.RFC1751. 3 | # 4 | # Part of the Python Cryptography Toolkit 5 | # 6 | # Written by Andrew Kuchling and others 7 | # 8 | # =================================================================== 9 | # The contents of this file are dedicated to the public domain. To 10 | # the extent that dedication to the public domain is not available, 11 | # everyone is granted a worldwide, perpetual, royalty-free, 12 | # non-exclusive license to exercise all rights associated with the 13 | # contents of this file for any purpose whatsoever. 14 | # No rights are reserved. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | # SOFTWARE. 24 | # =================================================================== 25 | 26 | __revision__ = "$Id$" 27 | 28 | import binascii 29 | import unittest 30 | from Crypto.Util import RFC1751 31 | from Crypto.Util.py3compat import * 32 | 33 | test_data = [('EB33F77EE73D4053', 'TIDE ITCH SLOW REIN RULE MOT'), 34 | ('CCAC2AED591056BE4F90FD441C534766', 35 | 'RASH BUSH MILK LOOK BAD BRIM AVID GAFF BAIT ROT POD LOVE'), 36 | ('EFF81F9BFBC65350920CDD7416DE8009', 37 | 'TROD MUTE TAIL WARM CHAR KONG HAAG CITY BORE O TEAL AWL') 38 | ] 39 | 40 | class RFC1751Test_k2e (unittest.TestCase): 41 | 42 | def runTest (self): 43 | "Check converting keys to English" 44 | for key, words in test_data: 45 | key=binascii.a2b_hex(b(key)) 46 | self.assertEqual(RFC1751.key_to_english(key), words) 47 | 48 | class RFC1751Test_e2k (unittest.TestCase): 49 | 50 | def runTest (self): 51 | "Check converting English strings to keys" 52 | for key, words in test_data: 53 | key=binascii.a2b_hex(b(key)) 54 | self.assertEqual(RFC1751.english_to_key(words), key) 55 | 56 | # class RFC1751Test 57 | 58 | def get_tests(config={}): 59 | return [RFC1751Test_k2e(), RFC1751Test_e2k()] 60 | 61 | if __name__ == "__main__": 62 | unittest.main() 63 | -------------------------------------------------------------------------------- /Crypto/pct_warnings.py: -------------------------------------------------------------------------------- 1 | # -*- coding: ascii -*- 2 | # 3 | # pct_warnings.py : PyCrypto warnings file 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | # 26 | # Base classes. All our warnings inherit from one of these in order to allow 27 | # the user to specifically filter them. 28 | # 29 | 30 | class CryptoWarning(Warning): 31 | """Base class for PyCrypto warnings""" 32 | 33 | class CryptoDeprecationWarning(DeprecationWarning, CryptoWarning): 34 | """Base PyCrypto DeprecationWarning class""" 35 | 36 | class CryptoRuntimeWarning(RuntimeWarning, CryptoWarning): 37 | """Base PyCrypto RuntimeWarning class""" 38 | 39 | # 40 | # Warnings that we might actually use 41 | # 42 | 43 | class RandomPool_DeprecationWarning(CryptoDeprecationWarning): 44 | """Issued when Crypto.Util.randpool.RandomPool is instantiated.""" 45 | 46 | class ClockRewindWarning(CryptoRuntimeWarning): 47 | """Warning for when the system clock moves backwards.""" 48 | 49 | class GetRandomNumber_DeprecationWarning(CryptoDeprecationWarning): 50 | """Issued when Crypto.Util.number.getRandomNumber is invoked.""" 51 | 52 | class PowmInsecureWarning(CryptoRuntimeWarning): 53 | """Warning for when _fastmath is built without mpz_powm_sec""" 54 | 55 | # By default, we want this warning to be shown every time we compensate for 56 | # clock rewinding. 57 | import warnings as _warnings 58 | _warnings.filterwarnings('always', category=ClockRewindWarning, append=1) 59 | 60 | # vim:set ts=4 sw=4 sts=4 expandtab: 61 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_SHA.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/SHA.py: Self-test for the SHA-1 hash function 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Hash.SHA""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | from Crypto.Util.py3compat import * 30 | 31 | # Test vectors from various sources 32 | # This is a list of (expected_result, input[, description]) tuples. 33 | test_data = [ 34 | # FIPS PUB 180-2, A.1 - "One-Block Message" 35 | ('a9993e364706816aba3e25717850c26c9cd0d89d', 'abc'), 36 | 37 | # FIPS PUB 180-2, A.2 - "Multi-Block Message" 38 | ('84983e441c3bd26ebaae4aa1f95129e5e54670f1', 39 | 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'), 40 | 41 | # FIPS PUB 180-2, A.3 - "Long Message" 42 | # ('34aa973cd4c4daa4f61eeb2bdbad27316534016f', 43 | # 'a' * 10**6, 44 | # '"a" * 10**6'), 45 | 46 | # RFC 3174: Section 7.3, "TEST4" (multiple of 512 bits) 47 | ('dea356a2cddd90c7a7ecedc5ebb563934f460452', 48 | '01234567' * 80, 49 | '"01234567" * 80'), 50 | ] 51 | 52 | def get_tests(config={}): 53 | from Crypto.Hash import SHA 54 | from common import make_hash_tests 55 | return make_hash_tests(SHA, "SHA", test_data, 56 | digest_size=20, 57 | oid="\x06\x05\x2B\x0E\x03\x02\x1A") 58 | 59 | if __name__ == '__main__': 60 | import unittest 61 | suite = lambda: unittest.TestSuite(get_tests()) 62 | unittest.main(defaultTest='suite') 63 | 64 | # vim:set ts=4 sw=4 sts=4 expandtab: 65 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Cipher/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Cipher/__init__.py: Self-test for cipher modules 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test for cipher modules""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | def get_tests(config={}): 30 | tests = [] 31 | from Crypto.SelfTest.Cipher import test_AES; tests += test_AES.get_tests(config=config) 32 | from Crypto.SelfTest.Cipher import test_ARC2; tests += test_ARC2.get_tests(config=config) 33 | from Crypto.SelfTest.Cipher import test_ARC4; tests += test_ARC4.get_tests(config=config) 34 | from Crypto.SelfTest.Cipher import test_Blowfish; tests += test_Blowfish.get_tests(config=config) 35 | from Crypto.SelfTest.Cipher import test_CAST; tests += test_CAST.get_tests(config=config) 36 | from Crypto.SelfTest.Cipher import test_DES3; tests += test_DES3.get_tests(config=config) 37 | from Crypto.SelfTest.Cipher import test_DES; tests += test_DES.get_tests(config=config) 38 | from Crypto.SelfTest.Cipher import test_XOR; tests += test_XOR.get_tests(config=config) 39 | from Crypto.SelfTest.Cipher import test_pkcs1_15; tests += test_pkcs1_15.get_tests(config=config) 40 | from Crypto.SelfTest.Cipher import test_pkcs1_oaep; tests += test_pkcs1_oaep.get_tests(config=config) 41 | return tests 42 | 43 | if __name__ == '__main__': 44 | import unittest 45 | suite = lambda: unittest.TestSuite(get_tests()) 46 | unittest.main(defaultTest='suite') 47 | 48 | # vim:set ts=4 sw=4 sts=4 expandtab: 49 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_MD2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/MD2.py: Self-test for the MD2 hash function 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Hash.MD2""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | from Crypto.Util.py3compat import * 30 | 31 | # This is a list of (expected_result, input[, description]) tuples. 32 | test_data = [ 33 | # Test vectors from RFC 1319 34 | ('8350e5a3e24c153df2275c9f80692773', '', "'' (empty string)"), 35 | ('32ec01ec4a6dac72c0ab96fb34c0b5d1', 'a'), 36 | ('da853b0d3f88d99b30283a69e6ded6bb', 'abc'), 37 | ('ab4f496bfb2a530b219ff33031fe06b0', 'message digest'), 38 | 39 | ('4e8ddff3650292ab5a4108c3aa47940b', 'abcdefghijklmnopqrstuvwxyz', 40 | 'a-z'), 41 | 42 | ('da33def2a42df13975352846c30338cd', 43 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 44 | 'A-Z, a-z, 0-9'), 45 | 46 | ('d5976f79d83d3a0dc9806c3c66f3efd8', 47 | '1234567890123456789012345678901234567890123456' 48 | + '7890123456789012345678901234567890', 49 | "'1234567890' * 8"), 50 | ] 51 | 52 | def get_tests(config={}): 53 | from Crypto.Hash import MD2 54 | from common import make_hash_tests 55 | return make_hash_tests(MD2, "MD2", test_data, 56 | digest_size=16, 57 | oid="\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x02") 58 | 59 | if __name__ == '__main__': 60 | import unittest 61 | suite = lambda: unittest.TestSuite(get_tests()) 62 | unittest.main(defaultTest='suite') 63 | 64 | # vim:set ts=4 sw=4 sts=4 expandtab: 65 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_MD4.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/MD4.py: Self-test for the MD4 hash function 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Hash.MD4""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | from Crypto.Util.py3compat import * 30 | 31 | # This is a list of (expected_result, input[, description]) tuples. 32 | test_data = [ 33 | # Test vectors from RFC 1320 34 | ('31d6cfe0d16ae931b73c59d7e0c089c0', '', "'' (empty string)"), 35 | ('bde52cb31de33e46245e05fbdbd6fb24', 'a'), 36 | ('a448017aaf21d8525fc10ae87aa6729d', 'abc'), 37 | ('d9130a8164549fe818874806e1c7014b', 'message digest'), 38 | 39 | ('d79e1c308aa5bbcdeea8ed63df412da9', 'abcdefghijklmnopqrstuvwxyz', 40 | 'a-z'), 41 | 42 | ('043f8582f241db351ce627e153e7f0e4', 43 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 44 | 'A-Z, a-z, 0-9'), 45 | 46 | ('e33b4ddc9c38f2199c3e7b164fcc0536', 47 | '1234567890123456789012345678901234567890123456' 48 | + '7890123456789012345678901234567890', 49 | "'1234567890' * 8"), 50 | ] 51 | 52 | def get_tests(config={}): 53 | from Crypto.Hash import MD4 54 | from common import make_hash_tests 55 | return make_hash_tests(MD4, "MD4", test_data, 56 | digest_size=16, 57 | oid="\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x04") 58 | 59 | if __name__ == '__main__': 60 | import unittest 61 | suite = lambda: unittest.TestSuite(get_tests()) 62 | unittest.main(defaultTest='suite') 63 | 64 | # vim:set ts=4 sw=4 sts=4 expandtab: 65 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_MD5.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/MD5.py: Self-test for the MD5 hash function 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Hash.MD5""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | from Crypto.Util.py3compat import * 30 | 31 | # This is a list of (expected_result, input[, description]) tuples. 32 | test_data = [ 33 | # Test vectors from RFC 1321 34 | ('d41d8cd98f00b204e9800998ecf8427e', '', "'' (empty string)"), 35 | ('0cc175b9c0f1b6a831c399e269772661', 'a'), 36 | ('900150983cd24fb0d6963f7d28e17f72', 'abc'), 37 | ('f96b697d7cb7938d525a2f31aaf161d0', 'message digest'), 38 | 39 | ('c3fcd3d76192e4007dfb496cca67e13b', 'abcdefghijklmnopqrstuvwxyz', 40 | 'a-z'), 41 | 42 | ('d174ab98d277d9f5a5611c2c9f419d9f', 43 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 44 | 'A-Z, a-z, 0-9'), 45 | 46 | ('57edf4a22be3c955ac49da2e2107b67a', 47 | '1234567890123456789012345678901234567890123456' 48 | + '7890123456789012345678901234567890', 49 | "'1234567890' * 8"), 50 | ] 51 | 52 | def get_tests(config={}): 53 | from Crypto.Hash import MD5 54 | from common import make_hash_tests 55 | return make_hash_tests(MD5, "MD5", test_data, 56 | digest_size=16, 57 | oid="\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x05") 58 | 59 | if __name__ == '__main__': 60 | import unittest 61 | suite = lambda: unittest.TestSuite(get_tests()) 62 | unittest.main(defaultTest='suite') 63 | 64 | # vim:set ts=4 sw=4 sts=4 expandtab: 65 | -------------------------------------------------------------------------------- /Crypto/Hash/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # =================================================================== 4 | # The contents of this file are dedicated to the public domain. To 5 | # the extent that dedication to the public domain is not available, 6 | # everyone is granted a worldwide, perpetual, royalty-free, 7 | # non-exclusive license to exercise all rights associated with the 8 | # contents of this file for any purpose whatsoever. 9 | # No rights are reserved. 10 | # 11 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 12 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 13 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 15 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 17 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | # SOFTWARE. 19 | # =================================================================== 20 | 21 | """Hashing algorithms 22 | 23 | Hash functions take arbitrary binary strings as input, and produce a random-like output 24 | of fixed size that is dependent on the input; it should be practically infeasible 25 | to derive the original input data given only the hash function's 26 | output. In other words, the hash function is *one-way*. 27 | 28 | It should also not be practically feasible to find a second piece of data 29 | (a *second pre-image*) whose hash is the same as the original message 30 | (*weak collision resistance*). 31 | 32 | Finally, it should not be feasible to find two arbitrary messages with the 33 | same hash (*strong collision resistance*). 34 | 35 | The output of the hash function is called the *digest* of the input message. 36 | In general, the security of a hash function is related to the length of the 37 | digest. If the digest is *n* bits long, its security level is roughly comparable 38 | to the the one offered by an *n/2* bit encryption algorithm. 39 | 40 | Hash functions can be used simply as a integrity check, or, in 41 | association with a public-key algorithm, can be used to implement 42 | digital signatures. 43 | 44 | The hashing modules here all support the interface described in `PEP 45 | 247`_ , "API for Cryptographic Hash Functions". 46 | 47 | .. _`PEP 247` : http://www.python.org/dev/peps/pep-0247/ 48 | 49 | :undocumented: _MD2, _MD4, _RIPEMD160, _SHA224, _SHA256, _SHA384, _SHA512 50 | """ 51 | 52 | __all__ = ['HMAC', 'MD2', 'MD4', 'MD5', 'RIPEMD', 'SHA', 53 | 'SHA224', 'SHA256', 'SHA384', 'SHA512'] 54 | __revision__ = "$Id$" 55 | 56 | 57 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/Fortuna/test_SHAd256.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Random/Fortuna/test_SHAd256.py: Self-test for the SHAd256 hash function 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Random.Fortuna.SHAd256""" 26 | 27 | __revision__ = "$Id$" 28 | from Crypto.Util.py3compat import * 29 | 30 | # This is a list of (expected_result, input[, description]) tuples. 31 | test_data = [ 32 | # I could not find any test vectors for SHAd256, so I made these vectors by 33 | # feeding some sample data into several plain SHA256 implementations 34 | # (including OpenSSL, the "sha256sum" tool, and this implementation). 35 | # This is a subset of the resulting test vectors. The complete list can be 36 | # found at: http://www.dlitz.net/crypto/shad256-test-vectors/ 37 | ('5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456', 38 | '', "'' (empty string)"), 39 | ('4f8b42c22dd3729b519ba6f68d2da7cc5b2d606d05daed5ad5128cc03e6c6358', 40 | 'abc'), 41 | ('0cffe17f68954dac3a84fb1458bd5ec99209449749b2b308b7cb55812f9563af', 42 | 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') 43 | ] 44 | 45 | def get_tests(config={}): 46 | from Crypto.Random.Fortuna import SHAd256 47 | from Crypto.SelfTest.Hash.common import make_hash_tests 48 | return make_hash_tests(SHAd256, "SHAd256", test_data, 32) 49 | 50 | if __name__ == '__main__': 51 | import unittest 52 | suite = lambda: unittest.TestSuite(get_tests()) 53 | unittest.main(defaultTest='suite') 54 | 55 | # vim:set ts=4 sw=4 sts=4 expandtab: 56 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/__init__.py: Self-test for hash modules 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test for hash modules""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | def get_tests(config={}): 30 | tests = [] 31 | from Crypto.SelfTest.Hash import test_HMAC; tests += test_HMAC.get_tests(config=config) 32 | from Crypto.SelfTest.Hash import test_MD2; tests += test_MD2.get_tests(config=config) 33 | from Crypto.SelfTest.Hash import test_MD4; tests += test_MD4.get_tests(config=config) 34 | from Crypto.SelfTest.Hash import test_MD5; tests += test_MD5.get_tests(config=config) 35 | from Crypto.SelfTest.Hash import test_RIPEMD; tests += test_RIPEMD.get_tests(config=config) 36 | from Crypto.SelfTest.Hash import test_SHA; tests += test_SHA.get_tests(config=config) 37 | from Crypto.SelfTest.Hash import test_SHA256; tests += test_SHA256.get_tests(config=config) 38 | try: 39 | from Crypto.SelfTest.Hash import test_SHA224; tests += test_SHA224.get_tests(config=config) 40 | from Crypto.SelfTest.Hash import test_SHA384; tests += test_SHA384.get_tests(config=config) 41 | from Crypto.SelfTest.Hash import test_SHA512; tests += test_SHA512.get_tests(config=config) 42 | except ImportError: 43 | import sys 44 | sys.stderr.write("SelfTest: warning: not testing SHA224/SHA384/SHA512 modules (not available)\n") 45 | return tests 46 | 47 | if __name__ == '__main__': 48 | import unittest 49 | suite = lambda: unittest.TestSuite(get_tests()) 50 | unittest.main(defaultTest='suite') 51 | 52 | # vim:set ts=4 sw=4 sts=4 expandtab: 53 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Cipher/test_XOR.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Cipher/XOR.py: Self-test for the XOR "cipher" 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Cipher.XOR""" 26 | 27 | import unittest 28 | 29 | __revision__ = "$Id$" 30 | 31 | from Crypto.Util.py3compat import * 32 | 33 | # This is a list of (plaintext, ciphertext, key) tuples. 34 | test_data = [ 35 | # Test vectors written from scratch. (Nobody posts XOR test vectors on the web? How disappointing.) 36 | ('01', '01', 37 | '00', 38 | 'zero key'), 39 | 40 | ('0102040810204080', '0003050911214181', 41 | '01', 42 | '1-byte key'), 43 | 44 | ('0102040810204080', 'cda8c8a2dc8a8c2a', 45 | 'ccaa', 46 | '2-byte key'), 47 | 48 | ('ff'*64, 'fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e0'*2, 49 | '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f', 50 | '32-byte key'), 51 | ] 52 | 53 | class TruncationSelfTest(unittest.TestCase): 54 | 55 | def runTest(self): 56 | """33-byte key (should raise ValueError under current implementation)""" 57 | # Crypto.Cipher.XOR previously truncated its inputs at 32 bytes. Now 58 | # it should raise a ValueError if the length is too long. 59 | self.assertRaises(ValueError, XOR.new, "x"*33) 60 | 61 | def get_tests(config={}): 62 | global XOR 63 | from Crypto.Cipher import XOR 64 | from common import make_stream_tests 65 | return make_stream_tests(XOR, "XOR", test_data) + [TruncationSelfTest()] 66 | 67 | if __name__ == '__main__': 68 | import unittest 69 | suite = lambda: unittest.TestSuite(get_tests()) 70 | unittest.main(defaultTest='suite') 71 | 72 | # vim:set ts=4 sw=4 sts=4 expandtab: 73 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_SHA224.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/test_SHA224.py: Self-test for the SHA-224 hash function 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Hash.SHA224""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | # Test vectors from various sources 30 | # This is a list of (expected_result, input[, description]) tuples. 31 | test_data = [ 32 | 33 | # RFC 3874: Section 3.1, "Test Vector #1 34 | ('23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7', 'abc'), 35 | 36 | # RFC 3874: Section 3.2, "Test Vector #2 37 | ('75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525', 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'), 38 | 39 | # RFC 3874: Section 3.3, "Test Vector #3 40 | ('20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67', 'a' * 10**6, "'a' * 10**6"), 41 | 42 | # Examples from http://de.wikipedia.org/wiki/Secure_Hash_Algorithm 43 | ('d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f', ''), 44 | 45 | ('49b08defa65e644cbf8a2dd9270bdededabc741997d1dadd42026d7b', 46 | 'Franz jagt im komplett verwahrlosten Taxi quer durch Bayern'), 47 | 48 | ('58911e7fccf2971a7d07f93162d8bd13568e71aa8fc86fc1fe9043d1', 49 | 'Frank jagt im komplett verwahrlosten Taxi quer durch Bayern'), 50 | 51 | ] 52 | 53 | def get_tests(config={}): 54 | from Crypto.Hash import SHA224 55 | from common import make_hash_tests 56 | return make_hash_tests(SHA224, "SHA224", test_data, 57 | digest_size=28, 58 | oid='\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04') 59 | 60 | if __name__ == '__main__': 61 | import unittest 62 | suite = lambda: unittest.TestSuite(get_tests()) 63 | unittest.main(defaultTest='suite') 64 | 65 | # vim:set ts=4 sw=4 sts=4 expandtab: 66 | -------------------------------------------------------------------------------- /Crypto/Random/OSRNG/nt.py: -------------------------------------------------------------------------------- 1 | # 2 | # Random/OSRNG/nt.py : OS entropy source for MS Windows 3 | # 4 | # Written in 2008 by Dwayne C. Litzenberger 5 | # 6 | # =================================================================== 7 | # The contents of this file are dedicated to the public domain. To 8 | # the extent that dedication to the public domain is not available, 9 | # everyone is granted a worldwide, perpetual, royalty-free, 10 | # non-exclusive license to exercise all rights associated with the 11 | # contents of this file for any purpose whatsoever. 12 | # No rights are reserved. 13 | # 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 18 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 19 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | # =================================================================== 23 | 24 | 25 | __revision__ = "$Id$" 26 | __all__ = ['WindowsRNG'] 27 | 28 | import winrandom 29 | from rng_base import BaseRNG 30 | 31 | class WindowsRNG(BaseRNG): 32 | 33 | name = "" 34 | 35 | def __init__(self): 36 | self.__winrand = winrandom.new() 37 | BaseRNG.__init__(self) 38 | 39 | def flush(self): 40 | """Work around weakness in Windows RNG. 41 | 42 | The CryptGenRandom mechanism in some versions of Windows allows an 43 | attacker to learn 128 KiB of past and future output. As a workaround, 44 | this function reads 128 KiB of 'random' data from Windows and discards 45 | it. 46 | 47 | For more information about the weaknesses in CryptGenRandom, see 48 | _Cryptanalysis of the Random Number Generator of the Windows Operating 49 | System_, by Leo Dorrendorf and Zvi Gutterman and Benny Pinkas 50 | http://eprint.iacr.org/2007/419 51 | """ 52 | if self.closed: 53 | raise ValueError("I/O operation on closed file") 54 | data = self.__winrand.get_bytes(128*1024) 55 | assert (len(data) == 128*1024) 56 | BaseRNG.flush(self) 57 | 58 | def _close(self): 59 | self.__winrand = None 60 | 61 | def _read(self, N): 62 | # Unfortunately, research shows that CryptGenRandom doesn't provide 63 | # forward secrecy and fails the next-bit test unless we apply a 64 | # workaround, which we do here. See http://eprint.iacr.org/2007/419 65 | # for information on the vulnerability. 66 | self.flush() 67 | data = self.__winrand.get_bytes(N) 68 | self.flush() 69 | return data 70 | 71 | def new(*args, **kwargs): 72 | return WindowsRNG(*args, **kwargs) 73 | 74 | # vim:set ts=4 sw=4 sts=4 expandtab: 75 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_RIPEMD.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/test_RIPEMD.py: Self-test for the RIPEMD-160 hash function 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | #"""Self-test suite for Crypto.Hash.RIPEMD""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | from Crypto.Util.py3compat import * 30 | 31 | # This is a list of (expected_result, input[, description]) tuples. 32 | test_data = [ 33 | # Test vectors downloaded 2008-09-12 from 34 | # http://homes.esat.kuleuven.be/~bosselae/ripemd160.html 35 | ('9c1185a5c5e9fc54612808977ee8f548b2258d31', '', "'' (empty string)"), 36 | ('0bdc9d2d256b3ee9daae347be6f4dc835a467ffe', 'a'), 37 | ('8eb208f7e05d987a9b044a8e98c6b087f15a0bfc', 'abc'), 38 | ('5d0689ef49d2fae572b881b123a85ffa21595f36', 'message digest'), 39 | 40 | ('f71c27109c692c1b56bbdceb5b9d2865b3708dbc', 41 | 'abcdefghijklmnopqrstuvwxyz', 42 | 'a-z'), 43 | 44 | ('12a053384a9c0c88e405a06c27dcf49ada62eb2b', 45 | 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq', 46 | 'abcdbcd...pnopq'), 47 | 48 | ('b0e20b6e3116640286ed3a87a5713079b21f5189', 49 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 50 | 'A-Z, a-z, 0-9'), 51 | 52 | ('9b752e45573d4b39f4dbd3323cab82bf63326bfb', 53 | '1234567890' * 8, 54 | "'1234567890' * 8"), 55 | 56 | ('52783243c1697bdbe16d37f97f68f08325dc1528', 57 | 'a' * 10**6, 58 | '"a" * 10**6'), 59 | ] 60 | 61 | def get_tests(config={}): 62 | from Crypto.Hash import RIPEMD 63 | from common import make_hash_tests 64 | return make_hash_tests(RIPEMD, "RIPEMD", test_data, 65 | digest_size=20, 66 | oid="\x06\x05\x2b\x24\x03\02\x01") 67 | 68 | if __name__ == '__main__': 69 | import unittest 70 | suite = lambda: unittest.TestSuite(get_tests()) 71 | unittest.main(defaultTest='suite') 72 | 73 | # vim:set ts=4 sw=4 sts=4 expandtab: 74 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_SHA384.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/test_SHA.py: Self-test for the SHA-384 hash function 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Hash.SHA384""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | # Test vectors from various sources 30 | # This is a list of (expected_result, input[, description]) tuples. 31 | test_data = [ 32 | 33 | # RFC 4634: Section Page 8.4, "Test 1" 34 | ('cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7', 'abc'), 35 | 36 | # RFC 4634: Section Page 8.4, "Test 2.2" 37 | ('09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039', 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu'), 38 | 39 | # RFC 4634: Section Page 8.4, "Test 3" 40 | ('9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985', 'a' * 10**6, "'a' * 10**6"), 41 | 42 | # Taken from http://de.wikipedia.org/wiki/Secure_Hash_Algorithm 43 | ('38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b', ''), 44 | 45 | # Example from http://de.wikipedia.org/wiki/Secure_Hash_Algorithm 46 | ('71e8383a4cea32d6fd6877495db2ee353542f46fa44bc23100bca48f3366b84e809f0708e81041f427c6d5219a286677', 47 | 'Franz jagt im komplett verwahrlosten Taxi quer durch Bayern'), 48 | 49 | ] 50 | 51 | def get_tests(config={}): 52 | from Crypto.Hash import SHA384 53 | from common import make_hash_tests 54 | return make_hash_tests(SHA384, "SHA384", test_data, 55 | digest_size=48, 56 | oid='\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02') 57 | 58 | if __name__ == '__main__': 59 | import unittest 60 | suite = lambda: unittest.TestSuite(get_tests()) 61 | unittest.main(defaultTest='suite') 62 | 63 | # vim:set ts=4 sw=4 sts=4 expandtab: 64 | -------------------------------------------------------------------------------- /Crypto/Cipher/XOR.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Cipher/XOR.py : XOR 4 | # 5 | # =================================================================== 6 | # The contents of this file are dedicated to the public domain. To 7 | # the extent that dedication to the public domain is not available, 8 | # everyone is granted a worldwide, perpetual, royalty-free, 9 | # non-exclusive license to exercise all rights associated with the 10 | # contents of this file for any purpose whatsoever. 11 | # No rights are reserved. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 14 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 15 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 16 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 17 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 18 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | # SOFTWARE. 21 | # =================================================================== 22 | """XOR toy cipher 23 | 24 | XOR is one the simplest stream ciphers. Encryption and decryption are 25 | performed by XOR-ing data with a keystream made by contatenating 26 | the key. 27 | 28 | Do not use it for real applications! 29 | 30 | :undocumented: __revision__, __package__ 31 | """ 32 | 33 | __revision__ = "$Id$" 34 | 35 | from Crypto.Cipher import _XOR 36 | 37 | class XORCipher: 38 | """XOR cipher object""" 39 | 40 | def __init__(self, key, *args, **kwargs): 41 | """Initialize a XOR cipher object 42 | 43 | See also `new()` at the module level.""" 44 | self._cipher = _XOR.new(key, *args, **kwargs) 45 | self.block_size = self._cipher.block_size 46 | self.key_size = self._cipher.key_size 47 | 48 | def encrypt(self, plaintext): 49 | """Encrypt a piece of data. 50 | 51 | :Parameters: 52 | plaintext : byte string 53 | The piece of data to encrypt. It can be of any size. 54 | :Return: the encrypted data (byte string, as long as the 55 | plaintext). 56 | """ 57 | return self._cipher.encrypt(plaintext) 58 | 59 | def decrypt(self, ciphertext): 60 | """Decrypt a piece of data. 61 | 62 | :Parameters: 63 | ciphertext : byte string 64 | The piece of data to decrypt. It can be of any size. 65 | :Return: the decrypted data (byte string, as long as the 66 | ciphertext). 67 | """ 68 | return self._cipher.decrypt(ciphertext) 69 | 70 | def new(key, *args, **kwargs): 71 | """Create a new XOR cipher 72 | 73 | :Parameters: 74 | key : byte string 75 | The secret key to use in the symmetric cipher. 76 | Its length may vary from 1 to 32 bytes. 77 | 78 | :Return: an `XORCipher` object 79 | """ 80 | return XORCipher(key, *args, **kwargs) 81 | 82 | #: Size of a data block (in bytes) 83 | block_size = 1 84 | #: Size of a key (in bytes) 85 | key_size = xrange(1,32+1) 86 | 87 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_SHA512.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/test_SHA512.py: Self-test for the SHA-512 hash function 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Hash.SHA512""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | # Test vectors from various sources 30 | # This is a list of (expected_result, input[, description]) tuples. 31 | test_data = [ 32 | 33 | # RFC 4634: Section Page 8.4, "Test 1" 34 | ('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f', 'abc'), 35 | 36 | # RFC 4634: Section Page 8.4, "Test 2.1" 37 | ('8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909', 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu'), 38 | 39 | # RFC 4634: Section Page 8.4, "Test 3" 40 | ('e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b', 'a' * 10**6, "'a' * 10**6"), 41 | 42 | # Taken from http://de.wikipedia.org/wiki/Secure_Hash_Algorithm 43 | ('cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e', ''), 44 | 45 | ('af9ed2de700433b803240a552b41b5a472a6ef3fe1431a722b2063c75e9f07451f67a28e37d09cde769424c96aea6f8971389db9e1993d6c565c3c71b855723c', 'Franz jagt im komplett verwahrlosten Taxi quer durch Bayern'), 46 | ] 47 | 48 | def get_tests(config={}): 49 | from Crypto.Hash import SHA512 50 | from common import make_hash_tests 51 | return make_hash_tests(SHA512, "SHA512", test_data, 52 | digest_size=64, 53 | oid="\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03") 54 | 55 | if __name__ == '__main__': 56 | import unittest 57 | suite = lambda: unittest.TestSuite(get_tests()) 58 | unittest.main(defaultTest='suite') 59 | 60 | # vim:set ts=4 sw=4 sts=4 expandtab: 61 | -------------------------------------------------------------------------------- /Crypto/Hash/MD4.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # =================================================================== 4 | # The contents of this file are dedicated to the public domain. To 5 | # the extent that dedication to the public domain is not available, 6 | # everyone is granted a worldwide, perpetual, royalty-free, 7 | # non-exclusive license to exercise all rights associated with the 8 | # contents of this file for any purpose whatsoever. 9 | # No rights are reserved. 10 | # 11 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 12 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 13 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 15 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 17 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | # SOFTWARE. 19 | # =================================================================== 20 | 21 | """MD4 cryptographic hash algorithm. 22 | 23 | MD4 is specified in RFC1320_ and produces the 128 bit digest of a message. 24 | 25 | >>> from Crypto.Hash import MD4 26 | >>> 27 | >>> h = MD4.new() 28 | >>> h.update(b'Hello') 29 | >>> print h.hexdigest() 30 | 31 | MD4 stand for Message Digest version 4, and it was invented by Rivest in 1990. 32 | 33 | This algorithm is insecure. Do not use it for new designs. 34 | 35 | .. _RFC1320: http://tools.ietf.org/html/rfc1320 36 | """ 37 | 38 | _revision__ = "$Id$" 39 | 40 | __all__ = ['new', 'digest_size', 'MD4Hash' ] 41 | 42 | from Crypto.Util.py3compat import * 43 | from Crypto.Hash.hashalgo import HashAlgo 44 | 45 | import Crypto.Hash._MD4 as _MD4 46 | hashFactory = _MD4 47 | 48 | class MD4Hash(HashAlgo): 49 | """Class that implements an MD4 hash 50 | 51 | :undocumented: block_size 52 | """ 53 | 54 | #: ASN.1 Object identifier (OID):: 55 | #: 56 | #: id-md2 OBJECT IDENTIFIER ::= { 57 | #: iso(1) member-body(2) us(840) rsadsi(113549) 58 | #: digestAlgorithm(2) 4 59 | #: } 60 | #: 61 | #: This value uniquely identifies the MD4 algorithm. 62 | oid = b('\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x04') 63 | 64 | digest_size = 16 65 | block_size = 64 66 | 67 | def __init__(self, data=None): 68 | HashAlgo.__init__(self, hashFactory, data) 69 | 70 | def new(self, data=None): 71 | return MD4Hash(data) 72 | 73 | def new(data=None): 74 | """Return a fresh instance of the hash object. 75 | 76 | :Parameters: 77 | data : byte string 78 | The very first chunk of the message to hash. 79 | It is equivalent to an early call to `MD4Hash.update()`. 80 | Optional. 81 | 82 | :Return: A `MD4Hash` object 83 | """ 84 | return MD4Hash().new(data) 85 | 86 | #: The size of the resulting hash in bytes. 87 | digest_size = MD4Hash.digest_size 88 | 89 | #: The internal block size of the hash algorithm in bytes. 90 | block_size = MD4Hash.block_size 91 | 92 | -------------------------------------------------------------------------------- /Crypto/PublicKey/_RSA.py: -------------------------------------------------------------------------------- 1 | # 2 | # RSA.py : RSA encryption/decryption 3 | # 4 | # Part of the Python Cryptography Toolkit 5 | # 6 | # Written by Andrew Kuchling, Paul Swartz, and others 7 | # 8 | # =================================================================== 9 | # The contents of this file are dedicated to the public domain. To 10 | # the extent that dedication to the public domain is not available, 11 | # everyone is granted a worldwide, perpetual, royalty-free, 12 | # non-exclusive license to exercise all rights associated with the 13 | # contents of this file for any purpose whatsoever. 14 | # No rights are reserved. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | # SOFTWARE. 24 | # =================================================================== 25 | # 26 | 27 | __revision__ = "$Id$" 28 | 29 | from Crypto.PublicKey import pubkey 30 | from Crypto.Util import number 31 | 32 | def generate_py(bits, randfunc, progress_func=None, e=65537): 33 | """generate(bits:int, randfunc:callable, progress_func:callable, e:int) 34 | 35 | Generate an RSA key of length 'bits', public exponent 'e'(which must be 36 | odd), using 'randfunc' to get random data and 'progress_func', 37 | if present, to display the progress of the key generation. 38 | """ 39 | obj=RSAobj() 40 | obj.e = long(e) 41 | 42 | # Generate the prime factors of n 43 | if progress_func: 44 | progress_func('p,q\n') 45 | p = q = 1L 46 | while number.size(p*q) < bits: 47 | # Note that q might be one bit longer than p if somebody specifies an odd 48 | # number of bits for the key. (Why would anyone do that? You don't get 49 | # more security.) 50 | p = pubkey.getStrongPrime(bits>>1, obj.e, 1e-12, randfunc) 51 | q = pubkey.getStrongPrime(bits - (bits>>1), obj.e, 1e-12, randfunc) 52 | 53 | # It's OK for p to be larger than q, but let's be 54 | # kind to the function that will invert it for 55 | # th calculation of u. 56 | if p > q: 57 | (p, q)=(q, p) 58 | obj.p = p 59 | obj.q = q 60 | 61 | if progress_func: 62 | progress_func('u\n') 63 | obj.u = pubkey.inverse(obj.p, obj.q) 64 | obj.n = obj.p*obj.q 65 | 66 | if progress_func: 67 | progress_func('d\n') 68 | obj.d=pubkey.inverse(obj.e, (obj.p-1)*(obj.q-1)) 69 | 70 | assert bits <= 1+obj.size(), "Generated key is too small" 71 | 72 | return obj 73 | 74 | class RSAobj(pubkey.pubkey): 75 | 76 | def size(self): 77 | """size() : int 78 | Return the maximum number of bits that can be handled by this key. 79 | """ 80 | return number.size(self.n) - 1 81 | 82 | -------------------------------------------------------------------------------- /Crypto/Util/randpool.py: -------------------------------------------------------------------------------- 1 | # 2 | # randpool.py : Cryptographically strong random number generation 3 | # 4 | # Part of the Python Cryptography Toolkit 5 | # 6 | # Written by Andrew M. Kuchling, Mark Moraes, and others 7 | # 8 | # =================================================================== 9 | # The contents of this file are dedicated to the public domain. To 10 | # the extent that dedication to the public domain is not available, 11 | # everyone is granted a worldwide, perpetual, royalty-free, 12 | # non-exclusive license to exercise all rights associated with the 13 | # contents of this file for any purpose whatsoever. 14 | # No rights are reserved. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | # SOFTWARE. 24 | # =================================================================== 25 | # 26 | 27 | __revision__ = "$Id$" 28 | 29 | from Crypto.pct_warnings import RandomPool_DeprecationWarning 30 | import Crypto.Random 31 | import warnings 32 | 33 | class RandomPool: 34 | """Deprecated. Use Random.new() instead. 35 | 36 | See http://www.pycrypto.org/randpool-broken 37 | """ 38 | def __init__(self, numbytes = 160, cipher=None, hash=None, file=None): 39 | warnings.warn("This application uses RandomPool, which is BROKEN in older releases. See http://www.pycrypto.org/randpool-broken", 40 | RandomPool_DeprecationWarning) 41 | self.__rng = Crypto.Random.new() 42 | self.bytes = numbytes 43 | self.bits = self.bytes * 8 44 | self.entropy = self.bits 45 | 46 | def get_bytes(self, N): 47 | return self.__rng.read(N) 48 | 49 | def _updateEntropyEstimate(self, nbits): 50 | self.entropy += nbits 51 | if self.entropy < 0: 52 | self.entropy = 0 53 | elif self.entropy > self.bits: 54 | self.entropy = self.bits 55 | 56 | def _randomize(self, N=0, devname="/dev/urandom"): 57 | """Dummy _randomize() function""" 58 | self.__rng.flush() 59 | 60 | def randomize(self, N=0): 61 | """Dummy randomize() function""" 62 | self.__rng.flush() 63 | 64 | def stir(self, s=''): 65 | """Dummy stir() function""" 66 | self.__rng.flush() 67 | 68 | def stir_n(self, N=3): 69 | """Dummy stir_n() function""" 70 | self.__rng.flush() 71 | 72 | def add_event(self, s=''): 73 | """Dummy add_event() function""" 74 | self.__rng.flush() 75 | 76 | def getBytes(self, N): 77 | """Dummy getBytes() function""" 78 | return self.get_bytes(N) 79 | 80 | def addEvent(self, event, s=""): 81 | """Dummy addEvent() function""" 82 | return self.add_event() 83 | -------------------------------------------------------------------------------- /Crypto/Hash/MD2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # =================================================================== 4 | # The contents of this file are dedicated to the public domain. To 5 | # the extent that dedication to the public domain is not available, 6 | # everyone is granted a worldwide, perpetual, royalty-free, 7 | # non-exclusive license to exercise all rights associated with the 8 | # contents of this file for any purpose whatsoever. 9 | # No rights are reserved. 10 | # 11 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 12 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 13 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 15 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 17 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | # SOFTWARE. 19 | # =================================================================== 20 | 21 | """MD2 cryptographic hash algorithm. 22 | 23 | MD2 is specified in RFC1319_ and it produces the 128 bit digest of a message. 24 | 25 | >>> from Crypto.Hash import MD2 26 | >>> 27 | >>> h = MD2.new() 28 | >>> h.update(b'Hello') 29 | >>> print h.hexdigest() 30 | 31 | MD2 stand for Message Digest version 2, and it was invented by Rivest in 1989. 32 | 33 | This algorithm is both slow and insecure. Do not use it for new designs. 34 | 35 | .. _RFC1319: http://tools.ietf.org/html/rfc1319 36 | """ 37 | 38 | _revision__ = "$Id$" 39 | 40 | __all__ = ['new', 'digest_size', 'MD2Hash' ] 41 | 42 | from Crypto.Util.py3compat import * 43 | from Crypto.Hash.hashalgo import HashAlgo 44 | 45 | import Crypto.Hash._MD2 as _MD2 46 | hashFactory = _MD2 47 | 48 | class MD2Hash(HashAlgo): 49 | """Class that implements an MD2 hash 50 | 51 | :undocumented: block_size 52 | """ 53 | 54 | #: ASN.1 Object identifier (OID):: 55 | #: 56 | #: id-md2 OBJECT IDENTIFIER ::= { 57 | #: iso(1) member-body(2) us(840) rsadsi(113549) 58 | #: digestAlgorithm(2) 2 59 | #: } 60 | #: 61 | #: This value uniquely identifies the MD2 algorithm. 62 | oid = b('\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x02') 63 | 64 | digest_size = 16 65 | block_size = 16 66 | 67 | def __init__(self, data=None): 68 | HashAlgo.__init__(self, hashFactory, data) 69 | 70 | def new(self, data=None): 71 | return MD2Hash(data) 72 | 73 | def new(data=None): 74 | """Return a fresh instance of the hash object. 75 | 76 | :Parameters: 77 | data : byte string 78 | The very first chunk of the message to hash. 79 | It is equivalent to an early call to `MD2Hash.update()`. 80 | Optional. 81 | 82 | :Return: An `MD2Hash` object 83 | """ 84 | return MD2Hash().new(data) 85 | 86 | #: The size of the resulting hash in bytes. 87 | digest_size = MD2Hash.digest_size 88 | 89 | #: The internal block size of the hash algorithm in bytes. 90 | block_size = MD2Hash.block_size 91 | 92 | -------------------------------------------------------------------------------- /Crypto/Util/py21compat.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Util/py21compat.py : Compatibility code for Python 2.1 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Compatibility code for Python 2.1 26 | 27 | Currently, this just defines: 28 | - True and False 29 | - object 30 | - isinstance 31 | """ 32 | 33 | __revision__ = "$Id$" 34 | __all__ = [] 35 | 36 | import sys 37 | import __builtin__ 38 | 39 | # 'True' and 'False' aren't defined in Python 2.1. Define them. 40 | try: 41 | True, False 42 | except NameError: 43 | (True, False) = (1, 0) 44 | __all__ += ['True', 'False'] 45 | 46 | # New-style classes were introduced in Python 2.2. Defining "object" in Python 47 | # 2.1 lets us use new-style classes in versions of Python that support them, 48 | # while still maintaining backward compatibility with old-style classes 49 | try: 50 | object 51 | except NameError: 52 | class object: pass 53 | __all__ += ['object'] 54 | 55 | # Starting with Python 2.2, isinstance allows a tuple for the second argument. 56 | # Also, builtins like "tuple", "list", "str", "unicode", "int", and "long" 57 | # became first-class types, rather than functions. We want to support 58 | # constructs like: 59 | # isinstance(x, (int, long)) 60 | # So we hack it for Python 2.1. 61 | try: 62 | isinstance(5, (int, long)) 63 | except TypeError: 64 | __all__ += ['isinstance'] 65 | _builtin_type_map = { 66 | tuple: type(()), 67 | list: type([]), 68 | str: type(""), 69 | unicode: type(u""), 70 | int: type(0), 71 | long: type(0L), 72 | } 73 | def isinstance(obj, t): 74 | if not __builtin__.isinstance(t, type(())): 75 | # t is not a tuple 76 | return __builtin__.isinstance(obj, _builtin_type_map.get(t, t)) 77 | else: 78 | # t is a tuple 79 | for typ in t: 80 | if __builtin__.isinstance(obj, _builtin_type_map.get(typ, typ)): 81 | return True 82 | return False 83 | 84 | # vim:set ts=4 sw=4 sts=4 expandtab: 85 | -------------------------------------------------------------------------------- /Crypto/Random/OSRNG/posix.py: -------------------------------------------------------------------------------- 1 | # 2 | # Random/OSRNG/posix.py : OS entropy source for POSIX systems 3 | # 4 | # Written in 2008 by Dwayne C. Litzenberger 5 | # 6 | # =================================================================== 7 | # The contents of this file are dedicated to the public domain. To 8 | # the extent that dedication to the public domain is not available, 9 | # everyone is granted a worldwide, perpetual, royalty-free, 10 | # non-exclusive license to exercise all rights associated with the 11 | # contents of this file for any purpose whatsoever. 12 | # No rights are reserved. 13 | # 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 18 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 19 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | # =================================================================== 23 | 24 | 25 | __revision__ = "$Id$" 26 | __all__ = ['DevURandomRNG'] 27 | 28 | import errno 29 | import os 30 | import stat 31 | 32 | from rng_base import BaseRNG 33 | from Crypto.Util.py3compat import b 34 | 35 | class DevURandomRNG(BaseRNG): 36 | 37 | def __init__(self, devname=None): 38 | if devname is None: 39 | self.name = "/dev/urandom" 40 | else: 41 | self.name = devname 42 | 43 | # Test that /dev/urandom is a character special device 44 | f = open(self.name, "rb", 0) 45 | fmode = os.fstat(f.fileno())[stat.ST_MODE] 46 | if not stat.S_ISCHR(fmode): 47 | f.close() 48 | raise TypeError("%r is not a character special device" % (self.name,)) 49 | 50 | self.__file = f 51 | 52 | BaseRNG.__init__(self) 53 | 54 | def _close(self): 55 | self.__file.close() 56 | 57 | def _read(self, N): 58 | # Starting with Python 3 open with buffering=0 returns a FileIO object. 59 | # FileIO.read behaves like read(2) and not like fread(3) and thus we 60 | # have to handle the case that read returns less data as requested here 61 | # more carefully. 62 | data = b("") 63 | while len(data) < N: 64 | try: 65 | d = self.__file.read(N - len(data)) 66 | except IOError, e: 67 | # read(2) has been interrupted by a signal; redo the read 68 | if e.errno == errno.EINTR: 69 | continue 70 | raise 71 | 72 | if d is None: 73 | # __file is in non-blocking mode and no data is available 74 | return data 75 | if len(d) == 0: 76 | # __file is in blocking mode and arrived at EOF 77 | return data 78 | 79 | data += d 80 | return data 81 | 82 | def new(*args, **kwargs): 83 | return DevURandomRNG(*args, **kwargs) 84 | 85 | 86 | # vim:set ts=4 sw=4 sts=4 expandtab: 87 | -------------------------------------------------------------------------------- /Crypto/Hash/SHA224.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # =================================================================== 4 | # The contents of this file are dedicated to the public domain. To 5 | # the extent that dedication to the public domain is not available, 6 | # everyone is granted a worldwide, perpetual, royalty-free, 7 | # non-exclusive license to exercise all rights associated with the 8 | # contents of this file for any purpose whatsoever. 9 | # No rights are reserved. 10 | # 11 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 12 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 13 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 15 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 17 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | # SOFTWARE. 19 | # =================================================================== 20 | 21 | """SHA-224 cryptographic hash algorithm. 22 | 23 | SHA-224 belongs to the SHA-2_ family of cryptographic hashes. 24 | It produces the 224 bit digest of a message. 25 | 26 | >>> from Crypto.Hash import SHA224 27 | >>> 28 | >>> h = SHA224.new() 29 | >>> h.update(b'Hello') 30 | >>> print h.hexdigest() 31 | 32 | *SHA* stands for Secure Hash Algorithm. 33 | 34 | .. _SHA-2: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf 35 | """ 36 | 37 | _revision__ = "$Id$" 38 | 39 | __all__ = ['new', 'digest_size', 'SHA224Hash' ] 40 | 41 | from Crypto.Util.py3compat import * 42 | from Crypto.Hash.hashalgo import HashAlgo 43 | 44 | try: 45 | import hashlib 46 | hashFactory = hashlib.sha224 47 | 48 | except ImportError: 49 | from Crypto.Hash import _SHA224 50 | hashFactory = _SHA224 51 | 52 | class SHA224Hash(HashAlgo): 53 | """Class that implements a SHA-224 hash 54 | 55 | :undocumented: block_size 56 | """ 57 | 58 | #: ASN.1 Object identifier (OID):: 59 | #: 60 | #: id-sha224 OBJECT IDENTIFIER ::= { 61 | #: joint-iso-itu-t(2) country(16) us(840) organization(1) gov(101) csor(3) 62 | #: nistalgorithm(4) hashalgs(2) 4 63 | #: } 64 | #: 65 | #: This value uniquely identifies the SHA-224 algorithm. 66 | oid = b('\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04') 67 | 68 | digest_size = 28 69 | block_size = 64 70 | 71 | def __init__(self, data=None): 72 | HashAlgo.__init__(self, hashFactory, data) 73 | 74 | def new(self, data=None): 75 | return SHA224Hash(data) 76 | 77 | def new(data=None): 78 | """Return a fresh instance of the hash object. 79 | 80 | :Parameters: 81 | data : byte string 82 | The very first chunk of the message to hash. 83 | It is equivalent to an early call to `SHA224Hash.update()`. 84 | Optional. 85 | 86 | :Return: A `SHA224Hash` object 87 | """ 88 | return SHA224Hash().new(data) 89 | 90 | #: The size of the resulting hash in bytes. 91 | digest_size = SHA224Hash.digest_size 92 | 93 | #: The internal block size of the hash algorithm in bytes. 94 | block_size = SHA224Hash.block_size 95 | 96 | -------------------------------------------------------------------------------- /Crypto/Hash/SHA512.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # =================================================================== 4 | # The contents of this file are dedicated to the public domain. To 5 | # the extent that dedication to the public domain is not available, 6 | # everyone is granted a worldwide, perpetual, royalty-free, 7 | # non-exclusive license to exercise all rights associated with the 8 | # contents of this file for any purpose whatsoever. 9 | # No rights are reserved. 10 | # 11 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 12 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 13 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 15 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 17 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | # SOFTWARE. 19 | # =================================================================== 20 | 21 | """SHA-512 cryptographic hash algorithm. 22 | 23 | SHA-512 belongs to the SHA-2_ family of cryptographic hashes. 24 | It produces the 512 bit digest of a message. 25 | 26 | >>> from Crypto.Hash import SHA512 27 | >>> 28 | >>> h = SHA512.new() 29 | >>> h.update(b'Hello') 30 | >>> print h.hexdigest() 31 | 32 | *SHA* stands for Secure Hash Algorithm. 33 | 34 | .. _SHA-2: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf 35 | """ 36 | 37 | _revision__ = "$Id$" 38 | 39 | __all__ = ['new', 'digest_size', 'SHA512Hash' ] 40 | 41 | from Crypto.Util.py3compat import * 42 | from Crypto.Hash.hashalgo import HashAlgo 43 | 44 | try: 45 | import hashlib 46 | hashFactory = hashlib.sha512 47 | 48 | except ImportError: 49 | from Crypto.Hash import _SHA512 50 | hashFactory = _SHA512 51 | 52 | class SHA512Hash(HashAlgo): 53 | """Class that implements a SHA-512 hash 54 | 55 | :undocumented: block_size 56 | """ 57 | 58 | #: ASN.1 Object identifier (OID):: 59 | #: 60 | #: id-sha512 OBJECT IDENTIFIER ::= { 61 | #: joint-iso-itu-t(2) 62 | #: country(16) us(840) organization(1) gov(101) csor(3) nistalgorithm(4) hashalgs(2) 3 63 | #: } 64 | #: 65 | #: This value uniquely identifies the SHA-512 algorithm. 66 | oid = b('\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03') 67 | 68 | digest_size = 64 69 | block_size = 128 70 | 71 | def __init__(self, data=None): 72 | HashAlgo.__init__(self, hashFactory, data) 73 | 74 | def new(self, data=None): 75 | return SHA512Hash(data) 76 | 77 | def new(data=None): 78 | """Return a fresh instance of the hash object. 79 | 80 | :Parameters: 81 | data : byte string 82 | The very first chunk of the message to hash. 83 | It is equivalent to an early call to `SHA512Hash.update()`. 84 | Optional. 85 | 86 | :Return: A `SHA512Hash` object 87 | """ 88 | return SHA512Hash().new(data) 89 | 90 | #: The size of the resulting hash in bytes. 91 | digest_size = SHA512Hash.digest_size 92 | 93 | #: The internal block size of the hash algorithm in bytes. 94 | block_size = SHA512Hash.block_size 95 | 96 | -------------------------------------------------------------------------------- /Crypto/Random/OSRNG/rng_base.py: -------------------------------------------------------------------------------- 1 | # 2 | # Random/OSRNG/rng_base.py : Base class for OSRNG 3 | # 4 | # Written in 2008 by Dwayne C. Litzenberger 5 | # 6 | # =================================================================== 7 | # The contents of this file are dedicated to the public domain. To 8 | # the extent that dedication to the public domain is not available, 9 | # everyone is granted a worldwide, perpetual, royalty-free, 10 | # non-exclusive license to exercise all rights associated with the 11 | # contents of this file for any purpose whatsoever. 12 | # No rights are reserved. 13 | # 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 18 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 19 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | # =================================================================== 23 | 24 | __revision__ = "$Id$" 25 | 26 | import sys 27 | if sys.version_info[0] == 2 and sys.version_info[1] == 1: 28 | from Crypto.Util.py21compat import * 29 | 30 | class BaseRNG(object): 31 | 32 | def __init__(self): 33 | self.closed = False 34 | self._selftest() 35 | 36 | def __del__(self): 37 | self.close() 38 | 39 | def _selftest(self): 40 | # Test that urandom can return data 41 | data = self.read(16) 42 | if len(data) != 16: 43 | raise AssertionError("read truncated") 44 | 45 | # Test that we get different data every time (if we don't, the RNG is 46 | # probably malfunctioning) 47 | data2 = self.read(16) 48 | if data == data2: 49 | raise AssertionError("OS RNG returned duplicate data") 50 | 51 | # PEP 343: Support for the "with" statement 52 | def __enter__(self): 53 | pass 54 | def __exit__(self): 55 | """PEP 343 support""" 56 | self.close() 57 | 58 | def close(self): 59 | if not self.closed: 60 | self._close() 61 | self.closed = True 62 | 63 | def flush(self): 64 | pass 65 | 66 | def read(self, N=-1): 67 | """Return N bytes from the RNG.""" 68 | if self.closed: 69 | raise ValueError("I/O operation on closed file") 70 | if not isinstance(N, (long, int)): 71 | raise TypeError("an integer is required") 72 | if N < 0: 73 | raise ValueError("cannot read to end of infinite stream") 74 | elif N == 0: 75 | return "" 76 | data = self._read(N) 77 | if len(data) != N: 78 | raise AssertionError("%s produced truncated output (requested %d, got %d)" % (self.name, N, len(data))) 79 | return data 80 | 81 | def _close(self): 82 | raise NotImplementedError("child class must implement this") 83 | 84 | def _read(self, N): 85 | raise NotImplementedError("child class must implement this") 86 | 87 | 88 | # vim:set ts=4 sw=4 sts=4 expandtab: 89 | -------------------------------------------------------------------------------- /Crypto/Hash/SHA.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # =================================================================== 4 | # The contents of this file are dedicated to the public domain. To 5 | # the extent that dedication to the public domain is not available, 6 | # everyone is granted a worldwide, perpetual, royalty-free, 7 | # non-exclusive license to exercise all rights associated with the 8 | # contents of this file for any purpose whatsoever. 9 | # No rights are reserved. 10 | # 11 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 12 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 13 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 15 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 17 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | # SOFTWARE. 19 | # =================================================================== 20 | 21 | """SHA-1 cryptographic hash algorithm. 22 | 23 | SHA-1_ produces the 160 bit digest of a message. 24 | 25 | >>> from Crypto.Hash import SHA 26 | >>> 27 | >>> h = SHA.new() 28 | >>> h.update(b'Hello') 29 | >>> print h.hexdigest() 30 | 31 | *SHA* stands for Secure Hash Algorithm. 32 | 33 | This algorithm is not considered secure. Do not use it for new designs. 34 | 35 | .. _SHA-1: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf 36 | """ 37 | 38 | _revision__ = "$Id$" 39 | 40 | __all__ = ['new', 'digest_size', 'SHA1Hash' ] 41 | 42 | from Crypto.Util.py3compat import * 43 | from Crypto.Hash.hashalgo import HashAlgo 44 | 45 | try: 46 | # The sha module is deprecated in Python 2.6, so use hashlib when possible. 47 | import hashlib 48 | hashFactory = hashlib.sha1 49 | 50 | except ImportError: 51 | import sha 52 | hashFactory = sha 53 | 54 | class SHA1Hash(HashAlgo): 55 | """Class that implements a SHA-1 hash 56 | 57 | :undocumented: block_size 58 | """ 59 | 60 | #: ASN.1 Object identifier (OID):: 61 | #: 62 | #: id-sha1 OBJECT IDENTIFIER ::= { 63 | #: iso(1) identified-organization(3) oiw(14) secsig(3) 64 | #: algorithms(2) 26 65 | #: } 66 | #: 67 | #: This value uniquely identifies the SHA-1 algorithm. 68 | oid = b('\x06\x05\x2b\x0e\x03\x02\x1a') 69 | 70 | digest_size = 20 71 | block_size = 64 72 | 73 | def __init__(self, data=None): 74 | HashAlgo.__init__(self, hashFactory, data) 75 | 76 | def new(self, data=None): 77 | return SHA1Hash(data) 78 | 79 | def new(data=None): 80 | """Return a fresh instance of the hash object. 81 | 82 | :Parameters: 83 | data : byte string 84 | The very first chunk of the message to hash. 85 | It is equivalent to an early call to `SHA1Hash.update()`. 86 | Optional. 87 | 88 | :Return: A `SHA1Hash` object 89 | """ 90 | return SHA1Hash().new(data) 91 | 92 | #: The size of the resulting hash in bytes. 93 | digest_size = SHA1Hash.digest_size 94 | 95 | #: The internal block size of the hash algorithm in bytes. 96 | block_size = SHA1Hash.block_size 97 | 98 | 99 | -------------------------------------------------------------------------------- /Crypto/Hash/SHA256.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # =================================================================== 4 | # The contents of this file are dedicated to the public domain. To 5 | # the extent that dedication to the public domain is not available, 6 | # everyone is granted a worldwide, perpetual, royalty-free, 7 | # non-exclusive license to exercise all rights associated with the 8 | # contents of this file for any purpose whatsoever. 9 | # No rights are reserved. 10 | # 11 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 12 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 13 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 15 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 17 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | # SOFTWARE. 19 | # =================================================================== 20 | 21 | """SHA-256 cryptographic hash algorithm. 22 | 23 | SHA-256 belongs to the SHA-2_ family of cryptographic hashes. 24 | It produces the 256 bit digest of a message. 25 | 26 | >>> from Crypto.Hash import SHA256 27 | >>> 28 | >>> h = SHA256.new() 29 | >>> h.update(b'Hello') 30 | >>> print h.hexdigest() 31 | 32 | *SHA* stands for Secure Hash Algorithm. 33 | 34 | .. _SHA-2: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf 35 | """ 36 | 37 | _revision__ = "$Id$" 38 | 39 | __all__ = ['new', 'digest_size', 'SHA256Hash' ] 40 | 41 | from Crypto.Util.py3compat import * 42 | from Crypto.Hash.hashalgo import HashAlgo 43 | 44 | try: 45 | import hashlib 46 | hashFactory = hashlib.sha256 47 | 48 | except ImportError: 49 | from Crypto.Hash import _SHA256 50 | hashFactory = _SHA256 51 | 52 | class SHA256Hash(HashAlgo): 53 | """Class that implements a SHA-256 hash 54 | 55 | :undocumented: block_size 56 | """ 57 | 58 | #: ASN.1 Object identifier (OID):: 59 | #: 60 | #: id-sha256 OBJECT IDENTIFIER ::= { 61 | #: joint-iso-itu-t(2) country(16) us(840) organization(1) 62 | #: gov(101) csor(3) nistalgorithm(4) hashalgs(2) 1 63 | #: } 64 | #: 65 | #: This value uniquely identifies the SHA-256 algorithm. 66 | oid = b('\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01') 67 | 68 | digest_size = 32 69 | block_size = 64 70 | 71 | def __init__(self, data=None): 72 | HashAlgo.__init__(self, hashFactory, data) 73 | 74 | def new(self, data=None): 75 | return SHA256Hash(data) 76 | 77 | def new(data=None): 78 | """Return a fresh instance of the hash object. 79 | 80 | :Parameters: 81 | data : byte string 82 | The very first chunk of the message to hash. 83 | It is equivalent to an early call to `SHA256Hash.update()`. 84 | Optional. 85 | 86 | :Return: A `SHA256Hash` object 87 | """ 88 | return SHA256Hash().new(data) 89 | 90 | #: The size of the resulting hash in bytes. 91 | digest_size = SHA256Hash.digest_size 92 | 93 | #: The internal block size of the hash algorithm in bytes. 94 | block_size = SHA256Hash.block_size 95 | 96 | -------------------------------------------------------------------------------- /Crypto/Hash/SHA384.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # =================================================================== 4 | # The contents of this file are dedicated to the public domain. To 5 | # the extent that dedication to the public domain is not available, 6 | # everyone is granted a worldwide, perpetual, royalty-free, 7 | # non-exclusive license to exercise all rights associated with the 8 | # contents of this file for any purpose whatsoever. 9 | # No rights are reserved. 10 | # 11 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 12 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 13 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 15 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 17 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | # SOFTWARE. 19 | # =================================================================== 20 | 21 | """SHA-384 cryptographic hash algorithm. 22 | 23 | SHA-384 belongs to the SHA-2_ family of cryptographic hashes. 24 | It produces the 384 bit digest of a message. 25 | 26 | >>> from Crypto.Hash import SHA384 27 | >>> 28 | >>> h = SHA384.new() 29 | >>> h.update(b'Hello') 30 | >>> print h.hexdigest() 31 | 32 | *SHA* stands for Secure Hash Algorithm. 33 | 34 | .. _SHA-2: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf 35 | """ 36 | 37 | _revision__ = "$Id$" 38 | 39 | __all__ = ['new', 'digest_size', 'SHA384Hash' ] 40 | 41 | from Crypto.Util.py3compat import * 42 | from Crypto.Hash.hashalgo import HashAlgo 43 | 44 | try: 45 | import hashlib 46 | hashFactory = hashlib.sha384 47 | 48 | except ImportError: 49 | from Crypto.Hash import _SHA384 50 | hashFactory = _SHA384 51 | 52 | class SHA384Hash(HashAlgo): 53 | """Class that implements a SHA-384 hash 54 | 55 | :undocumented: block_size 56 | """ 57 | 58 | #: ASN.1 Object identifier (OID):: 59 | #: 60 | #: id-sha384 OBJECT IDENTIFIER ::= { 61 | #: joint-iso-itu-t(2) country(16) us(840) organization(1) gov(101) csor(3) 62 | #: nistalgorithm(4) hashalgs(2) 2 63 | #: } 64 | #: 65 | #: This value uniquely identifies the SHA-384 algorithm. 66 | oid = b('\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02') 67 | 68 | digest_size = 48 69 | block_size = 128 70 | 71 | def __init__(self, data=None): 72 | HashAlgo.__init__(self, hashFactory, data) 73 | 74 | def new(self, data=None): 75 | return SHA384Hash(data) 76 | 77 | def new(data=None): 78 | """Return a fresh instance of the hash object. 79 | 80 | :Parameters: 81 | data : byte string 82 | The very first chunk of the message to hash. 83 | It is equivalent to an early call to `SHA384Hash.update()`. 84 | Optional. 85 | 86 | :Return: A `SHA384Hash` object 87 | """ 88 | return SHA384Hash().new(data) 89 | 90 | #: The size of the resulting hash in bytes. 91 | digest_size = SHA384Hash.digest_size 92 | 93 | #: The internal block size of the hash algorithm in bytes. 94 | block_size = SHA384Hash.block_size 95 | 96 | 97 | -------------------------------------------------------------------------------- /Crypto/Hash/MD5.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # =================================================================== 4 | # The contents of this file are dedicated to the public domain. To 5 | # the extent that dedication to the public domain is not available, 6 | # everyone is granted a worldwide, perpetual, royalty-free, 7 | # non-exclusive license to exercise all rights associated with the 8 | # contents of this file for any purpose whatsoever. 9 | # No rights are reserved. 10 | # 11 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 12 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 13 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 15 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 17 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | # SOFTWARE. 19 | # =================================================================== 20 | 21 | """MD5 cryptographic hash algorithm. 22 | 23 | MD5 is specified in RFC1321_ and produces the 128 bit digest of a message. 24 | 25 | >>> from Crypto.Hash import MD5 26 | >>> 27 | >>> h = MD5.new() 28 | >>> h.update(b'Hello') 29 | >>> print h.hexdigest() 30 | 31 | MD5 stand for Message Digest version 5, and it was invented by Rivest in 1991. 32 | 33 | This algorithm is insecure. Do not use it for new designs. 34 | 35 | .. _RFC1321: http://tools.ietf.org/html/rfc1321 36 | """ 37 | 38 | _revision__ = "$Id$" 39 | 40 | __all__ = ['new', 'digest_size', 'MD5Hash' ] 41 | 42 | from Crypto.Util.py3compat import * 43 | from Crypto.Hash.hashalgo import HashAlgo 44 | 45 | try: 46 | # The md5 module is deprecated in Python 2.6, so use hashlib when possible. 47 | import hashlib 48 | hashFactory = hashlib.md5 49 | 50 | except ImportError: 51 | import md5 52 | hashFactory = md5 53 | 54 | class MD5Hash(HashAlgo): 55 | """Class that implements an MD5 hash 56 | 57 | :undocumented: block_size 58 | """ 59 | 60 | #: ASN.1 Object identifier (OID):: 61 | #: 62 | #: id-md5 OBJECT IDENTIFIER ::= { 63 | #: iso(1) member-body(2) us(840) rsadsi(113549) 64 | #: digestAlgorithm(2) 5 65 | #: } 66 | #: 67 | #: This value uniquely identifies the MD5 algorithm. 68 | oid = b('\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x05') 69 | 70 | digest_size = 16 71 | block_size = 64 72 | 73 | def __init__(self, data=None): 74 | HashAlgo.__init__(self, hashFactory, data) 75 | 76 | def new(self, data=None): 77 | return MD5Hash(data) 78 | 79 | def new(data=None): 80 | """Return a fresh instance of the hash object. 81 | 82 | :Parameters: 83 | data : byte string 84 | The very first chunk of the message to hash. 85 | It is equivalent to an early call to `MD5Hash.update()`. 86 | Optional. 87 | 88 | :Return: A `MD5Hash` object 89 | """ 90 | return MD5Hash().new(data) 91 | 92 | #: The size of the resulting hash in bytes. 93 | digest_size = MD5Hash.digest_size 94 | 95 | #: The internal block size of the hash algorithm in bytes. 96 | block_size = MD5Hash.block_size 97 | 98 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Protocol/test_chaffing.py: -------------------------------------------------------------------------------- 1 | # 2 | # Test script for Crypto.Protocol.Chaffing 3 | # 4 | # Part of the Python Cryptography Toolkit 5 | # 6 | # Written by Andrew Kuchling and others 7 | # 8 | # =================================================================== 9 | # The contents of this file are dedicated to the public domain. To 10 | # the extent that dedication to the public domain is not available, 11 | # everyone is granted a worldwide, perpetual, royalty-free, 12 | # non-exclusive license to exercise all rights associated with the 13 | # contents of this file for any purpose whatsoever. 14 | # No rights are reserved. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | # SOFTWARE. 24 | # =================================================================== 25 | 26 | __revision__ = "$Id$" 27 | 28 | import unittest 29 | from Crypto.Protocol import Chaffing 30 | 31 | text = """\ 32 | When in the Course of human events, it becomes necessary for one people to 33 | dissolve the political bands which have connected them with another, and to 34 | assume among the powers of the earth, the separate and equal station to which 35 | the Laws of Nature and of Nature's God entitle them, a decent respect to the 36 | opinions of mankind requires that they should declare the causes which impel 37 | them to the separation. 38 | 39 | We hold these truths to be self-evident, that all men are created equal, that 40 | they are endowed by their Creator with certain unalienable Rights, that among 41 | these are Life, Liberty, and the pursuit of Happiness. That to secure these 42 | rights, Governments are instituted among Men, deriving their just powers from 43 | the consent of the governed. That whenever any Form of Government becomes 44 | destructive of these ends, it is the Right of the People to alter or to 45 | abolish it, and to institute new Government, laying its foundation on such 46 | principles and organizing its powers in such form, as to them shall seem most 47 | likely to effect their Safety and Happiness. 48 | """ 49 | 50 | class ChaffingTest (unittest.TestCase): 51 | 52 | def runTest(self): 53 | "Simple tests of chaffing and winnowing" 54 | # Test constructors 55 | Chaffing.Chaff() 56 | Chaffing.Chaff(0.5, 1) 57 | self.assertRaises(ValueError, Chaffing.Chaff, factor=-1) 58 | self.assertRaises(ValueError, Chaffing.Chaff, blocksper=-1) 59 | 60 | data = [(1, 'data1', 'data1'), (2, 'data2', 'data2')] 61 | c = Chaffing.Chaff(1.0, 1) 62 | c.chaff(data) 63 | chaff = c.chaff(data) 64 | self.assertEqual(len(chaff), 4) 65 | 66 | c = Chaffing.Chaff(0.0, 1) 67 | chaff = c.chaff(data) 68 | self.assertEqual(len(chaff), 2) 69 | 70 | def get_tests(config={}): 71 | return [ChaffingTest()] 72 | 73 | if __name__ == "__main__": 74 | unittest.main() 75 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Protocol/test_AllOrNothing.py: -------------------------------------------------------------------------------- 1 | # 2 | # Test script for Crypto.Protocol.AllOrNothing 3 | # 4 | # Part of the Python Cryptography Toolkit 5 | # 6 | # Written by Andrew Kuchling and others 7 | # 8 | # =================================================================== 9 | # The contents of this file are dedicated to the public domain. To 10 | # the extent that dedication to the public domain is not available, 11 | # everyone is granted a worldwide, perpetual, royalty-free, 12 | # non-exclusive license to exercise all rights associated with the 13 | # contents of this file for any purpose whatsoever. 14 | # No rights are reserved. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | # SOFTWARE. 24 | # =================================================================== 25 | 26 | __revision__ = "$Id$" 27 | 28 | import unittest 29 | from Crypto.Protocol import AllOrNothing 30 | from Crypto.Util.py3compat import * 31 | 32 | text = b("""\ 33 | When in the Course of human events, it becomes necessary for one people to 34 | dissolve the political bands which have connected them with another, and to 35 | assume among the powers of the earth, the separate and equal station to which 36 | the Laws of Nature and of Nature's God entitle them, a decent respect to the 37 | opinions of mankind requires that they should declare the causes which impel 38 | them to the separation. 39 | 40 | We hold these truths to be self-evident, that all men are created equal, that 41 | they are endowed by their Creator with certain unalienable Rights, that among 42 | these are Life, Liberty, and the pursuit of Happiness. That to secure these 43 | rights, Governments are instituted among Men, deriving their just powers from 44 | the consent of the governed. That whenever any Form of Government becomes 45 | destructive of these ends, it is the Right of the People to alter or to 46 | abolish it, and to institute new Government, laying its foundation on such 47 | principles and organizing its powers in such form, as to them shall seem most 48 | likely to effect their Safety and Happiness. 49 | """) 50 | 51 | class AllOrNothingTest (unittest.TestCase): 52 | 53 | def runTest(self): 54 | "Simple test of AllOrNothing" 55 | 56 | from Crypto.Cipher import AES 57 | import base64 58 | 59 | # The current AllOrNothing will fail 60 | # every so often. Repeat the test 61 | # several times to force this. 62 | for i in range(50): 63 | x = AllOrNothing.AllOrNothing(AES) 64 | 65 | msgblocks = x.digest(text) 66 | 67 | # get a new undigest-only object so there's no leakage 68 | y = AllOrNothing.AllOrNothing(AES) 69 | text2 = y.undigest(msgblocks) 70 | self.assertEqual(text, text2) 71 | 72 | def get_tests(config={}): 73 | return [AllOrNothingTest()] 74 | 75 | if __name__ == "__main__": 76 | unittest.main() 77 | -------------------------------------------------------------------------------- /Crypto/Hash/RIPEMD.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # =================================================================== 4 | # The contents of this file are dedicated to the public domain. To 5 | # the extent that dedication to the public domain is not available, 6 | # everyone is granted a worldwide, perpetual, royalty-free, 7 | # non-exclusive license to exercise all rights associated with the 8 | # contents of this file for any purpose whatsoever. 9 | # No rights are reserved. 10 | # 11 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 12 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 13 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 15 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 17 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | # SOFTWARE. 19 | # =================================================================== 20 | 21 | """RIPEMD-160 cryptographic hash algorithm. 22 | 23 | RIPEMD-160_ produces the 160 bit digest of a message. 24 | 25 | >>> from Crypto.Hash import RIPEMD 26 | >>> 27 | >>> h = RIPEMD.new() 28 | >>> h.update(b'Hello') 29 | >>> print h.hexdigest() 30 | 31 | RIPEMD-160 stands for RACE Integrity Primitives Evaluation Message Digest 32 | with a 160 bit digest. It was invented by Dobbertin, Bosselaers, and Preneel. 33 | 34 | This algorithm is considered secure, although it has not been scrutinized as 35 | extensively as SHA-1. Moreover, it provides an informal security level of just 36 | 80bits. 37 | 38 | .. _RIPEMD-160: http://homes.esat.kuleuven.be/~bosselae/ripemd160.html 39 | """ 40 | 41 | _revision__ = "$Id$" 42 | 43 | __all__ = ['new', 'digest_size', 'RIPEMD160Hash' ] 44 | 45 | from Crypto.Util.py3compat import * 46 | from Crypto.Hash.hashalgo import HashAlgo 47 | 48 | import Crypto.Hash._RIPEMD160 as _RIPEMD160 49 | hashFactory = _RIPEMD160 50 | 51 | class RIPEMD160Hash(HashAlgo): 52 | """Class that implements a RIPMD-160 hash 53 | 54 | :undocumented: block_size 55 | """ 56 | 57 | #: ASN.1 Object identifier (OID):: 58 | #: 59 | #: id-ripemd160 OBJECT IDENTIFIER ::= { 60 | #: iso(1) identified-organization(3) teletrust(36) 61 | #: algorithm(3) hashAlgorithm(2) ripemd160(1) 62 | #: } 63 | #: 64 | #: This value uniquely identifies the RIPMD-160 algorithm. 65 | oid = b("\x06\x05\x2b\x24\x03\x02\x01") 66 | 67 | digest_size = 20 68 | block_size = 64 69 | 70 | def __init__(self, data=None): 71 | HashAlgo.__init__(self, hashFactory, data) 72 | 73 | def new(self, data=None): 74 | return RIPEMD160Hash(data) 75 | 76 | def new(data=None): 77 | """Return a fresh instance of the hash object. 78 | 79 | :Parameters: 80 | data : byte string 81 | The very first chunk of the message to hash. 82 | It is equivalent to an early call to `RIPEMD160Hash.update()`. 83 | Optional. 84 | 85 | :Return: A `RIPEMD160Hash` object 86 | """ 87 | return RIPEMD160Hash().new(data) 88 | 89 | #: The size of the resulting hash in bytes. 90 | digest_size = RIPEMD160Hash.digest_size 91 | 92 | #: The internal block size of the hash algorithm in bytes. 93 | block_size = RIPEMD160Hash.block_size 94 | 95 | -------------------------------------------------------------------------------- /Crypto/Random/Fortuna/SHAd256.py: -------------------------------------------------------------------------------- 1 | # -*- coding: ascii -*- 2 | # 3 | # Random/Fortuna/SHAd256.py : SHA_d-256 hash function implementation 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """\ 26 | SHA_d-256 hash function implementation. 27 | 28 | This module should comply with PEP 247. 29 | """ 30 | 31 | __revision__ = "$Id$" 32 | __all__ = ['new', 'digest_size'] 33 | 34 | import sys 35 | if sys.version_info[0] == 2 and sys.version_info[1] == 1: 36 | from Crypto.Util.py21compat import * 37 | from Crypto.Util.py3compat import * 38 | 39 | from binascii import b2a_hex 40 | 41 | from Crypto.Hash import SHA256 42 | 43 | assert SHA256.digest_size == 32 44 | 45 | class _SHAd256(object): 46 | """SHA-256, doubled. 47 | 48 | Returns SHA-256(SHA-256(data)). 49 | """ 50 | 51 | digest_size = SHA256.digest_size 52 | 53 | _internal = object() 54 | 55 | def __init__(self, internal_api_check, sha256_hash_obj): 56 | if internal_api_check is not self._internal: 57 | raise AssertionError("Do not instantiate this class directly. Use %s.new()" % (__name__,)) 58 | self._h = sha256_hash_obj 59 | 60 | # PEP 247 "copy" method 61 | def copy(self): 62 | """Return a copy of this hashing object""" 63 | return _SHAd256(SHAd256._internal, self._h.copy()) 64 | 65 | # PEP 247 "digest" method 66 | def digest(self): 67 | """Return the hash value of this object as a binary string""" 68 | retval = SHA256.new(self._h.digest()).digest() 69 | assert len(retval) == 32 70 | return retval 71 | 72 | # PEP 247 "hexdigest" method 73 | def hexdigest(self): 74 | """Return the hash value of this object as a (lowercase) hexadecimal string""" 75 | retval = b2a_hex(self.digest()) 76 | assert len(retval) == 64 77 | if sys.version_info[0] == 2: 78 | return retval 79 | else: 80 | return retval.decode() 81 | 82 | # PEP 247 "update" method 83 | def update(self, data): 84 | self._h.update(data) 85 | 86 | # PEP 247 module-level "digest_size" variable 87 | digest_size = _SHAd256.digest_size 88 | 89 | # PEP 247 module-level "new" function 90 | def new(data=None): 91 | """Return a new SHAd256 hashing object""" 92 | if not data: 93 | data=b("") 94 | sha = _SHAd256(_SHAd256._internal, SHA256.new(data)) 95 | sha.new = globals()['new'] 96 | return sha 97 | 98 | # vim:set ts=4 sw=4 sts=4 expandtab: 99 | -------------------------------------------------------------------------------- /Crypto/Cipher/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # =================================================================== 4 | # The contents of this file are dedicated to the public domain. To 5 | # the extent that dedication to the public domain is not available, 6 | # everyone is granted a worldwide, perpetual, royalty-free, 7 | # non-exclusive license to exercise all rights associated with the 8 | # contents of this file for any purpose whatsoever. 9 | # No rights are reserved. 10 | # 11 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 12 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 13 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 14 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 15 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 17 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | # SOFTWARE. 19 | # =================================================================== 20 | 21 | """Symmetric- and asymmetric-key encryption algorithms. 22 | 23 | Encryption algorithms transform plaintext in some way that 24 | is dependent on a key or key pair, producing ciphertext. 25 | 26 | Symmetric algorithms 27 | -------------------- 28 | 29 | Encryption can easily be reversed, if (and, hopefully, only if) 30 | one knows the same key. 31 | In other words, sender and receiver share the same key. 32 | 33 | The symmetric encryption modules here all support the interface described in PEP 34 | 272, "API for Block Encryption Algorithms". 35 | 36 | If you don't know which algorithm to choose, use AES because it's 37 | standard and has undergone a fair bit of examination. 38 | 39 | ======================== ======= ======================== 40 | Module name Type Description 41 | ======================== ======= ======================== 42 | `Crypto.Cipher.AES` Block Advanced Encryption Standard 43 | `Crypto.Cipher.ARC2` Block Alleged RC2 44 | `Crypto.Cipher.ARC4` Stream Alleged RC4 45 | `Crypto.Cipher.Blowfish` Block Blowfish 46 | `Crypto.Cipher.CAST` Block CAST 47 | `Crypto.Cipher.DES` Block The Data Encryption Standard. 48 | Very commonly used in the past, 49 | but today its 56-bit keys are too small. 50 | `Crypto.Cipher.DES3` Block Triple DES. 51 | `Crypto.Cipher.XOR` Stream The simple XOR cipher. 52 | ======================== ======= ======================== 53 | 54 | 55 | Asymmetric algorithms 56 | --------------------- 57 | 58 | For asymmetric algorithms, the key to be used for decryption is totally 59 | different and cannot be derived in a feasible way from the key used 60 | for encryption. Put differently, sender and receiver each own one half 61 | of a key pair. The encryption key is often called ``public`` whereas 62 | the decryption key is called ``private``. 63 | 64 | ========================== ======================= 65 | Module name Description 66 | ========================== ======================= 67 | `Crypto.Cipher.PKCS1_v1_5` PKCS#1 v1.5 encryption, based on RSA key pairs 68 | `Crypto.Cipher.PKCS1_OAEP` PKCS#1 OAEP encryption, based on RSA key pairs 69 | ========================== ======================= 70 | 71 | :undocumented: __revision__, __package__, _AES, _ARC2, _ARC4, _Blowfish 72 | _CAST, _DES, _DES3, _XOR 73 | """ 74 | 75 | __all__ = ['AES', 'ARC2', 'ARC4', 76 | 'Blowfish', 'CAST', 'DES', 'DES3', 77 | 'XOR', 78 | 'PKCS1_v1_5', 'PKCS1_OAEP' 79 | ] 80 | 81 | __revision__ = "$Id$" 82 | 83 | 84 | -------------------------------------------------------------------------------- /Crypto/SelfTest/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/__init__.py: Self-test for PyCrypto 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self tests 26 | 27 | These tests should perform quickly and can ideally be used every time an 28 | application runs. 29 | """ 30 | 31 | __revision__ = "$Id$" 32 | 33 | import sys 34 | import unittest 35 | from StringIO import StringIO 36 | 37 | class SelfTestError(Exception): 38 | def __init__(self, message, result): 39 | Exception.__init__(self, message, result) 40 | self.message = message 41 | self.result = result 42 | 43 | def run(module=None, verbosity=0, stream=None, tests=None, config=None, **kwargs): 44 | """Execute self-tests. 45 | 46 | This raises SelfTestError if any test is unsuccessful. 47 | 48 | You may optionally pass in a sub-module of SelfTest if you only want to 49 | perform some of the tests. For example, the following would test only the 50 | hash modules: 51 | 52 | Crypto.SelfTest.run(Crypto.SelfTest.Hash) 53 | 54 | """ 55 | if config is None: 56 | config = {} 57 | suite = unittest.TestSuite() 58 | if module is None: 59 | if tests is None: 60 | tests = get_tests(config=config) 61 | suite.addTests(tests) 62 | else: 63 | if tests is None: 64 | suite.addTests(module.get_tests(config=config)) 65 | else: 66 | raise ValueError("'module' and 'tests' arguments are mutually exclusive") 67 | if stream is None: 68 | kwargs['stream'] = StringIO() 69 | runner = unittest.TextTestRunner(verbosity=verbosity, **kwargs) 70 | result = runner.run(suite) 71 | if not result.wasSuccessful(): 72 | if stream is None: 73 | sys.stderr.write(stream.getvalue()) 74 | raise SelfTestError("Self-test failed", result) 75 | return result 76 | 77 | def get_tests(config={}): 78 | tests = [] 79 | from Crypto.SelfTest import Cipher; tests += Cipher.get_tests(config=config) 80 | from Crypto.SelfTest import Hash; tests += Hash.get_tests(config=config) 81 | from Crypto.SelfTest import Protocol; tests += Protocol.get_tests(config=config) 82 | from Crypto.SelfTest import PublicKey; tests += PublicKey.get_tests(config=config) 83 | from Crypto.SelfTest import Random; tests += Random.get_tests(config=config) 84 | from Crypto.SelfTest import Util; tests += Util.get_tests(config=config) 85 | from Crypto.SelfTest import Signature; tests += Signature.get_tests(config=config) 86 | return tests 87 | 88 | if __name__ == '__main__': 89 | suite = lambda: unittest.TestSuite(get_tests()) 90 | unittest.main(defaultTest='suite') 91 | 92 | # vim:set ts=4 sw=4 sts=4 expandtab: 93 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Random/Fortuna/test_FortunaGenerator.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Random/Fortuna/test_FortunaGenerator.py: Self-test for the FortunaGenerator module 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-tests for Crypto.Random.Fortuna.FortunaGenerator""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | import sys 30 | if sys.version_info[0] == 2 and sys.version_info[1] == 1: 31 | from Crypto.Util.py21compat import * 32 | from Crypto.Util.py3compat import * 33 | 34 | import unittest 35 | from binascii import b2a_hex 36 | 37 | class FortunaGeneratorTests(unittest.TestCase): 38 | def setUp(self): 39 | global FortunaGenerator 40 | from Crypto.Random.Fortuna import FortunaGenerator 41 | 42 | def test_generator(self): 43 | """FortunaGenerator.AESGenerator""" 44 | fg = FortunaGenerator.AESGenerator() 45 | 46 | # We shouldn't be able to read data until we've seeded the generator 47 | self.assertRaises(Exception, fg.pseudo_random_data, 1) 48 | self.assertEqual(0, fg.counter.next_value()) 49 | 50 | # Seed the generator, which should set the key and increment the counter. 51 | fg.reseed(b("Hello")) 52 | self.assertEqual(b("0ea6919d4361551364242a4ba890f8f073676e82cf1a52bb880f7e496648b565"), b2a_hex(fg.key)) 53 | self.assertEqual(1, fg.counter.next_value()) 54 | 55 | # Read 2 full blocks from the generator 56 | self.assertEqual(b("7cbe2c17684ac223d08969ee8b565616") + # counter=1 57 | b("717661c0d2f4758bd6ba140bf3791abd"), # counter=2 58 | b2a_hex(fg.pseudo_random_data(32))) 59 | 60 | # Meanwhile, the generator will have re-keyed itself and incremented its counter 61 | self.assertEqual(b("33a1bb21987859caf2bbfc5615bef56d") + # counter=3 62 | b("e6b71ff9f37112d0c193a135160862b7"), # counter=4 63 | b2a_hex(fg.key)) 64 | self.assertEqual(5, fg.counter.next_value()) 65 | 66 | # Read another 2 blocks from the generator 67 | self.assertEqual(b("fd6648ba3086e919cee34904ef09a7ff") + # counter=5 68 | b("021f77580558b8c3e9248275f23042bf"), # counter=6 69 | b2a_hex(fg.pseudo_random_data(32))) 70 | 71 | 72 | # Try to read more than 2**20 bytes using the internal function. This should fail. 73 | self.assertRaises(AssertionError, fg._pseudo_random_data, 2**20+1) 74 | 75 | def get_tests(config={}): 76 | from Crypto.SelfTest.st_common import list_test_cases 77 | return list_test_cases(FortunaGeneratorTests) 78 | 79 | if __name__ == '__main__': 80 | suite = lambda: unittest.TestSuite(get_tests()) 81 | unittest.main(defaultTest='suite') 82 | 83 | # vim:set ts=4 sw=4 sts=4 expandtab: 84 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Cipher/test_ARC4.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Cipher/ARC4.py: Self-test for the Alleged-RC4 cipher 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Cipher.ARC4""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | from Crypto.Util.py3compat import * 30 | 31 | # This is a list of (plaintext, ciphertext, key[, description]) tuples. 32 | test_data = [ 33 | # Test vectors from Eric Rescorla's message with the subject 34 | # "RC4 compatibility testing", sent to the cipherpunks mailing list on 35 | # September 13, 1994. 36 | # http://cypherpunks.venona.com/date/1994/09/msg00420.html 37 | 38 | ('0123456789abcdef', '75b7878099e0c596', '0123456789abcdef', 39 | 'Test vector 0'), 40 | 41 | ('0000000000000000', '7494c2e7104b0879', '0123456789abcdef', 42 | 'Test vector 1'), 43 | 44 | ('0000000000000000', 'de188941a3375d3a', '0000000000000000', 45 | 'Test vector 2'), 46 | 47 | ('00000000000000000000', 'd6a141a7ec3c38dfbd61', 'ef012345', 48 | 'Test vector 3'), 49 | 50 | ('01' * 512, 51 | '7595c3e6114a09780c4ad452338e1ffd9a1be9498f813d76533449b6778dcad8' 52 | + 'c78a8d2ba9ac66085d0e53d59c26c2d1c490c1ebbe0ce66d1b6b1b13b6b919b8' 53 | + '47c25a91447a95e75e4ef16779cde8bf0a95850e32af9689444fd377108f98fd' 54 | + 'cbd4e726567500990bcc7e0ca3c4aaa304a387d20f3b8fbbcd42a1bd311d7a43' 55 | + '03dda5ab078896ae80c18b0af66dff319616eb784e495ad2ce90d7f772a81747' 56 | + 'b65f62093b1e0db9e5ba532fafec47508323e671327df9444432cb7367cec82f' 57 | + '5d44c0d00b67d650a075cd4b70dedd77eb9b10231b6b5b741347396d62897421' 58 | + 'd43df9b42e446e358e9c11a9b2184ecbef0cd8e7a877ef968f1390ec9b3d35a5' 59 | + '585cb009290e2fcde7b5ec66d9084be44055a619d9dd7fc3166f9487f7cb2729' 60 | + '12426445998514c15d53a18c864ce3a2b7555793988126520eacf2e3066e230c' 61 | + '91bee4dd5304f5fd0405b35bd99c73135d3d9bc335ee049ef69b3867bf2d7bd1' 62 | + 'eaa595d8bfc0066ff8d31509eb0c6caa006c807a623ef84c3d33c195d23ee320' 63 | + 'c40de0558157c822d4b8c569d849aed59d4e0fd7f379586b4b7ff684ed6a189f' 64 | + '7486d49b9c4bad9ba24b96abf924372c8a8fffb10d55354900a77a3db5f205e1' 65 | + 'b99fcd8660863a159ad4abe40fa48934163ddde542a6585540fd683cbfd8c00f' 66 | + '12129a284deacc4cdefe58be7137541c047126c8d49e2755ab181ab7e940b0c0', 67 | '0123456789abcdef', 68 | "Test vector 4"), 69 | ] 70 | 71 | def get_tests(config={}): 72 | from Crypto.Cipher import ARC4 73 | from common import make_stream_tests 74 | return make_stream_tests(ARC4, "ARC4", test_data) 75 | 76 | if __name__ == '__main__': 77 | import unittest 78 | suite = lambda: unittest.TestSuite(get_tests()) 79 | unittest.main(defaultTest='suite') 80 | 81 | # vim:set ts=4 sw=4 sts=4 expandtab: 82 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Hash/test_SHA256.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/test_SHA256.py: Self-test for the SHA-256 hash function 4 | # 5 | # Written in 2008 by Dwayne C. Litzenberger 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Self-test suite for Crypto.Hash.SHA256""" 26 | 27 | __revision__ = "$Id$" 28 | 29 | import unittest 30 | from Crypto.Util.py3compat import * 31 | 32 | class LargeSHA256Test(unittest.TestCase): 33 | def runTest(self): 34 | """SHA256: 512/520 MiB test""" 35 | from Crypto.Hash import SHA256 36 | zeros = bchr(0x00) * (1024*1024) 37 | 38 | h = SHA256.new(zeros) 39 | for i in xrange(511): 40 | h.update(zeros) 41 | 42 | # This test vector is from PyCrypto's old testdata.py file. 43 | self.assertEqual('9acca8e8c22201155389f65abbf6bc9723edc7384ead80503839f49dcc56d767', h.hexdigest()) # 512 MiB 44 | 45 | for i in xrange(8): 46 | h.update(zeros) 47 | 48 | # This test vector is from PyCrypto's old testdata.py file. 49 | self.assertEqual('abf51ad954b246009dfe5a50ecd582fd5b8f1b8b27f30393853c3ef721e7fa6e', h.hexdigest()) # 520 MiB 50 | 51 | def get_tests(config={}): 52 | # Test vectors from FIPS PUB 180-2 53 | # This is a list of (expected_result, input[, description]) tuples. 54 | test_data = [ 55 | # FIPS PUB 180-2, B.1 - "One-Block Message" 56 | ('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', 57 | 'abc'), 58 | 59 | # FIPS PUB 180-2, B.2 - "Multi-Block Message" 60 | ('248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1', 61 | 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'), 62 | 63 | # FIPS PUB 180-2, B.3 - "Long Message" 64 | ('cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0', 65 | 'a' * 10**6, 66 | '"a" * 10**6'), 67 | 68 | # Test for an old PyCrypto bug. 69 | ('f7fd017a3c721ce7ff03f3552c0813adcc48b7f33f07e5e2ba71e23ea393d103', 70 | 'This message is precisely 55 bytes long, to test a bug.', 71 | 'Length = 55 (mod 64)'), 72 | 73 | # Example from http://de.wikipedia.org/wiki/Secure_Hash_Algorithm 74 | ('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', ''), 75 | 76 | ('d32b568cd1b96d459e7291ebf4b25d007f275c9f13149beeb782fac0716613f8', 77 | 'Franz jagt im komplett verwahrlosten Taxi quer durch Bayern'), 78 | ] 79 | 80 | from Crypto.Hash import SHA256 81 | from common import make_hash_tests 82 | tests = make_hash_tests(SHA256, "SHA256", test_data, 83 | digest_size=32, 84 | oid="\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01") 85 | 86 | if config.get('slow_tests'): 87 | tests += [LargeSHA256Test()] 88 | 89 | return tests 90 | 91 | if __name__ == '__main__': 92 | import unittest 93 | suite = lambda: unittest.TestSuite(get_tests()) 94 | unittest.main(defaultTest='suite') 95 | 96 | # vim:set ts=4 sw=4 sts=4 expandtab: 97 | -------------------------------------------------------------------------------- /Crypto/PublicKey/_DSA.py: -------------------------------------------------------------------------------- 1 | 2 | # 3 | # DSA.py : Digital Signature Algorithm 4 | # 5 | # Part of the Python Cryptography Toolkit 6 | # 7 | # Written by Andrew Kuchling, Paul Swartz, and others 8 | # 9 | # =================================================================== 10 | # The contents of this file are dedicated to the public domain. To 11 | # the extent that dedication to the public domain is not available, 12 | # everyone is granted a worldwide, perpetual, royalty-free, 13 | # non-exclusive license to exercise all rights associated with the 14 | # contents of this file for any purpose whatsoever. 15 | # No rights are reserved. 16 | # 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | # SOFTWARE. 25 | # =================================================================== 26 | # 27 | 28 | __revision__ = "$Id$" 29 | 30 | from Crypto.PublicKey.pubkey import * 31 | from Crypto.Util import number 32 | from Crypto.Util.number import bytes_to_long, long_to_bytes 33 | from Crypto.Hash import SHA 34 | from Crypto.Util.py3compat import * 35 | 36 | class error (Exception): 37 | pass 38 | 39 | def generateQ(randfunc): 40 | S=randfunc(20) 41 | hash1=SHA.new(S).digest() 42 | hash2=SHA.new(long_to_bytes(bytes_to_long(S)+1)).digest() 43 | q = bignum(0) 44 | for i in range(0,20): 45 | c=bord(hash1[i])^bord(hash2[i]) 46 | if i==0: 47 | c=c | 128 48 | if i==19: 49 | c= c | 1 50 | q=q*256+c 51 | while (not isPrime(q)): 52 | q=q+2 53 | if pow(2,159L) < q < pow(2,160L): 54 | return S, q 55 | raise RuntimeError('Bad q value generated') 56 | 57 | def generate_py(bits, randfunc, progress_func=None): 58 | """generate(bits:int, randfunc:callable, progress_func:callable) 59 | 60 | Generate a DSA key of length 'bits', using 'randfunc' to get 61 | random data and 'progress_func', if present, to display 62 | the progress of the key generation. 63 | """ 64 | 65 | if bits<160: 66 | raise ValueError('Key length < 160 bits') 67 | obj=DSAobj() 68 | # Generate string S and prime q 69 | if progress_func: 70 | progress_func('p,q\n') 71 | while (1): 72 | S, obj.q = generateQ(randfunc) 73 | n=divmod(bits-1, 160)[0] 74 | C, N, V = 0, 2, {} 75 | b=(obj.q >> 5) & 15 76 | powb=pow(bignum(2), b) 77 | powL1=pow(bignum(2), bits-1) 78 | while C<4096: 79 | for k in range(0, n+1): 80 | V[k]=bytes_to_long(SHA.new(S+bstr(N)+bstr(k)).digest()) 81 | W=V[n] % powb 82 | for k in range(n-1, -1, -1): 83 | W=(W<<160L)+V[k] 84 | X=W+powL1 85 | p=X-(X%(2*obj.q)-1) 86 | if powL1<=p and isPrime(p): 87 | break 88 | C, N = C+1, N+n+1 89 | if C<4096: 90 | break 91 | if progress_func: 92 | progress_func('4096 multiples failed\n') 93 | 94 | obj.p = p 95 | power=divmod(p-1, obj.q)[0] 96 | if progress_func: 97 | progress_func('h,g\n') 98 | while (1): 99 | h=bytes_to_long(randfunc(bits)) % (p-1) 100 | g=pow(h, power, p) 101 | if 11: 102 | break 103 | obj.g=g 104 | if progress_func: 105 | progress_func('x,y\n') 106 | while (1): 107 | x=bytes_to_long(randfunc(20)) 108 | if 0 < x < obj.q: 109 | break 110 | obj.x, obj.y = x, pow(g, x, p) 111 | return obj 112 | 113 | class DSAobj: 114 | pass 115 | 116 | -------------------------------------------------------------------------------- /Crypto/SelfTest/Protocol/test_KDF.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Protocol/test_KDF.py: Self-test for key derivation functions 4 | # 5 | # =================================================================== 6 | # The contents of this file are dedicated to the public domain. To 7 | # the extent that dedication to the public domain is not available, 8 | # everyone is granted a worldwide, perpetual, royalty-free, 9 | # non-exclusive license to exercise all rights associated with the 10 | # contents of this file for any purpose whatsoever. 11 | # No rights are reserved. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 14 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 15 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 16 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 17 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 18 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | # SOFTWARE. 21 | # =================================================================== 22 | 23 | __revision__ = "$Id$" 24 | 25 | import unittest 26 | from binascii import unhexlify 27 | 28 | from Crypto.SelfTest.st_common import list_test_cases 29 | from Crypto.Hash import SHA as SHA1,HMAC 30 | 31 | from Crypto.Protocol.KDF import * 32 | 33 | def t2b(t): return unhexlify(b(t)) 34 | 35 | class PBKDF1_Tests(unittest.TestCase): 36 | 37 | # List of tuples with test data. 38 | # Each tuple is made up by: 39 | # Item #0: a pass phrase 40 | # Item #1: salt (8 bytes encoded in hex) 41 | # Item #2: output key length 42 | # Item #3: iterations to use 43 | # Item #4: expected result (encoded in hex) 44 | _testData = ( 45 | # From http://www.di-mgt.com.au/cryptoKDFs.html#examplespbkdf 46 | ("password","78578E5A5D63CB06",16,1000,"DC19847E05C64D2FAF10EBFB4A3D2A20"), 47 | ) 48 | 49 | def test1(self): 50 | v = self._testData[0] 51 | res = PBKDF1(v[0], t2b(v[1]), v[2], v[3], SHA1) 52 | self.assertEqual(res, t2b(v[4])) 53 | 54 | class PBKDF2_Tests(unittest.TestCase): 55 | 56 | # List of tuples with test data. 57 | # Each tuple is made up by: 58 | # Item #0: a pass phrase 59 | # Item #1: salt (encoded in hex) 60 | # Item #2: output key length 61 | # Item #3: iterations to use 62 | # Item #4: expected result (encoded in hex) 63 | _testData = ( 64 | # From http://www.di-mgt.com.au/cryptoKDFs.html#examplespbkdf 65 | ("password","78578E5A5D63CB06",24,2048,"BFDE6BE94DF7E11DD409BCE20A0255EC327CB936FFE93643"), 66 | # From RFC 6050 67 | ("password","73616c74", 20, 1, "0c60c80f961f0e71f3a9b524af6012062fe037a6"), 68 | ("password","73616c74", 20, 2, "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957"), 69 | ("password","73616c74", 20, 4096, "4b007901b765489abead49d926f721d065a429c1"), 70 | ("passwordPASSWORDpassword","73616c7453414c5473616c7453414c5473616c7453414c5473616c7453414c5473616c74", 71 | 25, 4096, "3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038"), 72 | ( 'pass\x00word',"7361006c74",16,4096, "56fa6aa75548099dcc37d7f03425e0c3"), 73 | ) 74 | 75 | def test1(self): 76 | # Test only for HMAC-SHA1 as PRF 77 | 78 | def prf(p,s): 79 | return HMAC.new(p,s,SHA1).digest() 80 | 81 | for i in xrange(len(self._testData)): 82 | v = self._testData[i] 83 | res = PBKDF2(v[0], t2b(v[1]), v[2], v[3]) 84 | res2 = PBKDF2(v[0], t2b(v[1]), v[2], v[3], prf) 85 | self.assertEqual(res, t2b(v[4])) 86 | self.assertEqual(res, res2) 87 | 88 | def get_tests(config={}): 89 | tests = [] 90 | tests += list_test_cases(PBKDF1_Tests) 91 | tests += list_test_cases(PBKDF2_Tests) 92 | return tests 93 | 94 | if __name__ == '__main__': 95 | suite = lambda: unittest.TestSuite(get_tests()) 96 | unittest.main(defaultTest='suite') 97 | 98 | # vim:set ts=4 sw=4 sts=4 99 | -------------------------------------------------------------------------------- /Crypto/Util/py3compat.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Util/py3compat.py : Compatibility code for handling Py3k / Python 2.x 4 | # 5 | # Written in 2010 by Thorsten Behrens 6 | # 7 | # =================================================================== 8 | # The contents of this file are dedicated to the public domain. To 9 | # the extent that dedication to the public domain is not available, 10 | # everyone is granted a worldwide, perpetual, royalty-free, 11 | # non-exclusive license to exercise all rights associated with the 12 | # contents of this file for any purpose whatsoever. 13 | # No rights are reserved. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # =================================================================== 24 | 25 | """Compatibility code for handling string/bytes changes from Python 2.x to Py3k 26 | 27 | In Python 2.x, strings (of type ''str'') contain binary data, including encoded 28 | Unicode text (e.g. UTF-8). The separate type ''unicode'' holds Unicode text. 29 | Unicode literals are specified via the u'...' prefix. Indexing or slicing 30 | either type always produces a string of the same type as the original. 31 | Data read from a file is always of '''str'' type. 32 | 33 | In Python 3.x, strings (type ''str'') may only contain Unicode text. The u'...' 34 | prefix and the ''unicode'' type are now redundant. A new type (called 35 | ''bytes'') has to be used for binary data (including any particular 36 | ''encoding'' of a string). The b'...' prefix allows one to specify a binary 37 | literal. Indexing or slicing a string produces another string. Slicing a byte 38 | string produces another byte string, but the indexing operation produces an 39 | integer. Data read from a file is of '''str'' type if the file was opened in 40 | text mode, or of ''bytes'' type otherwise. 41 | 42 | Since PyCrypto aims at supporting both Python 2.x and 3.x, the following helper 43 | functions are used to keep the rest of the library as independent as possible 44 | from the actual Python version. 45 | 46 | In general, the code should always deal with binary strings, and use integers 47 | instead of 1-byte character strings. 48 | 49 | b(s) 50 | Take a text string literal (with no prefix or with u'...' prefix) and 51 | make a byte string. 52 | bchr(c) 53 | Take an integer and make a 1-character byte string. 54 | bord(c) 55 | Take the result of indexing on a byte string and make an integer. 56 | tobytes(s) 57 | Take a text string, a byte string, or a sequence of character taken from 58 | a byte string and make a byte string. 59 | """ 60 | 61 | __revision__ = "$Id$" 62 | 63 | import sys 64 | 65 | if sys.version_info[0] == 2: 66 | def b(s): 67 | return s 68 | def bchr(s): 69 | return chr(s) 70 | def bstr(s): 71 | return str(s) 72 | def bord(s): 73 | return ord(s) 74 | if sys.version_info[1] == 1: 75 | def tobytes(s): 76 | try: 77 | return s.encode('latin-1') 78 | except: 79 | return ''.join(s) 80 | else: 81 | def tobytes(s): 82 | if isinstance(s, unicode): 83 | return s.encode("latin-1") 84 | else: 85 | return ''.join(s) 86 | else: 87 | def b(s): 88 | return s.encode("latin-1") # utf-8 would cause some side-effects we don't want 89 | def bchr(s): 90 | return bytes([s]) 91 | def bstr(s): 92 | if isinstance(s,str): 93 | return bytes(s,"latin-1") 94 | else: 95 | return bytes(s) 96 | def bord(s): 97 | return s 98 | def tobytes(s): 99 | if isinstance(s,bytes): 100 | return s 101 | else: 102 | if isinstance(s,str): 103 | return s.encode("latin-1") 104 | else: 105 | return bytes(s) 106 | 107 | # vim:set ts=4 sw=4 sts=4 expandtab: 108 | --------------------------------------------------------------------------------