forked from riobard/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
14 changed files
with
337 additions
and
222 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,19 @@ | ||
// Package core provides essential interfaces for Shadowsocks | ||
package core | ||
|
||
import ( | ||
"net" | ||
) | ||
|
||
type Cipher interface { | ||
StreamConnCipher | ||
PacketConnCipher | ||
} | ||
|
||
type StreamConnCipher interface { | ||
StreamConn(net.Conn) net.Conn | ||
} | ||
|
||
type PacketConnCipher interface { | ||
PacketConn(net.PacketConn) net.PacketConn | ||
} |
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
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
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
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 shadowaead | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/sha1" | ||
"io" | ||
"strconv" | ||
|
||
"golang.org/x/crypto/chacha20poly1305" | ||
"golang.org/x/crypto/hkdf" | ||
) | ||
|
||
type Cipher interface { | ||
KeySize() int | ||
SaltSize() int | ||
Encrypter(salt []byte) (cipher.AEAD, error) | ||
Decrypter(salt []byte) (cipher.AEAD, error) | ||
} | ||
|
||
type KeySizeError int | ||
|
||
func (e KeySizeError) Error() string { | ||
return "key size error: need " + strconv.Itoa(int(e)) + " bytes" | ||
} | ||
|
||
func hkdfSHA1(secret, salt, info, outkey []byte) { | ||
r := hkdf.New(sha1.New, secret, salt, info) | ||
if _, err := io.ReadFull(r, outkey); err != nil { | ||
panic(err) // should never happen | ||
} | ||
} | ||
|
||
type metaCipher struct { | ||
psk []byte | ||
makeAEAD func(key []byte) (cipher.AEAD, error) | ||
} | ||
|
||
func (a *metaCipher) KeySize() int { return len(a.psk) } | ||
func (a *metaCipher) SaltSize() int { | ||
if ks := a.KeySize(); ks > 16 { | ||
return ks | ||
} | ||
return 16 | ||
} | ||
func (a *metaCipher) Encrypter(salt []byte) (cipher.AEAD, error) { | ||
subkey := make([]byte, a.KeySize()) | ||
hkdfSHA1(a.psk, salt, []byte("ss-subkey"), subkey) | ||
aead, err := a.makeAEAD(subkey) | ||
return aead, err | ||
} | ||
func (a *metaCipher) Decrypter(salt []byte) (cipher.AEAD, error) { | ||
subkey := make([]byte, a.KeySize()) | ||
hkdfSHA1(a.psk, salt, []byte("ss-subkey"), subkey) | ||
return a.makeAEAD(subkey) | ||
} | ||
|
||
func aesGCM(key []byte) (cipher.AEAD, error) { | ||
blk, err := aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return cipher.NewGCM(blk) | ||
} | ||
|
||
func AESGCM(psk []byte) (Cipher, error) { | ||
switch l := len(psk); l { | ||
case 16, 24, 32: // AES 128/196/256 | ||
default: | ||
return nil, aes.KeySizeError(l) | ||
} | ||
return &metaCipher{psk: psk, makeAEAD: aesGCM}, nil | ||
} | ||
|
||
func Chacha20IETFPoly1305(psk []byte) (Cipher, error) { | ||
if len(psk) != chacha20poly1305.KeySize { | ||
return nil, KeySizeError(chacha20poly1305.KeySize) | ||
} | ||
return &metaCipher{psk: psk, makeAEAD: chacha20poly1305.New}, nil | ||
} |
Oops, something went wrong.