blob: 60133447f4b5146ee505643183fba1c0ad7da8a3 [file] [log] [blame]
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +02001/*
2 * Hello world example of using the hashing functions of mbed TLS
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020045 */
46
47/*
48 * This program illustrates various ways of hashing a buffer.
49 * You normally need only one of these two includes.
50 */
51#include "mbedtls/sha256.h" /* SHA-256 only */
52#include "mbedtls/md.h" /* generic interface */
53
54#if defined(TARGET_LIKE_MBED)
Manuel Pégourié-Gonnard71956c92015-10-21 17:59:05 +020055#include "mbed-drivers/mbed.h"
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020056#endif
57#include <cstdio>
58
59static void print_hex(const char *title, const unsigned char buf[], size_t len)
60{
61 printf("%s: ", title);
62
63 for (size_t i = 0; i < len; i++)
64 printf("%02x", buf[i]);
65
66 printf("\r\n");
67}
68
69static const char hello_str[] = "Hello, world!";
70static const unsigned char *hello_buffer = (const unsigned char *) hello_str;
71static const size_t hello_len = sizeof hello_str - 1;
72
73int example(void)
74{
75 printf( "\r\n\r\n" );
76
77 /*
78 * Method 1: use all-in-one function of a specific SHA-xxx module
79 */
80 unsigned char output1[32]; /* SHA-256 outputs 32 bytes */
81
82 /* 0 here means use the full SHA-256, not the SHA-224 variant */
83 mbedtls_sha256(hello_buffer, hello_len, output1, 0);
84
85 print_hex("Method 1", output1, sizeof output1);
86
87
88 /*
89 * Method 2: use the streaming interface of a specific SHA-xxx module
90 * This is useful if we get our input piecewise.
91 */
92 unsigned char output2[32];
93 mbedtls_sha256_context ctx2;
94
95 mbedtls_sha256_init(&ctx2);
96 mbedtls_sha256_starts(&ctx2, 0); /* SHA-256, not 224 */
97
98 /* Simulating multiple fragments */
99 mbedtls_sha256_update(&ctx2, hello_buffer, 1);
100 mbedtls_sha256_update(&ctx2, hello_buffer + 1, 1);
101 mbedtls_sha256_update(&ctx2, hello_buffer + 2, hello_len - 2);
102
103 mbedtls_sha256_finish(&ctx2, output2);
104 print_hex("Method 2", output2, sizeof output2);
105
106 /* Or you could re-use the context by doing mbedtls_sha256_starts() again */
107 mbedtls_sha256_free(&ctx2);
108
109 /*
110 * Method 3: use all-in-one function of the generice interface
111 */
112 unsigned char output3[MBEDTLS_MD_MAX_SIZE]; /* Enough for any hash */
113
114 /* Can easily pick any hash you want, by identifier */
115 const mbedtls_md_info_t *md_info3 = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
116
117 if (md_info3 == NULL)
118 {
119 printf("SHA256 not available\r\n");
120 return 1;
121 }
122
123 int ret3 = mbedtls_md(md_info3, hello_buffer, hello_len, output3);
124
125 if (ret3 != 0)
126 {
127 printf("md() returned -0x%04X\r\n", -ret3);
128 return 1;
129 }
130
131 print_hex("Method 3", output3, mbedtls_md_get_size(md_info3));
132
133
134 /*
135 * Method 4: streaming & generic interface
136 */
137 unsigned char output4[MBEDTLS_MD_MAX_SIZE]; /* Enough for any hash */
138
139 const mbedtls_md_info_t *md_info4 = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
140
141 if (md_info4 == NULL)
142 {
143 printf("SHA256 not available\r\n");
144 return 1;
145 }
146
147 mbedtls_md_context_t ctx4;
148
149 mbedtls_md_init(&ctx4);
150
151 int ret4 = mbedtls_md_init_ctx(&ctx4, md_info4);
152 if (ret4 != 0)
153 {
154 printf("md_init_ctx() returned -0x%04X\r\n", -ret4);
155 return 1;
156 }
157
158 mbedtls_md_starts(&ctx4);
159
160 /* Simulating multiple fragments */
161 mbedtls_md_update(&ctx4, hello_buffer, 1);
162 mbedtls_md_update(&ctx4, hello_buffer + 1, 1);
163 mbedtls_md_update(&ctx4, hello_buffer + 2, hello_len - 2);
164
165 mbedtls_md_finish(&ctx4, output4);
166 print_hex("Method 4", output4, mbedtls_md_get_size(md_info4));
167
168 /* Or you could re-use the context by doing mbedtls_md_starts() again */
169 mbedtls_md_free(&ctx4);
170
171
172 printf("\r\nDONE\r\n");
173
174 return 0;
175}
176
177#if defined(TARGET_LIKE_MBED)
178
Manuel Pégourié-Gonnard71956c92015-10-21 17:59:05 +0200179#include "mbed-drivers/test_env.h"
Manuel Pégourié-Gonnarde87b04c2015-08-11 04:09:44 +0200180#include "minar/minar.h"
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200181
Manuel Pégourié-Gonnarde87b04c2015-08-11 04:09:44 +0200182static void run() {
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200183 MBED_HOSTTEST_TIMEOUT(10);
184 MBED_HOSTTEST_SELECT(default);
185 MBED_HOSTTEST_DESCRIPTION(mbed TLS example on hashing);
186 MBED_HOSTTEST_START("MBEDTLS_EX_HASHING");
187 MBED_HOSTTEST_RESULT(example() == 0);
188}
189
Manuel Pégourié-Gonnarde87b04c2015-08-11 04:09:44 +0200190void app_start(int, char*[]) {
Janos Follath60ddf162016-03-17 13:55:07 +0000191 /* Use 115200 bps for consistency with other examples */
192 get_stdio_serial().baud(115200);
Manuel Pégourié-Gonnardca4fb712015-09-18 14:36:57 +0200193 minar::Scheduler::postCallback(mbed::util::FunctionPointer0<void>(run).bind());
Manuel Pégourié-Gonnarde87b04c2015-08-11 04:09:44 +0200194}
195
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200196#else
197
198int main() {
199 return example();
200}
201
202#endif