md5-asm.S 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Fast MD5 implementation for PPC
  3. *
  4. * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #include <asm/ppc_asm.h>
  13. #include <asm/asm-offsets.h>
  14. #define rHP r3
  15. #define rWP r4
  16. #define rH0 r0
  17. #define rH1 r6
  18. #define rH2 r7
  19. #define rH3 r5
  20. #define rW00 r8
  21. #define rW01 r9
  22. #define rW02 r10
  23. #define rW03 r11
  24. #define rW04 r12
  25. #define rW05 r14
  26. #define rW06 r15
  27. #define rW07 r16
  28. #define rW08 r17
  29. #define rW09 r18
  30. #define rW10 r19
  31. #define rW11 r20
  32. #define rW12 r21
  33. #define rW13 r22
  34. #define rW14 r23
  35. #define rW15 r24
  36. #define rT0 r25
  37. #define rT1 r26
  38. #define INITIALIZE \
  39. PPC_STLU r1,-INT_FRAME_SIZE(r1); \
  40. SAVE_8GPRS(14, r1); /* push registers onto stack */ \
  41. SAVE_4GPRS(22, r1); \
  42. SAVE_GPR(26, r1)
  43. #define FINALIZE \
  44. REST_8GPRS(14, r1); /* pop registers from stack */ \
  45. REST_4GPRS(22, r1); \
  46. REST_GPR(26, r1); \
  47. addi r1,r1,INT_FRAME_SIZE;
  48. #ifdef __BIG_ENDIAN__
  49. #define LOAD_DATA(reg, off) \
  50. lwbrx reg,0,rWP; /* load data */
  51. #define INC_PTR \
  52. addi rWP,rWP,4; /* increment per word */
  53. #define NEXT_BLOCK /* nothing to do */
  54. #else
  55. #define LOAD_DATA(reg, off) \
  56. lwz reg,off(rWP); /* load data */
  57. #define INC_PTR /* nothing to do */
  58. #define NEXT_BLOCK \
  59. addi rWP,rWP,64; /* increment per block */
  60. #endif
  61. #define R_00_15(a, b, c, d, w0, w1, p, q, off, k0h, k0l, k1h, k1l) \
  62. LOAD_DATA(w0, off) /* W */ \
  63. and rT0,b,c; /* 1: f = b and c */ \
  64. INC_PTR /* ptr++ */ \
  65. andc rT1,d,b; /* 1: f' = ~b and d */ \
  66. LOAD_DATA(w1, off+4) /* W */ \
  67. or rT0,rT0,rT1; /* 1: f = f or f' */ \
  68. addi w0,w0,k0l; /* 1: wk = w + k */ \
  69. add a,a,rT0; /* 1: a = a + f */ \
  70. addis w0,w0,k0h; /* 1: wk = w + k' */ \
  71. addis w1,w1,k1h; /* 2: wk = w + k */ \
  72. add a,a,w0; /* 1: a = a + wk */ \
  73. addi w1,w1,k1l; /* 2: wk = w + k' */ \
  74. rotrwi a,a,p; /* 1: a = a rotl x */ \
  75. add d,d,w1; /* 2: a = a + wk */ \
  76. add a,a,b; /* 1: a = a + b */ \
  77. and rT0,a,b; /* 2: f = b and c */ \
  78. andc rT1,c,a; /* 2: f' = ~b and d */ \
  79. or rT0,rT0,rT1; /* 2: f = f or f' */ \
  80. add d,d,rT0; /* 2: a = a + f */ \
  81. INC_PTR /* ptr++ */ \
  82. rotrwi d,d,q; /* 2: a = a rotl x */ \
  83. add d,d,a; /* 2: a = a + b */
  84. #define R_16_31(a, b, c, d, w0, w1, p, q, k0h, k0l, k1h, k1l) \
  85. andc rT0,c,d; /* 1: f = c and ~d */ \
  86. and rT1,b,d; /* 1: f' = b and d */ \
  87. addi w0,w0,k0l; /* 1: wk = w + k */ \
  88. or rT0,rT0,rT1; /* 1: f = f or f' */ \
  89. addis w0,w0,k0h; /* 1: wk = w + k' */ \
  90. add a,a,rT0; /* 1: a = a + f */ \
  91. addi w1,w1,k1l; /* 2: wk = w + k */ \
  92. add a,a,w0; /* 1: a = a + wk */ \
  93. addis w1,w1,k1h; /* 2: wk = w + k' */ \
  94. andc rT0,b,c; /* 2: f = c and ~d */ \
  95. rotrwi a,a,p; /* 1: a = a rotl x */ \
  96. add a,a,b; /* 1: a = a + b */ \
  97. add d,d,w1; /* 2: a = a + wk */ \
  98. and rT1,a,c; /* 2: f' = b and d */ \
  99. or rT0,rT0,rT1; /* 2: f = f or f' */ \
  100. add d,d,rT0; /* 2: a = a + f */ \
  101. rotrwi d,d,q; /* 2: a = a rotl x */ \
  102. add d,d,a; /* 2: a = a +b */
  103. #define R_32_47(a, b, c, d, w0, w1, p, q, k0h, k0l, k1h, k1l) \
  104. xor rT0,b,c; /* 1: f' = b xor c */ \
  105. addi w0,w0,k0l; /* 1: wk = w + k */ \
  106. xor rT1,rT0,d; /* 1: f = f xor f' */ \
  107. addis w0,w0,k0h; /* 1: wk = w + k' */ \
  108. add a,a,rT1; /* 1: a = a + f */ \
  109. addi w1,w1,k1l; /* 2: wk = w + k */ \
  110. add a,a,w0; /* 1: a = a + wk */ \
  111. addis w1,w1,k1h; /* 2: wk = w + k' */ \
  112. rotrwi a,a,p; /* 1: a = a rotl x */ \
  113. add d,d,w1; /* 2: a = a + wk */ \
  114. add a,a,b; /* 1: a = a + b */ \
  115. xor rT1,rT0,a; /* 2: f = b xor f' */ \
  116. add d,d,rT1; /* 2: a = a + f */ \
  117. rotrwi d,d,q; /* 2: a = a rotl x */ \
  118. add d,d,a; /* 2: a = a + b */
  119. #define R_48_63(a, b, c, d, w0, w1, p, q, k0h, k0l, k1h, k1l) \
  120. addi w0,w0,k0l; /* 1: w = w + k */ \
  121. orc rT0,b,d; /* 1: f = b or ~d */ \
  122. addis w0,w0,k0h; /* 1: w = w + k' */ \
  123. xor rT0,rT0,c; /* 1: f = f xor c */ \
  124. add a,a,w0; /* 1: a = a + wk */ \
  125. addi w1,w1,k1l; /* 2: w = w + k */ \
  126. add a,a,rT0; /* 1: a = a + f */ \
  127. addis w1,w1,k1h; /* 2: w = w + k' */ \
  128. rotrwi a,a,p; /* 1: a = a rotl x */ \
  129. add a,a,b; /* 1: a = a + b */ \
  130. orc rT0,a,c; /* 2: f = b or ~d */ \
  131. add d,d,w1; /* 2: a = a + wk */ \
  132. xor rT0,rT0,b; /* 2: f = f xor c */ \
  133. add d,d,rT0; /* 2: a = a + f */ \
  134. rotrwi d,d,q; /* 2: a = a rotl x */ \
  135. add d,d,a; /* 2: a = a + b */
  136. _GLOBAL(ppc_md5_transform)
  137. INITIALIZE
  138. mtctr r5
  139. lwz rH0,0(rHP)
  140. lwz rH1,4(rHP)
  141. lwz rH2,8(rHP)
  142. lwz rH3,12(rHP)
  143. ppc_md5_main:
  144. R_00_15(rH0, rH1, rH2, rH3, rW00, rW01, 25, 20, 0,
  145. 0xd76b, -23432, 0xe8c8, -18602)
  146. R_00_15(rH2, rH3, rH0, rH1, rW02, rW03, 15, 10, 8,
  147. 0x2420, 0x70db, 0xc1be, -12562)
  148. R_00_15(rH0, rH1, rH2, rH3, rW04, rW05, 25, 20, 16,
  149. 0xf57c, 0x0faf, 0x4788, -14806)
  150. R_00_15(rH2, rH3, rH0, rH1, rW06, rW07, 15, 10, 24,
  151. 0xa830, 0x4613, 0xfd47, -27391)
  152. R_00_15(rH0, rH1, rH2, rH3, rW08, rW09, 25, 20, 32,
  153. 0x6981, -26408, 0x8b45, -2129)
  154. R_00_15(rH2, rH3, rH0, rH1, rW10, rW11, 15, 10, 40,
  155. 0xffff, 0x5bb1, 0x895d, -10306)
  156. R_00_15(rH0, rH1, rH2, rH3, rW12, rW13, 25, 20, 48,
  157. 0x6b90, 0x1122, 0xfd98, 0x7193)
  158. R_00_15(rH2, rH3, rH0, rH1, rW14, rW15, 15, 10, 56,
  159. 0xa679, 0x438e, 0x49b4, 0x0821)
  160. R_16_31(rH0, rH1, rH2, rH3, rW01, rW06, 27, 23,
  161. 0x0d56, 0x6e0c, 0x1810, 0x6d2d)
  162. R_16_31(rH2, rH3, rH0, rH1, rW11, rW00, 18, 12,
  163. 0x9d02, -32109, 0x124c, 0x2332)
  164. R_16_31(rH0, rH1, rH2, rH3, rW05, rW10, 27, 23,
  165. 0x8ea7, 0x4a33, 0x0245, -18270)
  166. R_16_31(rH2, rH3, rH0, rH1, rW15, rW04, 18, 12,
  167. 0x8eee, -8608, 0xf258, -5095)
  168. R_16_31(rH0, rH1, rH2, rH3, rW09, rW14, 27, 23,
  169. 0x969d, -10697, 0x1cbe, -15288)
  170. R_16_31(rH2, rH3, rH0, rH1, rW03, rW08, 18, 12,
  171. 0x3317, 0x3e99, 0xdbd9, 0x7c15)
  172. R_16_31(rH0, rH1, rH2, rH3, rW13, rW02, 27, 23,
  173. 0xac4b, 0x7772, 0xd8cf, 0x331d)
  174. R_16_31(rH2, rH3, rH0, rH1, rW07, rW12, 18, 12,
  175. 0x6a28, 0x6dd8, 0x219a, 0x3b68)
  176. R_32_47(rH0, rH1, rH2, rH3, rW05, rW08, 28, 21,
  177. 0x29cb, 0x28e5, 0x4218, -7788)
  178. R_32_47(rH2, rH3, rH0, rH1, rW11, rW14, 16, 9,
  179. 0x473f, 0x06d1, 0x3aae, 0x3036)
  180. R_32_47(rH0, rH1, rH2, rH3, rW01, rW04, 28, 21,
  181. 0xaea1, -15134, 0x640b, -11295)
  182. R_32_47(rH2, rH3, rH0, rH1, rW07, rW10, 16, 9,
  183. 0x8f4c, 0x4887, 0xbc7c, -22499)
  184. R_32_47(rH0, rH1, rH2, rH3, rW13, rW00, 28, 21,
  185. 0x7eb8, -27199, 0x00ea, 0x6050)
  186. R_32_47(rH2, rH3, rH0, rH1, rW03, rW06, 16, 9,
  187. 0xe01a, 0x22fe, 0x4447, 0x69c5)
  188. R_32_47(rH0, rH1, rH2, rH3, rW09, rW12, 28, 21,
  189. 0xb7f3, 0x0253, 0x59b1, 0x4d5b)
  190. R_32_47(rH2, rH3, rH0, rH1, rW15, rW02, 16, 9,
  191. 0x4701, -27017, 0xc7bd, -19859)
  192. R_48_63(rH0, rH1, rH2, rH3, rW00, rW07, 26, 22,
  193. 0x0988, -1462, 0x4c70, -19401)
  194. R_48_63(rH2, rH3, rH0, rH1, rW14, rW05, 17, 11,
  195. 0xadaf, -5221, 0xfc99, 0x66f7)
  196. R_48_63(rH0, rH1, rH2, rH3, rW12, rW03, 26, 22,
  197. 0x7e80, -16418, 0xba1e, -25587)
  198. R_48_63(rH2, rH3, rH0, rH1, rW10, rW01, 17, 11,
  199. 0x4130, 0x380d, 0xe0c5, 0x738d)
  200. lwz rW00,0(rHP)
  201. R_48_63(rH0, rH1, rH2, rH3, rW08, rW15, 26, 22,
  202. 0xe837, -30770, 0xde8a, 0x69e8)
  203. lwz rW14,4(rHP)
  204. R_48_63(rH2, rH3, rH0, rH1, rW06, rW13, 17, 11,
  205. 0x9e79, 0x260f, 0x256d, -27941)
  206. lwz rW12,8(rHP)
  207. R_48_63(rH0, rH1, rH2, rH3, rW04, rW11, 26, 22,
  208. 0xab75, -20775, 0x4f9e, -28397)
  209. lwz rW10,12(rHP)
  210. R_48_63(rH2, rH3, rH0, rH1, rW02, rW09, 17, 11,
  211. 0x662b, 0x7c56, 0x11b2, 0x0358)
  212. add rH0,rH0,rW00
  213. stw rH0,0(rHP)
  214. add rH1,rH1,rW14
  215. stw rH1,4(rHP)
  216. add rH2,rH2,rW12
  217. stw rH2,8(rHP)
  218. add rH3,rH3,rW10
  219. stw rH3,12(rHP)
  220. NEXT_BLOCK
  221. bdnz ppc_md5_main
  222. FINALIZE
  223. blr