Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/camellia/asm/cmll-x86.pl

raw
1 #! /usr/bin/env perl
2 # Copyright 2008-2025 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9
10 # ====================================================================
11 # Copyright (c) 2008 Andy Polyakov <https://github.com/dot-asm>
12 #
13 # This module may be used under the terms of either the GNU General
14 # Public License version 2 or later, the GNU Lesser General Public
15 # License version 2.1 or later, the Mozilla Public License version
16 # 1.1 or the BSD License. The exact terms of either license are
17 # distributed along with this module.
18 # ====================================================================
19
20 # Performance in cycles per processed byte (less is better) in
21 # 'openssl speed ...' benchmark:
22 #
23 # AMD K8 Core2 PIII P4
24 # -evp camellia-128-ecb 21.5 22.8 27.0 28.9
25 # + over gcc 3.4.6 +90/11% +70/10% +53/4% +160/64%
26 # + over icc 8.0 +48/19% +21/15% +21/17% +55/37%
27 #
28 # camellia-128-cbc 17.3 21.1 23.9 25.9
29 #
30 # 128-bit key setup 196 280 256 240 cycles/key
31 # + over gcc 3.4.6 +30/0% +17/11% +11/0% +63/40%
32 # + over icc 8.0 +18/3% +10/0% +10/3% +21/10%
33 #
34 # Pairs of numbers in "+" rows represent performance improvement over
35 # compiler generated position-independent code, PIC, and non-PIC
36 # respectively. PIC results are of greater relevance, as this module
37 # is position-independent, i.e. suitable for a shared library or PIE.
38 # Position independence "costs" one register, which is why compilers
39 # are so close with non-PIC results, they have an extra register to
40 # spare. CBC results are better than ECB ones thanks to "zero-copy"
41 # private _x86_* interface, and are ~30-40% better than with compiler
42 # generated cmll_cbc.o, and reach ~80-90% of x86_64 performance on
43 # same CPU (where applicable).
44
45 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
46 push(@INC,"${dir}","${dir}../../perlasm");
47 require "x86asm.pl";
48
49 $OPENSSL=1;
50
51 $output = pop and open STDOUT,">$output";
52
53 &asm_init($ARGV[0],$ARGV[$#ARGV] eq "386");
54
55 @T=("eax","ebx","ecx","edx");
56 $idx="esi";
57 $key="edi";
58 $Tbl="ebp";
59
60 # stack frame layout in _x86_Camellia_* routines, frame is allocated
61 # by caller
62 $__ra=&DWP(0,"esp"); # return address
63 $__s0=&DWP(4,"esp"); # s0 backing store
64 $__s1=&DWP(8,"esp"); # s1 backing store
65 $__s2=&DWP(12,"esp"); # s2 backing store
66 $__s3=&DWP(16,"esp"); # s3 backing store
67 $__end=&DWP(20,"esp"); # pointer to end/start of key schedule
68
69 # stack frame layout in Camellia_[en|crypt] routines, which differs from
70 # above by 4 and overlaps by pointer to end/start of key schedule
71 $_end=&DWP(16,"esp");
72 $_esp=&DWP(20,"esp");
73
74 # const unsigned int Camellia_SBOX[4][256];
75 # Well, sort of... Camellia_SBOX[0][] is interleaved with [1][],
76 # and [2][] - with [3][]. This is done to optimize code size.
77 $SBOX1_1110=0; # Camellia_SBOX[0]
78 $SBOX4_4404=4; # Camellia_SBOX[1]
79 $SBOX2_0222=2048; # Camellia_SBOX[2]
80 $SBOX3_3033=2052; # Camellia_SBOX[3]
81 &static_label("Camellia_SIGMA");
82 &static_label("Camellia_SBOX");
83
84 sub Camellia_Feistel {
85 my $i=@_[0];
86 my $seed=defined(@_[1])?@_[1]:0;
87 my $scale=$seed<0?-8:8;
88 my $frame=defined(@_[2])?@_[2]:0;
89 my $j=($i&1)*2;
90 my $t0=@T[($j)%4],$t1=@T[($j+1)%4],$t2=@T[($j+2)%4],$t3=@T[($j+3)%4];
91
92 &xor ($t0,$idx); # t0^=key[0]
93 &xor ($t1,&DWP($seed+$i*$scale+4,$key)); # t1^=key[1]
94 &movz ($idx,&HB($t0)); # (t0>>8)&0xff
95 &mov ($t3,&DWP($SBOX3_3033,$Tbl,$idx,8)); # t3=SBOX3_3033[0]
96 &movz ($idx,&LB($t0)); # (t0>>0)&0xff
97 &xor ($t3,&DWP($SBOX4_4404,$Tbl,$idx,8)); # t3^=SBOX4_4404[0]
98 &shr ($t0,16);
99 &movz ($idx,&LB($t1)); # (t1>>0)&0xff
100 &mov ($t2,&DWP($SBOX1_1110,$Tbl,$idx,8)); # t2=SBOX1_1110[1]
101 &movz ($idx,&HB($t0)); # (t0>>24)&0xff
102 &xor ($t3,&DWP($SBOX1_1110,$Tbl,$idx,8)); # t3^=SBOX1_1110[0]
103 &movz ($idx,&HB($t1)); # (t1>>8)&0xff
104 &xor ($t2,&DWP($SBOX4_4404,$Tbl,$idx,8)); # t2^=SBOX4_4404[1]
105 &shr ($t1,16);
106 &movz ($t0,&LB($t0)); # (t0>>16)&0xff
107 &xor ($t3,&DWP($SBOX2_0222,$Tbl,$t0,8)); # t3^=SBOX2_0222[0]
108 &movz ($idx,&HB($t1)); # (t1>>24)&0xff
109 &mov ($t0,&DWP($frame+4*(($j+3)%4),"esp")); # prefetch "s3"
110 &xor ($t2,$t3); # t2^=t3
111 &rotr ($t3,8); # t3=RightRotate(t3,8)
112 &xor ($t2,&DWP($SBOX2_0222,$Tbl,$idx,8)); # t2^=SBOX2_0222[1]
113 &movz ($idx,&LB($t1)); # (t1>>16)&0xff
114 &mov ($t1,&DWP($frame+4*(($j+2)%4),"esp")); # prefetch "s2"
115 &xor ($t3,$t0); # t3^=s3
116 &xor ($t2,&DWP($SBOX3_3033,$Tbl,$idx,8)); # t2^=SBOX3_3033[1]
117 &mov ($idx,&DWP($seed+($i+1)*$scale,$key)); # prefetch key[i+1]
118 &xor ($t3,$t2); # t3^=t2
119 &mov (&DWP($frame+4*(($j+3)%4),"esp"),$t3); # s3=t3
120 &xor ($t2,$t1); # t2^=s2
121 &mov (&DWP($frame+4*(($j+2)%4),"esp"),$t2); # s2=t2
122 }
123
124 # void Camellia_EncryptBlock_Rounds(
125 # int grandRounds,
126 # const Byte plaintext[],
127 # const KEY_TABLE_TYPE keyTable,
128 # Byte ciphertext[])
129 &function_begin("Camellia_EncryptBlock_Rounds");
130 &mov ("eax",&wparam(0)); # load grandRounds
131 &mov ($idx,&wparam(1)); # load plaintext pointer
132 &mov ($key,&wparam(2)); # load key schedule pointer
133
134 &mov ("ebx","esp");
135 &sub ("esp",7*4); # place for s[0-3],keyEnd,esp and ra
136 &and ("esp",-64);
137
138 # place stack frame just "above mod 1024" the key schedule
139 # this ensures that cache associativity of 2 suffices
140 &lea ("ecx",&DWP(-64-63,$key));
141 &sub ("ecx","esp");
142 &neg ("ecx");
143 &and ("ecx",0x3C0); # modulo 1024, but aligned to cache-line
144 &sub ("esp","ecx");
145 &add ("esp",4); # 4 is reserved for callee's return address
146
147 &shl ("eax",6);
148 &lea ("eax",&DWP(0,$key,"eax"));
149 &mov ($_esp,"ebx"); # save %esp
150 &mov ($_end,"eax"); # save keyEnd
151
152 &call (&label("pic_point"));
153 &set_label("pic_point");
154 &blindpop($Tbl);
155 &lea ($Tbl,&DWP(&label("Camellia_SBOX")."-".&label("pic_point"),$Tbl));
156
157 &mov (@T[0],&DWP(0,$idx)); # load plaintext
158 &mov (@T[1],&DWP(4,$idx));
159 &mov (@T[2],&DWP(8,$idx));
160 &bswap (@T[0]);
161 &mov (@T[3],&DWP(12,$idx));
162 &bswap (@T[1]);
163 &bswap (@T[2]);
164 &bswap (@T[3]);
165
166 &call ("_x86_Camellia_encrypt");
167
168 &mov ("esp",$_esp);
169 &bswap (@T[0]);
170 &mov ($idx,&wparam(3)); # load ciphertext pointer
171 &bswap (@T[1]);
172 &bswap (@T[2]);
173 &bswap (@T[3]);
174 &mov (&DWP(0,$idx),@T[0]); # write ciphertext
175 &mov (&DWP(4,$idx),@T[1]);
176 &mov (&DWP(8,$idx),@T[2]);
177 &mov (&DWP(12,$idx),@T[3]);
178 &function_end("Camellia_EncryptBlock_Rounds");
179 # V1.x API
180 &function_begin_B("Camellia_EncryptBlock");
181 &mov ("eax",128);
182 &sub ("eax",&wparam(0)); # load keyBitLength
183 &mov ("eax",3);
184 &adc ("eax",0); # keyBitLength==128?3:4
185 &mov (&wparam(0),"eax");
186 &jmp (&label("Camellia_EncryptBlock_Rounds"));
187 &function_end_B("Camellia_EncryptBlock");
188
189 if ($OPENSSL) {
190 # void Camellia_encrypt(
191 # const unsigned char *in,
192 # unsigned char *out,
193 # const CAMELLIA_KEY *key)
194 &function_begin("Camellia_encrypt");
195 &mov ($idx,&wparam(0)); # load plaintext pointer
196 &mov ($key,&wparam(2)); # load key schedule pointer
197
198 &mov ("ebx","esp");
199 &sub ("esp",7*4); # place for s[0-3],keyEnd,esp and ra
200 &and ("esp",-64);
201 &mov ("eax",&DWP(272,$key)); # load grandRounds counter
202
203 # place stack frame just "above mod 1024" the key schedule
204 # this ensures that cache associativity of 2 suffices
205 &lea ("ecx",&DWP(-64-63,$key));
206 &sub ("ecx","esp");
207 &neg ("ecx");
208 &and ("ecx",0x3C0); # modulo 1024, but aligned to cache-line
209 &sub ("esp","ecx");
210 &add ("esp",4); # 4 is reserved for callee's return address
211
212 &shl ("eax",6);
213 &lea ("eax",&DWP(0,$key,"eax"));
214 &mov ($_esp,"ebx"); # save %esp
215 &mov ($_end,"eax"); # save keyEnd
216
217 &call (&label("pic_point"));
218 &set_label("pic_point");
219 &blindpop($Tbl);
220 &lea ($Tbl,&DWP(&label("Camellia_SBOX")."-".&label("pic_point"),$Tbl));
221
222 &mov (@T[0],&DWP(0,$idx)); # load plaintext
223 &mov (@T[1],&DWP(4,$idx));
224 &mov (@T[2],&DWP(8,$idx));
225 &bswap (@T[0]);
226 &mov (@T[3],&DWP(12,$idx));
227 &bswap (@T[1]);
228 &bswap (@T[2]);
229 &bswap (@T[3]);
230
231 &call ("_x86_Camellia_encrypt");
232
233 &mov ("esp",$_esp);
234 &bswap (@T[0]);
235 &mov ($idx,&wparam(1)); # load ciphertext pointer
236 &bswap (@T[1]);
237 &bswap (@T[2]);
238 &bswap (@T[3]);
239 &mov (&DWP(0,$idx),@T[0]); # write ciphertext
240 &mov (&DWP(4,$idx),@T[1]);
241 &mov (&DWP(8,$idx),@T[2]);
242 &mov (&DWP(12,$idx),@T[3]);
243 &function_end("Camellia_encrypt");
244 }
245
246 &function_begin_B("_x86_Camellia_encrypt");
247 &xor (@T[0],&DWP(0,$key)); # ^=key[0-3]
248 &xor (@T[1],&DWP(4,$key));
249 &xor (@T[2],&DWP(8,$key));
250 &xor (@T[3],&DWP(12,$key));
251 &mov ($idx,&DWP(16,$key)); # prefetch key[4]
252
253 &mov ($__s0,@T[0]); # save s[0-3]
254 &mov ($__s1,@T[1]);
255 &mov ($__s2,@T[2]);
256 &mov ($__s3,@T[3]);
257
258 &set_label("loop",16);
259 for ($i=0;$i<6;$i++) { Camellia_Feistel($i,16,4); }
260
261 &add ($key,16*4);
262 &cmp ($key,$__end);
263 &je (&label("done"));
264
265 # @T[0-1] are preloaded, $idx is preloaded with key[0]
266 &and ($idx,@T[0]);
267 &mov (@T[3],$__s3);
268 &rotl ($idx,1);
269 &mov (@T[2],@T[3]);
270 &xor (@T[1],$idx);
271 &or (@T[2],&DWP(12,$key));
272 &mov ($__s1,@T[1]); # s1^=LeftRotate(s0&key[0],1);
273 &xor (@T[2],$__s2);
274
275 &mov ($idx,&DWP(4,$key));
276 &mov ($__s2,@T[2]); # s2^=s3|key[3];
277 &or ($idx,@T[1]);
278 &and (@T[2],&DWP(8,$key));
279 &xor (@T[0],$idx);
280 &rotl (@T[2],1);
281 &mov ($__s0,@T[0]); # s0^=s1|key[1];
282 &xor (@T[3],@T[2]);
283 &mov ($idx,&DWP(16,$key)); # prefetch key[4]
284 &mov ($__s3,@T[3]); # s3^=LeftRotate(s2&key[2],1);
285 &jmp (&label("loop"));
286
287 &set_label("done",8);
288 &mov (@T[2],@T[0]); # SwapHalf
289 &mov (@T[3],@T[1]);
290 &mov (@T[0],$__s2);
291 &mov (@T[1],$__s3);
292 &xor (@T[0],$idx); # $idx is preloaded with key[0]
293 &xor (@T[1],&DWP(4,$key));
294 &xor (@T[2],&DWP(8,$key));
295 &xor (@T[3],&DWP(12,$key));
296 &ret ();
297 &function_end_B("_x86_Camellia_encrypt");
298
299 # void Camellia_DecryptBlock_Rounds(
300 # int grandRounds,
301 # const Byte ciphertext[],
302 # const KEY_TABLE_TYPE keyTable,
303 # Byte plaintext[])
304 &function_begin("Camellia_DecryptBlock_Rounds");
305 &mov ("eax",&wparam(0)); # load grandRounds
306 &mov ($idx,&wparam(1)); # load ciphertext pointer
307 &mov ($key,&wparam(2)); # load key schedule pointer
308
309 &mov ("ebx","esp");
310 &sub ("esp",7*4); # place for s[0-3],keyEnd,esp and ra
311 &and ("esp",-64);
312
313 # place stack frame just "above mod 1024" the key schedule
314 # this ensures that cache associativity of 2 suffices
315 &lea ("ecx",&DWP(-64-63,$key));
316 &sub ("ecx","esp");
317 &neg ("ecx");
318 &and ("ecx",0x3C0); # modulo 1024, but aligned to cache-line
319 &sub ("esp","ecx");
320 &add ("esp",4); # 4 is reserved for callee's return address
321
322 &shl ("eax",6);
323 &mov (&DWP(4*4,"esp"),$key); # save keyStart
324 &lea ($key,&DWP(0,$key,"eax"));
325 &mov (&DWP(5*4,"esp"),"ebx");# save %esp
326
327 &call (&label("pic_point"));
328 &set_label("pic_point");
329 &blindpop($Tbl);
330 &lea ($Tbl,&DWP(&label("Camellia_SBOX")."-".&label("pic_point"),$Tbl));
331
332 &mov (@T[0],&DWP(0,$idx)); # load ciphertext
333 &mov (@T[1],&DWP(4,$idx));
334 &mov (@T[2],&DWP(8,$idx));
335 &bswap (@T[0]);
336 &mov (@T[3],&DWP(12,$idx));
337 &bswap (@T[1]);
338 &bswap (@T[2]);
339 &bswap (@T[3]);
340
341 &call ("_x86_Camellia_decrypt");
342
343 &mov ("esp",&DWP(5*4,"esp"));
344 &bswap (@T[0]);
345 &mov ($idx,&wparam(3)); # load plaintext pointer
346 &bswap (@T[1]);
347 &bswap (@T[2]);
348 &bswap (@T[3]);
349 &mov (&DWP(0,$idx),@T[0]); # write plaintext
350 &mov (&DWP(4,$idx),@T[1]);
351 &mov (&DWP(8,$idx),@T[2]);
352 &mov (&DWP(12,$idx),@T[3]);
353 &function_end("Camellia_DecryptBlock_Rounds");
354 # V1.x API
355 &function_begin_B("Camellia_DecryptBlock");
356 &mov ("eax",128);
357 &sub ("eax",&wparam(0)); # load keyBitLength
358 &mov ("eax",3);
359 &adc ("eax",0); # keyBitLength==128?3:4
360 &mov (&wparam(0),"eax");
361 &jmp (&label("Camellia_DecryptBlock_Rounds"));
362 &function_end_B("Camellia_DecryptBlock");
363
364 if ($OPENSSL) {
365 # void Camellia_decrypt(
366 # const unsigned char *in,
367 # unsigned char *out,
368 # const CAMELLIA_KEY *key)
369 &function_begin("Camellia_decrypt");
370 &mov ($idx,&wparam(0)); # load ciphertext pointer
371 &mov ($key,&wparam(2)); # load key schedule pointer
372
373 &mov ("ebx","esp");
374 &sub ("esp",7*4); # place for s[0-3],keyEnd,esp and ra
375 &and ("esp",-64);
376 &mov ("eax",&DWP(272,$key)); # load grandRounds counter
377
378 # place stack frame just "above mod 1024" the key schedule
379 # this ensures that cache associativity of 2 suffices
380 &lea ("ecx",&DWP(-64-63,$key));
381 &sub ("ecx","esp");
382 &neg ("ecx");
383 &and ("ecx",0x3C0); # modulo 1024, but aligned to cache-line
384 &sub ("esp","ecx");
385 &add ("esp",4); # 4 is reserved for callee's return address
386
387 &shl ("eax",6);
388 &mov (&DWP(4*4,"esp"),$key); # save keyStart
389 &lea ($key,&DWP(0,$key,"eax"));
390 &mov (&DWP(5*4,"esp"),"ebx");# save %esp
391
392 &call (&label("pic_point"));
393 &set_label("pic_point");
394 &blindpop($Tbl);
395 &lea ($Tbl,&DWP(&label("Camellia_SBOX")."-".&label("pic_point"),$Tbl));
396
397 &mov (@T[0],&DWP(0,$idx)); # load ciphertext
398 &mov (@T[1],&DWP(4,$idx));
399 &mov (@T[2],&DWP(8,$idx));
400 &bswap (@T[0]);
401 &mov (@T[3],&DWP(12,$idx));
402 &bswap (@T[1]);
403 &bswap (@T[2]);
404 &bswap (@T[3]);
405
406 &call ("_x86_Camellia_decrypt");
407
408 &mov ("esp",&DWP(5*4,"esp"));
409 &bswap (@T[0]);
410 &mov ($idx,&wparam(1)); # load plaintext pointer
411 &bswap (@T[1]);
412 &bswap (@T[2]);
413 &bswap (@T[3]);
414 &mov (&DWP(0,$idx),@T[0]); # write plaintext
415 &mov (&DWP(4,$idx),@T[1]);
416 &mov (&DWP(8,$idx),@T[2]);
417 &mov (&DWP(12,$idx),@T[3]);
418 &function_end("Camellia_decrypt");
419 }
420
421 &function_begin_B("_x86_Camellia_decrypt");
422 &xor (@T[0],&DWP(0,$key)); # ^=key[0-3]
423 &xor (@T[1],&DWP(4,$key));
424 &xor (@T[2],&DWP(8,$key));
425 &xor (@T[3],&DWP(12,$key));
426 &mov ($idx,&DWP(-8,$key)); # prefetch key[-2]
427
428 &mov ($__s0,@T[0]); # save s[0-3]
429 &mov ($__s1,@T[1]);
430 &mov ($__s2,@T[2]);
431 &mov ($__s3,@T[3]);
432
433 &set_label("loop",16);
434 for ($i=0;$i<6;$i++) { Camellia_Feistel($i,-8,4); }
435
436 &sub ($key,16*4);
437 &cmp ($key,$__end);
438 &je (&label("done"));
439
440 # @T[0-1] are preloaded, $idx is preloaded with key[2]
441 &and ($idx,@T[0]);
442 &mov (@T[3],$__s3);
443 &rotl ($idx,1);
444 &mov (@T[2],@T[3]);
445 &xor (@T[1],$idx);
446 &or (@T[2],&DWP(4,$key));
447 &mov ($__s1,@T[1]); # s1^=LeftRotate(s0&key[0],1);
448 &xor (@T[2],$__s2);
449
450 &mov ($idx,&DWP(12,$key));
451 &mov ($__s2,@T[2]); # s2^=s3|key[3];
452 &or ($idx,@T[1]);
453 &and (@T[2],&DWP(0,$key));
454 &xor (@T[0],$idx);
455 &rotl (@T[2],1);
456 &mov ($__s0,@T[0]); # s0^=s1|key[1];
457 &xor (@T[3],@T[2]);
458 &mov ($idx,&DWP(-8,$key)); # prefetch key[4]
459 &mov ($__s3,@T[3]); # s3^=LeftRotate(s2&key[2],1);
460 &jmp (&label("loop"));
461
462 &set_label("done",8);
463 &mov (@T[2],@T[0]); # SwapHalf
464 &mov (@T[3],@T[1]);
465 &mov (@T[0],$__s2);
466 &mov (@T[1],$__s3);
467 &xor (@T[2],$idx); # $idx is preloaded with key[2]
468 &xor (@T[3],&DWP(12,$key));
469 &xor (@T[0],&DWP(0,$key));
470 &xor (@T[1],&DWP(4,$key));
471 &ret ();
472 &function_end_B("_x86_Camellia_decrypt");
473
474 # shld is very slow on Intel P4 family. Even on AMD it limits
475 # instruction decode rate [because it's VectorPath] and consequently
476 # performance. PIII, PM and Core[2] seem to be the only ones which
477 # execute this code ~7% faster...
478 sub __rotl128 {
479 my ($i0,$i1,$i2,$i3,$rot,$rnd,@T)=@_;
480
481 $rnd *= 2;
482 if ($rot) {
483 &mov ($idx,$i0);
484 &shld ($i0,$i1,$rot);
485 &shld ($i1,$i2,$rot);
486 &shld ($i2,$i3,$rot);
487 &shld ($i3,$idx,$rot);
488 }
489 &mov (&DWP(-128+4*$rnd++,$key),shift(@T)) if ($i0 eq @T[0]);
490 &mov (&DWP(-128+4*$rnd++,$key),shift(@T)) if ($i1 eq @T[0]);
491 &mov (&DWP(-128+4*$rnd++,$key),shift(@T)) if ($i2 eq @T[0]);
492 &mov (&DWP(-128+4*$rnd++,$key),shift(@T)) if ($i3 eq @T[0]);
493 }
494
495 # ... Implementing 128-bit rotate without shld gives >3x performance
496 # improvement on P4, only ~7% degradation on other Intel CPUs and
497 # not worse performance on AMD. This is therefore preferred.
498 sub _rotl128 {
499 my ($i0,$i1,$i2,$i3,$rot,$rnd,@T)=@_;
500
501 $rnd *= 2;
502 if ($rot) {
503 &mov ($Tbl,$i0);
504 &shl ($i0,$rot);
505 &mov ($idx,$i1);
506 &shr ($idx,32-$rot);
507 &shl ($i1,$rot);
508 &or ($i0,$idx);
509 &mov ($idx,$i2);
510 &shl ($i2,$rot);
511 &mov (&DWP(-128+4*$rnd++,$key),shift(@T)) if ($i0 eq @T[0]);
512 &shr ($idx,32-$rot);
513 &or ($i1,$idx);
514 &shr ($Tbl,32-$rot);
515 &mov ($idx,$i3);
516 &shr ($idx,32-$rot);
517 &mov (&DWP(-128+4*$rnd++,$key),shift(@T)) if ($i1 eq @T[0]);
518 &shl ($i3,$rot);
519 &or ($i2,$idx);
520 &or ($i3,$Tbl);
521 &mov (&DWP(-128+4*$rnd++,$key),shift(@T)) if ($i2 eq @T[0]);
522 &mov (&DWP(-128+4*$rnd++,$key),shift(@T)) if ($i3 eq @T[0]);
523 } else {
524 &mov (&DWP(-128+4*$rnd++,$key),shift(@T)) if ($i0 eq @T[0]);
525 &mov (&DWP(-128+4*$rnd++,$key),shift(@T)) if ($i1 eq @T[0]);
526 &mov (&DWP(-128+4*$rnd++,$key),shift(@T)) if ($i2 eq @T[0]);
527 &mov (&DWP(-128+4*$rnd++,$key),shift(@T)) if ($i3 eq @T[0]);
528 }
529 }
530
531 sub _saveround {
532 my ($rnd,$key,@T)=@_;
533 my $bias=int(@T[0])?shift(@T):0;
534
535 &mov (&DWP($bias+$rnd*8+0,$key),@T[0]);
536 &mov (&DWP($bias+$rnd*8+4,$key),@T[1]) if ($#T>=1);
537 &mov (&DWP($bias+$rnd*8+8,$key),@T[2]) if ($#T>=2);
538 &mov (&DWP($bias+$rnd*8+12,$key),@T[3]) if ($#T>=3);
539 }
540
541 sub _loadround {
542 my ($rnd,$key,@T)=@_;
543 my $bias=int(@T[0])?shift(@T):0;
544
545 &mov (@T[0],&DWP($bias+$rnd*8+0,$key));
546 &mov (@T[1],&DWP($bias+$rnd*8+4,$key)) if ($#T>=1);
547 &mov (@T[2],&DWP($bias+$rnd*8+8,$key)) if ($#T>=2);
548 &mov (@T[3],&DWP($bias+$rnd*8+12,$key)) if ($#T>=3);
549 }
550
551 # void Camellia_Ekeygen(
552 # const int keyBitLength,
553 # const Byte *rawKey,
554 # KEY_TABLE_TYPE keyTable)
555 &function_begin("Camellia_Ekeygen");
556 { my $step=0;
557
558 &stack_push(4); # place for s[0-3]
559
560 &mov ($Tbl,&wparam(0)); # load arguments
561 &mov ($idx,&wparam(1));
562 &mov ($key,&wparam(2));
563
564 &mov (@T[0],&DWP(0,$idx)); # load 0-127 bits
565 &mov (@T[1],&DWP(4,$idx));
566 &mov (@T[2],&DWP(8,$idx));
567 &mov (@T[3],&DWP(12,$idx));
568
569 &bswap (@T[0]);
570 &bswap (@T[1]);
571 &bswap (@T[2]);
572 &bswap (@T[3]);
573
574 &_saveround (0,$key,@T); # KL<<<0
575
576 &cmp ($Tbl,128);
577 &je (&label("1st128"));
578
579 &mov (@T[0],&DWP(16,$idx)); # load 128-191 bits
580 &mov (@T[1],&DWP(20,$idx));
581 &cmp ($Tbl,192);
582 &je (&label("1st192"));
583 &mov (@T[2],&DWP(24,$idx)); # load 192-255 bits
584 &mov (@T[3],&DWP(28,$idx));
585 &jmp (&label("1st256"));
586 &set_label("1st192",4);
587 &mov (@T[2],@T[0]);
588 &mov (@T[3],@T[1]);
589 &not (@T[2]);
590 &not (@T[3]);
591 &set_label("1st256",4);
592 &bswap (@T[0]);
593 &bswap (@T[1]);
594 &bswap (@T[2]);
595 &bswap (@T[3]);
596
597 &_saveround (4,$key,@T); # temporary storage for KR!
598
599 &xor (@T[0],&DWP(0*8+0,$key)); # KR^KL
600 &xor (@T[1],&DWP(0*8+4,$key));
601 &xor (@T[2],&DWP(1*8+0,$key));
602 &xor (@T[3],&DWP(1*8+4,$key));
603
604 &set_label("1st128",4);
605 &call (&label("pic_point"));
606 &set_label("pic_point");
607 &blindpop($Tbl);
608 &lea ($Tbl,&DWP(&label("Camellia_SBOX")."-".&label("pic_point"),$Tbl));
609 &lea ($key,&DWP(&label("Camellia_SIGMA")."-".&label("Camellia_SBOX"),$Tbl));
610
611 &mov ($idx,&DWP($step*8,$key)); # prefetch SIGMA[0]
612 &mov (&swtmp(0),@T[0]); # save s[0-3]
613 &mov (&swtmp(1),@T[1]);
614 &mov (&swtmp(2),@T[2]);
615 &mov (&swtmp(3),@T[3]);
616 &Camellia_Feistel($step++);
617 &Camellia_Feistel($step++);
618 &mov (@T[2],&swtmp(2));
619 &mov (@T[3],&swtmp(3));
620
621 &mov ($idx,&wparam(2));
622 &xor (@T[0],&DWP(0*8+0,$idx)); # ^KL
623 &xor (@T[1],&DWP(0*8+4,$idx));
624 &xor (@T[2],&DWP(1*8+0,$idx));
625 &xor (@T[3],&DWP(1*8+4,$idx));
626
627 &mov ($idx,&DWP($step*8,$key)); # prefetch SIGMA[4]
628 &mov (&swtmp(0),@T[0]); # save s[0-3]
629 &mov (&swtmp(1),@T[1]);
630 &mov (&swtmp(2),@T[2]);
631 &mov (&swtmp(3),@T[3]);
632 &Camellia_Feistel($step++);
633 &Camellia_Feistel($step++);
634 &mov (@T[2],&swtmp(2));
635 &mov (@T[3],&swtmp(3));
636
637 &mov ($idx,&wparam(0));
638 &cmp ($idx,128);
639 &jne (&label("2nd256"));
640
641 &mov ($key,&wparam(2));
642 &lea ($key,&DWP(128,$key)); # size optimization
643
644 ####### process KA
645 &_saveround (2,$key,-128,@T); # KA<<<0
646 &_rotl128 (@T,15,6,@T); # KA<<<15
647 &_rotl128 (@T,15,8,@T); # KA<<<(15+15=30)
648 &_rotl128 (@T,15,12,@T[0],@T[1]); # KA<<<(30+15=45)
649 &_rotl128 (@T,15,14,@T); # KA<<<(45+15=60)
650 push (@T,shift(@T)); # rotl128(@T,32);
651 &_rotl128 (@T,2,20,@T); # KA<<<(60+32+2=94)
652 &_rotl128 (@T,17,24,@T); # KA<<<(94+17=111)
653
654 ####### process KL
655 &_loadround (0,$key,-128,@T); # load KL
656 &_rotl128 (@T,15,4,@T); # KL<<<15
657 &_rotl128 (@T,30,10,@T); # KL<<<(15+30=45)
658 &_rotl128 (@T,15,13,@T[2],@T[3]); # KL<<<(45+15=60)
659 &_rotl128 (@T,17,16,@T); # KL<<<(60+17=77)
660 &_rotl128 (@T,17,18,@T); # KL<<<(77+17=94)
661 &_rotl128 (@T,17,22,@T); # KL<<<(94+17=111)
662
663 while (@T[0] ne "eax") # restore order
664 { unshift (@T,pop(@T)); }
665
666 &mov ("eax",3); # 3 grandRounds
667 &jmp (&label("done"));
668
669 &set_label("2nd256",16);
670 &mov ($idx,&wparam(2));
671 &_saveround (6,$idx,@T); # temporary storage for KA!
672
673 &xor (@T[0],&DWP(4*8+0,$idx)); # KA^KR
674 &xor (@T[1],&DWP(4*8+4,$idx));
675 &xor (@T[2],&DWP(5*8+0,$idx));
676 &xor (@T[3],&DWP(5*8+4,$idx));
677
678 &mov ($idx,&DWP($step*8,$key)); # prefetch SIGMA[8]
679 &mov (&swtmp(0),@T[0]); # save s[0-3]
680 &mov (&swtmp(1),@T[1]);
681 &mov (&swtmp(2),@T[2]);
682 &mov (&swtmp(3),@T[3]);
683 &Camellia_Feistel($step++);
684 &Camellia_Feistel($step++);
685 &mov (@T[2],&swtmp(2));
686 &mov (@T[3],&swtmp(3));
687
688 &mov ($key,&wparam(2));
689 &lea ($key,&DWP(128,$key)); # size optimization
690
691 ####### process KB
692 &_saveround (2,$key,-128,@T); # KB<<<0
693 &_rotl128 (@T,30,10,@T); # KB<<<30
694 &_rotl128 (@T,30,20,@T); # KB<<<(30+30=60)
695 push (@T,shift(@T)); # rotl128(@T,32);
696 &_rotl128 (@T,19,32,@T); # KB<<<(60+32+19=111)
697
698 ####### process KR
699 &_loadround (4,$key,-128,@T); # load KR
700 &_rotl128 (@T,15,4,@T); # KR<<<15
701 &_rotl128 (@T,15,8,@T); # KR<<<(15+15=30)
702 &_rotl128 (@T,30,18,@T); # KR<<<(30+30=60)
703 push (@T,shift(@T)); # rotl128(@T,32);
704 &_rotl128 (@T,2,26,@T); # KR<<<(60+32+2=94)
705
706 ####### process KA
707 &_loadround (6,$key,-128,@T); # load KA
708 &_rotl128 (@T,15,6,@T); # KA<<<15
709 &_rotl128 (@T,30,14,@T); # KA<<<(15+30=45)
710 push (@T,shift(@T)); # rotl128(@T,32);
711 &_rotl128 (@T,0,24,@T); # KA<<<(45+32+0=77)
712 &_rotl128 (@T,17,28,@T); # KA<<<(77+17=94)
713
714 ####### process KL
715 &_loadround (0,$key,-128,@T); # load KL
716 push (@T,shift(@T)); # rotl128(@T,32);
717 &_rotl128 (@T,13,12,@T); # KL<<<(32+13=45)
718 &_rotl128 (@T,15,16,@T); # KL<<<(45+15=60)
719 &_rotl128 (@T,17,22,@T); # KL<<<(60+17=77)
720 push (@T,shift(@T)); # rotl128(@T,32);
721 &_rotl128 (@T,2,30,@T); # KL<<<(77+32+2=111)
722
723 while (@T[0] ne "eax") # restore order
724 { unshift (@T,pop(@T)); }
725
726 &mov ("eax",4); # 4 grandRounds
727 &set_label("done");
728 &lea ("edx",&DWP(272-128,$key)); # end of key schedule
729 &stack_pop(4);
730 }
731 &function_end("Camellia_Ekeygen");
732
733 if ($OPENSSL) {
734 # int Camellia_set_key (
735 # const unsigned char *userKey,
736 # int bits,
737 # CAMELLIA_KEY *key)
738 &function_begin_B("Camellia_set_key");
739 &push ("ebx");
740 &mov ("ecx",&wparam(0)); # pull arguments
741 &mov ("ebx",&wparam(1));
742 &mov ("edx",&wparam(2));
743
744 &mov ("eax",-1);
745 &test ("ecx","ecx");
746 &jz (&label("done")); # userKey==NULL?
747 &test ("edx","edx");
748 &jz (&label("done")); # key==NULL?
749
750 &mov ("eax",-2);
751 &cmp ("ebx",256);
752 &je (&label("arg_ok")); # bits==256?
753 &cmp ("ebx",192);
754 &je (&label("arg_ok")); # bits==192?
755 &cmp ("ebx",128);
756 &jne (&label("done")); # bits!=128?
757 &set_label("arg_ok",4);
758
759 &push ("edx"); # push arguments
760 &push ("ecx");
761 &push ("ebx");
762 &call ("Camellia_Ekeygen");
763 &stack_pop(3);
764
765 # eax holds grandRounds and edx points at where to put it
766 &mov (&DWP(0,"edx"),"eax");
767 &xor ("eax","eax");
768 &set_label("done",4);
769 &pop ("ebx");
770 &ret ();
771 &function_end_B("Camellia_set_key");
772 }
773
774 @SBOX=(
775 112,130, 44,236,179, 39,192,229,228,133, 87, 53,234, 12,174, 65,
776 35,239,107,147, 69, 25,165, 33,237, 14, 79, 78, 29,101,146,189,
777 134,184,175,143,124,235, 31,206, 62, 48,220, 95, 94,197, 11, 26,
778 166,225, 57,202,213, 71, 93, 61,217, 1, 90,214, 81, 86,108, 77,
779 139, 13,154,102,251,204,176, 45,116, 18, 43, 32,240,177,132,153,
780 223, 76,203,194, 52,126,118, 5,109,183,169, 49,209, 23, 4,215,
781 20, 88, 58, 97,222, 27, 17, 28, 50, 15,156, 22, 83, 24,242, 34,
782 254, 68,207,178,195,181,122,145, 36, 8,232,168, 96,252,105, 80,
783 170,208,160,125,161,137, 98,151, 84, 91, 30,149,224,255,100,210,
784 16,196, 0, 72,163,247,117,219,138, 3,230,218, 9, 63,221,148,
785 135, 92,131, 2,205, 74,144, 51,115,103,246,243,157,127,191,226,
786 82,155,216, 38,200, 55,198, 59,129,150,111, 75, 19,190, 99, 46,
787 233,121,167,140,159,110,188,142, 41,245,249,182, 47,253,180, 89,
788 120,152, 6,106,231, 70,113,186,212, 37,171, 66,136,162,141,250,
789 114, 7,185, 85,248,238,172, 10, 54, 73, 42,104, 60, 56,241,164,
790 64, 40,211,123,187,201, 67,193, 21,227,173,244,119,199,128,158);
791
792 sub S1110 { my $i=shift; $i=@SBOX[$i]; return $i<<24|$i<<16|$i<<8; }
793 sub S4404 { my $i=shift; $i=($i<<1|$i>>7)&0xff; $i=@SBOX[$i]; return $i<<24|$i<<16|$i; }
794 sub S0222 { my $i=shift; $i=@SBOX[$i]; $i=($i<<1|$i>>7)&0xff; return $i<<16|$i<<8|$i; }
795 sub S3033 { my $i=shift; $i=@SBOX[$i]; $i=($i>>1|$i<<7)&0xff; return $i<<24|$i<<8|$i; }
796
797 &set_label("Camellia_SIGMA",64);
798 &data_word(
799 0xa09e667f, 0x3bcc908b, 0xb67ae858, 0x4caa73b2,
800 0xc6ef372f, 0xe94f82be, 0x54ff53a5, 0xf1d36f1c,
801 0x10e527fa, 0xde682d1d, 0xb05688c2, 0xb3e6c1fd,
802 0, 0, 0, 0);
803 &set_label("Camellia_SBOX",64);
804 # tables are interleaved, remember?
805 for ($i=0;$i<256;$i++) { &data_word(&S1110($i),&S4404($i)); }
806 for ($i=0;$i<256;$i++) { &data_word(&S0222($i),&S3033($i)); }
807
808 # void Camellia_cbc_encrypt (const void char *inp, unsigned char *out,
809 # size_t length, const CAMELLIA_KEY *key,
810 # unsigned char *ivp,const int enc);
811 {
812 # stack frame layout
813 # -4(%esp) # return address 0(%esp)
814 # 0(%esp) # s0 4(%esp)
815 # 4(%esp) # s1 8(%esp)
816 # 8(%esp) # s2 12(%esp)
817 # 12(%esp) # s3 16(%esp)
818 # 16(%esp) # end of key schedule 20(%esp)
819 # 20(%esp) # %esp backup
820 my $_inp=&DWP(24,"esp"); #copy of wparam(0)
821 my $_out=&DWP(28,"esp"); #copy of wparam(1)
822 my $_len=&DWP(32,"esp"); #copy of wparam(2)
823 my $_key=&DWP(36,"esp"); #copy of wparam(3)
824 my $_ivp=&DWP(40,"esp"); #copy of wparam(4)
825 my $ivec=&DWP(44,"esp"); #ivec[16]
826 my $_tmp=&DWP(44,"esp"); #volatile variable [yes, aliases with ivec]
827 my ($s0,$s1,$s2,$s3) = @T;
828
829 &function_begin("Camellia_cbc_encrypt");
830 &mov ($s2 eq "ecx"? $s2 : "",&wparam(2)); # load len
831 &cmp ($s2,0);
832 &je (&label("enc_out"));
833
834 &pushf ();
835 &cld ();
836
837 &mov ($s0,&wparam(0)); # load inp
838 &mov ($s1,&wparam(1)); # load out
839 #&mov ($s2,&wparam(2)); # load len
840 &mov ($s3,&wparam(3)); # load key
841 &mov ($Tbl,&wparam(4)); # load ivp
842
843 # allocate aligned stack frame...
844 &lea ($idx,&DWP(-64,"esp"));
845 &and ($idx,-64);
846
847 # place stack frame just "above mod 1024" the key schedule
848 # this ensures that cache associativity of 2 suffices
849 &lea ($key,&DWP(-64-63,$s3));
850 &sub ($key,$idx);
851 &neg ($key);
852 &and ($key,0x3C0); # modulo 1024, but aligned to cache-line
853 &sub ($idx,$key);
854
855 &mov ($key,&wparam(5)); # load enc
856
857 &exch ("esp",$idx);
858 &add ("esp",4); # reserve for return address!
859 &mov ($_esp,$idx); # save %esp
860
861 &mov ($_inp,$s0); # save copy of inp
862 &mov ($_out,$s1); # save copy of out
863 &mov ($_len,$s2); # save copy of len
864 &mov ($_key,$s3); # save copy of key
865 &mov ($_ivp,$Tbl); # save copy of ivp
866
867 &call (&label("pic_point")); # make it PIC!
868 &set_label("pic_point");
869 &blindpop($Tbl);
870 &lea ($Tbl,&DWP(&label("Camellia_SBOX")."-".&label("pic_point"),$Tbl));
871
872 &mov ($idx,32);
873 &set_label("prefetch_sbox",4);
874 &mov ($s0,&DWP(0,$Tbl));
875 &mov ($s1,&DWP(32,$Tbl));
876 &mov ($s2,&DWP(64,$Tbl));
877 &mov ($s3,&DWP(96,$Tbl));
878 &lea ($Tbl,&DWP(128,$Tbl));
879 &dec ($idx);
880 &jnz (&label("prefetch_sbox"));
881 &mov ($s0,$_key);
882 &sub ($Tbl,4096);
883 &mov ($idx,$_inp);
884 &mov ($s3,&DWP(272,$s0)); # load grandRounds
885
886 &cmp ($key,0);
887 &je (&label("DECRYPT"));
888
889 &mov ($s2,$_len);
890 &mov ($key,$_ivp);
891 &shl ($s3,6);
892 &lea ($s3,&DWP(0,$s0,$s3));
893 &mov ($_end,$s3);
894
895 &test ($s2,0xFFFFFFF0);
896 &jz (&label("enc_tail")); # short input...
897
898 &mov ($s0,&DWP(0,$key)); # load iv
899 &mov ($s1,&DWP(4,$key));
900
901 &set_label("enc_loop",4);
902 &mov ($s2,&DWP(8,$key));
903 &mov ($s3,&DWP(12,$key));
904
905 &xor ($s0,&DWP(0,$idx)); # xor input data
906 &xor ($s1,&DWP(4,$idx));
907 &xor ($s2,&DWP(8,$idx));
908 &bswap ($s0);
909 &xor ($s3,&DWP(12,$idx));
910 &bswap ($s1);
911 &mov ($key,$_key); # load key
912 &bswap ($s2);
913 &bswap ($s3);
914
915 &call ("_x86_Camellia_encrypt");
916
917 &mov ($idx,$_inp); # load inp
918 &mov ($key,$_out); # load out
919
920 &bswap ($s0);
921 &bswap ($s1);
922 &bswap ($s2);
923 &mov (&DWP(0,$key),$s0); # save output data
924 &bswap ($s3);
925 &mov (&DWP(4,$key),$s1);
926 &mov (&DWP(8,$key),$s2);
927 &mov (&DWP(12,$key),$s3);
928
929 &mov ($s2,$_len); # load len
930
931 &lea ($idx,&DWP(16,$idx));
932 &mov ($_inp,$idx); # save inp
933
934 &lea ($s3,&DWP(16,$key));
935 &mov ($_out,$s3); # save out
936
937 &sub ($s2,16);
938 &test ($s2,0xFFFFFFF0);
939 &mov ($_len,$s2); # save len
940 &jnz (&label("enc_loop"));
941 &test ($s2,15);
942 &jnz (&label("enc_tail"));
943 &mov ($idx,$_ivp); # load ivp
944 &mov ($s2,&DWP(8,$key)); # restore last dwords
945 &mov ($s3,&DWP(12,$key));
946 &mov (&DWP(0,$idx),$s0); # save ivec
947 &mov (&DWP(4,$idx),$s1);
948 &mov (&DWP(8,$idx),$s2);
949 &mov (&DWP(12,$idx),$s3);
950
951 &mov ("esp",$_esp);
952 &popf ();
953 &set_label("enc_out");
954 &function_end_A();
955 &pushf (); # kludge, never executed
956
957 &set_label("enc_tail",4);
958 &mov ($s0,$key eq "edi" ? $key : "");
959 &mov ($key,$_out); # load out
960 &push ($s0); # push ivp
961 &mov ($s1,16);
962 &sub ($s1,$s2);
963 &cmp ($key,$idx); # compare with inp
964 &je (&label("enc_in_place"));
965 &align (4);
966 &data_word(0xA4F3F689); # rep movsb # copy input
967 &jmp (&label("enc_skip_in_place"));
968 &set_label("enc_in_place");
969 &lea ($key,&DWP(0,$key,$s2));
970 &set_label("enc_skip_in_place");
971 &mov ($s2,$s1);
972 &xor ($s0,$s0);
973 &align (4);
974 &data_word(0xAAF3F689); # rep stosb # zero tail
975 &pop ($key); # pop ivp
976
977 &mov ($idx,$_out); # output as input
978 &mov ($s0,&DWP(0,$key));
979 &mov ($s1,&DWP(4,$key));
980 &mov ($_len,16); # len=16
981 &jmp (&label("enc_loop")); # one more spin...
982
983 #----------------------------- DECRYPT -----------------------------#
984 &set_label("DECRYPT",16);
985 &shl ($s3,6);
986 &lea ($s3,&DWP(0,$s0,$s3));
987 &mov ($_end,$s0);
988 &mov ($_key,$s3);
989
990 &cmp ($idx,$_out);
991 &je (&label("dec_in_place")); # in-place processing...
992
993 &mov ($key,$_ivp); # load ivp
994 &mov ($_tmp,$key);
995
996 &set_label("dec_loop",4);
997 &mov ($s0,&DWP(0,$idx)); # read input
998 &mov ($s1,&DWP(4,$idx));
999 &mov ($s2,&DWP(8,$idx));
1000 &bswap ($s0);
1001 &mov ($s3,&DWP(12,$idx));
1002 &bswap ($s1);
1003 &mov ($key,$_key); # load key
1004 &bswap ($s2);
1005 &bswap ($s3);
1006
1007 &call ("_x86_Camellia_decrypt");
1008
1009 &mov ($key,$_tmp); # load ivp
1010 &mov ($idx,$_len); # load len
1011
1012 &bswap ($s0);
1013 &bswap ($s1);
1014 &bswap ($s2);
1015 &xor ($s0,&DWP(0,$key)); # xor iv
1016 &bswap ($s3);
1017 &xor ($s1,&DWP(4,$key));
1018 &xor ($s2,&DWP(8,$key));
1019 &xor ($s3,&DWP(12,$key));
1020
1021 &sub ($idx,16);
1022 &jc (&label("dec_partial"));
1023 &mov ($_len,$idx); # save len
1024 &mov ($idx,$_inp); # load inp
1025 &mov ($key,$_out); # load out
1026
1027 &mov (&DWP(0,$key),$s0); # write output
1028 &mov (&DWP(4,$key),$s1);
1029 &mov (&DWP(8,$key),$s2);
1030 &mov (&DWP(12,$key),$s3);
1031
1032 &mov ($_tmp,$idx); # save ivp
1033 &lea ($idx,&DWP(16,$idx));
1034 &mov ($_inp,$idx); # save inp
1035
1036 &lea ($key,&DWP(16,$key));
1037 &mov ($_out,$key); # save out
1038
1039 &jnz (&label("dec_loop"));
1040 &mov ($key,$_tmp); # load temp ivp
1041 &set_label("dec_end");
1042 &mov ($idx,$_ivp); # load user ivp
1043 &mov ($s0,&DWP(0,$key)); # load iv
1044 &mov ($s1,&DWP(4,$key));
1045 &mov ($s2,&DWP(8,$key));
1046 &mov ($s3,&DWP(12,$key));
1047 &mov (&DWP(0,$idx),$s0); # copy back to user
1048 &mov (&DWP(4,$idx),$s1);
1049 &mov (&DWP(8,$idx),$s2);
1050 &mov (&DWP(12,$idx),$s3);
1051 &jmp (&label("dec_out"));
1052
1053 &set_label("dec_partial",4);
1054 &lea ($key,$ivec);
1055 &mov (&DWP(0,$key),$s0); # dump output to stack
1056 &mov (&DWP(4,$key),$s1);
1057 &mov (&DWP(8,$key),$s2);
1058 &mov (&DWP(12,$key),$s3);
1059 &lea ($s2 eq "ecx" ? $s2 : "",&DWP(16,$idx));
1060 &mov ($idx eq "esi" ? $idx : "",$key);
1061 &mov ($key eq "edi" ? $key : "",$_out); # load out
1062 &data_word(0xA4F3F689); # rep movsb # copy output
1063 &mov ($key,$_inp); # use inp as temp ivp
1064 &jmp (&label("dec_end"));
1065
1066 &set_label("dec_in_place",4);
1067 &set_label("dec_in_place_loop");
1068 &lea ($key,$ivec);
1069 &mov ($s0,&DWP(0,$idx)); # read input
1070 &mov ($s1,&DWP(4,$idx));
1071 &mov ($s2,&DWP(8,$idx));
1072 &mov ($s3,&DWP(12,$idx));
1073
1074 &mov (&DWP(0,$key),$s0); # copy to temp
1075 &mov (&DWP(4,$key),$s1);
1076 &mov (&DWP(8,$key),$s2);
1077 &bswap ($s0);
1078 &mov (&DWP(12,$key),$s3);
1079 &bswap ($s1);
1080 &mov ($key,$_key); # load key
1081 &bswap ($s2);
1082 &bswap ($s3);
1083
1084 &call ("_x86_Camellia_decrypt");
1085
1086 &mov ($key,$_ivp); # load ivp
1087 &mov ($idx,$_out); # load out
1088
1089 &bswap ($s0);
1090 &bswap ($s1);
1091 &bswap ($s2);
1092 &xor ($s0,&DWP(0,$key)); # xor iv
1093 &bswap ($s3);
1094 &xor ($s1,&DWP(4,$key));
1095 &xor ($s2,&DWP(8,$key));
1096 &xor ($s3,&DWP(12,$key));
1097
1098 &mov (&DWP(0,$idx),$s0); # write output
1099 &mov (&DWP(4,$idx),$s1);
1100 &mov (&DWP(8,$idx),$s2);
1101 &mov (&DWP(12,$idx),$s3);
1102
1103 &lea ($idx,&DWP(16,$idx));
1104 &mov ($_out,$idx); # save out
1105
1106 &lea ($idx,$ivec);
1107 &mov ($s0,&DWP(0,$idx)); # read temp
1108 &mov ($s1,&DWP(4,$idx));
1109 &mov ($s2,&DWP(8,$idx));
1110 &mov ($s3,&DWP(12,$idx));
1111
1112 &mov (&DWP(0,$key),$s0); # copy iv
1113 &mov (&DWP(4,$key),$s1);
1114 &mov (&DWP(8,$key),$s2);
1115 &mov (&DWP(12,$key),$s3);
1116
1117 &mov ($idx,$_inp); # load inp
1118
1119 &lea ($idx,&DWP(16,$idx));
1120 &mov ($_inp,$idx); # save inp
1121
1122 &mov ($s2,$_len); # load len
1123 &sub ($s2,16);
1124 &jc (&label("dec_in_place_partial"));
1125 &mov ($_len,$s2); # save len
1126 &jnz (&label("dec_in_place_loop"));
1127 &jmp (&label("dec_out"));
1128
1129 &set_label("dec_in_place_partial",4);
1130 # one can argue if this is actually required...
1131 &mov ($key eq "edi" ? $key : "",$_out);
1132 &lea ($idx eq "esi" ? $idx : "",$ivec);
1133 &lea ($key,&DWP(0,$key,$s2));
1134 &lea ($idx,&DWP(16,$idx,$s2));
1135 &neg ($s2 eq "ecx" ? $s2 : "");
1136 &data_word(0xA4F3F689); # rep movsb # restore tail
1137
1138 &set_label("dec_out",4);
1139 &mov ("esp",$_esp);
1140 &popf ();
1141 &function_end("Camellia_cbc_encrypt");
1142 }
1143
1144 &asciz("Camellia for x86 by <https://github.com/dot-asm>");
1145
1146 &asm_finish();
1147
1148 close STDOUT or die "error closing STDOUT: $!";