micro_ecc_backend_ecc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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_MICRO_ECC)
  43. #include <stdbool.h>
  44. #include <string.h>
  45. #include <stddef.h>
  46. #include "app_util.h"
  47. #include "nrf_crypto_ecc.h"
  48. #include "nrf_crypto_rng.h"
  49. #include "nrf_crypto_shared.h"
  50. #include "micro_ecc_backend_ecc.h"
  51. #include "micro_ecc_backend_shared.h"
  52. #include "uECC.h"
  53. typedef uECC_Curve (*micro_ecc_curve_fn_t)(void);
  54. int nrf_crypto_backend_micro_ecc_rng_callback(uint8_t * dest, unsigned size)
  55. {
  56. #if NRF_MODULE_ENABLED(NRF_CRYPTO_RNG)
  57. ret_code_t result;
  58. result = nrf_crypto_rng_vector_generate(dest, size);
  59. // Return values compatible with mbed TLS
  60. if (result != NRF_SUCCESS)
  61. {
  62. return 0;
  63. }
  64. return 1;
  65. #else
  66. UNUSED_PARAMETER(dest);
  67. UNUSED_PARAMETER(size);
  68. return 0;
  69. #endif
  70. }
  71. uECC_Curve nrf_crypto_backend_micro_ecc_curve_get(
  72. nrf_crypto_backend_micro_ecc_common_key_t const * p_key)
  73. {
  74. nrf_crypto_internal_ecc_key_header_t const * p_key_header =
  75. (nrf_crypto_internal_ecc_key_header_t const *)p_key;
  76. //lint -save -e611 (Suspicious cast)
  77. micro_ecc_curve_fn_t micro_ecc_curve_fn =
  78. (micro_ecc_curve_fn_t)p_key_header->p_info->p_backend_data;
  79. //lint -restore
  80. uECC_Curve p_micro_ecc_curve = micro_ecc_curve_fn();
  81. return p_micro_ecc_curve;
  82. }
  83. ret_code_t nrf_crypto_backend_micro_ecc_key_pair_generate(
  84. void * p_context,
  85. void * p_private_key,
  86. void * p_public_key)
  87. {
  88. int result;
  89. nrf_crypto_backend_micro_ecc_common_key_t * p_prv =
  90. (nrf_crypto_backend_micro_ecc_common_key_t *)p_private_key;
  91. nrf_crypto_backend_micro_ecc_common_key_t * p_pub =
  92. (nrf_crypto_backend_micro_ecc_common_key_t *)p_public_key;
  93. uECC_Curve p_micro_ecc_curve = nrf_crypto_backend_micro_ecc_curve_get(p_prv);
  94. uECC_set_rng(nrf_crypto_backend_micro_ecc_rng_callback);
  95. result = uECC_make_key((uint8_t *)(&p_pub->key[0]),
  96. (uint8_t *)(&p_prv->key[0]),
  97. p_micro_ecc_curve);
  98. if (result == 0)
  99. {
  100. return NRF_ERROR_CRYPTO_INTERNAL;
  101. }
  102. return NRF_SUCCESS;
  103. }
  104. ret_code_t nrf_crypto_backend_micro_ecc_public_key_calculate(
  105. void * p_context,
  106. void const * p_private_key,
  107. void * p_public_key)
  108. {
  109. int result;
  110. nrf_crypto_backend_micro_ecc_common_key_t const * p_prv =
  111. (nrf_crypto_backend_micro_ecc_common_key_t const *)p_private_key;
  112. nrf_crypto_backend_micro_ecc_common_key_t * p_pub =
  113. (nrf_crypto_backend_micro_ecc_common_key_t *)p_public_key;
  114. uECC_Curve p_micro_ecc_curve = nrf_crypto_backend_micro_ecc_curve_get(p_prv);
  115. result = uECC_compute_public_key((uint8_t *)(&p_prv->key[0]),
  116. (uint8_t *)(&p_pub->key[0]),
  117. p_micro_ecc_curve);
  118. if (result == 0)
  119. {
  120. return NRF_ERROR_CRYPTO_INTERNAL;
  121. }
  122. return NRF_SUCCESS;
  123. }
  124. ret_code_t nrf_crypto_backend_micro_ecc_private_key_from_raw(
  125. void * p_private_key,
  126. uint8_t const * p_raw_data)
  127. {
  128. nrf_crypto_backend_micro_ecc_common_key_t * p_prv =
  129. (nrf_crypto_backend_micro_ecc_common_key_t *)p_private_key;
  130. nrf_crypto_ecc_curve_info_t const * p_info = p_prv->header.p_info;
  131. #if ECC_BACKEND_SWAP_BYTES
  132. nrf_crypto_internal_swap_endian((uint8_t *)(&p_prv->key[0]),
  133. p_raw_data,
  134. p_info->raw_private_key_size);
  135. #else
  136. memcpy(&p_prv->key[0], p_raw_data, p_info->raw_private_key_size);
  137. #endif
  138. return NRF_SUCCESS;
  139. }
  140. ret_code_t nrf_crypto_backend_micro_ecc_private_key_to_raw(
  141. void const * p_private_key,
  142. uint8_t * p_raw_data)
  143. {
  144. nrf_crypto_backend_micro_ecc_common_key_t const * p_prv =
  145. (nrf_crypto_backend_micro_ecc_common_key_t const *)p_private_key;
  146. nrf_crypto_ecc_curve_info_t const * p_info = p_prv->header.p_info;
  147. #if ECC_BACKEND_SWAP_BYTES
  148. nrf_crypto_internal_swap_endian(p_raw_data,
  149. (uint8_t *)(&p_prv->key[0]),
  150. p_info->raw_private_key_size);
  151. #else
  152. memcpy(p_raw_data, &p_prv->key[0], p_info->raw_private_key_size);
  153. #endif
  154. return NRF_SUCCESS;
  155. }
  156. ret_code_t nrf_crypto_backend_micro_ecc_public_key_from_raw(
  157. void * p_public_key,
  158. uint8_t const * p_raw_data)
  159. {
  160. nrf_crypto_backend_micro_ecc_common_key_t * p_pub =
  161. (nrf_crypto_backend_micro_ecc_common_key_t *)p_public_key;
  162. nrf_crypto_ecc_curve_info_t const * p_info = p_pub->header.p_info;
  163. #if ECC_BACKEND_SWAP_BYTES
  164. nrf_crypto_internal_double_swap_endian((uint8_t *)(&p_pub->key[0]),
  165. p_raw_data,
  166. p_info->raw_private_key_size);
  167. #else
  168. memcpy(&p_pub->key[0], p_raw_data, p_info->raw_public_key_size);
  169. #endif
  170. return NRF_SUCCESS;
  171. }
  172. ret_code_t nrf_crypto_backend_micro_ecc_public_key_to_raw(
  173. void const * p_public_key,
  174. uint8_t * p_raw_data)
  175. {
  176. nrf_crypto_backend_micro_ecc_common_key_t const * p_pub =
  177. (nrf_crypto_backend_micro_ecc_common_key_t const *)p_public_key;
  178. nrf_crypto_ecc_curve_info_t const * p_info = p_pub->header.p_info;
  179. #if ECC_BACKEND_SWAP_BYTES
  180. nrf_crypto_internal_double_swap_endian(p_raw_data,
  181. (uint8_t *)(&p_pub->key[0]),
  182. p_info->raw_private_key_size);
  183. #else
  184. memcpy(p_raw_data, &p_pub->key[0], p_info->raw_public_key_size);
  185. #endif
  186. return NRF_SUCCESS;
  187. }
  188. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP192R1)
  189. // Make sure that common key structure match secp192r1 (NIST 192-bit) key structure to safely cast types.
  190. STATIC_ASSERT(offsetof(nrf_crypto_backend_micro_ecc_common_key_t, key) ==
  191. offsetof(nrf_crypto_backend_secp192r1_private_key_t, key),
  192. "Common uECC private key structure does not match secp192r1 (NIST 192-bit) one.");
  193. STATIC_ASSERT(offsetof(nrf_crypto_backend_micro_ecc_common_key_t, key) ==
  194. offsetof(nrf_crypto_backend_secp192r1_public_key_t, key),
  195. "Common ECC public key structure does not match secp192r1 (NIST 192-bit) one.");
  196. const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_secp192r1_curve_info =
  197. {
  198. .public_key_size = sizeof(nrf_crypto_backend_secp192r1_public_key_t),
  199. .private_key_size = sizeof(nrf_crypto_backend_secp192r1_private_key_t),
  200. .curve_type = NRF_CRYPTO_ECC_SECP192R1_CURVE_TYPE,
  201. .raw_private_key_size = NRF_CRYPTO_ECC_SECP192R1_RAW_PRIVATE_KEY_SIZE,
  202. .raw_public_key_size = NRF_CRYPTO_ECC_SECP192R1_RAW_PUBLIC_KEY_SIZE,
  203. //lint -save -e611 -e546 (Suspicious cast, Suspicious use of &)
  204. .p_backend_data = (void *)&uECC_secp192r1,
  205. //lint -restore
  206. };
  207. #endif
  208. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP224R1)
  209. // Make sure that common key structure match secp224r1 (NIST 224-bit) key structure to safely cast types.
  210. STATIC_ASSERT(offsetof(nrf_crypto_backend_micro_ecc_common_key_t, key) ==
  211. offsetof(nrf_crypto_backend_secp224r1_private_key_t, key),
  212. "Common uECC private key structure does not match secp224r1 (NIST 224-bit) one.");
  213. STATIC_ASSERT(offsetof(nrf_crypto_backend_micro_ecc_common_key_t, key) ==
  214. offsetof(nrf_crypto_backend_secp224r1_public_key_t, key),
  215. "Common ECC public key structure does not match secp224r1 (NIST 224-bit) one.");
  216. const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_secp224r1_curve_info =
  217. {
  218. .public_key_size = sizeof(nrf_crypto_backend_secp224r1_public_key_t),
  219. .private_key_size = sizeof(nrf_crypto_backend_secp224r1_private_key_t),
  220. .curve_type = NRF_CRYPTO_ECC_SECP224R1_CURVE_TYPE,
  221. .raw_private_key_size = NRF_CRYPTO_ECC_SECP224R1_RAW_PRIVATE_KEY_SIZE,
  222. .raw_public_key_size = NRF_CRYPTO_ECC_SECP224R1_RAW_PUBLIC_KEY_SIZE,
  223. //lint -save -e611 -e546 (Suspicious cast, Suspicious use of &)
  224. .p_backend_data = (void *)&uECC_secp224r1,
  225. //lint -restore
  226. };
  227. #endif
  228. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256R1)
  229. // Make sure that common key structure match secp256r1 (NIST 256-bit) key structure to safely cast types.
  230. STATIC_ASSERT(offsetof(nrf_crypto_backend_micro_ecc_common_key_t, key) ==
  231. offsetof(nrf_crypto_backend_secp256r1_private_key_t, key),
  232. "Common uECC private key structure does not match secp256r1 (NIST 256-bit) one.");
  233. STATIC_ASSERT(offsetof(nrf_crypto_backend_micro_ecc_common_key_t, key) ==
  234. offsetof(nrf_crypto_backend_secp256r1_public_key_t, key),
  235. "Common ECC public key structure does not match secp256r1 (NIST 256-bit) one.");
  236. const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_secp256r1_curve_info =
  237. {
  238. .public_key_size = sizeof(nrf_crypto_backend_secp256r1_public_key_t),
  239. .private_key_size = sizeof(nrf_crypto_backend_secp256r1_private_key_t),
  240. .curve_type = NRF_CRYPTO_ECC_SECP256R1_CURVE_TYPE,
  241. .raw_private_key_size = NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE,
  242. .raw_public_key_size = NRF_CRYPTO_ECC_SECP256R1_RAW_PUBLIC_KEY_SIZE,
  243. //lint -save -e611 -e546 (Suspicious cast, Suspicious use of &)
  244. .p_backend_data = (void *)&uECC_secp256r1,
  245. //lint -restore
  246. };
  247. #endif
  248. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256K1)
  249. // Make sure that common key structure match secp256k1 (Koblitz 256-bit) key structure to safely cast types.
  250. STATIC_ASSERT(offsetof(nrf_crypto_backend_micro_ecc_common_key_t, key) ==
  251. offsetof(nrf_crypto_backend_secp256k1_private_key_t, key),
  252. "Common uECC private key structure does not match secp256k1 (Koblitz 256-bit) one.");
  253. STATIC_ASSERT(offsetof(nrf_crypto_backend_micro_ecc_common_key_t, key) ==
  254. offsetof(nrf_crypto_backend_secp256k1_public_key_t, key),
  255. "Common ECC public key structure does not match secp256k1 (Koblitz 256-bit) one.");
  256. const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_secp256k1_curve_info =
  257. {
  258. .public_key_size = sizeof(nrf_crypto_backend_secp256k1_public_key_t),
  259. .private_key_size = sizeof(nrf_crypto_backend_secp256k1_private_key_t),
  260. .curve_type = NRF_CRYPTO_ECC_SECP256K1_CURVE_TYPE,
  261. .raw_private_key_size = NRF_CRYPTO_ECC_SECP256K1_RAW_PRIVATE_KEY_SIZE,
  262. .raw_public_key_size = NRF_CRYPTO_ECC_SECP256K1_RAW_PUBLIC_KEY_SIZE,
  263. //lint -save -e611 -e546 (Suspicious cast, Suspicious use of &)
  264. .p_backend_data = (void *)&uECC_secp256k1,
  265. //lint -restore
  266. };
  267. #endif
  268. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MICRO_ECC)