main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_hkdf main.c
  43. * @{
  44. * @ingroup nrf_crypto_example
  45. *
  46. * @brief HMAC based Key Derivation Function (HKDF) Example Application main file.
  47. *
  48. * @details This file contains the source code for a sample application that demonstrates using the
  49. * nrf_crypto library to do HKDF calculations. Different backends can be used by configuring the
  50. * HMAC backend in @c sdk_config.h 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. // Test data from RFC 5869 Test Case 1 (https://tools.ietf.org/html/rfc5869)
  60. // Input key material.
  61. static uint8_t m_ikm[] =
  62. {
  63. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  64. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
  65. };
  66. // Salt.
  67. static uint8_t m_salt[] =
  68. {
  69. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c
  70. };
  71. // Additional info material optionally used to increase entropy.
  72. static uint8_t m_ainfo[] =
  73. {
  74. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9
  75. };
  76. // Expected output key material (result of HKDF operation).
  77. static uint8_t m_expected_okm[] =
  78. {
  79. 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  80. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c, 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  81. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18, 0x58, 0x65
  82. };
  83. static uint8_t m_okm[sizeof(m_expected_okm)];
  84. static nrf_crypto_hmac_context_t m_context;
  85. static void log_init(void)
  86. {
  87. ret_code_t err_code = NRF_LOG_INIT(NULL);
  88. APP_ERROR_CHECK(err_code);
  89. NRF_LOG_DEFAULT_BACKENDS_INIT();
  90. }
  91. int main(void)
  92. {
  93. uint32_t err_code = NRF_SUCCESS;
  94. size_t okm_len = sizeof(m_okm);
  95. log_init();
  96. err_code = nrf_crypto_init();
  97. APP_ERROR_CHECK(err_code);
  98. NRF_LOG_INFO("HKDF example started.");
  99. err_code = nrf_crypto_hkdf_calculate(&m_context,
  100. &g_nrf_crypto_hmac_sha256_info,
  101. m_okm,
  102. &okm_len,
  103. m_ikm,
  104. sizeof(m_ikm),
  105. m_salt,
  106. sizeof(m_salt),
  107. m_ainfo,
  108. sizeof(m_ainfo),
  109. NRF_CRYPTO_HKDF_EXTRACT_AND_EXPAND);
  110. APP_ERROR_CHECK(err_code);
  111. if (memcmp(m_okm, m_expected_okm, sizeof(m_expected_okm)) == 0)
  112. {
  113. NRF_LOG_INFO("HKDF example executed successfully.");
  114. }
  115. else
  116. {
  117. NRF_LOG_ERROR("HKDF example failed!!!");
  118. }
  119. for (;;)
  120. {
  121. }
  122. }
  123. /**
  124. *@}
  125. **/