1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package box
- import (
- "io"
- "golang.org/x/crypto/curve25519"
- "golang.org/x/crypto/nacl/secretbox"
- "golang.org/x/crypto/salsa20/salsa"
- )
- const Overhead = secretbox.Overhead
- func GenerateKey(rand io.Reader) (publicKey, privateKey *[32]byte, err error) {
- publicKey = new([32]byte)
- privateKey = new([32]byte)
- _, err = io.ReadFull(rand, privateKey[:])
- if err != nil {
- publicKey = nil
- privateKey = nil
- return
- }
- curve25519.ScalarBaseMult(publicKey, privateKey)
- return
- }
- var zeros [16]byte
- func Precompute(sharedKey, peersPublicKey, privateKey *[32]byte) {
- curve25519.ScalarMult(sharedKey, privateKey, peersPublicKey)
- salsa.HSalsa20(sharedKey, &zeros, sharedKey, &salsa.Sigma)
- }
- func Seal(out, message []byte, nonce *[24]byte, peersPublicKey, privateKey *[32]byte) []byte {
- var sharedKey [32]byte
- Precompute(&sharedKey, peersPublicKey, privateKey)
- return secretbox.Seal(out, message, nonce, &sharedKey)
- }
- func SealAfterPrecomputation(out, message []byte, nonce *[24]byte, sharedKey *[32]byte) []byte {
- return secretbox.Seal(out, message, nonce, sharedKey)
- }
- func Open(out, box []byte, nonce *[24]byte, peersPublicKey, privateKey *[32]byte) ([]byte, bool) {
- var sharedKey [32]byte
- Precompute(&sharedKey, peersPublicKey, privateKey)
- return secretbox.Open(out, box, nonce, &sharedKey)
- }
- func OpenAfterPrecomputation(out, box []byte, nonce *[24]byte, sharedKey *[32]byte) ([]byte, bool) {
- return secretbox.Open(out, box, nonce, sharedKey)
- }
|