chacha20poly1305.go

 1// Copyright 2016 The Go Authors. All rights reserved.
 2// Use of this source code is governed by a BSD-style
 3// license that can be found in the LICENSE file.
 4
 5// Package chacha20poly1305 implements the ChaCha20-Poly1305 AEAD and its
 6// extended nonce variant XChaCha20-Poly1305, as specified in RFC 8439 and
 7// draft-irtf-cfrg-xchacha-01.
 8package chacha20poly1305
 9
10import (
11	"crypto/cipher"
12	"errors"
13)
14
15const (
16	// KeySize is the size of the key used by this AEAD, in bytes.
17	KeySize = 32
18
19	// NonceSize is the size of the nonce used with the standard variant of this
20	// AEAD, in bytes.
21	//
22	// Note that this is too short to be safely generated at random if the same
23	// key is reused more than 2³² times.
24	NonceSize = 12
25
26	// NonceSizeX is the size of the nonce used with the XChaCha20-Poly1305
27	// variant of this AEAD, in bytes.
28	NonceSizeX = 24
29
30	// Overhead is the size of the Poly1305 authentication tag, and the
31	// difference between a ciphertext length and its plaintext.
32	Overhead = 16
33)
34
35type chacha20poly1305 struct {
36	key [KeySize]byte
37}
38
39// New returns a ChaCha20-Poly1305 AEAD that uses the given 256-bit key.
40func New(key []byte) (cipher.AEAD, error) {
41	if len(key) != KeySize {
42		return nil, errors.New("chacha20poly1305: bad key length")
43	}
44	ret := new(chacha20poly1305)
45	copy(ret.key[:], key)
46	return ret, nil
47}
48
49func (c *chacha20poly1305) NonceSize() int {
50	return NonceSize
51}
52
53func (c *chacha20poly1305) Overhead() int {
54	return Overhead
55}
56
57func (c *chacha20poly1305) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
58	if len(nonce) != NonceSize {
59		panic("chacha20poly1305: bad nonce length passed to Seal")
60	}
61
62	if uint64(len(plaintext)) > (1<<38)-64 {
63		panic("chacha20poly1305: plaintext too large")
64	}
65
66	return c.seal(dst, nonce, plaintext, additionalData)
67}
68
69var errOpen = errors.New("chacha20poly1305: message authentication failed")
70
71func (c *chacha20poly1305) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
72	if len(nonce) != NonceSize {
73		panic("chacha20poly1305: bad nonce length passed to Open")
74	}
75	if len(ciphertext) < 16 {
76		return nil, errOpen
77	}
78	if uint64(len(ciphertext)) > (1<<38)-48 {
79		panic("chacha20poly1305: ciphertext too large")
80	}
81
82	return c.open(dst, nonce, ciphertext, additionalData)
83}
84
85// sliceForAppend takes a slice and a requested number of bytes. It returns a
86// slice with the contents of the given slice followed by that many bytes and a
87// second slice that aliases into it and contains only the extra bytes. If the
88// original slice has sufficient capacity then no allocation is performed.
89func sliceForAppend(in []byte, n int) (head, tail []byte) {
90	if total := len(in) + n; cap(in) >= total {
91		head = in[:total]
92	} else {
93		head = make([]byte, total)
94		copy(head, in)
95	}
96	tail = head[len(in):]
97	return
98}