chacha20poly1305.go 2.8 KB

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