From eb9b6f0a9b716423202fc390672bfb78f9f0eb8d Mon Sep 17 00:00:00 2001 From: clowwindy Date: Wed, 8 Oct 2014 21:03:01 +0800 Subject: [PATCH] does not require M2Crypto anymore --- setup.py | 2 +- shadowsocks/crypto/ctypes_openssl.py | 32 ++++++++++++++++++++----- shadowsocks/crypto/m2.py | 36 ++++++++++++++-------------- shadowsocks/crypto/rc4_md5.py | 16 ++++++++++--- shadowsocks/encrypt.py | 3 ++- 5 files changed, 60 insertions(+), 29 deletions(-) diff --git a/setup.py b/setup.py index 8ddf3c5..8388e3f 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open('README.rst') as f: setup( name="shadowsocks", - version="2.3", + version="2.3.1", license='MIT', description="A fast tunnel proxy that help you get through firewalls", author='clowwindy', diff --git a/shadowsocks/crypto/ctypes_openssl.py b/shadowsocks/crypto/ctypes_openssl.py index de3514c..7821d5b 100644 --- a/shadowsocks/crypto/ctypes_openssl.py +++ b/shadowsocks/crypto/ctypes_openssl.py @@ -35,7 +35,12 @@ def load_openssl(): from ctypes import CDLL, c_char_p, c_int, c_long, byref,\ create_string_buffer, c_void_p from ctypes.util import find_library - libcrypto_path = find_library('crypto') + for p in ('crypto', 'eay32', 'libeay32'): + libcrypto_path = find_library(p) + if libcrypto_path: + break + else: + raise Exception('libcrypto(OpenSSL) not found') logging.info('loading libcrypto from %s', libcrypto_path) libcrypto = CDLL(libcrypto_path) libcrypto.EVP_get_cipherbyname.restype = c_void_p @@ -56,7 +61,7 @@ def load_openssl(): loaded = True -def load_ctr_cipher(cipher_name): +def load_cipher(cipher_name): func_name = 'EVP_' + cipher_name.replace('-', '_') cipher = getattr(libcrypto, func_name, None) if cipher: @@ -70,10 +75,9 @@ class CtypesCrypto(object): if not loaded: load_openssl() self._ctx = None - if 'ctr' in cipher_name: - cipher = load_ctr_cipher(cipher_name) - else: - cipher = libcrypto.EVP_get_cipherbyname(cipher_name) + cipher = libcrypto.EVP_get_cipherbyname(cipher_name) + if not cipher: + cipher = load_cipher(cipher_name) if not cipher: raise Exception('cipher %s not found in libcrypto' % cipher_name) key_ptr = c_char_p(key) @@ -110,6 +114,12 @@ class CtypesCrypto(object): ciphers = { + 'aes-128-cfb': (16, 16, CtypesCrypto), + 'aes-192-cfb': (24, 16, CtypesCrypto), + 'aes-256-cfb': (32, 16, CtypesCrypto), + 'aes-128-ofb': (16, 16, CtypesCrypto), + 'aes-192-ofb': (24, 16, CtypesCrypto), + 'aes-256-ofb': (32, 16, CtypesCrypto), 'aes-128-ctr': (16, 16, CtypesCrypto), 'aes-192-ctr': (24, 16, CtypesCrypto), 'aes-256-ctr': (32, 16, CtypesCrypto), @@ -119,6 +129,16 @@ ciphers = { 'aes-128-cfb1': (16, 16, CtypesCrypto), 'aes-192-cfb1': (24, 16, CtypesCrypto), 'aes-256-cfb1': (32, 16, CtypesCrypto), + 'bf-cfb': (16, 8, CtypesCrypto), + 'camellia-128-cfb': (16, 16, CtypesCrypto), + 'camellia-192-cfb': (24, 16, CtypesCrypto), + 'camellia-256-cfb': (32, 16, CtypesCrypto), + 'cast5-cfb': (16, 8, CtypesCrypto), + 'des-cfb': (8, 8, CtypesCrypto), + 'idea-cfb': (16, 8, CtypesCrypto), + 'rc2-cfb': (16, 8, CtypesCrypto), + 'rc4': (16, 0, CtypesCrypto), + 'seed-cfb': (16, 16, CtypesCrypto), } diff --git a/shadowsocks/crypto/m2.py b/shadowsocks/crypto/m2.py index d2a4927..c140061 100644 --- a/shadowsocks/crypto/m2.py +++ b/shadowsocks/crypto/m2.py @@ -47,21 +47,21 @@ def err(alg, key, iv, op, key_as_bytes=0, d=None, salt=None, i=1, padding=1): sys.exit(1) -if not has_m2: - create_cipher = err - -ciphers = { - 'aes-128-cfb': (16, 16, create_cipher), - 'aes-192-cfb': (24, 16, create_cipher), - 'aes-256-cfb': (32, 16, create_cipher), - 'bf-cfb': (16, 8, create_cipher), - 'camellia-128-cfb': (16, 16, create_cipher), - 'camellia-192-cfb': (24, 16, create_cipher), - 'camellia-256-cfb': (32, 16, create_cipher), - 'cast5-cfb': (16, 8, create_cipher), - 'des-cfb': (8, 8, create_cipher), - 'idea-cfb': (16, 8, create_cipher), - 'rc2-cfb': (16, 8, create_cipher), - 'rc4': (16, 0, create_cipher), - 'seed-cfb': (16, 16, create_cipher), -} +if has_m2: + ciphers = { + 'aes-128-cfb': (16, 16, create_cipher), + 'aes-192-cfb': (24, 16, create_cipher), + 'aes-256-cfb': (32, 16, create_cipher), + 'bf-cfb': (16, 8, create_cipher), + 'camellia-128-cfb': (16, 16, create_cipher), + 'camellia-192-cfb': (24, 16, create_cipher), + 'camellia-256-cfb': (32, 16, create_cipher), + 'cast5-cfb': (16, 8, create_cipher), + 'des-cfb': (8, 8, create_cipher), + 'idea-cfb': (16, 8, create_cipher), + 'rc2-cfb': (16, 8, create_cipher), + 'rc4': (16, 0, create_cipher), + 'seed-cfb': (16, 16, create_cipher), + } +else: + ciphers = {} diff --git a/shadowsocks/crypto/rc4_md5.py b/shadowsocks/crypto/rc4_md5.py index 571f666..3dac6d5 100644 --- a/shadowsocks/crypto/rc4_md5.py +++ b/shadowsocks/crypto/rc4_md5.py @@ -26,17 +26,27 @@ import hashlib __all__ = ['ciphers'] +m2_not_found = False + def create_cipher(alg, key, iv, op, key_as_bytes=0, d=None, salt=None, i=1, padding=1): + global m2_not_found + md5 = hashlib.md5() md5.update(key) md5.update(iv) rc4_key = md5.digest() - import M2Crypto.EVP - return M2Crypto.EVP.Cipher('rc4', rc4_key, '', op, key_as_bytes=0, - d='md5', salt=None, i=1, padding=1) + if not m2_not_found: + try: + import M2Crypto.EVP + return M2Crypto.EVP.Cipher('rc4', rc4_key, '', op, key_as_bytes=0, + d='md5', salt=None, i=1, padding=1) + except: + m2_not_found = True + import ctypes_openssl + return ctypes_openssl.CtypesCrypto('rc4', rc4_key, '', op) ciphers = { diff --git a/shadowsocks/encrypt.py b/shadowsocks/encrypt.py index 2f9872a..2e66175 100644 --- a/shadowsocks/encrypt.py +++ b/shadowsocks/encrypt.py @@ -34,10 +34,11 @@ import crypto.ctypes_openssl method_supported = {} -method_supported.update(crypto.m2.ciphers) method_supported.update(crypto.rc4_md5.ciphers) method_supported.update(crypto.salsa20_ctr.ciphers) method_supported.update(crypto.ctypes_openssl.ciphers) +# let M2Crypto override ctypes_openssl +method_supported.update(crypto.m2.ciphers) def random_string(length):