chacha20poly1305.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. package chacha20poly1305 // import "golang.org/x/crypto/chacha20poly1305"
  6. import (
  7. "crypto/cipher"
  8. "encoding/binary"
  9. "errors"
  10. )
  11. const (
  12. // KeySize is the size of the key used by this AEAD, in bytes.
  13. KeySize = 32
  14. // NonceSize is the size of the nonce used with the standard variant of this
  15. // AEAD, in bytes.
  16. //
  17. // Note that this is too short to be safely generated at random if the same
  18. // key is reused more than 2³² times.
  19. NonceSize = 12
  20. )
  21. type chacha20poly1305 struct {
  22. key [8]uint32
  23. }
  24. // New returns a ChaCha20-Poly1305 AEAD that uses the given 256-bit key.
  25. func New(key []byte) (cipher.AEAD, error) {
  26. if len(key) != KeySize {
  27. return nil, errors.New("chacha20poly1305: bad key length")
  28. }
  29. ret := new(chacha20poly1305)
  30. ret.key[0] = binary.LittleEndian.Uint32(key[0:4])
  31. ret.key[1] = binary.LittleEndian.Uint32(key[4:8])
  32. ret.key[2] = binary.LittleEndian.Uint32(key[8:12])
  33. ret.key[3] = binary.LittleEndian.Uint32(key[12:16])
  34. ret.key[4] = binary.LittleEndian.Uint32(key[16:20])
  35. ret.key[5] = binary.LittleEndian.Uint32(key[20:24])
  36. ret.key[6] = binary.LittleEndian.Uint32(key[24:28])
  37. ret.key[7] = binary.LittleEndian.Uint32(key[28:32])
  38. return ret, nil
  39. }
  40. func (c *chacha20poly1305) NonceSize() int {
  41. return NonceSize
  42. }
  43. func (c *chacha20poly1305) Overhead() int {
  44. return 16
  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. }