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");
46 open STDOUT,">$output";
50 %PADLOCK_PREFETCH=(ecb=>128, cbc=>64); # prefetch errata
51 $PADLOCK_CHUNK=512; # Must be a power of 2 larger than 16
59 &function_begin_B("padlock_capability");
72 &jnc (&label("noluck"));
75 &cmp ("ebx","0x".unpack("H*",'tneC'));
76 &jne (&label("zhaoxin"));
77 &cmp ("edx","0x".unpack("H*",'Hrua'));
78 &jne (&label("noluck"));
79 &cmp ("ecx","0x".unpack("H*",'slua'));
80 &jne (&label("noluck"));
81 &jmp (&label("zhaoxinEnd"));
82 &set_label("zhaoxin");
83 &cmp ("ebx","0x".unpack("H*",'hS '));
84 &jne (&label("noluck"));
85 &cmp ("edx","0x".unpack("H*",'hgna'));
86 &jne (&label("noluck"));
87 &cmp ("ecx","0x".unpack("H*",' ia'));
88 &jne (&label("noluck"));
89 &set_label("zhaoxinEnd");
90 &mov ("eax",0xC0000000);
94 &cmp ("edx",0xC0000001);
95 &jb (&label("noluck"));
101 &cmp ("eax",0x06ff); # check for Nano
103 &mov ("eax",0xC0000001);
108 &shl ("ebx",4); # bit#4 denotes Nano
109 &and ("eax",0xffffffef);
111 &set_label("noluck");
114 &function_end_B("padlock_capability")
116 &function_begin_B("padlock_key_bswap");
117 &mov ("edx",&wparam(0));
118 &mov ("ecx",&DWP(240,"edx"));
119 &set_label("bswap_loop");
120 &mov ("eax",&DWP(0,"edx"));
122 &mov (&DWP(0,"edx"),"eax");
123 &lea ("edx",&DWP(4,"edx"));
125 &jnz (&label("bswap_loop"));
127 &function_end_B("padlock_key_bswap");
129 # This is heuristic key context tracing. At first one
130 # believes that one should use atomic swap instructions,
131 # but it's not actually necessary. Point is that if
132 # padlock_saved_context was changed by another thread
133 # after we've read it and before we compare it with ctx,
134 # our key *shall* be reloaded upon thread context switch
135 # and we are therefore set in either case...
136 &static_label("padlock_saved_context");
138 &function_begin_B("padlock_verify_context");
139 &mov ($ctx,&wparam(0));
140 &lea ("eax",($::win32 or $::coff) ? &DWP(&label("padlock_saved_context")) :
141 &DWP(&label("padlock_saved_context")."-".&label("verify_pic_point")));
143 &call ("_padlock_verify_ctx");
144 &set_label("verify_pic_point");
145 &lea ("esp",&DWP(4,"esp"));
147 &function_end_B("padlock_verify_context");
149 &function_begin_B("_padlock_verify_ctx");
150 &add ("eax",&DWP(0,"esp")) if(!($::win32 or $::coff));# &padlock_saved_context
151 &bt (&DWP(4,"esp"),30); # eflags
152 &jnc (&label("verified"));
153 &cmp ($ctx,&DWP(0,"eax"));
154 &je (&label("verified"));
157 &set_label("verified");
158 &mov (&DWP(0,"eax"),$ctx);
160 &function_end_B("_padlock_verify_ctx");
162 &function_begin_B("padlock_reload_key");
166 &function_end_B("padlock_reload_key");
168 &function_begin_B("padlock_aes_block");
172 &mov ($out,&wparam(0)); # must be 16-byte aligned
173 &mov ($inp,&wparam(1)); # must be 16-byte aligned
174 &mov ($ctx,&wparam(2));
176 &lea ("ebx",&DWP(32,$ctx)); # key
177 &lea ($ctx,&DWP(16,$ctx)); # control word
178 &data_byte(0xf3,0x0f,0xa7,0xc8); # rep xcryptecb
183 &function_end_B("padlock_aes_block");
186 my ($mode,$opcode) = @_;
187 # int padlock_$mode_encrypt(void *out, const void *inp,
188 # struct padlock_cipher_data *ctx, size_t len);
189 &function_begin("padlock_${mode}_encrypt");
190 &mov ($out,&wparam(0));
191 &mov ($inp,&wparam(1));
192 &mov ($ctx,&wparam(2));
193 &mov ($len,&wparam(3));
195 &jnz (&label("${mode}_abort"));
197 &jnz (&label("${mode}_abort"));
198 &lea ("eax",($::win32 or $::coff) ? &DWP(&label("padlock_saved_context")) :
199 &DWP(&label("padlock_saved_context")."-".&label("${mode}_pic_point")));
202 &call ("_padlock_verify_ctx");
203 &set_label("${mode}_pic_point");
204 &lea ($ctx,&DWP(16,$ctx)); # control word
206 if ($mode eq "ctr32") {
207 &movq ("mm0",&QWP(-16,$ctx)); # load [upper part of] counter
210 &test (&DWP(0,$ctx),1<<5); # align bit in control word
211 &jnz (&label("${mode}_aligned"));
213 &setz ("al"); # !out_misaligned
215 &setz ("bl"); # !inp_misaligned
217 &jnz (&label("${mode}_aligned"));
220 &mov ($chunk,$PADLOCK_CHUNK);
221 ¬ ("eax"); # out_misaligned?-1:0
222 &lea ("ebp",&DWP(-24,"esp"));
224 &cmovc ($chunk,$len); # chunk=len>PADLOCK_CHUNK?PADLOCK_CHUNK:len
225 &and ("eax",$chunk); # out_misaligned?chunk:0
228 &and ($chunk,$PADLOCK_CHUNK-1); # chunk=len%PADLOCK_CHUNK
229 &lea ("esp",&DWP(0,"eax","ebp")); # alloca
230 &mov ("eax",$PADLOCK_CHUNK);
231 &cmovz ($chunk,"eax"); # chunk=chunk?:PADLOCK_CHUNK
235 &mov (&DWP(16,"ebp"),"eax");
236 if ($PADLOCK_PREFETCH{$mode}) {
238 &ja (&label("${mode}_loop"));
239 &mov ("eax",$inp); # check if prefetch crosses page
244 &and ("eax",0xfff); # distance to page boundary
245 &cmp ("eax",$PADLOCK_PREFETCH{$mode});
246 &mov ("eax",-$PADLOCK_PREFETCH{$mode});
247 &cmovae ("eax",$chunk); # mask=distance<prefetch?-prefetch:-1
249 &jz (&label("${mode}_unaligned_tail"));
251 &jmp (&label("${mode}_loop"));
253 &set_label("${mode}_loop",16);
254 &mov (&DWP(0,"ebp"),$out); # save parameters
255 &mov (&DWP(4,"ebp"),$inp);
256 &mov (&DWP(8,"ebp"),$len);
258 &mov (&DWP(12,"ebp"),$chunk); # chunk
259 if ($mode eq "ctr32") {
260 &mov ("ecx",&DWP(-4,$ctx));
262 &mov ("eax",&DWP(-8,$ctx)); # borrow $len
263 &set_label("${mode}_prepare");
264 &mov (&DWP(12,"esp",$out),"ecx");
266 &movq (&QWP(0,"esp",$out),"mm0");
268 &mov (&DWP(8,"esp",$out),"eax");
270 &lea ($out,&DWP(16,$out));
272 &jb (&label("${mode}_prepare"));
274 &mov (&DWP(-4,$ctx),"ecx");
275 &lea ($inp,&DWP(0,"esp"));
276 &lea ($out,&DWP(0,"esp"));
279 &test ($out,0x0f); # out_misaligned
280 &cmovnz ($out,"esp");
281 &test ($inp,0x0f); # inp_misaligned
282 &jz (&label("${mode}_inp_aligned"));
284 &data_byte(0xf3,0xa5); # rep movsl
288 &set_label("${mode}_inp_aligned");
290 &lea ("eax",&DWP(-16,$ctx)); # ivp
291 &lea ("ebx",&DWP(16,$ctx)); # key
292 &shr ($len,4); # len/=AES_BLOCK_SIZE
293 &data_byte(0xf3,0x0f,0xa7,$opcode); # rep xcrypt*
294 if ($mode !~ /ecb|ctr/) {
295 &movaps ("xmm0",&QWP(0,"eax"));
296 &movaps (&QWP(-16,$ctx),"xmm0"); # copy [or refresh] iv
298 &mov ($out,&DWP(0,"ebp")); # restore parameters
299 &mov ($chunk,&DWP(12,"ebp"));
300 if ($mode eq "ctr32") {
301 &mov ($inp,&DWP(4,"ebp"));
303 &set_label("${mode}_xor");
304 &movups ("xmm1",&QWP(0,$inp,$len));
305 &lea ($len,&DWP(16,$len));
306 &pxor ("xmm1",&QWP(-16,"esp",$len));
307 &movups (&QWP(-16,$out,$len),"xmm1");
309 &jb (&label("${mode}_xor"));
312 &jz (&label("${mode}_out_aligned"));
314 &lea ($inp,&DWP(0,"esp"));
316 &data_byte(0xf3,0xa5); # rep movsl
318 &set_label("${mode}_out_aligned");
319 &mov ($inp,&DWP(4,"ebp"));
321 &mov ($len,&DWP(8,"ebp"));
325 &mov ($chunk,$PADLOCK_CHUNK);
326 if (!$PADLOCK_PREFETCH{$mode}) {
327 &jnz (&label("${mode}_loop"));
329 &jz (&label("${mode}_break"));
331 &jae (&label("${mode}_loop"));
333 &set_label("${mode}_unaligned_tail");
337 &sub ("esp","eax"); # alloca
338 &mov ("eax", $out); # save parameters
341 &lea ($out,&DWP(0,"esp"));
342 &data_byte(0xf3,0xa5); # rep movsl
344 &mov ($out,"eax"); # restore parameters
346 &jmp (&label("${mode}_loop"));
348 &set_label("${mode}_break",16);
350 if ($mode ne "ctr32") {
352 &je (&label("${mode}_done"));
354 &pxor ("xmm0","xmm0");
355 &lea ("eax",&DWP(0,"esp"));
356 &set_label("${mode}_bzero");
357 &movaps (&QWP(0,"eax"),"xmm0");
358 &lea ("eax",&DWP(16,"eax"));
360 &ja (&label("${mode}_bzero"));
362 &set_label("${mode}_done");
363 &mov ("ebp",&DWP(16,"ebp"));
364 &lea ("esp",&DWP(24,"ebp"));
365 if ($mode ne "ctr32") {
366 &jmp (&label("${mode}_exit"));
368 &set_label("${mode}_aligned",16);
369 if ($PADLOCK_PREFETCH{$mode}) {
370 &lea ("ebp",&DWP(0,$inp,$len));
372 &and ("ebp",0xfff); # distance to page boundary
374 &cmp ("ebp",$PADLOCK_PREFETCH{$mode});
375 &mov ("ebp",$PADLOCK_PREFETCH{$mode}-1);
376 &cmovae ("ebp","eax");
377 &and ("ebp",$len); # remainder
379 &jz (&label("${mode}_aligned_tail"));
381 &lea ("eax",&DWP(-16,$ctx)); # ivp
382 &lea ("ebx",&DWP(16,$ctx)); # key
383 &shr ($len,4); # len/=AES_BLOCK_SIZE
384 &data_byte(0xf3,0x0f,0xa7,$opcode); # rep xcrypt*
385 if ($mode ne "ecb") {
386 &movaps ("xmm0",&QWP(0,"eax"));
387 &movaps (&QWP(-16,$ctx),"xmm0"); # copy [or refresh] iv
389 if ($PADLOCK_PREFETCH{$mode}) {
391 &jz (&label("${mode}_exit"));
393 &set_label("${mode}_aligned_tail");
395 &lea ("ebp",&DWP(-24,"esp"));
401 &mov (&DWP(16,"ebp"),"eax");
402 &mov ("eax", $out); # save parameters
405 &lea ($out,&DWP(0,"esp"));
406 &data_byte(0xf3,0xa5); # rep movsl
408 &mov ($out,"eax"); # restore parameters
410 &jmp (&label("${mode}_loop"));
412 &set_label("${mode}_exit"); }
414 &lea ("esp",&DWP(4,"esp")); # popf
415 &emms () if ($mode eq "ctr32");
416 &set_label("${mode}_abort");
417 &function_end("padlock_${mode}_encrypt");
420 &generate_mode("ecb",0xc8);
421 &generate_mode("cbc",0xd0);
422 &generate_mode("cfb",0xe0);
423 &generate_mode("ofb",0xe8);
424 &generate_mode("ctr32",0xc8); # yes, it implements own CTR with ECB opcode,
425 # because hardware CTR was introduced later
426 # and even has errata on certain C7 stepping.
427 # own implementation *always* works, though
428 # ~15% slower than dedicated hardware...
430 &function_begin_B("padlock_xstore");
432 &mov ("edi",&wparam(0));
433 &mov ("edx",&wparam(1));
434 &data_byte(0x0f,0xa7,0xc0); # xstore
437 &function_end_B("padlock_xstore");
439 &function_begin_B("_win32_segv_handler");
440 &mov ("eax",1); # ExceptionContinueSearch
441 &mov ("edx",&wparam(0)); # *ExceptionRecord
442 &mov ("ecx",&wparam(2)); # *ContextRecord
443 &cmp (&DWP(0,"edx"),0xC0000005) # ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION
444 &jne (&label("ret"));
445 &add (&DWP(184,"ecx"),4); # skip over rep sha*
446 &mov ("eax",0); # ExceptionContinueExecution
449 &function_end_B("_win32_segv_handler");
450 &safeseh("_win32_segv_handler") if ($::win32);
452 &function_begin_B("padlock_sha1_oneshot");
456 &mov ("edi",&wparam(0));
457 &mov ("esi",&wparam(1));
458 &mov ("ecx",&wparam(2));
459 if ($::win32 or $::coff) {
460 &push (&::islabel("_win32_segv_handler"));
461 &data_byte(0x64,0xff,0x30); # push %fs:(%eax)
462 &data_byte(0x64,0x89,0x20); # mov %esp,%fs:(%eax)
464 &mov ("edx","esp"); # put aside %esp
465 &add ("esp",-128); # 32 is enough but spec says 128
466 &movups ("xmm0",&QWP(0,"edi")); # copy-in context
468 &mov ("eax",&DWP(16,"edi"));
469 &movaps (&QWP(0,"esp"),"xmm0");
471 &mov (&DWP(16,"esp"),"eax");
473 &data_byte(0xf3,0x0f,0xa6,0xc8); # rep xsha1
474 &movaps ("xmm0",&QWP(0,"esp"));
475 &mov ("eax",&DWP(16,"esp"));
476 &mov ("esp","edx"); # restore %esp
477 if ($::win32 or $::coff) {
478 &data_byte(0x64,0x8f,0x05,0,0,0,0); # pop %fs:0
479 &lea ("esp",&DWP(4,"esp"));
481 &mov ("edi",&wparam(0));
482 &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
483 &mov (&DWP(16,"edi"),"eax");
487 &function_end_B("padlock_sha1_oneshot");
489 &function_begin_B("padlock_sha1_blocks");
492 &mov ("edi",&wparam(0));
493 &mov ("esi",&wparam(1));
494 &mov ("edx","esp"); # put aside %esp
495 &mov ("ecx",&wparam(2));
497 &movups ("xmm0",&QWP(0,"edi")); # copy-in context
499 &mov ("eax",&DWP(16,"edi"));
500 &movaps (&QWP(0,"esp"),"xmm0");
502 &mov (&DWP(16,"esp"),"eax");
504 &data_byte(0xf3,0x0f,0xa6,0xc8); # rep xsha1
505 &movaps ("xmm0",&QWP(0,"esp"));
506 &mov ("eax",&DWP(16,"esp"));
507 &mov ("esp","edx"); # restore %esp
508 &mov ("edi",&wparam(0));
509 &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
510 &mov (&DWP(16,"edi"),"eax");
514 &function_end_B("padlock_sha1_blocks");
516 &function_begin_B("padlock_sha256_oneshot");
520 &mov ("edi",&wparam(0));
521 &mov ("esi",&wparam(1));
522 &mov ("ecx",&wparam(2));
523 if ($::win32 or $::coff) {
524 &push (&::islabel("_win32_segv_handler"));
525 &data_byte(0x64,0xff,0x30); # push %fs:(%eax)
526 &data_byte(0x64,0x89,0x20); # mov %esp,%fs:(%eax)
528 &mov ("edx","esp"); # put aside %esp
530 &movups ("xmm0",&QWP(0,"edi")); # copy-in context
532 &movups ("xmm1",&QWP(16,"edi"));
533 &movaps (&QWP(0,"esp"),"xmm0");
535 &movaps (&QWP(16,"esp"),"xmm1");
537 &data_byte(0xf3,0x0f,0xa6,0xd0); # rep xsha256
538 &movaps ("xmm0",&QWP(0,"esp"));
539 &movaps ("xmm1",&QWP(16,"esp"));
540 &mov ("esp","edx"); # restore %esp
541 if ($::win32 or $::coff) {
542 &data_byte(0x64,0x8f,0x05,0,0,0,0); # pop %fs:0
543 &lea ("esp",&DWP(4,"esp"));
545 &mov ("edi",&wparam(0));
546 &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
547 &movups (&QWP(16,"edi"),"xmm1");
551 &function_end_B("padlock_sha256_oneshot");
553 &function_begin_B("padlock_sha256_blocks");
556 &mov ("edi",&wparam(0));
557 &mov ("esi",&wparam(1));
558 &mov ("ecx",&wparam(2));
559 &mov ("edx","esp"); # put aside %esp
561 &movups ("xmm0",&QWP(0,"edi")); # copy-in context
563 &movups ("xmm1",&QWP(16,"edi"));
564 &movaps (&QWP(0,"esp"),"xmm0");
566 &movaps (&QWP(16,"esp"),"xmm1");
568 &data_byte(0xf3,0x0f,0xa6,0xd0); # rep xsha256
569 &movaps ("xmm0",&QWP(0,"esp"));
570 &movaps ("xmm1",&QWP(16,"esp"));
571 &mov ("esp","edx"); # restore %esp
572 &mov ("edi",&wparam(0));
573 &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
574 &movups (&QWP(16,"edi"),"xmm1");
578 &function_end_B("padlock_sha256_blocks");
580 &function_begin_B("padlock_sha512_blocks");
583 &mov ("edi",&wparam(0));
584 &mov ("esi",&wparam(1));
585 &mov ("ecx",&wparam(2));
586 &mov ("edx","esp"); # put aside %esp
588 &movups ("xmm0",&QWP(0,"edi")); # copy-in context
590 &movups ("xmm1",&QWP(16,"edi"));
591 &movups ("xmm2",&QWP(32,"edi"));
592 &movups ("xmm3",&QWP(48,"edi"));
593 &movaps (&QWP(0,"esp"),"xmm0");
595 &movaps (&QWP(16,"esp"),"xmm1");
596 &movaps (&QWP(32,"esp"),"xmm2");
597 &movaps (&QWP(48,"esp"),"xmm3");
598 &data_byte(0xf3,0x0f,0xa6,0xe0); # rep xsha512
599 &movaps ("xmm0",&QWP(0,"esp"));
600 &movaps ("xmm1",&QWP(16,"esp"));
601 &movaps ("xmm2",&QWP(32,"esp"));
602 &movaps ("xmm3",&QWP(48,"esp"));
603 &mov ("esp","edx"); # restore %esp
604 &mov ("edi",&wparam(0));
605 &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
606 &movups (&QWP(16,"edi"),"xmm1");
607 &movups (&QWP(32,"edi"),"xmm2");
608 &movups (&QWP(48,"edi"),"xmm3");
612 &function_end_B("padlock_sha512_blocks");
614 &asciz ("VIA Padlock x86 module, CRYPTOGAMS by <appro\@openssl.org>");
618 # Essentially this variable belongs in thread local storage.
619 # Having this variable global on the other hand can only cause
620 # few bogus key reloads [if any at all on signle-CPU system],
621 # so we accept the penalty...
622 &set_label("padlock_saved_context",4);