md_hmac_demo.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * MD API multi-part HMAC demonstration.
  3. *
  4. * This programs computes the HMAC of two messages using the multi-part API.
  5. *
  6. * This is a companion to psa/hmac_demo.c, doing the same operations with the
  7. * legacy MD API. The goal is that comparing the two programs will help people
  8. * migrating to the PSA Crypto API.
  9. *
  10. * When it comes to multi-part HMAC operations, the `mbedtls_md_context`
  11. * serves a dual purpose (1) hold the key, and (2) save progress information
  12. * for the current operation. With PSA those roles are held by two disinct
  13. * objects: (1) a psa_key_id_t to hold the key, and (2) a psa_operation_t for
  14. * multi-part progress.
  15. *
  16. * This program and its companion psa/hmac_demo.c illustrate this by doing the
  17. * same sequence of multi-part HMAC computation with both APIs; looking at the
  18. * two side by side should make the differences and similarities clear.
  19. */
  20. /*
  21. * Copyright The Mbed TLS Contributors
  22. * SPDX-License-Identifier: Apache-2.0
  23. *
  24. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  25. * not use this file except in compliance with the License.
  26. * You may obtain a copy of the License at
  27. *
  28. * http://www.apache.org/licenses/LICENSE-2.0
  29. *
  30. * Unless required by applicable law or agreed to in writing, software
  31. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  32. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  33. * See the License for the specific language governing permissions and
  34. * limitations under the License.
  35. */
  36. /* First include Mbed TLS headers to get the Mbed TLS configuration and
  37. * platform definitions that we'll use in this program. Also include
  38. * standard C headers for functions we'll use here. */
  39. #include "mbedtls/build_info.h"
  40. #include "mbedtls/md.h"
  41. #include "mbedtls/platform_util.h" // for mbedtls_platform_zeroize
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. /* If the build options we need are not enabled, compile a placeholder. */
  45. #if !defined(MBEDTLS_MD_C)
  46. int main(void)
  47. {
  48. printf("MBEDTLS_MD_C not defined\r\n");
  49. return 0;
  50. }
  51. #else
  52. /* The real program starts here. */
  53. /* Dummy inputs for HMAC */
  54. const unsigned char msg1_part1[] = { 0x01, 0x02 };
  55. const unsigned char msg1_part2[] = { 0x03, 0x04 };
  56. const unsigned char msg2_part1[] = { 0x05, 0x05 };
  57. const unsigned char msg2_part2[] = { 0x06, 0x06 };
  58. /* Dummy key material - never do this in production!
  59. * This example program uses SHA-256, so a 32-byte key makes sense. */
  60. const unsigned char key_bytes[32] = { 0 };
  61. /* Print the contents of a buffer in hex */
  62. void print_buf(const char *title, unsigned char *buf, size_t len)
  63. {
  64. printf("%s:", title);
  65. for (size_t i = 0; i < len; i++) {
  66. printf(" %02x", buf[i]);
  67. }
  68. printf("\n");
  69. }
  70. /* Run an Mbed TLS function and bail out if it fails.
  71. * A string description of the error code can be recovered with:
  72. * programs/util/strerror <value> */
  73. #define CHK(expr) \
  74. do \
  75. { \
  76. ret = (expr); \
  77. if (ret != 0) \
  78. { \
  79. printf("Error %d at line %d: %s\n", \
  80. ret, \
  81. __LINE__, \
  82. #expr); \
  83. goto exit; \
  84. } \
  85. } while (0)
  86. /*
  87. * This function demonstrates computation of the HMAC of two messages using
  88. * the multipart API.
  89. */
  90. int hmac_demo(void)
  91. {
  92. int ret;
  93. const mbedtls_md_type_t alg = MBEDTLS_MD_SHA256;
  94. unsigned char out[MBEDTLS_MD_MAX_SIZE]; // safe but not optimal
  95. mbedtls_md_context_t ctx;
  96. mbedtls_md_init(&ctx);
  97. /* prepare context and load key */
  98. // the last argument to setup is 1 to enable HMAC (not just hashing)
  99. const mbedtls_md_info_t *info = mbedtls_md_info_from_type(alg);
  100. CHK(mbedtls_md_setup(&ctx, info, 1));
  101. CHK(mbedtls_md_hmac_starts(&ctx, key_bytes, sizeof(key_bytes)));
  102. /* compute HMAC(key, msg1_part1 | msg1_part2) */
  103. CHK(mbedtls_md_hmac_update(&ctx, msg1_part1, sizeof(msg1_part1)));
  104. CHK(mbedtls_md_hmac_update(&ctx, msg1_part2, sizeof(msg1_part2)));
  105. CHK(mbedtls_md_hmac_finish(&ctx, out));
  106. print_buf("msg1", out, mbedtls_md_get_size(info));
  107. /* compute HMAC(key, msg2_part1 | msg2_part2) */
  108. CHK(mbedtls_md_hmac_reset(&ctx)); // prepare for new operation
  109. CHK(mbedtls_md_hmac_update(&ctx, msg2_part1, sizeof(msg2_part1)));
  110. CHK(mbedtls_md_hmac_update(&ctx, msg2_part2, sizeof(msg2_part2)));
  111. CHK(mbedtls_md_hmac_finish(&ctx, out));
  112. print_buf("msg2", out, mbedtls_md_get_size(info));
  113. exit:
  114. mbedtls_md_free(&ctx);
  115. mbedtls_platform_zeroize(out, sizeof(out));
  116. return ret;
  117. }
  118. int main(void)
  119. {
  120. int ret;
  121. CHK(hmac_demo());
  122. exit:
  123. return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
  124. }
  125. #endif