|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/aes" |
| 5 | + "crypto/cipher" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "golang.org/x/crypto/chacha20poly1305" |
| 12 | + |
| 13 | + "github.com/Yawning/chacha20" |
| 14 | + "github.com/riobard/go-shadowsocks2/core" |
| 15 | + "github.com/riobard/go-shadowsocks2/shadowaead" |
| 16 | + "github.com/riobard/go-shadowsocks2/shadowstream" |
| 17 | +) |
| 18 | + |
| 19 | +// ErrKeySize means the supplied key size does not meet the requirement of cipher choosed. |
| 20 | +var ErrKeySize = errors.New("key size error") |
| 21 | + |
| 22 | +func pickCipher(name string, key []byte) (core.StreamConnCipher, core.PacketConnCipher, error) { |
| 23 | + |
| 24 | + switch strings.ToLower(name) { |
| 25 | + case "aes-128-gcm", "aes-192-gcm", "aes-256-gcm": |
| 26 | + aead, err := aesGCM(key, 0) // 0 for standard 12-byte nonce |
| 27 | + return aeadStream(aead), aeadPacket(aead), err |
| 28 | + |
| 29 | + case "aes-128-gcm-16", "aes-192-gcm-16", "aes-256-gcm-16": |
| 30 | + aead, err := aesGCM(key, 16) // 16-byte nonce for better collision avoidance |
| 31 | + return aeadStream(aead), aeadPacket(aead), err |
| 32 | + |
| 33 | + case "chacha20-ietf-poly1305": |
| 34 | + aead, err := chacha20poly1305.New(key) |
| 35 | + return aeadStream(aead), aeadPacket(aead), err |
| 36 | + |
| 37 | + case "aes-128-ctr", "aes-192-ctr", "aes-256-ctr": |
| 38 | + ciph, err := aesCTR(key) |
| 39 | + return streamStream(ciph), streamPacket(ciph), err |
| 40 | + |
| 41 | + case "aes-128-cfb", "aes-192-cfb", "aes-256-cfb": |
| 42 | + ciph, err := aesCFB(key) |
| 43 | + return streamStream(ciph), streamPacket(ciph), err |
| 44 | + |
| 45 | + case "chacha20-ietf": |
| 46 | + if len(key) != chacha20.KeySize { |
| 47 | + return nil, nil, ErrKeySize |
| 48 | + } |
| 49 | + k := chacha20ietfkey(key) |
| 50 | + return streamStream(k), streamPacket(k), nil |
| 51 | + |
| 52 | + case "dummy": // only for benchmarking and debugging |
| 53 | + return dummyStream(), dummyPacket(), nil |
| 54 | + |
| 55 | + default: |
| 56 | + err := fmt.Errorf("cipher not supported: %s", name) |
| 57 | + return nil, nil, err |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func dummyStream() core.StreamConnCipher { |
| 62 | + return func(c net.Conn) net.Conn { return c } |
| 63 | +} |
| 64 | +func dummyPacket() core.PacketConnCipher { |
| 65 | + return func(c net.PacketConn) net.PacketConn { return c } |
| 66 | +} |
| 67 | + |
| 68 | +func aeadStream(aead cipher.AEAD) core.StreamConnCipher { |
| 69 | + return func(c net.Conn) net.Conn { return shadowaead.NewConn(c, aead) } |
| 70 | +} |
| 71 | +func aeadPacket(aead cipher.AEAD) core.PacketConnCipher { |
| 72 | + return func(c net.PacketConn) net.PacketConn { return shadowaead.NewPacketConn(c, aead) } |
| 73 | +} |
| 74 | + |
| 75 | +func aesGCM(key []byte, nonceSize int) (cipher.AEAD, error) { |
| 76 | + blk, err := aes.NewCipher(key) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + if nonceSize > 0 { |
| 81 | + return cipher.NewGCMWithNonceSize(blk, nonceSize) |
| 82 | + } |
| 83 | + return cipher.NewGCM(blk) // standard 12-byte nonce |
| 84 | +} |
| 85 | + |
| 86 | +func streamStream(ciph shadowstream.Cipher) core.StreamConnCipher { |
| 87 | + return func(c net.Conn) net.Conn { return shadowstream.NewConn(c, ciph) } |
| 88 | +} |
| 89 | + |
| 90 | +func streamPacket(ciph shadowstream.Cipher) core.PacketConnCipher { |
| 91 | + return func(c net.PacketConn) net.PacketConn { return shadowstream.NewPacketConn(c, ciph) } |
| 92 | +} |
| 93 | + |
| 94 | +type ctrStream struct{ cipher.Block } |
| 95 | + |
| 96 | +func (b *ctrStream) IVSize() int { return b.BlockSize() } |
| 97 | +func (b *ctrStream) Encrypter(iv []byte) cipher.Stream { return cipher.NewCTR(b, iv) } |
| 98 | +func (b *ctrStream) Decrypter(iv []byte) cipher.Stream { return b.Encrypter(iv) } |
| 99 | + |
| 100 | +func aesCTR(key []byte) (shadowstream.Cipher, error) { |
| 101 | + blk, err := aes.NewCipher(key) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + return &ctrStream{blk}, nil |
| 106 | +} |
| 107 | + |
| 108 | +type cfbStream struct{ cipher.Block } |
| 109 | + |
| 110 | +func (b *cfbStream) IVSize() int { return b.BlockSize() } |
| 111 | +func (b *cfbStream) Encrypter(iv []byte) cipher.Stream { return cipher.NewCFBEncrypter(b, iv) } |
| 112 | +func (b *cfbStream) Decrypter(iv []byte) cipher.Stream { return cipher.NewCFBDecrypter(b, iv) } |
| 113 | + |
| 114 | +func aesCFB(key []byte) (shadowstream.Cipher, error) { |
| 115 | + blk, err := aes.NewCipher(key) |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + return &ctrStream{blk}, nil |
| 120 | +} |
| 121 | + |
| 122 | +type chacha20ietfkey []byte |
| 123 | + |
| 124 | +func (k chacha20ietfkey) IVSize() int { return chacha20.INonceSize } |
| 125 | +func (k chacha20ietfkey) Encrypter(iv []byte) cipher.Stream { |
| 126 | + ciph, err := chacha20.NewCipher(k, iv) |
| 127 | + if err != nil { |
| 128 | + panic(err) |
| 129 | + } |
| 130 | + return ciph |
| 131 | +} |
| 132 | +func (k chacha20ietfkey) Decrypter(iv []byte) cipher.Stream { return k.Encrypter(iv) } |
0 commit comments