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 # Assembler helpers for Padlock engine. See even e_padlock-x86.pl for
17 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
19 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
21 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
22 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
23 ( $xlate="${dir}../../crypto/perlasm/x86_64-xlate.pl" and -f $xlate) or
24 die "can't locate x86_64-xlate.pl";
26 open STDOUT,"| $^X $xlate $flavour $output";
30 $PADLOCK_CHUNK=512; # Must be a power of 2 between 32 and 2^20
38 ($arg1,$arg2,$arg3,$arg4)=$win64?("%rcx","%rdx","%r8", "%r9") : # Win64 order
39 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
42 .globl padlock_capability
43 .type padlock_capability,\@abi-omnipotent
50 cmp \$`"0x".unpack("H*",'tneC')`,%ebx
52 cmp \$`"0x".unpack("H*",'Hrua')`,%edx
54 cmp \$`"0x".unpack("H*",'slua')`,%ecx
66 or \$0x10,%eax # set Nano bit#4
70 .size padlock_capability,.-padlock_capability
72 .globl padlock_key_bswap
73 .type padlock_key_bswap,\@abi-omnipotent,0
85 .size padlock_key_bswap,.-padlock_key_bswap
87 .globl padlock_verify_context
88 .type padlock_verify_context,\@abi-omnipotent
90 padlock_verify_context:
93 lea .Lpadlock_saved_context(%rip),%rax
94 call _padlock_verify_ctx
97 .size padlock_verify_context,.-padlock_verify_context
99 .type _padlock_verify_ctx,\@abi-omnipotent
112 .size _padlock_verify_ctx,.-_padlock_verify_ctx
114 .globl padlock_reload_key
115 .type padlock_reload_key,\@abi-omnipotent
121 .size padlock_reload_key,.-padlock_reload_key
123 .globl padlock_aes_block
124 .type padlock_aes_block,\@function,3
129 lea 32($ctx),%rbx # key
130 lea 16($ctx),$ctx # control word
131 .byte 0xf3,0x0f,0xa7,0xc8 # rep xcryptecb
134 .size padlock_aes_block,.-padlock_aes_block
136 .globl padlock_xstore
137 .type padlock_xstore,\@function,2
141 .byte 0x0f,0xa7,0xc0 # xstore
143 .size padlock_xstore,.-padlock_xstore
145 .globl padlock_sha1_oneshot
146 .type padlock_sha1_oneshot,\@function,3
148 padlock_sha1_oneshot:
151 .byte 0xf3,0x0f,0xa6,0xc8 # rep xsha1
153 .size padlock_sha1_oneshot,.-padlock_sha1_oneshot
155 .globl padlock_sha1_blocks
156 .type padlock_sha1_blocks,\@function,3
161 .byte 0xf3,0x0f,0xa6,0xc8 # rep xsha1
163 .size padlock_sha1_blocks,.-padlock_sha1_blocks
165 .globl padlock_sha256_oneshot
166 .type padlock_sha256_oneshot,\@function,3
168 padlock_sha256_oneshot:
171 .byte 0xf3,0x0f,0xa6,0xd0 # rep xsha256
173 .size padlock_sha256_oneshot,.-padlock_sha256_oneshot
175 .globl padlock_sha256_blocks
176 .type padlock_sha256_blocks,\@function,3
178 padlock_sha256_blocks:
181 .byte 0xf3,0x0f,0xa6,0xd0 # rep xsha256
183 .size padlock_sha256_blocks,.-padlock_sha256_blocks
185 .globl padlock_sha512_blocks
186 .type padlock_sha512_blocks,\@function,3
188 padlock_sha512_blocks:
190 .byte 0xf3,0x0f,0xa6,0xe0 # rep xha512
192 .size padlock_sha512_blocks,.-padlock_sha512_blocks
196 my ($mode,$opcode) = @_;
197 # int padlock_$mode_encrypt(void *out, const void *inp,
198 # struct padlock_cipher_data *ctx, size_t len);
200 .globl padlock_${mode}_encrypt
201 .type padlock_${mode}_encrypt,\@function,4
203 padlock_${mode}_encrypt:
212 lea .Lpadlock_saved_context(%rip),%rax
215 call _padlock_verify_ctx
216 lea 16($ctx),$ctx # control word
219 testl \$`1<<5`,($ctx) # align bit in control word
220 jnz .L${mode}_aligned
222 setz %al # !out_misaligned
224 setz %bl # !inp_misaligned
226 jnz .L${mode}_aligned
228 mov \$$PADLOCK_CHUNK,$chunk
229 not %rax # out_misaligned?-1:0
232 cmovc $len,$chunk # chunk=len>PADLOCK_CHUNK?PADLOCK_CHUNK:len
233 and $chunk,%rax # out_misaligned?chunk:0
236 and \$$PADLOCK_CHUNK-1,$chunk # chunk%=PADLOCK_CHUNK
239 $code.=<<___ if ($mode eq "ctr32");
240 mov -4($ctx),%eax # pull 32-bit counter
243 and \$`$PADLOCK_CHUNK/16-1`,%eax
247 cmova %rax,$chunk # don't let counter cross PADLOCK_CHUNK
253 cmp $len,$chunk # ctr32 artefact
254 cmova $len,$chunk # ctr32 artefact
255 mov $out,%r8 # save parameters
260 test \$0x0f,$out # out_misaligned
262 test \$0x0f,$inp # inp_misaligned
263 jz .L${mode}_inp_aligned
265 .byte 0xf3,0x48,0xa5 # rep movsq
269 .L${mode}_inp_aligned:
270 lea -16($ctx),%rax # ivp
271 lea 16($ctx),%rbx # key
273 .byte 0xf3,0x0f,0xa7,$opcode # rep xcrypt*
275 $code.=<<___ if ($mode !~ /ecb|ctr/);
277 movdqa %xmm0,-16($ctx) # copy [or refresh] iv
279 $code.=<<___ if ($mode eq "ctr32");
280 mov -4($ctx),%eax # pull 32-bit counter
281 test \$0xffff0000,%eax
282 jnz .L${mode}_no_corr
290 mov %r8,$out # restore paramters
293 jz .L${mode}_out_aligned
297 .byte 0xf3,0x48,0xa5 # rep movsq
299 .L${mode}_out_aligned:
305 mov \$$PADLOCK_CHUNK,$chunk
316 .byte 0xf3,0x48,0xab # rep stosq
324 $code.=<<___ if ($mode eq "ctr32");
325 mov -4($ctx),%eax # pull 32-bit counter
326 mov \$`16*0x10000`,$chunk
332 jz .L${mode}_aligned_loop
335 cmova %rax,$chunk # don't let counter cross 2^16
336 jmp .L${mode}_aligned_loop
338 .L${mode}_aligned_loop:
341 mov $len,%r10 # save parameters
346 lea -16($ctx),%rax # ivp
347 lea 16($ctx),%rbx # key
348 shr \$4,$len # len/=AES_BLOCK_SIZE
349 .byte 0xf3,0x0f,0xa7,$opcode # rep xcrypt*
351 $code.=<<___ if ($mode !~ /ecb|ctr/);
353 movdqa %xmm0,-16($ctx) # copy [or refresh] iv
355 $code.=<<___ if ($mode eq "ctr32");
356 mov -4($ctx),%eax # pull 32-bit counter
362 mov %r11,$chunk # restore paramters
365 mov \$`16*0x10000`,$chunk
366 jnz .L${mode}_aligned_loop
376 .size padlock_${mode}_encrypt,.-padlock_${mode}_encrypt
380 &generate_mode("ecb",0xc8);
381 &generate_mode("cbc",0xd0);
382 &generate_mode("cfb",0xe0);
383 &generate_mode("ofb",0xe8);
384 &generate_mode("ctr32",0xd8); # all 64-bit CPUs have working CTR...
387 .asciz "VIA Padlock x86_64 module, CRYPTOGAMS by <appro\@openssl.org>"
391 .Lpadlock_saved_context:
394 $code =~ s/\`([^\`]*)\`/eval($1)/gem;