pk_ecc.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * ECC setters for PK.
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. #include "common.h"
  8. #include "mbedtls/pk.h"
  9. #include "mbedtls/error.h"
  10. #include "mbedtls/ecp.h"
  11. #include "pk_internal.h"
  12. #if defined(MBEDTLS_PK_C) && defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  13. int mbedtls_pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id)
  14. {
  15. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  16. size_t ec_bits;
  17. psa_ecc_family_t ec_family = mbedtls_ecc_group_to_psa(grp_id, &ec_bits);
  18. /* group may already be initialized; if so, make sure IDs match */
  19. if ((pk->ec_family != 0 && pk->ec_family != ec_family) ||
  20. (pk->ec_bits != 0 && pk->ec_bits != ec_bits)) {
  21. return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
  22. }
  23. /* set group */
  24. pk->ec_family = ec_family;
  25. pk->ec_bits = ec_bits;
  26. return 0;
  27. #else /* MBEDTLS_PK_USE_PSA_EC_DATA */
  28. mbedtls_ecp_keypair *ecp = mbedtls_pk_ec_rw(*pk);
  29. /* grp may already be initialized; if so, make sure IDs match */
  30. if (mbedtls_pk_ec_ro(*pk)->grp.id != MBEDTLS_ECP_DP_NONE &&
  31. mbedtls_pk_ec_ro(*pk)->grp.id != grp_id) {
  32. return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
  33. }
  34. /* set group */
  35. return mbedtls_ecp_group_load(&(ecp->grp), grp_id);
  36. #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
  37. }
  38. int mbedtls_pk_ecc_set_key(mbedtls_pk_context *pk, unsigned char *key, size_t key_len)
  39. {
  40. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  41. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  42. psa_key_usage_t flags;
  43. psa_status_t status;
  44. psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(pk->ec_family));
  45. if (pk->ec_family == PSA_ECC_FAMILY_MONTGOMERY) {
  46. /* Do not set algorithm here because Montgomery keys cannot do ECDSA and
  47. * the PK module cannot do ECDH. When the key will be used in TLS for
  48. * ECDH, it will be exported and then re-imported with proper flags
  49. * and algorithm. */
  50. flags = PSA_KEY_USAGE_EXPORT;
  51. } else {
  52. psa_set_key_algorithm(&attributes,
  53. MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(PSA_ALG_ANY_HASH));
  54. flags = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE |
  55. PSA_KEY_USAGE_EXPORT;
  56. }
  57. psa_set_key_usage_flags(&attributes, flags);
  58. status = psa_import_key(&attributes, key, key_len, &pk->priv_id);
  59. return psa_pk_status_to_mbedtls(status);
  60. #else /* MBEDTLS_PK_USE_PSA_EC_DATA */
  61. mbedtls_ecp_keypair *eck = mbedtls_pk_ec_rw(*pk);
  62. int ret = mbedtls_ecp_read_key(eck->grp.id, eck, key, key_len);
  63. if (ret != 0) {
  64. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret);
  65. }
  66. return 0;
  67. #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
  68. }
  69. int mbedtls_pk_ecc_set_pubkey_from_prv(mbedtls_pk_context *pk,
  70. const unsigned char *prv, size_t prv_len,
  71. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  72. {
  73. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  74. (void) f_rng;
  75. (void) p_rng;
  76. (void) prv;
  77. (void) prv_len;
  78. psa_status_t status;
  79. status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
  80. &pk->pub_raw_len);
  81. return psa_pk_status_to_mbedtls(status);
  82. #elif defined(MBEDTLS_USE_PSA_CRYPTO) /* && !MBEDTLS_PK_USE_PSA_EC_DATA */
  83. (void) f_rng;
  84. (void) p_rng;
  85. psa_status_t status;
  86. mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
  87. size_t curve_bits;
  88. psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(eck->grp.id, &curve_bits);
  89. /* Import private key into PSA, from serialized input */
  90. mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
  91. psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
  92. psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
  93. psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
  94. status = psa_import_key(&key_attr, prv, prv_len, &key_id);
  95. if (status != PSA_SUCCESS) {
  96. return psa_pk_status_to_mbedtls(status);
  97. }
  98. /* Export public key from PSA */
  99. unsigned char pub[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
  100. size_t pub_len;
  101. status = psa_export_public_key(key_id, pub, sizeof(pub), &pub_len);
  102. psa_status_t destruction_status = psa_destroy_key(key_id);
  103. if (status != PSA_SUCCESS) {
  104. return psa_pk_status_to_mbedtls(status);
  105. } else if (destruction_status != PSA_SUCCESS) {
  106. return psa_pk_status_to_mbedtls(destruction_status);
  107. }
  108. /* Load serialized public key into ecp_keypair structure */
  109. return mbedtls_ecp_point_read_binary(&eck->grp, &eck->Q, pub, pub_len);
  110. #else /* MBEDTLS_USE_PSA_CRYPTO */
  111. (void) prv;
  112. (void) prv_len;
  113. mbedtls_ecp_keypair *eck = (mbedtls_ecp_keypair *) pk->pk_ctx;
  114. return mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, f_rng, p_rng);
  115. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  116. }
  117. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  118. /*
  119. * Set the public key: fallback using ECP_LIGHT in the USE_PSA_EC_DATA case.
  120. *
  121. * Normally, when MBEDTLS_PK_USE_PSA_EC_DATA is enabled, we only use PSA
  122. * functions to handle keys. However, currently psa_import_key() does not
  123. * support compressed points. In case that support was explicitly requested,
  124. * this fallback uses ECP functions to get the job done. This is the reason
  125. * why MBEDTLS_PK_PARSE_EC_COMPRESSED auto-enables MBEDTLS_ECP_LIGHT.
  126. *
  127. * [in/out] pk: in: must have the group set, see mbedtls_pk_ecc_set_group().
  128. * out: will have the public key set.
  129. * [in] pub, pub_len: the public key as an ECPoint,
  130. * in any format supported by ECP.
  131. *
  132. * Return:
  133. * - 0 on success;
  134. * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
  135. * but not supported;
  136. * - another error code otherwise.
  137. */
  138. static int pk_ecc_set_pubkey_psa_ecp_fallback(mbedtls_pk_context *pk,
  139. const unsigned char *pub,
  140. size_t pub_len)
  141. {
  142. #if !defined(MBEDTLS_PK_PARSE_EC_COMPRESSED)
  143. (void) pk;
  144. (void) pub;
  145. (void) pub_len;
  146. return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
  147. #else /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
  148. mbedtls_ecp_keypair ecp_key;
  149. mbedtls_ecp_group_id ecp_group_id;
  150. int ret;
  151. ecp_group_id = mbedtls_ecc_group_from_psa(pk->ec_family, pk->ec_bits);
  152. mbedtls_ecp_keypair_init(&ecp_key);
  153. ret = mbedtls_ecp_group_load(&(ecp_key.grp), ecp_group_id);
  154. if (ret != 0) {
  155. goto exit;
  156. }
  157. ret = mbedtls_ecp_point_read_binary(&(ecp_key.grp), &ecp_key.Q,
  158. pub, pub_len);
  159. if (ret != 0) {
  160. goto exit;
  161. }
  162. ret = mbedtls_ecp_point_write_binary(&(ecp_key.grp), &ecp_key.Q,
  163. MBEDTLS_ECP_PF_UNCOMPRESSED,
  164. &pk->pub_raw_len, pk->pub_raw,
  165. sizeof(pk->pub_raw));
  166. exit:
  167. mbedtls_ecp_keypair_free(&ecp_key);
  168. return ret;
  169. #endif /* MBEDTLS_PK_PARSE_EC_COMPRESSED */
  170. }
  171. #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
  172. int mbedtls_pk_ecc_set_pubkey(mbedtls_pk_context *pk, const unsigned char *pub, size_t pub_len)
  173. {
  174. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  175. /* Load the key */
  176. if (!PSA_ECC_FAMILY_IS_WEIERSTRASS(pk->ec_family) || *pub == 0x04) {
  177. /* Format directly supported by PSA:
  178. * - non-Weierstrass curves that only have one format;
  179. * - uncompressed format for Weierstrass curves. */
  180. if (pub_len > sizeof(pk->pub_raw)) {
  181. return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
  182. }
  183. memcpy(pk->pub_raw, pub, pub_len);
  184. pk->pub_raw_len = pub_len;
  185. } else {
  186. /* Other format, try the fallback */
  187. int ret = pk_ecc_set_pubkey_psa_ecp_fallback(pk, pub, pub_len);
  188. if (ret != 0) {
  189. return ret;
  190. }
  191. }
  192. /* Validate the key by trying to import it */
  193. mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
  194. psa_key_attributes_t key_attrs = PSA_KEY_ATTRIBUTES_INIT;
  195. psa_set_key_usage_flags(&key_attrs, 0);
  196. psa_set_key_type(&key_attrs, PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family));
  197. psa_set_key_bits(&key_attrs, pk->ec_bits);
  198. if ((psa_import_key(&key_attrs, pk->pub_raw, pk->pub_raw_len,
  199. &key_id) != PSA_SUCCESS) ||
  200. (psa_destroy_key(key_id) != PSA_SUCCESS)) {
  201. return MBEDTLS_ERR_PK_INVALID_PUBKEY;
  202. }
  203. return 0;
  204. #else /* MBEDTLS_PK_USE_PSA_EC_DATA */
  205. int ret;
  206. mbedtls_ecp_keypair *ec_key = (mbedtls_ecp_keypair *) pk->pk_ctx;
  207. ret = mbedtls_ecp_point_read_binary(&ec_key->grp, &ec_key->Q, pub, pub_len);
  208. if (ret != 0) {
  209. return ret;
  210. }
  211. return mbedtls_ecp_check_pubkey(&ec_key->grp, &ec_key->Q);
  212. #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
  213. }
  214. #endif /* MBEDTLS_PK_C && MBEDTLS_PK_HAVE_ECC_KEYS */