2 # Copyright 2011-2023 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"));
120 &set_label("bswap_loop");
121 &mov ("eax",&DWP(0,"edx"));
123 &mov (&DWP(0,"edx"),"eax");
124 &lea ("edx",&DWP(4,"edx"));
126 &jnz (&label("bswap_loop"));
128 &function_end_B("padlock_key_bswap");
130 # This is heuristic key context tracing. At first one
131 # believes that one should use atomic swap instructions,
132 # but it's not actually necessary. Point is that if
133 # padlock_saved_context was changed by another thread
134 # after we've read it and before we compare it with ctx,
135 # our key *shall* be reloaded upon thread context switch
136 # and we are therefore set in either case...
137 &static_label("padlock_saved_context");
139 &function_begin_B("padlock_verify_context");
140 &mov ($ctx,&wparam(0));
141 &lea ("eax",($::win32 or $::coff) ? &DWP(&label("padlock_saved_context")) :
142 &DWP(&label("padlock_saved_context")."-".&label("verify_pic_point")));
144 &call ("_padlock_verify_ctx");
145 &set_label("verify_pic_point");
146 &lea ("esp",&DWP(4,"esp"));
148 &function_end_B("padlock_verify_context");
150 &function_begin_B("_padlock_verify_ctx");
151 &add ("eax",&DWP(0,"esp")) if(!($::win32 or $::coff));# &padlock_saved_context
152 &bt (&DWP(4,"esp"),30); # eflags
153 &jnc (&label("verified"));
154 &cmp ($ctx,&DWP(0,"eax"));
155 &je (&label("verified"));
158 &set_label("verified");
159 &mov (&DWP(0,"eax"),$ctx);
161 &function_end_B("_padlock_verify_ctx");
163 &function_begin_B("padlock_reload_key");
167 &function_end_B("padlock_reload_key");
169 &function_begin_B("padlock_aes_block");
173 &mov ($out,&wparam(0)); # must be 16-byte aligned
174 &mov ($inp,&wparam(1)); # must be 16-byte aligned
175 &mov ($ctx,&wparam(2));
177 &lea ("ebx",&DWP(32,$ctx)); # key
178 &lea ($ctx,&DWP(16,$ctx)); # control word
179 &data_byte(0xf3,0x0f,0xa7,0xc8); # rep xcryptecb
184 &function_end_B("padlock_aes_block");
187 my ($mode,$opcode) = @_;
188 # int padlock_$mode_encrypt(void *out, const void *inp,
189 # struct padlock_cipher_data *ctx, size_t len);
190 &function_begin("padlock_${mode}_encrypt");
191 &mov ($out,&wparam(0));
192 &mov ($inp,&wparam(1));
193 &mov ($ctx,&wparam(2));
194 &mov ($len,&wparam(3));
196 &jnz (&label("${mode}_abort"));
198 &jnz (&label("${mode}_abort"));
199 &lea ("eax",($::win32 or $::coff) ? &DWP(&label("padlock_saved_context")) :
200 &DWP(&label("padlock_saved_context")."-".&label("${mode}_pic_point")));
203 &call ("_padlock_verify_ctx");
204 &set_label("${mode}_pic_point");
205 &lea ($ctx,&DWP(16,$ctx)); # control word
207 if ($mode eq "ctr32") {
208 &movq ("mm0",&QWP(-16,$ctx)); # load [upper part of] counter
211 &test (&DWP(0,$ctx),1<<5); # align bit in control word
212 &jnz (&label("${mode}_aligned"));
214 &setz ("al"); # !out_misaligned
216 &setz ("bl"); # !inp_misaligned
218 &jnz (&label("${mode}_aligned"));
221 &mov ($chunk,$PADLOCK_CHUNK);
222 ¬ ("eax"); # out_misaligned?-1:0
223 &lea ("ebp",&DWP(-24,"esp"));
225 &cmovc ($chunk,$len); # chunk=len>PADLOCK_CHUNK?PADLOCK_CHUNK:len
226 &and ("eax",$chunk); # out_misaligned?chunk:0
229 &and ($chunk,$PADLOCK_CHUNK-1); # chunk=len%PADLOCK_CHUNK
230 &lea ("esp",&DWP(0,"eax","ebp")); # alloca
231 &mov ("eax",$PADLOCK_CHUNK);
232 &cmovz ($chunk,"eax"); # chunk=chunk?:PADLOCK_CHUNK
236 &mov (&DWP(16,"ebp"),"eax");
237 if ($PADLOCK_PREFETCH{$mode}) {
239 &ja (&label("${mode}_loop"));
240 &mov ("eax",$inp); # check if prefetch crosses page
245 &and ("eax",0xfff); # distance to page boundary
246 &cmp ("eax",$PADLOCK_PREFETCH{$mode});
247 &mov ("eax",-$PADLOCK_PREFETCH{$mode});
248 &cmovae ("eax",$chunk); # mask=distance<prefetch?-prefetch:-1
250 &jz (&label("${mode}_unaligned_tail"));
252 &jmp (&label("${mode}_loop"));
254 &set_label("${mode}_loop",16);
255 &mov (&DWP(0,"ebp"),$out); # save parameters
256 &mov (&DWP(4,"ebp"),$inp);
257 &mov (&DWP(8,"ebp"),$len);
259 &mov (&DWP(12,"ebp"),$chunk); # chunk
260 if ($mode eq "ctr32") {
261 &mov ("ecx",&DWP(-4,$ctx));
263 &mov ("eax",&DWP(-8,$ctx)); # borrow $len
264 &set_label("${mode}_prepare");
265 &mov (&DWP(12,"esp",$out),"ecx");
267 &movq (&QWP(0,"esp",$out),"mm0");
269 &mov (&DWP(8,"esp",$out),"eax");
271 &lea ($out,&DWP(16,$out));
273 &jb (&label("${mode}_prepare"));
275 &mov (&DWP(-4,$ctx),"ecx");
276 &lea ($inp,&DWP(0,"esp"));
277 &lea ($out,&DWP(0,"esp"));
280 &test ($out,0x0f); # out_misaligned
281 &cmovnz ($out,"esp");
282 &test ($inp,0x0f); # inp_misaligned
283 &jz (&label("${mode}_inp_aligned"));
285 &data_byte(0xf3,0xa5); # rep movsl
289 &set_label("${mode}_inp_aligned");
291 &lea ("eax",&DWP(-16,$ctx)); # ivp
292 &lea ("ebx",&DWP(16,$ctx)); # key
293 &shr ($len,4); # len/=AES_BLOCK_SIZE
294 &data_byte(0xf3,0x0f,0xa7,$opcode); # rep xcrypt*
295 if ($mode !~ /ecb|ctr/) {
296 &movaps ("xmm0",&QWP(0,"eax"));
297 &movaps (&QWP(-16,$ctx),"xmm0"); # copy [or refresh] iv
299 &mov ($out,&DWP(0,"ebp")); # restore parameters
300 &mov ($chunk,&DWP(12,"ebp"));
301 if ($mode eq "ctr32") {
302 &mov ($inp,&DWP(4,"ebp"));
304 &set_label("${mode}_xor");
305 &movups ("xmm1",&QWP(0,$inp,$len));
306 &lea ($len,&DWP(16,$len));
307 &pxor ("xmm1",&QWP(-16,"esp",$len));
308 &movups (&QWP(-16,$out,$len),"xmm1");
310 &jb (&label("${mode}_xor"));
313 &jz (&label("${mode}_out_aligned"));
315 &lea ($inp,&DWP(0,"esp"));
317 &data_byte(0xf3,0xa5); # rep movsl
319 &set_label("${mode}_out_aligned");
320 &mov ($inp,&DWP(4,"ebp"));
322 &mov ($len,&DWP(8,"ebp"));
326 &mov ($chunk,$PADLOCK_CHUNK);
327 if (!$PADLOCK_PREFETCH{$mode}) {
328 &jnz (&label("${mode}_loop"));
330 &jz (&label("${mode}_break"));
332 &jae (&label("${mode}_loop"));
334 &set_label("${mode}_unaligned_tail");
338 &sub ("esp","eax"); # alloca
339 &mov ("eax", $out); # save parameters
342 &lea ($out,&DWP(0,"esp"));
343 &data_byte(0xf3,0xa5); # rep movsl
345 &mov ($out,"eax"); # restore parameters
347 &jmp (&label("${mode}_loop"));
349 &set_label("${mode}_break",16);
351 if ($mode ne "ctr32") {
353 &je (&label("${mode}_done"));
355 &pxor ("xmm0","xmm0");
356 &lea ("eax",&DWP(0,"esp"));
357 &set_label("${mode}_bzero");
358 &movaps (&QWP(0,"eax"),"xmm0");
359 &lea ("eax",&DWP(16,"eax"));
361 &ja (&label("${mode}_bzero"));
363 &set_label("${mode}_done");
364 &mov ("ebp",&DWP(16,"ebp"));
365 &lea ("esp",&DWP(24,"ebp"));
366 if ($mode ne "ctr32") {
367 &jmp (&label("${mode}_exit"));
369 &set_label("${mode}_aligned",16);
370 if ($PADLOCK_PREFETCH{$mode}) {
371 &lea ("ebp",&DWP(0,$inp,$len));
373 &and ("ebp",0xfff); # distance to page boundary
375 &cmp ("ebp",$PADLOCK_PREFETCH{$mode});
376 &mov ("ebp",$PADLOCK_PREFETCH{$mode}-1);
377 &cmovae ("ebp","eax");
378 &and ("ebp",$len); # remainder
380 &jz (&label("${mode}_aligned_tail"));
382 &lea ("eax",&DWP(-16,$ctx)); # ivp
383 &lea ("ebx",&DWP(16,$ctx)); # key
384 &shr ($len,4); # len/=AES_BLOCK_SIZE
385 &data_byte(0xf3,0x0f,0xa7,$opcode); # rep xcrypt*
386 if ($mode ne "ecb") {
387 &movaps ("xmm0",&QWP(0,"eax"));
388 &movaps (&QWP(-16,$ctx),"xmm0"); # copy [or refresh] iv
390 if ($PADLOCK_PREFETCH{$mode}) {
392 &jz (&label("${mode}_exit"));
394 &set_label("${mode}_aligned_tail");
396 &lea ("ebp",&DWP(-24,"esp"));
402 &mov (&DWP(16,"ebp"),"eax");
403 &mov ("eax", $out); # save parameters
406 &lea ($out,&DWP(0,"esp"));
407 &data_byte(0xf3,0xa5); # rep movsl
409 &mov ($out,"eax"); # restore parameters
411 &jmp (&label("${mode}_loop"));
413 &set_label("${mode}_exit"); }
415 &lea ("esp",&DWP(4,"esp")); # popf
416 &emms () if ($mode eq "ctr32");
417 &set_label("${mode}_abort");
418 &function_end("padlock_${mode}_encrypt");
421 &generate_mode("ecb",0xc8);
422 &generate_mode("cbc",0xd0);
423 &generate_mode("cfb",0xe0);
424 &generate_mode("ofb",0xe8);
425 &generate_mode("ctr32",0xc8); # yes, it implements own CTR with ECB opcode,
426 # because hardware CTR was introduced later
427 # and even has errata on certain C7 stepping.
428 # own implementation *always* works, though
429 # ~15% slower than dedicated hardware...
431 &function_begin_B("padlock_xstore");
433 &mov ("edi",&wparam(0));
434 &mov ("edx",&wparam(1));
435 &data_byte(0x0f,0xa7,0xc0); # xstore
438 &function_end_B("padlock_xstore");
440 &function_begin_B("_win32_segv_handler");
441 &mov ("eax",1); # ExceptionContinueSearch
442 &mov ("edx",&wparam(0)); # *ExceptionRecord
443 &mov ("ecx",&wparam(2)); # *ContextRecord
444 &cmp (&DWP(0,"edx"),0xC0000005) # ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION
445 &jne (&label("ret"));
446 &add (&DWP(184,"ecx"),4); # skip over rep sha*
447 &mov ("eax",0); # ExceptionContinueExecution
450 &function_end_B("_win32_segv_handler");
451 &safeseh("_win32_segv_handler") if ($::win32);
453 &function_begin_B("padlock_sha1_oneshot");
457 &mov ("edi",&wparam(0));
458 &mov ("esi",&wparam(1));
459 &mov ("ecx",&wparam(2));
460 if ($::win32 or $::coff) {
461 &push (&::islabel("_win32_segv_handler"));
462 &data_byte(0x64,0xff,0x30); # push %fs:(%eax)
463 &data_byte(0x64,0x89,0x20); # mov %esp,%fs:(%eax)
465 &mov ("edx","esp"); # put aside %esp
466 &add ("esp",-128); # 32 is enough but spec says 128
467 &movups ("xmm0",&QWP(0,"edi")); # copy-in context
469 &mov ("eax",&DWP(16,"edi"));
470 &movaps (&QWP(0,"esp"),"xmm0");
472 &mov (&DWP(16,"esp"),"eax");
474 &data_byte(0xf3,0x0f,0xa6,0xc8); # rep xsha1
475 &movaps ("xmm0",&QWP(0,"esp"));
476 &mov ("eax",&DWP(16,"esp"));
477 &mov ("esp","edx"); # restore %esp
478 if ($::win32 or $::coff) {
479 &data_byte(0x64,0x8f,0x05,0,0,0,0); # pop %fs:0
480 &lea ("esp",&DWP(4,"esp"));
482 &mov ("edi",&wparam(0));
483 &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
484 &mov (&DWP(16,"edi"),"eax");
488 &function_end_B("padlock_sha1_oneshot");
490 &function_begin_B("padlock_sha1_blocks");
493 &mov ("edi",&wparam(0));
494 &mov ("esi",&wparam(1));
495 &mov ("edx","esp"); # put aside %esp
496 &mov ("ecx",&wparam(2));
498 &movups ("xmm0",&QWP(0,"edi")); # copy-in context
500 &mov ("eax",&DWP(16,"edi"));
501 &movaps (&QWP(0,"esp"),"xmm0");
503 &mov (&DWP(16,"esp"),"eax");
505 &data_byte(0xf3,0x0f,0xa6,0xc8); # rep xsha1
506 &movaps ("xmm0",&QWP(0,"esp"));
507 &mov ("eax",&DWP(16,"esp"));
508 &mov ("esp","edx"); # restore %esp
509 &mov ("edi",&wparam(0));
510 &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
511 &mov (&DWP(16,"edi"),"eax");
515 &function_end_B("padlock_sha1_blocks");
517 &function_begin_B("padlock_sha256_oneshot");
521 &mov ("edi",&wparam(0));
522 &mov ("esi",&wparam(1));
523 &mov ("ecx",&wparam(2));
524 if ($::win32 or $::coff) {
525 &push (&::islabel("_win32_segv_handler"));
526 &data_byte(0x64,0xff,0x30); # push %fs:(%eax)
527 &data_byte(0x64,0x89,0x20); # mov %esp,%fs:(%eax)
529 &mov ("edx","esp"); # put aside %esp
531 &movups ("xmm0",&QWP(0,"edi")); # copy-in context
533 &movups ("xmm1",&QWP(16,"edi"));
534 &movaps (&QWP(0,"esp"),"xmm0");
536 &movaps (&QWP(16,"esp"),"xmm1");
538 &data_byte(0xf3,0x0f,0xa6,0xd0); # rep xsha256
539 &movaps ("xmm0",&QWP(0,"esp"));
540 &movaps ("xmm1",&QWP(16,"esp"));
541 &mov ("esp","edx"); # restore %esp
542 if ($::win32 or $::coff) {
543 &data_byte(0x64,0x8f,0x05,0,0,0,0); # pop %fs:0
544 &lea ("esp",&DWP(4,"esp"));
546 &mov ("edi",&wparam(0));
547 &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
548 &movups (&QWP(16,"edi"),"xmm1");
552 &function_end_B("padlock_sha256_oneshot");
554 &function_begin_B("padlock_sha256_blocks");
557 &mov ("edi",&wparam(0));
558 &mov ("esi",&wparam(1));
559 &mov ("ecx",&wparam(2));
560 &mov ("edx","esp"); # put aside %esp
562 &movups ("xmm0",&QWP(0,"edi")); # copy-in context
564 &movups ("xmm1",&QWP(16,"edi"));
565 &movaps (&QWP(0,"esp"),"xmm0");
567 &movaps (&QWP(16,"esp"),"xmm1");
569 &data_byte(0xf3,0x0f,0xa6,0xd0); # rep xsha256
570 &movaps ("xmm0",&QWP(0,"esp"));
571 &movaps ("xmm1",&QWP(16,"esp"));
572 &mov ("esp","edx"); # restore %esp
573 &mov ("edi",&wparam(0));
574 &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
575 &movups (&QWP(16,"edi"),"xmm1");
579 &function_end_B("padlock_sha256_blocks");
581 &function_begin_B("padlock_sha512_blocks");
584 &mov ("edi",&wparam(0));
585 &mov ("esi",&wparam(1));
586 &mov ("ecx",&wparam(2));
587 &mov ("edx","esp"); # put aside %esp
589 &movups ("xmm0",&QWP(0,"edi")); # copy-in context
591 &movups ("xmm1",&QWP(16,"edi"));
592 &movups ("xmm2",&QWP(32,"edi"));
593 &movups ("xmm3",&QWP(48,"edi"));
594 &movaps (&QWP(0,"esp"),"xmm0");
596 &movaps (&QWP(16,"esp"),"xmm1");
597 &movaps (&QWP(32,"esp"),"xmm2");
598 &movaps (&QWP(48,"esp"),"xmm3");
599 &data_byte(0xf3,0x0f,0xa6,0xe0); # rep xsha512
600 &movaps ("xmm0",&QWP(0,"esp"));
601 &movaps ("xmm1",&QWP(16,"esp"));
602 &movaps ("xmm2",&QWP(32,"esp"));
603 &movaps ("xmm3",&QWP(48,"esp"));
604 &mov ("esp","edx"); # restore %esp
605 &mov ("edi",&wparam(0));
606 &movups (&QWP(0,"edi"),"xmm0"); # copy-out context
607 &movups (&QWP(16,"edi"),"xmm1");
608 &movups (&QWP(32,"edi"),"xmm2");
609 &movups (&QWP(48,"edi"),"xmm3");
613 &function_end_B("padlock_sha512_blocks");
615 &asciz ("VIA Padlock x86 module, CRYPTOGAMS by <appro\@openssl.org>");
619 # Essentially this variable belongs in thread local storage.
620 # Having this variable global on the other hand can only cause
621 # few bogus key reloads [if any at all on signle-CPU system],
622 # so we accept the penalty...
623 &set_label("padlock_saved_context",4);