You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.0 KiB

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