blob: 8eb3c1b05cd612e77a78eedcac99ba0f5160d70a [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
5 *
6 * This file is part of mbed TLS (https://tls.mbed.org)
7 */
8
9/*
10 * This program illustrates various ways of hashing a buffer.
11 * You normally need only one of these two includes.
12 */
13#include "mbedtls/sha256.h" /* SHA-256 only */
14#include "mbedtls/md.h" /* generic interface */
15
16#if defined(TARGET_LIKE_MBED)
17#include "mbed/mbed.h"
18#endif
19#include <cstdio>
20
21static void print_hex(const char *title, const unsigned char buf[], size_t len)
22{
23 printf("%s: ", title);
24
25 for (size_t i = 0; i < len; i++)
26 printf("%02x", buf[i]);
27
28 printf("\r\n");
29}
30
31static const char hello_str[] = "Hello, world!";
32static const unsigned char *hello_buffer = (const unsigned char *) hello_str;
33static const size_t hello_len = sizeof hello_str - 1;
34
35int example(void)
36{
37 printf( "\r\n\r\n" );
38
39 /*
40 * Method 1: use all-in-one function of a specific SHA-xxx module
41 */
42 unsigned char output1[32]; /* SHA-256 outputs 32 bytes */
43
44 /* 0 here means use the full SHA-256, not the SHA-224 variant */
45 mbedtls_sha256(hello_buffer, hello_len, output1, 0);
46
47 print_hex("Method 1", output1, sizeof output1);
48
49
50 /*
51 * Method 2: use the streaming interface of a specific SHA-xxx module
52 * This is useful if we get our input piecewise.
53 */
54 unsigned char output2[32];
55 mbedtls_sha256_context ctx2;
56
57 mbedtls_sha256_init(&ctx2);
58 mbedtls_sha256_starts(&ctx2, 0); /* SHA-256, not 224 */
59
60 /* Simulating multiple fragments */
61 mbedtls_sha256_update(&ctx2, hello_buffer, 1);
62 mbedtls_sha256_update(&ctx2, hello_buffer + 1, 1);
63 mbedtls_sha256_update(&ctx2, hello_buffer + 2, hello_len - 2);
64
65 mbedtls_sha256_finish(&ctx2, output2);
66 print_hex("Method 2", output2, sizeof output2);
67
68 /* Or you could re-use the context by doing mbedtls_sha256_starts() again */
69 mbedtls_sha256_free(&ctx2);
70
71 /*
72 * Method 3: use all-in-one function of the generice interface
73 */
74 unsigned char output3[MBEDTLS_MD_MAX_SIZE]; /* Enough for any hash */
75
76 /* Can easily pick any hash you want, by identifier */
77 const mbedtls_md_info_t *md_info3 = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
78
79 if (md_info3 == NULL)
80 {
81 printf("SHA256 not available\r\n");
82 return 1;
83 }
84
85 int ret3 = mbedtls_md(md_info3, hello_buffer, hello_len, output3);
86
87 if (ret3 != 0)
88 {
89 printf("md() returned -0x%04X\r\n", -ret3);
90 return 1;
91 }
92
93 print_hex("Method 3", output3, mbedtls_md_get_size(md_info3));
94
95
96 /*
97 * Method 4: streaming & generic interface
98 */
99 unsigned char output4[MBEDTLS_MD_MAX_SIZE]; /* Enough for any hash */
100
101 const mbedtls_md_info_t *md_info4 = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
102
103 if (md_info4 == NULL)
104 {
105 printf("SHA256 not available\r\n");
106 return 1;
107 }
108
109 mbedtls_md_context_t ctx4;
110
111 mbedtls_md_init(&ctx4);
112
113 int ret4 = mbedtls_md_init_ctx(&ctx4, md_info4);
114 if (ret4 != 0)
115 {
116 printf("md_init_ctx() returned -0x%04X\r\n", -ret4);
117 return 1;
118 }
119
120 mbedtls_md_starts(&ctx4);
121
122 /* Simulating multiple fragments */
123 mbedtls_md_update(&ctx4, hello_buffer, 1);
124 mbedtls_md_update(&ctx4, hello_buffer + 1, 1);
125 mbedtls_md_update(&ctx4, hello_buffer + 2, hello_len - 2);
126
127 mbedtls_md_finish(&ctx4, output4);
128 print_hex("Method 4", output4, mbedtls_md_get_size(md_info4));
129
130 /* Or you could re-use the context by doing mbedtls_md_starts() again */
131 mbedtls_md_free(&ctx4);
132
133
134 printf("\r\nDONE\r\n");
135
136 return 0;
137}
138
139#if defined(TARGET_LIKE_MBED)
140
141#include "mbed/test_env.h"
142
143int main() {
144 MBED_HOSTTEST_TIMEOUT(10);
145 MBED_HOSTTEST_SELECT(default);
146 MBED_HOSTTEST_DESCRIPTION(mbed TLS example on hashing);
147 MBED_HOSTTEST_START("MBEDTLS_EX_HASHING");
148 MBED_HOSTTEST_RESULT(example() == 0);
149}
150
151#else
152
153int main() {
154 return example();
155}
156
157#endif