// Copyright 2012 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.
/*Package secretbox encrypts and authenticates small messages.Secretbox uses XSalsa20 and Poly1305 to encrypt and authenticate messages withsecret-key cryptography. The length of messages is not hidden.It is the caller's responsibility to ensure the uniqueness of nonces—forexample, by using nonce 1 for the first message, nonce 2 for the secondmessage, etc. Nonces are long enough that randomly generated nonces havenegligible 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 decryptand process plaintext before authenticating it. This is very dangerous, andthis API does not allow it, but a protocol that uses excessive message sizesmight 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 reasonablechunk size.This package is interoperable with NaCl: https://nacl.cr.yp.to/secretbox.html.*/
package secretboximport ()// Overhead is the number of bytes of overhead when boxing a message.constOverhead = poly1305.TagSize// setup produces a sub-key and Salsa20 counter given a nonce and key.func setup( *[32]byte, *[16]byte, *[24]byte, *[32]byte) {// We use XSalsa20 for encryption so first we need to generate a // key and nonce with HSalsa20.var [16]bytecopy([:], [:])salsa.HSalsa20(, &, , &salsa.Sigma)// The final 8 bytes of the original nonce form the new nonce.copy([:], [16:])}// sliceForAppend takes a slice and a requested number of bytes. It returns a// slice with the contents of the given slice followed by that many bytes and a// second slice that aliases into it and contains only the extra bytes. If the// original slice has sufficient capacity then no allocation is performed.func sliceForAppend( []byte, int) (, []byte) {if := len() + ; cap() >= { = [:] } else { = make([]byte, )copy(, ) } = [len():]return}// Seal appends an encrypted and authenticated copy of message to out, which// must not overlap message. The key and nonce pair must be unique for each// distinct message and the output will be Overhead bytes longer than message.func (, []byte, *[24]byte, *[32]byte) []byte {var [32]bytevar [16]bytesetup(&, &, , )// The Poly1305 key is generated by encrypting 32 bytes of zeros. Since // Salsa20 works with 64-byte blocks, we also generate 32 bytes of // keystream as a side effect.var [64]bytesalsa.XORKeyStream([:], [:], &, &)var [32]bytecopy([:], [:]) , := sliceForAppend(, len()+poly1305.TagSize)ifalias.AnyOverlap(, ) {panic("nacl: invalid buffer overlap") }// We XOR up to 32 bytes of message with the keystream generated from // the first block. := iflen() > 32 { = [:32] } := = [poly1305.TagSize:]for , := range { [] = [32+] ^ } = [len():] := = [len():]// Now encrypt the rest. [8] = 1salsa.XORKeyStream(, , &, &)var [poly1305.TagSize]bytepoly1305.Sum(&, , &)copy(, [:])return}// 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 (, []byte, *[24]byte, *[32]byte) ([]byte, bool) {iflen() < Overhead {returnnil, false }var [32]bytevar [16]bytesetup(&, &, , )// The Poly1305 key is generated by encrypting 32 bytes of zeros. Since // Salsa20 works with 64-byte blocks, we also generate 32 bytes of // keystream as a side effect.var [64]bytesalsa.XORKeyStream([:], [:], &, &)var [32]bytecopy([:], [:])var [poly1305.TagSize]bytecopy([:], )if !poly1305.Verify(&, [poly1305.TagSize:], &) {returnnil, false } , := sliceForAppend(, len()-Overhead)ifalias.AnyOverlap(, ) {panic("nacl: invalid buffer overlap") }// We XOR up to 32 bytes of box with the keystream generated from // the first block. = [Overhead:] := iflen() > 32 { = [:32] }for , := range { [] = [32+] ^ } = [len():] = [len():]// Now decrypt the rest. [8] = 1salsa.XORKeyStream(, , &, &)return , true}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.