vpaes-x86_64.pl 30 KB

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