xchacha20poly1305.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2018 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
  5. import (
  6. "crypto/cipher"
  7. "errors"
  8. "golang.org/x/crypto/chacha20"
  9. )
  10. type xchacha20poly1305 struct {
  11. key [KeySize]byte
  12. }
  13. // NewX returns a XChaCha20-Poly1305 AEAD that uses the given 256-bit key.
  14. //
  15. // XChaCha20-Poly1305 is a ChaCha20-Poly1305 variant that takes a longer nonce,
  16. // suitable to be generated randomly without risk of collisions. It should be
  17. // preferred when nonce uniqueness cannot be trivially ensured, or whenever
  18. // nonces are randomly generated.
  19. func NewX(key []byte) (cipher.AEAD, error) {
  20. if len(key) != KeySize {
  21. return nil, errors.New("chacha20poly1305: bad key length")
  22. }
  23. ret := new(xchacha20poly1305)
  24. copy(ret.key[:], key)
  25. return ret, nil
  26. }
  27. func (*xchacha20poly1305) NonceSize() int {
  28. return NonceSizeX
  29. }
  30. func (*xchacha20poly1305) Overhead() int {
  31. return Overhead
  32. }
  33. func (x *xchacha20poly1305) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
  34. if len(nonce) != NonceSizeX {
  35. panic("chacha20poly1305: bad nonce length passed to Seal")
  36. }
  37. // XChaCha20-Poly1305 technically supports a 64-bit counter, so there is no
  38. // size limit. However, since we reuse the ChaCha20-Poly1305 implementation,
  39. // the second half of the counter is not available. This is unlikely to be
  40. // an issue because the cipher.AEAD API requires the entire message to be in
  41. // memory, and the counter overflows at 256 GB.
  42. if uint64(len(plaintext)) > (1<<38)-64 {
  43. panic("chacha20poly1305: plaintext too large")
  44. }
  45. c := new(chacha20poly1305)
  46. hKey, _ := chacha20.HChaCha20(x.key[:], nonce[0:16])
  47. copy(c.key[:], hKey)
  48. // The first 4 bytes of the final nonce are unused counter space.
  49. cNonce := make([]byte, NonceSize)
  50. copy(cNonce[4:12], nonce[16:24])
  51. return c.seal(dst, cNonce[:], plaintext, additionalData)
  52. }
  53. func (x *xchacha20poly1305) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
  54. if len(nonce) != NonceSizeX {
  55. panic("chacha20poly1305: bad nonce length passed to Open")
  56. }
  57. if len(ciphertext) < 16 {
  58. return nil, errOpen
  59. }
  60. if uint64(len(ciphertext)) > (1<<38)-48 {
  61. panic("chacha20poly1305: ciphertext too large")
  62. }
  63. c := new(chacha20poly1305)
  64. hKey, _ := chacha20.HChaCha20(x.key[:], nonce[0:16])
  65. copy(c.key[:], hKey)
  66. // The first 4 bytes of the final nonce are unused counter space.
  67. cNonce := make([]byte, NonceSize)
  68. copy(cNonce[4:12], nonce[16:24])
  69. return c.open(dst, cNonce[:], ciphertext, additionalData)
  70. }