vppn/node/cipher-routing.go
2024-12-19 20:53:52 +01:00

27 lines
783 B
Go

package node
import "golang.org/x/crypto/nacl/box"
type routingCipher struct {
sharedKey [32]byte
}
func newRoutingCipher(privKey, pubKey []byte) routingCipher {
shared := [32]byte{}
box.Precompute(&shared, (*[32]byte)(pubKey), (*[32]byte)(privKey))
return routingCipher{shared}
}
func (rc routingCipher) Encrypt(h xHeader, data, out []byte) []byte {
const s = routingHeaderSize
out = out[:s+routingCipherOverhead+len(data)]
h.Marshal(routingStreamID, out[:s])
box.SealAfterPrecomputation(out[s:s], data, (*[24]byte)(out[:s]), &rc.sharedKey)
return out
}
func (rc routingCipher) Decrypt(encrypted, out []byte) (data []byte, ok bool) {
const s = routingHeaderSize
return box.OpenAfterPrecomputation(out[:0], encrypted[s:], (*[24]byte)(encrypted[:s]), &rc.sharedKey)
}