You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

160 lines
4.4 KiB

  1. package main
  2. import (
  3. "crypto/cipher"
  4. "crypto/subtle"
  5. "encoding/binary"
  6. "errors"
  7. )
  8. // See draft-agl-tls-chacha20poly1305-04 and
  9. // draft-irtf-cfrg-chacha20-poly1305-10. Where the two differ, the
  10. // draft-agl-tls-chacha20poly1305-04 variant is implemented.
  11. func leftRotate(a uint32, n uint) uint32 {
  12. return (a << n) | (a >> (32 - n))
  13. }
  14. func chaChaQuarterRound(state *[16]uint32, a, b, c, d int) {
  15. state[a] += state[b]
  16. state[d] = leftRotate(state[d]^state[a], 16)
  17. state[c] += state[d]
  18. state[b] = leftRotate(state[b]^state[c], 12)
  19. state[a] += state[b]
  20. state[d] = leftRotate(state[d]^state[a], 8)
  21. state[c] += state[d]
  22. state[b] = leftRotate(state[b]^state[c], 7)
  23. }
  24. func chaCha20Block(state *[16]uint32, out []byte) {
  25. var workingState [16]uint32
  26. copy(workingState[:], state[:])
  27. for i := 0; i < 10; i++ {
  28. chaChaQuarterRound(&workingState, 0, 4, 8, 12)
  29. chaChaQuarterRound(&workingState, 1, 5, 9, 13)
  30. chaChaQuarterRound(&workingState, 2, 6, 10, 14)
  31. chaChaQuarterRound(&workingState, 3, 7, 11, 15)
  32. chaChaQuarterRound(&workingState, 0, 5, 10, 15)
  33. chaChaQuarterRound(&workingState, 1, 6, 11, 12)
  34. chaChaQuarterRound(&workingState, 2, 7, 8, 13)
  35. chaChaQuarterRound(&workingState, 3, 4, 9, 14)
  36. }
  37. for i := 0; i < 16; i++ {
  38. binary.LittleEndian.PutUint32(out[i*4:i*4+4], workingState[i]+state[i])
  39. }
  40. }
  41. // sliceForAppend takes a slice and a requested number of bytes. It returns a
  42. // slice with the contents of the given slice followed by that many bytes and a
  43. // second slice that aliases into it and contains only the extra bytes. If the
  44. // original slice has sufficient capacity then no allocation is performed.
  45. func sliceForAppend(in []byte, n int) (head, tail []byte) {
  46. if total := len(in) + n; cap(in) >= total {
  47. head = in[:total]
  48. } else {
  49. head = make([]byte, total)
  50. copy(head, in)
  51. }
  52. tail = head[len(in):]
  53. return
  54. }
  55. type chaCha20Poly1305 struct {
  56. key [32]byte
  57. }
  58. func newChaCha20Poly1305(key []byte) (cipher.AEAD, error) {
  59. if len(key) != 32 {
  60. return nil, errors.New("bad key length")
  61. }
  62. aead := new(chaCha20Poly1305)
  63. copy(aead.key[:], key)
  64. return aead, nil
  65. }
  66. func (c *chaCha20Poly1305) NonceSize() int { return 8 }
  67. func (c *chaCha20Poly1305) Overhead() int { return 16 }
  68. func (c *chaCha20Poly1305) chaCha20(out, in, nonce []byte, counter uint64) {
  69. var state [16]uint32
  70. state[0] = 0x61707865
  71. state[1] = 0x3320646e
  72. state[2] = 0x79622d32
  73. state[3] = 0x6b206574
  74. for i := 0; i < 8; i++ {
  75. state[4+i] = binary.LittleEndian.Uint32(c.key[i*4 : i*4+4])
  76. }
  77. state[14] = binary.LittleEndian.Uint32(nonce[0:4])
  78. state[15] = binary.LittleEndian.Uint32(nonce[4:8])
  79. for i := 0; i < len(in); i += 64 {
  80. state[12] = uint32(counter & 0xffffffff)
  81. state[13] = uint32(counter >> 32)
  82. var tmp [64]byte
  83. chaCha20Block(&state, tmp[:])
  84. count := 64
  85. if len(in)-i < count {
  86. count = len(in) - i
  87. }
  88. for j := 0; j < count; j++ {
  89. out[i+j] = in[i+j] ^ tmp[j]
  90. }
  91. counter++
  92. }
  93. }
  94. func (c *chaCha20Poly1305) poly1305(tag *[16]byte, nonce, ciphertext, additionalData []byte) {
  95. input := make([]byte, 0, len(additionalData)+8+len(ciphertext)+8)
  96. input = append(input, additionalData...)
  97. input, out := sliceForAppend(input, 8)
  98. binary.LittleEndian.PutUint64(out, uint64(len(additionalData)))
  99. input = append(input, ciphertext...)
  100. input, out = sliceForAppend(input, 8)
  101. binary.LittleEndian.PutUint64(out, uint64(len(ciphertext)))
  102. var poly1305Key [32]byte
  103. c.chaCha20(poly1305Key[:], poly1305Key[:], nonce, 0)
  104. poly1305Sum(tag, input, &poly1305Key)
  105. }
  106. func (c *chaCha20Poly1305) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
  107. if len(nonce) != 8 {
  108. panic("Bad nonce length")
  109. }
  110. ret, out := sliceForAppend(dst, len(plaintext)+16)
  111. c.chaCha20(out[:len(plaintext)], plaintext, nonce, 1)
  112. var tag [16]byte
  113. c.poly1305(&tag, nonce, out[:len(plaintext)], additionalData)
  114. copy(out[len(plaintext):], tag[:])
  115. return ret
  116. }
  117. func (c *chaCha20Poly1305) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
  118. if len(nonce) != 8 {
  119. panic("Bad nonce length")
  120. }
  121. if len(ciphertext) < 16 {
  122. return nil, errors.New("chacha20: message authentication failed")
  123. }
  124. plaintextLen := len(ciphertext) - 16
  125. var tag [16]byte
  126. c.poly1305(&tag, nonce, ciphertext[:plaintextLen], additionalData)
  127. if subtle.ConstantTimeCompare(tag[:], ciphertext[plaintextLen:]) != 1 {
  128. return nil, errors.New("chacha20: message authentication failed")
  129. }
  130. ret, out := sliceForAppend(dst, plaintextLen)
  131. c.chaCha20(out, ciphertext[:plaintextLen], nonce, 1)
  132. return ret, nil
  133. }