main.cpp 4.8 KB

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