Browse Source

does not require M2Crypto anymore

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

2
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',

32
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),
}

36
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 = {}

16
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 = {

3
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):

Loading…
Cancel
Save