Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

92 строки
2.7 KiB

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