Skip to content

Commit

Permalink
Add chacha20-ietf crypto (shadowsocks#590)
Browse files Browse the repository at this point in the history
* Add chacha20-ietf crypto

* fix chacha20-ietf

* PEP8

* Update sodium.py (shadowsocks-backup#1)

pep8
  • Loading branch information
smounives authored and mengskysama committed Aug 17, 2016
1 parent 8e8ee5d commit 6ef14e5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name="shadowsocks",
version="2.8.2",
version="2.9.0",
license='http://www.apache.org/licenses/LICENSE-2.0',
description="A fast tunnel proxy that help you get through firewalls",
author='clowwindy',
Expand Down
23 changes: 21 additions & 2 deletions shadowsocks/crypto/sodium.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from __future__ import absolute_import, division, print_function, \
with_statement

from ctypes import c_char_p, c_int, c_ulonglong, byref, \
from ctypes import c_char_p, c_int, c_ulonglong, byref, c_ulong, \
create_string_buffer, c_void_p

from shadowsocks.crypto import util
Expand All @@ -29,7 +29,7 @@

buf_size = 2048

# for salsa20 and chacha20
# for salsa20 and chacha20 and chacha20-ietf
BLOCK_SIZE = 64


Expand All @@ -51,6 +51,13 @@ def load_libsodium():
c_ulonglong,
c_char_p, c_ulonglong,
c_char_p)
libsodium.crypto_stream_chacha20_ietf_xor_ic.restype = c_int
libsodium.crypto_stream_chacha20_ietf_xor_ic.argtypes = (c_void_p,
c_char_p,
c_ulonglong,
c_char_p,
c_ulong,
c_char_p)

buf = create_string_buffer(buf_size)
loaded = True
Expand All @@ -68,6 +75,8 @@ def __init__(self, cipher_name, key, iv, op):
self.cipher = libsodium.crypto_stream_salsa20_xor_ic
elif cipher_name == 'chacha20':
self.cipher = libsodium.crypto_stream_chacha20_xor_ic
elif cipher_name == 'chacha20-ietf':
self.cipher = libsodium.crypto_stream_chacha20_ietf_xor_ic
else:
raise Exception('Unknown cipher')
# byte counter, not block counter
Expand Down Expand Up @@ -97,6 +106,7 @@ def update(self, data):
ciphers = {
'salsa20': (32, 8, SodiumCrypto),
'chacha20': (32, 8, SodiumCrypto),
'chacha20-ietf': (32, 12, SodiumCrypto),
}


Expand All @@ -115,6 +125,15 @@ def test_chacha20():
util.run_cipher(cipher, decipher)


def test_chacha20_ietf():

cipher = SodiumCrypto('chacha20-ietf', b'k' * 32, b'i' * 16, 1)
decipher = SodiumCrypto('chacha20-ietf', b'k' * 32, b'i' * 16, 0)

util.run_cipher(cipher, decipher)


if __name__ == '__main__':
test_chacha20()
test_salsa20()
test_chacha20_ietf()
10 changes: 10 additions & 0 deletions tests/chacha20-ietf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"server":"127.0.0.1",
"server_port":8388,
"local_port":1081,
"password":"salsa20_password",
"timeout":60,
"method":"chacha20-ietf",
"local_address":"127.0.0.1",
"fast_open":false
}
8 changes: 4 additions & 4 deletions tests/libsodium/install.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/bash

if [ ! -d libsodium-1.0.1 ]; then
wget https://github.com/jedisct1/libsodium/releases/download/1.0.1/libsodium-1.0.1.tar.gz || exit 1
tar xf libsodium-1.0.1.tar.gz || exit 1
if [ ! -d libsodium-1.0.11 ]; then
wget https://github.com/jedisct1/libsodium/releases/download/1.0.11/libsodium-1.0.11.tar.gz || exit 1
tar xf libsodium-1.0.11.tar.gz || exit 1
fi
pushd libsodium-1.0.1
pushd libsodium-1.0.11
./configure && make -j2 && make install || exit 1
sudo ldconfig
popd

0 comments on commit 6ef14e5

Please sign in to comment.