Browse Source

does not require M2Crypto anymore

auth
clowwindy 10 years ago
parent
commit
eb9b6f0a9b
  1. 2
      setup.py
  2. 30
      shadowsocks/crypto/ctypes_openssl.py
  3. 6
      shadowsocks/crypto/m2.py
  4. 10
      shadowsocks/crypto/rc4_md5.py
  5. 3
      shadowsocks/encrypt.py

2
setup.py

@ -6,7 +6,7 @@ with open('README.rst') as f:
setup( setup(
name="shadowsocks", name="shadowsocks",
version="2.3", version="2.3.1",
license='MIT', license='MIT',
description="A fast tunnel proxy that help you get through firewalls", description="A fast tunnel proxy that help you get through firewalls",
author='clowwindy', author='clowwindy',

30
shadowsocks/crypto/ctypes_openssl.py

@ -35,7 +35,12 @@ def load_openssl():
from ctypes import CDLL, c_char_p, c_int, c_long, byref,\ from ctypes import CDLL, c_char_p, c_int, c_long, byref,\
create_string_buffer, c_void_p create_string_buffer, c_void_p
from ctypes.util import find_library 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) logging.info('loading libcrypto from %s', libcrypto_path)
libcrypto = CDLL(libcrypto_path) libcrypto = CDLL(libcrypto_path)
libcrypto.EVP_get_cipherbyname.restype = c_void_p libcrypto.EVP_get_cipherbyname.restype = c_void_p
@ -56,7 +61,7 @@ def load_openssl():
loaded = True loaded = True
def load_ctr_cipher(cipher_name): def load_cipher(cipher_name):
func_name = 'EVP_' + cipher_name.replace('-', '_') func_name = 'EVP_' + cipher_name.replace('-', '_')
cipher = getattr(libcrypto, func_name, None) cipher = getattr(libcrypto, func_name, None)
if cipher: if cipher:
@ -70,10 +75,9 @@ class CtypesCrypto(object):
if not loaded: if not loaded:
load_openssl() load_openssl()
self._ctx = None 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: if not cipher:
raise Exception('cipher %s not found in libcrypto' % cipher_name) raise Exception('cipher %s not found in libcrypto' % cipher_name)
key_ptr = c_char_p(key) key_ptr = c_char_p(key)
@ -110,6 +114,12 @@ class CtypesCrypto(object):
ciphers = { 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-128-ctr': (16, 16, CtypesCrypto),
'aes-192-ctr': (24, 16, CtypesCrypto), 'aes-192-ctr': (24, 16, CtypesCrypto),
'aes-256-ctr': (32, 16, CtypesCrypto), 'aes-256-ctr': (32, 16, CtypesCrypto),
@ -119,6 +129,16 @@ ciphers = {
'aes-128-cfb1': (16, 16, CtypesCrypto), 'aes-128-cfb1': (16, 16, CtypesCrypto),
'aes-192-cfb1': (24, 16, CtypesCrypto), 'aes-192-cfb1': (24, 16, CtypesCrypto),
'aes-256-cfb1': (32, 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),
} }

6
shadowsocks/crypto/m2.py

@ -47,9 +47,7 @@ def err(alg, key, iv, op, key_as_bytes=0, d=None, salt=None, i=1, padding=1):
sys.exit(1) sys.exit(1)
if not has_m2: if has_m2:
create_cipher = err
ciphers = { ciphers = {
'aes-128-cfb': (16, 16, create_cipher), 'aes-128-cfb': (16, 16, create_cipher),
'aes-192-cfb': (24, 16, create_cipher), 'aes-192-cfb': (24, 16, create_cipher),
@ -65,3 +63,5 @@ ciphers = {
'rc4': (16, 0, create_cipher), 'rc4': (16, 0, create_cipher),
'seed-cfb': (16, 16, create_cipher), 'seed-cfb': (16, 16, create_cipher),
} }
else:
ciphers = {}

10
shadowsocks/crypto/rc4_md5.py

@ -26,17 +26,27 @@ import hashlib
__all__ = ['ciphers'] __all__ = ['ciphers']
m2_not_found = False
def create_cipher(alg, key, iv, op, key_as_bytes=0, d=None, salt=None, def create_cipher(alg, key, iv, op, key_as_bytes=0, d=None, salt=None,
i=1, padding=1): i=1, padding=1):
global m2_not_found
md5 = hashlib.md5() md5 = hashlib.md5()
md5.update(key) md5.update(key)
md5.update(iv) md5.update(iv)
rc4_key = md5.digest() rc4_key = md5.digest()
if not m2_not_found:
try:
import M2Crypto.EVP import M2Crypto.EVP
return M2Crypto.EVP.Cipher('rc4', rc4_key, '', op, key_as_bytes=0, return M2Crypto.EVP.Cipher('rc4', rc4_key, '', op, key_as_bytes=0,
d='md5', salt=None, i=1, padding=1) 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 = { ciphers = {

3
shadowsocks/encrypt.py

@ -34,10 +34,11 @@ import crypto.ctypes_openssl
method_supported = {} method_supported = {}
method_supported.update(crypto.m2.ciphers)
method_supported.update(crypto.rc4_md5.ciphers) method_supported.update(crypto.rc4_md5.ciphers)
method_supported.update(crypto.salsa20_ctr.ciphers) method_supported.update(crypto.salsa20_ctr.ciphers)
method_supported.update(crypto.ctypes_openssl.ciphers) method_supported.update(crypto.ctypes_openssl.ciphers)
# let M2Crypto override ctypes_openssl
method_supported.update(crypto.m2.ciphers)
def random_string(length): def random_string(length):

Loading…
Cancel
Save