Skip to content

Fix issues in Cryptocell 310 shax_alt discovered by On Target Testing #8728

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 2 commits into from
Dec 12, 2018

Conversation

RonEld
Copy link
Contributor

@RonEld RonEld commented Nov 13, 2018

Description

Initialize the Cryptocell context in the mbedtls_shax_process()
function, in case it wasn't initialized. The Process function can be
called without calling to the starts function. Discovered by
On Target Testing on the NRF52840_DK platform.

Note: This might have conflicts with #8643 , but not dependent on it.

Pull request type

[x] Fix
[ ] Refactor
[ ] Target update
[ ] Functionality change
[ ] Docs update
[ ] Test update
[ ] Breaking change

@cmonr cmonr requested a review from a team November 13, 2018 15:11
@0xc0170
Copy link
Contributor

0xc0170 commented Nov 15, 2018

@RonEld is @ARMmbed/mbed-os-tls the team that should review this fix? If yes, please review

@RonEld
Copy link
Contributor Author

RonEld commented Nov 15, 2018

@0xc0170 I would say the @ARMmbed/mbed-os-crypto team should review this

@cmonr cmonr requested a review from a team November 22, 2018 02:22
@cmonr
Copy link
Contributor

cmonr commented Nov 22, 2018

Restarting CI

@0xc0170
Copy link
Contributor

0xc0170 commented Nov 22, 2018

Aborting CI, will start once it is approved

@mbed-ci
Copy link

mbed-ci commented Nov 22, 2018

Test run: SUCCESS

Summary: 4 of 4 test jobs passed
Build number : 3
Build artifacts
Build logs

@0xc0170
Copy link
Contributor

0xc0170 commented Nov 22, 2018

It was completed, waiting for reviewers

Copy link
Contributor

@yanesca yanesca left a comment

Choose a reason for hiding this comment

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

I don't think that this is the right way to resolve the issue. I left a comment about my thoughts in the code.

int ret = 0;
if( ctx->is_cc_initiated == 0 )
{
ret = init_cc( ctx );
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think that calling mbedtls_internal_sha1_process() should be supported for several reasons:

  1. This is an internal function, users shouldn't call it anyway.
  2. If anybody, Mbed TLS, the tests or our users call it without calling mbedtls_sha1_starts() first, then they are violating the API

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As you can see::
sha1_process_wrap calls mbedtls_internal_sha1_process(). I guess it's ok, since it's internal. The test for mbedtlos_md_process() only calls setup and process, so perhaps it's a bug in the test...

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is a bug in the test, it should call mbedtls_md_starts( ) too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

created Mbed-TLS/mbedtls#2227 to track this

@@ -33,6 +33,7 @@ extern "C" {
typedef struct
{
CRYS_HASHUserContext_t crys_hash_ctx;
int is_cc_initiated;
Copy link
Contributor

Choose a reason for hiding this comment

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

style: I think the word we are looking for here is "initialised" and not "initiated".

Copy link
Contributor Author

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 Nov 22, 2018

@yanesca Thanks for the review!
If we agreee the issue is in the test code, and not in the driver, then I guess we can close this PR

@yanesca
Copy link
Contributor

yanesca commented Nov 22, 2018

I think we agree, but I think that the style changes in this PR are still valuable. Maybe we could still merge those or just cherry-pick them to the top of one of the other PRs.

Also, before doing anything we should raise an issue for the bug in the tests. Which repository are they in?

@RonEld
Copy link
Contributor Author

RonEld commented Nov 22, 2018

The tests are from the Mbed TLS repository
I can change do the style changes early next week

@yanesca
Copy link
Contributor

yanesca commented Nov 22, 2018

Can you point me to the failing test and/or raise an issue about this in the Mbed TLS repository?

@RonEld
Copy link
Contributor Author

RonEld commented Nov 22, 2018

@yanesca mbedtls_md_process() is the failing test.
However, after thinking some more, I think we still need to have this initiated mechanism.
The TLS code, for example here clones an already started SHA context. I think that once cloning, we should consider the context as not initiated, as the HW may have switched context, so we need to initiate again.

@RonEld
Copy link
Contributor Author

RonEld commented Nov 28, 2018

@yanesca As discussed offline, I have changed the implementation of the internal process functions, to return the unsupported error.
Once reviewed and approved, I will rebase to resolve the conflicts

@RonEld
Copy link
Contributor Author

RonEld commented Nov 28, 2018

I have taken Andres' stress test from #5079 and modified to test cloning:

int sha256()
{
    int ret, next;
    size_t i, j;
    mbedtls_sha256_context *ctx;
    mbedtls_sha256_context *cloned_ctx;
    unsigned char output[32];
    size_t proc_len[SHA256_CTX_COUNT];
    size_t next_len;

    if( ( ctx = (mbedtls_sha256_context *)mbedtls_calloc( SHA256_CTX_COUNT,
                                    sizeof( mbedtls_sha256_context ) ) ) == NULL )
    {
        mbedtls_printf( "  !  mbedtls_calloc() return NULL\r\n" );
        return( -1 );
    }
    
    if( ( cloned_ctx = (mbedtls_sha256_context *)mbedtls_calloc( SHA256_CTX_COUNT,
                                    sizeof( mbedtls_sha256_context ) ) ) == NULL )
    {
        mbedtls_printf( "  !  mbedtls_calloc() return NULL\r\n" );
        return( -1 );
    }

    /* Initialise all context structures */
    for( i = 0; i < SHA256_CTX_COUNT; i++ )
    {
        mbedtls_sha256_init( &ctx[i] );
        mbedtls_sha256_init( &cloned_ctx[i] );
    }

    for( j = 1; j < REPETITIONS; j++ )
    {
        int cloned[SHA256_CTX_COUNT] = {0};
        /* Start the contexts */
        for( i = 0; i < SHA256_CTX_COUNT; i++ )
            mbedtls_sha256_starts( &ctx[i], 0 );

        memset( proc_len, 0, sizeof( proc_len ) );

        do
        {
            next = 0;

            for( i = 0; i < SHA256_CTX_COUNT; i++ )
            {
                /* Nothing left to do */
                if( proc_len[i] == sha256_ilens[i] )
                    continue;

                next = 1;

                next_len = sha256_ilens[i] / SPLIT + 1;
                next_len = ( next_len + proc_len[i] <= sha256_ilens[i] ) ? next_len :
                            sha256_ilens[i] - proc_len[i];
                if( proc_len[i] >= sha256_ilens[i] / 2 && !cloned[i] )
                {
                    mbedtls_sha256_clone( &cloned_ctx[i], &ctx[i] );
                    cloned[i] = 1;
                }
                mbedtls_sha256_update( ( cloned[i])? &cloned_ctx[i]:&ctx[i], sha256_inputs[i] + proc_len[i],
                                    next_len );
                    
                proc_len[i] += next_len;
            }
        }
        while( next == 1 );

        /* Verify the outputs */
        for( i = 0; i < SHA256_CTX_COUNT; i++ )
        {
            mbedtls_sha256_finish( cloned[i]? &cloned_ctx[i]:&ctx[i], output );

            if( memcmp( output, sha256_results[i], sizeof( output ) ) != 0 )
            {
                mbedtls_printf( "%d:  !  memcmp() result is not 0 at "
                                " repetition %u:%u\r\n",
                                __LINE__, j, i );
                ret = -2;
                goto exit;
            }
        }
    }

    ret = 0;

exit:
    for( i = 0; i < SHA256_CTX_COUNT; i++ )
        mbedtls_sha256_free( &ctx[i] );

    mbedtls_free( ctx );

    return( ret );
}

This test passes with the original implementation. So, I will remove the original commit, keeping the style fix, and latest commit for not supporting the internal process function

Ron Eldor added 2 commits November 28, 2018 15:31
1. Change camelcase variables to Mbed OS style.
2. Remove functions declarations from the `_alt` header, since they
are now added from the module header regardless whether an alternative
implementation exists.
3. Remove the `extern "c"` declaration from the `_alt` headers.
4. Remove redundant extra lines.
Return the `MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED` for the
internal process function, as it is not public.
@RonEld RonEld force-pushed the cryptocell_sha_alt_fixes branch from b04800b to bcbda3e Compare November 28, 2018 13:38
@RonEld
Copy link
Contributor Author

RonEld commented Nov 28, 2018

@yanesca As mentioned in previous comment, I have rebased to resolved conflicts, and removed the first commit that is not needed.
A reference branch from before the rebase is in https://github.com/RonEld/mbed-os/tree/cryptocell_sha_alt_fixes_before_rebase

@cmonr
Copy link
Contributor

cmonr commented Nov 29, 2018

@yanesca Please re-review.
@ARMmbed/mbed-os-tls Please review.

@yanesca
Copy link
Contributor

yanesca commented Nov 30, 2018

Looks good to me! I will approve formally as soon as @ARMmbed/mbed-os-tls has approved it too.

@RonEld
Copy link
Contributor Author

RonEld commented Dec 9, 2018

@yanesca I don't think this should be reviewed by @ARMmbed/mbed-os-tls but by @ARMmbed/mbed-os-crypto as this is a crypto bug fix

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.

Looks good to me.

@RonEld
Copy link
Contributor Author

RonEld commented Dec 11, 2018

@cmonr @0xc0170 Could you start CI please?

@cmonr
Copy link
Contributor

cmonr commented Dec 11, 2018

Will start CI when able.

We're wrapping up RC3 PRs right now.

@cmonr
Copy link
Contributor

cmonr commented Dec 12, 2018

CI started

@mbed-ci
Copy link

mbed-ci commented Dec 12, 2018

Test run: SUCCESS

Summary: 11 of 11 test jobs passed
Build number : 4
Build artifacts

@0xc0170 0xc0170 merged commit 985afb8 into ARMmbed:master Dec 12, 2018
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.

6 participants