vpaes-x86_64.pl 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. #!/usr/bin/env perl
  2. ######################################################################
  3. ## Constant-time SSSE3 AES core implementation.
  4. ## version 0.1
  5. ##
  6. ## By Mike Hamburg (Stanford University), 2009
  7. ## Public domain.
  8. ##
  9. ## For details see http://shiftleft.org/papers/vector_aes/ and
  10. ## http://crypto.stanford.edu/vpaes/.
  11. ######################################################################
  12. # September 2011.
  13. #
  14. # Interface to OpenSSL as "almost" drop-in replacement for
  15. # aes-x86_64.pl. "Almost" refers to the fact that AES_cbc_encrypt
  16. # doesn't handle partial vectors (doesn't have to if called from
  17. # EVP only). "Drop-in" implies that this module doesn't share key
  18. # schedule structure with the original nor does it make assumption
  19. # about its alignment...
  20. #
  21. # Performance summary. aes-x86_64.pl column lists large-block CBC
  22. # encrypt/decrypt/with-hyper-threading-off(*) results in cycles per
  23. # byte processed with 128-bit key, and vpaes-x86_64.pl column -
  24. # encrypt/decrypt.
  25. #
  26. # aes-x86_64.pl vpaes-x86_64.pl
  27. #
  28. # Core 2(**) 30.5/43.7/14.3 21.8/25.7(***)
  29. # Nehalem 30.5/42.2/14.6 9.8/11.8
  30. # Atom 63.9/79.0/32.1 64.0/84.8(***)
  31. #
  32. # (*) "Hyper-threading" in the context refers rather to cache shared
  33. # among multiple cores, than to specifically Intel HTT. As vast
  34. # majority of contemporary cores share cache, slower code path
  35. # is common place. In other words "with-hyper-threading-off"
  36. # results are presented mostly for reference purposes.
  37. #
  38. # (**) "Core 2" refers to initial 65nm design, a.k.a. Conroe.
  39. #
  40. # (***) Less impressive improvement on Core 2 and Atom is due to slow
  41. # pshufb, yet it's respectable +40%/78% improvement on Core 2
  42. # (as implied, over "hyper-threading-safe" code path).
  43. #
  44. # <appro@openssl.org>
  45. $flavour = shift;
  46. $output = shift;
  47. if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
  48. $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  49. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  50. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  51. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  52. die "can't locate x86_64-xlate.pl";
  53. open STDOUT,"| $^X $xlate $flavour $output";
  54. $PREFIX="vpaes";
  55. $code.=<<___;
  56. .text
  57. ##
  58. ## _aes_encrypt_core
  59. ##
  60. ## AES-encrypt %xmm0.
  61. ##
  62. ## Inputs:
  63. ## %xmm0 = input
  64. ## %xmm9-%xmm15 as in _vpaes_preheat
  65. ## (%rdx) = scheduled keys
  66. ##
  67. ## Output in %xmm0
  68. ## Clobbers %xmm1-%xmm5, %r9, %r10, %r11, %rax
  69. ## Preserves %xmm6 - %xmm8 so you get some local vectors
  70. ##
  71. ##
  72. .type _vpaes_encrypt_core,\@abi-omnipotent
  73. .align 16
  74. _vpaes_encrypt_core:
  75. mov %rdx, %r9
  76. mov \$16, %r11
  77. mov 240(%rdx),%eax
  78. movdqa %xmm9, %xmm1
  79. movdqa .Lk_ipt(%rip), %xmm2 # iptlo
  80. pandn %xmm0, %xmm1
  81. movdqu (%r9), %xmm5 # round0 key
  82. psrld \$4, %xmm1
  83. pand %xmm9, %xmm0
  84. pshufb %xmm0, %xmm2
  85. movdqa .Lk_ipt+16(%rip), %xmm0 # ipthi
  86. pshufb %xmm1, %xmm0
  87. pxor %xmm5, %xmm2
  88. pxor %xmm2, %xmm0
  89. add \$16, %r9
  90. lea .Lk_mc_backward(%rip),%r10
  91. jmp .Lenc_entry
  92. .align 16
  93. .Lenc_loop:
  94. # middle of middle round
  95. movdqa %xmm13, %xmm4 # 4 : sb1u
  96. pshufb %xmm2, %xmm4 # 4 = sb1u
  97. pxor %xmm5, %xmm4 # 4 = sb1u + k
  98. movdqa %xmm12, %xmm0 # 0 : sb1t
  99. pshufb %xmm3, %xmm0 # 0 = sb1t
  100. pxor %xmm4, %xmm0 # 0 = A
  101. movdqa %xmm15, %xmm5 # 4 : sb2u
  102. pshufb %xmm2, %xmm5 # 4 = sb2u
  103. movdqa -0x40(%r11,%r10), %xmm1 # .Lk_mc_forward[]
  104. movdqa %xmm14, %xmm2 # 2 : sb2t
  105. pshufb %xmm3, %xmm2 # 2 = sb2t
  106. pxor %xmm5, %xmm2 # 2 = 2A
  107. movdqa (%r11,%r10), %xmm4 # .Lk_mc_backward[]
  108. movdqa %xmm0, %xmm3 # 3 = A
  109. pshufb %xmm1, %xmm0 # 0 = B
  110. add \$16, %r9 # next key
  111. pxor %xmm2, %xmm0 # 0 = 2A+B
  112. pshufb %xmm4, %xmm3 # 3 = D
  113. add \$16, %r11 # next mc
  114. pxor %xmm0, %xmm3 # 3 = 2A+B+D
  115. pshufb %xmm1, %xmm0 # 0 = 2B+C
  116. and \$0x30, %r11 # ... mod 4
  117. pxor %xmm3, %xmm0 # 0 = 2A+3B+C+D
  118. sub \$1,%rax # nr--
  119. .Lenc_entry:
  120. # top of round
  121. movdqa %xmm9, %xmm1 # 1 : i
  122. pandn %xmm0, %xmm1 # 1 = i<<4
  123. psrld \$4, %xmm1 # 1 = i
  124. pand %xmm9, %xmm0 # 0 = k
  125. movdqa %xmm11, %xmm5 # 2 : a/k
  126. pshufb %xmm0, %xmm5 # 2 = a/k
  127. pxor %xmm1, %xmm0 # 0 = j
  128. movdqa %xmm10, %xmm3 # 3 : 1/i
  129. pshufb %xmm1, %xmm3 # 3 = 1/i
  130. pxor %xmm5, %xmm3 # 3 = iak = 1/i + a/k
  131. movdqa %xmm10, %xmm4 # 4 : 1/j
  132. pshufb %xmm0, %xmm4 # 4 = 1/j
  133. pxor %xmm5, %xmm4 # 4 = jak = 1/j + a/k
  134. movdqa %xmm10, %xmm2 # 2 : 1/iak
  135. pshufb %xmm3, %xmm2 # 2 = 1/iak
  136. pxor %xmm0, %xmm2 # 2 = io
  137. movdqa %xmm10, %xmm3 # 3 : 1/jak
  138. movdqu (%r9), %xmm5
  139. pshufb %xmm4, %xmm3 # 3 = 1/jak
  140. pxor %xmm1, %xmm3 # 3 = jo
  141. jnz .Lenc_loop
  142. # middle of last round
  143. movdqa -0x60(%r10), %xmm4 # 3 : sbou .Lk_sbo
  144. movdqa -0x50(%r10), %xmm0 # 0 : sbot .Lk_sbo+16
  145. pshufb %xmm2, %xmm4 # 4 = sbou
  146. pxor %xmm5, %xmm4 # 4 = sb1u + k
  147. pshufb %xmm3, %xmm0 # 0 = sb1t
  148. movdqa 0x40(%r11,%r10), %xmm1 # .Lk_sr[]
  149. pxor %xmm4, %xmm0 # 0 = A
  150. pshufb %xmm1, %xmm0
  151. ret
  152. .size _vpaes_encrypt_core,.-_vpaes_encrypt_core
  153. ##
  154. ## Decryption core
  155. ##
  156. ## Same API as encryption core.
  157. ##
  158. .type _vpaes_decrypt_core,\@abi-omnipotent
  159. .align 16
  160. _vpaes_decrypt_core:
  161. mov %rdx, %r9 # load key
  162. mov 240(%rdx),%eax
  163. movdqa %xmm9, %xmm1
  164. movdqa .Lk_dipt(%rip), %xmm2 # iptlo
  165. pandn %xmm0, %xmm1
  166. mov %rax, %r11
  167. psrld \$4, %xmm1
  168. movdqu (%r9), %xmm5 # round0 key
  169. shl \$4, %r11
  170. pand %xmm9, %xmm0
  171. pshufb %xmm0, %xmm2
  172. movdqa .Lk_dipt+16(%rip), %xmm0 # ipthi
  173. xor \$0x30, %r11
  174. lea .Lk_dsbd(%rip),%r10
  175. pshufb %xmm1, %xmm0
  176. and \$0x30, %r11
  177. pxor %xmm5, %xmm2
  178. movdqa .Lk_mc_forward+48(%rip), %xmm5
  179. pxor %xmm2, %xmm0
  180. add \$16, %r9
  181. add %r10, %r11
  182. jmp .Ldec_entry
  183. .align 16
  184. .Ldec_loop:
  185. ##
  186. ## Inverse mix columns
  187. ##
  188. movdqa -0x20(%r10),%xmm4 # 4 : sb9u
  189. pshufb %xmm2, %xmm4 # 4 = sb9u
  190. pxor %xmm0, %xmm4
  191. movdqa -0x10(%r10),%xmm0 # 0 : sb9t
  192. pshufb %xmm3, %xmm0 # 0 = sb9t
  193. pxor %xmm4, %xmm0 # 0 = ch
  194. add \$16, %r9 # next round key
  195. pshufb %xmm5, %xmm0 # MC ch
  196. movdqa 0x00(%r10),%xmm4 # 4 : sbdu
  197. pshufb %xmm2, %xmm4 # 4 = sbdu
  198. pxor %xmm0, %xmm4 # 4 = ch
  199. movdqa 0x10(%r10),%xmm0 # 0 : sbdt
  200. pshufb %xmm3, %xmm0 # 0 = sbdt
  201. pxor %xmm4, %xmm0 # 0 = ch
  202. sub \$1,%rax # nr--
  203. pshufb %xmm5, %xmm0 # MC ch
  204. movdqa 0x20(%r10),%xmm4 # 4 : sbbu
  205. pshufb %xmm2, %xmm4 # 4 = sbbu
  206. pxor %xmm0, %xmm4 # 4 = ch
  207. movdqa 0x30(%r10),%xmm0 # 0 : sbbt
  208. pshufb %xmm3, %xmm0 # 0 = sbbt
  209. pxor %xmm4, %xmm0 # 0 = ch
  210. pshufb %xmm5, %xmm0 # MC ch
  211. movdqa 0x40(%r10),%xmm4 # 4 : sbeu
  212. pshufb %xmm2, %xmm4 # 4 = sbeu
  213. pxor %xmm0, %xmm4 # 4 = ch
  214. movdqa 0x50(%r10),%xmm0 # 0 : sbet
  215. pshufb %xmm3, %xmm0 # 0 = sbet
  216. pxor %xmm4, %xmm0 # 0 = ch
  217. palignr \$12, %xmm5, %xmm5
  218. .Ldec_entry:
  219. # top of round
  220. movdqa %xmm9, %xmm1 # 1 : i
  221. pandn %xmm0, %xmm1 # 1 = i<<4
  222. psrld \$4, %xmm1 # 1 = i
  223. pand %xmm9, %xmm0 # 0 = k
  224. movdqa %xmm11, %xmm2 # 2 : a/k
  225. pshufb %xmm0, %xmm2 # 2 = a/k
  226. pxor %xmm1, %xmm0 # 0 = j
  227. movdqa %xmm10, %xmm3 # 3 : 1/i
  228. pshufb %xmm1, %xmm3 # 3 = 1/i
  229. pxor %xmm2, %xmm3 # 3 = iak = 1/i + a/k
  230. movdqa %xmm10, %xmm4 # 4 : 1/j
  231. pshufb %xmm0, %xmm4 # 4 = 1/j
  232. pxor %xmm2, %xmm4 # 4 = jak = 1/j + a/k
  233. movdqa %xmm10, %xmm2 # 2 : 1/iak
  234. pshufb %xmm3, %xmm2 # 2 = 1/iak
  235. pxor %xmm0, %xmm2 # 2 = io
  236. movdqa %xmm10, %xmm3 # 3 : 1/jak
  237. pshufb %xmm4, %xmm3 # 3 = 1/jak
  238. pxor %xmm1, %xmm3 # 3 = jo
  239. movdqu (%r9), %xmm0
  240. jnz .Ldec_loop
  241. # middle of last round
  242. movdqa 0x60(%r10), %xmm4 # 3 : sbou
  243. pshufb %xmm2, %xmm4 # 4 = sbou
  244. pxor %xmm0, %xmm4 # 4 = sb1u + k
  245. movdqa 0x70(%r10), %xmm0 # 0 : sbot
  246. movdqa .Lk_sr-.Lk_dsbd(%r11), %xmm2
  247. pshufb %xmm3, %xmm0 # 0 = sb1t
  248. pxor %xmm4, %xmm0 # 0 = A
  249. pshufb %xmm2, %xmm0
  250. ret
  251. .size _vpaes_decrypt_core,.-_vpaes_decrypt_core
  252. ########################################################
  253. ## ##
  254. ## AES key schedule ##
  255. ## ##
  256. ########################################################
  257. .type _vpaes_schedule_core,\@abi-omnipotent
  258. .align 16
  259. _vpaes_schedule_core:
  260. # rdi = key
  261. # rsi = size in bits
  262. # rdx = buffer
  263. # rcx = direction. 0=encrypt, 1=decrypt
  264. call _vpaes_preheat # load the tables
  265. movdqa .Lk_rcon(%rip), %xmm8 # load rcon
  266. movdqu (%rdi), %xmm0 # load key (unaligned)
  267. # input transform
  268. movdqa %xmm0, %xmm3
  269. lea .Lk_ipt(%rip), %r11
  270. call _vpaes_schedule_transform
  271. movdqa %xmm0, %xmm7
  272. lea .Lk_sr(%rip),%r10
  273. test %rcx, %rcx
  274. jnz .Lschedule_am_decrypting
  275. # encrypting, output zeroth round key after transform
  276. movdqu %xmm0, (%rdx)
  277. jmp .Lschedule_go
  278. .Lschedule_am_decrypting:
  279. # decrypting, output zeroth round key after shiftrows
  280. movdqa (%r8,%r10),%xmm1
  281. pshufb %xmm1, %xmm3
  282. movdqu %xmm3, (%rdx)
  283. xor \$0x30, %r8
  284. .Lschedule_go:
  285. cmp \$192, %esi
  286. ja .Lschedule_256
  287. je .Lschedule_192
  288. # 128: fall though
  289. ##
  290. ## .schedule_128
  291. ##
  292. ## 128-bit specific part of key schedule.
  293. ##
  294. ## This schedule is really simple, because all its parts
  295. ## are accomplished by the subroutines.
  296. ##
  297. .Lschedule_128:
  298. mov \$10, %esi
  299. .Loop_schedule_128:
  300. call _vpaes_schedule_round
  301. dec %rsi
  302. jz .Lschedule_mangle_last
  303. call _vpaes_schedule_mangle # write output
  304. jmp .Loop_schedule_128
  305. ##
  306. ## .aes_schedule_192
  307. ##
  308. ## 192-bit specific part of key schedule.
  309. ##
  310. ## The main body of this schedule is the same as the 128-bit
  311. ## schedule, but with more smearing. The long, high side is
  312. ## stored in %xmm7 as before, and the short, low side is in
  313. ## the high bits of %xmm6.
  314. ##
  315. ## This schedule is somewhat nastier, however, because each
  316. ## round produces 192 bits of key material, or 1.5 round keys.
  317. ## Therefore, on each cycle we do 2 rounds and produce 3 round
  318. ## keys.
  319. ##
  320. .align 16
  321. .Lschedule_192:
  322. movdqu 8(%rdi),%xmm0 # load key part 2 (very unaligned)
  323. call _vpaes_schedule_transform # input transform
  324. movdqa %xmm0, %xmm6 # save short part
  325. pxor %xmm4, %xmm4 # clear 4
  326. movhlps %xmm4, %xmm6 # clobber low side with zeros
  327. mov \$4, %esi
  328. .Loop_schedule_192:
  329. call _vpaes_schedule_round
  330. palignr \$8,%xmm6,%xmm0
  331. call _vpaes_schedule_mangle # save key n
  332. call _vpaes_schedule_192_smear
  333. call _vpaes_schedule_mangle # save key n+1
  334. call _vpaes_schedule_round
  335. dec %rsi
  336. jz .Lschedule_mangle_last
  337. call _vpaes_schedule_mangle # save key n+2
  338. call _vpaes_schedule_192_smear
  339. jmp .Loop_schedule_192
  340. ##
  341. ## .aes_schedule_256
  342. ##
  343. ## 256-bit specific part of key schedule.
  344. ##
  345. ## The structure here is very similar to the 128-bit
  346. ## schedule, but with an additional "low side" in
  347. ## %xmm6. The low side's rounds are the same as the
  348. ## high side's, except no rcon and no rotation.
  349. ##
  350. .align 16
  351. .Lschedule_256:
  352. movdqu 16(%rdi),%xmm0 # load key part 2 (unaligned)
  353. call _vpaes_schedule_transform # input transform
  354. mov \$7, %esi
  355. .Loop_schedule_256:
  356. call _vpaes_schedule_mangle # output low result
  357. movdqa %xmm0, %xmm6 # save cur_lo in xmm6
  358. # high round
  359. call _vpaes_schedule_round
  360. dec %rsi
  361. jz .Lschedule_mangle_last
  362. call _vpaes_schedule_mangle
  363. # low round. swap xmm7 and xmm6
  364. pshufd \$0xFF, %xmm0, %xmm0
  365. movdqa %xmm7, %xmm5
  366. movdqa %xmm6, %xmm7
  367. call _vpaes_schedule_low_round
  368. movdqa %xmm5, %xmm7
  369. jmp .Loop_schedule_256
  370. ##
  371. ## .aes_schedule_mangle_last
  372. ##
  373. ## Mangler for last round of key schedule
  374. ## Mangles %xmm0
  375. ## when encrypting, outputs out(%xmm0) ^ 63
  376. ## when decrypting, outputs unskew(%xmm0)
  377. ##
  378. ## Always called right before return... jumps to cleanup and exits
  379. ##
  380. .align 16
  381. .Lschedule_mangle_last:
  382. # schedule last round key from xmm0
  383. lea .Lk_deskew(%rip),%r11 # prepare to deskew
  384. test %rcx, %rcx
  385. jnz .Lschedule_mangle_last_dec
  386. # encrypting
  387. movdqa (%r8,%r10),%xmm1
  388. pshufb %xmm1, %xmm0 # output permute
  389. lea .Lk_opt(%rip), %r11 # prepare to output transform
  390. add \$32, %rdx
  391. .Lschedule_mangle_last_dec:
  392. add \$-16, %rdx
  393. pxor .Lk_s63(%rip), %xmm0
  394. call _vpaes_schedule_transform # output transform
  395. movdqu %xmm0, (%rdx) # save last key
  396. # cleanup
  397. pxor %xmm0, %xmm0
  398. pxor %xmm1, %xmm1
  399. pxor %xmm2, %xmm2
  400. pxor %xmm3, %xmm3
  401. pxor %xmm4, %xmm4
  402. pxor %xmm5, %xmm5
  403. pxor %xmm6, %xmm6
  404. pxor %xmm7, %xmm7
  405. ret
  406. .size _vpaes_schedule_core,.-_vpaes_schedule_core
  407. ##
  408. ## .aes_schedule_192_smear
  409. ##
  410. ## Smear the short, low side in the 192-bit key schedule.
  411. ##
  412. ## Inputs:
  413. ## %xmm7: high side, b a x y
  414. ## %xmm6: low side, d c 0 0
  415. ## %xmm13: 0
  416. ##
  417. ## Outputs:
  418. ## %xmm6: b+c+d b+c 0 0
  419. ## %xmm0: b+c+d b+c b a
  420. ##
  421. .type _vpaes_schedule_192_smear,\@abi-omnipotent
  422. .align 16
  423. _vpaes_schedule_192_smear:
  424. pshufd \$0x80, %xmm6, %xmm0 # d c 0 0 -> c 0 0 0
  425. pxor %xmm0, %xmm6 # -> c+d c 0 0
  426. pshufd \$0xFE, %xmm7, %xmm0 # b a _ _ -> b b b a
  427. pxor %xmm0, %xmm6 # -> b+c+d b+c b a
  428. movdqa %xmm6, %xmm0
  429. pxor %xmm1, %xmm1
  430. movhlps %xmm1, %xmm6 # clobber low side with zeros
  431. ret
  432. .size _vpaes_schedule_192_smear,.-_vpaes_schedule_192_smear
  433. ##
  434. ## .aes_schedule_round
  435. ##
  436. ## Runs one main round of the key schedule on %xmm0, %xmm7
  437. ##
  438. ## Specifically, runs subbytes on the high dword of %xmm0
  439. ## then rotates it by one byte and xors into the low dword of
  440. ## %xmm7.
  441. ##
  442. ## Adds rcon from low byte of %xmm8, then rotates %xmm8 for
  443. ## next rcon.
  444. ##
  445. ## Smears the dwords of %xmm7 by xoring the low into the
  446. ## second low, result into third, result into highest.
  447. ##
  448. ## Returns results in %xmm7 = %xmm0.
  449. ## Clobbers %xmm1-%xmm4, %r11.
  450. ##
  451. .type _vpaes_schedule_round,\@abi-omnipotent
  452. .align 16
  453. _vpaes_schedule_round:
  454. # extract rcon from xmm8
  455. pxor %xmm1, %xmm1
  456. palignr \$15, %xmm8, %xmm1
  457. palignr \$15, %xmm8, %xmm8
  458. pxor %xmm1, %xmm7
  459. # rotate
  460. pshufd \$0xFF, %xmm0, %xmm0
  461. palignr \$1, %xmm0, %xmm0
  462. # fall through...
  463. # low round: same as high round, but no rotation and no rcon.
  464. _vpaes_schedule_low_round:
  465. # smear xmm7
  466. movdqa %xmm7, %xmm1
  467. pslldq \$4, %xmm7
  468. pxor %xmm1, %xmm7
  469. movdqa %xmm7, %xmm1
  470. pslldq \$8, %xmm7
  471. pxor %xmm1, %xmm7
  472. pxor .Lk_s63(%rip), %xmm7
  473. # subbytes
  474. movdqa %xmm9, %xmm1
  475. pandn %xmm0, %xmm1
  476. psrld \$4, %xmm1 # 1 = i
  477. pand %xmm9, %xmm0 # 0 = k
  478. movdqa %xmm11, %xmm2 # 2 : a/k
  479. pshufb %xmm0, %xmm2 # 2 = a/k
  480. pxor %xmm1, %xmm0 # 0 = j
  481. movdqa %xmm10, %xmm3 # 3 : 1/i
  482. pshufb %xmm1, %xmm3 # 3 = 1/i
  483. pxor %xmm2, %xmm3 # 3 = iak = 1/i + a/k
  484. movdqa %xmm10, %xmm4 # 4 : 1/j
  485. pshufb %xmm0, %xmm4 # 4 = 1/j
  486. pxor %xmm2, %xmm4 # 4 = jak = 1/j + a/k
  487. movdqa %xmm10, %xmm2 # 2 : 1/iak
  488. pshufb %xmm3, %xmm2 # 2 = 1/iak
  489. pxor %xmm0, %xmm2 # 2 = io
  490. movdqa %xmm10, %xmm3 # 3 : 1/jak
  491. pshufb %xmm4, %xmm3 # 3 = 1/jak
  492. pxor %xmm1, %xmm3 # 3 = jo
  493. movdqa %xmm13, %xmm4 # 4 : sbou
  494. pshufb %xmm2, %xmm4 # 4 = sbou
  495. movdqa %xmm12, %xmm0 # 0 : sbot
  496. pshufb %xmm3, %xmm0 # 0 = sb1t
  497. pxor %xmm4, %xmm0 # 0 = sbox output
  498. # add in smeared stuff
  499. pxor %xmm7, %xmm0
  500. movdqa %xmm0, %xmm7
  501. ret
  502. .size _vpaes_schedule_round,.-_vpaes_schedule_round
  503. ##
  504. ## .aes_schedule_transform
  505. ##
  506. ## Linear-transform %xmm0 according to tables at (%r11)
  507. ##
  508. ## Requires that %xmm9 = 0x0F0F... as in preheat
  509. ## Output in %xmm0
  510. ## Clobbers %xmm1, %xmm2
  511. ##
  512. .type _vpaes_schedule_transform,\@abi-omnipotent
  513. .align 16
  514. _vpaes_schedule_transform:
  515. movdqa %xmm9, %xmm1
  516. pandn %xmm0, %xmm1
  517. psrld \$4, %xmm1
  518. pand %xmm9, %xmm0
  519. movdqa (%r11), %xmm2 # lo
  520. pshufb %xmm0, %xmm2
  521. movdqa 16(%r11), %xmm0 # hi
  522. pshufb %xmm1, %xmm0
  523. pxor %xmm2, %xmm0
  524. ret
  525. .size _vpaes_schedule_transform,.-_vpaes_schedule_transform
  526. ##
  527. ## .aes_schedule_mangle
  528. ##
  529. ## Mangle xmm0 from (basis-transformed) standard version
  530. ## to our version.
  531. ##
  532. ## On encrypt,
  533. ## xor with 0x63
  534. ## multiply by circulant 0,1,1,1
  535. ## apply shiftrows transform
  536. ##
  537. ## On decrypt,
  538. ## xor with 0x63
  539. ## multiply by "inverse mixcolumns" circulant E,B,D,9
  540. ## deskew
  541. ## apply shiftrows transform
  542. ##
  543. ##
  544. ## Writes out to (%rdx), and increments or decrements it
  545. ## Keeps track of round number mod 4 in %r8
  546. ## Preserves xmm0
  547. ## Clobbers xmm1-xmm5
  548. ##
  549. .type _vpaes_schedule_mangle,\@abi-omnipotent
  550. .align 16
  551. _vpaes_schedule_mangle:
  552. movdqa %xmm0, %xmm4 # save xmm0 for later
  553. movdqa .Lk_mc_forward(%rip),%xmm5
  554. test %rcx, %rcx
  555. jnz .Lschedule_mangle_dec
  556. # encrypting
  557. add \$16, %rdx
  558. pxor .Lk_s63(%rip),%xmm4
  559. pshufb %xmm5, %xmm4
  560. movdqa %xmm4, %xmm3
  561. pshufb %xmm5, %xmm4
  562. pxor %xmm4, %xmm3
  563. pshufb %xmm5, %xmm4
  564. pxor %xmm4, %xmm3
  565. jmp .Lschedule_mangle_both
  566. .align 16
  567. .Lschedule_mangle_dec:
  568. # inverse mix columns
  569. lea .Lk_dksd(%rip),%r11
  570. movdqa %xmm9, %xmm1
  571. pandn %xmm4, %xmm1
  572. psrld \$4, %xmm1 # 1 = hi
  573. pand %xmm9, %xmm4 # 4 = lo
  574. movdqa 0x00(%r11), %xmm2
  575. pshufb %xmm4, %xmm2
  576. movdqa 0x10(%r11), %xmm3
  577. pshufb %xmm1, %xmm3
  578. pxor %xmm2, %xmm3
  579. pshufb %xmm5, %xmm3
  580. movdqa 0x20(%r11), %xmm2
  581. pshufb %xmm4, %xmm2
  582. pxor %xmm3, %xmm2
  583. movdqa 0x30(%r11), %xmm3
  584. pshufb %xmm1, %xmm3
  585. pxor %xmm2, %xmm3
  586. pshufb %xmm5, %xmm3
  587. movdqa 0x40(%r11), %xmm2
  588. pshufb %xmm4, %xmm2
  589. pxor %xmm3, %xmm2
  590. movdqa 0x50(%r11), %xmm3
  591. pshufb %xmm1, %xmm3
  592. pxor %xmm2, %xmm3
  593. pshufb %xmm5, %xmm3
  594. movdqa 0x60(%r11), %xmm2
  595. pshufb %xmm4, %xmm2
  596. pxor %xmm3, %xmm2
  597. movdqa 0x70(%r11), %xmm3
  598. pshufb %xmm1, %xmm3
  599. pxor %xmm2, %xmm3
  600. add \$-16, %rdx
  601. .Lschedule_mangle_both:
  602. movdqa (%r8,%r10),%xmm1
  603. pshufb %xmm1,%xmm3
  604. add \$-16, %r8
  605. and \$0x30, %r8
  606. movdqu %xmm3, (%rdx)
  607. ret
  608. .size _vpaes_schedule_mangle,.-_vpaes_schedule_mangle
  609. #
  610. # Interface to OpenSSL
  611. #
  612. .globl ${PREFIX}_set_encrypt_key
  613. .type ${PREFIX}_set_encrypt_key,\@function,3
  614. .align 16
  615. ${PREFIX}_set_encrypt_key:
  616. ___
  617. $code.=<<___ if ($win64);
  618. lea -0xb8(%rsp),%rsp
  619. movaps %xmm6,0x10(%rsp)
  620. movaps %xmm7,0x20(%rsp)
  621. movaps %xmm8,0x30(%rsp)
  622. movaps %xmm9,0x40(%rsp)
  623. movaps %xmm10,0x50(%rsp)
  624. movaps %xmm11,0x60(%rsp)
  625. movaps %xmm12,0x70(%rsp)
  626. movaps %xmm13,0x80(%rsp)
  627. movaps %xmm14,0x90(%rsp)
  628. movaps %xmm15,0xa0(%rsp)
  629. .Lenc_key_body:
  630. ___
  631. $code.=<<___;
  632. mov %esi,%eax
  633. shr \$5,%eax
  634. add \$5,%eax
  635. mov %eax,240(%rdx) # AES_KEY->rounds = nbits/32+5;
  636. mov \$0,%ecx
  637. mov \$0x30,%r8d
  638. call _vpaes_schedule_core
  639. ___
  640. $code.=<<___ if ($win64);
  641. movaps 0x10(%rsp),%xmm6
  642. movaps 0x20(%rsp),%xmm7
  643. movaps 0x30(%rsp),%xmm8
  644. movaps 0x40(%rsp),%xmm9
  645. movaps 0x50(%rsp),%xmm10
  646. movaps 0x60(%rsp),%xmm11
  647. movaps 0x70(%rsp),%xmm12
  648. movaps 0x80(%rsp),%xmm13
  649. movaps 0x90(%rsp),%xmm14
  650. movaps 0xa0(%rsp),%xmm15
  651. lea 0xb8(%rsp),%rsp
  652. .Lenc_key_epilogue:
  653. ___
  654. $code.=<<___;
  655. xor %eax,%eax
  656. ret
  657. .size ${PREFIX}_set_encrypt_key,.-${PREFIX}_set_encrypt_key
  658. .globl ${PREFIX}_set_decrypt_key
  659. .type ${PREFIX}_set_decrypt_key,\@function,3
  660. .align 16
  661. ${PREFIX}_set_decrypt_key:
  662. ___
  663. $code.=<<___ if ($win64);
  664. lea -0xb8(%rsp),%rsp
  665. movaps %xmm6,0x10(%rsp)
  666. movaps %xmm7,0x20(%rsp)
  667. movaps %xmm8,0x30(%rsp)
  668. movaps %xmm9,0x40(%rsp)
  669. movaps %xmm10,0x50(%rsp)
  670. movaps %xmm11,0x60(%rsp)
  671. movaps %xmm12,0x70(%rsp)
  672. movaps %xmm13,0x80(%rsp)
  673. movaps %xmm14,0x90(%rsp)
  674. movaps %xmm15,0xa0(%rsp)
  675. .Ldec_key_body:
  676. ___
  677. $code.=<<___;
  678. mov %esi,%eax
  679. shr \$5,%eax
  680. add \$5,%eax
  681. mov %eax,240(%rdx) # AES_KEY->rounds = nbits/32+5;
  682. shl \$4,%eax
  683. lea 16(%rdx,%rax),%rdx
  684. mov \$1,%ecx
  685. mov %esi,%r8d
  686. shr \$1,%r8d
  687. and \$32,%r8d
  688. xor \$32,%r8d # nbits==192?0:32
  689. call _vpaes_schedule_core
  690. ___
  691. $code.=<<___ if ($win64);
  692. movaps 0x10(%rsp),%xmm6
  693. movaps 0x20(%rsp),%xmm7
  694. movaps 0x30(%rsp),%xmm8
  695. movaps 0x40(%rsp),%xmm9
  696. movaps 0x50(%rsp),%xmm10
  697. movaps 0x60(%rsp),%xmm11
  698. movaps 0x70(%rsp),%xmm12
  699. movaps 0x80(%rsp),%xmm13
  700. movaps 0x90(%rsp),%xmm14
  701. movaps 0xa0(%rsp),%xmm15
  702. lea 0xb8(%rsp),%rsp
  703. .Ldec_key_epilogue:
  704. ___
  705. $code.=<<___;
  706. xor %eax,%eax
  707. ret
  708. .size ${PREFIX}_set_decrypt_key,.-${PREFIX}_set_decrypt_key
  709. .globl ${PREFIX}_encrypt
  710. .type ${PREFIX}_encrypt,\@function,3
  711. .align 16
  712. ${PREFIX}_encrypt:
  713. ___
  714. $code.=<<___ if ($win64);
  715. lea -0xb8(%rsp),%rsp
  716. movaps %xmm6,0x10(%rsp)
  717. movaps %xmm7,0x20(%rsp)
  718. movaps %xmm8,0x30(%rsp)
  719. movaps %xmm9,0x40(%rsp)
  720. movaps %xmm10,0x50(%rsp)
  721. movaps %xmm11,0x60(%rsp)
  722. movaps %xmm12,0x70(%rsp)
  723. movaps %xmm13,0x80(%rsp)
  724. movaps %xmm14,0x90(%rsp)
  725. movaps %xmm15,0xa0(%rsp)
  726. .Lenc_body:
  727. ___
  728. $code.=<<___;
  729. movdqu (%rdi),%xmm0
  730. call _vpaes_preheat
  731. call _vpaes_encrypt_core
  732. movdqu %xmm0,(%rsi)
  733. ___
  734. $code.=<<___ if ($win64);
  735. movaps 0x10(%rsp),%xmm6
  736. movaps 0x20(%rsp),%xmm7
  737. movaps 0x30(%rsp),%xmm8
  738. movaps 0x40(%rsp),%xmm9
  739. movaps 0x50(%rsp),%xmm10
  740. movaps 0x60(%rsp),%xmm11
  741. movaps 0x70(%rsp),%xmm12
  742. movaps 0x80(%rsp),%xmm13
  743. movaps 0x90(%rsp),%xmm14
  744. movaps 0xa0(%rsp),%xmm15
  745. lea 0xb8(%rsp),%rsp
  746. .Lenc_epilogue:
  747. ___
  748. $code.=<<___;
  749. ret
  750. .size ${PREFIX}_encrypt,.-${PREFIX}_encrypt
  751. .globl ${PREFIX}_decrypt
  752. .type ${PREFIX}_decrypt,\@function,3
  753. .align 16
  754. ${PREFIX}_decrypt:
  755. ___
  756. $code.=<<___ if ($win64);
  757. lea -0xb8(%rsp),%rsp
  758. movaps %xmm6,0x10(%rsp)
  759. movaps %xmm7,0x20(%rsp)
  760. movaps %xmm8,0x30(%rsp)
  761. movaps %xmm9,0x40(%rsp)
  762. movaps %xmm10,0x50(%rsp)
  763. movaps %xmm11,0x60(%rsp)
  764. movaps %xmm12,0x70(%rsp)
  765. movaps %xmm13,0x80(%rsp)
  766. movaps %xmm14,0x90(%rsp)
  767. movaps %xmm15,0xa0(%rsp)
  768. .Ldec_body:
  769. ___
  770. $code.=<<___;
  771. movdqu (%rdi),%xmm0
  772. call _vpaes_preheat
  773. call _vpaes_decrypt_core
  774. movdqu %xmm0,(%rsi)
  775. ___
  776. $code.=<<___ if ($win64);
  777. movaps 0x10(%rsp),%xmm6
  778. movaps 0x20(%rsp),%xmm7
  779. movaps 0x30(%rsp),%xmm8
  780. movaps 0x40(%rsp),%xmm9
  781. movaps 0x50(%rsp),%xmm10
  782. movaps 0x60(%rsp),%xmm11
  783. movaps 0x70(%rsp),%xmm12
  784. movaps 0x80(%rsp),%xmm13
  785. movaps 0x90(%rsp),%xmm14
  786. movaps 0xa0(%rsp),%xmm15
  787. lea 0xb8(%rsp),%rsp
  788. .Ldec_epilogue:
  789. ___
  790. $code.=<<___;
  791. ret
  792. .size ${PREFIX}_decrypt,.-${PREFIX}_decrypt
  793. ___
  794. {
  795. my ($inp,$out,$len,$key,$ivp,$enc)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9");
  796. # void AES_cbc_encrypt (const void char *inp, unsigned char *out,
  797. # size_t length, const AES_KEY *key,
  798. # unsigned char *ivp,const int enc);
  799. $code.=<<___;
  800. .globl ${PREFIX}_cbc_encrypt
  801. .type ${PREFIX}_cbc_encrypt,\@function,6
  802. .align 16
  803. ${PREFIX}_cbc_encrypt:
  804. xchg $key,$len
  805. ___
  806. ($len,$key)=($key,$len);
  807. $code.=<<___;
  808. ___
  809. $code.=<<___ if ($win64);
  810. lea -0xb8(%rsp),%rsp
  811. movaps %xmm6,0x10(%rsp)
  812. movaps %xmm7,0x20(%rsp)
  813. movaps %xmm8,0x30(%rsp)
  814. movaps %xmm9,0x40(%rsp)
  815. movaps %xmm10,0x50(%rsp)
  816. movaps %xmm11,0x60(%rsp)
  817. movaps %xmm12,0x70(%rsp)
  818. movaps %xmm13,0x80(%rsp)
  819. movaps %xmm14,0x90(%rsp)
  820. movaps %xmm15,0xa0(%rsp)
  821. .Lcbc_body:
  822. ___
  823. $code.=<<___;
  824. movdqu ($ivp),%xmm6 # load IV
  825. sub $inp,$out
  826. sub \$16,$len
  827. call _vpaes_preheat
  828. cmp \$0,${enc}d
  829. je .Lcbc_dec_loop
  830. jmp .Lcbc_enc_loop
  831. .align 16
  832. .Lcbc_enc_loop:
  833. movdqu ($inp),%xmm0
  834. pxor %xmm6,%xmm0
  835. call _vpaes_encrypt_core
  836. movdqa %xmm0,%xmm6
  837. movdqu %xmm0,($out,$inp)
  838. lea 16($inp),$inp
  839. sub \$16,$len
  840. jnc .Lcbc_enc_loop
  841. jmp .Lcbc_done
  842. .align 16
  843. .Lcbc_dec_loop:
  844. movdqu ($inp),%xmm0
  845. movdqa %xmm0,%xmm7
  846. call _vpaes_decrypt_core
  847. pxor %xmm6,%xmm0
  848. movdqa %xmm7,%xmm6
  849. movdqu %xmm0,($out,$inp)
  850. lea 16($inp),$inp
  851. sub \$16,$len
  852. jnc .Lcbc_dec_loop
  853. .Lcbc_done:
  854. movdqu %xmm6,($ivp) # save IV
  855. ___
  856. $code.=<<___ if ($win64);
  857. movaps 0x10(%rsp),%xmm6
  858. movaps 0x20(%rsp),%xmm7
  859. movaps 0x30(%rsp),%xmm8
  860. movaps 0x40(%rsp),%xmm9
  861. movaps 0x50(%rsp),%xmm10
  862. movaps 0x60(%rsp),%xmm11
  863. movaps 0x70(%rsp),%xmm12
  864. movaps 0x80(%rsp),%xmm13
  865. movaps 0x90(%rsp),%xmm14
  866. movaps 0xa0(%rsp),%xmm15
  867. lea 0xb8(%rsp),%rsp
  868. .Lcbc_epilogue:
  869. ___
  870. $code.=<<___;
  871. ret
  872. .size ${PREFIX}_cbc_encrypt,.-${PREFIX}_cbc_encrypt
  873. ___
  874. }
  875. $code.=<<___;
  876. ##
  877. ## _aes_preheat
  878. ##
  879. ## Fills register %r10 -> .aes_consts (so you can -fPIC)
  880. ## and %xmm9-%xmm15 as specified below.
  881. ##
  882. .type _vpaes_preheat,\@abi-omnipotent
  883. .align 16
  884. _vpaes_preheat:
  885. lea .Lk_s0F(%rip), %r10
  886. movdqa -0x20(%r10), %xmm10 # .Lk_inv
  887. movdqa -0x10(%r10), %xmm11 # .Lk_inv+16
  888. movdqa 0x00(%r10), %xmm9 # .Lk_s0F
  889. movdqa 0x30(%r10), %xmm13 # .Lk_sb1
  890. movdqa 0x40(%r10), %xmm12 # .Lk_sb1+16
  891. movdqa 0x50(%r10), %xmm15 # .Lk_sb2
  892. movdqa 0x60(%r10), %xmm14 # .Lk_sb2+16
  893. ret
  894. .size _vpaes_preheat,.-_vpaes_preheat
  895. ########################################################
  896. ## ##
  897. ## Constants ##
  898. ## ##
  899. ########################################################
  900. .type _vpaes_consts,\@object
  901. .align 64
  902. _vpaes_consts:
  903. .Lk_inv: # inv, inva
  904. .quad 0x0E05060F0D080180, 0x040703090A0B0C02
  905. .quad 0x01040A060F0B0780, 0x030D0E0C02050809
  906. .Lk_s0F: # s0F
  907. .quad 0x0F0F0F0F0F0F0F0F, 0x0F0F0F0F0F0F0F0F
  908. .Lk_ipt: # input transform (lo, hi)
  909. .quad 0xC2B2E8985A2A7000, 0xCABAE09052227808
  910. .quad 0x4C01307D317C4D00, 0xCD80B1FCB0FDCC81
  911. .Lk_sb1: # sb1u, sb1t
  912. .quad 0xB19BE18FCB503E00, 0xA5DF7A6E142AF544
  913. .quad 0x3618D415FAE22300, 0x3BF7CCC10D2ED9EF
  914. .Lk_sb2: # sb2u, sb2t
  915. .quad 0xE27A93C60B712400, 0x5EB7E955BC982FCD
  916. .quad 0x69EB88400AE12900, 0xC2A163C8AB82234A
  917. .Lk_sbo: # sbou, sbot
  918. .quad 0xD0D26D176FBDC700, 0x15AABF7AC502A878
  919. .quad 0xCFE474A55FBB6A00, 0x8E1E90D1412B35FA
  920. .Lk_mc_forward: # mc_forward
  921. .quad 0x0407060500030201, 0x0C0F0E0D080B0A09
  922. .quad 0x080B0A0904070605, 0x000302010C0F0E0D
  923. .quad 0x0C0F0E0D080B0A09, 0x0407060500030201
  924. .quad 0x000302010C0F0E0D, 0x080B0A0904070605
  925. .Lk_mc_backward:# mc_backward
  926. .quad 0x0605040702010003, 0x0E0D0C0F0A09080B
  927. .quad 0x020100030E0D0C0F, 0x0A09080B06050407
  928. .quad 0x0E0D0C0F0A09080B, 0x0605040702010003
  929. .quad 0x0A09080B06050407, 0x020100030E0D0C0F
  930. .Lk_sr: # sr
  931. .quad 0x0706050403020100, 0x0F0E0D0C0B0A0908
  932. .quad 0x030E09040F0A0500, 0x0B06010C07020D08
  933. .quad 0x0F060D040B020900, 0x070E050C030A0108
  934. .quad 0x0B0E0104070A0D00, 0x0306090C0F020508
  935. .Lk_rcon: # rcon
  936. .quad 0x1F8391B9AF9DEEB6, 0x702A98084D7C7D81
  937. .Lk_s63: # s63: all equal to 0x63 transformed
  938. .quad 0x5B5B5B5B5B5B5B5B, 0x5B5B5B5B5B5B5B5B
  939. .Lk_opt: # output transform
  940. .quad 0xFF9F4929D6B66000, 0xF7974121DEBE6808
  941. .quad 0x01EDBD5150BCEC00, 0xE10D5DB1B05C0CE0
  942. .Lk_deskew: # deskew tables: inverts the sbox's "skew"
  943. .quad 0x07E4A34047A4E300, 0x1DFEB95A5DBEF91A
  944. .quad 0x5F36B5DC83EA6900, 0x2841C2ABF49D1E77
  945. ##
  946. ## Decryption stuff
  947. ## Key schedule constants
  948. ##
  949. .Lk_dksd: # decryption key schedule: invskew x*D
  950. .quad 0xFEB91A5DA3E44700, 0x0740E3A45A1DBEF9
  951. .quad 0x41C277F4B5368300, 0x5FDC69EAAB289D1E
  952. .Lk_dksb: # decryption key schedule: invskew x*B
  953. .quad 0x9A4FCA1F8550D500, 0x03D653861CC94C99
  954. .quad 0x115BEDA7B6FC4A00, 0xD993256F7E3482C8
  955. .Lk_dkse: # decryption key schedule: invskew x*E + 0x63
  956. .quad 0xD5031CCA1FC9D600, 0x53859A4C994F5086
  957. .quad 0xA23196054FDC7BE8, 0xCD5EF96A20B31487
  958. .Lk_dks9: # decryption key schedule: invskew x*9
  959. .quad 0xB6116FC87ED9A700, 0x4AED933482255BFC
  960. .quad 0x4576516227143300, 0x8BB89FACE9DAFDCE
  961. ##
  962. ## Decryption stuff
  963. ## Round function constants
  964. ##
  965. .Lk_dipt: # decryption input transform
  966. .quad 0x0F505B040B545F00, 0x154A411E114E451A
  967. .quad 0x86E383E660056500, 0x12771772F491F194
  968. .Lk_dsb9: # decryption sbox output *9*u, *9*t
  969. .quad 0x851C03539A86D600, 0xCAD51F504F994CC9
  970. .quad 0xC03B1789ECD74900, 0x725E2C9EB2FBA565
  971. .Lk_dsbd: # decryption sbox output *D*u, *D*t
  972. .quad 0x7D57CCDFE6B1A200, 0xF56E9B13882A4439
  973. .quad 0x3CE2FAF724C6CB00, 0x2931180D15DEEFD3
  974. .Lk_dsbb: # decryption sbox output *B*u, *B*t
  975. .quad 0xD022649296B44200, 0x602646F6B0F2D404
  976. .quad 0xC19498A6CD596700, 0xF3FF0C3E3255AA6B
  977. .Lk_dsbe: # decryption sbox output *E*u, *E*t
  978. .quad 0x46F2929626D4D000, 0x2242600464B4F6B0
  979. .quad 0x0C55A6CDFFAAC100, 0x9467F36B98593E32
  980. .Lk_dsbo: # decryption sbox final output
  981. .quad 0x1387EA537EF94000, 0xC7AA6DB9D4943E2D
  982. .quad 0x12D7560F93441D00, 0xCA4B8159D8C58E9C
  983. .asciz "Vector Permutaion AES for x86_64, Mike Hamburg (Stanford University)"
  984. .align 64
  985. .size _vpaes_consts,.-_vpaes_consts
  986. ___
  987. if ($win64) {
  988. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  989. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  990. $rec="%rcx";
  991. $frame="%rdx";
  992. $context="%r8";
  993. $disp="%r9";
  994. $code.=<<___;
  995. .extern __imp_RtlVirtualUnwind
  996. .type se_handler,\@abi-omnipotent
  997. .align 16
  998. se_handler:
  999. push %rsi
  1000. push %rdi
  1001. push %rbx
  1002. push %rbp
  1003. push %r12
  1004. push %r13
  1005. push %r14
  1006. push %r15
  1007. pushfq
  1008. sub \$64,%rsp
  1009. mov 120($context),%rax # pull context->Rax
  1010. mov 248($context),%rbx # pull context->Rip
  1011. mov 8($disp),%rsi # disp->ImageBase
  1012. mov 56($disp),%r11 # disp->HandlerData
  1013. mov 0(%r11),%r10d # HandlerData[0]
  1014. lea (%rsi,%r10),%r10 # prologue label
  1015. cmp %r10,%rbx # context->Rip<prologue label
  1016. jb .Lin_prologue
  1017. mov 152($context),%rax # pull context->Rsp
  1018. mov 4(%r11),%r10d # HandlerData[1]
  1019. lea (%rsi,%r10),%r10 # epilogue label
  1020. cmp %r10,%rbx # context->Rip>=epilogue label
  1021. jae .Lin_prologue
  1022. lea 16(%rax),%rsi # %xmm save area
  1023. lea 512($context),%rdi # &context.Xmm6
  1024. mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
  1025. .long 0xa548f3fc # cld; rep movsq
  1026. lea 0xb8(%rax),%rax # adjust stack pointer
  1027. .Lin_prologue:
  1028. mov 8(%rax),%rdi
  1029. mov 16(%rax),%rsi
  1030. mov %rax,152($context) # restore context->Rsp
  1031. mov %rsi,168($context) # restore context->Rsi
  1032. mov %rdi,176($context) # restore context->Rdi
  1033. mov 40($disp),%rdi # disp->ContextRecord
  1034. mov $context,%rsi # context
  1035. mov \$`1232/8`,%ecx # sizeof(CONTEXT)
  1036. .long 0xa548f3fc # cld; rep movsq
  1037. mov $disp,%rsi
  1038. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  1039. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  1040. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  1041. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  1042. mov 40(%rsi),%r10 # disp->ContextRecord
  1043. lea 56(%rsi),%r11 # &disp->HandlerData
  1044. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  1045. mov %r10,32(%rsp) # arg5
  1046. mov %r11,40(%rsp) # arg6
  1047. mov %r12,48(%rsp) # arg7
  1048. mov %rcx,56(%rsp) # arg8, (NULL)
  1049. call *__imp_RtlVirtualUnwind(%rip)
  1050. mov \$1,%eax # ExceptionContinueSearch
  1051. add \$64,%rsp
  1052. popfq
  1053. pop %r15
  1054. pop %r14
  1055. pop %r13
  1056. pop %r12
  1057. pop %rbp
  1058. pop %rbx
  1059. pop %rdi
  1060. pop %rsi
  1061. ret
  1062. .size se_handler,.-se_handler
  1063. .section .pdata
  1064. .align 4
  1065. .rva .LSEH_begin_${PREFIX}_set_encrypt_key
  1066. .rva .LSEH_end_${PREFIX}_set_encrypt_key
  1067. .rva .LSEH_info_${PREFIX}_set_encrypt_key
  1068. .rva .LSEH_begin_${PREFIX}_set_decrypt_key
  1069. .rva .LSEH_end_${PREFIX}_set_decrypt_key
  1070. .rva .LSEH_info_${PREFIX}_set_decrypt_key
  1071. .rva .LSEH_begin_${PREFIX}_encrypt
  1072. .rva .LSEH_end_${PREFIX}_encrypt
  1073. .rva .LSEH_info_${PREFIX}_encrypt
  1074. .rva .LSEH_begin_${PREFIX}_decrypt
  1075. .rva .LSEH_end_${PREFIX}_decrypt
  1076. .rva .LSEH_info_${PREFIX}_decrypt
  1077. .rva .LSEH_begin_${PREFIX}_cbc_encrypt
  1078. .rva .LSEH_end_${PREFIX}_cbc_encrypt
  1079. .rva .LSEH_info_${PREFIX}_cbc_encrypt
  1080. .section .xdata
  1081. .align 8
  1082. .LSEH_info_${PREFIX}_set_encrypt_key:
  1083. .byte 9,0,0,0
  1084. .rva se_handler
  1085. .rva .Lenc_key_body,.Lenc_key_epilogue # HandlerData[]
  1086. .LSEH_info_${PREFIX}_set_decrypt_key:
  1087. .byte 9,0,0,0
  1088. .rva se_handler
  1089. .rva .Ldec_key_body,.Ldec_key_epilogue # HandlerData[]
  1090. .LSEH_info_${PREFIX}_encrypt:
  1091. .byte 9,0,0,0
  1092. .rva se_handler
  1093. .rva .Lenc_body,.Lenc_epilogue # HandlerData[]
  1094. .LSEH_info_${PREFIX}_decrypt:
  1095. .byte 9,0,0,0
  1096. .rva se_handler
  1097. .rva .Ldec_body,.Ldec_epilogue # HandlerData[]
  1098. .LSEH_info_${PREFIX}_cbc_encrypt:
  1099. .byte 9,0,0,0
  1100. .rva se_handler
  1101. .rva .Lcbc_body,.Lcbc_epilogue # HandlerData[]
  1102. ___
  1103. }
  1104. $code =~ s/\`([^\`]*)\`/eval($1)/gem;
  1105. print $code;
  1106. close STDOUT;