Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@8:c68a6dc8d494, 2016-08-20 (annotated)
- Committer:
- mbed_official
- Date:
- Sat Aug 20 00:00:19 2016 +0100
- Revision:
- 8:c68a6dc8d494
- Parent:
- 0:faef56ba465f
- Child:
- 45:9152ca3b575e
Updating mbed-os to mbed-os-5.1.2
Commit copied from https://github.com/ARMmbed/mbed-os-example-tls
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Janos Follath |
0:faef56ba465f | 1 | /* |
| Janos Follath |
0:faef56ba465f | 2 | * Hello world example of using the hashing functions of mbed TLS |
| Janos Follath |
0:faef56ba465f | 3 | * |
| Janos Follath |
0:faef56ba465f | 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 | |
| Janos Follath |
0:faef56ba465f | 50 | static int example(void) |
| Janos Follath |
0:faef56ba465f | 51 | { |
| Janos Follath |
0:faef56ba465f | 52 | mbedtls_printf("\r\n\r\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 | { |
| Janos Follath |
0:faef56ba465f | 96 | mbedtls_printf("SHA256 not available\r\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 | { |
| Janos Follath |
0:faef56ba465f | 104 | mbedtls_printf("md() returned -0x%04X\r\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 | { |
| Janos Follath |
0:faef56ba465f | 120 | mbedtls_printf("SHA256 not available\r\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 | { |
| Janos Follath |
0:faef56ba465f | 131 | mbedtls_printf("md_init_ctx() returned -0x%04X\r\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 | |
| Janos Follath |
0:faef56ba465f | 149 | mbedtls_printf("\r\nDONE\r\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() { |
| Janos Follath |
0:faef56ba465f | 155 | int ret = example(); |
| Janos Follath |
0:faef56ba465f | 156 | if (ret != 0) { |
| Janos Follath |
0:faef56ba465f | 157 | mbedtls_printf("Example failed with error %d\r\n", ret); |
| Janos Follath |
0:faef56ba465f | 158 | } |
| Janos Follath |
0:faef56ba465f | 159 | } |