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.

171 lines
6.6 KiB

  1. // Copyright 2013 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 sha3
  5. // This file implements the core Keccak permutation function necessary for computing SHA3.
  6. // This is implemented in a separate file to allow for replacement by an optimized implementation.
  7. // Nothing in this package is exported.
  8. // For the detailed specification, refer to the Keccak web site (http://keccak.noekeon.org/).
  9. // rc stores the round constants for use in the ι step.
  10. var rc = [...]uint64{
  11. 0x0000000000000001,
  12. 0x0000000000008082,
  13. 0x800000000000808A,
  14. 0x8000000080008000,
  15. 0x000000000000808B,
  16. 0x0000000080000001,
  17. 0x8000000080008081,
  18. 0x8000000000008009,
  19. 0x000000000000008A,
  20. 0x0000000000000088,
  21. 0x0000000080008009,
  22. 0x000000008000000A,
  23. 0x000000008000808B,
  24. 0x800000000000008B,
  25. 0x8000000000008089,
  26. 0x8000000000008003,
  27. 0x8000000000008002,
  28. 0x8000000000000080,
  29. 0x000000000000800A,
  30. 0x800000008000000A,
  31. 0x8000000080008081,
  32. 0x8000000000008080,
  33. 0x0000000080000001,
  34. 0x8000000080008008,
  35. }
  36. // ro_xx represent the rotation offsets for use in the χ step.
  37. // Defining them as const instead of in an array allows the compiler to insert constant shifts.
  38. const (
  39. ro_00 = 0
  40. ro_01 = 36
  41. ro_02 = 3
  42. ro_03 = 41
  43. ro_04 = 18
  44. ro_05 = 1
  45. ro_06 = 44
  46. ro_07 = 10
  47. ro_08 = 45
  48. ro_09 = 2
  49. ro_10 = 62
  50. ro_11 = 6
  51. ro_12 = 43
  52. ro_13 = 15
  53. ro_14 = 61
  54. ro_15 = 28
  55. ro_16 = 55
  56. ro_17 = 25
  57. ro_18 = 21
  58. ro_19 = 56
  59. ro_20 = 27
  60. ro_21 = 20
  61. ro_22 = 39
  62. ro_23 = 8
  63. ro_24 = 14
  64. )
  65. // keccakF computes the complete Keccak-f function consisting of 24 rounds with a different
  66. // constant (rc) in each round. This implementation fully unrolls the round function to avoid
  67. // inner loops, as well as pre-calculating shift offsets.
  68. func (d *digest) keccakF() {
  69. for _, roundConstant := range rc {
  70. // θ step
  71. d.c[0] = d.a[0] ^ d.a[5] ^ d.a[10] ^ d.a[15] ^ d.a[20]
  72. d.c[1] = d.a[1] ^ d.a[6] ^ d.a[11] ^ d.a[16] ^ d.a[21]
  73. d.c[2] = d.a[2] ^ d.a[7] ^ d.a[12] ^ d.a[17] ^ d.a[22]
  74. d.c[3] = d.a[3] ^ d.a[8] ^ d.a[13] ^ d.a[18] ^ d.a[23]
  75. d.c[4] = d.a[4] ^ d.a[9] ^ d.a[14] ^ d.a[19] ^ d.a[24]
  76. d.d[0] = d.c[4] ^ (d.c[1]<<1 ^ d.c[1]>>63)
  77. d.d[1] = d.c[0] ^ (d.c[2]<<1 ^ d.c[2]>>63)
  78. d.d[2] = d.c[1] ^ (d.c[3]<<1 ^ d.c[3]>>63)
  79. d.d[3] = d.c[2] ^ (d.c[4]<<1 ^ d.c[4]>>63)
  80. d.d[4] = d.c[3] ^ (d.c[0]<<1 ^ d.c[0]>>63)
  81. d.a[0] ^= d.d[0]
  82. d.a[1] ^= d.d[1]
  83. d.a[2] ^= d.d[2]
  84. d.a[3] ^= d.d[3]
  85. d.a[4] ^= d.d[4]
  86. d.a[5] ^= d.d[0]
  87. d.a[6] ^= d.d[1]
  88. d.a[7] ^= d.d[2]
  89. d.a[8] ^= d.d[3]
  90. d.a[9] ^= d.d[4]
  91. d.a[10] ^= d.d[0]
  92. d.a[11] ^= d.d[1]
  93. d.a[12] ^= d.d[2]
  94. d.a[13] ^= d.d[3]
  95. d.a[14] ^= d.d[4]
  96. d.a[15] ^= d.d[0]
  97. d.a[16] ^= d.d[1]
  98. d.a[17] ^= d.d[2]
  99. d.a[18] ^= d.d[3]
  100. d.a[19] ^= d.d[4]
  101. d.a[20] ^= d.d[0]
  102. d.a[21] ^= d.d[1]
  103. d.a[22] ^= d.d[2]
  104. d.a[23] ^= d.d[3]
  105. d.a[24] ^= d.d[4]
  106. // ρ and π steps
  107. d.b[0] = d.a[0]
  108. d.b[1] = d.a[6]<<ro_06 ^ d.a[6]>>(64-ro_06)
  109. d.b[2] = d.a[12]<<ro_12 ^ d.a[12]>>(64-ro_12)
  110. d.b[3] = d.a[18]<<ro_18 ^ d.a[18]>>(64-ro_18)
  111. d.b[4] = d.a[24]<<ro_24 ^ d.a[24]>>(64-ro_24)
  112. d.b[5] = d.a[3]<<ro_15 ^ d.a[3]>>(64-ro_15)
  113. d.b[6] = d.a[9]<<ro_21 ^ d.a[9]>>(64-ro_21)
  114. d.b[7] = d.a[10]<<ro_02 ^ d.a[10]>>(64-ro_02)
  115. d.b[8] = d.a[16]<<ro_08 ^ d.a[16]>>(64-ro_08)
  116. d.b[9] = d.a[22]<<ro_14 ^ d.a[22]>>(64-ro_14)
  117. d.b[10] = d.a[1]<<ro_05 ^ d.a[1]>>(64-ro_05)
  118. d.b[11] = d.a[7]<<ro_11 ^ d.a[7]>>(64-ro_11)
  119. d.b[12] = d.a[13]<<ro_17 ^ d.a[13]>>(64-ro_17)
  120. d.b[13] = d.a[19]<<ro_23 ^ d.a[19]>>(64-ro_23)
  121. d.b[14] = d.a[20]<<ro_04 ^ d.a[20]>>(64-ro_04)
  122. d.b[15] = d.a[4]<<ro_20 ^ d.a[4]>>(64-ro_20)
  123. d.b[16] = d.a[5]<<ro_01 ^ d.a[5]>>(64-ro_01)
  124. d.b[17] = d.a[11]<<ro_07 ^ d.a[11]>>(64-ro_07)
  125. d.b[18] = d.a[17]<<ro_13 ^ d.a[17]>>(64-ro_13)
  126. d.b[19] = d.a[23]<<ro_19 ^ d.a[23]>>(64-ro_19)
  127. d.b[20] = d.a[2]<<ro_10 ^ d.a[2]>>(64-ro_10)
  128. d.b[21] = d.a[8]<<ro_16 ^ d.a[8]>>(64-ro_16)
  129. d.b[22] = d.a[14]<<ro_22 ^ d.a[14]>>(64-ro_22)
  130. d.b[23] = d.a[15]<<ro_03 ^ d.a[15]>>(64-ro_03)
  131. d.b[24] = d.a[21]<<ro_09 ^ d.a[21]>>(64-ro_09)
  132. // χ step
  133. d.a[0] = d.b[0] ^ (^d.b[1] & d.b[2])
  134. d.a[1] = d.b[1] ^ (^d.b[2] & d.b[3])
  135. d.a[2] = d.b[2] ^ (^d.b[3] & d.b[4])
  136. d.a[3] = d.b[3] ^ (^d.b[4] & d.b[0])
  137. d.a[4] = d.b[4] ^ (^d.b[0] & d.b[1])
  138. d.a[5] = d.b[5] ^ (^d.b[6] & d.b[7])
  139. d.a[6] = d.b[6] ^ (^d.b[7] & d.b[8])
  140. d.a[7] = d.b[7] ^ (^d.b[8] & d.b[9])
  141. d.a[8] = d.b[8] ^ (^d.b[9] & d.b[5])
  142. d.a[9] = d.b[9] ^ (^d.b[5] & d.b[6])
  143. d.a[10] = d.b[10] ^ (^d.b[11] & d.b[12])
  144. d.a[11] = d.b[11] ^ (^d.b[12] & d.b[13])
  145. d.a[12] = d.b[12] ^ (^d.b[13] & d.b[14])
  146. d.a[13] = d.b[13] ^ (^d.b[14] & d.b[10])
  147. d.a[14] = d.b[14] ^ (^d.b[10] & d.b[11])
  148. d.a[15] = d.b[15] ^ (^d.b[16] & d.b[17])
  149. d.a[16] = d.b[16] ^ (^d.b[17] & d.b[18])
  150. d.a[17] = d.b[17] ^ (^d.b[18] & d.b[19])
  151. d.a[18] = d.b[18] ^ (^d.b[19] & d.b[15])
  152. d.a[19] = d.b[19] ^ (^d.b[15] & d.b[16])
  153. d.a[20] = d.b[20] ^ (^d.b[21] & d.b[22])
  154. d.a[21] = d.b[21] ^ (^d.b[22] & d.b[23])
  155. d.a[22] = d.b[22] ^ (^d.b[23] & d.b[24])
  156. d.a[23] = d.b[23] ^ (^d.b[24] & d.b[20])
  157. d.a[24] = d.b[24] ^ (^d.b[20] & d.b[21])
  158. // ι step
  159. d.a[0] ^= roundConstant
  160. }
  161. }