sha256block_amd64.s 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. // SHA256 block routine. See sha256block.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 <= 63
  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 63 {
  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. MOVL (index*4)(SI), AX; \
  47. BSWAPL AX; \
  48. MOVL AX, (index*4)(BP)
  49. // Wt = SIGMA1(Wt-2) + Wt-7 + SIGMA0(Wt-15) + Wt-16; for 16 <= t <= 63
  50. // SIGMA0(x) = ROTR(7,x) XOR ROTR(18,x) XOR SHR(3,x)
  51. // SIGMA1(x) = ROTR(17,x) XOR ROTR(19,x) XOR SHR(10,x)
  52. #define MSGSCHEDULE1(index) \
  53. MOVL ((index-2)*4)(BP), AX; \
  54. MOVL AX, CX; \
  55. RORL $17, AX; \
  56. MOVL CX, DX; \
  57. RORL $19, CX; \
  58. SHRL $10, DX; \
  59. MOVL ((index-15)*4)(BP), BX; \
  60. XORL CX, AX; \
  61. MOVL BX, CX; \
  62. XORL DX, AX; \
  63. RORL $7, BX; \
  64. MOVL CX, DX; \
  65. SHRL $3, DX; \
  66. RORL $18, CX; \
  67. ADDL ((index-7)*4)(BP), AX; \
  68. XORL CX, BX; \
  69. XORL DX, BX; \
  70. ADDL ((index-16)*4)(BP), BX; \
  71. ADDL BX, AX; \
  72. MOVL AX, ((index)*4)(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(6,x) XOR ROTR(11,x) XOR ROTR(25,x)
  77. // Ch(x, y, z) = (x AND y) XOR (NOT x AND z)
  78. #define SHA256T1(const, e, f, g, h) \
  79. ADDL AX, h; \
  80. MOVL e, AX; \
  81. ADDL $const, h; \
  82. MOVL e, CX; \
  83. RORL $6, AX; \
  84. MOVL e, DX; \
  85. RORL $11, CX; \
  86. XORL CX, AX; \
  87. MOVL e, CX; \
  88. RORL $25, DX; \
  89. ANDL f, CX; \
  90. XORL AX, DX; \
  91. MOVL e, AX; \
  92. NOTL AX; \
  93. ADDL DX, h; \
  94. ANDL g, AX; \
  95. XORL CX, AX; \
  96. ADDL h, AX
  97. // Calculate T2 in BX - uses BX, CX, DX and DI registers.
  98. // T2 = BIGSIGMA0(a) + Maj(a, b, c)
  99. // BIGSIGMA0(x) = ROTR(2,x) XOR ROTR(13,x) XOR ROTR(22,x)
  100. // Maj(x, y, z) = (x AND y) XOR (x AND z) XOR (y AND z)
  101. #define SHA256T2(a, b, c) \
  102. MOVL a, DI; \
  103. MOVL c, BX; \
  104. RORL $2, DI; \
  105. MOVL a, DX; \
  106. ANDL b, BX; \
  107. RORL $13, DX; \
  108. MOVL a, CX; \
  109. ANDL c, CX; \
  110. XORL DX, DI; \
  111. XORL CX, BX; \
  112. MOVL a, DX; \
  113. MOVL b, CX; \
  114. RORL $22, DX; \
  115. ANDL a, CX; \
  116. XORL CX, BX; \
  117. XORL DX, DI; \
  118. ADDL DI, BX
  119. // Calculate T1 and T2, then e = d + T1 and a = T1 + T2.
  120. // The values for e and a are stored in d and h, ready for rotation.
  121. #define SHA256ROUND(index, const, a, b, c, d, e, f, g, h) \
  122. SHA256T1(const, e, f, g, h); \
  123. SHA256T2(a, b, c); \
  124. MOVL BX, h; \
  125. ADDL AX, d; \
  126. ADDL AX, h
  127. #define SHA256ROUND0(index, const, a, b, c, d, e, f, g, h) \
  128. MSGSCHEDULE0(index); \
  129. SHA256ROUND(index, const, a, b, c, d, e, f, g, h)
  130. #define SHA256ROUND1(index, const, a, b, c, d, e, f, g, h) \
  131. MSGSCHEDULE1(index); \
  132. SHA256ROUND(index, const, a, b, c, d, e, f, g, h)
  133. TEXT ·block(SB),0,$264-32
  134. MOVQ p_base+8(FP), SI
  135. MOVQ p_len+16(FP), DX
  136. SHRQ $6, DX
  137. SHLQ $6, DX
  138. LEAQ (SI)(DX*1), DI
  139. MOVQ DI, 256(SP)
  140. CMPQ SI, DI
  141. JEQ end
  142. MOVQ dig+0(FP), BP
  143. MOVL (0*4)(BP), R8 // a = H0
  144. MOVL (1*4)(BP), R9 // b = H1
  145. MOVL (2*4)(BP), R10 // c = H2
  146. MOVL (3*4)(BP), R11 // d = H3
  147. MOVL (4*4)(BP), R12 // e = H4
  148. MOVL (5*4)(BP), R13 // f = H5
  149. MOVL (6*4)(BP), R14 // g = H6
  150. MOVL (7*4)(BP), R15 // h = H7
  151. loop:
  152. MOVQ SP, BP // message schedule
  153. SHA256ROUND0(0, 0x428a2f98, R8, R9, R10, R11, R12, R13, R14, R15)
  154. SHA256ROUND0(1, 0x71374491, R15, R8, R9, R10, R11, R12, R13, R14)
  155. SHA256ROUND0(2, 0xb5c0fbcf, R14, R15, R8, R9, R10, R11, R12, R13)
  156. SHA256ROUND0(3, 0xe9b5dba5, R13, R14, R15, R8, R9, R10, R11, R12)
  157. SHA256ROUND0(4, 0x3956c25b, R12, R13, R14, R15, R8, R9, R10, R11)
  158. SHA256ROUND0(5, 0x59f111f1, R11, R12, R13, R14, R15, R8, R9, R10)
  159. SHA256ROUND0(6, 0x923f82a4, R10, R11, R12, R13, R14, R15, R8, R9)
  160. SHA256ROUND0(7, 0xab1c5ed5, R9, R10, R11, R12, R13, R14, R15, R8)
  161. SHA256ROUND0(8, 0xd807aa98, R8, R9, R10, R11, R12, R13, R14, R15)
  162. SHA256ROUND0(9, 0x12835b01, R15, R8, R9, R10, R11, R12, R13, R14)
  163. SHA256ROUND0(10, 0x243185be, R14, R15, R8, R9, R10, R11, R12, R13)
  164. SHA256ROUND0(11, 0x550c7dc3, R13, R14, R15, R8, R9, R10, R11, R12)
  165. SHA256ROUND0(12, 0x72be5d74, R12, R13, R14, R15, R8, R9, R10, R11)
  166. SHA256ROUND0(13, 0x80deb1fe, R11, R12, R13, R14, R15, R8, R9, R10)
  167. SHA256ROUND0(14, 0x9bdc06a7, R10, R11, R12, R13, R14, R15, R8, R9)
  168. SHA256ROUND0(15, 0xc19bf174, R9, R10, R11, R12, R13, R14, R15, R8)
  169. SHA256ROUND1(16, 0xe49b69c1, R8, R9, R10, R11, R12, R13, R14, R15)
  170. SHA256ROUND1(17, 0xefbe4786, R15, R8, R9, R10, R11, R12, R13, R14)
  171. SHA256ROUND1(18, 0x0fc19dc6, R14, R15, R8, R9, R10, R11, R12, R13)
  172. SHA256ROUND1(19, 0x240ca1cc, R13, R14, R15, R8, R9, R10, R11, R12)
  173. SHA256ROUND1(20, 0x2de92c6f, R12, R13, R14, R15, R8, R9, R10, R11)
  174. SHA256ROUND1(21, 0x4a7484aa, R11, R12, R13, R14, R15, R8, R9, R10)
  175. SHA256ROUND1(22, 0x5cb0a9dc, R10, R11, R12, R13, R14, R15, R8, R9)
  176. SHA256ROUND1(23, 0x76f988da, R9, R10, R11, R12, R13, R14, R15, R8)
  177. SHA256ROUND1(24, 0x983e5152, R8, R9, R10, R11, R12, R13, R14, R15)
  178. SHA256ROUND1(25, 0xa831c66d, R15, R8, R9, R10, R11, R12, R13, R14)
  179. SHA256ROUND1(26, 0xb00327c8, R14, R15, R8, R9, R10, R11, R12, R13)
  180. SHA256ROUND1(27, 0xbf597fc7, R13, R14, R15, R8, R9, R10, R11, R12)
  181. SHA256ROUND1(28, 0xc6e00bf3, R12, R13, R14, R15, R8, R9, R10, R11)
  182. SHA256ROUND1(29, 0xd5a79147, R11, R12, R13, R14, R15, R8, R9, R10)
  183. SHA256ROUND1(30, 0x06ca6351, R10, R11, R12, R13, R14, R15, R8, R9)
  184. SHA256ROUND1(31, 0x14292967, R9, R10, R11, R12, R13, R14, R15, R8)
  185. SHA256ROUND1(32, 0x27b70a85, R8, R9, R10, R11, R12, R13, R14, R15)
  186. SHA256ROUND1(33, 0x2e1b2138, R15, R8, R9, R10, R11, R12, R13, R14)
  187. SHA256ROUND1(34, 0x4d2c6dfc, R14, R15, R8, R9, R10, R11, R12, R13)
  188. SHA256ROUND1(35, 0x53380d13, R13, R14, R15, R8, R9, R10, R11, R12)
  189. SHA256ROUND1(36, 0x650a7354, R12, R13, R14, R15, R8, R9, R10, R11)
  190. SHA256ROUND1(37, 0x766a0abb, R11, R12, R13, R14, R15, R8, R9, R10)
  191. SHA256ROUND1(38, 0x81c2c92e, R10, R11, R12, R13, R14, R15, R8, R9)
  192. SHA256ROUND1(39, 0x92722c85, R9, R10, R11, R12, R13, R14, R15, R8)
  193. SHA256ROUND1(40, 0xa2bfe8a1, R8, R9, R10, R11, R12, R13, R14, R15)
  194. SHA256ROUND1(41, 0xa81a664b, R15, R8, R9, R10, R11, R12, R13, R14)
  195. SHA256ROUND1(42, 0xc24b8b70, R14, R15, R8, R9, R10, R11, R12, R13)
  196. SHA256ROUND1(43, 0xc76c51a3, R13, R14, R15, R8, R9, R10, R11, R12)
  197. SHA256ROUND1(44, 0xd192e819, R12, R13, R14, R15, R8, R9, R10, R11)
  198. SHA256ROUND1(45, 0xd6990624, R11, R12, R13, R14, R15, R8, R9, R10)
  199. SHA256ROUND1(46, 0xf40e3585, R10, R11, R12, R13, R14, R15, R8, R9)
  200. SHA256ROUND1(47, 0x106aa070, R9, R10, R11, R12, R13, R14, R15, R8)
  201. SHA256ROUND1(48, 0x19a4c116, R8, R9, R10, R11, R12, R13, R14, R15)
  202. SHA256ROUND1(49, 0x1e376c08, R15, R8, R9, R10, R11, R12, R13, R14)
  203. SHA256ROUND1(50, 0x2748774c, R14, R15, R8, R9, R10, R11, R12, R13)
  204. SHA256ROUND1(51, 0x34b0bcb5, R13, R14, R15, R8, R9, R10, R11, R12)
  205. SHA256ROUND1(52, 0x391c0cb3, R12, R13, R14, R15, R8, R9, R10, R11)
  206. SHA256ROUND1(53, 0x4ed8aa4a, R11, R12, R13, R14, R15, R8, R9, R10)
  207. SHA256ROUND1(54, 0x5b9cca4f, R10, R11, R12, R13, R14, R15, R8, R9)
  208. SHA256ROUND1(55, 0x682e6ff3, R9, R10, R11, R12, R13, R14, R15, R8)
  209. SHA256ROUND1(56, 0x748f82ee, R8, R9, R10, R11, R12, R13, R14, R15)
  210. SHA256ROUND1(57, 0x78a5636f, R15, R8, R9, R10, R11, R12, R13, R14)
  211. SHA256ROUND1(58, 0x84c87814, R14, R15, R8, R9, R10, R11, R12, R13)
  212. SHA256ROUND1(59, 0x8cc70208, R13, R14, R15, R8, R9, R10, R11, R12)
  213. SHA256ROUND1(60, 0x90befffa, R12, R13, R14, R15, R8, R9, R10, R11)
  214. SHA256ROUND1(61, 0xa4506ceb, R11, R12, R13, R14, R15, R8, R9, R10)
  215. SHA256ROUND1(62, 0xbef9a3f7, R10, R11, R12, R13, R14, R15, R8, R9)
  216. SHA256ROUND1(63, 0xc67178f2, R9, R10, R11, R12, R13, R14, R15, R8)
  217. MOVQ dig+0(FP), BP
  218. ADDL (0*4)(BP), R8 // H0 = a + H0
  219. MOVL R8, (0*4)(BP)
  220. ADDL (1*4)(BP), R9 // H1 = b + H1
  221. MOVL R9, (1*4)(BP)
  222. ADDL (2*4)(BP), R10 // H2 = c + H2
  223. MOVL R10, (2*4)(BP)
  224. ADDL (3*4)(BP), R11 // H3 = d + H3
  225. MOVL R11, (3*4)(BP)
  226. ADDL (4*4)(BP), R12 // H4 = e + H4
  227. MOVL R12, (4*4)(BP)
  228. ADDL (5*4)(BP), R13 // H5 = f + H5
  229. MOVL R13, (5*4)(BP)
  230. ADDL (6*4)(BP), R14 // H6 = g + H6
  231. MOVL R14, (6*4)(BP)
  232. ADDL (7*4)(BP), R15 // H7 = h + H7
  233. MOVL R15, (7*4)(BP)
  234. ADDQ $64, SI
  235. CMPQ SI, 256(SP)
  236. JB loop
  237. end:
  238. RET