main.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * Copyright (c) 2017 - 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_hash main.c
  43. * @{
  44. * @ingroup nrf_crypto_hash
  45. * @brief Cryptographic Hash Example Application main file.
  46. *
  47. * This file contains the source code for a sample application that demonstrates using the
  48. * nrf_crypto library to do hash calculations. Different backends can be used by adjusting
  49. * @ref sdk_config.h accordingly.
  50. *
  51. */
  52. #include <stdbool.h>
  53. #include <stdint.h>
  54. #include "boards.h"
  55. #include "nrf_assert.h"
  56. #include "nrf_log_default_backends.h"
  57. #include "nrf_log.h"
  58. #include "nrf_log_ctrl.h"
  59. #include "nrf_crypto.h"
  60. #include "nrf_crypto_hash.h"
  61. static uint8_t m_message[] = "abc";
  62. const size_t m_message_len = 3; // Skipping null termination
  63. nrf_crypto_hash_sha256_digest_t m_digest;
  64. nrf_crypto_hash_sha256_digest_t m_digest_swapped;
  65. const size_t m_digest_len = NRF_CRYPTO_HASH_SIZE_SHA256;
  66. nrf_crypto_hash_sha512_digest_t m_digest512;
  67. const size_t m_digest_len512 = NRF_CRYPTO_HASH_SIZE_SHA512;
  68. static uint8_t m_expected_digest[NRF_CRYPTO_HASH_SIZE_SHA256] =
  69. {
  70. 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
  71. 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
  72. };
  73. nrf_crypto_hash_context_t hash_context;
  74. static void print_array(const uint8_t *string, size_t len)
  75. {
  76. #if NRF_LOG_ENABLED
  77. for(size_t i = 0; i < len; i++)
  78. {
  79. NRF_LOG_RAW_INFO("%02x", string[i]);
  80. }
  81. #endif // NRF_LOG_ENABLED
  82. }
  83. #define PRINT_HEX(msg, res, len) \
  84. do \
  85. { \
  86. NRF_LOG_RAW_INFO(msg); \
  87. NRF_LOG_RAW_INFO("\r\n"); \
  88. print_array(res, len); \
  89. NRF_LOG_RAW_INFO("\r\n") \
  90. } while(0)
  91. /**
  92. * @brief Function for initializing the nrf log module.
  93. */
  94. static void log_init(void)
  95. {
  96. ret_code_t err_code = NRF_LOG_INIT(NULL);
  97. APP_ERROR_CHECK(err_code);
  98. NRF_LOG_DEFAULT_BACKENDS_INIT();
  99. }
  100. /**
  101. * @brief Function for application main entry.
  102. */
  103. int main(void)
  104. {
  105. ret_code_t err_code = NRF_SUCCESS;
  106. size_t digest_len = m_digest_len;
  107. log_init();
  108. NRF_LOG_INFO("Hash example started.\r\n");
  109. // Initialize crypto subsystem
  110. err_code = nrf_crypto_init();
  111. APP_ERROR_CHECK(err_code);
  112. NRF_LOG_INFO("Calculating SHA-256 hash with init/update/finalize");
  113. // Initialize the hash context
  114. err_code = nrf_crypto_hash_init(&hash_context, &g_nrf_crypto_hash_sha256_info);
  115. APP_ERROR_CHECK(err_code);
  116. // Run the update function (this can be run multiples of time if the data is accessible
  117. // in smaller chunks, e.g. when received on-air.
  118. err_code = nrf_crypto_hash_update(&hash_context, m_message, m_message_len);
  119. APP_ERROR_CHECK(err_code);
  120. // Run the finalize when all data has been fed to the update function.
  121. // this gives you the result
  122. err_code = nrf_crypto_hash_finalize(&hash_context, m_digest, &digest_len);
  123. APP_ERROR_CHECK(err_code);
  124. PRINT_HEX("Calculated SHA-256 digest: ", m_digest, digest_len);
  125. if(digest_len != NRF_CRYPTO_HASH_SIZE_SHA256)
  126. {
  127. NRF_LOG_ERROR("Invalid size of hash: 0x%04x, expected: %0x%04x", digest_len, NRF_CRYPTO_HASH_SIZE_SHA256);
  128. while(1);
  129. }
  130. if (memcmp(m_digest, m_expected_digest, NRF_CRYPTO_HASH_SIZE_SHA256) == 0)
  131. {
  132. NRF_LOG_INFO("SHA-256 hash calculation was correct\r\n");
  133. }
  134. else
  135. {
  136. NRF_LOG_ERROR("SHA-256 hash calculation failed\r\n");
  137. while(1);
  138. }
  139. NRF_LOG_INFO("Calculating SHA-256 hash with nrf_crypto_hash_compute (integrated version)");
  140. // Integrated version
  141. err_code = nrf_crypto_hash_calculate(&hash_context, // Context or NULL to allocate internally
  142. &g_nrf_crypto_hash_sha256_info, // Info structure configures hash mode
  143. m_message, // Input buffer
  144. m_message_len, // Input size
  145. m_digest, // Result buffer
  146. &digest_len); // Result size
  147. APP_ERROR_CHECK(err_code);
  148. PRINT_HEX("Calculated SHA-256 digest: ", m_digest, digest_len);
  149. if (memcmp(m_digest, m_expected_digest, NRF_CRYPTO_HASH_SIZE_SHA256) == 0)
  150. {
  151. NRF_LOG_INFO("Integrated SHA-256 hash calculation was correct\r\n");
  152. }
  153. else
  154. {
  155. NRF_LOG_ERROR("Integrated SHA-256 hash calculation failed\r\n");
  156. while(1);
  157. }
  158. NRF_LOG_INFO("Hash example executed successfully.");
  159. for (;;)
  160. {
  161. }
  162. }
  163. /**
  164. *@}
  165. **/