Support Chaha20-Poly1305

This commit is contained in:
Qian Wang 2019-06-14 20:26:26 +10:00
parent 078a382963
commit 00069b7a69
5 changed files with 48 additions and 0 deletions

View File

@ -192,6 +192,12 @@ start:
log.Println(err)
return
}
case 0x02:
crypto, err = mux.MakeCPCipher(sta.UID)
if err != nil {
log.Println(err)
return
}
}
obfs := mux.MakeObfs(sta.UID, crypto)

View File

@ -160,7 +160,15 @@ func dispatchConnection(conn net.Conn, sta *server.State) {
goWeb(data)
return
}
case 0x02:
crypto, err = mux.MakeCPCipher(UID)
if err != nil {
log.Println(err)
goWeb(data)
return
}
default:
log.Println("Unknown encryption method")
goWeb(data)
return
}

View File

@ -112,6 +112,8 @@ func (sta *State) ParseConfig(conf string) (err error) {
sta.EncryptionMethod = 0x00
case "aes":
sta.EncryptionMethod = 0x01
case "chacha20-poly1305":
sta.EncryptionMethod = 0x02
default:
return errors.New("Unknown encryption method")
}

View File

@ -4,6 +4,7 @@ import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"golang.org/x/crypto/chacha20poly1305"
)
type Crypto interface {
@ -60,3 +61,33 @@ func (a *AES) decrypt(ciphertext []byte, nonce []byte) ([]byte, error) {
}
return plain, nil
}
type C20P1305 struct {
cipher cipher.AEAD
}
func MakeCPCipher(key []byte) (*C20P1305, error) {
c, err := chacha20poly1305.New(key)
if err != nil {
return nil, err
}
ret := C20P1305{
c,
}
return &ret, nil
}
func (c *C20P1305) encrypt(plaintext []byte, nonce []byte) ([]byte, error) {
ciphertext := c.cipher.Seal(nil, nonce, plaintext, nil)
ret := make([]byte, len(plaintext)+16)
copy(ret, ciphertext)
return ret, nil
}
func (c *C20P1305) decrypt(ciphertext []byte, nonce []byte) ([]byte, error) {
plain, err := c.cipher.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
return plain, nil
}

View File

@ -47,6 +47,7 @@ func MakeObfs(key []byte, algo Crypto) Obfser {
// Composing final obfsed message
// We don't use util.AddRecordLayer here to avoid unnecessary malloc
// TODO: allocate this in the beginning and do everything in place
obfsed := make([]byte, 5+headerLen+len(encryptedPayload))
obfsed[0] = 0x17
obfsed[1] = 0x03