| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package chacha |
| 7 |
|
| 8 |
import ( |
| 9 |
"unsafe" |
| 10 |
|
| 11 |
"github.com/enceve/crypto" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
func XORKeyStream(dst, src []byte, nonce *[12]byte, key *[32]byte, counter uint32, rounds int) { |
| 19 |
length := len(src) |
| 20 |
if len(dst) < length { |
| 21 |
panic("chacha20/chacha: dst buffer is to small") |
| 22 |
} |
| 23 |
if rounds <= 0 || rounds%2 != 0 { |
| 24 |
panic("chacha20/chacha: rounds must be a multiple of 2") |
| 25 |
} |
| 26 |
|
| 27 |
var state [64]byte |
| 28 |
|
| 29 |
copy(state[:], constants[:]) |
| 30 |
|
| 31 |
statePtr := (*[8]uint64)(unsafe.Pointer(&state[0])) |
| 32 |
keyPtr := (*[4]uint64)(unsafe.Pointer(&key[0])) |
| 33 |
|
| 34 |
statePtr[2] = keyPtr[0] |
| 35 |
statePtr[3] = keyPtr[1] |
| 36 |
statePtr[4] = keyPtr[2] |
| 37 |
statePtr[5] = keyPtr[3] |
| 38 |
|
| 39 |
statePtr[6] = (*(*uint64)(unsafe.Pointer(&nonce[0])) << 32) | uint64(counter) |
| 40 |
|
| 41 |
statePtr[7] = *(*uint64)(unsafe.Pointer(&nonce[4])) |
| 42 |
|
| 43 |
if length >= 64 { |
| 44 |
XORBlocks(dst, src, &state, rounds) |
| 45 |
} |
| 46 |
|
| 47 |
if n := length & (^(64 - 1)); length-n > 0 { |
| 48 |
var block [64]byte |
| 49 |
Core(&block, &state, rounds) |
| 50 |
|
| 51 |
crypto.XOR(dst[n:], src[n:], block[:]) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
func NewCipher(nonce *[12]byte, key *[32]byte, rounds int) *Cipher { |
| 58 |
if rounds <= 0 || rounds%2 != 0 { |
| 59 |
panic("chacha20/chacha: rounds must be a multiply of 2") |
| 60 |
} |
| 61 |
c := new(Cipher) |
| 62 |
c.rounds = rounds |
| 63 |
|
| 64 |
copy(c.state[:], constants[:]) |
| 65 |
|
| 66 |
statePtr := (*[8]uint64)(unsafe.Pointer(&(c.state[0]))) |
| 67 |
keyPtr := (*[4]uint64)(unsafe.Pointer(&key[0])) |
| 68 |
|
| 69 |
statePtr[2] = keyPtr[0] |
| 70 |
statePtr[3] = keyPtr[1] |
| 71 |
statePtr[4] = keyPtr[2] |
| 72 |
statePtr[5] = keyPtr[3] |
| 73 |
|
| 74 |
statePtr[6] = (*(*uint64)(unsafe.Pointer(&nonce[0])) << 32) |
| 75 |
|
| 76 |
statePtr[7] = *(*uint64)(unsafe.Pointer(&nonce[4])) |
| 77 |
|
| 78 |
return c |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
func (c *Cipher) XORKeyStream(dst, src []byte) { |
| 84 |
length := len(src) |
| 85 |
if len(dst) < length { |
| 86 |
panic("chacha20/chacha: dst buffer is to small") |
| 87 |
} |
| 88 |
|
| 89 |
if c.off > 0 { |
| 90 |
n := crypto.XOR(dst, src, c.block[c.off:]) |
| 91 |
if n == length { |
| 92 |
c.off += n |
| 93 |
return |
| 94 |
} |
| 95 |
src = src[n:] |
| 96 |
dst = dst[n:] |
| 97 |
length -= n |
| 98 |
c.off = 0 |
| 99 |
} |
| 100 |
|
| 101 |
if length >= 64 { |
| 102 |
XORBlocks(dst, src, &(c.state), c.rounds) |
| 103 |
} |
| 104 |
|
| 105 |
if n := length & (^(64 - 1)); length-n > 0 { |
| 106 |
Core(&(c.block), &(c.state), c.rounds) |
| 107 |
|
| 108 |
c.off += crypto.XOR(dst[n:], src[n:], c.block[:]) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
func XORBlocks(dst, src []byte, state *[64]byte, rounds int) |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
func Core(dst *[64]byte, state *[64]byte, rounds int) |