wacky encryption

This commit is contained in:
2026-07-22 13:53:30 +02:00
parent c09c5c4fd0
commit c9c4cc0af1
6 changed files with 204 additions and 67 deletions

View File

@@ -1,36 +0,0 @@
package shared
import (
"crypto/rand"
"fmt"
"golang.org/x/crypto/chacha20poly1305"
)
func EncryptSym(key []byte, plaintext []byte) ([]byte, error) {
aead, err := chacha20poly1305.NewX(key)
if err != nil {
return nil, err
}
nonce := make([]byte, chacha20poly1305.NonceSizeX)
if _, err := rand.Read(nonce); err != nil {
return nil, err
}
return aead.Seal(nonce, nonce, plaintext, nil), nil
}
func DecryptSym(key []byte, ciphertext []byte) ([]byte, error) {
aead, err := chacha20poly1305.NewX(key)
if err != nil {
return nil, err
}
if len(ciphertext) < aead.NonceSize() {
return nil, fmt.Errorf("ciphertext too short")
}
nonce, ciphertext := ciphertext[:aead.NonceSize()], ciphertext[aead.NonceSize():]
return aead.Open(nil, nonce, ciphertext, nil)
}

View File

@@ -6,14 +6,30 @@ const PROTO_VERSION uint16 = 1
const (
unused uint16 = iota
// - empty -
// (client -> server) requests an IP
// [ pubkey - 1216 bytes ]
REQ_REGISTER
// (server -> client) assigns an IP
// [ ip - 16 bytes ]
RESP_REGISTER
// [ encrypted pkt ]
REQ_DATA
// [ encrypted pkt ]
RESP_DATA
// (client -> server -> client2) relays an encrypted packet to a specified peer
// [ destIP - 16 bytes ] [ srcIP - 16 bytes ] [ encrypted pkt ]
ENC_PKT
// (client -> server -> *) broadcasts an unsecured packet
// [ plaintext pkt ]
BROADCAST_PKT
// (client -> server) requests peer's pubkey from the server for encapsulation
// [ ip - 16 bytes ]
REQ_GET_PUBKEY
// (server -> client) provides requested pubkey
// [ ip - 16 bytes ] [ pubkey - 1216 bytes ]
RESP_GET_PUBKEY
// (client -> server -> client2) establishes a session key with another peer
// [ destIP - 16 bytes ] [ srcIP - 16 bytes ] [ ciphertext - 1120 bytes ]
REQ_ESTABLISH
// (client2 -> server -> client) acknowledges the session key was established
// [ destIP - 16 bytes ] [ srcIP - 16 bytes ]
RESP_ESTABLISH
)
func BuildPkt(pktType uint16, data []byte) []byte {