blob: ec7fbf40fdc126a24cf03754bbdff2912b7be834 [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 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
45 *
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020046 * This file is part of mbed TLS (https://tls.mbed.org)
47 */
48
49/*
50 * This program illustrates various ways of hashing a buffer.
51 * You normally need only one of these two includes.
52 */
53#include "mbedtls/sha256.h" /* SHA-256 only */
54#include "mbedtls/md.h" /* generic interface */
55
56#if defined(TARGET_LIKE_MBED)
Manuel Pégourié-Gonnard71956c92015-10-21 17:59:05 +020057#include "mbed-drivers/mbed.h"
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020058#endif
59#include <cstdio>
60
61static void print_hex(const char *title, const unsigned char buf[], size_t len)
62{
63 printf("%s: ", title);
64
65 for (size_t i = 0; i < len; i++)
66 printf("%02x", buf[i]);
67
68 printf("\r\n");
69}
70
71static const char hello_str[] = "Hello, world!";
72static const unsigned char *hello_buffer = (const unsigned char *) hello_str;
73static const size_t hello_len = sizeof hello_str - 1;
74
75int example(void)
76{
77 printf( "\r\n\r\n" );
78
79 /*
80 * Method 1: use all-in-one function of a specific SHA-xxx module
81 */
82 unsigned char output1[32]; /* SHA-256 outputs 32 bytes */
83
84 /* 0 here means use the full SHA-256, not the SHA-224 variant */
85 mbedtls_sha256(hello_buffer, hello_len, output1, 0);
86
87 print_hex("Method 1", output1, sizeof output1);
88
89
90 /*
91 * Method 2: use the streaming interface of a specific SHA-xxx module
92 * This is useful if we get our input piecewise.
93 */
94 unsigned char output2[32];
95 mbedtls_sha256_context ctx2;
96
97 mbedtls_sha256_init(&ctx2);
98 mbedtls_sha256_starts(&ctx2, 0); /* SHA-256, not 224 */
99
100 /* Simulating multiple fragments */
101 mbedtls_sha256_update(&ctx2, hello_buffer, 1);
102 mbedtls_sha256_update(&ctx2, hello_buffer + 1, 1);
103 mbedtls_sha256_update(&ctx2, hello_buffer + 2, hello_len - 2);
104
105 mbedtls_sha256_finish(&ctx2, output2);
106 print_hex("Method 2", output2, sizeof output2);
107
108 /* Or you could re-use the context by doing mbedtls_sha256_starts() again */
109 mbedtls_sha256_free(&ctx2);
110
111 /*
112 * Method 3: use all-in-one function of the generice interface
113 */
114 unsigned char output3[MBEDTLS_MD_MAX_SIZE]; /* Enough for any hash */
115
116 /* Can easily pick any hash you want, by identifier */
117 const mbedtls_md_info_t *md_info3 = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
118
119 if (md_info3 == NULL)
120 {
121 printf("SHA256 not available\r\n");
122 return 1;
123 }
124
125 int ret3 = mbedtls_md(md_info3, hello_buffer, hello_len, output3);
126
127 if (ret3 != 0)
128 {
129 printf("md() returned -0x%04X\r\n", -ret3);
130 return 1;
131 }
132
133 print_hex("Method 3", output3, mbedtls_md_get_size(md_info3));
134
135
136 /*
137 * Method 4: streaming & generic interface
138 */
139 unsigned char output4[MBEDTLS_MD_MAX_SIZE]; /* Enough for any hash */
140
141 const mbedtls_md_info_t *md_info4 = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
142
143 if (md_info4 == NULL)
144 {
145 printf("SHA256 not available\r\n");
146 return 1;
147 }
148
149 mbedtls_md_context_t ctx4;
150
151 mbedtls_md_init(&ctx4);
152
153 int ret4 = mbedtls_md_init_ctx(&ctx4, md_info4);
154 if (ret4 != 0)
155 {
156 printf("md_init_ctx() returned -0x%04X\r\n", -ret4);
157 return 1;
158 }
159
160 mbedtls_md_starts(&ctx4);
161
162 /* Simulating multiple fragments */
163 mbedtls_md_update(&ctx4, hello_buffer, 1);
164 mbedtls_md_update(&ctx4, hello_buffer + 1, 1);
165 mbedtls_md_update(&ctx4, hello_buffer + 2, hello_len - 2);
166
167 mbedtls_md_finish(&ctx4, output4);
168 print_hex("Method 4", output4, mbedtls_md_get_size(md_info4));
169
170 /* Or you could re-use the context by doing mbedtls_md_starts() again */
171 mbedtls_md_free(&ctx4);
172
173
174 printf("\r\nDONE\r\n");
175
176 return 0;
177}
178
179#if defined(TARGET_LIKE_MBED)
180
Manuel Pégourié-Gonnard71956c92015-10-21 17:59:05 +0200181#include "mbed-drivers/test_env.h"
Manuel Pégourié-Gonnarde87b04c2015-08-11 04:09:44 +0200182#include "minar/minar.h"
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200183
Manuel Pégourié-Gonnarde87b04c2015-08-11 04:09:44 +0200184static void run() {
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200185 MBED_HOSTTEST_TIMEOUT(10);
186 MBED_HOSTTEST_SELECT(default);
187 MBED_HOSTTEST_DESCRIPTION(mbed TLS example on hashing);
188 MBED_HOSTTEST_START("MBEDTLS_EX_HASHING");
189 MBED_HOSTTEST_RESULT(example() == 0);
190}
191
Manuel Pégourié-Gonnarde87b04c2015-08-11 04:09:44 +0200192void app_start(int, char*[]) {
Janos Follath60ddf162016-03-17 13:55:07 +0000193 /* Use 115200 bps for consistency with other examples */
194 get_stdio_serial().baud(115200);
Manuel Pégourié-Gonnardca4fb712015-09-18 14:36:57 +0200195 minar::Scheduler::postCallback(mbed::util::FunctionPointer0<void>(run).bind());
Manuel Pégourié-Gonnarde87b04c2015-08-11 04:09:44 +0200196}
197
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200198#else
199
200int main() {
201 return example();
202}
203
204#endif