Repository navigation
Serve System.Security.Cryptography from the framework's IL on macOS and Windows - #531
Merged
Merged
Conversation
Per-call cost on small inputs and throughput on 1 MiB for SHA-1/256/512, MD5, HMAC-SHA256, IncrementalHash, AES-CBC/ECB, CryptoStream, PBKDF2 and RandomNumberGenerator, csharpc against NativeAOT on Linux, macOS and Windows. Committed before serving System.Security.Cryptography from the framework's IL on macOS and Windows (#449), so the first run measures csharpc's managed copies there.
…nd Windows macOS and Windows compiled the X.509/RSA/TLS half of the assembly from the framework's IL but kept csharpc's managed copies for hashing, HMAC, AES, random numbers, PBKDF2, SHA-3 and HashAlgorithmName, which lacked HMAC instances, KMAC, SHAKE, HKDF, AesGcm/AesCcm/ChaCha20Poly1305 and TripleDES and were 5x to 135x slower than NativeAOT on several operations. Both assemblies are now the framework's IL whole, as on Linux and as under NativeAOT: CNG on Windows, the NativeAOT pack's Apple crypto shim on macOS. - The copies (Hashing.cs, Sha3, Pbkdf2, SymmetricCrypto, RandomNumberGenerator, HashAlgorithmName, Rsa, RSAParameters, the TLS and Negotiate stand-ins) are compiled for WASI only, which has no OS crypto and no NativeAOT target. The macOS CommonCrypto and Windows BCrypt block-cipher glue is deleted (AesNative.cs keeps only the helpers WASI's copies use, as CryptoHelpers.cs). - The native TLS mode no longer drops a hand-picked family of shadows: there is none left in those assemblies, so IlFirst's partial mechanism is deleted and the mode only decides which shims may be linked (framework index keyed on it). - Swift calls: .NET 11's macOS crypto calls CryptoKit entry points with CallConvSwift, which csharpc ignored. The throwing ones (AES-GCM, ChaCha20-Poly1305, AES key wrap) return their error in Swift's error register, so a C call would never report a forged tag. P/Invokes with CallConvSwift now go through an internal swiftcc thunk (textual IR, since the C API cannot mark a swifterror alloca) that stores the error where the SwiftError* points; shapes it does not cover (SwiftSelf, indirect results, other structs, Swift function pointers) are explicit diagnostics. CoreLib's Swift marker types are served from IL. - OperatingSystem.IsIOSVersionAtLeast and the other version checks for OSes csharpc never targets return false (the macOS crypto IL asks them). Samples 1604 (the version checks) and 1605 (AEAD and key-wrap tamper rejection) are new; 1490/1491/1522/1264 are live entries where .NET's own output follows the OS (no SHA-3, SHAKE, KMAC or AES-CCM on macOS; SSPI and GSS.framework statuses). tests/size/crypto.cs measures hashing, HMAC and AES.
Real .NET on Windows reports an unknown hash name with CNG's own error text, so 1490 is compared with it there (a live entry, like 467). The new crypto size program gets budgets from its first CI round on every platform, and the il-first page, ADR 0022 and the roadmap record the flip's sizes and timings against the copies and NativeAOT, with AES-CBC decryption (1.5x-1.9x NativeAOT on every OS) as the remaining run-time gap.
jonathanperis
marked this pull request as ready for review
November 29, 2026 14:11
Contributor
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: 0f2c03ef-eb72-4be6-bcf9-6e0267c88ce5) |
A Swift function reports failure in the error register and never unwinds,
so the Swift-call thunk and its call are nounwind: inlined into a try region
they stay plain calls. Emitting them as invokes also hit an LLVM 18 AArch64
bug: an invoke of a swifterror call whose landing pad does not resume loses
its end-of-range label, and the Mach-O exception table failed to assemble
(".uleb128 expression is not absolute") for samples 1491 and 1605 at -O2.
Owner
Author
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.
Part of the roadmap flip list (#449).
What changed and why
On macOS and Windows,
System.Security.Cryptographywas mixed: X.509/RSA/TLS came from the framework's IL, but hashing, HMAC, AES, RNG, PBKDF2, SHA-3 andHashAlgorithmNamewere csharpc's managed copies, which lacked HMAC instances, KMAC, SHAKE, HKDF,AesGcm/AesCcm/ChaCha20Poly1305,TripleDES(compile-time "not supported yet") and were up to 135x slower than NativeAOT. BothSystem.Security.CryptographyandSystem.Net.Securityare now the framework's IL whole on macOS and Windows, as on Linux and as under NativeAOT: CNG on Windows, the NativeAOT pack's Apple crypto shim on macOS.Hashing.cs,Crypto/Sha3.cs,Pbkdf2.cs,SymmetricCrypto.cs,RandomNumberGenerator.cs,HashAlgorithmName.cs,Rsa.cs,Cryptography.cs,Net/Tls.cs,Net/Negotiate.cscompile for WASI only. WASI keeps them: it has no OS crypto, NativeAOT does not target it, and the browser/WASI build of the assembly is not in the pinned framework (it throws PlatformNotSupportedException for most of this surface).Crypto/AesNative.csis gone (the helpers WASI uses remain, asCrypto/CryptoHelpers.cs). So are the Windows CNG error branch inHashing.cs, the native-TLS shadow family (NativeTlsprefixes,Apply), IlFirst's partial mechanism and theCSHARPC_NATIVE_TLSdefine.NativeTlsnow only decides which pack shims may be linked, and the framework index is keyed on it.CallConvSwift, and csharpc ignored that attribute. The throwing ones (AES-GCM, ChaCha20-Poly1305, AES key wrap) return their error in Swift's error register (x21), so a C call would never have reported a forged tag. EveryCallConvSwiftP/Invoke now goes through an internalswiftccthunk (CodeGen/ModuleCodeGen.SwiftCalls.cs). The thunk passes struct arguments as their fields and uses aswifterrorslot that is stored back through theSwiftError*. It is written as textual IR parsed into the module's context, because LLVM's C API cannot mark aswifterroralloca. The thunk and its call arenounwind, because Swift functions report errors in a register and never unwind. They therefore stay plain calls insidetryregions, which also avoids an LLVM 18 AArch64 bug found on the macOS runner: aninvokeof aswifterrorcall whose landing pad doesn'tresumeloses its end-of-range EH label, and the exception table fails to assemble (.uleb128 expression is not absolute, 1491/1605 at -O2). CoreLib's Swift marker types are now served from IL (upstream-types/System.Runtime.InteropServices.Swift.txt).OperatingSystem.Is{IOS,MacCatalyst,TvOS,WatchOS,Android,FreeBSD}VersionAtLeastnow return false; the macOS crypto IL asks them.Measurements (GitHub runners,
benchmarks/tls/crypto_hash_aes.cs, best of 5; ratio = csharpc / NativeAOT)The first commit added only the benchmark, so the first TLS-benchmark run measured the copies.
In absolute time every operation is at least as fast as with the copies. The one regression in ratio, macOS AES-CBC decrypt (1.22x → 1.51x), is still faster in absolute terms: 676 → 367 µs, because NativeAOT's own number moved between runs. AES-CBC decryption at 1.5x–1.9x NativeAOT on every OS, Linux included before this PR, is csharpc codegen in the framework's decryptor path; it is listed under "Run time against NativeAOT" in the roadmap. Checksums are equal everywhere.
Size (stripped): the benchmark program is 458 → 598 KiB on macOS and 481 → 560 KiB on Windows; NativeAOT builds it at 1,928 and 1,743 KiB. The new
tests/size/crypto.cs(hashing, HMAC, AES-CBC) is budgeted from its first CI round: linux-x86_64 378,704 B, linux-aarch64 362,280, macos-arm64 446,440, windows-x86_64 396,288, wasi-wasm32 401,608. The existing budgets are unchanged (json's macOS/Windows budgets were already above the new sizes).What the tests prove
live:where real .NET's output follows the OS: 1264, 1490 and 1491 on macOS (no SHA-3, SHAKE, KMAC or AES-CCM in Apple's crypto), 1490 on Windows (CNG's unknown-hash text, as in 467), 1522 on both (GSS.framework, SSPI).tests/slicer.shpasses 10/10,tests/size.shpasses, and macOS cross-compiles of 1605/1490/1491/1264/538/467/1522 have no diagnostics (the IR showsswiftcc+swifterrorfor the AEAD calls).What stays an explicit diagnostic
Swift calls passing
SwiftSelforSwiftIndirectResult, structs other than up to two pointer-sized fields, struct results, non-Apple/non-arm64 targets, anddelegate* unmanaged[Swift]function pointers. None of these is ever a C call.Note
High Risk
Changes cryptography implementation and adds Swift P/Invoke lowering on a security-critical path; correctness is covered by sample 1605 and benchmark checksums, but AES-CBC decrypt remains slower than NativeAOT on all OSes.
Overview
macOS and Windows now compile the full
System.Security.CryptographyandSystem.Net.Securitystacks from the shared framework's IL (CNG / Apple crypto shim), matching Linux and NativeAOT instead of a mixed model where hashes, HMAC, AES, RNG, PBKDF2, and related types stayed on slow corelib shadows.The large managed crypto implementations and platform cipher glue are removed or gated to WASI-only (
Hashing.cs,SymmetricCrypto.cs,AesNative.csdeleted on desktop targets; helpers live inCryptoHelpers.csfor WASM).NativeTlsno longer drops partial shadow families—it only controls which native shims link—andIlFirst.UsePartial/CSHARPC_NATIVE_TLSare removed from the driver.Swift
CallConvP/Invokes for macOS CryptoKit (AES-GCM, ChaCha20-Poly1305, key wrap) are lowered through a newswiftccthunk (ModuleCodeGen.SwiftCalls.cs) so authentication failures surface correctly instead of bogus success.OperatingSystem.Is*VersionAtLeastfor non-desktop platforms return false for framework IL.Adds
benchmarks/tls/crypto_hash_aes.cs, size testtests/size/crypto.cs, samples 1604 / 1605 (OS version checks and AEAD tamper rejection), and updates platform-skips and docs for #449.Reviewed by Cursor Bugbot for commit 1c85512. Bugbot is set up for automated code reviews on this repo. Configure here.