twofish-i586-asm_32.S 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /***************************************************************************
  2. * Copyright (C) 2006 by Joachim Fritschi, <jfritschi@freenet.de> *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. .file "twofish-i586-asm.S"
  20. .text
  21. #include <linux/linkage.h>
  22. #include <asm/asm-offsets.h>
  23. /* return address at 0 */
  24. #define in_blk 12 /* input byte array address parameter*/
  25. #define out_blk 8 /* output byte array address parameter*/
  26. #define ctx 4 /* Twofish context structure */
  27. #define a_offset 0
  28. #define b_offset 4
  29. #define c_offset 8
  30. #define d_offset 12
  31. /* Structure of the crypto context struct*/
  32. #define s0 0 /* S0 Array 256 Words each */
  33. #define s1 1024 /* S1 Array */
  34. #define s2 2048 /* S2 Array */
  35. #define s3 3072 /* S3 Array */
  36. #define w 4096 /* 8 whitening keys (word) */
  37. #define k 4128 /* key 1-32 ( word ) */
  38. /* define a few register aliases to allow macro substitution */
  39. #define R0D %eax
  40. #define R0B %al
  41. #define R0H %ah
  42. #define R1D %ebx
  43. #define R1B %bl
  44. #define R1H %bh
  45. #define R2D %ecx
  46. #define R2B %cl
  47. #define R2H %ch
  48. #define R3D %edx
  49. #define R3B %dl
  50. #define R3H %dh
  51. /* performs input whitening */
  52. #define input_whitening(src,context,offset)\
  53. xor w+offset(context), src;
  54. /* performs input whitening */
  55. #define output_whitening(src,context,offset)\
  56. xor w+16+offset(context), src;
  57. /*
  58. * a input register containing a (rotated 16)
  59. * b input register containing b
  60. * c input register containing c
  61. * d input register containing d (already rol $1)
  62. * operations on a and b are interleaved to increase performance
  63. */
  64. #define encrypt_round(a,b,c,d,round)\
  65. push d ## D;\
  66. movzx b ## B, %edi;\
  67. mov s1(%ebp,%edi,4),d ## D;\
  68. movzx a ## B, %edi;\
  69. mov s2(%ebp,%edi,4),%esi;\
  70. movzx b ## H, %edi;\
  71. ror $16, b ## D;\
  72. xor s2(%ebp,%edi,4),d ## D;\
  73. movzx a ## H, %edi;\
  74. ror $16, a ## D;\
  75. xor s3(%ebp,%edi,4),%esi;\
  76. movzx b ## B, %edi;\
  77. xor s3(%ebp,%edi,4),d ## D;\
  78. movzx a ## B, %edi;\
  79. xor (%ebp,%edi,4), %esi;\
  80. movzx b ## H, %edi;\
  81. ror $15, b ## D;\
  82. xor (%ebp,%edi,4), d ## D;\
  83. movzx a ## H, %edi;\
  84. xor s1(%ebp,%edi,4),%esi;\
  85. pop %edi;\
  86. add d ## D, %esi;\
  87. add %esi, d ## D;\
  88. add k+round(%ebp), %esi;\
  89. xor %esi, c ## D;\
  90. rol $15, c ## D;\
  91. add k+4+round(%ebp),d ## D;\
  92. xor %edi, d ## D;
  93. /*
  94. * a input register containing a (rotated 16)
  95. * b input register containing b
  96. * c input register containing c
  97. * d input register containing d (already rol $1)
  98. * operations on a and b are interleaved to increase performance
  99. * last round has different rotations for the output preparation
  100. */
  101. #define encrypt_last_round(a,b,c,d,round)\
  102. push d ## D;\
  103. movzx b ## B, %edi;\
  104. mov s1(%ebp,%edi,4),d ## D;\
  105. movzx a ## B, %edi;\
  106. mov s2(%ebp,%edi,4),%esi;\
  107. movzx b ## H, %edi;\
  108. ror $16, b ## D;\
  109. xor s2(%ebp,%edi,4),d ## D;\
  110. movzx a ## H, %edi;\
  111. ror $16, a ## D;\
  112. xor s3(%ebp,%edi,4),%esi;\
  113. movzx b ## B, %edi;\
  114. xor s3(%ebp,%edi,4),d ## D;\
  115. movzx a ## B, %edi;\
  116. xor (%ebp,%edi,4), %esi;\
  117. movzx b ## H, %edi;\
  118. ror $16, b ## D;\
  119. xor (%ebp,%edi,4), d ## D;\
  120. movzx a ## H, %edi;\
  121. xor s1(%ebp,%edi,4),%esi;\
  122. pop %edi;\
  123. add d ## D, %esi;\
  124. add %esi, d ## D;\
  125. add k+round(%ebp), %esi;\
  126. xor %esi, c ## D;\
  127. ror $1, c ## D;\
  128. add k+4+round(%ebp),d ## D;\
  129. xor %edi, d ## D;
  130. /*
  131. * a input register containing a
  132. * b input register containing b (rotated 16)
  133. * c input register containing c
  134. * d input register containing d (already rol $1)
  135. * operations on a and b are interleaved to increase performance
  136. */
  137. #define decrypt_round(a,b,c,d,round)\
  138. push c ## D;\
  139. movzx a ## B, %edi;\
  140. mov (%ebp,%edi,4), c ## D;\
  141. movzx b ## B, %edi;\
  142. mov s3(%ebp,%edi,4),%esi;\
  143. movzx a ## H, %edi;\
  144. ror $16, a ## D;\
  145. xor s1(%ebp,%edi,4),c ## D;\
  146. movzx b ## H, %edi;\
  147. ror $16, b ## D;\
  148. xor (%ebp,%edi,4), %esi;\
  149. movzx a ## B, %edi;\
  150. xor s2(%ebp,%edi,4),c ## D;\
  151. movzx b ## B, %edi;\
  152. xor s1(%ebp,%edi,4),%esi;\
  153. movzx a ## H, %edi;\
  154. ror $15, a ## D;\
  155. xor s3(%ebp,%edi,4),c ## D;\
  156. movzx b ## H, %edi;\
  157. xor s2(%ebp,%edi,4),%esi;\
  158. pop %edi;\
  159. add %esi, c ## D;\
  160. add c ## D, %esi;\
  161. add k+round(%ebp), c ## D;\
  162. xor %edi, c ## D;\
  163. add k+4+round(%ebp),%esi;\
  164. xor %esi, d ## D;\
  165. rol $15, d ## D;
  166. /*
  167. * a input register containing a
  168. * b input register containing b (rotated 16)
  169. * c input register containing c
  170. * d input register containing d (already rol $1)
  171. * operations on a and b are interleaved to increase performance
  172. * last round has different rotations for the output preparation
  173. */
  174. #define decrypt_last_round(a,b,c,d,round)\
  175. push c ## D;\
  176. movzx a ## B, %edi;\
  177. mov (%ebp,%edi,4), c ## D;\
  178. movzx b ## B, %edi;\
  179. mov s3(%ebp,%edi,4),%esi;\
  180. movzx a ## H, %edi;\
  181. ror $16, a ## D;\
  182. xor s1(%ebp,%edi,4),c ## D;\
  183. movzx b ## H, %edi;\
  184. ror $16, b ## D;\
  185. xor (%ebp,%edi,4), %esi;\
  186. movzx a ## B, %edi;\
  187. xor s2(%ebp,%edi,4),c ## D;\
  188. movzx b ## B, %edi;\
  189. xor s1(%ebp,%edi,4),%esi;\
  190. movzx a ## H, %edi;\
  191. ror $16, a ## D;\
  192. xor s3(%ebp,%edi,4),c ## D;\
  193. movzx b ## H, %edi;\
  194. xor s2(%ebp,%edi,4),%esi;\
  195. pop %edi;\
  196. add %esi, c ## D;\
  197. add c ## D, %esi;\
  198. add k+round(%ebp), c ## D;\
  199. xor %edi, c ## D;\
  200. add k+4+round(%ebp),%esi;\
  201. xor %esi, d ## D;\
  202. ror $1, d ## D;
  203. ENTRY(twofish_enc_blk)
  204. push %ebp /* save registers according to calling convention*/
  205. push %ebx
  206. push %esi
  207. push %edi
  208. mov ctx + 16(%esp), %ebp /* abuse the base pointer: set new base
  209. * pointer to the ctx address */
  210. mov in_blk+16(%esp),%edi /* input address in edi */
  211. mov (%edi), %eax
  212. mov b_offset(%edi), %ebx
  213. mov c_offset(%edi), %ecx
  214. mov d_offset(%edi), %edx
  215. input_whitening(%eax,%ebp,a_offset)
  216. ror $16, %eax
  217. input_whitening(%ebx,%ebp,b_offset)
  218. input_whitening(%ecx,%ebp,c_offset)
  219. input_whitening(%edx,%ebp,d_offset)
  220. rol $1, %edx
  221. encrypt_round(R0,R1,R2,R3,0);
  222. encrypt_round(R2,R3,R0,R1,8);
  223. encrypt_round(R0,R1,R2,R3,2*8);
  224. encrypt_round(R2,R3,R0,R1,3*8);
  225. encrypt_round(R0,R1,R2,R3,4*8);
  226. encrypt_round(R2,R3,R0,R1,5*8);
  227. encrypt_round(R0,R1,R2,R3,6*8);
  228. encrypt_round(R2,R3,R0,R1,7*8);
  229. encrypt_round(R0,R1,R2,R3,8*8);
  230. encrypt_round(R2,R3,R0,R1,9*8);
  231. encrypt_round(R0,R1,R2,R3,10*8);
  232. encrypt_round(R2,R3,R0,R1,11*8);
  233. encrypt_round(R0,R1,R2,R3,12*8);
  234. encrypt_round(R2,R3,R0,R1,13*8);
  235. encrypt_round(R0,R1,R2,R3,14*8);
  236. encrypt_last_round(R2,R3,R0,R1,15*8);
  237. output_whitening(%eax,%ebp,c_offset)
  238. output_whitening(%ebx,%ebp,d_offset)
  239. output_whitening(%ecx,%ebp,a_offset)
  240. output_whitening(%edx,%ebp,b_offset)
  241. mov out_blk+16(%esp),%edi;
  242. mov %eax, c_offset(%edi)
  243. mov %ebx, d_offset(%edi)
  244. mov %ecx, (%edi)
  245. mov %edx, b_offset(%edi)
  246. pop %edi
  247. pop %esi
  248. pop %ebx
  249. pop %ebp
  250. mov $1, %eax
  251. ret
  252. ENDPROC(twofish_enc_blk)
  253. ENTRY(twofish_dec_blk)
  254. push %ebp /* save registers according to calling convention*/
  255. push %ebx
  256. push %esi
  257. push %edi
  258. mov ctx + 16(%esp), %ebp /* abuse the base pointer: set new base
  259. * pointer to the ctx address */
  260. mov in_blk+16(%esp),%edi /* input address in edi */
  261. mov (%edi), %eax
  262. mov b_offset(%edi), %ebx
  263. mov c_offset(%edi), %ecx
  264. mov d_offset(%edi), %edx
  265. output_whitening(%eax,%ebp,a_offset)
  266. output_whitening(%ebx,%ebp,b_offset)
  267. ror $16, %ebx
  268. output_whitening(%ecx,%ebp,c_offset)
  269. output_whitening(%edx,%ebp,d_offset)
  270. rol $1, %ecx
  271. decrypt_round(R0,R1,R2,R3,15*8);
  272. decrypt_round(R2,R3,R0,R1,14*8);
  273. decrypt_round(R0,R1,R2,R3,13*8);
  274. decrypt_round(R2,R3,R0,R1,12*8);
  275. decrypt_round(R0,R1,R2,R3,11*8);
  276. decrypt_round(R2,R3,R0,R1,10*8);
  277. decrypt_round(R0,R1,R2,R3,9*8);
  278. decrypt_round(R2,R3,R0,R1,8*8);
  279. decrypt_round(R0,R1,R2,R3,7*8);
  280. decrypt_round(R2,R3,R0,R1,6*8);
  281. decrypt_round(R0,R1,R2,R3,5*8);
  282. decrypt_round(R2,R3,R0,R1,4*8);
  283. decrypt_round(R0,R1,R2,R3,3*8);
  284. decrypt_round(R2,R3,R0,R1,2*8);
  285. decrypt_round(R0,R1,R2,R3,1*8);
  286. decrypt_last_round(R2,R3,R0,R1,0);
  287. input_whitening(%eax,%ebp,c_offset)
  288. input_whitening(%ebx,%ebp,d_offset)
  289. input_whitening(%ecx,%ebp,a_offset)
  290. input_whitening(%edx,%ebp,b_offset)
  291. mov out_blk+16(%esp),%edi;
  292. mov %eax, c_offset(%edi)
  293. mov %ebx, d_offset(%edi)
  294. mov %ecx, (%edi)
  295. mov %edx, b_offset(%edi)
  296. pop %edi
  297. pop %esi
  298. pop %ebx
  299. pop %ebp
  300. mov $1, %eax
  301. ret
  302. ENDPROC(twofish_dec_blk)