SPARKTLSCrypto provides SPARK/Ada cryptographic primitives used by SPARKTLS.
This crate is under active development and should be treated as alpha-quality software until the surrounding SPARKTLS stack reaches its verification and release goals.
The current crate is intended for x86_64-linux builds. SHA-256 has a
software fallback when SHA-NI is unavailable, but other accelerated units still
contain x86/x86_64 inline assembly and CPUID/XGETBV feature detection. ARM and
RISC-V support should be treated as future work until those units have
portable stubs or architecture-selected bodies.
The timing tests are also x86_64-specific: ctgrind and dudect harnesses use
Valgrind and rdtsc.
Every hand-written x86_64 unit (AES-NI, PCLMULQDQ GHASH, VAES/VPCLMULQDQ AES-GCM on 256-bit and on 512-bit registers, the AVX-512 AEAD units, the BMI2/ADX Montgomery multiply behind RSA and the P-256 field) is chosen at run time from CPUID and falls back to the proven SPARK implementation when the feature is absent.
AES-GCM has four tiers: the proven portable code; AES-NI + PCLMULQDQ
(4-block stripes); VAES + VPCLMULQDQ on ymm (16-block stripes; AMD Zen 3
and later, Intel Gracemont E-cores such as the N100/N150, and other CPUs
without AVX-512); and VAES + VPCLMULQDQ on zmm (16-block stripes, and
32-block ones for decryption).
Every tier gives the same bytes. The stitched stripe kernels, which
interleave AES-CTR with aggregated GHASH, are generated by
tools/generate_gcm_kernels.py; --check verifies the generated regions
in the unit bodies and runs in ci/check.sh. To turn all of them off
and run only the proven code, build with
SPARKTLSCRYPTO_ASM=disabled alr buildThe choice is compiled in (SPARKTLSCrypto.Tier_Config, selected by
source directory, with its own obj/disabled and lib/disabled) and is
visible in the build. The library reads no environment variable, file or
other runtime input to make it, and needs no Ada runtime support beyond
what the arithmetic itself uses. The smoke tests check the BMI2/ADX tier
against the SPARK Montgomery multiply on random operands and print which
path is active.
Valgrind's virtual CPU does not advertise BMI2/ADX, so the ctgrind lane
builds with SPARKTLSCRYPTO_ASM=assume_bmi2_adx, which reports the tier
present without CPUID so memcheck's taint tracking runs the assembly
(Valgrind 3.22 executes the instructions). That build faults on a CPU
without the instructions and is for the test lanes only.
CTGRIND_PORTABLE=1 runs the lane on the portable path instead.
AES_GCM.Self_Test is the FIPS 140-3 cryptographic algorithm self-test
for AES-GCM (IG 10.3.A): NIST CAVP known answers for encryption and
decryption, AES-128 and AES-256, through the prepared paths on the tier
this CPU will use, with forged records that must be rejected and zeroed.
A caller runs it before the first use of AES-GCM.
Use Alire to build the library:
alr buildFor a checked debug build:
SPARKTLSCRYPTO_BUILD_MODE=debug \
SPARKTLSCRYPTO_RUNTIME_CHECKS=enabled \
SPARKTLSCRYPTO_CONTRACTS=enabled \
alr buildOptimized builds default to a reproducible x86-64 configuration: the generic
code targets x86-64-v2, and the handwritten AVX-512 units are compiled with
their required feature flags for runtime dispatch. For local benchmarking on
the current host CPU, use:
SPARKTLSCRYPTO_BUILD_MODE=native alr buildThe Nix flake pins the host tools used by CI:
nix develop --command alr buildThe CI lane intentionally builds and checks the crate but does not run
gnatprove; proof runs are currently too expensive and toolchain-sensitive
for a default hosted CI gate.
Manual proof runs use the Alire-pinned GNATprove toolchain:
nix develop --command bash ci/proof.shThis scopes GNATprove to the SPARKTLSCrypto source units, records the invocation
header, and classifies dependency findings separately. GNATprove may still
report SPARKNaCl obligations pulled in through semantic dependencies; reports
under src/sparktlscrypto-* are SPARKTLSCrypto findings, while SPARKNaCl
findings should be checked against the upstream crate's own proof expectations.
To increase prover strength for this crate-local proof:
SPARKTLSCRYPTO_PROOF_LEVEL=2 nix develop --command bash ci/proof.shOptional timing checks live under tests/timing:
nix develop --command bash ci/timing.sh ctgrind
nix develop --command bash ci/timing.sh dudectThank you to Chris Allen (@bitemyapp) for a substantial set of performance and constant-time contributions, each with differential, timing and benchmark evidence:
- A precomputed fixed-base Curve25519 table, with a constant-time row scan and cached mixed addition, for X25519 key generation and Ed25519 signing
- Prepared AES-GCM keys
- A shorter AVX-512 GHASH reduction
- Fused sixteen-block VAES/GHASH
- 64-bit carries in Curve25519 field multiplication
- Removal of secret-dependent branches from the portable GHASH
- Tracing the GCC array-slice miscompilation behind the
-fno-tree-vrpbuild flag to its root cause