Skip to content

fix(libsodium): disable C11 Annex K declarations to fix build with picolibc and update library to 1.0.22#730

Merged
mahavirj merged 2 commits into
espressif:masterfrom
hrushikesh430:fix/libsoduim_annex_k_decleartaion_issue
May 7, 2026
Merged

fix(libsodium): disable C11 Annex K declarations to fix build with picolibc and update library to 1.0.22#730
mahavirj merged 2 commits into
espressif:masterfrom
hrushikesh430:fix/libsoduim_annex_k_decleartaion_issue

Conversation

@hrushikesh430
Copy link
Copy Markdown
Collaborator

@hrushikesh430 hrushikesh430 commented Apr 17, 2026

Problem

Building libsodium with ESP-IDF v6.0+ (picolibc) fails with:

picolibc/include/errno.h:43:9: error: unknown type name '__errno_t'
picolibc/include/string.h:219:9: error: unknown type name '__rsize_t'

Root Cause

libsodium's utils.c defines __STDC_WANT_LIB_EXT1__=1 to request C11 Annex K types. In picolibc those types (__errno_t, __rsize_t) live in sys/_types.h, gated behind that same macro.

Via the force-include in libsodium/CMakeLists.txt, the PSA → mbedTLS → libc chain pulls sys/_types.h in before utils.c sets the macro:

port_include/sodium/crypto_hash_sha256.h
  └── <psa/crypto.h>
      └── <psa/crypto_struct.h>
          └── <psa/crypto_driver_contexts_primitives.h>
              └── … (PSA driver context chain) …
                  └── <mbedtls/platform_time.h>
                      └── <time.h>
                          └── <sys/_types.h>   ← typedefs gated here

Once the include guard fires, __errno_t / __rsize_t are never declared, and later errno.h / string.h fail. The libsodium test app doesn't reach this chain, so CI never tripped on it.

Fix

Add -D__STDC_WANT_LIB_EXT1__=0 as a compile definition in libsodium/CMakeLists.txt. The -D applies before any source, overriding utils.c, so picolibc skips the Annex K declarations regardless of include order. libsodium on ESP-IDF does not use Annex K functions.

Component version bumped to 1.0.22.

References

  • picolibc#868 — upstream fix rejected; per C standard, only applications should define __STDC_WANT_LIB_EXT1__
  • zephyr#104666 — same issue in Zephyr

Update libsodium to v1.0.22

This PR also bumps the libsodium submodule from 1.0.21-RELEASE to 1.0.22-RELEASE, so the 1.0.22 component version actually matches the underlying source.

What changed:

  • Submodule pointer moved to the 1.0.22-RELEASE tag.
  • New 1.0.22 sources added to the build: ML-KEM768, X-Wing hybrid KEM (ML-KEM768 + X25519), the high-level crypto_kem_* API, and SHA-3.
  • sbom_libsodium.yml updated with the new version and commit hash.
  • Upstream KEM tests (kem, kem_mlkem768, kem_xwing) added to the test app to verify ML-KEM and X-Wing on target.
Found no CVE using command esp-idf-sbom manifest check --local-db --extended-scan libsodium/

@hrushikesh430 hrushikesh430 self-assigned this Apr 17, 2026
@hrushikesh430 hrushikesh430 force-pushed the fix/libsoduim_annex_k_decleartaion_issue branch from dc2e917 to e76ac0e Compare April 17, 2026 08:27
@hrushikesh430
Copy link
Copy Markdown
Collaborator Author

@mahavirj @AdityaHPatwardhan PTAL

@mahavirj
Copy link
Copy Markdown
Member

@Lapshin PTAL

@Lapshin
Copy link
Copy Markdown
Collaborator

Lapshin commented Apr 20, 2026

@hrushikesh430 , thank you for the PR. I found it difficult to follow the description due to factual inconsistencies introduced by AI.

This is safe because libsodium does not use any Annex K functions (memset_s, etc.) on ESP-IDF — it uses its own sodium_memzero() implementation instead.

Could not find if Espressif's own implementation exists. Could you add a link?

Looks like it is "safe" for now, because original libsodium defines HAVE_MEMSET_S and others based on OS which looks wrong to me. It should be defined by configure script or something similar to their build system. It could be a task to send a patch to upstream (cc @mahavirj )

This happens in real user projects that have multiple ESP-IDF components (WiFi, HTTP, MQTT, etc.) whose headers transitively pull in sys/_types.h before libsodium's utils.c can set the macro.

From your description, it sounds like utils.c sets macro globally for every other translation unit. But this is not true. utils.c sets macro only for it's own code (until you do #include "utils.c" in another translation unit). And it's done at the very top of the file (looks correct to me). I see one way to reproduce the issue by passing -include <file> in cmdline

libsodium can fail to compile with:

Could you please share a minimal example that reproduces the issue?

@mahavirj
Copy link
Copy Markdown
Member

mahavirj commented May 5, 2026

@Lapshin @hrushikesh430

Include file dependency is coming from the CMakeLists include injection here:

"$<$<COMPILE_LANGUAGE:C>:SHELL:-include ${CMAKE_CURRENT_SOURCE_DIR}/port_include/sodium/crypto_hash_sha256.h>"
"$<$<COMPILE_LANGUAGE:C>:SHELL:-include ${CMAKE_CURRENT_SOURCE_DIR}/port_include/sodium/crypto_hash_sha512.h>")

  port_include/sodium/crypto_hash_sha256.h                                             
    └── <psa/crypto.h>                                    ← the entry point
        └── <psa/crypto_struct.h>                                                      
            └── <psa/crypto_driver_contexts_primitives.h>
                └── … (PSA driver context chain) …                                     
                    └── <mbedtls/platform_time.h>                                      
                        └── <time.h>                       ← first libc header         
                            └── <sys/_types.h>             ← typedefs gated here 

The fix from this PR should work well. @hrushikesh430 Can you please update the PR description and also bump the component version?

CC @Ashish285

@mahavirj mahavirj requested a review from Ashish285 May 5, 2026 08:09
@Ashish285
Copy link
Copy Markdown
Collaborator

Ashish285 commented May 5, 2026

LGTM! The fix is similar as mbedTLS 4.1 (with tf-psa-crypto 1.1).

@Lapshin, is it safe to set __STDC_WANT_LIB_EXT1__=0 as default in ESP-IDF when building with picolibc?

@Lapshin
Copy link
Copy Markdown
Collaborator

Lapshin commented May 5, 2026

@Ashish285 , toolchains are not required to have this feature supported. I think this can be disabled by default

@hrushikesh430 hrushikesh430 force-pushed the fix/libsoduim_annex_k_decleartaion_issue branch from e76ac0e to fa50372 Compare May 6, 2026 06:30
@hrushikesh430
Copy link
Copy Markdown
Collaborator Author

@mahavirj @Ashish285 @AdityaHPatwardhan Updated the PR, PTAL.

…colibc

Upstream libsodium's utils.c defines __STDC_WANT_LIB_EXT1__=1, which
requests C11 Annex K bounds-checking types (errno_t, rsize_t) from the
C library. With ESP-IDF v6.0+ (which uses picolibc instead of newlib),
this can cause build failures when sys/_types.h is transitively included
by other component headers before utils.c sets the macro. The include
guard in sys/_types.h prevents re-inclusion, so the conditional Annex K
type definitions (__errno_t, __rsize_t) are never reached.

Set __STDC_WANT_LIB_EXT1__=0 as a compile definition so the flag is
applied before any header is processed, avoiding the issue regardless
of include order. This is safe because libsodium does not use any
Annex K functions on ESP-IDF.
@hrushikesh430 hrushikesh430 force-pushed the fix/libsoduim_annex_k_decleartaion_issue branch from 308b3be to c8e87bd Compare May 6, 2026 08:46
@hrushikesh430 hrushikesh430 force-pushed the fix/libsoduim_annex_k_decleartaion_issue branch from c8e87bd to a3dcc6f Compare May 6, 2026 09:17
@mahavirj mahavirj changed the title fix(libsodium): disable C11 Annex K declarations to fix build with picolibc fix(libsodium): disable C11 Annex K declarations to fix build with picolibc and update library to 1.0.22 May 7, 2026
@mahavirj mahavirj merged commit 799cc45 into espressif:master May 7, 2026
85 checks passed
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.

4 participants