Skip to content

Privatize functions and some types/macros in cipher.h #259

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

Merged
merged 5 commits into from
May 21, 2025

Conversation

felixc-arm
Copy link
Contributor

@felixc-arm felixc-arm commented Apr 16, 2025

Privatizes all functions and some types/macros (below) by guarding them with MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS. Any types/macros not listed/privatized here will break the build if they are privatized.

Types privatized:

mbedtls_cipher_padding_t
enum that includes MBEDTLS_KEY_LENGTH_<NONE|DES|DES_EDE|DES_EDE3>
mbedtls_cipher_base_t

Macros privatized:

MBEDTLS_MAX_KEY_LENGTH
MBEDTLS_CIPHER_VARIABLE_IV_LEN
MBEDTLS_CIPHER_VARIABLE_KEY_LEN
MBEDTLS_KEY_BITLEN_SHIFT (required unlinking from Doxygen comment)
MBEDTLS_IV_SIZE_SHIFT (required unlinking from Doxygen comment)

Resolves #220

PR checklist

  • changelog not required because: will come later encompassing all privatization using MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
  • framework PR not required
  • mbedtls development PR not required because: crypto change only
  • mbedtls 3.6 PR not required because: 4.0/1.0 work
  • tests not required because: no functional change

@felixc-arm felixc-arm added needs-review Every commit must be reviewed by at least two team members needs-reviewer This PR needs someone to pick it up for review priority-high High priority - will be reviewed soon api-break This issue/PR breaks the API and must wait for a new major version and removed priority-high High priority - will be reviewed soon labels Apr 17, 2025
@felixc-arm felixc-arm marked this pull request as ready for review April 17, 2025 15:06
@felixc-arm
Copy link
Contributor Author

A lot more types & macros could probably be privatized too, however doing so would break the CI for other reasons. e.g. MBEDTLS_KEY_BITLEN_SHIFT breaks the doxygen build as it's used in a comment above. So I guess the question is do we want to do any extra work to get more types/macros privatized or just only privatize ones that don't break the CI to keep the amount of work down?

@gilles-peskine-arm
Copy link
Contributor

do we want to do any extra work to get more types/macros privatized or just only privatize ones that don't break the CI to keep the amount of work down?

I think we should try to find a reasonable compromise between the two. We don't have time to spend hours analyzing every single macro. But we should take quick wins.

For example, MBEDTLS_KEY_BITLEN_SHIFT is only used in existing Doxygen comments that will not be part of the public documentation (and actually shouldn't be even in 3.x, since it only appears in the documentation of a private field of a structure, and those should not be documented). So here I think the best solution is to unlink the reference in the Doxygen comment, and guard the macro.

I wouldn't insist on doing this kind of analysis for all macros, especially for ones that are used in more places or in more complex ways. But I'd prefer to privatize macros when the analysis and fix is as easy as this case.

@felixc-arm
Copy link
Contributor Author

Sounds good - I've re-privatized those two macros then. I can't see any other macros/types that are privatized as easily, so the ones privatized in this PR should be the complete list - on my end at least.

bjwtaylor
bjwtaylor previously approved these changes Apr 25, 2025
@mpg mpg self-requested a review May 6, 2025 08:45
@mpg mpg removed the needs-reviewer This PR needs someone to pick it up for review label May 6, 2025
@felixc-arm felixc-arm force-pushed the privatize-cipher-functions branch from a940dde to 5cdde0f Compare May 13, 2025 10:36
@felixc-arm felixc-arm added the priority-high High priority - will be reviewed soon label May 13, 2025
Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, but I think now that GCC/CCM are done, we can probably go a bit further here. For example, in a local copy of this branch (with development merged in) I was able to make mbedtls_cipher_mode_t private and things are still compiling (at least in the default config).

@felixc-arm
Copy link
Contributor Author

Ah yes - done that now & updated the PR description to include the newly-privatized types/macros, cheers!

@felixc-arm
Copy link
Contributor Author

...And the CI reminded my why they were left unprivatized (at least mbedtls_cipher_id_t and mbedtls_cipher_mode_t) - they are present in drivers/builtin/src/psa_crypto_cipher.h which is then used to build libtestdriver1 which can't find privatized types. Although mbedtls_cipher_base_t & MBEDTLS_MAX_KEY_LENGTH are now also privatized (presuming CI passes...)

@mpg
Copy link
Contributor

mpg commented May 15, 2025

Ah, thanks for trying it out and explaining the results!

Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@mpg mpg requested a review from bjwtaylor May 15, 2025 08:28
@mpg
Copy link
Contributor

mpg commented May 21, 2025

@bjwtaylor Gentle reminder that this is waiting for you to review again :)

@mpg mpg added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members labels May 21, 2025
@mpg mpg added this pull request to the merge queue May 21, 2025
Merged via the queue into Mbed-TLS:development with commit d999dcb May 21, 2025
3 of 5 checks passed
@felixc-arm felixc-arm deleted the privatize-cipher-functions branch May 28, 2025 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-break This issue/PR breaks the API and must wait for a new major version approved Design and code approved - may be waiting for CI or backports priority-high High priority - will be reviewed soon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make cipher.h functions private
4 participants