sha512block_amd64.s 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. #include "textflag.h"
  5. // SHA512 block routine. See sha512block.go for Go equivalent.
  6. //
  7. // The algorithm is detailed in FIPS 180-4:
  8. //
  9. // http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf
  10. //
  11. // Wt = Mt; for 0 <= t <= 15
  12. // Wt = SIGMA1(Wt-2) + SIGMA0(Wt-15) + Wt-16; for 16 <= t <= 79
  13. //
  14. // a = H0
  15. // b = H1
  16. // c = H2
  17. // d = H3
  18. // e = H4
  19. // f = H5
  20. // g = H6
  21. // h = H7
  22. //
  23. // for t = 0 to 79 {
  24. // T1 = h + BIGSIGMA1(e) + Ch(e,f,g) + Kt + Wt
  25. // T2 = BIGSIGMA0(a) + Maj(a,b,c)
  26. // h = g
  27. // g = f
  28. // f = e
  29. // e = d + T1
  30. // d = c
  31. // c = b
  32. // b = a
  33. // a = T1 + T2
  34. // }
  35. //
  36. // H0 = a + H0
  37. // H1 = b + H1
  38. // H2 = c + H2
  39. // H3 = d + H3
  40. // H4 = e + H4
  41. // H5 = f + H5
  42. // H6 = g + H6
  43. // H7 = h + H7
  44. // Wt = Mt; for 0 <= t <= 15
  45. #define MSGSCHEDULE0(index) \
  46. MOVQ (index*8)(SI), AX; \
  47. BSWAPQ AX; \
  48. MOVQ AX, (index*8)(BP)
  49. // Wt = SIGMA1(Wt-2) + Wt-7 + SIGMA0(Wt-15) + Wt-16; for 16 <= t <= 79
  50. // SIGMA0(x) = ROTR(1,x) XOR ROTR(8,x) XOR SHR(7,x)
  51. // SIGMA1(x) = ROTR(19,x) XOR ROTR(61,x) XOR SHR(6,x)
  52. #define MSGSCHEDULE1(index) \
  53. MOVQ ((index-2)*8)(BP), AX; \
  54. MOVQ AX, CX; \
  55. RORQ $19, AX; \
  56. MOVQ CX, DX; \
  57. RORQ $61, CX; \
  58. SHRQ $6, DX; \
  59. MOVQ ((index-15)*8)(BP), BX; \
  60. XORQ CX, AX; \
  61. MOVQ BX, CX; \
  62. XORQ DX, AX; \
  63. RORQ $1, BX; \
  64. MOVQ CX, DX; \
  65. SHRQ $7, DX; \
  66. RORQ $8, CX; \
  67. ADDQ ((index-7)*8)(BP), AX; \
  68. XORQ CX, BX; \
  69. XORQ DX, BX; \
  70. ADDQ ((index-16)*8)(BP), BX; \
  71. ADDQ BX, AX; \
  72. MOVQ AX, ((index)*8)(BP)
  73. // Calculate T1 in AX - uses AX, CX and DX registers.
  74. // h is also used as an accumulator. Wt is passed in AX.
  75. // T1 = h + BIGSIGMA1(e) + Ch(e, f, g) + Kt + Wt
  76. // BIGSIGMA1(x) = ROTR(14,x) XOR ROTR(18,x) XOR ROTR(41,x)
  77. // Ch(x, y, z) = (x AND y) XOR (NOT x AND z)
  78. #define SHA512T1(const, e, f, g, h) \
  79. MOVQ $const, DX; \
  80. ADDQ AX, h; \
  81. MOVQ e, AX; \
  82. ADDQ DX, h; \
  83. MOVQ e, CX; \
  84. RORQ $14, AX; \
  85. MOVQ e, DX; \
  86. RORQ $18, CX; \
  87. XORQ CX, AX; \
  88. MOVQ e, CX; \
  89. RORQ $41, DX; \
  90. ANDQ f, CX; \
  91. XORQ AX, DX; \
  92. MOVQ e, AX; \
  93. NOTQ AX; \
  94. ADDQ DX, h; \
  95. ANDQ g, AX; \
  96. XORQ CX, AX; \
  97. ADDQ h, AX
  98. // Calculate T2 in BX - uses BX, CX, DX and DI registers.
  99. // T2 = BIGSIGMA0(a) + Maj(a, b, c)
  100. // BIGSIGMA0(x) = ROTR(28,x) XOR ROTR(34,x) XOR ROTR(39,x)
  101. // Maj(x, y, z) = (x AND y) XOR (x AND z) XOR (y AND z)
  102. #define SHA512T2(a, b, c) \
  103. MOVQ a, DI; \
  104. MOVQ c, BX; \
  105. RORQ $28, DI; \
  106. MOVQ a, DX; \
  107. ANDQ b, BX; \
  108. RORQ $34, DX; \
  109. MOVQ a, CX; \
  110. ANDQ c, CX; \
  111. XORQ DX, DI; \
  112. XORQ CX, BX; \
  113. MOVQ a, DX; \
  114. MOVQ b, CX; \
  115. RORQ $39, DX; \
  116. ANDQ a, CX; \
  117. XORQ CX, BX; \
  118. XORQ DX, DI; \
  119. ADDQ DI, BX
  120. // Calculate T1 and T2, then e = d + T1 and a = T1 + T2.
  121. // The values for e and a are stored in d and h, ready for rotation.
  122. #define SHA512ROUND(index, const, a, b, c, d, e, f, g, h) \
  123. SHA512T1(const, e, f, g, h); \
  124. SHA512T2(a, b, c); \
  125. MOVQ BX, h; \
  126. ADDQ AX, d; \
  127. ADDQ AX, h
  128. #define SHA512ROUND0(index, const, a, b, c, d, e, f, g, h) \
  129. MSGSCHEDULE0(index); \
  130. SHA512ROUND(index, const, a, b, c, d, e, f, g, h)
  131. #define SHA512ROUND1(index, const, a, b, c, d, e, f, g, h) \
  132. MSGSCHEDULE1(index); \
  133. SHA512ROUND(index, const, a, b, c, d, e, f, g, h)
  134. TEXT ·block(SB),0,$648-32
  135. MOVQ p_base+8(FP), SI
  136. MOVQ p_len+16(FP), DX
  137. SHRQ $7, DX
  138. SHLQ $7, DX
  139. LEAQ (SI)(DX*1), DI
  140. MOVQ DI, 640(SP)
  141. CMPQ SI, DI
  142. JEQ end
  143. MOVQ dig+0(FP), BP
  144. MOVQ (0*8)(BP), R8 // a = H0
  145. MOVQ (1*8)(BP), R9 // b = H1
  146. MOVQ (2*8)(BP), R10 // c = H2
  147. MOVQ (3*8)(BP), R11 // d = H3
  148. MOVQ (4*8)(BP), R12 // e = H4
  149. MOVQ (5*8)(BP), R13 // f = H5
  150. MOVQ (6*8)(BP), R14 // g = H6
  151. MOVQ (7*8)(BP), R15 // h = H7
  152. loop:
  153. MOVQ SP, BP // message schedule
  154. SHA512ROUND0(0, 0x428a2f98d728ae22, R8, R9, R10, R11, R12, R13, R14, R15)
  155. SHA512ROUND0(1, 0x7137449123ef65cd, R15, R8, R9, R10, R11, R12, R13, R14)
  156. SHA512ROUND0(2, 0xb5c0fbcfec4d3b2f, R14, R15, R8, R9, R10, R11, R12, R13)
  157. SHA512ROUND0(3, 0xe9b5dba58189dbbc, R13, R14, R15, R8, R9, R10, R11, R12)
  158. SHA512ROUND0(4, 0x3956c25bf348b538, R12, R13, R14, R15, R8, R9, R10, R11)
  159. SHA512ROUND0(5, 0x59f111f1b605d019, R11, R12, R13, R14, R15, R8, R9, R10)
  160. SHA512ROUND0(6, 0x923f82a4af194f9b, R10, R11, R12, R13, R14, R15, R8, R9)
  161. SHA512ROUND0(7, 0xab1c5ed5da6d8118, R9, R10, R11, R12, R13, R14, R15, R8)
  162. SHA512ROUND0(8, 0xd807aa98a3030242, R8, R9, R10, R11, R12, R13, R14, R15)
  163. SHA512ROUND0(9, 0x12835b0145706fbe, R15, R8, R9, R10, R11, R12, R13, R14)
  164. SHA512ROUND0(10, 0x243185be4ee4b28c, R14, R15, R8, R9, R10, R11, R12, R13)
  165. SHA512ROUND0(11, 0x550c7dc3d5ffb4e2, R13, R14, R15, R8, R9, R10, R11, R12)
  166. SHA512ROUND0(12, 0x72be5d74f27b896f, R12, R13, R14, R15, R8, R9, R10, R11)
  167. SHA512ROUND0(13, 0x80deb1fe3b1696b1, R11, R12, R13, R14, R15, R8, R9, R10)
  168. SHA512ROUND0(14, 0x9bdc06a725c71235, R10, R11, R12, R13, R14, R15, R8, R9)
  169. SHA512ROUND0(15, 0xc19bf174cf692694, R9, R10, R11, R12, R13, R14, R15, R8)
  170. SHA512ROUND1(16, 0xe49b69c19ef14ad2, R8, R9, R10, R11, R12, R13, R14, R15)
  171. SHA512ROUND1(17, 0xefbe4786384f25e3, R15, R8, R9, R10, R11, R12, R13, R14)
  172. SHA512ROUND1(18, 0x0fc19dc68b8cd5b5, R14, R15, R8, R9, R10, R11, R12, R13)
  173. SHA512ROUND1(19, 0x240ca1cc77ac9c65, R13, R14, R15, R8, R9, R10, R11, R12)
  174. SHA512ROUND1(20, 0x2de92c6f592b0275, R12, R13, R14, R15, R8, R9, R10, R11)
  175. SHA512ROUND1(21, 0x4a7484aa6ea6e483, R11, R12, R13, R14, R15, R8, R9, R10)
  176. SHA512ROUND1(22, 0x5cb0a9dcbd41fbd4, R10, R11, R12, R13, R14, R15, R8, R9)
  177. SHA512ROUND1(23, 0x76f988da831153b5, R9, R10, R11, R12, R13, R14, R15, R8)
  178. SHA512ROUND1(24, 0x983e5152ee66dfab, R8, R9, R10, R11, R12, R13, R14, R15)
  179. SHA512ROUND1(25, 0xa831c66d2db43210, R15, R8, R9, R10, R11, R12, R13, R14)
  180. SHA512ROUND1(26, 0xb00327c898fb213f, R14, R15, R8, R9, R10, R11, R12, R13)
  181. SHA512ROUND1(27, 0xbf597fc7beef0ee4, R13, R14, R15, R8, R9, R10, R11, R12)
  182. SHA512ROUND1(28, 0xc6e00bf33da88fc2, R12, R13, R14, R15, R8, R9, R10, R11)
  183. SHA512ROUND1(29, 0xd5a79147930aa725, R11, R12, R13, R14, R15, R8, R9, R10)
  184. SHA512ROUND1(30, 0x06ca6351e003826f, R10, R11, R12, R13, R14, R15, R8, R9)
  185. SHA512ROUND1(31, 0x142929670a0e6e70, R9, R10, R11, R12, R13, R14, R15, R8)
  186. SHA512ROUND1(32, 0x27b70a8546d22ffc, R8, R9, R10, R11, R12, R13, R14, R15)
  187. SHA512ROUND1(33, 0x2e1b21385c26c926, R15, R8, R9, R10, R11, R12, R13, R14)
  188. SHA512ROUND1(34, 0x4d2c6dfc5ac42aed, R14, R15, R8, R9, R10, R11, R12, R13)
  189. SHA512ROUND1(35, 0x53380d139d95b3df, R13, R14, R15, R8, R9, R10, R11, R12)
  190. SHA512ROUND1(36, 0x650a73548baf63de, R12, R13, R14, R15, R8, R9, R10, R11)
  191. SHA512ROUND1(37, 0x766a0abb3c77b2a8, R11, R12, R13, R14, R15, R8, R9, R10)
  192. SHA512ROUND1(38, 0x81c2c92e47edaee6, R10, R11, R12, R13, R14, R15, R8, R9)
  193. SHA512ROUND1(39, 0x92722c851482353b, R9, R10, R11, R12, R13, R14, R15, R8)
  194. SHA512ROUND1(40, 0xa2bfe8a14cf10364, R8, R9, R10, R11, R12, R13, R14, R15)
  195. SHA512ROUND1(41, 0xa81a664bbc423001, R15, R8, R9, R10, R11, R12, R13, R14)
  196. SHA512ROUND1(42, 0xc24b8b70d0f89791, R14, R15, R8, R9, R10, R11, R12, R13)
  197. SHA512ROUND1(43, 0xc76c51a30654be30, R13, R14, R15, R8, R9, R10, R11, R12)
  198. SHA512ROUND1(44, 0xd192e819d6ef5218, R12, R13, R14, R15, R8, R9, R10, R11)
  199. SHA512ROUND1(45, 0xd69906245565a910, R11, R12, R13, R14, R15, R8, R9, R10)
  200. SHA512ROUND1(46, 0xf40e35855771202a, R10, R11, R12, R13, R14, R15, R8, R9)
  201. SHA512ROUND1(47, 0x106aa07032bbd1b8, R9, R10, R11, R12, R13, R14, R15, R8)
  202. SHA512ROUND1(48, 0x19a4c116b8d2d0c8, R8, R9, R10, R11, R12, R13, R14, R15)
  203. SHA512ROUND1(49, 0x1e376c085141ab53, R15, R8, R9, R10, R11, R12, R13, R14)
  204. SHA512ROUND1(50, 0x2748774cdf8eeb99, R14, R15, R8, R9, R10, R11, R12, R13)
  205. SHA512ROUND1(51, 0x34b0bcb5e19b48a8, R13, R14, R15, R8, R9, R10, R11, R12)
  206. SHA512ROUND1(52, 0x391c0cb3c5c95a63, R12, R13, R14, R15, R8, R9, R10, R11)
  207. SHA512ROUND1(53, 0x4ed8aa4ae3418acb, R11, R12, R13, R14, R15, R8, R9, R10)
  208. SHA512ROUND1(54, 0x5b9cca4f7763e373, R10, R11, R12, R13, R14, R15, R8, R9)
  209. SHA512ROUND1(55, 0x682e6ff3d6b2b8a3, R9, R10, R11, R12, R13, R14, R15, R8)
  210. SHA512ROUND1(56, 0x748f82ee5defb2fc, R8, R9, R10, R11, R12, R13, R14, R15)
  211. SHA512ROUND1(57, 0x78a5636f43172f60, R15, R8, R9, R10, R11, R12, R13, R14)
  212. SHA512ROUND1(58, 0x84c87814a1f0ab72, R14, R15, R8, R9, R10, R11, R12, R13)
  213. SHA512ROUND1(59, 0x8cc702081a6439ec, R13, R14, R15, R8, R9, R10, R11, R12)
  214. SHA512ROUND1(60, 0x90befffa23631e28, R12, R13, R14, R15, R8, R9, R10, R11)
  215. SHA512ROUND1(61, 0xa4506cebde82bde9, R11, R12, R13, R14, R15, R8, R9, R10)
  216. SHA512ROUND1(62, 0xbef9a3f7b2c67915, R10, R11, R12, R13, R14, R15, R8, R9)
  217. SHA512ROUND1(63, 0xc67178f2e372532b, R9, R10, R11, R12, R13, R14, R15, R8)
  218. SHA512ROUND1(64, 0xca273eceea26619c, R8, R9, R10, R11, R12, R13, R14, R15)
  219. SHA512ROUND1(65, 0xd186b8c721c0c207, R15, R8, R9, R10, R11, R12, R13, R14)
  220. SHA512ROUND1(66, 0xeada7dd6cde0eb1e, R14, R15, R8, R9, R10, R11, R12, R13)
  221. SHA512ROUND1(67, 0xf57d4f7fee6ed178, R13, R14, R15, R8, R9, R10, R11, R12)
  222. SHA512ROUND1(68, 0x06f067aa72176fba, R12, R13, R14, R15, R8, R9, R10, R11)
  223. SHA512ROUND1(69, 0x0a637dc5a2c898a6, R11, R12, R13, R14, R15, R8, R9, R10)
  224. SHA512ROUND1(70, 0x113f9804bef90dae, R10, R11, R12, R13, R14, R15, R8, R9)
  225. SHA512ROUND1(71, 0x1b710b35131c471b, R9, R10, R11, R12, R13, R14, R15, R8)
  226. SHA512ROUND1(72, 0x28db77f523047d84, R8, R9, R10, R11, R12, R13, R14, R15)
  227. SHA512ROUND1(73, 0x32caab7b40c72493, R15, R8, R9, R10, R11, R12, R13, R14)
  228. SHA512ROUND1(74, 0x3c9ebe0a15c9bebc, R14, R15, R8, R9, R10, R11, R12, R13)
  229. SHA512ROUND1(75, 0x431d67c49c100d4c, R13, R14, R15, R8, R9, R10, R11, R12)
  230. SHA512ROUND1(76, 0x4cc5d4becb3e42b6, R12, R13, R14, R15, R8, R9, R10, R11)
  231. SHA512ROUND1(77, 0x597f299cfc657e2a, R11, R12, R13, R14, R15, R8, R9, R10)
  232. SHA512ROUND1(78, 0x5fcb6fab3ad6faec, R10, R11, R12, R13, R14, R15, R8, R9)
  233. SHA512ROUND1(79, 0x6c44198c4a475817, R9, R10, R11, R12, R13, R14, R15, R8)
  234. MOVQ dig+0(FP), BP
  235. ADDQ (0*8)(BP), R8 // H0 = a + H0
  236. MOVQ R8, (0*8)(BP)
  237. ADDQ (1*8)(BP), R9 // H1 = b + H1
  238. MOVQ R9, (1*8)(BP)
  239. ADDQ (2*8)(BP), R10 // H2 = c + H2
  240. MOVQ R10, (2*8)(BP)
  241. ADDQ (3*8)(BP), R11 // H3 = d + H3
  242. MOVQ R11, (3*8)(BP)
  243. ADDQ (4*8)(BP), R12 // H4 = e + H4
  244. MOVQ R12, (4*8)(BP)
  245. ADDQ (5*8)(BP), R13 // H5 = f + H5
  246. MOVQ R13, (5*8)(BP)
  247. ADDQ (6*8)(BP), R14 // H6 = g + H6
  248. MOVQ R14, (6*8)(BP)
  249. ADDQ (7*8)(BP), R15 // H7 = h + H7
  250. MOVQ R15, (7*8)(BP)
  251. ADDQ $128, SI
  252. CMPQ SI, 640(SP)
  253. JB loop
  254. end:
  255. RET