Skip to content

Initialize platform in trng test #9493

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 3 commits into from
Feb 19, 2019

Conversation

RonEld
Copy link
Contributor

@RonEld RonEld commented Jan 24, 2019

Description

Add calls to mbedtls_platform_setup() and
mbedtls_platform_terminate() to the trng greentea test, to
initialize the hardware acceleration engines, in some platforms.
However, this involves hal test including Mbed TLS API, which I am not sure is architecturally correct. On the other hand, mbedtls_platform_setup() is intended for initializing the HW acceleration engines, where needed.

This should fix IOTPART-6810

@ashok-rao Could you please check that this resolves the issue you encountered on LAIRD BL654, and if it does, please return the usage of Cryptocell in that library?

Pull request type

[x] Fix
[ ] Refactor
[ ] Target update
[ ] Functionality change
[ ] Docs update
[ ] Test update
[ ] Breaking change

Reviewers

@ARMmbed/mbed-os-crypto

@RonEld RonEld force-pushed the add_platform_initialization_in_trng_test branch from 20ad736 to 7dfe3e4 Compare January 24, 2019 16:52
@ciarmcom ciarmcom requested review from a team January 24, 2019 18:00
@ciarmcom
Copy link
Member

@RonEld, thank you for your changes.
@ARMmbed/mbed-os-hal @ARMmbed/mbed-os-test @ARMmbed/mbed-os-maintainers please review.

@dannybenor
Copy link

@Patater are there any other initialization functions required? @bulislaw Should this and other init functions called at some global part of mbedos?

@RonEld
Copy link
Contributor Author

RonEld commented Jan 27, 2019

Note:

  • psa_crypto_init() is already called inside inject_entropy_for_psa(), in line 85, so no other psa crypto initialization function required.

@dannybenor
Copy link

@RonEld this function should not be called in the test, but in mbed OS. We need the test to make sure mbed OS will work. Please move the call to a platform specific initialization code

Copy link

@dannybenor dannybenor left a comment

Choose a reason for hiding this comment

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

The call to mbedtls_platform_setup should be part of mbed OS and not the test

@RonEld
Copy link
Contributor Author

RonEld commented Jan 27, 2019

@dannybenor Although I agree with you, this is the design of the function, and of Mbed OS design.
It was original in the Mbed OS boot, but after long discussions, it was decided it should be called by the applications requesting Mbed TLS \ crypto operations.
@bulislaw please confirm that this is still the case

@RonEld
Copy link
Contributor Author

RonEld commented Jan 27, 2019

More information in IOTSSL-1576

@dannybenor
Copy link

@bulislaw need your input here. We have functionality in mbedOS that uses crypto. Adding a new target should not brake it. Adding the initialization to the test is not helping. If the function mbedtls_platform_terminate() is required for some targets, code should not work for any target if the function is not called.

@RonEld
Copy link
Contributor Author

RonEld commented Jan 27, 2019

In current design, modules in Mbed OS, that use Mbed TLS \ crypto, should call mbedtls_platform_setup() and mbedtls_platform_terminate(), like BLE does.
We have added a reference counter in the platform context, that will not actually terminate the platform hw acceleration, if still used.
See #7099

@dannybenor
Copy link

dannybenor commented Jan 28, 2019

@liatvindzberg @erezlandau FYI
@RonEld @sbutcher-arm why the function mbedtls_platform_setup() is not called inside psa_crypto_init()?
The implementation of devicekey and securestore passed a lot of review of the crypto team and nobody pointed that we need to call the function mbedtls_platform_setup(). That means we are setting a big trap for any developer.
The parameter mbedtls_platform_context looks like something not required if no other TLS function uses it.
https://context.reverso.net/translation/hebrew-english/%D7%91%D7%90%D7%A8%D7%96%D7%99%D7%9D+%D7%A0%D7%A4%D7%9C%D7%94+%D7%A9%D7%9C%D7%94%D7%91%D7%AA

@RonEld
Copy link
Contributor Author

RonEld commented Jan 28, 2019

why the function mbedtls_platform_setup() is not called inside psa_crypto_init()?

This is in discussion in ARMmbed/mbed-crypto#24 . IMHO, once PSA will be fully integrated within Mbed OS, mbedtls_platfor_setup() will be deprecated, or at least not handle the crypto operations. This function precedes PSA crypto.

The implementation of devicekey and securestore passed a lot of review of the crypto team and nobody pointed that we need to call the function mbedtls_platform_setup().

For PSA point of view, mbedtls_platform_setup() should not be called, and as I mentioned in previous answer, it should probably not be used once all crypto operations be called in SA crypto. Until that happens, mbedtls_platform_setup() and mbedtls_platform_terminate() should be called as well, and I think it's a mistake \ overlook that you weren't been pointed out on that.

@0xc0170 0xc0170 requested a review from bulislaw January 28, 2019 09:15
@bulislaw
Copy link
Member

bulislaw commented Feb 5, 2019

That's a breaking change isn't it? As in current use of TLS/crypto won't work without this change? Generally speaking, my personal opinion is, that we shouldn't initialise modules globally unless they are used by the OS itself. In this case user is well aware that he/she will need TLS/crypto so they should initialise it. That being said, it's a breaking change, is there a way for us to hide it in existing calls so that we are backward compatible? Could public API initialise the modules if they weren't initialised before?

@RonEld
Copy link
Contributor Author

RonEld commented Feb 6, 2019

I wouldn't say it's a breaking change. but a bug fix.
Without this fix, platforms that implemented their alternative platform initialization function( at the moment only NRF52840 AFAIK) will simply fail on this test. Other platforms will use the default implementation of mbedtls_platform_setup() which is empty in Mbed TLS.
Ever since this API was introduced, in mbedtls-2.7 and then mbed-os-5.8, this function was recomended to be used whenever a cryptographic operation is used. See https://os.mbed.com/docs/mbed-os/v5.11/porting/hardware-accelerated-crypto.html#mbed-tls-platform-context and IOTSSL-1576

That being said, it's a breaking change, is there a way for us to hide it in existing calls so that we are backward compatible? Could public API initialise the modules if they weren't initialised before?

As mentioned, I don't think it's a breaking change, but a bug fix, and we are backwards compatible. The NRF52480 didn't work before on this test, since the Cryptocell library porting was introduced in #6794
and now it does( It was a breaking change in that PR though). As for initialize the modules if they weren't initialized before, well, since #7099, there is a refernce counter in the platform context, so it is posisble, however, this requires all the Mbed TLS APIto call this function, which means changing Mbed TLS for this transition to mbed crypto stage, which I think is redundant. note that in ARMmbed/mbed-crypto#24 we are discussing how to integrate this function with psa_crypto_init(), but until this is done, and psa crypto is fully incorporated in Mbed OS, I think this change is needed.

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 7, 2019

This "simple" fix seems to be difficult to understand the implications. @RonEld Where is this captured (any docs) that these 2 functions need to be invoked when crypto used ? How we can proceed further?

@ARMmbed/mbed-os-crypto Please review

@RonEld
Copy link
Contributor Author

RonEld commented Feb 7, 2019

@RonEld
Copy link
Contributor Author

RonEld commented Feb 7, 2019

Copy link
Contributor

@yanesca yanesca left a comment

Choose a reason for hiding this comment

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

The implications of this fix:

  • The test will pass for every properly functioning and integrated TRNG
  • The test won't catch that some part of the OS uses the TRNG but does not call mbedtls_platform_setup() before.
  • We can't see yet how this affects future plans (see mbedtls_platform_setup and psa_crypto_init mbed-crypto#24)

If we are unhappy with the fix we have two other options:

  1. Leave it as it is and have correct implementations fail the test suite.
  2. Move mbedtls_platform_setup() back to Mbed OS boot.

This fix is a clear improvement to option 1 and I can see why we don't want option 2.

That being said this test does not catch that a part of the OS uses TRNG but does not call mbedtls_platform_setup(). This failure can potentially be silent and still catastrophic for security. Optimally we should identify every part of the OS that uses TRNG and add a test for that module that makes sure that the platform initialisation has been done where it is needed.

I am approving since this fix is a clear improvement, but it would be best to cover the gaps in the test suite. If that is out of scope for this PR, then we should raise an issue about it. (My approval is conditional on fixing the gap or raising an issue for it.)

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 7, 2019

Thanks @yanesca ! @dannybenor Do you agree with the above statement about options and approval for this PR?

@dannybenor
Copy link

O do not agree with adding the call to the test.
We need to find a better way to integrate into mbed os

@RonEld
Copy link
Contributor Author

RonEld commented Feb 7, 2019

From what you are saying, you also do not agree of having psa_crypto_init in the test, as it is basically intended to do the same thing, once crypto will be fully integrated

@RonEld
Copy link
Contributor Author

RonEld commented Feb 10, 2019

Note: Having the initialization done at boot time, causing the hw acceleration engine to always be "on", might have side effects of high power consumption. see #9372 for example.

@simonbutcher
Copy link
Contributor

@RonEld @sbutcher-arm why the function mbedtls_platform_setup() is not called inside psa_crypto_init()?
The implementation of devicekey and securestore passed a lot of review of the crypto team and nobody pointed that we need to call the function mbedtls_platform_setup(). That means we are setting a big trap for any developer.

At the moment we're moving the crypto APIs from Mbed TLS to PSA. We haven't completed that work yet, and it's still in transition. When we have completed that work, I'd expect users of only Crypto APIs to not need to call mbedtls_platform_setup(). That's not true yet, but it should become true when we've finished the work.

cc: @Patater

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 11, 2019

@RonEld Can you talk to @dannybenor to resolve this please? We shall find a path forward or close this PR

@dannybenor
Copy link

@RonEld This PR can be enhanced to make sure all uses of crypto in mbed OS will work, not only this test.
Will be good also to make sure it is clear in the documentation

@mbed-ci
Copy link

mbed-ci commented Feb 15, 2019

Test run: FAILED

Summary: 1 of 1 test jobs failed
Build number : 1
Build artifacts

Failed test jobs:

  • jenkins-ci/mbed-os-ci_unittests

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 15, 2019

Unittest failures are related, please review

@RonEld
Copy link
Contributor Author

RonEld commented Feb 18, 2019

I have fixed failures in unittests.

Once CI completes successfully, I will squash the commits to dcc194f

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 18, 2019

CI started

Ron Eldor added 3 commits February 18, 2019 11:43
Add calls to `mbedtls_platform_setup()` and
`mbedtls_platform_terminate()` to the trng greentea test, to
initialize the hardware acceleration engines, in some platforms.
Add calls to `mbedtls_platform_setup()`
and `mbedtls_platform_teardown()` to all modules and tests using Mbed TLS.
Use a singleton Mutex in platforms_alt functions, to be shared
with the trng function, to save RAM. Rename `platform_alt.c`
to `platform_alt.cpp` as the mutex is in a `singletonPtr`
template class.
@RonEld
Copy link
Contributor Author

RonEld commented Feb 18, 2019

@0xc0170 Do you want me to squash the CI fixes, or is there a need for reviewing them before?

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 18, 2019

As soon as CI finishes, the log could be cleaned. Let us know once done

@mbed-ci
Copy link

mbed-ci commented Feb 18, 2019

Test run: SUCCESS

Summary: 12 of 12 test jobs passed
Build number : 2
Build artifacts

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 18, 2019

@RonEld This shall be ready for integration, do you want to squash and we rerun ci?

@RonEld RonEld force-pushed the add_platform_initialization_in_trng_test branch from a70a563 to 77f9faf Compare February 18, 2019 11:45
@RonEld
Copy link
Contributor Author

RonEld commented Feb 18, 2019

@0xc0170 done

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 18, 2019

CI restarted

@mbed-ci
Copy link

mbed-ci commented Feb 18, 2019

Test run: FAILED

Summary: 1 of 12 test jobs failed
Build number : 3
Build artifacts

Failed test jobs:

  • jenkins-ci/mbed-os-ci_greentea-test

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 19, 2019

I restarted CI to get new results

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 19, 2019

@RonEld all green, reviewed, this shall be now ready for integration

@cmonr cmonr merged commit feae56e into ARMmbed:master Feb 19, 2019
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.