| 1 |
// Copyright (c) 2016, Google Inc. |
| 2 |
// |
| 3 |
// Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
// purpose with or without fee is hereby granted, provided that the above |
| 5 |
// copyright notice and this permission notice appear in all copies. |
| 6 |
// |
| 7 |
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 |
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 |
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 |
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 |
|
| 15 |
package runner |
| 16 |
|
| 17 |
import ( |
| 18 |
"crypto/cipher" |
| 19 |
"crypto/subtle" |
| 20 |
"encoding/binary" |
| 21 |
"errors" |
| 22 |
|
| 23 |
"./poly1305" |
| 24 |
) |
| 25 |
|
| 26 |
// See RFC 7539. |
| 27 |
|
| 28 |
func leftRotate(a uint32, n uint) uint32 { |
| 29 |
return (a << n) | (a >> (32 - n)) |
| 30 |
} |
| 31 |
|
| 32 |
func chaChaQuarterRound(state *[16]uint32, a, b, c, d int) { |
| 33 |
state[a] += state[b] |
| 34 |
state[d] = leftRotate(state[d]^state[a], 16) |
| 35 |
|
| 36 |
state[c] += state[d] |
| 37 |
state[b] = leftRotate(state[b]^state[c], 12) |
| 38 |
|
| 39 |
state[a] += state[b] |
| 40 |
state[d] = leftRotate(state[d]^state[a], 8) |
| 41 |
|
| 42 |
state[c] += state[d] |
| 43 |
state[b] = leftRotate(state[b]^state[c], 7) |
| 44 |
} |
| 45 |
|
| 46 |
func chaCha20Block(state *[16]uint32, out []byte) { |
| 47 |
var workingState [16]uint32 |
| 48 |
copy(workingState[:], state[:]) |
| 49 |
for i := 0; i < 10; i++ { |
| 50 |
chaChaQuarterRound(&workingState, 0, 4, 8, 12) |
| 51 |
chaChaQuarterRound(&workingState, 1, 5, 9, 13) |
| 52 |
chaChaQuarterRound(&workingState, 2, 6, 10, 14) |
| 53 |
chaChaQuarterRound(&workingState, 3, 7, 11, 15) |
| 54 |
chaChaQuarterRound(&workingState, 0, 5, 10, 15) |
| 55 |
chaChaQuarterRound(&workingState, 1, 6, 11, 12) |
| 56 |
chaChaQuarterRound(&workingState, 2, 7, 8, 13) |
| 57 |
chaChaQuarterRound(&workingState, 3, 4, 9, 14) |
| 58 |
} |
| 59 |
for i := 0; i < 16; i++ { |
| 60 |
binary.LittleEndian.PutUint32(out[i*4:i*4+4], workingState[i]+state[i]) |
| 61 |
} |
| 62 |
} |
| 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 |
} |
| 78 |
|
| 79 |
func chaCha20(out, in, key, nonce []byte, counter uint64) { |
| 80 |
var state [16]uint32 |
| 81 |
state[0] = 0x61707865 |
| 82 |
state[1] = 0x3320646e |
| 83 |
state[2] = 0x79622d32 |
| 84 |
state[3] = 0x6b206574 |
| 85 |
for i := 0; i < 8; i++ { |
| 86 |
state[4+i] = binary.LittleEndian.Uint32(key[i*4 : i*4+4]) |
| 87 |
} |
| 88 |
|
| 89 |
switch len(nonce) { |
| 90 |
case 8: |
| 91 |
state[14] = binary.LittleEndian.Uint32(nonce[0:4]) |
| 92 |
state[15] = binary.LittleEndian.Uint32(nonce[4:8]) |
| 93 |
case 12: |
| 94 |
state[13] = binary.LittleEndian.Uint32(nonce[0:4]) |
| 95 |
state[14] = binary.LittleEndian.Uint32(nonce[4:8]) |
| 96 |
state[15] = binary.LittleEndian.Uint32(nonce[8:12]) |
| 97 |
default: |
| 98 |
panic("bad nonce length") |
| 99 |
} |
| 100 |
|
| 101 |
for i := 0; i < len(in); i += 64 { |
| 102 |
state[12] = uint32(counter) |
| 103 |
|
| 104 |
var tmp [64]byte |
| 105 |
chaCha20Block(&state, tmp[:]) |
| 106 |
count := 64 |
| 107 |
if len(in)-i < count { |
| 108 |
count = len(in) - i |
| 109 |
} |
| 110 |
for j := 0; j < count; j++ { |
| 111 |
out[i+j] = in[i+j] ^ tmp[j] |
| 112 |
} |
| 113 |
|
| 114 |
counter++ |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
// chaCha20Poly1305 implements the AEAD from |
| 119 |
// RFC 7539 and draft-agl-tls-chacha20poly1305-04. |
| 120 |
type chaCha20Poly1305 struct { |
| 121 |
key [32]byte |
| 122 |
// oldMode, if true, indicates that the draft spec should be |
| 123 |
// implemented rather than the final, RFC version. |
| 124 |
oldMode bool |
| 125 |
} |
| 126 |
|
| 127 |
func newChaCha20Poly1305(key []byte) (cipher.AEAD, error) { |
| 128 |
if len(key) != 32 { |
| 129 |
return nil, errors.New("bad key length") |
| 130 |
} |
| 131 |
aead := new(chaCha20Poly1305) |
| 132 |
copy(aead.key[:], key) |
| 133 |
return aead, nil |
| 134 |
} |
| 135 |
|
| 136 |
func newChaCha20Poly1305Old(key []byte) (cipher.AEAD, error) { |
| 137 |
if len(key) != 32 { |
| 138 |
return nil, errors.New("bad key length") |
| 139 |
} |
| 140 |
aead := &chaCha20Poly1305{ |
| 141 |
oldMode: true, |
| 142 |
} |
| 143 |
copy(aead.key[:], key) |
| 144 |
return aead, nil |
| 145 |
} |
| 146 |
|
| 147 |
func (c *chaCha20Poly1305) NonceSize() int { |
| 148 |
if c.oldMode { |
| 149 |
return 8 |
| 150 |
} else { |
| 151 |
return 12 |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
func (c *chaCha20Poly1305) Overhead() int { return 16 } |
| 156 |
|
| 157 |
func (c *chaCha20Poly1305) poly1305(tag *[16]byte, nonce, ciphertext, additionalData []byte) { |
| 158 |
input := make([]byte, 0, len(additionalData)+15+len(ciphertext)+15+8+8) |
| 159 |
input = append(input, additionalData...) |
| 160 |
var zeros [15]byte |
| 161 |
if pad := len(input) % 16; pad != 0 { |
| 162 |
input = append(input, zeros[:16-pad]...) |
| 163 |
} |
| 164 |
input = append(input, ciphertext...) |
| 165 |
if pad := len(input) % 16; pad != 0 { |
| 166 |
input = append(input, zeros[:16-pad]...) |
| 167 |
} |
| 168 |
input, out := sliceForAppend(input, 8) |
| 169 |
binary.LittleEndian.PutUint64(out, uint64(len(additionalData))) |
| 170 |
input, out = sliceForAppend(input, 8) |
| 171 |
binary.LittleEndian.PutUint64(out, uint64(len(ciphertext))) |
| 172 |
|
| 173 |
var poly1305Key [32]byte |
| 174 |
chaCha20(poly1305Key[:], poly1305Key[:], c.key[:], nonce, 0) |
| 175 |
|
| 176 |
poly1305.Sum(tag, input, &poly1305Key) |
| 177 |
} |
| 178 |
|
| 179 |
func (c *chaCha20Poly1305) poly1305Old(tag *[16]byte, nonce, ciphertext, additionalData []byte) { |
| 180 |
input := make([]byte, 0, len(additionalData)+8+len(ciphertext)+8) |
| 181 |
input = append(input, additionalData...) |
| 182 |
input, out := sliceForAppend(input, 8) |
| 183 |
binary.LittleEndian.PutUint64(out, uint64(len(additionalData))) |
| 184 |
input = append(input, ciphertext...) |
| 185 |
input, out = sliceForAppend(input, 8) |
| 186 |
binary.LittleEndian.PutUint64(out, uint64(len(ciphertext))) |
| 187 |
|
| 188 |
var poly1305Key [32]byte |
| 189 |
chaCha20(poly1305Key[:], poly1305Key[:], c.key[:], nonce, 0) |
| 190 |
|
| 191 |
poly1305.Sum(tag, input, &poly1305Key) |
| 192 |
} |
| 193 |
|
| 194 |
func (c *chaCha20Poly1305) Seal(dst, nonce, plaintext, additionalData []byte) []byte { |
| 195 |
if len(nonce) != c.NonceSize() { |
| 196 |
panic("Bad nonce length") |
| 197 |
} |
| 198 |
|
| 199 |
ret, out := sliceForAppend(dst, len(plaintext)+16) |
| 200 |
chaCha20(out[:len(plaintext)], plaintext, c.key[:], nonce, 1) |
| 201 |
|
| 202 |
var tag [16]byte |
| 203 |
if c.oldMode { |
| 204 |
c.poly1305Old(&tag, nonce, out[:len(plaintext)], additionalData) |
| 205 |
} else { |
| 206 |
c.poly1305(&tag, nonce, out[:len(plaintext)], additionalData) |
| 207 |
} |
| 208 |
copy(out[len(plaintext):], tag[:]) |
| 209 |
|
| 210 |
return ret |
| 211 |
} |
| 212 |
|
| 213 |
func (c *chaCha20Poly1305) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) { |
| 214 |
if len(nonce) != c.NonceSize() { |
| 215 |
panic("Bad nonce length") |
| 216 |
} |
| 217 |
if len(ciphertext) < 16 { |
| 218 |
return nil, errors.New("chacha20: message authentication failed") |
| 219 |
} |
| 220 |
plaintextLen := len(ciphertext) - 16 |
| 221 |
|
| 222 |
var tag [16]byte |
| 223 |
if c.oldMode { |
| 224 |
c.poly1305Old(&tag, nonce, ciphertext[:plaintextLen], additionalData) |
| 225 |
} else { |
| 226 |
c.poly1305(&tag, nonce, ciphertext[:plaintextLen], additionalData) |
| 227 |
} |
| 228 |
if subtle.ConstantTimeCompare(tag[:], ciphertext[plaintextLen:]) != 1 { |
| 229 |
return nil, errors.New("chacha20: message authentication failed") |
| 230 |
} |
| 231 |
|
| 232 |
ret, out := sliceForAppend(dst, plaintextLen) |
| 233 |
chaCha20(out, ciphertext[:plaintextLen], c.key[:], nonce, 1) |
| 234 |
return ret, nil |
| 235 |
} |