Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

2478 linhas
62 KiB

  1. #!/usr/bin/env perl
  2. # ====================================================================
  3. # Written by Andy Polyakov <appro@openssl.org> 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. # Specific modes and adaptation for Linux kernel by Ard Biesheuvel
  9. # <ard.biesheuvel@linaro.org>. Permission to use under GPL terms is
  10. # granted.
  11. # ====================================================================
  12. # Bit-sliced AES for ARM NEON
  13. #
  14. # February 2012.
  15. #
  16. # This implementation is direct adaptation of bsaes-x86_64 module for
  17. # ARM NEON. Except that this module is endian-neutral [in sense that
  18. # it can be compiled for either endianness] by courtesy of vld1.8's
  19. # neutrality. Initial version doesn't implement interface to OpenSSL,
  20. # only low-level primitives and unsupported entry points, just enough
  21. # to collect performance results, which for Cortex-A8 core are:
  22. #
  23. # encrypt 19.5 cycles per byte processed with 128-bit key
  24. # decrypt 22.1 cycles per byte processed with 128-bit key
  25. # key conv. 440 cycles per 128-bit key/0.18 of 8x block
  26. #
  27. # Snapdragon S4 encrypts byte in 17.6 cycles and decrypts in 19.7,
  28. # which is [much] worse than anticipated (for further details see
  29. # http://www.openssl.org/~appro/Snapdragon-S4.html).
  30. #
  31. # Cortex-A15 manages in 14.2/16.1 cycles [when integer-only code
  32. # manages in 20.0 cycles].
  33. #
  34. # When comparing to x86_64 results keep in mind that NEON unit is
  35. # [mostly] single-issue and thus can't [fully] benefit from
  36. # instruction-level parallelism. And when comparing to aes-armv4
  37. # results keep in mind key schedule conversion overhead (see
  38. # bsaes-x86_64.pl for further details)...
  39. #
  40. # <appro@openssl.org>
  41. # April-August 2013
  42. #
  43. # Add CBC, CTR and XTS subroutines, adapt for kernel use.
  44. #
  45. # <ard.biesheuvel@linaro.org>
  46. while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {}
  47. open STDOUT,">$output";
  48. my ($inp,$out,$len,$key)=("r0","r1","r2","r3");
  49. my @XMM=map("q$_",(0..15));
  50. {
  51. my ($key,$rounds,$const)=("r4","r5","r6");
  52. sub Dlo() { shift=~m|q([1]?[0-9])|?"d".($1*2):""; }
  53. sub Dhi() { shift=~m|q([1]?[0-9])|?"d".($1*2+1):""; }
  54. sub Sbox {
  55. # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
  56. # output in lsb > [b0, b1, b4, b6, b3, b7, b2, b5] < msb
  57. my @b=@_[0..7];
  58. my @t=@_[8..11];
  59. my @s=@_[12..15];
  60. &InBasisChange (@b);
  61. &Inv_GF256 (@b[6,5,0,3,7,1,4,2],@t,@s);
  62. &OutBasisChange (@b[7,1,4,2,6,5,0,3]);
  63. }
  64. sub InBasisChange {
  65. # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
  66. # output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb
  67. my @b=@_[0..7];
  68. $code.=<<___;
  69. veor @b[2], @b[2], @b[1]
  70. veor @b[5], @b[5], @b[6]
  71. veor @b[3], @b[3], @b[0]
  72. veor @b[6], @b[6], @b[2]
  73. veor @b[5], @b[5], @b[0]
  74. veor @b[6], @b[6], @b[3]
  75. veor @b[3], @b[3], @b[7]
  76. veor @b[7], @b[7], @b[5]
  77. veor @b[3], @b[3], @b[4]
  78. veor @b[4], @b[4], @b[5]
  79. veor @b[2], @b[2], @b[7]
  80. veor @b[3], @b[3], @b[1]
  81. veor @b[1], @b[1], @b[5]
  82. ___
  83. }
  84. sub OutBasisChange {
  85. # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
  86. # output in lsb > [b6, b1, b2, b4, b7, b0, b3, b5] < msb
  87. my @b=@_[0..7];
  88. $code.=<<___;
  89. veor @b[0], @b[0], @b[6]
  90. veor @b[1], @b[1], @b[4]
  91. veor @b[4], @b[4], @b[6]
  92. veor @b[2], @b[2], @b[0]
  93. veor @b[6], @b[6], @b[1]
  94. veor @b[1], @b[1], @b[5]
  95. veor @b[5], @b[5], @b[3]
  96. veor @b[3], @b[3], @b[7]
  97. veor @b[7], @b[7], @b[5]
  98. veor @b[2], @b[2], @b[5]
  99. veor @b[4], @b[4], @b[7]
  100. ___
  101. }
  102. sub InvSbox {
  103. # input in lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
  104. # output in lsb > [b0, b1, b6, b4, b2, b7, b3, b5] < msb
  105. my @b=@_[0..7];
  106. my @t=@_[8..11];
  107. my @s=@_[12..15];
  108. &InvInBasisChange (@b);
  109. &Inv_GF256 (@b[5,1,2,6,3,7,0,4],@t,@s);
  110. &InvOutBasisChange (@b[3,7,0,4,5,1,2,6]);
  111. }
  112. sub InvInBasisChange { # OutBasisChange in reverse (with twist)
  113. my @b=@_[5,1,2,6,3,7,0,4];
  114. $code.=<<___
  115. veor @b[1], @b[1], @b[7]
  116. veor @b[4], @b[4], @b[7]
  117. veor @b[7], @b[7], @b[5]
  118. veor @b[1], @b[1], @b[3]
  119. veor @b[2], @b[2], @b[5]
  120. veor @b[3], @b[3], @b[7]
  121. veor @b[6], @b[6], @b[1]
  122. veor @b[2], @b[2], @b[0]
  123. veor @b[5], @b[5], @b[3]
  124. veor @b[4], @b[4], @b[6]
  125. veor @b[0], @b[0], @b[6]
  126. veor @b[1], @b[1], @b[4]
  127. ___
  128. }
  129. sub InvOutBasisChange { # InBasisChange in reverse
  130. my @b=@_[2,5,7,3,6,1,0,4];
  131. $code.=<<___;
  132. veor @b[1], @b[1], @b[5]
  133. veor @b[2], @b[2], @b[7]
  134. veor @b[3], @b[3], @b[1]
  135. veor @b[4], @b[4], @b[5]
  136. veor @b[7], @b[7], @b[5]
  137. veor @b[3], @b[3], @b[4]
  138. veor @b[5], @b[5], @b[0]
  139. veor @b[3], @b[3], @b[7]
  140. veor @b[6], @b[6], @b[2]
  141. veor @b[2], @b[2], @b[1]
  142. veor @b[6], @b[6], @b[3]
  143. veor @b[3], @b[3], @b[0]
  144. veor @b[5], @b[5], @b[6]
  145. ___
  146. }
  147. sub Mul_GF4 {
  148. #;*************************************************************
  149. #;* Mul_GF4: Input x0-x1,y0-y1 Output x0-x1 Temp t0 (8) *
  150. #;*************************************************************
  151. my ($x0,$x1,$y0,$y1,$t0,$t1)=@_;
  152. $code.=<<___;
  153. veor $t0, $y0, $y1
  154. vand $t0, $t0, $x0
  155. veor $x0, $x0, $x1
  156. vand $t1, $x1, $y0
  157. vand $x0, $x0, $y1
  158. veor $x1, $t1, $t0
  159. veor $x0, $x0, $t1
  160. ___
  161. }
  162. sub Mul_GF4_N { # not used, see next subroutine
  163. # multiply and scale by N
  164. my ($x0,$x1,$y0,$y1,$t0)=@_;
  165. $code.=<<___;
  166. veor $t0, $y0, $y1
  167. vand $t0, $t0, $x0
  168. veor $x0, $x0, $x1
  169. vand $x1, $x1, $y0
  170. vand $x0, $x0, $y1
  171. veor $x1, $x1, $x0
  172. veor $x0, $x0, $t0
  173. ___
  174. }
  175. sub Mul_GF4_N_GF4 {
  176. # interleaved Mul_GF4_N and Mul_GF4
  177. my ($x0,$x1,$y0,$y1,$t0,
  178. $x2,$x3,$y2,$y3,$t1)=@_;
  179. $code.=<<___;
  180. veor $t0, $y0, $y1
  181. veor $t1, $y2, $y3
  182. vand $t0, $t0, $x0
  183. vand $t1, $t1, $x2
  184. veor $x0, $x0, $x1
  185. veor $x2, $x2, $x3
  186. vand $x1, $x1, $y0
  187. vand $x3, $x3, $y2
  188. vand $x0, $x0, $y1
  189. vand $x2, $x2, $y3
  190. veor $x1, $x1, $x0
  191. veor $x2, $x2, $x3
  192. veor $x0, $x0, $t0
  193. veor $x3, $x3, $t1
  194. ___
  195. }
  196. sub Mul_GF16_2 {
  197. my @x=@_[0..7];
  198. my @y=@_[8..11];
  199. my @t=@_[12..15];
  200. $code.=<<___;
  201. veor @t[0], @x[0], @x[2]
  202. veor @t[1], @x[1], @x[3]
  203. ___
  204. &Mul_GF4 (@x[0], @x[1], @y[0], @y[1], @t[2..3]);
  205. $code.=<<___;
  206. veor @y[0], @y[0], @y[2]
  207. veor @y[1], @y[1], @y[3]
  208. ___
  209. Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
  210. @x[2], @x[3], @y[2], @y[3], @t[2]);
  211. $code.=<<___;
  212. veor @x[0], @x[0], @t[0]
  213. veor @x[2], @x[2], @t[0]
  214. veor @x[1], @x[1], @t[1]
  215. veor @x[3], @x[3], @t[1]
  216. veor @t[0], @x[4], @x[6]
  217. veor @t[1], @x[5], @x[7]
  218. ___
  219. &Mul_GF4_N_GF4 (@t[0], @t[1], @y[0], @y[1], @t[3],
  220. @x[6], @x[7], @y[2], @y[3], @t[2]);
  221. $code.=<<___;
  222. veor @y[0], @y[0], @y[2]
  223. veor @y[1], @y[1], @y[3]
  224. ___
  225. &Mul_GF4 (@x[4], @x[5], @y[0], @y[1], @t[2..3]);
  226. $code.=<<___;
  227. veor @x[4], @x[4], @t[0]
  228. veor @x[6], @x[6], @t[0]
  229. veor @x[5], @x[5], @t[1]
  230. veor @x[7], @x[7], @t[1]
  231. ___
  232. }
  233. sub Inv_GF256 {
  234. #;********************************************************************
  235. #;* Inv_GF256: Input x0-x7 Output x0-x7 Temp t0-t3,s0-s3 (144) *
  236. #;********************************************************************
  237. my @x=@_[0..7];
  238. my @t=@_[8..11];
  239. my @s=@_[12..15];
  240. # direct optimizations from hardware
  241. $code.=<<___;
  242. veor @t[3], @x[4], @x[6]
  243. veor @t[2], @x[5], @x[7]
  244. veor @t[1], @x[1], @x[3]
  245. veor @s[1], @x[7], @x[6]
  246. vmov @t[0], @t[2]
  247. veor @s[0], @x[0], @x[2]
  248. vorr @t[2], @t[2], @t[1]
  249. veor @s[3], @t[3], @t[0]
  250. vand @s[2], @t[3], @s[0]
  251. vorr @t[3], @t[3], @s[0]
  252. veor @s[0], @s[0], @t[1]
  253. vand @t[0], @t[0], @t[1]
  254. veor @t[1], @x[3], @x[2]
  255. vand @s[3], @s[3], @s[0]
  256. vand @s[1], @s[1], @t[1]
  257. veor @t[1], @x[4], @x[5]
  258. veor @s[0], @x[1], @x[0]
  259. veor @t[3], @t[3], @s[1]
  260. veor @t[2], @t[2], @s[1]
  261. vand @s[1], @t[1], @s[0]
  262. vorr @t[1], @t[1], @s[0]
  263. veor @t[3], @t[3], @s[3]
  264. veor @t[0], @t[0], @s[1]
  265. veor @t[2], @t[2], @s[2]
  266. veor @t[1], @t[1], @s[3]
  267. veor @t[0], @t[0], @s[2]
  268. vand @s[0], @x[7], @x[3]
  269. veor @t[1], @t[1], @s[2]
  270. vand @s[1], @x[6], @x[2]
  271. vand @s[2], @x[5], @x[1]
  272. vorr @s[3], @x[4], @x[0]
  273. veor @t[3], @t[3], @s[0]
  274. veor @t[1], @t[1], @s[2]
  275. veor @t[0], @t[0], @s[3]
  276. veor @t[2], @t[2], @s[1]
  277. @ Inv_GF16 \t0, \t1, \t2, \t3, \s0, \s1, \s2, \s3
  278. @ new smaller inversion
  279. vand @s[2], @t[3], @t[1]
  280. vmov @s[0], @t[0]
  281. veor @s[1], @t[2], @s[2]
  282. veor @s[3], @t[0], @s[2]
  283. veor @s[2], @t[0], @s[2] @ @s[2]=@s[3]
  284. vbsl @s[1], @t[1], @t[0]
  285. vbsl @s[3], @t[3], @t[2]
  286. veor @t[3], @t[3], @t[2]
  287. vbsl @s[0], @s[1], @s[2]
  288. vbsl @t[0], @s[2], @s[1]
  289. vand @s[2], @s[0], @s[3]
  290. veor @t[1], @t[1], @t[0]
  291. veor @s[2], @s[2], @t[3]
  292. ___
  293. # output in s3, s2, s1, t1
  294. # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \t2, \t3, \t0, \t1, \s0, \s1, \s2, \s3
  295. # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \s3, \s2, \s1, \t1, \s0, \t0, \t2, \t3
  296. &Mul_GF16_2(@x,@s[3,2,1],@t[1],@s[0],@t[0,2,3]);
  297. ### output msb > [x3,x2,x1,x0,x7,x6,x5,x4] < lsb
  298. }
  299. # AES linear components
  300. sub ShiftRows {
  301. my @x=@_[0..7];
  302. my @t=@_[8..11];
  303. my $mask=pop;
  304. $code.=<<___;
  305. vldmia $key!, {@t[0]-@t[3]}
  306. veor @t[0], @t[0], @x[0]
  307. veor @t[1], @t[1], @x[1]
  308. vtbl.8 `&Dlo(@x[0])`, {@t[0]}, `&Dlo($mask)`
  309. vtbl.8 `&Dhi(@x[0])`, {@t[0]}, `&Dhi($mask)`
  310. vldmia $key!, {@t[0]}
  311. veor @t[2], @t[2], @x[2]
  312. vtbl.8 `&Dlo(@x[1])`, {@t[1]}, `&Dlo($mask)`
  313. vtbl.8 `&Dhi(@x[1])`, {@t[1]}, `&Dhi($mask)`
  314. vldmia $key!, {@t[1]}
  315. veor @t[3], @t[3], @x[3]
  316. vtbl.8 `&Dlo(@x[2])`, {@t[2]}, `&Dlo($mask)`
  317. vtbl.8 `&Dhi(@x[2])`, {@t[2]}, `&Dhi($mask)`
  318. vldmia $key!, {@t[2]}
  319. vtbl.8 `&Dlo(@x[3])`, {@t[3]}, `&Dlo($mask)`
  320. vtbl.8 `&Dhi(@x[3])`, {@t[3]}, `&Dhi($mask)`
  321. vldmia $key!, {@t[3]}
  322. veor @t[0], @t[0], @x[4]
  323. veor @t[1], @t[1], @x[5]
  324. vtbl.8 `&Dlo(@x[4])`, {@t[0]}, `&Dlo($mask)`
  325. vtbl.8 `&Dhi(@x[4])`, {@t[0]}, `&Dhi($mask)`
  326. veor @t[2], @t[2], @x[6]
  327. vtbl.8 `&Dlo(@x[5])`, {@t[1]}, `&Dlo($mask)`
  328. vtbl.8 `&Dhi(@x[5])`, {@t[1]}, `&Dhi($mask)`
  329. veor @t[3], @t[3], @x[7]
  330. vtbl.8 `&Dlo(@x[6])`, {@t[2]}, `&Dlo($mask)`
  331. vtbl.8 `&Dhi(@x[6])`, {@t[2]}, `&Dhi($mask)`
  332. vtbl.8 `&Dlo(@x[7])`, {@t[3]}, `&Dlo($mask)`
  333. vtbl.8 `&Dhi(@x[7])`, {@t[3]}, `&Dhi($mask)`
  334. ___
  335. }
  336. sub MixColumns {
  337. # modified to emit output in order suitable for feeding back to aesenc[last]
  338. my @x=@_[0..7];
  339. my @t=@_[8..15];
  340. my $inv=@_[16]; # optional
  341. $code.=<<___;
  342. vext.8 @t[0], @x[0], @x[0], #12 @ x0 <<< 32
  343. vext.8 @t[1], @x[1], @x[1], #12
  344. veor @x[0], @x[0], @t[0] @ x0 ^ (x0 <<< 32)
  345. vext.8 @t[2], @x[2], @x[2], #12
  346. veor @x[1], @x[1], @t[1]
  347. vext.8 @t[3], @x[3], @x[3], #12
  348. veor @x[2], @x[2], @t[2]
  349. vext.8 @t[4], @x[4], @x[4], #12
  350. veor @x[3], @x[3], @t[3]
  351. vext.8 @t[5], @x[5], @x[5], #12
  352. veor @x[4], @x[4], @t[4]
  353. vext.8 @t[6], @x[6], @x[6], #12
  354. veor @x[5], @x[5], @t[5]
  355. vext.8 @t[7], @x[7], @x[7], #12
  356. veor @x[6], @x[6], @t[6]
  357. veor @t[1], @t[1], @x[0]
  358. veor @x[7], @x[7], @t[7]
  359. vext.8 @x[0], @x[0], @x[0], #8 @ (x0 ^ (x0 <<< 32)) <<< 64)
  360. veor @t[2], @t[2], @x[1]
  361. veor @t[0], @t[0], @x[7]
  362. veor @t[1], @t[1], @x[7]
  363. vext.8 @x[1], @x[1], @x[1], #8
  364. veor @t[5], @t[5], @x[4]
  365. veor @x[0], @x[0], @t[0]
  366. veor @t[6], @t[6], @x[5]
  367. veor @x[1], @x[1], @t[1]
  368. vext.8 @t[0], @x[4], @x[4], #8
  369. veor @t[4], @t[4], @x[3]
  370. vext.8 @t[1], @x[5], @x[5], #8
  371. veor @t[7], @t[7], @x[6]
  372. vext.8 @x[4], @x[3], @x[3], #8
  373. veor @t[3], @t[3], @x[2]
  374. vext.8 @x[5], @x[7], @x[7], #8
  375. veor @t[4], @t[4], @x[7]
  376. vext.8 @x[3], @x[6], @x[6], #8
  377. veor @t[3], @t[3], @x[7]
  378. vext.8 @x[6], @x[2], @x[2], #8
  379. veor @x[7], @t[1], @t[5]
  380. ___
  381. $code.=<<___ if (!$inv);
  382. veor @x[2], @t[0], @t[4]
  383. veor @x[4], @x[4], @t[3]
  384. veor @x[5], @x[5], @t[7]
  385. veor @x[3], @x[3], @t[6]
  386. @ vmov @x[2], @t[0]
  387. veor @x[6], @x[6], @t[2]
  388. @ vmov @x[7], @t[1]
  389. ___
  390. $code.=<<___ if ($inv);
  391. veor @t[3], @t[3], @x[4]
  392. veor @x[5], @x[5], @t[7]
  393. veor @x[2], @x[3], @t[6]
  394. veor @x[3], @t[0], @t[4]
  395. veor @x[4], @x[6], @t[2]
  396. vmov @x[6], @t[3]
  397. @ vmov @x[7], @t[1]
  398. ___
  399. }
  400. sub InvMixColumns_orig {
  401. my @x=@_[0..7];
  402. my @t=@_[8..15];
  403. $code.=<<___;
  404. @ multiplication by 0x0e
  405. vext.8 @t[7], @x[7], @x[7], #12
  406. vmov @t[2], @x[2]
  407. veor @x[2], @x[2], @x[5] @ 2 5
  408. veor @x[7], @x[7], @x[5] @ 7 5
  409. vext.8 @t[0], @x[0], @x[0], #12
  410. vmov @t[5], @x[5]
  411. veor @x[5], @x[5], @x[0] @ 5 0 [1]
  412. veor @x[0], @x[0], @x[1] @ 0 1
  413. vext.8 @t[1], @x[1], @x[1], #12
  414. veor @x[1], @x[1], @x[2] @ 1 25
  415. veor @x[0], @x[0], @x[6] @ 01 6 [2]
  416. vext.8 @t[3], @x[3], @x[3], #12
  417. veor @x[1], @x[1], @x[3] @ 125 3 [4]
  418. veor @x[2], @x[2], @x[0] @ 25 016 [3]
  419. veor @x[3], @x[3], @x[7] @ 3 75
  420. veor @x[7], @x[7], @x[6] @ 75 6 [0]
  421. vext.8 @t[6], @x[6], @x[6], #12
  422. vmov @t[4], @x[4]
  423. veor @x[6], @x[6], @x[4] @ 6 4
  424. veor @x[4], @x[4], @x[3] @ 4 375 [6]
  425. veor @x[3], @x[3], @x[7] @ 375 756=36
  426. veor @x[6], @x[6], @t[5] @ 64 5 [7]
  427. veor @x[3], @x[3], @t[2] @ 36 2
  428. vext.8 @t[5], @t[5], @t[5], #12
  429. veor @x[3], @x[3], @t[4] @ 362 4 [5]
  430. ___
  431. my @y = @x[7,5,0,2,1,3,4,6];
  432. $code.=<<___;
  433. @ multiplication by 0x0b
  434. veor @y[1], @y[1], @y[0]
  435. veor @y[0], @y[0], @t[0]
  436. vext.8 @t[2], @t[2], @t[2], #12
  437. veor @y[1], @y[1], @t[1]
  438. veor @y[0], @y[0], @t[5]
  439. vext.8 @t[4], @t[4], @t[4], #12
  440. veor @y[1], @y[1], @t[6]
  441. veor @y[0], @y[0], @t[7]
  442. veor @t[7], @t[7], @t[6] @ clobber t[7]
  443. veor @y[3], @y[3], @t[0]
  444. veor @y[1], @y[1], @y[0]
  445. vext.8 @t[0], @t[0], @t[0], #12
  446. veor @y[2], @y[2], @t[1]
  447. veor @y[4], @y[4], @t[1]
  448. vext.8 @t[1], @t[1], @t[1], #12
  449. veor @y[2], @y[2], @t[2]
  450. veor @y[3], @y[3], @t[2]
  451. veor @y[5], @y[5], @t[2]
  452. veor @y[2], @y[2], @t[7]
  453. vext.8 @t[2], @t[2], @t[2], #12
  454. veor @y[3], @y[3], @t[3]
  455. veor @y[6], @y[6], @t[3]
  456. veor @y[4], @y[4], @t[3]
  457. veor @y[7], @y[7], @t[4]
  458. vext.8 @t[3], @t[3], @t[3], #12
  459. veor @y[5], @y[5], @t[4]
  460. veor @y[7], @y[7], @t[7]
  461. veor @t[7], @t[7], @t[5] @ clobber t[7] even more
  462. veor @y[3], @y[3], @t[5]
  463. veor @y[4], @y[4], @t[4]
  464. veor @y[5], @y[5], @t[7]
  465. vext.8 @t[4], @t[4], @t[4], #12
  466. veor @y[6], @y[6], @t[7]
  467. veor @y[4], @y[4], @t[7]
  468. veor @t[7], @t[7], @t[5]
  469. vext.8 @t[5], @t[5], @t[5], #12
  470. @ multiplication by 0x0d
  471. veor @y[4], @y[4], @y[7]
  472. veor @t[7], @t[7], @t[6] @ restore t[7]
  473. veor @y[7], @y[7], @t[4]
  474. vext.8 @t[6], @t[6], @t[6], #12
  475. veor @y[2], @y[2], @t[0]
  476. veor @y[7], @y[7], @t[5]
  477. vext.8 @t[7], @t[7], @t[7], #12
  478. veor @y[2], @y[2], @t[2]
  479. veor @y[3], @y[3], @y[1]
  480. veor @y[1], @y[1], @t[1]
  481. veor @y[0], @y[0], @t[0]
  482. veor @y[3], @y[3], @t[0]
  483. veor @y[1], @y[1], @t[5]
  484. veor @y[0], @y[0], @t[5]
  485. vext.8 @t[0], @t[0], @t[0], #12
  486. veor @y[1], @y[1], @t[7]
  487. veor @y[0], @y[0], @t[6]
  488. veor @y[3], @y[3], @y[1]
  489. veor @y[4], @y[4], @t[1]
  490. vext.8 @t[1], @t[1], @t[1], #12
  491. veor @y[7], @y[7], @t[7]
  492. veor @y[4], @y[4], @t[2]
  493. veor @y[5], @y[5], @t[2]
  494. veor @y[2], @y[2], @t[6]
  495. veor @t[6], @t[6], @t[3] @ clobber t[6]
  496. vext.8 @t[2], @t[2], @t[2], #12
  497. veor @y[4], @y[4], @y[7]
  498. veor @y[3], @y[3], @t[6]
  499. veor @y[6], @y[6], @t[6]
  500. veor @y[5], @y[5], @t[5]
  501. vext.8 @t[5], @t[5], @t[5], #12
  502. veor @y[6], @y[6], @t[4]
  503. vext.8 @t[4], @t[4], @t[4], #12
  504. veor @y[5], @y[5], @t[6]
  505. veor @y[6], @y[6], @t[7]
  506. vext.8 @t[7], @t[7], @t[7], #12
  507. veor @t[6], @t[6], @t[3] @ restore t[6]
  508. vext.8 @t[3], @t[3], @t[3], #12
  509. @ multiplication by 0x09
  510. veor @y[4], @y[4], @y[1]
  511. veor @t[1], @t[1], @y[1] @ t[1]=y[1]
  512. veor @t[0], @t[0], @t[5] @ clobber t[0]
  513. vext.8 @t[6], @t[6], @t[6], #12
  514. veor @t[1], @t[1], @t[5]
  515. veor @y[3], @y[3], @t[0]
  516. veor @t[0], @t[0], @y[0] @ t[0]=y[0]
  517. veor @t[1], @t[1], @t[6]
  518. veor @t[6], @t[6], @t[7] @ clobber t[6]
  519. veor @y[4], @y[4], @t[1]
  520. veor @y[7], @y[7], @t[4]
  521. veor @y[6], @y[6], @t[3]
  522. veor @y[5], @y[5], @t[2]
  523. veor @t[4], @t[4], @y[4] @ t[4]=y[4]
  524. veor @t[3], @t[3], @y[3] @ t[3]=y[3]
  525. veor @t[5], @t[5], @y[5] @ t[5]=y[5]
  526. veor @t[2], @t[2], @y[2] @ t[2]=y[2]
  527. veor @t[3], @t[3], @t[7]
  528. veor @XMM[5], @t[5], @t[6]
  529. veor @XMM[6], @t[6], @y[6] @ t[6]=y[6]
  530. veor @XMM[2], @t[2], @t[6]
  531. veor @XMM[7], @t[7], @y[7] @ t[7]=y[7]
  532. vmov @XMM[0], @t[0]
  533. vmov @XMM[1], @t[1]
  534. @ vmov @XMM[2], @t[2]
  535. vmov @XMM[3], @t[3]
  536. vmov @XMM[4], @t[4]
  537. @ vmov @XMM[5], @t[5]
  538. @ vmov @XMM[6], @t[6]
  539. @ vmov @XMM[7], @t[7]
  540. ___
  541. }
  542. sub InvMixColumns {
  543. my @x=@_[0..7];
  544. my @t=@_[8..15];
  545. # Thanks to Jussi Kivilinna for providing pointer to
  546. #
  547. # | 0e 0b 0d 09 | | 02 03 01 01 | | 05 00 04 00 |
  548. # | 09 0e 0b 0d | = | 01 02 03 01 | x | 00 05 00 04 |
  549. # | 0d 09 0e 0b | | 01 01 02 03 | | 04 00 05 00 |
  550. # | 0b 0d 09 0e | | 03 01 01 02 | | 00 04 00 05 |
  551. $code.=<<___;
  552. @ multiplication by 0x05-0x00-0x04-0x00
  553. vext.8 @t[0], @x[0], @x[0], #8
  554. vext.8 @t[6], @x[6], @x[6], #8
  555. vext.8 @t[7], @x[7], @x[7], #8
  556. veor @t[0], @t[0], @x[0]
  557. vext.8 @t[1], @x[1], @x[1], #8
  558. veor @t[6], @t[6], @x[6]
  559. vext.8 @t[2], @x[2], @x[2], #8
  560. veor @t[7], @t[7], @x[7]
  561. vext.8 @t[3], @x[3], @x[3], #8
  562. veor @t[1], @t[1], @x[1]
  563. vext.8 @t[4], @x[4], @x[4], #8
  564. veor @t[2], @t[2], @x[2]
  565. vext.8 @t[5], @x[5], @x[5], #8
  566. veor @t[3], @t[3], @x[3]
  567. veor @t[4], @t[4], @x[4]
  568. veor @t[5], @t[5], @x[5]
  569. veor @x[0], @x[0], @t[6]
  570. veor @x[1], @x[1], @t[6]
  571. veor @x[2], @x[2], @t[0]
  572. veor @x[4], @x[4], @t[2]
  573. veor @x[3], @x[3], @t[1]
  574. veor @x[1], @x[1], @t[7]
  575. veor @x[2], @x[2], @t[7]
  576. veor @x[4], @x[4], @t[6]
  577. veor @x[5], @x[5], @t[3]
  578. veor @x[3], @x[3], @t[6]
  579. veor @x[6], @x[6], @t[4]
  580. veor @x[4], @x[4], @t[7]
  581. veor @x[5], @x[5], @t[7]
  582. veor @x[7], @x[7], @t[5]
  583. ___
  584. &MixColumns (@x,@t,1); # flipped 2<->3 and 4<->6
  585. }
  586. sub swapmove {
  587. my ($a,$b,$n,$mask,$t)=@_;
  588. $code.=<<___;
  589. vshr.u64 $t, $b, #$n
  590. veor $t, $t, $a
  591. vand $t, $t, $mask
  592. veor $a, $a, $t
  593. vshl.u64 $t, $t, #$n
  594. veor $b, $b, $t
  595. ___
  596. }
  597. sub swapmove2x {
  598. my ($a0,$b0,$a1,$b1,$n,$mask,$t0,$t1)=@_;
  599. $code.=<<___;
  600. vshr.u64 $t0, $b0, #$n
  601. vshr.u64 $t1, $b1, #$n
  602. veor $t0, $t0, $a0
  603. veor $t1, $t1, $a1
  604. vand $t0, $t0, $mask
  605. vand $t1, $t1, $mask
  606. veor $a0, $a0, $t0
  607. vshl.u64 $t0, $t0, #$n
  608. veor $a1, $a1, $t1
  609. vshl.u64 $t1, $t1, #$n
  610. veor $b0, $b0, $t0
  611. veor $b1, $b1, $t1
  612. ___
  613. }
  614. sub bitslice {
  615. my @x=reverse(@_[0..7]);
  616. my ($t0,$t1,$t2,$t3)=@_[8..11];
  617. $code.=<<___;
  618. vmov.i8 $t0,#0x55 @ compose .LBS0
  619. vmov.i8 $t1,#0x33 @ compose .LBS1
  620. ___
  621. &swapmove2x(@x[0,1,2,3],1,$t0,$t2,$t3);
  622. &swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
  623. $code.=<<___;
  624. vmov.i8 $t0,#0x0f @ compose .LBS2
  625. ___
  626. &swapmove2x(@x[0,2,1,3],2,$t1,$t2,$t3);
  627. &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
  628. &swapmove2x(@x[0,4,1,5],4,$t0,$t2,$t3);
  629. &swapmove2x(@x[2,6,3,7],4,$t0,$t2,$t3);
  630. }
  631. $code.=<<___;
  632. #if defined(__arm__)
  633. #ifndef __KERNEL__
  634. # include "arm_arch.h"
  635. # define VFP_ABI_PUSH vstmdb sp!,{d8-d15}
  636. # define VFP_ABI_POP vldmia sp!,{d8-d15}
  637. # define VFP_ABI_FRAME 0x40
  638. #else
  639. # define VFP_ABI_PUSH
  640. # define VFP_ABI_POP
  641. # define VFP_ABI_FRAME 0
  642. # define BSAES_ASM_EXTENDED_KEY
  643. # define XTS_CHAIN_TWEAK
  644. # define __ARM_ARCH__ __LINUX_ARM_ARCH__
  645. #endif
  646. #ifdef __thumb__
  647. # define adrl adr
  648. #endif
  649. #if __ARM_ARCH__>=7
  650. .text
  651. .syntax unified @ ARMv7-capable assembler is expected to handle this
  652. #ifdef __thumb2__
  653. .thumb
  654. #else
  655. .code 32
  656. #endif
  657. .fpu neon
  658. .type _bsaes_decrypt8,%function
  659. .align 4
  660. _bsaes_decrypt8:
  661. adr $const,_bsaes_decrypt8
  662. vldmia $key!, {@XMM[9]} @ round 0 key
  663. add $const,$const,#.LM0ISR-_bsaes_decrypt8
  664. vldmia $const!, {@XMM[8]} @ .LM0ISR
  665. veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
  666. veor @XMM[11], @XMM[1], @XMM[9]
  667. vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
  668. vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
  669. veor @XMM[12], @XMM[2], @XMM[9]
  670. vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
  671. vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
  672. veor @XMM[13], @XMM[3], @XMM[9]
  673. vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
  674. vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
  675. veor @XMM[14], @XMM[4], @XMM[9]
  676. vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
  677. vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
  678. veor @XMM[15], @XMM[5], @XMM[9]
  679. vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
  680. vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
  681. veor @XMM[10], @XMM[6], @XMM[9]
  682. vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
  683. vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
  684. veor @XMM[11], @XMM[7], @XMM[9]
  685. vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
  686. vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
  687. vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
  688. vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
  689. ___
  690. &bitslice (@XMM[0..7, 8..11]);
  691. $code.=<<___;
  692. sub $rounds,$rounds,#1
  693. b .Ldec_sbox
  694. .align 4
  695. .Ldec_loop:
  696. ___
  697. &ShiftRows (@XMM[0..7, 8..12]);
  698. $code.=".Ldec_sbox:\n";
  699. &InvSbox (@XMM[0..7, 8..15]);
  700. $code.=<<___;
  701. subs $rounds,$rounds,#1
  702. bcc .Ldec_done
  703. ___
  704. &InvMixColumns (@XMM[0,1,6,4,2,7,3,5, 8..15]);
  705. $code.=<<___;
  706. vldmia $const, {@XMM[12]} @ .LISR
  707. ite eq @ Thumb2 thing, sanity check in ARM
  708. addeq $const,$const,#0x10
  709. bne .Ldec_loop
  710. vldmia $const, {@XMM[12]} @ .LISRM0
  711. b .Ldec_loop
  712. .align 4
  713. .Ldec_done:
  714. ___
  715. &bitslice (@XMM[0,1,6,4,2,7,3,5, 8..11]);
  716. $code.=<<___;
  717. vldmia $key, {@XMM[8]} @ last round key
  718. veor @XMM[6], @XMM[6], @XMM[8]
  719. veor @XMM[4], @XMM[4], @XMM[8]
  720. veor @XMM[2], @XMM[2], @XMM[8]
  721. veor @XMM[7], @XMM[7], @XMM[8]
  722. veor @XMM[3], @XMM[3], @XMM[8]
  723. veor @XMM[5], @XMM[5], @XMM[8]
  724. veor @XMM[0], @XMM[0], @XMM[8]
  725. veor @XMM[1], @XMM[1], @XMM[8]
  726. bx lr
  727. .size _bsaes_decrypt8,.-_bsaes_decrypt8
  728. .type _bsaes_const,%object
  729. .align 6
  730. _bsaes_const:
  731. .LM0ISR: @ InvShiftRows constants
  732. .quad 0x0a0e0206070b0f03, 0x0004080c0d010509
  733. .LISR:
  734. .quad 0x0504070602010003, 0x0f0e0d0c080b0a09
  735. .LISRM0:
  736. .quad 0x01040b0e0205080f, 0x0306090c00070a0d
  737. .LM0SR: @ ShiftRows constants
  738. .quad 0x0a0e02060f03070b, 0x0004080c05090d01
  739. .LSR:
  740. .quad 0x0504070600030201, 0x0f0e0d0c0a09080b
  741. .LSRM0:
  742. .quad 0x0304090e00050a0f, 0x01060b0c0207080d
  743. .LM0:
  744. .quad 0x02060a0e03070b0f, 0x0004080c0105090d
  745. .LREVM0SR:
  746. .quad 0x090d01050c000408, 0x03070b0f060a0e02
  747. .asciz "Bit-sliced AES for NEON, CRYPTOGAMS by <appro\@openssl.org>"
  748. .align 6
  749. .size _bsaes_const,.-_bsaes_const
  750. .type _bsaes_encrypt8,%function
  751. .align 4
  752. _bsaes_encrypt8:
  753. adr $const,_bsaes_encrypt8
  754. vldmia $key!, {@XMM[9]} @ round 0 key
  755. sub $const,$const,#_bsaes_encrypt8-.LM0SR
  756. vldmia $const!, {@XMM[8]} @ .LM0SR
  757. _bsaes_encrypt8_alt:
  758. veor @XMM[10], @XMM[0], @XMM[9] @ xor with round0 key
  759. veor @XMM[11], @XMM[1], @XMM[9]
  760. vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
  761. vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
  762. veor @XMM[12], @XMM[2], @XMM[9]
  763. vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
  764. vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
  765. veor @XMM[13], @XMM[3], @XMM[9]
  766. vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
  767. vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
  768. veor @XMM[14], @XMM[4], @XMM[9]
  769. vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
  770. vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
  771. veor @XMM[15], @XMM[5], @XMM[9]
  772. vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
  773. vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
  774. veor @XMM[10], @XMM[6], @XMM[9]
  775. vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
  776. vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
  777. veor @XMM[11], @XMM[7], @XMM[9]
  778. vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
  779. vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
  780. vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
  781. vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
  782. _bsaes_encrypt8_bitslice:
  783. ___
  784. &bitslice (@XMM[0..7, 8..11]);
  785. $code.=<<___;
  786. sub $rounds,$rounds,#1
  787. b .Lenc_sbox
  788. .align 4
  789. .Lenc_loop:
  790. ___
  791. &ShiftRows (@XMM[0..7, 8..12]);
  792. $code.=".Lenc_sbox:\n";
  793. &Sbox (@XMM[0..7, 8..15]);
  794. $code.=<<___;
  795. subs $rounds,$rounds,#1
  796. bcc .Lenc_done
  797. ___
  798. &MixColumns (@XMM[0,1,4,6,3,7,2,5, 8..15]);
  799. $code.=<<___;
  800. vldmia $const, {@XMM[12]} @ .LSR
  801. ite eq @ Thumb2 thing, samity check in ARM
  802. addeq $const,$const,#0x10
  803. bne .Lenc_loop
  804. vldmia $const, {@XMM[12]} @ .LSRM0
  805. b .Lenc_loop
  806. .align 4
  807. .Lenc_done:
  808. ___
  809. # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
  810. &bitslice (@XMM[0,1,4,6,3,7,2,5, 8..11]);
  811. $code.=<<___;
  812. vldmia $key, {@XMM[8]} @ last round key
  813. veor @XMM[4], @XMM[4], @XMM[8]
  814. veor @XMM[6], @XMM[6], @XMM[8]
  815. veor @XMM[3], @XMM[3], @XMM[8]
  816. veor @XMM[7], @XMM[7], @XMM[8]
  817. veor @XMM[2], @XMM[2], @XMM[8]
  818. veor @XMM[5], @XMM[5], @XMM[8]
  819. veor @XMM[0], @XMM[0], @XMM[8]
  820. veor @XMM[1], @XMM[1], @XMM[8]
  821. bx lr
  822. .size _bsaes_encrypt8,.-_bsaes_encrypt8
  823. ___
  824. }
  825. {
  826. my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
  827. sub bitslice_key {
  828. my @x=reverse(@_[0..7]);
  829. my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
  830. &swapmove (@x[0,1],1,$bs0,$t2,$t3);
  831. $code.=<<___;
  832. @ &swapmove(@x[2,3],1,$t0,$t2,$t3);
  833. vmov @x[2], @x[0]
  834. vmov @x[3], @x[1]
  835. ___
  836. #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
  837. &swapmove2x (@x[0,2,1,3],2,$bs1,$t2,$t3);
  838. $code.=<<___;
  839. @ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
  840. vmov @x[4], @x[0]
  841. vmov @x[6], @x[2]
  842. vmov @x[5], @x[1]
  843. vmov @x[7], @x[3]
  844. ___
  845. &swapmove2x (@x[0,4,1,5],4,$bs2,$t2,$t3);
  846. &swapmove2x (@x[2,6,3,7],4,$bs2,$t2,$t3);
  847. }
  848. $code.=<<___;
  849. .type _bsaes_key_convert,%function
  850. .align 4
  851. _bsaes_key_convert:
  852. adr $const,_bsaes_key_convert
  853. vld1.8 {@XMM[7]}, [$inp]! @ load round 0 key
  854. sub $const,$const,#_bsaes_key_convert-.LM0
  855. vld1.8 {@XMM[15]}, [$inp]! @ load round 1 key
  856. vmov.i8 @XMM[8], #0x01 @ bit masks
  857. vmov.i8 @XMM[9], #0x02
  858. vmov.i8 @XMM[10], #0x04
  859. vmov.i8 @XMM[11], #0x08
  860. vmov.i8 @XMM[12], #0x10
  861. vmov.i8 @XMM[13], #0x20
  862. vldmia $const, {@XMM[14]} @ .LM0
  863. #ifdef __ARMEL__
  864. vrev32.8 @XMM[7], @XMM[7]
  865. vrev32.8 @XMM[15], @XMM[15]
  866. #endif
  867. sub $rounds,$rounds,#1
  868. vstmia $out!, {@XMM[7]} @ save round 0 key
  869. b .Lkey_loop
  870. .align 4
  871. .Lkey_loop:
  872. vtbl.8 `&Dlo(@XMM[7])`,{@XMM[15]},`&Dlo(@XMM[14])`
  873. vtbl.8 `&Dhi(@XMM[7])`,{@XMM[15]},`&Dhi(@XMM[14])`
  874. vmov.i8 @XMM[6], #0x40
  875. vmov.i8 @XMM[15], #0x80
  876. vtst.8 @XMM[0], @XMM[7], @XMM[8]
  877. vtst.8 @XMM[1], @XMM[7], @XMM[9]
  878. vtst.8 @XMM[2], @XMM[7], @XMM[10]
  879. vtst.8 @XMM[3], @XMM[7], @XMM[11]
  880. vtst.8 @XMM[4], @XMM[7], @XMM[12]
  881. vtst.8 @XMM[5], @XMM[7], @XMM[13]
  882. vtst.8 @XMM[6], @XMM[7], @XMM[6]
  883. vtst.8 @XMM[7], @XMM[7], @XMM[15]
  884. vld1.8 {@XMM[15]}, [$inp]! @ load next round key
  885. vmvn @XMM[0], @XMM[0] @ "pnot"
  886. vmvn @XMM[1], @XMM[1]
  887. vmvn @XMM[5], @XMM[5]
  888. vmvn @XMM[6], @XMM[6]
  889. #ifdef __ARMEL__
  890. vrev32.8 @XMM[15], @XMM[15]
  891. #endif
  892. subs $rounds,$rounds,#1
  893. vstmia $out!,{@XMM[0]-@XMM[7]} @ write bit-sliced round key
  894. bne .Lkey_loop
  895. vmov.i8 @XMM[7],#0x63 @ compose .L63
  896. @ don't save last round key
  897. bx lr
  898. .size _bsaes_key_convert,.-_bsaes_key_convert
  899. ___
  900. }
  901. if (0) { # following four functions are unsupported interface
  902. # used for benchmarking...
  903. $code.=<<___;
  904. .globl bsaes_enc_key_convert
  905. .hidden bsaes_enc_key_convert
  906. .type bsaes_enc_key_convert,%function
  907. .align 4
  908. bsaes_enc_key_convert:
  909. stmdb sp!,{r4-r6,lr}
  910. vstmdb sp!,{d8-d15} @ ABI specification says so
  911. ldr r5,[$inp,#240] @ pass rounds
  912. mov r4,$inp @ pass key
  913. mov r12,$out @ pass key schedule
  914. bl _bsaes_key_convert
  915. veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
  916. vstmia r12, {@XMM[7]} @ save last round key
  917. vldmia sp!,{d8-d15}
  918. ldmia sp!,{r4-r6,pc}
  919. .size bsaes_enc_key_convert,.-bsaes_enc_key_convert
  920. .globl bsaes_encrypt_128
  921. .hidden bsaes_encrypt_128
  922. .type bsaes_encrypt_128,%function
  923. .align 4
  924. bsaes_encrypt_128:
  925. stmdb sp!,{r4-r6,lr}
  926. vstmdb sp!,{d8-d15} @ ABI specification says so
  927. .Lenc128_loop:
  928. vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
  929. vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
  930. mov r4,$key @ pass the key
  931. vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
  932. mov r5,#10 @ pass rounds
  933. vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
  934. bl _bsaes_encrypt8
  935. vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
  936. vst1.8 {@XMM[4]}, [$out]!
  937. vst1.8 {@XMM[6]}, [$out]!
  938. vst1.8 {@XMM[3]}, [$out]!
  939. vst1.8 {@XMM[7]}, [$out]!
  940. vst1.8 {@XMM[2]}, [$out]!
  941. subs $len,$len,#0x80
  942. vst1.8 {@XMM[5]}, [$out]!
  943. bhi .Lenc128_loop
  944. vldmia sp!,{d8-d15}
  945. ldmia sp!,{r4-r6,pc}
  946. .size bsaes_encrypt_128,.-bsaes_encrypt_128
  947. .globl bsaes_dec_key_convert
  948. .hidden bsaes_dec_key_convert
  949. .type bsaes_dec_key_convert,%function
  950. .align 4
  951. bsaes_dec_key_convert:
  952. stmdb sp!,{r4-r6,lr}
  953. vstmdb sp!,{d8-d15} @ ABI specification says so
  954. ldr r5,[$inp,#240] @ pass rounds
  955. mov r4,$inp @ pass key
  956. mov r12,$out @ pass key schedule
  957. bl _bsaes_key_convert
  958. vldmia $out, {@XMM[6]}
  959. vstmia r12, {@XMM[15]} @ save last round key
  960. veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
  961. vstmia $out, {@XMM[7]}
  962. vldmia sp!,{d8-d15}
  963. ldmia sp!,{r4-r6,pc}
  964. .size bsaes_dec_key_convert,.-bsaes_dec_key_convert
  965. .globl bsaes_decrypt_128
  966. .hidden bsaes_decrypt_128
  967. .type bsaes_decrypt_128,%function
  968. .align 4
  969. bsaes_decrypt_128:
  970. stmdb sp!,{r4-r6,lr}
  971. vstmdb sp!,{d8-d15} @ ABI specification says so
  972. .Ldec128_loop:
  973. vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
  974. vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
  975. mov r4,$key @ pass the key
  976. vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
  977. mov r5,#10 @ pass rounds
  978. vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
  979. bl _bsaes_decrypt8
  980. vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
  981. vst1.8 {@XMM[6]}, [$out]!
  982. vst1.8 {@XMM[4]}, [$out]!
  983. vst1.8 {@XMM[2]}, [$out]!
  984. vst1.8 {@XMM[7]}, [$out]!
  985. vst1.8 {@XMM[3]}, [$out]!
  986. subs $len,$len,#0x80
  987. vst1.8 {@XMM[5]}, [$out]!
  988. bhi .Ldec128_loop
  989. vldmia sp!,{d8-d15}
  990. ldmia sp!,{r4-r6,pc}
  991. .size bsaes_decrypt_128,.-bsaes_decrypt_128
  992. ___
  993. }
  994. {
  995. my ($inp,$out,$len,$key, $ivp,$fp,$rounds)=map("r$_",(0..3,8..10));
  996. my ($keysched)=("sp");
  997. $code.=<<___;
  998. .extern AES_cbc_encrypt
  999. .extern AES_decrypt
  1000. .global bsaes_cbc_encrypt
  1001. .hidden bsaes_cbc_encrypt
  1002. .type bsaes_cbc_encrypt,%function
  1003. .align 5
  1004. bsaes_cbc_encrypt:
  1005. #ifndef __KERNEL__
  1006. cmp $len, #128
  1007. #ifndef __thumb__
  1008. blo AES_cbc_encrypt
  1009. #else
  1010. bhs 1f
  1011. b AES_cbc_encrypt
  1012. 1:
  1013. #endif
  1014. #endif
  1015. @ it is up to the caller to make sure we are called with enc == 0
  1016. mov ip, sp
  1017. stmdb sp!, {r4-r10, lr}
  1018. VFP_ABI_PUSH
  1019. ldr $ivp, [ip] @ IV is 1st arg on the stack
  1020. mov $len, $len, lsr#4 @ len in 16 byte blocks
  1021. sub sp, #0x10 @ scratch space to carry over the IV
  1022. mov $fp, sp @ save sp
  1023. ldr $rounds, [$key, #240] @ get # of rounds
  1024. #ifndef BSAES_ASM_EXTENDED_KEY
  1025. @ allocate the key schedule on the stack
  1026. sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
  1027. add r12, #`128-32` @ sifze of bit-slices key schedule
  1028. @ populate the key schedule
  1029. mov r4, $key @ pass key
  1030. mov r5, $rounds @ pass # of rounds
  1031. mov sp, r12 @ sp is $keysched
  1032. bl _bsaes_key_convert
  1033. vldmia $keysched, {@XMM[6]}
  1034. vstmia r12, {@XMM[15]} @ save last round key
  1035. veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
  1036. vstmia $keysched, {@XMM[7]}
  1037. #else
  1038. ldr r12, [$key, #244]
  1039. eors r12, #1
  1040. beq 0f
  1041. @ populate the key schedule
  1042. str r12, [$key, #244]
  1043. mov r4, $key @ pass key
  1044. mov r5, $rounds @ pass # of rounds
  1045. add r12, $key, #248 @ pass key schedule
  1046. bl _bsaes_key_convert
  1047. add r4, $key, #248
  1048. vldmia r4, {@XMM[6]}
  1049. vstmia r12, {@XMM[15]} @ save last round key
  1050. veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
  1051. vstmia r4, {@XMM[7]}
  1052. .align 2
  1053. 0:
  1054. #endif
  1055. vld1.8 {@XMM[15]}, [$ivp] @ load IV
  1056. b .Lcbc_dec_loop
  1057. .align 4
  1058. .Lcbc_dec_loop:
  1059. subs $len, $len, #0x8
  1060. bmi .Lcbc_dec_loop_finish
  1061. vld1.8 {@XMM[0]-@XMM[1]}, [$inp]! @ load input
  1062. vld1.8 {@XMM[2]-@XMM[3]}, [$inp]!
  1063. #ifndef BSAES_ASM_EXTENDED_KEY
  1064. mov r4, $keysched @ pass the key
  1065. #else
  1066. add r4, $key, #248
  1067. #endif
  1068. vld1.8 {@XMM[4]-@XMM[5]}, [$inp]!
  1069. mov r5, $rounds
  1070. vld1.8 {@XMM[6]-@XMM[7]}, [$inp]
  1071. sub $inp, $inp, #0x60
  1072. vstmia $fp, {@XMM[15]} @ put aside IV
  1073. bl _bsaes_decrypt8
  1074. vldmia $fp, {@XMM[14]} @ reload IV
  1075. vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
  1076. veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
  1077. vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
  1078. veor @XMM[1], @XMM[1], @XMM[8]
  1079. veor @XMM[6], @XMM[6], @XMM[9]
  1080. vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
  1081. veor @XMM[4], @XMM[4], @XMM[10]
  1082. veor @XMM[2], @XMM[2], @XMM[11]
  1083. vld1.8 {@XMM[14]-@XMM[15]}, [$inp]!
  1084. veor @XMM[7], @XMM[7], @XMM[12]
  1085. vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
  1086. veor @XMM[3], @XMM[3], @XMM[13]
  1087. vst1.8 {@XMM[6]}, [$out]!
  1088. veor @XMM[5], @XMM[5], @XMM[14]
  1089. vst1.8 {@XMM[4]}, [$out]!
  1090. vst1.8 {@XMM[2]}, [$out]!
  1091. vst1.8 {@XMM[7]}, [$out]!
  1092. vst1.8 {@XMM[3]}, [$out]!
  1093. vst1.8 {@XMM[5]}, [$out]!
  1094. b .Lcbc_dec_loop
  1095. .Lcbc_dec_loop_finish:
  1096. adds $len, $len, #8
  1097. beq .Lcbc_dec_done
  1098. vld1.8 {@XMM[0]}, [$inp]! @ load input
  1099. cmp $len, #2
  1100. blo .Lcbc_dec_one
  1101. vld1.8 {@XMM[1]}, [$inp]!
  1102. #ifndef BSAES_ASM_EXTENDED_KEY
  1103. mov r4, $keysched @ pass the key
  1104. #else
  1105. add r4, $key, #248
  1106. #endif
  1107. mov r5, $rounds
  1108. vstmia $fp, {@XMM[15]} @ put aside IV
  1109. beq .Lcbc_dec_two
  1110. vld1.8 {@XMM[2]}, [$inp]!
  1111. cmp $len, #4
  1112. blo .Lcbc_dec_three
  1113. vld1.8 {@XMM[3]}, [$inp]!
  1114. beq .Lcbc_dec_four
  1115. vld1.8 {@XMM[4]}, [$inp]!
  1116. cmp $len, #6
  1117. blo .Lcbc_dec_five
  1118. vld1.8 {@XMM[5]}, [$inp]!
  1119. beq .Lcbc_dec_six
  1120. vld1.8 {@XMM[6]}, [$inp]!
  1121. sub $inp, $inp, #0x70
  1122. bl _bsaes_decrypt8
  1123. vldmia $fp, {@XMM[14]} @ reload IV
  1124. vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
  1125. veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
  1126. vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
  1127. veor @XMM[1], @XMM[1], @XMM[8]
  1128. veor @XMM[6], @XMM[6], @XMM[9]
  1129. vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
  1130. veor @XMM[4], @XMM[4], @XMM[10]
  1131. veor @XMM[2], @XMM[2], @XMM[11]
  1132. vld1.8 {@XMM[15]}, [$inp]!
  1133. veor @XMM[7], @XMM[7], @XMM[12]
  1134. vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
  1135. veor @XMM[3], @XMM[3], @XMM[13]
  1136. vst1.8 {@XMM[6]}, [$out]!
  1137. vst1.8 {@XMM[4]}, [$out]!
  1138. vst1.8 {@XMM[2]}, [$out]!
  1139. vst1.8 {@XMM[7]}, [$out]!
  1140. vst1.8 {@XMM[3]}, [$out]!
  1141. b .Lcbc_dec_done
  1142. .align 4
  1143. .Lcbc_dec_six:
  1144. sub $inp, $inp, #0x60
  1145. bl _bsaes_decrypt8
  1146. vldmia $fp,{@XMM[14]} @ reload IV
  1147. vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
  1148. veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
  1149. vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
  1150. veor @XMM[1], @XMM[1], @XMM[8]
  1151. veor @XMM[6], @XMM[6], @XMM[9]
  1152. vld1.8 {@XMM[12]}, [$inp]!
  1153. veor @XMM[4], @XMM[4], @XMM[10]
  1154. veor @XMM[2], @XMM[2], @XMM[11]
  1155. vld1.8 {@XMM[15]}, [$inp]!
  1156. veor @XMM[7], @XMM[7], @XMM[12]
  1157. vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
  1158. vst1.8 {@XMM[6]}, [$out]!
  1159. vst1.8 {@XMM[4]}, [$out]!
  1160. vst1.8 {@XMM[2]}, [$out]!
  1161. vst1.8 {@XMM[7]}, [$out]!
  1162. b .Lcbc_dec_done
  1163. .align 4
  1164. .Lcbc_dec_five:
  1165. sub $inp, $inp, #0x50
  1166. bl _bsaes_decrypt8
  1167. vldmia $fp, {@XMM[14]} @ reload IV
  1168. vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
  1169. veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
  1170. vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
  1171. veor @XMM[1], @XMM[1], @XMM[8]
  1172. veor @XMM[6], @XMM[6], @XMM[9]
  1173. vld1.8 {@XMM[15]}, [$inp]!
  1174. veor @XMM[4], @XMM[4], @XMM[10]
  1175. vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
  1176. veor @XMM[2], @XMM[2], @XMM[11]
  1177. vst1.8 {@XMM[6]}, [$out]!
  1178. vst1.8 {@XMM[4]}, [$out]!
  1179. vst1.8 {@XMM[2]}, [$out]!
  1180. b .Lcbc_dec_done
  1181. .align 4
  1182. .Lcbc_dec_four:
  1183. sub $inp, $inp, #0x40
  1184. bl _bsaes_decrypt8
  1185. vldmia $fp, {@XMM[14]} @ reload IV
  1186. vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
  1187. veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
  1188. vld1.8 {@XMM[10]}, [$inp]!
  1189. veor @XMM[1], @XMM[1], @XMM[8]
  1190. veor @XMM[6], @XMM[6], @XMM[9]
  1191. vld1.8 {@XMM[15]}, [$inp]!
  1192. veor @XMM[4], @XMM[4], @XMM[10]
  1193. vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
  1194. vst1.8 {@XMM[6]}, [$out]!
  1195. vst1.8 {@XMM[4]}, [$out]!
  1196. b .Lcbc_dec_done
  1197. .align 4
  1198. .Lcbc_dec_three:
  1199. sub $inp, $inp, #0x30
  1200. bl _bsaes_decrypt8
  1201. vldmia $fp, {@XMM[14]} @ reload IV
  1202. vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ reload input
  1203. veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
  1204. vld1.8 {@XMM[15]}, [$inp]!
  1205. veor @XMM[1], @XMM[1], @XMM[8]
  1206. veor @XMM[6], @XMM[6], @XMM[9]
  1207. vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
  1208. vst1.8 {@XMM[6]}, [$out]!
  1209. b .Lcbc_dec_done
  1210. .align 4
  1211. .Lcbc_dec_two:
  1212. sub $inp, $inp, #0x20
  1213. bl _bsaes_decrypt8
  1214. vldmia $fp, {@XMM[14]} @ reload IV
  1215. vld1.8 {@XMM[8]}, [$inp]! @ reload input
  1216. veor @XMM[0], @XMM[0], @XMM[14] @ ^= IV
  1217. vld1.8 {@XMM[15]}, [$inp]! @ reload input
  1218. veor @XMM[1], @XMM[1], @XMM[8]
  1219. vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
  1220. b .Lcbc_dec_done
  1221. .align 4
  1222. .Lcbc_dec_one:
  1223. sub $inp, $inp, #0x10
  1224. mov $rounds, $out @ save original out pointer
  1225. mov $out, $fp @ use the iv scratch space as out buffer
  1226. mov r2, $key
  1227. vmov @XMM[4],@XMM[15] @ just in case ensure that IV
  1228. vmov @XMM[5],@XMM[0] @ and input are preserved
  1229. bl AES_decrypt
  1230. vld1.8 {@XMM[0]}, [$fp,:64] @ load result
  1231. veor @XMM[0], @XMM[0], @XMM[4] @ ^= IV
  1232. vmov @XMM[15], @XMM[5] @ @XMM[5] holds input
  1233. vst1.8 {@XMM[0]}, [$rounds] @ write output
  1234. .Lcbc_dec_done:
  1235. #ifndef BSAES_ASM_EXTENDED_KEY
  1236. vmov.i32 q0, #0
  1237. vmov.i32 q1, #0
  1238. .Lcbc_dec_bzero: @ wipe key schedule [if any]
  1239. vstmia $keysched!, {q0-q1}
  1240. cmp $keysched, $fp
  1241. bne .Lcbc_dec_bzero
  1242. #endif
  1243. mov sp, $fp
  1244. add sp, #0x10 @ add sp,$fp,#0x10 is no good for thumb
  1245. vst1.8 {@XMM[15]}, [$ivp] @ return IV
  1246. VFP_ABI_POP
  1247. ldmia sp!, {r4-r10, pc}
  1248. .size bsaes_cbc_encrypt,.-bsaes_cbc_encrypt
  1249. ___
  1250. }
  1251. {
  1252. my ($inp,$out,$len,$key, $ctr,$fp,$rounds)=(map("r$_",(0..3,8..10)));
  1253. my $const = "r6"; # shared with _bsaes_encrypt8_alt
  1254. my $keysched = "sp";
  1255. $code.=<<___;
  1256. .extern AES_encrypt
  1257. .global bsaes_ctr32_encrypt_blocks
  1258. .hidden bsaes_ctr32_encrypt_blocks
  1259. .type bsaes_ctr32_encrypt_blocks,%function
  1260. .align 5
  1261. bsaes_ctr32_encrypt_blocks:
  1262. cmp $len, #8 @ use plain AES for
  1263. blo .Lctr_enc_short @ small sizes
  1264. mov ip, sp
  1265. stmdb sp!, {r4-r10, lr}
  1266. VFP_ABI_PUSH
  1267. ldr $ctr, [ip] @ ctr is 1st arg on the stack
  1268. sub sp, sp, #0x10 @ scratch space to carry over the ctr
  1269. mov $fp, sp @ save sp
  1270. ldr $rounds, [$key, #240] @ get # of rounds
  1271. #ifndef BSAES_ASM_EXTENDED_KEY
  1272. @ allocate the key schedule on the stack
  1273. sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
  1274. add r12, #`128-32` @ size of bit-sliced key schedule
  1275. @ populate the key schedule
  1276. mov r4, $key @ pass key
  1277. mov r5, $rounds @ pass # of rounds
  1278. mov sp, r12 @ sp is $keysched
  1279. bl _bsaes_key_convert
  1280. veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
  1281. vstmia r12, {@XMM[7]} @ save last round key
  1282. vld1.8 {@XMM[0]}, [$ctr] @ load counter
  1283. add $ctr, $const, #.LREVM0SR-.LM0 @ borrow $ctr
  1284. vldmia $keysched, {@XMM[4]} @ load round0 key
  1285. #else
  1286. ldr r12, [$key, #244]
  1287. eors r12, #1
  1288. beq 0f
  1289. @ populate the key schedule
  1290. str r12, [$key, #244]
  1291. mov r4, $key @ pass key
  1292. mov r5, $rounds @ pass # of rounds
  1293. add r12, $key, #248 @ pass key schedule
  1294. bl _bsaes_key_convert
  1295. veor @XMM[7],@XMM[7],@XMM[15] @ fix up last round key
  1296. vstmia r12, {@XMM[7]} @ save last round key
  1297. .align 2
  1298. 0: add r12, $key, #248
  1299. vld1.8 {@XMM[0]}, [$ctr] @ load counter
  1300. adrl $ctr, .LREVM0SR @ borrow $ctr
  1301. vldmia r12, {@XMM[4]} @ load round0 key
  1302. sub sp, #0x10 @ place for adjusted round0 key
  1303. #endif
  1304. vmov.i32 @XMM[8],#1 @ compose 1<<96
  1305. veor @XMM[9],@XMM[9],@XMM[9]
  1306. vrev32.8 @XMM[0],@XMM[0]
  1307. vext.8 @XMM[8],@XMM[9],@XMM[8],#4
  1308. vrev32.8 @XMM[4],@XMM[4]
  1309. vadd.u32 @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
  1310. vstmia $keysched, {@XMM[4]} @ save adjusted round0 key
  1311. b .Lctr_enc_loop
  1312. .align 4
  1313. .Lctr_enc_loop:
  1314. vadd.u32 @XMM[10], @XMM[8], @XMM[9] @ compose 3<<96
  1315. vadd.u32 @XMM[1], @XMM[0], @XMM[8] @ +1
  1316. vadd.u32 @XMM[2], @XMM[0], @XMM[9] @ +2
  1317. vadd.u32 @XMM[3], @XMM[0], @XMM[10] @ +3
  1318. vadd.u32 @XMM[4], @XMM[1], @XMM[10]
  1319. vadd.u32 @XMM[5], @XMM[2], @XMM[10]
  1320. vadd.u32 @XMM[6], @XMM[3], @XMM[10]
  1321. vadd.u32 @XMM[7], @XMM[4], @XMM[10]
  1322. vadd.u32 @XMM[10], @XMM[5], @XMM[10] @ next counter
  1323. @ Borrow prologue from _bsaes_encrypt8 to use the opportunity
  1324. @ to flip byte order in 32-bit counter
  1325. vldmia $keysched, {@XMM[9]} @ load round0 key
  1326. #ifndef BSAES_ASM_EXTENDED_KEY
  1327. add r4, $keysched, #0x10 @ pass next round key
  1328. #else
  1329. add r4, $key, #`248+16`
  1330. #endif
  1331. vldmia $ctr, {@XMM[8]} @ .LREVM0SR
  1332. mov r5, $rounds @ pass rounds
  1333. vstmia $fp, {@XMM[10]} @ save next counter
  1334. sub $const, $ctr, #.LREVM0SR-.LSR @ pass constants
  1335. bl _bsaes_encrypt8_alt
  1336. subs $len, $len, #8
  1337. blo .Lctr_enc_loop_done
  1338. vld1.8 {@XMM[8]-@XMM[9]}, [$inp]! @ load input
  1339. vld1.8 {@XMM[10]-@XMM[11]}, [$inp]!
  1340. veor @XMM[0], @XMM[8]
  1341. veor @XMM[1], @XMM[9]
  1342. vld1.8 {@XMM[12]-@XMM[13]}, [$inp]!
  1343. veor @XMM[4], @XMM[10]
  1344. veor @XMM[6], @XMM[11]
  1345. vld1.8 {@XMM[14]-@XMM[15]}, [$inp]!
  1346. veor @XMM[3], @XMM[12]
  1347. vst1.8 {@XMM[0]-@XMM[1]}, [$out]! @ write output
  1348. veor @XMM[7], @XMM[13]
  1349. veor @XMM[2], @XMM[14]
  1350. vst1.8 {@XMM[4]}, [$out]!
  1351. veor @XMM[5], @XMM[15]
  1352. vst1.8 {@XMM[6]}, [$out]!
  1353. vmov.i32 @XMM[8], #1 @ compose 1<<96
  1354. vst1.8 {@XMM[3]}, [$out]!
  1355. veor @XMM[9], @XMM[9], @XMM[9]
  1356. vst1.8 {@XMM[7]}, [$out]!
  1357. vext.8 @XMM[8], @XMM[9], @XMM[8], #4
  1358. vst1.8 {@XMM[2]}, [$out]!
  1359. vadd.u32 @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
  1360. vst1.8 {@XMM[5]}, [$out]!
  1361. vldmia $fp, {@XMM[0]} @ load counter
  1362. bne .Lctr_enc_loop
  1363. b .Lctr_enc_done
  1364. .align 4
  1365. .Lctr_enc_loop_done:
  1366. add $len, $len, #8
  1367. vld1.8 {@XMM[8]}, [$inp]! @ load input
  1368. veor @XMM[0], @XMM[8]
  1369. vst1.8 {@XMM[0]}, [$out]! @ write output
  1370. cmp $len, #2
  1371. blo .Lctr_enc_done
  1372. vld1.8 {@XMM[9]}, [$inp]!
  1373. veor @XMM[1], @XMM[9]
  1374. vst1.8 {@XMM[1]}, [$out]!
  1375. beq .Lctr_enc_done
  1376. vld1.8 {@XMM[10]}, [$inp]!
  1377. veor @XMM[4], @XMM[10]
  1378. vst1.8 {@XMM[4]}, [$out]!
  1379. cmp $len, #4
  1380. blo .Lctr_enc_done
  1381. vld1.8 {@XMM[11]}, [$inp]!
  1382. veor @XMM[6], @XMM[11]
  1383. vst1.8 {@XMM[6]}, [$out]!
  1384. beq .Lctr_enc_done
  1385. vld1.8 {@XMM[12]}, [$inp]!
  1386. veor @XMM[3], @XMM[12]
  1387. vst1.8 {@XMM[3]}, [$out]!
  1388. cmp $len, #6
  1389. blo .Lctr_enc_done
  1390. vld1.8 {@XMM[13]}, [$inp]!
  1391. veor @XMM[7], @XMM[13]
  1392. vst1.8 {@XMM[7]}, [$out]!
  1393. beq .Lctr_enc_done
  1394. vld1.8 {@XMM[14]}, [$inp]
  1395. veor @XMM[2], @XMM[14]
  1396. vst1.8 {@XMM[2]}, [$out]!
  1397. .Lctr_enc_done:
  1398. vmov.i32 q0, #0
  1399. vmov.i32 q1, #0
  1400. #ifndef BSAES_ASM_EXTENDED_KEY
  1401. .Lctr_enc_bzero: @ wipe key schedule [if any]
  1402. vstmia $keysched!, {q0-q1}
  1403. cmp $keysched, $fp
  1404. bne .Lctr_enc_bzero
  1405. #else
  1406. vstmia $keysched, {q0-q1}
  1407. #endif
  1408. mov sp, $fp
  1409. add sp, #0x10 @ add sp,$fp,#0x10 is no good for thumb
  1410. VFP_ABI_POP
  1411. ldmia sp!, {r4-r10, pc} @ return
  1412. .align 4
  1413. .Lctr_enc_short:
  1414. ldr ip, [sp] @ ctr pointer is passed on stack
  1415. stmdb sp!, {r4-r8, lr}
  1416. mov r4, $inp @ copy arguments
  1417. mov r5, $out
  1418. mov r6, $len
  1419. mov r7, $key
  1420. ldr r8, [ip, #12] @ load counter LSW
  1421. vld1.8 {@XMM[1]}, [ip] @ load whole counter value
  1422. #ifdef __ARMEL__
  1423. rev r8, r8
  1424. #endif
  1425. sub sp, sp, #0x10
  1426. vst1.8 {@XMM[1]}, [sp,:64] @ copy counter value
  1427. sub sp, sp, #0x10
  1428. .Lctr_enc_short_loop:
  1429. add r0, sp, #0x10 @ input counter value
  1430. mov r1, sp @ output on the stack
  1431. mov r2, r7 @ key
  1432. bl AES_encrypt
  1433. vld1.8 {@XMM[0]}, [r4]! @ load input
  1434. vld1.8 {@XMM[1]}, [sp,:64] @ load encrypted counter
  1435. add r8, r8, #1
  1436. #ifdef __ARMEL__
  1437. rev r0, r8
  1438. str r0, [sp, #0x1c] @ next counter value
  1439. #else
  1440. str r8, [sp, #0x1c] @ next counter value
  1441. #endif
  1442. veor @XMM[0],@XMM[0],@XMM[1]
  1443. vst1.8 {@XMM[0]}, [r5]! @ store output
  1444. subs r6, r6, #1
  1445. bne .Lctr_enc_short_loop
  1446. vmov.i32 q0, #0
  1447. vmov.i32 q1, #0
  1448. vstmia sp!, {q0-q1}
  1449. ldmia sp!, {r4-r8, pc}
  1450. .size bsaes_ctr32_encrypt_blocks,.-bsaes_ctr32_encrypt_blocks
  1451. ___
  1452. }
  1453. {
  1454. ######################################################################
  1455. # void bsaes_xts_[en|de]crypt(const char *inp,char *out,size_t len,
  1456. # const AES_KEY *key1, const AES_KEY *key2,
  1457. # const unsigned char iv[16]);
  1458. #
  1459. my ($inp,$out,$len,$key,$rounds,$magic,$fp)=(map("r$_",(7..10,1..3)));
  1460. my $const="r6"; # returned by _bsaes_key_convert
  1461. my $twmask=@XMM[5];
  1462. my @T=@XMM[6..7];
  1463. $code.=<<___;
  1464. .globl bsaes_xts_encrypt
  1465. .hidden bsaes_xts_encrypt
  1466. .type bsaes_xts_encrypt,%function
  1467. .align 4
  1468. bsaes_xts_encrypt:
  1469. mov ip, sp
  1470. stmdb sp!, {r4-r10, lr} @ 0x20
  1471. VFP_ABI_PUSH
  1472. mov r6, sp @ future $fp
  1473. mov $inp, r0
  1474. mov $out, r1
  1475. mov $len, r2
  1476. mov $key, r3
  1477. sub r0, sp, #0x10 @ 0x10
  1478. bic r0, #0xf @ align at 16 bytes
  1479. mov sp, r0
  1480. #ifdef XTS_CHAIN_TWEAK
  1481. ldr r0, [ip] @ pointer to input tweak
  1482. #else
  1483. @ generate initial tweak
  1484. ldr r0, [ip, #4] @ iv[]
  1485. mov r1, sp
  1486. ldr r2, [ip, #0] @ key2
  1487. bl AES_encrypt
  1488. mov r0,sp @ pointer to initial tweak
  1489. #endif
  1490. ldr $rounds, [$key, #240] @ get # of rounds
  1491. mov $fp, r6
  1492. #ifndef BSAES_ASM_EXTENDED_KEY
  1493. @ allocate the key schedule on the stack
  1494. sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
  1495. @ add r12, #`128-32` @ size of bit-sliced key schedule
  1496. sub r12, #`32+16` @ place for tweak[9]
  1497. @ populate the key schedule
  1498. mov r4, $key @ pass key
  1499. mov r5, $rounds @ pass # of rounds
  1500. mov sp, r12
  1501. add r12, #0x90 @ pass key schedule
  1502. bl _bsaes_key_convert
  1503. veor @XMM[7], @XMM[7], @XMM[15] @ fix up last round key
  1504. vstmia r12, {@XMM[7]} @ save last round key
  1505. #else
  1506. ldr r12, [$key, #244]
  1507. eors r12, #1
  1508. beq 0f
  1509. str r12, [$key, #244]
  1510. mov r4, $key @ pass key
  1511. mov r5, $rounds @ pass # of rounds
  1512. add r12, $key, #248 @ pass key schedule
  1513. bl _bsaes_key_convert
  1514. veor @XMM[7], @XMM[7], @XMM[15] @ fix up last round key
  1515. vstmia r12, {@XMM[7]}
  1516. .align 2
  1517. 0: sub sp, #0x90 @ place for tweak[9]
  1518. #endif
  1519. vld1.8 {@XMM[8]}, [r0] @ initial tweak
  1520. adr $magic, .Lxts_magic
  1521. subs $len, #0x80
  1522. blo .Lxts_enc_short
  1523. b .Lxts_enc_loop
  1524. .align 4
  1525. .Lxts_enc_loop:
  1526. vldmia $magic, {$twmask} @ load XTS magic
  1527. vshr.s64 @T[0], @XMM[8], #63
  1528. mov r0, sp
  1529. vand @T[0], @T[0], $twmask
  1530. ___
  1531. for($i=9;$i<16;$i++) {
  1532. $code.=<<___;
  1533. vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
  1534. vst1.64 {@XMM[$i-1]}, [r0,:128]!
  1535. vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
  1536. vshr.s64 @T[1], @XMM[$i], #63
  1537. veor @XMM[$i], @XMM[$i], @T[0]
  1538. vand @T[1], @T[1], $twmask
  1539. ___
  1540. @T=reverse(@T);
  1541. $code.=<<___ if ($i>=10);
  1542. vld1.8 {@XMM[$i-10]}, [$inp]!
  1543. ___
  1544. $code.=<<___ if ($i>=11);
  1545. veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
  1546. ___
  1547. }
  1548. $code.=<<___;
  1549. vadd.u64 @XMM[8], @XMM[15], @XMM[15]
  1550. vst1.64 {@XMM[15]}, [r0,:128]!
  1551. vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
  1552. veor @XMM[8], @XMM[8], @T[0]
  1553. vst1.64 {@XMM[8]}, [r0,:128] @ next round tweak
  1554. vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
  1555. veor @XMM[5], @XMM[5], @XMM[13]
  1556. #ifndef BSAES_ASM_EXTENDED_KEY
  1557. add r4, sp, #0x90 @ pass key schedule
  1558. #else
  1559. add r4, $key, #248 @ pass key schedule
  1560. #endif
  1561. veor @XMM[6], @XMM[6], @XMM[14]
  1562. mov r5, $rounds @ pass rounds
  1563. veor @XMM[7], @XMM[7], @XMM[15]
  1564. mov r0, sp
  1565. bl _bsaes_encrypt8
  1566. vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
  1567. vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
  1568. veor @XMM[0], @XMM[0], @XMM[ 8]
  1569. vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
  1570. veor @XMM[1], @XMM[1], @XMM[ 9]
  1571. veor @XMM[8], @XMM[4], @XMM[10]
  1572. vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
  1573. veor @XMM[9], @XMM[6], @XMM[11]
  1574. vld1.64 {@XMM[14]-@XMM[15]}, [r0,:128]!
  1575. veor @XMM[10], @XMM[3], @XMM[12]
  1576. vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
  1577. veor @XMM[11], @XMM[7], @XMM[13]
  1578. veor @XMM[12], @XMM[2], @XMM[14]
  1579. vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
  1580. veor @XMM[13], @XMM[5], @XMM[15]
  1581. vst1.8 {@XMM[12]-@XMM[13]}, [$out]!
  1582. vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
  1583. subs $len, #0x80
  1584. bpl .Lxts_enc_loop
  1585. .Lxts_enc_short:
  1586. adds $len, #0x70
  1587. bmi .Lxts_enc_done
  1588. vldmia $magic, {$twmask} @ load XTS magic
  1589. vshr.s64 @T[0], @XMM[8], #63
  1590. mov r0, sp
  1591. vand @T[0], @T[0], $twmask
  1592. ___
  1593. for($i=9;$i<16;$i++) {
  1594. $code.=<<___;
  1595. vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
  1596. vst1.64 {@XMM[$i-1]}, [r0,:128]!
  1597. vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
  1598. vshr.s64 @T[1], @XMM[$i], #63
  1599. veor @XMM[$i], @XMM[$i], @T[0]
  1600. vand @T[1], @T[1], $twmask
  1601. ___
  1602. @T=reverse(@T);
  1603. $code.=<<___ if ($i>=10);
  1604. vld1.8 {@XMM[$i-10]}, [$inp]!
  1605. subs $len, #0x10
  1606. bmi .Lxts_enc_`$i-9`
  1607. ___
  1608. $code.=<<___ if ($i>=11);
  1609. veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
  1610. ___
  1611. }
  1612. $code.=<<___;
  1613. sub $len, #0x10
  1614. vst1.64 {@XMM[15]}, [r0,:128] @ next round tweak
  1615. vld1.8 {@XMM[6]}, [$inp]!
  1616. veor @XMM[5], @XMM[5], @XMM[13]
  1617. #ifndef BSAES_ASM_EXTENDED_KEY
  1618. add r4, sp, #0x90 @ pass key schedule
  1619. #else
  1620. add r4, $key, #248 @ pass key schedule
  1621. #endif
  1622. veor @XMM[6], @XMM[6], @XMM[14]
  1623. mov r5, $rounds @ pass rounds
  1624. mov r0, sp
  1625. bl _bsaes_encrypt8
  1626. vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
  1627. vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
  1628. veor @XMM[0], @XMM[0], @XMM[ 8]
  1629. vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
  1630. veor @XMM[1], @XMM[1], @XMM[ 9]
  1631. veor @XMM[8], @XMM[4], @XMM[10]
  1632. vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
  1633. veor @XMM[9], @XMM[6], @XMM[11]
  1634. vld1.64 {@XMM[14]}, [r0,:128]!
  1635. veor @XMM[10], @XMM[3], @XMM[12]
  1636. vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
  1637. veor @XMM[11], @XMM[7], @XMM[13]
  1638. veor @XMM[12], @XMM[2], @XMM[14]
  1639. vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
  1640. vst1.8 {@XMM[12]}, [$out]!
  1641. vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
  1642. b .Lxts_enc_done
  1643. .align 4
  1644. .Lxts_enc_6:
  1645. vst1.64 {@XMM[14]}, [r0,:128] @ next round tweak
  1646. veor @XMM[4], @XMM[4], @XMM[12]
  1647. #ifndef BSAES_ASM_EXTENDED_KEY
  1648. add r4, sp, #0x90 @ pass key schedule
  1649. #else
  1650. add r4, $key, #248 @ pass key schedule
  1651. #endif
  1652. veor @XMM[5], @XMM[5], @XMM[13]
  1653. mov r5, $rounds @ pass rounds
  1654. mov r0, sp
  1655. bl _bsaes_encrypt8
  1656. vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
  1657. vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
  1658. veor @XMM[0], @XMM[0], @XMM[ 8]
  1659. vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
  1660. veor @XMM[1], @XMM[1], @XMM[ 9]
  1661. veor @XMM[8], @XMM[4], @XMM[10]
  1662. vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
  1663. veor @XMM[9], @XMM[6], @XMM[11]
  1664. veor @XMM[10], @XMM[3], @XMM[12]
  1665. vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
  1666. veor @XMM[11], @XMM[7], @XMM[13]
  1667. vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
  1668. vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
  1669. b .Lxts_enc_done
  1670. @ put this in range for both ARM and Thumb mode adr instructions
  1671. .align 5
  1672. .Lxts_magic:
  1673. .quad 1, 0x87
  1674. .align 5
  1675. .Lxts_enc_5:
  1676. vst1.64 {@XMM[13]}, [r0,:128] @ next round tweak
  1677. veor @XMM[3], @XMM[3], @XMM[11]
  1678. #ifndef BSAES_ASM_EXTENDED_KEY
  1679. add r4, sp, #0x90 @ pass key schedule
  1680. #else
  1681. add r4, $key, #248 @ pass key schedule
  1682. #endif
  1683. veor @XMM[4], @XMM[4], @XMM[12]
  1684. mov r5, $rounds @ pass rounds
  1685. mov r0, sp
  1686. bl _bsaes_encrypt8
  1687. vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
  1688. vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
  1689. veor @XMM[0], @XMM[0], @XMM[ 8]
  1690. vld1.64 {@XMM[12]}, [r0,:128]!
  1691. veor @XMM[1], @XMM[1], @XMM[ 9]
  1692. veor @XMM[8], @XMM[4], @XMM[10]
  1693. vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
  1694. veor @XMM[9], @XMM[6], @XMM[11]
  1695. veor @XMM[10], @XMM[3], @XMM[12]
  1696. vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
  1697. vst1.8 {@XMM[10]}, [$out]!
  1698. vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
  1699. b .Lxts_enc_done
  1700. .align 4
  1701. .Lxts_enc_4:
  1702. vst1.64 {@XMM[12]}, [r0,:128] @ next round tweak
  1703. veor @XMM[2], @XMM[2], @XMM[10]
  1704. #ifndef BSAES_ASM_EXTENDED_KEY
  1705. add r4, sp, #0x90 @ pass key schedule
  1706. #else
  1707. add r4, $key, #248 @ pass key schedule
  1708. #endif
  1709. veor @XMM[3], @XMM[3], @XMM[11]
  1710. mov r5, $rounds @ pass rounds
  1711. mov r0, sp
  1712. bl _bsaes_encrypt8
  1713. vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
  1714. vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
  1715. veor @XMM[0], @XMM[0], @XMM[ 8]
  1716. veor @XMM[1], @XMM[1], @XMM[ 9]
  1717. veor @XMM[8], @XMM[4], @XMM[10]
  1718. vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
  1719. veor @XMM[9], @XMM[6], @XMM[11]
  1720. vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
  1721. vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
  1722. b .Lxts_enc_done
  1723. .align 4
  1724. .Lxts_enc_3:
  1725. vst1.64 {@XMM[11]}, [r0,:128] @ next round tweak
  1726. veor @XMM[1], @XMM[1], @XMM[9]
  1727. #ifndef BSAES_ASM_EXTENDED_KEY
  1728. add r4, sp, #0x90 @ pass key schedule
  1729. #else
  1730. add r4, $key, #248 @ pass key schedule
  1731. #endif
  1732. veor @XMM[2], @XMM[2], @XMM[10]
  1733. mov r5, $rounds @ pass rounds
  1734. mov r0, sp
  1735. bl _bsaes_encrypt8
  1736. vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
  1737. vld1.64 {@XMM[10]}, [r0,:128]!
  1738. veor @XMM[0], @XMM[0], @XMM[ 8]
  1739. veor @XMM[1], @XMM[1], @XMM[ 9]
  1740. veor @XMM[8], @XMM[4], @XMM[10]
  1741. vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
  1742. vst1.8 {@XMM[8]}, [$out]!
  1743. vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
  1744. b .Lxts_enc_done
  1745. .align 4
  1746. .Lxts_enc_2:
  1747. vst1.64 {@XMM[10]}, [r0,:128] @ next round tweak
  1748. veor @XMM[0], @XMM[0], @XMM[8]
  1749. #ifndef BSAES_ASM_EXTENDED_KEY
  1750. add r4, sp, #0x90 @ pass key schedule
  1751. #else
  1752. add r4, $key, #248 @ pass key schedule
  1753. #endif
  1754. veor @XMM[1], @XMM[1], @XMM[9]
  1755. mov r5, $rounds @ pass rounds
  1756. mov r0, sp
  1757. bl _bsaes_encrypt8
  1758. vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
  1759. veor @XMM[0], @XMM[0], @XMM[ 8]
  1760. veor @XMM[1], @XMM[1], @XMM[ 9]
  1761. vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
  1762. vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
  1763. b .Lxts_enc_done
  1764. .align 4
  1765. .Lxts_enc_1:
  1766. mov r0, sp
  1767. veor @XMM[0], @XMM[8]
  1768. mov r1, sp
  1769. vst1.8 {@XMM[0]}, [sp,:128]
  1770. mov r2, $key
  1771. mov r4, $fp @ preserve fp
  1772. bl AES_encrypt
  1773. vld1.8 {@XMM[0]}, [sp,:128]
  1774. veor @XMM[0], @XMM[0], @XMM[8]
  1775. vst1.8 {@XMM[0]}, [$out]!
  1776. mov $fp, r4
  1777. vmov @XMM[8], @XMM[9] @ next round tweak
  1778. .Lxts_enc_done:
  1779. #ifndef XTS_CHAIN_TWEAK
  1780. adds $len, #0x10
  1781. beq .Lxts_enc_ret
  1782. sub r6, $out, #0x10
  1783. .Lxts_enc_steal:
  1784. ldrb r0, [$inp], #1
  1785. ldrb r1, [$out, #-0x10]
  1786. strb r0, [$out, #-0x10]
  1787. strb r1, [$out], #1
  1788. subs $len, #1
  1789. bhi .Lxts_enc_steal
  1790. vld1.8 {@XMM[0]}, [r6]
  1791. mov r0, sp
  1792. veor @XMM[0], @XMM[0], @XMM[8]
  1793. mov r1, sp
  1794. vst1.8 {@XMM[0]}, [sp,:128]
  1795. mov r2, $key
  1796. mov r4, $fp @ preserve fp
  1797. bl AES_encrypt
  1798. vld1.8 {@XMM[0]}, [sp,:128]
  1799. veor @XMM[0], @XMM[0], @XMM[8]
  1800. vst1.8 {@XMM[0]}, [r6]
  1801. mov $fp, r4
  1802. #endif
  1803. .Lxts_enc_ret:
  1804. bic r0, $fp, #0xf
  1805. vmov.i32 q0, #0
  1806. vmov.i32 q1, #0
  1807. #ifdef XTS_CHAIN_TWEAK
  1808. ldr r1, [$fp, #0x20+VFP_ABI_FRAME] @ chain tweak
  1809. #endif
  1810. .Lxts_enc_bzero: @ wipe key schedule [if any]
  1811. vstmia sp!, {q0-q1}
  1812. cmp sp, r0
  1813. bne .Lxts_enc_bzero
  1814. mov sp, $fp
  1815. #ifdef XTS_CHAIN_TWEAK
  1816. vst1.8 {@XMM[8]}, [r1]
  1817. #endif
  1818. VFP_ABI_POP
  1819. ldmia sp!, {r4-r10, pc} @ return
  1820. .size bsaes_xts_encrypt,.-bsaes_xts_encrypt
  1821. .globl bsaes_xts_decrypt
  1822. .hidden bsaes_xts_decrypt
  1823. .type bsaes_xts_decrypt,%function
  1824. .align 4
  1825. bsaes_xts_decrypt:
  1826. mov ip, sp
  1827. stmdb sp!, {r4-r10, lr} @ 0x20
  1828. VFP_ABI_PUSH
  1829. mov r6, sp @ future $fp
  1830. mov $inp, r0
  1831. mov $out, r1
  1832. mov $len, r2
  1833. mov $key, r3
  1834. sub r0, sp, #0x10 @ 0x10
  1835. bic r0, #0xf @ align at 16 bytes
  1836. mov sp, r0
  1837. #ifdef XTS_CHAIN_TWEAK
  1838. ldr r0, [ip] @ pointer to input tweak
  1839. #else
  1840. @ generate initial tweak
  1841. ldr r0, [ip, #4] @ iv[]
  1842. mov r1, sp
  1843. ldr r2, [ip, #0] @ key2
  1844. bl AES_encrypt
  1845. mov r0, sp @ pointer to initial tweak
  1846. #endif
  1847. ldr $rounds, [$key, #240] @ get # of rounds
  1848. mov $fp, r6
  1849. #ifndef BSAES_ASM_EXTENDED_KEY
  1850. @ allocate the key schedule on the stack
  1851. sub r12, sp, $rounds, lsl#7 @ 128 bytes per inner round key
  1852. @ add r12, #`128-32` @ size of bit-sliced key schedule
  1853. sub r12, #`32+16` @ place for tweak[9]
  1854. @ populate the key schedule
  1855. mov r4, $key @ pass key
  1856. mov r5, $rounds @ pass # of rounds
  1857. mov sp, r12
  1858. add r12, #0x90 @ pass key schedule
  1859. bl _bsaes_key_convert
  1860. add r4, sp, #0x90
  1861. vldmia r4, {@XMM[6]}
  1862. vstmia r12, {@XMM[15]} @ save last round key
  1863. veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
  1864. vstmia r4, {@XMM[7]}
  1865. #else
  1866. ldr r12, [$key, #244]
  1867. eors r12, #1
  1868. beq 0f
  1869. str r12, [$key, #244]
  1870. mov r4, $key @ pass key
  1871. mov r5, $rounds @ pass # of rounds
  1872. add r12, $key, #248 @ pass key schedule
  1873. bl _bsaes_key_convert
  1874. add r4, $key, #248
  1875. vldmia r4, {@XMM[6]}
  1876. vstmia r12, {@XMM[15]} @ save last round key
  1877. veor @XMM[7], @XMM[7], @XMM[6] @ fix up round 0 key
  1878. vstmia r4, {@XMM[7]}
  1879. .align 2
  1880. 0: sub sp, #0x90 @ place for tweak[9]
  1881. #endif
  1882. vld1.8 {@XMM[8]}, [r0] @ initial tweak
  1883. adr $magic, .Lxts_magic
  1884. tst $len, #0xf @ if not multiple of 16
  1885. it ne @ Thumb2 thing, sanity check in ARM
  1886. subne $len, #0x10 @ subtract another 16 bytes
  1887. subs $len, #0x80
  1888. blo .Lxts_dec_short
  1889. b .Lxts_dec_loop
  1890. .align 4
  1891. .Lxts_dec_loop:
  1892. vldmia $magic, {$twmask} @ load XTS magic
  1893. vshr.s64 @T[0], @XMM[8], #63
  1894. mov r0, sp
  1895. vand @T[0], @T[0], $twmask
  1896. ___
  1897. for($i=9;$i<16;$i++) {
  1898. $code.=<<___;
  1899. vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
  1900. vst1.64 {@XMM[$i-1]}, [r0,:128]!
  1901. vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
  1902. vshr.s64 @T[1], @XMM[$i], #63
  1903. veor @XMM[$i], @XMM[$i], @T[0]
  1904. vand @T[1], @T[1], $twmask
  1905. ___
  1906. @T=reverse(@T);
  1907. $code.=<<___ if ($i>=10);
  1908. vld1.8 {@XMM[$i-10]}, [$inp]!
  1909. ___
  1910. $code.=<<___ if ($i>=11);
  1911. veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
  1912. ___
  1913. }
  1914. $code.=<<___;
  1915. vadd.u64 @XMM[8], @XMM[15], @XMM[15]
  1916. vst1.64 {@XMM[15]}, [r0,:128]!
  1917. vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
  1918. veor @XMM[8], @XMM[8], @T[0]
  1919. vst1.64 {@XMM[8]}, [r0,:128] @ next round tweak
  1920. vld1.8 {@XMM[6]-@XMM[7]}, [$inp]!
  1921. veor @XMM[5], @XMM[5], @XMM[13]
  1922. #ifndef BSAES_ASM_EXTENDED_KEY
  1923. add r4, sp, #0x90 @ pass key schedule
  1924. #else
  1925. add r4, $key, #248 @ pass key schedule
  1926. #endif
  1927. veor @XMM[6], @XMM[6], @XMM[14]
  1928. mov r5, $rounds @ pass rounds
  1929. veor @XMM[7], @XMM[7], @XMM[15]
  1930. mov r0, sp
  1931. bl _bsaes_decrypt8
  1932. vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
  1933. vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
  1934. veor @XMM[0], @XMM[0], @XMM[ 8]
  1935. vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
  1936. veor @XMM[1], @XMM[1], @XMM[ 9]
  1937. veor @XMM[8], @XMM[6], @XMM[10]
  1938. vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
  1939. veor @XMM[9], @XMM[4], @XMM[11]
  1940. vld1.64 {@XMM[14]-@XMM[15]}, [r0,:128]!
  1941. veor @XMM[10], @XMM[2], @XMM[12]
  1942. vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
  1943. veor @XMM[11], @XMM[7], @XMM[13]
  1944. veor @XMM[12], @XMM[3], @XMM[14]
  1945. vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
  1946. veor @XMM[13], @XMM[5], @XMM[15]
  1947. vst1.8 {@XMM[12]-@XMM[13]}, [$out]!
  1948. vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
  1949. subs $len, #0x80
  1950. bpl .Lxts_dec_loop
  1951. .Lxts_dec_short:
  1952. adds $len, #0x70
  1953. bmi .Lxts_dec_done
  1954. vldmia $magic, {$twmask} @ load XTS magic
  1955. vshr.s64 @T[0], @XMM[8], #63
  1956. mov r0, sp
  1957. vand @T[0], @T[0], $twmask
  1958. ___
  1959. for($i=9;$i<16;$i++) {
  1960. $code.=<<___;
  1961. vadd.u64 @XMM[$i], @XMM[$i-1], @XMM[$i-1]
  1962. vst1.64 {@XMM[$i-1]}, [r0,:128]!
  1963. vswp `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
  1964. vshr.s64 @T[1], @XMM[$i], #63
  1965. veor @XMM[$i], @XMM[$i], @T[0]
  1966. vand @T[1], @T[1], $twmask
  1967. ___
  1968. @T=reverse(@T);
  1969. $code.=<<___ if ($i>=10);
  1970. vld1.8 {@XMM[$i-10]}, [$inp]!
  1971. subs $len, #0x10
  1972. bmi .Lxts_dec_`$i-9`
  1973. ___
  1974. $code.=<<___ if ($i>=11);
  1975. veor @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
  1976. ___
  1977. }
  1978. $code.=<<___;
  1979. sub $len, #0x10
  1980. vst1.64 {@XMM[15]}, [r0,:128] @ next round tweak
  1981. vld1.8 {@XMM[6]}, [$inp]!
  1982. veor @XMM[5], @XMM[5], @XMM[13]
  1983. #ifndef BSAES_ASM_EXTENDED_KEY
  1984. add r4, sp, #0x90 @ pass key schedule
  1985. #else
  1986. add r4, $key, #248 @ pass key schedule
  1987. #endif
  1988. veor @XMM[6], @XMM[6], @XMM[14]
  1989. mov r5, $rounds @ pass rounds
  1990. mov r0, sp
  1991. bl _bsaes_decrypt8
  1992. vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
  1993. vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
  1994. veor @XMM[0], @XMM[0], @XMM[ 8]
  1995. vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
  1996. veor @XMM[1], @XMM[1], @XMM[ 9]
  1997. veor @XMM[8], @XMM[6], @XMM[10]
  1998. vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
  1999. veor @XMM[9], @XMM[4], @XMM[11]
  2000. vld1.64 {@XMM[14]}, [r0,:128]!
  2001. veor @XMM[10], @XMM[2], @XMM[12]
  2002. vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
  2003. veor @XMM[11], @XMM[7], @XMM[13]
  2004. veor @XMM[12], @XMM[3], @XMM[14]
  2005. vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
  2006. vst1.8 {@XMM[12]}, [$out]!
  2007. vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
  2008. b .Lxts_dec_done
  2009. .align 4
  2010. .Lxts_dec_6:
  2011. vst1.64 {@XMM[14]}, [r0,:128] @ next round tweak
  2012. veor @XMM[4], @XMM[4], @XMM[12]
  2013. #ifndef BSAES_ASM_EXTENDED_KEY
  2014. add r4, sp, #0x90 @ pass key schedule
  2015. #else
  2016. add r4, $key, #248 @ pass key schedule
  2017. #endif
  2018. veor @XMM[5], @XMM[5], @XMM[13]
  2019. mov r5, $rounds @ pass rounds
  2020. mov r0, sp
  2021. bl _bsaes_decrypt8
  2022. vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
  2023. vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
  2024. veor @XMM[0], @XMM[0], @XMM[ 8]
  2025. vld1.64 {@XMM[12]-@XMM[13]}, [r0,:128]!
  2026. veor @XMM[1], @XMM[1], @XMM[ 9]
  2027. veor @XMM[8], @XMM[6], @XMM[10]
  2028. vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
  2029. veor @XMM[9], @XMM[4], @XMM[11]
  2030. veor @XMM[10], @XMM[2], @XMM[12]
  2031. vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
  2032. veor @XMM[11], @XMM[7], @XMM[13]
  2033. vst1.8 {@XMM[10]-@XMM[11]}, [$out]!
  2034. vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
  2035. b .Lxts_dec_done
  2036. .align 4
  2037. .Lxts_dec_5:
  2038. vst1.64 {@XMM[13]}, [r0,:128] @ next round tweak
  2039. veor @XMM[3], @XMM[3], @XMM[11]
  2040. #ifndef BSAES_ASM_EXTENDED_KEY
  2041. add r4, sp, #0x90 @ pass key schedule
  2042. #else
  2043. add r4, $key, #248 @ pass key schedule
  2044. #endif
  2045. veor @XMM[4], @XMM[4], @XMM[12]
  2046. mov r5, $rounds @ pass rounds
  2047. mov r0, sp
  2048. bl _bsaes_decrypt8
  2049. vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
  2050. vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
  2051. veor @XMM[0], @XMM[0], @XMM[ 8]
  2052. vld1.64 {@XMM[12]}, [r0,:128]!
  2053. veor @XMM[1], @XMM[1], @XMM[ 9]
  2054. veor @XMM[8], @XMM[6], @XMM[10]
  2055. vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
  2056. veor @XMM[9], @XMM[4], @XMM[11]
  2057. veor @XMM[10], @XMM[2], @XMM[12]
  2058. vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
  2059. vst1.8 {@XMM[10]}, [$out]!
  2060. vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
  2061. b .Lxts_dec_done
  2062. .align 4
  2063. .Lxts_dec_4:
  2064. vst1.64 {@XMM[12]}, [r0,:128] @ next round tweak
  2065. veor @XMM[2], @XMM[2], @XMM[10]
  2066. #ifndef BSAES_ASM_EXTENDED_KEY
  2067. add r4, sp, #0x90 @ pass key schedule
  2068. #else
  2069. add r4, $key, #248 @ pass key schedule
  2070. #endif
  2071. veor @XMM[3], @XMM[3], @XMM[11]
  2072. mov r5, $rounds @ pass rounds
  2073. mov r0, sp
  2074. bl _bsaes_decrypt8
  2075. vld1.64 {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
  2076. vld1.64 {@XMM[10]-@XMM[11]}, [r0,:128]!
  2077. veor @XMM[0], @XMM[0], @XMM[ 8]
  2078. veor @XMM[1], @XMM[1], @XMM[ 9]
  2079. veor @XMM[8], @XMM[6], @XMM[10]
  2080. vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
  2081. veor @XMM[9], @XMM[4], @XMM[11]
  2082. vst1.8 {@XMM[8]-@XMM[9]}, [$out]!
  2083. vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
  2084. b .Lxts_dec_done
  2085. .align 4
  2086. .Lxts_dec_3:
  2087. vst1.64 {@XMM[11]}, [r0,:128] @ next round tweak
  2088. veor @XMM[1], @XMM[1], @XMM[9]
  2089. #ifndef BSAES_ASM_EXTENDED_KEY
  2090. add r4, sp, #0x90 @ pass key schedule
  2091. #else
  2092. add r4, $key, #248 @ pass key schedule
  2093. #endif
  2094. veor @XMM[2], @XMM[2], @XMM[10]
  2095. mov r5, $rounds @ pass rounds
  2096. mov r0, sp
  2097. bl _bsaes_decrypt8
  2098. vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
  2099. vld1.64 {@XMM[10]}, [r0,:128]!
  2100. veor @XMM[0], @XMM[0], @XMM[ 8]
  2101. veor @XMM[1], @XMM[1], @XMM[ 9]
  2102. veor @XMM[8], @XMM[6], @XMM[10]
  2103. vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
  2104. vst1.8 {@XMM[8]}, [$out]!
  2105. vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
  2106. b .Lxts_dec_done
  2107. .align 4
  2108. .Lxts_dec_2:
  2109. vst1.64 {@XMM[10]}, [r0,:128] @ next round tweak
  2110. veor @XMM[0], @XMM[0], @XMM[8]
  2111. #ifndef BSAES_ASM_EXTENDED_KEY
  2112. add r4, sp, #0x90 @ pass key schedule
  2113. #else
  2114. add r4, $key, #248 @ pass key schedule
  2115. #endif
  2116. veor @XMM[1], @XMM[1], @XMM[9]
  2117. mov r5, $rounds @ pass rounds
  2118. mov r0, sp
  2119. bl _bsaes_decrypt8
  2120. vld1.64 {@XMM[8]-@XMM[9]}, [r0,:128]!
  2121. veor @XMM[0], @XMM[0], @XMM[ 8]
  2122. veor @XMM[1], @XMM[1], @XMM[ 9]
  2123. vst1.8 {@XMM[0]-@XMM[1]}, [$out]!
  2124. vld1.64 {@XMM[8]}, [r0,:128] @ next round tweak
  2125. b .Lxts_dec_done
  2126. .align 4
  2127. .Lxts_dec_1:
  2128. mov r0, sp
  2129. veor @XMM[0], @XMM[8]
  2130. mov r1, sp
  2131. vst1.8 {@XMM[0]}, [sp,:128]
  2132. mov r2, $key
  2133. mov r4, $fp @ preserve fp
  2134. mov r5, $magic @ preserve magic
  2135. bl AES_decrypt
  2136. vld1.8 {@XMM[0]}, [sp,:128]
  2137. veor @XMM[0], @XMM[0], @XMM[8]
  2138. vst1.8 {@XMM[0]}, [$out]!
  2139. mov $fp, r4
  2140. mov $magic, r5
  2141. vmov @XMM[8], @XMM[9] @ next round tweak
  2142. .Lxts_dec_done:
  2143. #ifndef XTS_CHAIN_TWEAK
  2144. adds $len, #0x10
  2145. beq .Lxts_dec_ret
  2146. @ calculate one round of extra tweak for the stolen ciphertext
  2147. vldmia $magic, {$twmask}
  2148. vshr.s64 @XMM[6], @XMM[8], #63
  2149. vand @XMM[6], @XMM[6], $twmask
  2150. vadd.u64 @XMM[9], @XMM[8], @XMM[8]
  2151. vswp `&Dhi("@XMM[6]")`,`&Dlo("@XMM[6]")`
  2152. veor @XMM[9], @XMM[9], @XMM[6]
  2153. @ perform the final decryption with the last tweak value
  2154. vld1.8 {@XMM[0]}, [$inp]!
  2155. mov r0, sp
  2156. veor @XMM[0], @XMM[0], @XMM[9]
  2157. mov r1, sp
  2158. vst1.8 {@XMM[0]}, [sp,:128]
  2159. mov r2, $key
  2160. mov r4, $fp @ preserve fp
  2161. bl AES_decrypt
  2162. vld1.8 {@XMM[0]}, [sp,:128]
  2163. veor @XMM[0], @XMM[0], @XMM[9]
  2164. vst1.8 {@XMM[0]}, [$out]
  2165. mov r6, $out
  2166. .Lxts_dec_steal:
  2167. ldrb r1, [$out]
  2168. ldrb r0, [$inp], #1
  2169. strb r1, [$out, #0x10]
  2170. strb r0, [$out], #1
  2171. subs $len, #1
  2172. bhi .Lxts_dec_steal
  2173. vld1.8 {@XMM[0]}, [r6]
  2174. mov r0, sp
  2175. veor @XMM[0], @XMM[8]
  2176. mov r1, sp
  2177. vst1.8 {@XMM[0]}, [sp,:128]
  2178. mov r2, $key
  2179. bl AES_decrypt
  2180. vld1.8 {@XMM[0]}, [sp,:128]
  2181. veor @XMM[0], @XMM[0], @XMM[8]
  2182. vst1.8 {@XMM[0]}, [r6]
  2183. mov $fp, r4
  2184. #endif
  2185. .Lxts_dec_ret:
  2186. bic r0, $fp, #0xf
  2187. vmov.i32 q0, #0
  2188. vmov.i32 q1, #0
  2189. #ifdef XTS_CHAIN_TWEAK
  2190. ldr r1, [$fp, #0x20+VFP_ABI_FRAME] @ chain tweak
  2191. #endif
  2192. .Lxts_dec_bzero: @ wipe key schedule [if any]
  2193. vstmia sp!, {q0-q1}
  2194. cmp sp, r0
  2195. bne .Lxts_dec_bzero
  2196. mov sp, $fp
  2197. #ifdef XTS_CHAIN_TWEAK
  2198. vst1.8 {@XMM[8]}, [r1]
  2199. #endif
  2200. VFP_ABI_POP
  2201. ldmia sp!, {r4-r10, pc} @ return
  2202. .size bsaes_xts_decrypt,.-bsaes_xts_decrypt
  2203. ___
  2204. }
  2205. $code.=<<___;
  2206. #endif
  2207. #endif
  2208. ___
  2209. $code =~ s/\`([^\`]*)\`/eval($1)/gem;
  2210. open SELF,$0;
  2211. while(<SELF>) {
  2212. next if (/^#!/);
  2213. last if (!s/^#/@/ and !/^$/);
  2214. print;
  2215. }
  2216. close SELF;
  2217. print $code;
  2218. close STDOUT;