Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20231201-openssl-compat-fixes #7031

Merged

Conversation

douzzer
Copy link
Contributor

@douzzer douzzer commented Dec 5, 2023

fix AES-related code, in both crypto and TLS layers, for various uninitialized data and resource leak defects around wc_AesInit() and wc_AesFree():

  • followup to 20231128-misc-fixes #7009 and Add missing wc_AesInit calls. #7011

  • adds WC_DEBUG_CIPHER_LIFECYCLE, which embeds asserts in low-level AES implementations for proper usage of wc_AesInit() and wc_AesFree().

  • fixes native CMAC, AES-EAX, and AES-XTS implementations to assure resource release.

  • adds missing wc_AesXtsInit() API, and adds a new wc_AesXtsSetKey_NoInit().

  • fixes misspellings in EVP that unconditionally gated out AES-OFB and AES-XTS.

  • fixes misspellings in EVP that unconditionally gated out AES-CBC and AES-CFB code in wolfSSL_EVP_CIPHER_CTX_cleanup_cipher().

  • openssl compat AES low level cipher API has no counterpart to wc_AesFree(), so these compat APIs will now be gated out in configurations where they would otherwise leak memory or file descriptors (WOLFSSL_AFALG, WOLFSSL_DEVCRYPTO, WOLF_CRYPTO_CB, etc.). A new macro, WC_AESFREE_IS_MANDATORY, is defined in wolfcrypt/aes.h to streamline this dependency.

  • fixes 40 missing EVP_CIPHER_CTX_cleanup()s and 11 wc_AesFree()s in src/ssl.c, src/ssl_crypto.c, tests/api.c, and wolfcrypt/test/test.c.

tested with wolfssl-multi-test.sh ... super-quick-check fips-140-3-dev-kcapi cross-armv7a-armasm-fips-140-3-ready-sp-all-testsuite-sanitizer cross-armv7a-all-armasm-testwolfcrypt-sanitizer sp-all-asm-smallstack-sanitizer-fips-140-3-dev sanitizer-all-asm-smallstack-async all-sp-all-Os-sanitizer cross-aarch64-all-armasm-unittest-sanitizer linuxkm-aesni-ksanitize-insmod sanitizer-defaults all-afalg, with -DWC_DEBUG_CIPHER_LIFECYCLE added to all non-fips sanitizer scenarios.

…itialized data and resource leak defects around wc_AesInit() and wc_AesFree():

* followup to wolfSSL#7009 "20231128-misc-fixes" and  wolfSSL#7011 "Add missing wc_AesInit calls."

* adds WC_DEBUG_CIPHER_LIFECYCLE, which embeds asserts in low-level AES implementations for proper usage of wc_AesInit() and wc_AesFree().

* fixes native CMAC, AES-EAX, and AES-XTS implementations to assure resource release.

* adds missing wc_AesXtsInit() API, and adds a new wc_AesXtsSetKey_NoInit().

* fixes misspellings in EVP that unconditionally gated out AES-OFB and AES-XTS.

* fixes misspellings in EVP that unconditionally gated out AES-CBC and AES-CFB code in wolfSSL_EVP_CIPHER_CTX_cleanup_cipher().

* openssl compat AES low level cipher API has no counterpart to wc_AesFree(), so these compat APIs will now be gated out in configurations where they would otherwise leak memory or file descriptors (WOLFSSL_AFALG, WOLFSSL_DEVCRYPTO, WOLF_CRYPTO_CB, etc.).  A new macro, WC_AESFREE_IS_MANDATORY, is defined in wolfcrypt/aes.h to streamline this dependency.

* fixes 40 missing EVP_CIPHER_CTX_cleanup()s and 11 wc_AesFree()s in src/ssl.c, src/ssl_crypto.c, tests/api.c, and wolfcrypt/test/test.c.
@douzzer douzzer requested a review from philljj December 6, 2023 05:42
wolfssl/wolfcrypt/aes.h Show resolved Hide resolved
wolfssl/wolfcrypt/aes.h Show resolved Hide resolved
wolfcrypt/src/cmac.c Outdated Show resolved Hide resolved
wolfcrypt/src/aes.c Outdated Show resolved Hide resolved
wolfcrypt/src/aes.c Outdated Show resolved Hide resolved
@douzzer
Copy link
Contributor Author

douzzer commented Dec 6, 2023

re wolfssl/wolfcrypt/aes.h L 402:

Usual pattern is to have configuration type settings like this in wolfssl/wolfcrypt/settings.h and configure.ac.

The macros at issue refer only to the macros used in struct Aes defined right above it, and the overall logic is specific to the native AES implementation, so it seemed like technical debt to put it anywhere else.

I.e. WC_AESFREE_IS_MANDATORY is like the other #defines in wolfssl/wolfcrypt/aes.h, e.g. BS_WORD_SIZE and friends, and is like SHA256_NOINLINE, USE_ECC_B_PARAM, etc., in other headers.

wolfcrypt/src/cmac.c Outdated Show resolved Hide resolved
@douzzer douzzer requested a review from philljj December 6, 2023 19:02
…e wc_CmacFinal() as wc_CmacFinalNoFree() removing its deallocation clauses, and add new wc_CmacFinal() that calls wc_CmacFinalNoFree() then calls wc_CmacFree() unconditionally, for compatibility with legacy client code (some of which may have previously leaked).

tests/api.c: modify test_wc_CmacFinal() to use wc_CmacFinalNoFree() except for the final call.

wolfcrypt/src/aes.c:
* fix wc_AesEaxEncryptAuth() and wc_AesEaxDecryptAuth() to call wc_AesEaxFree() only if wc_AesEaxInit() succeeded.
* fix wc_AesEaxInit() to free all resources on failure.
* revert wc_AesEaxEncryptFinal() and wc_AesEaxDecryptFinal() changes, then change wc_CmacFinal() calls in them to wc_CmacFinalNoFree() calls.
* wc_AesEaxFree(): add wc_CmacFree() calls.
wolfcrypt/src/aes.c Outdated Show resolved Hide resolved
wolfcrypt/src/aes.c Outdated Show resolved Hide resolved
wolfcrypt/src/cmac.c Outdated Show resolved Hide resolved
wolfcrypt/src/evp.c Outdated Show resolved Hide resolved
wolfcrypt/src/aes.c Show resolved Hide resolved
wolfcrypt/src/aes.c Outdated Show resolved Hide resolved
…acFinalNoFree(), and wc_CmacFree();

rename wc_AesXtsSetKey_NoInit() to wc_AesXtsSetKeyNoInit() for morphological consistency;

refactor wc_AesXtsSetKey() to call wc_AesXtsSetKeyNoInit() and clean up on failure;

readability tweak in wolfSSL_EVP_CipherFinal().
@douzzer douzzer requested a review from SparkiDev December 7, 2023 01:40
@douzzer
Copy link
Contributor Author

douzzer commented Dec 7, 2023

retest this please.

@douzzer
Copy link
Contributor Author

douzzer commented Dec 7, 2023

retest this please.

@douzzer douzzer closed this Dec 7, 2023
@douzzer douzzer reopened this Dec 7, 2023
@douzzer douzzer assigned wolfSSL-Bot and unassigned douzzer Dec 7, 2023
@douzzer douzzer requested a review from philljj December 7, 2023 21:34
@JacobBarthelmeh JacobBarthelmeh merged commit ac447d1 into wolfSSL:master Dec 9, 2023
184 checks passed
douzzer added a commit to douzzer/wolfssl that referenced this pull request Dec 11, 2023
…ate out AES-XTS functionality that depends on new APIs added in wolfSSL#7031 (b14aba4 and 931ac4e) (AES-XTS is non-FIPS in FIPS <5.3).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants