LEFT | RIGHT |
(no file at all) | |
| 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. |
| 4 |
| 5 // Package salsa provides low-level access to functions in the Salsa family. |
| 6 package salsa |
| 7 |
| 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'} |
| 10 |
| 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 |
| 13 // out. |
| 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(i
n[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[1
9])<<24 |
| 27 x12 := uint32(k[20]) | uint32(k[21])<<8 | uint32(k[22])<<16 | uint32(k[2
3])<<24 |
| 28 x13 := uint32(k[24]) | uint32(k[25])<<8 | uint32(k[26])<<16 | uint32(k[2
7])<<24 |
| 29 x14 := uint32(k[28]) | uint32(k[29])<<8 | uint32(k[30])<<16 | uint32(k[3
1])<<24 |
| 30 x15 := uint32(c[12]) | uint32(c[13])<<8 | uint32(c[14])<<16 | uint32(c[1
5])<<24 |
| 31 |
| 32 for i := 0; i < 20; i += 2 { |
| 33 u := x0 + x12 |
| 34 x4 ^= u<<7 | u>>(32-7) |
| 35 u = x4 + x0 |
| 36 x8 ^= u<<9 | u>>(32-9) |
| 37 u = x8 + x4 |
| 38 x12 ^= u<<13 | u>>(32-13) |
| 39 u = x12 + x8 |
| 40 x0 ^= u<<18 | u>>(32-18) |
| 41 |
| 42 u = x5 + x1 |
| 43 x9 ^= u<<7 | u>>(32-7) |
| 44 u = x9 + x5 |
| 45 x13 ^= u<<9 | u>>(32-9) |
| 46 u = x13 + x9 |
| 47 x1 ^= u<<13 | u>>(32-13) |
| 48 u = x1 + x13 |
| 49 x5 ^= u<<18 | u>>(32-18) |
| 50 |
| 51 u = x10 + x6 |
| 52 x14 ^= u<<7 | u>>(32-7) |
| 53 u = x14 + x10 |
| 54 x2 ^= u<<9 | u>>(32-9) |
| 55 u = x2 + x14 |
| 56 x6 ^= u<<13 | u>>(32-13) |
| 57 u = x6 + x2 |
| 58 x10 ^= u<<18 | u>>(32-18) |
| 59 |
| 60 u = x15 + x11 |
| 61 x3 ^= u<<7 | u>>(32-7) |
| 62 u = x3 + x15 |
| 63 x7 ^= u<<9 | u>>(32-9) |
| 64 u = x7 + x3 |
| 65 x11 ^= u<<13 | u>>(32-13) |
| 66 u = x11 + x7 |
| 67 x15 ^= u<<18 | u>>(32-18) |
| 68 |
| 69 u = x0 + x3 |
| 70 x1 ^= u<<7 | u>>(32-7) |
| 71 u = x1 + x0 |
| 72 x2 ^= u<<9 | u>>(32-9) |
| 73 u = x2 + x1 |
| 74 x3 ^= u<<13 | u>>(32-13) |
| 75 u = x3 + x2 |
| 76 x0 ^= u<<18 | u>>(32-18) |
| 77 |
| 78 u = x5 + x4 |
| 79 x6 ^= u<<7 | u>>(32-7) |
| 80 u = x6 + x5 |
| 81 x7 ^= u<<9 | u>>(32-9) |
| 82 u = x7 + x6 |
| 83 x4 ^= u<<13 | u>>(32-13) |
| 84 u = x4 + x7 |
| 85 x5 ^= u<<18 | u>>(32-18) |
| 86 |
| 87 u = x10 + x9 |
| 88 x11 ^= u<<7 | u>>(32-7) |
| 89 u = x11 + x10 |
| 90 x8 ^= u<<9 | u>>(32-9) |
| 91 u = x8 + x11 |
| 92 x9 ^= u<<13 | u>>(32-13) |
| 93 u = x9 + x8 |
| 94 x10 ^= u<<18 | u>>(32-18) |
| 95 |
| 96 u = x15 + x14 |
| 97 x12 ^= u<<7 | u>>(32-7) |
| 98 u = x12 + x15 |
| 99 x13 ^= u<<9 | u>>(32-9) |
| 100 u = x13 + x12 |
| 101 x14 ^= u<<13 | u>>(32-13) |
| 102 u = x14 + x13 |
| 103 x15 ^= u<<18 | u>>(32-18) |
| 104 } |
| 105 out[0] = byte(x0) |
| 106 out[1] = byte(x0 >> 8) |
| 107 out[2] = byte(x0 >> 16) |
| 108 out[3] = byte(x0 >> 24) |
| 109 |
| 110 out[4] = byte(x5) |
| 111 out[5] = byte(x5 >> 8) |
| 112 out[6] = byte(x5 >> 16) |
| 113 out[7] = byte(x5 >> 24) |
| 114 |
| 115 out[8] = byte(x10) |
| 116 out[9] = byte(x10 >> 8) |
| 117 out[10] = byte(x10 >> 16) |
| 118 out[11] = byte(x10 >> 24) |
| 119 |
| 120 out[12] = byte(x15) |
| 121 out[13] = byte(x15 >> 8) |
| 122 out[14] = byte(x15 >> 16) |
| 123 out[15] = byte(x15 >> 24) |
| 124 |
| 125 out[16] = byte(x6) |
| 126 out[17] = byte(x6 >> 8) |
| 127 out[18] = byte(x6 >> 16) |
| 128 out[19] = byte(x6 >> 24) |
| 129 |
| 130 out[20] = byte(x7) |
| 131 out[21] = byte(x7 >> 8) |
| 132 out[22] = byte(x7 >> 16) |
| 133 out[23] = byte(x7 >> 24) |
| 134 |
| 135 out[24] = byte(x8) |
| 136 out[25] = byte(x8 >> 8) |
| 137 out[26] = byte(x8 >> 16) |
| 138 out[27] = byte(x8 >> 24) |
| 139 |
| 140 out[28] = byte(x9) |
| 141 out[29] = byte(x9 >> 8) |
| 142 out[30] = byte(x9 >> 16) |
| 143 out[31] = byte(x9 >> 24) |
| 144 } |
LEFT | RIGHT |