Hello world example of using the authenticated encryption with mbed TLS. The canonical source for this example lives at https://github.com/ARMmbed/mbed-os-example-tls

mbed TLS Benchmark example on mbed OS

This application performs authenticated encryption and authenticated decryption of a buffer. It serves as a tutorial for the basic authenticated encryption functions of mbed TLS.

Getting started

Building with mbed CLI

If you'd like to use mbed CLI to build this, then you should set up your environment if you have not done so already. For instructions, refer to the main readme. The instructions on this page relate to using the developer.mbed.org Online Compiler

Import the program in to the Online Compiler, select your board from the drop down in the top right hand corner and then compile the application. Once it has built, you can drag and drop the binary onto your device.

Monitoring the application

The output in the terminal window should be similar to this:

terminal output

plaintext message: 536f6d65207468696e67732061726520626574746572206c65667420756e7265616400
ciphertext: c57f7afb94f14c7977d785d08682a2596bd62ee9dcf216b8cccd997afee9b402f5de1739e8e6467aa363749ef39392e5c66622b01c7203ec0a3d14
decrypted: 536f6d65207468696e67732061726520626574746572206c65667420756e7265616400

DONE

Files at this revision

API Documentation at this revision

Comitter:
mbed_official
Date:
Thu May 10 05:15:36 2018 +0100
Parent:
62:8ee0f0dbb6a1
Child:
64:f627ee99da9b
Commit message:
Merge pull request #165 from ARMmbed/feature-platform-init

Add platform setup and teardown support
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-tls

Changed in this revision

authcrypt.cpp Show annotated file Show diff for this revision Revisions of this file
authcrypt.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
--- a/authcrypt.cpp	Wed May 02 06:30:15 2018 +0100
+++ b/authcrypt.cpp	Thu May 10 05:15:36 2018 +0100
@@ -28,8 +28,6 @@
 #include "mbedtls/debug.h"
 #endif
 
-#include "mbedtls/platform.h"
-
 #include <string.h>
 
 const unsigned char Authcrypt::secret_key[16] = {
@@ -41,8 +39,11 @@
 
 const char Authcrypt::metadata[] = "eg sequence number, routing info";
 
-Authcrypt::Authcrypt()
+Authcrypt::Authcrypt(mbedtls_platform_context* platform_ctx)
 {
+    // The platform context can be used by cryptographic calls which require it.
+    // Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more information.
+    _platform_ctx = platform_ctx;
     memset(ciphertext, 0, sizeof(ciphertext));
     memset(decrypted, 0, sizeof(decrypted));
 
--- a/authcrypt.h	Wed May 02 06:30:15 2018 +0100
+++ b/authcrypt.h	Thu May 10 05:15:36 2018 +0100
@@ -23,6 +23,7 @@
 #include "mbedtls/cipher.h"
 #include "mbedtls/entropy.h"
 #include "mbedtls/ctr_drbg.h"
+#include "mbedtls/platform.h"
 
 /**
  * This class implements the logic to demonstrate authenticated encryption using
@@ -34,7 +35,7 @@
     /**
      * Construct an Authcrypt instance
      */
-    Authcrypt();
+    Authcrypt(mbedtls_platform_context* platform_ctx);
 
     /**
      * Free any allocated resources
@@ -103,6 +104,11 @@
      * The block cipher configuration
      */
     mbedtls_cipher_context_t cipher;
+    
+    /**
+     * The platform context
+     */
+    mbedtls_platform_context* _platform_ctx;
 };
 
 #endif /* _AUTHCRYPT_H_ */
--- a/main.cpp	Wed May 02 06:30:15 2018 +0100
+++ b/main.cpp	Thu May 10 05:15:36 2018 +0100
@@ -24,15 +24,23 @@
 #include "mbedtls/platform.h"
 
 int main() {
-    int exit_code = MBEDTLS_EXIT_SUCCESS;
-    Authcrypt *authcrypt = new Authcrypt();
+    mbedtls_platform_context platform_ctx;
+    int exit_code = MBEDTLS_EXIT_FAILURE;
 
-    if (authcrypt->run() != 0) {
+    if((exit_code = mbedtls_platform_setup(&platform_ctx)) != 0) {
+        printf("Platform initialization failed with error %d\r\n", exit_code);
+        return MBEDTLS_EXIT_FAILURE;
+    }
+
+    Authcrypt *authcrypt = new Authcrypt(&platform_ctx);
+
+    if ((exit_code = authcrypt->run()) != 0) {
+        mbedtls_printf("Example failed with error %d\r\n", exit_code);
         exit_code = MBEDTLS_EXIT_FAILURE;
-        mbedtls_printf("\r\nFAIL\r\n");
     }
 
     delete authcrypt;
 
+    mbedtls_platform_teardown(&platform_ctx);
     return exit_code;
 }
--- a/mbed-os.lib	Wed May 02 06:30:15 2018 +0100
+++ b/mbed-os.lib	Thu May 10 05:15:36 2018 +0100
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#c05d72c3c005fbb7e92c3994c32bda45218ae7fe
+https://github.com/ARMmbed/mbed-os/#ae6c7c60f91c89cbf755a2f3c8ec9c66635849fd