nrf_crypto_eddsa.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #include "nrf_crypto_error.h"
  41. #include "nrf_crypto_ecc.h"
  42. #include "nrf_crypto_eddsa.h"
  43. #include "nrf_crypto_mem.h"
  44. #include "app_util.h"
  45. #include "sdk_macros.h"
  46. #if NRF_CRYPTO_ECC_ENABLED && NRF_CRYPTO_ECC_ED25519_ENABLED
  47. ret_code_t nrf_crypto_eddsa_sign(nrf_crypto_eddsa_sign_context_t * p_context,
  48. nrf_crypto_ecc_private_key_t const * p_private_key,
  49. uint8_t const * p_message,
  50. size_t message_size,
  51. uint8_t * p_signature,
  52. size_t * p_signature_size)
  53. {
  54. ret_code_t result = NRF_SUCCESS;
  55. void * p_allocated_context = NULL;
  56. // Get pointer to header
  57. nrf_crypto_internal_ecc_key_header_t const * p_private_key_header =
  58. (nrf_crypto_internal_ecc_key_header_t const *)p_private_key;
  59. // Verify parameters (zero-length message is valid per RFC 8032)
  60. if (message_size > 0)
  61. {
  62. VERIFY_TRUE(p_message != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  63. }
  64. result = nrf_crypto_internal_ecc_key_input_check(
  65. p_private_key_header,
  66. NRF_CRYPTO_INTERNAL_ECC_PRIVATE_KEY_INIT_VALUE);
  67. VERIFY_SUCCESS(result);
  68. result = nrf_crypto_internal_ecc_raw_output_prepare(p_signature,
  69. p_signature_size,
  70. NRF_CRYPTO_EDDSA_ED25519_SIGNATURE_SIZE);
  71. VERIFY_SUCCESS(result);
  72. // Allocate context if not provided
  73. if (p_context == NULL && NRF_CRYPTO_BACKEND_ED25519_SIGN_CONTEXT_SIZE > 0)
  74. {
  75. p_allocated_context = NRF_CRYPTO_ALLOC(NRF_CRYPTO_BACKEND_ED25519_SIGN_CONTEXT_SIZE);
  76. VERIFY_TRUE(p_allocated_context != NULL, NRF_ERROR_CRYPTO_ALLOC_FAILED);
  77. p_context = p_allocated_context;
  78. }
  79. // Execute backend implementation
  80. result = nrf_crypto_backend_ed25519_sign(p_context,
  81. p_private_key,
  82. p_message,
  83. message_size,
  84. p_signature);
  85. // Deallocate context if allocated
  86. if (p_allocated_context != NULL)
  87. {
  88. NRF_CRYPTO_FREE(p_allocated_context);
  89. }
  90. return result;
  91. }
  92. ret_code_t nrf_crypto_eddsa_verify(nrf_crypto_eddsa_verify_context_t * p_context,
  93. nrf_crypto_ecc_public_key_t const * p_public_key,
  94. uint8_t const * p_message,
  95. size_t message_size,
  96. uint8_t const * p_signature,
  97. size_t signature_size)
  98. {
  99. ret_code_t result = NRF_SUCCESS;
  100. void * p_allocated_context = NULL;
  101. // Get pointer to header
  102. nrf_crypto_internal_ecc_key_header_t const * p_public_key_header =
  103. (nrf_crypto_internal_ecc_key_header_t const *)p_public_key;
  104. // Verify parameters (zero-length message is valid per RFC 8032)
  105. if (message_size > 0)
  106. {
  107. VERIFY_TRUE(p_message != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  108. }
  109. result = nrf_crypto_internal_ecc_key_input_check(
  110. p_public_key_header,
  111. NRF_CRYPTO_INTERNAL_ECC_PUBLIC_KEY_INIT_VALUE);
  112. VERIFY_SUCCESS(result);
  113. result = nrf_crypto_internal_ecc_raw_input_check(p_signature,
  114. signature_size,
  115. NRF_CRYPTO_EDDSA_ED25519_SIGNATURE_SIZE);
  116. VERIFY_SUCCESS(result);
  117. // Allocate context if not provided
  118. if (p_context == NULL && NRF_CRYPTO_BACKEND_ED25519_VERIFY_CONTEXT_SIZE > 0)
  119. {
  120. p_allocated_context = NRF_CRYPTO_ALLOC(NRF_CRYPTO_BACKEND_ED25519_VERIFY_CONTEXT_SIZE);
  121. VERIFY_TRUE(p_allocated_context != NULL, NRF_ERROR_CRYPTO_ALLOC_FAILED);
  122. p_context = p_allocated_context;
  123. }
  124. // Execute backend implementation
  125. result = nrf_crypto_backend_ed25519_verify(p_context,
  126. p_public_key,
  127. p_message,
  128. message_size,
  129. p_signature);
  130. // Deallocate context if allocated
  131. if (p_allocated_context != NULL)
  132. {
  133. NRF_CRYPTO_FREE(p_allocated_context);
  134. }
  135. return result;
  136. }
  137. #endif // NRF_CRYPTO_ECC_ENABLED && NRF_CRYPTO_ECC_ED25519_ENABLED