#775 fix UDP decrypt_all issue
This commit is contained in:
parent
d5cf37ab67
commit
3b9689aaa0
4 changed files with 35 additions and 31 deletions
|
@ -22,7 +22,8 @@ from ctypes import c_char_p, c_int, c_long, byref,\
|
||||||
|
|
||||||
from shadowsocks import common
|
from shadowsocks import common
|
||||||
from shadowsocks.crypto import util
|
from shadowsocks.crypto import util
|
||||||
from shadowsocks.crypto.aead import *
|
from shadowsocks.crypto.aead import AeadCryptoBase, EVP_CTRL_AEAD_SET_IVLEN, \
|
||||||
|
nonce_increment, EVP_CTRL_AEAD_GET_TAG, EVP_CTRL_AEAD_SET_TAG
|
||||||
|
|
||||||
__all__ = ['ciphers']
|
__all__ = ['ciphers']
|
||||||
|
|
||||||
|
@ -117,7 +118,8 @@ class OpenSSLCryptoBase(object):
|
||||||
buf = create_string_buffer(buf_size)
|
buf = create_string_buffer(buf_size)
|
||||||
libcrypto.EVP_CipherUpdate(
|
libcrypto.EVP_CipherUpdate(
|
||||||
self._ctx, byref(buf),
|
self._ctx, byref(buf),
|
||||||
byref(cipher_out_len), c_char_p(data), l)
|
byref(cipher_out_len), c_char_p(data), l
|
||||||
|
)
|
||||||
# buf is copied to a str object when we access buf.raw
|
# buf is copied to a str object when we access buf.raw
|
||||||
return buf.raw[:cipher_out_len.value]
|
return buf.raw[:cipher_out_len.value]
|
||||||
|
|
||||||
|
@ -141,7 +143,8 @@ class OpenSSLAeadCrypto(OpenSSLCryptoBase, AeadCryptoBase):
|
||||||
r = libcrypto.EVP_CipherInit_ex(
|
r = libcrypto.EVP_CipherInit_ex(
|
||||||
self._ctx,
|
self._ctx,
|
||||||
self._cipher, None,
|
self._cipher, None,
|
||||||
None, None, c_int(op))
|
None, None, c_int(op)
|
||||||
|
)
|
||||||
if not r:
|
if not r:
|
||||||
self.clean()
|
self.clean()
|
||||||
raise Exception('can not initialize cipher context')
|
raise Exception('can not initialize cipher context')
|
||||||
|
@ -149,7 +152,9 @@ class OpenSSLAeadCrypto(OpenSSLCryptoBase, AeadCryptoBase):
|
||||||
r = libcrypto.EVP_CIPHER_CTX_ctrl(
|
r = libcrypto.EVP_CIPHER_CTX_ctrl(
|
||||||
self._ctx,
|
self._ctx,
|
||||||
c_int(EVP_CTRL_AEAD_SET_IVLEN),
|
c_int(EVP_CTRL_AEAD_SET_IVLEN),
|
||||||
c_int(self._nlen), None)
|
c_int(self._nlen),
|
||||||
|
None
|
||||||
|
)
|
||||||
if not r:
|
if not r:
|
||||||
raise Exception('Set ivlen failed')
|
raise Exception('Set ivlen failed')
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ from ctypes import c_char_p, c_int, c_ulonglong, byref, c_ulong, \
|
||||||
create_string_buffer, c_void_p
|
create_string_buffer, c_void_p
|
||||||
|
|
||||||
from shadowsocks.crypto import util
|
from shadowsocks.crypto import util
|
||||||
from shadowsocks.crypto.aead import *
|
from shadowsocks.crypto.aead import AeadCryptoBase
|
||||||
|
|
||||||
__all__ = ['ciphers']
|
__all__ = ['ciphers']
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,6 @@ from __future__ import absolute_import, division, print_function, \
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
from ctypes import create_string_buffer
|
|
||||||
|
|
||||||
|
|
||||||
def find_library_nt(name):
|
def find_library_nt(name):
|
||||||
|
@ -34,7 +33,7 @@ def find_library_nt(name):
|
||||||
results.append(fname)
|
results.append(fname)
|
||||||
if fname.lower().endswith(".dll"):
|
if fname.lower().endswith(".dll"):
|
||||||
continue
|
continue
|
||||||
fname = fname + ".dll"
|
fname += ".dll"
|
||||||
if os.path.isfile(fname):
|
if os.path.isfile(fname):
|
||||||
results.append(fname)
|
results.append(fname)
|
||||||
return results
|
return results
|
||||||
|
@ -111,9 +110,9 @@ def run_cipher(cipher, decipher):
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
|
|
||||||
BLOCK_SIZE = 16384
|
block_size = 16384
|
||||||
rounds = 1 * 1024
|
rounds = 1 * 1024
|
||||||
plain = urandom(BLOCK_SIZE * rounds)
|
plain = urandom(block_size * rounds)
|
||||||
|
|
||||||
results = []
|
results = []
|
||||||
pos = 0
|
pos = 0
|
||||||
|
@ -132,7 +131,7 @@ def run_cipher(cipher, decipher):
|
||||||
results.append(decipher.decrypt(c[pos:pos + l]))
|
results.append(decipher.decrypt(c[pos:pos + l]))
|
||||||
pos += l
|
pos += l
|
||||||
end = time.time()
|
end = time.time()
|
||||||
print('speed: %d bytes/s' % (BLOCK_SIZE * rounds / (end - start)))
|
print('speed: %d bytes/s' % (block_size * rounds / (end - start)))
|
||||||
assert b''.join(results) == plain
|
assert b''.join(results) == plain
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue