pk.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * Public Key abstraction layer
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "common.h"
  20. #if defined(MBEDTLS_PK_C)
  21. #include "mbedtls/pk.h"
  22. #include "mbedtls/pk_internal.h"
  23. #include "mbedtls/platform_util.h"
  24. #include "mbedtls/error.h"
  25. #if defined(MBEDTLS_RSA_C)
  26. #include "mbedtls/rsa.h"
  27. #endif
  28. #if defined(MBEDTLS_ECP_C)
  29. #include "mbedtls/ecp.h"
  30. #endif
  31. #if defined(MBEDTLS_ECDSA_C)
  32. #include "mbedtls/ecdsa.h"
  33. #endif
  34. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  35. #include "mbedtls/psa_util.h"
  36. #endif
  37. #include <limits.h>
  38. #include <stdint.h>
  39. /* Parameter validation macros based on platform_util.h */
  40. #define PK_VALIDATE_RET(cond) \
  41. MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA)
  42. #define PK_VALIDATE(cond) \
  43. MBEDTLS_INTERNAL_VALIDATE(cond)
  44. /*
  45. * Initialise a mbedtls_pk_context
  46. */
  47. void mbedtls_pk_init(mbedtls_pk_context *ctx)
  48. {
  49. PK_VALIDATE(ctx != NULL);
  50. ctx->pk_info = NULL;
  51. ctx->pk_ctx = NULL;
  52. }
  53. /*
  54. * Free (the components of) a mbedtls_pk_context
  55. */
  56. void mbedtls_pk_free(mbedtls_pk_context *ctx)
  57. {
  58. if (ctx == NULL) {
  59. return;
  60. }
  61. if (ctx->pk_info != NULL) {
  62. ctx->pk_info->ctx_free_func(ctx->pk_ctx);
  63. }
  64. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
  65. }
  66. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  67. /*
  68. * Initialize a restart context
  69. */
  70. void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
  71. {
  72. PK_VALIDATE(ctx != NULL);
  73. ctx->pk_info = NULL;
  74. ctx->rs_ctx = NULL;
  75. }
  76. /*
  77. * Free the components of a restart context
  78. */
  79. void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
  80. {
  81. if (ctx == NULL || ctx->pk_info == NULL ||
  82. ctx->pk_info->rs_free_func == NULL) {
  83. return;
  84. }
  85. ctx->pk_info->rs_free_func(ctx->rs_ctx);
  86. ctx->pk_info = NULL;
  87. ctx->rs_ctx = NULL;
  88. }
  89. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  90. /*
  91. * Get pk_info structure from type
  92. */
  93. const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
  94. {
  95. switch (pk_type) {
  96. #if defined(MBEDTLS_RSA_C)
  97. case MBEDTLS_PK_RSA:
  98. return &mbedtls_rsa_info;
  99. #endif
  100. #if defined(MBEDTLS_ECP_C)
  101. case MBEDTLS_PK_ECKEY:
  102. return &mbedtls_eckey_info;
  103. case MBEDTLS_PK_ECKEY_DH:
  104. return &mbedtls_eckeydh_info;
  105. #endif
  106. #if defined(MBEDTLS_ECDSA_C)
  107. case MBEDTLS_PK_ECDSA:
  108. return &mbedtls_ecdsa_info;
  109. #endif
  110. /* MBEDTLS_PK_RSA_ALT omitted on purpose */
  111. default:
  112. return NULL;
  113. }
  114. }
  115. /*
  116. * Initialise context
  117. */
  118. int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
  119. {
  120. PK_VALIDATE_RET(ctx != NULL);
  121. if (info == NULL || ctx->pk_info != NULL) {
  122. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  123. }
  124. if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
  125. return MBEDTLS_ERR_PK_ALLOC_FAILED;
  126. }
  127. ctx->pk_info = info;
  128. return 0;
  129. }
  130. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  131. /*
  132. * Initialise a PSA-wrapping context
  133. */
  134. int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
  135. const psa_key_id_t key)
  136. {
  137. const mbedtls_pk_info_t * const info = &mbedtls_pk_opaque_info;
  138. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  139. psa_key_id_t *pk_ctx;
  140. psa_key_type_t type;
  141. if (ctx == NULL || ctx->pk_info != NULL) {
  142. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  143. }
  144. if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
  145. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  146. }
  147. type = psa_get_key_type(&attributes);
  148. psa_reset_key_attributes(&attributes);
  149. /* Current implementation of can_do() relies on this. */
  150. if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
  151. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  152. }
  153. if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
  154. return MBEDTLS_ERR_PK_ALLOC_FAILED;
  155. }
  156. ctx->pk_info = info;
  157. pk_ctx = (psa_key_id_t *) ctx->pk_ctx;
  158. *pk_ctx = key;
  159. return 0;
  160. }
  161. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  162. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  163. /*
  164. * Initialize an RSA-alt context
  165. */
  166. int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
  167. mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
  168. mbedtls_pk_rsa_alt_sign_func sign_func,
  169. mbedtls_pk_rsa_alt_key_len_func key_len_func)
  170. {
  171. mbedtls_rsa_alt_context *rsa_alt;
  172. const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
  173. PK_VALIDATE_RET(ctx != NULL);
  174. if (ctx->pk_info != NULL) {
  175. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  176. }
  177. if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
  178. return MBEDTLS_ERR_PK_ALLOC_FAILED;
  179. }
  180. ctx->pk_info = info;
  181. rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
  182. rsa_alt->key = key;
  183. rsa_alt->decrypt_func = decrypt_func;
  184. rsa_alt->sign_func = sign_func;
  185. rsa_alt->key_len_func = key_len_func;
  186. return 0;
  187. }
  188. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  189. /*
  190. * Tell if a PK can do the operations of the given type
  191. */
  192. int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
  193. {
  194. /* A context with null pk_info is not set up yet and can't do anything.
  195. * For backward compatibility, also accept NULL instead of a context
  196. * pointer. */
  197. if (ctx == NULL || ctx->pk_info == NULL) {
  198. return 0;
  199. }
  200. return ctx->pk_info->can_do(type);
  201. }
  202. /*
  203. * Helper for mbedtls_pk_sign and mbedtls_pk_verify
  204. */
  205. static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
  206. {
  207. const mbedtls_md_info_t *md_info;
  208. if (*hash_len != 0 && md_alg == MBEDTLS_MD_NONE) {
  209. return 0;
  210. }
  211. if ((md_info = mbedtls_md_info_from_type(md_alg)) == NULL) {
  212. return -1;
  213. }
  214. if (*hash_len != 0 && *hash_len != mbedtls_md_get_size(md_info)) {
  215. return -1;
  216. }
  217. *hash_len = mbedtls_md_get_size(md_info);
  218. return 0;
  219. }
  220. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  221. /*
  222. * Helper to set up a restart context if needed
  223. */
  224. static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
  225. const mbedtls_pk_info_t *info)
  226. {
  227. /* Don't do anything if already set up or invalid */
  228. if (ctx == NULL || ctx->pk_info != NULL) {
  229. return 0;
  230. }
  231. /* Should never happen when we're called */
  232. if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
  233. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  234. }
  235. if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
  236. return MBEDTLS_ERR_PK_ALLOC_FAILED;
  237. }
  238. ctx->pk_info = info;
  239. return 0;
  240. }
  241. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  242. /*
  243. * Verify a signature (restartable)
  244. */
  245. int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
  246. mbedtls_md_type_t md_alg,
  247. const unsigned char *hash, size_t hash_len,
  248. const unsigned char *sig, size_t sig_len,
  249. mbedtls_pk_restart_ctx *rs_ctx)
  250. {
  251. PK_VALIDATE_RET(ctx != NULL);
  252. PK_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE && hash_len == 0) ||
  253. hash != NULL);
  254. PK_VALIDATE_RET(sig != NULL);
  255. if (ctx->pk_info == NULL ||
  256. pk_hashlen_helper(md_alg, &hash_len) != 0) {
  257. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  258. }
  259. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  260. /* optimization: use non-restartable version if restart disabled */
  261. if (rs_ctx != NULL &&
  262. mbedtls_ecp_restart_is_enabled() &&
  263. ctx->pk_info->verify_rs_func != NULL) {
  264. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  265. if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
  266. return ret;
  267. }
  268. ret = ctx->pk_info->verify_rs_func(ctx->pk_ctx,
  269. md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
  270. if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
  271. mbedtls_pk_restart_free(rs_ctx);
  272. }
  273. return ret;
  274. }
  275. #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  276. (void) rs_ctx;
  277. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  278. if (ctx->pk_info->verify_func == NULL) {
  279. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  280. }
  281. return ctx->pk_info->verify_func(ctx->pk_ctx, md_alg, hash, hash_len,
  282. sig, sig_len);
  283. }
  284. /*
  285. * Verify a signature
  286. */
  287. int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  288. const unsigned char *hash, size_t hash_len,
  289. const unsigned char *sig, size_t sig_len)
  290. {
  291. return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
  292. sig, sig_len, NULL);
  293. }
  294. /*
  295. * Verify a signature with options
  296. */
  297. int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
  298. mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  299. const unsigned char *hash, size_t hash_len,
  300. const unsigned char *sig, size_t sig_len)
  301. {
  302. PK_VALIDATE_RET(ctx != NULL);
  303. PK_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE && hash_len == 0) ||
  304. hash != NULL);
  305. PK_VALIDATE_RET(sig != NULL);
  306. if (ctx->pk_info == NULL) {
  307. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  308. }
  309. if (!mbedtls_pk_can_do(ctx, type)) {
  310. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  311. }
  312. if (type == MBEDTLS_PK_RSASSA_PSS) {
  313. #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
  314. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  315. const mbedtls_pk_rsassa_pss_options *pss_opts;
  316. #if SIZE_MAX > UINT_MAX
  317. if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
  318. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  319. }
  320. #endif /* SIZE_MAX > UINT_MAX */
  321. if (options == NULL) {
  322. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  323. }
  324. pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
  325. if (sig_len < mbedtls_pk_get_len(ctx)) {
  326. return MBEDTLS_ERR_RSA_VERIFY_FAILED;
  327. }
  328. ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
  329. NULL, NULL, MBEDTLS_RSA_PUBLIC,
  330. md_alg, (unsigned int) hash_len, hash,
  331. pss_opts->mgf1_hash_id,
  332. pss_opts->expected_salt_len,
  333. sig);
  334. if (ret != 0) {
  335. return ret;
  336. }
  337. if (sig_len > mbedtls_pk_get_len(ctx)) {
  338. return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
  339. }
  340. return 0;
  341. #else
  342. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  343. #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
  344. }
  345. /* General case: no options */
  346. if (options != NULL) {
  347. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  348. }
  349. return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
  350. }
  351. /*
  352. * Make a signature (restartable)
  353. */
  354. int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
  355. mbedtls_md_type_t md_alg,
  356. const unsigned char *hash, size_t hash_len,
  357. unsigned char *sig, size_t *sig_len,
  358. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  359. mbedtls_pk_restart_ctx *rs_ctx)
  360. {
  361. PK_VALIDATE_RET(ctx != NULL);
  362. PK_VALIDATE_RET((md_alg == MBEDTLS_MD_NONE && hash_len == 0) ||
  363. hash != NULL);
  364. PK_VALIDATE_RET(sig != NULL);
  365. if (ctx->pk_info == NULL ||
  366. pk_hashlen_helper(md_alg, &hash_len) != 0) {
  367. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  368. }
  369. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  370. /* optimization: use non-restartable version if restart disabled */
  371. if (rs_ctx != NULL &&
  372. mbedtls_ecp_restart_is_enabled() &&
  373. ctx->pk_info->sign_rs_func != NULL) {
  374. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  375. if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
  376. return ret;
  377. }
  378. ret = ctx->pk_info->sign_rs_func(ctx->pk_ctx, md_alg,
  379. hash, hash_len, sig, sig_len, f_rng, p_rng,
  380. rs_ctx->rs_ctx);
  381. if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
  382. mbedtls_pk_restart_free(rs_ctx);
  383. }
  384. return ret;
  385. }
  386. #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  387. (void) rs_ctx;
  388. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  389. if (ctx->pk_info->sign_func == NULL) {
  390. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  391. }
  392. return ctx->pk_info->sign_func(ctx->pk_ctx, md_alg, hash, hash_len,
  393. sig, sig_len, f_rng, p_rng);
  394. }
  395. /*
  396. * Make a signature
  397. */
  398. int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  399. const unsigned char *hash, size_t hash_len,
  400. unsigned char *sig, size_t *sig_len,
  401. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  402. {
  403. return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
  404. sig, sig_len, f_rng, p_rng, NULL);
  405. }
  406. /*
  407. * Decrypt message
  408. */
  409. int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
  410. const unsigned char *input, size_t ilen,
  411. unsigned char *output, size_t *olen, size_t osize,
  412. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  413. {
  414. PK_VALIDATE_RET(ctx != NULL);
  415. PK_VALIDATE_RET(input != NULL || ilen == 0);
  416. PK_VALIDATE_RET(output != NULL || osize == 0);
  417. PK_VALIDATE_RET(olen != NULL);
  418. if (ctx->pk_info == NULL) {
  419. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  420. }
  421. if (ctx->pk_info->decrypt_func == NULL) {
  422. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  423. }
  424. return ctx->pk_info->decrypt_func(ctx->pk_ctx, input, ilen,
  425. output, olen, osize, f_rng, p_rng);
  426. }
  427. /*
  428. * Encrypt message
  429. */
  430. int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
  431. const unsigned char *input, size_t ilen,
  432. unsigned char *output, size_t *olen, size_t osize,
  433. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  434. {
  435. PK_VALIDATE_RET(ctx != NULL);
  436. PK_VALIDATE_RET(input != NULL || ilen == 0);
  437. PK_VALIDATE_RET(output != NULL || osize == 0);
  438. PK_VALIDATE_RET(olen != NULL);
  439. if (ctx->pk_info == NULL) {
  440. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  441. }
  442. if (ctx->pk_info->encrypt_func == NULL) {
  443. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  444. }
  445. return ctx->pk_info->encrypt_func(ctx->pk_ctx, input, ilen,
  446. output, olen, osize, f_rng, p_rng);
  447. }
  448. /*
  449. * Check public-private key pair
  450. */
  451. int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv)
  452. {
  453. PK_VALIDATE_RET(pub != NULL);
  454. PK_VALIDATE_RET(prv != NULL);
  455. if (pub->pk_info == NULL ||
  456. prv->pk_info == NULL) {
  457. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  458. }
  459. if (prv->pk_info->check_pair_func == NULL) {
  460. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  461. }
  462. if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
  463. if (pub->pk_info->type != MBEDTLS_PK_RSA) {
  464. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  465. }
  466. } else {
  467. if (pub->pk_info != prv->pk_info) {
  468. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  469. }
  470. }
  471. return prv->pk_info->check_pair_func(pub->pk_ctx, prv->pk_ctx);
  472. }
  473. /*
  474. * Get key size in bits
  475. */
  476. size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
  477. {
  478. /* For backward compatibility, accept NULL or a context that
  479. * isn't set up yet, and return a fake value that should be safe. */
  480. if (ctx == NULL || ctx->pk_info == NULL) {
  481. return 0;
  482. }
  483. return ctx->pk_info->get_bitlen(ctx->pk_ctx);
  484. }
  485. /*
  486. * Export debug information
  487. */
  488. int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
  489. {
  490. PK_VALIDATE_RET(ctx != NULL);
  491. if (ctx->pk_info == NULL) {
  492. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  493. }
  494. if (ctx->pk_info->debug_func == NULL) {
  495. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  496. }
  497. ctx->pk_info->debug_func(ctx->pk_ctx, items);
  498. return 0;
  499. }
  500. /*
  501. * Access the PK type name
  502. */
  503. const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
  504. {
  505. if (ctx == NULL || ctx->pk_info == NULL) {
  506. return "invalid PK";
  507. }
  508. return ctx->pk_info->name;
  509. }
  510. /*
  511. * Access the PK type
  512. */
  513. mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
  514. {
  515. if (ctx == NULL || ctx->pk_info == NULL) {
  516. return MBEDTLS_PK_NONE;
  517. }
  518. return ctx->pk_info->type;
  519. }
  520. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  521. /*
  522. * Load the key to a PSA key slot,
  523. * then turn the PK context into a wrapper for that key slot.
  524. *
  525. * Currently only works for EC private keys.
  526. */
  527. int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
  528. psa_key_id_t *key,
  529. psa_algorithm_t hash_alg)
  530. {
  531. #if !defined(MBEDTLS_ECP_C)
  532. ((void) pk);
  533. ((void) key);
  534. ((void) hash_alg);
  535. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  536. #else
  537. const mbedtls_ecp_keypair *ec;
  538. unsigned char d[MBEDTLS_ECP_MAX_BYTES];
  539. size_t d_len;
  540. psa_ecc_family_t curve_id;
  541. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  542. psa_key_type_t key_type;
  543. size_t bits;
  544. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  545. psa_status_t status;
  546. /* export the private key material in the format PSA wants */
  547. if (mbedtls_pk_get_type(pk) != MBEDTLS_PK_ECKEY) {
  548. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  549. }
  550. ec = mbedtls_pk_ec(*pk);
  551. d_len = (ec->grp.nbits + 7) / 8;
  552. if ((ret = mbedtls_mpi_write_binary(&ec->d, d, d_len)) != 0) {
  553. return ret;
  554. }
  555. curve_id = mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
  556. key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_id);
  557. /* prepare the key attributes */
  558. psa_set_key_type(&attributes, key_type);
  559. psa_set_key_bits(&attributes, bits);
  560. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
  561. psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(hash_alg));
  562. /* import private key into PSA */
  563. status = psa_import_key(&attributes, d, d_len, key);
  564. mbedtls_platform_zeroize(d, sizeof(d));
  565. if (status != PSA_SUCCESS) {
  566. return MBEDTLS_ERR_PK_HW_ACCEL_FAILED;
  567. }
  568. /* make PK context wrap the key slot */
  569. mbedtls_pk_free(pk);
  570. mbedtls_pk_init(pk);
  571. return mbedtls_pk_setup_opaque(pk, *key);
  572. #endif /* MBEDTLS_ECP_C */
  573. }
  574. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  575. #endif /* MBEDTLS_PK_C */