forked from shadowsocks/go-shadowsocks2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
390 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package shadowstream | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/md5" | ||
"crypto/rc4" | ||
"strconv" | ||
|
||
"golang.org/x/crypto/chacha20" | ||
) | ||
|
||
// Cipher generates a pair of stream ciphers for encryption and decryption. | ||
type Cipher interface { | ||
IVSize() int | ||
Encrypter(iv []byte) cipher.Stream | ||
Decrypter(iv []byte) cipher.Stream | ||
} | ||
|
||
type KeySizeError int | ||
|
||
func (e KeySizeError) Error() string { | ||
return "key size error: need " + strconv.Itoa(int(e)) + " bytes" | ||
} | ||
|
||
// CTR mode | ||
type ctrStream struct{ cipher.Block } | ||
|
||
func (b *ctrStream) IVSize() int { return b.BlockSize() } | ||
func (b *ctrStream) Decrypter(iv []byte) cipher.Stream { return b.Encrypter(iv) } | ||
func (b *ctrStream) Encrypter(iv []byte) cipher.Stream { return cipher.NewCTR(b, iv) } | ||
|
||
func AESCTR(key []byte) (Cipher, error) { | ||
blk, err := aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ctrStream{blk}, nil | ||
} | ||
|
||
// CFB mode | ||
type cfbStream struct{ cipher.Block } | ||
|
||
func (b *cfbStream) IVSize() int { return b.BlockSize() } | ||
func (b *cfbStream) Decrypter(iv []byte) cipher.Stream { return cipher.NewCFBDecrypter(b, iv) } | ||
func (b *cfbStream) Encrypter(iv []byte) cipher.Stream { return cipher.NewCFBEncrypter(b, iv) } | ||
|
||
func AESCFB(key []byte) (Cipher, error) { | ||
blk, err := aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &cfbStream{blk}, nil | ||
} | ||
|
||
// IETF-variant of chacha20 | ||
type chacha20ietfkey []byte | ||
|
||
func (k chacha20ietfkey) IVSize() int { return chacha20.NonceSize } | ||
func (k chacha20ietfkey) Decrypter(iv []byte) cipher.Stream { return k.Encrypter(iv) } | ||
func (k chacha20ietfkey) Encrypter(iv []byte) cipher.Stream { | ||
ciph, err := chacha20.NewUnauthenticatedCipher(k, iv) | ||
if err != nil { | ||
panic(err) // should never happen | ||
} | ||
return ciph | ||
} | ||
|
||
func Chacha20IETF(key []byte) (Cipher, error) { | ||
if len(key) != chacha20.KeySize { | ||
return nil, KeySizeError(chacha20.KeySize) | ||
} | ||
return chacha20ietfkey(key), nil | ||
} | ||
|
||
type xchacha20key []byte | ||
|
||
func (k xchacha20key) IVSize() int { return chacha20.NonceSizeX } | ||
func (k xchacha20key) Decrypter(iv []byte) cipher.Stream { return k.Encrypter(iv) } | ||
func (k xchacha20key) Encrypter(iv []byte) cipher.Stream { | ||
ciph, err := chacha20.NewUnauthenticatedCipher(k, iv) | ||
if err != nil { | ||
panic(err) // should never happen | ||
} | ||
return ciph | ||
} | ||
|
||
func Xchacha20(key []byte) (Cipher, error) { | ||
if len(key) != chacha20.KeySize { | ||
return nil, KeySizeError(chacha20.KeySize) | ||
} | ||
return xchacha20key(key), nil | ||
} | ||
|
||
type rc4Md5Key []byte | ||
|
||
func (k rc4Md5Key) IVSize() int { | ||
return 16 | ||
} | ||
func (k rc4Md5Key) Encrypter(iv []byte) cipher.Stream { | ||
h := md5.New() | ||
h.Write([]byte(k)) | ||
h.Write(iv) | ||
rc4key := h.Sum(nil) | ||
c, _ := rc4.NewCipher(rc4key) | ||
return c | ||
} | ||
func (k rc4Md5Key) Decrypter(iv []byte) cipher.Stream { | ||
return k.Encrypter(iv) | ||
} | ||
func RC4MD5(key []byte) (Cipher, error) { | ||
return rc4Md5Key(key), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package shadowstream | ||
|
||
import ( | ||
"crypto/rand" | ||
"errors" | ||
"io" | ||
"net" | ||
"sync" | ||
) | ||
|
||
// ErrShortPacket means the packet is too short to be a valid encrypted packet. | ||
var ErrShortPacket = errors.New("short packet") | ||
|
||
// Pack encrypts plaintext using stream cipher s and a random IV. | ||
// Returns a slice of dst containing random IV and ciphertext. | ||
// Ensure len(dst) >= s.IVSize() + len(plaintext). | ||
func Pack(dst, plaintext []byte, s Cipher) ([]byte, error) { | ||
if len(dst) < s.IVSize()+len(plaintext) { | ||
return nil, io.ErrShortBuffer | ||
} | ||
iv := dst[:s.IVSize()] | ||
_, err := rand.Read(iv) | ||
if err != nil { | ||
return nil, err | ||
} | ||
s.Encrypter(iv).XORKeyStream(dst[len(iv):], plaintext) | ||
return dst[:len(iv)+len(plaintext)], nil | ||
} | ||
|
||
// Unpack decrypts pkt using stream cipher s. | ||
// Returns a slice of dst containing decrypted plaintext. | ||
func Unpack(dst, pkt []byte, s Cipher) ([]byte, error) { | ||
if len(pkt) < s.IVSize() { | ||
return nil, ErrShortPacket | ||
} | ||
if len(dst) < len(pkt)-s.IVSize() { | ||
return nil, io.ErrShortBuffer | ||
} | ||
iv := pkt[:s.IVSize()] | ||
s.Decrypter(iv).XORKeyStream(dst, pkt[len(iv):]) | ||
return dst[:len(pkt)-len(iv)], nil | ||
} | ||
|
||
type PacketConn struct { | ||
net.PacketConn | ||
Cipher | ||
} | ||
|
||
// NewPacketConn wraps a net.PacketConn with stream cipher encryption/decryption. | ||
func NewPacketConn(c net.PacketConn, ciph Cipher) *PacketConn { | ||
return &PacketConn{PacketConn: c, Cipher: ciph} | ||
} | ||
|
||
const maxPacketSize = 64 * 1024 | ||
|
||
var bufPool = sync.Pool{New: func() any { return make([]byte, maxPacketSize) }} | ||
|
||
func (c *PacketConn) WriteTo(b []byte, addr net.Addr) (int, error) { | ||
buf := bufPool.Get().([]byte) | ||
defer bufPool.Put(buf) | ||
buf, err := Pack(buf, b, c.Cipher) | ||
if err != nil { | ||
return 0, err | ||
} | ||
_, err = c.PacketConn.WriteTo(buf, addr) | ||
return len(b), err | ||
} | ||
|
||
func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error) { | ||
n, addr, err := c.PacketConn.ReadFrom(b) | ||
if err != nil { | ||
return n, addr, err | ||
} | ||
bb, err := Unpack(b[c.IVSize():], b[:n], c.Cipher) | ||
if err != nil { | ||
return n, addr, err | ||
} | ||
copy(b, bb) | ||
return len(bb), addr, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
package shadowstream | ||
|
||
import ( | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"io" | ||
"net" | ||
) | ||
|
||
const bufSize = 2048 | ||
|
||
type Writer struct { | ||
io.Writer | ||
cipher.Stream | ||
buf [bufSize]byte | ||
} | ||
|
||
// NewWriter wraps an io.Writer with stream cipher encryption. | ||
func NewWriter(w io.Writer, s cipher.Stream) *Writer { return &Writer{Writer: w, Stream: s} } | ||
|
||
func (w *Writer) Write(p []byte) (n int, err error) { | ||
buf := w.buf[:] | ||
for nw := 0; n < len(p) && err == nil; n += nw { | ||
end := n + len(buf) | ||
if end > len(p) { | ||
end = len(p) | ||
} | ||
w.XORKeyStream(buf, p[n:end]) | ||
nw, err = w.Writer.Write(buf[:end-n]) | ||
} | ||
return | ||
} | ||
|
||
func (w *Writer) ReadFrom(r io.Reader) (n int64, err error) { | ||
buf := w.buf[:] | ||
for { | ||
nr, er := r.Read(buf) | ||
n += int64(nr) | ||
b := buf[:nr] | ||
w.XORKeyStream(b, b) | ||
if _, err = w.Writer.Write(b); err != nil { | ||
return | ||
} | ||
if er != nil { | ||
if er != io.EOF { // ignore EOF as per io.ReaderFrom contract | ||
err = er | ||
} | ||
return | ||
} | ||
} | ||
} | ||
|
||
type Reader struct { | ||
io.Reader | ||
cipher.Stream | ||
buf [bufSize]byte | ||
} | ||
|
||
// NewReader wraps an io.Reader with stream cipher decryption. | ||
func NewReader(r io.Reader, s cipher.Stream) *Reader { return &Reader{Reader: r, Stream: s} } | ||
|
||
func (r *Reader) Read(p []byte) (n int, err error) { | ||
n, err = r.Reader.Read(p) | ||
if err != nil { | ||
return 0, err | ||
} | ||
r.XORKeyStream(p, p[:n]) | ||
return | ||
} | ||
|
||
func (r *Reader) WriteTo(w io.Writer) (n int64, err error) { | ||
buf := r.buf[:] | ||
for { | ||
nr, er := r.Reader.Read(buf) | ||
if nr > 0 { | ||
r.XORKeyStream(buf, buf[:nr]) | ||
nw, ew := w.Write(buf[:nr]) | ||
n += int64(nw) | ||
if ew != nil { | ||
err = ew | ||
return | ||
} | ||
} | ||
if er != nil { | ||
if er != io.EOF { // ignore EOF as per io.Copy contract (using src.WriteTo shortcut) | ||
err = er | ||
} | ||
return | ||
} | ||
} | ||
} | ||
|
||
// A Conn represents a Shadowsocks connection. It implements the net.Conn interface. | ||
type Conn struct { | ||
net.Conn | ||
Cipher | ||
r *Reader | ||
w *Writer | ||
readIV []byte | ||
writeIV []byte | ||
} | ||
|
||
// NewConn wraps a stream-oriented net.Conn with stream cipher encryption/decryption. | ||
func NewConn(c net.Conn, ciph Cipher) *Conn { return &Conn{Conn: c, Cipher: ciph} } | ||
|
||
func (c *Conn) initReader() error { | ||
if c.r == nil { | ||
iv, err := c.ObtainReadIV() | ||
if err != nil { | ||
return err | ||
} | ||
c.r = NewReader(c.Conn, c.Decrypter(iv)) | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Conn) Read(b []byte) (int, error) { | ||
if c.r == nil { | ||
if err := c.initReader(); err != nil { | ||
return 0, err | ||
} | ||
} | ||
return c.r.Read(b) | ||
} | ||
|
||
func (c *Conn) WriteTo(w io.Writer) (int64, error) { | ||
if c.r == nil { | ||
if err := c.initReader(); err != nil { | ||
return 0, err | ||
} | ||
} | ||
return c.r.WriteTo(w) | ||
} | ||
|
||
func (c *Conn) initWriter() error { | ||
if c.w == nil { | ||
iv, err := c.ObtainWriteIV() | ||
if err != nil { | ||
return err | ||
} | ||
if _, err := c.Conn.Write(iv); err != nil { | ||
return err | ||
} | ||
c.w = NewWriter(c.Conn, c.Encrypter(iv)) | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Conn) Write(b []byte) (int, error) { | ||
if c.w == nil { | ||
if err := c.initWriter(); err != nil { | ||
return 0, err | ||
} | ||
} | ||
return c.w.Write(b) | ||
} | ||
|
||
func (c *Conn) ReadFrom(r io.Reader) (int64, error) { | ||
if c.w == nil { | ||
if err := c.initWriter(); err != nil { | ||
return 0, err | ||
} | ||
} | ||
return c.w.ReadFrom(r) | ||
} | ||
|
||
func (c *Conn) ObtainWriteIV() ([]byte, error) { | ||
if len(c.writeIV) == c.IVSize() { | ||
return c.writeIV, nil | ||
} | ||
|
||
iv := make([]byte, c.IVSize()) | ||
|
||
if _, err := rand.Read(iv); err != nil { | ||
return nil, err | ||
} | ||
|
||
c.writeIV = iv | ||
|
||
return iv, nil | ||
} | ||
|
||
func (c *Conn) ObtainReadIV() ([]byte, error) { | ||
if len(c.readIV) == c.IVSize() { | ||
return c.readIV, nil | ||
} | ||
|
||
iv := make([]byte, c.IVSize()) | ||
|
||
if _, err := io.ReadFull(c.Conn, iv); err != nil { | ||
return nil, err | ||
} | ||
|
||
c.readIV = iv | ||
|
||
return iv, nil | ||
} |