text
[RRG-proxmark3.git] / common / mbedtls / pk.h
blobc00a5c10c1df7f5dc4ac80e816567e75277ed6ef
1 /**
2 * \file pk.h
4 * \brief Public Key abstraction layer
5 */
6 /*
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #ifndef MBEDTLS_PK_H
24 #define MBEDTLS_PK_H
26 #if !defined(MBEDTLS_CONFIG_FILE)
27 #include "mbedtls/config.h"
28 #else
29 #include MBEDTLS_CONFIG_FILE
30 #endif
32 #include "mbedtls/md.h"
34 #if defined(MBEDTLS_RSA_C)
35 #include "mbedtls/rsa.h"
36 #endif
38 #if defined(MBEDTLS_ECP_C)
39 #include "mbedtls/ecp.h"
40 #endif
42 #if defined(MBEDTLS_ECDSA_C)
43 #include "mbedtls/ecdsa.h"
44 #endif
46 #if defined(MBEDTLS_USE_PSA_CRYPTO)
47 #include "psa/crypto.h"
48 #endif
50 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
51 !defined(inline) && !defined(__cplusplus)
52 #define inline __inline
53 #endif
55 #define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80 /**< Memory allocation failed. */
56 #define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00 /**< Type mismatch, eg attempt to encrypt with an ECDSA key */
57 #define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80 /**< Bad input parameters to function. */
58 #define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00 /**< Read/write of file failed. */
59 #define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80 /**< Unsupported key version */
60 #define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00 /**< Invalid key tag or value. */
61 #define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80 /**< Key algorithm is unsupported (only RSA and EC are supported). */
62 #define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00 /**< Private key password can't be empty. */
63 #define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80 /**< Given private key password does not allow for correct decryption. */
64 #define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00 /**< The pubkey tag or value is invalid (only RSA and EC are supported). */
65 #define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80 /**< The algorithm tag or value is invalid. */
66 #define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00 /**< Elliptic curve is unsupported (only NIST curves are supported). */
67 #define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980 /**< Unavailable feature, e.g. RSA disabled for RSA key. */
68 #define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900 /**< The buffer contains a valid signature followed by more data. */
70 /* MBEDTLS_ERR_PK_HW_ACCEL_FAILED is deprecated and should not be used. */
71 #define MBEDTLS_ERR_PK_HW_ACCEL_FAILED -0x3880 /**< PK hardware accelerator failed. */
73 #ifdef __cplusplus
74 extern "C" {
75 #endif
77 /**
78 * \brief Public key types
80 typedef enum {
81 MBEDTLS_PK_NONE = 0,
82 MBEDTLS_PK_RSA,
83 MBEDTLS_PK_ECKEY,
84 MBEDTLS_PK_ECKEY_DH,
85 MBEDTLS_PK_ECDSA,
86 MBEDTLS_PK_RSA_ALT,
87 MBEDTLS_PK_RSASSA_PSS,
88 MBEDTLS_PK_OPAQUE,
89 } mbedtls_pk_type_t;
91 /**
92 * \brief Options for RSASSA-PSS signature verification.
93 * See \c mbedtls_rsa_rsassa_pss_verify_ext()
95 typedef struct mbedtls_pk_rsassa_pss_options {
96 mbedtls_md_type_t mgf1_hash_id;
97 int expected_salt_len;
99 } mbedtls_pk_rsassa_pss_options;
102 * \brief Maximum size of a signature made by mbedtls_pk_sign().
104 /* We need to set MBEDTLS_PK_SIGNATURE_MAX_SIZE to the maximum signature
105 * size among the supported signature types. Do it by starting at 0,
106 * then incrementally increasing to be large enough for each supported
107 * signature mechanism.
109 * The resulting value can be 0, for example if MBEDTLS_ECDH_C is enabled
110 * (which allows the pk module to be included) but neither MBEDTLS_ECDSA_C
111 * nor MBEDTLS_RSA_C nor any opaque signature mechanism (PSA or RSA_ALT).
113 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE 0
115 #if ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT) ) && \
116 MBEDTLS_MPI_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
117 /* For RSA, the signature can be as large as the bignum module allows.
118 * For RSA_ALT, the signature size is not necessarily tied to what the
119 * bignum module can do, but in the absence of any specific setting,
120 * we use that (rsa_alt_sign_wrap in pk_wrap will check). */
121 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
122 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
123 #endif
125 #if defined(MBEDTLS_ECDSA_C) && \
126 MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_PK_SIGNATURE_MAX_SIZE
127 /* For ECDSA, the ecdsa module exports a constant for the maximum
128 * signature size. */
129 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
130 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
131 #endif
133 #if defined(MBEDTLS_USE_PSA_CRYPTO)
134 #if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
135 /* PSA_SIGNATURE_MAX_SIZE is the maximum size of a signature made
136 * through the PSA API in the PSA representation. */
137 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
138 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE
139 #endif
141 #if PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 > MBEDTLS_PK_SIGNATURE_MAX_SIZE
142 /* The Mbed TLS representation is different for ECDSA signatures:
143 * PSA uses the raw concatenation of r and s,
144 * whereas Mbed TLS uses the ASN.1 representation (SEQUENCE of two INTEGERs).
145 * Add the overhead of ASN.1: up to (1+2) + 2 * (1+2+1) for the
146 * types, lengths (represented by up to 2 bytes), and potential leading
147 * zeros of the INTEGERs and the SEQUENCE. */
148 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
149 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE ( PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 )
150 #endif
151 #endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */
154 * \brief Types for interfacing with the debug module
156 typedef enum {
157 MBEDTLS_PK_DEBUG_NONE = 0,
158 MBEDTLS_PK_DEBUG_MPI,
159 MBEDTLS_PK_DEBUG_ECP,
160 } mbedtls_pk_debug_type;
163 * \brief Item to send to the debug module
165 typedef struct mbedtls_pk_debug_item {
166 mbedtls_pk_debug_type type;
167 const char *name;
168 void *value;
169 } mbedtls_pk_debug_item;
171 /** Maximum number of item send for debugging, plus 1 */
172 #define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
175 * \brief Public key information and operations
177 typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
180 * \brief Public key container
182 typedef struct mbedtls_pk_context {
183 const mbedtls_pk_info_t *pk_info; /**< Public key information */
184 void *pk_ctx; /**< Underlying public key context */
185 } mbedtls_pk_context;
187 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
189 * \brief Context for resuming operations
191 typedef struct {
192 const mbedtls_pk_info_t *pk_info; /**< Public key information */
193 void *rs_ctx; /**< Underlying restart context */
194 } mbedtls_pk_restart_ctx;
195 #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
196 /* Now we can declare functions that take a pointer to that */
197 typedef void mbedtls_pk_restart_ctx;
198 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
200 #if defined(MBEDTLS_RSA_C)
202 * Quick access to an RSA context inside a PK context.
204 * \warning You must make sure the PK context actually holds an RSA context
205 * before using this function!
207 static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk) {
208 return ((mbedtls_rsa_context *)(pk).pk_ctx);
210 #endif /* MBEDTLS_RSA_C */
212 #if defined(MBEDTLS_ECP_C)
214 * Quick access to an EC context inside a PK context.
216 * \warning You must make sure the PK context actually holds an EC context
217 * before using this function!
219 static inline mbedtls_ecp_keypair *mbedtls_pk_ec(const mbedtls_pk_context pk) {
220 return ((mbedtls_ecp_keypair *)(pk).pk_ctx);
222 #endif /* MBEDTLS_ECP_C */
224 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
226 * \brief Types for RSA-alt abstraction
228 typedef int (*mbedtls_pk_rsa_alt_decrypt_func)(void *ctx, int mode, size_t *olen,
229 const unsigned char *input, unsigned char *output,
230 size_t output_max_len);
231 typedef int (*mbedtls_pk_rsa_alt_sign_func)(void *ctx,
232 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
233 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
234 const unsigned char *hash, unsigned char *sig);
235 typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)(void *ctx);
236 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
239 * \brief Return information associated with the given PK type
241 * \param pk_type PK type to search for.
243 * \return The PK info associated with the type or NULL if not found.
245 const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type);
248 * \brief Initialize a #mbedtls_pk_context (as NONE).
250 * \param ctx The context to initialize.
251 * This must not be \c NULL.
253 void mbedtls_pk_init(mbedtls_pk_context *ctx);
256 * \brief Free the components of a #mbedtls_pk_context.
258 * \param ctx The context to clear. It must have been initialized.
259 * If this is \c NULL, this function does nothing.
261 * \note For contexts that have been set up with
262 * mbedtls_pk_setup_opaque(), this does not free the underlying
263 * PSA key and you still need to call psa_destroy_key()
264 * independently if you want to destroy that key.
266 void mbedtls_pk_free(mbedtls_pk_context *ctx);
268 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
270 * \brief Initialize a restart context
272 * \param ctx The context to initialize.
273 * This must not be \c NULL.
275 void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx);
278 * \brief Free the components of a restart context
280 * \param ctx The context to clear. It must have been initialized.
281 * If this is \c NULL, this function does nothing.
283 void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx);
284 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
287 * \brief Initialize a PK context with the information given
288 * and allocates the type-specific PK subcontext.
290 * \param ctx Context to initialize. It must not have been set
291 * up yet (type #MBEDTLS_PK_NONE).
292 * \param info Information to use
294 * \return 0 on success,
295 * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
296 * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
298 * \note For contexts holding an RSA-alt key, use
299 * \c mbedtls_pk_setup_rsa_alt() instead.
301 int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info);
303 #if defined(MBEDTLS_USE_PSA_CRYPTO)
305 * \brief Initialize a PK context to wrap a PSA key.
307 * \note This function replaces mbedtls_pk_setup() for contexts
308 * that wrap a (possibly opaque) PSA key instead of
309 * storing and manipulating the key material directly.
311 * \param ctx The context to initialize. It must be empty (type NONE).
312 * \param key The PSA key to wrap, which must hold an ECC key pair
313 * (see notes below).
315 * \note The wrapped key must remain valid as long as the
316 * wrapping PK context is in use, that is at least between
317 * the point this function is called and the point
318 * mbedtls_pk_free() is called on this context. The wrapped
319 * key might then be independently used or destroyed.
321 * \note This function is currently only available for ECC key
322 * pairs (that is, ECC keys containing private key material).
323 * Support for other key types may be added later.
325 * \return \c 0 on success.
326 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input
327 * (context already used, invalid key identifier).
328 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the key is not an
329 * ECC key pair.
330 * \return #MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
332 int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
333 const psa_key_id_t key);
334 #endif /* MBEDTLS_USE_PSA_CRYPTO */
336 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
338 * \brief Initialize an RSA-alt context
340 * \param ctx Context to initialize. It must not have been set
341 * up yet (type #MBEDTLS_PK_NONE).
342 * \param key RSA key pointer
343 * \param decrypt_func Decryption function
344 * \param sign_func Signing function
345 * \param key_len_func Function returning key length in bytes
347 * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
348 * context wasn't already initialized as RSA_ALT.
350 * \note This function replaces \c mbedtls_pk_setup() for RSA-alt.
352 int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
353 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
354 mbedtls_pk_rsa_alt_sign_func sign_func,
355 mbedtls_pk_rsa_alt_key_len_func key_len_func);
356 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
359 * \brief Get the size in bits of the underlying key
361 * \param ctx The context to query. It must have been initialized.
363 * \return Key size in bits, or 0 on error
365 size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx);
368 * \brief Get the length in bytes of the underlying key
370 * \param ctx The context to query. It must have been initialized.
372 * \return Key length in bytes, or 0 on error
374 static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx) {
375 return ((mbedtls_pk_get_bitlen(ctx) + 7) / 8);
379 * \brief Tell if a context can do the operation given by type
381 * \param ctx The context to query. It must have been initialized.
382 * \param type The desired type.
384 * \return 1 if the context can do operations on the given type.
385 * \return 0 if the context cannot do the operations on the given
386 * type. This is always the case for a context that has
387 * been initialized but not set up, or that has been
388 * cleared with mbedtls_pk_free().
390 int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type);
393 * \brief Verify signature (including padding if relevant).
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
402 * \return 0 on success (signature is valid),
403 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
404 * signature in sig but its length is less than \p siglen,
405 * or a specific error code.
407 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
408 * Use \c mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, ... )
409 * to verify RSASSA_PSS signatures.
411 * \note If hash_len is 0, then the length associated with md_alg
412 * is used instead, or an error returned if it is invalid.
414 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
416 int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
417 const unsigned char *hash, size_t hash_len,
418 const unsigned char *sig, size_t sig_len);
421 * \brief Restartable version of \c mbedtls_pk_verify()
423 * \note Performs the same job as \c mbedtls_pk_verify(), but can
424 * return early and restart according to the limit set with
425 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
426 * operations. For RSA, same as \c mbedtls_pk_verify().
428 * \param ctx The PK context to use. It must have been set up.
429 * \param md_alg Hash algorithm used (see notes)
430 * \param hash Hash of the message to sign
431 * \param hash_len Hash length or 0 (see notes)
432 * \param sig Signature to verify
433 * \param sig_len Signature length
434 * \param rs_ctx Restart context (NULL to disable restart)
436 * \return See \c mbedtls_pk_verify(), or
437 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
438 * operations was reached: see \c mbedtls_ecp_set_max_ops().
440 int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
441 mbedtls_md_type_t md_alg,
442 const unsigned char *hash, size_t hash_len,
443 const unsigned char *sig, size_t sig_len,
444 mbedtls_pk_restart_ctx *rs_ctx);
447 * \brief Verify signature, with options.
448 * (Includes verification of the padding depending on type.)
450 * \param type Signature type (inc. possible padding type) to verify
451 * \param options Pointer to type-specific options, or NULL
452 * \param ctx The PK context to use. It must have been set up.
453 * \param md_alg Hash algorithm used (see notes)
454 * \param hash Hash of the message to sign
455 * \param hash_len Hash length or 0 (see notes)
456 * \param sig Signature to verify
457 * \param sig_len Signature length
459 * \return 0 on success (signature is valid),
460 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
461 * used for this type of signatures,
462 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
463 * signature in sig but its length is less than \p siglen,
464 * or a specific error code.
466 * \note If hash_len is 0, then the length associated with md_alg
467 * is used instead, or an error returned if it is invalid.
469 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
471 * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point
472 * to a mbedtls_pk_rsassa_pss_options structure,
473 * otherwise it must be NULL.
475 int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
476 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
477 const unsigned char *hash, size_t hash_len,
478 const unsigned char *sig, size_t sig_len);
481 * \brief Make signature, including padding if relevant.
483 * \param ctx The PK context to use. It must have been set up
484 * with a private key.
485 * \param md_alg Hash algorithm used (see notes)
486 * \param hash Hash of the message to sign
487 * \param hash_len Hash length or 0 (see notes)
488 * \param sig Place to write the signature.
489 * It must have enough room for the signature.
490 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
491 * You may use a smaller buffer if it is large enough
492 * given the key type.
493 * \param sig_len On successful return,
494 * the number of bytes written to \p sig.
495 * \param f_rng RNG function
496 * \param p_rng RNG parameter
498 * \return 0 on success, or a specific error code.
500 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
501 * There is no interface in the PK module to make RSASSA-PSS
502 * signatures yet.
504 * \note If hash_len is 0, then the length associated with md_alg
505 * is used instead, or an error returned if it is invalid.
507 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
508 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
510 int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
511 const unsigned char *hash, size_t hash_len,
512 unsigned char *sig, size_t *sig_len,
513 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
516 * \brief Restartable version of \c mbedtls_pk_sign()
518 * \note Performs the same job as \c mbedtls_pk_sign(), but can
519 * return early and restart according to the limit set with
520 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
521 * operations. For RSA, same as \c mbedtls_pk_sign().
523 * \param ctx The PK context to use. It must have been set up
524 * with a private key.
525 * \param md_alg Hash algorithm used (see notes for mbedtls_pk_sign())
526 * \param hash Hash of the message to sign
527 * \param hash_len Hash length or 0 (see notes for mbedtls_pk_sign())
528 * \param sig Place to write the signature.
529 * It must have enough room for the signature.
530 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
531 * You may use a smaller buffer if it is large enough
532 * given the key type.
533 * \param sig_len On successful return,
534 * the number of bytes written to \p sig.
535 * \param f_rng RNG function
536 * \param p_rng RNG parameter
537 * \param rs_ctx Restart context (NULL to disable restart)
539 * \return See \c mbedtls_pk_sign().
540 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
541 * operations was reached: see \c mbedtls_ecp_set_max_ops().
543 int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
544 mbedtls_md_type_t md_alg,
545 const unsigned char *hash, size_t hash_len,
546 unsigned char *sig, size_t *sig_len,
547 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
548 mbedtls_pk_restart_ctx *rs_ctx);
551 * \brief Decrypt message (including padding if relevant).
553 * \param ctx The PK context to use. It must have been set up
554 * with a private key.
555 * \param input Input to decrypt
556 * \param ilen Input size
557 * \param output Decrypted output
558 * \param olen Decrypted message length
559 * \param osize Size of the output buffer
560 * \param f_rng RNG function
561 * \param p_rng RNG parameter
563 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
565 * \return 0 on success, or a specific error code.
567 int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
568 const unsigned char *input, size_t ilen,
569 unsigned char *output, size_t *olen, size_t osize,
570 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
573 * \brief Encrypt message (including padding if relevant).
575 * \param ctx The PK context to use. It must have been set up.
576 * \param input Message to encrypt
577 * \param ilen Message size
578 * \param output Encrypted output
579 * \param olen Encrypted output length
580 * \param osize Size of the output buffer
581 * \param f_rng RNG function
582 * \param p_rng RNG parameter
584 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
586 * \return 0 on success, or a specific error code.
588 int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
589 const unsigned char *input, size_t ilen,
590 unsigned char *output, size_t *olen, size_t osize,
591 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
594 * \brief Check if a public-private pair of keys matches.
596 * \param pub Context holding a public key.
597 * \param prv Context holding a private (and public) key.
599 * \return \c 0 on success (keys were checked and match each other).
600 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the keys could not
601 * be checked - in that case they may or may not match.
602 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA if a context is invalid.
603 * \return Another non-zero value if the keys do not match.
605 int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv);
608 * \brief Export debug information
610 * \param ctx The PK context to use. It must have been initialized.
611 * \param items Place to write debug items
613 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
615 int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items);
618 * \brief Access the type name
620 * \param ctx The PK context to use. It must have been initialized.
622 * \return Type name on success, or "invalid PK"
624 const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx);
627 * \brief Get the key type
629 * \param ctx The PK context to use. It must have been initialized.
631 * \return Type on success.
632 * \return #MBEDTLS_PK_NONE for a context that has not been set up.
634 mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx);
636 #if defined(MBEDTLS_PK_PARSE_C)
637 /** \ingroup pk_module */
639 * \brief Parse a private key in PEM or DER format
641 * \param ctx The PK context to fill. It must have been initialized
642 * but not set up.
643 * \param key Input buffer to parse.
644 * The buffer must contain the input exactly, with no
645 * extra trailing material. For PEM, the buffer must
646 * contain a null-terminated string.
647 * \param keylen Size of \b key in bytes.
648 * For PEM data, this includes the terminating null byte,
649 * so \p keylen must be equal to `strlen(key) + 1`.
650 * \param pwd Optional password for decryption.
651 * Pass \c NULL if expecting a non-encrypted key.
652 * Pass a string of \p pwdlen bytes if expecting an encrypted
653 * key; a non-encrypted key will also be accepted.
654 * The empty password is not supported.
655 * \param pwdlen Size of the password in bytes.
656 * Ignored if \p pwd is \c NULL.
658 * \note On entry, ctx must be empty, either freshly initialised
659 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
660 * specific key type, check the result with mbedtls_pk_can_do().
662 * \note The key is also checked for correctness.
664 * \return 0 if successful, or a specific PK or PEM error code
666 int mbedtls_pk_parse_key(mbedtls_pk_context *ctx,
667 const unsigned char *key, size_t keylen,
668 const unsigned char *pwd, size_t pwdlen);
670 /** \ingroup pk_module */
672 * \brief Parse a public key in PEM or DER format
674 * \param ctx The PK context to fill. It must have been initialized
675 * but not set up.
676 * \param key Input buffer to parse.
677 * The buffer must contain the input exactly, with no
678 * extra trailing material. For PEM, the buffer must
679 * contain a null-terminated string.
680 * \param keylen Size of \b key in bytes.
681 * For PEM data, this includes the terminating null byte,
682 * so \p keylen must be equal to `strlen(key) + 1`.
684 * \note On entry, ctx must be empty, either freshly initialised
685 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
686 * specific key type, check the result with mbedtls_pk_can_do().
688 * \note The key is also checked for correctness.
690 * \return 0 if successful, or a specific PK or PEM error code
692 int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
693 const unsigned char *key, size_t keylen);
695 #if defined(MBEDTLS_FS_IO)
696 /** \ingroup pk_module */
698 * \brief Load and parse a private key
700 * \param ctx The PK context to fill. It must have been initialized
701 * but not set up.
702 * \param path filename to read the private key from
703 * \param password Optional password to decrypt the file.
704 * Pass \c NULL if expecting a non-encrypted key.
705 * Pass a null-terminated string if expecting an encrypted
706 * key; a non-encrypted key will also be accepted.
707 * The empty password is not supported.
709 * \note On entry, ctx must be empty, either freshly initialised
710 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
711 * specific key type, check the result with mbedtls_pk_can_do().
713 * \note The key is also checked for correctness.
715 * \return 0 if successful, or a specific PK or PEM error code
717 int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
718 const char *path, const char *password);
720 /** \ingroup pk_module */
722 * \brief Load and parse a public key
724 * \param ctx The PK context to fill. It must have been initialized
725 * but not set up.
726 * \param path filename to read the public key from
728 * \note On entry, ctx must be empty, either freshly initialised
729 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If
730 * you need a specific key type, check the result with
731 * mbedtls_pk_can_do().
733 * \note The key is also checked for correctness.
735 * \return 0 if successful, or a specific PK or PEM error code
737 int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path);
738 #endif /* MBEDTLS_FS_IO */
739 #endif /* MBEDTLS_PK_PARSE_C */
741 #if defined(MBEDTLS_PK_WRITE_C)
743 * \brief Write a private key to a PKCS#1 or SEC1 DER structure
744 * Note: data is written at the end of the buffer! Use the
745 * return value to determine where you should start
746 * using the buffer
748 * \param ctx PK context which must contain a valid private key.
749 * \param buf buffer to write to
750 * \param size size of the buffer
752 * \return length of data written if successful, or a specific
753 * error code
755 int mbedtls_pk_write_key_der(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
758 * \brief Write a public key to a SubjectPublicKeyInfo DER structure
759 * Note: data is written at the end of the buffer! Use the
760 * return value to determine where you should start
761 * using the buffer
763 * \param ctx PK context which must contain a valid public or private key.
764 * \param buf buffer to write to
765 * \param size size of the buffer
767 * \return length of data written if successful, or a specific
768 * error code
770 int mbedtls_pk_write_pubkey_der(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
772 #if defined(MBEDTLS_PEM_WRITE_C)
774 * \brief Write a public key to a PEM string
776 * \param ctx PK context which must contain a valid public or private key.
777 * \param buf Buffer to write to. The output includes a
778 * terminating null byte.
779 * \param size Size of the buffer in bytes.
781 * \return 0 if successful, or a specific error code
783 int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
786 * \brief Write a private key to a PKCS#1 or SEC1 PEM string
788 * \param ctx PK context which must contain a valid private key.
789 * \param buf Buffer to write to. The output includes a
790 * terminating null byte.
791 * \param size Size of the buffer in bytes.
793 * \return 0 if successful, or a specific error code
795 int mbedtls_pk_write_key_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
796 #endif /* MBEDTLS_PEM_WRITE_C */
797 #endif /* MBEDTLS_PK_WRITE_C */
800 * WARNING: Low-level functions. You probably do not want to use these unless
801 * you are certain you do ;)
804 #if defined(MBEDTLS_PK_PARSE_C)
806 * \brief Parse a SubjectPublicKeyInfo DER structure
808 * \param p the position in the ASN.1 data
809 * \param end end of the buffer
810 * \param pk The PK context to fill. It must have been initialized
811 * but not set up.
813 * \return 0 if successful, or a specific PK error code
815 int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
816 mbedtls_pk_context *pk);
817 #endif /* MBEDTLS_PK_PARSE_C */
819 #if defined(MBEDTLS_PK_WRITE_C)
821 * \brief Write a subjectPublicKey to ASN.1 data
822 * Note: function works backwards in data buffer
824 * \param p reference to current position pointer
825 * \param start start of the buffer (for bounds-checking)
826 * \param key PK context which must contain a valid public or private key.
828 * \return the length written or a negative error code
830 int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
831 const mbedtls_pk_context *key);
832 #endif /* MBEDTLS_PK_WRITE_C */
835 * Internal module functions. You probably do not want to use these unless you
836 * know you do.
838 #if defined(MBEDTLS_FS_IO)
839 int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n);
840 #endif
842 #if defined(MBEDTLS_USE_PSA_CRYPTO)
844 * \brief Turn an EC key into an opaque one.
846 * \warning This is a temporary utility function for tests. It might
847 * change or be removed at any time without notice.
849 * \note Only ECDSA keys are supported so far. Signing with the
850 * specified hash is the only allowed use of that key.
852 * \param pk Input: the EC key to import to a PSA key.
853 * Output: a PK context wrapping that PSA key.
854 * \param key Output: a PSA key identifier.
855 * It's the caller's responsibility to call
856 * psa_destroy_key() on that key identifier after calling
857 * mbedtls_pk_free() on the PK context.
858 * \param hash_alg The hash algorithm to allow for use with that key.
860 * \return \c 0 if successful.
861 * \return An Mbed TLS error code otherwise.
863 int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
864 psa_key_id_t *key,
865 psa_algorithm_t hash_alg);
866 #endif /* MBEDTLS_USE_PSA_CRYPTO */
868 #ifdef __cplusplus
870 #endif
872 #endif /* MBEDTLS_PK_H */