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

psa: Define mbedtls_ecc_group_to_psa() inline #3301

Merged
merged 2 commits into from
May 19, 2020

Conversation

Patater
Copy link
Contributor

@Patater Patater commented May 4, 2020

On dual world platforms, we want to run the PK module (pk.c) on the NS
side so TLS can use PSA APIs via the PK interface. PK currently has a
hard dependency on mbedtls_ecc_group_to_psa() which is declared in
crypto_extra.h, but only defined in psa_crypto.c, which is only built
for the S side.

Without this change, dual world platforms get error messages like the
following.

[Error] @0,0: L6218E: Undefined symbol mbedtls_ecc_group_to_psa (referred from BUILD/LPC55S69_NS/ARM/mbed-os/features/mbedtls/mbed-crypto/src/pk.o)

Make mbedtls_ecc_group_to_psa() inline within crypto_extra.h so that it
is available to both NS and S world code.

Fixes #3300

Signed-off-by: Darryl Green [email protected]
Signed-off-by: Jaeden Amero [email protected]

Status

READY

Requires Backporting

NO - PSA related change, and no LTS branches contain PSA.

Migrations

mbedtls_ecc_group_to_psa() is becoming inline. This will require a rebuild of code using this function.

Todos

  • Tests - covered by Mbed OS CI on LPC55S69
  • [ ] Documentation
  • Changelog updated
  • [ ] Backported

@Patater Patater added Arm Contribution component-crypto Crypto primitives and low-level interfaces component-platform Portability layer and build scripts labels May 4, 2020
@mpg mpg self-requested a review May 4, 2020 10:22
@Patater Patater added the needs-ci Needs to pass CI tests label May 4, 2020
@mpg
Copy link
Contributor

mpg commented May 5, 2020

The CI's not very happy about this:

In file included from ../include/psa/crypto.h:3774:0,
                 from ../include/mbedtls/pk.h:49,
                 from pk.c:29:
pk.c: In function 'mbedtls_pk_wrap_as_opaque':
../include/psa/crypto_struct.h:460:33: error: 'bits' may be used uninitialized in this function [-Werror=maybe-uninitialized]
         attributes->core.bits = (psa_key_bits_t) bits;
                                 ^~~~~~~~~~~~~~~~~~~~~
pk.c:608:12: note: 'bits' was declared here
     size_t bits;
            ^~~~

I think setting *bits = 0 in the default: leg of the switch statement of the function that was made inline would resolve this.

Otherwise this looks good to me, but as noted in the PR description this changes the ABI - not a problem, but something we need to remember when preparing the next release.

@mpg mpg added the needs-work label May 5, 2020
@Patater Patater force-pushed the inline-mbedtls_ecc_group_to_psa branch 2 times, most recently from 96c0d8e to d32988b Compare May 5, 2020 09:21
@Patater
Copy link
Contributor Author

Patater commented May 5, 2020

Rebased to set *bits = 0 in mbedtls_ecc_group_to_psa() in the default case, by adding a commit sharing justification for the change.

@Patater Patater added needs-review Every commit must be reviewed by at least two team members, and removed needs-work labels May 5, 2020
mpg
mpg previously approved these changes May 5, 2020
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

@Patater
Copy link
Contributor Author

Patater commented May 5, 2020

The only CI failure is with the ABI test, which this PR changes intentionally. SO version will need bumping.

+ scripts/abi_check.py -o FETCH_HEAD -n HEAD -s identifiers --brief
Compatibility issues found for libmbedcrypto
<reports>
<report kind="binary" version="1.2">

<removed_symbols>
  <header name="psa_crypto.c">
    <library name="libmbedcrypto.so.4">
      <name>mbedtls_ecc_group_to_psa</name>
    </library>
  </header>
</removed_symbols>

</report>
<report kind="source" version="1.2">

<removed_symbols>
  <header name="psa_crypto.c">
    <library name="libmbedcrypto.so.4">
      <name>mbedtls_ecc_group_to_psa</name>
    </library>
  </header>
</removed_symbols>

</report>
</reports>
Checking evolution from FETCH_HEAD (d9f694960f10c23cc5d7cb6de440fe3584ff7979) to HEAD (91c06de53602a64063f1ae43d3a44a2562deaab9)
No compatibility issues for libmbedx509
No compatibility issues for libmbedtls
script returned exit code 1

@Patater Patater removed the needs-ci Needs to pass CI tests label May 5, 2020
Patater and others added 2 commits May 5, 2020 12:41
Clear bits in mbedtls_ecc_group_to_psa() to avoid static analyzers and
possibly compilers from warning that bits may be used uninitialized in
certain code paths.

For example, if mbedtls_ecc_group_to_psa() were to be inlined in
crypto_extra.h, the following compiler warning is likely.

    In file included from ../include/psa/crypto.h:3774:0,
                     from ../include/mbedtls/pk.h:49,
                     from pk.c:29:
    pk.c: In function 'mbedtls_pk_wrap_as_opaque':
    ../include/psa/crypto_struct.h:460:33: error: 'bits' may be used uninitialized in this function [-Werror=maybe-uninitialized]
             attributes->core.bits = (psa_key_bits_t) bits;
                                     ^~~~~~~~~~~~~~~~~~~~~
    pk.c:608:12: note: 'bits' was declared here
         size_t bits;
                ^~~~

Signed-off-by: Jaeden Amero <[email protected]>
On dual world platforms, we want to run the PK module (pk.c) on the NS
side so TLS can use PSA APIs via the PK interface. PK currently has a
hard dependency on mbedtls_ecc_group_to_psa() which is declared in
crypto_extra.h, but only defined in psa_crypto.c, which is only built
for the S side.

Without this change, dual world platforms get error messages like the
following.

    [Error] @0,0: L6218E: Undefined symbol mbedtls_ecc_group_to_psa (referred from BUILD/LPC55S69_NS/ARM/mbed-os/features/mbedtls/mbed-crypto/src/pk.o)

Make mbedtls_ecc_group_to_psa() inline within crypto_extra.h so that it
is available to both NS and S world code.

Fixes Mbed-TLS#3300

Signed-off-by: Darryl Green <[email protected]>
Signed-off-by: Jaeden Amero <[email protected]>
@Patater Patater force-pushed the inline-mbedtls_ecc_group_to_psa branch from d32988b to 2f0eb51 Compare May 5, 2020 11:42
@Patater
Copy link
Contributor Author

Patater commented May 5, 2020

Rebased to revise commit message. It said "the following" a bit too often. No code changes.

@Patater Patater requested a review from mpg May 5, 2020 14:21
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.

Still looks good to me.

@@ -578,8 +578,55 @@ psa_status_t psa_get_key_domain_parameters(
* (`PSA_ECC_CURVE_xxx`).
* \return \c 0 on failure (\p grpid is not recognized).
*/
psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid,
size_t *bits );
static inline psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not a small function thus is it ok to inline it regarding code size? No other way to do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure it is so large, but I'm not one to decide. The compiler may decide it is too large or complicated and avoid inlining, when code size optimizing flags are employed.

Alternatives would include making a new C file to implement this function, included in the S target build and again in the NS target build.

Copy link
Contributor

Choose a reason for hiding this comment

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

I decided to do some measurements:

scripts/config.pl baremetal
make CC=arm-none-eabi-gcc CFLAGS='-Os -mthumb -march=armv6-m' lib
arm-none-eabi-size -t library/libmbedcrypto.a | tail -n1

Before this PR: 236581
After this PR: 236601
Difference: +20 bytes

IMO this difference is perfectly acceptable, especially considering that this function is meant to disappear at some point, as it's an artifact of the transition from the legacy crypto APIs to PSA (should probably become useless at some point in the 3.x line and then be removed in 4.0).

Copy link
Contributor

Choose a reason for hiding this comment

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

Given the small size impact and that it is a temporary situation, fine by me then.

@mpg
Copy link
Contributor

mpg commented May 18, 2020

Note: the only test failing in the CI is the API/ABI checker, which is fully expected.

Copy link
Contributor

@ronald-cron-arm ronald-cron-arm left a comment

Choose a reason for hiding this comment

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

LGTM

@ronald-cron-arm ronald-cron-arm 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 18, 2020
@mpg mpg merged commit 5eae4dd into Mbed-TLS:development May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports component-crypto Crypto primitives and low-level interfaces component-platform Portability layer and build scripts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pk.c fails to link on dual world systems
4 participants