1
1
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-10 21:01:02 +02:00

add chacha

This commit is contained in:
Zack Scholl 2021-04-21 17:02:38 -07:00
parent 5b0883e1fe
commit 3359e7996f
3 changed files with 25 additions and 23 deletions

View file

@ -2,6 +2,7 @@ package croc
import (
"bytes"
"crypto/cipher"
"crypto/rand"
"encoding/binary"
"encoding/json"
@ -74,7 +75,7 @@ type Options struct {
type Client struct {
Options Options
Pake *pake.Pake
Key []byte
Key cipher.AEAD
ExternalIP, ExternalIPConnected string
// steps involved in forming relationship
@ -881,7 +882,7 @@ func (c *Client) processMessagePake(m message.Message) (err error) {
if err != nil {
return err
}
c.Key, _, err = crypt.New(key, salt)
c.Key, _, err = crypt.NewArgon2(key, salt)
if err != nil {
return err
}
@ -1419,7 +1420,7 @@ func (c *Client) receiveData(i int) {
continue
}
data, err = crypt.Decrypt(data, c.Key)
data, err = crypt.DecryptChaCha(data, c.Key)
if err != nil {
panic(err)
}
@ -1512,13 +1513,13 @@ func (c *Client) sendData(i int) {
var err error
var dataToSend []byte
if c.Options.NoCompress {
dataToSend, err = crypt.Encrypt(
dataToSend, err = crypt.EncryptChaCha(
append(posByte, data[:n]...),
c.Key,
)
} else {
dataToSend, err = crypt.Encrypt(
dataToSend, err = crypt.EncryptChaCha(
compress.Compress(
append(posByte, data[:n]...),
),

View file

@ -1,6 +1,7 @@
package message
import (
"crypto/cipher"
"encoding/json"
"github.com/schollz/croc/v9/src/comm"
@ -24,7 +25,7 @@ func (m Message) String() string {
}
// Send will send out
func Send(c *comm.Comm, key []byte, m Message) (err error) {
func Send(c *comm.Comm, key cipher.AEAD, m Message) (err error) {
mSend, err := Encode(key, m)
if err != nil {
return
@ -34,7 +35,7 @@ func Send(c *comm.Comm, key []byte, m Message) (err error) {
}
// Encode will convert to bytes
func Encode(key []byte, m Message) (b []byte, err error) {
func Encode(key cipher.AEAD, m Message) (b []byte, err error) {
b, err = json.Marshal(m)
if err != nil {
return
@ -42,7 +43,7 @@ func Encode(key []byte, m Message) (b []byte, err error) {
b = compress.Compress(b)
if key != nil {
log.Debugf("writing %s message (encrypted)", m.Type)
b, err = crypt.Encrypt(b, key)
b, err = crypt.EncryptChaCha(b, key)
} else {
log.Debugf("writing %s message (unencrypted)", m.Type)
}
@ -50,9 +51,9 @@ func Encode(key []byte, m Message) (b []byte, err error) {
}
// Decode will convert from bytes
func Decode(key []byte, b []byte) (m Message, err error) {
func Decode(key cipher.AEAD, b []byte) (m Message, err error) {
if key != nil {
b, err = crypt.Decrypt(b, key)
b, err = crypt.DecryptChaCha(b, key)
if err != nil {
return
}

View file

@ -184,7 +184,7 @@ func (s *server) clientCommunication(port string, c *comm.Comm) (room string, er
if err != nil {
return
}
strongKeyForEncryption, _, err := crypt.New(strongKey, salt)
strongKeyForEncryption, _, err := crypt.NewArgon2(strongKey, salt)
if err != nil {
return
}
@ -194,13 +194,13 @@ func (s *server) clientCommunication(port string, c *comm.Comm) (room string, er
if err != nil {
return
}
passwordBytes, err := crypt.Decrypt(passwordBytesEnc, strongKeyForEncryption)
passwordBytes, err := crypt.DecryptChaCha(passwordBytesEnc, strongKeyForEncryption)
if err != nil {
return
}
if strings.TrimSpace(string(passwordBytes)) != s.password {
err = fmt.Errorf("bad password")
enc, _ := crypt.Decrypt([]byte(err.Error()), strongKeyForEncryption)
enc, _ := crypt.DecryptChaCha([]byte(err.Error()), strongKeyForEncryption)
if err := c.Send(enc); err != nil {
return "", fmt.Errorf("send error: %w", err)
}
@ -213,7 +213,7 @@ func (s *server) clientCommunication(port string, c *comm.Comm) (room string, er
banner = "ok"
}
log.Debugf("sending '%s'", banner)
bSend, err := crypt.Encrypt([]byte(banner+"|||"+c.Connection().RemoteAddr().String()), strongKeyForEncryption)
bSend, err := crypt.EncryptChaCha([]byte(banner+"|||"+c.Connection().RemoteAddr().String()), strongKeyForEncryption)
if err != nil {
return
}
@ -228,7 +228,7 @@ func (s *server) clientCommunication(port string, c *comm.Comm) (room string, er
if err != nil {
return
}
roomBytes, err := crypt.Decrypt(enc, strongKeyForEncryption)
roomBytes, err := crypt.DecryptChaCha(enc, strongKeyForEncryption)
if err != nil {
return
}
@ -244,7 +244,7 @@ func (s *server) clientCommunication(port string, c *comm.Comm) (room string, er
s.rooms.Unlock()
// tell the client that they got the room
bSend, err = crypt.Encrypt([]byte("ok"), strongKeyForEncryption)
bSend, err = crypt.EncryptChaCha([]byte("ok"), strongKeyForEncryption)
if err != nil {
return
}
@ -259,7 +259,7 @@ func (s *server) clientCommunication(port string, c *comm.Comm) (room string, er
}
if s.rooms.rooms[room].full {
s.rooms.Unlock()
bSend, err = crypt.Encrypt([]byte("room full"), strongKeyForEncryption)
bSend, err = crypt.EncryptChaCha([]byte("room full"), strongKeyForEncryption)
if err != nil {
return
}
@ -293,7 +293,7 @@ func (s *server) clientCommunication(port string, c *comm.Comm) (room string, er
}(otherConnection, c, &wg)
// tell the sender everything is ready
bSend, err = crypt.Encrypt([]byte("ok"), strongKeyForEncryption)
bSend, err = crypt.EncryptChaCha([]byte("ok"), strongKeyForEncryption)
if err != nil {
return
}
@ -438,7 +438,7 @@ func ConnectToTCPServer(address, password, room string, timelimit ...time.Durati
}
log.Debugf("strong key: %x", strongKey)
strongKeyForEncryption, salt, err := crypt.New(strongKey, nil)
strongKeyForEncryption, salt, err := crypt.NewArgon2(strongKey, nil)
if err != nil {
return
}
@ -449,7 +449,7 @@ func ConnectToTCPServer(address, password, room string, timelimit ...time.Durati
}
log.Debug("sending password")
bSend, err := crypt.Encrypt([]byte(password), strongKeyForEncryption)
bSend, err := crypt.EncryptChaCha([]byte(password), strongKeyForEncryption)
if err != nil {
return
}
@ -462,7 +462,7 @@ func ConnectToTCPServer(address, password, room string, timelimit ...time.Durati
if err != nil {
return
}
data, err := crypt.Decrypt(enc, strongKeyForEncryption)
data, err := crypt.DecryptChaCha(enc, strongKeyForEncryption)
if err != nil {
return
}
@ -473,7 +473,7 @@ func ConnectToTCPServer(address, password, room string, timelimit ...time.Durati
banner = strings.Split(string(data), "|||")[0]
ipaddr = strings.Split(string(data), "|||")[1]
log.Debug("sending room")
bSend, err = crypt.Encrypt([]byte(room), strongKeyForEncryption)
bSend, err = crypt.EncryptChaCha([]byte(room), strongKeyForEncryption)
if err != nil {
return
}
@ -486,7 +486,7 @@ func ConnectToTCPServer(address, password, room string, timelimit ...time.Durati
if err != nil {
return
}
data, err = crypt.Decrypt(enc, strongKeyForEncryption)
data, err = crypt.DecryptChaCha(enc, strongKeyForEncryption)
if err != nil {
return
}