1 // Copyright 2012 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.
5 // +build !amd64 appengine gccgo
11 // core applies the Salsa20 core function to 16-byte input in, 32-byte key k,
12 // and 16-byte constant c, and puts the result into 64-byte array out.
13 func core(out *[64]byte, in *[16]byte, k *[32]byte, c *[16]byte) {
14 j0 := uint32(c[0]) | uint32(c[1])<<8 | uint32(c[2])<<16 | uint32(c[3])<<24
15 j1 := uint32(k[0]) | uint32(k[1])<<8 | uint32(k[2])<<16 | uint32(k[3])<<24
16 j2 := uint32(k[4]) | uint32(k[5])<<8 | uint32(k[6])<<16 | uint32(k[7])<<24
17 j3 := uint32(k[8]) | uint32(k[9])<<8 | uint32(k[10])<<16 | uint32(k[11])<<24
18 j4 := uint32(k[12]) | uint32(k[13])<<8 | uint32(k[14])<<16 | uint32(k[15])<<24
19 j5 := uint32(c[4]) | uint32(c[5])<<8 | uint32(c[6])<<16 | uint32(c[7])<<24
20 j6 := uint32(in[0]) | uint32(in[1])<<8 | uint32(in[2])<<16 | uint32(in[3])<<24
21 j7 := uint32(in[4]) | uint32(in[5])<<8 | uint32(in[6])<<16 | uint32(in[7])<<24
22 j8 := uint32(in[8]) | uint32(in[9])<<8 | uint32(in[10])<<16 | uint32(in[11])<<24
23 j9 := uint32(in[12]) | uint32(in[13])<<8 | uint32(in[14])<<16 | uint32(in[15])<<24
24 j10 := uint32(c[8]) | uint32(c[9])<<8 | uint32(c[10])<<16 | uint32(c[11])<<24
25 j11 := uint32(k[16]) | uint32(k[17])<<8 | uint32(k[18])<<16 | uint32(k[19])<<24
26 j12 := uint32(k[20]) | uint32(k[21])<<8 | uint32(k[22])<<16 | uint32(k[23])<<24
27 j13 := uint32(k[24]) | uint32(k[25])<<8 | uint32(k[26])<<16 | uint32(k[27])<<24
28 j14 := uint32(k[28]) | uint32(k[29])<<8 | uint32(k[30])<<16 | uint32(k[31])<<24
29 j15 := uint32(c[12]) | uint32(c[13])<<8 | uint32(c[14])<<16 | uint32(c[15])<<24
31 x0, x1, x2, x3, x4, x5, x6, x7, x8 := j0, j1, j2, j3, j4, j5, j6, j7, j8
32 x9, x10, x11, x12, x13, x14, x15 := j9, j10, j11, j12, j13, j14, j15
34 for i := 0; i < rounds; i += 2 {
36 x4 ^= u<<7 | u>>(32-7)
38 x8 ^= u<<9 | u>>(32-9)
40 x12 ^= u<<13 | u>>(32-13)
42 x0 ^= u<<18 | u>>(32-18)
45 x9 ^= u<<7 | u>>(32-7)
47 x13 ^= u<<9 | u>>(32-9)
49 x1 ^= u<<13 | u>>(32-13)
51 x5 ^= u<<18 | u>>(32-18)
54 x14 ^= u<<7 | u>>(32-7)
56 x2 ^= u<<9 | u>>(32-9)
58 x6 ^= u<<13 | u>>(32-13)
60 x10 ^= u<<18 | u>>(32-18)
63 x3 ^= u<<7 | u>>(32-7)
65 x7 ^= u<<9 | u>>(32-9)
67 x11 ^= u<<13 | u>>(32-13)
69 x15 ^= u<<18 | u>>(32-18)
72 x1 ^= u<<7 | u>>(32-7)
74 x2 ^= u<<9 | u>>(32-9)
76 x3 ^= u<<13 | u>>(32-13)
78 x0 ^= u<<18 | u>>(32-18)
81 x6 ^= u<<7 | u>>(32-7)
83 x7 ^= u<<9 | u>>(32-9)
85 x4 ^= u<<13 | u>>(32-13)
87 x5 ^= u<<18 | u>>(32-18)
90 x11 ^= u<<7 | u>>(32-7)
92 x8 ^= u<<9 | u>>(32-9)
94 x9 ^= u<<13 | u>>(32-13)
96 x10 ^= u<<18 | u>>(32-18)
99 x12 ^= u<<7 | u>>(32-7)
101 x13 ^= u<<9 | u>>(32-9)
103 x14 ^= u<<13 | u>>(32-13)
105 x15 ^= u<<18 | u>>(32-18)
125 out[1] = byte(x0 >> 8)
126 out[2] = byte(x0 >> 16)
127 out[3] = byte(x0 >> 24)
130 out[5] = byte(x1 >> 8)
131 out[6] = byte(x1 >> 16)
132 out[7] = byte(x1 >> 24)
135 out[9] = byte(x2 >> 8)
136 out[10] = byte(x2 >> 16)
137 out[11] = byte(x2 >> 24)
140 out[13] = byte(x3 >> 8)
141 out[14] = byte(x3 >> 16)
142 out[15] = byte(x3 >> 24)
145 out[17] = byte(x4 >> 8)
146 out[18] = byte(x4 >> 16)
147 out[19] = byte(x4 >> 24)
150 out[21] = byte(x5 >> 8)
151 out[22] = byte(x5 >> 16)
152 out[23] = byte(x5 >> 24)
155 out[25] = byte(x6 >> 8)
156 out[26] = byte(x6 >> 16)
157 out[27] = byte(x6 >> 24)
160 out[29] = byte(x7 >> 8)
161 out[30] = byte(x7 >> 16)
162 out[31] = byte(x7 >> 24)
165 out[33] = byte(x8 >> 8)
166 out[34] = byte(x8 >> 16)
167 out[35] = byte(x8 >> 24)
170 out[37] = byte(x9 >> 8)
171 out[38] = byte(x9 >> 16)
172 out[39] = byte(x9 >> 24)
175 out[41] = byte(x10 >> 8)
176 out[42] = byte(x10 >> 16)
177 out[43] = byte(x10 >> 24)
180 out[45] = byte(x11 >> 8)
181 out[46] = byte(x11 >> 16)
182 out[47] = byte(x11 >> 24)
185 out[49] = byte(x12 >> 8)
186 out[50] = byte(x12 >> 16)
187 out[51] = byte(x12 >> 24)
190 out[53] = byte(x13 >> 8)
191 out[54] = byte(x13 >> 16)
192 out[55] = byte(x13 >> 24)
195 out[57] = byte(x14 >> 8)
196 out[58] = byte(x14 >> 16)
197 out[59] = byte(x14 >> 24)
200 out[61] = byte(x15 >> 8)
201 out[62] = byte(x15 >> 16)
202 out[63] = byte(x15 >> 24)
205 // XORKeyStream crypts bytes from in to out using the given key and counters.
206 // In and out may be the same slice but otherwise should not overlap. Counter
207 // contains the raw salsa20 counter bytes (both nonce and block counter).
208 func XORKeyStream(out, in []byte, counter *[16]byte, key *[32]byte) {
210 var counterCopy [16]byte
211 copy(counterCopy[:], counter[:])
214 core(&block, &counterCopy, key, &Sigma)
215 for i, x := range block {
219 for i := 8; i < 16; i++ {
220 u += uint32(counterCopy[i])
221 counterCopy[i] = byte(u)
229 core(&block, &counterCopy, key, &Sigma)
230 for i, v := range in {
231 out[i] = v ^ block[i]