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:
Mon Nov 18 14:00:38 2019 +0000
Revision:
83:9f7b1df6f95a
Parent:
75:cc01992ff516
Merge pull request #264 from dgreen-arm/point-master-at-mbed-os-master

Point master branch at Mbed OS master
.
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
mbed_official 72:6973f9247848 43 mbedtls_printf("\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 75:cc01992ff516 50 static int example()
Janos Follath 0:faef56ba465f 51 {
mbed_official 72:6973f9247848 52 mbedtls_printf("\n\n");
Janos Follath 0:faef56ba465f 53
Janos Follath 0:faef56ba465f 54 /*
Janos Follath 0:faef56ba465f 55 * Method 1: use all-in-one function of a specific SHA-xxx module
Janos Follath 0:faef56ba465f 56 */
Janos Follath 0:faef56ba465f 57 unsigned char output1[32]; /* SHA-256 outputs 32 bytes */
Janos Follath 0:faef56ba465f 58
Janos Follath 0:faef56ba465f 59 /* 0 here means use the full SHA-256, not the SHA-224 variant */
Janos Follath 0:faef56ba465f 60 mbedtls_sha256(hello_buffer, hello_len, output1, 0);
Janos Follath 0:faef56ba465f 61
Janos Follath 0:faef56ba465f 62 print_hex("Method 1", output1, sizeof output1);
Janos Follath 0:faef56ba465f 63
Janos Follath 0:faef56ba465f 64
Janos Follath 0:faef56ba465f 65 /*
Janos Follath 0:faef56ba465f 66 * Method 2: use the streaming interface of a specific SHA-xxx module
Janos Follath 0:faef56ba465f 67 * This is useful if we get our input piecewise.
Janos Follath 0:faef56ba465f 68 */
Janos Follath 0:faef56ba465f 69 unsigned char output2[32];
Janos Follath 0:faef56ba465f 70 mbedtls_sha256_context ctx2;
Janos Follath 0:faef56ba465f 71
Janos Follath 0:faef56ba465f 72 mbedtls_sha256_init(&ctx2);
Janos Follath 0:faef56ba465f 73 mbedtls_sha256_starts(&ctx2, 0); /* SHA-256, not 224 */
Janos Follath 0:faef56ba465f 74
Janos Follath 0:faef56ba465f 75 /* Simulating multiple fragments */
Janos Follath 0:faef56ba465f 76 mbedtls_sha256_update(&ctx2, hello_buffer, 1);
Janos Follath 0:faef56ba465f 77 mbedtls_sha256_update(&ctx2, hello_buffer + 1, 1);
Janos Follath 0:faef56ba465f 78 mbedtls_sha256_update(&ctx2, hello_buffer + 2, hello_len - 2);
Janos Follath 0:faef56ba465f 79
Janos Follath 0:faef56ba465f 80 mbedtls_sha256_finish(&ctx2, output2);
Janos Follath 0:faef56ba465f 81 print_hex("Method 2", output2, sizeof output2);
Janos Follath 0:faef56ba465f 82
Janos Follath 0:faef56ba465f 83 /* Or you could re-use the context by doing mbedtls_sha256_starts() again */
Janos Follath 0:faef56ba465f 84 mbedtls_sha256_free(&ctx2);
Janos Follath 0:faef56ba465f 85
Janos Follath 0:faef56ba465f 86 /*
Janos Follath 0:faef56ba465f 87 * Method 3: use all-in-one function of the generice interface
Janos Follath 0:faef56ba465f 88 */
Janos Follath 0:faef56ba465f 89 unsigned char output3[MBEDTLS_MD_MAX_SIZE]; /* Enough for any hash */
Janos Follath 0:faef56ba465f 90
Janos Follath 0:faef56ba465f 91 /* Can easily pick any hash you want, by identifier */
Janos Follath 0:faef56ba465f 92 const mbedtls_md_info_t *md_info3 = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
Janos Follath 0:faef56ba465f 93
Janos Follath 0:faef56ba465f 94 if (md_info3 == NULL)
Janos Follath 0:faef56ba465f 95 {
mbed_official 72:6973f9247848 96 mbedtls_printf("SHA256 not available\n");
Janos Follath 0:faef56ba465f 97 return 1;
Janos Follath 0:faef56ba465f 98 }
Janos Follath 0:faef56ba465f 99
Janos Follath 0:faef56ba465f 100 int ret3 = mbedtls_md(md_info3, hello_buffer, hello_len, output3);
Janos Follath 0:faef56ba465f 101
Janos Follath 0:faef56ba465f 102 if (ret3 != 0)
Janos Follath 0:faef56ba465f 103 {
mbed_official 72:6973f9247848 104 mbedtls_printf("md() returned -0x%04X\n", -ret3);
Janos Follath 0:faef56ba465f 105 return 1;
Janos Follath 0:faef56ba465f 106 }
Janos Follath 0:faef56ba465f 107
Janos Follath 0:faef56ba465f 108 print_hex("Method 3", output3, mbedtls_md_get_size(md_info3));
Janos Follath 0:faef56ba465f 109
Janos Follath 0:faef56ba465f 110
Janos Follath 0:faef56ba465f 111 /*
Janos Follath 0:faef56ba465f 112 * Method 4: streaming & generic interface
Janos Follath 0:faef56ba465f 113 */
Janos Follath 0:faef56ba465f 114 unsigned char output4[MBEDTLS_MD_MAX_SIZE]; /* Enough for any hash */
Janos Follath 0:faef56ba465f 115
Janos Follath 0:faef56ba465f 116 const mbedtls_md_info_t *md_info4 = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
Janos Follath 0:faef56ba465f 117
Janos Follath 0:faef56ba465f 118 if (md_info4 == NULL)
Janos Follath 0:faef56ba465f 119 {
mbed_official 72:6973f9247848 120 mbedtls_printf("SHA256 not available\n");
Janos Follath 0:faef56ba465f 121 return 1;
Janos Follath 0:faef56ba465f 122 }
Janos Follath 0:faef56ba465f 123
Janos Follath 0:faef56ba465f 124 mbedtls_md_context_t ctx4;
Janos Follath 0:faef56ba465f 125
Janos Follath 0:faef56ba465f 126 mbedtls_md_init(&ctx4);
Janos Follath 0:faef56ba465f 127
Janos Follath 0:faef56ba465f 128 int ret4 = mbedtls_md_init_ctx(&ctx4, md_info4);
Janos Follath 0:faef56ba465f 129 if (ret4 != 0)
Janos Follath 0:faef56ba465f 130 {
mbed_official 72:6973f9247848 131 mbedtls_printf("md_init_ctx() returned -0x%04X\n", -ret4);
Janos Follath 0:faef56ba465f 132 return 1;
Janos Follath 0:faef56ba465f 133 }
Janos Follath 0:faef56ba465f 134
Janos Follath 0:faef56ba465f 135 mbedtls_md_starts(&ctx4);
Janos Follath 0:faef56ba465f 136
Janos Follath 0:faef56ba465f 137 /* Simulating multiple fragments */
Janos Follath 0:faef56ba465f 138 mbedtls_md_update(&ctx4, hello_buffer, 1);
Janos Follath 0:faef56ba465f 139 mbedtls_md_update(&ctx4, hello_buffer + 1, 1);
Janos Follath 0:faef56ba465f 140 mbedtls_md_update(&ctx4, hello_buffer + 2, hello_len - 2);
Janos Follath 0:faef56ba465f 141
Janos Follath 0:faef56ba465f 142 mbedtls_md_finish(&ctx4, output4);
Janos Follath 0:faef56ba465f 143 print_hex("Method 4", output4, mbedtls_md_get_size(md_info4));
Janos Follath 0:faef56ba465f 144
Janos Follath 0:faef56ba465f 145 /* Or you could re-use the context by doing mbedtls_md_starts() again */
Janos Follath 0:faef56ba465f 146 mbedtls_md_free(&ctx4);
Janos Follath 0:faef56ba465f 147
Janos Follath 0:faef56ba465f 148
mbed_official 72:6973f9247848 149 mbedtls_printf("\nDONE\n");
Janos Follath 0:faef56ba465f 150
Janos Follath 0:faef56ba465f 151 return 0;
Janos Follath 0:faef56ba465f 152 }
Janos Follath 0:faef56ba465f 153
Janos Follath 0:faef56ba465f 154 int main() {
mbed_official 60:4ca36ac89018 155 int exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 60:4ca36ac89018 156
mbed_official 75:cc01992ff516 157 if((exit_code = mbedtls_platform_setup(NULL)) != 0) {
mbed_official 72:6973f9247848 158 printf("Platform initialization failed with error %d\n", exit_code);
mbed_official 60:4ca36ac89018 159 return MBEDTLS_EXIT_FAILURE;
Janos Follath 0:faef56ba465f 160 }
mbed_official 60:4ca36ac89018 161
mbed_official 75:cc01992ff516 162 exit_code = example();
mbed_official 60:4ca36ac89018 163 if (exit_code != 0) {
mbed_official 72:6973f9247848 164 mbedtls_printf("Example failed with error %d\n", exit_code);
mbed_official 60:4ca36ac89018 165 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 60:4ca36ac89018 166 }
mbed_official 60:4ca36ac89018 167
mbed_official 75:cc01992ff516 168 mbedtls_platform_teardown(NULL);
mbed_official 60:4ca36ac89018 169 return exit_code;
Janos Follath 0:faef56ba465f 170 }