main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Copyright (c) 2018 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup nrf_crypto_example_hmac main.c
  43. * @{
  44. * @ingroup nrf_crypto_example
  45. *
  46. * @brief Hash-based message authentication (HMAC) Example Application main file.
  47. *
  48. * @details This file contains the source code for a sample application that demonstrates using the
  49. * @lib_crypto_hmac library to do HMAC calculations. Different backends can be used by adjusting
  50. * @ref sdk_config accordingly.
  51. *
  52. */
  53. #include <stdint.h>
  54. #include "app_error.h"
  55. #include "nrf_log.h"
  56. #include "nrf_log_ctrl.h"
  57. #include "nrf_log_default_backends.h"
  58. #include "nrf_crypto.h"
  59. static uint8_t m_data[] = {0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65};
  60. static uint8_t m_key[] = {0x6b, 0x65, 0x79};
  61. static uint8_t m_digest[NRF_CRYPTO_HASH_SIZE_SHA256] = {0};
  62. static nrf_crypto_hmac_context_t m_context;
  63. static uint8_t m_expected_digest[NRF_CRYPTO_HASH_SIZE_SHA256] =
  64. {
  65. 0x6e, 0x9e, 0xf2, 0x9b, 0x75, 0xff, 0xfc, 0x5b, 0x7a, 0xba, 0xe5, 0x27, 0xd5, 0x8f, 0xda, 0xdb,
  66. 0x2f, 0xe4, 0x2e, 0x72, 0x19, 0x01, 0x19, 0x76, 0x91, 0x73, 0x43, 0x06, 0x5f, 0x58, 0xed, 0x4a
  67. };
  68. static void log_init(void)
  69. {
  70. ret_code_t err_code = NRF_LOG_INIT(NULL);
  71. APP_ERROR_CHECK(err_code);
  72. NRF_LOG_DEFAULT_BACKENDS_INIT();
  73. }
  74. int main(void)
  75. {
  76. uint32_t err_code = NRF_SUCCESS;
  77. size_t digest_len = sizeof(m_digest);
  78. log_init();
  79. err_code = nrf_crypto_init();
  80. APP_ERROR_CHECK(err_code);
  81. NRF_LOG_INFO("HMAC example started.");
  82. // Initialize frontend (which also initializes backend).
  83. err_code = nrf_crypto_hmac_init(&m_context,
  84. &g_nrf_crypto_hmac_sha256_info,
  85. m_key,
  86. sizeof(m_key));
  87. APP_ERROR_CHECK(err_code);
  88. // Push all data in one go (could be done repeatedly)
  89. err_code = nrf_crypto_hmac_update(&m_context, m_data, sizeof(m_data));
  90. APP_ERROR_CHECK(err_code);
  91. // Finish calculation
  92. err_code = nrf_crypto_hmac_finalize(&m_context, m_digest, &digest_len);
  93. APP_ERROR_CHECK(err_code);
  94. // Print digest (result).
  95. NRF_LOG_INFO("Calculated HMAC (length %u:)", digest_len)
  96. NRF_LOG_HEXDUMP_INFO(m_digest, digest_len);
  97. // Compare calculated digest with the expected digest.
  98. if (memcmp(m_digest, m_expected_digest, digest_len) == 0)
  99. {
  100. NRF_LOG_INFO("HMAC example executed successfully.");
  101. }
  102. else
  103. {
  104. NRF_LOG_ERROR("HMAC example failed!!!");
  105. while(1);
  106. }
  107. for (;;)
  108. {
  109. }
  110. }
  111. /**
  112. *@}
  113. **/