...

Source file src/golang.org/x/crypto/salsa20/salsa/hsalsa20.go

Documentation: golang.org/x/crypto/salsa20/salsa

     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 // import "golang.org/x/crypto/salsa20/salsa"
     7  
     8  import "math/bits"
     9  
    10  // Sigma is the Salsa20 constant for 256-bit keys.
    11  var Sigma = [16]byte{'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'}
    12  
    13  // HSalsa20 applies the HSalsa20 core function to a 16-byte input in, 32-byte
    14  // key k, and 16-byte constant c, and puts the result into the 32-byte array
    15  // out.
    16  func HSalsa20(out *[32]byte, in *[16]byte, k *[32]byte, c *[16]byte) {
    17  	x0 := uint32(c[0]) | uint32(c[1])<<8 | uint32(c[2])<<16 | uint32(c[3])<<24
    18  	x1 := uint32(k[0]) | uint32(k[1])<<8 | uint32(k[2])<<16 | uint32(k[3])<<24
    19  	x2 := uint32(k[4]) | uint32(k[5])<<8 | uint32(k[6])<<16 | uint32(k[7])<<24
    20  	x3 := uint32(k[8]) | uint32(k[9])<<8 | uint32(k[10])<<16 | uint32(k[11])<<24
    21  	x4 := uint32(k[12]) | uint32(k[13])<<8 | uint32(k[14])<<16 | uint32(k[15])<<24
    22  	x5 := uint32(c[4]) | uint32(c[5])<<8 | uint32(c[6])<<16 | uint32(c[7])<<24
    23  	x6 := uint32(in[0]) | uint32(in[1])<<8 | uint32(in[2])<<16 | uint32(in[3])<<24
    24  	x7 := uint32(in[4]) | uint32(in[5])<<8 | uint32(in[6])<<16 | uint32(in[7])<<24
    25  	x8 := uint32(in[8]) | uint32(in[9])<<8 | uint32(in[10])<<16 | uint32(in[11])<<24
    26  	x9 := uint32(in[12]) | uint32(in[13])<<8 | uint32(in[14])<<16 | uint32(in[15])<<24
    27  	x10 := uint32(c[8]) | uint32(c[9])<<8 | uint32(c[10])<<16 | uint32(c[11])<<24
    28  	x11 := uint32(k[16]) | uint32(k[17])<<8 | uint32(k[18])<<16 | uint32(k[19])<<24
    29  	x12 := uint32(k[20]) | uint32(k[21])<<8 | uint32(k[22])<<16 | uint32(k[23])<<24
    30  	x13 := uint32(k[24]) | uint32(k[25])<<8 | uint32(k[26])<<16 | uint32(k[27])<<24
    31  	x14 := uint32(k[28]) | uint32(k[29])<<8 | uint32(k[30])<<16 | uint32(k[31])<<24
    32  	x15 := uint32(c[12]) | uint32(c[13])<<8 | uint32(c[14])<<16 | uint32(c[15])<<24
    33  
    34  	for i := 0; i < 20; i += 2 {
    35  		u := x0 + x12
    36  		x4 ^= bits.RotateLeft32(u, 7)
    37  		u = x4 + x0
    38  		x8 ^= bits.RotateLeft32(u, 9)
    39  		u = x8 + x4
    40  		x12 ^= bits.RotateLeft32(u, 13)
    41  		u = x12 + x8
    42  		x0 ^= bits.RotateLeft32(u, 18)
    43  
    44  		u = x5 + x1
    45  		x9 ^= bits.RotateLeft32(u, 7)
    46  		u = x9 + x5
    47  		x13 ^= bits.RotateLeft32(u, 9)
    48  		u = x13 + x9
    49  		x1 ^= bits.RotateLeft32(u, 13)
    50  		u = x1 + x13
    51  		x5 ^= bits.RotateLeft32(u, 18)
    52  
    53  		u = x10 + x6
    54  		x14 ^= bits.RotateLeft32(u, 7)
    55  		u = x14 + x10
    56  		x2 ^= bits.RotateLeft32(u, 9)
    57  		u = x2 + x14
    58  		x6 ^= bits.RotateLeft32(u, 13)
    59  		u = x6 + x2
    60  		x10 ^= bits.RotateLeft32(u, 18)
    61  
    62  		u = x15 + x11
    63  		x3 ^= bits.RotateLeft32(u, 7)
    64  		u = x3 + x15
    65  		x7 ^= bits.RotateLeft32(u, 9)
    66  		u = x7 + x3
    67  		x11 ^= bits.RotateLeft32(u, 13)
    68  		u = x11 + x7
    69  		x15 ^= bits.RotateLeft32(u, 18)
    70  
    71  		u = x0 + x3
    72  		x1 ^= bits.RotateLeft32(u, 7)
    73  		u = x1 + x0
    74  		x2 ^= bits.RotateLeft32(u, 9)
    75  		u = x2 + x1
    76  		x3 ^= bits.RotateLeft32(u, 13)
    77  		u = x3 + x2
    78  		x0 ^= bits.RotateLeft32(u, 18)
    79  
    80  		u = x5 + x4
    81  		x6 ^= bits.RotateLeft32(u, 7)
    82  		u = x6 + x5
    83  		x7 ^= bits.RotateLeft32(u, 9)
    84  		u = x7 + x6
    85  		x4 ^= bits.RotateLeft32(u, 13)
    86  		u = x4 + x7
    87  		x5 ^= bits.RotateLeft32(u, 18)
    88  
    89  		u = x10 + x9
    90  		x11 ^= bits.RotateLeft32(u, 7)
    91  		u = x11 + x10
    92  		x8 ^= bits.RotateLeft32(u, 9)
    93  		u = x8 + x11
    94  		x9 ^= bits.RotateLeft32(u, 13)
    95  		u = x9 + x8
    96  		x10 ^= bits.RotateLeft32(u, 18)
    97  
    98  		u = x15 + x14
    99  		x12 ^= bits.RotateLeft32(u, 7)
   100  		u = x12 + x15
   101  		x13 ^= bits.RotateLeft32(u, 9)
   102  		u = x13 + x12
   103  		x14 ^= bits.RotateLeft32(u, 13)
   104  		u = x14 + x13
   105  		x15 ^= bits.RotateLeft32(u, 18)
   106  	}
   107  	out[0] = byte(x0)
   108  	out[1] = byte(x0 >> 8)
   109  	out[2] = byte(x0 >> 16)
   110  	out[3] = byte(x0 >> 24)
   111  
   112  	out[4] = byte(x5)
   113  	out[5] = byte(x5 >> 8)
   114  	out[6] = byte(x5 >> 16)
   115  	out[7] = byte(x5 >> 24)
   116  
   117  	out[8] = byte(x10)
   118  	out[9] = byte(x10 >> 8)
   119  	out[10] = byte(x10 >> 16)
   120  	out[11] = byte(x10 >> 24)
   121  
   122  	out[12] = byte(x15)
   123  	out[13] = byte(x15 >> 8)
   124  	out[14] = byte(x15 >> 16)
   125  	out[15] = byte(x15 >> 24)
   126  
   127  	out[16] = byte(x6)
   128  	out[17] = byte(x6 >> 8)
   129  	out[18] = byte(x6 >> 16)
   130  	out[19] = byte(x6 >> 24)
   131  
   132  	out[20] = byte(x7)
   133  	out[21] = byte(x7 >> 8)
   134  	out[22] = byte(x7 >> 16)
   135  	out[23] = byte(x7 >> 24)
   136  
   137  	out[24] = byte(x8)
   138  	out[25] = byte(x8 >> 8)
   139  	out[26] = byte(x8 >> 16)
   140  	out[27] = byte(x8 >> 24)
   141  
   142  	out[28] = byte(x9)
   143  	out[29] = byte(x9 >> 8)
   144  	out[30] = byte(x9 >> 16)
   145  	out[31] = byte(x9 >> 24)
   146  }
   147  

View as plain text