Skip to content

Refactor authcrypt example #95

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 6 commits into from
Jul 14, 2017
Merged

Conversation

andresag01
Copy link

The example is refactored to use C++ classes and allocate the bulk of
the mbed TLS context structures in the heap instead of stack.

This change should fix #91.

Copy link
Contributor

@RonEld RonEld left a comment

Choose a reason for hiding this comment

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

Since it seems that all you did is move the code to C++ style, and put the object in the heap, I have more of C++ comments, in addition to what I already wrote.
Since this is a C++ Class now, I would split the code to authcrypt.h , which defines the class, authcrypt.cpp which implements the class (except specific cases where you want iniline implementation) , and main.cpp which allocates and uses the object.


class Authcrypt {
public:
Authcrypt() : ciphertext(), decrypted()
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the default constructors are being called, no real need to implicitly call them.
I would add in the CTOR body also:
memset(ciphertext,0,sizeof(ciphertext))
memset(decrypted,0,sizeof(decrypted))

if (ret != 0) {
mbedtls_printf("mbedtls_cipher_setup() returned -0x%04X\r\n", -ret);
return 1;
mbedtls_entropy_free(&entropy);
Copy link
Contributor

Choose a reason for hiding this comment

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

in general practice, it is better to deallocate\terminate resources in opposite sequence as theyu were initiated.
I would change the sequence to:
mbedtls_cipher_free
mbedtls_ctr_drbg_free
mbedtls_entropy_free

int ret = example();
if (ret != 0) {
int ret;
Authcrypt *authcrypt = new Authcrypt();
Copy link
Contributor

Choose a reason for hiding this comment

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

check if the heap is contigous. If not, the ciphertext and decrypted will not be properly read by HW acceleration engines

@andresag01
Copy link
Author

@RonEld: I have fixed some of the issues you pointed out. However, I do not think that we should split the source file into .h and .c as this is a fairly simple class.

@RonEld
Copy link
Contributor

RonEld commented Jun 14, 2017

@andresag01 thanks for addressing my comments
I think that although it is a simple class, it is not so readable, now that the implementation and definition are together.
In addition, without the split, there is no data abstraction and encapsulation here

@mazimkhan
Copy link

On splitting the code I agree with Ron. When we look at C++ code we want to see two things

  • How the class is defined i.e. its data, methods, permissions etc.
  • Method implementation
    With everything together it is hard to view the class itself.

if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) {
mbedtls_printf("Something bad is happening! Data is not "
"authentic!\r\n");
return 1;

Choose a reason for hiding this comment

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

Why are we changing return from ret to 0? At https://github.com/ARMmbed/mbed-os-example-tls/pull/95/files#diff-64da713f1246019fb29c1e604285e6e0R211 the return code is printed again and will always have 1.
Applies to all places returning 1.

Copy link
Author

Choose a reason for hiding this comment

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

Ahh thanks for spotting that. This is because I did not modify the original code, just changed the memory allocation. I will change this as well.

/* Message that should be protected */
static const char message[] = "Some things are better left unread";
/* Metadata transmitted in the clear but authenticated */
static const char metadata[] = "eg sequence number, routing info";

Choose a reason for hiding this comment

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

Any particular reason for leaving message, metadata and secret_key outside the class. If this data belongs to this class then it should be encapsulated inside this class.

int ret = example();
if (ret != 0) {
int ret;
Authcrypt *authcrypt = new Authcrypt();

Choose a reason for hiding this comment

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

new can fail to allocate the memory. We can assert or simply print error if we can run the example.

@andresag01
Copy link
Author

@mazimkhan, @RonEld: I have modified the example according to your comments. Could you please take a look again?

@mazimkhan
Copy link

Can you please also put a NULL check here b3cebca#diff-64da713f1246019fb29c1e604285e6e0R28

@andresag01
Copy link
Author

@mazimkhan: I looked into this at some point, but I do not think new returns null: http://www.cplusplus.com/reference/new/operator%20new/. I believe the normal behaviour is to throw an exception if allocation is not possible. However, exceptions are disabled in mbed OS. An alternative could be to use nothrow, but that is not enabled in mbed OS either...

Copy link
Contributor

@RonEld RonEld left a comment

Choose a reason for hiding this comment

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

I added a comment, and still needs verifying whether the heap is contigous


const char Authcrypt::message[] = "Some things are better left unread";

const char Authcrypt::metadata[] = "eg sequence number, routing info";
Copy link
Contributor

Choose a reason for hiding this comment

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

why not initialize message and metadata in the CTOR:

Authcrypt():message("Some things are better left unread"),metadata("eg sequence number, routing info")

This way, these members would remain private

Copy link
Author

Choose a reason for hiding this comment

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

These members are already private. They are under the private section of the class declaration.

Copy link
Contributor

Choose a reason for hiding this comment

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

message and metadata are static const members, in the public section

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for pointing it out that they were public, I have now made them private members.

@mazimkhan
Copy link

mazimkhan commented Jun 15, 2017

Referring to my previous comment #95 (comment)

Just check whether mbed-os throws bad alloc if new fails or returns NULL. For former we don't need to do anything.

@andresag01
Copy link
Author

@mazimkhan: I do not think there is anything we can do. If new fails we just die, check here: https://github.com/ARMmbed/mbed-os/blob/master/platform/mbed_retarget.cpp#L941

@mazimkhan
Copy link

Thanks for looking into this.

@mazimkhan
Copy link

LGTM

@andresag01
Copy link
Author

Rebase changes to run the CI with the latest mbed OS version 5.5.2

@andresag01
Copy link
Author

The change has been approved and the relevant tests pass in the CI, merging this PR.

@andresag01 andresag01 merged commit 1fe10cb into ARMmbed:master Jul 14, 2017
@sg- sg- removed the enhancement label Jul 14, 2017
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.

Authcrypt compiled with make_gcc_arm fails with runtime error
5 participants