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 // Package salsa provides low-level access to functions in the Salsa family.
6 package salsa // import "golang.org/x/crypto/salsa20/salsa"
8 // Sigma is the Salsa20 constant for 256-bit keys.
9 var Sigma = [16]byte{'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'}
11 // HSalsa20 applies the HSalsa20 core function to a 16-byte input in, 32-byte
12 // key k, and 16-byte constant c, and puts the result into the 32-byte array
14 func HSalsa20(out *[32]byte, in *[16]byte, k *[32]byte, c *[16]byte) {
15 x0 := uint32(c[0]) | uint32(c[1])<<8 | uint32(c[2])<<16 | uint32(c[3])<<24
16 x1 := uint32(k[0]) | uint32(k[1])<<8 | uint32(k[2])<<16 | uint32(k[3])<<24
17 x2 := uint32(k[4]) | uint32(k[5])<<8 | uint32(k[6])<<16 | uint32(k[7])<<24
18 x3 := uint32(k[8]) | uint32(k[9])<<8 | uint32(k[10])<<16 | uint32(k[11])<<24
19 x4 := uint32(k[12]) | uint32(k[13])<<8 | uint32(k[14])<<16 | uint32(k[15])<<24
20 x5 := uint32(c[4]) | uint32(c[5])<<8 | uint32(c[6])<<16 | uint32(c[7])<<24
21 x6 := uint32(in[0]) | uint32(in[1])<<8 | uint32(in[2])<<16 | uint32(in[3])<<24
22 x7 := uint32(in[4]) | uint32(in[5])<<8 | uint32(in[6])<<16 | uint32(in[7])<<24
23 x8 := uint32(in[8]) | uint32(in[9])<<8 | uint32(in[10])<<16 | uint32(in[11])<<24
24 x9 := uint32(in[12]) | uint32(in[13])<<8 | uint32(in[14])<<16 | uint32(in[15])<<24
25 x10 := uint32(c[8]) | uint32(c[9])<<8 | uint32(c[10])<<16 | uint32(c[11])<<24
26 x11 := uint32(k[16]) | uint32(k[17])<<8 | uint32(k[18])<<16 | uint32(k[19])<<24
27 x12 := uint32(k[20]) | uint32(k[21])<<8 | uint32(k[22])<<16 | uint32(k[23])<<24
28 x13 := uint32(k[24]) | uint32(k[25])<<8 | uint32(k[26])<<16 | uint32(k[27])<<24
29 x14 := uint32(k[28]) | uint32(k[29])<<8 | uint32(k[30])<<16 | uint32(k[31])<<24
30 x15 := uint32(c[12]) | uint32(c[13])<<8 | uint32(c[14])<<16 | uint32(c[15])<<24
32 for i := 0; i < 20; i += 2 {
34 x4 ^= u<<7 | u>>(32-7)
36 x8 ^= u<<9 | u>>(32-9)
38 x12 ^= u<<13 | u>>(32-13)
40 x0 ^= u<<18 | u>>(32-18)
43 x9 ^= u<<7 | u>>(32-7)
45 x13 ^= u<<9 | u>>(32-9)
47 x1 ^= u<<13 | u>>(32-13)
49 x5 ^= u<<18 | u>>(32-18)
52 x14 ^= u<<7 | u>>(32-7)
54 x2 ^= u<<9 | u>>(32-9)
56 x6 ^= u<<13 | u>>(32-13)
58 x10 ^= u<<18 | u>>(32-18)
61 x3 ^= u<<7 | u>>(32-7)
63 x7 ^= u<<9 | u>>(32-9)
65 x11 ^= u<<13 | u>>(32-13)
67 x15 ^= u<<18 | u>>(32-18)
70 x1 ^= u<<7 | u>>(32-7)
72 x2 ^= u<<9 | u>>(32-9)
74 x3 ^= u<<13 | u>>(32-13)
76 x0 ^= u<<18 | u>>(32-18)
79 x6 ^= u<<7 | u>>(32-7)
81 x7 ^= u<<9 | u>>(32-9)
83 x4 ^= u<<13 | u>>(32-13)
85 x5 ^= u<<18 | u>>(32-18)
88 x11 ^= u<<7 | u>>(32-7)
90 x8 ^= u<<9 | u>>(32-9)
92 x9 ^= u<<13 | u>>(32-13)
94 x10 ^= u<<18 | u>>(32-18)
97 x12 ^= u<<7 | u>>(32-7)
99 x13 ^= u<<9 | u>>(32-9)
101 x14 ^= u<<13 | u>>(32-13)
103 x15 ^= u<<18 | u>>(32-18)
106 out[1] = byte(x0 >> 8)
107 out[2] = byte(x0 >> 16)
108 out[3] = byte(x0 >> 24)
111 out[5] = byte(x5 >> 8)
112 out[6] = byte(x5 >> 16)
113 out[7] = byte(x5 >> 24)
116 out[9] = byte(x10 >> 8)
117 out[10] = byte(x10 >> 16)
118 out[11] = byte(x10 >> 24)
121 out[13] = byte(x15 >> 8)
122 out[14] = byte(x15 >> 16)
123 out[15] = byte(x15 >> 24)
126 out[17] = byte(x6 >> 8)
127 out[18] = byte(x6 >> 16)
128 out[19] = byte(x6 >> 24)
131 out[21] = byte(x7 >> 8)
132 out[22] = byte(x7 >> 16)
133 out[23] = byte(x7 >> 24)
136 out[25] = byte(x8 >> 8)
137 out[26] = byte(x8 >> 16)
138 out[27] = byte(x8 >> 24)
141 out[29] = byte(x9 >> 8)
142 out[30] = byte(x9 >> 16)
143 out[31] = byte(x9 >> 24)