oberon_backend_ecc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 OBERON_BACKEND_ECC_H__
  41. #define OBERON_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_OBERON)
  45. #include <stdint.h>
  46. #include <stdbool.h>
  47. #include "nrf_crypto_ecc_shared.h"
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51. /** @internal See @ref nrf_crypto_backend_ecc_private_key_from_raw_fn_t.
  52. */
  53. ret_code_t nrf_crypto_backend_oberon_private_key_from_raw(
  54. void * p_private_key,
  55. uint8_t const * p_raw_data);
  56. /** @internal See @ref nrf_crypto_backend_ecc_private_key_to_raw_fn_t.
  57. */
  58. ret_code_t nrf_crypto_backend_oberon_private_key_to_raw(
  59. void const * p_private_key,
  60. uint8_t * p_raw_data);
  61. /** @internal See @ref nrf_crypto_backend_ecc_public_key_from_raw_fn_t.
  62. */
  63. ret_code_t nrf_crypto_backend_oberon_public_key_from_raw(
  64. void * p_public_key,
  65. uint8_t const * p_raw_data);
  66. /** @internal See @ref nrf_crypto_backend_ecc_public_key_to_raw_fn_t.
  67. */
  68. ret_code_t nrf_crypto_backend_oberon_public_key_to_raw(
  69. void const * p_public_key,
  70. uint8_t * p_raw_data);
  71. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_OBERON_ECC_SECP256R1)
  72. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_SECP256R1)
  73. #error "More than one backend enabled for secp256r1 (NIST 256-bit).");
  74. #endif
  75. #define NRF_CRYPTO_ECC_SECP256R1_ENABLED 1
  76. /** @internal @brief Generates random number that can be used as a private key for secp256r1.
  77. *
  78. * It uses RNG from libary frontend to generate random numbers.
  79. *
  80. * @param[out] data Array where generated random number will be placed.
  81. * @returns NRF_SUCCESS or error code passed from RNG frontend.
  82. */
  83. ret_code_t nrf_crypto_backend_oberon_ecc_secp256r1_rng(uint8_t data[32]);
  84. /** @internal @brief Structure holding private key for Oberon's secp256r1.
  85. */
  86. typedef struct
  87. {
  88. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header. */
  89. uint8_t key[32]; /**< @internal @brief Raw key. */
  90. } nrf_crypto_backend_secp256r1_private_key_t;
  91. /** @internal @brief Structure holding public key for Oberon's secp256r1.
  92. */
  93. typedef struct
  94. {
  95. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header. */
  96. uint8_t key[64]; /**< @internal @brief Raw key. */
  97. } nrf_crypto_backend_secp256r1_public_key_t;
  98. /** @internal See @ref nrf_crypto_backend_ecc_key_pair_generate_fn_t.
  99. */
  100. ret_code_t nrf_crypto_backend_secp256r1_key_pair_generate(
  101. void * p_context,
  102. void * p_private_key,
  103. void * p_public_key);
  104. /** @internal See @ref nrf_crypto_backend_ecc_public_key_calculate_fn_t.
  105. */
  106. ret_code_t nrf_crypto_backend_secp256r1_public_key_calculate(
  107. void * p_context,
  108. void const * p_private_key,
  109. void * p_public_key);
  110. // Common key conversion functions
  111. #define nrf_crypto_backend_secp256r1_private_key_from_raw \
  112. nrf_crypto_backend_oberon_private_key_from_raw
  113. #define nrf_crypto_backend_secp256r1_private_key_to_raw \
  114. nrf_crypto_backend_oberon_private_key_to_raw
  115. #define nrf_crypto_backend_secp256r1_public_key_from_raw \
  116. nrf_crypto_backend_oberon_public_key_from_raw
  117. #define nrf_crypto_backend_secp256r1_public_key_to_raw \
  118. nrf_crypto_backend_oberon_public_key_to_raw
  119. // Free is not required for oberon keys
  120. #define nrf_crypto_backend_secp256r1_private_key_free NULL
  121. #define nrf_crypto_backend_secp256r1_public_key_free NULL
  122. // Context is not used in oberon functions
  123. #define NRF_CRYPTO_BACKEND_SECP256R1_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  124. #define NRF_CRYPTO_BACKEND_SECP256R1_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  125. // Dummy typedef for unused context
  126. typedef uint32_t nrf_crypto_backend_secp256r1_key_pair_generate_context_t;
  127. typedef uint32_t nrf_crypto_backend_secp256r1_public_key_calculate_context_t;
  128. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_OBERON_ECC_SECP256R1)
  129. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_OBERON_ECC_CURVE25519)
  130. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_CURVE25519)
  131. #error "More than one backend enabled for Curve25519.");
  132. #endif
  133. #define NRF_CRYPTO_ECC_CURVE25519_ENABLED 1
  134. /** @internal @brief Structure holding private key for Oberon's Curve25519.
  135. */
  136. typedef struct
  137. {
  138. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header. */
  139. uint8_t key[32]; /**< @internal @brief Raw key in little endian order. */
  140. } nrf_crypto_backend_curve25519_private_key_t;
  141. /** @internal @brief Structure holding public key for Oberon's Curve25519.
  142. */
  143. typedef struct
  144. {
  145. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header. */
  146. uint8_t key[32]; /**< @internal @brief Raw key in little endian order. */
  147. } nrf_crypto_backend_curve25519_public_key_t;
  148. /** @internal See @ref nrf_crypto_backend_ecc_key_pair_generate_fn_t.
  149. */
  150. ret_code_t nrf_crypto_backend_curve25519_key_pair_generate(
  151. void * p_context,
  152. void * p_private_key,
  153. void * p_public_key);
  154. /** @internal See @ref nrf_crypto_backend_ecc_public_key_calculate_fn_t.
  155. */
  156. ret_code_t nrf_crypto_backend_curve25519_public_key_calculate(
  157. void * p_context,
  158. void const * p_private_key,
  159. void * p_public_key);
  160. // Common key conversion functions
  161. #define nrf_crypto_backend_curve25519_private_key_from_raw \
  162. nrf_crypto_backend_oberon_private_key_from_raw
  163. #define nrf_crypto_backend_curve25519_private_key_to_raw \
  164. nrf_crypto_backend_oberon_private_key_to_raw
  165. #define nrf_crypto_backend_curve25519_public_key_from_raw \
  166. nrf_crypto_backend_oberon_public_key_from_raw
  167. #define nrf_crypto_backend_curve25519_public_key_to_raw \
  168. nrf_crypto_backend_oberon_public_key_to_raw
  169. // Free is not required for oberon keys
  170. #define nrf_crypto_backend_curve25519_private_key_free NULL
  171. #define nrf_crypto_backend_curve25519_public_key_free NULL
  172. // Context is not used in oberon functions
  173. #define NRF_CRYPTO_BACKEND_CURVE25519_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  174. #define NRF_CRYPTO_BACKEND_CURVE25519_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  175. // Dummy typedef for unused context
  176. typedef uint32_t nrf_crypto_backend_curve25519_key_pair_generate_context_t;
  177. typedef uint32_t nrf_crypto_backend_curve25519_public_key_calculate_context_t;
  178. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_OBERON_ECC_CURVE25519)
  179. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_OBERON_ECC_ED25519)
  180. #if NRF_MODULE_ENABLED(NRF_CRYPTO_ECC_ED25519)
  181. #error "More than one backend enabled for Ed25519.");
  182. #endif
  183. #define NRF_CRYPTO_ECC_ED25519_ENABLED 1
  184. /** @internal @brief Structure holding private key for Oberon's Ed25519.
  185. */
  186. typedef struct
  187. {
  188. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header. */
  189. uint8_t private_part[32]; /**< @internal @brief Raw private key. */
  190. uint8_t public_part[32]; /**< @internal @brief Raw public key. It is also required for Ed25519 signing. */
  191. } nrf_crypto_backend_ed25519_private_key_t;
  192. /** @internal @brief Structure holding private key for Oberon's Ed25519.
  193. */
  194. typedef struct
  195. {
  196. nrf_crypto_internal_ecc_key_header_t header; /**< @internal @brief Common ECC key header. */
  197. uint8_t key[32]; /**< @internal @brief Raw key. */
  198. } nrf_crypto_backend_ed25519_public_key_t;
  199. /** @internal See @ref nrf_crypto_backend_ecc_private_key_from_raw_fn_t.
  200. */
  201. ret_code_t nrf_crypto_backend_ed25519_private_key_from_raw(
  202. void * p_private_key,
  203. uint8_t const * p_raw_data);
  204. /** @internal See @ref nrf_crypto_backend_ecc_key_pair_generate_fn_t.
  205. */
  206. ret_code_t nrf_crypto_backend_ed25519_key_pair_generate(
  207. void * p_context,
  208. void * p_private_key,
  209. void * p_public_key);
  210. /** @internal See @ref nrf_crypto_backend_ecc_public_key_calculate_fn_t.
  211. */
  212. ret_code_t nrf_crypto_backend_ed25519_public_key_calculate(
  213. void * p_context,
  214. void const * p_private_key,
  215. void * p_public_key);
  216. // Common key conversion functions
  217. #define nrf_crypto_backend_ed25519_private_key_to_raw \
  218. nrf_crypto_backend_oberon_private_key_to_raw
  219. #define nrf_crypto_backend_ed25519_public_key_from_raw \
  220. nrf_crypto_backend_oberon_public_key_from_raw
  221. #define nrf_crypto_backend_ed25519_public_key_to_raw \
  222. nrf_crypto_backend_oberon_public_key_to_raw
  223. // Free is not required for oberon keys
  224. #define nrf_crypto_backend_ed25519_private_key_free NULL
  225. #define nrf_crypto_backend_ed25519_public_key_free NULL
  226. // Context is not used in oberon functions
  227. #define NRF_CRYPTO_BACKEND_ED25519_KEY_PAIR_GENERATE_CONTEXT_SIZE 0
  228. #define NRF_CRYPTO_BACKEND_ED25519_PUBLIC_KEY_CALCULATE_CONTEXT_SIZE 0
  229. // Dummy typedef for unused context
  230. typedef uint32_t nrf_crypto_backend_ed25519_key_pair_generate_context_t;
  231. typedef uint32_t nrf_crypto_backend_ed25519_public_key_calculate_context_t;
  232. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_OBERON_ECC_ED25519)
  233. #ifdef __cplusplus
  234. }
  235. #endif
  236. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_OBERON)
  237. #endif // OBERON_BACKEND_ECC_H__