You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

502 lines
13 KiB

  1. #!/usr/bin/env perl
  2. #
  3. # ====================================================================
  4. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  5. # project. The module is, however, dual licensed under OpenSSL and
  6. # CRYPTOGAMS licenses depending on where you obtain it. For further
  7. # details see http://www.openssl.org/~appro/cryptogams/.
  8. # ====================================================================
  9. #
  10. # April 2010
  11. #
  12. # The module implements "4-bit" GCM GHASH function and underlying
  13. # single multiplication operation in GF(2^128). "4-bit" means that it
  14. # uses 256 bytes per-key table [+32 bytes shared table]. There is no
  15. # experimental performance data available yet. The only approximation
  16. # that can be made at this point is based on code size. Inner loop is
  17. # 32 instructions long and on single-issue core should execute in <40
  18. # cycles. Having verified that gcc 3.4 didn't unroll corresponding
  19. # loop, this assembler loop body was found to be ~3x smaller than
  20. # compiler-generated one...
  21. #
  22. # July 2010
  23. #
  24. # Rescheduling for dual-issue pipeline resulted in 8.5% improvement on
  25. # Cortex A8 core and ~25 cycles per processed byte (which was observed
  26. # to be ~3 times faster than gcc-generated code:-)
  27. #
  28. # February 2011
  29. #
  30. # Profiler-assisted and platform-specific optimization resulted in 7%
  31. # improvement on Cortex A8 core and ~23.5 cycles per byte.
  32. #
  33. # March 2011
  34. #
  35. # Add NEON implementation featuring polynomial multiplication, i.e. no
  36. # lookup tables involved. On Cortex A8 it was measured to process one
  37. # byte in 15 cycles or 55% faster than integer-only code.
  38. #
  39. # April 2014
  40. #
  41. # Switch to multiplication algorithm suggested in paper referred
  42. # below and combine it with reduction algorithm from x86 module.
  43. # Performance improvement over previous version varies from 65% on
  44. # Snapdragon S4 to 110% on Cortex A9. In absolute terms Cortex A8
  45. # processes one byte in 8.45 cycles, A9 - in 10.2, Snapdragon S4 -
  46. # in 9.33.
  47. #
  48. # Câmara, D.; Gouvêa, C. P. L.; López, J. & Dahab, R.: Fast Software
  49. # Polynomial Multiplication on ARM Processors using the NEON Engine.
  50. #
  51. # http://conradoplg.cryptoland.net/files/2010/12/mocrysen13.pdf
  52. # ====================================================================
  53. # Note about "528B" variant. In ARM case it makes lesser sense to
  54. # implement it for following reasons:
  55. #
  56. # - performance improvement won't be anywhere near 50%, because 128-
  57. # bit shift operation is neatly fused with 128-bit xor here, and
  58. # "538B" variant would eliminate only 4-5 instructions out of 32
  59. # in the inner loop (meaning that estimated improvement is ~15%);
  60. # - ARM-based systems are often embedded ones and extra memory
  61. # consumption might be unappreciated (for so little improvement);
  62. #
  63. # Byte order [in]dependence. =========================================
  64. #
  65. # Caller is expected to maintain specific *dword* order in Htable,
  66. # namely with *least* significant dword of 128-bit value at *lower*
  67. # address. This differs completely from C code and has everything to
  68. # do with ldm instruction and order in which dwords are "consumed" by
  69. # algorithm. *Byte* order within these dwords in turn is whatever
  70. # *native* byte order on current platform. See gcm128.c for working
  71. # example...
  72. while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {}
  73. open STDOUT,">$output";
  74. $Xi="r0"; # argument block
  75. $Htbl="r1";
  76. $inp="r2";
  77. $len="r3";
  78. $Zll="r4"; # variables
  79. $Zlh="r5";
  80. $Zhl="r6";
  81. $Zhh="r7";
  82. $Tll="r8";
  83. $Tlh="r9";
  84. $Thl="r10";
  85. $Thh="r11";
  86. $nlo="r12";
  87. ################# r13 is stack pointer
  88. $nhi="r14";
  89. ################# r15 is program counter
  90. $rem_4bit=$inp; # used in gcm_gmult_4bit
  91. $cnt=$len;
  92. sub Zsmash() {
  93. my $i=12;
  94. my @args=@_;
  95. for ($Zll,$Zlh,$Zhl,$Zhh) {
  96. $code.=<<___;
  97. #if __ARM_ARCH__>=7 && defined(__ARMEL__)
  98. rev $_,$_
  99. str $_,[$Xi,#$i]
  100. #elif defined(__ARMEB__)
  101. str $_,[$Xi,#$i]
  102. #else
  103. mov $Tlh,$_,lsr#8
  104. strb $_,[$Xi,#$i+3]
  105. mov $Thl,$_,lsr#16
  106. strb $Tlh,[$Xi,#$i+2]
  107. mov $Thh,$_,lsr#24
  108. strb $Thl,[$Xi,#$i+1]
  109. strb $Thh,[$Xi,#$i]
  110. #endif
  111. ___
  112. $code.="\t".shift(@args)."\n";
  113. $i-=4;
  114. }
  115. }
  116. $code=<<___;
  117. #if defined(__arm__)
  118. #include "arm_arch.h"
  119. .syntax unified
  120. .text
  121. .code 32
  122. .type rem_4bit,%object
  123. .align 5
  124. rem_4bit:
  125. .short 0x0000,0x1C20,0x3840,0x2460
  126. .short 0x7080,0x6CA0,0x48C0,0x54E0
  127. .short 0xE100,0xFD20,0xD940,0xC560
  128. .short 0x9180,0x8DA0,0xA9C0,0xB5E0
  129. .size rem_4bit,.-rem_4bit
  130. .type rem_4bit_get,%function
  131. rem_4bit_get:
  132. sub $rem_4bit,pc,#8
  133. sub $rem_4bit,$rem_4bit,#32 @ &rem_4bit
  134. b .Lrem_4bit_got
  135. nop
  136. .size rem_4bit_get,.-rem_4bit_get
  137. .global gcm_ghash_4bit
  138. .hidden gcm_ghash_4bit
  139. .type gcm_ghash_4bit,%function
  140. gcm_ghash_4bit:
  141. sub r12,pc,#8
  142. add $len,$inp,$len @ $len to point at the end
  143. stmdb sp!,{r3-r11,lr} @ save $len/end too
  144. sub r12,r12,#48 @ &rem_4bit
  145. ldmia r12,{r4-r11} @ copy rem_4bit ...
  146. stmdb sp!,{r4-r11} @ ... to stack
  147. ldrb $nlo,[$inp,#15]
  148. ldrb $nhi,[$Xi,#15]
  149. .Louter:
  150. eor $nlo,$nlo,$nhi
  151. and $nhi,$nlo,#0xf0
  152. and $nlo,$nlo,#0x0f
  153. mov $cnt,#14
  154. add $Zhh,$Htbl,$nlo,lsl#4
  155. ldmia $Zhh,{$Zll-$Zhh} @ load Htbl[nlo]
  156. add $Thh,$Htbl,$nhi
  157. ldrb $nlo,[$inp,#14]
  158. and $nhi,$Zll,#0xf @ rem
  159. ldmia $Thh,{$Tll-$Thh} @ load Htbl[nhi]
  160. add $nhi,$nhi,$nhi
  161. eor $Zll,$Tll,$Zll,lsr#4
  162. ldrh $Tll,[sp,$nhi] @ rem_4bit[rem]
  163. eor $Zll,$Zll,$Zlh,lsl#28
  164. ldrb $nhi,[$Xi,#14]
  165. eor $Zlh,$Tlh,$Zlh,lsr#4
  166. eor $Zlh,$Zlh,$Zhl,lsl#28
  167. eor $Zhl,$Thl,$Zhl,lsr#4
  168. eor $Zhl,$Zhl,$Zhh,lsl#28
  169. eor $Zhh,$Thh,$Zhh,lsr#4
  170. eor $nlo,$nlo,$nhi
  171. and $nhi,$nlo,#0xf0
  172. and $nlo,$nlo,#0x0f
  173. eor $Zhh,$Zhh,$Tll,lsl#16
  174. .Linner:
  175. add $Thh,$Htbl,$nlo,lsl#4
  176. and $nlo,$Zll,#0xf @ rem
  177. subs $cnt,$cnt,#1
  178. add $nlo,$nlo,$nlo
  179. ldmia $Thh,{$Tll-$Thh} @ load Htbl[nlo]
  180. eor $Zll,$Tll,$Zll,lsr#4
  181. eor $Zll,$Zll,$Zlh,lsl#28
  182. eor $Zlh,$Tlh,$Zlh,lsr#4
  183. eor $Zlh,$Zlh,$Zhl,lsl#28
  184. ldrh $Tll,[sp,$nlo] @ rem_4bit[rem]
  185. eor $Zhl,$Thl,$Zhl,lsr#4
  186. ldrbpl $nlo,[$inp,$cnt]
  187. eor $Zhl,$Zhl,$Zhh,lsl#28
  188. eor $Zhh,$Thh,$Zhh,lsr#4
  189. add $Thh,$Htbl,$nhi
  190. and $nhi,$Zll,#0xf @ rem
  191. eor $Zhh,$Zhh,$Tll,lsl#16 @ ^= rem_4bit[rem]
  192. add $nhi,$nhi,$nhi
  193. ldmia $Thh,{$Tll-$Thh} @ load Htbl[nhi]
  194. eor $Zll,$Tll,$Zll,lsr#4
  195. ldrbpl $Tll,[$Xi,$cnt]
  196. eor $Zll,$Zll,$Zlh,lsl#28
  197. eor $Zlh,$Tlh,$Zlh,lsr#4
  198. ldrh $Tlh,[sp,$nhi]
  199. eor $Zlh,$Zlh,$Zhl,lsl#28
  200. eor $Zhl,$Thl,$Zhl,lsr#4
  201. eor $Zhl,$Zhl,$Zhh,lsl#28
  202. eorpl $nlo,$nlo,$Tll
  203. eor $Zhh,$Thh,$Zhh,lsr#4
  204. andpl $nhi,$nlo,#0xf0
  205. andpl $nlo,$nlo,#0x0f
  206. eor $Zhh,$Zhh,$Tlh,lsl#16 @ ^= rem_4bit[rem]
  207. bpl .Linner
  208. ldr $len,[sp,#32] @ re-load $len/end
  209. add $inp,$inp,#16
  210. mov $nhi,$Zll
  211. ___
  212. &Zsmash("cmp\t$inp,$len","ldrbne\t$nlo,[$inp,#15]");
  213. $code.=<<___;
  214. bne .Louter
  215. add sp,sp,#36
  216. #if __ARM_ARCH__>=5
  217. ldmia sp!,{r4-r11,pc}
  218. #else
  219. ldmia sp!,{r4-r11,lr}
  220. tst lr,#1
  221. moveq pc,lr @ be binary compatible with V4, yet
  222. bx lr @ interoperable with Thumb ISA:-)
  223. #endif
  224. .size gcm_ghash_4bit,.-gcm_ghash_4bit
  225. .global gcm_gmult_4bit
  226. .hidden gcm_gmult_4bit
  227. .type gcm_gmult_4bit,%function
  228. gcm_gmult_4bit:
  229. stmdb sp!,{r4-r11,lr}
  230. ldrb $nlo,[$Xi,#15]
  231. b rem_4bit_get
  232. .Lrem_4bit_got:
  233. and $nhi,$nlo,#0xf0
  234. and $nlo,$nlo,#0x0f
  235. mov $cnt,#14
  236. add $Zhh,$Htbl,$nlo,lsl#4
  237. ldmia $Zhh,{$Zll-$Zhh} @ load Htbl[nlo]
  238. ldrb $nlo,[$Xi,#14]
  239. add $Thh,$Htbl,$nhi
  240. and $nhi,$Zll,#0xf @ rem
  241. ldmia $Thh,{$Tll-$Thh} @ load Htbl[nhi]
  242. add $nhi,$nhi,$nhi
  243. eor $Zll,$Tll,$Zll,lsr#4
  244. ldrh $Tll,[$rem_4bit,$nhi] @ rem_4bit[rem]
  245. eor $Zll,$Zll,$Zlh,lsl#28
  246. eor $Zlh,$Tlh,$Zlh,lsr#4
  247. eor $Zlh,$Zlh,$Zhl,lsl#28
  248. eor $Zhl,$Thl,$Zhl,lsr#4
  249. eor $Zhl,$Zhl,$Zhh,lsl#28
  250. eor $Zhh,$Thh,$Zhh,lsr#4
  251. and $nhi,$nlo,#0xf0
  252. eor $Zhh,$Zhh,$Tll,lsl#16
  253. and $nlo,$nlo,#0x0f
  254. .Loop:
  255. add $Thh,$Htbl,$nlo,lsl#4
  256. and $nlo,$Zll,#0xf @ rem
  257. subs $cnt,$cnt,#1
  258. add $nlo,$nlo,$nlo
  259. ldmia $Thh,{$Tll-$Thh} @ load Htbl[nlo]
  260. eor $Zll,$Tll,$Zll,lsr#4
  261. eor $Zll,$Zll,$Zlh,lsl#28
  262. eor $Zlh,$Tlh,$Zlh,lsr#4
  263. eor $Zlh,$Zlh,$Zhl,lsl#28
  264. ldrh $Tll,[$rem_4bit,$nlo] @ rem_4bit[rem]
  265. eor $Zhl,$Thl,$Zhl,lsr#4
  266. ldrbpl $nlo,[$Xi,$cnt]
  267. eor $Zhl,$Zhl,$Zhh,lsl#28
  268. eor $Zhh,$Thh,$Zhh,lsr#4
  269. add $Thh,$Htbl,$nhi
  270. and $nhi,$Zll,#0xf @ rem
  271. eor $Zhh,$Zhh,$Tll,lsl#16 @ ^= rem_4bit[rem]
  272. add $nhi,$nhi,$nhi
  273. ldmia $Thh,{$Tll-$Thh} @ load Htbl[nhi]
  274. eor $Zll,$Tll,$Zll,lsr#4
  275. eor $Zll,$Zll,$Zlh,lsl#28
  276. eor $Zlh,$Tlh,$Zlh,lsr#4
  277. ldrh $Tll,[$rem_4bit,$nhi] @ rem_4bit[rem]
  278. eor $Zlh,$Zlh,$Zhl,lsl#28
  279. eor $Zhl,$Thl,$Zhl,lsr#4
  280. eor $Zhl,$Zhl,$Zhh,lsl#28
  281. eor $Zhh,$Thh,$Zhh,lsr#4
  282. andpl $nhi,$nlo,#0xf0
  283. andpl $nlo,$nlo,#0x0f
  284. eor $Zhh,$Zhh,$Tll,lsl#16 @ ^= rem_4bit[rem]
  285. bpl .Loop
  286. ___
  287. &Zsmash();
  288. $code.=<<___;
  289. #if __ARM_ARCH__>=5
  290. ldmia sp!,{r4-r11,pc}
  291. #else
  292. ldmia sp!,{r4-r11,lr}
  293. tst lr,#1
  294. moveq pc,lr @ be binary compatible with V4, yet
  295. bx lr @ interoperable with Thumb ISA:-)
  296. #endif
  297. .size gcm_gmult_4bit,.-gcm_gmult_4bit
  298. ___
  299. {
  300. my ($Xl,$Xm,$Xh,$IN)=map("q$_",(0..3));
  301. my ($t0,$t1,$t2,$t3)=map("q$_",(8..12));
  302. my ($Hlo,$Hhi,$Hhl,$k48,$k32,$k16)=map("d$_",(26..31));
  303. sub clmul64x64 {
  304. my ($r,$a,$b)=@_;
  305. $code.=<<___;
  306. vext.8 $t0#lo, $a, $a, #1 @ A1
  307. vmull.p8 $t0, $t0#lo, $b @ F = A1*B
  308. vext.8 $r#lo, $b, $b, #1 @ B1
  309. vmull.p8 $r, $a, $r#lo @ E = A*B1
  310. vext.8 $t1#lo, $a, $a, #2 @ A2
  311. vmull.p8 $t1, $t1#lo, $b @ H = A2*B
  312. vext.8 $t3#lo, $b, $b, #2 @ B2
  313. vmull.p8 $t3, $a, $t3#lo @ G = A*B2
  314. vext.8 $t2#lo, $a, $a, #3 @ A3
  315. veor $t0, $t0, $r @ L = E + F
  316. vmull.p8 $t2, $t2#lo, $b @ J = A3*B
  317. vext.8 $r#lo, $b, $b, #3 @ B3
  318. veor $t1, $t1, $t3 @ M = G + H
  319. vmull.p8 $r, $a, $r#lo @ I = A*B3
  320. veor $t0#lo, $t0#lo, $t0#hi @ t0 = (L) (P0 + P1) << 8
  321. vand $t0#hi, $t0#hi, $k48
  322. vext.8 $t3#lo, $b, $b, #4 @ B4
  323. veor $t1#lo, $t1#lo, $t1#hi @ t1 = (M) (P2 + P3) << 16
  324. vand $t1#hi, $t1#hi, $k32
  325. vmull.p8 $t3, $a, $t3#lo @ K = A*B4
  326. veor $t2, $t2, $r @ N = I + J
  327. veor $t0#lo, $t0#lo, $t0#hi
  328. veor $t1#lo, $t1#lo, $t1#hi
  329. veor $t2#lo, $t2#lo, $t2#hi @ t2 = (N) (P4 + P5) << 24
  330. vand $t2#hi, $t2#hi, $k16
  331. vext.8 $t0, $t0, $t0, #15
  332. veor $t3#lo, $t3#lo, $t3#hi @ t3 = (K) (P6 + P7) << 32
  333. vmov.i64 $t3#hi, #0
  334. vext.8 $t1, $t1, $t1, #14
  335. veor $t2#lo, $t2#lo, $t2#hi
  336. vmull.p8 $r, $a, $b @ D = A*B
  337. vext.8 $t3, $t3, $t3, #12
  338. vext.8 $t2, $t2, $t2, #13
  339. veor $t0, $t0, $t1
  340. veor $t2, $t2, $t3
  341. veor $r, $r, $t0
  342. veor $r, $r, $t2
  343. ___
  344. }
  345. $code.=<<___;
  346. #if __ARM_ARCH__>=7
  347. .fpu neon
  348. .global gcm_init_neon
  349. .hidden gcm_init_neon
  350. .type gcm_init_neon,%function
  351. .align 4
  352. gcm_init_neon:
  353. vld1.64 $IN#hi,[r1,:64]! @ load H
  354. vmov.i8 $t0,#0xe1
  355. vld1.64 $IN#lo,[r1,:64]
  356. vshl.i64 $t0#hi,#57
  357. vshr.u64 $t0#lo,#63 @ t0=0xc2....01
  358. vdup.8 $t1,$IN#hi[7]
  359. vshr.u64 $Hlo,$IN#lo,#63
  360. vshr.s8 $t1,#7 @ broadcast carry bit
  361. vshl.i64 $IN,$IN,#1
  362. vand $t0,$t0,$t1
  363. vorr $IN#hi,$Hlo @ H<<<=1
  364. veor $IN,$IN,$t0 @ twisted H
  365. vstmia r0,{$IN}
  366. bx lr
  367. .size gcm_init_neon,.-gcm_init_neon
  368. .global gcm_gmult_neon
  369. .hidden gcm_gmult_neon
  370. .type gcm_gmult_neon,%function
  371. .align 4
  372. gcm_gmult_neon:
  373. vld1.64 $IN#hi,[$Xi,:64]! @ load Xi
  374. vld1.64 $IN#lo,[$Xi,:64]!
  375. vmov.i64 $k48,#0x0000ffffffffffff
  376. vldmia $Htbl,{$Hlo-$Hhi} @ load twisted H
  377. vmov.i64 $k32,#0x00000000ffffffff
  378. #ifdef __ARMEL__
  379. vrev64.8 $IN,$IN
  380. #endif
  381. vmov.i64 $k16,#0x000000000000ffff
  382. veor $Hhl,$Hlo,$Hhi @ Karatsuba pre-processing
  383. mov $len,#16
  384. b .Lgmult_neon
  385. .size gcm_gmult_neon,.-gcm_gmult_neon
  386. .global gcm_ghash_neon
  387. .hidden gcm_ghash_neon
  388. .type gcm_ghash_neon,%function
  389. .align 4
  390. gcm_ghash_neon:
  391. vld1.64 $Xl#hi,[$Xi,:64]! @ load Xi
  392. vld1.64 $Xl#lo,[$Xi,:64]!
  393. vmov.i64 $k48,#0x0000ffffffffffff
  394. vldmia $Htbl,{$Hlo-$Hhi} @ load twisted H
  395. vmov.i64 $k32,#0x00000000ffffffff
  396. #ifdef __ARMEL__
  397. vrev64.8 $Xl,$Xl
  398. #endif
  399. vmov.i64 $k16,#0x000000000000ffff
  400. veor $Hhl,$Hlo,$Hhi @ Karatsuba pre-processing
  401. .Loop_neon:
  402. vld1.64 $IN#hi,[$inp]! @ load inp
  403. vld1.64 $IN#lo,[$inp]!
  404. #ifdef __ARMEL__
  405. vrev64.8 $IN,$IN
  406. #endif
  407. veor $IN,$Xl @ inp^=Xi
  408. .Lgmult_neon:
  409. ___
  410. &clmul64x64 ($Xl,$Hlo,"$IN#lo"); # H.lo·Xi.lo
  411. $code.=<<___;
  412. veor $IN#lo,$IN#lo,$IN#hi @ Karatsuba pre-processing
  413. ___
  414. &clmul64x64 ($Xm,$Hhl,"$IN#lo"); # (H.lo+H.hi)·(Xi.lo+Xi.hi)
  415. &clmul64x64 ($Xh,$Hhi,"$IN#hi"); # H.hi·Xi.hi
  416. $code.=<<___;
  417. veor $Xm,$Xm,$Xl @ Karatsuba post-processing
  418. veor $Xm,$Xm,$Xh
  419. veor $Xl#hi,$Xl#hi,$Xm#lo
  420. veor $Xh#lo,$Xh#lo,$Xm#hi @ Xh|Xl - 256-bit result
  421. @ equivalent of reduction_avx from ghash-x86_64.pl
  422. vshl.i64 $t1,$Xl,#57 @ 1st phase
  423. vshl.i64 $t2,$Xl,#62
  424. veor $t2,$t2,$t1 @
  425. vshl.i64 $t1,$Xl,#63
  426. veor $t2, $t2, $t1 @
  427. veor $Xl#hi,$Xl#hi,$t2#lo @
  428. veor $Xh#lo,$Xh#lo,$t2#hi
  429. vshr.u64 $t2,$Xl,#1 @ 2nd phase
  430. veor $Xh,$Xh,$Xl
  431. veor $Xl,$Xl,$t2 @
  432. vshr.u64 $t2,$t2,#6
  433. vshr.u64 $Xl,$Xl,#1 @
  434. veor $Xl,$Xl,$Xh @
  435. veor $Xl,$Xl,$t2 @
  436. subs $len,#16
  437. bne .Loop_neon
  438. #ifdef __ARMEL__
  439. vrev64.8 $Xl,$Xl
  440. #endif
  441. sub $Xi,#16
  442. vst1.64 $Xl#hi,[$Xi,:64]! @ write out Xi
  443. vst1.64 $Xl#lo,[$Xi,:64]
  444. bx lr
  445. .size gcm_ghash_neon,.-gcm_ghash_neon
  446. #endif
  447. ___
  448. }
  449. $code.=<<___;
  450. .asciz "GHASH for ARMv4/NEON, CRYPTOGAMS by <appro\@openssl.org>"
  451. .align 2
  452. #endif
  453. ___
  454. foreach (split("\n",$code)) {
  455. s/\`([^\`]*)\`/eval $1/geo;
  456. s/\bq([0-9]+)#(lo|hi)/sprintf "d%d",2*$1+($2 eq "hi")/geo or
  457. s/\bbx\s+lr\b/.word\t0xe12fff1e/go; # make it possible to compile with -march=armv4
  458. print $_,"\n";
  459. }
  460. close STDOUT; # enforce flush