2
0

vpaes-x86_64.pl 31 KB

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