Use AES hardware support on Apple arm64 and with GCC on aarch64 - #1
Closed
mtrossbach wants to merge 1 commit into
Closed
mtrossbach wants to merge 1 commit into
mtrossbach wants to merge 1 commit into
Conversation
On 64-bit Apple ARM platforms the NEON AES code was compiled, but the runtime check always reported that no hardware support is available, so the table-based software implementation was used. With GCC on aarch64 no hardware code path was compiled at all, unless the crypto extension was enabled via compiler flags. - Report AES hardware support on 64-bit Apple ARM platforms (macOS, iOS, iPadOS, tvOS, watchOS, visionOS) - Enable the NEON AES code path for GCC >= 6 on aarch64 via a function target attribute, analogous to the existing clang handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi Ulrich,
first of all, thank you for SQLite3 Multiple Ciphers! It's a great piece of work, and the modular cipher design made it easy to find my way around the code.
While measuring AES performance on Apple Silicon, I noticed two gaps in
src/aes_hardware.c:This small PR closes both. The encryption output doesn't change in any way; the only difference is that the much faster hardware path is now picked wherever it's available.
What changes (only
src/aes_hardware.c, +23 lines)aesHardwareAvailableOnPlatform()had no Apple branch and fell through toreturn 0. As a result,aes128cbc,aes256cbcandsqlcipherused the table-based software AES. All 64-bit ARM processors Apple uses (A7 and later, M1 and later) implement the ARMv8 cryptographic extensions, so the new branch simply returns 1. OpenSSL and Botan make the same assumption, and the bundled libaegis already relies on it (src/aegis/common/cpu.c).-march=...+cryptono hardware path was compiled. For GCC ≥ 6, the NEON code path is now enabled with the function attributetarget("+crypto"), just like the existing clang handling. The runtime check (getauxval(AT_HWCAP) & HWCAP_AES) was already in place.A nice side effect: the existing CI jobs on
macos-latestandubuntu-24.04-arm(gcc) will now really exercise the hardware code paths. At the moment they quietly test the software fallback.Performance (Apple M4 Max)
aes256cbcdatabase, 100,000 rows × 1,000 bytes:How I tested it
test1–test4,sqlciphertest) produce identical output before and after the change.aes128cbc,aes256cbcandsqlcipher.aesHardwareAvailable()goes from 0 to 1, and the known-answer test passes.autoreconf; ./configure; make):ARM64 machines aren't always at hand, so I tried to test as broadly as I could. I'm happy to run anything else you'd like to see. If you prefer a different approach, I'll gladly adjust it. One example would be a
sysctlbyname()-based check on Apple instead of the constant.While digging into the AES code, I came across a few more small things around AES and random number generation. I'd like to propose them separately, each as its own small PR or issue, so they stay easy to review.
Thanks a lot for taking a look!
Best regards,
Markus