The XChaCha20 construction does not have an authoritative spec, but this implementation is based on the following documents: https://cr.yp.to/snuffle/xsalsa-20081128.pdf https://download.libsodium.org/doc/secret-key_cryptography/aead.html http://loup-vaillant.fr/tutorials/chacha20-design https://tools.ietf.org/html/draft-paragon-paseto-rfc-00#section-7 Tested against the following implementations: https://github.com/jedisct1/libsodium/blob/7cdf3f0e841/test/default/aead_xchacha20poly1305.c https://git.kernel.org/pub/scm/linux/kernel/git/zx2c4/linux.git/diff/lib/zinc/selftest/chacha20poly1305.h?h=zinc https://git.zx2c4.com/wireguard-go/tree/xchacha20poly1305/xchacha20.go name time/op speed Chacha20Poly1305/Open-64-8 225ns ± 1% 283MB/s ± 1% Chacha20Poly1305/Open-64-X-8 390ns ± 0% 164MB/s ± 0% Chacha20Poly1305/Seal-64-8 222ns ± 0% 287MB/s ± 0% Chacha20Poly1305/Seal-64-X-8 386ns ± 0% 165MB/s ± 1% Chacha20Poly1305/Open-1350-8 1.12µs ± 1% 1.21GB/s ± 1% Chacha20Poly1305/Open-1350-X-8 1.28µs ± 0% 1.05GB/s ± 0% Chacha20Poly1305/Seal-1350-8 1.15µs ± 0% 1.17GB/s ± 0% Chacha20Poly1305/Seal-1350-X-8 1.32µs ± 1% 1.02GB/s ± 0% Chacha20Poly1305/Open-8192-8 5.53µs ± 0% 1.48GB/s ± 0% Chacha20Poly1305/Open-8192-X-8 5.71µs ± 1% 1.44GB/s ± 1% Chacha20Poly1305/Seal-8192-8 5.54µs ± 1% 1.48GB/s ± 1% Chacha20Poly1305/Seal-8192-X-8 5.74µs ± 1% 1.43GB/s ± 1% Updates golang/go#24485 Change-Id: Iea6f3b4c2be67f16f56720a200dcc895c0f9d520 Reviewed-on: https://go-review.googlesource.com/127819 Run-TryBot: Filippo Valsorda <filippo@golang.org> Reviewed-by: Adam Langley <agl@golang.org>
111 lines
3.4 KiB
Go
111 lines
3.4 KiB
Go
// Copyright 2018 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 chacha20poly1305
|
|
|
|
import (
|
|
"crypto/cipher"
|
|
"encoding/binary"
|
|
"errors"
|
|
|
|
"golang.org/x/crypto/internal/chacha20"
|
|
)
|
|
|
|
const (
|
|
// NonceSizeX is the size of the nonce used with the XChaCha20-Poly1305
|
|
// variant of this AEAD, in bytes.
|
|
NonceSizeX = 24
|
|
)
|
|
|
|
type xchacha20poly1305 struct {
|
|
key [8]uint32
|
|
}
|
|
|
|
// NewX returns a XChaCha20-Poly1305 AEAD that uses the given 256-bit key.
|
|
//
|
|
// XChaCha20-Poly1305 is a ChaCha20-Poly1305 variant that takes a longer nonce,
|
|
// suitable to be generated randomly without risk of collisions. It should be
|
|
// preferred when nonce uniqueness cannot be trivially ensured, or whenever
|
|
// nonces are randomly generated.
|
|
func NewX(key []byte) (cipher.AEAD, error) {
|
|
if len(key) != KeySize {
|
|
return nil, errors.New("chacha20poly1305: bad key length")
|
|
}
|
|
ret := new(xchacha20poly1305)
|
|
ret.key[0] = binary.LittleEndian.Uint32(key[0:4])
|
|
ret.key[1] = binary.LittleEndian.Uint32(key[4:8])
|
|
ret.key[2] = binary.LittleEndian.Uint32(key[8:12])
|
|
ret.key[3] = binary.LittleEndian.Uint32(key[12:16])
|
|
ret.key[4] = binary.LittleEndian.Uint32(key[16:20])
|
|
ret.key[5] = binary.LittleEndian.Uint32(key[20:24])
|
|
ret.key[6] = binary.LittleEndian.Uint32(key[24:28])
|
|
ret.key[7] = binary.LittleEndian.Uint32(key[28:32])
|
|
return ret, nil
|
|
}
|
|
|
|
func (*xchacha20poly1305) NonceSize() int {
|
|
return NonceSizeX
|
|
}
|
|
|
|
func (*xchacha20poly1305) Overhead() int {
|
|
return 16
|
|
}
|
|
|
|
func (x *xchacha20poly1305) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
|
|
if len(nonce) != NonceSizeX {
|
|
panic("chacha20poly1305: bad nonce length passed to Seal")
|
|
}
|
|
|
|
// XChaCha20-Poly1305 technically supports a 64-bit counter, so there is no
|
|
// size limit. However, since we reuse the ChaCha20-Poly1305 implementation,
|
|
// the second half of the counter is not available. This is unlikely to be
|
|
// an issue because the cipher.AEAD API requires the entire message to be in
|
|
// memory, and the counter overflows at 256 GB.
|
|
if uint64(len(plaintext)) > (1<<38)-64 {
|
|
panic("chacha20poly1305: plaintext too large")
|
|
}
|
|
|
|
hNonce := [4]uint32{
|
|
binary.LittleEndian.Uint32(nonce[0:4]),
|
|
binary.LittleEndian.Uint32(nonce[4:8]),
|
|
binary.LittleEndian.Uint32(nonce[8:12]),
|
|
binary.LittleEndian.Uint32(nonce[12:16]),
|
|
}
|
|
c := &chacha20poly1305{
|
|
key: chacha20.HChaCha20(&x.key, &hNonce),
|
|
}
|
|
// The first 4 bytes of the final nonce are unused counter space.
|
|
cNonce := make([]byte, NonceSize)
|
|
copy(cNonce[4:12], nonce[16:24])
|
|
|
|
return c.seal(dst, cNonce[:], plaintext, additionalData)
|
|
}
|
|
|
|
func (x *xchacha20poly1305) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
|
|
if len(nonce) != NonceSizeX {
|
|
panic("chacha20poly1305: bad nonce length passed to Open")
|
|
}
|
|
if len(ciphertext) < 16 {
|
|
return nil, errOpen
|
|
}
|
|
if uint64(len(ciphertext)) > (1<<38)-48 {
|
|
panic("chacha20poly1305: ciphertext too large")
|
|
}
|
|
|
|
hNonce := [4]uint32{
|
|
binary.LittleEndian.Uint32(nonce[0:4]),
|
|
binary.LittleEndian.Uint32(nonce[4:8]),
|
|
binary.LittleEndian.Uint32(nonce[8:12]),
|
|
binary.LittleEndian.Uint32(nonce[12:16]),
|
|
}
|
|
c := &chacha20poly1305{
|
|
key: chacha20.HChaCha20(&x.key, &hNonce),
|
|
}
|
|
// The first 4 bytes of the final nonce are unused counter space.
|
|
cNonce := make([]byte, NonceSize)
|
|
copy(cNonce[4:12], nonce[16:24])
|
|
|
|
return c.open(dst, cNonce[:], ciphertext, additionalData)
|
|
}
|