Skip to content

Add option to build SHA-512 without SHA-384 #179

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 16 commits into from
Jan 30, 2020
Merged

Add option to build SHA-512 without SHA-384 #179

merged 16 commits into from
Jan 30, 2020

Conversation

mpg
Copy link
Contributor

@mpg mpg commented Jul 17, 2019

This adds a config.h option MBEDTLS_SHA512_NO_SHA384 that allows to build with SHA-512 enabled but not SHA-384, for people who don't need SHA-384 and want to save on code size.

This is very comparable to https://github.com/ARMmbed/mbedtls-restricted/pull/621 (which should be side-ported).

Contrary to SHA-256 however, it seems desirable to also have a symmetrical option for people who only need SHA-384 but not SHA-512. In order to keep this PR small and allow it to move forward quickly, it was not included but can be done later.

Reviewers, please double-check if I didn't miss anything to do with PSA as I'm less familiar with that part.

Size, building with CFLAGS='-Os -mcpu=cortex-m0plus -mthumb' CC=arm-none-eabi-gcc (ARM-GCC 7.3) after running scripts/config.pl baremetal (don't pay attention to the absolute values as this is a full config, only the difference is interesting):

Default Without selftest
libmbedcrypto.a with SHA-384 228769 192320
libmbedcrypto.a without SHA-384 228353 192128
gain in Bytes 416 192

Note: If #327 is merged first, this PR will need to be modified to add a dependency on !MBEDTLS_SHA512_NO_SHA384 to some test cases added by #327.

@mpg mpg added enhancement New feature or request needs: review The pull request is ready for review. This generally means that it has no known issues. labels Jul 17, 2019
@gilles-peskine-arm
Copy link
Collaborator

Please extend tests/scripts/depends-hashes.pl to support the new options.

@Patater Patater added the needs: work The pull request needs rework before it can be merged. label Sep 10, 2019
@mpg
Copy link
Contributor Author

mpg commented Sep 11, 2019

Note: an extension of depends-hashes.pl for a similar option has been done in cousin PR https://github.com/ARMmbed/mbedtls-restricted/pull/621 and would be pretty straightforward to adapt here.

@mpg mpg removed the needs: work The pull request needs rework before it can be merged. label Sep 11, 2019
@mpg
Copy link
Contributor Author

mpg commented Sep 11, 2019

@gilles-peskine-arm I rebased in order to resolve conflicts (previous state is at https://github.com/mpg/mbed-crypto/tree/sha512-no-sha384-1) and extended depends-hashes.pl as you suggested. I think this is now ready for review again.

Copy link
Collaborator

@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.

What's there is good other than md_wrap.c and cosmetic issues in test data. But several things are missing.

@gilles-peskine-arm gilles-peskine-arm added needs: work The pull request needs rework before it can be merged. and removed needs: review The pull request is ready for review. This generally means that it has no known issues. labels Sep 11, 2019
@piotr-now piotr-now self-requested a review December 16, 2019 15:00
@@ -59,8 +59,10 @@ typedef struct mbedtls_sha512_context
uint64_t total[2]; /*!< The number of Bytes processed. */
uint64_t state[8]; /*!< The intermediate digest state. */
unsigned char buffer[128]; /*!< The data block being processed. */
#if !defined(MBEDTLS_SHA512_NO_SHA384)
int is384; /*!< Determines which function to use:
0: Use SHA-512, or 1: Use SHA-384. */
Copy link
Contributor

Choose a reason for hiding this comment

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

I would change int is384; in to char is384; or even bool to keep good practice with structure padding

Copy link
Contributor Author

@mpg mpg Jan 6, 2020

Choose a reason for hiding this comment

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

That would be an ABI change, so I'd rather not do it here.

extern const mbedtls_md_info_t mbedtls_sha384_info;
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

There is many places when you use this sentence with double negation "#if !defined(MBEDTLS_SHA512_NO_SHA384)" an it makes riding it is not very friendly. This is a second advantage to use option like MBEDTLS_SHA512_WITH_SHA384

Copy link
Contributor

Choose a reason for hiding this comment

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

Whether mbedtls_sha384_info shouldn't be excluded in md.c also?

mpg added 6 commits January 6, 2020 11:40
Saves 140 bytes on sha512.o, measured with:

arm-none-eabi-gcc -Wall -Wextra -Iinclude -Os -mcpu=cortex-m0plus -mthumb -c library/sha512.c && arm-none-eabi-size sha512.o

arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 7-2018-q2-update) 7.3.1 20180622 (release) [ARM/embedded-7-branch revision 261907]

Todo:
- fix selftest
- fix dependencies in test suites
- implement in MD layer
@mpg
Copy link
Contributor Author

mpg commented Jan 6, 2020

I just rebased on current development and force-pushed in order to resolve conflicts. The previous state of the branch was saved at https://github.com/mpg/mbed-crypto/tree/sha512-no-sha384-2

I'll push more commits in order to address review comments later.

@mpg
Copy link
Contributor Author

mpg commented Jan 14, 2020

@gilles-peskine-arm @piotr-now I believe I addressed your comments, either by changing the code as suggested, or commenting why I'd rather not. This is ready for you to review again.

piotr-now
piotr-now previously approved these changes Jan 14, 2020
Copy link
Collaborator

@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.

LGTM apart from a few minor issues.

There's no test of trying to use SHA-384 when it's disabled. But this is a preexisting test gap so this PR doesn't need to do anything about it.

mpg added 2 commits January 24, 2020 10:59
Other cases in this switch statement aren't guarded either.
Other modules have similar internal macros using _LENGTH in the name.
@mpg
Copy link
Contributor Author

mpg commented Jan 24, 2020

@gilles-peskine-arm Thanks for your feedback, and clarifying when I didn't immediately understand it. I believe I've addressed all of it so feel free to review again!

@mpg mpg added needs: review The pull request is ready for review. This generally means that it has no known issues. and removed needs: work The pull request needs rework before it can be merged. labels Jan 24, 2020
@mpg
Copy link
Contributor Author

mpg commented Jan 27, 2020

The CI passes all tests except the ones involving Mbed OS, which is a known issue independent from this PR, so it's as good as a pass as far as merging this PR is concerned.

Copy link
Collaborator

@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.

One minor issue I missed earlier, other than that LGTM

library/sha512.c Outdated
@@ -587,7 +587,7 @@ static const unsigned char sha512_test_sum[][64] =
0x4E, 0xAD, 0xB2, 0x17, 0xAD, 0x8C, 0xC0, 0x9B }
};

#define ARRAY_LEN(a) ( sizeof( a ) / sizeof( a[0] ) )
#define ARRAY_LENGTH(a) ( sizeof( a ) / sizeof( a[0] ) )
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
#define ARRAY_LENGTH(a) ( sizeof( a ) / sizeof( a[0] ) )
#define ARRAY_LENGTH( a ) ( sizeof( a ) / sizeof( ( a )[0] ) )

@mpg
Copy link
Contributor Author

mpg commented Jan 29, 2020

@gilles-peskine-arm Thanks for catching that issue. I fixed it, this should now be ready for re-review.

@piotr-now You reviewed and approved a previous version of this PR, do you think you'd be able to review it again? Thanks!

@piotr-now
Copy link
Contributor

LGTM

@mpg
Copy link
Contributor Author

mpg commented Jan 30, 2020

The CI passes all tests except for the ones relying on mbed-os, which is a know issue with mbed-os, unrelated to this PR, so it's a s good as a pass.

@mpg mpg added ready for merge Design and code approved, CI passed, and likewise for backports. Label added by gatekeepers only. and removed needs: review The pull request is ready for review. This generally means that it has no known issues. labels Jan 30, 2020
@mpg mpg merged commit f712e16 into ARMmbed:development Jan 30, 2020
gilles-peskine-arm added a commit to gilles-peskine-arm/mbed-crypto that referenced this pull request Feb 3, 2020
Previously in d875285:
* ARMmbed#333: Streamline PSA key type encodings: prepare
* ARMmbed#323: Initialise return values to an error

Previously in dbcb442:
* ARMmbed#291: Test MBEDTLS_CTR_DRBG_USE_128_BIT_KEY
* ARMmbed#334: Fix some pylint warnings

Previously in ceceedb:
* ARMmbed#348: Bump version to Mbed TLS 2.20.0 and crypto SO version to 4
* ARMmbed#354: Fix incrementing pointer instead of value

In this commit:
* ARMmbed#349: Fix minor defects found by Coverity
* ARMmbed#179: Add option to build SHA-512 without SHA-384
* ARMmbed#327: Implement psa_hash_compute and psa_hash_compare
* ARMmbed#330: Streamline PSA key type and curve encodings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ready for merge Design and code approved, CI passed, and likewise for backports. Label added by gatekeepers only.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants