Skip to content

crypto: Let a build leave ML-KEM-768 out - #2486

Open
gabrielmancini wants to merge 1 commit into
atomvm:release-0.7from
iobee-io:mlkem-optional
Open

gabrielmancini wants to merge 1 commit into
atomvm:release-0.7from
iobee-io:mlkem-optional

Conversation

@gabrielmancini

Copy link
Copy Markdown

With a libsodium new enough to provide it, ML-KEM-768 is compiled in whenever AVM_USE_LIBSODIUM is on. On ESP32 that costs about 52 KB of flash, 45 KB of it libsodium's unrolled Keccak. A device with no use for post-quantum key encapsulation pays for it anyway: on a 4 MB board the free space in our app partition went from 111 KB to 57 KB.

AVM_DISABLE_MLKEM (CMake) and CONFIG_AVM_DISABLE_MLKEM (ESP32 Kconfig), both off by default, leave it out. crypto:encapsulate_key/2 and crypto:decapsulate_key/3 are then missing, as in a build without libsodium, and test_crypto_pk already skips itself in that case.

Our fleet of ESP32 boards has run with CONFIG_AVM_DISABLE_MLKEM=y since late October.

@petermm

petermm commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

non-blocking nits:

PR review: crypto: Let a build leave ML-KEM-768 out

Commit: e58ba548e148f968d6509058bea34c1e662c635a
Verdict: No merge-blocking correctness issues found in the source review. The independent oracle review reached the same conclusion. The notes below are non-blocking.

Findings

1. Low — The existing suite cannot verify the opt-out

test_crypto_pk.erl:431–439 treats a failed availability probe as success. The probe also swallows every exception. If either build system stops propagating the flag, an opt-out build can still pass by running the enabled tests. If a default build accidentally drops support, it can pass by skipping them.

Run paired builds against the same ML-KEM-capable libsodium with the option unset and enabled. Assert that erlang:is_builtin/3 reports both encapsulation NIFs present in the default build and absent in the opt-out build. Also check successful key generation in the default build, badarg from crypto:generate_key(mlkem768, []) in the opt-out build, and continued Ed25519/X25519 operation in both. Keep the portable suite's skip for environments where ML-KEM is not expected. function_exported/3 is not a substitute: it can see the Erlang stubs even when the NIFs are absent.

2. Low — Document the full effect and the required libsodium capability

The programmer's guide doesn't mention the opt-out. The new ESP32 help implies any libsodium build supports ML-KEM and mentions only encapsulation and decapsulation. The same guard also removes the ML-KEM branch of crypto:generate_key/2. Clarify that in both places; the availability notes in crypto.erl should also mention that ML-KEM can be disabled at build time.

diff --git a/doc/src/programmers-guide.md b/doc/src/programmers-guide.md
--- a/doc/src/programmers-guide.md
+++ b/doc/src/programmers-guide.md
@@ -1219,5 +1219,9 @@
 libsodium to be installed on the build host (e.g. `libsodium-dev` on Debian/Ubuntu), or the
 `espressif/libsodium` component on ESP32.  On generic UNIX, `-DAVM_STATIC_LIBSODIUM=ON` links
 `libsodium.a` into the AtomVM binary instead of depending on the shared library at run time.
 
+Set `-DAVM_DISABLE_MLKEM=ON` on generic UNIX or `CONFIG_AVM_DISABLE_MLKEM=y` on ESP32 to omit
+ML-KEM-768.  This disables `crypto:generate_key(mlkem768, [])`, `crypto:encapsulate_key/2`,
+and `crypto:decapsulate_key/3` without disabling Ed25519 or X25519.
+
 ```{important}
diff --git a/src/platforms/esp32/main/Kconfig.projbuild b/src/platforms/esp32/main/Kconfig.projbuild
--- a/src/platforms/esp32/main/Kconfig.projbuild
+++ b/src/platforms/esp32/main/Kconfig.projbuild
@@ -50,10 +50,9 @@
         bool "Leave ML-KEM-768 out of crypto"
         default n
         help
-            With libsodium (AVM_USE_LIBSODIUM), crypto:encapsulate_key/2 and
-            crypto:decapsulate_key/3 offer ML-KEM-768. It takes about 52 KB
-            of flash, mostly libsodium's Keccak. Say y to leave it out on a
-            device that does not use it: those functions are then missing, as
-            in a build without libsodium.
+            When AVM_USE_LIBSODIUM is enabled and libsodium provides ML-KEM-768,
+            it takes about 52 KB of flash, mostly libsodium's Keccak. Say y to
+            omit ML-KEM-768 key generation, encapsulation and decapsulation
+            without disabling the other libsodium-backed algorithms.
 
     config AVM_CREATE_STACKTRACES

Review scope and limits

  • Both options are off by default. The PUBLIC compile definition is necessary: otp_crypto.c is compiled by the generic Unix platform target and the ESP32 avm_sys component, not by the core target that carries the definition.
  • The availability guard covers key generation and both NIFs. Ed25519 and X25519 remain under the separate libsodium guard.
  • This was a source review. No builds or runtime tests were run, and the reported ~52 KB ESP32 flash saving was not independently measured. To validate that number, compare application image sizes for the same board, toolchain, libsodium and configuration, changing only the opt-out.

With libsodium new enough to provide it, ML-KEM-768 is compiled in
whenever AVM_USE_LIBSODIUM is on. On ESP32 that is 52,880 bytes of
flash (ESP-IDF v5.5.5, libsodium 1.0.22, the same build with and
without it), which a device without a use for post-quantum key
encapsulation pays for anyway.

AVM_DISABLE_MLKEM (CMake) and CONFIG_AVM_DISABLE_MLKEM (ESP32 Kconfig),
both off by default, leave it out: crypto:generate_key(mlkem768, []),
crypto:encapsulate_key/2 and crypto:decapsulate_key/3 are then
unavailable, as in a build without ML-KEM. Ed25519 and X25519 stay.

Signed-off-by: Gabriel Mancini <gabriel.mancini@gmail.com>
@gabrielmancini

Copy link
Copy Markdown
Author

Thanks for the review. I took both documentation points, amended into the commit:

  • programmers-guide.md: your paragraph on -DAVM_DISABLE_MLKEM=ON / CONFIG_AVM_DISABLE_MLKEM=y, including that it also removes crypto:generate_key(mlkem768, []) and leaves Ed25519 and X25519 alone.
  • Kconfig.projbuild: your help text, which no longer implies that every libsodium build provides ML-KEM-768.
  • crypto.erl: the availability notes of generate_key/2, encapsulate_key/2 and decapsulate_key/3 now say that a build with AVM_DISABLE_MLKEM leaves them out.
  • The commit message also names generate_key now, and carries the paired size below instead of the earlier estimate.

On the test: agreed, the current suite can't tell an opt-out build from a default build that lost ML-KEM. A paired build against the same ML-KEM-capable libsodium, checking with erlang:is_builtin/3 (default: both NIFs present and generate_key(mlkem768, []) works; opt-out: both absent and badarg; Ed25519/X25519 working in both), needs a new CI matrix entry. Would you rather have that as a follow-up PR, so this one stays small?

On the 52 KB: I measured it paired now, as you described. Same commit (this PR), board config, ESP-IDF v5.5.5 and espressif/libsodium 1.0.221; only CONFIG_AVM_DISABLE_MLKEM changed:

CONFIG_AVM_DISABLE_MLKEM atomvm-esp32.bin free in a 0x1f0000 app partition
not set (ML-KEM in) 1,884,864 bytes 0x23d40 (143 KB, 7%)
y 1,831,984 bytes 0x30bd0 (195 KB, 10%)

That is 52,880 bytes. The number in the PR description had come from our app partition losing about that much when ML-KEM arrived with the libsodium update, not from a paired build.

The failing jobs (Elixir master Code.ensure_loaded?/1, wasm -Wunsequenced-volatile in mbedTLS, ESP32 JIT test_ssl with spi_flash_mmap(INST) failed, macOS test_net_kernel timeout) fail the same way on #2488 and #2489, so they don't look related to this change.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants