Package box
Package box authenticates and encrypts small messages using public-key cryptography.
Box uses Curve25519, XSalsa20 and Poly1305 to encrypt and authenticate
messages. The length of messages is not hidden.
It is the caller's responsibility to ensure the uniqueness of nonces—for
example, by using nonce 1 for the first message, nonce 2 for the second
message, etc. Nonces are long enough that randomly generated nonces have
negligible risk of collision.
Messages should be small because:
1. The whole message needs to be held in memory to be processed.
2. Using large messages pressures implementations on small machines to decrypt
and process plaintext before authenticating it. This is very dangerous, and
this API does not allow it, but a protocol that uses excessive message sizes
might present some implementations with no other choice.
3. Fixed overheads will be sufficiently amortised by messages as small as 8KB.
4. Performance may be improved by working with messages that fit into data caches.
Thus large amounts of data should be chunked so that each message is small.
(Each message still needs a unique nonce.) If in doubt, 16KB is a reasonable
chunk size.
This package is interoperable with NaCl: https://nacl.cr.yp.to/box.html.
Anonymous sealing/opening is an extension of NaCl defined by and interoperable
with libsodium:
https://libsodium.gitbook.io/doc/public-key_cryptography/sealed_boxes.
▾ Example
Code:
senderPublicKey, senderPrivateKey, err := box.GenerateKey(crypto_rand.Reader)
if err != nil {
panic(err)
}
recipientPublicKey, recipientPrivateKey, err := box.GenerateKey(crypto_rand.Reader)
if err != nil {
panic(err)
}
var nonce [24]byte
if _, err := io.ReadFull(crypto_rand.Reader, nonce[:]); err != nil {
panic(err)
}
msg := []byte("Alas, poor Yorick! I knew him, Horatio")
encrypted := box.Seal(nonce[:], msg, &nonce, recipientPublicKey, senderPrivateKey)
var decryptNonce [24]byte
copy(decryptNonce[:], encrypted[:24])
decrypted, ok := box.Open(nil, encrypted[24:], &decryptNonce, senderPublicKey, recipientPrivateKey)
if !ok {
panic("decryption error")
}
fmt.Println(string(decrypted))
Output:
Alas, poor Yorick! I knew him, Horatio
▾ Example (Precompute)
Code:
senderPublicKey, senderPrivateKey, err := box.GenerateKey(crypto_rand.Reader)
if err != nil {
panic(err)
}
recipientPublicKey, recipientPrivateKey, err := box.GenerateKey(crypto_rand.Reader)
if err != nil {
panic(err)
}
sharedEncryptKey := new([32]byte)
box.Precompute(sharedEncryptKey, recipientPublicKey, senderPrivateKey)
var nonce [24]byte
if _, err := io.ReadFull(crypto_rand.Reader, nonce[:]); err != nil {
panic(err)
}
msg := []byte("A fellow of infinite jest, of most excellent fancy")
encrypted := box.SealAfterPrecomputation(nonce[:], msg, &nonce, sharedEncryptKey)
var sharedDecryptKey [32]byte
box.Precompute(&sharedDecryptKey, senderPublicKey, recipientPrivateKey)
var decryptNonce [24]byte
copy(decryptNonce[:], encrypted[:24])
decrypted, ok := box.OpenAfterPrecomputation(nil, encrypted[24:], &decryptNonce, &sharedDecryptKey)
if !ok {
panic("decryption error")
}
fmt.Println(string(decrypted))
Output:
A fellow of infinite jest, of most excellent fancy
- Constants
- func GenerateKey(rand io.Reader) (publicKey, privateKey *[32]byte, err error)
- func Open(out, box []byte, nonce *[24]byte, peersPublicKey, privateKey *[32]byte) ([]byte, bool)
- func OpenAfterPrecomputation(out, box []byte, nonce *[24]byte, sharedKey *[32]byte) ([]byte, bool)
- func OpenAnonymous(out, box []byte, publicKey, privateKey *[32]byte) (message []byte, ok bool)
- func Precompute(sharedKey, peersPublicKey, privateKey *[32]byte)
- func Seal(out, message []byte, nonce *[24]byte, peersPublicKey, privateKey *[32]byte) []byte
- func SealAfterPrecomputation(out, message []byte, nonce *[24]byte, sharedKey *[32]byte) []byte
- func SealAnonymous(out, message []byte, recipient *[32]byte, rand io.Reader) ([]byte, error)
Package files
box.go
Constants
const (
Overhead = secretbox.Overhead
AnonymousOverhead = Overhead + 32
)
func GenerateKey(rand io.Reader) (publicKey, privateKey *[32]byte, err error)
GenerateKey generates a new public/private key pair suitable for use with
Seal and Open.
func Open(out, box []byte, nonce *[24]byte, peersPublicKey, privateKey *[32]byte) ([]byte, bool)
Open authenticates and decrypts a box produced by Seal and appends the
message to out, which must not overlap box. The output will be Overhead
bytes smaller than box.
func OpenAfterPrecomputation(out, box []byte, nonce *[24]byte, sharedKey *[32]byte) ([]byte, bool)
OpenAfterPrecomputation performs the same actions as Open, but takes a
shared key as generated by Precompute.
func OpenAnonymous(out, box []byte, publicKey, privateKey *[32]byte) (message []byte, ok bool)
OpenAnonymous authenticates and decrypts a box produced by SealAnonymous and
appends the message to out, which must not overlap box. The output will be
AnonymousOverhead bytes smaller than box.
func Precompute(sharedKey, peersPublicKey, privateKey *[32]byte)
Precompute calculates the shared key between peersPublicKey and privateKey
and writes it to sharedKey. The shared key can be used with
OpenAfterPrecomputation and SealAfterPrecomputation to speed up processing
when using the same pair of keys repeatedly.
func Seal(out, message []byte, nonce *[24]byte, peersPublicKey, privateKey *[32]byte) []byte
Seal appends an encrypted and authenticated copy of message to out, which
will be Overhead bytes longer than the original and must not overlap it. The
nonce must be unique for each distinct message for a given pair of keys.
func SealAfterPrecomputation(out, message []byte, nonce *[24]byte, sharedKey *[32]byte) []byte
SealAfterPrecomputation performs the same actions as Seal, but takes a
shared key as generated by Precompute.
func SealAnonymous(out, message []byte, recipient *[32]byte, rand io.Reader) ([]byte, error)
SealAnonymous appends an encrypted and authenticated copy of message to out,
which will be AnonymousOverhead bytes longer than the original and must not
overlap it. This differs from Seal in that the sender is not required to
provide a private key.