| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package chacha20 |
| 5 |
|
| 6 |
import ( |
| 7 |
"crypto/cipher" |
| 8 |
"crypto/subtle" |
| 9 |
"errors" |
| 10 |
|
| 11 |
"github.com/enceve/crypto" |
| 12 |
"github.com/enceve/crypto/chacha20/chacha" |
| 13 |
"github.com/enceve/crypto/poly1305" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
const TagSize = poly1305.TagSize |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
func NewChaCha20Poly1305(key *[32]byte) cipher.AEAD { |
| 23 |
c := &aead{tagsize: TagSize} |
| 24 |
c.key = *key |
| 25 |
return c |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
func NewChaCha20Poly1305WithTagSize(key *[32]byte, tagsize int) (cipher.AEAD, error) { |
| 32 |
if tagsize < 1 || tagsize > TagSize { |
| 33 |
return nil, errors.New("tag size must be between 1 and 16") |
| 34 |
} |
| 35 |
c := &aead{tagsize: tagsize} |
| 36 |
c.key = *key |
| 37 |
return c, nil |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
type aead struct { |
| 42 |
key [32]byte |
| 43 |
tagsize int |
| 44 |
} |
| 45 |
|
| 46 |
func (c *aead) Overhead() int { return c.tagsize } |
| 47 |
|
| 48 |
func (c *aead) NonceSize() int { return NonceSize } |
| 49 |
|
| 50 |
func (c *aead) Seal(dst, nonce, plaintext, additionalData []byte) []byte { |
| 51 |
if n := len(nonce); n != NonceSize { |
| 52 |
panic(crypto.NonceSizeError(n)) |
| 53 |
} |
| 54 |
if len(dst) < len(plaintext)+c.tagsize { |
| 55 |
panic("dst buffer to small") |
| 56 |
} |
| 57 |
var Nonce [12]byte |
| 58 |
copy(Nonce[:], nonce) |
| 59 |
|
| 60 |
|
| 61 |
var polyKey [32]byte |
| 62 |
chacha.XORKeyStream(polyKey[:], polyKey[:], &Nonce, &(c.key), 0, 20) |
| 63 |
|
| 64 |
|
| 65 |
n := len(plaintext) |
| 66 |
chacha.XORKeyStream(dst, plaintext, &Nonce, &(c.key), 1, 20) |
| 67 |
|
| 68 |
|
| 69 |
var tag [poly1305.TagSize]byte |
| 70 |
authenticate(&tag, dst[:n], additionalData, &polyKey) |
| 71 |
return append(dst[:n], tag[:c.tagsize]...) |
| 72 |
} |
| 73 |
|
| 74 |
func (c *aead) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) { |
| 75 |
if n := len(nonce); n != NonceSize { |
| 76 |
return nil, crypto.NonceSizeError(n) |
| 77 |
} |
| 78 |
if len(ciphertext) < c.tagsize { |
| 79 |
return nil, crypto.AuthenticationError{} |
| 80 |
} |
| 81 |
if len(dst) < len(ciphertext)-c.tagsize { |
| 82 |
panic("dst buffer to small") |
| 83 |
} |
| 84 |
var Nonce [12]byte |
| 85 |
|
| 86 |
copy(Nonce[:], nonce) |
| 87 |
|
| 88 |
hash := ciphertext[len(ciphertext)-c.tagsize:] |
| 89 |
ciphertext = ciphertext[:len(ciphertext)-c.tagsize] |
| 90 |
|
| 91 |
|
| 92 |
var polyKey [32]byte |
| 93 |
chacha.XORKeyStream(polyKey[:], polyKey[:], &Nonce, &(c.key), 0, 20) |
| 94 |
|
| 95 |
|
| 96 |
var tag [poly1305.TagSize]byte |
| 97 |
authenticate(&tag, ciphertext, additionalData, &polyKey) |
| 98 |
if subtle.ConstantTimeCompare(tag[:c.tagsize], hash[:c.tagsize]) != 1 { |
| 99 |
return nil, crypto.AuthenticationError{} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
chacha.XORKeyStream(dst, ciphertext, &Nonce, &(c.key), 1, 20) |
| 104 |
return dst[:len(ciphertext)], nil |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
func authenticate(out *[TagSize]byte, ciphertext, additionalData []byte, key *[32]byte) { |
| 110 |
ctLen := uint64(len(ciphertext)) |
| 111 |
adLen := uint64(len(additionalData)) |
| 112 |
padAD, padCT := adLen%16, ctLen%16 |
| 113 |
|
| 114 |
var buf [16]byte |
| 115 |
buf[0] = byte(adLen) |
| 116 |
buf[1] = byte(adLen >> 8) |
| 117 |
buf[2] = byte(adLen >> 16) |
| 118 |
buf[3] = byte(adLen >> 24) |
| 119 |
buf[4] = byte(adLen >> 32) |
| 120 |
buf[5] = byte(adLen >> 40) |
| 121 |
buf[6] = byte(adLen >> 48) |
| 122 |
buf[7] = byte(adLen >> 56) |
| 123 |
buf[8] = byte(ctLen) |
| 124 |
buf[9] = byte(ctLen >> 8) |
| 125 |
buf[10] = byte(ctLen >> 16) |
| 126 |
buf[11] = byte(ctLen >> 24) |
| 127 |
buf[12] = byte(ctLen >> 32) |
| 128 |
buf[13] = byte(ctLen >> 40) |
| 129 |
buf[14] = byte(ctLen >> 48) |
| 130 |
buf[15] = byte(ctLen >> 56) |
| 131 |
|
| 132 |
poly := poly1305.New(key) |
| 133 |
poly.Write(additionalData) |
| 134 |
if padAD > 0 { |
| 135 |
poly.Write(make([]byte, 16-padAD)) |
| 136 |
} |
| 137 |
poly.Write(ciphertext) |
| 138 |
if padCT > 0 { |
| 139 |
poly.Write(make([]byte, 16-padCT)) |
| 140 |
} |
| 141 |
poly.Write(buf[:]) |
| 142 |
poly.Sum(out) |
| 143 |
} |