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

SHA-3 support #5820

Merged
merged 27 commits into from
Jun 7, 2023
Merged

SHA-3 support #5820

merged 27 commits into from
Jun 7, 2023

Conversation

polhenarejos
Copy link
Contributor

@polhenarejos polhenarejos commented May 8, 2022

Description

It adds support for SHA-3 with 224, 256, 384 and 512.

Splitted from #5800.

Status

READY

Requires Backporting

NO

Migrations

NO

Additional comments

Any additional information that could be of interest

Todos

Signed-off-by: Pol Henarejos <[email protected]>
@polhenarejos polhenarejos changed the title SHA-3 support. SHA-3 support May 9, 2022
@tom-cosgrove-arm tom-cosgrove-arm added enhancement needs-review Every commit must be reviewed by at least two team members, component-crypto Crypto primitives and low-level interfaces needs-ci Needs to pass CI tests Community needs-reviewer This PR needs someone to pick it up for review priority-medium Medium priority - this can be reviewed as time permits labels May 9, 2022
@tom-cosgrove-arm tom-cosgrove-arm added priority-scheduled This PR is big - it will require time to be scheduled for review and removed priority-medium Medium priority - this can be reviewed as time permits labels May 9, 2022
@gilles-peskine-arm gilles-peskine-arm removed the needs-ci Needs to pass CI tests label May 13, 2022
Copy link
Contributor

@d3zd3z d3zd3z left a comment

Choose a reason for hiding this comment

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

Just a minor formatting issue, and potential clarification on the meaning of the 'olen' parameter. This may be documented better once the SHAKE variants come in.

Signed-off-by: Pol Henarejos <[email protected]>
@mpg
Copy link
Contributor

mpg commented May 20, 2022

For reference, there was some earlier work in this area: #1549 (a continuation of #479). We're closing the previous PRs and keeping this one as it's fresh and active while the previous one now has numerous conflicts, but the previous PR can be used as a comparison point. In particular, #1549 did some work on tests, so we should make the the coverage in this PR is at least as good, and if not, import relevant tests from the previous PR to complete coverage.

@mpg mpg mentioned this pull request May 20, 2022
@polhenarejos
Copy link
Contributor Author

Added SHA3 to MD to support HMAC with SHA3. Also added tests (for test suite) from #1549 but no self-tests yet. Are self-tests necessary despite of test suite? Seems test suites are more complete.

@d3zd3z
Copy link
Contributor

d3zd3z commented May 20, 2022

@polhenarejos wrote:

Are self-tests necessary despite of test suite? Seems test suites are more complete.

Some types of compliance will require self tests (e.g. FIPS-140 and friends), which require confirmation of the code actually running in the environment (this is supposed to help detect modified code and similar). It can be useful to be able to run a quick test at run time to make sure the operations are as expected.

Copy link
Contributor

@d3zd3z d3zd3z left a comment

Choose a reason for hiding this comment

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

In addition, a few of the newer commits have a fairly long first line (git convention is to have a <70 character summary line, a blank line, and then a longer description). This makes commands like 'git shortlog' and 'git log --oneline' more useful. For the "olen parameter shall..." commit, perhaps a description line that is something like "sha3: Fix olen parameter handling", a blank line, and then the existing text refilled to 75 columns or so.

For "olen = 0 is not allowed" just put a blank line after that line

For "Fix when reusing..." probably put the blank line before the value in parens, remove the parens, and make that a complete sentence.

"Fix travis build" could probably use a better description, since the issue being fixed is actually a bit more than just for travis. The enum values shouldn't be conditionalized, which happens to let Travis work.

library/sha3.c Outdated
break;
}

if( p == NULL || p->id == MBEDTLS_SHA3_NONE )
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any reason to still have the p == NULL check? It is always assigned above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really. It is a paranoid check I usually do before -> operator.

Copy link

Choose a reason for hiding this comment

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

Sorry, just happened to see this, and wanted to point out some static analysis tools like Coverity will very likely warn about this. Not only cannot p be NULL (because it points to a global variable), but also it is dereferenced before the check. So even as a "just in case", this would do more harm than good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds logic. I pushed a change.

Copy link
Contributor

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

@polhenarejos polhenarejos force-pushed the sha3 branch 2 times, most recently from 45003eb to 39fb1d5 Compare February 7, 2023 18:49
Signed-off-by: Pol Henarejos <[email protected]>
Copy link
Contributor

@daverodgman daverodgman left a comment

Choose a reason for hiding this comment

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

Mostly LGTM - most of my comments are pretty minor.

Testing for bad arguments, and tidying up the public interface are the main ones IMO.

I had some time to kill on the plane when I reviewed this so also did some performance optimisation. I'll push that in a separate PR as soon as this gets merged so as not to hold this one up.

@@ -0,0 +1,3 @@
Features
* Add SHA3 family hash functions.
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: please be specific about which variants have been added. Please write SHA3 as SHA-3 (lots of instances of this).

int mbedtls_sha3_starts(mbedtls_sha3_context *ctx, mbedtls_sha3_id id)
{
mbedtls_sha3_family_functions *p = NULL;
if (ctx == NULL) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @d3zd3z - we have quite a lot of NULL checks here that aren't needed. I suggest being consistent with the level of null checking in the other hash implementations.

/**
* \brief Checkup routine for the algorithms implemented
* by this module: SHA3-224, SHA3-256, SHA3-384, SHA3-512,
* SHAKE128, SHAKE256, cSHAKE128 and cSHAKE256.
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this doesn't implement SHAKE please update this.

return MBEDTLS_ERR_SHA3_BAD_INPUT_DATA;
}

if (ilen == 0 || input == NULL) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The check for ilen == 0 can be removed, it's covered by the while condition

{
uint64_t lane[5];
uint64_t *s = ctx->state;
int i;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: normally I'd prefer to just initialise this in the for loop where it's used.

*
* The structure is used SHA-3 checksum calculations.
*/
typedef struct mbedtls_sha3_context {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
typedef struct mbedtls_sha3_context {
typedef struct {

Can simplify this a little I think

@@ -148,3 +149,142 @@ void sha512_selftest()
TEST_EQUAL(mbedtls_sha512_self_test(1), 0);
}
/* END_CASE */

Copy link
Contributor

Choose a reason for hiding this comment

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

Testing mostly looks good, but we don't have any tests for bad arguments - e.g., passing an invalid hash id, or olen too short.


uint16_t r;
uint16_t olen;
uint8_t xor_byte;
Copy link
Contributor

@daverodgman daverodgman May 30, 2023

Choose a reason for hiding this comment

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

I suggest removing xor_byte and simply using a #define in sha3.c, since this is the same for all implemented algorithms.


uint16_t r;
uint16_t olen;
uint8_t xor_byte;
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest removing xor_byte (replace with a #define in sha3.c), id (it's not used after init), and r (not used after init).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I put xor_byte as a variable thinking on SHAKE, CSHAKE and KMAC, which all use different xor_byte. Another approach can be a lookup table based on id.

* The structure is used SHA-3 checksum calculations.
*/
typedef struct mbedtls_sha3_context {
uint64_t state[25];
Copy link
Contributor

Choose a reason for hiding this comment

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

Everything in this struct should use MBEDTLS_PRIVATE

Copy link
Contributor

@daverodgman daverodgman left a comment

Choose a reason for hiding this comment

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

Mostly LGTM - most of my comments are pretty minor.

Testing for bad arguments, and tidying up the public interface are the main ones IMO.

I had some time to kill on the plane when I reviewed this so also did some performance optimisation. I'll push that in a separate PR as soon as this gets merged so as not to hold this one up.

@daverodgman daverodgman added needs-work historical-reviewed Reviewed & agreed to keep legacy PR/issue and removed needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review labels May 30, 2023
@daverodgman daverodgman merged commit 2d80769 into Mbed-TLS:development Jun 7, 2023
@daverodgman
Copy link
Contributor

Merged via #7708

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component-crypto Crypto primitives and low-level interfaces enhancement historical-reviewed Reviewed & agreed to keep legacy PR/issue needs-work priority-scheduled This PR is big - it will require time to be scheduled for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants