2 # Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
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
10 # ====================================================================
11 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
12 # project. The module is, however, dual licensed under OpenSSL and
13 # CRYPTOGAMS licenses depending on where you obtain it. For further
14 # details see http://www.openssl.org/~appro/cryptogams/.
15 # ====================================================================
19 # Assembler helpers for Padlock engine. Compared to original engine
20 # version relying on inline assembler and compiled with gcc 3.4.6 it
21 # was measured to provide ~100% improvement on misaligned data in ECB
22 # mode and ~75% in CBC mode. For aligned data improvement can be
23 # observed for short inputs only, e.g. 45% for 64-byte messages in
24 # ECB mode, 20% in CBC. Difference in performance for aligned vs.
25 # misaligned data depends on misalignment and is either ~1.8x or 2.9x.
26 # These are approximately same factors as for hardware support, so
27 # there is little reason to rely on the latter. On the contrary, it
28 # might actually hurt performance in mixture of aligned and misaligned
29 # buffers, because a) if you choose to flip 'align' flag in control
30 # word on per-buffer basis, then you'd have to reload key context,
31 # which incurs penalty; b) if you choose to set 'align' flag
32 # permanently, it limits performance even for aligned data to ~1/2.
33 # All above mentioned results were collected on 1.5GHz C7. Nano on the
34 # other hand handles unaligned data more gracefully. Depending on
35 # algorithm and how unaligned data is, hardware can be up to 70% more
36 # efficient than below software alignment procedures, nor does 'align'
37 # flag have affect on aligned performance [if has any meaning at all].
38 # Therefore suggestion is to unconditionally set 'align' flag on Nano
39 # for optimal performance.
41 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
42 push(@INC,"${dir}","${dir}../../crypto/perlasm");
45 $output=pop and open STDOUT,">$output";
49 %PADLOCK_PREFETCH=(ecb=>128, cbc=>64); # prefetch errata
50 $PADLOCK_CHUNK=512; # Must be a power of 2 larger than 16
58 &function_begin_B("padlock_capability");
71 &jnc (&label("noluck"));
74 &cmp ("ebx","0x".unpack("H*",'tneC'));
75 &jne (&label("zhaoxin"));
76 &cmp ("edx","0x".unpack("H*",'Hrua'));
77 &jne (&label("noluck"));
78 &cmp ("ecx","0x".unpack("H*",'slua'));
79 &jne (&label("noluck"));
80 &jmp (&label("zhaoxinEnd"));
81 &set_label("zhaoxin");
82 &cmp ("ebx","0x".unpack("H*",'hS '));
83 &jne (&label("noluck"));
84 &cmp ("edx","0x".unpack("H*",'hgna'));
85 &jne (&label("noluck"));
86 &cmp ("ecx","0x".unpack("H*",' ia'));
87 &jne (&label("noluck"));
88 &set_label("zhaoxinEnd");
89 &mov ("eax",0xC0000000);
93 &cmp ("edx",0xC0000001);
94 &jb (&label("noluck"));
100 &cmp ("eax",0x06ff); # check for Nano
102 &mov ("eax",0xC0000001);
107 &shl ("ebx",4); # bit#4 denotes Nano
108 &and ("eax",0xffffffef);
110 &set_label("noluck");
113 &function_end_B("padlock_capability")
115 &function_begin_B("padlock_key_bswap");
116 &mov ("edx",&wparam(0));
117 &mov ("ecx",&DWP(240,"edx"));
118 &set_label("bswap_loop");
119 &mov ("eax",&DWP(0,"edx"));
121 &mov (&DWP(0,"edx"),"eax");
122 &lea ("edx",&DWP(4,"edx"));
124 &jnz (&label("bswap_loop"));
126 &function_end_B("padlock_key_bswap");
128 # This is heuristic key context tracing. At first one
129 # believes that one should use atomic swap instructions,
130 # but it's not actually necessary. Point is that if
131 # padlock_saved_context was changed by another thread
132 # after we've read it and before we compare it with ctx,
133 # our key *shall* be reloaded upon thread context switch
134 # and we are therefore set in either case...
135 &static_label("padlock_saved_context");
137 &function_begin_B("padlock_verify_context");
138 &mov ($ctx,&wparam(0));
139 &lea ("eax",($::win32 or $::coff) ? &DWP(&label("padlock_saved_context")) :
140 &DWP(&label("padlock_saved_context")."-".&label("verify_pic_point")));
142 &call ("_padlock_verify_ctx");
143 &set_label("verify_pic_point");
144 &lea ("esp",&DWP(4,"esp"));
146 &function_end_B("padlock_verify_context");
148 &function_begin_B("_padlock_verify_ctx");
149 &add ("eax",&DWP(0,"esp")) if(!($::win32 or $::coff));# &padlock_saved_context
150 &bt (&DWP(4,"esp"),30); # eflags
151 &jnc (&label("verified"));
152 &cmp ($ctx,&DWP(0,"eax"));
153 &je (&label("verified"));
156 &set_label("verified");
157 &mov (&DWP(0,"eax"),$ctx);
159 &function_end_B("_padlock_verify_ctx");
161 &function_begin_B("padlock_reload_key");
165 &function_end_B("padlock_reload_key");
167 &function_begin_B("padlock_aes_block");
171 &mov ($out,&wparam(0)); # must be 16-byte aligned
172 &mov ($inp,&wparam(1)); # must be 16-byte aligned
173 &mov ($ctx,&wparam(2));
175 &lea ("ebx",&DWP(32,$ctx)); # key
176 &lea ($ctx,&DWP(16,$ctx)); # control word
177 &data_byte(0xf3,0x0f,0xa7,0xc8); # rep xcryptecb
182 &function_end_B("padlock_aes_block");
185 my ($mode,$opcode) = @_;
186 # int padlock_$mode_encrypt(void *out, const void *inp,
187 # struct padlock_cipher_data *ctx, size_t len);
188 &function_begin("padlock_${mode}_encrypt");
189 &mov ($out,&wparam(0));
190 &mov ($inp,&wparam(1));
191 &mov ($ctx,&wparam(2));
192 &mov ($len,&wparam(3));
194 &jnz (&label("${mode}_abort"));
196 &jnz (&label("${mode}_abort"));
197 &lea ("eax",($::win32 or $::coff) ? &DWP(&label("padlock_saved_context")) :
198 &DWP(&label("padlock_saved_context")."-".&label("${mode}_pic_point")));
201 &call ("_padlock_verify_ctx");
202 &set_label("${mode}_pic_point");
203 &lea ($ctx,&DWP(16,$ctx)); # control word
205 if ($mode eq "ctr32") {
206 &movq ("mm0",&QWP(-16,$ctx)); # load [upper part of] counter
209 &test (&DWP(0,$ctx),1<<5); # align bit in control word
210 &jnz (&label("${mode}_aligned"));
212 &setz ("al"); # !out_misaligned
214 &setz ("bl"); # !inp_misaligned
216 &jnz (&label("${mode}_aligned"));
219 &mov ($chunk,$PADLOCK_CHUNK);
220 ¬ ("eax"); # out_misaligned?-1:0
221 &lea ("ebp",&DWP(-24,"esp"));
223 &cmovc ($chunk,$len); # chunk=len>PADLOCK_CHUNK?PADLOCK_CHUNK:len
224 &and ("eax",$chunk); # out_misaligned?chunk:0
227 &and ($chunk,$PADLOCK_CHUNK-1); # chunk=len%PADLOCK_CHUNK
228 &lea ("esp",&DWP(0,"eax","ebp")); # alloca
229 &mov ("eax",$PADLOCK_CHUNK);
230 &cmovz ($chunk,"eax"); # chunk=chunk?:PADLOCK_CHUNK
234 &mov (&DWP(16,"ebp"),"eax");
235 if ($PADLOCK_PREFETCH{$mode}) {
237 &ja (&label("${mode}_loop"));
238 &mov ("eax",$inp); # check if prefetch crosses page
243 &and ("eax",0xfff); # distance to page boundary
244 &cmp ("eax",$PADLOCK_PREFETCH{$mode});
245 &mov ("eax",-$PADLOCK_PREFETCH{$mode});
246 &cmovae ("eax",$chunk); # mask=distance<prefetch?-prefetch:-1
248 &jz (&label("${mode}_unaligned_tail"));
250 &jmp (&label("${mode}_loop"));
252 &set_label("${mode}_loop",16);
253 &mov (&DWP(0,"ebp"),$out); # save parameters
254 &mov (&DWP(4,"ebp"),$inp);
255 &mov (&DWP(8,"ebp"),$len);
257 &mov (&DWP(12,"ebp"),$chunk); # chunk
258 if ($mode eq "ctr32") {
259 &mov ("ecx",&DWP(-4,$ctx));
261 &mov ("eax",&DWP(-8,$ctx)); # borrow $len
262 &set_label("${mode}_prepare");
263 &mov (&DWP(12,"esp",$out),"ecx");
265 &movq (&QWP(0,"esp",$out),"mm0");
267 &mov (&DWP(8,"esp",$out),"eax");
269 &lea ($out,&DWP(16,$out));
271 &jb (&label("${mode}_prepare"));
273 &mov (&DWP(-4,$ctx),"ecx");
274 &lea ($inp,&DWP(0,"esp"));
275 &lea ($out,&DWP(0,"esp"));
278 &test ($out,0x0f); # out_misaligned
279 &cmovnz ($out,"esp");
280 &test ($inp,0x0f); # inp_misaligned
281 &jz (&label("${mode}_inp_aligned"));
283 &data_byte(0xf3,0xa5); # rep movsl
287 &set_label("${mode}_inp_aligned");
289 &lea ("eax",&DWP(-16,$ctx)); # ivp
290 &lea ("ebx",&DWP(16,$ctx)); # key
291 &shr ($len,4); # len/=AES_BLOCK_SIZE
292 &data_byte(0xf3,0x0f,0xa7,$opcode); # rep xcrypt*
293 if ($mode !~ /ecb|ctr/) {
294 &movaps ("xmm0",&QWP(0,"eax"));
295 &movaps (&QWP(-16,$ctx),"xmm0"); # copy [or refresh] iv
297 &mov ($out,&DWP(0,"ebp")); # restore parameters
298 &mov ($chunk,&DWP(12,"ebp"));
299 if ($mode eq "ctr32") {
300 &mov ($inp,&DWP(4,"ebp"));
302 &set_label("${mode}_xor");
303 &movups ("xmm1",&QWP(0,$inp,$len));
304 &lea ($len,&DWP(16,$len));
305 &pxor ("xmm1",&QWP(-16,"esp",$len));
306 &movups (&QWP(-16,$out,$len),"xmm1");
308 &jb (&label("${mode}_xor"));
311 &jz (&label("${mode}_out_aligned"));
313 &lea ($inp,&DWP(0,"esp"));
315 &data_byte(0xf3,0xa5); # rep movsl
317 &set_label("${mode}_out_aligned");
318 &mov ($inp,&DWP(4,"ebp"));
320 &mov ($len,&DWP(8,"ebp"));
324 &mov ($chunk,$PADLOCK_CHUNK);
325 if (!$PADLOCK_PREFETCH{$mode}) {
326 &jnz (&label("${mode}_loop"));
328 &jz (&label("${mode}_break"));
330 &jae (&label("${mode}_loop"));
332 &set_label("${mode}_unaligned_tail");
336 &sub ("esp","eax"); # alloca
337 &mov ("eax", $out); # save parameters
340 &lea ($out,&DWP(0,"esp"));
341 &data_byte(0xf3,0xa5); # rep movsl
343 &mov ($out,"eax"); # restore parameters
345 &jmp (&label("${mode}_loop"));
347 &set_label("${mode}_break",16);
349 if ($mode ne "ctr32") {
351 &je (&label("${mode}_done"));
353 &pxor ("xmm0","xmm0");
354 &lea ("eax",&DWP(0,"esp"));
355 &set_label("${mode}_bzero");
356 &movaps (&QWP(0,"eax"),"xmm0");
357 &lea ("eax",&DWP(16,"eax"));
359 &ja (&label("${mode}_bzero"));
361 &set_label("${mode}_done");
362 &mov ("ebp",&DWP(16,"ebp"));
363 &lea ("esp",&DWP(24,"ebp"));
364 if ($mode ne "ctr32") {
365 &jmp (&label("${mode}_exit"));
367 &set_label("${mode}_aligned",16);
368 if ($PADLOCK_PREFETCH{$mode}) {
369 &lea ("ebp",&DWP(0,$inp,$len));
371 &and ("ebp",0xfff); # distance to page boundary
373 &cmp ("ebp",$PADLOCK_PREFETCH{$mode});
374 &mov ("ebp",$PADLOCK_PREFETCH{$mode}-1);
375 &cmovae ("ebp","eax");
376 &and ("ebp",$len); # remainder
378 &jz (&label("${mode}_aligned_tail"));
380 &lea ("eax",&DWP(-16,$ctx)); # ivp
381 &lea ("ebx",&DWP(16,$ctx)); # key
382 &shr ($len,4); # len/=AES_BLOCK_SIZE
383 &data_byte(0xf3,0x0f,0xa7,$opcode); # rep xcrypt*
384 if ($mode ne "ecb") {
385 &movaps ("xmm0",&QWP(0,"eax"));
386 &movaps (&QWP(-16,$ctx),"xmm0"); # copy [or refresh] iv
388 if ($PADLOCK_PREFETCH{$mode}) {
390 &jz (&label("${mode}_exit"));
392 &set_label("${mode}_aligned_tail");
394 &lea ("ebp",&DWP(-24,"esp"));
400 &mov (&DWP(16,"ebp"),"eax");
401 &mov ("eax", $out); # save parameters
404 &lea ($out,&DWP(0,"esp"));
405 &data_byte(0xf3,0xa5); # rep movsl
407 &mov ($out,"eax"); # restore parameters
409 &jmp (&label("${mode}_loop"));
411 &set_label("${mode}_exit"); }
413 &lea ("esp",&DWP(4,"esp")); # popf
414 &emms () if ($mode eq "ctr32");
415 &set_label("${mode}_abort");
416 &function_end("padlock_${mode}_encrypt");
419 &generate_mode("ecb",0xc8);
420 &generate_mode("cbc",0xd0);
421 &generate_mode("cfb",0xe0);
422 &generate_mode("ofb",0xe8);
423 &generate_mode("ctr32",0xc8); # yes, it implements own CTR with ECB opcode,
424 # because hardware CTR was introduced later
425 # and even has errata on certain C7 stepping.
426 # own implementation *always* works, though
427 # ~15% slower than dedicated hardware...
429 &function_begin_B("padlock_xstore");
431 &mov ("edi",&wparam(0));
432 &mov ("edx",&wparam(1));
433 &data_byte(0x0f,0xa7,0xc0); # xstore
436 &function_end_B("padlock_xstore");
438 &function_begin_B("_win32_segv_handler");
439 &mov ("eax",1); # ExceptionContinueSearch
440 &mov ("edx",&wparam(0)); # *ExceptionRecord
441 &mov ("ecx",&wparam(2)); # *ContextRecord
442 &cmp (&DWP(0,"edx"),0xC0000005) # ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION
443 &jne (&label("ret"));
444 &add (&DWP(184,"ecx"),4); # skip over rep sha*
445 &mov ("eax",0); # ExceptionContinueExecution
448 &function_end_B("_win32_segv_handler");
449 &safeseh("_win32_segv_handler") if ($::win32);
451 &function_begin_B("padlock_sha1_oneshot");
455 &mov ("edi",&wparam(0));
456 &mov ("esi",&wparam(1));
457 &mov ("ecx",&wparam(2));
458 if ($::win32 or $::coff) {
459 &push (&::islabel("_win32_segv_handler"));
460 &data_byte(0x64,0xff,0x30); # push %fs:(%eax)
461 &data_byte(0x64,0x89,0x20); # mov %esp,%fs:(%eax)
463 &mov ("edx","esp"); # put aside %esp
464 &add ("esp",-128); # 32 is enough but spec says 128
465 &movups ("xmm0",&QWP(0,"edi")); # copy-in context
467 &mov ("eax",&DWP(16,"edi"));
468 &movaps (&QWP(0,"esp"),"xmm0");
470 &mov (&DWP(16,"esp"),"eax");
472 &data_byte(0xf3,0x0f,0xa6,0xc8); # rep xsha1
473 &movaps ("xmm0",&QWP(0,"esp"));
474 &mov ("eax",&DWP(16,"esp"));
475 &mov ("esp","edx"); # restore %esp
476 if ($::win32 or $::coff) {
477 &data_byte(0x64,0x8f,0x05,0,0,0,0); # pop %fs:0
478 &lea ("esp",&DWP(4,"esp"));
480 &mov ("edi",&wparam(0));
481 &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
482 &mov (&DWP(16,"edi"),"eax");
486 &function_end_B("padlock_sha1_oneshot");
488 &function_begin_B("padlock_sha1_blocks");
491 &mov ("edi",&wparam(0));
492 &mov ("esi",&wparam(1));
493 &mov ("edx","esp"); # put aside %esp
494 &mov ("ecx",&wparam(2));
496 &movups ("xmm0",&QWP(0,"edi")); # copy-in context
498 &mov ("eax",&DWP(16,"edi"));
499 &movaps (&QWP(0,"esp"),"xmm0");
501 &mov (&DWP(16,"esp"),"eax");
503 &data_byte(0xf3,0x0f,0xa6,0xc8); # rep xsha1
504 &movaps ("xmm0",&QWP(0,"esp"));
505 &mov ("eax",&DWP(16,"esp"));
506 &mov ("esp","edx"); # restore %esp
507 &mov ("edi",&wparam(0));
508 &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
509 &mov (&DWP(16,"edi"),"eax");
513 &function_end_B("padlock_sha1_blocks");
515 &function_begin_B("padlock_sha256_oneshot");
519 &mov ("edi",&wparam(0));
520 &mov ("esi",&wparam(1));
521 &mov ("ecx",&wparam(2));
522 if ($::win32 or $::coff) {
523 &push (&::islabel("_win32_segv_handler"));
524 &data_byte(0x64,0xff,0x30); # push %fs:(%eax)
525 &data_byte(0x64,0x89,0x20); # mov %esp,%fs:(%eax)
527 &mov ("edx","esp"); # put aside %esp
529 &movups ("xmm0",&QWP(0,"edi")); # copy-in context
531 &movups ("xmm1",&QWP(16,"edi"));
532 &movaps (&QWP(0,"esp"),"xmm0");
534 &movaps (&QWP(16,"esp"),"xmm1");
536 &data_byte(0xf3,0x0f,0xa6,0xd0); # rep xsha256
537 &movaps ("xmm0",&QWP(0,"esp"));
538 &movaps ("xmm1",&QWP(16,"esp"));
539 &mov ("esp","edx"); # restore %esp
540 if ($::win32 or $::coff) {
541 &data_byte(0x64,0x8f,0x05,0,0,0,0); # pop %fs:0
542 &lea ("esp",&DWP(4,"esp"));
544 &mov ("edi",&wparam(0));
545 &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
546 &movups (&QWP(16,"edi"),"xmm1");
550 &function_end_B("padlock_sha256_oneshot");
552 &function_begin_B("padlock_sha256_blocks");
555 &mov ("edi",&wparam(0));
556 &mov ("esi",&wparam(1));
557 &mov ("ecx",&wparam(2));
558 &mov ("edx","esp"); # put aside %esp
560 &movups ("xmm0",&QWP(0,"edi")); # copy-in context
562 &movups ("xmm1",&QWP(16,"edi"));
563 &movaps (&QWP(0,"esp"),"xmm0");
565 &movaps (&QWP(16,"esp"),"xmm1");
567 &data_byte(0xf3,0x0f,0xa6,0xd0); # rep xsha256
568 &movaps ("xmm0",&QWP(0,"esp"));
569 &movaps ("xmm1",&QWP(16,"esp"));
570 &mov ("esp","edx"); # restore %esp
571 &mov ("edi",&wparam(0));
572 &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
573 &movups (&QWP(16,"edi"),"xmm1");
577 &function_end_B("padlock_sha256_blocks");
579 &function_begin_B("padlock_sha512_blocks");
582 &mov ("edi",&wparam(0));
583 &mov ("esi",&wparam(1));
584 &mov ("ecx",&wparam(2));
585 &mov ("edx","esp"); # put aside %esp
587 &movups ("xmm0",&QWP(0,"edi")); # copy-in context
589 &movups ("xmm1",&QWP(16,"edi"));
590 &movups ("xmm2",&QWP(32,"edi"));
591 &movups ("xmm3",&QWP(48,"edi"));
592 &movaps (&QWP(0,"esp"),"xmm0");
594 &movaps (&QWP(16,"esp"),"xmm1");
595 &movaps (&QWP(32,"esp"),"xmm2");
596 &movaps (&QWP(48,"esp"),"xmm3");
597 &data_byte(0xf3,0x0f,0xa6,0xe0); # rep xsha512
598 &movaps ("xmm0",&QWP(0,"esp"));
599 &movaps ("xmm1",&QWP(16,"esp"));
600 &movaps ("xmm2",&QWP(32,"esp"));
601 &movaps ("xmm3",&QWP(48,"esp"));
602 &mov ("esp","edx"); # restore %esp
603 &mov ("edi",&wparam(0));
604 &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
605 &movups (&QWP(16,"edi"),"xmm1");
606 &movups (&QWP(32,"edi"),"xmm2");
607 &movups (&QWP(48,"edi"),"xmm3");
611 &function_end_B("padlock_sha512_blocks");
613 &asciz ("VIA Padlock x86 module, CRYPTOGAMS by <appro\@openssl.org>");
617 # Essentially this variable belongs in thread local storage.
618 # Having this variable global on the other hand can only cause
619 # few bogus key reloads [if any at all on signle-CPU system],
620 # so we accept the penalty...
621 &set_label("padlock_saved_context",4);