main.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /**
  41. * @defgroup nrf_crypto_eddsa_example
  42. * @{
  43. * @ingroup nrf_crypto_eddsa
  44. * @brief EdDSA Example Application main file.
  45. *
  46. * This file contains the source code for a sample application that demonstrates the usage of the
  47. * nrf_crypto library to generate and verify an EdDSA signature. Different backends can be
  48. * used by adjusting @ref sdk_config.h.
  49. *
  50. */
  51. #include <stdbool.h>
  52. #include <stdint.h>
  53. #include <string.h>
  54. #include "nrf_assert.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_message[] = "Authentic message";
  60. const size_t m_message_len = sizeof(m_message) - 1; // Skipping null termination
  61. static nrf_crypto_ecc_key_pair_generate_context_t m_key_pair_generate_context;
  62. static nrf_crypto_eddsa_sign_context_t m_sign_context;
  63. static nrf_crypto_eddsa_verify_context_t m_verify_context;
  64. /** @brief Function for initializing the nrf log module.
  65. */
  66. static void log_init(void)
  67. {
  68. ret_code_t err_code = NRF_LOG_INIT(NULL);
  69. APP_ERROR_CHECK(err_code);
  70. NRF_LOG_DEFAULT_BACKENDS_INIT();
  71. }
  72. /** @brief Function for the application main entry.
  73. */
  74. int main(void)
  75. {
  76. static nrf_crypto_ecc_private_key_t priv_key;
  77. static nrf_crypto_ecc_public_key_t pub_key;
  78. static uint8_t raw_priv_key[NRF_CRYPTO_ECC_ED25519_RAW_PRIVATE_KEY_SIZE];
  79. static uint8_t raw_pub_key[NRF_CRYPTO_ECC_ED25519_RAW_PUBLIC_KEY_SIZE];
  80. static uint8_t signature[NRF_CRYPTO_EDDSA_ED25519_SIGNATURE_SIZE];
  81. size_t signature_size = sizeof(signature);
  82. size_t raw_priv_key_size = sizeof(raw_priv_key);
  83. size_t raw_pub_key_size = sizeof(raw_pub_key);
  84. ret_code_t err_code;
  85. log_init();
  86. NRF_LOG_INFO("EdDSA example started.");
  87. err_code = nrf_crypto_init();
  88. APP_ERROR_CHECK(err_code);
  89. NRF_LOG_INFO("Message:")
  90. NRF_LOG_HEXDUMP_INFO(m_message, m_message_len);
  91. // Generate key pair.
  92. err_code = nrf_crypto_ecc_key_pair_generate(&m_key_pair_generate_context,
  93. &g_nrf_crypto_ecc_ed25519_curve_info,
  94. &priv_key,
  95. &pub_key);
  96. APP_ERROR_CHECK(err_code);
  97. // Print private key.
  98. err_code = nrf_crypto_ecc_private_key_to_raw(&priv_key,
  99. raw_priv_key,
  100. &raw_priv_key_size);
  101. APP_ERROR_CHECK(err_code);
  102. NRF_LOG_INFO("Private key:")
  103. NRF_LOG_HEXDUMP_INFO(raw_priv_key, raw_priv_key_size);
  104. // Print public key.
  105. err_code = nrf_crypto_ecc_public_key_to_raw(&pub_key,
  106. raw_pub_key,
  107. &raw_pub_key_size);
  108. APP_ERROR_CHECK(err_code);
  109. NRF_LOG_INFO("Public key:")
  110. NRF_LOG_HEXDUMP_INFO(raw_pub_key, raw_pub_key_size);
  111. // Sign message.
  112. err_code = nrf_crypto_eddsa_sign(&m_sign_context,
  113. &priv_key,
  114. m_message,
  115. m_message_len,
  116. signature,
  117. &signature_size);
  118. APP_ERROR_CHECK(err_code);
  119. NRF_LOG_INFO("Signature:")
  120. NRF_LOG_HEXDUMP_INFO(signature, signature_size);
  121. // Verify message.
  122. err_code = nrf_crypto_eddsa_verify(&m_verify_context,
  123. &pub_key,
  124. m_message,
  125. m_message_len,
  126. signature,
  127. sizeof(signature));
  128. if (err_code == NRF_SUCCESS)
  129. {
  130. NRF_LOG_INFO("Signature is valid. Message is authentic.");
  131. }
  132. else if (err_code == NRF_ERROR_CRYPTO_ECDSA_INVALID_SIGNATURE)
  133. {
  134. NRF_LOG_ERROR("Signature is invalid. Message is not authentic.");
  135. while(1);
  136. }
  137. else
  138. {
  139. APP_ERROR_CHECK(err_code);
  140. }
  141. NRF_LOG_INFO("EdDSA example executed successfully.");
  142. for (;;)
  143. {
  144. }
  145. }
  146. /** @}
  147. */