mbedtls_backend_ecc.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. #ifndef MBEDTLS_BACKEND_ECC_H__
  41. #define MBEDTLS_BACKEND_ECC_H__
  42. #include "sdk_config.h"
  43. #include "nordic_common.h"
  44. #if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
  45. #include <stdint.h>
  46. #include <stdbool.h>
  47. #include "nrf_crypto_ecc_shared.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. /*lint -restore*/
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59. /** @internal @brief Common structure holding private key for mbed TLS.
  60. */
  61. typedef struct
  62. {
  63. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */
  64. mbedtls_mpi key; /**< @internal @brief mbed TLS specific key representation */
  65. } nrf_crypto_backend_mbedtls_ecc_private_key_t;
  66. /** @internal @brief Common structure holding public key for mbed TLS.
  67. */
  68. typedef struct
  69. {
  70. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */
  71. mbedtls_ecp_point key; /**< @internal @brief mbed TLS specific key representation */
  72. } nrf_crypto_backend_mbedtls_ecc_public_key_t;
  73. /** @internal See @ref nrf_crypto_backend_ecc_key_pair_generate_fn_t.
  74. */
  75. ret_code_t nrf_crypto_backend_mbedtls_key_pair_generate(
  76. void * p_context,
  77. void * p_private_key,
  78. void * p_public_key);
  79. /** @internal See @ref nrf_crypto_backend_ecc_public_key_calculate_fn_t.
  80. */
  81. ret_code_t nrf_crypto_backend_mbedtls_public_key_calculate(
  82. void * p_context,
  83. void const * p_private_key,
  84. void * p_public_key);
  85. /** @internal See @ref nrf_crypto_backend_ecc_private_key_from_raw_fn_t.
  86. */
  87. ret_code_t nrf_crypto_backend_mbedtls_private_key_from_raw(
  88. void * p_private_key,
  89. uint8_t const * p_raw_data);
  90. /** @internal See @ref nrf_crypto_backend_ecc_private_key_to_raw_fn_t.
  91. */
  92. ret_code_t nrf_crypto_backend_mbedtls_private_key_to_raw(
  93. void const * p_private_key,
  94. uint8_t * p_raw_data);
  95. /** @internal See @ref nrf_crypto_backend_ecc_public_key_from_raw_fn_t.
  96. */
  97. ret_code_t nrf_crypto_backend_mbedtls_public_key_from_raw(
  98. void * p_public_key,
  99. uint8_t const * p_raw_data);
  100. /** @internal See @ref nrf_crypto_backend_ecc_public_key_to_raw_fn_t.
  101. */
  102. ret_code_t nrf_crypto_backend_mbedtls_public_key_to_raw(
  103. void const * p_public_key,
  104. uint8_t * p_raw_data);
  105. /** @internal See @ref nrf_crypto_backend_ecc_key_free_fn_t.
  106. */
  107. ret_code_t nrf_crypto_backend_mbedtls_private_key_free(
  108. void * p_private_key);
  109. /** @internal See @ref nrf_crypto_backend_ecc_key_free_fn_t.
  110. */
  111. ret_code_t nrf_crypto_backend_mbedtls_public_key_free(
  112. void * p_public_key);
  113. /** @internal @brief Loads mbed TLS ECC group of specified curve type.
  114. *
  115. * @param[out] p_group Pointer to place where to load a group. Data have to be later deallocated.
  116. * @param[in] curve_type ECC curve type from enum @ref nrf_crypto_ecc_curve_type_t.
  117. * @returns true on success, false if curve is not supported or no found in mbed TLS.
  118. */
  119. bool nrf_crypto_backend_mbedtls_ecc_group_load(
  120. mbedtls_ecp_group * p_group,
  121. struct nrf_crypto_ecc_curve_info_s const * p_info);
  122. /** @internal @brief Function that can be used as a parameter to mbed TLS functions requiring random
  123. * number generator.
  124. *
  125. * It uses RNG from libary front end to generate random numbers.
  126. *
  127. * @param[in] p_param Opaque pointer passed by mbed TLS. Unused by this implementation.
  128. * @param[out] p_data Pointer where to put random number.
  129. * @returns 0 on success, mbed TLS error code on error.
  130. */
  131. int nrf_crypto_backend_mbedtls_ecc_mbedtls_rng(void * p_param, unsigned char * p_data, size_t size);
  132. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192R1)
  133. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP192R1)
  134. #error "More than one backend enabled for secp192r1 (NIST 192-bit).");
  135. #endif
  136. #define NRF_CRYPTO_ECC_SECP192R1_ENABLED 1
  137. // Aliases for one common MBEDTLS implementation
  138. #define nrf_crypto_backend_secp192r1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
  139. #define nrf_crypto_backend_secp192r1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
  140. #define nrf_crypto_backend_secp192r1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
  141. #define nrf_crypto_backend_secp192r1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
  142. #define nrf_crypto_backend_secp192r1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
  143. #define nrf_crypto_backend_secp192r1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
  144. #define nrf_crypto_backend_secp192r1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
  145. #define nrf_crypto_backend_secp192r1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
  146. // mbed TLS does not require context, so its size is 0.
  147. #define NRF_CRYPTO_BACKEND_SECP192R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  148. #define NRF_CRYPTO_BACKEND_SECP192R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  149. // All MBEDTLS curve types share the same data structures
  150. typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_secp192r1_private_key_t;
  151. typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_secp192r1_public_key_t;
  152. // Dummy typedef for unused context
  153. typedef uint32_t nrf_crypto_backend_secp192r1_key_pair_generate_context_t;
  154. typedef uint32_t nrf_crypto_backend_secp192r1_public_key_calculate_context_t;
  155. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192R1)
  156. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224R1)
  157. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP224R1)
  158. #error "More than one backend enabled for secp224r1 (NIST 224-bit).");
  159. #endif
  160. #define NRF_CRYPTO_ECC_SECP224R1_ENABLED 1
  161. // Aliases for one common MBEDTLS implementation
  162. #define nrf_crypto_backend_secp224r1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
  163. #define nrf_crypto_backend_secp224r1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
  164. #define nrf_crypto_backend_secp224r1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
  165. #define nrf_crypto_backend_secp224r1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
  166. #define nrf_crypto_backend_secp224r1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
  167. #define nrf_crypto_backend_secp224r1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
  168. #define nrf_crypto_backend_secp224r1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
  169. #define nrf_crypto_backend_secp224r1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
  170. // mbed TLS does not require context, so its size is 0.
  171. #define NRF_CRYPTO_BACKEND_SECP224R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  172. #define NRF_CRYPTO_BACKEND_SECP224R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  173. // All MBEDTLS curve types share the same data structures
  174. typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_secp224r1_private_key_t;
  175. typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_secp224r1_public_key_t;
  176. // Dummy typedef for unused context
  177. typedef uint32_t nrf_crypto_backend_secp224r1_key_pair_generate_context_t;
  178. typedef uint32_t nrf_crypto_backend_secp224r1_public_key_calculate_context_t;
  179. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224R1)
  180. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256R1)
  181. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP256R1)
  182. #error "More than one backend enabled for secp256r1 (NIST 256-bit).");
  183. #endif
  184. #define NRF_CRYPTO_ECC_SECP256R1_ENABLED 1
  185. // Aliases for one common MBEDTLS implementation
  186. #define nrf_crypto_backend_secp256r1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
  187. #define nrf_crypto_backend_secp256r1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
  188. #define nrf_crypto_backend_secp256r1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
  189. #define nrf_crypto_backend_secp256r1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
  190. #define nrf_crypto_backend_secp256r1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
  191. #define nrf_crypto_backend_secp256r1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
  192. #define nrf_crypto_backend_secp256r1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
  193. #define nrf_crypto_backend_secp256r1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
  194. // mbed TLS does not require context, so its size is 0.
  195. #define NRF_CRYPTO_BACKEND_SECP256R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  196. #define NRF_CRYPTO_BACKEND_SECP256R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  197. // All MBEDTLS curve types share the same data structures
  198. typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_secp256r1_private_key_t;
  199. typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_secp256r1_public_key_t;
  200. // Dummy typedef for unused context
  201. typedef uint32_t nrf_crypto_backend_secp256r1_key_pair_generate_context_t;
  202. typedef uint32_t nrf_crypto_backend_secp256r1_public_key_calculate_context_t;
  203. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256R1)
  204. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP384R1)
  205. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP384R1)
  206. #error "More than one backend enabled for secp384r1 (NIST 384-bit).");
  207. #endif
  208. #define NRF_CRYPTO_ECC_SECP384R1_ENABLED 1
  209. // Aliases for one common MBEDTLS implementation
  210. #define nrf_crypto_backend_secp384r1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
  211. #define nrf_crypto_backend_secp384r1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
  212. #define nrf_crypto_backend_secp384r1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
  213. #define nrf_crypto_backend_secp384r1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
  214. #define nrf_crypto_backend_secp384r1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
  215. #define nrf_crypto_backend_secp384r1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
  216. #define nrf_crypto_backend_secp384r1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
  217. #define nrf_crypto_backend_secp384r1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
  218. // mbed TLS does not require context, so its size is 0.
  219. #define NRF_CRYPTO_BACKEND_SECP384R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  220. #define NRF_CRYPTO_BACKEND_SECP384R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  221. // All MBEDTLS curve types share the same data structures
  222. typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_secp384r1_private_key_t;
  223. typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_secp384r1_public_key_t;
  224. // Dummy typedef for unused context
  225. typedef uint32_t nrf_crypto_backend_secp384r1_key_pair_generate_context_t;
  226. typedef uint32_t nrf_crypto_backend_secp384r1_public_key_calculate_context_t;
  227. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP384R1)
  228. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP521R1)
  229. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP521R1)
  230. #error "More than one backend enabled for secp521r1 (NIST 521-bit).");
  231. #endif
  232. #define NRF_CRYPTO_ECC_SECP521R1_ENABLED 1
  233. // Aliases for one common MBEDTLS implementation
  234. #define nrf_crypto_backend_secp521r1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
  235. #define nrf_crypto_backend_secp521r1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
  236. #define nrf_crypto_backend_secp521r1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
  237. #define nrf_crypto_backend_secp521r1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
  238. #define nrf_crypto_backend_secp521r1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
  239. #define nrf_crypto_backend_secp521r1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
  240. #define nrf_crypto_backend_secp521r1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
  241. #define nrf_crypto_backend_secp521r1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
  242. // mbed TLS does not require context, so its size is 0.
  243. #define NRF_CRYPTO_BACKEND_SECP521R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  244. #define NRF_CRYPTO_BACKEND_SECP521R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  245. // All MBEDTLS curve types share the same data structures
  246. typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_secp521r1_private_key_t;
  247. typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_secp521r1_public_key_t;
  248. // Dummy typedef for unused context
  249. typedef uint32_t nrf_crypto_backend_secp521r1_key_pair_generate_context_t;
  250. typedef uint32_t nrf_crypto_backend_secp521r1_public_key_calculate_context_t;
  251. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP521R1)
  252. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192K1)
  253. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP192K1)
  254. #error "More than one backend enabled for secp192k1 (Koblitz 192-bit).");
  255. #endif
  256. #define NRF_CRYPTO_ECC_SECP192K1_ENABLED 1
  257. // Aliases for one common MBEDTLS implementation
  258. #define nrf_crypto_backend_secp192k1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
  259. #define nrf_crypto_backend_secp192k1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
  260. #define nrf_crypto_backend_secp192k1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
  261. #define nrf_crypto_backend_secp192k1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
  262. #define nrf_crypto_backend_secp192k1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
  263. #define nrf_crypto_backend_secp192k1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
  264. #define nrf_crypto_backend_secp192k1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
  265. #define nrf_crypto_backend_secp192k1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
  266. // mbed TLS does not require context, so its size is 0.
  267. #define NRF_CRYPTO_BACKEND_SECP192K1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  268. #define NRF_CRYPTO_BACKEND_SECP192K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  269. // All MBEDTLS curve types share the same data structures
  270. typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_secp192k1_private_key_t;
  271. typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_secp192k1_public_key_t;
  272. // Dummy typedef for unused context
  273. typedef uint32_t nrf_crypto_backend_secp192k1_key_pair_generate_context_t;
  274. typedef uint32_t nrf_crypto_backend_secp192k1_public_key_calculate_context_t;
  275. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192K1)
  276. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224K1)
  277. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP224K1)
  278. #error "More than one backend enabled for secp224k1 (Koblitz 224-bit).");
  279. #endif
  280. #define NRF_CRYPTO_ECC_SECP224K1_ENABLED 1
  281. // Aliases for one common MBEDTLS implementation
  282. #define nrf_crypto_backend_secp224k1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
  283. #define nrf_crypto_backend_secp224k1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
  284. #define nrf_crypto_backend_secp224k1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
  285. #define nrf_crypto_backend_secp224k1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
  286. #define nrf_crypto_backend_secp224k1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
  287. #define nrf_crypto_backend_secp224k1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
  288. #define nrf_crypto_backend_secp224k1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
  289. #define nrf_crypto_backend_secp224k1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
  290. // mbed TLS does not require context, so its size is 0.
  291. #define NRF_CRYPTO_BACKEND_SECP224K1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  292. #define NRF_CRYPTO_BACKEND_SECP224K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  293. // All MBEDTLS curve types share the same data structures
  294. typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_secp224k1_private_key_t;
  295. typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_secp224k1_public_key_t;
  296. // Dummy typedef for unused context
  297. typedef uint32_t nrf_crypto_backend_secp224k1_key_pair_generate_context_t;
  298. typedef uint32_t nrf_crypto_backend_secp224k1_public_key_calculate_context_t;
  299. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224K1)
  300. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256K1)
  301. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP256K1)
  302. #error "More than one backend enabled for secp256k1 (Koblitz 256-bit).");
  303. #endif
  304. #define NRF_CRYPTO_ECC_SECP256K1_ENABLED 1
  305. // Aliases for one common MBEDTLS implementation
  306. #define nrf_crypto_backend_secp256k1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
  307. #define nrf_crypto_backend_secp256k1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
  308. #define nrf_crypto_backend_secp256k1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
  309. #define nrf_crypto_backend_secp256k1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
  310. #define nrf_crypto_backend_secp256k1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
  311. #define nrf_crypto_backend_secp256k1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
  312. #define nrf_crypto_backend_secp256k1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
  313. #define nrf_crypto_backend_secp256k1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
  314. // mbed TLS does not require context, so its size is 0.
  315. #define NRF_CRYPTO_BACKEND_SECP256K1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  316. #define NRF_CRYPTO_BACKEND_SECP256K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  317. // All MBEDTLS curve types share the same data structures
  318. typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_secp256k1_private_key_t;
  319. typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_secp256k1_public_key_t;
  320. // Dummy typedef for unused context
  321. typedef uint32_t nrf_crypto_backend_secp256k1_key_pair_generate_context_t;
  322. typedef uint32_t nrf_crypto_backend_secp256k1_public_key_calculate_context_t;
  323. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256K1)
  324. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP256R1)
  325. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_BP256R1)
  326. #error "More than one backend enabled for bp256r1 (Brainpool 256-bit).");
  327. #endif
  328. #define NRF_CRYPTO_ECC_BP256R1_ENABLED 1
  329. // Aliases for one common MBEDTLS implementation
  330. #define nrf_crypto_backend_bp256r1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
  331. #define nrf_crypto_backend_bp256r1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
  332. #define nrf_crypto_backend_bp256r1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
  333. #define nrf_crypto_backend_bp256r1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
  334. #define nrf_crypto_backend_bp256r1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
  335. #define nrf_crypto_backend_bp256r1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
  336. #define nrf_crypto_backend_bp256r1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
  337. #define nrf_crypto_backend_bp256r1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
  338. // mbed TLS does not require context, so its size is 0.
  339. #define NRF_CRYPTO_BACKEND_BP256R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  340. #define NRF_CRYPTO_BACKEND_BP256R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  341. // All MBEDTLS curve types share the same data structures
  342. typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_bp256r1_private_key_t;
  343. typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_bp256r1_public_key_t;
  344. // Dummy typedef for unused context
  345. typedef uint32_t nrf_crypto_backend_bp256r1_key_pair_generate_context_t;
  346. typedef uint32_t nrf_crypto_backend_bp256r1_public_key_calculate_context_t;
  347. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP256R1)
  348. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP384R1)
  349. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_BP384R1)
  350. #error "More than one backend enabled for bp384r1 (Brainpool 384-bit).");
  351. #endif
  352. #define NRF_CRYPTO_ECC_BP384R1_ENABLED 1
  353. // Aliases for one common MBEDTLS implementation
  354. #define nrf_crypto_backend_bp384r1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
  355. #define nrf_crypto_backend_bp384r1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
  356. #define nrf_crypto_backend_bp384r1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
  357. #define nrf_crypto_backend_bp384r1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
  358. #define nrf_crypto_backend_bp384r1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
  359. #define nrf_crypto_backend_bp384r1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
  360. #define nrf_crypto_backend_bp384r1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
  361. #define nrf_crypto_backend_bp384r1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
  362. // mbed TLS does not require context, so its size is 0.
  363. #define NRF_CRYPTO_BACKEND_BP384R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  364. #define NRF_CRYPTO_BACKEND_BP384R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  365. // All MBEDTLS curve types share the same data structures
  366. typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_bp384r1_private_key_t;
  367. typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_bp384r1_public_key_t;
  368. // Dummy typedef for unused context
  369. typedef uint32_t nrf_crypto_backend_bp384r1_key_pair_generate_context_t;
  370. typedef uint32_t nrf_crypto_backend_bp384r1_public_key_calculate_context_t;
  371. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP384R1)
  372. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP512R1)
  373. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_BP512R1)
  374. #error "More than one backend enabled for bp512r1 (Brainpool 512-bit).");
  375. #endif
  376. #define NRF_CRYPTO_ECC_BP512R1_ENABLED 1
  377. // Aliases for one common MBEDTLS implementation
  378. #define nrf_crypto_backend_bp512r1_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
  379. #define nrf_crypto_backend_bp512r1_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
  380. #define nrf_crypto_backend_bp512r1_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
  381. #define nrf_crypto_backend_bp512r1_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
  382. #define nrf_crypto_backend_bp512r1_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
  383. #define nrf_crypto_backend_bp512r1_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
  384. #define nrf_crypto_backend_bp512r1_private_key_free nrf_crypto_backend_mbedtls_private_key_free
  385. #define nrf_crypto_backend_bp512r1_public_key_free nrf_crypto_backend_mbedtls_public_key_free
  386. // mbed TLS does not require context, so its size is 0.
  387. #define NRF_CRYPTO_BACKEND_BP512R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  388. #define NRF_CRYPTO_BACKEND_BP512R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  389. // All MBEDTLS curve types share the same data structures
  390. typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_bp512r1_private_key_t;
  391. typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_bp512r1_public_key_t;
  392. // Dummy typedef for unused context
  393. typedef uint32_t nrf_crypto_backend_bp512r1_key_pair_generate_context_t;
  394. typedef uint32_t nrf_crypto_backend_bp512r1_public_key_calculate_context_t;
  395. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP512R1)
  396. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_CURVE25519)
  397. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_CURVE25519)
  398. #error "More than one backend enabled for Curve25519.");
  399. #endif
  400. #define NRF_CRYPTO_ECC_CURVE25519_ENABLED 1
  401. // Aliases for one common MBEDTLS implementation
  402. #define nrf_crypto_backend_curve25519_key_pair_generate nrf_crypto_backend_mbedtls_key_pair_generate
  403. #define nrf_crypto_backend_curve25519_public_key_calculate nrf_crypto_backend_mbedtls_public_key_calculate
  404. #define nrf_crypto_backend_curve25519_private_key_from_raw nrf_crypto_backend_mbedtls_private_key_from_raw
  405. #define nrf_crypto_backend_curve25519_private_key_to_raw nrf_crypto_backend_mbedtls_private_key_to_raw
  406. #define nrf_crypto_backend_curve25519_public_key_from_raw nrf_crypto_backend_mbedtls_public_key_from_raw
  407. #define nrf_crypto_backend_curve25519_public_key_to_raw nrf_crypto_backend_mbedtls_public_key_to_raw
  408. #define nrf_crypto_backend_curve25519_private_key_free nrf_crypto_backend_mbedtls_private_key_free
  409. #define nrf_crypto_backend_curve25519_public_key_free nrf_crypto_backend_mbedtls_public_key_free
  410. // mbed TLS does not require context, so its size is 0.
  411. #define NRF_CRYPTO_BACKEND_CURVE25519_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  412. #define NRF_CRYPTO_BACKEND_CURVE25519_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  413. // All MBEDTLS curve types share the same data structures
  414. typedef nrf_crypto_backend_mbedtls_ecc_private_key_t nrf_crypto_backend_curve25519_private_key_t;
  415. typedef nrf_crypto_backend_mbedtls_ecc_public_key_t nrf_crypto_backend_curve25519_public_key_t;
  416. // Dummy typedef for unused context
  417. typedef uint32_t nrf_crypto_backend_curve25519_key_pair_generate_context_t;
  418. typedef uint32_t nrf_crypto_backend_curve25519_public_key_calculate_context_t;
  419. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_ECC_CURVE25519)
  420. #ifdef __cplusplus
  421. }
  422. #endif
  423. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
  424. #endif // MBEDTLS_BACKEND_ECC_H__