sha1-powerpc-asm.S 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * SHA-1 implementation for PowerPC.
  3. *
  4. * Copyright (C) 2005 Paul Mackerras <paulus@samba.org>
  5. */
  6. #include <asm/ppc_asm.h>
  7. #include <asm/asm-offsets.h>
  8. #ifdef __BIG_ENDIAN__
  9. #define LWZ(rt, d, ra) \
  10. lwz rt,d(ra)
  11. #else
  12. #define LWZ(rt, d, ra) \
  13. li rt,d; \
  14. lwbrx rt,rt,ra
  15. #endif
  16. /*
  17. * We roll the registers for T, A, B, C, D, E around on each
  18. * iteration; T on iteration t is A on iteration t+1, and so on.
  19. * We use registers 7 - 12 for this.
  20. */
  21. #define RT(t) ((((t)+5)%6)+7)
  22. #define RA(t) ((((t)+4)%6)+7)
  23. #define RB(t) ((((t)+3)%6)+7)
  24. #define RC(t) ((((t)+2)%6)+7)
  25. #define RD(t) ((((t)+1)%6)+7)
  26. #define RE(t) ((((t)+0)%6)+7)
  27. /* We use registers 16 - 31 for the W values */
  28. #define W(t) (((t)%16)+16)
  29. #define LOADW(t) \
  30. LWZ(W(t),(t)*4,r4)
  31. #define STEPD0_LOAD(t) \
  32. andc r0,RD(t),RB(t); \
  33. and r6,RB(t),RC(t); \
  34. rotlwi RT(t),RA(t),5; \
  35. or r6,r6,r0; \
  36. add r0,RE(t),r15; \
  37. add RT(t),RT(t),r6; \
  38. add r14,r0,W(t); \
  39. LWZ(W((t)+4),((t)+4)*4,r4); \
  40. rotlwi RB(t),RB(t),30; \
  41. add RT(t),RT(t),r14
  42. #define STEPD0_UPDATE(t) \
  43. and r6,RB(t),RC(t); \
  44. andc r0,RD(t),RB(t); \
  45. rotlwi RT(t),RA(t),5; \
  46. rotlwi RB(t),RB(t),30; \
  47. or r6,r6,r0; \
  48. add r0,RE(t),r15; \
  49. xor r5,W((t)+4-3),W((t)+4-8); \
  50. add RT(t),RT(t),r6; \
  51. xor W((t)+4),W((t)+4-16),W((t)+4-14); \
  52. add r0,r0,W(t); \
  53. xor W((t)+4),W((t)+4),r5; \
  54. add RT(t),RT(t),r0; \
  55. rotlwi W((t)+4),W((t)+4),1
  56. #define STEPD1(t) \
  57. xor r6,RB(t),RC(t); \
  58. rotlwi RT(t),RA(t),5; \
  59. rotlwi RB(t),RB(t),30; \
  60. xor r6,r6,RD(t); \
  61. add r0,RE(t),r15; \
  62. add RT(t),RT(t),r6; \
  63. add r0,r0,W(t); \
  64. add RT(t),RT(t),r0
  65. #define STEPD1_UPDATE(t) \
  66. xor r6,RB(t),RC(t); \
  67. rotlwi RT(t),RA(t),5; \
  68. rotlwi RB(t),RB(t),30; \
  69. xor r6,r6,RD(t); \
  70. add r0,RE(t),r15; \
  71. xor r5,W((t)+4-3),W((t)+4-8); \
  72. add RT(t),RT(t),r6; \
  73. xor W((t)+4),W((t)+4-16),W((t)+4-14); \
  74. add r0,r0,W(t); \
  75. xor W((t)+4),W((t)+4),r5; \
  76. add RT(t),RT(t),r0; \
  77. rotlwi W((t)+4),W((t)+4),1
  78. #define STEPD2_UPDATE(t) \
  79. and r6,RB(t),RC(t); \
  80. and r0,RB(t),RD(t); \
  81. rotlwi RT(t),RA(t),5; \
  82. or r6,r6,r0; \
  83. rotlwi RB(t),RB(t),30; \
  84. and r0,RC(t),RD(t); \
  85. xor r5,W((t)+4-3),W((t)+4-8); \
  86. or r6,r6,r0; \
  87. xor W((t)+4),W((t)+4-16),W((t)+4-14); \
  88. add r0,RE(t),r15; \
  89. add RT(t),RT(t),r6; \
  90. add r0,r0,W(t); \
  91. xor W((t)+4),W((t)+4),r5; \
  92. add RT(t),RT(t),r0; \
  93. rotlwi W((t)+4),W((t)+4),1
  94. #define STEP0LD4(t) \
  95. STEPD0_LOAD(t); \
  96. STEPD0_LOAD((t)+1); \
  97. STEPD0_LOAD((t)+2); \
  98. STEPD0_LOAD((t)+3)
  99. #define STEPUP4(t, fn) \
  100. STEP##fn##_UPDATE(t); \
  101. STEP##fn##_UPDATE((t)+1); \
  102. STEP##fn##_UPDATE((t)+2); \
  103. STEP##fn##_UPDATE((t)+3)
  104. #define STEPUP20(t, fn) \
  105. STEPUP4(t, fn); \
  106. STEPUP4((t)+4, fn); \
  107. STEPUP4((t)+8, fn); \
  108. STEPUP4((t)+12, fn); \
  109. STEPUP4((t)+16, fn)
  110. _GLOBAL(powerpc_sha_transform)
  111. PPC_STLU r1,-INT_FRAME_SIZE(r1)
  112. SAVE_8GPRS(14, r1)
  113. SAVE_10GPRS(22, r1)
  114. /* Load up A - E */
  115. lwz RA(0),0(r3) /* A */
  116. lwz RB(0),4(r3) /* B */
  117. lwz RC(0),8(r3) /* C */
  118. lwz RD(0),12(r3) /* D */
  119. lwz RE(0),16(r3) /* E */
  120. LOADW(0)
  121. LOADW(1)
  122. LOADW(2)
  123. LOADW(3)
  124. lis r15,0x5a82 /* K0-19 */
  125. ori r15,r15,0x7999
  126. STEP0LD4(0)
  127. STEP0LD4(4)
  128. STEP0LD4(8)
  129. STEPUP4(12, D0)
  130. STEPUP4(16, D0)
  131. lis r15,0x6ed9 /* K20-39 */
  132. ori r15,r15,0xeba1
  133. STEPUP20(20, D1)
  134. lis r15,0x8f1b /* K40-59 */
  135. ori r15,r15,0xbcdc
  136. STEPUP20(40, D2)
  137. lis r15,0xca62 /* K60-79 */
  138. ori r15,r15,0xc1d6
  139. STEPUP4(60, D1)
  140. STEPUP4(64, D1)
  141. STEPUP4(68, D1)
  142. STEPUP4(72, D1)
  143. lwz r20,16(r3)
  144. STEPD1(76)
  145. lwz r19,12(r3)
  146. STEPD1(77)
  147. lwz r18,8(r3)
  148. STEPD1(78)
  149. lwz r17,4(r3)
  150. STEPD1(79)
  151. lwz r16,0(r3)
  152. add r20,RE(80),r20
  153. add RD(0),RD(80),r19
  154. add RC(0),RC(80),r18
  155. add RB(0),RB(80),r17
  156. add RA(0),RA(80),r16
  157. mr RE(0),r20
  158. stw RA(0),0(r3)
  159. stw RB(0),4(r3)
  160. stw RC(0),8(r3)
  161. stw RD(0),12(r3)
  162. stw RE(0),16(r3)
  163. REST_8GPRS(14, r1)
  164. REST_10GPRS(22, r1)
  165. addi r1,r1,INT_FRAME_SIZE
  166. blr