aes-mips.pl 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170
  1. #! /usr/bin/env perl
  2. # Copyright 2010-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. # ====================================================================
  9. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  10. # project. The module is, however, dual licensed under OpenSSL and
  11. # CRYPTOGAMS licenses depending on where you obtain it. For further
  12. # details see http://www.openssl.org/~appro/cryptogams/.
  13. # ====================================================================
  14. # AES for MIPS
  15. # October 2010
  16. #
  17. # Code uses 1K[+256B] S-box and on single-issue core [such as R5000]
  18. # spends ~68 cycles per byte processed with 128-bit key. This is ~16%
  19. # faster than gcc-generated code, which is not very impressive. But
  20. # recall that compressed S-box requires extra processing, namely
  21. # additional rotations. Rotations are implemented with lwl/lwr pairs,
  22. # which is normally used for loading unaligned data. Another cool
  23. # thing about this module is its endian neutrality, which means that
  24. # it processes data without ever changing byte order...
  25. # September 2012
  26. #
  27. # Add MIPS32R2 (~10% less instructions) and SmartMIPS ASE (further
  28. # ~25% less instructions) code. Note that there is no run-time switch,
  29. # instead, code path is chosen upon pre-process time, pass -mips32r2
  30. # or/and -msmartmips.
  31. ######################################################################
  32. # There is a number of MIPS ABI in use, O32 and N32/64 are most
  33. # widely used. Then there is a new contender: NUBI. It appears that if
  34. # one picks the latter, it's possible to arrange code in ABI neutral
  35. # manner. Therefore let's stick to NUBI register layout:
  36. #
  37. ($zero,$at,$t0,$t1,$t2)=map("\$$_",(0..2,24,25));
  38. ($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$$_",(4..11));
  39. ($s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7,$s8,$s9,$s10,$s11)=map("\$$_",(12..23));
  40. ($gp,$tp,$sp,$fp,$ra)=map("\$$_",(3,28..31));
  41. #
  42. # The return value is placed in $a0. Following coding rules facilitate
  43. # interoperability:
  44. #
  45. # - never ever touch $tp, "thread pointer", former $gp;
  46. # - copy return value to $t0, former $v0 [or to $a0 if you're adapting
  47. # old code];
  48. # - on O32 populate $a4-$a7 with 'lw $aN,4*N($sp)' if necessary;
  49. #
  50. # For reference here is register layout for N32/64 MIPS ABIs:
  51. #
  52. # ($zero,$at,$v0,$v1)=map("\$$_",(0..3));
  53. # ($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$$_",(4..11));
  54. # ($t0,$t1,$t2,$t3,$t8,$t9)=map("\$$_",(12..15,24,25));
  55. # ($s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7)=map("\$$_",(16..23));
  56. # ($gp,$sp,$fp,$ra)=map("\$$_",(28..31));
  57. #
  58. $flavour = shift || "o32"; # supported flavours are o32,n32,64,nubi32,nubi64
  59. if ($flavour =~ /64|n32/i) {
  60. $PTR_LA="dla";
  61. $PTR_ADD="daddu"; # incidentally works even on n32
  62. $PTR_SUB="dsubu"; # incidentally works even on n32
  63. $PTR_INS="dins";
  64. $REG_S="sd";
  65. $REG_L="ld";
  66. $PTR_SLL="dsll"; # incidentally works even on n32
  67. $SZREG=8;
  68. } else {
  69. $PTR_LA="la";
  70. $PTR_ADD="addu";
  71. $PTR_SUB="subu";
  72. $PTR_INS="ins";
  73. $REG_S="sw";
  74. $REG_L="lw";
  75. $PTR_SLL="sll";
  76. $SZREG=4;
  77. }
  78. $pf = ($flavour =~ /nubi/i) ? $t0 : $t2;
  79. #
  80. # <appro@openssl.org>
  81. #
  82. ######################################################################
  83. $big_endian=(`echo MIPSEB | $ENV{CC} -E -`=~/MIPSEB/)?0:1 if ($ENV{CC});
  84. for (@ARGV) { $output=$_ if (/\w[\w\-]*\.\w+$/); }
  85. open STDOUT,">$output";
  86. if (!defined($big_endian))
  87. { $big_endian=(unpack('L',pack('N',1))==1); }
  88. while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
  89. open STDOUT,">$output";
  90. my ($MSB,$LSB)=(0,3); # automatically converted to little-endian
  91. $code.=<<___;
  92. #include "mips_arch.h"
  93. .text
  94. #if !defined(__mips_eabi) && (!defined(__vxworks) || defined(__pic__))
  95. .option pic2
  96. #endif
  97. .set noat
  98. ___
  99. {{{
  100. my $FRAMESIZE=16*$SZREG;
  101. my $SAVED_REGS_MASK = ($flavour =~ /nubi/i) ? "0xc0fff008" : "0xc0ff0000";
  102. my ($inp,$out,$key,$Tbl,$s0,$s1,$s2,$s3)=($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7);
  103. my ($i0,$i1,$i2,$i3)=($at,$t0,$t1,$t2);
  104. my ($t0,$t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9,$t10,$t11) = map("\$$_",(12..23));
  105. my ($key0,$cnt)=($gp,$fp);
  106. # instruction ordering is "stolen" from output from MIPSpro assembler
  107. # invoked with -mips3 -O3 arguments...
  108. $code.=<<___;
  109. .align 5
  110. .ent _mips_AES_encrypt
  111. _mips_AES_encrypt:
  112. .frame $sp,0,$ra
  113. .set reorder
  114. lw $t0,0($key)
  115. lw $t1,4($key)
  116. lw $t2,8($key)
  117. lw $t3,12($key)
  118. lw $cnt,240($key)
  119. $PTR_ADD $key0,$key,16
  120. xor $s0,$t0
  121. xor $s1,$t1
  122. xor $s2,$t2
  123. xor $s3,$t3
  124. subu $cnt,1
  125. #if defined(__mips_smartmips)
  126. ext $i0,$s1,16,8
  127. .Loop_enc:
  128. ext $i1,$s2,16,8
  129. ext $i2,$s3,16,8
  130. ext $i3,$s0,16,8
  131. lwxs $t0,$i0($Tbl) # Te1[s1>>16]
  132. ext $i0,$s2,8,8
  133. lwxs $t1,$i1($Tbl) # Te1[s2>>16]
  134. ext $i1,$s3,8,8
  135. lwxs $t2,$i2($Tbl) # Te1[s3>>16]
  136. ext $i2,$s0,8,8
  137. lwxs $t3,$i3($Tbl) # Te1[s0>>16]
  138. ext $i3,$s1,8,8
  139. lwxs $t4,$i0($Tbl) # Te2[s2>>8]
  140. ext $i0,$s3,0,8
  141. lwxs $t5,$i1($Tbl) # Te2[s3>>8]
  142. ext $i1,$s0,0,8
  143. lwxs $t6,$i2($Tbl) # Te2[s0>>8]
  144. ext $i2,$s1,0,8
  145. lwxs $t7,$i3($Tbl) # Te2[s1>>8]
  146. ext $i3,$s2,0,8
  147. lwxs $t8,$i0($Tbl) # Te3[s3]
  148. ext $i0,$s0,24,8
  149. lwxs $t9,$i1($Tbl) # Te3[s0]
  150. ext $i1,$s1,24,8
  151. lwxs $t10,$i2($Tbl) # Te3[s1]
  152. ext $i2,$s2,24,8
  153. lwxs $t11,$i3($Tbl) # Te3[s2]
  154. ext $i3,$s3,24,8
  155. rotr $t0,$t0,8
  156. rotr $t1,$t1,8
  157. rotr $t2,$t2,8
  158. rotr $t3,$t3,8
  159. rotr $t4,$t4,16
  160. rotr $t5,$t5,16
  161. rotr $t6,$t6,16
  162. rotr $t7,$t7,16
  163. xor $t0,$t4
  164. lwxs $t4,$i0($Tbl) # Te0[s0>>24]
  165. xor $t1,$t5
  166. lwxs $t5,$i1($Tbl) # Te0[s1>>24]
  167. xor $t2,$t6
  168. lwxs $t6,$i2($Tbl) # Te0[s2>>24]
  169. xor $t3,$t7
  170. lwxs $t7,$i3($Tbl) # Te0[s3>>24]
  171. rotr $t8,$t8,24
  172. lw $s0,0($key0)
  173. rotr $t9,$t9,24
  174. lw $s1,4($key0)
  175. rotr $t10,$t10,24
  176. lw $s2,8($key0)
  177. rotr $t11,$t11,24
  178. lw $s3,12($key0)
  179. xor $t0,$t8
  180. xor $t1,$t9
  181. xor $t2,$t10
  182. xor $t3,$t11
  183. xor $t0,$t4
  184. xor $t1,$t5
  185. xor $t2,$t6
  186. xor $t3,$t7
  187. subu $cnt,1
  188. $PTR_ADD $key0,16
  189. xor $s0,$t0
  190. xor $s1,$t1
  191. xor $s2,$t2
  192. xor $s3,$t3
  193. .set noreorder
  194. bnez $cnt,.Loop_enc
  195. ext $i0,$s1,16,8
  196. _xtr $i0,$s1,16-2
  197. #else
  198. _xtr $i0,$s1,16-2
  199. .Loop_enc:
  200. _xtr $i1,$s2,16-2
  201. _xtr $i2,$s3,16-2
  202. _xtr $i3,$s0,16-2
  203. and $i0,0x3fc
  204. and $i1,0x3fc
  205. and $i2,0x3fc
  206. and $i3,0x3fc
  207. $PTR_ADD $i0,$Tbl
  208. $PTR_ADD $i1,$Tbl
  209. $PTR_ADD $i2,$Tbl
  210. $PTR_ADD $i3,$Tbl
  211. #if defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2)
  212. lw $t0,0($i0) # Te1[s1>>16]
  213. _xtr $i0,$s2,8-2
  214. lw $t1,0($i1) # Te1[s2>>16]
  215. _xtr $i1,$s3,8-2
  216. lw $t2,0($i2) # Te1[s3>>16]
  217. _xtr $i2,$s0,8-2
  218. lw $t3,0($i3) # Te1[s0>>16]
  219. _xtr $i3,$s1,8-2
  220. #else
  221. lwl $t0,3($i0) # Te1[s1>>16]
  222. lwl $t1,3($i1) # Te1[s2>>16]
  223. lwl $t2,3($i2) # Te1[s3>>16]
  224. lwl $t3,3($i3) # Te1[s0>>16]
  225. lwr $t0,2($i0) # Te1[s1>>16]
  226. _xtr $i0,$s2,8-2
  227. lwr $t1,2($i1) # Te1[s2>>16]
  228. _xtr $i1,$s3,8-2
  229. lwr $t2,2($i2) # Te1[s3>>16]
  230. _xtr $i2,$s0,8-2
  231. lwr $t3,2($i3) # Te1[s0>>16]
  232. _xtr $i3,$s1,8-2
  233. #endif
  234. and $i0,0x3fc
  235. and $i1,0x3fc
  236. and $i2,0x3fc
  237. and $i3,0x3fc
  238. $PTR_ADD $i0,$Tbl
  239. $PTR_ADD $i1,$Tbl
  240. $PTR_ADD $i2,$Tbl
  241. $PTR_ADD $i3,$Tbl
  242. #if defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2)
  243. rotr $t0,$t0,8
  244. rotr $t1,$t1,8
  245. rotr $t2,$t2,8
  246. rotr $t3,$t3,8
  247. # if defined(_MIPSEL)
  248. lw $t4,0($i0) # Te2[s2>>8]
  249. _xtr $i0,$s3,0-2
  250. lw $t5,0($i1) # Te2[s3>>8]
  251. _xtr $i1,$s0,0-2
  252. lw $t6,0($i2) # Te2[s0>>8]
  253. _xtr $i2,$s1,0-2
  254. lw $t7,0($i3) # Te2[s1>>8]
  255. _xtr $i3,$s2,0-2
  256. and $i0,0x3fc
  257. and $i1,0x3fc
  258. and $i2,0x3fc
  259. and $i3,0x3fc
  260. $PTR_ADD $i0,$Tbl
  261. $PTR_ADD $i1,$Tbl
  262. $PTR_ADD $i2,$Tbl
  263. $PTR_ADD $i3,$Tbl
  264. lw $t8,0($i0) # Te3[s3]
  265. $PTR_INS $i0,$s0,2,8
  266. lw $t9,0($i1) # Te3[s0]
  267. $PTR_INS $i1,$s1,2,8
  268. lw $t10,0($i2) # Te3[s1]
  269. $PTR_INS $i2,$s2,2,8
  270. lw $t11,0($i3) # Te3[s2]
  271. $PTR_INS $i3,$s3,2,8
  272. # else
  273. lw $t4,0($i0) # Te2[s2>>8]
  274. $PTR_INS $i0,$s3,2,8
  275. lw $t5,0($i1) # Te2[s3>>8]
  276. $PTR_INS $i1,$s0,2,8
  277. lw $t6,0($i2) # Te2[s0>>8]
  278. $PTR_INS $i2,$s1,2,8
  279. lw $t7,0($i3) # Te2[s1>>8]
  280. $PTR_INS $i3,$s2,2,8
  281. lw $t8,0($i0) # Te3[s3]
  282. _xtr $i0,$s0,24-2
  283. lw $t9,0($i1) # Te3[s0]
  284. _xtr $i1,$s1,24-2
  285. lw $t10,0($i2) # Te3[s1]
  286. _xtr $i2,$s2,24-2
  287. lw $t11,0($i3) # Te3[s2]
  288. _xtr $i3,$s3,24-2
  289. and $i0,0x3fc
  290. and $i1,0x3fc
  291. and $i2,0x3fc
  292. and $i3,0x3fc
  293. $PTR_ADD $i0,$Tbl
  294. $PTR_ADD $i1,$Tbl
  295. $PTR_ADD $i2,$Tbl
  296. $PTR_ADD $i3,$Tbl
  297. # endif
  298. rotr $t4,$t4,16
  299. rotr $t5,$t5,16
  300. rotr $t6,$t6,16
  301. rotr $t7,$t7,16
  302. rotr $t8,$t8,24
  303. rotr $t9,$t9,24
  304. rotr $t10,$t10,24
  305. rotr $t11,$t11,24
  306. #else
  307. lwl $t4,2($i0) # Te2[s2>>8]
  308. lwl $t5,2($i1) # Te2[s3>>8]
  309. lwl $t6,2($i2) # Te2[s0>>8]
  310. lwl $t7,2($i3) # Te2[s1>>8]
  311. lwr $t4,1($i0) # Te2[s2>>8]
  312. _xtr $i0,$s3,0-2
  313. lwr $t5,1($i1) # Te2[s3>>8]
  314. _xtr $i1,$s0,0-2
  315. lwr $t6,1($i2) # Te2[s0>>8]
  316. _xtr $i2,$s1,0-2
  317. lwr $t7,1($i3) # Te2[s1>>8]
  318. _xtr $i3,$s2,0-2
  319. and $i0,0x3fc
  320. and $i1,0x3fc
  321. and $i2,0x3fc
  322. and $i3,0x3fc
  323. $PTR_ADD $i0,$Tbl
  324. $PTR_ADD $i1,$Tbl
  325. $PTR_ADD $i2,$Tbl
  326. $PTR_ADD $i3,$Tbl
  327. lwl $t8,1($i0) # Te3[s3]
  328. lwl $t9,1($i1) # Te3[s0]
  329. lwl $t10,1($i2) # Te3[s1]
  330. lwl $t11,1($i3) # Te3[s2]
  331. lwr $t8,0($i0) # Te3[s3]
  332. _xtr $i0,$s0,24-2
  333. lwr $t9,0($i1) # Te3[s0]
  334. _xtr $i1,$s1,24-2
  335. lwr $t10,0($i2) # Te3[s1]
  336. _xtr $i2,$s2,24-2
  337. lwr $t11,0($i3) # Te3[s2]
  338. _xtr $i3,$s3,24-2
  339. and $i0,0x3fc
  340. and $i1,0x3fc
  341. and $i2,0x3fc
  342. and $i3,0x3fc
  343. $PTR_ADD $i0,$Tbl
  344. $PTR_ADD $i1,$Tbl
  345. $PTR_ADD $i2,$Tbl
  346. $PTR_ADD $i3,$Tbl
  347. #endif
  348. xor $t0,$t4
  349. lw $t4,0($i0) # Te0[s0>>24]
  350. xor $t1,$t5
  351. lw $t5,0($i1) # Te0[s1>>24]
  352. xor $t2,$t6
  353. lw $t6,0($i2) # Te0[s2>>24]
  354. xor $t3,$t7
  355. lw $t7,0($i3) # Te0[s3>>24]
  356. xor $t0,$t8
  357. lw $s0,0($key0)
  358. xor $t1,$t9
  359. lw $s1,4($key0)
  360. xor $t2,$t10
  361. lw $s2,8($key0)
  362. xor $t3,$t11
  363. lw $s3,12($key0)
  364. xor $t0,$t4
  365. xor $t1,$t5
  366. xor $t2,$t6
  367. xor $t3,$t7
  368. subu $cnt,1
  369. $PTR_ADD $key0,16
  370. xor $s0,$t0
  371. xor $s1,$t1
  372. xor $s2,$t2
  373. xor $s3,$t3
  374. .set noreorder
  375. bnez $cnt,.Loop_enc
  376. _xtr $i0,$s1,16-2
  377. #endif
  378. .set reorder
  379. _xtr $i1,$s2,16-2
  380. _xtr $i2,$s3,16-2
  381. _xtr $i3,$s0,16-2
  382. and $i0,0x3fc
  383. and $i1,0x3fc
  384. and $i2,0x3fc
  385. and $i3,0x3fc
  386. $PTR_ADD $i0,$Tbl
  387. $PTR_ADD $i1,$Tbl
  388. $PTR_ADD $i2,$Tbl
  389. $PTR_ADD $i3,$Tbl
  390. lbu $t0,2($i0) # Te4[s1>>16]
  391. _xtr $i0,$s2,8-2
  392. lbu $t1,2($i1) # Te4[s2>>16]
  393. _xtr $i1,$s3,8-2
  394. lbu $t2,2($i2) # Te4[s3>>16]
  395. _xtr $i2,$s0,8-2
  396. lbu $t3,2($i3) # Te4[s0>>16]
  397. _xtr $i3,$s1,8-2
  398. and $i0,0x3fc
  399. and $i1,0x3fc
  400. and $i2,0x3fc
  401. and $i3,0x3fc
  402. $PTR_ADD $i0,$Tbl
  403. $PTR_ADD $i1,$Tbl
  404. $PTR_ADD $i2,$Tbl
  405. $PTR_ADD $i3,$Tbl
  406. #if defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2)
  407. # if defined(_MIPSEL)
  408. lbu $t4,2($i0) # Te4[s2>>8]
  409. $PTR_INS $i0,$s0,2,8
  410. lbu $t5,2($i1) # Te4[s3>>8]
  411. $PTR_INS $i1,$s1,2,8
  412. lbu $t6,2($i2) # Te4[s0>>8]
  413. $PTR_INS $i2,$s2,2,8
  414. lbu $t7,2($i3) # Te4[s1>>8]
  415. $PTR_INS $i3,$s3,2,8
  416. lbu $t8,2($i0) # Te4[s0>>24]
  417. _xtr $i0,$s3,0-2
  418. lbu $t9,2($i1) # Te4[s1>>24]
  419. _xtr $i1,$s0,0-2
  420. lbu $t10,2($i2) # Te4[s2>>24]
  421. _xtr $i2,$s1,0-2
  422. lbu $t11,2($i3) # Te4[s3>>24]
  423. _xtr $i3,$s2,0-2
  424. and $i0,0x3fc
  425. and $i1,0x3fc
  426. and $i2,0x3fc
  427. and $i3,0x3fc
  428. $PTR_ADD $i0,$Tbl
  429. $PTR_ADD $i1,$Tbl
  430. $PTR_ADD $i2,$Tbl
  431. $PTR_ADD $i3,$Tbl
  432. # else
  433. lbu $t4,2($i0) # Te4[s2>>8]
  434. _xtr $i0,$s0,24-2
  435. lbu $t5,2($i1) # Te4[s3>>8]
  436. _xtr $i1,$s1,24-2
  437. lbu $t6,2($i2) # Te4[s0>>8]
  438. _xtr $i2,$s2,24-2
  439. lbu $t7,2($i3) # Te4[s1>>8]
  440. _xtr $i3,$s3,24-2
  441. and $i0,0x3fc
  442. and $i1,0x3fc
  443. and $i2,0x3fc
  444. and $i3,0x3fc
  445. $PTR_ADD $i0,$Tbl
  446. $PTR_ADD $i1,$Tbl
  447. $PTR_ADD $i2,$Tbl
  448. $PTR_ADD $i3,$Tbl
  449. lbu $t8,2($i0) # Te4[s0>>24]
  450. $PTR_INS $i0,$s3,2,8
  451. lbu $t9,2($i1) # Te4[s1>>24]
  452. $PTR_INS $i1,$s0,2,8
  453. lbu $t10,2($i2) # Te4[s2>>24]
  454. $PTR_INS $i2,$s1,2,8
  455. lbu $t11,2($i3) # Te4[s3>>24]
  456. $PTR_INS $i3,$s2,2,8
  457. # endif
  458. _ins $t0,16
  459. _ins $t1,16
  460. _ins $t2,16
  461. _ins $t3,16
  462. _ins2 $t0,$t4,8
  463. lbu $t4,2($i0) # Te4[s3]
  464. _ins2 $t1,$t5,8
  465. lbu $t5,2($i1) # Te4[s0]
  466. _ins2 $t2,$t6,8
  467. lbu $t6,2($i2) # Te4[s1]
  468. _ins2 $t3,$t7,8
  469. lbu $t7,2($i3) # Te4[s2]
  470. _ins2 $t0,$t8,24
  471. lw $s0,0($key0)
  472. _ins2 $t1,$t9,24
  473. lw $s1,4($key0)
  474. _ins2 $t2,$t10,24
  475. lw $s2,8($key0)
  476. _ins2 $t3,$t11,24
  477. lw $s3,12($key0)
  478. _ins2 $t0,$t4,0
  479. _ins2 $t1,$t5,0
  480. _ins2 $t2,$t6,0
  481. _ins2 $t3,$t7,0
  482. #else
  483. lbu $t4,2($i0) # Te4[s2>>8]
  484. _xtr $i0,$s0,24-2
  485. lbu $t5,2($i1) # Te4[s3>>8]
  486. _xtr $i1,$s1,24-2
  487. lbu $t6,2($i2) # Te4[s0>>8]
  488. _xtr $i2,$s2,24-2
  489. lbu $t7,2($i3) # Te4[s1>>8]
  490. _xtr $i3,$s3,24-2
  491. and $i0,0x3fc
  492. and $i1,0x3fc
  493. and $i2,0x3fc
  494. and $i3,0x3fc
  495. $PTR_ADD $i0,$Tbl
  496. $PTR_ADD $i1,$Tbl
  497. $PTR_ADD $i2,$Tbl
  498. $PTR_ADD $i3,$Tbl
  499. lbu $t8,2($i0) # Te4[s0>>24]
  500. _xtr $i0,$s3,0-2
  501. lbu $t9,2($i1) # Te4[s1>>24]
  502. _xtr $i1,$s0,0-2
  503. lbu $t10,2($i2) # Te4[s2>>24]
  504. _xtr $i2,$s1,0-2
  505. lbu $t11,2($i3) # Te4[s3>>24]
  506. _xtr $i3,$s2,0-2
  507. and $i0,0x3fc
  508. and $i1,0x3fc
  509. and $i2,0x3fc
  510. and $i3,0x3fc
  511. $PTR_ADD $i0,$Tbl
  512. $PTR_ADD $i1,$Tbl
  513. $PTR_ADD $i2,$Tbl
  514. $PTR_ADD $i3,$Tbl
  515. _ins $t0,16
  516. _ins $t1,16
  517. _ins $t2,16
  518. _ins $t3,16
  519. _ins $t4,8
  520. _ins $t5,8
  521. _ins $t6,8
  522. _ins $t7,8
  523. xor $t0,$t4
  524. lbu $t4,2($i0) # Te4[s3]
  525. xor $t1,$t5
  526. lbu $t5,2($i1) # Te4[s0]
  527. xor $t2,$t6
  528. lbu $t6,2($i2) # Te4[s1]
  529. xor $t3,$t7
  530. lbu $t7,2($i3) # Te4[s2]
  531. _ins $t8,24
  532. lw $s0,0($key0)
  533. _ins $t9,24
  534. lw $s1,4($key0)
  535. _ins $t10,24
  536. lw $s2,8($key0)
  537. _ins $t11,24
  538. lw $s3,12($key0)
  539. xor $t0,$t8
  540. xor $t1,$t9
  541. xor $t2,$t10
  542. xor $t3,$t11
  543. _ins $t4,0
  544. _ins $t5,0
  545. _ins $t6,0
  546. _ins $t7,0
  547. xor $t0,$t4
  548. xor $t1,$t5
  549. xor $t2,$t6
  550. xor $t3,$t7
  551. #endif
  552. xor $s0,$t0
  553. xor $s1,$t1
  554. xor $s2,$t2
  555. xor $s3,$t3
  556. jr $ra
  557. .end _mips_AES_encrypt
  558. .align 5
  559. .globl AES_encrypt
  560. .ent AES_encrypt
  561. AES_encrypt:
  562. .frame $sp,$FRAMESIZE,$ra
  563. .mask $SAVED_REGS_MASK,-$SZREG
  564. .set noreorder
  565. ___
  566. $code.=<<___ if ($flavour =~ /o32/i); # o32 PIC-ification
  567. .cpload $pf
  568. ___
  569. $code.=<<___;
  570. $PTR_SUB $sp,$FRAMESIZE
  571. $REG_S $ra,$FRAMESIZE-1*$SZREG($sp)
  572. $REG_S $fp,$FRAMESIZE-2*$SZREG($sp)
  573. $REG_S $s11,$FRAMESIZE-3*$SZREG($sp)
  574. $REG_S $s10,$FRAMESIZE-4*$SZREG($sp)
  575. $REG_S $s9,$FRAMESIZE-5*$SZREG($sp)
  576. $REG_S $s8,$FRAMESIZE-6*$SZREG($sp)
  577. $REG_S $s7,$FRAMESIZE-7*$SZREG($sp)
  578. $REG_S $s6,$FRAMESIZE-8*$SZREG($sp)
  579. $REG_S $s5,$FRAMESIZE-9*$SZREG($sp)
  580. $REG_S $s4,$FRAMESIZE-10*$SZREG($sp)
  581. ___
  582. $code.=<<___ if ($flavour =~ /nubi/i); # optimize non-nubi prologue
  583. $REG_S \$15,$FRAMESIZE-11*$SZREG($sp)
  584. $REG_S \$14,$FRAMESIZE-12*$SZREG($sp)
  585. $REG_S \$13,$FRAMESIZE-13*$SZREG($sp)
  586. $REG_S \$12,$FRAMESIZE-14*$SZREG($sp)
  587. $REG_S $gp,$FRAMESIZE-15*$SZREG($sp)
  588. ___
  589. $code.=<<___ if ($flavour !~ /o32/i); # non-o32 PIC-ification
  590. .cplocal $Tbl
  591. .cpsetup $pf,$zero,AES_encrypt
  592. ___
  593. $code.=<<___;
  594. .set reorder
  595. $PTR_LA $Tbl,AES_Te # PIC-ified 'load address'
  596. #if defined(_MIPS_ARCH_MIPS32R6) || defined(_MIPS_ARCH_MIPS64R6)
  597. lw $s0,0($inp)
  598. lw $s1,4($inp)
  599. lw $s2,8($inp)
  600. lw $s3,12($inp)
  601. #else
  602. lwl $s0,0+$MSB($inp)
  603. lwl $s1,4+$MSB($inp)
  604. lwl $s2,8+$MSB($inp)
  605. lwl $s3,12+$MSB($inp)
  606. lwr $s0,0+$LSB($inp)
  607. lwr $s1,4+$LSB($inp)
  608. lwr $s2,8+$LSB($inp)
  609. lwr $s3,12+$LSB($inp)
  610. #endif
  611. bal _mips_AES_encrypt
  612. #if defined(_MIPS_ARCH_MIPS32R6) || defined(_MIPS_ARCH_MIPS64R6)
  613. sw $s0,0($out)
  614. sw $s1,4($out)
  615. sw $s2,8($out)
  616. sw $s3,12($out)
  617. #else
  618. swr $s0,0+$LSB($out)
  619. swr $s1,4+$LSB($out)
  620. swr $s2,8+$LSB($out)
  621. swr $s3,12+$LSB($out)
  622. swl $s0,0+$MSB($out)
  623. swl $s1,4+$MSB($out)
  624. swl $s2,8+$MSB($out)
  625. swl $s3,12+$MSB($out)
  626. #endif
  627. .set noreorder
  628. $REG_L $ra,$FRAMESIZE-1*$SZREG($sp)
  629. $REG_L $fp,$FRAMESIZE-2*$SZREG($sp)
  630. $REG_L $s11,$FRAMESIZE-3*$SZREG($sp)
  631. $REG_L $s10,$FRAMESIZE-4*$SZREG($sp)
  632. $REG_L $s9,$FRAMESIZE-5*$SZREG($sp)
  633. $REG_L $s8,$FRAMESIZE-6*$SZREG($sp)
  634. $REG_L $s7,$FRAMESIZE-7*$SZREG($sp)
  635. $REG_L $s6,$FRAMESIZE-8*$SZREG($sp)
  636. $REG_L $s5,$FRAMESIZE-9*$SZREG($sp)
  637. $REG_L $s4,$FRAMESIZE-10*$SZREG($sp)
  638. ___
  639. $code.=<<___ if ($flavour =~ /nubi/i);
  640. $REG_L \$15,$FRAMESIZE-11*$SZREG($sp)
  641. $REG_L \$14,$FRAMESIZE-12*$SZREG($sp)
  642. $REG_L \$13,$FRAMESIZE-13*$SZREG($sp)
  643. $REG_L \$12,$FRAMESIZE-14*$SZREG($sp)
  644. $REG_L $gp,$FRAMESIZE-15*$SZREG($sp)
  645. ___
  646. $code.=<<___;
  647. jr $ra
  648. $PTR_ADD $sp,$FRAMESIZE
  649. .end AES_encrypt
  650. ___
  651. $code.=<<___;
  652. .align 5
  653. .ent _mips_AES_decrypt
  654. _mips_AES_decrypt:
  655. .frame $sp,0,$ra
  656. .set reorder
  657. lw $t0,0($key)
  658. lw $t1,4($key)
  659. lw $t2,8($key)
  660. lw $t3,12($key)
  661. lw $cnt,240($key)
  662. $PTR_ADD $key0,$key,16
  663. xor $s0,$t0
  664. xor $s1,$t1
  665. xor $s2,$t2
  666. xor $s3,$t3
  667. subu $cnt,1
  668. #if defined(__mips_smartmips)
  669. ext $i0,$s3,16,8
  670. .Loop_dec:
  671. ext $i1,$s0,16,8
  672. ext $i2,$s1,16,8
  673. ext $i3,$s2,16,8
  674. lwxs $t0,$i0($Tbl) # Td1[s3>>16]
  675. ext $i0,$s2,8,8
  676. lwxs $t1,$i1($Tbl) # Td1[s0>>16]
  677. ext $i1,$s3,8,8
  678. lwxs $t2,$i2($Tbl) # Td1[s1>>16]
  679. ext $i2,$s0,8,8
  680. lwxs $t3,$i3($Tbl) # Td1[s2>>16]
  681. ext $i3,$s1,8,8
  682. lwxs $t4,$i0($Tbl) # Td2[s2>>8]
  683. ext $i0,$s1,0,8
  684. lwxs $t5,$i1($Tbl) # Td2[s3>>8]
  685. ext $i1,$s2,0,8
  686. lwxs $t6,$i2($Tbl) # Td2[s0>>8]
  687. ext $i2,$s3,0,8
  688. lwxs $t7,$i3($Tbl) # Td2[s1>>8]
  689. ext $i3,$s0,0,8
  690. lwxs $t8,$i0($Tbl) # Td3[s1]
  691. ext $i0,$s0,24,8
  692. lwxs $t9,$i1($Tbl) # Td3[s2]
  693. ext $i1,$s1,24,8
  694. lwxs $t10,$i2($Tbl) # Td3[s3]
  695. ext $i2,$s2,24,8
  696. lwxs $t11,$i3($Tbl) # Td3[s0]
  697. ext $i3,$s3,24,8
  698. rotr $t0,$t0,8
  699. rotr $t1,$t1,8
  700. rotr $t2,$t2,8
  701. rotr $t3,$t3,8
  702. rotr $t4,$t4,16
  703. rotr $t5,$t5,16
  704. rotr $t6,$t6,16
  705. rotr $t7,$t7,16
  706. xor $t0,$t4
  707. lwxs $t4,$i0($Tbl) # Td0[s0>>24]
  708. xor $t1,$t5
  709. lwxs $t5,$i1($Tbl) # Td0[s1>>24]
  710. xor $t2,$t6
  711. lwxs $t6,$i2($Tbl) # Td0[s2>>24]
  712. xor $t3,$t7
  713. lwxs $t7,$i3($Tbl) # Td0[s3>>24]
  714. rotr $t8,$t8,24
  715. lw $s0,0($key0)
  716. rotr $t9,$t9,24
  717. lw $s1,4($key0)
  718. rotr $t10,$t10,24
  719. lw $s2,8($key0)
  720. rotr $t11,$t11,24
  721. lw $s3,12($key0)
  722. xor $t0,$t8
  723. xor $t1,$t9
  724. xor $t2,$t10
  725. xor $t3,$t11
  726. xor $t0,$t4
  727. xor $t1,$t5
  728. xor $t2,$t6
  729. xor $t3,$t7
  730. subu $cnt,1
  731. $PTR_ADD $key0,16
  732. xor $s0,$t0
  733. xor $s1,$t1
  734. xor $s2,$t2
  735. xor $s3,$t3
  736. .set noreorder
  737. bnez $cnt,.Loop_dec
  738. ext $i0,$s3,16,8
  739. _xtr $i0,$s3,16-2
  740. #else
  741. _xtr $i0,$s3,16-2
  742. .Loop_dec:
  743. _xtr $i1,$s0,16-2
  744. _xtr $i2,$s1,16-2
  745. _xtr $i3,$s2,16-2
  746. and $i0,0x3fc
  747. and $i1,0x3fc
  748. and $i2,0x3fc
  749. and $i3,0x3fc
  750. $PTR_ADD $i0,$Tbl
  751. $PTR_ADD $i1,$Tbl
  752. $PTR_ADD $i2,$Tbl
  753. $PTR_ADD $i3,$Tbl
  754. #if defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2)
  755. lw $t0,0($i0) # Td1[s3>>16]
  756. _xtr $i0,$s2,8-2
  757. lw $t1,0($i1) # Td1[s0>>16]
  758. _xtr $i1,$s3,8-2
  759. lw $t2,0($i2) # Td1[s1>>16]
  760. _xtr $i2,$s0,8-2
  761. lw $t3,0($i3) # Td1[s2>>16]
  762. _xtr $i3,$s1,8-2
  763. #else
  764. lwl $t0,3($i0) # Td1[s3>>16]
  765. lwl $t1,3($i1) # Td1[s0>>16]
  766. lwl $t2,3($i2) # Td1[s1>>16]
  767. lwl $t3,3($i3) # Td1[s2>>16]
  768. lwr $t0,2($i0) # Td1[s3>>16]
  769. _xtr $i0,$s2,8-2
  770. lwr $t1,2($i1) # Td1[s0>>16]
  771. _xtr $i1,$s3,8-2
  772. lwr $t2,2($i2) # Td1[s1>>16]
  773. _xtr $i2,$s0,8-2
  774. lwr $t3,2($i3) # Td1[s2>>16]
  775. _xtr $i3,$s1,8-2
  776. #endif
  777. and $i0,0x3fc
  778. and $i1,0x3fc
  779. and $i2,0x3fc
  780. and $i3,0x3fc
  781. $PTR_ADD $i0,$Tbl
  782. $PTR_ADD $i1,$Tbl
  783. $PTR_ADD $i2,$Tbl
  784. $PTR_ADD $i3,$Tbl
  785. #if defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2)
  786. rotr $t0,$t0,8
  787. rotr $t1,$t1,8
  788. rotr $t2,$t2,8
  789. rotr $t3,$t3,8
  790. # if defined(_MIPSEL)
  791. lw $t4,0($i0) # Td2[s2>>8]
  792. _xtr $i0,$s1,0-2
  793. lw $t5,0($i1) # Td2[s3>>8]
  794. _xtr $i1,$s2,0-2
  795. lw $t6,0($i2) # Td2[s0>>8]
  796. _xtr $i2,$s3,0-2
  797. lw $t7,0($i3) # Td2[s1>>8]
  798. _xtr $i3,$s0,0-2
  799. and $i0,0x3fc
  800. and $i1,0x3fc
  801. and $i2,0x3fc
  802. and $i3,0x3fc
  803. $PTR_ADD $i0,$Tbl
  804. $PTR_ADD $i1,$Tbl
  805. $PTR_ADD $i2,$Tbl
  806. $PTR_ADD $i3,$Tbl
  807. lw $t8,0($i0) # Td3[s1]
  808. $PTR_INS $i0,$s0,2,8
  809. lw $t9,0($i1) # Td3[s2]
  810. $PTR_INS $i1,$s1,2,8
  811. lw $t10,0($i2) # Td3[s3]
  812. $PTR_INS $i2,$s2,2,8
  813. lw $t11,0($i3) # Td3[s0]
  814. $PTR_INS $i3,$s3,2,8
  815. #else
  816. lw $t4,0($i0) # Td2[s2>>8]
  817. $PTR_INS $i0,$s1,2,8
  818. lw $t5,0($i1) # Td2[s3>>8]
  819. $PTR_INS $i1,$s2,2,8
  820. lw $t6,0($i2) # Td2[s0>>8]
  821. $PTR_INS $i2,$s3,2,8
  822. lw $t7,0($i3) # Td2[s1>>8]
  823. $PTR_INS $i3,$s0,2,8
  824. lw $t8,0($i0) # Td3[s1]
  825. _xtr $i0,$s0,24-2
  826. lw $t9,0($i1) # Td3[s2]
  827. _xtr $i1,$s1,24-2
  828. lw $t10,0($i2) # Td3[s3]
  829. _xtr $i2,$s2,24-2
  830. lw $t11,0($i3) # Td3[s0]
  831. _xtr $i3,$s3,24-2
  832. and $i0,0x3fc
  833. and $i1,0x3fc
  834. and $i2,0x3fc
  835. and $i3,0x3fc
  836. $PTR_ADD $i0,$Tbl
  837. $PTR_ADD $i1,$Tbl
  838. $PTR_ADD $i2,$Tbl
  839. $PTR_ADD $i3,$Tbl
  840. #endif
  841. rotr $t4,$t4,16
  842. rotr $t5,$t5,16
  843. rotr $t6,$t6,16
  844. rotr $t7,$t7,16
  845. rotr $t8,$t8,24
  846. rotr $t9,$t9,24
  847. rotr $t10,$t10,24
  848. rotr $t11,$t11,24
  849. #else
  850. lwl $t4,2($i0) # Td2[s2>>8]
  851. lwl $t5,2($i1) # Td2[s3>>8]
  852. lwl $t6,2($i2) # Td2[s0>>8]
  853. lwl $t7,2($i3) # Td2[s1>>8]
  854. lwr $t4,1($i0) # Td2[s2>>8]
  855. _xtr $i0,$s1,0-2
  856. lwr $t5,1($i1) # Td2[s3>>8]
  857. _xtr $i1,$s2,0-2
  858. lwr $t6,1($i2) # Td2[s0>>8]
  859. _xtr $i2,$s3,0-2
  860. lwr $t7,1($i3) # Td2[s1>>8]
  861. _xtr $i3,$s0,0-2
  862. and $i0,0x3fc
  863. and $i1,0x3fc
  864. and $i2,0x3fc
  865. and $i3,0x3fc
  866. $PTR_ADD $i0,$Tbl
  867. $PTR_ADD $i1,$Tbl
  868. $PTR_ADD $i2,$Tbl
  869. $PTR_ADD $i3,$Tbl
  870. lwl $t8,1($i0) # Td3[s1]
  871. lwl $t9,1($i1) # Td3[s2]
  872. lwl $t10,1($i2) # Td3[s3]
  873. lwl $t11,1($i3) # Td3[s0]
  874. lwr $t8,0($i0) # Td3[s1]
  875. _xtr $i0,$s0,24-2
  876. lwr $t9,0($i1) # Td3[s2]
  877. _xtr $i1,$s1,24-2
  878. lwr $t10,0($i2) # Td3[s3]
  879. _xtr $i2,$s2,24-2
  880. lwr $t11,0($i3) # Td3[s0]
  881. _xtr $i3,$s3,24-2
  882. and $i0,0x3fc
  883. and $i1,0x3fc
  884. and $i2,0x3fc
  885. and $i3,0x3fc
  886. $PTR_ADD $i0,$Tbl
  887. $PTR_ADD $i1,$Tbl
  888. $PTR_ADD $i2,$Tbl
  889. $PTR_ADD $i3,$Tbl
  890. #endif
  891. xor $t0,$t4
  892. lw $t4,0($i0) # Td0[s0>>24]
  893. xor $t1,$t5
  894. lw $t5,0($i1) # Td0[s1>>24]
  895. xor $t2,$t6
  896. lw $t6,0($i2) # Td0[s2>>24]
  897. xor $t3,$t7
  898. lw $t7,0($i3) # Td0[s3>>24]
  899. xor $t0,$t8
  900. lw $s0,0($key0)
  901. xor $t1,$t9
  902. lw $s1,4($key0)
  903. xor $t2,$t10
  904. lw $s2,8($key0)
  905. xor $t3,$t11
  906. lw $s3,12($key0)
  907. xor $t0,$t4
  908. xor $t1,$t5
  909. xor $t2,$t6
  910. xor $t3,$t7
  911. subu $cnt,1
  912. $PTR_ADD $key0,16
  913. xor $s0,$t0
  914. xor $s1,$t1
  915. xor $s2,$t2
  916. xor $s3,$t3
  917. .set noreorder
  918. bnez $cnt,.Loop_dec
  919. _xtr $i0,$s3,16-2
  920. #endif
  921. .set reorder
  922. lw $t4,1024($Tbl) # prefetch Td4
  923. _xtr $i0,$s3,16
  924. lw $t5,1024+32($Tbl)
  925. _xtr $i1,$s0,16
  926. lw $t6,1024+64($Tbl)
  927. _xtr $i2,$s1,16
  928. lw $t7,1024+96($Tbl)
  929. _xtr $i3,$s2,16
  930. lw $t8,1024+128($Tbl)
  931. and $i0,0xff
  932. lw $t9,1024+160($Tbl)
  933. and $i1,0xff
  934. lw $t10,1024+192($Tbl)
  935. and $i2,0xff
  936. lw $t11,1024+224($Tbl)
  937. and $i3,0xff
  938. $PTR_ADD $i0,$Tbl
  939. $PTR_ADD $i1,$Tbl
  940. $PTR_ADD $i2,$Tbl
  941. $PTR_ADD $i3,$Tbl
  942. lbu $t0,1024($i0) # Td4[s3>>16]
  943. _xtr $i0,$s2,8
  944. lbu $t1,1024($i1) # Td4[s0>>16]
  945. _xtr $i1,$s3,8
  946. lbu $t2,1024($i2) # Td4[s1>>16]
  947. _xtr $i2,$s0,8
  948. lbu $t3,1024($i3) # Td4[s2>>16]
  949. _xtr $i3,$s1,8
  950. and $i0,0xff
  951. and $i1,0xff
  952. and $i2,0xff
  953. and $i3,0xff
  954. $PTR_ADD $i0,$Tbl
  955. $PTR_ADD $i1,$Tbl
  956. $PTR_ADD $i2,$Tbl
  957. $PTR_ADD $i3,$Tbl
  958. #if defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2)
  959. # if defined(_MIPSEL)
  960. lbu $t4,1024($i0) # Td4[s2>>8]
  961. $PTR_INS $i0,$s0,0,8
  962. lbu $t5,1024($i1) # Td4[s3>>8]
  963. $PTR_INS $i1,$s1,0,8
  964. lbu $t6,1024($i2) # Td4[s0>>8]
  965. $PTR_INS $i2,$s2,0,8
  966. lbu $t7,1024($i3) # Td4[s1>>8]
  967. $PTR_INS $i3,$s3,0,8
  968. lbu $t8,1024($i0) # Td4[s0>>24]
  969. _xtr $i0,$s1,0
  970. lbu $t9,1024($i1) # Td4[s1>>24]
  971. _xtr $i1,$s2,0
  972. lbu $t10,1024($i2) # Td4[s2>>24]
  973. _xtr $i2,$s3,0
  974. lbu $t11,1024($i3) # Td4[s3>>24]
  975. _xtr $i3,$s0,0
  976. $PTR_ADD $i0,$Tbl
  977. $PTR_ADD $i1,$Tbl
  978. $PTR_ADD $i2,$Tbl
  979. $PTR_ADD $i3,$Tbl
  980. # else
  981. lbu $t4,1024($i0) # Td4[s2>>8]
  982. _xtr $i0,$s0,24
  983. lbu $t5,1024($i1) # Td4[s3>>8]
  984. _xtr $i1,$s1,24
  985. lbu $t6,1024($i2) # Td4[s0>>8]
  986. _xtr $i2,$s2,24
  987. lbu $t7,1024($i3) # Td4[s1>>8]
  988. _xtr $i3,$s3,24
  989. $PTR_ADD $i0,$Tbl
  990. $PTR_ADD $i1,$Tbl
  991. $PTR_ADD $i2,$Tbl
  992. $PTR_ADD $i3,$Tbl
  993. lbu $t8,1024($i0) # Td4[s0>>24]
  994. $PTR_INS $i0,$s1,0,8
  995. lbu $t9,1024($i1) # Td4[s1>>24]
  996. $PTR_INS $i1,$s2,0,8
  997. lbu $t10,1024($i2) # Td4[s2>>24]
  998. $PTR_INS $i2,$s3,0,8
  999. lbu $t11,1024($i3) # Td4[s3>>24]
  1000. $PTR_INS $i3,$s0,0,8
  1001. # endif
  1002. _ins $t0,16
  1003. _ins $t1,16
  1004. _ins $t2,16
  1005. _ins $t3,16
  1006. _ins2 $t0,$t4,8
  1007. lbu $t4,1024($i0) # Td4[s1]
  1008. _ins2 $t1,$t5,8
  1009. lbu $t5,1024($i1) # Td4[s2]
  1010. _ins2 $t2,$t6,8
  1011. lbu $t6,1024($i2) # Td4[s3]
  1012. _ins2 $t3,$t7,8
  1013. lbu $t7,1024($i3) # Td4[s0]
  1014. _ins2 $t0,$t8,24
  1015. lw $s0,0($key0)
  1016. _ins2 $t1,$t9,24
  1017. lw $s1,4($key0)
  1018. _ins2 $t2,$t10,24
  1019. lw $s2,8($key0)
  1020. _ins2 $t3,$t11,24
  1021. lw $s3,12($key0)
  1022. _ins2 $t0,$t4,0
  1023. _ins2 $t1,$t5,0
  1024. _ins2 $t2,$t6,0
  1025. _ins2 $t3,$t7,0
  1026. #else
  1027. lbu $t4,1024($i0) # Td4[s2>>8]
  1028. _xtr $i0,$s0,24
  1029. lbu $t5,1024($i1) # Td4[s3>>8]
  1030. _xtr $i1,$s1,24
  1031. lbu $t6,1024($i2) # Td4[s0>>8]
  1032. _xtr $i2,$s2,24
  1033. lbu $t7,1024($i3) # Td4[s1>>8]
  1034. _xtr $i3,$s3,24
  1035. $PTR_ADD $i0,$Tbl
  1036. $PTR_ADD $i1,$Tbl
  1037. $PTR_ADD $i2,$Tbl
  1038. $PTR_ADD $i3,$Tbl
  1039. lbu $t8,1024($i0) # Td4[s0>>24]
  1040. _xtr $i0,$s1,0
  1041. lbu $t9,1024($i1) # Td4[s1>>24]
  1042. _xtr $i1,$s2,0
  1043. lbu $t10,1024($i2) # Td4[s2>>24]
  1044. _xtr $i2,$s3,0
  1045. lbu $t11,1024($i3) # Td4[s3>>24]
  1046. _xtr $i3,$s0,0
  1047. $PTR_ADD $i0,$Tbl
  1048. $PTR_ADD $i1,$Tbl
  1049. $PTR_ADD $i2,$Tbl
  1050. $PTR_ADD $i3,$Tbl
  1051. _ins $t0,16
  1052. _ins $t1,16
  1053. _ins $t2,16
  1054. _ins $t3,16
  1055. _ins $t4,8
  1056. _ins $t5,8
  1057. _ins $t6,8
  1058. _ins $t7,8
  1059. xor $t0,$t4
  1060. lbu $t4,1024($i0) # Td4[s1]
  1061. xor $t1,$t5
  1062. lbu $t5,1024($i1) # Td4[s2]
  1063. xor $t2,$t6
  1064. lbu $t6,1024($i2) # Td4[s3]
  1065. xor $t3,$t7
  1066. lbu $t7,1024($i3) # Td4[s0]
  1067. _ins $t8,24
  1068. lw $s0,0($key0)
  1069. _ins $t9,24
  1070. lw $s1,4($key0)
  1071. _ins $t10,24
  1072. lw $s2,8($key0)
  1073. _ins $t11,24
  1074. lw $s3,12($key0)
  1075. xor $t0,$t8
  1076. xor $t1,$t9
  1077. xor $t2,$t10
  1078. xor $t3,$t11
  1079. _ins $t4,0
  1080. _ins $t5,0
  1081. _ins $t6,0
  1082. _ins $t7,0
  1083. xor $t0,$t4
  1084. xor $t1,$t5
  1085. xor $t2,$t6
  1086. xor $t3,$t7
  1087. #endif
  1088. xor $s0,$t0
  1089. xor $s1,$t1
  1090. xor $s2,$t2
  1091. xor $s3,$t3
  1092. jr $ra
  1093. .end _mips_AES_decrypt
  1094. .align 5
  1095. .globl AES_decrypt
  1096. .ent AES_decrypt
  1097. AES_decrypt:
  1098. .frame $sp,$FRAMESIZE,$ra
  1099. .mask $SAVED_REGS_MASK,-$SZREG
  1100. .set noreorder
  1101. ___
  1102. $code.=<<___ if ($flavour =~ /o32/i); # o32 PIC-ification
  1103. .cpload $pf
  1104. ___
  1105. $code.=<<___;
  1106. $PTR_SUB $sp,$FRAMESIZE
  1107. $REG_S $ra,$FRAMESIZE-1*$SZREG($sp)
  1108. $REG_S $fp,$FRAMESIZE-2*$SZREG($sp)
  1109. $REG_S $s11,$FRAMESIZE-3*$SZREG($sp)
  1110. $REG_S $s10,$FRAMESIZE-4*$SZREG($sp)
  1111. $REG_S $s9,$FRAMESIZE-5*$SZREG($sp)
  1112. $REG_S $s8,$FRAMESIZE-6*$SZREG($sp)
  1113. $REG_S $s7,$FRAMESIZE-7*$SZREG($sp)
  1114. $REG_S $s6,$FRAMESIZE-8*$SZREG($sp)
  1115. $REG_S $s5,$FRAMESIZE-9*$SZREG($sp)
  1116. $REG_S $s4,$FRAMESIZE-10*$SZREG($sp)
  1117. ___
  1118. $code.=<<___ if ($flavour =~ /nubi/i); # optimize non-nubi prologue
  1119. $REG_S \$15,$FRAMESIZE-11*$SZREG($sp)
  1120. $REG_S \$14,$FRAMESIZE-12*$SZREG($sp)
  1121. $REG_S \$13,$FRAMESIZE-13*$SZREG($sp)
  1122. $REG_S \$12,$FRAMESIZE-14*$SZREG($sp)
  1123. $REG_S $gp,$FRAMESIZE-15*$SZREG($sp)
  1124. ___
  1125. $code.=<<___ if ($flavour !~ /o32/i); # non-o32 PIC-ification
  1126. .cplocal $Tbl
  1127. .cpsetup $pf,$zero,AES_decrypt
  1128. ___
  1129. $code.=<<___;
  1130. .set reorder
  1131. $PTR_LA $Tbl,AES_Td # PIC-ified 'load address'
  1132. #if defined(_MIPS_ARCH_MIPS32R6) || defined(_MIPS_ARCH_MIPS64R6)
  1133. lw $s0,0($inp)
  1134. lw $s1,4($inp)
  1135. lw $s2,8($inp)
  1136. lw $s3,12($inp)
  1137. #else
  1138. lwl $s0,0+$MSB($inp)
  1139. lwl $s1,4+$MSB($inp)
  1140. lwl $s2,8+$MSB($inp)
  1141. lwl $s3,12+$MSB($inp)
  1142. lwr $s0,0+$LSB($inp)
  1143. lwr $s1,4+$LSB($inp)
  1144. lwr $s2,8+$LSB($inp)
  1145. lwr $s3,12+$LSB($inp)
  1146. #endif
  1147. bal _mips_AES_decrypt
  1148. #if defined(_MIPS_ARCH_MIPS32R6) || defined(_MIPS_ARCH_MIPS64R6)
  1149. sw $s0,0($out)
  1150. sw $s1,4($out)
  1151. sw $s2,8($out)
  1152. sw $s3,12($out)
  1153. #else
  1154. swr $s0,0+$LSB($out)
  1155. swr $s1,4+$LSB($out)
  1156. swr $s2,8+$LSB($out)
  1157. swr $s3,12+$LSB($out)
  1158. swl $s0,0+$MSB($out)
  1159. swl $s1,4+$MSB($out)
  1160. swl $s2,8+$MSB($out)
  1161. swl $s3,12+$MSB($out)
  1162. #endif
  1163. .set noreorder
  1164. $REG_L $ra,$FRAMESIZE-1*$SZREG($sp)
  1165. $REG_L $fp,$FRAMESIZE-2*$SZREG($sp)
  1166. $REG_L $s11,$FRAMESIZE-3*$SZREG($sp)
  1167. $REG_L $s10,$FRAMESIZE-4*$SZREG($sp)
  1168. $REG_L $s9,$FRAMESIZE-5*$SZREG($sp)
  1169. $REG_L $s8,$FRAMESIZE-6*$SZREG($sp)
  1170. $REG_L $s7,$FRAMESIZE-7*$SZREG($sp)
  1171. $REG_L $s6,$FRAMESIZE-8*$SZREG($sp)
  1172. $REG_L $s5,$FRAMESIZE-9*$SZREG($sp)
  1173. $REG_L $s4,$FRAMESIZE-10*$SZREG($sp)
  1174. ___
  1175. $code.=<<___ if ($flavour =~ /nubi/i);
  1176. $REG_L \$15,$FRAMESIZE-11*$SZREG($sp)
  1177. $REG_L \$14,$FRAMESIZE-12*$SZREG($sp)
  1178. $REG_L \$13,$FRAMESIZE-13*$SZREG($sp)
  1179. $REG_L \$12,$FRAMESIZE-14*$SZREG($sp)
  1180. $REG_L $gp,$FRAMESIZE-15*$SZREG($sp)
  1181. ___
  1182. $code.=<<___;
  1183. jr $ra
  1184. $PTR_ADD $sp,$FRAMESIZE
  1185. .end AES_decrypt
  1186. ___
  1187. }}}
  1188. {{{
  1189. my $FRAMESIZE=8*$SZREG;
  1190. my $SAVED_REGS_MASK = ($flavour =~ /nubi/i) ? "0xc000f008" : "0xc0000000";
  1191. my ($inp,$bits,$key,$Tbl)=($a0,$a1,$a2,$a3);
  1192. my ($rk0,$rk1,$rk2,$rk3,$rk4,$rk5,$rk6,$rk7)=($a4,$a5,$a6,$a7,$s0,$s1,$s2,$s3);
  1193. my ($i0,$i1,$i2,$i3)=($at,$t0,$t1,$t2);
  1194. my ($rcon,$cnt)=($gp,$fp);
  1195. $code.=<<___;
  1196. .align 5
  1197. .ent _mips_AES_set_encrypt_key
  1198. _mips_AES_set_encrypt_key:
  1199. .frame $sp,0,$ra
  1200. .set noreorder
  1201. beqz $inp,.Lekey_done
  1202. li $t0,-1
  1203. beqz $key,.Lekey_done
  1204. $PTR_ADD $rcon,$Tbl,256
  1205. .set reorder
  1206. #if defined(_MIPS_ARCH_MIPS32R6) || defined(_MIPS_ARCH_MIPS64R6)
  1207. lw $rk0,0($inp) # load 128 bits
  1208. lw $rk1,4($inp)
  1209. lw $rk2,8($inp)
  1210. lw $rk3,12($inp)
  1211. #else
  1212. lwl $rk0,0+$MSB($inp) # load 128 bits
  1213. lwl $rk1,4+$MSB($inp)
  1214. lwl $rk2,8+$MSB($inp)
  1215. lwl $rk3,12+$MSB($inp)
  1216. lwr $rk0,0+$LSB($inp)
  1217. lwr $rk1,4+$LSB($inp)
  1218. lwr $rk2,8+$LSB($inp)
  1219. lwr $rk3,12+$LSB($inp)
  1220. #endif
  1221. li $at,128
  1222. .set noreorder
  1223. beq $bits,$at,.L128bits
  1224. li $cnt,10
  1225. .set reorder
  1226. #if defined(_MIPS_ARCH_MIPS32R6) || defined(_MIPS_ARCH_MIPS64R6)
  1227. lw $rk4,16($inp) # load 192 bits
  1228. lw $rk5,20($inp)
  1229. #else
  1230. lwl $rk4,16+$MSB($inp) # load 192 bits
  1231. lwl $rk5,20+$MSB($inp)
  1232. lwr $rk4,16+$LSB($inp)
  1233. lwr $rk5,20+$LSB($inp)
  1234. #endif
  1235. li $at,192
  1236. .set noreorder
  1237. beq $bits,$at,.L192bits
  1238. li $cnt,8
  1239. .set reorder
  1240. #if defined(_MIPS_ARCH_MIPS32R6) || defined(_MIPS_ARCH_MIPS64R6)
  1241. lw $rk6,24($inp) # load 256 bits
  1242. lw $rk7,28($inp)
  1243. #else
  1244. lwl $rk6,24+$MSB($inp) # load 256 bits
  1245. lwl $rk7,28+$MSB($inp)
  1246. lwr $rk6,24+$LSB($inp)
  1247. lwr $rk7,28+$LSB($inp)
  1248. #endif
  1249. li $at,256
  1250. .set noreorder
  1251. beq $bits,$at,.L256bits
  1252. li $cnt,7
  1253. b .Lekey_done
  1254. li $t0,-2
  1255. .align 4
  1256. .L128bits:
  1257. .set reorder
  1258. srl $i0,$rk3,16
  1259. srl $i1,$rk3,8
  1260. and $i0,0xff
  1261. and $i1,0xff
  1262. and $i2,$rk3,0xff
  1263. srl $i3,$rk3,24
  1264. $PTR_ADD $i0,$Tbl
  1265. $PTR_ADD $i1,$Tbl
  1266. $PTR_ADD $i2,$Tbl
  1267. $PTR_ADD $i3,$Tbl
  1268. lbu $i0,0($i0)
  1269. lbu $i1,0($i1)
  1270. lbu $i2,0($i2)
  1271. lbu $i3,0($i3)
  1272. sw $rk0,0($key)
  1273. sw $rk1,4($key)
  1274. sw $rk2,8($key)
  1275. sw $rk3,12($key)
  1276. subu $cnt,1
  1277. $PTR_ADD $key,16
  1278. _bias $i0,24
  1279. _bias $i1,16
  1280. _bias $i2,8
  1281. _bias $i3,0
  1282. xor $rk0,$i0
  1283. lw $i0,0($rcon)
  1284. xor $rk0,$i1
  1285. xor $rk0,$i2
  1286. xor $rk0,$i3
  1287. xor $rk0,$i0
  1288. xor $rk1,$rk0
  1289. xor $rk2,$rk1
  1290. xor $rk3,$rk2
  1291. .set noreorder
  1292. bnez $cnt,.L128bits
  1293. $PTR_ADD $rcon,4
  1294. sw $rk0,0($key)
  1295. sw $rk1,4($key)
  1296. sw $rk2,8($key)
  1297. li $cnt,10
  1298. sw $rk3,12($key)
  1299. li $t0,0
  1300. sw $cnt,80($key)
  1301. b .Lekey_done
  1302. $PTR_SUB $key,10*16
  1303. .align 4
  1304. .L192bits:
  1305. .set reorder
  1306. srl $i0,$rk5,16
  1307. srl $i1,$rk5,8
  1308. and $i0,0xff
  1309. and $i1,0xff
  1310. and $i2,$rk5,0xff
  1311. srl $i3,$rk5,24
  1312. $PTR_ADD $i0,$Tbl
  1313. $PTR_ADD $i1,$Tbl
  1314. $PTR_ADD $i2,$Tbl
  1315. $PTR_ADD $i3,$Tbl
  1316. lbu $i0,0($i0)
  1317. lbu $i1,0($i1)
  1318. lbu $i2,0($i2)
  1319. lbu $i3,0($i3)
  1320. sw $rk0,0($key)
  1321. sw $rk1,4($key)
  1322. sw $rk2,8($key)
  1323. sw $rk3,12($key)
  1324. sw $rk4,16($key)
  1325. sw $rk5,20($key)
  1326. subu $cnt,1
  1327. $PTR_ADD $key,24
  1328. _bias $i0,24
  1329. _bias $i1,16
  1330. _bias $i2,8
  1331. _bias $i3,0
  1332. xor $rk0,$i0
  1333. lw $i0,0($rcon)
  1334. xor $rk0,$i1
  1335. xor $rk0,$i2
  1336. xor $rk0,$i3
  1337. xor $rk0,$i0
  1338. xor $rk1,$rk0
  1339. xor $rk2,$rk1
  1340. xor $rk3,$rk2
  1341. xor $rk4,$rk3
  1342. xor $rk5,$rk4
  1343. .set noreorder
  1344. bnez $cnt,.L192bits
  1345. $PTR_ADD $rcon,4
  1346. sw $rk0,0($key)
  1347. sw $rk1,4($key)
  1348. sw $rk2,8($key)
  1349. li $cnt,12
  1350. sw $rk3,12($key)
  1351. li $t0,0
  1352. sw $cnt,48($key)
  1353. b .Lekey_done
  1354. $PTR_SUB $key,12*16
  1355. .align 4
  1356. .L256bits:
  1357. .set reorder
  1358. srl $i0,$rk7,16
  1359. srl $i1,$rk7,8
  1360. and $i0,0xff
  1361. and $i1,0xff
  1362. and $i2,$rk7,0xff
  1363. srl $i3,$rk7,24
  1364. $PTR_ADD $i0,$Tbl
  1365. $PTR_ADD $i1,$Tbl
  1366. $PTR_ADD $i2,$Tbl
  1367. $PTR_ADD $i3,$Tbl
  1368. lbu $i0,0($i0)
  1369. lbu $i1,0($i1)
  1370. lbu $i2,0($i2)
  1371. lbu $i3,0($i3)
  1372. sw $rk0,0($key)
  1373. sw $rk1,4($key)
  1374. sw $rk2,8($key)
  1375. sw $rk3,12($key)
  1376. sw $rk4,16($key)
  1377. sw $rk5,20($key)
  1378. sw $rk6,24($key)
  1379. sw $rk7,28($key)
  1380. subu $cnt,1
  1381. _bias $i0,24
  1382. _bias $i1,16
  1383. _bias $i2,8
  1384. _bias $i3,0
  1385. xor $rk0,$i0
  1386. lw $i0,0($rcon)
  1387. xor $rk0,$i1
  1388. xor $rk0,$i2
  1389. xor $rk0,$i3
  1390. xor $rk0,$i0
  1391. xor $rk1,$rk0
  1392. xor $rk2,$rk1
  1393. xor $rk3,$rk2
  1394. beqz $cnt,.L256bits_done
  1395. srl $i0,$rk3,24
  1396. srl $i1,$rk3,16
  1397. srl $i2,$rk3,8
  1398. and $i3,$rk3,0xff
  1399. and $i1,0xff
  1400. and $i2,0xff
  1401. $PTR_ADD $i0,$Tbl
  1402. $PTR_ADD $i1,$Tbl
  1403. $PTR_ADD $i2,$Tbl
  1404. $PTR_ADD $i3,$Tbl
  1405. lbu $i0,0($i0)
  1406. lbu $i1,0($i1)
  1407. lbu $i2,0($i2)
  1408. lbu $i3,0($i3)
  1409. sll $i0,24
  1410. sll $i1,16
  1411. sll $i2,8
  1412. xor $rk4,$i0
  1413. xor $rk4,$i1
  1414. xor $rk4,$i2
  1415. xor $rk4,$i3
  1416. xor $rk5,$rk4
  1417. xor $rk6,$rk5
  1418. xor $rk7,$rk6
  1419. $PTR_ADD $key,32
  1420. .set noreorder
  1421. b .L256bits
  1422. $PTR_ADD $rcon,4
  1423. .L256bits_done:
  1424. sw $rk0,32($key)
  1425. sw $rk1,36($key)
  1426. sw $rk2,40($key)
  1427. li $cnt,14
  1428. sw $rk3,44($key)
  1429. li $t0,0
  1430. sw $cnt,48($key)
  1431. $PTR_SUB $key,12*16
  1432. .Lekey_done:
  1433. jr $ra
  1434. nop
  1435. .end _mips_AES_set_encrypt_key
  1436. .globl AES_set_encrypt_key
  1437. .ent AES_set_encrypt_key
  1438. AES_set_encrypt_key:
  1439. .frame $sp,$FRAMESIZE,$ra
  1440. .mask $SAVED_REGS_MASK,-$SZREG
  1441. .set noreorder
  1442. ___
  1443. $code.=<<___ if ($flavour =~ /o32/i); # o32 PIC-ification
  1444. .cpload $pf
  1445. ___
  1446. $code.=<<___;
  1447. $PTR_SUB $sp,$FRAMESIZE
  1448. $REG_S $ra,$FRAMESIZE-1*$SZREG($sp)
  1449. $REG_S $fp,$FRAMESIZE-2*$SZREG($sp)
  1450. ___
  1451. $code.=<<___ if ($flavour =~ /nubi/i); # optimize non-nubi prologue
  1452. $REG_S $s3,$FRAMESIZE-3*$SZREG($sp)
  1453. $REG_S $s2,$FRAMESIZE-4*$SZREG($sp)
  1454. $REG_S $s1,$FRAMESIZE-5*$SZREG($sp)
  1455. $REG_S $s0,$FRAMESIZE-6*$SZREG($sp)
  1456. $REG_S $gp,$FRAMESIZE-7*$SZREG($sp)
  1457. ___
  1458. $code.=<<___ if ($flavour !~ /o32/i); # non-o32 PIC-ification
  1459. .cplocal $Tbl
  1460. .cpsetup $pf,$zero,AES_set_encrypt_key
  1461. ___
  1462. $code.=<<___;
  1463. .set reorder
  1464. $PTR_LA $Tbl,AES_Te4 # PIC-ified 'load address'
  1465. bal _mips_AES_set_encrypt_key
  1466. .set noreorder
  1467. move $a0,$t0
  1468. $REG_L $ra,$FRAMESIZE-1*$SZREG($sp)
  1469. $REG_L $fp,$FRAMESIZE-2*$SZREG($sp)
  1470. ___
  1471. $code.=<<___ if ($flavour =~ /nubi/i);
  1472. $REG_L $s3,$FRAMESIZE-11*$SZREG($sp)
  1473. $REG_L $s2,$FRAMESIZE-12*$SZREG($sp)
  1474. $REG_L $s1,$FRAMESIZE-13*$SZREG($sp)
  1475. $REG_L $s0,$FRAMESIZE-14*$SZREG($sp)
  1476. $REG_L $gp,$FRAMESIZE-15*$SZREG($sp)
  1477. ___
  1478. $code.=<<___;
  1479. jr $ra
  1480. $PTR_ADD $sp,$FRAMESIZE
  1481. .end AES_set_encrypt_key
  1482. ___
  1483. my ($head,$tail)=($inp,$bits);
  1484. my ($tp1,$tp2,$tp4,$tp8,$tp9,$tpb,$tpd,$tpe)=($a4,$a5,$a6,$a7,$s0,$s1,$s2,$s3);
  1485. my ($m,$x80808080,$x7f7f7f7f,$x1b1b1b1b)=($at,$t0,$t1,$t2);
  1486. $code.=<<___;
  1487. .align 5
  1488. .globl AES_set_decrypt_key
  1489. .ent AES_set_decrypt_key
  1490. AES_set_decrypt_key:
  1491. .frame $sp,$FRAMESIZE,$ra
  1492. .mask $SAVED_REGS_MASK,-$SZREG
  1493. .set noreorder
  1494. ___
  1495. $code.=<<___ if ($flavour =~ /o32/i); # o32 PIC-ification
  1496. .cpload $pf
  1497. ___
  1498. $code.=<<___;
  1499. $PTR_SUB $sp,$FRAMESIZE
  1500. $REG_S $ra,$FRAMESIZE-1*$SZREG($sp)
  1501. $REG_S $fp,$FRAMESIZE-2*$SZREG($sp)
  1502. ___
  1503. $code.=<<___ if ($flavour =~ /nubi/i); # optimize non-nubi prologue
  1504. $REG_S $s3,$FRAMESIZE-3*$SZREG($sp)
  1505. $REG_S $s2,$FRAMESIZE-4*$SZREG($sp)
  1506. $REG_S $s1,$FRAMESIZE-5*$SZREG($sp)
  1507. $REG_S $s0,$FRAMESIZE-6*$SZREG($sp)
  1508. $REG_S $gp,$FRAMESIZE-7*$SZREG($sp)
  1509. ___
  1510. $code.=<<___ if ($flavour !~ /o32/i); # non-o32 PIC-ification
  1511. .cplocal $Tbl
  1512. .cpsetup $pf,$zero,AES_set_decrypt_key
  1513. ___
  1514. $code.=<<___;
  1515. .set reorder
  1516. $PTR_LA $Tbl,AES_Te4 # PIC-ified 'load address'
  1517. bal _mips_AES_set_encrypt_key
  1518. bltz $t0,.Ldkey_done
  1519. sll $at,$cnt,4
  1520. $PTR_ADD $head,$key,0
  1521. $PTR_ADD $tail,$key,$at
  1522. .align 4
  1523. .Lswap:
  1524. lw $rk0,0($head)
  1525. lw $rk1,4($head)
  1526. lw $rk2,8($head)
  1527. lw $rk3,12($head)
  1528. lw $rk4,0($tail)
  1529. lw $rk5,4($tail)
  1530. lw $rk6,8($tail)
  1531. lw $rk7,12($tail)
  1532. sw $rk0,0($tail)
  1533. sw $rk1,4($tail)
  1534. sw $rk2,8($tail)
  1535. sw $rk3,12($tail)
  1536. $PTR_ADD $head,16
  1537. $PTR_SUB $tail,16
  1538. sw $rk4,-16($head)
  1539. sw $rk5,-12($head)
  1540. sw $rk6,-8($head)
  1541. sw $rk7,-4($head)
  1542. bne $head,$tail,.Lswap
  1543. lw $tp1,16($key) # modulo-scheduled
  1544. lui $x80808080,0x8080
  1545. subu $cnt,1
  1546. or $x80808080,0x8080
  1547. sll $cnt,2
  1548. $PTR_ADD $key,16
  1549. lui $x1b1b1b1b,0x1b1b
  1550. nor $x7f7f7f7f,$zero,$x80808080
  1551. or $x1b1b1b1b,0x1b1b
  1552. .align 4
  1553. .Lmix:
  1554. and $m,$tp1,$x80808080
  1555. and $tp2,$tp1,$x7f7f7f7f
  1556. srl $tp4,$m,7
  1557. addu $tp2,$tp2 # tp2<<1
  1558. subu $m,$tp4
  1559. and $m,$x1b1b1b1b
  1560. xor $tp2,$m
  1561. and $m,$tp2,$x80808080
  1562. and $tp4,$tp2,$x7f7f7f7f
  1563. srl $tp8,$m,7
  1564. addu $tp4,$tp4 # tp4<<1
  1565. subu $m,$tp8
  1566. and $m,$x1b1b1b1b
  1567. xor $tp4,$m
  1568. and $m,$tp4,$x80808080
  1569. and $tp8,$tp4,$x7f7f7f7f
  1570. srl $tp9,$m,7
  1571. addu $tp8,$tp8 # tp8<<1
  1572. subu $m,$tp9
  1573. and $m,$x1b1b1b1b
  1574. xor $tp8,$m
  1575. xor $tp9,$tp8,$tp1
  1576. xor $tpe,$tp8,$tp4
  1577. xor $tpb,$tp9,$tp2
  1578. xor $tpd,$tp9,$tp4
  1579. #if defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2)
  1580. rotr $tp1,$tpd,16
  1581. xor $tpe,$tp2
  1582. rotr $tp2,$tp9,8
  1583. xor $tpe,$tp1
  1584. rotr $tp4,$tpb,24
  1585. xor $tpe,$tp2
  1586. lw $tp1,4($key) # modulo-scheduled
  1587. xor $tpe,$tp4
  1588. #else
  1589. _ror $tp1,$tpd,16
  1590. xor $tpe,$tp2
  1591. _ror $tp2,$tpd,-16
  1592. xor $tpe,$tp1
  1593. _ror $tp1,$tp9,8
  1594. xor $tpe,$tp2
  1595. _ror $tp2,$tp9,-24
  1596. xor $tpe,$tp1
  1597. _ror $tp1,$tpb,24
  1598. xor $tpe,$tp2
  1599. _ror $tp2,$tpb,-8
  1600. xor $tpe,$tp1
  1601. lw $tp1,4($key) # modulo-scheduled
  1602. xor $tpe,$tp2
  1603. #endif
  1604. subu $cnt,1
  1605. sw $tpe,0($key)
  1606. $PTR_ADD $key,4
  1607. bnez $cnt,.Lmix
  1608. li $t0,0
  1609. .Ldkey_done:
  1610. .set noreorder
  1611. move $a0,$t0
  1612. $REG_L $ra,$FRAMESIZE-1*$SZREG($sp)
  1613. $REG_L $fp,$FRAMESIZE-2*$SZREG($sp)
  1614. ___
  1615. $code.=<<___ if ($flavour =~ /nubi/i);
  1616. $REG_L $s3,$FRAMESIZE-11*$SZREG($sp)
  1617. $REG_L $s2,$FRAMESIZE-12*$SZREG($sp)
  1618. $REG_L $s1,$FRAMESIZE-13*$SZREG($sp)
  1619. $REG_L $s0,$FRAMESIZE-14*$SZREG($sp)
  1620. $REG_L $gp,$FRAMESIZE-15*$SZREG($sp)
  1621. ___
  1622. $code.=<<___;
  1623. jr $ra
  1624. $PTR_ADD $sp,$FRAMESIZE
  1625. .end AES_set_decrypt_key
  1626. ___
  1627. }}}
  1628. ######################################################################
  1629. # Tables are kept in endian-neutral manner
  1630. $code.=<<___;
  1631. .rdata
  1632. .align 10
  1633. AES_Te:
  1634. .byte 0xc6,0x63,0x63,0xa5, 0xf8,0x7c,0x7c,0x84 # Te0
  1635. .byte 0xee,0x77,0x77,0x99, 0xf6,0x7b,0x7b,0x8d
  1636. .byte 0xff,0xf2,0xf2,0x0d, 0xd6,0x6b,0x6b,0xbd
  1637. .byte 0xde,0x6f,0x6f,0xb1, 0x91,0xc5,0xc5,0x54
  1638. .byte 0x60,0x30,0x30,0x50, 0x02,0x01,0x01,0x03
  1639. .byte 0xce,0x67,0x67,0xa9, 0x56,0x2b,0x2b,0x7d
  1640. .byte 0xe7,0xfe,0xfe,0x19, 0xb5,0xd7,0xd7,0x62
  1641. .byte 0x4d,0xab,0xab,0xe6, 0xec,0x76,0x76,0x9a
  1642. .byte 0x8f,0xca,0xca,0x45, 0x1f,0x82,0x82,0x9d
  1643. .byte 0x89,0xc9,0xc9,0x40, 0xfa,0x7d,0x7d,0x87
  1644. .byte 0xef,0xfa,0xfa,0x15, 0xb2,0x59,0x59,0xeb
  1645. .byte 0x8e,0x47,0x47,0xc9, 0xfb,0xf0,0xf0,0x0b
  1646. .byte 0x41,0xad,0xad,0xec, 0xb3,0xd4,0xd4,0x67
  1647. .byte 0x5f,0xa2,0xa2,0xfd, 0x45,0xaf,0xaf,0xea
  1648. .byte 0x23,0x9c,0x9c,0xbf, 0x53,0xa4,0xa4,0xf7
  1649. .byte 0xe4,0x72,0x72,0x96, 0x9b,0xc0,0xc0,0x5b
  1650. .byte 0x75,0xb7,0xb7,0xc2, 0xe1,0xfd,0xfd,0x1c
  1651. .byte 0x3d,0x93,0x93,0xae, 0x4c,0x26,0x26,0x6a
  1652. .byte 0x6c,0x36,0x36,0x5a, 0x7e,0x3f,0x3f,0x41
  1653. .byte 0xf5,0xf7,0xf7,0x02, 0x83,0xcc,0xcc,0x4f
  1654. .byte 0x68,0x34,0x34,0x5c, 0x51,0xa5,0xa5,0xf4
  1655. .byte 0xd1,0xe5,0xe5,0x34, 0xf9,0xf1,0xf1,0x08
  1656. .byte 0xe2,0x71,0x71,0x93, 0xab,0xd8,0xd8,0x73
  1657. .byte 0x62,0x31,0x31,0x53, 0x2a,0x15,0x15,0x3f
  1658. .byte 0x08,0x04,0x04,0x0c, 0x95,0xc7,0xc7,0x52
  1659. .byte 0x46,0x23,0x23,0x65, 0x9d,0xc3,0xc3,0x5e
  1660. .byte 0x30,0x18,0x18,0x28, 0x37,0x96,0x96,0xa1
  1661. .byte 0x0a,0x05,0x05,0x0f, 0x2f,0x9a,0x9a,0xb5
  1662. .byte 0x0e,0x07,0x07,0x09, 0x24,0x12,0x12,0x36
  1663. .byte 0x1b,0x80,0x80,0x9b, 0xdf,0xe2,0xe2,0x3d
  1664. .byte 0xcd,0xeb,0xeb,0x26, 0x4e,0x27,0x27,0x69
  1665. .byte 0x7f,0xb2,0xb2,0xcd, 0xea,0x75,0x75,0x9f
  1666. .byte 0x12,0x09,0x09,0x1b, 0x1d,0x83,0x83,0x9e
  1667. .byte 0x58,0x2c,0x2c,0x74, 0x34,0x1a,0x1a,0x2e
  1668. .byte 0x36,0x1b,0x1b,0x2d, 0xdc,0x6e,0x6e,0xb2
  1669. .byte 0xb4,0x5a,0x5a,0xee, 0x5b,0xa0,0xa0,0xfb
  1670. .byte 0xa4,0x52,0x52,0xf6, 0x76,0x3b,0x3b,0x4d
  1671. .byte 0xb7,0xd6,0xd6,0x61, 0x7d,0xb3,0xb3,0xce
  1672. .byte 0x52,0x29,0x29,0x7b, 0xdd,0xe3,0xe3,0x3e
  1673. .byte 0x5e,0x2f,0x2f,0x71, 0x13,0x84,0x84,0x97
  1674. .byte 0xa6,0x53,0x53,0xf5, 0xb9,0xd1,0xd1,0x68
  1675. .byte 0x00,0x00,0x00,0x00, 0xc1,0xed,0xed,0x2c
  1676. .byte 0x40,0x20,0x20,0x60, 0xe3,0xfc,0xfc,0x1f
  1677. .byte 0x79,0xb1,0xb1,0xc8, 0xb6,0x5b,0x5b,0xed
  1678. .byte 0xd4,0x6a,0x6a,0xbe, 0x8d,0xcb,0xcb,0x46
  1679. .byte 0x67,0xbe,0xbe,0xd9, 0x72,0x39,0x39,0x4b
  1680. .byte 0x94,0x4a,0x4a,0xde, 0x98,0x4c,0x4c,0xd4
  1681. .byte 0xb0,0x58,0x58,0xe8, 0x85,0xcf,0xcf,0x4a
  1682. .byte 0xbb,0xd0,0xd0,0x6b, 0xc5,0xef,0xef,0x2a
  1683. .byte 0x4f,0xaa,0xaa,0xe5, 0xed,0xfb,0xfb,0x16
  1684. .byte 0x86,0x43,0x43,0xc5, 0x9a,0x4d,0x4d,0xd7
  1685. .byte 0x66,0x33,0x33,0x55, 0x11,0x85,0x85,0x94
  1686. .byte 0x8a,0x45,0x45,0xcf, 0xe9,0xf9,0xf9,0x10
  1687. .byte 0x04,0x02,0x02,0x06, 0xfe,0x7f,0x7f,0x81
  1688. .byte 0xa0,0x50,0x50,0xf0, 0x78,0x3c,0x3c,0x44
  1689. .byte 0x25,0x9f,0x9f,0xba, 0x4b,0xa8,0xa8,0xe3
  1690. .byte 0xa2,0x51,0x51,0xf3, 0x5d,0xa3,0xa3,0xfe
  1691. .byte 0x80,0x40,0x40,0xc0, 0x05,0x8f,0x8f,0x8a
  1692. .byte 0x3f,0x92,0x92,0xad, 0x21,0x9d,0x9d,0xbc
  1693. .byte 0x70,0x38,0x38,0x48, 0xf1,0xf5,0xf5,0x04
  1694. .byte 0x63,0xbc,0xbc,0xdf, 0x77,0xb6,0xb6,0xc1
  1695. .byte 0xaf,0xda,0xda,0x75, 0x42,0x21,0x21,0x63
  1696. .byte 0x20,0x10,0x10,0x30, 0xe5,0xff,0xff,0x1a
  1697. .byte 0xfd,0xf3,0xf3,0x0e, 0xbf,0xd2,0xd2,0x6d
  1698. .byte 0x81,0xcd,0xcd,0x4c, 0x18,0x0c,0x0c,0x14
  1699. .byte 0x26,0x13,0x13,0x35, 0xc3,0xec,0xec,0x2f
  1700. .byte 0xbe,0x5f,0x5f,0xe1, 0x35,0x97,0x97,0xa2
  1701. .byte 0x88,0x44,0x44,0xcc, 0x2e,0x17,0x17,0x39
  1702. .byte 0x93,0xc4,0xc4,0x57, 0x55,0xa7,0xa7,0xf2
  1703. .byte 0xfc,0x7e,0x7e,0x82, 0x7a,0x3d,0x3d,0x47
  1704. .byte 0xc8,0x64,0x64,0xac, 0xba,0x5d,0x5d,0xe7
  1705. .byte 0x32,0x19,0x19,0x2b, 0xe6,0x73,0x73,0x95
  1706. .byte 0xc0,0x60,0x60,0xa0, 0x19,0x81,0x81,0x98
  1707. .byte 0x9e,0x4f,0x4f,0xd1, 0xa3,0xdc,0xdc,0x7f
  1708. .byte 0x44,0x22,0x22,0x66, 0x54,0x2a,0x2a,0x7e
  1709. .byte 0x3b,0x90,0x90,0xab, 0x0b,0x88,0x88,0x83
  1710. .byte 0x8c,0x46,0x46,0xca, 0xc7,0xee,0xee,0x29
  1711. .byte 0x6b,0xb8,0xb8,0xd3, 0x28,0x14,0x14,0x3c
  1712. .byte 0xa7,0xde,0xde,0x79, 0xbc,0x5e,0x5e,0xe2
  1713. .byte 0x16,0x0b,0x0b,0x1d, 0xad,0xdb,0xdb,0x76
  1714. .byte 0xdb,0xe0,0xe0,0x3b, 0x64,0x32,0x32,0x56
  1715. .byte 0x74,0x3a,0x3a,0x4e, 0x14,0x0a,0x0a,0x1e
  1716. .byte 0x92,0x49,0x49,0xdb, 0x0c,0x06,0x06,0x0a
  1717. .byte 0x48,0x24,0x24,0x6c, 0xb8,0x5c,0x5c,0xe4
  1718. .byte 0x9f,0xc2,0xc2,0x5d, 0xbd,0xd3,0xd3,0x6e
  1719. .byte 0x43,0xac,0xac,0xef, 0xc4,0x62,0x62,0xa6
  1720. .byte 0x39,0x91,0x91,0xa8, 0x31,0x95,0x95,0xa4
  1721. .byte 0xd3,0xe4,0xe4,0x37, 0xf2,0x79,0x79,0x8b
  1722. .byte 0xd5,0xe7,0xe7,0x32, 0x8b,0xc8,0xc8,0x43
  1723. .byte 0x6e,0x37,0x37,0x59, 0xda,0x6d,0x6d,0xb7
  1724. .byte 0x01,0x8d,0x8d,0x8c, 0xb1,0xd5,0xd5,0x64
  1725. .byte 0x9c,0x4e,0x4e,0xd2, 0x49,0xa9,0xa9,0xe0
  1726. .byte 0xd8,0x6c,0x6c,0xb4, 0xac,0x56,0x56,0xfa
  1727. .byte 0xf3,0xf4,0xf4,0x07, 0xcf,0xea,0xea,0x25
  1728. .byte 0xca,0x65,0x65,0xaf, 0xf4,0x7a,0x7a,0x8e
  1729. .byte 0x47,0xae,0xae,0xe9, 0x10,0x08,0x08,0x18
  1730. .byte 0x6f,0xba,0xba,0xd5, 0xf0,0x78,0x78,0x88
  1731. .byte 0x4a,0x25,0x25,0x6f, 0x5c,0x2e,0x2e,0x72
  1732. .byte 0x38,0x1c,0x1c,0x24, 0x57,0xa6,0xa6,0xf1
  1733. .byte 0x73,0xb4,0xb4,0xc7, 0x97,0xc6,0xc6,0x51
  1734. .byte 0xcb,0xe8,0xe8,0x23, 0xa1,0xdd,0xdd,0x7c
  1735. .byte 0xe8,0x74,0x74,0x9c, 0x3e,0x1f,0x1f,0x21
  1736. .byte 0x96,0x4b,0x4b,0xdd, 0x61,0xbd,0xbd,0xdc
  1737. .byte 0x0d,0x8b,0x8b,0x86, 0x0f,0x8a,0x8a,0x85
  1738. .byte 0xe0,0x70,0x70,0x90, 0x7c,0x3e,0x3e,0x42
  1739. .byte 0x71,0xb5,0xb5,0xc4, 0xcc,0x66,0x66,0xaa
  1740. .byte 0x90,0x48,0x48,0xd8, 0x06,0x03,0x03,0x05
  1741. .byte 0xf7,0xf6,0xf6,0x01, 0x1c,0x0e,0x0e,0x12
  1742. .byte 0xc2,0x61,0x61,0xa3, 0x6a,0x35,0x35,0x5f
  1743. .byte 0xae,0x57,0x57,0xf9, 0x69,0xb9,0xb9,0xd0
  1744. .byte 0x17,0x86,0x86,0x91, 0x99,0xc1,0xc1,0x58
  1745. .byte 0x3a,0x1d,0x1d,0x27, 0x27,0x9e,0x9e,0xb9
  1746. .byte 0xd9,0xe1,0xe1,0x38, 0xeb,0xf8,0xf8,0x13
  1747. .byte 0x2b,0x98,0x98,0xb3, 0x22,0x11,0x11,0x33
  1748. .byte 0xd2,0x69,0x69,0xbb, 0xa9,0xd9,0xd9,0x70
  1749. .byte 0x07,0x8e,0x8e,0x89, 0x33,0x94,0x94,0xa7
  1750. .byte 0x2d,0x9b,0x9b,0xb6, 0x3c,0x1e,0x1e,0x22
  1751. .byte 0x15,0x87,0x87,0x92, 0xc9,0xe9,0xe9,0x20
  1752. .byte 0x87,0xce,0xce,0x49, 0xaa,0x55,0x55,0xff
  1753. .byte 0x50,0x28,0x28,0x78, 0xa5,0xdf,0xdf,0x7a
  1754. .byte 0x03,0x8c,0x8c,0x8f, 0x59,0xa1,0xa1,0xf8
  1755. .byte 0x09,0x89,0x89,0x80, 0x1a,0x0d,0x0d,0x17
  1756. .byte 0x65,0xbf,0xbf,0xda, 0xd7,0xe6,0xe6,0x31
  1757. .byte 0x84,0x42,0x42,0xc6, 0xd0,0x68,0x68,0xb8
  1758. .byte 0x82,0x41,0x41,0xc3, 0x29,0x99,0x99,0xb0
  1759. .byte 0x5a,0x2d,0x2d,0x77, 0x1e,0x0f,0x0f,0x11
  1760. .byte 0x7b,0xb0,0xb0,0xcb, 0xa8,0x54,0x54,0xfc
  1761. .byte 0x6d,0xbb,0xbb,0xd6, 0x2c,0x16,0x16,0x3a
  1762. AES_Td:
  1763. .byte 0x51,0xf4,0xa7,0x50, 0x7e,0x41,0x65,0x53 # Td0
  1764. .byte 0x1a,0x17,0xa4,0xc3, 0x3a,0x27,0x5e,0x96
  1765. .byte 0x3b,0xab,0x6b,0xcb, 0x1f,0x9d,0x45,0xf1
  1766. .byte 0xac,0xfa,0x58,0xab, 0x4b,0xe3,0x03,0x93
  1767. .byte 0x20,0x30,0xfa,0x55, 0xad,0x76,0x6d,0xf6
  1768. .byte 0x88,0xcc,0x76,0x91, 0xf5,0x02,0x4c,0x25
  1769. .byte 0x4f,0xe5,0xd7,0xfc, 0xc5,0x2a,0xcb,0xd7
  1770. .byte 0x26,0x35,0x44,0x80, 0xb5,0x62,0xa3,0x8f
  1771. .byte 0xde,0xb1,0x5a,0x49, 0x25,0xba,0x1b,0x67
  1772. .byte 0x45,0xea,0x0e,0x98, 0x5d,0xfe,0xc0,0xe1
  1773. .byte 0xc3,0x2f,0x75,0x02, 0x81,0x4c,0xf0,0x12
  1774. .byte 0x8d,0x46,0x97,0xa3, 0x6b,0xd3,0xf9,0xc6
  1775. .byte 0x03,0x8f,0x5f,0xe7, 0x15,0x92,0x9c,0x95
  1776. .byte 0xbf,0x6d,0x7a,0xeb, 0x95,0x52,0x59,0xda
  1777. .byte 0xd4,0xbe,0x83,0x2d, 0x58,0x74,0x21,0xd3
  1778. .byte 0x49,0xe0,0x69,0x29, 0x8e,0xc9,0xc8,0x44
  1779. .byte 0x75,0xc2,0x89,0x6a, 0xf4,0x8e,0x79,0x78
  1780. .byte 0x99,0x58,0x3e,0x6b, 0x27,0xb9,0x71,0xdd
  1781. .byte 0xbe,0xe1,0x4f,0xb6, 0xf0,0x88,0xad,0x17
  1782. .byte 0xc9,0x20,0xac,0x66, 0x7d,0xce,0x3a,0xb4
  1783. .byte 0x63,0xdf,0x4a,0x18, 0xe5,0x1a,0x31,0x82
  1784. .byte 0x97,0x51,0x33,0x60, 0x62,0x53,0x7f,0x45
  1785. .byte 0xb1,0x64,0x77,0xe0, 0xbb,0x6b,0xae,0x84
  1786. .byte 0xfe,0x81,0xa0,0x1c, 0xf9,0x08,0x2b,0x94
  1787. .byte 0x70,0x48,0x68,0x58, 0x8f,0x45,0xfd,0x19
  1788. .byte 0x94,0xde,0x6c,0x87, 0x52,0x7b,0xf8,0xb7
  1789. .byte 0xab,0x73,0xd3,0x23, 0x72,0x4b,0x02,0xe2
  1790. .byte 0xe3,0x1f,0x8f,0x57, 0x66,0x55,0xab,0x2a
  1791. .byte 0xb2,0xeb,0x28,0x07, 0x2f,0xb5,0xc2,0x03
  1792. .byte 0x86,0xc5,0x7b,0x9a, 0xd3,0x37,0x08,0xa5
  1793. .byte 0x30,0x28,0x87,0xf2, 0x23,0xbf,0xa5,0xb2
  1794. .byte 0x02,0x03,0x6a,0xba, 0xed,0x16,0x82,0x5c
  1795. .byte 0x8a,0xcf,0x1c,0x2b, 0xa7,0x79,0xb4,0x92
  1796. .byte 0xf3,0x07,0xf2,0xf0, 0x4e,0x69,0xe2,0xa1
  1797. .byte 0x65,0xda,0xf4,0xcd, 0x06,0x05,0xbe,0xd5
  1798. .byte 0xd1,0x34,0x62,0x1f, 0xc4,0xa6,0xfe,0x8a
  1799. .byte 0x34,0x2e,0x53,0x9d, 0xa2,0xf3,0x55,0xa0
  1800. .byte 0x05,0x8a,0xe1,0x32, 0xa4,0xf6,0xeb,0x75
  1801. .byte 0x0b,0x83,0xec,0x39, 0x40,0x60,0xef,0xaa
  1802. .byte 0x5e,0x71,0x9f,0x06, 0xbd,0x6e,0x10,0x51
  1803. .byte 0x3e,0x21,0x8a,0xf9, 0x96,0xdd,0x06,0x3d
  1804. .byte 0xdd,0x3e,0x05,0xae, 0x4d,0xe6,0xbd,0x46
  1805. .byte 0x91,0x54,0x8d,0xb5, 0x71,0xc4,0x5d,0x05
  1806. .byte 0x04,0x06,0xd4,0x6f, 0x60,0x50,0x15,0xff
  1807. .byte 0x19,0x98,0xfb,0x24, 0xd6,0xbd,0xe9,0x97
  1808. .byte 0x89,0x40,0x43,0xcc, 0x67,0xd9,0x9e,0x77
  1809. .byte 0xb0,0xe8,0x42,0xbd, 0x07,0x89,0x8b,0x88
  1810. .byte 0xe7,0x19,0x5b,0x38, 0x79,0xc8,0xee,0xdb
  1811. .byte 0xa1,0x7c,0x0a,0x47, 0x7c,0x42,0x0f,0xe9
  1812. .byte 0xf8,0x84,0x1e,0xc9, 0x00,0x00,0x00,0x00
  1813. .byte 0x09,0x80,0x86,0x83, 0x32,0x2b,0xed,0x48
  1814. .byte 0x1e,0x11,0x70,0xac, 0x6c,0x5a,0x72,0x4e
  1815. .byte 0xfd,0x0e,0xff,0xfb, 0x0f,0x85,0x38,0x56
  1816. .byte 0x3d,0xae,0xd5,0x1e, 0x36,0x2d,0x39,0x27
  1817. .byte 0x0a,0x0f,0xd9,0x64, 0x68,0x5c,0xa6,0x21
  1818. .byte 0x9b,0x5b,0x54,0xd1, 0x24,0x36,0x2e,0x3a
  1819. .byte 0x0c,0x0a,0x67,0xb1, 0x93,0x57,0xe7,0x0f
  1820. .byte 0xb4,0xee,0x96,0xd2, 0x1b,0x9b,0x91,0x9e
  1821. .byte 0x80,0xc0,0xc5,0x4f, 0x61,0xdc,0x20,0xa2
  1822. .byte 0x5a,0x77,0x4b,0x69, 0x1c,0x12,0x1a,0x16
  1823. .byte 0xe2,0x93,0xba,0x0a, 0xc0,0xa0,0x2a,0xe5
  1824. .byte 0x3c,0x22,0xe0,0x43, 0x12,0x1b,0x17,0x1d
  1825. .byte 0x0e,0x09,0x0d,0x0b, 0xf2,0x8b,0xc7,0xad
  1826. .byte 0x2d,0xb6,0xa8,0xb9, 0x14,0x1e,0xa9,0xc8
  1827. .byte 0x57,0xf1,0x19,0x85, 0xaf,0x75,0x07,0x4c
  1828. .byte 0xee,0x99,0xdd,0xbb, 0xa3,0x7f,0x60,0xfd
  1829. .byte 0xf7,0x01,0x26,0x9f, 0x5c,0x72,0xf5,0xbc
  1830. .byte 0x44,0x66,0x3b,0xc5, 0x5b,0xfb,0x7e,0x34
  1831. .byte 0x8b,0x43,0x29,0x76, 0xcb,0x23,0xc6,0xdc
  1832. .byte 0xb6,0xed,0xfc,0x68, 0xb8,0xe4,0xf1,0x63
  1833. .byte 0xd7,0x31,0xdc,0xca, 0x42,0x63,0x85,0x10
  1834. .byte 0x13,0x97,0x22,0x40, 0x84,0xc6,0x11,0x20
  1835. .byte 0x85,0x4a,0x24,0x7d, 0xd2,0xbb,0x3d,0xf8
  1836. .byte 0xae,0xf9,0x32,0x11, 0xc7,0x29,0xa1,0x6d
  1837. .byte 0x1d,0x9e,0x2f,0x4b, 0xdc,0xb2,0x30,0xf3
  1838. .byte 0x0d,0x86,0x52,0xec, 0x77,0xc1,0xe3,0xd0
  1839. .byte 0x2b,0xb3,0x16,0x6c, 0xa9,0x70,0xb9,0x99
  1840. .byte 0x11,0x94,0x48,0xfa, 0x47,0xe9,0x64,0x22
  1841. .byte 0xa8,0xfc,0x8c,0xc4, 0xa0,0xf0,0x3f,0x1a
  1842. .byte 0x56,0x7d,0x2c,0xd8, 0x22,0x33,0x90,0xef
  1843. .byte 0x87,0x49,0x4e,0xc7, 0xd9,0x38,0xd1,0xc1
  1844. .byte 0x8c,0xca,0xa2,0xfe, 0x98,0xd4,0x0b,0x36
  1845. .byte 0xa6,0xf5,0x81,0xcf, 0xa5,0x7a,0xde,0x28
  1846. .byte 0xda,0xb7,0x8e,0x26, 0x3f,0xad,0xbf,0xa4
  1847. .byte 0x2c,0x3a,0x9d,0xe4, 0x50,0x78,0x92,0x0d
  1848. .byte 0x6a,0x5f,0xcc,0x9b, 0x54,0x7e,0x46,0x62
  1849. .byte 0xf6,0x8d,0x13,0xc2, 0x90,0xd8,0xb8,0xe8
  1850. .byte 0x2e,0x39,0xf7,0x5e, 0x82,0xc3,0xaf,0xf5
  1851. .byte 0x9f,0x5d,0x80,0xbe, 0x69,0xd0,0x93,0x7c
  1852. .byte 0x6f,0xd5,0x2d,0xa9, 0xcf,0x25,0x12,0xb3
  1853. .byte 0xc8,0xac,0x99,0x3b, 0x10,0x18,0x7d,0xa7
  1854. .byte 0xe8,0x9c,0x63,0x6e, 0xdb,0x3b,0xbb,0x7b
  1855. .byte 0xcd,0x26,0x78,0x09, 0x6e,0x59,0x18,0xf4
  1856. .byte 0xec,0x9a,0xb7,0x01, 0x83,0x4f,0x9a,0xa8
  1857. .byte 0xe6,0x95,0x6e,0x65, 0xaa,0xff,0xe6,0x7e
  1858. .byte 0x21,0xbc,0xcf,0x08, 0xef,0x15,0xe8,0xe6
  1859. .byte 0xba,0xe7,0x9b,0xd9, 0x4a,0x6f,0x36,0xce
  1860. .byte 0xea,0x9f,0x09,0xd4, 0x29,0xb0,0x7c,0xd6
  1861. .byte 0x31,0xa4,0xb2,0xaf, 0x2a,0x3f,0x23,0x31
  1862. .byte 0xc6,0xa5,0x94,0x30, 0x35,0xa2,0x66,0xc0
  1863. .byte 0x74,0x4e,0xbc,0x37, 0xfc,0x82,0xca,0xa6
  1864. .byte 0xe0,0x90,0xd0,0xb0, 0x33,0xa7,0xd8,0x15
  1865. .byte 0xf1,0x04,0x98,0x4a, 0x41,0xec,0xda,0xf7
  1866. .byte 0x7f,0xcd,0x50,0x0e, 0x17,0x91,0xf6,0x2f
  1867. .byte 0x76,0x4d,0xd6,0x8d, 0x43,0xef,0xb0,0x4d
  1868. .byte 0xcc,0xaa,0x4d,0x54, 0xe4,0x96,0x04,0xdf
  1869. .byte 0x9e,0xd1,0xb5,0xe3, 0x4c,0x6a,0x88,0x1b
  1870. .byte 0xc1,0x2c,0x1f,0xb8, 0x46,0x65,0x51,0x7f
  1871. .byte 0x9d,0x5e,0xea,0x04, 0x01,0x8c,0x35,0x5d
  1872. .byte 0xfa,0x87,0x74,0x73, 0xfb,0x0b,0x41,0x2e
  1873. .byte 0xb3,0x67,0x1d,0x5a, 0x92,0xdb,0xd2,0x52
  1874. .byte 0xe9,0x10,0x56,0x33, 0x6d,0xd6,0x47,0x13
  1875. .byte 0x9a,0xd7,0x61,0x8c, 0x37,0xa1,0x0c,0x7a
  1876. .byte 0x59,0xf8,0x14,0x8e, 0xeb,0x13,0x3c,0x89
  1877. .byte 0xce,0xa9,0x27,0xee, 0xb7,0x61,0xc9,0x35
  1878. .byte 0xe1,0x1c,0xe5,0xed, 0x7a,0x47,0xb1,0x3c
  1879. .byte 0x9c,0xd2,0xdf,0x59, 0x55,0xf2,0x73,0x3f
  1880. .byte 0x18,0x14,0xce,0x79, 0x73,0xc7,0x37,0xbf
  1881. .byte 0x53,0xf7,0xcd,0xea, 0x5f,0xfd,0xaa,0x5b
  1882. .byte 0xdf,0x3d,0x6f,0x14, 0x78,0x44,0xdb,0x86
  1883. .byte 0xca,0xaf,0xf3,0x81, 0xb9,0x68,0xc4,0x3e
  1884. .byte 0x38,0x24,0x34,0x2c, 0xc2,0xa3,0x40,0x5f
  1885. .byte 0x16,0x1d,0xc3,0x72, 0xbc,0xe2,0x25,0x0c
  1886. .byte 0x28,0x3c,0x49,0x8b, 0xff,0x0d,0x95,0x41
  1887. .byte 0x39,0xa8,0x01,0x71, 0x08,0x0c,0xb3,0xde
  1888. .byte 0xd8,0xb4,0xe4,0x9c, 0x64,0x56,0xc1,0x90
  1889. .byte 0x7b,0xcb,0x84,0x61, 0xd5,0x32,0xb6,0x70
  1890. .byte 0x48,0x6c,0x5c,0x74, 0xd0,0xb8,0x57,0x42
  1891. .byte 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38 # Td4
  1892. .byte 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb
  1893. .byte 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87
  1894. .byte 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb
  1895. .byte 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d
  1896. .byte 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e
  1897. .byte 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2
  1898. .byte 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25
  1899. .byte 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16
  1900. .byte 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92
  1901. .byte 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda
  1902. .byte 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84
  1903. .byte 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a
  1904. .byte 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06
  1905. .byte 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02
  1906. .byte 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b
  1907. .byte 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea
  1908. .byte 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73
  1909. .byte 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85
  1910. .byte 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e
  1911. .byte 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89
  1912. .byte 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b
  1913. .byte 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20
  1914. .byte 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4
  1915. .byte 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31
  1916. .byte 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f
  1917. .byte 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d
  1918. .byte 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef
  1919. .byte 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0
  1920. .byte 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61
  1921. .byte 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26
  1922. .byte 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
  1923. AES_Te4:
  1924. .byte 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5 # Te4
  1925. .byte 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76
  1926. .byte 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0
  1927. .byte 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0
  1928. .byte 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc
  1929. .byte 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15
  1930. .byte 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a
  1931. .byte 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75
  1932. .byte 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0
  1933. .byte 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84
  1934. .byte 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b
  1935. .byte 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf
  1936. .byte 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85
  1937. .byte 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8
  1938. .byte 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5
  1939. .byte 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2
  1940. .byte 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17
  1941. .byte 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73
  1942. .byte 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88
  1943. .byte 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb
  1944. .byte 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c
  1945. .byte 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79
  1946. .byte 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9
  1947. .byte 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08
  1948. .byte 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6
  1949. .byte 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a
  1950. .byte 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e
  1951. .byte 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e
  1952. .byte 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94
  1953. .byte 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf
  1954. .byte 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68
  1955. .byte 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  1956. .byte 0x01,0x00,0x00,0x00, 0x02,0x00,0x00,0x00 # rcon
  1957. .byte 0x04,0x00,0x00,0x00, 0x08,0x00,0x00,0x00
  1958. .byte 0x10,0x00,0x00,0x00, 0x20,0x00,0x00,0x00
  1959. .byte 0x40,0x00,0x00,0x00, 0x80,0x00,0x00,0x00
  1960. .byte 0x1B,0x00,0x00,0x00, 0x36,0x00,0x00,0x00
  1961. ___
  1962. foreach (split("\n",$code)) {
  1963. s/\`([^\`]*)\`/eval $1/ge;
  1964. # made-up _instructions, _xtr, _ins, _ror and _bias, cope
  1965. # with byte order dependencies...
  1966. if (/^\s+_/) {
  1967. s/(_[a-z]+\s+)(\$[0-9]+),([^,]+)(#.*)*$/$1$2,$2,$3/;
  1968. s/_xtr\s+(\$[0-9]+),(\$[0-9]+),([0-9]+(\-2)*)/
  1969. sprintf("srl\t$1,$2,%d",$big_endian ? eval($3)
  1970. : eval("24-$3"))/e or
  1971. s/_ins\s+(\$[0-9]+),(\$[0-9]+),([0-9]+)/
  1972. sprintf("sll\t$1,$2,%d",$big_endian ? eval($3)
  1973. : eval("24-$3"))/e or
  1974. s/_ins2\s+(\$[0-9]+),(\$[0-9]+),([0-9]+)/
  1975. sprintf("ins\t$1,$2,%d,8",$big_endian ? eval($3)
  1976. : eval("24-$3"))/e or
  1977. s/_ror\s+(\$[0-9]+),(\$[0-9]+),(\-?[0-9]+)/
  1978. sprintf("srl\t$1,$2,%d",$big_endian ? eval($3)
  1979. : eval("$3*-1"))/e or
  1980. s/_bias\s+(\$[0-9]+),(\$[0-9]+),([0-9]+)/
  1981. sprintf("sll\t$1,$2,%d",$big_endian ? eval($3)
  1982. : eval("($3-16)&31"))/e;
  1983. s/srl\s+(\$[0-9]+),(\$[0-9]+),\-([0-9]+)/
  1984. sprintf("sll\t$1,$2,$3")/e or
  1985. s/srl\s+(\$[0-9]+),(\$[0-9]+),0/
  1986. sprintf("and\t$1,$2,0xff")/e or
  1987. s/(sll\s+\$[0-9]+,\$[0-9]+,0)/#$1/;
  1988. }
  1989. # convert lwl/lwr and swr/swl to little-endian order
  1990. if (!$big_endian && /^\s+[sl]w[lr]\s+/) {
  1991. s/([sl]wl.*)([0-9]+)\((\$[0-9]+)\)/
  1992. sprintf("$1%d($3)",eval("$2-$2%4+($2%4-1)&3"))/e or
  1993. s/([sl]wr.*)([0-9]+)\((\$[0-9]+)\)/
  1994. sprintf("$1%d($3)",eval("$2-$2%4+($2%4+1)&3"))/e;
  1995. }
  1996. if (!$big_endian) {
  1997. s/(rotr\s+\$[0-9]+,\$[0-9]+),([0-9]+)/sprintf("$1,%d",32-$2)/e;
  1998. s/(ext\s+\$[0-9]+,\$[0-9]+),([0-9]+),8/sprintf("$1,%d,8",24-$2)/e;
  1999. }
  2000. print $_,"\n";
  2001. }
  2002. close STDOUT;