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