optiga_backend_ecc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. * Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_config.h"
  41. #include "nordic_common.h"
  42. #if NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_OPTIGA)
  43. #include <stdbool.h>
  44. #include <string.h>
  45. #include "nrf_crypto_ecc.h"
  46. #include "nrf_crypto_ecdh.h"
  47. #include "nrf_crypto_mem.h"
  48. #include "nrf_crypto_rng.h"
  49. #include "nrf_crypto_shared.h"
  50. #include "nrf_assert.h"
  51. #include "optiga_backend_ecc.h"
  52. /*lint -save -e????*/
  53. #include "optiga/optiga_crypt.h"
  54. /*lint -restore*/
  55. int nrf_crypto_backend_optiga_ecc_optiga_rng(void * p_param, unsigned char * p_data, size_t size)
  56. {
  57. #if NRF_MODULE_ENABLED(NRF_CRYPTO_RNG)
  58. return NRF_SUCCESS;
  59. #else
  60. return NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE;
  61. #endif
  62. }
  63. static const uint8_t der_pub_key_header[] = {
  64. 0x03, // ASN.1 BITSTRING
  65. 0x42, // bytes following
  66. 0x00, // no unused bits
  67. 0x04 // uncompressed key, see https://tools.ietf.org/html/rfc5480#section-2.2
  68. };
  69. #define DER_PUB_KEY_HEADER_LEN (sizeof(der_pub_key_header)/sizeof(der_pub_key_header[0]))
  70. // for our purposes we always have 1 byte tag + 1 byte length
  71. #define DER_OCTET_STRING_HEADER_LEN 2
  72. // lengths for the ASN.1 DER encoded keys imported and exported by OPTIGA
  73. #define OPTIGA_SECP256R1_PRIV_KEY_LEN (DER_OCTET_STRING_HEADER_LEN + NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE)
  74. #define OPTIGA_SECP256R1_PUBL_KEY_LEN (DER_PUB_KEY_HEADER_LEN + NRF_CRYPTO_ECC_SECP256R1_RAW_PUBLIC_KEY_SIZE)
  75. ret_code_t nrf_crypto_backend_optiga_key_pair_generate(
  76. void * p_context,
  77. void * p_private_key,
  78. void * p_public_key)
  79. {
  80. optiga_lib_status_t res = OPTIGA_LIB_ERROR;
  81. nrf_crypto_backend_secp256r1_public_key_t * p_pub =
  82. (nrf_crypto_backend_secp256r1_public_key_t *) p_public_key;
  83. nrf_crypto_backend_secp256r1_private_key_t * p_priv =
  84. (nrf_crypto_backend_secp256r1_private_key_t *) p_private_key;
  85. bool export_private_key;
  86. if (p_priv->oid == 0)
  87. {
  88. // OID=0 was implicitly specified when initializising, mostly due to Nordic internal code calling our API
  89. p_priv->oid = (optiga_key_id_t)0xE100;
  90. export_private_key = false;
  91. }
  92. else if (p_priv->oid == NRF_CRYPTO_INFINEON_PRIVKEY_HOST_OID)
  93. {
  94. export_private_key = true;
  95. }
  96. else // any other value for OID, we assume the OID was purposefully specified by caller
  97. {
  98. export_private_key = false;
  99. }
  100. void * priv_key;
  101. if (export_private_key)
  102. {
  103. //lint -save -e611 -e545 (Suspicious cast, Suspicious use of &)
  104. priv_key = (void*) &p_priv->raw_privkey;
  105. //lint -restore
  106. p_priv->oid = (optiga_key_id_t)NRF_CRYPTO_INFINEON_PRIVKEY_HOST_OID;
  107. }
  108. else
  109. {
  110. //lint -save -e611 -e545 (Suspicious cast, Suspicious use of &)
  111. priv_key = (void*) &p_priv->oid;
  112. memset(p_priv->raw_privkey, 0, OPTIGA_SECP256R1_PRIV_KEY_LEN);
  113. //lint -restore
  114. }
  115. // Set all flags because the nrf_crypto API does not allow to specify key use
  116. const optiga_key_usage_t key_usage = (optiga_key_usage_t)(OPTIGA_KEY_USAGE_AUTHENTICATION |
  117. OPTIGA_KEY_USAGE_SIGN |
  118. OPTIGA_KEY_USAGE_KEY_AGREEMENT);
  119. uint16_t publ_key_len = OPTIGA_SECP256R1_PUBL_KEY_LEN;
  120. res = optiga_crypt_ecc_generate_keypair(OPTIGA_ECC_NIST_P_256,
  121. key_usage,
  122. export_private_key,
  123. priv_key,
  124. p_pub->raw_pubkey,
  125. &publ_key_len);
  126. if(res != OPTIGA_LIB_SUCCESS)
  127. {
  128. // error in the optiga library
  129. return NRF_ERROR_CRYPTO_INTERNAL;
  130. }
  131. if(publ_key_len != OPTIGA_SECP256R1_PUBL_KEY_LEN)
  132. {
  133. // unexpected length
  134. return NRF_ERROR_CRYPTO_INTERNAL;
  135. }
  136. // mark the public key as stored in host memory
  137. p_pub->oid = (optiga_key_id_t)NRF_CRYPTO_INFINEON_PUBKEY_HOST_OID;
  138. return NRF_SUCCESS;
  139. }
  140. ret_code_t nrf_crypto_backend_optiga_public_key_calculate(
  141. void * p_context,
  142. void const * p_private_key,
  143. void * p_public_key)
  144. {
  145. return NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE;
  146. }
  147. ret_code_t nrf_crypto_backend_optiga_private_key_from_raw(
  148. void * p_private_key,
  149. uint8_t const * p_raw_data)
  150. {
  151. return NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE;
  152. }
  153. #define DER_TAG_OCTET_STRING 0x04
  154. ret_code_t nrf_crypto_backend_optiga_private_key_to_raw(
  155. void const * p_private_key,
  156. uint8_t * p_raw_data)
  157. {
  158. nrf_crypto_backend_secp256r1_private_key_t * p_priv =
  159. (nrf_crypto_backend_secp256r1_private_key_t *)p_private_key;
  160. nrf_crypto_ecc_curve_info_t const * p_info = p_priv->header.p_info;
  161. if(p_priv->oid != NRF_CRYPTO_INFINEON_PRIVKEY_HOST_OID)
  162. {
  163. // must use magic OID for private key exported to host
  164. return NRF_ERROR_CRYPTO_INTERNAL;
  165. }
  166. uint8_t* p_key = p_priv->raw_privkey;
  167. if(*p_key != DER_TAG_OCTET_STRING)
  168. {
  169. // private key must be encoded as DER OCTET STRING
  170. return NRF_ERROR_CRYPTO_INTERNAL;
  171. }
  172. p_key++;
  173. if(p_info == &g_nrf_crypto_ecc_secp256r1_curve_info)
  174. {
  175. if(*p_key != NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE)
  176. {
  177. // wrong length
  178. return NRF_ERROR_CRYPTO_INTERNAL;
  179. }
  180. p_key++;
  181. memcpy(p_raw_data, p_key, NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE);
  182. return NRF_SUCCESS;
  183. }
  184. return NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE;
  185. }
  186. ret_code_t nrf_crypto_backend_optiga_public_key_from_raw(
  187. void * p_public_key,
  188. uint8_t const * p_raw_data)
  189. {
  190. nrf_crypto_backend_secp256r1_public_key_t * p_pub =
  191. (nrf_crypto_backend_secp256r1_public_key_t *)p_public_key;
  192. nrf_crypto_ecc_curve_info_t const * p_info = p_pub->header.p_info;
  193. if (p_info == &g_nrf_crypto_ecc_secp256r1_curve_info)
  194. {
  195. // copy header
  196. memcpy(p_pub->raw_pubkey, der_pub_key_header, DER_PUB_KEY_HEADER_LEN);
  197. // copy public key data
  198. memcpy(p_pub->raw_pubkey + DER_PUB_KEY_HEADER_LEN, p_raw_data, NRF_CRYPTO_ECC_SECP256R1_RAW_PUBLIC_KEY_SIZE);
  199. // Set OID to magic number for host-supplied public key
  200. p_pub->oid = (optiga_key_id_t)NRF_CRYPTO_INFINEON_PUBKEY_HOST_OID;
  201. return NRF_SUCCESS;
  202. }
  203. return NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE;
  204. }
  205. ret_code_t nrf_crypto_backend_optiga_public_key_to_raw(
  206. void const * p_public_key,
  207. uint8_t * p_raw_data)
  208. {
  209. nrf_crypto_backend_secp256r1_public_key_t * p_pub =
  210. (nrf_crypto_backend_secp256r1_public_key_t *)p_public_key;
  211. nrf_crypto_ecc_curve_info_t const * p_info = p_pub->header.p_info;
  212. if(p_pub->oid != NRF_CRYPTO_INFINEON_PUBKEY_HOST_OID)
  213. {
  214. // must use magic OID for host supplied public key
  215. return NRF_ERROR_CRYPTO_INTERNAL;
  216. }
  217. if (p_info == &g_nrf_crypto_ecc_secp256r1_curve_info)
  218. {
  219. if(memcmp(p_pub->raw_pubkey, der_pub_key_header, DER_PUB_KEY_HEADER_LEN) != 0) {
  220. // public key not correctly encoded
  221. return NRF_ERROR_CRYPTO_INTERNAL;
  222. }
  223. memcpy(p_raw_data,
  224. p_pub->raw_pubkey + DER_PUB_KEY_HEADER_LEN,
  225. NRF_CRYPTO_ECC_SECP256R1_RAW_PUBLIC_KEY_SIZE);
  226. return NRF_SUCCESS;
  227. }
  228. return NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE;
  229. }
  230. ret_code_t nrf_crypto_backend_optiga_private_key_free(
  231. void * p_private_key)
  232. {
  233. return NRF_ERROR_CRYPTO_INTERNAL;
  234. }
  235. ret_code_t nrf_crypto_backend_optiga_public_key_free(
  236. void * p_public_key)
  237. {
  238. return NRF_ERROR_CRYPTO_INTERNAL;
  239. }
  240. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_OPTIGA_ECC_SECP256R1)
  241. const nrf_crypto_ecc_curve_info_t g_nrf_crypto_ecc_secp256r1_curve_info =
  242. {
  243. .public_key_size = sizeof(nrf_crypto_backend_secp256r1_public_key_t),
  244. .private_key_size = sizeof(nrf_crypto_backend_optiga_ecc_private_key_t),
  245. .curve_type = NRF_CRYPTO_ECC_SECP256R1_CURVE_TYPE,
  246. .raw_private_key_size = NRF_CRYPTO_ECC_SECP256R1_RAW_PRIVATE_KEY_SIZE,
  247. .raw_public_key_size = NRF_CRYPTO_ECC_SECP256R1_RAW_PUBLIC_KEY_SIZE,
  248. .p_backend_data = (void *)OPTIGA_ECC_NIST_P_256,
  249. };
  250. #endif
  251. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_OPTIGA)