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

Fix hmac_drbg failure in benchmark, with threading #1758

Merged
merged 2 commits into from
Sep 27, 2018

Conversation

RonEld
Copy link
Contributor

@RonEld RonEld commented Jun 20, 2018

Description

Remove redunadnat calls to hmac_drbg_free() between seeding operations,
which make the mutex invalid. Fixes #1095

Status

READY

Requires Backporting

Yes
Which branch?
mbedtls-2.1
mbedtls-2.7

Migrations

NO

Todos

  • Tests
  • Documentation
  • Changelog updated
  • Backported

Steps to test or reproduce

Outline the steps to test or reproduce the PR here.

@RonEld RonEld added bug mbed TLS team needs-review Every commit must be reviewed by at least two team members, needs-backports Backports are missing or are pending review and approval. component-crypto Crypto primitives and low-level interfaces labels Jun 20, 2018
Copy link
Contributor

@andresag01 andresag01 left a comment

Choose a reason for hiding this comment

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

I briefly looked at this PR and left a question regarding API usage. Also, please note that this problem also happens in the (mbed-os-example-tls repo](https://github.com/ARMmbed/mbed-os-example-tls/blob/master/benchmark/main.cpp#L689).

@@ -648,7 +648,6 @@ int main( int argc, char *argv[] )
TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
mbedtls_exit(1) );
mbedtls_hmac_drbg_free( &hmac_drbg );
Copy link
Contributor

Choose a reason for hiding this comment

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

I definitely think that calling mbedtls_hmac_drbg_free() here is not correct, but I am not sure whether just removing it should work in all cases. That is, there are probably no issues with just calling mbedtls_hmac_drbg_seed() repeatedly when using the default software code, but I am not sure what happens if, for example, the implementation is a hardware accelerator. Are we expected to call init->seed->seed->free or is it necessary to do init->seed->free->init->seed->free?

Also, I was trying to check what happens inside mbedtls_hmac_drbg_seed(), so I looked up the documentation for mbedtls_md_setup() which is called internally from seed and found the following:

 *                  It should be called after mbedtls_md_init() or
 *                  mbedtls_md_free(). Makes it necessary to call
 *                  mbedtls_md_free() later.

So according to this, it not necessary to call init at all when using the MD abstraction?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@andresag01 if by HW acceleration you mean the HW entropy source, then I don't see a problem. Calling seed the second time is similar to reseeding. The HW should be stateless and should return the desirted entropy whenever asked.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok

@RonEld
Copy link
Contributor Author

RonEld commented Jun 27, 2018

@andresag01 I agree that the mbed-os-example-tls has the same issue.
Do you know if MBEDTLS_THREADING_C can be defined in Mbed OS?

@andresag01
Copy link
Contributor

As far as I remember, we had some pending changes to Mbed TLS to make sure that the Mbed TLS multithreading abstraction used through MBEDTLS_THREADING_C had an implementation in Mbed OS, but I do not think it has been merged because some changes are needed to the Mbed TLS APIs (e.g. #1131) that have not been finalized. So I think for now the mbed-os-example-tls will work, but it would still be good to fix it there as well or to raise an issue.

ChangeLog Outdated
= mbed TLS x.xx.x branch released xxxx-xx-xx

Bugfix
* Fix efailure in hmac_drbg in the benchmark sample application, when
Copy link
Contributor

Choose a reason for hiding this comment

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

'efailure?' - typo surely?

@simonbutcher simonbutcher added needs-work and removed needs-review Every commit must be reviewed by at least two team members, labels Jul 4, 2018
@RonEld RonEld added the needs-review Every commit must be reviewed by at least two team members, label Jul 4, 2018
@RonEld
Copy link
Contributor Author

RonEld commented Jul 4, 2018

@sbutcher-arm I fixed the typo

I will rebase to resolve conflicts once the PR is reviewed.

simonbutcher
simonbutcher previously approved these changes Jul 5, 2018
@simonbutcher
Copy link
Contributor

@RonEld - I've approved it, but I think you should go ahead and rebase now, else you'll need another review now, and another two after rebasing, which will just slow things down.

Ron Eldor added 2 commits July 5, 2018 14:33
Remove redunadnat calls to `hmac_drbg_free()` between seeding operations,
which make the mutex invalid. Fixes Mbed-TLS#1095
Fix typo in ChangeLog entry.
@RonEld
Copy link
Contributor Author

RonEld commented Jul 5, 2018

@sbutcher-arm I have rebased and resolved conflicts.
Could you please review?
cc @andresag01

@RonEld RonEld removed the needs-work label Jul 5, 2018
@RonEld
Copy link
Contributor Author

RonEld commented Jul 30, 2018

Backported to branches 2.1 and 2.7 in #1898 and #1899

cc @andresag01 @sbutcher-arm

Copy link
Contributor

@simonbutcher simonbutcher 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 - however, it needs back porting to the Mbed OS example as you can see here.

@simonbutcher simonbutcher 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 Sep 8, 2018
@simonbutcher
Copy link
Contributor

simonbutcher commented Sep 8, 2018

@RonEld - Can you please backport this to the equivalent Mbed OS example?

@andresag01
Copy link
Contributor

@sbutcher-arm @RonEld: Please note that the PR ARMmbed/mbed-os-example-tls#194 already addresses these problems.

@RonEld
Copy link
Contributor Author

RonEld commented Sep 12, 2018

Removing the "needs backports" label, as all backports have been fully approved

@RonEld RonEld removed the needs-backports Backports are missing or are pending review and approval. label Sep 12, 2018
@simonbutcher simonbutcher merged commit 636179a into Mbed-TLS:development Sep 27, 2018
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 bug component-crypto Crypto primitives and low-level interfaces
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants