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 # ====================================================================
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...
22 # Note about "528B" variant. In ARM case it makes lesser sense to
23 # implement it for following reasons:
25 # - performance improvement won't be anywhere near 50%, because 128-
26 # bit shift operation is neatly fused with 128-bit xor here, and
27 # "538B" variant would eliminate only 4-5 instructions out of 32
28 # in the inner loop (meaning that estimated improvement is ~15%);
29 # - ARM-based systems are often embedded ones and extra memory
30 # consumption might be unappreciated (for so little improvement);
32 # Byte order [in]dependence. =========================================
34 # Caller is expected to maintain specific *dword* order in Htable,
35 # namely with *least* significant dword of 128-bit value at *lower*
36 # address. This differs completely from C code and has everything to
37 # do with ldm instruction and order in which dwords are "consumed" by
38 # algorithm. *Byte* order within these dwords in turn is whatever
39 # *native* byte order on current platform. See gcm128.c for working
42 while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {}
43 open STDOUT,">$output";
45 $Xi="r0"; # argument block
49 $Zll="r4"; # variables
58 ################# r13 is stack pointer
60 ################# r15 is program counter
62 $rem_4bit=$inp; # used in gcm_gmult_4bit
68 for ($Zll,$Zlh,$Zhl,$Zhh) {
69 # can be reduced to single "str $_,[$Xi,$i]" on big-endian platforms
79 $code.="\t".shift(@args)."\n";
88 .type rem_4bit,%object
91 .short 0x0000,0x1C20,0x3840,0x2460
92 .short 0x7080,0x6CA0,0x48C0,0x54E0
93 .short 0xE100,0xFD20,0xD940,0xC560
94 .short 0x9180,0x8DA0,0xA9C0,0xB5E0
95 .size rem_4bit,.-rem_4bit
97 .type rem_4bit_get,%function
100 sub $rem_4bit,$rem_4bit,#32 @ &rem_4bit
103 .size rem_4bit_get,.-rem_4bit_get
105 .global gcm_ghash_4bit
106 .type gcm_ghash_4bit,%function
109 add $len,$inp,$len @ $len to point at the end
110 stmdb sp!,{r3-r11,lr} @ save $len/end too
111 sub r12,r12,#48 @ &rem_4bit
113 ldmia r12,{r4-r11} @ copy rem_4bit ...
114 stmdb sp!,{r4-r11} @ ... to stack
124 add $Zhh,$Htbl,$nlo,lsl#4
125 ldmia $Zhh,{$Zll-$Zhh} @ load Htbl[nlo]
129 and $nhi,$Zll,#0xf @ rem
130 ldmia $Thh,{$Tll-$Thh} @ load Htbl[nhi]
132 eor $Zll,$Tll,$Zll,lsr#4
133 ldrh $Tll,[sp,$nhi] @ rem_4bit[rem]
134 eor $Zll,$Zll,$Zlh,lsl#28
136 eor $Zlh,$Tlh,$Zlh,lsr#4
137 eor $Zlh,$Zlh,$Zhl,lsl#28
138 eor $Zhl,$Thl,$Zhl,lsr#4
139 eor $Zhl,$Zhl,$Zhh,lsl#28
140 eor $Zhh,$Thh,$Zhh,lsr#4
142 eor $Zhh,$Zhh,$Tll,lsl#16
147 add $Thh,$Htbl,$nlo,lsl#4
149 ldmia $Thh,{$Tll-$Thh} @ load Htbl[nlo]
150 and $nlo,$Zll,#0xf @ rem
152 eor $Zll,$Tll,$Zll,lsr#4
153 ldrh $Tll,[sp,$nlo] @ rem_4bit[rem]
154 eor $Zll,$Zll,$Zlh,lsl#28
155 eor $Zlh,$Tlh,$Zlh,lsr#4
156 eor $Zlh,$Zlh,$Zhl,lsl#28
157 eor $Zhl,$Thl,$Zhl,lsr#4
158 eor $Zhl,$Zhl,$Zhh,lsl#28
159 eor $Zhh,$Thh,$Zhh,lsr#4
160 ldrplb $nlo,[$inp,$cnt]
163 eor $Zhh,$Zhh,$Tll,lsl#16 @ ^= rem_4bit[rem]
164 ldmia $Thh,{$Tll-$Thh} @ load Htbl[nhi]
165 and $nhi,$Zll,#0xf @ rem
167 eor $Zll,$Tll,$Zll,lsr#4
168 ldrh $Tll,[sp,$nhi] @ rem_4bit[rem]
169 eor $Zll,$Zll,$Zlh,lsl#28
170 ldrplb $nhi,[$Xi,$cnt]
171 eor $Zlh,$Tlh,$Zlh,lsr#4
172 eor $Zlh,$Zlh,$Zhl,lsl#28
173 eor $Zhl,$Thl,$Zhl,lsr#4
174 eor $Zhl,$Zhl,$Zhh,lsl#28
175 eor $Zhh,$Thh,$Zhh,lsr#4
177 eor $Zhh,$Zhh,$Tll,lsl#16 @ ^= rem_4bit[rem]
178 andpl $nhi,$nlo,#0xf0
179 andpl $nlo,$nlo,#0x0f
182 ldr $len,[sp,#32] @ re-load $len/end
186 &Zsmash("cmp\t$inp,$len","ldrneb\t$nlo,[$inp,#15]");
191 ldmia sp!,{r4-r11,lr}
193 moveq pc,lr @ be binary compatible with V4, yet
194 bx lr @ interoperable with Thumb ISA:-)
195 .size gcm_ghash_4bit,.-gcm_ghash_4bit
197 .global gcm_gmult_4bit
198 .type gcm_gmult_4bit,%function
200 stmdb sp!,{r4-r11,lr}
208 add $Zhh,$Htbl,$nlo,lsl#4
209 ldmia $Zhh,{$Zll-$Zhh} @ load Htbl[nlo]
213 and $nhi,$Zll,#0xf @ rem
214 ldmia $Thh,{$Tll-$Thh} @ load Htbl[nhi]
216 eor $Zll,$Tll,$Zll,lsr#4
217 ldrh $Tll,[$rem_4bit,$nhi] @ rem_4bit[rem]
218 eor $Zll,$Zll,$Zlh,lsl#28
219 eor $Zlh,$Tlh,$Zlh,lsr#4
220 eor $Zlh,$Zlh,$Zhl,lsl#28
221 eor $Zhl,$Thl,$Zhl,lsr#4
222 eor $Zhl,$Zhl,$Zhh,lsl#28
223 eor $Zhh,$Thh,$Zhh,lsr#4
225 eor $Zhh,$Zhh,$Tll,lsl#16
229 add $Thh,$Htbl,$nlo,lsl#4
231 ldmia $Thh,{$Tll-$Thh} @ load Htbl[nlo]
232 and $nlo,$Zll,#0xf @ rem
234 eor $Zll,$Tll,$Zll,lsr#4
235 ldrh $Tll,[$rem_4bit,$nlo] @ rem_4bit[rem]
236 eor $Zll,$Zll,$Zlh,lsl#28
237 eor $Zlh,$Tlh,$Zlh,lsr#4
238 eor $Zlh,$Zlh,$Zhl,lsl#28
239 eor $Zhl,$Thl,$Zhl,lsr#4
240 eor $Zhl,$Zhl,$Zhh,lsl#28
241 eor $Zhh,$Thh,$Zhh,lsr#4
242 ldrplb $nlo,[$Xi,$cnt]
245 eor $Zhh,$Zhh,$Tll,lsl#16 @ ^= rem_4bit[rem]
246 ldmia $Thh,{$Tll-$Thh} @ load Htbl[nhi]
247 and $nhi,$Zll,#0xf @ rem
249 eor $Zll,$Tll,$Zll,lsr#4
250 ldrh $Tll,[$rem_4bit,$nhi] @ rem_4bit[rem]
251 eor $Zll,$Zll,$Zlh,lsl#28
252 eor $Zlh,$Tlh,$Zlh,lsr#4
253 eor $Zlh,$Zlh,$Zhl,lsl#28
254 eor $Zhl,$Thl,$Zhl,lsr#4
255 eor $Zhl,$Zhl,$Zhh,lsl#28
256 eor $Zhh,$Thh,$Zhh,lsr#4
257 andpl $nhi,$nlo,#0xf0
258 eor $Zhh,$Zhh,$Tll,lsl#16 @ ^= rem_4bit[rem]
259 andpl $nlo,$nlo,#0x0f
264 ldmia sp!,{r4-r11,lr}
266 moveq pc,lr @ be binary compatible with V4, yet
267 bx lr @ interoperable with Thumb ISA:-)
268 .size gcm_gmult_4bit,.-gcm_gmult_4bit
269 .asciz "GHASH for ARMv4, CRYPTOGAMS by <appro\@openssl.org>"
273 $code =~ s/\`([^\`]*)\`/eval $1/gem;
274 $code =~ s/\bbx\s+lr\b/.word\t0xe12fff1e/gm; # make it possible to compile with -march=armv4
276 close STDOUT; # enforce flush