-
Notifications
You must be signed in to change notification settings - Fork 51
Add platform setup and teardown support #165
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
Conversation
Although logically it is correct, I think it is better to have the calls where all the cryptography functions are, so |
@RonEld Currently the platform context is not needed or used by the tests. Even if it were, it would be an external dependency for a test case (not a part of ones setup), so I'd opt for a dependency injection using a function parameter. |
The CI fails on two known issues - memory allocation failure on one board, and lack of serial on another. |
@AndrzejKurek I agree, but if you don't have access to |
@RonEld I don't really understand. If the platform context is somehow needed in the, for example, benchmark test, it can be passed to the function handling the test (and therefore where the cryptographic calls are made, as you suggested), instead of making it a global structure. |
@AndrzejKurek I believe we are talking about the same thing. |
@RonEld And I'm saying that platform initialization is not part of the benchmark test, it is an "external" (from the benchmark test point of view) dependency, which could be satisfied using dependency injection - which would be creating it within main. This way, we can for example swap the platform context for a debug platform context, or extend it in any way we want to. This is a common pattern in, for example, unit testing, where objects or dependencies can be swapped with their respective mocks to perform the testing efficiently and at the relevant layer of abstraction. |
OK, so let's compromise on having the test constructors getting the context as parameter? (or adding a "set platform context" function?). Without the tests themselves getting access to the context, there is no much use in having this context |
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.
minor comments, in addition to the context setup location discussion
There is style inconsistency in the if
statements. Please remove white-space after parentheses for consistency
authcrypt/main.cpp
Outdated
@@ -24,6 +24,11 @@ | |||
#include "mbedtls/platform.h" | |||
|
|||
int main() { | |||
mbedtls_platform_context platform_ctx; | |||
if(mbedtls_platform_setup(&platform_ctx)) { |
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.
I prefer explicitly checking result for !=0
benchmark/main.cpp
Outdated
@@ -961,8 +961,14 @@ static int benchmark( int argc, char *argv[] ) | |||
} | |||
|
|||
int main(void) { | |||
mbedtls_platform_context platform_ctx; | |||
if(mbedtls_platform_setup(&platform_ctx)) { |
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.
I prefer explicitly checking result for !=0
tls-client/main.cpp
Outdated
@@ -412,6 +412,10 @@ class HelloHTTPS { | |||
* The main loop of the HTTPS Hello World test | |||
*/ | |||
int main() { | |||
mbedtls_platform_context platform_ctx; | |||
if( mbedtls_platform_setup(&platform_ctx)) { |
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.
I prefer explicitly checking result for !=0
hashing/main.cpp
Outdated
@@ -152,8 +152,14 @@ static int example(void) | |||
} | |||
|
|||
int main() { | |||
mbedtls_platform_context platform_ctx; | |||
if( mbedtls_platform_setup(&platform_ctx)) { |
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.
I prefer explicitly checking result for !=0
@RonEld The tests getting the context is the next step, but I'm hesitant to make this change without the tests doing anything with the context at all. Do You think that adding it, adding a (void) call in the beginning and commenting that it can be used if necessary will be OK? |
I believe you will probably get compilation warnings for unused parameter, but we can handle this. |
@RonEld I've addressed your comments, please re-review. |
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.
One minor comment added.
At the moment I am not approving nor rejecting.
I have two general comments:
- As mentioned before, please add comment about the parameter.
- Since this is
.cpp
code, the pointer is sent by value. I think it would be better to change the parameter type to const by reference (const mbedtls_platform_context& platform_ctx
)
@@ -23,6 +23,7 @@ | |||
#include "mbedtls/cipher.h" | |||
#include "mbedtls/entropy.h" | |||
#include "mbedtls/ctr_drbg.h" | |||
#include "mbedtls/platform.h" |
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.
In this case it's better to have mbedtls_platform_context*
as forward declaration , instead of adding include for the header. I prefer minimal includes in header files, unless really needed, and keep the include in the .cpp
file
Hi @RonEld,
|
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.
minor comments about the comments position.
I'll approve after this will be changerd,
authcrypt/main.cpp
Outdated
@@ -30,6 +30,8 @@ int main() { | |||
} | |||
|
|||
int exit_code = MBEDTLS_EXIT_SUCCESS; | |||
// The platform context is passed just in case any crypto calls need it. | |||
// Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more information. |
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.
Please move this to the Authcrypt
ctor, and have same comment like in the hashing
and benchmark
examples.
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.
Done.
tls-client/main.cpp
Outdated
@@ -443,6 +443,8 @@ int main() { | |||
return 1; | |||
} | |||
|
|||
// The platform context is passed just in case any crypto calls need it. | |||
// Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more information. |
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.
Please move this to the HelloHTTPS
ctor, and have same comment like in the hashing
and benchmark
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.
Done.
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.
Much better, but please change the comment to be same as in hash and benchmarking ( with the comment on the uninitialized parameter)
@RonEld - but the variable is being used, so the comment is irrelevant in the two classes that save it as a member. |
You are right, but it is being used by setting a member of the class. We should be consistent, and have this member for |
I don't understand your request. Benchmark and hash tests don't use classes, they are just functions. |
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.
Sorry, my bad. I haven't noticed that benchmark and hash weren't changed to classes, like the others
authcrypt/main.cpp
Outdated
@@ -25,16 +25,18 @@ | |||
|
|||
int main() { | |||
mbedtls_platform_context platform_ctx; | |||
if(mbedtls_platform_setup(&platform_ctx) != 0) { | |||
return -1; | |||
int exit_code = MBEDTLS_EXIT_FAILURE; |
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.
As far as I can tell, regardless of whther mbedtls_platform_setup and run return, MBEDTLS_EXIT_FAILURE will always be returned. Is this correct?
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.
No, the value will be overriden by platform_setup and authcrypt->run.
benchmark/main.cpp
Outdated
@@ -965,13 +965,19 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) | |||
|
|||
int main(void) { | |||
mbedtls_platform_context platform_ctx; | |||
if(mbedtls_platform_setup(&platform_ctx) != 0) { | |||
return -1; | |||
int exit_code = MBEDTLS_EXIT_FAILURE; |
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.
Again, I think this function will always return MBEDTLS_EXIT_FAILURE, a failure code...
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.
No, the value will be overriden by platform_setup and benchmark(...).
hashing/main.cpp
Outdated
@@ -158,13 +158,19 @@ static int example(mbedtls_platform_context* ctx) | |||
|
|||
int main() { | |||
mbedtls_platform_context platform_ctx; | |||
if(mbedtls_platform_setup(&platform_ctx) != 0) { | |||
return -1; | |||
int exit_code = MBEDTLS_EXIT_FAILURE; |
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.
I think this function will always return MBEDTLS_EXIT_FAILURE, a failure code...
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.
No, the value will be overridden by platform_setup and example(...)
tls-client/main.cpp
Outdated
@@ -417,8 +417,11 @@ class HelloHTTPS { | |||
*/ | |||
int main() { | |||
mbedtls_platform_context platform_ctx; | |||
if(mbedtls_platform_setup(&platform_ctx) != 0) { | |||
return -1; | |||
int ret = MBEDTLS_EXIT_FAILURE; |
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.
I think this function will always return MBEDTLS_EXIT_FAILURE, a failure code...
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.
Ret is only used here to store the value of platform setup and print the error. When errors occur, sure, it returns MBEDTLS_EXIT_FAILURE, but when everything goes right, MBEDTLS_EXIT_SUCCESS is returned (at the end).
Add platform setup and teardown for authcrypt, benchmark, hashing and tls-client examples.
006a991
to
c46f9b6
Compare
We've just merged a large refactoring to the examples. In the interest of getting this platform PR in sooner, I've rebased and resolved conflicts. The old branch is available at https://github.com/ARMmbed/mbed-os-example-tls/tree/feature-platform-init-old for reference, if desired. |
Add platform context as a parameter for tests to enable the usage of it in cryptographic calls
Unify error handling in tests
c46f9b6
to
4bd4bca
Compare
CI passes on most targets, but got an out of memory error on KW24D with IAR.
|
@Patater - this error also happens on master. |
@@ -41,8 +39,11 @@ const char Authcrypt::message[] = "Some things are better left unread"; | |||
|
|||
const char Authcrypt::metadata[] = "eg sequence number, routing info"; | |||
|
|||
Authcrypt::Authcrypt() | |||
Authcrypt::Authcrypt(mbedtls_platform_context* platform_ctx) |
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.
Why is it required to pass platform_ctx
to the application(test)?
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.
It is not required, but we do so as a way of showing the user how to access it. It's an approach that we have chosen with Ron after a discussion that can be followed above, on this PR.
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.
This can be limited to the tests that actually test things from the platform context.
@@ -300,7 +300,7 @@ typedef struct { | |||
rsa, dhm, ecdsa, ecdh; | |||
} todo_list; | |||
|
|||
static int benchmark( int argc, char *argv[] ) | |||
static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) |
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.
@@ -309,7 +309,10 @@ static int benchmark( int argc, char *argv[] ) | |||
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) | |||
unsigned char malloc_buf[HEAP_SIZE] = { 0 }; | |||
#endif | |||
|
|||
// The call below is used to avoid the "unused parameter" warning. | |||
// The context itself can be used by cryptographic calls which require it. |
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.
An application like this test will not directly call a cryptographic function. The cryptographic functions should have a way of accessing the context like a getter
as suggested in Mbed-TLS/mbedtls#1200
int exit_code = MBEDTLS_EXIT_FAILURE; | ||
|
||
if((exit_code = mbedtls_platform_setup(&platform_ctx)) != 0) { | ||
printf("Platform initialization failed with error %d\r\n", exit_code); |
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.
printf
-> mbedtls_printf
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_printf may be a substituted for a custom printer function that may not work if the platform setup fails.
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.
Agreed.
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.
Overall looks good. Only thing I don't like is passing platform_ctx
to test functions that don't need it. But if that is approved by design I approve this PR.
Add platform setup and teardown for authcrypt, benchmark, hashing and tls-client examples.
Tested on K64F with GCC_ARM.