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

SHA-256 Hash example on mbed OS

This application performs hashing of a buffer with SHA-256 using various APIs. It serves as a tutorial for the basic hashing APIs 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

Method 1: 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3
Method 2: 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3
Method 3: 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3
Method 4: 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3

DONE
Committer:
mbed_official
Date:
Thu May 10 05:15:45 2018 +0100
Revision:
60:4ca36ac89018
Parent:
45:9152ca3b575e
Child:
72:6973f9247848
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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Janos Follath 0:faef56ba465f 1 /*
mbed_official 45:9152ca3b575e 2 * Hello world example of using the hashing functions of Mbed TLS
Janos Follath 0:faef56ba465f 3 *
mbed_official 45:9152ca3b575e 4 * Copyright (C) 2016, Arm Limited, All Rights Reserved
Janos Follath 0:faef56ba465f 5 * SPDX-License-Identifier: Apache-2.0
Janos Follath 0:faef56ba465f 6 *
Janos Follath 0:faef56ba465f 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Janos Follath 0:faef56ba465f 8 * not use this file except in compliance with the License.
Janos Follath 0:faef56ba465f 9 * You may obtain a copy of the License at
Janos Follath 0:faef56ba465f 10 *
Janos Follath 0:faef56ba465f 11 * http://www.apache.org/licenses/LICENSE-2.0
Janos Follath 0:faef56ba465f 12 *
Janos Follath 0:faef56ba465f 13 * Unless required by applicable law or agreed to in writing, software
Janos Follath 0:faef56ba465f 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Janos Follath 0:faef56ba465f 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Janos Follath 0:faef56ba465f 16 * See the License for the specific language governing permissions and
Janos Follath 0:faef56ba465f 17 * limitations under the License.
Janos Follath 0:faef56ba465f 18 */
Janos Follath 0:faef56ba465f 19
Janos Follath 0:faef56ba465f 20 /*
Janos Follath 0:faef56ba465f 21 * This program illustrates various ways of hashing a buffer.
Janos Follath 0:faef56ba465f 22 * You normally need only one of these two includes.
Janos Follath 0:faef56ba465f 23 */
Janos Follath 0:faef56ba465f 24 #include "mbed.h"
Janos Follath 0:faef56ba465f 25 #include "mbedtls/sha256.h" /* SHA-256 only */
Janos Follath 0:faef56ba465f 26 #include "mbedtls/md.h" /* generic interface */
Janos Follath 0:faef56ba465f 27
Janos Follath 0:faef56ba465f 28 #if DEBUG_LEVEL > 0
Janos Follath 0:faef56ba465f 29 #include "mbedtls/debug.h"
Janos Follath 0:faef56ba465f 30 #endif
Janos Follath 0:faef56ba465f 31
Janos Follath 0:faef56ba465f 32 #include "mbedtls/platform.h"
Janos Follath 0:faef56ba465f 33
Janos Follath 0:faef56ba465f 34 #include <string.h>
Janos Follath 0:faef56ba465f 35
Janos Follath 0:faef56ba465f 36 static void print_hex(const char *title, const unsigned char buf[], size_t len)
Janos Follath 0:faef56ba465f 37 {
Janos Follath 0:faef56ba465f 38 mbedtls_printf("%s: ", title);
Janos Follath 0:faef56ba465f 39
Janos Follath 0:faef56ba465f 40 for (size_t i = 0; i < len; i++)
Janos Follath 0:faef56ba465f 41 mbedtls_printf("%02x", buf[i]);
Janos Follath 0:faef56ba465f 42
Janos Follath 0:faef56ba465f 43 mbedtls_printf("\r\n");
Janos Follath 0:faef56ba465f 44 }
Janos Follath 0:faef56ba465f 45
Janos Follath 0:faef56ba465f 46 static const char hello_str[] = "Hello, world!";
Janos Follath 0:faef56ba465f 47 static const unsigned char *hello_buffer = (const unsigned char *) hello_str;
Janos Follath 0:faef56ba465f 48 static const size_t hello_len = strlen(hello_str);
Janos Follath 0:faef56ba465f 49
mbed_official 60:4ca36ac89018 50 static int example(mbedtls_platform_context* ctx)
Janos Follath 0:faef56ba465f 51 {
mbed_official 60:4ca36ac89018 52 // The call below is used to avoid the "unused parameter" warning.
mbed_official 60:4ca36ac89018 53 // The context itself can be used by cryptographic calls which require it.
mbed_official 60:4ca36ac89018 54 // Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more information.
mbed_official 60:4ca36ac89018 55 (void)ctx;
mbed_official 60:4ca36ac89018 56
Janos Follath 0:faef56ba465f 57 mbedtls_printf("\r\n\r\n");
Janos Follath 0:faef56ba465f 58
Janos Follath 0:faef56ba465f 59 /*
Janos Follath 0:faef56ba465f 60 * Method 1: use all-in-one function of a specific SHA-xxx module
Janos Follath 0:faef56ba465f 61 */
Janos Follath 0:faef56ba465f 62 unsigned char output1[32]; /* SHA-256 outputs 32 bytes */
Janos Follath 0:faef56ba465f 63
Janos Follath 0:faef56ba465f 64 /* 0 here means use the full SHA-256, not the SHA-224 variant */
Janos Follath 0:faef56ba465f 65 mbedtls_sha256(hello_buffer, hello_len, output1, 0);
Janos Follath 0:faef56ba465f 66
Janos Follath 0:faef56ba465f 67 print_hex("Method 1", output1, sizeof output1);
Janos Follath 0:faef56ba465f 68
Janos Follath 0:faef56ba465f 69
Janos Follath 0:faef56ba465f 70 /*
Janos Follath 0:faef56ba465f 71 * Method 2: use the streaming interface of a specific SHA-xxx module
Janos Follath 0:faef56ba465f 72 * This is useful if we get our input piecewise.
Janos Follath 0:faef56ba465f 73 */
Janos Follath 0:faef56ba465f 74 unsigned char output2[32];
Janos Follath 0:faef56ba465f 75 mbedtls_sha256_context ctx2;
Janos Follath 0:faef56ba465f 76
Janos Follath 0:faef56ba465f 77 mbedtls_sha256_init(&ctx2);
Janos Follath 0:faef56ba465f 78 mbedtls_sha256_starts(&ctx2, 0); /* SHA-256, not 224 */
Janos Follath 0:faef56ba465f 79
Janos Follath 0:faef56ba465f 80 /* Simulating multiple fragments */
Janos Follath 0:faef56ba465f 81 mbedtls_sha256_update(&ctx2, hello_buffer, 1);
Janos Follath 0:faef56ba465f 82 mbedtls_sha256_update(&ctx2, hello_buffer + 1, 1);
Janos Follath 0:faef56ba465f 83 mbedtls_sha256_update(&ctx2, hello_buffer + 2, hello_len - 2);
Janos Follath 0:faef56ba465f 84
Janos Follath 0:faef56ba465f 85 mbedtls_sha256_finish(&ctx2, output2);
Janos Follath 0:faef56ba465f 86 print_hex("Method 2", output2, sizeof output2);
Janos Follath 0:faef56ba465f 87
Janos Follath 0:faef56ba465f 88 /* Or you could re-use the context by doing mbedtls_sha256_starts() again */
Janos Follath 0:faef56ba465f 89 mbedtls_sha256_free(&ctx2);
Janos Follath 0:faef56ba465f 90
Janos Follath 0:faef56ba465f 91 /*
Janos Follath 0:faef56ba465f 92 * Method 3: use all-in-one function of the generice interface
Janos Follath 0:faef56ba465f 93 */
Janos Follath 0:faef56ba465f 94 unsigned char output3[MBEDTLS_MD_MAX_SIZE]; /* Enough for any hash */
Janos Follath 0:faef56ba465f 95
Janos Follath 0:faef56ba465f 96 /* Can easily pick any hash you want, by identifier */
Janos Follath 0:faef56ba465f 97 const mbedtls_md_info_t *md_info3 = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
Janos Follath 0:faef56ba465f 98
Janos Follath 0:faef56ba465f 99 if (md_info3 == NULL)
Janos Follath 0:faef56ba465f 100 {
Janos Follath 0:faef56ba465f 101 mbedtls_printf("SHA256 not available\r\n");
Janos Follath 0:faef56ba465f 102 return 1;
Janos Follath 0:faef56ba465f 103 }
Janos Follath 0:faef56ba465f 104
Janos Follath 0:faef56ba465f 105 int ret3 = mbedtls_md(md_info3, hello_buffer, hello_len, output3);
Janos Follath 0:faef56ba465f 106
Janos Follath 0:faef56ba465f 107 if (ret3 != 0)
Janos Follath 0:faef56ba465f 108 {
Janos Follath 0:faef56ba465f 109 mbedtls_printf("md() returned -0x%04X\r\n", -ret3);
Janos Follath 0:faef56ba465f 110 return 1;
Janos Follath 0:faef56ba465f 111 }
Janos Follath 0:faef56ba465f 112
Janos Follath 0:faef56ba465f 113 print_hex("Method 3", output3, mbedtls_md_get_size(md_info3));
Janos Follath 0:faef56ba465f 114
Janos Follath 0:faef56ba465f 115
Janos Follath 0:faef56ba465f 116 /*
Janos Follath 0:faef56ba465f 117 * Method 4: streaming & generic interface
Janos Follath 0:faef56ba465f 118 */
Janos Follath 0:faef56ba465f 119 unsigned char output4[MBEDTLS_MD_MAX_SIZE]; /* Enough for any hash */
Janos Follath 0:faef56ba465f 120
Janos Follath 0:faef56ba465f 121 const mbedtls_md_info_t *md_info4 = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
Janos Follath 0:faef56ba465f 122
Janos Follath 0:faef56ba465f 123 if (md_info4 == NULL)
Janos Follath 0:faef56ba465f 124 {
Janos Follath 0:faef56ba465f 125 mbedtls_printf("SHA256 not available\r\n");
Janos Follath 0:faef56ba465f 126 return 1;
Janos Follath 0:faef56ba465f 127 }
Janos Follath 0:faef56ba465f 128
Janos Follath 0:faef56ba465f 129 mbedtls_md_context_t ctx4;
Janos Follath 0:faef56ba465f 130
Janos Follath 0:faef56ba465f 131 mbedtls_md_init(&ctx4);
Janos Follath 0:faef56ba465f 132
Janos Follath 0:faef56ba465f 133 int ret4 = mbedtls_md_init_ctx(&ctx4, md_info4);
Janos Follath 0:faef56ba465f 134 if (ret4 != 0)
Janos Follath 0:faef56ba465f 135 {
Janos Follath 0:faef56ba465f 136 mbedtls_printf("md_init_ctx() returned -0x%04X\r\n", -ret4);
Janos Follath 0:faef56ba465f 137 return 1;
Janos Follath 0:faef56ba465f 138 }
Janos Follath 0:faef56ba465f 139
Janos Follath 0:faef56ba465f 140 mbedtls_md_starts(&ctx4);
Janos Follath 0:faef56ba465f 141
Janos Follath 0:faef56ba465f 142 /* Simulating multiple fragments */
Janos Follath 0:faef56ba465f 143 mbedtls_md_update(&ctx4, hello_buffer, 1);
Janos Follath 0:faef56ba465f 144 mbedtls_md_update(&ctx4, hello_buffer + 1, 1);
Janos Follath 0:faef56ba465f 145 mbedtls_md_update(&ctx4, hello_buffer + 2, hello_len - 2);
Janos Follath 0:faef56ba465f 146
Janos Follath 0:faef56ba465f 147 mbedtls_md_finish(&ctx4, output4);
Janos Follath 0:faef56ba465f 148 print_hex("Method 4", output4, mbedtls_md_get_size(md_info4));
Janos Follath 0:faef56ba465f 149
Janos Follath 0:faef56ba465f 150 /* Or you could re-use the context by doing mbedtls_md_starts() again */
Janos Follath 0:faef56ba465f 151 mbedtls_md_free(&ctx4);
Janos Follath 0:faef56ba465f 152
Janos Follath 0:faef56ba465f 153
Janos Follath 0:faef56ba465f 154 mbedtls_printf("\r\nDONE\r\n");
Janos Follath 0:faef56ba465f 155
Janos Follath 0:faef56ba465f 156 return 0;
Janos Follath 0:faef56ba465f 157 }
Janos Follath 0:faef56ba465f 158
Janos Follath 0:faef56ba465f 159 int main() {
mbed_official 60:4ca36ac89018 160 mbedtls_platform_context platform_ctx;
mbed_official 60:4ca36ac89018 161 int exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 60:4ca36ac89018 162
mbed_official 60:4ca36ac89018 163 if((exit_code = mbedtls_platform_setup(&platform_ctx)) != 0) {
mbed_official 60:4ca36ac89018 164 printf("Platform initialization failed with error %d\r\n", exit_code);
mbed_official 60:4ca36ac89018 165 return MBEDTLS_EXIT_FAILURE;
Janos Follath 0:faef56ba465f 166 }
mbed_official 60:4ca36ac89018 167
mbed_official 60:4ca36ac89018 168 exit_code = example(&platform_ctx);
mbed_official 60:4ca36ac89018 169 if (exit_code != 0) {
mbed_official 60:4ca36ac89018 170 mbedtls_printf("Example failed with error %d\r\n", exit_code);
mbed_official 60:4ca36ac89018 171 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 60:4ca36ac89018 172 }
mbed_official 60:4ca36ac89018 173
mbed_official 60:4ca36ac89018 174 mbedtls_platform_teardown(&platform_ctx);
mbed_official 60:4ca36ac89018 175 return exit_code;
Janos Follath 0:faef56ba465f 176 }