micro_ecc_backend_ecc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 MICRO_ECC_BACKEND_ECC_H__
  41. #define MICRO_ECC_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_MICRO_ECC)
  45. #include <stdint.h>
  46. #include <stdbool.h>
  47. #include "nrf_crypto_ecc_shared.h"
  48. #include "uECC.h"
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. /** @internal See @ref nrf_crypto_backend_ecc_key_pair_generate_fn_t.
  53. */
  54. ret_code_t nrf_crypto_backend_micro_ecc_key_pair_generate(
  55. void * p_context,
  56. void * p_private_key,
  57. void * p_public_key);
  58. /** @internal See @ref nrf_crypto_backend_ecc_public_key_calculate_fn_t.
  59. */
  60. ret_code_t nrf_crypto_backend_micro_ecc_public_key_calculate(
  61. void * p_context,
  62. void const * p_private_key,
  63. void * p_public_key);
  64. /** @internal See @ref nrf_crypto_backend_ecc_private_key_from_raw_fn_t.
  65. */
  66. ret_code_t nrf_crypto_backend_micro_ecc_private_key_from_raw(
  67. void * p_private_key,
  68. uint8_t const * p_raw_data);
  69. /** @internal See @ref nrf_crypto_backend_ecc_private_key_to_raw_fn_t.
  70. */
  71. ret_code_t nrf_crypto_backend_micro_ecc_private_key_to_raw(
  72. void const * p_private_key,
  73. uint8_t * p_raw_data);
  74. /** @internal See @ref nrf_crypto_backend_ecc_public_key_from_raw_fn_t.
  75. */
  76. ret_code_t nrf_crypto_backend_micro_ecc_public_key_from_raw(
  77. void * p_public_key,
  78. uint8_t const * p_raw_data);
  79. /** @internal See @ref nrf_crypto_backend_ecc_public_key_to_raw_fn_t.
  80. */
  81. ret_code_t nrf_crypto_backend_micro_ecc_public_key_to_raw(
  82. void const * p_public_key,
  83. uint8_t * p_raw_data);
  84. /** @internal @brief Represents common uECC backend key structure.
  85. */
  86. typedef struct
  87. {
  88. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */
  89. uint32_t key[1]; /**< @internal @brief micro-ecc specific key representation */
  90. } nrf_crypto_backend_micro_ecc_common_key_t;
  91. /** @internal @brief Callback RNG function that can be provided to uECC API.
  92. * @param dest Destination buffer.
  93. * @param size Size of the buffer.
  94. * @return 1 on success, 0 on error.
  95. */
  96. int nrf_crypto_backend_micro_ecc_rng_callback(uint8_t * dest, unsigned size);
  97. /** @internal @brief Gets uECC type based on provided key.
  98. * @param p_key uECC backend key (public or private).
  99. * @return uECC specific value representing a curve.
  100. */
  101. uECC_Curve nrf_crypto_backend_micro_ecc_curve_get(
  102. nrf_crypto_backend_micro_ecc_common_key_t const * p_key);
  103. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP192R1)
  104. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP192R1)
  105. #error "More than one backend enabled for secp192r1 (NIST 192-bit).");
  106. #endif
  107. #define NRF_CRYPTO_ECC_SECP192R1_ENABLED 1
  108. /** @internal @brief Structure holding private key for secp192r1 (NIST 192-bit) in micro-ecc.
  109. */
  110. typedef struct
  111. {
  112. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */
  113. uint32_t key[192 / 32]; /**< @internal @brief micro-ecc specific key representation */
  114. } nrf_crypto_backend_secp192r1_private_key_t;
  115. /** @internal @brief Structure holding public key for secp192r1 (NIST 192-bit) in micro-ecc.
  116. */
  117. typedef struct
  118. {
  119. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */
  120. uint32_t key[2 * 192 / 32]; /**< @internal @brief micro-ecc specific key representation */
  121. } nrf_crypto_backend_secp192r1_public_key_t;
  122. // Aliases for one common micro-ecc implementation
  123. #define nrf_crypto_backend_secp192r1_key_pair_generate nrf_crypto_backend_micro_ecc_key_pair_generate
  124. #define nrf_crypto_backend_secp192r1_public_key_calculate nrf_crypto_backend_micro_ecc_public_key_calculate
  125. #define nrf_crypto_backend_secp192r1_private_key_from_raw nrf_crypto_backend_micro_ecc_private_key_from_raw
  126. #define nrf_crypto_backend_secp192r1_private_key_to_raw nrf_crypto_backend_micro_ecc_private_key_to_raw
  127. #define nrf_crypto_backend_secp192r1_public_key_from_raw nrf_crypto_backend_micro_ecc_public_key_from_raw
  128. #define nrf_crypto_backend_secp192r1_public_key_to_raw nrf_crypto_backend_micro_ecc_public_key_to_raw
  129. #define nrf_crypto_backend_secp192r1_private_key_free NULL
  130. #define nrf_crypto_backend_secp192r1_public_key_free NULL
  131. #define NRF_CRYPTO_BACKEND_SECP192R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  132. #define NRF_CRYPTO_BACKEND_SECP192R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  133. // Dummy typedef for unused context
  134. typedef uint32_t nrf_crypto_backend_secp192r1_key_pair_generate_context_t;
  135. typedef uint32_t nrf_crypto_backend_secp192r1_public_key_calculate_context_t;
  136. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP192R1)
  137. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP224R1)
  138. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP224R1)
  139. #error "More than one backend enabled for secp224r1 (NIST 224-bit).");
  140. #endif
  141. #define NRF_CRYPTO_ECC_SECP224R1_ENABLED 1
  142. /** @internal @brief Structure holding private key for secp224r1 (NIST 224-bit) in micro-ecc.
  143. */
  144. typedef struct
  145. {
  146. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */
  147. uint32_t key[224 / 32]; /**< @internal @brief micro-ecc specific key representation */
  148. } nrf_crypto_backend_secp224r1_private_key_t;
  149. /** @internal @brief Structure holding public key for secp224r1 (NIST 224-bit) in micro-ecc.
  150. */
  151. typedef struct
  152. {
  153. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */
  154. uint32_t key[2 * 224 / 32]; /**< @internal @brief micro-ecc specific key representation */
  155. } nrf_crypto_backend_secp224r1_public_key_t;
  156. // Aliases for one common micro-ecc implementation
  157. #define nrf_crypto_backend_secp224r1_key_pair_generate nrf_crypto_backend_micro_ecc_key_pair_generate
  158. #define nrf_crypto_backend_secp224r1_public_key_calculate nrf_crypto_backend_micro_ecc_public_key_calculate
  159. #define nrf_crypto_backend_secp224r1_private_key_from_raw nrf_crypto_backend_micro_ecc_private_key_from_raw
  160. #define nrf_crypto_backend_secp224r1_private_key_to_raw nrf_crypto_backend_micro_ecc_private_key_to_raw
  161. #define nrf_crypto_backend_secp224r1_public_key_from_raw nrf_crypto_backend_micro_ecc_public_key_from_raw
  162. #define nrf_crypto_backend_secp224r1_public_key_to_raw nrf_crypto_backend_micro_ecc_public_key_to_raw
  163. #define nrf_crypto_backend_secp224r1_private_key_free NULL
  164. #define nrf_crypto_backend_secp224r1_public_key_free NULL
  165. #define NRF_CRYPTO_BACKEND_SECP224R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  166. #define NRF_CRYPTO_BACKEND_SECP224R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  167. // Dummy typedef for unused context
  168. typedef uint32_t nrf_crypto_backend_secp224r1_key_pair_generate_context_t;
  169. typedef uint32_t nrf_crypto_backend_secp224r1_public_key_calculate_context_t;
  170. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP224R1)
  171. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256R1)
  172. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP256R1)
  173. #error "More than one backend enabled for secp256r1 (NIST 256-bit).");
  174. #endif
  175. #define NRF_CRYPTO_ECC_SECP256R1_ENABLED 1
  176. /** @internal @brief Structure holding private key for secp256r1 (NIST 256-bit) in micro-ecc.
  177. */
  178. typedef struct
  179. {
  180. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */
  181. uint32_t key[256 / 32]; /**< @internal @brief micro-ecc specific key representation */
  182. } nrf_crypto_backend_secp256r1_private_key_t;
  183. /** @internal @brief Structure holding public key for secp256r1 (NIST 256-bit) in micro-ecc.
  184. */
  185. typedef struct
  186. {
  187. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */
  188. uint32_t key[2 * 256 / 32]; /**< @internal @brief micro-ecc specific key representation */
  189. } nrf_crypto_backend_secp256r1_public_key_t;
  190. // Aliases for one common micro-ecc implementation
  191. #define nrf_crypto_backend_secp256r1_key_pair_generate nrf_crypto_backend_micro_ecc_key_pair_generate
  192. #define nrf_crypto_backend_secp256r1_public_key_calculate nrf_crypto_backend_micro_ecc_public_key_calculate
  193. #define nrf_crypto_backend_secp256r1_private_key_from_raw nrf_crypto_backend_micro_ecc_private_key_from_raw
  194. #define nrf_crypto_backend_secp256r1_private_key_to_raw nrf_crypto_backend_micro_ecc_private_key_to_raw
  195. #define nrf_crypto_backend_secp256r1_public_key_from_raw nrf_crypto_backend_micro_ecc_public_key_from_raw
  196. #define nrf_crypto_backend_secp256r1_public_key_to_raw nrf_crypto_backend_micro_ecc_public_key_to_raw
  197. #define nrf_crypto_backend_secp256r1_private_key_free NULL
  198. #define nrf_crypto_backend_secp256r1_public_key_free NULL
  199. #define NRF_CRYPTO_BACKEND_SECP256R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  200. #define NRF_CRYPTO_BACKEND_SECP256R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  201. // Dummy typedef for unused context
  202. typedef uint32_t nrf_crypto_backend_secp256r1_key_pair_generate_context_t;
  203. typedef uint32_t nrf_crypto_backend_secp256r1_public_key_calculate_context_t;
  204. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256R1)
  205. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256K1)
  206. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP256K1)
  207. #error "More than one backend enabled for secp256k1 (Koblitz 256-bit).");
  208. #endif
  209. #define NRF_CRYPTO_ECC_SECP256K1_ENABLED 1
  210. /** @internal @brief Structure holding private key for secp256k1 (Koblitz 256-bit) in micro-ecc.
  211. */
  212. typedef struct
  213. {
  214. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */
  215. uint32_t key[256 / 32]; /**< @internal @brief micro-ecc specific key representation */
  216. } nrf_crypto_backend_secp256k1_private_key_t;
  217. /** @internal @brief Structure holding public key for secp256k1 (Koblitz 256-bit) in micro-ecc.
  218. */
  219. typedef struct
  220. {
  221. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header */
  222. uint32_t key[2 * 256 / 32]; /**< @internal @brief micro-ecc specific key representation */
  223. } nrf_crypto_backend_secp256k1_public_key_t;
  224. // Aliases for one common micro-ecc implementation
  225. #define nrf_crypto_backend_secp256k1_key_pair_generate nrf_crypto_backend_micro_ecc_key_pair_generate
  226. #define nrf_crypto_backend_secp256k1_public_key_calculate nrf_crypto_backend_micro_ecc_public_key_calculate
  227. #define nrf_crypto_backend_secp256k1_private_key_from_raw nrf_crypto_backend_micro_ecc_private_key_from_raw
  228. #define nrf_crypto_backend_secp256k1_private_key_to_raw nrf_crypto_backend_micro_ecc_private_key_to_raw
  229. #define nrf_crypto_backend_secp256k1_public_key_from_raw nrf_crypto_backend_micro_ecc_public_key_from_raw
  230. #define nrf_crypto_backend_secp256k1_public_key_to_raw nrf_crypto_backend_micro_ecc_public_key_to_raw
  231. #define nrf_crypto_backend_secp256k1_private_key_free NULL
  232. #define nrf_crypto_backend_secp256k1_public_key_free NULL
  233. #define NRF_CRYPTO_BACKEND_SECP256K1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  234. #define NRF_CRYPTO_BACKEND_SECP256K1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  235. // Dummy typedef for unused context
  236. typedef uint32_t nrf_crypto_backend_secp256k1_key_pair_generate_context_t;
  237. typedef uint32_t nrf_crypto_backend_secp256k1_public_key_calculate_context_t;
  238. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256K1)
  239. #ifdef __cplusplus
  240. }
  241. #endif
  242. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MICRO_ECC)
  243. #endif // MICRO_ECC_BACKEND_ECC_H__