chacha20poly1305.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 as specified in RFC 7539,
  5. // and its extended nonce variant XChaCha20-Poly1305.
  6. package chacha20poly1305 // import "golang.org/x/crypto/chacha20poly1305"
  7. import (
  8. "crypto/cipher"
  9. "encoding/binary"
  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. )
  25. type chacha20poly1305 struct {
  26. key [8]uint32
  27. }
  28. // New returns a ChaCha20-Poly1305 AEAD that uses the given 256-bit key.
  29. func New(key []byte) (cipher.AEAD, error) {
  30. if len(key) != KeySize {
  31. return nil, errors.New("chacha20poly1305: bad key length")
  32. }
  33. ret := new(chacha20poly1305)
  34. ret.key[0] = binary.LittleEndian.Uint32(key[0:4])
  35. ret.key[1] = binary.LittleEndian.Uint32(key[4:8])
  36. ret.key[2] = binary.LittleEndian.Uint32(key[8:12])
  37. ret.key[3] = binary.LittleEndian.Uint32(key[12:16])
  38. ret.key[4] = binary.LittleEndian.Uint32(key[16:20])
  39. ret.key[5] = binary.LittleEndian.Uint32(key[20:24])
  40. ret.key[6] = binary.LittleEndian.Uint32(key[24:28])
  41. ret.key[7] = binary.LittleEndian.Uint32(key[28:32])
  42. return ret, nil
  43. }
  44. func (c *chacha20poly1305) NonceSize() int {
  45. return NonceSize
  46. }
  47. func (c *chacha20poly1305) Overhead() int {
  48. return 16
  49. }
  50. func (c *chacha20poly1305) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
  51. if len(nonce) != NonceSize {
  52. panic("chacha20poly1305: bad nonce length passed to Seal")
  53. }
  54. if uint64(len(plaintext)) > (1<<38)-64 {
  55. panic("chacha20poly1305: plaintext too large")
  56. }
  57. return c.seal(dst, nonce, plaintext, additionalData)
  58. }
  59. var errOpen = errors.New("chacha20poly1305: message authentication failed")
  60. func (c *chacha20poly1305) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
  61. if len(nonce) != NonceSize {
  62. panic("chacha20poly1305: bad nonce length passed to Open")
  63. }
  64. if len(ciphertext) < 16 {
  65. return nil, errOpen
  66. }
  67. if uint64(len(ciphertext)) > (1<<38)-48 {
  68. panic("chacha20poly1305: ciphertext too large")
  69. }
  70. return c.open(dst, nonce, ciphertext, additionalData)
  71. }
  72. // sliceForAppend takes a slice and a requested number of bytes. It returns a
  73. // slice with the contents of the given slice followed by that many bytes and a
  74. // second slice that aliases into it and contains only the extra bytes. If the
  75. // original slice has sufficient capacity then no allocation is performed.
  76. func sliceForAppend(in []byte, n int) (head, tail []byte) {
  77. if total := len(in) + n; cap(in) >= total {
  78. head = in[:total]
  79. } else {
  80. head = make([]byte, total)
  81. copy(head, in)
  82. }
  83. tail = head[len(in):]
  84. return
  85. }