Skip to content

Conversation

@Patater
Copy link
Contributor

@Patater Patater commented Nov 7, 2019

  • Improve the example by commenting where global keys are used for strictly pedagogical purposes.
  • Align code snippets with changes in Mbed Crypto's getting started guide, to ensure we keep testing our getting started guide (as this example is the only time those snippets are tested, currently)
  • Reduce stack usage by using global static const keys, instead of #defines for initializer lists to place keys on the stack.

@Patater Patater added the bug Something isn't working label Nov 7, 2019
@Patater
Copy link
Contributor Author

Patater commented Nov 7, 2019

Tested with ARMC6 and the develop profile. IAR not yet tested.

Copy link

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

Ok for the workaround, but this should be documented as a workaround in the code itself, especially since this is in example code.

psa_status_t status;
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
uint8_t key[] = RSA_KEY;
static uint8_t key[] = RSA_KEY;

Choose a reason for hiding this comment

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

Having a static variable is unusual, and usually done when the value needs to be preserved between calls, which is not the case here. Please add a comment in the code to indicate why these two variables are static.

psa_status_t status;
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
uint8_t key[] = RSA_KEY;
static uint8_t key[] = RSA_KEY;

Choose a reason for hiding this comment

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

Preexisting, but would be nice to do what you're at it: key should be const.

@Patater
Copy link
Contributor Author

Patater commented Nov 7, 2019

This is not meant as any sort of workaround. This is meant to reduce stack usage to make the example more portable. We could use a global key instead if use of static in this situation appears uncouth.

@Patater
Copy link
Contributor Author

Patater commented Nov 7, 2019

New patchset uses static const for key and adds a comment explaining why the writable signature is static, as that might be a bit surprising. static const is idiomatic enough that it doesn't need a comment.

uint8_t hash[] = "INPUT_FOR_SIGN";
uint8_t signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
/* 'signature' is static to reduce stack usage. */
static uint8_t signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Making signature static is not necessary for the targets we run this example on so far. So, if we dislike adding static here, we can remove it as we don't need it yet. Just making the key static const is good enough to pass the tests with ARMC6-develop at least.

@Patater
Copy link
Contributor Author

Patater commented Nov 7, 2019

Buddy PR at ARMmbed/mbed-crypto#317

Copy link
Contributor

@dgreen-arm dgreen-arm left a comment

Choose a reason for hiding this comment

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

With these changes, the example works with latest Mbed OS using ARMC6 or IAR

Make it more clear that one should not have global, hard-coded keys in
their code. We do this for example purposes only and real-world
applications should follow secure practices.
The getting started guide in Mbed Crypto was recently changed to make it
more obvious that `AES_KEY` and `RSA_KEY` were shorthand for key
material. The guide did this by using function parameters instead of
all-caps shorthand. In order to better match what is in our getting
started guide with this getting started example, we make the same
function parameter changes here, passing global key data in via
`main()`.

Passing global key data in via `main()` has the additional benefit of
reducing stack usage of each example snippet function. This enables the
example to run on more boards, those with more limited stack space.
@Patater Patater changed the title getting-started: Reduce RSA sign stack usage getting-started: Align with upstream getting started snippets, comment about pedogogical code, and reduce stack usage Nov 8, 2019
@Patater Patater changed the title getting-started: Align with upstream getting started snippets, comment about pedogogical code, and reduce stack usage getting-started: Align with upstream getting started snippets, comment about pedagogical code, and reduce stack usage Nov 8, 2019
@Patater
Copy link
Contributor Author

Patater commented Nov 8, 2019

New patchset makes the keys global static const instead to reduce RAM usage, while also improving the readability of the example.

Copy link
Contributor

@dgreen-arm dgreen-arm left a comment

Choose a reason for hiding this comment

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

Example runs successfully on ARMC6 and IAR, LGTM

@gilles-peskine-arm
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants