github.com/xmplusdev/xray-core@v1.8.10/common/crypto/internal/chacha_core.generated.go (about)

     1  package internal
     2  
     3  import "encoding/binary"
     4  
     5  func ChaCha20Block(s *[16]uint32, out []byte, rounds int) {
     6  	x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15 := s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[8], s[9], s[10], s[11], s[12], s[13], s[14], s[15]
     7  	for i := 0; i < rounds; i += 2 {
     8  		var x uint32
     9  
    10  		x0 += x4
    11  		x = x12 ^ x0
    12  		x12 = (x << 16) | (x >> (32 - 16))
    13  		x8 += x12
    14  		x = x4 ^ x8
    15  		x4 = (x << 12) | (x >> (32 - 12))
    16  		x0 += x4
    17  		x = x12 ^ x0
    18  		x12 = (x << 8) | (x >> (32 - 8))
    19  		x8 += x12
    20  		x = x4 ^ x8
    21  		x4 = (x << 7) | (x >> (32 - 7))
    22  		x1 += x5
    23  		x = x13 ^ x1
    24  		x13 = (x << 16) | (x >> (32 - 16))
    25  		x9 += x13
    26  		x = x5 ^ x9
    27  		x5 = (x << 12) | (x >> (32 - 12))
    28  		x1 += x5
    29  		x = x13 ^ x1
    30  		x13 = (x << 8) | (x >> (32 - 8))
    31  		x9 += x13
    32  		x = x5 ^ x9
    33  		x5 = (x << 7) | (x >> (32 - 7))
    34  		x2 += x6
    35  		x = x14 ^ x2
    36  		x14 = (x << 16) | (x >> (32 - 16))
    37  		x10 += x14
    38  		x = x6 ^ x10
    39  		x6 = (x << 12) | (x >> (32 - 12))
    40  		x2 += x6
    41  		x = x14 ^ x2
    42  		x14 = (x << 8) | (x >> (32 - 8))
    43  		x10 += x14
    44  		x = x6 ^ x10
    45  		x6 = (x << 7) | (x >> (32 - 7))
    46  		x3 += x7
    47  		x = x15 ^ x3
    48  		x15 = (x << 16) | (x >> (32 - 16))
    49  		x11 += x15
    50  		x = x7 ^ x11
    51  		x7 = (x << 12) | (x >> (32 - 12))
    52  		x3 += x7
    53  		x = x15 ^ x3
    54  		x15 = (x << 8) | (x >> (32 - 8))
    55  		x11 += x15
    56  		x = x7 ^ x11
    57  		x7 = (x << 7) | (x >> (32 - 7))
    58  		x0 += x5
    59  		x = x15 ^ x0
    60  		x15 = (x << 16) | (x >> (32 - 16))
    61  		x10 += x15
    62  		x = x5 ^ x10
    63  		x5 = (x << 12) | (x >> (32 - 12))
    64  		x0 += x5
    65  		x = x15 ^ x0
    66  		x15 = (x << 8) | (x >> (32 - 8))
    67  		x10 += x15
    68  		x = x5 ^ x10
    69  		x5 = (x << 7) | (x >> (32 - 7))
    70  		x1 += x6
    71  		x = x12 ^ x1
    72  		x12 = (x << 16) | (x >> (32 - 16))
    73  		x11 += x12
    74  		x = x6 ^ x11
    75  		x6 = (x << 12) | (x >> (32 - 12))
    76  		x1 += x6
    77  		x = x12 ^ x1
    78  		x12 = (x << 8) | (x >> (32 - 8))
    79  		x11 += x12
    80  		x = x6 ^ x11
    81  		x6 = (x << 7) | (x >> (32 - 7))
    82  		x2 += x7
    83  		x = x13 ^ x2
    84  		x13 = (x << 16) | (x >> (32 - 16))
    85  		x8 += x13
    86  		x = x7 ^ x8
    87  		x7 = (x << 12) | (x >> (32 - 12))
    88  		x2 += x7
    89  		x = x13 ^ x2
    90  		x13 = (x << 8) | (x >> (32 - 8))
    91  		x8 += x13
    92  		x = x7 ^ x8
    93  		x7 = (x << 7) | (x >> (32 - 7))
    94  		x3 += x4
    95  		x = x14 ^ x3
    96  		x14 = (x << 16) | (x >> (32 - 16))
    97  		x9 += x14
    98  		x = x4 ^ x9
    99  		x4 = (x << 12) | (x >> (32 - 12))
   100  		x3 += x4
   101  		x = x14 ^ x3
   102  		x14 = (x << 8) | (x >> (32 - 8))
   103  		x9 += x14
   104  		x = x4 ^ x9
   105  		x4 = (x << 7) | (x >> (32 - 7))
   106  	}
   107  	binary.LittleEndian.PutUint32(out[0:4], s[0]+x0)
   108  	binary.LittleEndian.PutUint32(out[4:8], s[1]+x1)
   109  	binary.LittleEndian.PutUint32(out[8:12], s[2]+x2)
   110  	binary.LittleEndian.PutUint32(out[12:16], s[3]+x3)
   111  	binary.LittleEndian.PutUint32(out[16:20], s[4]+x4)
   112  	binary.LittleEndian.PutUint32(out[20:24], s[5]+x5)
   113  	binary.LittleEndian.PutUint32(out[24:28], s[6]+x6)
   114  	binary.LittleEndian.PutUint32(out[28:32], s[7]+x7)
   115  	binary.LittleEndian.PutUint32(out[32:36], s[8]+x8)
   116  	binary.LittleEndian.PutUint32(out[36:40], s[9]+x9)
   117  	binary.LittleEndian.PutUint32(out[40:44], s[10]+x10)
   118  	binary.LittleEndian.PutUint32(out[44:48], s[11]+x11)
   119  	binary.LittleEndian.PutUint32(out[48:52], s[12]+x12)
   120  	binary.LittleEndian.PutUint32(out[52:56], s[13]+x13)
   121  	binary.LittleEndian.PutUint32(out[56:60], s[14]+x14)
   122  	binary.LittleEndian.PutUint32(out[60:64], s[15]+x15)
   123  }