xchacha20poly1305.go 3.2 KB

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