xchacha20poly1305.go 3.4 KB

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