mbedtls_backend_ecdsa.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Copyright (c) 2018 - 2020, 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 "sdk_config.h"
  41. #include "nordic_common.h"
  42. #if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
  43. #include <stdint.h>
  44. #include <stdbool.h>
  45. #include <string.h>
  46. #include "nrf_crypto_ecc.h"
  47. #include "nrf_crypto_ecdsa.h"
  48. /*lint -save -e????*/
  49. #if !defined(MBEDTLS_CONFIG_FILE)
  50. #include "mbedtls/config.h"
  51. #else
  52. #include MBEDTLS_CONFIG_FILE
  53. #endif
  54. #include "mbedtls/ecp.h"
  55. #include "mbedtls/ecdsa.h"
  56. #include "mbedtls/sha256.h"
  57. #include "mbedtls/sha512.h"
  58. /*lint -restore*/
  59. ret_code_t nrf_crypto_backend_mbedtls_sign(
  60. void * p_context,
  61. void const * p_private_key,
  62. uint8_t const * p_data,
  63. size_t data_size,
  64. uint8_t * p_signature)
  65. {
  66. int result;
  67. mbedtls_mpi r_mpi;
  68. mbedtls_mpi s_mpi;
  69. mbedtls_ecp_group group;
  70. nrf_crypto_backend_mbedtls_ecc_private_key_t const * p_prv =
  71. (nrf_crypto_backend_mbedtls_ecc_private_key_t const *)p_private_key;
  72. nrf_crypto_ecc_curve_info_t const * p_info = p_prv->header.p_info;
  73. if (!nrf_crypto_backend_mbedtls_ecc_group_load(&group, p_info))
  74. {
  75. return NRF_ERROR_CRYPTO_INTERNAL;
  76. }
  77. mbedtls_mpi_init(&r_mpi);
  78. mbedtls_mpi_init(&s_mpi);
  79. result = mbedtls_ecdsa_sign(&group,
  80. &r_mpi,
  81. &s_mpi,
  82. &p_prv->key,
  83. p_data,
  84. data_size,
  85. nrf_crypto_backend_mbedtls_ecc_mbedtls_rng,
  86. NULL);
  87. mbedtls_ecp_group_free(&group);
  88. if (result == 0)
  89. {
  90. result = mbedtls_mpi_write_binary(&r_mpi, p_signature, p_info->raw_private_key_size);
  91. if (result == 0)
  92. {
  93. result = mbedtls_mpi_write_binary(&s_mpi,
  94. &p_signature[p_info->raw_private_key_size],
  95. p_info->raw_private_key_size);
  96. }
  97. }
  98. mbedtls_mpi_free(&r_mpi);
  99. mbedtls_mpi_free(&s_mpi);
  100. if (result != 0)
  101. {
  102. return NRF_ERROR_CRYPTO_INTERNAL;
  103. }
  104. return NRF_SUCCESS;
  105. }
  106. ret_code_t nrf_crypto_backend_mbedtls_verify(
  107. void * p_context,
  108. void const * p_public_key,
  109. uint8_t const * p_data,
  110. size_t data_size,
  111. uint8_t const * p_signature)
  112. {
  113. int result;
  114. mbedtls_mpi r_mpi;
  115. mbedtls_mpi s_mpi;
  116. mbedtls_ecp_group group;
  117. nrf_crypto_backend_mbedtls_ecc_public_key_t const * p_pub =
  118. (nrf_crypto_backend_mbedtls_ecc_public_key_t const *)p_public_key;
  119. nrf_crypto_ecc_curve_info_t const * p_info = p_pub->header.p_info;
  120. if (!nrf_crypto_backend_mbedtls_ecc_group_load(&group, p_info))
  121. {
  122. return NRF_ERROR_CRYPTO_INTERNAL;
  123. }
  124. mbedtls_mpi_init(&r_mpi);
  125. mbedtls_mpi_init(&s_mpi);
  126. result = mbedtls_mpi_read_binary(&r_mpi, p_signature, p_info->raw_private_key_size);
  127. if (result == 0)
  128. {
  129. result = mbedtls_mpi_read_binary(&s_mpi,
  130. &p_signature[p_info->raw_private_key_size],
  131. p_info->raw_private_key_size);
  132. if (result == 0)
  133. {
  134. result = mbedtls_ecdsa_verify(&group, p_data, data_size, &p_pub->key, &r_mpi, &s_mpi);
  135. }
  136. }
  137. mbedtls_ecp_group_free(&group);
  138. mbedtls_mpi_free(&r_mpi);
  139. mbedtls_mpi_free(&s_mpi);
  140. if (result == MBEDTLS_ERR_ECP_VERIFY_FAILED)
  141. {
  142. return NRF_ERROR_CRYPTO_ECDSA_INVALID_SIGNATURE;
  143. }
  144. else if (result != 0)
  145. {
  146. return NRF_ERROR_CRYPTO_INTERNAL;
  147. }
  148. return NRF_SUCCESS;
  149. }
  150. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)