aes-armv4.pl 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. #!/usr/bin/env perl
  2. # ====================================================================
  3. # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
  4. # project. The module is, however, dual licensed under OpenSSL and
  5. # CRYPTOGAMS licenses depending on where you obtain it. For further
  6. # details see http://www.openssl.org/~appro/cryptogams/.
  7. # ====================================================================
  8. # AES for ARMv4
  9. # January 2007.
  10. #
  11. # Code uses single 1K S-box and is >2 times faster than code generated
  12. # by gcc-3.4.1. This is thanks to unique feature of ARMv4 ISA, which
  13. # allows to merge logical or arithmetic operation with shift or rotate
  14. # in one instruction and emit combined result every cycle. The module
  15. # is endian-neutral. The performance is ~42 cycles/byte for 128-bit
  16. # key [on single-issue Xscale PXA250 core].
  17. # May 2007.
  18. #
  19. # AES_set_[en|de]crypt_key is added.
  20. # July 2010.
  21. #
  22. # Rescheduling for dual-issue pipeline resulted in 12% improvement on
  23. # Cortex A8 core and ~25 cycles per byte processed with 128-bit key.
  24. # February 2011.
  25. #
  26. # Profiler-assisted and platform-specific optimization resulted in 16%
  27. # improvement on Cortex A8 core and ~21.5 cycles per byte.
  28. while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {}
  29. open STDOUT,">$output";
  30. $s0="r0";
  31. $s1="r1";
  32. $s2="r2";
  33. $s3="r3";
  34. $t1="r4";
  35. $t2="r5";
  36. $t3="r6";
  37. $i1="r7";
  38. $i2="r8";
  39. $i3="r9";
  40. $tbl="r10";
  41. $key="r11";
  42. $rounds="r12";
  43. $code=<<___;
  44. #include "arm_arch.h"
  45. .text
  46. .code 32
  47. .type AES_Te,%object
  48. .align 5
  49. AES_Te:
  50. .word 0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d
  51. .word 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554
  52. .word 0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d
  53. .word 0xe7fefe19, 0xb5d7d762, 0x4dababe6, 0xec76769a
  54. .word 0x8fcaca45, 0x1f82829d, 0x89c9c940, 0xfa7d7d87
  55. .word 0xeffafa15, 0xb25959eb, 0x8e4747c9, 0xfbf0f00b
  56. .word 0x41adadec, 0xb3d4d467, 0x5fa2a2fd, 0x45afafea
  57. .word 0x239c9cbf, 0x53a4a4f7, 0xe4727296, 0x9bc0c05b
  58. .word 0x75b7b7c2, 0xe1fdfd1c, 0x3d9393ae, 0x4c26266a
  59. .word 0x6c36365a, 0x7e3f3f41, 0xf5f7f702, 0x83cccc4f
  60. .word 0x6834345c, 0x51a5a5f4, 0xd1e5e534, 0xf9f1f108
  61. .word 0xe2717193, 0xabd8d873, 0x62313153, 0x2a15153f
  62. .word 0x0804040c, 0x95c7c752, 0x46232365, 0x9dc3c35e
  63. .word 0x30181828, 0x379696a1, 0x0a05050f, 0x2f9a9ab5
  64. .word 0x0e070709, 0x24121236, 0x1b80809b, 0xdfe2e23d
  65. .word 0xcdebeb26, 0x4e272769, 0x7fb2b2cd, 0xea75759f
  66. .word 0x1209091b, 0x1d83839e, 0x582c2c74, 0x341a1a2e
  67. .word 0x361b1b2d, 0xdc6e6eb2, 0xb45a5aee, 0x5ba0a0fb
  68. .word 0xa45252f6, 0x763b3b4d, 0xb7d6d661, 0x7db3b3ce
  69. .word 0x5229297b, 0xdde3e33e, 0x5e2f2f71, 0x13848497
  70. .word 0xa65353f5, 0xb9d1d168, 0x00000000, 0xc1eded2c
  71. .word 0x40202060, 0xe3fcfc1f, 0x79b1b1c8, 0xb65b5bed
  72. .word 0xd46a6abe, 0x8dcbcb46, 0x67bebed9, 0x7239394b
  73. .word 0x944a4ade, 0x984c4cd4, 0xb05858e8, 0x85cfcf4a
  74. .word 0xbbd0d06b, 0xc5efef2a, 0x4faaaae5, 0xedfbfb16
  75. .word 0x864343c5, 0x9a4d4dd7, 0x66333355, 0x11858594
  76. .word 0x8a4545cf, 0xe9f9f910, 0x04020206, 0xfe7f7f81
  77. .word 0xa05050f0, 0x783c3c44, 0x259f9fba, 0x4ba8a8e3
  78. .word 0xa25151f3, 0x5da3a3fe, 0x804040c0, 0x058f8f8a
  79. .word 0x3f9292ad, 0x219d9dbc, 0x70383848, 0xf1f5f504
  80. .word 0x63bcbcdf, 0x77b6b6c1, 0xafdada75, 0x42212163
  81. .word 0x20101030, 0xe5ffff1a, 0xfdf3f30e, 0xbfd2d26d
  82. .word 0x81cdcd4c, 0x180c0c14, 0x26131335, 0xc3ecec2f
  83. .word 0xbe5f5fe1, 0x359797a2, 0x884444cc, 0x2e171739
  84. .word 0x93c4c457, 0x55a7a7f2, 0xfc7e7e82, 0x7a3d3d47
  85. .word 0xc86464ac, 0xba5d5de7, 0x3219192b, 0xe6737395
  86. .word 0xc06060a0, 0x19818198, 0x9e4f4fd1, 0xa3dcdc7f
  87. .word 0x44222266, 0x542a2a7e, 0x3b9090ab, 0x0b888883
  88. .word 0x8c4646ca, 0xc7eeee29, 0x6bb8b8d3, 0x2814143c
  89. .word 0xa7dede79, 0xbc5e5ee2, 0x160b0b1d, 0xaddbdb76
  90. .word 0xdbe0e03b, 0x64323256, 0x743a3a4e, 0x140a0a1e
  91. .word 0x924949db, 0x0c06060a, 0x4824246c, 0xb85c5ce4
  92. .word 0x9fc2c25d, 0xbdd3d36e, 0x43acacef, 0xc46262a6
  93. .word 0x399191a8, 0x319595a4, 0xd3e4e437, 0xf279798b
  94. .word 0xd5e7e732, 0x8bc8c843, 0x6e373759, 0xda6d6db7
  95. .word 0x018d8d8c, 0xb1d5d564, 0x9c4e4ed2, 0x49a9a9e0
  96. .word 0xd86c6cb4, 0xac5656fa, 0xf3f4f407, 0xcfeaea25
  97. .word 0xca6565af, 0xf47a7a8e, 0x47aeaee9, 0x10080818
  98. .word 0x6fbabad5, 0xf0787888, 0x4a25256f, 0x5c2e2e72
  99. .word 0x381c1c24, 0x57a6a6f1, 0x73b4b4c7, 0x97c6c651
  100. .word 0xcbe8e823, 0xa1dddd7c, 0xe874749c, 0x3e1f1f21
  101. .word 0x964b4bdd, 0x61bdbddc, 0x0d8b8b86, 0x0f8a8a85
  102. .word 0xe0707090, 0x7c3e3e42, 0x71b5b5c4, 0xcc6666aa
  103. .word 0x904848d8, 0x06030305, 0xf7f6f601, 0x1c0e0e12
  104. .word 0xc26161a3, 0x6a35355f, 0xae5757f9, 0x69b9b9d0
  105. .word 0x17868691, 0x99c1c158, 0x3a1d1d27, 0x279e9eb9
  106. .word 0xd9e1e138, 0xebf8f813, 0x2b9898b3, 0x22111133
  107. .word 0xd26969bb, 0xa9d9d970, 0x078e8e89, 0x339494a7
  108. .word 0x2d9b9bb6, 0x3c1e1e22, 0x15878792, 0xc9e9e920
  109. .word 0x87cece49, 0xaa5555ff, 0x50282878, 0xa5dfdf7a
  110. .word 0x038c8c8f, 0x59a1a1f8, 0x09898980, 0x1a0d0d17
  111. .word 0x65bfbfda, 0xd7e6e631, 0x844242c6, 0xd06868b8
  112. .word 0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11
  113. .word 0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a
  114. @ Te4[256]
  115. .byte 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5
  116. .byte 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76
  117. .byte 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0
  118. .byte 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0
  119. .byte 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc
  120. .byte 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15
  121. .byte 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a
  122. .byte 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75
  123. .byte 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0
  124. .byte 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84
  125. .byte 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b
  126. .byte 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf
  127. .byte 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85
  128. .byte 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8
  129. .byte 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5
  130. .byte 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2
  131. .byte 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17
  132. .byte 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73
  133. .byte 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88
  134. .byte 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb
  135. .byte 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c
  136. .byte 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79
  137. .byte 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9
  138. .byte 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08
  139. .byte 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6
  140. .byte 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a
  141. .byte 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e
  142. .byte 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e
  143. .byte 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94
  144. .byte 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf
  145. .byte 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68
  146. .byte 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  147. @ rcon[]
  148. .word 0x01000000, 0x02000000, 0x04000000, 0x08000000
  149. .word 0x10000000, 0x20000000, 0x40000000, 0x80000000
  150. .word 0x1B000000, 0x36000000, 0, 0, 0, 0, 0, 0
  151. .size AES_Te,.-AES_Te
  152. @ void AES_encrypt(const unsigned char *in, unsigned char *out,
  153. @ const AES_KEY *key) {
  154. .global AES_encrypt
  155. .type AES_encrypt,%function
  156. .align 5
  157. AES_encrypt:
  158. sub r3,pc,#8 @ AES_encrypt
  159. stmdb sp!,{r1,r4-r12,lr}
  160. mov $rounds,r0 @ inp
  161. mov $key,r2
  162. sub $tbl,r3,#AES_encrypt-AES_Te @ Te
  163. #if __ARM_ARCH__<7
  164. ldrb $s0,[$rounds,#3] @ load input data in endian-neutral
  165. ldrb $t1,[$rounds,#2] @ manner...
  166. ldrb $t2,[$rounds,#1]
  167. ldrb $t3,[$rounds,#0]
  168. orr $s0,$s0,$t1,lsl#8
  169. ldrb $s1,[$rounds,#7]
  170. orr $s0,$s0,$t2,lsl#16
  171. ldrb $t1,[$rounds,#6]
  172. orr $s0,$s0,$t3,lsl#24
  173. ldrb $t2,[$rounds,#5]
  174. ldrb $t3,[$rounds,#4]
  175. orr $s1,$s1,$t1,lsl#8
  176. ldrb $s2,[$rounds,#11]
  177. orr $s1,$s1,$t2,lsl#16
  178. ldrb $t1,[$rounds,#10]
  179. orr $s1,$s1,$t3,lsl#24
  180. ldrb $t2,[$rounds,#9]
  181. ldrb $t3,[$rounds,#8]
  182. orr $s2,$s2,$t1,lsl#8
  183. ldrb $s3,[$rounds,#15]
  184. orr $s2,$s2,$t2,lsl#16
  185. ldrb $t1,[$rounds,#14]
  186. orr $s2,$s2,$t3,lsl#24
  187. ldrb $t2,[$rounds,#13]
  188. ldrb $t3,[$rounds,#12]
  189. orr $s3,$s3,$t1,lsl#8
  190. orr $s3,$s3,$t2,lsl#16
  191. orr $s3,$s3,$t3,lsl#24
  192. #else
  193. ldr $s0,[$rounds,#0]
  194. ldr $s1,[$rounds,#4]
  195. ldr $s2,[$rounds,#8]
  196. ldr $s3,[$rounds,#12]
  197. #ifdef __ARMEL__
  198. rev $s0,$s0
  199. rev $s1,$s1
  200. rev $s2,$s2
  201. rev $s3,$s3
  202. #endif
  203. #endif
  204. bl _armv4_AES_encrypt
  205. ldr $rounds,[sp],#4 @ pop out
  206. #if __ARM_ARCH__>=7
  207. #ifdef __ARMEL__
  208. rev $s0,$s0
  209. rev $s1,$s1
  210. rev $s2,$s2
  211. rev $s3,$s3
  212. #endif
  213. str $s0,[$rounds,#0]
  214. str $s1,[$rounds,#4]
  215. str $s2,[$rounds,#8]
  216. str $s3,[$rounds,#12]
  217. #else
  218. mov $t1,$s0,lsr#24 @ write output in endian-neutral
  219. mov $t2,$s0,lsr#16 @ manner...
  220. mov $t3,$s0,lsr#8
  221. strb $t1,[$rounds,#0]
  222. strb $t2,[$rounds,#1]
  223. mov $t1,$s1,lsr#24
  224. strb $t3,[$rounds,#2]
  225. mov $t2,$s1,lsr#16
  226. strb $s0,[$rounds,#3]
  227. mov $t3,$s1,lsr#8
  228. strb $t1,[$rounds,#4]
  229. strb $t2,[$rounds,#5]
  230. mov $t1,$s2,lsr#24
  231. strb $t3,[$rounds,#6]
  232. mov $t2,$s2,lsr#16
  233. strb $s1,[$rounds,#7]
  234. mov $t3,$s2,lsr#8
  235. strb $t1,[$rounds,#8]
  236. strb $t2,[$rounds,#9]
  237. mov $t1,$s3,lsr#24
  238. strb $t3,[$rounds,#10]
  239. mov $t2,$s3,lsr#16
  240. strb $s2,[$rounds,#11]
  241. mov $t3,$s3,lsr#8
  242. strb $t1,[$rounds,#12]
  243. strb $t2,[$rounds,#13]
  244. strb $t3,[$rounds,#14]
  245. strb $s3,[$rounds,#15]
  246. #endif
  247. #if __ARM_ARCH__>=5
  248. ldmia sp!,{r4-r12,pc}
  249. #else
  250. ldmia sp!,{r4-r12,lr}
  251. tst lr,#1
  252. moveq pc,lr @ be binary compatible with V4, yet
  253. bx lr @ interoperable with Thumb ISA:-)
  254. #endif
  255. .size AES_encrypt,.-AES_encrypt
  256. .type _armv4_AES_encrypt,%function
  257. .align 2
  258. _armv4_AES_encrypt:
  259. str lr,[sp,#-4]! @ push lr
  260. ldmia $key!,{$t1-$i1}
  261. eor $s0,$s0,$t1
  262. ldr $rounds,[$key,#240-16]
  263. eor $s1,$s1,$t2
  264. eor $s2,$s2,$t3
  265. eor $s3,$s3,$i1
  266. sub $rounds,$rounds,#1
  267. mov lr,#255
  268. and $i1,lr,$s0
  269. and $i2,lr,$s0,lsr#8
  270. and $i3,lr,$s0,lsr#16
  271. mov $s0,$s0,lsr#24
  272. .Lenc_loop:
  273. ldr $t1,[$tbl,$i1,lsl#2] @ Te3[s0>>0]
  274. and $i1,lr,$s1,lsr#16 @ i0
  275. ldr $t2,[$tbl,$i2,lsl#2] @ Te2[s0>>8]
  276. and $i2,lr,$s1
  277. ldr $t3,[$tbl,$i3,lsl#2] @ Te1[s0>>16]
  278. and $i3,lr,$s1,lsr#8
  279. ldr $s0,[$tbl,$s0,lsl#2] @ Te0[s0>>24]
  280. mov $s1,$s1,lsr#24
  281. ldr $i1,[$tbl,$i1,lsl#2] @ Te1[s1>>16]
  282. ldr $i2,[$tbl,$i2,lsl#2] @ Te3[s1>>0]
  283. ldr $i3,[$tbl,$i3,lsl#2] @ Te2[s1>>8]
  284. eor $s0,$s0,$i1,ror#8
  285. ldr $s1,[$tbl,$s1,lsl#2] @ Te0[s1>>24]
  286. and $i1,lr,$s2,lsr#8 @ i0
  287. eor $t2,$t2,$i2,ror#8
  288. and $i2,lr,$s2,lsr#16 @ i1
  289. eor $t3,$t3,$i3,ror#8
  290. and $i3,lr,$s2
  291. ldr $i1,[$tbl,$i1,lsl#2] @ Te2[s2>>8]
  292. eor $s1,$s1,$t1,ror#24
  293. ldr $i2,[$tbl,$i2,lsl#2] @ Te1[s2>>16]
  294. mov $s2,$s2,lsr#24
  295. ldr $i3,[$tbl,$i3,lsl#2] @ Te3[s2>>0]
  296. eor $s0,$s0,$i1,ror#16
  297. ldr $s2,[$tbl,$s2,lsl#2] @ Te0[s2>>24]
  298. and $i1,lr,$s3 @ i0
  299. eor $s1,$s1,$i2,ror#8
  300. and $i2,lr,$s3,lsr#8 @ i1
  301. eor $t3,$t3,$i3,ror#16
  302. and $i3,lr,$s3,lsr#16 @ i2
  303. ldr $i1,[$tbl,$i1,lsl#2] @ Te3[s3>>0]
  304. eor $s2,$s2,$t2,ror#16
  305. ldr $i2,[$tbl,$i2,lsl#2] @ Te2[s3>>8]
  306. mov $s3,$s3,lsr#24
  307. ldr $i3,[$tbl,$i3,lsl#2] @ Te1[s3>>16]
  308. eor $s0,$s0,$i1,ror#24
  309. ldr $i1,[$key],#16
  310. eor $s1,$s1,$i2,ror#16
  311. ldr $s3,[$tbl,$s3,lsl#2] @ Te0[s3>>24]
  312. eor $s2,$s2,$i3,ror#8
  313. ldr $t1,[$key,#-12]
  314. eor $s3,$s3,$t3,ror#8
  315. ldr $t2,[$key,#-8]
  316. eor $s0,$s0,$i1
  317. ldr $t3,[$key,#-4]
  318. and $i1,lr,$s0
  319. eor $s1,$s1,$t1
  320. and $i2,lr,$s0,lsr#8
  321. eor $s2,$s2,$t2
  322. and $i3,lr,$s0,lsr#16
  323. eor $s3,$s3,$t3
  324. mov $s0,$s0,lsr#24
  325. subs $rounds,$rounds,#1
  326. bne .Lenc_loop
  327. add $tbl,$tbl,#2
  328. ldrb $t1,[$tbl,$i1,lsl#2] @ Te4[s0>>0]
  329. and $i1,lr,$s1,lsr#16 @ i0
  330. ldrb $t2,[$tbl,$i2,lsl#2] @ Te4[s0>>8]
  331. and $i2,lr,$s1
  332. ldrb $t3,[$tbl,$i3,lsl#2] @ Te4[s0>>16]
  333. and $i3,lr,$s1,lsr#8
  334. ldrb $s0,[$tbl,$s0,lsl#2] @ Te4[s0>>24]
  335. mov $s1,$s1,lsr#24
  336. ldrb $i1,[$tbl,$i1,lsl#2] @ Te4[s1>>16]
  337. ldrb $i2,[$tbl,$i2,lsl#2] @ Te4[s1>>0]
  338. ldrb $i3,[$tbl,$i3,lsl#2] @ Te4[s1>>8]
  339. eor $s0,$i1,$s0,lsl#8
  340. ldrb $s1,[$tbl,$s1,lsl#2] @ Te4[s1>>24]
  341. and $i1,lr,$s2,lsr#8 @ i0
  342. eor $t2,$i2,$t2,lsl#8
  343. and $i2,lr,$s2,lsr#16 @ i1
  344. eor $t3,$i3,$t3,lsl#8
  345. and $i3,lr,$s2
  346. ldrb $i1,[$tbl,$i1,lsl#2] @ Te4[s2>>8]
  347. eor $s1,$t1,$s1,lsl#24
  348. ldrb $i2,[$tbl,$i2,lsl#2] @ Te4[s2>>16]
  349. mov $s2,$s2,lsr#24
  350. ldrb $i3,[$tbl,$i3,lsl#2] @ Te4[s2>>0]
  351. eor $s0,$i1,$s0,lsl#8
  352. ldrb $s2,[$tbl,$s2,lsl#2] @ Te4[s2>>24]
  353. and $i1,lr,$s3 @ i0
  354. eor $s1,$s1,$i2,lsl#16
  355. and $i2,lr,$s3,lsr#8 @ i1
  356. eor $t3,$i3,$t3,lsl#8
  357. and $i3,lr,$s3,lsr#16 @ i2
  358. ldrb $i1,[$tbl,$i1,lsl#2] @ Te4[s3>>0]
  359. eor $s2,$t2,$s2,lsl#24
  360. ldrb $i2,[$tbl,$i2,lsl#2] @ Te4[s3>>8]
  361. mov $s3,$s3,lsr#24
  362. ldrb $i3,[$tbl,$i3,lsl#2] @ Te4[s3>>16]
  363. eor $s0,$i1,$s0,lsl#8
  364. ldr $i1,[$key,#0]
  365. ldrb $s3,[$tbl,$s3,lsl#2] @ Te4[s3>>24]
  366. eor $s1,$s1,$i2,lsl#8
  367. ldr $t1,[$key,#4]
  368. eor $s2,$s2,$i3,lsl#16
  369. ldr $t2,[$key,#8]
  370. eor $s3,$t3,$s3,lsl#24
  371. ldr $t3,[$key,#12]
  372. eor $s0,$s0,$i1
  373. eor $s1,$s1,$t1
  374. eor $s2,$s2,$t2
  375. eor $s3,$s3,$t3
  376. sub $tbl,$tbl,#2
  377. ldr pc,[sp],#4 @ pop and return
  378. .size _armv4_AES_encrypt,.-_armv4_AES_encrypt
  379. .global AES_set_encrypt_key
  380. .type AES_set_encrypt_key,%function
  381. .align 5
  382. AES_set_encrypt_key:
  383. sub r3,pc,#8 @ AES_set_encrypt_key
  384. teq r0,#0
  385. moveq r0,#-1
  386. beq .Labrt
  387. teq r2,#0
  388. moveq r0,#-1
  389. beq .Labrt
  390. teq r1,#128
  391. beq .Lok
  392. teq r1,#192
  393. beq .Lok
  394. teq r1,#256
  395. movne r0,#-1
  396. bne .Labrt
  397. .Lok: stmdb sp!,{r4-r12,lr}
  398. sub $tbl,r3,#AES_set_encrypt_key-AES_Te-1024 @ Te4
  399. mov $rounds,r0 @ inp
  400. mov lr,r1 @ bits
  401. mov $key,r2 @ key
  402. #if __ARM_ARCH__<7
  403. ldrb $s0,[$rounds,#3] @ load input data in endian-neutral
  404. ldrb $t1,[$rounds,#2] @ manner...
  405. ldrb $t2,[$rounds,#1]
  406. ldrb $t3,[$rounds,#0]
  407. orr $s0,$s0,$t1,lsl#8
  408. ldrb $s1,[$rounds,#7]
  409. orr $s0,$s0,$t2,lsl#16
  410. ldrb $t1,[$rounds,#6]
  411. orr $s0,$s0,$t3,lsl#24
  412. ldrb $t2,[$rounds,#5]
  413. ldrb $t3,[$rounds,#4]
  414. orr $s1,$s1,$t1,lsl#8
  415. ldrb $s2,[$rounds,#11]
  416. orr $s1,$s1,$t2,lsl#16
  417. ldrb $t1,[$rounds,#10]
  418. orr $s1,$s1,$t3,lsl#24
  419. ldrb $t2,[$rounds,#9]
  420. ldrb $t3,[$rounds,#8]
  421. orr $s2,$s2,$t1,lsl#8
  422. ldrb $s3,[$rounds,#15]
  423. orr $s2,$s2,$t2,lsl#16
  424. ldrb $t1,[$rounds,#14]
  425. orr $s2,$s2,$t3,lsl#24
  426. ldrb $t2,[$rounds,#13]
  427. ldrb $t3,[$rounds,#12]
  428. orr $s3,$s3,$t1,lsl#8
  429. str $s0,[$key],#16
  430. orr $s3,$s3,$t2,lsl#16
  431. str $s1,[$key,#-12]
  432. orr $s3,$s3,$t3,lsl#24
  433. str $s2,[$key,#-8]
  434. str $s3,[$key,#-4]
  435. #else
  436. ldr $s0,[$rounds,#0]
  437. ldr $s1,[$rounds,#4]
  438. ldr $s2,[$rounds,#8]
  439. ldr $s3,[$rounds,#12]
  440. #ifdef __ARMEL__
  441. rev $s0,$s0
  442. rev $s1,$s1
  443. rev $s2,$s2
  444. rev $s3,$s3
  445. #endif
  446. str $s0,[$key],#16
  447. str $s1,[$key,#-12]
  448. str $s2,[$key,#-8]
  449. str $s3,[$key,#-4]
  450. #endif
  451. teq lr,#128
  452. bne .Lnot128
  453. mov $rounds,#10
  454. str $rounds,[$key,#240-16]
  455. add $t3,$tbl,#256 @ rcon
  456. mov lr,#255
  457. .L128_loop:
  458. and $t2,lr,$s3,lsr#24
  459. and $i1,lr,$s3,lsr#16
  460. ldrb $t2,[$tbl,$t2]
  461. and $i2,lr,$s3,lsr#8
  462. ldrb $i1,[$tbl,$i1]
  463. and $i3,lr,$s3
  464. ldrb $i2,[$tbl,$i2]
  465. orr $t2,$t2,$i1,lsl#24
  466. ldrb $i3,[$tbl,$i3]
  467. orr $t2,$t2,$i2,lsl#16
  468. ldr $t1,[$t3],#4 @ rcon[i++]
  469. orr $t2,$t2,$i3,lsl#8
  470. eor $t2,$t2,$t1
  471. eor $s0,$s0,$t2 @ rk[4]=rk[0]^...
  472. eor $s1,$s1,$s0 @ rk[5]=rk[1]^rk[4]
  473. str $s0,[$key],#16
  474. eor $s2,$s2,$s1 @ rk[6]=rk[2]^rk[5]
  475. str $s1,[$key,#-12]
  476. eor $s3,$s3,$s2 @ rk[7]=rk[3]^rk[6]
  477. str $s2,[$key,#-8]
  478. subs $rounds,$rounds,#1
  479. str $s3,[$key,#-4]
  480. bne .L128_loop
  481. sub r2,$key,#176
  482. b .Ldone
  483. .Lnot128:
  484. #if __ARM_ARCH__<7
  485. ldrb $i2,[$rounds,#19]
  486. ldrb $t1,[$rounds,#18]
  487. ldrb $t2,[$rounds,#17]
  488. ldrb $t3,[$rounds,#16]
  489. orr $i2,$i2,$t1,lsl#8
  490. ldrb $i3,[$rounds,#23]
  491. orr $i2,$i2,$t2,lsl#16
  492. ldrb $t1,[$rounds,#22]
  493. orr $i2,$i2,$t3,lsl#24
  494. ldrb $t2,[$rounds,#21]
  495. ldrb $t3,[$rounds,#20]
  496. orr $i3,$i3,$t1,lsl#8
  497. orr $i3,$i3,$t2,lsl#16
  498. str $i2,[$key],#8
  499. orr $i3,$i3,$t3,lsl#24
  500. str $i3,[$key,#-4]
  501. #else
  502. ldr $i2,[$rounds,#16]
  503. ldr $i3,[$rounds,#20]
  504. #ifdef __ARMEL__
  505. rev $i2,$i2
  506. rev $i3,$i3
  507. #endif
  508. str $i2,[$key],#8
  509. str $i3,[$key,#-4]
  510. #endif
  511. teq lr,#192
  512. bne .Lnot192
  513. mov $rounds,#12
  514. str $rounds,[$key,#240-24]
  515. add $t3,$tbl,#256 @ rcon
  516. mov lr,#255
  517. mov $rounds,#8
  518. .L192_loop:
  519. and $t2,lr,$i3,lsr#24
  520. and $i1,lr,$i3,lsr#16
  521. ldrb $t2,[$tbl,$t2]
  522. and $i2,lr,$i3,lsr#8
  523. ldrb $i1,[$tbl,$i1]
  524. and $i3,lr,$i3
  525. ldrb $i2,[$tbl,$i2]
  526. orr $t2,$t2,$i1,lsl#24
  527. ldrb $i3,[$tbl,$i3]
  528. orr $t2,$t2,$i2,lsl#16
  529. ldr $t1,[$t3],#4 @ rcon[i++]
  530. orr $t2,$t2,$i3,lsl#8
  531. eor $i3,$t2,$t1
  532. eor $s0,$s0,$i3 @ rk[6]=rk[0]^...
  533. eor $s1,$s1,$s0 @ rk[7]=rk[1]^rk[6]
  534. str $s0,[$key],#24
  535. eor $s2,$s2,$s1 @ rk[8]=rk[2]^rk[7]
  536. str $s1,[$key,#-20]
  537. eor $s3,$s3,$s2 @ rk[9]=rk[3]^rk[8]
  538. str $s2,[$key,#-16]
  539. subs $rounds,$rounds,#1
  540. str $s3,[$key,#-12]
  541. subeq r2,$key,#216
  542. beq .Ldone
  543. ldr $i1,[$key,#-32]
  544. ldr $i2,[$key,#-28]
  545. eor $i1,$i1,$s3 @ rk[10]=rk[4]^rk[9]
  546. eor $i3,$i2,$i1 @ rk[11]=rk[5]^rk[10]
  547. str $i1,[$key,#-8]
  548. str $i3,[$key,#-4]
  549. b .L192_loop
  550. .Lnot192:
  551. #if __ARM_ARCH__<7
  552. ldrb $i2,[$rounds,#27]
  553. ldrb $t1,[$rounds,#26]
  554. ldrb $t2,[$rounds,#25]
  555. ldrb $t3,[$rounds,#24]
  556. orr $i2,$i2,$t1,lsl#8
  557. ldrb $i3,[$rounds,#31]
  558. orr $i2,$i2,$t2,lsl#16
  559. ldrb $t1,[$rounds,#30]
  560. orr $i2,$i2,$t3,lsl#24
  561. ldrb $t2,[$rounds,#29]
  562. ldrb $t3,[$rounds,#28]
  563. orr $i3,$i3,$t1,lsl#8
  564. orr $i3,$i3,$t2,lsl#16
  565. str $i2,[$key],#8
  566. orr $i3,$i3,$t3,lsl#24
  567. str $i3,[$key,#-4]
  568. #else
  569. ldr $i2,[$rounds,#24]
  570. ldr $i3,[$rounds,#28]
  571. #ifdef __ARMEL__
  572. rev $i2,$i2
  573. rev $i3,$i3
  574. #endif
  575. str $i2,[$key],#8
  576. str $i3,[$key,#-4]
  577. #endif
  578. mov $rounds,#14
  579. str $rounds,[$key,#240-32]
  580. add $t3,$tbl,#256 @ rcon
  581. mov lr,#255
  582. mov $rounds,#7
  583. .L256_loop:
  584. and $t2,lr,$i3,lsr#24
  585. and $i1,lr,$i3,lsr#16
  586. ldrb $t2,[$tbl,$t2]
  587. and $i2,lr,$i3,lsr#8
  588. ldrb $i1,[$tbl,$i1]
  589. and $i3,lr,$i3
  590. ldrb $i2,[$tbl,$i2]
  591. orr $t2,$t2,$i1,lsl#24
  592. ldrb $i3,[$tbl,$i3]
  593. orr $t2,$t2,$i2,lsl#16
  594. ldr $t1,[$t3],#4 @ rcon[i++]
  595. orr $t2,$t2,$i3,lsl#8
  596. eor $i3,$t2,$t1
  597. eor $s0,$s0,$i3 @ rk[8]=rk[0]^...
  598. eor $s1,$s1,$s0 @ rk[9]=rk[1]^rk[8]
  599. str $s0,[$key],#32
  600. eor $s2,$s2,$s1 @ rk[10]=rk[2]^rk[9]
  601. str $s1,[$key,#-28]
  602. eor $s3,$s3,$s2 @ rk[11]=rk[3]^rk[10]
  603. str $s2,[$key,#-24]
  604. subs $rounds,$rounds,#1
  605. str $s3,[$key,#-20]
  606. subeq r2,$key,#256
  607. beq .Ldone
  608. and $t2,lr,$s3
  609. and $i1,lr,$s3,lsr#8
  610. ldrb $t2,[$tbl,$t2]
  611. and $i2,lr,$s3,lsr#16
  612. ldrb $i1,[$tbl,$i1]
  613. and $i3,lr,$s3,lsr#24
  614. ldrb $i2,[$tbl,$i2]
  615. orr $t2,$t2,$i1,lsl#8
  616. ldrb $i3,[$tbl,$i3]
  617. orr $t2,$t2,$i2,lsl#16
  618. ldr $t1,[$key,#-48]
  619. orr $t2,$t2,$i3,lsl#24
  620. ldr $i1,[$key,#-44]
  621. ldr $i2,[$key,#-40]
  622. eor $t1,$t1,$t2 @ rk[12]=rk[4]^...
  623. ldr $i3,[$key,#-36]
  624. eor $i1,$i1,$t1 @ rk[13]=rk[5]^rk[12]
  625. str $t1,[$key,#-16]
  626. eor $i2,$i2,$i1 @ rk[14]=rk[6]^rk[13]
  627. str $i1,[$key,#-12]
  628. eor $i3,$i3,$i2 @ rk[15]=rk[7]^rk[14]
  629. str $i2,[$key,#-8]
  630. str $i3,[$key,#-4]
  631. b .L256_loop
  632. .Ldone: mov r0,#0
  633. ldmia sp!,{r4-r12,lr}
  634. .Labrt: tst lr,#1
  635. moveq pc,lr @ be binary compatible with V4, yet
  636. bx lr @ interoperable with Thumb ISA:-)
  637. .size AES_set_encrypt_key,.-AES_set_encrypt_key
  638. .global AES_set_decrypt_key
  639. .type AES_set_decrypt_key,%function
  640. .align 5
  641. AES_set_decrypt_key:
  642. str lr,[sp,#-4]! @ push lr
  643. bl AES_set_encrypt_key
  644. teq r0,#0
  645. ldrne lr,[sp],#4 @ pop lr
  646. bne .Labrt
  647. stmdb sp!,{r4-r12}
  648. ldr $rounds,[r2,#240] @ AES_set_encrypt_key preserves r2,
  649. mov $key,r2 @ which is AES_KEY *key
  650. mov $i1,r2
  651. add $i2,r2,$rounds,lsl#4
  652. .Linv: ldr $s0,[$i1]
  653. ldr $s1,[$i1,#4]
  654. ldr $s2,[$i1,#8]
  655. ldr $s3,[$i1,#12]
  656. ldr $t1,[$i2]
  657. ldr $t2,[$i2,#4]
  658. ldr $t3,[$i2,#8]
  659. ldr $i3,[$i2,#12]
  660. str $s0,[$i2],#-16
  661. str $s1,[$i2,#16+4]
  662. str $s2,[$i2,#16+8]
  663. str $s3,[$i2,#16+12]
  664. str $t1,[$i1],#16
  665. str $t2,[$i1,#-12]
  666. str $t3,[$i1,#-8]
  667. str $i3,[$i1,#-4]
  668. teq $i1,$i2
  669. bne .Linv
  670. ___
  671. $mask80=$i1;
  672. $mask1b=$i2;
  673. $mask7f=$i3;
  674. $code.=<<___;
  675. ldr $s0,[$key,#16]! @ prefetch tp1
  676. mov $mask80,#0x80
  677. mov $mask1b,#0x1b
  678. orr $mask80,$mask80,#0x8000
  679. orr $mask1b,$mask1b,#0x1b00
  680. orr $mask80,$mask80,$mask80,lsl#16
  681. orr $mask1b,$mask1b,$mask1b,lsl#16
  682. sub $rounds,$rounds,#1
  683. mvn $mask7f,$mask80
  684. mov $rounds,$rounds,lsl#2 @ (rounds-1)*4
  685. .Lmix: and $t1,$s0,$mask80
  686. and $s1,$s0,$mask7f
  687. sub $t1,$t1,$t1,lsr#7
  688. and $t1,$t1,$mask1b
  689. eor $s1,$t1,$s1,lsl#1 @ tp2
  690. and $t1,$s1,$mask80
  691. and $s2,$s1,$mask7f
  692. sub $t1,$t1,$t1,lsr#7
  693. and $t1,$t1,$mask1b
  694. eor $s2,$t1,$s2,lsl#1 @ tp4
  695. and $t1,$s2,$mask80
  696. and $s3,$s2,$mask7f
  697. sub $t1,$t1,$t1,lsr#7
  698. and $t1,$t1,$mask1b
  699. eor $s3,$t1,$s3,lsl#1 @ tp8
  700. eor $t1,$s1,$s2
  701. eor $t2,$s0,$s3 @ tp9
  702. eor $t1,$t1,$s3 @ tpe
  703. eor $t1,$t1,$s1,ror#24
  704. eor $t1,$t1,$t2,ror#24 @ ^= ROTATE(tpb=tp9^tp2,8)
  705. eor $t1,$t1,$s2,ror#16
  706. eor $t1,$t1,$t2,ror#16 @ ^= ROTATE(tpd=tp9^tp4,16)
  707. eor $t1,$t1,$t2,ror#8 @ ^= ROTATE(tp9,24)
  708. ldr $s0,[$key,#4] @ prefetch tp1
  709. str $t1,[$key],#4
  710. subs $rounds,$rounds,#1
  711. bne .Lmix
  712. mov r0,#0
  713. #if __ARM_ARCH__>=5
  714. ldmia sp!,{r4-r12,pc}
  715. #else
  716. ldmia sp!,{r4-r12,lr}
  717. tst lr,#1
  718. moveq pc,lr @ be binary compatible with V4, yet
  719. bx lr @ interoperable with Thumb ISA:-)
  720. #endif
  721. .size AES_set_decrypt_key,.-AES_set_decrypt_key
  722. .type AES_Td,%object
  723. .align 5
  724. AES_Td:
  725. .word 0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96
  726. .word 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393
  727. .word 0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25
  728. .word 0x4fe5d7fc, 0xc52acbd7, 0x26354480, 0xb562a38f
  729. .word 0xdeb15a49, 0x25ba1b67, 0x45ea0e98, 0x5dfec0e1
  730. .word 0xc32f7502, 0x814cf012, 0x8d4697a3, 0x6bd3f9c6
  731. .word 0x038f5fe7, 0x15929c95, 0xbf6d7aeb, 0x955259da
  732. .word 0xd4be832d, 0x587421d3, 0x49e06929, 0x8ec9c844
  733. .word 0x75c2896a, 0xf48e7978, 0x99583e6b, 0x27b971dd
  734. .word 0xbee14fb6, 0xf088ad17, 0xc920ac66, 0x7dce3ab4
  735. .word 0x63df4a18, 0xe51a3182, 0x97513360, 0x62537f45
  736. .word 0xb16477e0, 0xbb6bae84, 0xfe81a01c, 0xf9082b94
  737. .word 0x70486858, 0x8f45fd19, 0x94de6c87, 0x527bf8b7
  738. .word 0xab73d323, 0x724b02e2, 0xe31f8f57, 0x6655ab2a
  739. .word 0xb2eb2807, 0x2fb5c203, 0x86c57b9a, 0xd33708a5
  740. .word 0x302887f2, 0x23bfa5b2, 0x02036aba, 0xed16825c
  741. .word 0x8acf1c2b, 0xa779b492, 0xf307f2f0, 0x4e69e2a1
  742. .word 0x65daf4cd, 0x0605bed5, 0xd134621f, 0xc4a6fe8a
  743. .word 0x342e539d, 0xa2f355a0, 0x058ae132, 0xa4f6eb75
  744. .word 0x0b83ec39, 0x4060efaa, 0x5e719f06, 0xbd6e1051
  745. .word 0x3e218af9, 0x96dd063d, 0xdd3e05ae, 0x4de6bd46
  746. .word 0x91548db5, 0x71c45d05, 0x0406d46f, 0x605015ff
  747. .word 0x1998fb24, 0xd6bde997, 0x894043cc, 0x67d99e77
  748. .word 0xb0e842bd, 0x07898b88, 0xe7195b38, 0x79c8eedb
  749. .word 0xa17c0a47, 0x7c420fe9, 0xf8841ec9, 0x00000000
  750. .word 0x09808683, 0x322bed48, 0x1e1170ac, 0x6c5a724e
  751. .word 0xfd0efffb, 0x0f853856, 0x3daed51e, 0x362d3927
  752. .word 0x0a0fd964, 0x685ca621, 0x9b5b54d1, 0x24362e3a
  753. .word 0x0c0a67b1, 0x9357e70f, 0xb4ee96d2, 0x1b9b919e
  754. .word 0x80c0c54f, 0x61dc20a2, 0x5a774b69, 0x1c121a16
  755. .word 0xe293ba0a, 0xc0a02ae5, 0x3c22e043, 0x121b171d
  756. .word 0x0e090d0b, 0xf28bc7ad, 0x2db6a8b9, 0x141ea9c8
  757. .word 0x57f11985, 0xaf75074c, 0xee99ddbb, 0xa37f60fd
  758. .word 0xf701269f, 0x5c72f5bc, 0x44663bc5, 0x5bfb7e34
  759. .word 0x8b432976, 0xcb23c6dc, 0xb6edfc68, 0xb8e4f163
  760. .word 0xd731dcca, 0x42638510, 0x13972240, 0x84c61120
  761. .word 0x854a247d, 0xd2bb3df8, 0xaef93211, 0xc729a16d
  762. .word 0x1d9e2f4b, 0xdcb230f3, 0x0d8652ec, 0x77c1e3d0
  763. .word 0x2bb3166c, 0xa970b999, 0x119448fa, 0x47e96422
  764. .word 0xa8fc8cc4, 0xa0f03f1a, 0x567d2cd8, 0x223390ef
  765. .word 0x87494ec7, 0xd938d1c1, 0x8ccaa2fe, 0x98d40b36
  766. .word 0xa6f581cf, 0xa57ade28, 0xdab78e26, 0x3fadbfa4
  767. .word 0x2c3a9de4, 0x5078920d, 0x6a5fcc9b, 0x547e4662
  768. .word 0xf68d13c2, 0x90d8b8e8, 0x2e39f75e, 0x82c3aff5
  769. .word 0x9f5d80be, 0x69d0937c, 0x6fd52da9, 0xcf2512b3
  770. .word 0xc8ac993b, 0x10187da7, 0xe89c636e, 0xdb3bbb7b
  771. .word 0xcd267809, 0x6e5918f4, 0xec9ab701, 0x834f9aa8
  772. .word 0xe6956e65, 0xaaffe67e, 0x21bccf08, 0xef15e8e6
  773. .word 0xbae79bd9, 0x4a6f36ce, 0xea9f09d4, 0x29b07cd6
  774. .word 0x31a4b2af, 0x2a3f2331, 0xc6a59430, 0x35a266c0
  775. .word 0x744ebc37, 0xfc82caa6, 0xe090d0b0, 0x33a7d815
  776. .word 0xf104984a, 0x41ecdaf7, 0x7fcd500e, 0x1791f62f
  777. .word 0x764dd68d, 0x43efb04d, 0xccaa4d54, 0xe49604df
  778. .word 0x9ed1b5e3, 0x4c6a881b, 0xc12c1fb8, 0x4665517f
  779. .word 0x9d5eea04, 0x018c355d, 0xfa877473, 0xfb0b412e
  780. .word 0xb3671d5a, 0x92dbd252, 0xe9105633, 0x6dd64713
  781. .word 0x9ad7618c, 0x37a10c7a, 0x59f8148e, 0xeb133c89
  782. .word 0xcea927ee, 0xb761c935, 0xe11ce5ed, 0x7a47b13c
  783. .word 0x9cd2df59, 0x55f2733f, 0x1814ce79, 0x73c737bf
  784. .word 0x53f7cdea, 0x5ffdaa5b, 0xdf3d6f14, 0x7844db86
  785. .word 0xcaaff381, 0xb968c43e, 0x3824342c, 0xc2a3405f
  786. .word 0x161dc372, 0xbce2250c, 0x283c498b, 0xff0d9541
  787. .word 0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190
  788. .word 0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742
  789. @ Td4[256]
  790. .byte 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38
  791. .byte 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb
  792. .byte 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87
  793. .byte 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb
  794. .byte 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d
  795. .byte 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e
  796. .byte 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2
  797. .byte 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25
  798. .byte 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16
  799. .byte 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92
  800. .byte 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda
  801. .byte 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84
  802. .byte 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a
  803. .byte 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06
  804. .byte 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02
  805. .byte 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b
  806. .byte 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea
  807. .byte 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73
  808. .byte 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85
  809. .byte 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e
  810. .byte 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89
  811. .byte 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b
  812. .byte 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20
  813. .byte 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4
  814. .byte 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31
  815. .byte 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f
  816. .byte 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d
  817. .byte 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef
  818. .byte 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0
  819. .byte 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61
  820. .byte 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26
  821. .byte 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
  822. .size AES_Td,.-AES_Td
  823. @ void AES_decrypt(const unsigned char *in, unsigned char *out,
  824. @ const AES_KEY *key) {
  825. .global AES_decrypt
  826. .type AES_decrypt,%function
  827. .align 5
  828. AES_decrypt:
  829. sub r3,pc,#8 @ AES_decrypt
  830. stmdb sp!,{r1,r4-r12,lr}
  831. mov $rounds,r0 @ inp
  832. mov $key,r2
  833. sub $tbl,r3,#AES_decrypt-AES_Td @ Td
  834. #if __ARM_ARCH__<7
  835. ldrb $s0,[$rounds,#3] @ load input data in endian-neutral
  836. ldrb $t1,[$rounds,#2] @ manner...
  837. ldrb $t2,[$rounds,#1]
  838. ldrb $t3,[$rounds,#0]
  839. orr $s0,$s0,$t1,lsl#8
  840. ldrb $s1,[$rounds,#7]
  841. orr $s0,$s0,$t2,lsl#16
  842. ldrb $t1,[$rounds,#6]
  843. orr $s0,$s0,$t3,lsl#24
  844. ldrb $t2,[$rounds,#5]
  845. ldrb $t3,[$rounds,#4]
  846. orr $s1,$s1,$t1,lsl#8
  847. ldrb $s2,[$rounds,#11]
  848. orr $s1,$s1,$t2,lsl#16
  849. ldrb $t1,[$rounds,#10]
  850. orr $s1,$s1,$t3,lsl#24
  851. ldrb $t2,[$rounds,#9]
  852. ldrb $t3,[$rounds,#8]
  853. orr $s2,$s2,$t1,lsl#8
  854. ldrb $s3,[$rounds,#15]
  855. orr $s2,$s2,$t2,lsl#16
  856. ldrb $t1,[$rounds,#14]
  857. orr $s2,$s2,$t3,lsl#24
  858. ldrb $t2,[$rounds,#13]
  859. ldrb $t3,[$rounds,#12]
  860. orr $s3,$s3,$t1,lsl#8
  861. orr $s3,$s3,$t2,lsl#16
  862. orr $s3,$s3,$t3,lsl#24
  863. #else
  864. ldr $s0,[$rounds,#0]
  865. ldr $s1,[$rounds,#4]
  866. ldr $s2,[$rounds,#8]
  867. ldr $s3,[$rounds,#12]
  868. #ifdef __ARMEL__
  869. rev $s0,$s0
  870. rev $s1,$s1
  871. rev $s2,$s2
  872. rev $s3,$s3
  873. #endif
  874. #endif
  875. bl _armv4_AES_decrypt
  876. ldr $rounds,[sp],#4 @ pop out
  877. #if __ARM_ARCH__>=7
  878. #ifdef __ARMEL__
  879. rev $s0,$s0
  880. rev $s1,$s1
  881. rev $s2,$s2
  882. rev $s3,$s3
  883. #endif
  884. str $s0,[$rounds,#0]
  885. str $s1,[$rounds,#4]
  886. str $s2,[$rounds,#8]
  887. str $s3,[$rounds,#12]
  888. #else
  889. mov $t1,$s0,lsr#24 @ write output in endian-neutral
  890. mov $t2,$s0,lsr#16 @ manner...
  891. mov $t3,$s0,lsr#8
  892. strb $t1,[$rounds,#0]
  893. strb $t2,[$rounds,#1]
  894. mov $t1,$s1,lsr#24
  895. strb $t3,[$rounds,#2]
  896. mov $t2,$s1,lsr#16
  897. strb $s0,[$rounds,#3]
  898. mov $t3,$s1,lsr#8
  899. strb $t1,[$rounds,#4]
  900. strb $t2,[$rounds,#5]
  901. mov $t1,$s2,lsr#24
  902. strb $t3,[$rounds,#6]
  903. mov $t2,$s2,lsr#16
  904. strb $s1,[$rounds,#7]
  905. mov $t3,$s2,lsr#8
  906. strb $t1,[$rounds,#8]
  907. strb $t2,[$rounds,#9]
  908. mov $t1,$s3,lsr#24
  909. strb $t3,[$rounds,#10]
  910. mov $t2,$s3,lsr#16
  911. strb $s2,[$rounds,#11]
  912. mov $t3,$s3,lsr#8
  913. strb $t1,[$rounds,#12]
  914. strb $t2,[$rounds,#13]
  915. strb $t3,[$rounds,#14]
  916. strb $s3,[$rounds,#15]
  917. #endif
  918. #if __ARM_ARCH__>=5
  919. ldmia sp!,{r4-r12,pc}
  920. #else
  921. ldmia sp!,{r4-r12,lr}
  922. tst lr,#1
  923. moveq pc,lr @ be binary compatible with V4, yet
  924. bx lr @ interoperable with Thumb ISA:-)
  925. #endif
  926. .size AES_decrypt,.-AES_decrypt
  927. .type _armv4_AES_decrypt,%function
  928. .align 2
  929. _armv4_AES_decrypt:
  930. str lr,[sp,#-4]! @ push lr
  931. ldmia $key!,{$t1-$i1}
  932. eor $s0,$s0,$t1
  933. ldr $rounds,[$key,#240-16]
  934. eor $s1,$s1,$t2
  935. eor $s2,$s2,$t3
  936. eor $s3,$s3,$i1
  937. sub $rounds,$rounds,#1
  938. mov lr,#255
  939. and $i1,lr,$s0,lsr#16
  940. and $i2,lr,$s0,lsr#8
  941. and $i3,lr,$s0
  942. mov $s0,$s0,lsr#24
  943. .Ldec_loop:
  944. ldr $t1,[$tbl,$i1,lsl#2] @ Td1[s0>>16]
  945. and $i1,lr,$s1 @ i0
  946. ldr $t2,[$tbl,$i2,lsl#2] @ Td2[s0>>8]
  947. and $i2,lr,$s1,lsr#16
  948. ldr $t3,[$tbl,$i3,lsl#2] @ Td3[s0>>0]
  949. and $i3,lr,$s1,lsr#8
  950. ldr $s0,[$tbl,$s0,lsl#2] @ Td0[s0>>24]
  951. mov $s1,$s1,lsr#24
  952. ldr $i1,[$tbl,$i1,lsl#2] @ Td3[s1>>0]
  953. ldr $i2,[$tbl,$i2,lsl#2] @ Td1[s1>>16]
  954. ldr $i3,[$tbl,$i3,lsl#2] @ Td2[s1>>8]
  955. eor $s0,$s0,$i1,ror#24
  956. ldr $s1,[$tbl,$s1,lsl#2] @ Td0[s1>>24]
  957. and $i1,lr,$s2,lsr#8 @ i0
  958. eor $t2,$i2,$t2,ror#8
  959. and $i2,lr,$s2 @ i1
  960. eor $t3,$i3,$t3,ror#8
  961. and $i3,lr,$s2,lsr#16
  962. ldr $i1,[$tbl,$i1,lsl#2] @ Td2[s2>>8]
  963. eor $s1,$s1,$t1,ror#8
  964. ldr $i2,[$tbl,$i2,lsl#2] @ Td3[s2>>0]
  965. mov $s2,$s2,lsr#24
  966. ldr $i3,[$tbl,$i3,lsl#2] @ Td1[s2>>16]
  967. eor $s0,$s0,$i1,ror#16
  968. ldr $s2,[$tbl,$s2,lsl#2] @ Td0[s2>>24]
  969. and $i1,lr,$s3,lsr#16 @ i0
  970. eor $s1,$s1,$i2,ror#24
  971. and $i2,lr,$s3,lsr#8 @ i1
  972. eor $t3,$i3,$t3,ror#8
  973. and $i3,lr,$s3 @ i2
  974. ldr $i1,[$tbl,$i1,lsl#2] @ Td1[s3>>16]
  975. eor $s2,$s2,$t2,ror#8
  976. ldr $i2,[$tbl,$i2,lsl#2] @ Td2[s3>>8]
  977. mov $s3,$s3,lsr#24
  978. ldr $i3,[$tbl,$i3,lsl#2] @ Td3[s3>>0]
  979. eor $s0,$s0,$i1,ror#8
  980. ldr $i1,[$key],#16
  981. eor $s1,$s1,$i2,ror#16
  982. ldr $s3,[$tbl,$s3,lsl#2] @ Td0[s3>>24]
  983. eor $s2,$s2,$i3,ror#24
  984. ldr $t1,[$key,#-12]
  985. eor $s0,$s0,$i1
  986. ldr $t2,[$key,#-8]
  987. eor $s3,$s3,$t3,ror#8
  988. ldr $t3,[$key,#-4]
  989. and $i1,lr,$s0,lsr#16
  990. eor $s1,$s1,$t1
  991. and $i2,lr,$s0,lsr#8
  992. eor $s2,$s2,$t2
  993. and $i3,lr,$s0
  994. eor $s3,$s3,$t3
  995. mov $s0,$s0,lsr#24
  996. subs $rounds,$rounds,#1
  997. bne .Ldec_loop
  998. add $tbl,$tbl,#1024
  999. ldr $t2,[$tbl,#0] @ prefetch Td4
  1000. ldr $t3,[$tbl,#32]
  1001. ldr $t1,[$tbl,#64]
  1002. ldr $t2,[$tbl,#96]
  1003. ldr $t3,[$tbl,#128]
  1004. ldr $t1,[$tbl,#160]
  1005. ldr $t2,[$tbl,#192]
  1006. ldr $t3,[$tbl,#224]
  1007. ldrb $s0,[$tbl,$s0] @ Td4[s0>>24]
  1008. ldrb $t1,[$tbl,$i1] @ Td4[s0>>16]
  1009. and $i1,lr,$s1 @ i0
  1010. ldrb $t2,[$tbl,$i2] @ Td4[s0>>8]
  1011. and $i2,lr,$s1,lsr#16
  1012. ldrb $t3,[$tbl,$i3] @ Td4[s0>>0]
  1013. and $i3,lr,$s1,lsr#8
  1014. ldrb $i1,[$tbl,$i1] @ Td4[s1>>0]
  1015. ldrb $s1,[$tbl,$s1,lsr#24] @ Td4[s1>>24]
  1016. ldrb $i2,[$tbl,$i2] @ Td4[s1>>16]
  1017. eor $s0,$i1,$s0,lsl#24
  1018. ldrb $i3,[$tbl,$i3] @ Td4[s1>>8]
  1019. eor $s1,$t1,$s1,lsl#8
  1020. and $i1,lr,$s2,lsr#8 @ i0
  1021. eor $t2,$t2,$i2,lsl#8
  1022. and $i2,lr,$s2 @ i1
  1023. ldrb $i1,[$tbl,$i1] @ Td4[s2>>8]
  1024. eor $t3,$t3,$i3,lsl#8
  1025. ldrb $i2,[$tbl,$i2] @ Td4[s2>>0]
  1026. and $i3,lr,$s2,lsr#16
  1027. ldrb $s2,[$tbl,$s2,lsr#24] @ Td4[s2>>24]
  1028. eor $s0,$s0,$i1,lsl#8
  1029. ldrb $i3,[$tbl,$i3] @ Td4[s2>>16]
  1030. eor $s1,$i2,$s1,lsl#16
  1031. and $i1,lr,$s3,lsr#16 @ i0
  1032. eor $s2,$t2,$s2,lsl#16
  1033. and $i2,lr,$s3,lsr#8 @ i1
  1034. ldrb $i1,[$tbl,$i1] @ Td4[s3>>16]
  1035. eor $t3,$t3,$i3,lsl#16
  1036. ldrb $i2,[$tbl,$i2] @ Td4[s3>>8]
  1037. and $i3,lr,$s3 @ i2
  1038. ldrb $i3,[$tbl,$i3] @ Td4[s3>>0]
  1039. ldrb $s3,[$tbl,$s3,lsr#24] @ Td4[s3>>24]
  1040. eor $s0,$s0,$i1,lsl#16
  1041. ldr $i1,[$key,#0]
  1042. eor $s1,$s1,$i2,lsl#8
  1043. ldr $t1,[$key,#4]
  1044. eor $s2,$i3,$s2,lsl#8
  1045. ldr $t2,[$key,#8]
  1046. eor $s3,$t3,$s3,lsl#24
  1047. ldr $t3,[$key,#12]
  1048. eor $s0,$s0,$i1
  1049. eor $s1,$s1,$t1
  1050. eor $s2,$s2,$t2
  1051. eor $s3,$s3,$t3
  1052. sub $tbl,$tbl,#1024
  1053. ldr pc,[sp],#4 @ pop and return
  1054. .size _armv4_AES_decrypt,.-_armv4_AES_decrypt
  1055. .asciz "AES for ARMv4, CRYPTOGAMS by <appro\@openssl.org>"
  1056. .align 2
  1057. ___
  1058. $code =~ s/\bbx\s+lr\b/.word\t0xe12fff1e/gm; # make it possible to compile with -march=armv4
  1059. print $code;
  1060. close STDOUT; # enforce flush