Skip to content

Commit

Permalink
integrate cipher with saltfilter
Browse files Browse the repository at this point in the history
  • Loading branch information
oif committed Feb 15, 2020
1 parent 8c89620 commit 5d517aa
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions shadowaead/cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import (
"crypto/aes"
"crypto/cipher"
"crypto/sha1"
"errors"
"io"
"strconv"

"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/hkdf"
)

// ErrRepeatedSalt means detected a reused salt
var ErrRepeatedSalt = errors.New("repeated salt detected")

type Cipher interface {
KeySize() int
SaltSize() int
Expand Down
7 changes: 7 additions & 0 deletions shadowaead/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io"
"net"
"sync"

"github.com/shadowsocks/go-shadowsocks2/internal"
)

// ErrShortPacket means that the packet is too short for a valid encrypted packet.
Expand All @@ -27,6 +29,7 @@ func Pack(dst, plaintext []byte, ciph Cipher) ([]byte, error) {
if err != nil {
return nil, err
}
internal.AddSalt(salt)

if len(dst) < saltSize+len(plaintext)+aead.Overhead() {
return nil, io.ErrShortBuffer
Expand All @@ -43,10 +46,14 @@ func Unpack(dst, pkt []byte, ciph Cipher) ([]byte, error) {
return nil, ErrShortPacket
}
salt := pkt[:saltSize]
if internal.TestSalt(salt) {
return nil, ErrRepeatedSalt
}
aead, err := ciph.Decrypter(salt)
if err != nil {
return nil, err
}
internal.AddSalt(salt)
if len(pkt) < saltSize+aead.Overhead() {
return nil, ErrShortPacket
}
Expand Down
8 changes: 7 additions & 1 deletion shadowaead/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"crypto/rand"
"io"
"net"

"github.com/shadowsocks/go-shadowsocks2/internal"
)

// payloadSizeMask is the maximum size of payload in bytes.
Expand Down Expand Up @@ -203,11 +205,14 @@ func (c *streamConn) initReader() error {
if _, err := io.ReadFull(c.Conn, salt); err != nil {
return err
}

if internal.TestSalt(salt) {
return ErrRepeatedSalt
}
aead, err := c.Decrypter(salt)
if err != nil {
return err
}
internal.AddSalt(salt)

c.r = newReader(c.Conn, aead)
return nil
Expand Down Expand Up @@ -244,6 +249,7 @@ func (c *streamConn) initWriter() error {
if err != nil {
return err
}
internal.AddSalt(salt)
c.w = newWriter(c.Conn, aead)
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions shadowstream/cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package shadowstream
import (
"crypto/aes"
"crypto/cipher"
"errors"
"strconv"

"github.com/aead/chacha20"
"github.com/aead/chacha20/chacha"
)

// ErrRepeatedSalt means detected a reused salt
var ErrRepeatedSalt = errors.New("repeated salt detected")

// Cipher generates a pair of stream ciphers for encryption and decryption.
type Cipher interface {
IVSize() int
Expand Down
8 changes: 7 additions & 1 deletion shadowstream/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io"
"net"
"sync"

"github.com/shadowsocks/go-shadowsocks2/internal"
)

// ErrShortPacket means the packet is too short to be a valid encrypted packet.
Expand All @@ -23,7 +25,7 @@ func Pack(dst, plaintext []byte, s Cipher) ([]byte, error) {
if err != nil {
return nil, err
}

internal.AddSalt(iv)
s.Encrypter(iv).XORKeyStream(dst[len(iv):], plaintext)
return dst[:len(iv)+len(plaintext)], nil
}
Expand All @@ -39,6 +41,10 @@ func Unpack(dst, pkt []byte, s Cipher) ([]byte, error) {
return nil, io.ErrShortBuffer
}
iv := pkt[:s.IVSize()]
if internal.TestSalt(iv) {
return nil, ErrRepeatedSalt
}
internal.AddSalt(iv)
s.Decrypter(iv).XORKeyStream(dst, pkt[len(iv):])
return dst[:len(pkt)-len(iv)], nil
}
Expand Down
7 changes: 7 additions & 0 deletions shadowstream/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"crypto/rand"
"io"
"net"

"github.com/shadowsocks/go-shadowsocks2/internal"
)

const bufSize = 32 * 1024
Expand Down Expand Up @@ -114,6 +116,10 @@ func (c *conn) initReader() error {
if _, err := io.ReadFull(c.Conn, iv); err != nil {
return err
}
if internal.TestSalt(iv) {
return ErrRepeatedSalt
}
internal.AddSalt(iv)
c.r = &reader{Reader: c.Conn, Stream: c.Decrypter(iv), buf: buf}
}
return nil
Expand Down Expand Up @@ -147,6 +153,7 @@ func (c *conn) initWriter() error {
if _, err := c.Conn.Write(iv); err != nil {
return err
}
internal.AddSalt(iv)
c.w = &writer{Writer: c.Conn, Stream: c.Encrypter(iv), buf: buf}
}
return nil
Expand Down

0 comments on commit 5d517aa

Please sign in to comment.