pk.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /**
  2. * \file pk.h
  3. *
  4. * \brief Public Key abstraction layer
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. #ifndef MBEDTLS_PK_H
  23. #define MBEDTLS_PK_H
  24. #if !defined(MBEDTLS_CONFIG_FILE)
  25. #include "mbedtls/config.h"
  26. #else
  27. #include MBEDTLS_CONFIG_FILE
  28. #endif
  29. #include "mbedtls/md.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. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  40. #include "psa/crypto.h"
  41. #endif
  42. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  43. !defined(inline) && !defined(__cplusplus)
  44. #define inline __inline
  45. #endif
  46. #define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80 /**< Memory allocation failed. */
  47. #define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00 /**< Type mismatch, eg attempt to encrypt with an ECDSA key */
  48. #define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80 /**< Bad input parameters to function. */
  49. #define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00 /**< Read/write of file failed. */
  50. #define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80 /**< Unsupported key version */
  51. #define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00 /**< Invalid key tag or value. */
  52. #define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80 /**< Key algorithm is unsupported (only RSA and EC are supported). */
  53. #define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00 /**< Private key password can't be empty. */
  54. #define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80 /**< Given private key password does not allow for correct decryption. */
  55. #define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00 /**< The pubkey tag or value is invalid (only RSA and EC are supported). */
  56. #define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80 /**< The algorithm tag or value is invalid. */
  57. #define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00 /**< Elliptic curve is unsupported (only NIST curves are supported). */
  58. #define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980 /**< Unavailable feature, e.g. RSA disabled for RSA key. */
  59. #define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900 /**< The buffer contains a valid signature followed by more data. */
  60. /* MBEDTLS_ERR_PK_HW_ACCEL_FAILED is deprecated and should not be used. */
  61. #define MBEDTLS_ERR_PK_HW_ACCEL_FAILED -0x3880 /**< PK hardware accelerator failed. */
  62. #ifdef __cplusplus
  63. extern "C" {
  64. #endif
  65. /**
  66. * \brief Public key types
  67. */
  68. typedef enum {
  69. MBEDTLS_PK_NONE=0,
  70. MBEDTLS_PK_RSA,
  71. MBEDTLS_PK_ECKEY,
  72. MBEDTLS_PK_ECKEY_DH,
  73. MBEDTLS_PK_ECDSA,
  74. MBEDTLS_PK_RSA_ALT,
  75. MBEDTLS_PK_RSASSA_PSS,
  76. MBEDTLS_PK_OPAQUE,
  77. } mbedtls_pk_type_t;
  78. /**
  79. * \brief Options for RSASSA-PSS signature verification.
  80. * See \c mbedtls_rsa_rsassa_pss_verify_ext()
  81. */
  82. typedef struct mbedtls_pk_rsassa_pss_options
  83. {
  84. mbedtls_md_type_t mgf1_hash_id;
  85. int expected_salt_len;
  86. } mbedtls_pk_rsassa_pss_options;
  87. /**
  88. * \brief Maximum size of a signature made by mbedtls_pk_sign().
  89. */
  90. /* We need to set MBEDTLS_PK_SIGNATURE_MAX_SIZE to the maximum signature
  91. * size among the supported signature types. Do it by starting at 0,
  92. * then incrementally increasing to be large enough for each supported
  93. * signature mechanism.
  94. *
  95. * The resulting value can be 0, for example if MBEDTLS_ECDH_C is enabled
  96. * (which allows the pk module to be included) but neither MBEDTLS_ECDSA_C
  97. * nor MBEDTLS_RSA_C nor any opaque signature mechanism (PSA or RSA_ALT).
  98. */
  99. #define MBEDTLS_PK_SIGNATURE_MAX_SIZE 0
  100. #if ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT) ) && \
  101. MBEDTLS_MPI_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
  102. /* For RSA, the signature can be as large as the bignum module allows.
  103. * For RSA_ALT, the signature size is not necessarily tied to what the
  104. * bignum module can do, but in the absence of any specific setting,
  105. * we use that (rsa_alt_sign_wrap in pk_wrap will check). */
  106. #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
  107. #define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
  108. #endif
  109. #if defined(MBEDTLS_ECDSA_C) && \
  110. MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_PK_SIGNATURE_MAX_SIZE
  111. /* For ECDSA, the ecdsa module exports a constant for the maximum
  112. * signature size. */
  113. #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
  114. #define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
  115. #endif
  116. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  117. #if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
  118. /* PSA_SIGNATURE_MAX_SIZE is the maximum size of a signature made
  119. * through the PSA API in the PSA representation. */
  120. #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
  121. #define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE
  122. #endif
  123. #if PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 > MBEDTLS_PK_SIGNATURE_MAX_SIZE
  124. /* The Mbed TLS representation is different for ECDSA signatures:
  125. * PSA uses the raw concatenation of r and s,
  126. * whereas Mbed TLS uses the ASN.1 representation (SEQUENCE of two INTEGERs).
  127. * Add the overhead of ASN.1: up to (1+2) + 2 * (1+2+1) for the
  128. * types, lengths (represented by up to 2 bytes), and potential leading
  129. * zeros of the INTEGERs and the SEQUENCE. */
  130. #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
  131. #define MBEDTLS_PK_SIGNATURE_MAX_SIZE ( PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 )
  132. #endif
  133. #endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */
  134. /**
  135. * \brief Types for interfacing with the debug module
  136. */
  137. typedef enum
  138. {
  139. MBEDTLS_PK_DEBUG_NONE = 0,
  140. MBEDTLS_PK_DEBUG_MPI,
  141. MBEDTLS_PK_DEBUG_ECP,
  142. } mbedtls_pk_debug_type;
  143. /**
  144. * \brief Item to send to the debug module
  145. */
  146. typedef struct mbedtls_pk_debug_item
  147. {
  148. mbedtls_pk_debug_type type;
  149. const char *name;
  150. void *value;
  151. } mbedtls_pk_debug_item;
  152. /** Maximum number of item send for debugging, plus 1 */
  153. #define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
  154. /**
  155. * \brief Public key information and operations
  156. */
  157. typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
  158. /**
  159. * \brief Public key container
  160. */
  161. typedef struct mbedtls_pk_context
  162. {
  163. const mbedtls_pk_info_t * pk_info; /**< Public key information */
  164. void * pk_ctx; /**< Underlying public key context */
  165. } mbedtls_pk_context;
  166. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  167. /**
  168. * \brief Context for resuming operations
  169. */
  170. typedef struct
  171. {
  172. const mbedtls_pk_info_t * pk_info; /**< Public key information */
  173. void * rs_ctx; /**< Underlying restart context */
  174. } mbedtls_pk_restart_ctx;
  175. #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  176. /* Now we can declare functions that take a pointer to that */
  177. typedef void mbedtls_pk_restart_ctx;
  178. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  179. #if defined(MBEDTLS_RSA_C)
  180. /**
  181. * Quick access to an RSA context inside a PK context.
  182. *
  183. * \warning You must make sure the PK context actually holds an RSA context
  184. * before using this function!
  185. */
  186. static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk )
  187. {
  188. return( (mbedtls_rsa_context *) (pk).pk_ctx );
  189. }
  190. #endif /* MBEDTLS_RSA_C */
  191. #if defined(MBEDTLS_ECP_C)
  192. /**
  193. * Quick access to an EC context inside a PK context.
  194. *
  195. * \warning You must make sure the PK context actually holds an EC context
  196. * before using this function!
  197. */
  198. static inline mbedtls_ecp_keypair *mbedtls_pk_ec( const mbedtls_pk_context pk )
  199. {
  200. return( (mbedtls_ecp_keypair *) (pk).pk_ctx );
  201. }
  202. #endif /* MBEDTLS_ECP_C */
  203. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  204. /**
  205. * \brief Types for RSA-alt abstraction
  206. */
  207. typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
  208. const unsigned char *input, unsigned char *output,
  209. size_t output_max_len );
  210. typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
  211. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  212. int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
  213. const unsigned char *hash, unsigned char *sig );
  214. typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
  215. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  216. /**
  217. * \brief Return information associated with the given PK type
  218. *
  219. * \param pk_type PK type to search for.
  220. *
  221. * \return The PK info associated with the type or NULL if not found.
  222. */
  223. const mbedtls_pk_info_t *mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type );
  224. /**
  225. * \brief Initialize a #mbedtls_pk_context (as NONE).
  226. *
  227. * \param ctx The context to initialize.
  228. * This must not be \c NULL.
  229. */
  230. void mbedtls_pk_init( mbedtls_pk_context *ctx );
  231. /**
  232. * \brief Free the components of a #mbedtls_pk_context.
  233. *
  234. * \param ctx The context to clear. It must have been initialized.
  235. * If this is \c NULL, this function does nothing.
  236. *
  237. * \note For contexts that have been set up with
  238. * mbedtls_pk_setup_opaque(), this does not free the underlying
  239. * PSA key and you still need to call psa_destroy_key()
  240. * independently if you want to destroy that key.
  241. */
  242. void mbedtls_pk_free( mbedtls_pk_context *ctx );
  243. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  244. /**
  245. * \brief Initialize a restart context
  246. *
  247. * \param ctx The context to initialize.
  248. * This must not be \c NULL.
  249. */
  250. void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx );
  251. /**
  252. * \brief Free the components of a restart context
  253. *
  254. * \param ctx The context to clear. It must have been initialized.
  255. * If this is \c NULL, this function does nothing.
  256. */
  257. void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx );
  258. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  259. /**
  260. * \brief Initialize a PK context with the information given
  261. * and allocates the type-specific PK subcontext.
  262. *
  263. * \param ctx Context to initialize. It must not have been set
  264. * up yet (type #MBEDTLS_PK_NONE).
  265. * \param info Information to use
  266. *
  267. * \return 0 on success,
  268. * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
  269. * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
  270. *
  271. * \note For contexts holding an RSA-alt key, use
  272. * \c mbedtls_pk_setup_rsa_alt() instead.
  273. */
  274. int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
  275. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  276. /**
  277. * \brief Initialize a PK context to wrap a PSA key.
  278. *
  279. * \note This function replaces mbedtls_pk_setup() for contexts
  280. * that wrap a (possibly opaque) PSA key instead of
  281. * storing and manipulating the key material directly.
  282. *
  283. * \param ctx The context to initialize. It must be empty (type NONE).
  284. * \param key The PSA key to wrap, which must hold an ECC key pair
  285. * (see notes below).
  286. *
  287. * \note The wrapped key must remain valid as long as the
  288. * wrapping PK context is in use, that is at least between
  289. * the point this function is called and the point
  290. * mbedtls_pk_free() is called on this context. The wrapped
  291. * key might then be independently used or destroyed.
  292. *
  293. * \note This function is currently only available for ECC key
  294. * pairs (that is, ECC keys containing private key material).
  295. * Support for other key types may be added later.
  296. *
  297. * \return \c 0 on success.
  298. * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input
  299. * (context already used, invalid key identifier).
  300. * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the key is not an
  301. * ECC key pair.
  302. * \return #MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
  303. */
  304. int mbedtls_pk_setup_opaque( mbedtls_pk_context *ctx,
  305. const psa_key_id_t key );
  306. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  307. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  308. /**
  309. * \brief Initialize an RSA-alt context
  310. *
  311. * \param ctx Context to initialize. It must not have been set
  312. * up yet (type #MBEDTLS_PK_NONE).
  313. * \param key RSA key pointer
  314. * \param decrypt_func Decryption function
  315. * \param sign_func Signing function
  316. * \param key_len_func Function returning key length in bytes
  317. *
  318. * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
  319. * context wasn't already initialized as RSA_ALT.
  320. *
  321. * \note This function replaces \c mbedtls_pk_setup() for RSA-alt.
  322. */
  323. int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
  324. mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
  325. mbedtls_pk_rsa_alt_sign_func sign_func,
  326. mbedtls_pk_rsa_alt_key_len_func key_len_func );
  327. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  328. /**
  329. * \brief Get the size in bits of the underlying key
  330. *
  331. * \param ctx The context to query. It must have been initialized.
  332. *
  333. * \return Key size in bits, or 0 on error
  334. */
  335. size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
  336. /**
  337. * \brief Get the length in bytes of the underlying key
  338. *
  339. * \param ctx The context to query. It must have been initialized.
  340. *
  341. * \return Key length in bytes, or 0 on error
  342. */
  343. static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
  344. {
  345. return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
  346. }
  347. /**
  348. * \brief Tell if a context can do the operation given by type
  349. *
  350. * \param ctx The context to query. It must have been initialized.
  351. * \param type The desired type.
  352. *
  353. * \return 1 if the context can do operations on the given type.
  354. * \return 0 if the context cannot do the operations on the given
  355. * type. This is always the case for a context that has
  356. * been initialized but not set up, or that has been
  357. * cleared with mbedtls_pk_free().
  358. */
  359. int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type );
  360. /**
  361. * \brief Verify signature (including padding if relevant).
  362. *
  363. * \param ctx The PK context to use. It must have been set up.
  364. * \param md_alg Hash algorithm used (see notes)
  365. * \param hash Hash of the message to sign
  366. * \param hash_len Hash length or 0 (see notes)
  367. * \param sig Signature to verify
  368. * \param sig_len Signature length
  369. *
  370. * \return 0 on success (signature is valid),
  371. * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
  372. * signature in sig but its length is less than \p siglen,
  373. * or a specific error code.
  374. *
  375. * \note For RSA keys, the default padding type is PKCS#1 v1.5.
  376. * Use \c mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, ... )
  377. * to verify RSASSA_PSS signatures.
  378. *
  379. * \note If hash_len is 0, then the length associated with md_alg
  380. * is used instead, or an error returned if it is invalid.
  381. *
  382. * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
  383. */
  384. int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  385. const unsigned char *hash, size_t hash_len,
  386. const unsigned char *sig, size_t sig_len );
  387. /**
  388. * \brief Restartable version of \c mbedtls_pk_verify()
  389. *
  390. * \note Performs the same job as \c mbedtls_pk_verify(), but can
  391. * return early and restart according to the limit set with
  392. * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
  393. * operations. For RSA, same as \c mbedtls_pk_verify().
  394. *
  395. * \param ctx The PK context to use. It must have been set up.
  396. * \param md_alg Hash algorithm used (see notes)
  397. * \param hash Hash of the message to sign
  398. * \param hash_len Hash length or 0 (see notes)
  399. * \param sig Signature to verify
  400. * \param sig_len Signature length
  401. * \param rs_ctx Restart context (NULL to disable restart)
  402. *
  403. * \return See \c mbedtls_pk_verify(), or
  404. * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  405. * operations was reached: see \c mbedtls_ecp_set_max_ops().
  406. */
  407. int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
  408. mbedtls_md_type_t md_alg,
  409. const unsigned char *hash, size_t hash_len,
  410. const unsigned char *sig, size_t sig_len,
  411. mbedtls_pk_restart_ctx *rs_ctx );
  412. /**
  413. * \brief Verify signature, with options.
  414. * (Includes verification of the padding depending on type.)
  415. *
  416. * \param type Signature type (inc. possible padding type) to verify
  417. * \param options Pointer to type-specific options, or NULL
  418. * \param ctx The PK context to use. It must have been set up.
  419. * \param md_alg Hash algorithm used (see notes)
  420. * \param hash Hash of the message to sign
  421. * \param hash_len Hash length or 0 (see notes)
  422. * \param sig Signature to verify
  423. * \param sig_len Signature length
  424. *
  425. * \return 0 on success (signature is valid),
  426. * #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
  427. * used for this type of signatures,
  428. * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
  429. * signature in sig but its length is less than \p siglen,
  430. * or a specific error code.
  431. *
  432. * \note If hash_len is 0, then the length associated with md_alg
  433. * is used instead, or an error returned if it is invalid.
  434. *
  435. * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
  436. *
  437. * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point
  438. * to a mbedtls_pk_rsassa_pss_options structure,
  439. * otherwise it must be NULL.
  440. */
  441. int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
  442. mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  443. const unsigned char *hash, size_t hash_len,
  444. const unsigned char *sig, size_t sig_len );
  445. /**
  446. * \brief Make signature, including padding if relevant.
  447. *
  448. * \param ctx The PK context to use. It must have been set up
  449. * with a private key.
  450. * \param md_alg Hash algorithm used (see notes)
  451. * \param hash Hash of the message to sign
  452. * \param hash_len Hash length or 0 (see notes)
  453. * \param sig Place to write the signature.
  454. * It must have enough room for the signature.
  455. * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
  456. * You may use a smaller buffer if it is large enough
  457. * given the key type.
  458. * \param sig_len On successful return,
  459. * the number of bytes written to \p sig.
  460. * \param f_rng RNG function
  461. * \param p_rng RNG parameter
  462. *
  463. * \return 0 on success, or a specific error code.
  464. *
  465. * \note For RSA keys, the default padding type is PKCS#1 v1.5.
  466. * There is no interface in the PK module to make RSASSA-PSS
  467. * signatures yet.
  468. *
  469. * \note If hash_len is 0, then the length associated with md_alg
  470. * is used instead, or an error returned if it is invalid.
  471. *
  472. * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
  473. * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
  474. */
  475. int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  476. const unsigned char *hash, size_t hash_len,
  477. unsigned char *sig, size_t *sig_len,
  478. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  479. /**
  480. * \brief Restartable version of \c mbedtls_pk_sign()
  481. *
  482. * \note Performs the same job as \c mbedtls_pk_sign(), but can
  483. * return early and restart according to the limit set with
  484. * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
  485. * operations. For RSA, same as \c mbedtls_pk_sign().
  486. *
  487. * \param ctx The PK context to use. It must have been set up
  488. * with a private key.
  489. * \param md_alg Hash algorithm used (see notes for mbedtls_pk_sign())
  490. * \param hash Hash of the message to sign
  491. * \param hash_len Hash length or 0 (see notes for mbedtls_pk_sign())
  492. * \param sig Place to write the signature.
  493. * It must have enough room for the signature.
  494. * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
  495. * You may use a smaller buffer if it is large enough
  496. * given the key type.
  497. * \param sig_len On successful return,
  498. * the number of bytes written to \p sig.
  499. * \param f_rng RNG function
  500. * \param p_rng RNG parameter
  501. * \param rs_ctx Restart context (NULL to disable restart)
  502. *
  503. * \return See \c mbedtls_pk_sign().
  504. * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  505. * operations was reached: see \c mbedtls_ecp_set_max_ops().
  506. */
  507. int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
  508. mbedtls_md_type_t md_alg,
  509. const unsigned char *hash, size_t hash_len,
  510. unsigned char *sig, size_t *sig_len,
  511. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  512. mbedtls_pk_restart_ctx *rs_ctx );
  513. /**
  514. * \brief Decrypt message (including padding if relevant).
  515. *
  516. * \param ctx The PK context to use. It must have been set up
  517. * with a private key.
  518. * \param input Input to decrypt
  519. * \param ilen Input size
  520. * \param output Decrypted output
  521. * \param olen Decrypted message length
  522. * \param osize Size of the output buffer
  523. * \param f_rng RNG function
  524. * \param p_rng RNG parameter
  525. *
  526. * \note For RSA keys, the default padding type is PKCS#1 v1.5.
  527. *
  528. * \return 0 on success, or a specific error code.
  529. */
  530. int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
  531. const unsigned char *input, size_t ilen,
  532. unsigned char *output, size_t *olen, size_t osize,
  533. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  534. /**
  535. * \brief Encrypt message (including padding if relevant).
  536. *
  537. * \param ctx The PK context to use. It must have been set up.
  538. * \param input Message to encrypt
  539. * \param ilen Message size
  540. * \param output Encrypted output
  541. * \param olen Encrypted output length
  542. * \param osize Size of the output buffer
  543. * \param f_rng RNG function
  544. * \param p_rng RNG parameter
  545. *
  546. * \note For RSA keys, the default padding type is PKCS#1 v1.5.
  547. *
  548. * \return 0 on success, or a specific error code.
  549. */
  550. int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
  551. const unsigned char *input, size_t ilen,
  552. unsigned char *output, size_t *olen, size_t osize,
  553. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  554. /**
  555. * \brief Check if a public-private pair of keys matches.
  556. *
  557. * \param pub Context holding a public key.
  558. * \param prv Context holding a private (and public) key.
  559. *
  560. * \return \c 0 on success (keys were checked and match each other).
  561. * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the keys could not
  562. * be checked - in that case they may or may not match.
  563. * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA if a context is invalid.
  564. * \return Another non-zero value if the keys do not match.
  565. */
  566. int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
  567. /**
  568. * \brief Export debug information
  569. *
  570. * \param ctx The PK context to use. It must have been initialized.
  571. * \param items Place to write debug items
  572. *
  573. * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
  574. */
  575. int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items );
  576. /**
  577. * \brief Access the type name
  578. *
  579. * \param ctx The PK context to use. It must have been initialized.
  580. *
  581. * \return Type name on success, or "invalid PK"
  582. */
  583. const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
  584. /**
  585. * \brief Get the key type
  586. *
  587. * \param ctx The PK context to use. It must have been initialized.
  588. *
  589. * \return Type on success.
  590. * \return #MBEDTLS_PK_NONE for a context that has not been set up.
  591. */
  592. mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx );
  593. #if defined(MBEDTLS_PK_PARSE_C)
  594. /** \ingroup pk_module */
  595. /**
  596. * \brief Parse a private key in PEM or DER format
  597. *
  598. * \param ctx The PK context to fill. It must have been initialized
  599. * but not set up.
  600. * \param key Input buffer to parse.
  601. * The buffer must contain the input exactly, with no
  602. * extra trailing material. For PEM, the buffer must
  603. * contain a null-terminated string.
  604. * \param keylen Size of \b key in bytes.
  605. * For PEM data, this includes the terminating null byte,
  606. * so \p keylen must be equal to `strlen(key) + 1`.
  607. * \param pwd Optional password for decryption.
  608. * Pass \c NULL if expecting a non-encrypted key.
  609. * Pass a string of \p pwdlen bytes if expecting an encrypted
  610. * key; a non-encrypted key will also be accepted.
  611. * The empty password is not supported.
  612. * \param pwdlen Size of the password in bytes.
  613. * Ignored if \p pwd is \c NULL.
  614. *
  615. * \note On entry, ctx must be empty, either freshly initialised
  616. * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
  617. * specific key type, check the result with mbedtls_pk_can_do().
  618. *
  619. * \note The key is also checked for correctness.
  620. *
  621. * \return 0 if successful, or a specific PK or PEM error code
  622. */
  623. int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
  624. const unsigned char *key, size_t keylen,
  625. const unsigned char *pwd, size_t pwdlen );
  626. /** \ingroup pk_module */
  627. /**
  628. * \brief Parse a public key in PEM or DER format
  629. *
  630. * \param ctx The PK context to fill. It must have been initialized
  631. * but not set up.
  632. * \param key Input buffer to parse.
  633. * The buffer must contain the input exactly, with no
  634. * extra trailing material. For PEM, the buffer must
  635. * contain a null-terminated string.
  636. * \param keylen Size of \b key in bytes.
  637. * For PEM data, this includes the terminating null byte,
  638. * so \p keylen must be equal to `strlen(key) + 1`.
  639. *
  640. * \note On entry, ctx must be empty, either freshly initialised
  641. * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
  642. * specific key type, check the result with mbedtls_pk_can_do().
  643. *
  644. * \note The key is also checked for correctness.
  645. *
  646. * \return 0 if successful, or a specific PK or PEM error code
  647. */
  648. int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
  649. const unsigned char *key, size_t keylen );
  650. #if defined(MBEDTLS_FS_IO)
  651. /** \ingroup pk_module */
  652. /**
  653. * \brief Load and parse a private key
  654. *
  655. * \param ctx The PK context to fill. It must have been initialized
  656. * but not set up.
  657. * \param path filename to read the private key from
  658. * \param password Optional password to decrypt the file.
  659. * Pass \c NULL if expecting a non-encrypted key.
  660. * Pass a null-terminated string if expecting an encrypted
  661. * key; a non-encrypted key will also be accepted.
  662. * The empty password is not supported.
  663. *
  664. * \note On entry, ctx must be empty, either freshly initialised
  665. * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
  666. * specific key type, check the result with mbedtls_pk_can_do().
  667. *
  668. * \note The key is also checked for correctness.
  669. *
  670. * \return 0 if successful, or a specific PK or PEM error code
  671. */
  672. int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
  673. const char *path, const char *password );
  674. /** \ingroup pk_module */
  675. /**
  676. * \brief Load and parse a public key
  677. *
  678. * \param ctx The PK context to fill. It must have been initialized
  679. * but not set up.
  680. * \param path filename to read the public key from
  681. *
  682. * \note On entry, ctx must be empty, either freshly initialised
  683. * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If
  684. * you need a specific key type, check the result with
  685. * mbedtls_pk_can_do().
  686. *
  687. * \note The key is also checked for correctness.
  688. *
  689. * \return 0 if successful, or a specific PK or PEM error code
  690. */
  691. int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path );
  692. #endif /* MBEDTLS_FS_IO */
  693. #endif /* MBEDTLS_PK_PARSE_C */
  694. #if defined(MBEDTLS_PK_WRITE_C)
  695. /**
  696. * \brief Write a private key to a PKCS#1 or SEC1 DER structure
  697. * Note: data is written at the end of the buffer! Use the
  698. * return value to determine where you should start
  699. * using the buffer
  700. *
  701. * \param ctx PK context which must contain a valid private key.
  702. * \param buf buffer to write to
  703. * \param size size of the buffer
  704. *
  705. * \return length of data written if successful, or a specific
  706. * error code
  707. */
  708. int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
  709. /**
  710. * \brief Write a public key to a SubjectPublicKeyInfo DER structure
  711. * Note: data is written at the end of the buffer! Use the
  712. * return value to determine where you should start
  713. * using the buffer
  714. *
  715. * \param ctx PK context which must contain a valid public or private key.
  716. * \param buf buffer to write to
  717. * \param size size of the buffer
  718. *
  719. * \return length of data written if successful, or a specific
  720. * error code
  721. */
  722. int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
  723. #if defined(MBEDTLS_PEM_WRITE_C)
  724. /**
  725. * \brief Write a public key to a PEM string
  726. *
  727. * \param ctx PK context which must contain a valid public or private key.
  728. * \param buf Buffer to write to. The output includes a
  729. * terminating null byte.
  730. * \param size Size of the buffer in bytes.
  731. *
  732. * \return 0 if successful, or a specific error code
  733. */
  734. int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
  735. /**
  736. * \brief Write a private key to a PKCS#1 or SEC1 PEM string
  737. *
  738. * \param ctx PK context which must contain a valid private key.
  739. * \param buf Buffer to write to. The output includes a
  740. * terminating null byte.
  741. * \param size Size of the buffer in bytes.
  742. *
  743. * \return 0 if successful, or a specific error code
  744. */
  745. int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
  746. #endif /* MBEDTLS_PEM_WRITE_C */
  747. #endif /* MBEDTLS_PK_WRITE_C */
  748. /*
  749. * WARNING: Low-level functions. You probably do not want to use these unless
  750. * you are certain you do ;)
  751. */
  752. #if defined(MBEDTLS_PK_PARSE_C)
  753. /**
  754. * \brief Parse a SubjectPublicKeyInfo DER structure
  755. *
  756. * \param p the position in the ASN.1 data
  757. * \param end end of the buffer
  758. * \param pk The PK context to fill. It must have been initialized
  759. * but not set up.
  760. *
  761. * \return 0 if successful, or a specific PK error code
  762. */
  763. int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
  764. mbedtls_pk_context *pk );
  765. #endif /* MBEDTLS_PK_PARSE_C */
  766. #if defined(MBEDTLS_PK_WRITE_C)
  767. /**
  768. * \brief Write a subjectPublicKey to ASN.1 data
  769. * Note: function works backwards in data buffer
  770. *
  771. * \param p reference to current position pointer
  772. * \param start start of the buffer (for bounds-checking)
  773. * \param key PK context which must contain a valid public or private key.
  774. *
  775. * \return the length written or a negative error code
  776. */
  777. int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
  778. const mbedtls_pk_context *key );
  779. #endif /* MBEDTLS_PK_WRITE_C */
  780. /*
  781. * Internal module functions. You probably do not want to use these unless you
  782. * know you do.
  783. */
  784. #if defined(MBEDTLS_FS_IO)
  785. int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
  786. #endif
  787. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  788. /**
  789. * \brief Turn an EC key into an opaque one.
  790. *
  791. * \warning This is a temporary utility function for tests. It might
  792. * change or be removed at any time without notice.
  793. *
  794. * \note Only ECDSA keys are supported so far. Signing with the
  795. * specified hash is the only allowed use of that key.
  796. *
  797. * \param pk Input: the EC key to import to a PSA key.
  798. * Output: a PK context wrapping that PSA key.
  799. * \param key Output: a PSA key identifier.
  800. * It's the caller's responsibility to call
  801. * psa_destroy_key() on that key identifier after calling
  802. * mbedtls_pk_free() on the PK context.
  803. * \param hash_alg The hash algorithm to allow for use with that key.
  804. *
  805. * \return \c 0 if successful.
  806. * \return An Mbed TLS error code otherwise.
  807. */
  808. int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
  809. psa_key_id_t *key,
  810. psa_algorithm_t hash_alg );
  811. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  812. #ifdef __cplusplus
  813. }
  814. #endif
  815. #endif /* MBEDTLS_PK_H */