|
@ -23,10 +23,11 @@ |
|
|
from __future__ import absolute_import, division, print_function, \ |
|
|
from __future__ import absolute_import, division, print_function, \ |
|
|
with_statement |
|
|
with_statement |
|
|
|
|
|
|
|
|
import logging |
|
|
from ctypes import c_char_p, c_int, c_ulonglong, byref, \ |
|
|
from ctypes import CDLL, c_char_p, c_int, c_ulonglong, byref, \ |
|
|
|
|
|
create_string_buffer, c_void_p |
|
|
create_string_buffer, c_void_p |
|
|
|
|
|
|
|
|
|
|
|
from shadowsocks.crypto import util |
|
|
|
|
|
|
|
|
__all__ = ['ciphers'] |
|
|
__all__ = ['ciphers'] |
|
|
|
|
|
|
|
|
libsodium = None |
|
|
libsodium = None |
|
@ -41,21 +42,11 @@ BLOCK_SIZE = 64 |
|
|
def load_libsodium(): |
|
|
def load_libsodium(): |
|
|
global loaded, libsodium, buf |
|
|
global loaded, libsodium, buf |
|
|
|
|
|
|
|
|
from ctypes.util import find_library |
|
|
libsodium = util.find_library('sodium', 'crypto_stream_salsa20_xor_ic', |
|
|
libsodium_path = None |
|
|
'libsodium') |
|
|
for p in ('sodium', 'libsodium'): |
|
|
if libsodium is None: |
|
|
libsodium_path = find_library(p) |
|
|
|
|
|
if libsodium_path: |
|
|
|
|
|
break |
|
|
|
|
|
else: |
|
|
|
|
|
import glob |
|
|
|
|
|
for libsodium_path in glob.glob('/usr/lib/libsodium.*'): |
|
|
|
|
|
pass |
|
|
|
|
|
if libsodium_path is None: |
|
|
|
|
|
raise Exception('libsodium not found') |
|
|
raise Exception('libsodium not found') |
|
|
logging.info('loading libsodium from %s', libsodium_path) |
|
|
|
|
|
libsodium = CDLL(libsodium_path) |
|
|
|
|
|
libsodium.sodium_init.restype = c_int |
|
|
|
|
|
libsodium.crypto_stream_salsa20_xor_ic.restype = c_int |
|
|
libsodium.crypto_stream_salsa20_xor_ic.restype = c_int |
|
|
libsodium.crypto_stream_salsa20_xor_ic.argtypes = (c_void_p, c_char_p, |
|
|
libsodium.crypto_stream_salsa20_xor_ic.argtypes = (c_void_p, c_char_p, |
|
|
c_ulonglong, |
|
|
c_ulonglong, |
|
@ -67,8 +58,6 @@ def load_libsodium(): |
|
|
c_char_p, c_ulonglong, |
|
|
c_char_p, c_ulonglong, |
|
|
c_char_p) |
|
|
c_char_p) |
|
|
|
|
|
|
|
|
libsodium.sodium_init() |
|
|
|
|
|
|
|
|
|
|
|
buf = create_string_buffer(buf_size) |
|
|
buf = create_string_buffer(buf_size) |
|
|
loaded = True |
|
|
loaded = True |
|
|
|
|
|
|
|
@ -118,8 +107,6 @@ ciphers = { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_salsa20(): |
|
|
def test_salsa20(): |
|
|
from shadowsocks.crypto import util |
|
|
|
|
|
|
|
|
|
|
|
cipher = Salsa20Crypto(b'salsa20', b'k' * 32, b'i' * 16, 1) |
|
|
cipher = Salsa20Crypto(b'salsa20', b'k' * 32, b'i' * 16, 1) |
|
|
decipher = Salsa20Crypto(b'salsa20', b'k' * 32, b'i' * 16, 0) |
|
|
decipher = Salsa20Crypto(b'salsa20', b'k' * 32, b'i' * 16, 0) |
|
|
|
|
|
|
|
@ -127,7 +114,6 @@ def test_salsa20(): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_chacha20(): |
|
|
def test_chacha20(): |
|
|
from shadowsocks.crypto import util |
|
|
|
|
|
|
|
|
|
|
|
cipher = Salsa20Crypto(b'chacha20', b'k' * 32, b'i' * 16, 1) |
|
|
cipher = Salsa20Crypto(b'chacha20', b'k' * 32, b'i' * 16, 1) |
|
|
decipher = Salsa20Crypto(b'chacha20', b'k' * 32, b'i' * 16, 0) |
|
|
decipher = Salsa20Crypto(b'chacha20', b'k' * 32, b'i' * 16, 0) |
|
|