-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Add reference counter for platform context #7099
Changes from 1 commit
ca94a49
c3b31bc
127b68f
4794389
1f5cee9
666ebe3
c1b6fdc
a2531b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* platform_alt.c | ||
* | ||
* Copyright (C) 2018, Arm Limited, All Rights Reserved | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#include "mbedtls/platform.h" | ||
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT) | ||
|
||
static int reference_count = 0; | ||
|
||
int mbedtls_platform_setup( mbedtls_platform_context *ctx ) | ||
{ | ||
int ret = 0; | ||
if( ctx == NULL ) | ||
return ( -1 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should be returning -1, however, we have failed to define in Mbed TLS any error codes for function. Maybe we should define error codes now locally in Mbed OS, and upstream them to Mbed TLS in time for the following release of Mbed OS? Possibly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, but as you said, there is currently no error codes available There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could define some locally in Mbed OS, and also submit a PR to upstream the same codes. As and when the version of Mbed TLS with the new codes becomes available, we can remove the local definitions. We control both sides (upstream and downstream) so there's no risk of reusing the same values. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created Mbed-TLS/mbedtls#1993 to add the platform error code |
||
|
||
reference_count++; | ||
|
||
if( reference_count == 1 ) | ||
{ | ||
/* call platform specific code to setup crypto driver*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a space after |
||
ret = crypto_platform_setup( &ctx->platform_impl_ctx ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes Is there anyway of avoiding this interface change? I'm not sure there is, but we should consider alternatives if we can. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree it needs documenting in the handbook. As for interface change, it's basically only a change in the existing name of the function ( |
||
} | ||
return ( ret ); | ||
} | ||
|
||
void mbedtls_platform_teardown( mbedtls_platform_context *ctx ) | ||
{ | ||
if( ctx == NULL ) | ||
return; | ||
|
||
if( reference_count == 0 ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need two Have you considered using a less than operator? eg.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree it looks a bit odd, but what you are suggesting is prone to errors:
Perhaps assigning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, it would be safer to reassign So...
|
||
return; | ||
|
||
reference_count--; | ||
|
||
if( reference_count == 0 ) | ||
{ | ||
/* call platform specific code to terminate crypto driver*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a space after |
||
crypto_platform_terminate( &ctx->platform_impl_ctx ); | ||
} | ||
} | ||
|
||
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where did this function declaration come from? Do we need it here? Where are the Doxygen comments for it? Why isn't
mbedtls_platform_teardown()
in this header as well?In the header file that documents
mbedtls_platform_setup()
andmbedtls_platform_teardown()
, we should comment as to their thread-safety. (As in, if it is safe to call the functions concurrently from multiple threads.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mbedtls_platform_init()
is a residue from previous work, it should be removedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably, but this should be done as a PR in the Mbed TLS repository
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mbedtls_platform_setup()
andmbedtls_platform_teardown()
are part of the Mbed TLS platform.h API, and described there