hmac_demo.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * PSA API multi-part HMAC demonstration.
  3. *
  4. * This programs computes the HMAC of two messages using the multi-part API.
  5. *
  6. * It comes with a companion program hash/md_hmac_demo.c, which does the same
  7. * operations with the legacy MD API. The goal is that comparing the two
  8. * programs will help people 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 hash/md_hmac_demo.c illustrate this by doing
  17. * the same sequence of multi-part HMAC computation with both APIs; looking at
  18. * the 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 "psa/crypto.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_PSA_CRYPTO_C) || \
  46. defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
  47. int main(void)
  48. {
  49. printf("MBEDTLS_PSA_CRYPTO_C not defined, "
  50. "and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined\r\n");
  51. return 0;
  52. }
  53. #else
  54. /* The real program starts here. */
  55. /* Dummy inputs for HMAC */
  56. const unsigned char msg1_part1[] = { 0x01, 0x02 };
  57. const unsigned char msg1_part2[] = { 0x03, 0x04 };
  58. const unsigned char msg2_part1[] = { 0x05, 0x05 };
  59. const unsigned char msg2_part2[] = { 0x06, 0x06 };
  60. /* Dummy key material - never do this in production!
  61. * This example program uses SHA-256, so a 32-byte key makes sense. */
  62. const unsigned char key_bytes[32] = { 0 };
  63. /* Print the contents of a buffer in hex */
  64. void print_buf(const char *title, uint8_t *buf, size_t len)
  65. {
  66. printf("%s:", title);
  67. for (size_t i = 0; i < len; i++) {
  68. printf(" %02x", buf[i]);
  69. }
  70. printf("\n");
  71. }
  72. /* Run a PSA function and bail out if it fails.
  73. * The symbolic name of the error code can be recovered using:
  74. * programs/psa/psa_constant_name status <value> */
  75. #define PSA_CHECK(expr) \
  76. do \
  77. { \
  78. status = (expr); \
  79. if (status != PSA_SUCCESS) \
  80. { \
  81. printf("Error %d at line %d: %s\n", \
  82. (int) status, \
  83. __LINE__, \
  84. #expr); \
  85. goto exit; \
  86. } \
  87. } \
  88. while (0)
  89. /*
  90. * This function demonstrates computation of the HMAC of two messages using
  91. * the multipart API.
  92. */
  93. psa_status_t hmac_demo(void)
  94. {
  95. psa_status_t status;
  96. const psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
  97. uint8_t out[PSA_MAC_MAX_SIZE]; // safe but not optimal
  98. /* PSA_MAC_LENGTH(PSA_KEY_TYPE_HMAC, 8 * sizeof( key_bytes ), alg)
  99. * should work but see https://github.com/Mbed-TLS/mbedtls/issues/4320 */
  100. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  101. psa_key_id_t key = 0;
  102. /* prepare key */
  103. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
  104. psa_set_key_algorithm(&attributes, alg);
  105. psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
  106. psa_set_key_bits(&attributes, 8 * sizeof(key_bytes)); // optional
  107. status = psa_import_key(&attributes,
  108. key_bytes, sizeof(key_bytes), &key);
  109. if (status != PSA_SUCCESS) {
  110. return status;
  111. }
  112. /* prepare operation */
  113. psa_mac_operation_t op = PSA_MAC_OPERATION_INIT;
  114. size_t out_len = 0;
  115. /* compute HMAC(key, msg1_part1 | msg1_part2) */
  116. PSA_CHECK(psa_mac_sign_setup(&op, key, alg));
  117. PSA_CHECK(psa_mac_update(&op, msg1_part1, sizeof(msg1_part1)));
  118. PSA_CHECK(psa_mac_update(&op, msg1_part2, sizeof(msg1_part2)));
  119. PSA_CHECK(psa_mac_sign_finish(&op, out, sizeof(out), &out_len));
  120. print_buf("msg1", out, out_len);
  121. /* compute HMAC(key, msg2_part1 | msg2_part2) */
  122. PSA_CHECK(psa_mac_sign_setup(&op, key, alg));
  123. PSA_CHECK(psa_mac_update(&op, msg2_part1, sizeof(msg2_part1)));
  124. PSA_CHECK(psa_mac_update(&op, msg2_part2, sizeof(msg2_part2)));
  125. PSA_CHECK(psa_mac_sign_finish(&op, out, sizeof(out), &out_len));
  126. print_buf("msg2", out, out_len);
  127. exit:
  128. psa_mac_abort(&op); // needed on error, harmless on success
  129. psa_destroy_key(key);
  130. mbedtls_platform_zeroize(out, sizeof(out));
  131. return status;
  132. }
  133. int main(void)
  134. {
  135. psa_status_t status = PSA_SUCCESS;
  136. /* Initialize the PSA crypto library. */
  137. PSA_CHECK(psa_crypto_init());
  138. /* Run the demo */
  139. PSA_CHECK(hmac_demo());
  140. /* Deinitialize the PSA crypto library. */
  141. mbedtls_psa_crypto_free();
  142. exit:
  143. return status == PSA_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
  144. }
  145. #endif