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:
Mon Oct 08 17:00:06 2018 +0100
Parent:
74:78d80b457e5e
Child:
76:f6633cd0c011
Commit message:
Merge pull request #126 from andresag01/line-endings

Change line endings from \r\n to \n only
.
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
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
--- a/authcrypt.cpp	Mon Oct 08 16:00:05 2018 +0100
+++ b/authcrypt.cpp	Mon Oct 08 17:00:06 2018 +0100
@@ -64,7 +64,7 @@
 
 int Authcrypt::run()
 {
-    mbedtls_printf("\r\n\r\n");
+    mbedtls_printf("\n\n");
     print_hex("plaintext message",
               reinterpret_cast<const unsigned char *>(message),
               sizeof(message));
@@ -76,7 +76,7 @@
     int ret = mbedtls_ctr_drbg_seed(&drbg, mbedtls_entropy_func, &entropy,
                                     secret_key, sizeof(secret_key));
     if (ret != 0) {
-        mbedtls_printf("mbedtls_ctr_drbg_seed() returned -0x%04X\r\n", -ret);
+        mbedtls_printf("mbedtls_ctr_drbg_seed() returned -0x%04X\n", -ret);
         return ret;
     }
 
@@ -84,14 +84,14 @@
     ret = mbedtls_cipher_setup(&cipher,
                     mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CCM));
     if (ret != 0) {
-        mbedtls_printf("mbedtls_cipher_setup() returned -0x%04X\r\n", -ret);
+        mbedtls_printf("mbedtls_cipher_setup() returned -0x%04X\n", -ret);
         return ret;
     }
 
     ret = mbedtls_cipher_setkey(&cipher, secret_key,
                                 8 * sizeof(secret_key), MBEDTLS_ENCRYPT);
     if (ret != 0) {
-        mbedtls_printf("mbedtls_cipher_setkey() returned -0x%04X\r\n", -ret);
+        mbedtls_printf("mbedtls_cipher_setkey() returned -0x%04X\n", -ret);
         return ret;
     }
 
@@ -126,7 +126,7 @@
                         ciphertext + nonce_len + sizeof(message),
                         tag_len);
     if (ret != 0) {
-        mbedtls_printf("mbedtls_cipher_auth_encrypt() returned -0x%04X\r\n",
+        mbedtls_printf("mbedtls_cipher_auth_encrypt() returned -0x%04X\n",
                        -ret);
         return ret;
     }
@@ -145,7 +145,7 @@
     ret = mbedtls_cipher_setkey(&cipher, secret_key, 8 * sizeof(secret_key),
                                 MBEDTLS_DECRYPT);
     if (ret != 0) {
-        mbedtls_printf("mbedtls_cipher_setkey() returned -0x%04X\r\n", -ret);
+        mbedtls_printf("mbedtls_cipher_setkey() returned -0x%04X\n", -ret);
         return ret;
     }
 
@@ -158,17 +158,17 @@
     /* Checking the return code is CRITICAL for security here */
     if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) {
         mbedtls_printf("Something bad is happening! Data is not "
-                       "authentic!\r\n");
+                       "authentic!\n");
         return ret;
     } else if (ret != 0) {
-        mbedtls_printf("mbedtls_cipher_authdecrypt() returned -0x%04X\r\n",
+        mbedtls_printf("mbedtls_cipher_authdecrypt() returned -0x%04X\n",
                        -ret);
         return ret;
     }
 
     print_hex("decrypted", decrypted, decrypted_len);
 
-    mbedtls_printf("\r\nDONE\r\n");
+    mbedtls_printf("\nDONE\n");
 
     return 0;
 }
@@ -182,5 +182,5 @@
     for (size_t i = 0; i < len; i++)
         mbedtls_printf("%02x", buf[i]);
 
-    mbedtls_printf("\r\n");
+    mbedtls_printf("\n");
 }
--- a/main.cpp	Mon Oct 08 16:00:05 2018 +0100
+++ b/main.cpp	Mon Oct 08 17:00:06 2018 +0100
@@ -28,14 +28,14 @@
     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);
+        printf("Platform initialization failed with error %d\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);
+        mbedtls_printf("Example failed with error %d\n", exit_code);
         exit_code = MBEDTLS_EXIT_FAILURE;
     }
 
--- a/mbed_app.json	Mon Oct 08 16:00:05 2018 +0100
+++ b/mbed_app.json	Mon Oct 08 17:00:06 2018 +0100
@@ -1,3 +1,8 @@
 {
-    "macros": ["MBEDTLS_USER_CONFIG_FILE=\"mbedtls_entropy_config.h\""]
+    "macros": ["MBEDTLS_USER_CONFIG_FILE=\"mbedtls_entropy_config.h\""],
+    "target_overrides": {
+        "*": {
+             "platform.stdio-convert-newlines": true
+        }
+    }
 }