<s> from __future__ import absolute_import , division , print_function , with_statement <EOL> from ctypes import c_char_p , c_int , c_long , byref , create_string_buffer , c_void_p <EOL> from shadowsocks import common <EOL> from shadowsocks . crypto import util <EOL> __all__ = [ '<STR_LIT>' ] <EOL> libcrypto = None <EOL> loaded = False <EOL> buf_size = <NUM_LIT> <EOL> def load_openssl ( ) : <EOL> global loaded , libcrypto , buf <EOL> libcrypto = util . find_library ( ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> if libcrypto is None : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> libcrypto . EVP_get_cipherbyname . restype = c_void_p <EOL> libcrypto . EVP_CIPHER_CTX_new . restype = c_void_p <EOL> libcrypto . EVP_CipherInit_ex . argtypes = ( c_void_p , c_void_p , c_char_p , <EOL> c_char_p , c_char_p , c_int ) <EOL> libcrypto . EVP_CipherUpdate . argtypes = ( c_void_p , c_void_p , c_void_p , <EOL> c_char_p , c_int ) <EOL> libcrypto . EVP_CIPHER_CTX_cleanup . argtypes = ( c_void_p , ) <EOL> libcrypto . EVP_CIPHER_CTX_free . argtypes = ( c_void_p , ) <EOL> if hasattr ( libcrypto , '<STR_LIT>' ) : <EOL> libcrypto . OpenSSL_add_all_ciphers ( ) <EOL> buf = create_string_buffer ( buf_size ) <EOL> loaded = True <EOL> def load_cipher ( cipher_name ) : <EOL> func_name = '<STR_LIT>' + cipher_name . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> if bytes != str : <EOL> func_name = str ( func_name , '<STR_LIT:utf-8>' ) <EOL> cipher = getattr ( libcrypto , func_name , None ) <EOL> if cipher : <EOL> cipher . restype = c_void_p <EOL> return cipher ( ) <EOL> return None <EOL> class OpenSSLCrypto ( object ) : <EOL> def __init__ ( self , cipher_name , key , iv , op ) : <EOL> self . _ctx = None <EOL> if not loaded : <EOL> load_openssl ( ) <EOL> cipher_name = common . to_bytes ( cipher_name ) <EOL> cipher = libcrypto . EVP_get_cipherbyname ( cipher_name ) <EOL> if not cipher : <EOL> cipher = load_cipher ( cipher_name ) <EOL> if not cipher : <EOL> raise Exception ( '<STR_LIT>' % cipher_name ) <EOL> key_ptr = c_char_p ( key ) <EOL> iv_ptr = c_char_p ( iv ) <EOL> self . _ctx = libcrypto . EVP_CIPHER_CTX_new ( ) <EOL> if not self . _ctx : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> r = libcrypto . EVP_CipherInit_ex ( self . _ctx , cipher , None , <EOL> key_ptr , iv_ptr , c_int ( op ) ) <EOL> if not r : <EOL> self . clean ( ) <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def update ( self , data ) : <EOL> global buf_size , buf <EOL> cipher_out_len = c_long ( <NUM_LIT:0> ) <EOL> l = len ( data ) <EOL> if buf_size < l : <EOL> buf_size = l * <NUM_LIT:2> <EOL> buf = create_string_buffer ( buf_size ) <EOL> libcrypto . EVP_CipherUpdate ( self . _ctx , byref ( buf ) , <EOL> byref ( cipher_out_len ) , c_char_p ( data ) , l ) <EOL> return buf . raw [ : cipher_out_len . value ] <EOL> def __del__ ( self ) : <EOL> self . clean ( ) <EOL> def clean ( self ) : <EOL> if self . _ctx : <EOL> libcrypto . EVP_CIPHER_CTX_cleanup ( self . _ctx ) <EOL> libcrypto . EVP_CIPHER_CTX_free ( self . _ctx ) <EOL> ciphers = { <EOL> '<STR_LIT>' : ( <NUM_LIT:16> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:32> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:16> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:32> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:16> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:32> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:16> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:32> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:16> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:32> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:16> , <NUM_LIT:8> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:16> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:32> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:16> , <NUM_LIT:8> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:8> , <NUM_LIT:8> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:16> , <NUM_LIT:8> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:16> , <NUM_LIT:8> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:16> , <NUM_LIT:0> , OpenSSLCrypto ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:16> , <NUM_LIT:16> , OpenSSLCrypto ) , <EOL> } <EOL> def run_method ( method ) : <EOL> cipher = OpenSSLCrypto ( method , b'<STR_LIT:k>' * <NUM_LIT:32> , b'<STR_LIT:i>' * <NUM_LIT:16> , <NUM_LIT:1> ) <EOL> decipher = OpenSSLCrypto ( method , b'<STR_LIT:k>' * <NUM_LIT:32> , b'<STR_LIT:i>' * <NUM_LIT:16> , <NUM_LIT:0> ) <EOL> util . run_cipher ( cipher , decipher ) <EOL> def test_aes_128_cfb ( ) : <EOL> run_method ( '<STR_LIT>' ) <EOL> def test_aes_256_cfb ( ) : <EOL> run_method ( '<STR_LIT>' ) <EOL> def test_aes_128_cfb8 ( ) : <EOL> run_method ( '<STR_LIT>' ) <EOL> def test_aes_256_ofb ( ) : <EOL> run_method ( '<STR_LIT>' ) <EOL> def test_aes_256_ctr ( ) : <EOL> run_method ( '<STR_LIT>' ) <EOL> def test_bf_cfb ( ) : <EOL> run_method ( '<STR_LIT>' ) <EOL> def test_rc4 ( ) : <EOL> run_method ( '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_aes_128_cfb ( ) </s>
