Skip to content

Commit 6ef14e5

Browse files
smounivesmengskysama
authored andcommitted
Add chacha20-ietf crypto (shadowsocks#590)
* Add chacha20-ietf crypto * fix chacha20-ietf * PEP8 * Update sodium.py (shadowsocks-backup#1) pep8
1 parent 8e8ee5d commit 6ef14e5

4 files changed

Lines changed: 36 additions & 7 deletions

File tree

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name="shadowsocks",
10-
version="2.8.2",
10+
version="2.9.0",
1111
license='http://www.apache.org/licenses/LICENSE-2.0',
1212
description="A fast tunnel proxy that help you get through firewalls",
1313
author='clowwindy',

shadowsocks/crypto/sodium.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from __future__ import absolute_import, division, print_function, \
1818
with_statement
1919

20-
from ctypes import c_char_p, c_int, c_ulonglong, byref, \
20+
from ctypes import c_char_p, c_int, c_ulonglong, byref, c_ulong, \
2121
create_string_buffer, c_void_p
2222

2323
from shadowsocks.crypto import util
@@ -29,7 +29,7 @@
2929

3030
buf_size = 2048
3131

32-
# for salsa20 and chacha20
32+
# for salsa20 and chacha20 and chacha20-ietf
3333
BLOCK_SIZE = 64
3434

3535

@@ -51,6 +51,13 @@ def load_libsodium():
5151
c_ulonglong,
5252
c_char_p, c_ulonglong,
5353
c_char_p)
54+
libsodium.crypto_stream_chacha20_ietf_xor_ic.restype = c_int
55+
libsodium.crypto_stream_chacha20_ietf_xor_ic.argtypes = (c_void_p,
56+
c_char_p,
57+
c_ulonglong,
58+
c_char_p,
59+
c_ulong,
60+
c_char_p)
5461

5562
buf = create_string_buffer(buf_size)
5663
loaded = True
@@ -68,6 +75,8 @@ def __init__(self, cipher_name, key, iv, op):
6875
self.cipher = libsodium.crypto_stream_salsa20_xor_ic
6976
elif cipher_name == 'chacha20':
7077
self.cipher = libsodium.crypto_stream_chacha20_xor_ic
78+
elif cipher_name == 'chacha20-ietf':
79+
self.cipher = libsodium.crypto_stream_chacha20_ietf_xor_ic
7180
else:
7281
raise Exception('Unknown cipher')
7382
# byte counter, not block counter
@@ -97,6 +106,7 @@ def update(self, data):
97106
ciphers = {
98107
'salsa20': (32, 8, SodiumCrypto),
99108
'chacha20': (32, 8, SodiumCrypto),
109+
'chacha20-ietf': (32, 12, SodiumCrypto),
100110
}
101111

102112

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

117127

128+
def test_chacha20_ietf():
129+
130+
cipher = SodiumCrypto('chacha20-ietf', b'k' * 32, b'i' * 16, 1)
131+
decipher = SodiumCrypto('chacha20-ietf', b'k' * 32, b'i' * 16, 0)
132+
133+
util.run_cipher(cipher, decipher)
134+
135+
118136
if __name__ == '__main__':
119137
test_chacha20()
120138
test_salsa20()
139+
test_chacha20_ietf()

tests/chacha20-ietf.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"server":"127.0.0.1",
3+
"server_port":8388,
4+
"local_port":1081,
5+
"password":"salsa20_password",
6+
"timeout":60,
7+
"method":"chacha20-ietf",
8+
"local_address":"127.0.0.1",
9+
"fast_open":false
10+
}

tests/libsodium/install.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/bin/bash
22

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

0 commit comments

Comments
 (0)