Skip to content

Commit

Permalink
support CTR mode
Browse files Browse the repository at this point in the history
  • Loading branch information
clowwindy committed Sep 16, 2014
1 parent 2122f96 commit 0e64945
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion shadowsocks/crypto/ctypes_openssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,24 @@ def load_openssl():
loaded = True


def load_ctr_cipher(cipher_name):
func_name = 'EVP_' + cipher_name.replace('-', '_')
cipher = getattr(libcrypto, func_name, None)
if cipher:
cipher.restype = c_void_p
return cipher()
return None


class CtypesCrypto(object):
def __init__(self, cipher_name, key, iv, op):
if not loaded:
load_openssl()
self._ctx = None
cipher = libcrypto.EVP_get_cipherbyname(cipher_name)
if 'ctr' in cipher_name:
cipher = load_ctr_cipher(cipher_name)
else:
cipher = libcrypto.EVP_get_cipherbyname(cipher_name)
if not cipher:
raise Exception('cipher %s not found in libcrypto' % cipher_name)
key_ptr = c_char_p(key)
Expand Down Expand Up @@ -91,6 +103,9 @@ def clean(self):


ciphers = {
'aes-128-ctr': (16, 16, CtypesCrypto),
'aes-192-ctr': (24, 16, CtypesCrypto),
'aes-256-ctr': (32, 16, CtypesCrypto),
'aes-128-cfb8': (16, 16, CtypesCrypto),
'aes-192-cfb8': (24, 16, CtypesCrypto),
'aes-256-cfb8': (32, 16, CtypesCrypto),
Expand Down

0 comments on commit 0e64945

Please sign in to comment.