Skip to content

Remove x86/x86_64 BSAES and AES_ASM support - #9677

Closed
bernd-edlinger wants to merge 3 commits into
openssl:masterfrom
bernd-edlinger:remove_bsaes_and_aes_assembler
Closed

bernd-edlinger wants to merge 3 commits into
openssl:masterfrom
bernd-edlinger:remove_bsaes_and_aes_assembler

Conversation

@bernd-edlinger

Copy link
Copy Markdown
Member

This leaves VPAES and AESNI support.
The VPAES performance is comparable but BSAES is not
completely constant time. There are table lookups
using secret key data in AES_set_encrypt/decrypt_key
and in ctr mode short data uses the non-constant
time AES_encrypt function instead of bit-slicing.
Furthermore the AES_ASM is by far outperformed
by recent GCC versions.
Since BSAES calls back to AES_ASM for short
data blocks the performance on those is also
worse than the pure software implementaion.

Fixes: #9640

Checklist
  • documentation is added or updated
  • tests are added or updated

@bernd-edlinger bernd-edlinger added the branch: master Applies to master branch label Aug 23, 2019
@bernd-edlinger

Copy link
Copy Markdown
Member Author

WIP because I cant get linux-x86 to work
all assembler modules are not getting the define OPENSSL_IA32_SSE2
so this is not working:
../openssl/Configure linux-x86

only if I add -DOPENSSL_IA32_SSE2 manually:
../openssl/Configure linux-x86 -DOPENSSL_IA32_SSE2
and
../openssl/Configure linux-x86 no-sse2
../openssl/Configure linux-x86 no-asm

@levitte

levitte commented Aug 23, 2019

Copy link
Copy Markdown
Member

Actually, it seems that the definition of OPENSSL_IA32_SSE2 got lost entirely... I'll check that out.

@levitte

levitte commented Aug 23, 2019

Copy link
Copy Markdown
Member

Nope, I was wrong about that, it does get defined in crypto/bn/build.info... or well, should

@bernd-edlinger

Copy link
Copy Markdown
Member Author

Yes, the define is there but only in the C and .s modules when they get compiled.
But the asm-generate perl scripts need the trigger to generate sse2 or higher.
That is only for 32 bit, 64 bit does not depend on it.

@levitte

levitte commented Aug 23, 2019

Copy link
Copy Markdown
Member

See #9679

@bernd-edlinger
bernd-edlinger force-pushed the remove_bsaes_and_aes_assembler branch from 45ac213 to 8981d50 Compare August 23, 2019 19:55
@bernd-edlinger bernd-edlinger changed the title WIP: Remove x86/x86_64 BSAES and AES_ASM support Remove x86/x86_64 BSAES and AES_ASM support Aug 23, 2019
@bernd-edlinger

Copy link
Copy Markdown
Member Author

Rebased, taking out of WIP.

@slontis

slontis commented Aug 27, 2019

Copy link
Copy Markdown
Member

@levitte does this need a CHANGES entry?

@bernd-edlinger

Copy link
Copy Markdown
Member Author

Is this OK for trunk?

@levitte

levitte commented Sep 13, 2019

Copy link
Copy Markdown
Member

@levitte does this need a CHANGES entry?

Is this a user visible change? From CONTRIBUTING:

  1. For user visible changes (API changes, behaviour changes, ...), consider adding a note in CHANGES. ...

I would say that yeah, if you like, but not mandatory.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better if you use defined(AES_ASM) than !defined(OPENSSL_NO_ASM), since there's potential lack of asm support even if configured without no-asm

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that will not work, because AES_ASM is no longer defined
in all cases.
Previously we had either AES_ASM or AES_ASM + BSAES_ASM + VPAES_ASM
or ASE_ASM + VPASE_ASM
but now we may also have AES_VPAES only.
But even without VPAES and BSAES we could still have aesni_encrypt/decrypt.
I did not remove aesni, that is constant time, but it needs a AESNI_CAPAPLE in
the ia32cap_P

@bernd-edlinger

Copy link
Copy Markdown
Member Author

Not sure if that is user visible, it does not change any API interface, just
replaces one implementation by a slightly faster one. Both are not constant time,
so the user should still hope his CPU has native AES support.

Comment thread crypto/aes/build.info Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this seems counterintuitive, but AES_ASM should still be defined

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to do that, but aes_core.c will stop to function if AES_ASM is defined:

diff --git a/crypto/aes/build.info b/crypto/aes/build.info
index fb67a7b..2020268 100644
--- a/crypto/aes/build.info
+++ b/crypto/aes/build.info
@@ -5,12 +5,12 @@ IF[{- !$disabled{asm} -}]
   $AESASM_x86=aes_core.c aes_cbc.c
   $AESDEF_x86=
   $AESASM_x86_sse2=vpaes-x86.s aesni-x86.s
-  $AESDEF_x86_sse2=VPAES_ASM
+  $AESDEF_x86_sse2=AES_ASM VPAES_ASM
 
   $AESASM_x86_64=\
         aes_core.c aes_cbc.c vpaes-x86_64.s aesni-x86_64.s \
         aesni-sha1-x86_64.s aesni-sha256-x86_64.s aesni-mb-x86_64.s
-  $AESDEF_x86_64=VPAES_ASM
+  $AESDEF_x86_64=AES_ASM VPAES_ASM
 
   $AESASM_ia64=aes_core.c aes_cbc.c aes-ia64.s
   $AESDEF_ia64=AES_ASM
diff --git a/crypto/include/internal/ciphermode_platform.h b/crypto/include/internal/ciphermode_platform.h
index e55654f..6001428 100644
--- a/crypto/include/internal/ciphermode_platform.h
+++ b/crypto/include/internal/ciphermode_platform.h
@@ -94,7 +94,7 @@ void AES_xts_decrypt(const unsigned char *inp, unsigned char *out, size_t len,
 #  endif
 # endif /* OPENSSL_CPUID_OBJ */
 
-# if     !defined(OPENSSL_NO_ASM) &&                     (  \
+# if     defined(AES_ASM) &&                             (  \
          ((defined(__i386)       || defined(__i386__)    || \
            defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \
          defined(__x86_64)       || defined(__x86_64__)  || \

.->

gcc  -Iinclude  -pthread -m64 -Wa,--noexecstack -Wall -O3 -DNDEBUG -DOPENSSL_IA32_SSE2  -MMD -MF test/buildtest_c_asn1t-bin-buildtest_asn1t.d.tmp -MT test/buildtest_c_asn1t-bin-buildtest_asn1t.o -c -o test/buildtest_c_asn1t-bin-buildtest_asn1t.o test/buildtest_asn1t.c
crypto/aes/fips-dso-aes_cbc.o: In function `AES_cbc_encrypt':
aes_cbc.c:(.text+0x8): undefined reference to `AES_decrypt'
aes_cbc.c:(.text+0x1b): undefined reference to `AES_encrypt'
crypto/aes/fips-dso-aes_ecb.o: In function `AES_ecb_encrypt':
aes_ecb.c:(.text+0x6): undefined reference to `AES_decrypt'
aes_ecb.c:(.text+0x11): undefined reference to `AES_encrypt'
providers/common/ciphers/fips-dso-cipher_aes_hw.o: In function `cipher_hw_aes_initkey':
cipher_aes_hw.c:(.text+0x3d): undefined reference to `AES_encrypt'
cipher_aes_hw.c:(.text+0x120): undefined reference to `AES_decrypt'
providers/common/ciphers/fips-dso-cipher_ccm_hw.o: In function `ccm_generic_aes_initkey':
cipher_ccm_hw.c:(.text+0x252): undefined reference to `AES_encrypt'
providers/common/ciphers/fips-dso-cipher_gcm_hw.o: In function `generic_aes_gcm_initkey':
cipher_gcm_hw.c:(.text+0x299): undefined reference to `AES_encrypt'
collect2: error: ld returned 1 exit status

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Figured it out by looking at how this is done in 1.1.1. From Configure:

        push @{$config{lib_defines}}, "AES_ASM" if ($target{aes_asm_src} =~ m/\baes-/);;

So you're right, AES_ASM shouldn't be defined here since only aes_core.c and aes_cbc.c are used.

Comment thread crypto/aes/build.info Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well

@levitte levitte Sep 13, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, you still need to add AES_ASM here (because vpaes-x86_64.pl)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clarify, why?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, AES_ASM is meant to be added at any time there is any assembler support for AES added, such as vpaes-x86_64.pl...

The history behind this is that OpenSSL used to have a bunch of {something}_asm_src variables, which became config target attributes in 1.1.x (see Configurations/00-base-templates.conf, and as soon as those had something that could be identified as assembler amongst their files, some kind of macro is created to indicate what assembler support is added. These macros were previously added with special code in Configure...

Note that there are some files that aren't really assembler or perlasm but still generate an indicator macro... I remember that @dot-asm sometimes said that ASM should really be called AUX because of that, i.e. some units are auxilliary code, even thought most of them are assembler or perlasm.

Unfortunately, the situation is a bit messy, i.e. not all C code listens to {SOMETHING}_ASM macros, but check compiler macros for diverse hardware and simply assumes that the corresponding auxilliary files are built in as well, so things are sometimes hard to keep in sync.

I would very much like to see some kind of clean-up to make all this driven by build.info data, but that's a major task, and it's not been high priority so far ("It works, don't fix it" 'n all that...). If you're up for the task, feel free, but I think that should happen in a separate PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the AES_ASM probably needs to be renamed globally to something new,
Probably there should be a define for AESNI_ASM, and maybe also for AES_GCM.
But I don't know for sure.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be the bigger clean-up job...

@dot-asm

dot-asm commented Sep 18, 2019

Copy link
Copy Markdown
Contributor

Furthermore the AES_ASM is by far outperformed by recent GCC versions.

This is a misleading assertion. If C version implemented same counter-measures as scalar assembly module, it would have been slower. To put it differently, counter-measures costs, and C version implements none. Apparent better performance is not a "recent GCC" phenomenon, it has been case for years. Of course one can argue that scalar assembly counter-measures are not sufficient by modern standards. But thing is that modern standards hold for modern processors, on which scalar module is not supposed to execute. So real question is how effective the counter-measures are on old systems. Are they shared-cache multi-core? If not, is timer interrupt's resolution sufficient? Of course one can still argue that one doesn't care about such old systems. Then say explicitly that, not that it's "slower."

@bernd-edlinger

Copy link
Copy Markdown
Member Author

Oh, sorry, I did not want to insult you.
It is just that my tools flagged the removed code as being not constant time,
as well as the no-asm variant of course, while on the other hand the perfomance
numbers were not as promised:

# aes-*-cbc benchmarks are improved by >70% [compared to gcc 3.3.2 on

However some counter-measures in the C code AES implementation would
be good, since many use no-asm, but having an option to turn on some
side-channel protection, or even constant time implementation would be great.

@dot-asm

dot-asm commented Sep 18, 2019

Copy link
Copy Markdown
Contributor

Oh, sorry, I did not want to insult you.

??? Did I sound insulted? I care only about accuracy, so if anything was insulted, it was accuracy.

the perfomance numbers were not as promised:

Next line reads "on Opteron 240 CPU," which is none-shared-cache processor, so CBC takes faster path [for larger blocks]. Did you fail to confirm it on the said processor with mentioned compiler? Well, not that I can confirm it by now, what I'm saying is rather that you can't really expect same coefficient on arbitrary system. As implied, key question is if cache is shared among multiple cores. Chances are that if you try to compare it on a contemporary processor, CBC will adhere to slow path at all input lengths, and you'll always observe worse performance...

@bernd-edlinger
bernd-edlinger force-pushed the remove_bsaes_and_aes_assembler branch from 8981d50 to ddcbbac Compare October 1, 2019 10:30
@bernd-edlinger

Copy link
Copy Markdown
Member Author

Well, when removing BSAES is not an option, then
it would be better to use VPAES instead of BSAES if both are possible.
Currently BSAES is slower and using non-constant time code for key schedue
and small data blocks.
So which way will we go?

@levitte

levitte commented Jan 16, 2020

Copy link
Copy Markdown
Member

A rebase might be a good direction to start on 😉

@bernd-edlinger
bernd-edlinger force-pushed the remove_bsaes_and_aes_assembler branch from ddcbbac to ca11107 Compare January 16, 2020 21:57
@bernd-edlinger

Copy link
Copy Markdown
Member Author

Rebase done.

@davidben

Copy link
Copy Markdown
Contributor

Well, when removing BSAES is not an option, then
it would be better to use VPAES instead of BSAES if both are possible.
Currently BSAES is slower and using non-constant time code for key schedue
and small data blocks.
So which way will we go?

For BoringSSL, we did a handful of things:

  • On x86_64, the VPAES and BSAES gap got much smaller after we implemented a variation of the _vpaes_encrypt_2x optimization from vpaes-armv8.pl for vpaes_ctr32_encrypt_blocks. (Note _vpaes_encrypt_2x isn't actually used anywhere in vpaes-armv8.pl, which is itself a missed opportunity.) With that done, we decided it was no longer worth carrying BSAES for x86_64.
  • On armv7, we translated vpaes-armv8.pl to armv7 to cover the non-parallel modes on NEON-capable chips. (Most of our 32-bit ARM callers require NEON at this point.) We weren't able to get the _vpaes_encrypt_2x strategy to help much on armv7. It seems armv7 NEON doesn't have great byte shufflers. Instead, we patched out the bsaes-armv7 to aes-armv4 fallback paths (at a perf cost) and wrote code to convert between vpaes-armv7 and bsaes-armv7 key schedule formats as needed when passing large inputs to ctr128_f.

The armv7 solution was a bit fussy. At the time, we didn't have the newly-written fallback implementation. I haven't looked at whether it would now make sense to use a simpler strategy.

The C bits have probably diverged, but I'm happy to upstream whatever assembly changes are of interest.

@bernd-edlinger

Copy link
Copy Markdown
Member Author

I'm happy to upstream whatever assembly changes are of interest.

Yes, sounds good, since armv7 will continue to use BSAES, even with this PR installed.

@davidben

Copy link
Copy Markdown
Contributor

Here's the x86_64 bit: #10884

@bernd-edlinger
bernd-edlinger force-pushed the remove_bsaes_and_aes_assembler branch from e5c33c2 to b49d02f Compare February 12, 2020 20:50
@bernd-edlinger

Copy link
Copy Markdown
Member Author

rebased.

@bernd-edlinger

Copy link
Copy Markdown
Member Author

This is an unhealthy situation that we cannot continue with this PR.
Furthermore I repeatedly ran into the duplicate implementaion of
the AES code in evp and in the provider, and fixed the wrog one.

This must cost you guys also some time. I guess I will clean up
that next. but I do not want to wait for this PR to happen any longer.

bernd-edlinger and others added 3 commits March 28, 2020 05:56
This leaves VPAES and AESNI support.
The VPAES performance is comparable but BSAES is not
completely constant time. There are table lookups
using secret key data in AES_set_encrypt/decrypt_key
and in ctr mode short data uses the non-constant
time AES_encrypt function instead of bit-slicing.
Furthermore the AES_ASM is by far outperformed
by recent GCC versions.
Since BSAES calls back to AES_ASM for short
data blocks the performance on those is also
worse than the pure software implementation.

Fixes: openssl#9640
This adds an analog to the (currently unused) "2x" optimization from
vpaes-armv8.pl and a ctr128_f implementation.
@bernd-edlinger
bernd-edlinger force-pushed the remove_bsaes_and_aes_assembler branch from b49d02f to 2807606 Compare March 28, 2020 04:57
@bernd-edlinger

Copy link
Copy Markdown
Member Author

Rebased, as you can see those assembler modules need adjustments all the time,
this costs everyone's time so why dont we remove them now?

@bernd-edlinger

Copy link
Copy Markdown
Member Author

This is especially paradox situation, as they are already removed in 1.1.1

@bernd-edlinger

Copy link
Copy Markdown
Member Author

Note this is part of my attempt to remove non-constant time AES code

@t8m

t8m commented Jul 3, 2024

Copy link
Copy Markdown
Member

Should this be revived?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

branch: master Applies to master branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AMD Phenom II X4 945: aes-128-ctr no-asm faster than default?

6 participants