You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
186 lines
5.7 KiB
186 lines
5.7 KiB
10 years ago
|
#!/usr/bin/env python
|
||
|
|
||
|
# Copyright (c) 2014 clowwindy
|
||
|
#
|
||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
|
# of this software and associated documentation files (the "Software"), to deal
|
||
|
# in the Software without restriction, including without limitation the rights
|
||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
|
# copies of the Software, and to permit persons to whom the Software is
|
||
|
# furnished to do so, subject to the following conditions:
|
||
|
#
|
||
|
# The above copyright notice and this permission notice shall be included in
|
||
|
# all copies or substantial portions of the Software.
|
||
|
#
|
||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||
|
# SOFTWARE.
|
||
|
|
||
10 years ago
|
from __future__ import absolute_import, division, print_function, \
|
||
|
with_statement
|
||
|
|
||
10 years ago
|
from ctypes import c_char_p, c_int, c_long, byref,\
|
||
10 years ago
|
create_string_buffer, c_void_p
|
||
10 years ago
|
|
||
10 years ago
|
from shadowsocks.crypto import util
|
||
|
|
||
10 years ago
|
__all__ = ['ciphers']
|
||
|
|
||
10 years ago
|
libcrypto = None
|
||
10 years ago
|
loaded = False
|
||
|
|
||
10 years ago
|
buf_size = 2048
|
||
|
|
||
10 years ago
|
|
||
|
def load_openssl():
|
||
10 years ago
|
global loaded, libcrypto, buf
|
||
|
|
||
10 years ago
|
libcrypto = util.find_library(('crypto', 'eay32'),
|
||
|
'EVP_get_cipherbyname',
|
||
|
'libcrypto')
|
||
|
if libcrypto is None:
|
||
10 years ago
|
raise Exception('libcrypto(OpenSSL) not found')
|
||
10 years ago
|
|
||
10 years ago
|
libcrypto.EVP_get_cipherbyname.restype = c_void_p
|
||
|
libcrypto.EVP_CIPHER_CTX_new.restype = c_void_p
|
||
|
|
||
|
libcrypto.EVP_CipherInit_ex.argtypes = (c_void_p, c_void_p, c_char_p,
|
||
|
c_char_p, c_char_p, c_int)
|
||
|
|
||
|
libcrypto.EVP_CipherUpdate.argtypes = (c_void_p, c_void_p, c_void_p,
|
||
|
c_char_p, c_int)
|
||
|
|
||
|
libcrypto.EVP_CIPHER_CTX_cleanup.argtypes = (c_void_p,)
|
||
|
libcrypto.EVP_CIPHER_CTX_free.argtypes = (c_void_p,)
|
||
10 years ago
|
if hasattr(libcrypto, 'OpenSSL_add_all_ciphers'):
|
||
|
libcrypto.OpenSSL_add_all_ciphers()
|
||
10 years ago
|
|
||
10 years ago
|
buf = create_string_buffer(buf_size)
|
||
10 years ago
|
loaded = True
|
||
|
|
||
|
|
||
10 years ago
|
def load_cipher(cipher_name):
|
||
10 years ago
|
func_name = b'EVP_' + cipher_name.replace(b'-', b'_')
|
||
|
if bytes != str:
|
||
|
func_name = str(func_name, 'utf-8')
|
||
10 years ago
|
cipher = getattr(libcrypto, func_name, None)
|
||
|
if cipher:
|
||
|
cipher.restype = c_void_p
|
||
|
return cipher()
|
||
|
return None
|
||
|
|
||
|
|
||
10 years ago
|
class OpenSSLCrypto(object):
|
||
10 years ago
|
def __init__(self, cipher_name, key, iv, op):
|
||
10 years ago
|
self._ctx = None
|
||
10 years ago
|
if not loaded:
|
||
|
load_openssl()
|
||
10 years ago
|
cipher = libcrypto.EVP_get_cipherbyname(cipher_name)
|
||
|
if not cipher:
|
||
|
cipher = load_cipher(cipher_name)
|
||
10 years ago
|
if not cipher:
|
||
|
raise Exception('cipher %s not found in libcrypto' % cipher_name)
|
||
|
key_ptr = c_char_p(key)
|
||
|
iv_ptr = c_char_p(iv)
|
||
10 years ago
|
self._ctx = libcrypto.EVP_CIPHER_CTX_new()
|
||
10 years ago
|
if not self._ctx:
|
||
|
raise Exception('can not create cipher context')
|
||
|
r = libcrypto.EVP_CipherInit_ex(self._ctx, cipher, None,
|
||
|
key_ptr, iv_ptr, c_int(op))
|
||
|
if not r:
|
||
|
self.clean()
|
||
|
raise Exception('can not initialize cipher context')
|
||
|
|
||
|
def update(self, data):
|
||
10 years ago
|
global buf_size, buf
|
||
10 years ago
|
cipher_out_len = c_long(0)
|
||
10 years ago
|
l = len(data)
|
||
|
if buf_size < l:
|
||
|
buf_size = l * 2
|
||
|
buf = create_string_buffer(buf_size)
|
||
10 years ago
|
libcrypto.EVP_CipherUpdate(self._ctx, byref(buf),
|
||
10 years ago
|
byref(cipher_out_len), c_char_p(data), l)
|
||
10 years ago
|
# buf is copied to a str object when we access buf.raw
|
||
10 years ago
|
return buf.raw[:cipher_out_len.value]
|
||
|
|
||
|
def __del__(self):
|
||
|
self.clean()
|
||
|
|
||
|
def clean(self):
|
||
|
if self._ctx:
|
||
|
libcrypto.EVP_CIPHER_CTX_cleanup(self._ctx)
|
||
|
libcrypto.EVP_CIPHER_CTX_free(self._ctx)
|
||
|
|
||
|
|
||
|
ciphers = {
|
||
10 years ago
|
b'aes-128-cfb': (16, 16, OpenSSLCrypto),
|
||
|
b'aes-192-cfb': (24, 16, OpenSSLCrypto),
|
||
|
b'aes-256-cfb': (32, 16, OpenSSLCrypto),
|
||
|
b'aes-128-ofb': (16, 16, OpenSSLCrypto),
|
||
|
b'aes-192-ofb': (24, 16, OpenSSLCrypto),
|
||
|
b'aes-256-ofb': (32, 16, OpenSSLCrypto),
|
||
|
b'aes-128-ctr': (16, 16, OpenSSLCrypto),
|
||
|
b'aes-192-ctr': (24, 16, OpenSSLCrypto),
|
||
|
b'aes-256-ctr': (32, 16, OpenSSLCrypto),
|
||
|
b'aes-128-cfb8': (16, 16, OpenSSLCrypto),
|
||
|
b'aes-192-cfb8': (24, 16, OpenSSLCrypto),
|
||
|
b'aes-256-cfb8': (32, 16, OpenSSLCrypto),
|
||
|
b'aes-128-cfb1': (16, 16, OpenSSLCrypto),
|
||
|
b'aes-192-cfb1': (24, 16, OpenSSLCrypto),
|
||
|
b'aes-256-cfb1': (32, 16, OpenSSLCrypto),
|
||
|
b'bf-cfb': (16, 8, OpenSSLCrypto),
|
||
|
b'camellia-128-cfb': (16, 16, OpenSSLCrypto),
|
||
|
b'camellia-192-cfb': (24, 16, OpenSSLCrypto),
|
||
|
b'camellia-256-cfb': (32, 16, OpenSSLCrypto),
|
||
|
b'cast5-cfb': (16, 8, OpenSSLCrypto),
|
||
|
b'des-cfb': (8, 8, OpenSSLCrypto),
|
||
|
b'idea-cfb': (16, 8, OpenSSLCrypto),
|
||
|
b'rc2-cfb': (16, 8, OpenSSLCrypto),
|
||
|
b'rc4': (16, 0, OpenSSLCrypto),
|
||
|
b'seed-cfb': (16, 16, OpenSSLCrypto),
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
|
||
10 years ago
|
def run_method(method):
|
||
10 years ago
|
|
||
10 years ago
|
cipher = OpenSSLCrypto(method, b'k' * 32, b'i' * 16, 1)
|
||
|
decipher = OpenSSLCrypto(method, b'k' * 32, b'i' * 16, 0)
|
||
10 years ago
|
|
||
10 years ago
|
util.run_cipher(cipher, decipher)
|
||
10 years ago
|
|
||
|
|
||
10 years ago
|
def test_aes_128_cfb():
|
||
|
run_method(b'aes-128-cfb')
|
||
|
|
||
|
|
||
|
def test_aes_256_cfb():
|
||
|
run_method(b'aes-256-cfb')
|
||
|
|
||
|
|
||
|
def test_aes_128_cfb8():
|
||
|
run_method(b'aes-128-cfb8')
|
||
|
|
||
|
|
||
|
def test_aes_256_ofb():
|
||
|
run_method(b'aes-256-ofb')
|
||
|
|
||
|
|
||
|
def test_aes_256_ctr():
|
||
|
run_method(b'aes-256-ctr')
|
||
|
|
||
|
|
||
|
def test_bf_cfb():
|
||
|
run_method(b'bf-cfb')
|
||
|
|
||
|
|
||
|
def test_rc4():
|
||
|
run_method(b'rc4')
|
||
|
|
||
|
|
||
10 years ago
|
if __name__ == '__main__':
|
||
10 years ago
|
test_aes_128_cfb()
|