cc310_backend_ecc.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 CC310_BACKEND_ECC_H__
  41. #define CC310_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_CC310)
  45. #include <stdbool.h>
  46. #include "nrf_crypto_ecc_shared.h"
  47. #include "crys_ecpki_kg.h"
  48. #include "crys_ec_mont_api.h"
  49. #include "crys_ec_edw_api.h"
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. /** @internal @brief Common structure holding private key for CC310.
  54. *
  55. * @details Used for most curves (except Curve25519 and Ed25519).
  56. */
  57. typedef struct
  58. {
  59. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */
  60. CRYS_ECPKI_UserPrivKey_t private_key; /**< @internal @brief CC310 specific key representation */
  61. } nrf_crypto_backend_cc310_ecc_private_key_t;
  62. /** @internal @brief Common structure holding public key for CC310.
  63. *
  64. * @details Used for most curves (except Curve25519 and Ed25519).
  65. */
  66. typedef struct
  67. {
  68. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */
  69. bool key_converted; /**< @internal @brief True if key was already converted from raw_public_key to cc310_public_key */
  70. union
  71. {
  72. CRYS_ECPKI_UserPublKey_t cc310_public_key; /**< @internal @brief CC310 specific key representation */
  73. uint8_t raw_public_key[132]; /**< @internal @brief raw key representation */
  74. } key;
  75. } nrf_crypto_backend_cc310_ecc_public_key_t;
  76. /** @internal @brief Common structure holding context for key pair generation.
  77. *
  78. * @details Used for most curves (except Curve25519 and Ed25519).
  79. */
  80. typedef struct
  81. {
  82. CRYS_ECPKI_KG_TempData_t temp_data; /**< @internal @brief Temporary buffer for CC310 internal storage */
  83. } nrf_crypto_backend_cc310_key_pair_generate_context_t;
  84. /** @internal See @ref nrf_crypto_backend_ecc_key_pair_generate_fn_t.
  85. *
  86. * @details Used for most curves (except Curve25519 and Ed25519).
  87. */
  88. ret_code_t nrf_crypto_backend_cc310_key_pair_generate(
  89. void * p_context,
  90. void * p_private_key,
  91. void * p_public_key);
  92. /** @internal See @ref nrf_crypto_backend_ecc_private_key_from_raw_fn_t.
  93. *
  94. * @details Used for most curves (except Curve25519 and Ed25519).
  95. */
  96. ret_code_t nrf_crypto_backend_cc310_private_key_from_raw(
  97. void * p_private_key,
  98. uint8_t const * p_raw_data);
  99. /** @internal See @ref nrf_crypto_backend_ecc_private_key_to_raw_fn_t.
  100. *
  101. * @details Used for most curves (except Curve25519 and Ed25519).
  102. */
  103. ret_code_t nrf_crypto_backend_cc310_private_key_to_raw(
  104. void const * p_private_key,
  105. uint8_t * p_raw_data);
  106. /** @internal See @ref nrf_crypto_backend_ecc_public_key_from_raw_fn_t.
  107. *
  108. * @details Used for most curves (except Curve25519 and Ed25519).
  109. */
  110. ret_code_t nrf_crypto_backend_cc310_public_key_from_raw(
  111. void * p_public_key,
  112. uint8_t const * p_raw_data);
  113. /** @internal See @ref nrf_crypto_backend_ecc_public_key_to_raw_fn_t.
  114. *
  115. * @details Used for most curves (except Curve25519 and Ed25519).
  116. */
  117. ret_code_t nrf_crypto_backend_cc310_public_key_to_raw(
  118. void const * p_public_key,
  119. uint8_t * p_raw_data);
  120. /** @internal @brief Convert error code from CC310 to nrf_crypto error code.
  121. *
  122. * @param[in] crys_error CC310 error code.
  123. * @return nrf_crypto error code.
  124. */
  125. ret_code_t nrf_crypto_backend_cc310_ecc_error_convert(uint32_t crys_error);
  126. /** @internal @brief Converts public key from raw to CC310 representation if not converted already.
  127. *
  128. * Data are read from p_pub->key.raw_public_key to stored into p_pub->cc310_public_key.
  129. *
  130. * @param[in] p_pub Public key to convert.
  131. * @param[in] p_temp_data Buffer for temporary data used by CC310 lib.
  132. * @return nrf_crypto error code.
  133. */
  134. ret_code_t nrf_crypto_backend_cc310_ecc_public_key_convert(
  135. nrf_crypto_backend_cc310_ecc_public_key_t * p_pub,
  136. CRYS_ECPKI_BUILD_TempData_t * p_temp_data);
  137. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP160R1)
  138. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP160R1)
  139. #error "More than one backend enabled for secp160r1 (NIST 160-bit).");
  140. #endif
  141. #define NRF_CRYPTO_ECC_SECP160R1_ENABLED 1
  142. // Aliases for one common CC310 implementation
  143. #define nrf_crypto_backend_secp160r1_key_pair_generate nrf_crypto_backend_cc310_key_pair_generate
  144. #define nrf_crypto_backend_secp160r1_public_key_calculate NULL
  145. #define nrf_crypto_backend_secp160r1_private_key_from_raw nrf_crypto_backend_cc310_private_key_from_raw
  146. #define nrf_crypto_backend_secp160r1_private_key_to_raw nrf_crypto_backend_cc310_private_key_to_raw
  147. #define nrf_crypto_backend_secp160r1_public_key_from_raw nrf_crypto_backend_cc310_public_key_from_raw
  148. #define nrf_crypto_backend_secp160r1_public_key_to_raw nrf_crypto_backend_cc310_public_key_to_raw
  149. #define nrf_crypto_backend_secp160r1_private_key_free NULL
  150. #define nrf_crypto_backend_secp160r1_public_key_free NULL
  151. // Context sizes required by CC310
  152. #define NRF_CRYPTO_BACKEND_SECP160R1_KEY_PAIR_GENERATE_CONTEXT_SIZE \
  153. sizeof(nrf_crypto_backend_cc310_key_pair_generate_context_t)
  154. #define NRF_CRYPTO_BACKEND_SECP160R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  155. // Most CC310 curve types share the same data structures for keys
  156. typedef nrf_crypto_backend_cc310_ecc_private_key_t nrf_crypto_backend_secp160r1_private_key_t;
  157. typedef nrf_crypto_backend_cc310_ecc_public_key_t nrf_crypto_backend_secp160r1_public_key_t;
  158. // Most CC310 curve types share the same data structures for context
  159. typedef nrf_crypto_backend_cc310_key_pair_generate_context_t
  160. nrf_crypto_backend_secp160r1_key_pair_generate_context_t;
  161. // Dummy typedef for unused context
  162. typedef uint32_t nrf_crypto_backend_secp160r1_public_key_calculate_context_t;
  163. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP160R1)
  164. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP160R2)
  165. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP160R2)
  166. #error "More than one backend enabled for secp160r2 (NIST 160-bit).");
  167. #endif
  168. #define NRF_CRYPTO_ECC_SECP160R2_ENABLED 1
  169. // Aliases for one common CC310 implementation
  170. #define nrf_crypto_backend_secp160r2_key_pair_generate nrf_crypto_backend_cc310_key_pair_generate
  171. #define nrf_crypto_backend_secp160r2_public_key_calculate NULL
  172. #define nrf_crypto_backend_secp160r2_private_key_from_raw nrf_crypto_backend_cc310_private_key_from_raw
  173. #define nrf_crypto_backend_secp160r2_private_key_to_raw nrf_crypto_backend_cc310_private_key_to_raw
  174. #define nrf_crypto_backend_secp160r2_public_key_from_raw nrf_crypto_backend_cc310_public_key_from_raw
  175. #define nrf_crypto_backend_secp160r2_public_key_to_raw nrf_crypto_backend_cc310_public_key_to_raw
  176. #define nrf_crypto_backend_secp160r2_private_key_free NULL
  177. #define nrf_crypto_backend_secp160r2_public_key_free NULL
  178. // Context sizes required by CC310
  179. #define NRF_CRYPTO_BACKEND_SECP160R2_KEY_PAIR_GENERATE_CONTEXT_SIZE \
  180. sizeof(nrf_crypto_backend_cc310_key_pair_generate_context_t)
  181. #define NRF_CRYPTO_BACKEND_SECP160R2_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  182. // Most CC310 curve types share the same data structures for keys
  183. typedef nrf_crypto_backend_cc310_ecc_private_key_t nrf_crypto_backend_secp160r2_private_key_t;
  184. typedef nrf_crypto_backend_cc310_ecc_public_key_t nrf_crypto_backend_secp160r2_public_key_t;
  185. // Most CC310 curve types share the same data structures for context
  186. typedef nrf_crypto_backend_cc310_key_pair_generate_context_t
  187. nrf_crypto_backend_secp160r2_key_pair_generate_context_t;
  188. // Dummy typedef for unused context
  189. typedef uint32_t nrf_crypto_backend_secp160r2_public_key_calculate_context_t;
  190. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP160R2)
  191. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP192R1)
  192. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP192R1)
  193. #error "More than one backend enabled for secp192r1 (NIST 192-bit).");
  194. #endif
  195. #define NRF_CRYPTO_ECC_SECP192R1_ENABLED 1
  196. // Aliases for one common CC310 implementation
  197. #define nrf_crypto_backend_secp192r1_key_pair_generate nrf_crypto_backend_cc310_key_pair_generate
  198. #define nrf_crypto_backend_secp192r1_public_key_calculate NULL
  199. #define nrf_crypto_backend_secp192r1_private_key_from_raw nrf_crypto_backend_cc310_private_key_from_raw
  200. #define nrf_crypto_backend_secp192r1_private_key_to_raw nrf_crypto_backend_cc310_private_key_to_raw
  201. #define nrf_crypto_backend_secp192r1_public_key_from_raw nrf_crypto_backend_cc310_public_key_from_raw
  202. #define nrf_crypto_backend_secp192r1_public_key_to_raw nrf_crypto_backend_cc310_public_key_to_raw
  203. #define nrf_crypto_backend_secp192r1_private_key_free NULL
  204. #define nrf_crypto_backend_secp192r1_public_key_free NULL
  205. // Context sizes required by CC310
  206. #define NRF_CRYPTO_BACKEND_SECP192R1_KEY_PAIR_GENERATE_CONTEXT_SIZE \
  207. sizeof(nrf_crypto_backend_cc310_key_pair_generate_context_t)
  208. #define NRF_CRYPTO_BACKEND_SECP192R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  209. // Most CC310 curve types share the same data structures for keys
  210. typedef nrf_crypto_backend_cc310_ecc_private_key_t nrf_crypto_backend_secp192r1_private_key_t;
  211. typedef nrf_crypto_backend_cc310_ecc_public_key_t nrf_crypto_backend_secp192r1_public_key_t;
  212. // Most CC310 curve types share the same data structures for context
  213. typedef nrf_crypto_backend_cc310_key_pair_generate_context_t
  214. nrf_crypto_backend_secp192r1_key_pair_generate_context_t;
  215. // Dummy typedef for unused context
  216. typedef uint32_t nrf_crypto_backend_secp192r1_public_key_calculate_context_t;
  217. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP192R1)
  218. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP224R1)
  219. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP224R1)
  220. #error "More than one backend enabled for secp224r1 (NIST 224-bit).");
  221. #endif
  222. #define NRF_CRYPTO_ECC_SECP224R1_ENABLED 1
  223. // Aliases for one common CC310 implementation
  224. #define nrf_crypto_backend_secp224r1_key_pair_generate nrf_crypto_backend_cc310_key_pair_generate
  225. #define nrf_crypto_backend_secp224r1_public_key_calculate NULL
  226. #define nrf_crypto_backend_secp224r1_private_key_from_raw nrf_crypto_backend_cc310_private_key_from_raw
  227. #define nrf_crypto_backend_secp224r1_private_key_to_raw nrf_crypto_backend_cc310_private_key_to_raw
  228. #define nrf_crypto_backend_secp224r1_public_key_from_raw nrf_crypto_backend_cc310_public_key_from_raw
  229. #define nrf_crypto_backend_secp224r1_public_key_to_raw nrf_crypto_backend_cc310_public_key_to_raw
  230. #define nrf_crypto_backend_secp224r1_private_key_free NULL
  231. #define nrf_crypto_backend_secp224r1_public_key_free NULL
  232. // Context sizes required by CC310
  233. #define NRF_CRYPTO_BACKEND_SECP224R1_KEY_PAIR_GENERATE_CONTEXT_SIZE \
  234. sizeof(nrf_crypto_backend_cc310_key_pair_generate_context_t)
  235. #define NRF_CRYPTO_BACKEND_SECP224R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  236. // Most CC310 curve types share the same data structures for keys
  237. typedef nrf_crypto_backend_cc310_ecc_private_key_t nrf_crypto_backend_secp224r1_private_key_t;
  238. typedef nrf_crypto_backend_cc310_ecc_public_key_t nrf_crypto_backend_secp224r1_public_key_t;
  239. // Most CC310 curve types share the same data structures for context
  240. typedef nrf_crypto_backend_cc310_key_pair_generate_context_t
  241. nrf_crypto_backend_secp224r1_key_pair_generate_context_t;
  242. // Dummy typedef for unused context
  243. typedef uint32_t nrf_crypto_backend_secp224r1_public_key_calculate_context_t;
  244. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP224R1)
  245. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP256R1)
  246. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP256R1)
  247. #error "More than one backend enabled for secp256r1 (NIST 256-bit).");
  248. #endif
  249. #define NRF_CRYPTO_ECC_SECP256R1_ENABLED 1
  250. // Aliases for one common CC310 implementation
  251. #define nrf_crypto_backend_secp256r1_key_pair_generate nrf_crypto_backend_cc310_key_pair_generate
  252. #define nrf_crypto_backend_secp256r1_public_key_calculate NULL
  253. #define nrf_crypto_backend_secp256r1_private_key_from_raw nrf_crypto_backend_cc310_private_key_from_raw
  254. #define nrf_crypto_backend_secp256r1_private_key_to_raw nrf_crypto_backend_cc310_private_key_to_raw
  255. #define nrf_crypto_backend_secp256r1_public_key_from_raw nrf_crypto_backend_cc310_public_key_from_raw
  256. #define nrf_crypto_backend_secp256r1_public_key_to_raw nrf_crypto_backend_cc310_public_key_to_raw
  257. #define nrf_crypto_backend_secp256r1_private_key_free NULL
  258. #define nrf_crypto_backend_secp256r1_public_key_free NULL
  259. // Context sizes required by CC310
  260. #define NRF_CRYPTO_BACKEND_SECP256R1_KEY_PAIR_GENERATE_CONTEXT_SIZE \
  261. sizeof(nrf_crypto_backend_cc310_key_pair_generate_context_t)
  262. #define NRF_CRYPTO_BACKEND_SECP256R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  263. // Most CC310 curve types share the same data structures for keys
  264. typedef nrf_crypto_backend_cc310_ecc_private_key_t nrf_crypto_backend_secp256r1_private_key_t;
  265. typedef nrf_crypto_backend_cc310_ecc_public_key_t nrf_crypto_backend_secp256r1_public_key_t;
  266. // Most CC310 curve types share the same data structures for context
  267. typedef nrf_crypto_backend_cc310_key_pair_generate_context_t
  268. nrf_crypto_backend_secp256r1_key_pair_generate_context_t;
  269. // Dummy typedef for unused context
  270. typedef uint32_t nrf_crypto_backend_secp256r1_public_key_calculate_context_t;
  271. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP256R1)
  272. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP384R1)
  273. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP384R1)
  274. #error "More than one backend enabled for secp384r1 (NIST 384-bit).");
  275. #endif
  276. #define NRF_CRYPTO_ECC_SECP384R1_ENABLED 1
  277. // Aliases for one common CC310 implementation
  278. #define nrf_crypto_backend_secp384r1_key_pair_generate nrf_crypto_backend_cc310_key_pair_generate
  279. #define nrf_crypto_backend_secp384r1_public_key_calculate NULL
  280. #define nrf_crypto_backend_secp384r1_private_key_from_raw nrf_crypto_backend_cc310_private_key_from_raw
  281. #define nrf_crypto_backend_secp384r1_private_key_to_raw nrf_crypto_backend_cc310_private_key_to_raw
  282. #define nrf_crypto_backend_secp384r1_public_key_from_raw nrf_crypto_backend_cc310_public_key_from_raw
  283. #define nrf_crypto_backend_secp384r1_public_key_to_raw nrf_crypto_backend_cc310_public_key_to_raw
  284. #define nrf_crypto_backend_secp384r1_private_key_free NULL
  285. #define nrf_crypto_backend_secp384r1_public_key_free NULL
  286. // Context sizes required by CC310
  287. #define NRF_CRYPTO_BACKEND_SECP384R1_KEY_PAIR_GENERATE_CONTEXT_SIZE \
  288. sizeof(nrf_crypto_backend_cc310_key_pair_generate_context_t)
  289. #define NRF_CRYPTO_BACKEND_SECP384R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  290. // Most CC310 curve types share the same data structures for keys
  291. typedef nrf_crypto_backend_cc310_ecc_private_key_t nrf_crypto_backend_secp384r1_private_key_t;
  292. typedef nrf_crypto_backend_cc310_ecc_public_key_t nrf_crypto_backend_secp384r1_public_key_t;
  293. // Most CC310 curve types share the same data structures for context
  294. typedef nrf_crypto_backend_cc310_key_pair_generate_context_t
  295. nrf_crypto_backend_secp384r1_key_pair_generate_context_t;
  296. // Dummy typedef for unused context
  297. typedef uint32_t nrf_crypto_backend_secp384r1_public_key_calculate_context_t;
  298. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP384R1)
  299. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP521R1)
  300. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP521R1)
  301. #error "More than one backend enabled for secp521r1 (NIST 521-bit).");
  302. #endif
  303. #define NRF_CRYPTO_ECC_SECP521R1_ENABLED 1
  304. // Aliases for one common CC310 implementation
  305. #define nrf_crypto_backend_secp521r1_key_pair_generate nrf_crypto_backend_cc310_key_pair_generate
  306. #define nrf_crypto_backend_secp521r1_public_key_calculate NULL
  307. #define nrf_crypto_backend_secp521r1_private_key_from_raw nrf_crypto_backend_cc310_private_key_from_raw
  308. #define nrf_crypto_backend_secp521r1_private_key_to_raw nrf_crypto_backend_cc310_private_key_to_raw
  309. #define nrf_crypto_backend_secp521r1_public_key_from_raw nrf_crypto_backend_cc310_public_key_from_raw
  310. #define nrf_crypto_backend_secp521r1_public_key_to_raw nrf_crypto_backend_cc310_public_key_to_raw
  311. #define nrf_crypto_backend_secp521r1_private_key_free NULL
  312. #define nrf_crypto_backend_secp521r1_public_key_free NULL
  313. // Context sizes required by CC310
  314. #define NRF_CRYPTO_BACKEND_SECP521R1_KEY_PAIR_GENERATE_CONTEXT_SIZE \
  315. sizeof(nrf_crypto_backend_cc310_key_pair_generate_context_t)
  316. #define NRF_CRYPTO_BACKEND_SECP521R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  317. // Most CC310 curve types share the same data structures for keys
  318. typedef nrf_crypto_backend_cc310_ecc_private_key_t nrf_crypto_backend_secp521r1_private_key_t;
  319. typedef nrf_crypto_backend_cc310_ecc_public_key_t nrf_crypto_backend_secp521r1_public_key_t;
  320. // Most CC310 curve types share the same data structures for context
  321. typedef nrf_crypto_backend_cc310_key_pair_generate_context_t
  322. nrf_crypto_backend_secp521r1_key_pair_generate_context_t;
  323. // Dummy typedef for unused context
  324. typedef uint32_t nrf_crypto_backend_secp521r1_public_key_calculate_context_t;
  325. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP521R1)
  326. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP160K1)
  327. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP160K1)
  328. #error "More than one backend enabled for secp160k1 (Koblitz 160-bit).");
  329. #endif
  330. #define NRF_CRYPTO_ECC_SECP160K1_ENABLED 1
  331. // Aliases for one common CC310 implementation
  332. #define nrf_crypto_backend_secp160k1_key_pair_generate nrf_crypto_backend_cc310_key_pair_generate
  333. #define nrf_crypto_backend_secp160k1_public_key_calculate NULL
  334. #define nrf_crypto_backend_secp160k1_private_key_from_raw nrf_crypto_backend_cc310_private_key_from_raw
  335. #define nrf_crypto_backend_secp160k1_private_key_to_raw nrf_crypto_backend_cc310_private_key_to_raw
  336. #define nrf_crypto_backend_secp160k1_public_key_from_raw nrf_crypto_backend_cc310_public_key_from_raw
  337. #define nrf_crypto_backend_secp160k1_public_key_to_raw nrf_crypto_backend_cc310_public_key_to_raw
  338. #define nrf_crypto_backend_secp160k1_private_key_free NULL
  339. #define nrf_crypto_backend_secp160k1_public_key_free NULL
  340. // Context sizes required by CC310
  341. #define NRF_CRYPTO_BACKEND_SECP160K1_KEY_PAIR_GENERATE_CONTEXT_SIZE \
  342. sizeof(nrf_crypto_backend_cc310_key_pair_generate_context_t)
  343. #define NRF_CRYPTO_BACKEND_SECP160K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  344. // Most CC310 curve types share the same data structures for keys
  345. typedef nrf_crypto_backend_cc310_ecc_private_key_t nrf_crypto_backend_secp160k1_private_key_t;
  346. typedef nrf_crypto_backend_cc310_ecc_public_key_t nrf_crypto_backend_secp160k1_public_key_t;
  347. // Most CC310 curve types share the same data structures for context
  348. typedef nrf_crypto_backend_cc310_key_pair_generate_context_t
  349. nrf_crypto_backend_secp160k1_key_pair_generate_context_t;
  350. // Dummy typedef for unused context
  351. typedef uint32_t nrf_crypto_backend_secp160k1_public_key_calculate_context_t;
  352. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP160K1)
  353. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP192K1)
  354. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP192K1)
  355. #error "More than one backend enabled for secp192k1 (Koblitz 192-bit).");
  356. #endif
  357. #define NRF_CRYPTO_ECC_SECP192K1_ENABLED 1
  358. // Aliases for one common CC310 implementation
  359. #define nrf_crypto_backend_secp192k1_key_pair_generate nrf_crypto_backend_cc310_key_pair_generate
  360. #define nrf_crypto_backend_secp192k1_public_key_calculate NULL
  361. #define nrf_crypto_backend_secp192k1_private_key_from_raw nrf_crypto_backend_cc310_private_key_from_raw
  362. #define nrf_crypto_backend_secp192k1_private_key_to_raw nrf_crypto_backend_cc310_private_key_to_raw
  363. #define nrf_crypto_backend_secp192k1_public_key_from_raw nrf_crypto_backend_cc310_public_key_from_raw
  364. #define nrf_crypto_backend_secp192k1_public_key_to_raw nrf_crypto_backend_cc310_public_key_to_raw
  365. #define nrf_crypto_backend_secp192k1_private_key_free NULL
  366. #define nrf_crypto_backend_secp192k1_public_key_free NULL
  367. // Context sizes required by CC310
  368. #define NRF_CRYPTO_BACKEND_SECP192K1_KEY_PAIR_GENERATE_CONTEXT_SIZE \
  369. sizeof(nrf_crypto_backend_cc310_key_pair_generate_context_t)
  370. #define NRF_CRYPTO_BACKEND_SECP192K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  371. // Most CC310 curve types share the same data structures for keys
  372. typedef nrf_crypto_backend_cc310_ecc_private_key_t nrf_crypto_backend_secp192k1_private_key_t;
  373. typedef nrf_crypto_backend_cc310_ecc_public_key_t nrf_crypto_backend_secp192k1_public_key_t;
  374. // Most CC310 curve types share the same data structures for context
  375. typedef nrf_crypto_backend_cc310_key_pair_generate_context_t
  376. nrf_crypto_backend_secp192k1_key_pair_generate_context_t;
  377. // Dummy typedef for unused context
  378. typedef uint32_t nrf_crypto_backend_secp192k1_public_key_calculate_context_t;
  379. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP192K1)
  380. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP224K1)
  381. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP224K1)
  382. #error "More than one backend enabled for secp224k1 (Koblitz 224-bit).");
  383. #endif
  384. #define NRF_CRYPTO_ECC_SECP224K1_ENABLED 1
  385. // Aliases for one common CC310 implementation
  386. #define nrf_crypto_backend_secp224k1_key_pair_generate nrf_crypto_backend_cc310_key_pair_generate
  387. #define nrf_crypto_backend_secp224k1_public_key_calculate NULL
  388. #define nrf_crypto_backend_secp224k1_private_key_from_raw nrf_crypto_backend_cc310_private_key_from_raw
  389. #define nrf_crypto_backend_secp224k1_private_key_to_raw nrf_crypto_backend_cc310_private_key_to_raw
  390. #define nrf_crypto_backend_secp224k1_public_key_from_raw nrf_crypto_backend_cc310_public_key_from_raw
  391. #define nrf_crypto_backend_secp224k1_public_key_to_raw nrf_crypto_backend_cc310_public_key_to_raw
  392. #define nrf_crypto_backend_secp224k1_private_key_free NULL
  393. #define nrf_crypto_backend_secp224k1_public_key_free NULL
  394. // Context sizes required by CC310
  395. #define NRF_CRYPTO_BACKEND_SECP224K1_KEY_PAIR_GENERATE_CONTEXT_SIZE \
  396. sizeof(nrf_crypto_backend_cc310_key_pair_generate_context_t)
  397. #define NRF_CRYPTO_BACKEND_SECP224K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  398. // Most CC310 curve types share the same data structures for keys
  399. typedef nrf_crypto_backend_cc310_ecc_private_key_t nrf_crypto_backend_secp224k1_private_key_t;
  400. typedef nrf_crypto_backend_cc310_ecc_public_key_t nrf_crypto_backend_secp224k1_public_key_t;
  401. // Most CC310 curve types share the same data structures for context
  402. typedef nrf_crypto_backend_cc310_key_pair_generate_context_t
  403. nrf_crypto_backend_secp224k1_key_pair_generate_context_t;
  404. // Dummy typedef for unused context
  405. typedef uint32_t nrf_crypto_backend_secp224k1_public_key_calculate_context_t;
  406. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP224K1)
  407. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP256K1)
  408. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP256K1)
  409. #error "More than one backend enabled for secp256k1 (Koblitz 256-bit).");
  410. #endif
  411. #define NRF_CRYPTO_ECC_SECP256K1_ENABLED 1
  412. // Aliases for one common CC310 implementation
  413. #define nrf_crypto_backend_secp256k1_key_pair_generate nrf_crypto_backend_cc310_key_pair_generate
  414. #define nrf_crypto_backend_secp256k1_public_key_calculate NULL
  415. #define nrf_crypto_backend_secp256k1_private_key_from_raw nrf_crypto_backend_cc310_private_key_from_raw
  416. #define nrf_crypto_backend_secp256k1_private_key_to_raw nrf_crypto_backend_cc310_private_key_to_raw
  417. #define nrf_crypto_backend_secp256k1_public_key_from_raw nrf_crypto_backend_cc310_public_key_from_raw
  418. #define nrf_crypto_backend_secp256k1_public_key_to_raw nrf_crypto_backend_cc310_public_key_to_raw
  419. #define nrf_crypto_backend_secp256k1_private_key_free NULL
  420. #define nrf_crypto_backend_secp256k1_public_key_free NULL
  421. // Context sizes required by CC310
  422. #define NRF_CRYPTO_BACKEND_SECP256K1_KEY_PAIR_GENERATE_CONTEXT_SIZE \
  423. sizeof(nrf_crypto_backend_cc310_key_pair_generate_context_t)
  424. #define NRF_CRYPTO_BACKEND_SECP256K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  425. // Most CC310 curve types share the same data structures for keys
  426. typedef nrf_crypto_backend_cc310_ecc_private_key_t nrf_crypto_backend_secp256k1_private_key_t;
  427. typedef nrf_crypto_backend_cc310_ecc_public_key_t nrf_crypto_backend_secp256k1_public_key_t;
  428. // Most CC310 curve types share the same data structures for context
  429. typedef nrf_crypto_backend_cc310_key_pair_generate_context_t
  430. nrf_crypto_backend_secp256k1_key_pair_generate_context_t;
  431. // Dummy typedef for unused context
  432. typedef uint32_t nrf_crypto_backend_secp256k1_public_key_calculate_context_t;
  433. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_SECP256K1)
  434. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_CURVE25519)
  435. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_CURVE25519)
  436. #error "More than one backend enabled for Curve25519.");
  437. #endif
  438. #define NRF_CRYPTO_ECC_CURVE25519_ENABLED 1
  439. /** @internal @brief Common structure holding context for Curve25519 (all operations).
  440. */
  441. typedef struct
  442. {
  443. CRYS_ECMONT_TempBuff_t temp_data; /**< @internal @brief Temporary buffer for CC310 internal storage */
  444. } nrf_crypto_backend_cc310_curve25519_context_t;
  445. /** @internal @brief Structure holding keys for Curve25519.
  446. */
  447. typedef struct
  448. {
  449. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header. */
  450. uint8_t key[32]; /**< @internal @brief Raw key in little-endian order. */
  451. } nrf_crypto_backend_curve25519_key_t;
  452. // Most CC310 curve types share the same data structures for keys
  453. typedef nrf_crypto_backend_curve25519_key_t nrf_crypto_backend_curve25519_private_key_t;
  454. typedef nrf_crypto_backend_curve25519_key_t nrf_crypto_backend_curve25519_public_key_t;
  455. /** @internal See @ref nrf_crypto_backend_ecc_key_pair_generate_fn_t.
  456. */
  457. ret_code_t nrf_crypto_backend_cc310_curve25519_key_pair_generate(
  458. void * p_context,
  459. void * p_private_key,
  460. void * p_public_key);
  461. ret_code_t nrf_crypto_backend_cc310_curve25519_key_from_raw(
  462. void * p_key,
  463. uint8_t const * p_raw_data);
  464. ret_code_t nrf_crypto_backend_cc310_curve25519_key_to_raw(
  465. void const * p_key,
  466. uint8_t * p_raw_data);
  467. // Aliases for Curve25519-specific CC310 implementation.
  468. #define nrf_crypto_backend_curve25519_key_pair_generate \
  469. nrf_crypto_backend_cc310_curve25519_key_pair_generate
  470. #define nrf_crypto_backend_curve25519_private_key_from_raw \
  471. nrf_crypto_backend_cc310_curve25519_key_from_raw
  472. #define nrf_crypto_backend_curve25519_private_key_to_raw \
  473. nrf_crypto_backend_cc310_curve25519_key_to_raw
  474. #define nrf_crypto_backend_curve25519_public_key_from_raw \
  475. nrf_crypto_backend_cc310_curve25519_key_from_raw
  476. #define nrf_crypto_backend_curve25519_public_key_to_raw \
  477. nrf_crypto_backend_cc310_curve25519_key_to_raw
  478. // Aliases for unused or unimplemented functions.
  479. #define nrf_crypto_backend_curve25519_public_key_calculate NULL
  480. #define nrf_crypto_backend_curve25519_private_key_free NULL
  481. #define nrf_crypto_backend_curve25519_public_key_free NULL
  482. // Context sizes required by CC310 Curve25519.
  483. #define NRF_CRYPTO_BACKEND_CURVE25519_KEY_PAIR_GENERATE_CONTEXT_SIZE \
  484. sizeof(nrf_crypto_backend_cc310_curve25519_context_t)
  485. #define NRF_CRYPTO_BACKEND_CURVE25519_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  486. // All CC310 Curve25519 operations share the same data structures for context.
  487. typedef nrf_crypto_backend_cc310_curve25519_context_t
  488. nrf_crypto_backend_curve25519_key_pair_generate_context_t;
  489. // Dummy typedef for unused context.
  490. typedef nrf_crypto_backend_cc310_curve25519_context_t
  491. nrf_crypto_backend_curve25519_public_key_calculate_context_t;
  492. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_CURVE25519)
  493. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_ED25519)
  494. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_ED25519)
  495. #error "More than one backend enabled for Ed25519.");
  496. #endif
  497. #define NRF_CRYPTO_ECC_ED25519_ENABLED 1
  498. /** @internal @brief Common structure holding context for Ed25519 (all operations).
  499. */
  500. typedef struct
  501. {
  502. CRYS_ECEDW_TempBuff_t temp_data; /**< @internal @brief Temporary buffer for CC310 internal storage. */
  503. } nrf_crypto_backend_cc310_ed25519_context_t;
  504. /** @internal @brief Structure holding private key for Ed25519.
  505. */
  506. typedef struct
  507. {
  508. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header. */
  509. uint8_t key[2 * CRYS_ECEDW_ORD_SIZE_IN_BYTES]; /**< @internal @brief Raw private key (seed || pubKey) in little-endian order. */
  510. } nrf_crypto_backend_ed25519_private_key_t;
  511. /** @internal @brief Structure holding public key for Ed25519.
  512. */
  513. typedef struct
  514. {
  515. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header. */
  516. uint8_t key[CRYS_ECEDW_ORD_SIZE_IN_BYTES]; /**< @internal @brief Raw public key in little-endian order. */
  517. } nrf_crypto_backend_ed25519_public_key_t;
  518. /** @internal See @ref nrf_crypto_backend_ecc_key_pair_generate_fn_t.
  519. */
  520. ret_code_t nrf_crypto_backend_cc310_ed25519_key_pair_generate(
  521. void * p_context,
  522. void * p_private_key,
  523. void * p_public_key);
  524. ret_code_t nrf_crypto_backend_cc310_ed25519_private_key_from_raw(
  525. void * p_key,
  526. uint8_t const * p_raw_data);
  527. ret_code_t nrf_crypto_backend_cc310_ed25519_private_key_to_raw(
  528. void const * p_key,
  529. uint8_t * p_raw_data);
  530. ret_code_t nrf_crypto_backend_cc310_ed25519_public_key_from_raw(
  531. void * p_key,
  532. uint8_t const * p_raw_data);
  533. ret_code_t nrf_crypto_backend_cc310_ed25519_public_key_to_raw(
  534. void const * p_key,
  535. uint8_t * p_raw_data);
  536. ret_code_t nrf_crypto_backend_cc310_ed25519_public_key_calculate(
  537. void * p_context,
  538. void const * p_private_key,
  539. void * p_public_key);
  540. // Aliases for Ed25519-specific CC310 implementation.
  541. #define nrf_crypto_backend_ed25519_key_pair_generate \
  542. nrf_crypto_backend_cc310_ed25519_key_pair_generate
  543. #define nrf_crypto_backend_ed25519_private_key_from_raw \
  544. nrf_crypto_backend_cc310_ed25519_private_key_from_raw
  545. #define nrf_crypto_backend_ed25519_private_key_to_raw \
  546. nrf_crypto_backend_cc310_ed25519_private_key_to_raw
  547. #define nrf_crypto_backend_ed25519_public_key_from_raw \
  548. nrf_crypto_backend_cc310_ed25519_public_key_from_raw
  549. #define nrf_crypto_backend_ed25519_public_key_to_raw \
  550. nrf_crypto_backend_cc310_ed25519_public_key_to_raw
  551. #define nrf_crypto_backend_ed25519_public_key_calculate \
  552. nrf_crypto_backend_cc310_ed25519_public_key_calculate
  553. // Aliases for unused or unimplemented functions.
  554. #define nrf_crypto_backend_ed25519_private_key_free NULL
  555. #define nrf_crypto_backend_ed25519_public_key_free NULL
  556. // Context sizes required by CC310 Ed25519
  557. #define NRF_CRYPTO_BACKEND_ED25519_KEY_PAIR_GENERATE_CONTEXT_SIZE \
  558. sizeof(nrf_crypto_backend_cc310_ed25519_context_t)
  559. #define NRF_CRYPTO_BACKEND_ED25519_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  560. // All CC310 Ed25519 operations share the same data structures for context.
  561. typedef nrf_crypto_backend_cc310_ed25519_context_t
  562. nrf_crypto_backend_ed25519_key_pair_generate_context_t;
  563. // Dummy typedef for unused context.
  564. typedef nrf_crypto_backend_cc310_ed25519_context_t
  565. nrf_crypto_backend_ed25519_public_key_calculate_context_t;
  566. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_ECC_ED25519)
  567. #ifdef __cplusplus
  568. }
  569. #endif
  570. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310)
  571. #endif // CC310_BACKEND_ECC_H__