aes-x86_64-asm.S 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* AES (Rijndael) implementation (FIPS PUB 197) for x86_64
  2. *
  3. * Copyright (C) 2005 Andreas Steinmetz, <ast@domdv.de>
  4. *
  5. * License:
  6. * This code can be distributed under the terms of the GNU General Public
  7. * License (GPL) Version 2 provided that the above header down to and
  8. * including this sentence is retained in full.
  9. */
  10. .extern aes_ft_tab
  11. .extern aes_it_tab
  12. .extern aes_fl_tab
  13. .extern aes_il_tab
  14. .text
  15. #define R1 %rax
  16. #define R1E %eax
  17. #define R1X %ax
  18. #define R1H %ah
  19. #define R1L %al
  20. #define R2 %rbx
  21. #define R2E %ebx
  22. #define R2X %bx
  23. #define R2H %bh
  24. #define R2L %bl
  25. #define R3 %rcx
  26. #define R3E %ecx
  27. #define R3X %cx
  28. #define R3H %ch
  29. #define R3L %cl
  30. #define R4 %rdx
  31. #define R4E %edx
  32. #define R4X %dx
  33. #define R4H %dh
  34. #define R4L %dl
  35. #define R5 %rsi
  36. #define R5E %esi
  37. #define R6 %rdi
  38. #define R6E %edi
  39. #define R7 %rbp
  40. #define R7E %ebp
  41. #define R8 %r8
  42. #define R9 %r9
  43. #define R10 %r10
  44. #define R11 %r11
  45. #define prologue(FUNC,BASE,B128,B192,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11) \
  46. .global FUNC; \
  47. .type FUNC,@function; \
  48. .align 8; \
  49. FUNC: movq r1,r2; \
  50. movq r3,r4; \
  51. leaq BASE+52(r8),r9; \
  52. movq r10,r11; \
  53. movl (r7),r5 ## E; \
  54. movl 4(r7),r1 ## E; \
  55. movl 8(r7),r6 ## E; \
  56. movl 12(r7),r7 ## E; \
  57. movl (r8),r10 ## E; \
  58. xorl -48(r9),r5 ## E; \
  59. xorl -44(r9),r1 ## E; \
  60. xorl -40(r9),r6 ## E; \
  61. xorl -36(r9),r7 ## E; \
  62. cmpl $24,r10 ## E; \
  63. jb B128; \
  64. leaq 32(r9),r9; \
  65. je B192; \
  66. leaq 32(r9),r9;
  67. #define epilogue(r1,r2,r3,r4,r5,r6,r7,r8,r9) \
  68. movq r1,r2; \
  69. movq r3,r4; \
  70. movl r5 ## E,(r9); \
  71. movl r6 ## E,4(r9); \
  72. movl r7 ## E,8(r9); \
  73. movl r8 ## E,12(r9); \
  74. ret;
  75. #define round(TAB,OFFSET,r1,r2,r3,r4,r5,r6,r7,r8,ra,rb,rc,rd) \
  76. movzbl r2 ## H,r5 ## E; \
  77. movzbl r2 ## L,r6 ## E; \
  78. movl TAB+1024(,r5,4),r5 ## E;\
  79. movw r4 ## X,r2 ## X; \
  80. movl TAB(,r6,4),r6 ## E; \
  81. roll $16,r2 ## E; \
  82. shrl $16,r4 ## E; \
  83. movzbl r4 ## H,r7 ## E; \
  84. movzbl r4 ## L,r4 ## E; \
  85. xorl OFFSET(r8),ra ## E; \
  86. xorl OFFSET+4(r8),rb ## E; \
  87. xorl TAB+3072(,r7,4),r5 ## E;\
  88. xorl TAB+2048(,r4,4),r6 ## E;\
  89. movzbl r1 ## L,r7 ## E; \
  90. movzbl r1 ## H,r4 ## E; \
  91. movl TAB+1024(,r4,4),r4 ## E;\
  92. movw r3 ## X,r1 ## X; \
  93. roll $16,r1 ## E; \
  94. shrl $16,r3 ## E; \
  95. xorl TAB(,r7,4),r5 ## E; \
  96. movzbl r3 ## H,r7 ## E; \
  97. movzbl r3 ## L,r3 ## E; \
  98. xorl TAB+3072(,r7,4),r4 ## E;\
  99. xorl TAB+2048(,r3,4),r5 ## E;\
  100. movzbl r1 ## H,r7 ## E; \
  101. movzbl r1 ## L,r3 ## E; \
  102. shrl $16,r1 ## E; \
  103. xorl TAB+3072(,r7,4),r6 ## E;\
  104. movl TAB+2048(,r3,4),r3 ## E;\
  105. movzbl r1 ## H,r7 ## E; \
  106. movzbl r1 ## L,r1 ## E; \
  107. xorl TAB+1024(,r7,4),r6 ## E;\
  108. xorl TAB(,r1,4),r3 ## E; \
  109. movzbl r2 ## H,r1 ## E; \
  110. movzbl r2 ## L,r7 ## E; \
  111. shrl $16,r2 ## E; \
  112. xorl TAB+3072(,r1,4),r3 ## E;\
  113. xorl TAB+2048(,r7,4),r4 ## E;\
  114. movzbl r2 ## H,r1 ## E; \
  115. movzbl r2 ## L,r2 ## E; \
  116. xorl OFFSET+8(r8),rc ## E; \
  117. xorl OFFSET+12(r8),rd ## E; \
  118. xorl TAB+1024(,r1,4),r3 ## E;\
  119. xorl TAB(,r2,4),r4 ## E;
  120. #define move_regs(r1,r2,r3,r4) \
  121. movl r3 ## E,r1 ## E; \
  122. movl r4 ## E,r2 ## E;
  123. #define entry(FUNC,BASE,B128,B192) \
  124. prologue(FUNC,BASE,B128,B192,R2,R8,R7,R9,R1,R3,R4,R6,R10,R5,R11)
  125. #define return epilogue(R8,R2,R9,R7,R5,R6,R3,R4,R11)
  126. #define encrypt_round(TAB,OFFSET) \
  127. round(TAB,OFFSET,R1,R2,R3,R4,R5,R6,R7,R10,R5,R6,R3,R4) \
  128. move_regs(R1,R2,R5,R6)
  129. #define encrypt_final(TAB,OFFSET) \
  130. round(TAB,OFFSET,R1,R2,R3,R4,R5,R6,R7,R10,R5,R6,R3,R4)
  131. #define decrypt_round(TAB,OFFSET) \
  132. round(TAB,OFFSET,R2,R1,R4,R3,R6,R5,R7,R10,R5,R6,R3,R4) \
  133. move_regs(R1,R2,R5,R6)
  134. #define decrypt_final(TAB,OFFSET) \
  135. round(TAB,OFFSET,R2,R1,R4,R3,R6,R5,R7,R10,R5,R6,R3,R4)
  136. /* void aes_encrypt(void *ctx, u8 *out, const u8 *in) */
  137. entry(aes_encrypt,0,enc128,enc192)
  138. encrypt_round(aes_ft_tab,-96)
  139. encrypt_round(aes_ft_tab,-80)
  140. enc192: encrypt_round(aes_ft_tab,-64)
  141. encrypt_round(aes_ft_tab,-48)
  142. enc128: encrypt_round(aes_ft_tab,-32)
  143. encrypt_round(aes_ft_tab,-16)
  144. encrypt_round(aes_ft_tab, 0)
  145. encrypt_round(aes_ft_tab, 16)
  146. encrypt_round(aes_ft_tab, 32)
  147. encrypt_round(aes_ft_tab, 48)
  148. encrypt_round(aes_ft_tab, 64)
  149. encrypt_round(aes_ft_tab, 80)
  150. encrypt_round(aes_ft_tab, 96)
  151. encrypt_final(aes_fl_tab,112)
  152. return
  153. /* void aes_decrypt(void *ctx, u8 *out, const u8 *in) */
  154. entry(aes_decrypt,240,dec128,dec192)
  155. decrypt_round(aes_it_tab,-96)
  156. decrypt_round(aes_it_tab,-80)
  157. dec192: decrypt_round(aes_it_tab,-64)
  158. decrypt_round(aes_it_tab,-48)
  159. dec128: decrypt_round(aes_it_tab,-32)
  160. decrypt_round(aes_it_tab,-16)
  161. decrypt_round(aes_it_tab, 0)
  162. decrypt_round(aes_it_tab, 16)
  163. decrypt_round(aes_it_tab, 32)
  164. decrypt_round(aes_it_tab, 48)
  165. decrypt_round(aes_it_tab, 64)
  166. decrypt_round(aes_it_tab, 80)
  167. decrypt_round(aes_it_tab, 96)
  168. decrypt_final(aes_il_tab,112)
  169. return