sparcv9_modes.pl 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  1. #!/usr/bin/env perl
  2. # Specific modes implementations for SPARC Architecture 2011. There
  3. # is T4 dependency though, an ASI value that is not specified in the
  4. # Architecture Manual. But as SPARC universe is rather monocultural,
  5. # we imply that processor capable of executing crypto instructions
  6. # can handle the ASI in question as well. This means that we ought to
  7. # keep eyes open when new processors emerge...
  8. #
  9. # As for above mentioned ASI. It's so called "block initializing
  10. # store" which cancels "read" in "read-update-write" on cache lines.
  11. # This is "cooperative" optimization, as it reduces overall pressure
  12. # on memory interface. Benefits can't be observed/quantified with
  13. # usual benchmarks, on the contrary you can notice that single-thread
  14. # performance for parallelizable modes is ~1.5% worse for largest
  15. # block sizes [though few percent better for not so long ones]. All
  16. # this based on suggestions from David Miller.
  17. sub asm_init { # to be called with @ARGV as argument
  18. for (@_) { $::abibits=64 if (/\-m64/ || /\-xarch\=v9/); }
  19. if ($::abibits==64) { $::bias=2047; $::frame=192; $::size_t_cc="%xcc"; }
  20. else { $::bias=0; $::frame=112; $::size_t_cc="%icc"; }
  21. }
  22. # unified interface
  23. my ($inp,$out,$len,$key,$ivec)=map("%i$_",(0..5));
  24. # local variables
  25. my ($ileft,$iright,$ooff,$omask,$ivoff,$blk_init)=map("%l$_",(0..7));
  26. sub alg_cbc_encrypt_implement {
  27. my ($alg,$bits) = @_;
  28. $::code.=<<___;
  29. .globl ${alg}${bits}_t4_cbc_encrypt
  30. .align 32
  31. ${alg}${bits}_t4_cbc_encrypt:
  32. save %sp, -$::frame, %sp
  33. cmp $len, 0
  34. be,pn $::size_t_cc, .L${bits}_cbc_enc_abort
  35. sub $inp, $out, $blk_init ! $inp!=$out
  36. ___
  37. $::code.=<<___ if (!$::evp);
  38. andcc $ivec, 7, $ivoff
  39. alignaddr $ivec, %g0, $ivec
  40. ldd [$ivec + 0], %f0 ! load ivec
  41. bz,pt %icc, 1f
  42. ldd [$ivec + 8], %f2
  43. ldd [$ivec + 16], %f4
  44. faligndata %f0, %f2, %f0
  45. faligndata %f2, %f4, %f2
  46. 1:
  47. ___
  48. $::code.=<<___ if ($::evp);
  49. ld [$ivec + 0], %f0
  50. ld [$ivec + 4], %f1
  51. ld [$ivec + 8], %f2
  52. ld [$ivec + 12], %f3
  53. ___
  54. $::code.=<<___;
  55. prefetch [$inp], 20
  56. prefetch [$inp + 63], 20
  57. call _${alg}${bits}_load_enckey
  58. and $inp, 7, $ileft
  59. andn $inp, 7, $inp
  60. sll $ileft, 3, $ileft
  61. mov 64, $iright
  62. mov 0xff, $omask
  63. sub $iright, $ileft, $iright
  64. and $out, 7, $ooff
  65. cmp $len, 127
  66. movrnz $ooff, 0, $blk_init ! if ( $out&7 ||
  67. movleu $::size_t_cc, 0, $blk_init ! $len<128 ||
  68. brnz,pn $blk_init, .L${bits}cbc_enc_blk ! $inp==$out)
  69. srl $omask, $ooff, $omask
  70. alignaddrl $out, %g0, $out
  71. srlx $len, 4, $len
  72. prefetch [$out], 22
  73. .L${bits}_cbc_enc_loop:
  74. ldx [$inp + 0], %o0
  75. brz,pt $ileft, 4f
  76. ldx [$inp + 8], %o1
  77. ldx [$inp + 16], %o2
  78. sllx %o0, $ileft, %o0
  79. srlx %o1, $iright, %g1
  80. sllx %o1, $ileft, %o1
  81. or %g1, %o0, %o0
  82. srlx %o2, $iright, %o2
  83. or %o2, %o1, %o1
  84. 4:
  85. xor %g4, %o0, %o0 ! ^= rk[0]
  86. xor %g5, %o1, %o1
  87. movxtod %o0, %f12
  88. movxtod %o1, %f14
  89. fxor %f12, %f0, %f0 ! ^= ivec
  90. fxor %f14, %f2, %f2
  91. prefetch [$out + 63], 22
  92. prefetch [$inp + 16+63], 20
  93. call _${alg}${bits}_encrypt_1x
  94. add $inp, 16, $inp
  95. brnz,pn $ooff, 2f
  96. sub $len, 1, $len
  97. std %f0, [$out + 0]
  98. std %f2, [$out + 8]
  99. brnz,pt $len, .L${bits}_cbc_enc_loop
  100. add $out, 16, $out
  101. ___
  102. $::code.=<<___ if ($::evp);
  103. st %f0, [$ivec + 0]
  104. st %f1, [$ivec + 4]
  105. st %f2, [$ivec + 8]
  106. st %f3, [$ivec + 12]
  107. ___
  108. $::code.=<<___ if (!$::evp);
  109. brnz,pn $ivoff, 3f
  110. nop
  111. std %f0, [$ivec + 0] ! write out ivec
  112. std %f2, [$ivec + 8]
  113. ___
  114. $::code.=<<___;
  115. .L${bits}_cbc_enc_abort:
  116. ret
  117. restore
  118. .align 16
  119. 2: ldxa [$inp]0x82, %o0 ! avoid read-after-write hazard
  120. ! and ~3x deterioration
  121. ! in inp==out case
  122. faligndata %f0, %f0, %f4 ! handle unaligned output
  123. faligndata %f0, %f2, %f6
  124. faligndata %f2, %f2, %f8
  125. stda %f4, [$out + $omask]0xc0 ! partial store
  126. std %f6, [$out + 8]
  127. add $out, 16, $out
  128. orn %g0, $omask, $omask
  129. stda %f8, [$out + $omask]0xc0 ! partial store
  130. brnz,pt $len, .L${bits}_cbc_enc_loop+4
  131. orn %g0, $omask, $omask
  132. ___
  133. $::code.=<<___ if ($::evp);
  134. st %f0, [$ivec + 0]
  135. st %f1, [$ivec + 4]
  136. st %f2, [$ivec + 8]
  137. st %f3, [$ivec + 12]
  138. ___
  139. $::code.=<<___ if (!$::evp);
  140. brnz,pn $ivoff, 3f
  141. nop
  142. std %f0, [$ivec + 0] ! write out ivec
  143. std %f2, [$ivec + 8]
  144. ret
  145. restore
  146. .align 16
  147. 3: alignaddrl $ivec, $ivoff, %g0 ! handle unaligned ivec
  148. mov 0xff, $omask
  149. srl $omask, $ivoff, $omask
  150. faligndata %f0, %f0, %f4
  151. faligndata %f0, %f2, %f6
  152. faligndata %f2, %f2, %f8
  153. stda %f4, [$ivec + $omask]0xc0
  154. std %f6, [$ivec + 8]
  155. add $ivec, 16, $ivec
  156. orn %g0, $omask, $omask
  157. stda %f8, [$ivec + $omask]0xc0
  158. ___
  159. $::code.=<<___;
  160. ret
  161. restore
  162. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  163. .align 32
  164. .L${bits}cbc_enc_blk:
  165. add $out, $len, $blk_init
  166. and $blk_init, 63, $blk_init ! tail
  167. sub $len, $blk_init, $len
  168. add $blk_init, 15, $blk_init ! round up to 16n
  169. srlx $len, 4, $len
  170. srl $blk_init, 4, $blk_init
  171. .L${bits}_cbc_enc_blk_loop:
  172. ldx [$inp + 0], %o0
  173. brz,pt $ileft, 5f
  174. ldx [$inp + 8], %o1
  175. ldx [$inp + 16], %o2
  176. sllx %o0, $ileft, %o0
  177. srlx %o1, $iright, %g1
  178. sllx %o1, $ileft, %o1
  179. or %g1, %o0, %o0
  180. srlx %o2, $iright, %o2
  181. or %o2, %o1, %o1
  182. 5:
  183. xor %g4, %o0, %o0 ! ^= rk[0]
  184. xor %g5, %o1, %o1
  185. movxtod %o0, %f12
  186. movxtod %o1, %f14
  187. fxor %f12, %f0, %f0 ! ^= ivec
  188. fxor %f14, %f2, %f2
  189. prefetch [$inp + 16+63], 20
  190. call _${alg}${bits}_encrypt_1x
  191. add $inp, 16, $inp
  192. sub $len, 1, $len
  193. stda %f0, [$out]0xe2 ! ASI_BLK_INIT, T4-specific
  194. add $out, 8, $out
  195. stda %f2, [$out]0xe2 ! ASI_BLK_INIT, T4-specific
  196. brnz,pt $len, .L${bits}_cbc_enc_blk_loop
  197. add $out, 8, $out
  198. membar #StoreLoad|#StoreStore
  199. brnz,pt $blk_init, .L${bits}_cbc_enc_loop
  200. mov $blk_init, $len
  201. ___
  202. $::code.=<<___ if ($::evp);
  203. st %f0, [$ivec + 0]
  204. st %f1, [$ivec + 4]
  205. st %f2, [$ivec + 8]
  206. st %f3, [$ivec + 12]
  207. ___
  208. $::code.=<<___ if (!$::evp);
  209. brnz,pn $ivoff, 3b
  210. nop
  211. std %f0, [$ivec + 0] ! write out ivec
  212. std %f2, [$ivec + 8]
  213. ___
  214. $::code.=<<___;
  215. ret
  216. restore
  217. .type ${alg}${bits}_t4_cbc_encrypt,#function
  218. .size ${alg}${bits}_t4_cbc_encrypt,.-${alg}${bits}_t4_cbc_encrypt
  219. ___
  220. }
  221. sub alg_cbc_decrypt_implement {
  222. my ($alg,$bits) = @_;
  223. $::code.=<<___;
  224. .globl ${alg}${bits}_t4_cbc_decrypt
  225. .align 32
  226. ${alg}${bits}_t4_cbc_decrypt:
  227. save %sp, -$::frame, %sp
  228. cmp $len, 0
  229. be,pn $::size_t_cc, .L${bits}_cbc_dec_abort
  230. sub $inp, $out, $blk_init ! $inp!=$out
  231. ___
  232. $::code.=<<___ if (!$::evp);
  233. andcc $ivec, 7, $ivoff
  234. alignaddr $ivec, %g0, $ivec
  235. ldd [$ivec + 0], %f12 ! load ivec
  236. bz,pt %icc, 1f
  237. ldd [$ivec + 8], %f14
  238. ldd [$ivec + 16], %f0
  239. faligndata %f12, %f14, %f12
  240. faligndata %f14, %f0, %f14
  241. 1:
  242. ___
  243. $::code.=<<___ if ($::evp);
  244. ld [$ivec + 0], %f12 ! load ivec
  245. ld [$ivec + 4], %f13
  246. ld [$ivec + 8], %f14
  247. ld [$ivec + 12], %f15
  248. ___
  249. $::code.=<<___;
  250. prefetch [$inp], 20
  251. prefetch [$inp + 63], 20
  252. call _${alg}${bits}_load_deckey
  253. and $inp, 7, $ileft
  254. andn $inp, 7, $inp
  255. sll $ileft, 3, $ileft
  256. mov 64, $iright
  257. mov 0xff, $omask
  258. sub $iright, $ileft, $iright
  259. and $out, 7, $ooff
  260. cmp $len, 255
  261. movrnz $ooff, 0, $blk_init ! if ( $out&7 ||
  262. movleu $::size_t_cc, 0, $blk_init ! $len<256 ||
  263. brnz,pn $blk_init, .L${bits}cbc_dec_blk ! $inp==$out)
  264. srl $omask, $ooff, $omask
  265. andcc $len, 16, %g0 ! is number of blocks even?
  266. srlx $len, 4, $len
  267. alignaddrl $out, %g0, $out
  268. bz %icc, .L${bits}_cbc_dec_loop2x
  269. prefetch [$out], 22
  270. .L${bits}_cbc_dec_loop:
  271. ldx [$inp + 0], %o0
  272. brz,pt $ileft, 4f
  273. ldx [$inp + 8], %o1
  274. ldx [$inp + 16], %o2
  275. sllx %o0, $ileft, %o0
  276. srlx %o1, $iright, %g1
  277. sllx %o1, $ileft, %o1
  278. or %g1, %o0, %o0
  279. srlx %o2, $iright, %o2
  280. or %o2, %o1, %o1
  281. 4:
  282. xor %g4, %o0, %o2 ! ^= rk[0]
  283. xor %g5, %o1, %o3
  284. movxtod %o2, %f0
  285. movxtod %o3, %f2
  286. prefetch [$out + 63], 22
  287. prefetch [$inp + 16+63], 20
  288. call _${alg}${bits}_decrypt_1x
  289. add $inp, 16, $inp
  290. fxor %f12, %f0, %f0 ! ^= ivec
  291. fxor %f14, %f2, %f2
  292. movxtod %o0, %f12
  293. movxtod %o1, %f14
  294. brnz,pn $ooff, 2f
  295. sub $len, 1, $len
  296. std %f0, [$out + 0]
  297. std %f2, [$out + 8]
  298. brnz,pt $len, .L${bits}_cbc_dec_loop2x
  299. add $out, 16, $out
  300. ___
  301. $::code.=<<___ if ($::evp);
  302. st %f12, [$ivec + 0]
  303. st %f13, [$ivec + 4]
  304. st %f14, [$ivec + 8]
  305. st %f15, [$ivec + 12]
  306. ___
  307. $::code.=<<___ if (!$::evp);
  308. brnz,pn $ivoff, .L${bits}_cbc_dec_unaligned_ivec
  309. nop
  310. std %f12, [$ivec + 0] ! write out ivec
  311. std %f14, [$ivec + 8]
  312. ___
  313. $::code.=<<___;
  314. .L${bits}_cbc_dec_abort:
  315. ret
  316. restore
  317. .align 16
  318. 2: ldxa [$inp]0x82, %o0 ! avoid read-after-write hazard
  319. ! and ~3x deterioration
  320. ! in inp==out case
  321. faligndata %f0, %f0, %f4 ! handle unaligned output
  322. faligndata %f0, %f2, %f6
  323. faligndata %f2, %f2, %f8
  324. stda %f4, [$out + $omask]0xc0 ! partial store
  325. std %f6, [$out + 8]
  326. add $out, 16, $out
  327. orn %g0, $omask, $omask
  328. stda %f8, [$out + $omask]0xc0 ! partial store
  329. brnz,pt $len, .L${bits}_cbc_dec_loop2x+4
  330. orn %g0, $omask, $omask
  331. ___
  332. $::code.=<<___ if ($::evp);
  333. st %f12, [$ivec + 0]
  334. st %f13, [$ivec + 4]
  335. st %f14, [$ivec + 8]
  336. st %f15, [$ivec + 12]
  337. ___
  338. $::code.=<<___ if (!$::evp);
  339. brnz,pn $ivoff, .L${bits}_cbc_dec_unaligned_ivec
  340. nop
  341. std %f12, [$ivec + 0] ! write out ivec
  342. std %f14, [$ivec + 8]
  343. ___
  344. $::code.=<<___;
  345. ret
  346. restore
  347. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  348. .align 32
  349. .L${bits}_cbc_dec_loop2x:
  350. ldx [$inp + 0], %o0
  351. ldx [$inp + 8], %o1
  352. ldx [$inp + 16], %o2
  353. brz,pt $ileft, 4f
  354. ldx [$inp + 24], %o3
  355. ldx [$inp + 32], %o4
  356. sllx %o0, $ileft, %o0
  357. srlx %o1, $iright, %g1
  358. or %g1, %o0, %o0
  359. sllx %o1, $ileft, %o1
  360. srlx %o2, $iright, %g1
  361. or %g1, %o1, %o1
  362. sllx %o2, $ileft, %o2
  363. srlx %o3, $iright, %g1
  364. or %g1, %o2, %o2
  365. sllx %o3, $ileft, %o3
  366. srlx %o4, $iright, %o4
  367. or %o4, %o3, %o3
  368. 4:
  369. xor %g4, %o0, %o4 ! ^= rk[0]
  370. xor %g5, %o1, %o5
  371. movxtod %o4, %f0
  372. movxtod %o5, %f2
  373. xor %g4, %o2, %o4
  374. xor %g5, %o3, %o5
  375. movxtod %o4, %f4
  376. movxtod %o5, %f6
  377. prefetch [$out + 63], 22
  378. prefetch [$inp + 32+63], 20
  379. call _${alg}${bits}_decrypt_2x
  380. add $inp, 32, $inp
  381. movxtod %o0, %f8
  382. movxtod %o1, %f10
  383. fxor %f12, %f0, %f0 ! ^= ivec
  384. fxor %f14, %f2, %f2
  385. movxtod %o2, %f12
  386. movxtod %o3, %f14
  387. fxor %f8, %f4, %f4
  388. fxor %f10, %f6, %f6
  389. brnz,pn $ooff, 2f
  390. sub $len, 2, $len
  391. std %f0, [$out + 0]
  392. std %f2, [$out + 8]
  393. std %f4, [$out + 16]
  394. std %f6, [$out + 24]
  395. brnz,pt $len, .L${bits}_cbc_dec_loop2x
  396. add $out, 32, $out
  397. ___
  398. $::code.=<<___ if ($::evp);
  399. st %f12, [$ivec + 0]
  400. st %f13, [$ivec + 4]
  401. st %f14, [$ivec + 8]
  402. st %f15, [$ivec + 12]
  403. ___
  404. $::code.=<<___ if (!$::evp);
  405. brnz,pn $ivoff, .L${bits}_cbc_dec_unaligned_ivec
  406. nop
  407. std %f12, [$ivec + 0] ! write out ivec
  408. std %f14, [$ivec + 8]
  409. ___
  410. $::code.=<<___;
  411. ret
  412. restore
  413. .align 16
  414. 2: ldxa [$inp]0x82, %o0 ! avoid read-after-write hazard
  415. ! and ~3x deterioration
  416. ! in inp==out case
  417. faligndata %f0, %f0, %f8 ! handle unaligned output
  418. faligndata %f0, %f2, %f0
  419. faligndata %f2, %f4, %f2
  420. faligndata %f4, %f6, %f4
  421. faligndata %f6, %f6, %f6
  422. stda %f8, [$out + $omask]0xc0 ! partial store
  423. std %f0, [$out + 8]
  424. std %f2, [$out + 16]
  425. std %f4, [$out + 24]
  426. add $out, 32, $out
  427. orn %g0, $omask, $omask
  428. stda %f6, [$out + $omask]0xc0 ! partial store
  429. brnz,pt $len, .L${bits}_cbc_dec_loop2x+4
  430. orn %g0, $omask, $omask
  431. ___
  432. $::code.=<<___ if ($::evp);
  433. st %f12, [$ivec + 0]
  434. st %f13, [$ivec + 4]
  435. st %f14, [$ivec + 8]
  436. st %f15, [$ivec + 12]
  437. ___
  438. $::code.=<<___ if (!$::evp);
  439. brnz,pn $ivoff, .L${bits}_cbc_dec_unaligned_ivec
  440. nop
  441. std %f12, [$ivec + 0] ! write out ivec
  442. std %f14, [$ivec + 8]
  443. ret
  444. restore
  445. .align 16
  446. .L${bits}_cbc_dec_unaligned_ivec:
  447. alignaddrl $ivec, $ivoff, %g0 ! handle unaligned ivec
  448. mov 0xff, $omask
  449. srl $omask, $ivoff, $omask
  450. faligndata %f12, %f12, %f0
  451. faligndata %f12, %f14, %f2
  452. faligndata %f14, %f14, %f4
  453. stda %f0, [$ivec + $omask]0xc0
  454. std %f2, [$ivec + 8]
  455. add $ivec, 16, $ivec
  456. orn %g0, $omask, $omask
  457. stda %f4, [$ivec + $omask]0xc0
  458. ___
  459. $::code.=<<___;
  460. ret
  461. restore
  462. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  463. .align 32
  464. .L${bits}cbc_dec_blk:
  465. add $out, $len, $blk_init
  466. and $blk_init, 63, $blk_init ! tail
  467. sub $len, $blk_init, $len
  468. add $blk_init, 15, $blk_init ! round up to 16n
  469. srlx $len, 4, $len
  470. srl $blk_init, 4, $blk_init
  471. sub $len, 1, $len
  472. add $blk_init, 1, $blk_init
  473. .L${bits}_cbc_dec_blk_loop2x:
  474. ldx [$inp + 0], %o0
  475. ldx [$inp + 8], %o1
  476. ldx [$inp + 16], %o2
  477. brz,pt $ileft, 5f
  478. ldx [$inp + 24], %o3
  479. ldx [$inp + 32], %o4
  480. sllx %o0, $ileft, %o0
  481. srlx %o1, $iright, %g1
  482. or %g1, %o0, %o0
  483. sllx %o1, $ileft, %o1
  484. srlx %o2, $iright, %g1
  485. or %g1, %o1, %o1
  486. sllx %o2, $ileft, %o2
  487. srlx %o3, $iright, %g1
  488. or %g1, %o2, %o2
  489. sllx %o3, $ileft, %o3
  490. srlx %o4, $iright, %o4
  491. or %o4, %o3, %o3
  492. 5:
  493. xor %g4, %o0, %o4 ! ^= rk[0]
  494. xor %g5, %o1, %o5
  495. movxtod %o4, %f0
  496. movxtod %o5, %f2
  497. xor %g4, %o2, %o4
  498. xor %g5, %o3, %o5
  499. movxtod %o4, %f4
  500. movxtod %o5, %f6
  501. prefetch [$inp + 32+63], 20
  502. call _${alg}${bits}_decrypt_2x
  503. add $inp, 32, $inp
  504. subcc $len, 2, $len
  505. movxtod %o0, %f8
  506. movxtod %o1, %f10
  507. fxor %f12, %f0, %f0 ! ^= ivec
  508. fxor %f14, %f2, %f2
  509. movxtod %o2, %f12
  510. movxtod %o3, %f14
  511. fxor %f8, %f4, %f4
  512. fxor %f10, %f6, %f6
  513. stda %f0, [$out]0xe2 ! ASI_BLK_INIT, T4-specific
  514. add $out, 8, $out
  515. stda %f2, [$out]0xe2 ! ASI_BLK_INIT, T4-specific
  516. add $out, 8, $out
  517. stda %f4, [$out]0xe2 ! ASI_BLK_INIT, T4-specific
  518. add $out, 8, $out
  519. stda %f6, [$out]0xe2 ! ASI_BLK_INIT, T4-specific
  520. bgu,pt $::size_t_cc, .L${bits}_cbc_dec_blk_loop2x
  521. add $out, 8, $out
  522. add $blk_init, $len, $len
  523. andcc $len, 1, %g0 ! is number of blocks even?
  524. membar #StoreLoad|#StoreStore
  525. bnz,pt %icc, .L${bits}_cbc_dec_loop
  526. srl $len, 0, $len
  527. brnz,pn $len, .L${bits}_cbc_dec_loop2x
  528. nop
  529. ___
  530. $::code.=<<___ if ($::evp);
  531. st %f12, [$ivec + 0] ! write out ivec
  532. st %f13, [$ivec + 4]
  533. st %f14, [$ivec + 8]
  534. st %f15, [$ivec + 12]
  535. ___
  536. $::code.=<<___ if (!$::evp);
  537. brnz,pn $ivoff, 3b
  538. nop
  539. std %f12, [$ivec + 0] ! write out ivec
  540. std %f14, [$ivec + 8]
  541. ___
  542. $::code.=<<___;
  543. ret
  544. restore
  545. .type ${alg}${bits}_t4_cbc_decrypt,#function
  546. .size ${alg}${bits}_t4_cbc_decrypt,.-${alg}${bits}_t4_cbc_decrypt
  547. ___
  548. }
  549. sub alg_ctr32_implement {
  550. my ($alg,$bits) = @_;
  551. $::code.=<<___;
  552. .globl ${alg}${bits}_t4_ctr32_encrypt
  553. .align 32
  554. ${alg}${bits}_t4_ctr32_encrypt:
  555. save %sp, -$::frame, %sp
  556. prefetch [$inp], 20
  557. prefetch [$inp + 63], 20
  558. call _${alg}${bits}_load_enckey
  559. sllx $len, 4, $len
  560. ld [$ivec + 0], %l4 ! counter
  561. ld [$ivec + 4], %l5
  562. ld [$ivec + 8], %l6
  563. ld [$ivec + 12], %l7
  564. sllx %l4, 32, %o5
  565. or %l5, %o5, %o5
  566. sllx %l6, 32, %g1
  567. xor %o5, %g4, %g4 ! ^= rk[0]
  568. xor %g1, %g5, %g5
  569. movxtod %g4, %f14 ! most significant 64 bits
  570. sub $inp, $out, $blk_init ! $inp!=$out
  571. and $inp, 7, $ileft
  572. andn $inp, 7, $inp
  573. sll $ileft, 3, $ileft
  574. mov 64, $iright
  575. mov 0xff, $omask
  576. sub $iright, $ileft, $iright
  577. and $out, 7, $ooff
  578. cmp $len, 255
  579. movrnz $ooff, 0, $blk_init ! if ( $out&7 ||
  580. movleu $::size_t_cc, 0, $blk_init ! $len<256 ||
  581. brnz,pn $blk_init, .L${bits}_ctr32_blk ! $inp==$out)
  582. srl $omask, $ooff, $omask
  583. andcc $len, 16, %g0 ! is number of blocks even?
  584. alignaddrl $out, %g0, $out
  585. bz %icc, .L${bits}_ctr32_loop2x
  586. srlx $len, 4, $len
  587. .L${bits}_ctr32_loop:
  588. ldx [$inp + 0], %o0
  589. brz,pt $ileft, 4f
  590. ldx [$inp + 8], %o1
  591. ldx [$inp + 16], %o2
  592. sllx %o0, $ileft, %o0
  593. srlx %o1, $iright, %g1
  594. sllx %o1, $ileft, %o1
  595. or %g1, %o0, %o0
  596. srlx %o2, $iright, %o2
  597. or %o2, %o1, %o1
  598. 4:
  599. xor %g5, %l7, %g1 ! ^= rk[0]
  600. add %l7, 1, %l7
  601. movxtod %g1, %f2
  602. srl %l7, 0, %l7 ! clruw
  603. prefetch [$out + 63], 22
  604. prefetch [$inp + 16+63], 20
  605. ___
  606. $::code.=<<___ if ($alg eq "aes");
  607. aes_eround01 %f16, %f14, %f2, %f4
  608. aes_eround23 %f18, %f14, %f2, %f2
  609. ___
  610. $::code.=<<___ if ($alg eq "cmll");
  611. camellia_f %f16, %f2, %f14, %f2
  612. camellia_f %f18, %f14, %f2, %f0
  613. ___
  614. $::code.=<<___;
  615. call _${alg}${bits}_encrypt_1x+8
  616. add $inp, 16, $inp
  617. movxtod %o0, %f10
  618. movxtod %o1, %f12
  619. fxor %f10, %f0, %f0 ! ^= inp
  620. fxor %f12, %f2, %f2
  621. brnz,pn $ooff, 2f
  622. sub $len, 1, $len
  623. std %f0, [$out + 0]
  624. std %f2, [$out + 8]
  625. brnz,pt $len, .L${bits}_ctr32_loop2x
  626. add $out, 16, $out
  627. ret
  628. restore
  629. .align 16
  630. 2: ldxa [$inp]0x82, %o0 ! avoid read-after-write hazard
  631. ! and ~3x deterioration
  632. ! in inp==out case
  633. faligndata %f0, %f0, %f4 ! handle unaligned output
  634. faligndata %f0, %f2, %f6
  635. faligndata %f2, %f2, %f8
  636. stda %f4, [$out + $omask]0xc0 ! partial store
  637. std %f6, [$out + 8]
  638. add $out, 16, $out
  639. orn %g0, $omask, $omask
  640. stda %f8, [$out + $omask]0xc0 ! partial store
  641. brnz,pt $len, .L${bits}_ctr32_loop2x+4
  642. orn %g0, $omask, $omask
  643. ret
  644. restore
  645. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  646. .align 32
  647. .L${bits}_ctr32_loop2x:
  648. ldx [$inp + 0], %o0
  649. ldx [$inp + 8], %o1
  650. ldx [$inp + 16], %o2
  651. brz,pt $ileft, 4f
  652. ldx [$inp + 24], %o3
  653. ldx [$inp + 32], %o4
  654. sllx %o0, $ileft, %o0
  655. srlx %o1, $iright, %g1
  656. or %g1, %o0, %o0
  657. sllx %o1, $ileft, %o1
  658. srlx %o2, $iright, %g1
  659. or %g1, %o1, %o1
  660. sllx %o2, $ileft, %o2
  661. srlx %o3, $iright, %g1
  662. or %g1, %o2, %o2
  663. sllx %o3, $ileft, %o3
  664. srlx %o4, $iright, %o4
  665. or %o4, %o3, %o3
  666. 4:
  667. xor %g5, %l7, %g1 ! ^= rk[0]
  668. add %l7, 1, %l7
  669. movxtod %g1, %f2
  670. srl %l7, 0, %l7 ! clruw
  671. xor %g5, %l7, %g1
  672. add %l7, 1, %l7
  673. movxtod %g1, %f6
  674. srl %l7, 0, %l7 ! clruw
  675. prefetch [$out + 63], 22
  676. prefetch [$inp + 32+63], 20
  677. ___
  678. $::code.=<<___ if ($alg eq "aes");
  679. aes_eround01 %f16, %f14, %f2, %f8
  680. aes_eround23 %f18, %f14, %f2, %f2
  681. aes_eround01 %f16, %f14, %f6, %f10
  682. aes_eround23 %f18, %f14, %f6, %f6
  683. ___
  684. $::code.=<<___ if ($alg eq "cmll");
  685. camellia_f %f16, %f2, %f14, %f2
  686. camellia_f %f16, %f6, %f14, %f6
  687. camellia_f %f18, %f14, %f2, %f0
  688. camellia_f %f18, %f14, %f6, %f4
  689. ___
  690. $::code.=<<___;
  691. call _${alg}${bits}_encrypt_2x+16
  692. add $inp, 32, $inp
  693. movxtod %o0, %f8
  694. movxtod %o1, %f10
  695. movxtod %o2, %f12
  696. fxor %f8, %f0, %f0 ! ^= inp
  697. movxtod %o3, %f8
  698. fxor %f10, %f2, %f2
  699. fxor %f12, %f4, %f4
  700. fxor %f8, %f6, %f6
  701. brnz,pn $ooff, 2f
  702. sub $len, 2, $len
  703. std %f0, [$out + 0]
  704. std %f2, [$out + 8]
  705. std %f4, [$out + 16]
  706. std %f6, [$out + 24]
  707. brnz,pt $len, .L${bits}_ctr32_loop2x
  708. add $out, 32, $out
  709. ret
  710. restore
  711. .align 16
  712. 2: ldxa [$inp]0x82, %o0 ! avoid read-after-write hazard
  713. ! and ~3x deterioration
  714. ! in inp==out case
  715. faligndata %f0, %f0, %f8 ! handle unaligned output
  716. faligndata %f0, %f2, %f0
  717. faligndata %f2, %f4, %f2
  718. faligndata %f4, %f6, %f4
  719. faligndata %f6, %f6, %f6
  720. stda %f8, [$out + $omask]0xc0 ! partial store
  721. std %f0, [$out + 8]
  722. std %f2, [$out + 16]
  723. std %f4, [$out + 24]
  724. add $out, 32, $out
  725. orn %g0, $omask, $omask
  726. stda %f6, [$out + $omask]0xc0 ! partial store
  727. brnz,pt $len, .L${bits}_ctr32_loop2x+4
  728. orn %g0, $omask, $omask
  729. ret
  730. restore
  731. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  732. .align 32
  733. .L${bits}_ctr32_blk:
  734. add $out, $len, $blk_init
  735. and $blk_init, 63, $blk_init ! tail
  736. sub $len, $blk_init, $len
  737. add $blk_init, 15, $blk_init ! round up to 16n
  738. srlx $len, 4, $len
  739. srl $blk_init, 4, $blk_init
  740. sub $len, 1, $len
  741. add $blk_init, 1, $blk_init
  742. .L${bits}_ctr32_blk_loop2x:
  743. ldx [$inp + 0], %o0
  744. ldx [$inp + 8], %o1
  745. ldx [$inp + 16], %o2
  746. brz,pt $ileft, 5f
  747. ldx [$inp + 24], %o3
  748. ldx [$inp + 32], %o4
  749. sllx %o0, $ileft, %o0
  750. srlx %o1, $iright, %g1
  751. or %g1, %o0, %o0
  752. sllx %o1, $ileft, %o1
  753. srlx %o2, $iright, %g1
  754. or %g1, %o1, %o1
  755. sllx %o2, $ileft, %o2
  756. srlx %o3, $iright, %g1
  757. or %g1, %o2, %o2
  758. sllx %o3, $ileft, %o3
  759. srlx %o4, $iright, %o4
  760. or %o4, %o3, %o3
  761. 5:
  762. xor %g5, %l7, %g1 ! ^= rk[0]
  763. add %l7, 1, %l7
  764. movxtod %g1, %f2
  765. srl %l7, 0, %l7 ! clruw
  766. xor %g5, %l7, %g1
  767. add %l7, 1, %l7
  768. movxtod %g1, %f6
  769. srl %l7, 0, %l7 ! clruw
  770. prefetch [$inp + 32+63], 20
  771. ___
  772. $::code.=<<___ if ($alg eq "aes");
  773. aes_eround01 %f16, %f14, %f2, %f8
  774. aes_eround23 %f18, %f14, %f2, %f2
  775. aes_eround01 %f16, %f14, %f6, %f10
  776. aes_eround23 %f18, %f14, %f6, %f6
  777. ___
  778. $::code.=<<___ if ($alg eq "cmll");
  779. camellia_f %f16, %f2, %f14, %f2
  780. camellia_f %f16, %f6, %f14, %f6
  781. camellia_f %f18, %f14, %f2, %f0
  782. camellia_f %f18, %f14, %f6, %f4
  783. ___
  784. $::code.=<<___;
  785. call _${alg}${bits}_encrypt_2x+16
  786. add $inp, 32, $inp
  787. subcc $len, 2, $len
  788. movxtod %o0, %f8
  789. movxtod %o1, %f10
  790. movxtod %o2, %f12
  791. fxor %f8, %f0, %f0 ! ^= inp
  792. movxtod %o3, %f8
  793. fxor %f10, %f2, %f2
  794. fxor %f12, %f4, %f4
  795. fxor %f8, %f6, %f6
  796. stda %f0, [$out]0xe2 ! ASI_BLK_INIT, T4-specific
  797. add $out, 8, $out
  798. stda %f2, [$out]0xe2 ! ASI_BLK_INIT, T4-specific
  799. add $out, 8, $out
  800. stda %f4, [$out]0xe2 ! ASI_BLK_INIT, T4-specific
  801. add $out, 8, $out
  802. stda %f6, [$out]0xe2 ! ASI_BLK_INIT, T4-specific
  803. bgu,pt $::size_t_cc, .L${bits}_ctr32_blk_loop2x
  804. add $out, 8, $out
  805. add $blk_init, $len, $len
  806. andcc $len, 1, %g0 ! is number of blocks even?
  807. membar #StoreLoad|#StoreStore
  808. bnz,pt %icc, .L${bits}_ctr32_loop
  809. srl $len, 0, $len
  810. brnz,pn $len, .L${bits}_ctr32_loop2x
  811. nop
  812. ret
  813. restore
  814. .type ${alg}${bits}_t4_ctr32_encrypt,#function
  815. .size ${alg}${bits}_t4_ctr32_encrypt,.-${alg}${bits}_t4_ctr32_encrypt
  816. ___
  817. }
  818. sub alg_xts_implement {
  819. my ($alg,$bits,$dir) = @_;
  820. my ($inp,$out,$len,$key1,$key2,$ivec)=map("%i$_",(0..5));
  821. my $rem=$ivec;
  822. $::code.=<<___;
  823. .globl ${alg}${bits}_t4_xts_${dir}crypt
  824. .align 32
  825. ${alg}${bits}_t4_xts_${dir}crypt:
  826. save %sp, -$::frame-16, %sp
  827. mov $ivec, %o0
  828. add %fp, $::bias-16, %o1
  829. call ${alg}_t4_encrypt
  830. mov $key2, %o2
  831. add %fp, $::bias-16, %l7
  832. ldxa [%l7]0x88, %g2
  833. add %fp, $::bias-8, %l7
  834. ldxa [%l7]0x88, %g3 ! %g3:%g2 is tweak
  835. sethi %hi(0x76543210), %l7
  836. or %l7, %lo(0x76543210), %l7
  837. bmask %l7, %g0, %g0 ! byte swap mask
  838. prefetch [$inp], 20
  839. prefetch [$inp + 63], 20
  840. call _${alg}${bits}_load_${dir}ckey
  841. and $len, 15, $rem
  842. and $len, -16, $len
  843. ___
  844. $code.=<<___ if ($dir eq "de");
  845. mov 0, %l7
  846. movrnz $rem, 16, %l7
  847. sub $len, %l7, $len
  848. ___
  849. $code.=<<___;
  850. sub $inp, $out, $blk_init ! $inp!=$out
  851. and $inp, 7, $ileft
  852. andn $inp, 7, $inp
  853. sll $ileft, 3, $ileft
  854. mov 64, $iright
  855. mov 0xff, $omask
  856. sub $iright, $ileft, $iright
  857. and $out, 7, $ooff
  858. cmp $len, 255
  859. movrnz $ooff, 0, $blk_init ! if ( $out&7 ||
  860. movleu $::size_t_cc, 0, $blk_init ! $len<256 ||
  861. brnz,pn $blk_init, .L${bits}_xts_${dir}blk ! $inp==$out)
  862. srl $omask, $ooff, $omask
  863. andcc $len, 16, %g0 ! is number of blocks even?
  864. ___
  865. $code.=<<___ if ($dir eq "de");
  866. brz,pn $len, .L${bits}_xts_${dir}steal
  867. ___
  868. $code.=<<___;
  869. alignaddrl $out, %g0, $out
  870. bz %icc, .L${bits}_xts_${dir}loop2x
  871. srlx $len, 4, $len
  872. .L${bits}_xts_${dir}loop:
  873. ldx [$inp + 0], %o0
  874. brz,pt $ileft, 4f
  875. ldx [$inp + 8], %o1
  876. ldx [$inp + 16], %o2
  877. sllx %o0, $ileft, %o0
  878. srlx %o1, $iright, %g1
  879. sllx %o1, $ileft, %o1
  880. or %g1, %o0, %o0
  881. srlx %o2, $iright, %o2
  882. or %o2, %o1, %o1
  883. 4:
  884. movxtod %g2, %f12
  885. movxtod %g3, %f14
  886. bshuffle %f12, %f12, %f12
  887. bshuffle %f14, %f14, %f14
  888. xor %g4, %o0, %o0 ! ^= rk[0]
  889. xor %g5, %o1, %o1
  890. movxtod %o0, %f0
  891. movxtod %o1, %f2
  892. fxor %f12, %f0, %f0 ! ^= tweak[0]
  893. fxor %f14, %f2, %f2
  894. prefetch [$out + 63], 22
  895. prefetch [$inp + 16+63], 20
  896. call _${alg}${bits}_${dir}crypt_1x
  897. add $inp, 16, $inp
  898. fxor %f12, %f0, %f0 ! ^= tweak[0]
  899. fxor %f14, %f2, %f2
  900. srax %g3, 63, %l7 ! next tweak value
  901. addcc %g2, %g2, %g2
  902. and %l7, 0x87, %l7
  903. addxc %g3, %g3, %g3
  904. xor %l7, %g2, %g2
  905. brnz,pn $ooff, 2f
  906. sub $len, 1, $len
  907. std %f0, [$out + 0]
  908. std %f2, [$out + 8]
  909. brnz,pt $len, .L${bits}_xts_${dir}loop2x
  910. add $out, 16, $out
  911. brnz,pn $rem, .L${bits}_xts_${dir}steal
  912. nop
  913. ret
  914. restore
  915. .align 16
  916. 2: ldxa [$inp]0x82, %o0 ! avoid read-after-write hazard
  917. ! and ~3x deterioration
  918. ! in inp==out case
  919. faligndata %f0, %f0, %f4 ! handle unaligned output
  920. faligndata %f0, %f2, %f6
  921. faligndata %f2, %f2, %f8
  922. stda %f4, [$out + $omask]0xc0 ! partial store
  923. std %f6, [$out + 8]
  924. add $out, 16, $out
  925. orn %g0, $omask, $omask
  926. stda %f8, [$out + $omask]0xc0 ! partial store
  927. brnz,pt $len, .L${bits}_xts_${dir}loop2x+4
  928. orn %g0, $omask, $omask
  929. brnz,pn $rem, .L${bits}_xts_${dir}steal
  930. nop
  931. ret
  932. restore
  933. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  934. .align 32
  935. .L${bits}_xts_${dir}loop2x:
  936. ldx [$inp + 0], %o0
  937. ldx [$inp + 8], %o1
  938. ldx [$inp + 16], %o2
  939. brz,pt $ileft, 4f
  940. ldx [$inp + 24], %o3
  941. ldx [$inp + 32], %o4
  942. sllx %o0, $ileft, %o0
  943. srlx %o1, $iright, %g1
  944. or %g1, %o0, %o0
  945. sllx %o1, $ileft, %o1
  946. srlx %o2, $iright, %g1
  947. or %g1, %o1, %o1
  948. sllx %o2, $ileft, %o2
  949. srlx %o3, $iright, %g1
  950. or %g1, %o2, %o2
  951. sllx %o3, $ileft, %o3
  952. srlx %o4, $iright, %o4
  953. or %o4, %o3, %o3
  954. 4:
  955. movxtod %g2, %f12
  956. movxtod %g3, %f14
  957. bshuffle %f12, %f12, %f12
  958. bshuffle %f14, %f14, %f14
  959. srax %g3, 63, %l7 ! next tweak value
  960. addcc %g2, %g2, %g2
  961. and %l7, 0x87, %l7
  962. addxc %g3, %g3, %g3
  963. xor %l7, %g2, %g2
  964. movxtod %g2, %f8
  965. movxtod %g3, %f10
  966. bshuffle %f8, %f8, %f8
  967. bshuffle %f10, %f10, %f10
  968. xor %g4, %o0, %o0 ! ^= rk[0]
  969. xor %g5, %o1, %o1
  970. xor %g4, %o2, %o2 ! ^= rk[0]
  971. xor %g5, %o3, %o3
  972. movxtod %o0, %f0
  973. movxtod %o1, %f2
  974. movxtod %o2, %f4
  975. movxtod %o3, %f6
  976. fxor %f12, %f0, %f0 ! ^= tweak[0]
  977. fxor %f14, %f2, %f2
  978. fxor %f8, %f4, %f4 ! ^= tweak[0]
  979. fxor %f10, %f6, %f6
  980. prefetch [$out + 63], 22
  981. prefetch [$inp + 32+63], 20
  982. call _${alg}${bits}_${dir}crypt_2x
  983. add $inp, 32, $inp
  984. movxtod %g2, %f8
  985. movxtod %g3, %f10
  986. srax %g3, 63, %l7 ! next tweak value
  987. addcc %g2, %g2, %g2
  988. and %l7, 0x87, %l7
  989. addxc %g3, %g3, %g3
  990. xor %l7, %g2, %g2
  991. bshuffle %f8, %f8, %f8
  992. bshuffle %f10, %f10, %f10
  993. fxor %f12, %f0, %f0 ! ^= tweak[0]
  994. fxor %f14, %f2, %f2
  995. fxor %f8, %f4, %f4
  996. fxor %f10, %f6, %f6
  997. brnz,pn $ooff, 2f
  998. sub $len, 2, $len
  999. std %f0, [$out + 0]
  1000. std %f2, [$out + 8]
  1001. std %f4, [$out + 16]
  1002. std %f6, [$out + 24]
  1003. brnz,pt $len, .L${bits}_xts_${dir}loop2x
  1004. add $out, 32, $out
  1005. fsrc2 %f4, %f0
  1006. fsrc2 %f6, %f2
  1007. brnz,pn $rem, .L${bits}_xts_${dir}steal
  1008. nop
  1009. ret
  1010. restore
  1011. .align 16
  1012. 2: ldxa [$inp]0x82, %o0 ! avoid read-after-write hazard
  1013. ! and ~3x deterioration
  1014. ! in inp==out case
  1015. faligndata %f0, %f0, %f8 ! handle unaligned output
  1016. faligndata %f0, %f2, %f10
  1017. faligndata %f2, %f4, %f12
  1018. faligndata %f4, %f6, %f14
  1019. faligndata %f6, %f6, %f0
  1020. stda %f8, [$out + $omask]0xc0 ! partial store
  1021. std %f10, [$out + 8]
  1022. std %f12, [$out + 16]
  1023. std %f14, [$out + 24]
  1024. add $out, 32, $out
  1025. orn %g0, $omask, $omask
  1026. stda %f0, [$out + $omask]0xc0 ! partial store
  1027. brnz,pt $len, .L${bits}_xts_${dir}loop2x+4
  1028. orn %g0, $omask, $omask
  1029. fsrc2 %f4, %f0
  1030. fsrc2 %f6, %f2
  1031. brnz,pn $rem, .L${bits}_xts_${dir}steal
  1032. nop
  1033. ret
  1034. restore
  1035. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1036. .align 32
  1037. .L${bits}_xts_${dir}blk:
  1038. add $out, $len, $blk_init
  1039. and $blk_init, 63, $blk_init ! tail
  1040. sub $len, $blk_init, $len
  1041. add $blk_init, 15, $blk_init ! round up to 16n
  1042. srlx $len, 4, $len
  1043. srl $blk_init, 4, $blk_init
  1044. sub $len, 1, $len
  1045. add $blk_init, 1, $blk_init
  1046. .L${bits}_xts_${dir}blk2x:
  1047. ldx [$inp + 0], %o0
  1048. ldx [$inp + 8], %o1
  1049. ldx [$inp + 16], %o2
  1050. brz,pt $ileft, 5f
  1051. ldx [$inp + 24], %o3
  1052. ldx [$inp + 32], %o4
  1053. sllx %o0, $ileft, %o0
  1054. srlx %o1, $iright, %g1
  1055. or %g1, %o0, %o0
  1056. sllx %o1, $ileft, %o1
  1057. srlx %o2, $iright, %g1
  1058. or %g1, %o1, %o1
  1059. sllx %o2, $ileft, %o2
  1060. srlx %o3, $iright, %g1
  1061. or %g1, %o2, %o2
  1062. sllx %o3, $ileft, %o3
  1063. srlx %o4, $iright, %o4
  1064. or %o4, %o3, %o3
  1065. 5:
  1066. movxtod %g2, %f12
  1067. movxtod %g3, %f14
  1068. bshuffle %f12, %f12, %f12
  1069. bshuffle %f14, %f14, %f14
  1070. srax %g3, 63, %l7 ! next tweak value
  1071. addcc %g2, %g2, %g2
  1072. and %l7, 0x87, %l7
  1073. addxc %g3, %g3, %g3
  1074. xor %l7, %g2, %g2
  1075. movxtod %g2, %f8
  1076. movxtod %g3, %f10
  1077. bshuffle %f8, %f8, %f8
  1078. bshuffle %f10, %f10, %f10
  1079. xor %g4, %o0, %o0 ! ^= rk[0]
  1080. xor %g5, %o1, %o1
  1081. xor %g4, %o2, %o2 ! ^= rk[0]
  1082. xor %g5, %o3, %o3
  1083. movxtod %o0, %f0
  1084. movxtod %o1, %f2
  1085. movxtod %o2, %f4
  1086. movxtod %o3, %f6
  1087. fxor %f12, %f0, %f0 ! ^= tweak[0]
  1088. fxor %f14, %f2, %f2
  1089. fxor %f8, %f4, %f4 ! ^= tweak[0]
  1090. fxor %f10, %f6, %f6
  1091. prefetch [$inp + 32+63], 20
  1092. call _${alg}${bits}_${dir}crypt_2x
  1093. add $inp, 32, $inp
  1094. movxtod %g2, %f8
  1095. movxtod %g3, %f10
  1096. srax %g3, 63, %l7 ! next tweak value
  1097. addcc %g2, %g2, %g2
  1098. and %l7, 0x87, %l7
  1099. addxc %g3, %g3, %g3
  1100. xor %l7, %g2, %g2
  1101. bshuffle %f8, %f8, %f8
  1102. bshuffle %f10, %f10, %f10
  1103. fxor %f12, %f0, %f0 ! ^= tweak[0]
  1104. fxor %f14, %f2, %f2
  1105. fxor %f8, %f4, %f4
  1106. fxor %f10, %f6, %f6
  1107. subcc $len, 2, $len
  1108. stda %f0, [$out]0xe2 ! ASI_BLK_INIT, T4-specific
  1109. add $out, 8, $out
  1110. stda %f2, [$out]0xe2 ! ASI_BLK_INIT, T4-specific
  1111. add $out, 8, $out
  1112. stda %f4, [$out]0xe2 ! ASI_BLK_INIT, T4-specific
  1113. add $out, 8, $out
  1114. stda %f6, [$out]0xe2 ! ASI_BLK_INIT, T4-specific
  1115. bgu,pt $::size_t_cc, .L${bits}_xts_${dir}blk2x
  1116. add $out, 8, $out
  1117. add $blk_init, $len, $len
  1118. andcc $len, 1, %g0 ! is number of blocks even?
  1119. membar #StoreLoad|#StoreStore
  1120. bnz,pt %icc, .L${bits}_xts_${dir}loop
  1121. srl $len, 0, $len
  1122. brnz,pn $len, .L${bits}_xts_${dir}loop2x
  1123. nop
  1124. fsrc2 %f4, %f0
  1125. fsrc2 %f6, %f2
  1126. brnz,pn $rem, .L${bits}_xts_${dir}steal
  1127. nop
  1128. ret
  1129. restore
  1130. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1131. ___
  1132. $code.=<<___ if ($dir eq "en");
  1133. .align 32
  1134. .L${bits}_xts_${dir}steal:
  1135. std %f0, [%fp + $::bias-16] ! copy of output
  1136. std %f2, [%fp + $::bias-8]
  1137. srl $ileft, 3, $ileft
  1138. add %fp, $::bias-16, %l7
  1139. add $inp, $ileft, $inp ! original $inp+$len&-15
  1140. add $out, $ooff, $out ! original $out+$len&-15
  1141. mov 0, $ileft
  1142. nop ! align
  1143. .L${bits}_xts_${dir}stealing:
  1144. ldub [$inp + $ileft], %o0
  1145. ldub [%l7 + $ileft], %o1
  1146. dec $rem
  1147. stb %o0, [%l7 + $ileft]
  1148. stb %o1, [$out + $ileft]
  1149. brnz $rem, .L${bits}_xts_${dir}stealing
  1150. inc $ileft
  1151. mov %l7, $inp
  1152. sub $out, 16, $out
  1153. mov 0, $ileft
  1154. sub $out, $ooff, $out
  1155. ba .L${bits}_xts_${dir}loop ! one more time
  1156. mov 1, $len ! $rem is 0
  1157. ___
  1158. $code.=<<___ if ($dir eq "de");
  1159. .align 32
  1160. .L${bits}_xts_${dir}steal:
  1161. ldx [$inp + 0], %o0
  1162. brz,pt $ileft, 8f
  1163. ldx [$inp + 8], %o1
  1164. ldx [$inp + 16], %o2
  1165. sllx %o0, $ileft, %o0
  1166. srlx %o1, $iright, %g1
  1167. sllx %o1, $ileft, %o1
  1168. or %g1, %o0, %o0
  1169. srlx %o2, $iright, %o2
  1170. or %o2, %o1, %o1
  1171. 8:
  1172. srax %g3, 63, %l7 ! next tweak value
  1173. addcc %g2, %g2, %o2
  1174. and %l7, 0x87, %l7
  1175. addxc %g3, %g3, %o3
  1176. xor %l7, %o2, %o2
  1177. movxtod %o2, %f12
  1178. movxtod %o3, %f14
  1179. bshuffle %f12, %f12, %f12
  1180. bshuffle %f14, %f14, %f14
  1181. xor %g4, %o0, %o0 ! ^= rk[0]
  1182. xor %g5, %o1, %o1
  1183. movxtod %o0, %f0
  1184. movxtod %o1, %f2
  1185. fxor %f12, %f0, %f0 ! ^= tweak[0]
  1186. fxor %f14, %f2, %f2
  1187. call _${alg}${bits}_${dir}crypt_1x
  1188. add $inp, 16, $inp
  1189. fxor %f12, %f0, %f0 ! ^= tweak[0]
  1190. fxor %f14, %f2, %f2
  1191. std %f0, [%fp + $::bias-16]
  1192. std %f2, [%fp + $::bias-8]
  1193. srl $ileft, 3, $ileft
  1194. add %fp, $::bias-16, %l7
  1195. add $inp, $ileft, $inp ! original $inp+$len&-15
  1196. add $out, $ooff, $out ! original $out+$len&-15
  1197. mov 0, $ileft
  1198. add $out, 16, $out
  1199. nop ! align
  1200. .L${bits}_xts_${dir}stealing:
  1201. ldub [$inp + $ileft], %o0
  1202. ldub [%l7 + $ileft], %o1
  1203. dec $rem
  1204. stb %o0, [%l7 + $ileft]
  1205. stb %o1, [$out + $ileft]
  1206. brnz $rem, .L${bits}_xts_${dir}stealing
  1207. inc $ileft
  1208. mov %l7, $inp
  1209. sub $out, 16, $out
  1210. mov 0, $ileft
  1211. sub $out, $ooff, $out
  1212. ba .L${bits}_xts_${dir}loop ! one more time
  1213. mov 1, $len ! $rem is 0
  1214. ___
  1215. $code.=<<___;
  1216. ret
  1217. restore
  1218. .type ${alg}${bits}_t4_xts_${dir}crypt,#function
  1219. .size ${alg}${bits}_t4_xts_${dir}crypt,.-${alg}${bits}_t4_xts_${dir}crypt
  1220. ___
  1221. }
  1222. # Purpose of these subroutines is to explicitly encode VIS instructions,
  1223. # so that one can compile the module without having to specify VIS
  1224. # extensions on compiler command line, e.g. -xarch=v9 vs. -xarch=v9a.
  1225. # Idea is to reserve for option to produce "universal" binary and let
  1226. # programmer detect if current CPU is VIS capable at run-time.
  1227. sub unvis {
  1228. my ($mnemonic,$rs1,$rs2,$rd)=@_;
  1229. my ($ref,$opf);
  1230. my %visopf = ( "faligndata" => 0x048,
  1231. "bshuffle" => 0x04c,
  1232. "fnot2" => 0x066,
  1233. "fxor" => 0x06c,
  1234. "fsrc2" => 0x078 );
  1235. $ref = "$mnemonic\t$rs1,$rs2,$rd";
  1236. if ($opf=$visopf{$mnemonic}) {
  1237. foreach ($rs1,$rs2,$rd) {
  1238. return $ref if (!/%f([0-9]{1,2})/);
  1239. $_=$1;
  1240. if ($1>=32) {
  1241. return $ref if ($1&1);
  1242. # re-encode for upper double register addressing
  1243. $_=($1|$1>>5)&31;
  1244. }
  1245. }
  1246. return sprintf ".word\t0x%08x !%s",
  1247. 0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
  1248. $ref;
  1249. } else {
  1250. return $ref;
  1251. }
  1252. }
  1253. sub unvis3 {
  1254. my ($mnemonic,$rs1,$rs2,$rd)=@_;
  1255. my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
  1256. my ($ref,$opf);
  1257. my %visopf = ( "addxc" => 0x011,
  1258. "addxccc" => 0x013,
  1259. "umulxhi" => 0x016,
  1260. "alignaddr" => 0x018,
  1261. "bmask" => 0x019,
  1262. "alignaddrl" => 0x01a );
  1263. $ref = "$mnemonic\t$rs1,$rs2,$rd";
  1264. if ($opf=$visopf{$mnemonic}) {
  1265. foreach ($rs1,$rs2,$rd) {
  1266. return $ref if (!/%([goli])([0-9])/);
  1267. $_=$bias{$1}+$2;
  1268. }
  1269. return sprintf ".word\t0x%08x !%s",
  1270. 0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
  1271. $ref;
  1272. } else {
  1273. return $ref;
  1274. }
  1275. }
  1276. sub unaes_round { # 4-argument instructions
  1277. my ($mnemonic,$rs1,$rs2,$rs3,$rd)=@_;
  1278. my ($ref,$opf);
  1279. my %aesopf = ( "aes_eround01" => 0,
  1280. "aes_eround23" => 1,
  1281. "aes_dround01" => 2,
  1282. "aes_dround23" => 3,
  1283. "aes_eround01_l"=> 4,
  1284. "aes_eround23_l"=> 5,
  1285. "aes_dround01_l"=> 6,
  1286. "aes_dround23_l"=> 7,
  1287. "aes_kexpand1" => 8 );
  1288. $ref = "$mnemonic\t$rs1,$rs2,$rs3,$rd";
  1289. if (defined($opf=$aesopf{$mnemonic})) {
  1290. $rs3 = ($rs3 =~ /%f([0-6]*[02468])/) ? (($1|$1>>5)&31) : $rs3;
  1291. foreach ($rs1,$rs2,$rd) {
  1292. return $ref if (!/%f([0-9]{1,2})/);
  1293. $_=$1;
  1294. if ($1>=32) {
  1295. return $ref if ($1&1);
  1296. # re-encode for upper double register addressing
  1297. $_=($1|$1>>5)&31;
  1298. }
  1299. }
  1300. return sprintf ".word\t0x%08x !%s",
  1301. 2<<30|$rd<<25|0x19<<19|$rs1<<14|$rs3<<9|$opf<<5|$rs2,
  1302. $ref;
  1303. } else {
  1304. return $ref;
  1305. }
  1306. }
  1307. sub unaes_kexpand { # 3-argument instructions
  1308. my ($mnemonic,$rs1,$rs2,$rd)=@_;
  1309. my ($ref,$opf);
  1310. my %aesopf = ( "aes_kexpand0" => 0x130,
  1311. "aes_kexpand2" => 0x131 );
  1312. $ref = "$mnemonic\t$rs1,$rs2,$rd";
  1313. if (defined($opf=$aesopf{$mnemonic})) {
  1314. foreach ($rs1,$rs2,$rd) {
  1315. return $ref if (!/%f([0-9]{1,2})/);
  1316. $_=$1;
  1317. if ($1>=32) {
  1318. return $ref if ($1&1);
  1319. # re-encode for upper double register addressing
  1320. $_=($1|$1>>5)&31;
  1321. }
  1322. }
  1323. return sprintf ".word\t0x%08x !%s",
  1324. 2<<30|$rd<<25|0x36<<19|$rs1<<14|$opf<<5|$rs2,
  1325. $ref;
  1326. } else {
  1327. return $ref;
  1328. }
  1329. }
  1330. sub uncamellia_f { # 4-argument instructions
  1331. my ($mnemonic,$rs1,$rs2,$rs3,$rd)=@_;
  1332. my ($ref,$opf);
  1333. $ref = "$mnemonic\t$rs1,$rs2,$rs3,$rd";
  1334. if (1) {
  1335. $rs3 = ($rs3 =~ /%f([0-6]*[02468])/) ? (($1|$1>>5)&31) : $rs3;
  1336. foreach ($rs1,$rs2,$rd) {
  1337. return $ref if (!/%f([0-9]{1,2})/);
  1338. $_=$1;
  1339. if ($1>=32) {
  1340. return $ref if ($1&1);
  1341. # re-encode for upper double register addressing
  1342. $_=($1|$1>>5)&31;
  1343. }
  1344. }
  1345. return sprintf ".word\t0x%08x !%s",
  1346. 2<<30|$rd<<25|0x19<<19|$rs1<<14|$rs3<<9|0xc<<5|$rs2,
  1347. $ref;
  1348. } else {
  1349. return $ref;
  1350. }
  1351. }
  1352. sub uncamellia3 { # 3-argument instructions
  1353. my ($mnemonic,$rs1,$rs2,$rd)=@_;
  1354. my ($ref,$opf);
  1355. my %cmllopf = ( "camellia_fl" => 0x13c,
  1356. "camellia_fli" => 0x13d );
  1357. $ref = "$mnemonic\t$rs1,$rs2,$rd";
  1358. if (defined($opf=$cmllopf{$mnemonic})) {
  1359. foreach ($rs1,$rs2,$rd) {
  1360. return $ref if (!/%f([0-9]{1,2})/);
  1361. $_=$1;
  1362. if ($1>=32) {
  1363. return $ref if ($1&1);
  1364. # re-encode for upper double register addressing
  1365. $_=($1|$1>>5)&31;
  1366. }
  1367. }
  1368. return sprintf ".word\t0x%08x !%s",
  1369. 2<<30|$rd<<25|0x36<<19|$rs1<<14|$opf<<5|$rs2,
  1370. $ref;
  1371. } else {
  1372. return $ref;
  1373. }
  1374. }
  1375. sub unmovxtox { # 2-argument instructions
  1376. my ($mnemonic,$rs,$rd)=@_;
  1377. my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24, "f" => 0 );
  1378. my ($ref,$opf);
  1379. my %movxopf = ( "movdtox" => 0x110,
  1380. "movstouw" => 0x111,
  1381. "movstosw" => 0x113,
  1382. "movxtod" => 0x118,
  1383. "movwtos" => 0x119 );
  1384. $ref = "$mnemonic\t$rs,$rd";
  1385. if (defined($opf=$movxopf{$mnemonic})) {
  1386. foreach ($rs,$rd) {
  1387. return $ref if (!/%([fgoli])([0-9]{1,2})/);
  1388. $_=$bias{$1}+$2;
  1389. if ($2>=32) {
  1390. return $ref if ($2&1);
  1391. # re-encode for upper double register addressing
  1392. $_=($2|$2>>5)&31;
  1393. }
  1394. }
  1395. return sprintf ".word\t0x%08x !%s",
  1396. 2<<30|$rd<<25|0x36<<19|$opf<<5|$rs,
  1397. $ref;
  1398. } else {
  1399. return $ref;
  1400. }
  1401. }
  1402. sub undes {
  1403. my ($mnemonic)=shift;
  1404. my @args=@_;
  1405. my ($ref,$opf);
  1406. my %desopf = ( "des_round" => 0b1001,
  1407. "des_ip" => 0b100110100,
  1408. "des_iip" => 0b100110101,
  1409. "des_kexpand" => 0b100110110 );
  1410. $ref = "$mnemonic\t".join(",",@_);
  1411. if (defined($opf=$desopf{$mnemonic})) { # 4-arg
  1412. if ($mnemonic eq "des_round") {
  1413. foreach (@args[0..3]) {
  1414. return $ref if (!/%f([0-9]{1,2})/);
  1415. $_=$1;
  1416. if ($1>=32) {
  1417. return $ref if ($1&1);
  1418. # re-encode for upper double register addressing
  1419. $_=($1|$1>>5)&31;
  1420. }
  1421. }
  1422. return sprintf ".word\t0x%08x !%s",
  1423. 2<<30|0b011001<<19|$opf<<5|$args[0]<<14|$args[1]|$args[2]<<9|$args[3]<<25,
  1424. $ref;
  1425. } elsif ($mnemonic eq "des_kexpand") { # 3-arg
  1426. foreach (@args[0..2]) {
  1427. return $ref if (!/(%f)?([0-9]{1,2})/);
  1428. $_=$2;
  1429. if ($2>=32) {
  1430. return $ref if ($2&1);
  1431. # re-encode for upper double register addressing
  1432. $_=($2|$2>>5)&31;
  1433. }
  1434. }
  1435. return sprintf ".word\t0x%08x !%s",
  1436. 2<<30|0b110110<<19|$opf<<5|$args[0]<<14|$args[1]|$args[2]<<25,
  1437. $ref;
  1438. } else { # 2-arg
  1439. foreach (@args[0..1]) {
  1440. return $ref if (!/%f([0-9]{1,2})/);
  1441. $_=$1;
  1442. if ($1>=32) {
  1443. return $ref if ($2&1);
  1444. # re-encode for upper double register addressing
  1445. $_=($1|$1>>5)&31;
  1446. }
  1447. }
  1448. return sprintf ".word\t0x%08x !%s",
  1449. 2<<30|0b110110<<19|$opf<<5|$args[0]<<14|$args[1]<<25,
  1450. $ref;
  1451. }
  1452. } else {
  1453. return $ref;
  1454. }
  1455. }
  1456. sub emit_assembler {
  1457. foreach (split("\n",$::code)) {
  1458. s/\`([^\`]*)\`/eval $1/ge;
  1459. s/\b(f[a-z]+2[sd]*)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2})\s*$/$1\t%f0,$2,$3/go;
  1460. s/\b(aes_[edk][^\s]*)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*([%fx0-9]+),\s*(%f[0-9]{1,2})/
  1461. &unaes_round($1,$2,$3,$4,$5)
  1462. /geo or
  1463. s/\b(aes_kexpand[02])\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*(%f[0-9]{1,2})/
  1464. &unaes_kexpand($1,$2,$3,$4)
  1465. /geo or
  1466. s/\b(camellia_f)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*([%fx0-9]+),\s*(%f[0-9]{1,2})/
  1467. &uncamellia_f($1,$2,$3,$4,$5)
  1468. /geo or
  1469. s/\b(camellia_[^s]+)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*(%f[0-9]{1,2})/
  1470. &uncamellia3($1,$2,$3,$4)
  1471. /geo or
  1472. s/\b(des_\w+)\s+(%f[0-9]{1,2}),\s*([%fx0-9]+)(?:,\s*(%f[0-9]{1,2})(?:,\s*(%f[0-9]{1,2}))?)?/
  1473. &undes($1,$2,$3,$4,$5)
  1474. /geo or
  1475. s/\b(mov[ds]to\w+)\s+(%f[0-9]{1,2}),\s*(%[goli][0-7])/
  1476. &unmovxtox($1,$2,$3)
  1477. /geo or
  1478. s/\b(mov[xw]to[ds])\s+(%[goli][0-7]),\s*(%f[0-9]{1,2})/
  1479. &unmovxtox($1,$2,$3)
  1480. /geo or
  1481. s/\b([fb][^\s]*)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*(%f[0-9]{1,2})/
  1482. &unvis($1,$2,$3,$4)
  1483. /geo or
  1484. s/\b(umulxhi|bmask|addxc[c]{0,2}|alignaddr[l]*)\s+(%[goli][0-7]),\s*(%[goli][0-7]),\s*(%[goli][0-7])/
  1485. &unvis3($1,$2,$3,$4)
  1486. /geo;
  1487. print $_,"\n";
  1488. }
  1489. }
  1490. 1;