Skip to content

CM3DS Maintenance Pull Request: TRNG support (3/4) #6169

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 1 commit into from
Apr 12, 2018

Conversation

hug-dev
Copy link
Contributor

@hug-dev hug-dev commented Feb 22, 2018

This pull request brings TRNG support to the CM3DS target.
It is the pull request 3 among the 4 concerning CM3DS Maintenance. Please check #6119 for the full scope.

Other pull requests regarding this target:

@ciarmcom
Copy link
Member

ARM Internal Ref: IOTSSL-2109

Copy link
Member

@bulislaw bulislaw left a comment

Choose a reason for hiding this comment

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

@0xc0170 @cmonr @adbridge @sg- This commit includes new external OS (apache2) project with all the bells and whistles. Any thoughts?

@cmonr
Copy link
Contributor

cmonr commented Feb 27, 2018

Seems fine to me as long as the source project is left asis, similar to how we include external libraries as well. Apache 2.0 is fine.

For reference: https://os.mbed.com/docs/latest/reference/guidelines.html#license

Or are you referring to the files in targets/TARGET_ARM_SSG/TARGET_CM3DS_MPS2/device/drivers/TZ-TRNG/host/* ?

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 27, 2018

Or are you referring to the files in targets/TARGET_ARM_SSG/TARGET_CM3DS_MPS2/device/drivers/TZ-TRNG/host/* ?

I believe so, that is 3rd library in this pull request. Looks good to me as it is

@hug-dev Please can you rebase?

@hug-dev
Copy link
Contributor Author

hug-dev commented Feb 27, 2018

Seems fine to me as long as the source project is left asis, similar to how we include external libraries as well. Apache 2.0 is fine.

Yes, I made sure to leave the project as it is, I had to make a pull request (ARM-software/TZ-TRNG#8) to the project to make it work with mbed-os.
version.txt file contains the commit SHA of the project and the (tiny) differences with it.

@hug-dev Please can you rebase?

Yes sure, I am going to do it.

@hug-dev
Copy link
Contributor Author

hug-dev commented Feb 27, 2018

Just rebased to latest master.

}

/* zeroe the rnd buff */
tztrng_memset((uint8_t *)rndWorkBuff, 0, CC_RND_WORK_BUFFER_SIZE_WORDS * sizeof(uint32_t));

Choose a reason for hiding this comment

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

Consider using sizeof( rndWorkBuff ) instead here.

}

/* try to reduce the memcpy */
if (requireBytes < (int)sourceOutSizeBytes)

Choose a reason for hiding this comment

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

Strictly speaking, this is a dangerous cast - if LLF_RND_GetTrngSource is compromised and sets sourceOutSizeBytes to a value above MAX_INT, then (int) sourceOutSizeBytes may be negative, and the resulting else branch performs - which uses sourceOutSizeBytes as a uint32_t - performs a fauly memcpy.

@hug-dev
Copy link
Contributor Author

hug-dev commented Mar 1, 2018

Thanks @hanno-arm for your review. I agree with both of them.

As it has been said before, the TZ-TRNG project introduced with this commit is an external open-source project (https://github.com/ARM-software/TZ-TRNG) which I do not have ownership with. For maintenance reasons, it would be better if there are no differences between the version of that project here and the original one.
For that matter, I can make a pull-request with your suggested changes to original project and, if they get merged, I can then import here the new version.

What do you think about that?

@0xc0170
Copy link
Contributor

0xc0170 commented Mar 8, 2018

For that matter, I can make a pull-request with your suggested changes to original project and, if they get merged, I can then import here the new version.

Sounds good 👍

@hanno-arm @mazimkhan Anything blocking that should take priority to go upstream?

if (requireBytes < (int)sourceOutSizeBytes)
tztrng_memcpy(outAddr, (uint8_t *)rndSrc_ptr, requireBytes);
else
tztrng_memcpy(outAddr, (uint8_t *)rndSrc_ptr, sourceOutSizeBytes);

Choose a reason for hiding this comment

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

rndSrc_ptr should also be scrubbed otherwise we are leaking entropy on stack.

Choose a reason for hiding this comment

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

If rndSrc_ptr is from rndWorkBuff then please clear rndWorkBuff.

Copy link
Contributor Author

@hug-dev hug-dev Mar 12, 2018

Choose a reason for hiding this comment

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

I am not sure to understand well: do you want me to write requireBytes or sourceOutSizeBytes zeroes at rndStr_ptr address location after the memcpy call? Also, should I do it for every iteration of the while loop or just once at the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for my question, I understood what you meant. I will send my pull-request soon.

Choose a reason for hiding this comment

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

No worries. Sorry for late reply. Yes I meant:

write requireBytes or sourceOutSizeBytes zeroes at rndStr_ptr address location after the memcpy call

hug-dev added a commit to hug-dev/TZ-TRNG that referenced this pull request Mar 14, 2018
The (int) cast of sourceOutSizeBytes could result in a negative value if
this variable is bigger than INT_MAX which can lead to a faulty memcpy.
Two other changes have been done on the same function:
* use sizeof on the array to have its size in bytes directly
* clear the rndWorkBuff array to not leak entropy on the stack

The change here proposed are a result of the review of the TZ-TRNG
driver integrated into CM3DS port on mbed OS. It has been reviewed in
the pull request ARMmbed/mbed-os#6169

Change-Id: Ifdfa8ac854365989723239d382f12f42648429f1
Signed-off-by: Hugues de Valon <[email protected]>
hug-dev added a commit to hug-dev/TZ-TRNG that referenced this pull request Mar 14, 2018
The (int) cast of sourceOutSizeBytes could result in a negative value if
this variable is bigger than INT_MAX which can lead to a faulty memcpy.
Two other changes have been done on the same function:
* use sizeof on the array to have its size in bytes directly
* clear the rndWorkBuff array to not leak entropy on the stack

The change here proposed are a result of the review of the TZ-TRNG
driver integrated into CM3DS port on mbed OS. It has been reviewed in
the pull request ARMmbed/mbed-os#6169

Signed-off-by: Hugues de Valon <[email protected]>
@hug-dev
Copy link
Contributor Author

hug-dev commented Mar 14, 2018

The pull request ARM-software/TZ-TRNG#9 has been added!

@cmonr
Copy link
Contributor

cmonr commented Mar 21, 2018

@hanno-arm @mazimkhan @0xc0170 Are y'all please with the updated commit?

* @param output_length The length of generated data
* @return 0 success, -1 fail
*/
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we including here doxygen (they are from header file copy paste?) ? If yes, we shall remove them. If there is another info that should be added here, could do via own implementation comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are indeed copied from the header file. I am going to remove them.

This patchs adds TRNG support using the upstreamed, open-source, TZ-TRNG
driver.
It also implements the HAL for TRNG and add it in features.
The mbed-os.py script deletes files that are unused by mbed-os.

Change-Id: Idf8eefd809f00d40e0ad3cf7657f2a8c2eca3505
Signed-off-by: Hugues de Valon <[email protected]>
@hug-dev
Copy link
Contributor Author

hug-dev commented Apr 6, 2018

I have just pushed an updated commit. It contains all the modifications previously done and, instead of adding a .mbedignore file adds a mbed-os.py script that deletes unused files. This same script was used before pushing and can be use in the future when new versions of the TRNG driver are out.

Copy link

@hanno-becker hanno-becker left a comment

Choose a reason for hiding this comment

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

The TRNG code in trng_api.c looks fine to me. As I understand @0xc0170, a review of the underlying CC driver code is not needed here.

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 10, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Apr 10, 2018

Build : SUCCESS

Build number : 1701
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/6169/

Triggering tests

/morph test
/morph uvisor-test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Apr 10, 2018

@mbed-ci
Copy link

mbed-ci commented Apr 10, 2018

@hug-dev
Copy link
Contributor Author

hug-dev commented Apr 10, 2018

I may be wrong but it seems that tests that failed are not related with this PR (as the files changed may not even be compiled in the tests). It is maybe that I am using an old Mbed OS version, if that is the case I can rebase this pull request.

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 10, 2018

/morph test

@mbed-ci
Copy link

mbed-ci commented Apr 11, 2018

@ashok-rao
Copy link
Contributor

Looks like failure on a different un-related platform..

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 11, 2018

That is correct, the target needs to be looked at , not related. I've just commented in another 3 PR with the same problem

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 11, 2018

/morph test

@mbed-ci
Copy link

mbed-ci commented Apr 12, 2018

Test : SUCCESS

Build number : 1523
Test logs :http://mbed-os-logs.s3-website-us-west-1.amazonaws.com/?prefix=logs/6169/1523

@cmonr cmonr merged commit 641e814 into ARMmbed:master Apr 12, 2018
@RonEld RonEld mentioned this pull request May 7, 2018
hug-dev added a commit to hug-dev/TZ-TRNG that referenced this pull request May 10, 2018
This commit updates the way this driver is integrated in Mbed OS.
It adds a Python script, mbed-os.py that deletes some files to use the
driver in 800-90B TRNG mode. It also updates the README.md with the new
instructions.
The pull request ARMmbed/mbed-os#6169 was merged in Mbed OS using this
new way.

Signed-off-by: Hugues de Valon <[email protected]>
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.

9 participants