pk.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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 "pk_wrap.h"
  23. #include "pkwrite.h"
  24. #include "hash_info.h"
  25. #include "mbedtls/platform_util.h"
  26. #include "mbedtls/error.h"
  27. #if defined(MBEDTLS_RSA_C)
  28. #include "mbedtls/rsa.h"
  29. #endif
  30. #if defined(MBEDTLS_ECP_C)
  31. #include "mbedtls/ecp.h"
  32. #endif
  33. #if defined(MBEDTLS_ECDSA_C)
  34. #include "mbedtls/ecdsa.h"
  35. #endif
  36. #if defined(MBEDTLS_PSA_CRYPTO_C)
  37. #include "mbedtls/psa_util.h"
  38. #define PSA_PK_TO_MBEDTLS_ERR(status) psa_pk_status_to_mbedtls(status)
  39. #define PSA_PK_RSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
  40. psa_to_pk_rsa_errors, \
  41. psa_pk_status_to_mbedtls)
  42. #define PSA_PK_ECDSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
  43. psa_to_pk_ecdsa_errors, \
  44. psa_pk_status_to_mbedtls)
  45. #endif
  46. #include <limits.h>
  47. #include <stdint.h>
  48. /*
  49. * Initialise a mbedtls_pk_context
  50. */
  51. void mbedtls_pk_init(mbedtls_pk_context *ctx)
  52. {
  53. ctx->pk_info = NULL;
  54. ctx->pk_ctx = NULL;
  55. }
  56. /*
  57. * Free (the components of) a mbedtls_pk_context
  58. */
  59. void mbedtls_pk_free(mbedtls_pk_context *ctx)
  60. {
  61. if (ctx == NULL) {
  62. return;
  63. }
  64. if (ctx->pk_info != NULL) {
  65. ctx->pk_info->ctx_free_func(ctx->pk_ctx);
  66. }
  67. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
  68. }
  69. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  70. /*
  71. * Initialize a restart context
  72. */
  73. void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
  74. {
  75. ctx->pk_info = NULL;
  76. ctx->rs_ctx = NULL;
  77. }
  78. /*
  79. * Free the components of a restart context
  80. */
  81. void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
  82. {
  83. if (ctx == NULL || ctx->pk_info == NULL ||
  84. ctx->pk_info->rs_free_func == NULL) {
  85. return;
  86. }
  87. ctx->pk_info->rs_free_func(ctx->rs_ctx);
  88. ctx->pk_info = NULL;
  89. ctx->rs_ctx = NULL;
  90. }
  91. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  92. /*
  93. * Get pk_info structure from type
  94. */
  95. const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
  96. {
  97. switch (pk_type) {
  98. #if defined(MBEDTLS_RSA_C)
  99. case MBEDTLS_PK_RSA:
  100. return &mbedtls_rsa_info;
  101. #endif
  102. #if defined(MBEDTLS_ECP_C)
  103. case MBEDTLS_PK_ECKEY:
  104. return &mbedtls_eckey_info;
  105. case MBEDTLS_PK_ECKEY_DH:
  106. return &mbedtls_eckeydh_info;
  107. #endif
  108. #if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
  109. case MBEDTLS_PK_ECDSA:
  110. return &mbedtls_ecdsa_info;
  111. #endif
  112. /* MBEDTLS_PK_RSA_ALT omitted on purpose */
  113. default:
  114. return NULL;
  115. }
  116. }
  117. /*
  118. * Initialise context
  119. */
  120. int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
  121. {
  122. if (info == NULL || ctx->pk_info != NULL) {
  123. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  124. }
  125. if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
  126. return MBEDTLS_ERR_PK_ALLOC_FAILED;
  127. }
  128. ctx->pk_info = info;
  129. return 0;
  130. }
  131. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  132. /*
  133. * Initialise a PSA-wrapping context
  134. */
  135. int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
  136. const mbedtls_svc_key_id_t key)
  137. {
  138. const mbedtls_pk_info_t *info = NULL;
  139. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  140. mbedtls_svc_key_id_t *pk_ctx;
  141. psa_key_type_t type;
  142. if (ctx == NULL || ctx->pk_info != NULL) {
  143. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  144. }
  145. if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
  146. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  147. }
  148. type = psa_get_key_type(&attributes);
  149. psa_reset_key_attributes(&attributes);
  150. if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
  151. info = &mbedtls_pk_ecdsa_opaque_info;
  152. } else if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
  153. info = &mbedtls_pk_rsa_opaque_info;
  154. } else {
  155. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  156. }
  157. if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
  158. return MBEDTLS_ERR_PK_ALLOC_FAILED;
  159. }
  160. ctx->pk_info = info;
  161. pk_ctx = (mbedtls_svc_key_id_t *) ctx->pk_ctx;
  162. *pk_ctx = key;
  163. return 0;
  164. }
  165. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  166. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  167. /*
  168. * Initialize an RSA-alt context
  169. */
  170. int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
  171. mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
  172. mbedtls_pk_rsa_alt_sign_func sign_func,
  173. mbedtls_pk_rsa_alt_key_len_func key_len_func)
  174. {
  175. mbedtls_rsa_alt_context *rsa_alt;
  176. const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
  177. if (ctx->pk_info != NULL) {
  178. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  179. }
  180. if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
  181. return MBEDTLS_ERR_PK_ALLOC_FAILED;
  182. }
  183. ctx->pk_info = info;
  184. rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
  185. rsa_alt->key = key;
  186. rsa_alt->decrypt_func = decrypt_func;
  187. rsa_alt->sign_func = sign_func;
  188. rsa_alt->key_len_func = key_len_func;
  189. return 0;
  190. }
  191. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  192. /*
  193. * Tell if a PK can do the operations of the given type
  194. */
  195. int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
  196. {
  197. /* A context with null pk_info is not set up yet and can't do anything.
  198. * For backward compatibility, also accept NULL instead of a context
  199. * pointer. */
  200. if (ctx == NULL || ctx->pk_info == NULL) {
  201. return 0;
  202. }
  203. return ctx->pk_info->can_do(type);
  204. }
  205. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  206. /*
  207. * Tell if a PK can do the operations of the given PSA algorithm
  208. */
  209. int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
  210. psa_key_usage_t usage)
  211. {
  212. psa_key_usage_t key_usage;
  213. /* A context with null pk_info is not set up yet and can't do anything.
  214. * For backward compatibility, also accept NULL instead of a context
  215. * pointer. */
  216. if (ctx == NULL || ctx->pk_info == NULL) {
  217. return 0;
  218. }
  219. /* Filter out non allowed algorithms */
  220. if (PSA_ALG_IS_ECDSA(alg) == 0 &&
  221. PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
  222. PSA_ALG_IS_RSA_PSS(alg) == 0 &&
  223. alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
  224. PSA_ALG_IS_ECDH(alg) == 0) {
  225. return 0;
  226. }
  227. /* Filter out non allowed usage flags */
  228. if (usage == 0 ||
  229. (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
  230. PSA_KEY_USAGE_DECRYPT |
  231. PSA_KEY_USAGE_DERIVE)) != 0) {
  232. return 0;
  233. }
  234. /* Wildcard hash is not allowed */
  235. if (PSA_ALG_IS_SIGN_HASH(alg) &&
  236. PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
  237. return 0;
  238. }
  239. if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
  240. mbedtls_pk_type_t type;
  241. if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
  242. type = MBEDTLS_PK_ECKEY;
  243. } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
  244. alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
  245. type = MBEDTLS_PK_RSA;
  246. } else if (PSA_ALG_IS_RSA_PSS(alg)) {
  247. type = MBEDTLS_PK_RSASSA_PSS;
  248. } else {
  249. return 0;
  250. }
  251. if (ctx->pk_info->can_do(type) == 0) {
  252. return 0;
  253. }
  254. switch (type) {
  255. case MBEDTLS_PK_ECKEY:
  256. key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
  257. break;
  258. case MBEDTLS_PK_RSA:
  259. case MBEDTLS_PK_RSASSA_PSS:
  260. key_usage = PSA_KEY_USAGE_SIGN_HASH |
  261. PSA_KEY_USAGE_SIGN_MESSAGE |
  262. PSA_KEY_USAGE_DECRYPT;
  263. break;
  264. default:
  265. /* Should never happen */
  266. return 0;
  267. }
  268. return (key_usage & usage) == usage;
  269. }
  270. const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx->pk_ctx;
  271. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  272. psa_algorithm_t key_alg, key_alg2;
  273. psa_status_t status;
  274. status = psa_get_key_attributes(*key, &attributes);
  275. if (status != PSA_SUCCESS) {
  276. return 0;
  277. }
  278. key_alg = psa_get_key_algorithm(&attributes);
  279. key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
  280. key_usage = psa_get_key_usage_flags(&attributes);
  281. psa_reset_key_attributes(&attributes);
  282. if ((key_usage & usage) != usage) {
  283. return 0;
  284. }
  285. /*
  286. * Common case: the key alg or alg2 only allows alg.
  287. * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
  288. * directly.
  289. * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
  290. * a fixed hash on key_alg/key_alg2.
  291. */
  292. if (alg == key_alg || alg == key_alg2) {
  293. return 1;
  294. }
  295. /*
  296. * If key_alg or key_alg2 is a hash-and-sign with a wildcard for the hash,
  297. * and alg is the same hash-and-sign family with any hash,
  298. * then alg is compliant with this key alg
  299. */
  300. if (PSA_ALG_IS_SIGN_HASH(alg)) {
  301. if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
  302. PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
  303. (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
  304. return 1;
  305. }
  306. if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
  307. PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
  308. (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
  309. return 1;
  310. }
  311. }
  312. return 0;
  313. }
  314. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  315. /*
  316. * Helper for mbedtls_pk_sign and mbedtls_pk_verify
  317. */
  318. static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
  319. {
  320. if (*hash_len != 0) {
  321. return 0;
  322. }
  323. *hash_len = mbedtls_hash_info_get_size(md_alg);
  324. if (*hash_len == 0) {
  325. return -1;
  326. }
  327. return 0;
  328. }
  329. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  330. /*
  331. * Helper to set up a restart context if needed
  332. */
  333. static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
  334. const mbedtls_pk_info_t *info)
  335. {
  336. /* Don't do anything if already set up or invalid */
  337. if (ctx == NULL || ctx->pk_info != NULL) {
  338. return 0;
  339. }
  340. /* Should never happen when we're called */
  341. if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
  342. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  343. }
  344. if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
  345. return MBEDTLS_ERR_PK_ALLOC_FAILED;
  346. }
  347. ctx->pk_info = info;
  348. return 0;
  349. }
  350. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  351. /*
  352. * Verify a signature (restartable)
  353. */
  354. int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
  355. mbedtls_md_type_t md_alg,
  356. const unsigned char *hash, size_t hash_len,
  357. const unsigned char *sig, size_t sig_len,
  358. mbedtls_pk_restart_ctx *rs_ctx)
  359. {
  360. if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
  361. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  362. }
  363. if (ctx->pk_info == NULL ||
  364. pk_hashlen_helper(md_alg, &hash_len) != 0) {
  365. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  366. }
  367. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  368. /* optimization: use non-restartable version if restart disabled */
  369. if (rs_ctx != NULL &&
  370. mbedtls_ecp_restart_is_enabled() &&
  371. ctx->pk_info->verify_rs_func != NULL) {
  372. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  373. if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
  374. return ret;
  375. }
  376. ret = ctx->pk_info->verify_rs_func(ctx->pk_ctx,
  377. md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
  378. if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
  379. mbedtls_pk_restart_free(rs_ctx);
  380. }
  381. return ret;
  382. }
  383. #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  384. (void) rs_ctx;
  385. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  386. if (ctx->pk_info->verify_func == NULL) {
  387. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  388. }
  389. return ctx->pk_info->verify_func(ctx->pk_ctx, md_alg, hash, hash_len,
  390. sig, sig_len);
  391. }
  392. /*
  393. * Verify a signature
  394. */
  395. int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  396. const unsigned char *hash, size_t hash_len,
  397. const unsigned char *sig, size_t sig_len)
  398. {
  399. return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
  400. sig, sig_len, NULL);
  401. }
  402. /*
  403. * Verify a signature with options
  404. */
  405. int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
  406. mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  407. const unsigned char *hash, size_t hash_len,
  408. const unsigned char *sig, size_t sig_len)
  409. {
  410. if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
  411. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  412. }
  413. if (ctx->pk_info == NULL) {
  414. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  415. }
  416. if (!mbedtls_pk_can_do(ctx, type)) {
  417. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  418. }
  419. if (type != MBEDTLS_PK_RSASSA_PSS) {
  420. /* General case: no options */
  421. if (options != NULL) {
  422. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  423. }
  424. return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
  425. }
  426. #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
  427. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  428. const mbedtls_pk_rsassa_pss_options *pss_opts;
  429. if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
  430. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  431. }
  432. if (options == NULL) {
  433. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  434. }
  435. pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
  436. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  437. if (pss_opts->mgf1_hash_id == md_alg) {
  438. unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
  439. unsigned char *p;
  440. int key_len;
  441. size_t signature_length;
  442. psa_status_t status = PSA_ERROR_DATA_CORRUPT;
  443. psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
  444. psa_algorithm_t psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
  445. mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
  446. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  447. psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
  448. p = buf + sizeof(buf);
  449. key_len = mbedtls_pk_write_pubkey(&p, buf, ctx);
  450. if (key_len < 0) {
  451. return key_len;
  452. }
  453. psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
  454. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
  455. psa_set_key_algorithm(&attributes, psa_sig_alg);
  456. status = psa_import_key(&attributes,
  457. buf + sizeof(buf) - key_len, key_len,
  458. &key_id);
  459. if (status != PSA_SUCCESS) {
  460. psa_destroy_key(key_id);
  461. return PSA_PK_TO_MBEDTLS_ERR(status);
  462. }
  463. /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
  464. * on a valid signature with trailing data in a buffer, but
  465. * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
  466. * so for this reason the passed sig_len is overwritten. Smaller
  467. * signature lengths should not be accepted for verification. */
  468. signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
  469. mbedtls_pk_get_len(ctx) : sig_len;
  470. status = psa_verify_hash(key_id, psa_sig_alg, hash,
  471. hash_len, sig, signature_length);
  472. destruction_status = psa_destroy_key(key_id);
  473. if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
  474. return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
  475. }
  476. if (status == PSA_SUCCESS) {
  477. status = destruction_status;
  478. }
  479. return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
  480. } else
  481. #endif
  482. {
  483. if (sig_len < mbedtls_pk_get_len(ctx)) {
  484. return MBEDTLS_ERR_RSA_VERIFY_FAILED;
  485. }
  486. ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
  487. md_alg, (unsigned int) hash_len, hash,
  488. pss_opts->mgf1_hash_id,
  489. pss_opts->expected_salt_len,
  490. sig);
  491. if (ret != 0) {
  492. return ret;
  493. }
  494. if (sig_len > mbedtls_pk_get_len(ctx)) {
  495. return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
  496. }
  497. return 0;
  498. }
  499. #else
  500. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  501. #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
  502. }
  503. /*
  504. * Make a signature (restartable)
  505. */
  506. int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
  507. mbedtls_md_type_t md_alg,
  508. const unsigned char *hash, size_t hash_len,
  509. unsigned char *sig, size_t sig_size, size_t *sig_len,
  510. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  511. mbedtls_pk_restart_ctx *rs_ctx)
  512. {
  513. if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
  514. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  515. }
  516. if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
  517. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  518. }
  519. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  520. /* optimization: use non-restartable version if restart disabled */
  521. if (rs_ctx != NULL &&
  522. mbedtls_ecp_restart_is_enabled() &&
  523. ctx->pk_info->sign_rs_func != NULL) {
  524. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  525. if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
  526. return ret;
  527. }
  528. ret = ctx->pk_info->sign_rs_func(ctx->pk_ctx, md_alg,
  529. hash, hash_len,
  530. sig, sig_size, sig_len,
  531. f_rng, p_rng, rs_ctx->rs_ctx);
  532. if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
  533. mbedtls_pk_restart_free(rs_ctx);
  534. }
  535. return ret;
  536. }
  537. #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  538. (void) rs_ctx;
  539. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  540. if (ctx->pk_info->sign_func == NULL) {
  541. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  542. }
  543. return ctx->pk_info->sign_func(ctx->pk_ctx, md_alg,
  544. hash, hash_len,
  545. sig, sig_size, sig_len,
  546. f_rng, p_rng);
  547. }
  548. /*
  549. * Make a signature
  550. */
  551. int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  552. const unsigned char *hash, size_t hash_len,
  553. unsigned char *sig, size_t sig_size, size_t *sig_len,
  554. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  555. {
  556. return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
  557. sig, sig_size, sig_len,
  558. f_rng, p_rng, NULL);
  559. }
  560. #if defined(MBEDTLS_PSA_CRYPTO_C)
  561. /*
  562. * Make a signature given a signature type.
  563. */
  564. int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
  565. mbedtls_pk_context *ctx,
  566. mbedtls_md_type_t md_alg,
  567. const unsigned char *hash, size_t hash_len,
  568. unsigned char *sig, size_t sig_size, size_t *sig_len,
  569. int (*f_rng)(void *, unsigned char *, size_t),
  570. void *p_rng)
  571. {
  572. #if defined(MBEDTLS_RSA_C)
  573. psa_algorithm_t psa_md_alg;
  574. #endif /* MBEDTLS_RSA_C */
  575. *sig_len = 0;
  576. if (ctx->pk_info == NULL) {
  577. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  578. }
  579. if (!mbedtls_pk_can_do(ctx, pk_type)) {
  580. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  581. }
  582. if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
  583. return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
  584. sig, sig_size, sig_len, f_rng, p_rng);
  585. }
  586. #if defined(MBEDTLS_RSA_C)
  587. psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
  588. if (psa_md_alg == 0) {
  589. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  590. }
  591. if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
  592. const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx->pk_ctx;
  593. psa_status_t status;
  594. status = psa_sign_hash(*key, PSA_ALG_RSA_PSS(psa_md_alg),
  595. hash, hash_len,
  596. sig, sig_size, sig_len);
  597. return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
  598. }
  599. return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
  600. ctx->pk_ctx, hash, hash_len,
  601. sig, sig_size, sig_len);
  602. #else /* MBEDTLS_RSA_C */
  603. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  604. #endif /* !MBEDTLS_RSA_C */
  605. }
  606. #endif /* MBEDTLS_PSA_CRYPTO_C */
  607. /*
  608. * Decrypt message
  609. */
  610. int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
  611. const unsigned char *input, size_t ilen,
  612. unsigned char *output, size_t *olen, size_t osize,
  613. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  614. {
  615. if (ctx->pk_info == NULL) {
  616. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  617. }
  618. if (ctx->pk_info->decrypt_func == NULL) {
  619. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  620. }
  621. return ctx->pk_info->decrypt_func(ctx->pk_ctx, input, ilen,
  622. output, olen, osize, f_rng, p_rng);
  623. }
  624. /*
  625. * Encrypt message
  626. */
  627. int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
  628. const unsigned char *input, size_t ilen,
  629. unsigned char *output, size_t *olen, size_t osize,
  630. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  631. {
  632. if (ctx->pk_info == NULL) {
  633. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  634. }
  635. if (ctx->pk_info->encrypt_func == NULL) {
  636. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  637. }
  638. return ctx->pk_info->encrypt_func(ctx->pk_ctx, input, ilen,
  639. output, olen, osize, f_rng, p_rng);
  640. }
  641. /*
  642. * Check public-private key pair
  643. */
  644. int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
  645. const mbedtls_pk_context *prv,
  646. int (*f_rng)(void *, unsigned char *, size_t),
  647. void *p_rng)
  648. {
  649. if (pub->pk_info == NULL ||
  650. prv->pk_info == NULL) {
  651. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  652. }
  653. if (f_rng == NULL) {
  654. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  655. }
  656. if (prv->pk_info->check_pair_func == NULL) {
  657. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  658. }
  659. if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
  660. if (pub->pk_info->type != MBEDTLS_PK_RSA) {
  661. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  662. }
  663. } else {
  664. if (pub->pk_info != prv->pk_info) {
  665. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  666. }
  667. }
  668. return prv->pk_info->check_pair_func(pub->pk_ctx, prv->pk_ctx, f_rng, p_rng);
  669. }
  670. /*
  671. * Get key size in bits
  672. */
  673. size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
  674. {
  675. /* For backward compatibility, accept NULL or a context that
  676. * isn't set up yet, and return a fake value that should be safe. */
  677. if (ctx == NULL || ctx->pk_info == NULL) {
  678. return 0;
  679. }
  680. return ctx->pk_info->get_bitlen(ctx->pk_ctx);
  681. }
  682. /*
  683. * Export debug information
  684. */
  685. int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
  686. {
  687. if (ctx->pk_info == NULL) {
  688. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  689. }
  690. if (ctx->pk_info->debug_func == NULL) {
  691. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  692. }
  693. ctx->pk_info->debug_func(ctx->pk_ctx, items);
  694. return 0;
  695. }
  696. /*
  697. * Access the PK type name
  698. */
  699. const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
  700. {
  701. if (ctx == NULL || ctx->pk_info == NULL) {
  702. return "invalid PK";
  703. }
  704. return ctx->pk_info->name;
  705. }
  706. /*
  707. * Access the PK type
  708. */
  709. mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
  710. {
  711. if (ctx == NULL || ctx->pk_info == NULL) {
  712. return MBEDTLS_PK_NONE;
  713. }
  714. return ctx->pk_info->type;
  715. }
  716. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  717. /*
  718. * Load the key to a PSA key slot,
  719. * then turn the PK context into a wrapper for that key slot.
  720. *
  721. * Currently only works for EC & RSA private keys.
  722. */
  723. int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
  724. mbedtls_svc_key_id_t *key,
  725. psa_algorithm_t alg,
  726. psa_key_usage_t usage,
  727. psa_algorithm_t alg2)
  728. {
  729. #if !defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_RSA_C)
  730. ((void) pk);
  731. ((void) key);
  732. ((void) alg);
  733. ((void) usage);
  734. ((void) alg2);
  735. #else
  736. #if defined(MBEDTLS_ECP_C)
  737. if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECKEY) {
  738. const mbedtls_ecp_keypair *ec;
  739. unsigned char d[MBEDTLS_ECP_MAX_BYTES];
  740. size_t d_len;
  741. psa_ecc_family_t curve_id;
  742. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  743. psa_key_type_t key_type;
  744. size_t bits;
  745. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  746. psa_status_t status;
  747. /* export the private key material in the format PSA wants */
  748. ec = mbedtls_pk_ec(*pk);
  749. d_len = PSA_BITS_TO_BYTES(ec->grp.nbits);
  750. if ((ret = mbedtls_mpi_write_binary(&ec->d, d, d_len)) != 0) {
  751. return ret;
  752. }
  753. curve_id = mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
  754. key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_id);
  755. /* prepare the key attributes */
  756. psa_set_key_type(&attributes, key_type);
  757. psa_set_key_bits(&attributes, bits);
  758. psa_set_key_usage_flags(&attributes, usage);
  759. psa_set_key_algorithm(&attributes, alg);
  760. if (alg2 != PSA_ALG_NONE) {
  761. psa_set_key_enrollment_algorithm(&attributes, alg2);
  762. }
  763. /* import private key into PSA */
  764. status = psa_import_key(&attributes, d, d_len, key);
  765. if (status != PSA_SUCCESS) {
  766. return PSA_PK_TO_MBEDTLS_ERR(status);
  767. }
  768. /* make PK context wrap the key slot */
  769. mbedtls_pk_free(pk);
  770. mbedtls_pk_init(pk);
  771. return mbedtls_pk_setup_opaque(pk, *key);
  772. } else
  773. #endif /* MBEDTLS_ECP_C */
  774. #if defined(MBEDTLS_RSA_C)
  775. if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA) {
  776. unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
  777. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  778. int key_len;
  779. psa_status_t status;
  780. /* export the private key material in the format PSA wants */
  781. key_len = mbedtls_pk_write_key_der(pk, buf, sizeof(buf));
  782. if (key_len <= 0) {
  783. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  784. }
  785. /* prepare the key attributes */
  786. psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
  787. psa_set_key_bits(&attributes, mbedtls_pk_get_bitlen(pk));
  788. psa_set_key_usage_flags(&attributes, usage);
  789. psa_set_key_algorithm(&attributes, alg);
  790. if (alg2 != PSA_ALG_NONE) {
  791. psa_set_key_enrollment_algorithm(&attributes, alg2);
  792. }
  793. /* import private key into PSA */
  794. status = psa_import_key(&attributes,
  795. buf + sizeof(buf) - key_len,
  796. key_len, key);
  797. mbedtls_platform_zeroize(buf, sizeof(buf));
  798. if (status != PSA_SUCCESS) {
  799. return PSA_PK_TO_MBEDTLS_ERR(status);
  800. }
  801. /* make PK context wrap the key slot */
  802. mbedtls_pk_free(pk);
  803. mbedtls_pk_init(pk);
  804. return mbedtls_pk_setup_opaque(pk, *key);
  805. } else
  806. #endif /* MBEDTLS_RSA_C */
  807. #endif /* !MBEDTLS_ECP_C && !MBEDTLS_RSA_C */
  808. return MBEDTLS_ERR_PK_TYPE_MISMATCH;
  809. }
  810. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  811. #endif /* MBEDTLS_PK_C */