nrf_crypto_ecdh.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * Copyright (c) 2018, 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_mem.h"
  42. #include "nrf_crypto_ecc.h"
  43. #include "nrf_crypto_ecdh.h"
  44. #include "app_util.h"
  45. #include "sdk_macros.h"
  46. #if NRF_CRYPTO_ECC_ENABLED
  47. #if NRF_CRYPTO_ECC_IMPLEMENTED_CURVES_COUNT > 1
  48. static const nrf_crypto_backend_ecdh_compute_fn_t compute_impl[] =
  49. {
  50. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  51. nrf_crypto_backend_secp160r1_ecdh_compute,
  52. #endif
  53. #if NRF_CRYPTO_ECC_SECP160R2_ENABLED
  54. nrf_crypto_backend_secp160r2_ecdh_compute,
  55. #endif
  56. #if NRF_CRYPTO_ECC_SECP192R1_ENABLED
  57. nrf_crypto_backend_secp192r1_ecdh_compute,
  58. #endif
  59. #if NRF_CRYPTO_ECC_SECP224R1_ENABLED
  60. nrf_crypto_backend_secp224r1_ecdh_compute,
  61. #endif
  62. #if NRF_CRYPTO_ECC_SECP256R1_ENABLED
  63. nrf_crypto_backend_secp256r1_ecdh_compute,
  64. #endif
  65. #if NRF_CRYPTO_ECC_SECP384R1_ENABLED
  66. nrf_crypto_backend_secp384r1_ecdh_compute,
  67. #endif
  68. #if NRF_CRYPTO_ECC_SECP521R1_ENABLED
  69. nrf_crypto_backend_secp521r1_ecdh_compute,
  70. #endif
  71. #if NRF_CRYPTO_ECC_SECP160K1_ENABLED
  72. nrf_crypto_backend_secp160k1_ecdh_compute,
  73. #endif
  74. #if NRF_CRYPTO_ECC_SECP192K1_ENABLED
  75. nrf_crypto_backend_secp192k1_ecdh_compute,
  76. #endif
  77. #if NRF_CRYPTO_ECC_SECP224K1_ENABLED
  78. nrf_crypto_backend_secp224k1_ecdh_compute,
  79. #endif
  80. #if NRF_CRYPTO_ECC_SECP256K1_ENABLED
  81. nrf_crypto_backend_secp256k1_ecdh_compute,
  82. #endif
  83. #if NRF_CRYPTO_ECC_BP256R1_ENABLED
  84. nrf_crypto_backend_bp256r1_ecdh_compute,
  85. #endif
  86. #if NRF_CRYPTO_ECC_BP384R1_ENABLED
  87. nrf_crypto_backend_bp384r1_ecdh_compute,
  88. #endif
  89. #if NRF_CRYPTO_ECC_BP512R1_ENABLED
  90. nrf_crypto_backend_bp512r1_ecdh_compute,
  91. #endif
  92. #if NRF_CRYPTO_ECC_CURVE25519_ENABLED
  93. nrf_crypto_backend_curve25519_ecdh_compute,
  94. #endif
  95. };
  96. static const uint16_t compute_impl_context_size[] =
  97. {
  98. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  99. NRF_CRYPTO_BACKEND_SECP160R1_ECDH_CONTEXT_SIZE,
  100. #endif
  101. #if NRF_CRYPTO_ECC_SECP160R2_ENABLED
  102. NRF_CRYPTO_BACKEND_SECP160R2_ECDH_CONTEXT_SIZE,
  103. #endif
  104. #if NRF_CRYPTO_ECC_SECP192R1_ENABLED
  105. NRF_CRYPTO_BACKEND_SECP192R1_ECDH_CONTEXT_SIZE,
  106. #endif
  107. #if NRF_CRYPTO_ECC_SECP224R1_ENABLED
  108. NRF_CRYPTO_BACKEND_SECP224R1_ECDH_CONTEXT_SIZE,
  109. #endif
  110. #if NRF_CRYPTO_ECC_SECP256R1_ENABLED
  111. NRF_CRYPTO_BACKEND_SECP256R1_ECDH_CONTEXT_SIZE,
  112. #endif
  113. #if NRF_CRYPTO_ECC_SECP384R1_ENABLED
  114. NRF_CRYPTO_BACKEND_SECP384R1_ECDH_CONTEXT_SIZE,
  115. #endif
  116. #if NRF_CRYPTO_ECC_SECP521R1_ENABLED
  117. NRF_CRYPTO_BACKEND_SECP521R1_ECDH_CONTEXT_SIZE,
  118. #endif
  119. #if NRF_CRYPTO_ECC_SECP160K1_ENABLED
  120. NRF_CRYPTO_BACKEND_SECP160K1_ECDH_CONTEXT_SIZE,
  121. #endif
  122. #if NRF_CRYPTO_ECC_SECP192K1_ENABLED
  123. NRF_CRYPTO_BACKEND_SECP192K1_ECDH_CONTEXT_SIZE,
  124. #endif
  125. #if NRF_CRYPTO_ECC_SECP224K1_ENABLED
  126. NRF_CRYPTO_BACKEND_SECP224K1_ECDH_CONTEXT_SIZE,
  127. #endif
  128. #if NRF_CRYPTO_ECC_SECP256K1_ENABLED
  129. NRF_CRYPTO_BACKEND_SECP256K1_ECDH_CONTEXT_SIZE,
  130. #endif
  131. #if NRF_CRYPTO_ECC_BP256R1_ENABLED
  132. NRF_CRYPTO_BACKEND_BP256R1_ECDH_CONTEXT_SIZE,
  133. #endif
  134. #if NRF_CRYPTO_ECC_BP384R1_ENABLED
  135. NRF_CRYPTO_BACKEND_BP384R1_ECDH_CONTEXT_SIZE,
  136. #endif
  137. #if NRF_CRYPTO_ECC_BP512R1_ENABLED
  138. NRF_CRYPTO_BACKEND_BP512R1_ECDH_CONTEXT_SIZE,
  139. #endif
  140. #if NRF_CRYPTO_ECC_CURVE25519_ENABLED
  141. NRF_CRYPTO_BACKEND_CURVE25519_ECDH_CONTEXT_SIZE,
  142. #endif
  143. };
  144. #define BACKEND_IMPL_GET(table, curve_type) (table)[(uint32_t)(curve_type)]
  145. #else
  146. #if NRF_CRYPTO_ECC_SECP160R1_ENABLED
  147. #define compute_impl nrf_crypto_backend_secp160r1_ecdh_compute
  148. #define compute_impl_context_size NRF_CRYPTO_BACKEND_SECP160R1_ECDH_CONTEXT_SIZE
  149. #elif NRF_CRYPTO_ECC_SECP160R2_ENABLED
  150. #define compute_impl nrf_crypto_backend_secp160r2_ecdh_compute
  151. #define compute_impl_context_size NRF_CRYPTO_BACKEND_SECP160R2_ECDH_CONTEXT_SIZE
  152. #elif NRF_CRYPTO_ECC_SECP192R1_ENABLED
  153. #define compute_impl nrf_crypto_backend_secp192r1_ecdh_compute
  154. #define compute_impl_context_size NRF_CRYPTO_BACKEND_SECP192R1_ECDH_CONTEXT_SIZE
  155. #elif NRF_CRYPTO_ECC_SECP224R1_ENABLED
  156. #define compute_impl nrf_crypto_backend_secp224r1_ecdh_compute
  157. #define compute_impl_context_size NRF_CRYPTO_BACKEND_SECP224R1_ECDH_CONTEXT_SIZE
  158. #elif NRF_CRYPTO_ECC_SECP256R1_ENABLED
  159. #define compute_impl nrf_crypto_backend_secp256r1_ecdh_compute
  160. #define compute_impl_context_size NRF_CRYPTO_BACKEND_SECP256R1_ECDH_CONTEXT_SIZE
  161. #elif NRF_CRYPTO_ECC_SECP384R1_ENABLED
  162. #define compute_impl nrf_crypto_backend_secp384r1_ecdh_compute
  163. #define compute_impl_context_size NRF_CRYPTO_BACKEND_SECP384R1_ECDH_CONTEXT_SIZE
  164. #elif NRF_CRYPTO_ECC_SECP521R1_ENABLED
  165. #define compute_impl nrf_crypto_backend_secp521r1_ecdh_compute
  166. #define compute_impl_context_size NRF_CRYPTO_BACKEND_SECP521R1_ECDH_CONTEXT_SIZE
  167. #elif NRF_CRYPTO_ECC_SECP160K1_ENABLED
  168. #define compute_impl nrf_crypto_backend_secp160k1_ecdh_compute
  169. #define compute_impl_context_size NRF_CRYPTO_BACKEND_SECP160K1_ECDH_CONTEXT_SIZE
  170. #elif NRF_CRYPTO_ECC_SECP192K1_ENABLED
  171. #define compute_impl nrf_crypto_backend_secp192k1_ecdh_compute
  172. #define compute_impl_context_size NRF_CRYPTO_BACKEND_SECP192K1_ECDH_CONTEXT_SIZE
  173. #elif NRF_CRYPTO_ECC_SECP224K1_ENABLED
  174. #define compute_impl nrf_crypto_backend_secp224k1_ecdh_compute
  175. #define compute_impl_context_size NRF_CRYPTO_BACKEND_SECP224K1_ECDH_CONTEXT_SIZE
  176. #elif NRF_CRYPTO_ECC_SECP256K1_ENABLED
  177. #define compute_impl nrf_crypto_backend_secp256k1_ecdh_compute
  178. #define compute_impl_context_size NRF_CRYPTO_BACKEND_SECP256K1_ECDH_CONTEXT_SIZE
  179. #elif NRF_CRYPTO_ECC_BP256R1_ENABLED
  180. #define compute_impl nrf_crypto_backend_bp256r1_ecdh_compute
  181. #define compute_impl_context_size NRF_CRYPTO_BACKEND_BP256R1_ECDH_CONTEXT_SIZE
  182. #elif NRF_CRYPTO_ECC_BP384R1_ENABLED
  183. #define compute_impl nrf_crypto_backend_bp384r1_ecdh_compute
  184. #define compute_impl_context_size NRF_CRYPTO_BACKEND_BP384R1_ECDH_CONTEXT_SIZE
  185. #elif NRF_CRYPTO_ECC_BP512R1_ENABLED
  186. #define compute_impl nrf_crypto_backend_bp512r1_ecdh_compute
  187. #define compute_impl_context_size NRF_CRYPTO_BACKEND_BP512R1_ECDH_CONTEXT_SIZE
  188. #elif NRF_CRYPTO_ECC_CURVE25519_ENABLED
  189. #define compute_impl nrf_crypto_backend_curve25519_ecdh_compute
  190. #define compute_impl_context_size NRF_CRYPTO_BACKEND_CURVE25519_ECDH_CONTEXT_SIZE
  191. #else
  192. #define compute_impl NULL
  193. #define compute_impl_context_size 0
  194. #endif
  195. #define BACKEND_IMPL_GET(function, curve_type) (function)
  196. #endif
  197. ret_code_t nrf_crypto_ecdh_compute(
  198. nrf_crypto_ecdh_context_t * p_context,
  199. nrf_crypto_ecc_private_key_t const * p_private_key,
  200. nrf_crypto_ecc_public_key_t const * p_public_key,
  201. uint8_t * p_shared_secret,
  202. size_t * p_shared_secret_size)
  203. {
  204. ret_code_t result;
  205. void * p_allocated_context = NULL;
  206. nrf_crypto_backend_ecdh_compute_fn_t backend_implementation;
  207. size_t context_size;
  208. nrf_crypto_ecc_curve_info_t const * p_info;
  209. // Get pointer to header for each key
  210. nrf_crypto_internal_ecc_key_header_t const * p_private_key_header =
  211. (nrf_crypto_internal_ecc_key_header_t const *)p_private_key;
  212. nrf_crypto_internal_ecc_key_header_t const * p_public_key_header =
  213. (nrf_crypto_internal_ecc_key_header_t const *)p_public_key;
  214. // Check and prepare parameters
  215. result = nrf_crypto_internal_ecc_key_input_check(
  216. p_private_key_header,
  217. NRF_CRYPTO_INTERNAL_ECC_PRIVATE_KEY_INIT_VALUE);
  218. VERIFY_SUCCESS(result);
  219. result = nrf_crypto_internal_ecc_key_input_check(
  220. p_public_key_header,
  221. NRF_CRYPTO_INTERNAL_ECC_PUBLIC_KEY_INIT_VALUE);
  222. VERIFY_SUCCESS(result);
  223. VERIFY_TRUE(p_private_key_header->p_info == p_public_key_header->p_info,
  224. NRF_ERROR_CRYPTO_ECDH_CURVE_MISMATCH);
  225. p_info = p_private_key_header->p_info;
  226. result = nrf_crypto_internal_ecc_raw_output_prepare(
  227. p_shared_secret,
  228. p_shared_secret_size,
  229. p_info->raw_private_key_size);
  230. VERIFY_SUCCESS(result);
  231. // Get backend specific information
  232. backend_implementation = BACKEND_IMPL_GET(compute_impl, p_info->curve_type);
  233. context_size = BACKEND_IMPL_GET(compute_impl_context_size, p_info->curve_type);
  234. VERIFY_TRUE(backend_implementation != NULL, NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE);
  235. // Allocate context if not provided
  236. if (p_context == NULL && context_size > 0)
  237. {
  238. p_allocated_context = NRF_CRYPTO_ALLOC(context_size);
  239. VERIFY_TRUE(p_allocated_context != NULL, NRF_ERROR_CRYPTO_ALLOC_FAILED);
  240. p_context = p_allocated_context;
  241. }
  242. // Execute backend implementation
  243. result = backend_implementation(p_context, p_private_key, p_public_key, p_shared_secret);
  244. // Deallocate context if allocated
  245. if (p_allocated_context != NULL)
  246. {
  247. NRF_CRYPTO_FREE(p_allocated_context);
  248. }
  249. return result;
  250. }
  251. #endif // NRF_CRYPTO_ECC_ENABLED