pk.c 18 KB

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