pk.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Public Key abstraction layer
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_PK_C)
  27. #include "mbedtls/pk.h"
  28. #include "mbedtls/pk_internal.h"
  29. #include "mbedtls/platform_util.h"
  30. #if defined(MBEDTLS_RSA_C)
  31. #include "mbedtls/rsa.h"
  32. #endif
  33. #if defined(MBEDTLS_ECP_C)
  34. #include "mbedtls/ecp.h"
  35. #endif
  36. #if defined(MBEDTLS_ECDSA_C)
  37. #include "mbedtls/ecdsa.h"
  38. #endif
  39. #include <limits.h>
  40. #include <stdint.h>
  41. /* Parameter validation macros based on platform_util.h */
  42. #define PK_VALIDATE_RET( cond ) \
  43. MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
  44. #define PK_VALIDATE( cond ) \
  45. MBEDTLS_INTERNAL_VALIDATE( cond )
  46. /*
  47. * Initialise a mbedtls_pk_context
  48. */
  49. void mbedtls_pk_init( mbedtls_pk_context *ctx )
  50. {
  51. PK_VALIDATE( ctx != NULL );
  52. ctx->pk_info = NULL;
  53. ctx->pk_ctx = NULL;
  54. }
  55. /*
  56. * Free (the components of) a mbedtls_pk_context
  57. */
  58. void mbedtls_pk_free( mbedtls_pk_context *ctx )
  59. {
  60. if( ctx == NULL )
  61. return;
  62. if ( ctx->pk_info != NULL )
  63. ctx->pk_info->ctx_free_func( ctx->pk_ctx );
  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. {
  84. return;
  85. }
  86. ctx->pk_info->rs_free_func( ctx->rs_ctx );
  87. ctx->pk_info = NULL;
  88. ctx->rs_ctx = NULL;
  89. }
  90. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  91. /*
  92. * Get pk_info structure from type
  93. */
  94. const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
  95. {
  96. switch( pk_type ) {
  97. #if defined(MBEDTLS_RSA_C)
  98. case MBEDTLS_PK_RSA:
  99. return( &mbedtls_rsa_info );
  100. #endif
  101. #if defined(MBEDTLS_ECP_C)
  102. case MBEDTLS_PK_ECKEY:
  103. return( &mbedtls_eckey_info );
  104. case MBEDTLS_PK_ECKEY_DH:
  105. return( &mbedtls_eckeydh_info );
  106. #endif
  107. #if defined(MBEDTLS_ECDSA_C)
  108. case MBEDTLS_PK_ECDSA:
  109. return( &mbedtls_ecdsa_info );
  110. #endif
  111. /* MBEDTLS_PK_RSA_ALT omitted on purpose */
  112. default:
  113. return( NULL );
  114. }
  115. }
  116. /*
  117. * Initialise context
  118. */
  119. int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
  120. {
  121. PK_VALIDATE_RET( ctx != NULL );
  122. if( info == NULL || ctx->pk_info != NULL )
  123. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  124. if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
  125. return( MBEDTLS_ERR_PK_ALLOC_FAILED );
  126. ctx->pk_info = info;
  127. return( 0 );
  128. }
  129. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  130. /*
  131. * Initialize an RSA-alt context
  132. */
  133. int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
  134. mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
  135. mbedtls_pk_rsa_alt_sign_func sign_func,
  136. mbedtls_pk_rsa_alt_key_len_func key_len_func )
  137. {
  138. mbedtls_rsa_alt_context *rsa_alt;
  139. const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
  140. PK_VALIDATE_RET( ctx != NULL );
  141. if( ctx->pk_info != NULL )
  142. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  143. if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
  144. return( MBEDTLS_ERR_PK_ALLOC_FAILED );
  145. ctx->pk_info = info;
  146. rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
  147. rsa_alt->key = key;
  148. rsa_alt->decrypt_func = decrypt_func;
  149. rsa_alt->sign_func = sign_func;
  150. rsa_alt->key_len_func = key_len_func;
  151. return( 0 );
  152. }
  153. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  154. /*
  155. * Tell if a PK can do the operations of the given type
  156. */
  157. int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
  158. {
  159. /* A context with null pk_info is not set up yet and can't do anything.
  160. * For backward compatibility, also accept NULL instead of a context
  161. * pointer. */
  162. if( ctx == NULL || ctx->pk_info == NULL )
  163. return( 0 );
  164. return( ctx->pk_info->can_do( type ) );
  165. }
  166. /*
  167. * Helper for mbedtls_pk_sign and mbedtls_pk_verify
  168. */
  169. static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
  170. {
  171. const mbedtls_md_info_t *md_info;
  172. if( *hash_len != 0 )
  173. return( 0 );
  174. if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
  175. return( -1 );
  176. *hash_len = mbedtls_md_get_size( md_info );
  177. return( 0 );
  178. }
  179. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  180. /*
  181. * Helper to set up a restart context if needed
  182. */
  183. static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
  184. const mbedtls_pk_info_t *info )
  185. {
  186. /* Don't do anything if already set up or invalid */
  187. if( ctx == NULL || ctx->pk_info != NULL )
  188. return( 0 );
  189. /* Should never happen when we're called */
  190. if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
  191. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  192. if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
  193. return( MBEDTLS_ERR_PK_ALLOC_FAILED );
  194. ctx->pk_info = info;
  195. return( 0 );
  196. }
  197. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  198. /*
  199. * Verify a signature (restartable)
  200. */
  201. int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
  202. mbedtls_md_type_t md_alg,
  203. const unsigned char *hash, size_t hash_len,
  204. const unsigned char *sig, size_t sig_len,
  205. mbedtls_pk_restart_ctx *rs_ctx )
  206. {
  207. PK_VALIDATE_RET( ctx != NULL );
  208. PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
  209. hash != NULL );
  210. PK_VALIDATE_RET( sig != NULL );
  211. if( ctx->pk_info == NULL ||
  212. pk_hashlen_helper( md_alg, &hash_len ) != 0 )
  213. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  214. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  215. /* optimization: use non-restartable version if restart disabled */
  216. if( rs_ctx != NULL &&
  217. mbedtls_ecp_restart_is_enabled() &&
  218. ctx->pk_info->verify_rs_func != NULL )
  219. {
  220. int ret;
  221. if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
  222. return( ret );
  223. ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
  224. md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
  225. if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
  226. mbedtls_pk_restart_free( rs_ctx );
  227. return( ret );
  228. }
  229. #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  230. (void) rs_ctx;
  231. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  232. if( ctx->pk_info->verify_func == NULL )
  233. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  234. return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
  235. sig, sig_len ) );
  236. }
  237. /*
  238. * Verify a signature
  239. */
  240. int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  241. const unsigned char *hash, size_t hash_len,
  242. const unsigned char *sig, size_t sig_len )
  243. {
  244. return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
  245. sig, sig_len, NULL ) );
  246. }
  247. /*
  248. * Verify a signature with options
  249. */
  250. int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
  251. mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  252. const unsigned char *hash, size_t hash_len,
  253. const unsigned char *sig, size_t sig_len )
  254. {
  255. PK_VALIDATE_RET( ctx != NULL );
  256. PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
  257. hash != NULL );
  258. PK_VALIDATE_RET( sig != NULL );
  259. if( ctx->pk_info == NULL )
  260. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  261. if( ! mbedtls_pk_can_do( ctx, type ) )
  262. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  263. if( type == MBEDTLS_PK_RSASSA_PSS )
  264. {
  265. #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
  266. int ret;
  267. const mbedtls_pk_rsassa_pss_options *pss_opts;
  268. #if SIZE_MAX > UINT_MAX
  269. if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
  270. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  271. #endif /* SIZE_MAX > UINT_MAX */
  272. if( options == NULL )
  273. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  274. pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
  275. if( sig_len < mbedtls_pk_get_len( ctx ) )
  276. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  277. ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
  278. NULL, NULL, MBEDTLS_RSA_PUBLIC,
  279. md_alg, (unsigned int) hash_len, hash,
  280. pss_opts->mgf1_hash_id,
  281. pss_opts->expected_salt_len,
  282. sig );
  283. if( ret != 0 )
  284. return( ret );
  285. if( sig_len > mbedtls_pk_get_len( ctx ) )
  286. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  287. return( 0 );
  288. #else
  289. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  290. #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
  291. }
  292. /* General case: no options */
  293. if( options != NULL )
  294. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  295. return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
  296. }
  297. /*
  298. * Make a signature (restartable)
  299. */
  300. int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
  301. mbedtls_md_type_t md_alg,
  302. const unsigned char *hash, size_t hash_len,
  303. unsigned char *sig, size_t *sig_len,
  304. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  305. mbedtls_pk_restart_ctx *rs_ctx )
  306. {
  307. PK_VALIDATE_RET( ctx != NULL );
  308. PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
  309. hash != NULL );
  310. PK_VALIDATE_RET( sig != NULL );
  311. if( ctx->pk_info == NULL ||
  312. pk_hashlen_helper( md_alg, &hash_len ) != 0 )
  313. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  314. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  315. /* optimization: use non-restartable version if restart disabled */
  316. if( rs_ctx != NULL &&
  317. mbedtls_ecp_restart_is_enabled() &&
  318. ctx->pk_info->sign_rs_func != NULL )
  319. {
  320. int ret;
  321. if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
  322. return( ret );
  323. ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
  324. hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
  325. if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
  326. mbedtls_pk_restart_free( rs_ctx );
  327. return( ret );
  328. }
  329. #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  330. (void) rs_ctx;
  331. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  332. if( ctx->pk_info->sign_func == NULL )
  333. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  334. return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
  335. sig, sig_len, f_rng, p_rng ) );
  336. }
  337. /*
  338. * Make a signature
  339. */
  340. int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  341. const unsigned char *hash, size_t hash_len,
  342. unsigned char *sig, size_t *sig_len,
  343. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  344. {
  345. return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
  346. sig, sig_len, f_rng, p_rng, NULL ) );
  347. }
  348. /*
  349. * Decrypt message
  350. */
  351. int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
  352. const unsigned char *input, size_t ilen,
  353. unsigned char *output, size_t *olen, size_t osize,
  354. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  355. {
  356. PK_VALIDATE_RET( ctx != NULL );
  357. PK_VALIDATE_RET( input != NULL || ilen == 0 );
  358. PK_VALIDATE_RET( output != NULL || osize == 0 );
  359. PK_VALIDATE_RET( olen != NULL );
  360. if( ctx->pk_info == NULL )
  361. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  362. if( ctx->pk_info->decrypt_func == NULL )
  363. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  364. return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
  365. output, olen, osize, f_rng, p_rng ) );
  366. }
  367. /*
  368. * Encrypt message
  369. */
  370. int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
  371. const unsigned char *input, size_t ilen,
  372. unsigned char *output, size_t *olen, size_t osize,
  373. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  374. {
  375. PK_VALIDATE_RET( ctx != NULL );
  376. PK_VALIDATE_RET( input != NULL || ilen == 0 );
  377. PK_VALIDATE_RET( output != NULL || osize == 0 );
  378. PK_VALIDATE_RET( olen != NULL );
  379. if( ctx->pk_info == NULL )
  380. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  381. if( ctx->pk_info->encrypt_func == NULL )
  382. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  383. return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
  384. output, olen, osize, f_rng, p_rng ) );
  385. }
  386. /*
  387. * Check public-private key pair
  388. */
  389. int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
  390. {
  391. PK_VALIDATE_RET( pub != NULL );
  392. PK_VALIDATE_RET( prv != NULL );
  393. if( pub->pk_info == NULL ||
  394. prv->pk_info == NULL ||
  395. prv->pk_info->check_pair_func == NULL )
  396. {
  397. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  398. }
  399. if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
  400. {
  401. if( pub->pk_info->type != MBEDTLS_PK_RSA )
  402. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  403. }
  404. else
  405. {
  406. if( pub->pk_info != prv->pk_info )
  407. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  408. }
  409. return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
  410. }
  411. /*
  412. * Get key size in bits
  413. */
  414. size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
  415. {
  416. /* For backward compatibility, accept NULL or a context that
  417. * isn't set up yet, and return a fake value that should be safe. */
  418. if( ctx == NULL || ctx->pk_info == NULL )
  419. return( 0 );
  420. return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
  421. }
  422. /*
  423. * Export debug information
  424. */
  425. int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
  426. {
  427. PK_VALIDATE_RET( ctx != NULL );
  428. if( ctx->pk_info == NULL )
  429. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  430. if( ctx->pk_info->debug_func == NULL )
  431. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  432. ctx->pk_info->debug_func( ctx->pk_ctx, items );
  433. return( 0 );
  434. }
  435. /*
  436. * Access the PK type name
  437. */
  438. const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
  439. {
  440. if( ctx == NULL || ctx->pk_info == NULL )
  441. return( "invalid PK" );
  442. return( ctx->pk_info->name );
  443. }
  444. /*
  445. * Access the PK type
  446. */
  447. mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
  448. {
  449. if( ctx == NULL || ctx->pk_info == NULL )
  450. return( MBEDTLS_PK_NONE );
  451. return( ctx->pk_info->type );
  452. }
  453. #endif /* MBEDTLS_PK_C */