cc310_backend_ecdsa.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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_CC310)
  43. #include <string.h>
  44. #include "ssi_pal_types.h"
  45. #include "ssi_pal_mem.h"
  46. #include "sns_silib.h"
  47. #include "crys_rnd.h"
  48. #include "crys_ecpki_ecdsa.h"
  49. #include "crys_ecpki_error.h"
  50. #include "crys_kdf_error.h"
  51. #include "crys_hash_error.h"
  52. #include "nrf_crypto_error.h"
  53. #include "nrf_crypto_ecc_shared.h"
  54. #include "nrf_crypto_ecdsa_shared.h"
  55. #include "nrf_crypto_ecdsa.h"
  56. #include "cc310_backend_ecdsa.h"
  57. #include "cc310_backend_shared.h"
  58. #include "cc310_backend_mutex.h"
  59. #define CC310_SHA1_DIGEST_SIZE (160 / 8) /**< @internal @brief Digest size of SHA-1 */
  60. #define CC310_SHA224_DIGEST_SIZE (224 / 8) /**< @internal @brief Digest size of SHA-224 */
  61. #define CC310_SHA256_DIGEST_SIZE (256 / 8) /**< @internal @brief Digest size of SHA-256 */
  62. #define CC310_SHA384_DIGEST_SIZE (384 / 8) /**< @internal @brief Digest size of SHA-384 */
  63. #define CC310_SHA512_DIGEST_SIZE (512 / 8) /**< @internal @brief Digest size of SHA-512 */
  64. /** @internal @brief Returns enum value of @ref CRYS_ECPKI_HASH_OpMode_t based on provided hash size.
  65. *
  66. * @param[in] data_size Hash size
  67. * @return Value from @ref CRYS_ECPKI_HASH_OpMode_t or CRYS_ECPKI_HASH_OpModeLast if
  68. * cannot find implemented hash with provided size.
  69. */
  70. static CRYS_ECPKI_HASH_OpMode_t hash_mode_from_size(uint32_t data_size)
  71. {
  72. CRYS_ECPKI_HASH_OpMode_t hash_mode;
  73. switch (data_size)
  74. {
  75. case CC310_SHA1_DIGEST_SIZE:
  76. hash_mode = CRYS_ECPKI_AFTER_HASH_SHA1_mode;
  77. break;
  78. case CC310_SHA224_DIGEST_SIZE:
  79. hash_mode = CRYS_ECPKI_AFTER_HASH_SHA224_mode;
  80. break;
  81. case CC310_SHA256_DIGEST_SIZE:
  82. hash_mode = CRYS_ECPKI_AFTER_HASH_SHA256_mode;
  83. break;
  84. case CC310_SHA384_DIGEST_SIZE:
  85. hash_mode = CRYS_ECPKI_AFTER_HASH_SHA384_mode;
  86. break;
  87. case CC310_SHA512_DIGEST_SIZE:
  88. hash_mode = CRYS_ECPKI_AFTER_HASH_SHA512_mode;
  89. break;
  90. default:
  91. hash_mode = CRYS_ECPKI_HASH_OpModeLast;
  92. break;
  93. }
  94. return hash_mode;
  95. }
  96. ret_code_t nrf_crypto_backend_cc310_sign(
  97. void * p_context,
  98. void const * p_private_key,
  99. uint8_t const * p_data,
  100. size_t data_size,
  101. uint8_t * p_signature)
  102. {
  103. ret_code_t result;
  104. CRYSError_t crys_error;
  105. uint32_t signature_size;
  106. CRYS_ECPKI_HASH_OpMode_t hash_mode = hash_mode_from_size(data_size);
  107. bool mutex_locked;
  108. nrf_crypto_backend_cc310_sign_context_t * p_ctx =
  109. (nrf_crypto_backend_cc310_sign_context_t *)p_context;
  110. nrf_crypto_backend_cc310_ecc_private_key_t * p_prv =
  111. (nrf_crypto_backend_cc310_ecc_private_key_t *)p_private_key;
  112. nrf_crypto_ecc_curve_info_t const * p_info = p_prv->header.p_info;
  113. if (hash_mode == CRYS_ECPKI_HASH_OpModeLast)
  114. {
  115. return NRF_ERROR_CRYPTO_INPUT_LENGTH;
  116. }
  117. signature_size = p_info->raw_public_key_size;
  118. mutex_locked = cc310_backend_mutex_trylock();
  119. VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY);
  120. crys_error = CRYS_ECDSA_Sign(p_context,
  121. nrf_crypto_backend_cc310_rng,
  122. &p_ctx->user_context,
  123. &p_prv->private_key,
  124. hash_mode,
  125. (uint8_t *)p_data,
  126. data_size,
  127. p_signature,
  128. &signature_size);
  129. cc310_backend_mutex_unlock();
  130. result = nrf_crypto_backend_cc310_ecc_error_convert(crys_error);
  131. if (result == NRF_SUCCESS && signature_size != p_info->raw_public_key_size)
  132. {
  133. return NRF_ERROR_CRYPTO_INTERNAL;
  134. }
  135. return result;
  136. }
  137. ret_code_t nrf_crypto_backend_cc310_verify(
  138. void * p_context,
  139. void const * p_public_key,
  140. uint8_t const * p_data,
  141. size_t data_size,
  142. uint8_t const * p_signature)
  143. {
  144. ret_code_t result;
  145. CRYSError_t crys_error;
  146. CRYS_ECPKI_HASH_OpMode_t hash_mode = hash_mode_from_size(data_size);
  147. bool mutex_locked;
  148. nrf_crypto_backend_cc310_verify_context_t * p_ctx =
  149. (nrf_crypto_backend_cc310_verify_context_t *)p_context;
  150. nrf_crypto_backend_cc310_ecc_public_key_t * p_pub =
  151. (nrf_crypto_backend_cc310_ecc_public_key_t *)p_public_key;
  152. nrf_crypto_ecc_curve_info_t const * p_info = p_pub->header.p_info;
  153. result = nrf_crypto_backend_cc310_ecc_public_key_convert(p_pub, &p_ctx->key_build_temp_data);
  154. if (result != NRF_SUCCESS)
  155. {
  156. return result;
  157. }
  158. if (hash_mode == CRYS_ECPKI_HASH_OpModeLast)
  159. {
  160. return NRF_ERROR_CRYPTO_INPUT_LENGTH;
  161. }
  162. mutex_locked = cc310_backend_mutex_trylock();
  163. VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY);
  164. crys_error = CRYS_ECDSA_Verify(&p_ctx->user_context,
  165. &p_pub->key.cc310_public_key,
  166. hash_mode,
  167. (uint8_t *)p_signature,
  168. p_info->raw_public_key_size,
  169. (uint8_t *)p_data,
  170. data_size);
  171. cc310_backend_mutex_unlock();
  172. result = nrf_crypto_backend_cc310_ecc_error_convert(crys_error);
  173. return result;
  174. }
  175. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310)