Fix memory leak (#921)

* Fix memory leak

* Fix memory leak(2)

* refine
This commit is contained in:
Esdeath 2017-09-06 15:07:19 +08:00 committed by mengskysama
parent d5026cf5ef
commit 893f9099b3
3 changed files with 36 additions and 11 deletions

View File

@ -155,9 +155,6 @@ class AeadCryptoBase(object):
# n, n > 0, waiting data # n, n > 0, waiting data
self._chunk = {'mlen': AEAD_MSG_LEN_UNKNOWN, 'data': b''} self._chunk = {'mlen': AEAD_MSG_LEN_UNKNOWN, 'data': b''}
self.encrypt_once = self.aead_encrypt
self.decrypt_once = self.aead_decrypt
# load libsodium for nonce increment # load libsodium for nonce increment
if not sodium_loaded: if not sodium_loaded:
crypto_path = dict(crypto_path) if crypto_path else dict() crypto_path = dict(crypto_path) if crypto_path else dict()

View File

@ -107,8 +107,11 @@ class OpenSSLCryptoBase(object):
if not self._ctx: if not self._ctx:
raise Exception('can not create cipher context') raise Exception('can not create cipher context')
self.encrypt_once = self.update def encrypt_once(self, data):
self.decrypt_once = self.update return self.update(data)
def decrypt_once(self, data):
return self.update(data)
def update(self, data): def update(self, data):
""" """
@ -136,6 +139,7 @@ class OpenSSLCryptoBase(object):
if self._ctx: if self._ctx:
ctx_cleanup(self._ctx) ctx_cleanup(self._ctx)
libcrypto.EVP_CIPHER_CTX_free(self._ctx) libcrypto.EVP_CIPHER_CTX_free(self._ctx)
self._ctx = None
class OpenSSLAeadCrypto(OpenSSLCryptoBase, AeadCryptoBase): class OpenSSLAeadCrypto(OpenSSLCryptoBase, AeadCryptoBase):
@ -267,6 +271,12 @@ class OpenSSLAeadCrypto(OpenSSLCryptoBase, AeadCryptoBase):
self.cipher_ctx_init() self.cipher_ctx_init()
return plaintext return plaintext
def encrypt_once(self, data):
return self.aead_encrypt(data)
def decrypt_once(self, data):
return self.aead_decrypt(data)
class OpenSSLStreamCrypto(OpenSSLCryptoBase): class OpenSSLStreamCrypto(OpenSSLCryptoBase):
""" """
@ -281,8 +291,12 @@ class OpenSSLStreamCrypto(OpenSSLCryptoBase):
if not r: if not r:
self.clean() self.clean()
raise Exception('can not initialize cipher context') raise Exception('can not initialize cipher context')
self.encrypt = self.update
self.decrypt = self.update def encrypt(self, data):
return self.update(data)
def decrypt(self, data):
return self.update(data)
ciphers = { ciphers = {

View File

@ -192,10 +192,18 @@ class SodiumCrypto(object):
raise Exception('Unknown cipher') raise Exception('Unknown cipher')
# byte counter, not block counter # byte counter, not block counter
self.counter = 0 self.counter = 0
self.encrypt = self.update
self.decrypt = self.update def encrypt(self, data):
self.encrypt_once = self.update return self.update(data)
self.decrypt_once = self.update
def decrypt(self, data):
return self.update(data)
def encrypt_once(self, data):
return self.update(data)
def decrypt_once(self, data):
return self.update(data)
def update(self, data): def update(self, data):
global buf_size, buf global buf_size, buf
@ -300,6 +308,12 @@ class SodiumAeadCrypto(AeadCryptoBase):
self.cipher_ctx_init() self.cipher_ctx_init()
return buf.raw[:cipher_out_len.value] return buf.raw[:cipher_out_len.value]
def encrypt_once(self, data):
return self.aead_encrypt(data)
def decrypt_once(self, data):
return self.aead_decrypt(data)
ciphers = { ciphers = {
'salsa20': (32, 8, SodiumCrypto), 'salsa20': (32, 8, SodiumCrypto),