aes-c64xplus.pl 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. #!/usr/bin/env perl
  2. #
  3. # ====================================================================
  4. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  5. # project. The module is, however, dual licensed under OpenSSL and
  6. # CRYPTOGAMS licenses depending on where you obtain it. For further
  7. # details see http://www.openssl.org/~appro/cryptogams/.
  8. # ====================================================================
  9. #
  10. # [Endian-neutral] AES for C64x+.
  11. #
  12. # Even though SPLOOPs are scheduled for 13 cycles, and thus expected
  13. # performance is ~8.5 cycles per byte processed with 128-bit key,
  14. # measured performance turned to be ~10 cycles per byte. Discrepancy
  15. # must be caused by limitations of L1D memory banking(*), see SPRU871
  16. # TI publication for further details. If any consolation it's still
  17. # ~20% faster than TI's linear assembly module anyway... Compared to
  18. # aes_core.c compiled with cl6x 6.0 with -mv6400+ -o2 options this
  19. # code is 3.75x faster and almost 3x smaller (tables included).
  20. #
  21. # (*) This means that there might be subtle correlation between data
  22. # and timing and one can wonder if it can be ... attacked:-(
  23. # On the other hand this also means that *if* one chooses to
  24. # implement *4* T-tables variant [instead of 1 T-table as in
  25. # this implementation, or in addition to], then one ought to
  26. # *interleave* them. Even though it complicates addressing,
  27. # references to interleaved tables would be guaranteed not to
  28. # clash. I reckon that it should be possible to break 8 cycles
  29. # per byte "barrier," i.e. improve by ~20%, naturally at the
  30. # cost of 8x increased pressure on L1D. 8x because you'd have
  31. # to interleave both Te and Td tables...
  32. while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
  33. open STDOUT,">$output";
  34. ($TEA,$TEB)=("A5","B5");
  35. ($KPA,$KPB)=("A3","B1");
  36. @K=("A6","B6","A7","B7");
  37. @s=("A8","B8","A9","B9");
  38. @Te0=@Td0=("A16","B16","A17","B17");
  39. @Te1=@Td1=("A18","B18","A19","B19");
  40. @Te2=@Td2=("A20","B20","A21","B21");
  41. @Te3=@Td3=("A22","B22","A23","B23");
  42. $code=<<___;
  43. .text
  44. .asg B3,RA
  45. .asg A4,INP
  46. .asg B4,OUT
  47. .asg A6,KEY
  48. .asg A4,RET
  49. .asg B15,SP
  50. .eval 24,EXT0
  51. .eval 16,EXT1
  52. .eval 8,EXT2
  53. .eval 0,EXT3
  54. .eval 8,TBL1
  55. .eval 16,TBL2
  56. .eval 24,TBL3
  57. .if .BIG_ENDIAN
  58. .eval 24-EXT0,EXT0
  59. .eval 24-EXT1,EXT1
  60. .eval 24-EXT2,EXT2
  61. .eval 24-EXT3,EXT3
  62. .eval 32-TBL1,TBL1
  63. .eval 32-TBL2,TBL2
  64. .eval 32-TBL3,TBL3
  65. .endif
  66. .global _AES_encrypt
  67. _AES_encrypt:
  68. .asmfunc
  69. MVK 1,B2
  70. __encrypt:
  71. [B2] LDNDW *INP++,A9:A8 ; load input
  72. || MVKL (AES_Te-_AES_encrypt),$TEA
  73. || ADDKPC _AES_encrypt,B0
  74. [B2] LDNDW *INP++,B9:B8
  75. || MVKH (AES_Te-_AES_encrypt),$TEA
  76. || ADD 0,KEY,$KPA
  77. || ADD 4,KEY,$KPB
  78. LDW *$KPA++[2],$Te0[0] ; zero round key
  79. || LDW *$KPB++[2],$Te0[1]
  80. || MVK 60,A0
  81. || ADD B0,$TEA,$TEA ; AES_Te
  82. LDW *KEY[A0],B0 ; rounds
  83. || MVK 1024,A0 ; sizeof(AES_Te)
  84. LDW *$KPA++[2],$Te0[2]
  85. || LDW *$KPB++[2],$Te0[3]
  86. || MV $TEA,$TEB
  87. NOP
  88. .if .BIG_ENDIAN
  89. MV A9,$s[0]
  90. || MV A8,$s[1]
  91. || MV B9,$s[2]
  92. || MV B8,$s[3]
  93. .else
  94. MV A8,$s[0]
  95. || MV A9,$s[1]
  96. || MV B8,$s[2]
  97. || MV B9,$s[3]
  98. .endif
  99. XOR $Te0[0],$s[0],$s[0]
  100. || XOR $Te0[1],$s[1],$s[1]
  101. || LDW *$KPA++[2],$K[0] ; 1st round key
  102. || LDW *$KPB++[2],$K[1]
  103. SUB B0,2,B0
  104. SPLOOPD 13
  105. || MVC B0,ILC
  106. || LDW *$KPA++[2],$K[2]
  107. || LDW *$KPB++[2],$K[3]
  108. ;;====================================================================
  109. EXTU $s[1],EXT1,24,$Te1[1]
  110. || EXTU $s[0],EXT3,24,$Te3[0]
  111. LDW *${TEB}[$Te1[1]],$Te1[1] ; Te1[s1>>8], t0
  112. || LDW *${TEA}[$Te3[0]],$Te3[0] ; Te3[s0>>24], t1
  113. || XOR $s[2],$Te0[2],$s[2] ; modulo-scheduled
  114. || XOR $s[3],$Te0[3],$s[3] ; modulo-scheduled
  115. || EXTU $s[1],EXT3,24,$Te3[1]
  116. || EXTU $s[0],EXT1,24,$Te1[0]
  117. LDW *${TEB}[$Te3[1]],$Te3[1] ; Te3[s1>>24], t2
  118. || LDW *${TEA}[$Te1[0]],$Te1[0] ; Te1[s0>>8], t3
  119. || EXTU $s[2],EXT2,24,$Te2[2]
  120. || EXTU $s[3],EXT2,24,$Te2[3]
  121. LDW *${TEA}[$Te2[2]],$Te2[2] ; Te2[s2>>16], t0
  122. || LDW *${TEB}[$Te2[3]],$Te2[3] ; Te2[s3>>16], t1
  123. || EXTU $s[3],EXT3,24,$Te3[3]
  124. || EXTU $s[2],EXT1,24,$Te1[2]
  125. LDW *${TEB}[$Te3[3]],$Te3[3] ; Te3[s3>>24], t0
  126. || LDW *${TEA}[$Te1[2]],$Te1[2] ; Te1[s2>>8], t1
  127. || EXTU $s[0],EXT2,24,$Te2[0]
  128. || EXTU $s[1],EXT2,24,$Te2[1]
  129. LDW *${TEA}[$Te2[0]],$Te2[0] ; Te2[s0>>16], t2
  130. || LDW *${TEB}[$Te2[1]],$Te2[1] ; Te2[s1>>16], t3
  131. || EXTU $s[3],EXT1,24,$Te1[3]
  132. || EXTU $s[2],EXT3,24,$Te3[2]
  133. LDW *${TEB}[$Te1[3]],$Te1[3] ; Te1[s3>>8], t2
  134. || LDW *${TEA}[$Te3[2]],$Te3[2] ; Te3[s2>>24], t3
  135. || ROTL $Te1[1],TBL1,$Te3[0] ; t0
  136. || ROTL $Te3[0],TBL3,$Te1[1] ; t1
  137. || EXTU $s[0],EXT0,24,$Te0[0]
  138. || EXTU $s[1],EXT0,24,$Te0[1]
  139. LDW *${TEA}[$Te0[0]],$Te0[0] ; Te0[s0], t0
  140. || LDW *${TEB}[$Te0[1]],$Te0[1] ; Te0[s1], t1
  141. || ROTL $Te3[1],TBL3,$Te1[0] ; t2
  142. || ROTL $Te1[0],TBL1,$Te3[1] ; t3
  143. || EXTU $s[2],EXT0,24,$Te0[2]
  144. || EXTU $s[3],EXT0,24,$Te0[3]
  145. LDW *${TEA}[$Te0[2]],$Te0[2] ; Te0[s2], t2
  146. || LDW *${TEB}[$Te0[3]],$Te0[3] ; Te0[s3], t3
  147. || ROTL $Te2[2],TBL2,$Te2[2] ; t0
  148. || ROTL $Te2[3],TBL2,$Te2[3] ; t1
  149. || XOR $K[0],$Te3[0],$s[0]
  150. || XOR $K[1],$Te1[1],$s[1]
  151. ROTL $Te3[3],TBL3,$Te1[2] ; t0
  152. || ROTL $Te1[2],TBL1,$Te3[3] ; t1
  153. || XOR $K[2],$Te1[0],$s[2]
  154. || XOR $K[3],$Te3[1],$s[3]
  155. || LDW *$KPA++[2],$K[0] ; next round key
  156. || LDW *$KPB++[2],$K[1]
  157. ROTL $Te2[0],TBL2,$Te2[0] ; t2
  158. || ROTL $Te2[1],TBL2,$Te2[1] ; t3
  159. || XOR $s[0],$Te2[2],$s[0]
  160. || XOR $s[1],$Te2[3],$s[1]
  161. || LDW *$KPA++[2],$K[2]
  162. || LDW *$KPB++[2],$K[3]
  163. ROTL $Te1[3],TBL1,$Te3[2] ; t2
  164. || ROTL $Te3[2],TBL3,$Te1[3] ; t3
  165. || XOR $s[0],$Te1[2],$s[0]
  166. || XOR $s[1],$Te3[3],$s[1]
  167. XOR $s[2],$Te2[0],$s[2]
  168. || XOR $s[3],$Te2[1],$s[3]
  169. || XOR $s[0],$Te0[0],$s[0]
  170. || XOR $s[1],$Te0[1],$s[1]
  171. SPKERNEL
  172. || XOR.L $s[2],$Te3[2],$s[2]
  173. || XOR.L $s[3],$Te1[3],$s[3]
  174. ;;====================================================================
  175. ADD.D ${TEA},A0,${TEA} ; point to Te4
  176. || ADD.D ${TEB},A0,${TEB}
  177. || EXTU $s[1],EXT1,24,$Te1[1]
  178. || EXTU $s[0],EXT3,24,$Te3[0]
  179. LDBU *${TEB}[$Te1[1]],$Te1[1] ; Te1[s1>>8], t0
  180. || LDBU *${TEA}[$Te3[0]],$Te3[0] ; Te3[s0>>24], t1
  181. || XOR $s[2],$Te0[2],$s[2] ; modulo-scheduled
  182. || XOR $s[3],$Te0[3],$s[3] ; modulo-scheduled
  183. || EXTU $s[0],EXT0,24,$Te0[0]
  184. || EXTU $s[1],EXT0,24,$Te0[1]
  185. LDBU *${TEA}[$Te0[0]],$Te0[0] ; Te0[s0], t0
  186. || LDBU *${TEB}[$Te0[1]],$Te0[1] ; Te0[s1], t1
  187. || EXTU $s[3],EXT3,24,$Te3[3]
  188. || EXTU $s[2],EXT1,24,$Te1[2]
  189. LDBU *${TEB}[$Te3[3]],$Te3[3] ; Te3[s3>>24], t0
  190. || LDBU *${TEA}[$Te1[2]],$Te1[2] ; Te1[s2>>8], t1
  191. || EXTU $s[2],EXT2,24,$Te2[2]
  192. || EXTU $s[3],EXT2,24,$Te2[3]
  193. LDBU *${TEA}[$Te2[2]],$Te2[2] ; Te2[s2>>16], t0
  194. || LDBU *${TEB}[$Te2[3]],$Te2[3] ; Te2[s3>>16], t1
  195. || EXTU $s[1],EXT3,24,$Te3[1]
  196. || EXTU $s[0],EXT1,24,$Te1[0]
  197. LDBU *${TEB}[$Te3[1]],$Te3[1] ; Te3[s1>>24], t2
  198. || LDBU *${TEA}[$Te1[0]],$Te1[0] ; Te1[s0>>8], t3
  199. || EXTU $s[3],EXT1,24,$Te1[3]
  200. || EXTU $s[2],EXT3,24,$Te3[2]
  201. LDBU *${TEB}[$Te1[3]],$Te1[3] ; Te1[s3>>8], t2
  202. || LDBU *${TEA}[$Te3[2]],$Te3[2] ; Te3[s2>>24], t3
  203. || EXTU $s[2],EXT0,24,$Te0[2]
  204. || EXTU $s[3],EXT0,24,$Te0[3]
  205. LDBU *${TEA}[$Te0[2]],$Te0[2] ; Te0[s2], t2
  206. || LDBU *${TEB}[$Te0[3]],$Te0[3] ; Te0[s3], t3
  207. || EXTU $s[0],EXT2,24,$Te2[0]
  208. || EXTU $s[1],EXT2,24,$Te2[1]
  209. LDBU *${TEA}[$Te2[0]],$Te2[0] ; Te2[s0>>16], t2
  210. || LDBU *${TEB}[$Te2[1]],$Te2[1] ; Te2[s1>>16], t3
  211. .if .BIG_ENDIAN
  212. PACK2 $Te0[0],$Te1[1],$Te0[0]
  213. || PACK2 $Te0[1],$Te1[2],$Te0[1]
  214. PACK2 $Te2[2],$Te3[3],$Te2[2]
  215. || PACK2 $Te2[3],$Te3[0],$Te2[3]
  216. PACKL4 $Te0[0],$Te2[2],$Te0[0]
  217. || PACKL4 $Te0[1],$Te2[3],$Te0[1]
  218. XOR $K[0],$Te0[0],$Te0[0] ; s[0]
  219. || XOR $K[1],$Te0[1],$Te0[1] ; s[1]
  220. PACK2 $Te0[2],$Te1[3],$Te0[2]
  221. || PACK2 $Te0[3],$Te1[0],$Te0[3]
  222. PACK2 $Te2[0],$Te3[1],$Te2[0]
  223. || PACK2 $Te2[1],$Te3[2],$Te2[1]
  224. || BNOP RA
  225. PACKL4 $Te0[2],$Te2[0],$Te0[2]
  226. || PACKL4 $Te0[3],$Te2[1],$Te0[3]
  227. XOR $K[2],$Te0[2],$Te0[2] ; s[2]
  228. || XOR $K[3],$Te0[3],$Te0[3] ; s[3]
  229. MV $Te0[0],A9
  230. || MV $Te0[1],A8
  231. MV $Te0[2],B9
  232. || MV $Te0[3],B8
  233. || [B2] STNDW A9:A8,*OUT++
  234. [B2] STNDW B9:B8,*OUT++
  235. .else
  236. PACK2 $Te1[1],$Te0[0],$Te1[1]
  237. || PACK2 $Te1[2],$Te0[1],$Te1[2]
  238. PACK2 $Te3[3],$Te2[2],$Te3[3]
  239. || PACK2 $Te3[0],$Te2[3],$Te3[0]
  240. PACKL4 $Te3[3],$Te1[1],$Te1[1]
  241. || PACKL4 $Te3[0],$Te1[2],$Te1[2]
  242. XOR $K[0],$Te1[1],$Te1[1] ; s[0]
  243. || XOR $K[1],$Te1[2],$Te1[2] ; s[1]
  244. PACK2 $Te1[3],$Te0[2],$Te1[3]
  245. || PACK2 $Te1[0],$Te0[3],$Te1[0]
  246. PACK2 $Te3[1],$Te2[0],$Te3[1]
  247. || PACK2 $Te3[2],$Te2[1],$Te3[2]
  248. || BNOP RA
  249. PACKL4 $Te3[1],$Te1[3],$Te1[3]
  250. || PACKL4 $Te3[2],$Te1[0],$Te1[0]
  251. XOR $K[2],$Te1[3],$Te1[3] ; s[2]
  252. || XOR $K[3],$Te1[0],$Te1[0] ; s[3]
  253. MV $Te1[1],A8
  254. || MV $Te1[2],A9
  255. MV $Te1[3],B8
  256. || MV $Te1[0],B9
  257. || [B2] STNDW A9:A8,*OUT++
  258. [B2] STNDW B9:B8,*OUT++
  259. .endif
  260. .endasmfunc
  261. .global _AES_decrypt
  262. _AES_decrypt:
  263. .asmfunc
  264. MVK 1,B2
  265. __decrypt:
  266. [B2] LDNDW *INP++,A9:A8 ; load input
  267. || MVKL (AES_Td-_AES_decrypt),$TEA
  268. || ADDKPC _AES_decrypt,B0
  269. [B2] LDNDW *INP++,B9:B8
  270. || MVKH (AES_Td-_AES_decrypt),$TEA
  271. || ADD 0,KEY,$KPA
  272. || ADD 4,KEY,$KPB
  273. LDW *$KPA++[2],$Td0[0] ; zero round key
  274. || LDW *$KPB++[2],$Td0[1]
  275. || MVK 60,A0
  276. || ADD B0,$TEA,$TEA ; AES_Td
  277. LDW *KEY[A0],B0 ; rounds
  278. || MVK 1024,A0 ; sizeof(AES_Td)
  279. LDW *$KPA++[2],$Td0[2]
  280. || LDW *$KPB++[2],$Td0[3]
  281. || MV $TEA,$TEB
  282. NOP
  283. .if .BIG_ENDIAN
  284. MV A9,$s[0]
  285. || MV A8,$s[1]
  286. || MV B9,$s[2]
  287. || MV B8,$s[3]
  288. .else
  289. MV A8,$s[0]
  290. || MV A9,$s[1]
  291. || MV B8,$s[2]
  292. || MV B9,$s[3]
  293. .endif
  294. XOR $Td0[0],$s[0],$s[0]
  295. || XOR $Td0[1],$s[1],$s[1]
  296. || LDW *$KPA++[2],$K[0] ; 1st round key
  297. || LDW *$KPB++[2],$K[1]
  298. SUB B0,2,B0
  299. SPLOOPD 13
  300. || MVC B0,ILC
  301. || LDW *$KPA++[2],$K[2]
  302. || LDW *$KPB++[2],$K[3]
  303. ;;====================================================================
  304. EXTU $s[1],EXT3,24,$Td3[1]
  305. || EXTU $s[0],EXT1,24,$Td1[0]
  306. LDW *${TEB}[$Td3[1]],$Td3[1] ; Td3[s1>>24], t0
  307. || LDW *${TEA}[$Td1[0]],$Td1[0] ; Td1[s0>>8], t1
  308. || XOR $s[2],$Td0[2],$s[2] ; modulo-scheduled
  309. || XOR $s[3],$Td0[3],$s[3] ; modulo-scheduled
  310. || EXTU $s[1],EXT1,24,$Td1[1]
  311. || EXTU $s[0],EXT3,24,$Td3[0]
  312. LDW *${TEB}[$Td1[1]],$Td1[1] ; Td1[s1>>8], t2
  313. || LDW *${TEA}[$Td3[0]],$Td3[0] ; Td3[s0>>24], t3
  314. || EXTU $s[2],EXT2,24,$Td2[2]
  315. || EXTU $s[3],EXT2,24,$Td2[3]
  316. LDW *${TEA}[$Td2[2]],$Td2[2] ; Td2[s2>>16], t0
  317. || LDW *${TEB}[$Td2[3]],$Td2[3] ; Td2[s3>>16], t1
  318. || EXTU $s[3],EXT1,24,$Td1[3]
  319. || EXTU $s[2],EXT3,24,$Td3[2]
  320. LDW *${TEB}[$Td1[3]],$Td1[3] ; Td1[s3>>8], t0
  321. || LDW *${TEA}[$Td3[2]],$Td3[2] ; Td3[s2>>24], t1
  322. || EXTU $s[0],EXT2,24,$Td2[0]
  323. || EXTU $s[1],EXT2,24,$Td2[1]
  324. LDW *${TEA}[$Td2[0]],$Td2[0] ; Td2[s0>>16], t2
  325. || LDW *${TEB}[$Td2[1]],$Td2[1] ; Td2[s1>>16], t3
  326. || EXTU $s[3],EXT3,24,$Td3[3]
  327. || EXTU $s[2],EXT1,24,$Td1[2]
  328. LDW *${TEB}[$Td3[3]],$Td3[3] ; Td3[s3>>24], t2
  329. || LDW *${TEA}[$Td1[2]],$Td1[2] ; Td1[s2>>8], t3
  330. || ROTL $Td3[1],TBL3,$Td1[0] ; t0
  331. || ROTL $Td1[0],TBL1,$Td3[1] ; t1
  332. || EXTU $s[0],EXT0,24,$Td0[0]
  333. || EXTU $s[1],EXT0,24,$Td0[1]
  334. LDW *${TEA}[$Td0[0]],$Td0[0] ; Td0[s0], t0
  335. || LDW *${TEB}[$Td0[1]],$Td0[1] ; Td0[s1], t1
  336. || ROTL $Td1[1],TBL1,$Td3[0] ; t2
  337. || ROTL $Td3[0],TBL3,$Td1[1] ; t3
  338. || EXTU $s[2],EXT0,24,$Td0[2]
  339. || EXTU $s[3],EXT0,24,$Td0[3]
  340. LDW *${TEA}[$Td0[2]],$Td0[2] ; Td0[s2], t2
  341. || LDW *${TEB}[$Td0[3]],$Td0[3] ; Td0[s3], t3
  342. || ROTL $Td2[2],TBL2,$Td2[2] ; t0
  343. || ROTL $Td2[3],TBL2,$Td2[3] ; t1
  344. || XOR $K[0],$Td1[0],$s[0]
  345. || XOR $K[1],$Td3[1],$s[1]
  346. ROTL $Td1[3],TBL1,$Td3[2] ; t0
  347. || ROTL $Td3[2],TBL3,$Td1[3] ; t1
  348. || XOR $K[2],$Td3[0],$s[2]
  349. || XOR $K[3],$Td1[1],$s[3]
  350. || LDW *$KPA++[2],$K[0] ; next round key
  351. || LDW *$KPB++[2],$K[1]
  352. ROTL $Td2[0],TBL2,$Td2[0] ; t2
  353. || ROTL $Td2[1],TBL2,$Td2[1] ; t3
  354. || XOR $s[0],$Td2[2],$s[0]
  355. || XOR $s[1],$Td2[3],$s[1]
  356. || LDW *$KPA++[2],$K[2]
  357. || LDW *$KPB++[2],$K[3]
  358. ROTL $Td3[3],TBL3,$Td1[2] ; t2
  359. || ROTL $Td1[2],TBL1,$Td3[3] ; t3
  360. || XOR $s[0],$Td3[2],$s[0]
  361. || XOR $s[1],$Td1[3],$s[1]
  362. XOR $s[2],$Td2[0],$s[2]
  363. || XOR $s[3],$Td2[1],$s[3]
  364. || XOR $s[0],$Td0[0],$s[0]
  365. || XOR $s[1],$Td0[1],$s[1]
  366. SPKERNEL
  367. || XOR.L $s[2],$Td1[2],$s[2]
  368. || XOR.L $s[3],$Td3[3],$s[3]
  369. ;;====================================================================
  370. ADD.D ${TEA},A0,${TEA} ; point to Td4
  371. || ADD.D ${TEB},A0,${TEB}
  372. || EXTU $s[1],EXT3,24,$Td3[1]
  373. || EXTU $s[0],EXT1,24,$Td1[0]
  374. LDBU *${TEB}[$Td3[1]],$Td3[1] ; Td3[s1>>24], t0
  375. || LDBU *${TEA}[$Td1[0]],$Td1[0] ; Td1[s0>>8], t1
  376. || XOR $s[2],$Td0[2],$s[2] ; modulo-scheduled
  377. || XOR $s[3],$Td0[3],$s[3] ; modulo-scheduled
  378. || EXTU $s[0],EXT0,24,$Td0[0]
  379. || EXTU $s[1],EXT0,24,$Td0[1]
  380. LDBU *${TEA}[$Td0[0]],$Td0[0] ; Td0[s0], t0
  381. || LDBU *${TEB}[$Td0[1]],$Td0[1] ; Td0[s1], t1
  382. || EXTU $s[2],EXT2,24,$Td2[2]
  383. || EXTU $s[3],EXT2,24,$Td2[3]
  384. LDBU *${TEA}[$Td2[2]],$Td2[2] ; Td2[s2>>16], t0
  385. || LDBU *${TEB}[$Td2[3]],$Td2[3] ; Td2[s3>>16], t1
  386. || EXTU $s[3],EXT1,24,$Td1[3]
  387. || EXTU $s[2],EXT3,24,$Td3[2]
  388. LDBU *${TEB}[$Td1[3]],$Td1[3] ; Td1[s3>>8], t0
  389. || LDBU *${TEA}[$Td3[2]],$Td3[2] ; Td3[s2>>24], t1
  390. || EXTU $s[1],EXT1,24,$Td1[1]
  391. || EXTU $s[0],EXT3,24,$Td3[0]
  392. LDBU *${TEB}[$Td1[1]],$Td1[1] ; Td1[s1>>8], t2
  393. || LDBU *${TEA}[$Td3[0]],$Td3[0] ; Td3[s0>>24], t3
  394. || EXTU $s[0],EXT2,24,$Td2[0]
  395. || EXTU $s[1],EXT2,24,$Td2[1]
  396. LDBU *${TEA}[$Td2[0]],$Td2[0] ; Td2[s0>>16], t2
  397. || LDBU *${TEB}[$Td2[1]],$Td2[1] ; Td2[s1>>16], t3
  398. || EXTU $s[3],EXT3,24,$Td3[3]
  399. || EXTU $s[2],EXT1,24,$Td1[2]
  400. LDBU *${TEB}[$Td3[3]],$Td3[3] ; Td3[s3>>24], t2
  401. || LDBU *${TEA}[$Td1[2]],$Td1[2] ; Td1[s2>>8], t3
  402. || EXTU $s[2],EXT0,24,$Td0[2]
  403. || EXTU $s[3],EXT0,24,$Td0[3]
  404. LDBU *${TEA}[$Td0[2]],$Td0[2] ; Td0[s2], t2
  405. || LDBU *${TEB}[$Td0[3]],$Td0[3] ; Td0[s3], t3
  406. .if .BIG_ENDIAN
  407. PACK2 $Td0[0],$Td1[3],$Td0[0]
  408. || PACK2 $Td0[1],$Td1[0],$Td0[1]
  409. PACK2 $Td2[2],$Td3[1],$Td2[2]
  410. || PACK2 $Td2[3],$Td3[2],$Td2[3]
  411. PACKL4 $Td0[0],$Td2[2],$Td0[0]
  412. || PACKL4 $Td0[1],$Td2[3],$Td0[1]
  413. XOR $K[0],$Td0[0],$Td0[0] ; s[0]
  414. || XOR $K[1],$Td0[1],$Td0[1] ; s[1]
  415. PACK2 $Td0[2],$Td1[1],$Td0[2]
  416. || PACK2 $Td0[3],$Td1[2],$Td0[3]
  417. PACK2 $Td2[0],$Td3[3],$Td2[0]
  418. || PACK2 $Td2[1],$Td3[0],$Td2[1]
  419. || BNOP RA
  420. PACKL4 $Td0[2],$Td2[0],$Td0[2]
  421. || PACKL4 $Td0[3],$Td2[1],$Td0[3]
  422. XOR $K[2],$Td0[2],$Td0[2] ; s[2]
  423. || XOR $K[3],$Td0[3],$Td0[3] ; s[3]
  424. MV $Td0[0],A9
  425. || MV $Td0[1],A8
  426. MV $Td0[2],B9
  427. || MV $Td0[3],B8
  428. || [B2] STNDW A9:A8,*OUT++
  429. [B2] STNDW B9:B8,*OUT++
  430. .else
  431. PACK2 $Td1[3],$Td0[0],$Td1[3]
  432. || PACK2 $Td1[0],$Td0[1],$Td1[0]
  433. PACK2 $Td3[1],$Td2[2],$Td3[1]
  434. || PACK2 $Td3[2],$Td2[3],$Td3[2]
  435. PACKL4 $Td3[1],$Td1[3],$Td1[3]
  436. || PACKL4 $Td3[2],$Td1[0],$Td1[0]
  437. XOR $K[0],$Td1[3],$Td1[3] ; s[0]
  438. || XOR $K[1],$Td1[0],$Td1[0] ; s[1]
  439. PACK2 $Td1[1],$Td0[2],$Td1[1]
  440. || PACK2 $Td1[2],$Td0[3],$Td1[2]
  441. PACK2 $Td3[3],$Td2[0],$Td3[3]
  442. || PACK2 $Td3[0],$Td2[1],$Td3[0]
  443. || BNOP RA
  444. PACKL4 $Td3[3],$Td1[1],$Td1[1]
  445. || PACKL4 $Td3[0],$Td1[2],$Td1[2]
  446. XOR $K[2],$Td1[1],$Td1[1] ; s[2]
  447. || XOR $K[3],$Td1[2],$Td1[2] ; s[3]
  448. MV $Td1[3],A8
  449. || MV $Td1[0],A9
  450. MV $Td1[1],B8
  451. || MV $Td1[2],B9
  452. || [B2] STNDW A9:A8,*OUT++
  453. [B2] STNDW B9:B8,*OUT++
  454. .endif
  455. .endasmfunc
  456. ___
  457. {
  458. my @K=(@K,@s); # extended key
  459. my @Te4=map("B$_",(16..19));
  460. my @Kx9=@Te0; # used in AES_set_decrypt_key
  461. my @KxB=@Te1;
  462. my @KxD=@Te2;
  463. my @KxE=@Te3;
  464. $code.=<<___;
  465. .asg OUT,BITS
  466. .global _AES_set_encrypt_key
  467. _AES_set_encrypt_key:
  468. __set_encrypt_key:
  469. .asmfunc
  470. MV INP,A0
  471. || SHRU BITS,5,BITS ; 128-192-256 -> 4-6-8
  472. || MV KEY,A1
  473. [!A0] B RA
  474. ||[!A0] MVK -1,RET
  475. ||[!A0] MVK 1,A1 ; only one B RA
  476. [!A1] B RA
  477. ||[!A1] MVK -1,RET
  478. ||[!A1] MVK 0,A0
  479. || MVK 0,B0
  480. || MVK 0,A1
  481. [A0] LDNDW *INP++,A9:A8
  482. || [A0] CMPEQ 4,BITS,B0
  483. || [A0] CMPLT 3,BITS,A1
  484. [B0] B key128?
  485. || [A1] LDNDW *INP++,B9:B8
  486. || [A0] CMPEQ 6,BITS,B0
  487. || [A0] CMPLT 5,BITS,A1
  488. [B0] B key192?
  489. || [A1] LDNDW *INP++,B17:B16
  490. || [A0] CMPEQ 8,BITS,B0
  491. || [A0] CMPLT 7,BITS,A1
  492. [B0] B key256?
  493. || [A1] LDNDW *INP++,B19:B18
  494. [A0] ADD 0,KEY,$KPA
  495. || [A0] ADD 4,KEY,$KPB
  496. || [A0] MVKL (AES_Te4-_AES_set_encrypt_key),$TEA
  497. || [A0] ADDKPC _AES_set_encrypt_key,B6
  498. [A0] MVKH (AES_Te4-_AES_set_encrypt_key),$TEA
  499. [A0] ADD B6,$TEA,$TEA ; AES_Te4
  500. NOP
  501. NOP
  502. BNOP RA,5
  503. || MVK -2,RET ; unknown bit lenght
  504. || MVK 0,B0 ; redundant
  505. ;;====================================================================
  506. ;;====================================================================
  507. key128?:
  508. .if .BIG_ENDIAN
  509. MV A9,$K[0]
  510. || MV A8,$K[1]
  511. || MV B9,$Te4[2]
  512. || MV B8,$K[3]
  513. .else
  514. MV A8,$K[0]
  515. || MV A9,$K[1]
  516. || MV B8,$Te4[2]
  517. || MV B9,$K[3]
  518. .endif
  519. MVK 256,A0
  520. || MVK 9,B0
  521. SPLOOPD 14
  522. || MVC B0,ILC
  523. || MV $TEA,$TEB
  524. || ADD $TEA,A0,A30 ; rcon
  525. ;;====================================================================
  526. LDW *A30++[1],A31 ; rcon[i]
  527. || MV $Te4[2],$K[2]
  528. || EXTU $K[3],EXT1,24,$Te4[0]
  529. LDBU *${TEB}[$Te4[0]],$Te4[0]
  530. || MV $K[3],A0
  531. || EXTU $K[3],EXT2,24,$Te4[1]
  532. LDBU *${TEB}[$Te4[1]],$Te4[1]
  533. || EXTU A0,EXT3,24,A0
  534. || EXTU $K[3],EXT0,24,$Te4[3]
  535. .if .BIG_ENDIAN
  536. LDBU *${TEA}[A0],$Te4[3]
  537. || LDBU *${TEB}[$Te4[3]],A0
  538. .else
  539. LDBU *${TEA}[A0],A0
  540. || LDBU *${TEB}[$Te4[3]],$Te4[3]
  541. .endif
  542. STW $K[0],*$KPA++[2]
  543. || STW $K[1],*$KPB++[2]
  544. STW $K[2],*$KPA++[2]
  545. || STW $K[3],*$KPB++[2]
  546. XOR A31,$K[0],$K[0] ; ^=rcon[i]
  547. .if .BIG_ENDIAN
  548. PACK2 $Te4[0],$Te4[1],$Te4[1]
  549. PACK2 $Te4[3],A0,$Te4[3]
  550. PACKL4 $Te4[1],$Te4[3],$Te4[3]
  551. .else
  552. PACK2 $Te4[1],$Te4[0],$Te4[1]
  553. PACK2 $Te4[3],A0,$Te4[3]
  554. PACKL4 $Te4[3],$Te4[1],$Te4[3]
  555. .endif
  556. XOR $Te4[3],$K[0],$Te4[0] ; K[0]
  557. XOR $Te4[0],$K[1],$K[1] ; K[1]
  558. MV $Te4[0],$K[0]
  559. || XOR $K[1],$K[2],$Te4[2] ; K[2]
  560. XOR $Te4[2],$K[3],$K[3] ; K[3]
  561. SPKERNEL
  562. ;;====================================================================
  563. BNOP RA
  564. MV $Te4[2],$K[2]
  565. || STW $K[0],*$KPA++[2]
  566. || STW $K[1],*$KPB++[2]
  567. STW $K[2],*$KPA++[2]
  568. || STW $K[3],*$KPB++[2]
  569. MVK 10,B0 ; rounds
  570. STW B0,*++${KPB}[15]
  571. MVK 0,RET
  572. ;;====================================================================
  573. ;;====================================================================
  574. key192?:
  575. .if .BIG_ENDIAN
  576. MV A9,$K[0]
  577. || MV A8,$K[1]
  578. || MV B9,$K[2]
  579. || MV B8,$K[3]
  580. MV B17,$Te4[2]
  581. || MV B16,$K[5]
  582. .else
  583. MV A8,$K[0]
  584. || MV A9,$K[1]
  585. || MV B8,$K[2]
  586. || MV B9,$K[3]
  587. MV B16,$Te4[2]
  588. || MV B17,$K[5]
  589. .endif
  590. MVK 256,A0
  591. || MVK 6,B0
  592. MV $TEA,$TEB
  593. || ADD $TEA,A0,A30 ; rcon
  594. ;;====================================================================
  595. loop192?:
  596. LDW *A30++[1],A31 ; rcon[i]
  597. || MV $Te4[2],$K[4]
  598. || EXTU $K[5],EXT1,24,$Te4[0]
  599. LDBU *${TEB}[$Te4[0]],$Te4[0]
  600. || MV $K[5],A0
  601. || EXTU $K[5],EXT2,24,$Te4[1]
  602. LDBU *${TEB}[$Te4[1]],$Te4[1]
  603. || EXTU A0,EXT3,24,A0
  604. || EXTU $K[5],EXT0,24,$Te4[3]
  605. .if .BIG_ENDIAN
  606. LDBU *${TEA}[A0],$Te4[3]
  607. || LDBU *${TEB}[$Te4[3]],A0
  608. .else
  609. LDBU *${TEA}[A0],A0
  610. || LDBU *${TEB}[$Te4[3]],$Te4[3]
  611. .endif
  612. STW $K[0],*$KPA++[2]
  613. || STW $K[1],*$KPB++[2]
  614. STW $K[2],*$KPA++[2]
  615. || STW $K[3],*$KPB++[2]
  616. STW $K[4],*$KPA++[2]
  617. || STW $K[5],*$KPB++[2]
  618. XOR A31,$K[0],$K[0] ; ^=rcon[i]
  619. .if .BIG_ENDIAN
  620. PACK2 $Te4[0],$Te4[1],$Te4[1]
  621. || PACK2 $Te4[3],A0,$Te4[3]
  622. PACKL4 $Te4[1],$Te4[3],$Te4[3]
  623. .else
  624. PACK2 $Te4[1],$Te4[0],$Te4[1]
  625. || PACK2 $Te4[3],A0,$Te4[3]
  626. PACKL4 $Te4[3],$Te4[1],$Te4[3]
  627. .endif
  628. BDEC loop192?,B0
  629. || XOR $Te4[3],$K[0],$Te4[0] ; K[0]
  630. XOR $Te4[0],$K[1],$K[1] ; K[1]
  631. MV $Te4[0],$K[0]
  632. || XOR $K[1],$K[2],$Te4[2] ; K[2]
  633. XOR $Te4[2],$K[3],$K[3] ; K[3]
  634. MV $Te4[2],$K[2]
  635. || XOR $K[3],$K[4],$Te4[2] ; K[4]
  636. XOR $Te4[2],$K[5],$K[5] ; K[5]
  637. ;;====================================================================
  638. BNOP RA
  639. STW $K[0],*$KPA++[2]
  640. || STW $K[1],*$KPB++[2]
  641. STW $K[2],*$KPA++[2]
  642. || STW $K[3],*$KPB++[2]
  643. MVK 12,B0 ; rounds
  644. STW B0,*++${KPB}[7]
  645. MVK 0,RET
  646. ;;====================================================================
  647. ;;====================================================================
  648. key256?:
  649. .if .BIG_ENDIAN
  650. MV A9,$K[0]
  651. || MV A8,$K[1]
  652. || MV B9,$K[2]
  653. || MV B8,$K[3]
  654. MV B17,$K[4]
  655. || MV B16,$K[5]
  656. || MV B19,$Te4[2]
  657. || MV B18,$K[7]
  658. .else
  659. MV A8,$K[0]
  660. || MV A9,$K[1]
  661. || MV B8,$K[2]
  662. || MV B9,$K[3]
  663. MV B16,$K[4]
  664. || MV B17,$K[5]
  665. || MV B18,$Te4[2]
  666. || MV B19,$K[7]
  667. .endif
  668. MVK 256,A0
  669. || MVK 6,B0
  670. MV $TEA,$TEB
  671. || ADD $TEA,A0,A30 ; rcon
  672. ;;====================================================================
  673. loop256?:
  674. LDW *A30++[1],A31 ; rcon[i]
  675. || MV $Te4[2],$K[6]
  676. || EXTU $K[7],EXT1,24,$Te4[0]
  677. LDBU *${TEB}[$Te4[0]],$Te4[0]
  678. || MV $K[7],A0
  679. || EXTU $K[7],EXT2,24,$Te4[1]
  680. LDBU *${TEB}[$Te4[1]],$Te4[1]
  681. || EXTU A0,EXT3,24,A0
  682. || EXTU $K[7],EXT0,24,$Te4[3]
  683. .if .BIG_ENDIAN
  684. LDBU *${TEA}[A0],$Te4[3]
  685. || LDBU *${TEB}[$Te4[3]],A0
  686. .else
  687. LDBU *${TEA}[A0],A0
  688. || LDBU *${TEB}[$Te4[3]],$Te4[3]
  689. .endif
  690. STW $K[0],*$KPA++[2]
  691. || STW $K[1],*$KPB++[2]
  692. STW $K[2],*$KPA++[2]
  693. || STW $K[3],*$KPB++[2]
  694. STW $K[4],*$KPA++[2]
  695. || STW $K[5],*$KPB++[2]
  696. STW $K[6],*$KPA++[2]
  697. || STW $K[7],*$KPB++[2]
  698. || XOR A31,$K[0],$K[0] ; ^=rcon[i]
  699. .if .BIG_ENDIAN
  700. PACK2 $Te4[0],$Te4[1],$Te4[1]
  701. || PACK2 $Te4[3],A0,$Te4[3]
  702. PACKL4 $Te4[1],$Te4[3],$Te4[3]
  703. ||[!B0] B done256?
  704. .else
  705. PACK2 $Te4[1],$Te4[0],$Te4[1]
  706. || PACK2 $Te4[3],A0,$Te4[3]
  707. PACKL4 $Te4[3],$Te4[1],$Te4[3]
  708. ||[!B0] B done256?
  709. .endif
  710. XOR $Te4[3],$K[0],$Te4[0] ; K[0]
  711. XOR $Te4[0],$K[1],$K[1] ; K[1]
  712. MV $Te4[0],$K[0]
  713. || XOR $K[1],$K[2],$Te4[2] ; K[2]
  714. XOR $Te4[2],$K[3],$K[3] ; K[3]
  715. MV $Te4[2],$K[2]
  716. || [B0] EXTU $K[3],EXT0,24,$Te4[0]
  717. || [B0] SUB B0,1,B0
  718. LDBU *${TEB}[$Te4[0]],$Te4[0]
  719. || MV $K[3],A0
  720. || EXTU $K[3],EXT1,24,$Te4[1]
  721. LDBU *${TEB}[$Te4[1]],$Te4[1]
  722. || EXTU A0,EXT2,24,A0
  723. || EXTU $K[3],EXT3,24,$Te4[3]
  724. .if .BIG_ENDIAN
  725. LDBU *${TEA}[A0],$Te4[3]
  726. || LDBU *${TEB}[$Te4[3]],A0
  727. NOP 3
  728. PACK2 $Te4[0],$Te4[1],$Te4[1]
  729. PACK2 $Te4[3],A0,$Te4[3]
  730. || B loop256?
  731. PACKL4 $Te4[1],$Te4[3],$Te4[3]
  732. .else
  733. LDBU *${TEA}[A0],A0
  734. || LDBU *${TEB}[$Te4[3]],$Te4[3]
  735. NOP 3
  736. PACK2 $Te4[1],$Te4[0],$Te4[1]
  737. PACK2 $Te4[3],A0,$Te4[3]
  738. || B loop256?
  739. PACKL4 $Te4[3],$Te4[1],$Te4[3]
  740. .endif
  741. XOR $Te4[3],$K[4],$Te4[0] ; K[4]
  742. XOR $Te4[0],$K[5],$K[5] ; K[5]
  743. MV $Te4[0],$K[4]
  744. || XOR $K[5],$K[6],$Te4[2] ; K[6]
  745. XOR $Te4[2],$K[7],$K[7] ; K[7]
  746. ;;====================================================================
  747. done256?:
  748. BNOP RA
  749. STW $K[0],*$KPA++[2]
  750. || STW $K[1],*$KPB++[2]
  751. STW $K[2],*$KPA++[2]
  752. || STW $K[3],*$KPB++[2]
  753. MVK 14,B0 ; rounds
  754. STW B0,*--${KPB}[1]
  755. MVK 0,RET
  756. .endasmfunc
  757. .global _AES_set_decrypt_key
  758. _AES_set_decrypt_key:
  759. .asmfunc
  760. B __set_encrypt_key ; guarantee local call
  761. MV KEY,B30 ; B30 is not modified
  762. MV RA, B31 ; B31 is not modified
  763. ADDKPC ret?,RA,2
  764. ret?: ; B0 holds rounds or zero
  765. [!B0] BNOP B31 ; return if zero
  766. [B0] SHL B0,4,A0 ; offset to last round key
  767. [B0] SHRU B0,1,B1
  768. [B0] SUB B1,1,B1
  769. [B0] MVK 0x0000001B,B3 ; AES polynomial
  770. [B0] MVKH 0x07000000,B3
  771. SPLOOPD 9 ; flip round keys
  772. || MVC B1,ILC
  773. || MV B30,$KPA
  774. || ADD B30,A0,$KPB
  775. || MVK 16,A0 ; sizeof(round key)
  776. ;;====================================================================
  777. LDW *${KPA}[0],A16
  778. || LDW *${KPB}[0],B16
  779. LDW *${KPA}[1],A17
  780. || LDW *${KPB}[1],B17
  781. LDW *${KPA}[2],A18
  782. || LDW *${KPB}[2],B18
  783. LDW *${KPA}[3],A19
  784. || ADD $KPA,A0,$KPA
  785. || LDW *${KPB}[3],B19
  786. || SUB $KPB,A0,$KPB
  787. NOP
  788. STW B16,*${KPA}[-4]
  789. || STW A16,*${KPB}[4]
  790. STW B17,*${KPA}[-3]
  791. || STW A17,*${KPB}[5]
  792. STW B18,*${KPA}[-2]
  793. || STW A18,*${KPB}[6]
  794. STW B19,*${KPA}[-1]
  795. || STW A19,*${KPB}[7]
  796. SPKERNEL
  797. ;;====================================================================
  798. SUB B0,1,B0 ; skip last round
  799. || ADD B30,A0,$KPA ; skip first round
  800. || ADD B30,A0,$KPB
  801. || MVC GFPGFR,B30 ; save GFPGFR
  802. LDW *${KPA}[0],$K[0]
  803. || LDW *${KPB}[1],$K[1]
  804. || MVC B3,GFPGFR
  805. LDW *${KPA}[2],$K[2]
  806. || LDW *${KPB}[3],$K[3]
  807. MVK 0x00000909,A24
  808. || MVK 0x00000B0B,B24
  809. MVKH 0x09090000,A24
  810. || MVKH 0x0B0B0000,B24
  811. MVC B0,ILC
  812. || SUB B0,1,B0
  813. GMPY4 $K[0],A24,$Kx9[0] ; ·0x09
  814. || GMPY4 $K[1],A24,$Kx9[1]
  815. || MVK 0x00000D0D,A25
  816. || MVK 0x00000E0E,B25
  817. GMPY4 $K[2],A24,$Kx9[2]
  818. || GMPY4 $K[3],A24,$Kx9[3]
  819. || MVKH 0x0D0D0000,A25
  820. || MVKH 0x0E0E0000,B25
  821. GMPY4 $K[0],B24,$KxB[0] ; ·0x0B
  822. || GMPY4 $K[1],B24,$KxB[1]
  823. GMPY4 $K[2],B24,$KxB[2]
  824. || GMPY4 $K[3],B24,$KxB[3]
  825. SPLOOP 11 ; InvMixColumns
  826. ;;====================================================================
  827. GMPY4 $K[0],A25,$KxD[0] ; ·0x0D
  828. || GMPY4 $K[1],A25,$KxD[1]
  829. || SWAP2 $Kx9[0],$Kx9[0] ; rotate by 16
  830. || SWAP2 $Kx9[1],$Kx9[1]
  831. || MV $K[0],$s[0] ; this or DINT
  832. || MV $K[1],$s[1]
  833. || [B0] LDW *${KPA}[4],$K[0]
  834. || [B0] LDW *${KPB}[5],$K[1]
  835. GMPY4 $K[2],A25,$KxD[2]
  836. || GMPY4 $K[3],A25,$KxD[3]
  837. || SWAP2 $Kx9[2],$Kx9[2]
  838. || SWAP2 $Kx9[3],$Kx9[3]
  839. || MV $K[2],$s[2]
  840. || MV $K[3],$s[3]
  841. || [B0] LDW *${KPA}[6],$K[2]
  842. || [B0] LDW *${KPB}[7],$K[3]
  843. GMPY4 $s[0],B25,$KxE[0] ; ·0x0E
  844. || GMPY4 $s[1],B25,$KxE[1]
  845. || XOR $Kx9[0],$KxB[0],$KxB[0]
  846. || XOR $Kx9[1],$KxB[1],$KxB[1]
  847. GMPY4 $s[2],B25,$KxE[2]
  848. || GMPY4 $s[3],B25,$KxE[3]
  849. || XOR $Kx9[2],$KxB[2],$KxB[2]
  850. || XOR $Kx9[3],$KxB[3],$KxB[3]
  851. ROTL $KxB[0],TBL3,$KxB[0]
  852. || ROTL $KxB[1],TBL3,$KxB[1]
  853. || SWAP2 $KxD[0],$KxD[0] ; rotate by 16
  854. || SWAP2 $KxD[1],$KxD[1]
  855. ROTL $KxB[2],TBL3,$KxB[2]
  856. || ROTL $KxB[3],TBL3,$KxB[3]
  857. || SWAP2 $KxD[2],$KxD[2]
  858. || SWAP2 $KxD[3],$KxD[3]
  859. XOR $KxE[0],$KxD[0],$KxE[0]
  860. || XOR $KxE[1],$KxD[1],$KxE[1]
  861. || [B0] GMPY4 $K[0],A24,$Kx9[0] ; ·0x09
  862. || [B0] GMPY4 $K[1],A24,$Kx9[1]
  863. || ADDAW $KPA,4,$KPA
  864. XOR $KxE[2],$KxD[2],$KxE[2]
  865. || XOR $KxE[3],$KxD[3],$KxE[3]
  866. || [B0] GMPY4 $K[2],A24,$Kx9[2]
  867. || [B0] GMPY4 $K[3],A24,$Kx9[3]
  868. || ADDAW $KPB,4,$KPB
  869. XOR $KxB[0],$KxE[0],$KxE[0]
  870. || XOR $KxB[1],$KxE[1],$KxE[1]
  871. || [B0] GMPY4 $K[0],B24,$KxB[0] ; ·0x0B
  872. || [B0] GMPY4 $K[1],B24,$KxB[1]
  873. XOR $KxB[2],$KxE[2],$KxE[2]
  874. || XOR $KxB[3],$KxE[3],$KxE[3]
  875. || [B0] GMPY4 $K[2],B24,$KxB[2]
  876. || [B0] GMPY4 $K[3],B24,$KxB[3]
  877. || STW $KxE[0],*${KPA}[-4]
  878. || STW $KxE[1],*${KPB}[-3]
  879. STW $KxE[2],*${KPA}[-2]
  880. || STW $KxE[3],*${KPB}[-1]
  881. || [B0] SUB B0,1,B0
  882. SPKERNEL
  883. ;;====================================================================
  884. BNOP B31,3
  885. MVC B30,GFPGFR ; restore GFPGFR(*)
  886. MVK 0,RET
  887. .endasmfunc
  888. ___
  889. # (*) Even though ABI doesn't specify GFPGFR as non-volatile, there
  890. # are code samples out there that *assume* its default value.
  891. }
  892. {
  893. my ($inp,$out,$blocks,$key,$ivp)=("A4","B4","A6","B6","A8");
  894. $code.=<<___;
  895. .global _AES_ctr32_encrypt
  896. _AES_ctr32_encrypt:
  897. .asmfunc
  898. LDNDW *${ivp}[0],A31:A30 ; load counter value
  899. || MV $blocks,A2 ; reassign $blocks
  900. || DMV RA,$key,B27:B26 ; reassign RA and $key
  901. LDNDW *${ivp}[1],B31:B30
  902. || MVK 0,B2 ; don't let __encrypt load input
  903. || MVK 0,A1 ; and postpone writing output
  904. .if .BIG_ENDIAN
  905. NOP
  906. .else
  907. NOP 4
  908. SWAP2 B31,B31 ; keep least significant 32 bits
  909. SWAP4 B31,B31 ; in host byte order
  910. .endif
  911. ctr32_loop?:
  912. [A2] BNOP __encrypt
  913. || [A1] XOR A29,A9,A9 ; input^Ek(counter)
  914. || [A1] XOR A28,A8,A8
  915. || [A2] LDNDW *INP++,A29:A28 ; load input
  916. [!A2] BNOP B27 ; return
  917. || [A1] XOR B29,B9,B9
  918. || [A1] XOR B28,B8,B8
  919. || [A2] LDNDW *INP++,B29:B28
  920. .if .BIG_ENDIAN
  921. [A1] STNDW A9:A8,*OUT++ ; save output
  922. || [A2] DMV A31,A30,A9:A8 ; pass counter value to __encrypt
  923. [A1] STNDW B9:B8,*OUT++
  924. || [A2] DMV B31,B30,B9:B8
  925. || [A2] ADD B30,1,B30 ; counter++
  926. .else
  927. [A1] STNDW A9:A8,*OUT++ ; save output
  928. || [A2] DMV A31,A30,A9:A8
  929. || [A2] SWAP2 B31,B0
  930. || [A2] ADD B31,1,B31 ; counter++
  931. [A1] STNDW B9:B8,*OUT++
  932. || [A2] MV B30,B8
  933. || [A2] SWAP4 B0,B9
  934. .endif
  935. [A2] ADDKPC ctr32_loop?,RA ; return to ctr32_loop?
  936. || [A2] MV B26,KEY ; pass $key
  937. || [A2] SUB A2,1,A2 ; $blocks--
  938. ||[!A1] MVK 1,A1
  939. NOP
  940. NOP
  941. .endasmfunc
  942. ___
  943. }
  944. # Tables are kept in endian-neutral manner
  945. $code.=<<___;
  946. .sect ".const:aes_asm"
  947. .align 128
  948. AES_Te:
  949. .byte 0xc6,0x63,0x63,0xa5, 0xf8,0x7c,0x7c,0x84
  950. .byte 0xee,0x77,0x77,0x99, 0xf6,0x7b,0x7b,0x8d
  951. .byte 0xff,0xf2,0xf2,0x0d, 0xd6,0x6b,0x6b,0xbd
  952. .byte 0xde,0x6f,0x6f,0xb1, 0x91,0xc5,0xc5,0x54
  953. .byte 0x60,0x30,0x30,0x50, 0x02,0x01,0x01,0x03
  954. .byte 0xce,0x67,0x67,0xa9, 0x56,0x2b,0x2b,0x7d
  955. .byte 0xe7,0xfe,0xfe,0x19, 0xb5,0xd7,0xd7,0x62
  956. .byte 0x4d,0xab,0xab,0xe6, 0xec,0x76,0x76,0x9a
  957. .byte 0x8f,0xca,0xca,0x45, 0x1f,0x82,0x82,0x9d
  958. .byte 0x89,0xc9,0xc9,0x40, 0xfa,0x7d,0x7d,0x87
  959. .byte 0xef,0xfa,0xfa,0x15, 0xb2,0x59,0x59,0xeb
  960. .byte 0x8e,0x47,0x47,0xc9, 0xfb,0xf0,0xf0,0x0b
  961. .byte 0x41,0xad,0xad,0xec, 0xb3,0xd4,0xd4,0x67
  962. .byte 0x5f,0xa2,0xa2,0xfd, 0x45,0xaf,0xaf,0xea
  963. .byte 0x23,0x9c,0x9c,0xbf, 0x53,0xa4,0xa4,0xf7
  964. .byte 0xe4,0x72,0x72,0x96, 0x9b,0xc0,0xc0,0x5b
  965. .byte 0x75,0xb7,0xb7,0xc2, 0xe1,0xfd,0xfd,0x1c
  966. .byte 0x3d,0x93,0x93,0xae, 0x4c,0x26,0x26,0x6a
  967. .byte 0x6c,0x36,0x36,0x5a, 0x7e,0x3f,0x3f,0x41
  968. .byte 0xf5,0xf7,0xf7,0x02, 0x83,0xcc,0xcc,0x4f
  969. .byte 0x68,0x34,0x34,0x5c, 0x51,0xa5,0xa5,0xf4
  970. .byte 0xd1,0xe5,0xe5,0x34, 0xf9,0xf1,0xf1,0x08
  971. .byte 0xe2,0x71,0x71,0x93, 0xab,0xd8,0xd8,0x73
  972. .byte 0x62,0x31,0x31,0x53, 0x2a,0x15,0x15,0x3f
  973. .byte 0x08,0x04,0x04,0x0c, 0x95,0xc7,0xc7,0x52
  974. .byte 0x46,0x23,0x23,0x65, 0x9d,0xc3,0xc3,0x5e
  975. .byte 0x30,0x18,0x18,0x28, 0x37,0x96,0x96,0xa1
  976. .byte 0x0a,0x05,0x05,0x0f, 0x2f,0x9a,0x9a,0xb5
  977. .byte 0x0e,0x07,0x07,0x09, 0x24,0x12,0x12,0x36
  978. .byte 0x1b,0x80,0x80,0x9b, 0xdf,0xe2,0xe2,0x3d
  979. .byte 0xcd,0xeb,0xeb,0x26, 0x4e,0x27,0x27,0x69
  980. .byte 0x7f,0xb2,0xb2,0xcd, 0xea,0x75,0x75,0x9f
  981. .byte 0x12,0x09,0x09,0x1b, 0x1d,0x83,0x83,0x9e
  982. .byte 0x58,0x2c,0x2c,0x74, 0x34,0x1a,0x1a,0x2e
  983. .byte 0x36,0x1b,0x1b,0x2d, 0xdc,0x6e,0x6e,0xb2
  984. .byte 0xb4,0x5a,0x5a,0xee, 0x5b,0xa0,0xa0,0xfb
  985. .byte 0xa4,0x52,0x52,0xf6, 0x76,0x3b,0x3b,0x4d
  986. .byte 0xb7,0xd6,0xd6,0x61, 0x7d,0xb3,0xb3,0xce
  987. .byte 0x52,0x29,0x29,0x7b, 0xdd,0xe3,0xe3,0x3e
  988. .byte 0x5e,0x2f,0x2f,0x71, 0x13,0x84,0x84,0x97
  989. .byte 0xa6,0x53,0x53,0xf5, 0xb9,0xd1,0xd1,0x68
  990. .byte 0x00,0x00,0x00,0x00, 0xc1,0xed,0xed,0x2c
  991. .byte 0x40,0x20,0x20,0x60, 0xe3,0xfc,0xfc,0x1f
  992. .byte 0x79,0xb1,0xb1,0xc8, 0xb6,0x5b,0x5b,0xed
  993. .byte 0xd4,0x6a,0x6a,0xbe, 0x8d,0xcb,0xcb,0x46
  994. .byte 0x67,0xbe,0xbe,0xd9, 0x72,0x39,0x39,0x4b
  995. .byte 0x94,0x4a,0x4a,0xde, 0x98,0x4c,0x4c,0xd4
  996. .byte 0xb0,0x58,0x58,0xe8, 0x85,0xcf,0xcf,0x4a
  997. .byte 0xbb,0xd0,0xd0,0x6b, 0xc5,0xef,0xef,0x2a
  998. .byte 0x4f,0xaa,0xaa,0xe5, 0xed,0xfb,0xfb,0x16
  999. .byte 0x86,0x43,0x43,0xc5, 0x9a,0x4d,0x4d,0xd7
  1000. .byte 0x66,0x33,0x33,0x55, 0x11,0x85,0x85,0x94
  1001. .byte 0x8a,0x45,0x45,0xcf, 0xe9,0xf9,0xf9,0x10
  1002. .byte 0x04,0x02,0x02,0x06, 0xfe,0x7f,0x7f,0x81
  1003. .byte 0xa0,0x50,0x50,0xf0, 0x78,0x3c,0x3c,0x44
  1004. .byte 0x25,0x9f,0x9f,0xba, 0x4b,0xa8,0xa8,0xe3
  1005. .byte 0xa2,0x51,0x51,0xf3, 0x5d,0xa3,0xa3,0xfe
  1006. .byte 0x80,0x40,0x40,0xc0, 0x05,0x8f,0x8f,0x8a
  1007. .byte 0x3f,0x92,0x92,0xad, 0x21,0x9d,0x9d,0xbc
  1008. .byte 0x70,0x38,0x38,0x48, 0xf1,0xf5,0xf5,0x04
  1009. .byte 0x63,0xbc,0xbc,0xdf, 0x77,0xb6,0xb6,0xc1
  1010. .byte 0xaf,0xda,0xda,0x75, 0x42,0x21,0x21,0x63
  1011. .byte 0x20,0x10,0x10,0x30, 0xe5,0xff,0xff,0x1a
  1012. .byte 0xfd,0xf3,0xf3,0x0e, 0xbf,0xd2,0xd2,0x6d
  1013. .byte 0x81,0xcd,0xcd,0x4c, 0x18,0x0c,0x0c,0x14
  1014. .byte 0x26,0x13,0x13,0x35, 0xc3,0xec,0xec,0x2f
  1015. .byte 0xbe,0x5f,0x5f,0xe1, 0x35,0x97,0x97,0xa2
  1016. .byte 0x88,0x44,0x44,0xcc, 0x2e,0x17,0x17,0x39
  1017. .byte 0x93,0xc4,0xc4,0x57, 0x55,0xa7,0xa7,0xf2
  1018. .byte 0xfc,0x7e,0x7e,0x82, 0x7a,0x3d,0x3d,0x47
  1019. .byte 0xc8,0x64,0x64,0xac, 0xba,0x5d,0x5d,0xe7
  1020. .byte 0x32,0x19,0x19,0x2b, 0xe6,0x73,0x73,0x95
  1021. .byte 0xc0,0x60,0x60,0xa0, 0x19,0x81,0x81,0x98
  1022. .byte 0x9e,0x4f,0x4f,0xd1, 0xa3,0xdc,0xdc,0x7f
  1023. .byte 0x44,0x22,0x22,0x66, 0x54,0x2a,0x2a,0x7e
  1024. .byte 0x3b,0x90,0x90,0xab, 0x0b,0x88,0x88,0x83
  1025. .byte 0x8c,0x46,0x46,0xca, 0xc7,0xee,0xee,0x29
  1026. .byte 0x6b,0xb8,0xb8,0xd3, 0x28,0x14,0x14,0x3c
  1027. .byte 0xa7,0xde,0xde,0x79, 0xbc,0x5e,0x5e,0xe2
  1028. .byte 0x16,0x0b,0x0b,0x1d, 0xad,0xdb,0xdb,0x76
  1029. .byte 0xdb,0xe0,0xe0,0x3b, 0x64,0x32,0x32,0x56
  1030. .byte 0x74,0x3a,0x3a,0x4e, 0x14,0x0a,0x0a,0x1e
  1031. .byte 0x92,0x49,0x49,0xdb, 0x0c,0x06,0x06,0x0a
  1032. .byte 0x48,0x24,0x24,0x6c, 0xb8,0x5c,0x5c,0xe4
  1033. .byte 0x9f,0xc2,0xc2,0x5d, 0xbd,0xd3,0xd3,0x6e
  1034. .byte 0x43,0xac,0xac,0xef, 0xc4,0x62,0x62,0xa6
  1035. .byte 0x39,0x91,0x91,0xa8, 0x31,0x95,0x95,0xa4
  1036. .byte 0xd3,0xe4,0xe4,0x37, 0xf2,0x79,0x79,0x8b
  1037. .byte 0xd5,0xe7,0xe7,0x32, 0x8b,0xc8,0xc8,0x43
  1038. .byte 0x6e,0x37,0x37,0x59, 0xda,0x6d,0x6d,0xb7
  1039. .byte 0x01,0x8d,0x8d,0x8c, 0xb1,0xd5,0xd5,0x64
  1040. .byte 0x9c,0x4e,0x4e,0xd2, 0x49,0xa9,0xa9,0xe0
  1041. .byte 0xd8,0x6c,0x6c,0xb4, 0xac,0x56,0x56,0xfa
  1042. .byte 0xf3,0xf4,0xf4,0x07, 0xcf,0xea,0xea,0x25
  1043. .byte 0xca,0x65,0x65,0xaf, 0xf4,0x7a,0x7a,0x8e
  1044. .byte 0x47,0xae,0xae,0xe9, 0x10,0x08,0x08,0x18
  1045. .byte 0x6f,0xba,0xba,0xd5, 0xf0,0x78,0x78,0x88
  1046. .byte 0x4a,0x25,0x25,0x6f, 0x5c,0x2e,0x2e,0x72
  1047. .byte 0x38,0x1c,0x1c,0x24, 0x57,0xa6,0xa6,0xf1
  1048. .byte 0x73,0xb4,0xb4,0xc7, 0x97,0xc6,0xc6,0x51
  1049. .byte 0xcb,0xe8,0xe8,0x23, 0xa1,0xdd,0xdd,0x7c
  1050. .byte 0xe8,0x74,0x74,0x9c, 0x3e,0x1f,0x1f,0x21
  1051. .byte 0x96,0x4b,0x4b,0xdd, 0x61,0xbd,0xbd,0xdc
  1052. .byte 0x0d,0x8b,0x8b,0x86, 0x0f,0x8a,0x8a,0x85
  1053. .byte 0xe0,0x70,0x70,0x90, 0x7c,0x3e,0x3e,0x42
  1054. .byte 0x71,0xb5,0xb5,0xc4, 0xcc,0x66,0x66,0xaa
  1055. .byte 0x90,0x48,0x48,0xd8, 0x06,0x03,0x03,0x05
  1056. .byte 0xf7,0xf6,0xf6,0x01, 0x1c,0x0e,0x0e,0x12
  1057. .byte 0xc2,0x61,0x61,0xa3, 0x6a,0x35,0x35,0x5f
  1058. .byte 0xae,0x57,0x57,0xf9, 0x69,0xb9,0xb9,0xd0
  1059. .byte 0x17,0x86,0x86,0x91, 0x99,0xc1,0xc1,0x58
  1060. .byte 0x3a,0x1d,0x1d,0x27, 0x27,0x9e,0x9e,0xb9
  1061. .byte 0xd9,0xe1,0xe1,0x38, 0xeb,0xf8,0xf8,0x13
  1062. .byte 0x2b,0x98,0x98,0xb3, 0x22,0x11,0x11,0x33
  1063. .byte 0xd2,0x69,0x69,0xbb, 0xa9,0xd9,0xd9,0x70
  1064. .byte 0x07,0x8e,0x8e,0x89, 0x33,0x94,0x94,0xa7
  1065. .byte 0x2d,0x9b,0x9b,0xb6, 0x3c,0x1e,0x1e,0x22
  1066. .byte 0x15,0x87,0x87,0x92, 0xc9,0xe9,0xe9,0x20
  1067. .byte 0x87,0xce,0xce,0x49, 0xaa,0x55,0x55,0xff
  1068. .byte 0x50,0x28,0x28,0x78, 0xa5,0xdf,0xdf,0x7a
  1069. .byte 0x03,0x8c,0x8c,0x8f, 0x59,0xa1,0xa1,0xf8
  1070. .byte 0x09,0x89,0x89,0x80, 0x1a,0x0d,0x0d,0x17
  1071. .byte 0x65,0xbf,0xbf,0xda, 0xd7,0xe6,0xe6,0x31
  1072. .byte 0x84,0x42,0x42,0xc6, 0xd0,0x68,0x68,0xb8
  1073. .byte 0x82,0x41,0x41,0xc3, 0x29,0x99,0x99,0xb0
  1074. .byte 0x5a,0x2d,0x2d,0x77, 0x1e,0x0f,0x0f,0x11
  1075. .byte 0x7b,0xb0,0xb0,0xcb, 0xa8,0x54,0x54,0xfc
  1076. .byte 0x6d,0xbb,0xbb,0xd6, 0x2c,0x16,0x16,0x3a
  1077. AES_Te4:
  1078. .byte 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5
  1079. .byte 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76
  1080. .byte 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0
  1081. .byte 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0
  1082. .byte 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc
  1083. .byte 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15
  1084. .byte 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a
  1085. .byte 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75
  1086. .byte 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0
  1087. .byte 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84
  1088. .byte 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b
  1089. .byte 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf
  1090. .byte 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85
  1091. .byte 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8
  1092. .byte 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5
  1093. .byte 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2
  1094. .byte 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17
  1095. .byte 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73
  1096. .byte 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88
  1097. .byte 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb
  1098. .byte 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c
  1099. .byte 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79
  1100. .byte 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9
  1101. .byte 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08
  1102. .byte 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6
  1103. .byte 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a
  1104. .byte 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e
  1105. .byte 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e
  1106. .byte 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94
  1107. .byte 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf
  1108. .byte 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68
  1109. .byte 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  1110. rcon:
  1111. .byte 0x01,0x00,0x00,0x00, 0x02,0x00,0x00,0x00
  1112. .byte 0x04,0x00,0x00,0x00, 0x08,0x00,0x00,0x00
  1113. .byte 0x10,0x00,0x00,0x00, 0x20,0x00,0x00,0x00
  1114. .byte 0x40,0x00,0x00,0x00, 0x80,0x00,0x00,0x00
  1115. .byte 0x1B,0x00,0x00,0x00, 0x36,0x00,0x00,0x00
  1116. .align 128
  1117. AES_Td:
  1118. .byte 0x51,0xf4,0xa7,0x50, 0x7e,0x41,0x65,0x53
  1119. .byte 0x1a,0x17,0xa4,0xc3, 0x3a,0x27,0x5e,0x96
  1120. .byte 0x3b,0xab,0x6b,0xcb, 0x1f,0x9d,0x45,0xf1
  1121. .byte 0xac,0xfa,0x58,0xab, 0x4b,0xe3,0x03,0x93
  1122. .byte 0x20,0x30,0xfa,0x55, 0xad,0x76,0x6d,0xf6
  1123. .byte 0x88,0xcc,0x76,0x91, 0xf5,0x02,0x4c,0x25
  1124. .byte 0x4f,0xe5,0xd7,0xfc, 0xc5,0x2a,0xcb,0xd7
  1125. .byte 0x26,0x35,0x44,0x80, 0xb5,0x62,0xa3,0x8f
  1126. .byte 0xde,0xb1,0x5a,0x49, 0x25,0xba,0x1b,0x67
  1127. .byte 0x45,0xea,0x0e,0x98, 0x5d,0xfe,0xc0,0xe1
  1128. .byte 0xc3,0x2f,0x75,0x02, 0x81,0x4c,0xf0,0x12
  1129. .byte 0x8d,0x46,0x97,0xa3, 0x6b,0xd3,0xf9,0xc6
  1130. .byte 0x03,0x8f,0x5f,0xe7, 0x15,0x92,0x9c,0x95
  1131. .byte 0xbf,0x6d,0x7a,0xeb, 0x95,0x52,0x59,0xda
  1132. .byte 0xd4,0xbe,0x83,0x2d, 0x58,0x74,0x21,0xd3
  1133. .byte 0x49,0xe0,0x69,0x29, 0x8e,0xc9,0xc8,0x44
  1134. .byte 0x75,0xc2,0x89,0x6a, 0xf4,0x8e,0x79,0x78
  1135. .byte 0x99,0x58,0x3e,0x6b, 0x27,0xb9,0x71,0xdd
  1136. .byte 0xbe,0xe1,0x4f,0xb6, 0xf0,0x88,0xad,0x17
  1137. .byte 0xc9,0x20,0xac,0x66, 0x7d,0xce,0x3a,0xb4
  1138. .byte 0x63,0xdf,0x4a,0x18, 0xe5,0x1a,0x31,0x82
  1139. .byte 0x97,0x51,0x33,0x60, 0x62,0x53,0x7f,0x45
  1140. .byte 0xb1,0x64,0x77,0xe0, 0xbb,0x6b,0xae,0x84
  1141. .byte 0xfe,0x81,0xa0,0x1c, 0xf9,0x08,0x2b,0x94
  1142. .byte 0x70,0x48,0x68,0x58, 0x8f,0x45,0xfd,0x19
  1143. .byte 0x94,0xde,0x6c,0x87, 0x52,0x7b,0xf8,0xb7
  1144. .byte 0xab,0x73,0xd3,0x23, 0x72,0x4b,0x02,0xe2
  1145. .byte 0xe3,0x1f,0x8f,0x57, 0x66,0x55,0xab,0x2a
  1146. .byte 0xb2,0xeb,0x28,0x07, 0x2f,0xb5,0xc2,0x03
  1147. .byte 0x86,0xc5,0x7b,0x9a, 0xd3,0x37,0x08,0xa5
  1148. .byte 0x30,0x28,0x87,0xf2, 0x23,0xbf,0xa5,0xb2
  1149. .byte 0x02,0x03,0x6a,0xba, 0xed,0x16,0x82,0x5c
  1150. .byte 0x8a,0xcf,0x1c,0x2b, 0xa7,0x79,0xb4,0x92
  1151. .byte 0xf3,0x07,0xf2,0xf0, 0x4e,0x69,0xe2,0xa1
  1152. .byte 0x65,0xda,0xf4,0xcd, 0x06,0x05,0xbe,0xd5
  1153. .byte 0xd1,0x34,0x62,0x1f, 0xc4,0xa6,0xfe,0x8a
  1154. .byte 0x34,0x2e,0x53,0x9d, 0xa2,0xf3,0x55,0xa0
  1155. .byte 0x05,0x8a,0xe1,0x32, 0xa4,0xf6,0xeb,0x75
  1156. .byte 0x0b,0x83,0xec,0x39, 0x40,0x60,0xef,0xaa
  1157. .byte 0x5e,0x71,0x9f,0x06, 0xbd,0x6e,0x10,0x51
  1158. .byte 0x3e,0x21,0x8a,0xf9, 0x96,0xdd,0x06,0x3d
  1159. .byte 0xdd,0x3e,0x05,0xae, 0x4d,0xe6,0xbd,0x46
  1160. .byte 0x91,0x54,0x8d,0xb5, 0x71,0xc4,0x5d,0x05
  1161. .byte 0x04,0x06,0xd4,0x6f, 0x60,0x50,0x15,0xff
  1162. .byte 0x19,0x98,0xfb,0x24, 0xd6,0xbd,0xe9,0x97
  1163. .byte 0x89,0x40,0x43,0xcc, 0x67,0xd9,0x9e,0x77
  1164. .byte 0xb0,0xe8,0x42,0xbd, 0x07,0x89,0x8b,0x88
  1165. .byte 0xe7,0x19,0x5b,0x38, 0x79,0xc8,0xee,0xdb
  1166. .byte 0xa1,0x7c,0x0a,0x47, 0x7c,0x42,0x0f,0xe9
  1167. .byte 0xf8,0x84,0x1e,0xc9, 0x00,0x00,0x00,0x00
  1168. .byte 0x09,0x80,0x86,0x83, 0x32,0x2b,0xed,0x48
  1169. .byte 0x1e,0x11,0x70,0xac, 0x6c,0x5a,0x72,0x4e
  1170. .byte 0xfd,0x0e,0xff,0xfb, 0x0f,0x85,0x38,0x56
  1171. .byte 0x3d,0xae,0xd5,0x1e, 0x36,0x2d,0x39,0x27
  1172. .byte 0x0a,0x0f,0xd9,0x64, 0x68,0x5c,0xa6,0x21
  1173. .byte 0x9b,0x5b,0x54,0xd1, 0x24,0x36,0x2e,0x3a
  1174. .byte 0x0c,0x0a,0x67,0xb1, 0x93,0x57,0xe7,0x0f
  1175. .byte 0xb4,0xee,0x96,0xd2, 0x1b,0x9b,0x91,0x9e
  1176. .byte 0x80,0xc0,0xc5,0x4f, 0x61,0xdc,0x20,0xa2
  1177. .byte 0x5a,0x77,0x4b,0x69, 0x1c,0x12,0x1a,0x16
  1178. .byte 0xe2,0x93,0xba,0x0a, 0xc0,0xa0,0x2a,0xe5
  1179. .byte 0x3c,0x22,0xe0,0x43, 0x12,0x1b,0x17,0x1d
  1180. .byte 0x0e,0x09,0x0d,0x0b, 0xf2,0x8b,0xc7,0xad
  1181. .byte 0x2d,0xb6,0xa8,0xb9, 0x14,0x1e,0xa9,0xc8
  1182. .byte 0x57,0xf1,0x19,0x85, 0xaf,0x75,0x07,0x4c
  1183. .byte 0xee,0x99,0xdd,0xbb, 0xa3,0x7f,0x60,0xfd
  1184. .byte 0xf7,0x01,0x26,0x9f, 0x5c,0x72,0xf5,0xbc
  1185. .byte 0x44,0x66,0x3b,0xc5, 0x5b,0xfb,0x7e,0x34
  1186. .byte 0x8b,0x43,0x29,0x76, 0xcb,0x23,0xc6,0xdc
  1187. .byte 0xb6,0xed,0xfc,0x68, 0xb8,0xe4,0xf1,0x63
  1188. .byte 0xd7,0x31,0xdc,0xca, 0x42,0x63,0x85,0x10
  1189. .byte 0x13,0x97,0x22,0x40, 0x84,0xc6,0x11,0x20
  1190. .byte 0x85,0x4a,0x24,0x7d, 0xd2,0xbb,0x3d,0xf8
  1191. .byte 0xae,0xf9,0x32,0x11, 0xc7,0x29,0xa1,0x6d
  1192. .byte 0x1d,0x9e,0x2f,0x4b, 0xdc,0xb2,0x30,0xf3
  1193. .byte 0x0d,0x86,0x52,0xec, 0x77,0xc1,0xe3,0xd0
  1194. .byte 0x2b,0xb3,0x16,0x6c, 0xa9,0x70,0xb9,0x99
  1195. .byte 0x11,0x94,0x48,0xfa, 0x47,0xe9,0x64,0x22
  1196. .byte 0xa8,0xfc,0x8c,0xc4, 0xa0,0xf0,0x3f,0x1a
  1197. .byte 0x56,0x7d,0x2c,0xd8, 0x22,0x33,0x90,0xef
  1198. .byte 0x87,0x49,0x4e,0xc7, 0xd9,0x38,0xd1,0xc1
  1199. .byte 0x8c,0xca,0xa2,0xfe, 0x98,0xd4,0x0b,0x36
  1200. .byte 0xa6,0xf5,0x81,0xcf, 0xa5,0x7a,0xde,0x28
  1201. .byte 0xda,0xb7,0x8e,0x26, 0x3f,0xad,0xbf,0xa4
  1202. .byte 0x2c,0x3a,0x9d,0xe4, 0x50,0x78,0x92,0x0d
  1203. .byte 0x6a,0x5f,0xcc,0x9b, 0x54,0x7e,0x46,0x62
  1204. .byte 0xf6,0x8d,0x13,0xc2, 0x90,0xd8,0xb8,0xe8
  1205. .byte 0x2e,0x39,0xf7,0x5e, 0x82,0xc3,0xaf,0xf5
  1206. .byte 0x9f,0x5d,0x80,0xbe, 0x69,0xd0,0x93,0x7c
  1207. .byte 0x6f,0xd5,0x2d,0xa9, 0xcf,0x25,0x12,0xb3
  1208. .byte 0xc8,0xac,0x99,0x3b, 0x10,0x18,0x7d,0xa7
  1209. .byte 0xe8,0x9c,0x63,0x6e, 0xdb,0x3b,0xbb,0x7b
  1210. .byte 0xcd,0x26,0x78,0x09, 0x6e,0x59,0x18,0xf4
  1211. .byte 0xec,0x9a,0xb7,0x01, 0x83,0x4f,0x9a,0xa8
  1212. .byte 0xe6,0x95,0x6e,0x65, 0xaa,0xff,0xe6,0x7e
  1213. .byte 0x21,0xbc,0xcf,0x08, 0xef,0x15,0xe8,0xe6
  1214. .byte 0xba,0xe7,0x9b,0xd9, 0x4a,0x6f,0x36,0xce
  1215. .byte 0xea,0x9f,0x09,0xd4, 0x29,0xb0,0x7c,0xd6
  1216. .byte 0x31,0xa4,0xb2,0xaf, 0x2a,0x3f,0x23,0x31
  1217. .byte 0xc6,0xa5,0x94,0x30, 0x35,0xa2,0x66,0xc0
  1218. .byte 0x74,0x4e,0xbc,0x37, 0xfc,0x82,0xca,0xa6
  1219. .byte 0xe0,0x90,0xd0,0xb0, 0x33,0xa7,0xd8,0x15
  1220. .byte 0xf1,0x04,0x98,0x4a, 0x41,0xec,0xda,0xf7
  1221. .byte 0x7f,0xcd,0x50,0x0e, 0x17,0x91,0xf6,0x2f
  1222. .byte 0x76,0x4d,0xd6,0x8d, 0x43,0xef,0xb0,0x4d
  1223. .byte 0xcc,0xaa,0x4d,0x54, 0xe4,0x96,0x04,0xdf
  1224. .byte 0x9e,0xd1,0xb5,0xe3, 0x4c,0x6a,0x88,0x1b
  1225. .byte 0xc1,0x2c,0x1f,0xb8, 0x46,0x65,0x51,0x7f
  1226. .byte 0x9d,0x5e,0xea,0x04, 0x01,0x8c,0x35,0x5d
  1227. .byte 0xfa,0x87,0x74,0x73, 0xfb,0x0b,0x41,0x2e
  1228. .byte 0xb3,0x67,0x1d,0x5a, 0x92,0xdb,0xd2,0x52
  1229. .byte 0xe9,0x10,0x56,0x33, 0x6d,0xd6,0x47,0x13
  1230. .byte 0x9a,0xd7,0x61,0x8c, 0x37,0xa1,0x0c,0x7a
  1231. .byte 0x59,0xf8,0x14,0x8e, 0xeb,0x13,0x3c,0x89
  1232. .byte 0xce,0xa9,0x27,0xee, 0xb7,0x61,0xc9,0x35
  1233. .byte 0xe1,0x1c,0xe5,0xed, 0x7a,0x47,0xb1,0x3c
  1234. .byte 0x9c,0xd2,0xdf,0x59, 0x55,0xf2,0x73,0x3f
  1235. .byte 0x18,0x14,0xce,0x79, 0x73,0xc7,0x37,0xbf
  1236. .byte 0x53,0xf7,0xcd,0xea, 0x5f,0xfd,0xaa,0x5b
  1237. .byte 0xdf,0x3d,0x6f,0x14, 0x78,0x44,0xdb,0x86
  1238. .byte 0xca,0xaf,0xf3,0x81, 0xb9,0x68,0xc4,0x3e
  1239. .byte 0x38,0x24,0x34,0x2c, 0xc2,0xa3,0x40,0x5f
  1240. .byte 0x16,0x1d,0xc3,0x72, 0xbc,0xe2,0x25,0x0c
  1241. .byte 0x28,0x3c,0x49,0x8b, 0xff,0x0d,0x95,0x41
  1242. .byte 0x39,0xa8,0x01,0x71, 0x08,0x0c,0xb3,0xde
  1243. .byte 0xd8,0xb4,0xe4,0x9c, 0x64,0x56,0xc1,0x90
  1244. .byte 0x7b,0xcb,0x84,0x61, 0xd5,0x32,0xb6,0x70
  1245. .byte 0x48,0x6c,0x5c,0x74, 0xd0,0xb8,0x57,0x42
  1246. AES_Td4:
  1247. .byte 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38
  1248. .byte 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb
  1249. .byte 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87
  1250. .byte 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb
  1251. .byte 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d
  1252. .byte 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e
  1253. .byte 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2
  1254. .byte 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25
  1255. .byte 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16
  1256. .byte 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92
  1257. .byte 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda
  1258. .byte 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84
  1259. .byte 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a
  1260. .byte 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06
  1261. .byte 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02
  1262. .byte 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b
  1263. .byte 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea
  1264. .byte 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73
  1265. .byte 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85
  1266. .byte 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e
  1267. .byte 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89
  1268. .byte 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b
  1269. .byte 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20
  1270. .byte 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4
  1271. .byte 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31
  1272. .byte 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f
  1273. .byte 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d
  1274. .byte 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef
  1275. .byte 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0
  1276. .byte 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61
  1277. .byte 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26
  1278. .byte 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
  1279. .cstring "AES for C64x+, CRYPTOGAMS by <appro\@openssl.org>"
  1280. .align 4
  1281. ___
  1282. print $code;