Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.  * \file pk.h
  3.  *
  4.  * \brief Public Key abstraction layer
  5.  */
  6. /*
  7.  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  8.  *  SPDX-License-Identifier: GPL-2.0
  9.  *
  10.  *  This program is free software; you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation; either version 2 of the License, or
  13.  *  (at your option) any later version.
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License along
  21.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  22.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23.  *
  24.  *  This file is part of mbed TLS (https://tls.mbed.org)
  25.  */
  26.  
  27. #ifndef MBEDTLS_PK_H
  28. #define MBEDTLS_PK_H
  29.  
  30. #if !defined(MBEDTLS_CONFIG_FILE)
  31. #include "config.h"
  32. #else
  33. #include MBEDTLS_CONFIG_FILE
  34. #endif
  35.  
  36. #include "md.h"
  37.  
  38. #if defined(MBEDTLS_RSA_C)
  39. #include "rsa.h"
  40. #endif
  41.  
  42. #if defined(MBEDTLS_ECP_C)
  43. #include "ecp.h"
  44. #endif
  45.  
  46. #if defined(MBEDTLS_ECDSA_C)
  47. #include "ecdsa.h"
  48. #endif
  49.  
  50. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  51.     !defined(inline) && !defined(__cplusplus)
  52. #define inline __inline
  53. #endif
  54.  
  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. */
  69.  
  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. */
  72.  
  73. #ifdef __cplusplus
  74. extern "C" {
  75. #endif
  76.  
  77. /**
  78.  * \brief          Public key types
  79.  */
  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_type_t;
  89.  
  90. /**
  91.  * \brief           Options for RSASSA-PSS signature verification.
  92.  *                  See \c mbedtls_rsa_rsassa_pss_verify_ext()
  93.  */
  94. typedef struct mbedtls_pk_rsassa_pss_options
  95. {
  96.     mbedtls_md_type_t mgf1_hash_id;
  97.     int expected_salt_len;
  98.  
  99. } mbedtls_pk_rsassa_pss_options;
  100.  
  101. /**
  102.  * \brief           Types for interfacing with the debug module
  103.  */
  104. typedef enum
  105. {
  106.     MBEDTLS_PK_DEBUG_NONE = 0,
  107.     MBEDTLS_PK_DEBUG_MPI,
  108.     MBEDTLS_PK_DEBUG_ECP,
  109. } mbedtls_pk_debug_type;
  110.  
  111. /**
  112.  * \brief           Item to send to the debug module
  113.  */
  114. typedef struct mbedtls_pk_debug_item
  115. {
  116.     mbedtls_pk_debug_type type;
  117.     const char *name;
  118.     void *value;
  119. } mbedtls_pk_debug_item;
  120.  
  121. /** Maximum number of item send for debugging, plus 1 */
  122. #define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
  123.  
  124. /**
  125.  * \brief           Public key information and operations
  126.  */
  127. typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
  128.  
  129. /**
  130.  * \brief           Public key container
  131.  */
  132. typedef struct mbedtls_pk_context
  133. {
  134.     const mbedtls_pk_info_t *   pk_info; /**< Public key information         */
  135.     void *                      pk_ctx;  /**< Underlying public key context  */
  136. } mbedtls_pk_context;
  137.  
  138. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  139. /**
  140.  * \brief           Context for resuming operations
  141.  */
  142. typedef struct
  143. {
  144.     const mbedtls_pk_info_t *   pk_info; /**< Public key information         */
  145.     void *                      rs_ctx;  /**< Underlying restart context     */
  146. } mbedtls_pk_restart_ctx;
  147. #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  148. /* Now we can declare functions that take a pointer to that */
  149. typedef void mbedtls_pk_restart_ctx;
  150. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  151.  
  152. #if defined(MBEDTLS_RSA_C)
  153. /**
  154.  * Quick access to an RSA context inside a PK context.
  155.  *
  156.  * \warning You must make sure the PK context actually holds an RSA context
  157.  * before using this function!
  158.  */
  159. static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk )
  160. {
  161.     return( (mbedtls_rsa_context *) (pk).pk_ctx );
  162. }
  163. #endif /* MBEDTLS_RSA_C */
  164.  
  165. #if defined(MBEDTLS_ECP_C)
  166. /**
  167.  * Quick access to an EC context inside a PK context.
  168.  *
  169.  * \warning You must make sure the PK context actually holds an EC context
  170.  * before using this function!
  171.  */
  172. static inline mbedtls_ecp_keypair *mbedtls_pk_ec( const mbedtls_pk_context pk )
  173. {
  174.     return( (mbedtls_ecp_keypair *) (pk).pk_ctx );
  175. }
  176. #endif /* MBEDTLS_ECP_C */
  177.  
  178. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  179. /**
  180.  * \brief           Types for RSA-alt abstraction
  181.  */
  182. typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
  183.                     const unsigned char *input, unsigned char *output,
  184.                     size_t output_max_len );
  185. typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
  186.                     int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  187.                     int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
  188.                     const unsigned char *hash, unsigned char *sig );
  189. typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
  190. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  191.  
  192. /**
  193.  * \brief           Return information associated with the given PK type
  194.  *
  195.  * \param pk_type   PK type to search for.
  196.  *
  197.  * \return          The PK info associated with the type or NULL if not found.
  198.  */
  199. const mbedtls_pk_info_t *mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type );
  200.  
  201. /**
  202.  * \brief           Initialize a #mbedtls_pk_context (as NONE).
  203.  *
  204.  * \param ctx       The context to initialize.
  205.  *                  This must not be \c NULL.
  206.  */
  207. void mbedtls_pk_init( mbedtls_pk_context *ctx );
  208.  
  209. /**
  210.  * \brief           Free the components of a #mbedtls_pk_context.
  211.  *
  212.  * \param ctx       The context to clear. It must have been initialized.
  213.  *                  If this is \c NULL, this function does nothing.
  214.  */
  215. void mbedtls_pk_free( mbedtls_pk_context *ctx );
  216.  
  217. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  218. /**
  219.  * \brief           Initialize a restart context
  220.  *
  221.  * \param ctx       The context to initialize.
  222.  *                  This must not be \c NULL.
  223.  */
  224. void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx );
  225.  
  226. /**
  227.  * \brief           Free the components of a restart context
  228.  *
  229.  * \param ctx       The context to clear. It must have been initialized.
  230.  *                  If this is \c NULL, this function does nothing.
  231.  */
  232. void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx );
  233. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  234.  
  235. /**
  236.  * \brief           Initialize a PK context with the information given
  237.  *                  and allocates the type-specific PK subcontext.
  238.  *
  239.  * \param ctx       Context to initialize. It must not have been set
  240.  *                  up yet (type #MBEDTLS_PK_NONE).
  241.  * \param info      Information to use
  242.  *
  243.  * \return          0 on success,
  244.  *                  MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
  245.  *                  MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
  246.  *
  247.  * \note            For contexts holding an RSA-alt key, use
  248.  *                  \c mbedtls_pk_setup_rsa_alt() instead.
  249.  */
  250. int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
  251.  
  252. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  253. /**
  254.  * \brief           Initialize an RSA-alt context
  255.  *
  256.  * \param ctx       Context to initialize. It must not have been set
  257.  *                  up yet (type #MBEDTLS_PK_NONE).
  258.  * \param key       RSA key pointer
  259.  * \param decrypt_func  Decryption function
  260.  * \param sign_func     Signing function
  261.  * \param key_len_func  Function returning key length in bytes
  262.  *
  263.  * \return          0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
  264.  *                  context wasn't already initialized as RSA_ALT.
  265.  *
  266.  * \note            This function replaces \c mbedtls_pk_setup() for RSA-alt.
  267.  */
  268. int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
  269.                          mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
  270.                          mbedtls_pk_rsa_alt_sign_func sign_func,
  271.                          mbedtls_pk_rsa_alt_key_len_func key_len_func );
  272. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  273.  
  274. /**
  275.  * \brief           Get the size in bits of the underlying key
  276.  *
  277.  * \param ctx       The context to query. It must have been initialized.
  278.  *
  279.  * \return          Key size in bits, or 0 on error
  280.  */
  281. size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
  282.  
  283. /**
  284.  * \brief           Get the length in bytes of the underlying key
  285.  *
  286.  * \param ctx       The context to query. It must have been initialized.
  287.  *
  288.  * \return          Key length in bytes, or 0 on error
  289.  */
  290. static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
  291. {
  292.     return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
  293. }
  294.  
  295. /**
  296.  * \brief           Tell if a context can do the operation given by type
  297.  *
  298.  * \param ctx       The context to query. It must have been initialized.
  299.  * \param type      The desired type.
  300.  *
  301.  * \return          1 if the context can do operations on the given type.
  302.  * \return          0 if the context cannot do the operations on the given
  303.  *                  type. This is always the case for a context that has
  304.  *                  been initialized but not set up, or that has been
  305.  *                  cleared with mbedtls_pk_free().
  306.  */
  307. int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type );
  308.  
  309. /**
  310.  * \brief           Verify signature (including padding if relevant).
  311.  *
  312.  * \param ctx       The PK context to use. It must have been set up.
  313.  * \param md_alg    Hash algorithm used (see notes)
  314.  * \param hash      Hash of the message to sign
  315.  * \param hash_len  Hash length or 0 (see notes)
  316.  * \param sig       Signature to verify
  317.  * \param sig_len   Signature length
  318.  *
  319.  * \return          0 on success (signature is valid),
  320.  *                  #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
  321.  *                  signature in sig but its length is less than \p siglen,
  322.  *                  or a specific error code.
  323.  *
  324.  * \note            For RSA keys, the default padding type is PKCS#1 v1.5.
  325.  *                  Use \c mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, ... )
  326.  *                  to verify RSASSA_PSS signatures.
  327.  *
  328.  * \note            If hash_len is 0, then the length associated with md_alg
  329.  *                  is used instead, or an error returned if it is invalid.
  330.  *
  331.  * \note            md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
  332.  */
  333. int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  334.                const unsigned char *hash, size_t hash_len,
  335.                const unsigned char *sig, size_t sig_len );
  336.  
  337. /**
  338.  * \brief           Restartable version of \c mbedtls_pk_verify()
  339.  *
  340.  * \note            Performs the same job as \c mbedtls_pk_verify(), but can
  341.  *                  return early and restart according to the limit set with
  342.  *                  \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
  343.  *                  operations. For RSA, same as \c mbedtls_pk_verify().
  344.  *
  345.  * \param ctx       The PK context to use. It must have been set up.
  346.  * \param md_alg    Hash algorithm used (see notes)
  347.  * \param hash      Hash of the message to sign
  348.  * \param hash_len  Hash length or 0 (see notes)
  349.  * \param sig       Signature to verify
  350.  * \param sig_len   Signature length
  351.  * \param rs_ctx    Restart context (NULL to disable restart)
  352.  *
  353.  * \return          See \c mbedtls_pk_verify(), or
  354.  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  355.  *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
  356.  */
  357. int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
  358.                mbedtls_md_type_t md_alg,
  359.                const unsigned char *hash, size_t hash_len,
  360.                const unsigned char *sig, size_t sig_len,
  361.                mbedtls_pk_restart_ctx *rs_ctx );
  362.  
  363. /**
  364.  * \brief           Verify signature, with options.
  365.  *                  (Includes verification of the padding depending on type.)
  366.  *
  367.  * \param type      Signature type (inc. possible padding type) to verify
  368.  * \param options   Pointer to type-specific options, or NULL
  369.  * \param ctx       The PK context to use. It must have been set up.
  370.  * \param md_alg    Hash algorithm used (see notes)
  371.  * \param hash      Hash of the message to sign
  372.  * \param hash_len  Hash length or 0 (see notes)
  373.  * \param sig       Signature to verify
  374.  * \param sig_len   Signature length
  375.  *
  376.  * \return          0 on success (signature is valid),
  377.  *                  #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
  378.  *                  used for this type of signatures,
  379.  *                  #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
  380.  *                  signature in sig but its length is less than \p siglen,
  381.  *                  or a specific error code.
  382.  *
  383.  * \note            If hash_len is 0, then the length associated with md_alg
  384.  *                  is used instead, or an error returned if it is invalid.
  385.  *
  386.  * \note            md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
  387.  *
  388.  * \note            If type is MBEDTLS_PK_RSASSA_PSS, then options must point
  389.  *                  to a mbedtls_pk_rsassa_pss_options structure,
  390.  *                  otherwise it must be NULL.
  391.  */
  392. int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
  393.                    mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  394.                    const unsigned char *hash, size_t hash_len,
  395.                    const unsigned char *sig, size_t sig_len );
  396.  
  397. /**
  398.  * \brief           Make signature, including padding if relevant.
  399.  *
  400.  * \param ctx       The PK context to use. It must have been set up
  401.  *                  with a private key.
  402.  * \param md_alg    Hash algorithm used (see notes)
  403.  * \param hash      Hash of the message to sign
  404.  * \param hash_len  Hash length or 0 (see notes)
  405.  * \param sig       Place to write the signature
  406.  * \param sig_len   Number of bytes written
  407.  * \param f_rng     RNG function
  408.  * \param p_rng     RNG parameter
  409.  *
  410.  * \return          0 on success, or a specific error code.
  411.  *
  412.  * \note            For RSA keys, the default padding type is PKCS#1 v1.5.
  413.  *                  There is no interface in the PK module to make RSASSA-PSS
  414.  *                  signatures yet.
  415.  *
  416.  * \note            If hash_len is 0, then the length associated with md_alg
  417.  *                  is used instead, or an error returned if it is invalid.
  418.  *
  419.  * \note            For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
  420.  *                  For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
  421.  *
  422.  * \note            In order to ensure enough space for the signature, the
  423.  *                  \p sig buffer size must be of at least
  424.  *                  `max(MBEDTLS_ECDSA_MAX_LEN, MBEDTLS_MPI_MAX_SIZE)` bytes.
  425.  */
  426. int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  427.              const unsigned char *hash, size_t hash_len,
  428.              unsigned char *sig, size_t *sig_len,
  429.              int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  430.  
  431. /**
  432.  * \brief           Restartable version of \c mbedtls_pk_sign()
  433.  *
  434.  * \note            Performs the same job as \c mbedtls_pk_sign(), but can
  435.  *                  return early and restart according to the limit set with
  436.  *                  \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
  437.  *                  operations. For RSA, same as \c mbedtls_pk_sign().
  438.  *
  439.  * \note            In order to ensure enough space for the signature, the
  440.  *                  \p sig buffer size must be of at least
  441.  *                  `max(MBEDTLS_ECDSA_MAX_LEN, MBEDTLS_MPI_MAX_SIZE)` bytes.
  442.  *
  443.  * \param ctx       The PK context to use. It must have been set up
  444.  *                  with a private key.
  445.  * \param md_alg    Hash algorithm used (see notes)
  446.  * \param hash      Hash of the message to sign
  447.  * \param hash_len  Hash length or 0 (see notes)
  448.  * \param sig       Place to write the signature
  449.  * \param sig_len   Number of bytes written
  450.  * \param f_rng     RNG function
  451.  * \param p_rng     RNG parameter
  452.  * \param rs_ctx    Restart context (NULL to disable restart)
  453.  *
  454.  * \return          See \c mbedtls_pk_sign(), or
  455.  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  456.  *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
  457.  */
  458. int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
  459.              mbedtls_md_type_t md_alg,
  460.              const unsigned char *hash, size_t hash_len,
  461.              unsigned char *sig, size_t *sig_len,
  462.              int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  463.              mbedtls_pk_restart_ctx *rs_ctx );
  464.  
  465. /**
  466.  * \brief           Decrypt message (including padding if relevant).
  467.  *
  468.  * \param ctx       The PK context to use. It must have been set up
  469.  *                  with a private key.
  470.  * \param input     Input to decrypt
  471.  * \param ilen      Input size
  472.  * \param output    Decrypted output
  473.  * \param olen      Decrypted message length
  474.  * \param osize     Size of the output buffer
  475.  * \param f_rng     RNG function
  476.  * \param p_rng     RNG parameter
  477.  *
  478.  * \note            For RSA keys, the default padding type is PKCS#1 v1.5.
  479.  *
  480.  * \return          0 on success, or a specific error code.
  481.  */
  482. int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
  483.                 const unsigned char *input, size_t ilen,
  484.                 unsigned char *output, size_t *olen, size_t osize,
  485.                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  486.  
  487. /**
  488.  * \brief           Encrypt message (including padding if relevant).
  489.  *
  490.  * \param ctx       The PK context to use. It must have been set up.
  491.  * \param input     Message to encrypt
  492.  * \param ilen      Message size
  493.  * \param output    Encrypted output
  494.  * \param olen      Encrypted output length
  495.  * \param osize     Size of the output buffer
  496.  * \param f_rng     RNG function
  497.  * \param p_rng     RNG parameter
  498.  *
  499.  * \note            For RSA keys, the default padding type is PKCS#1 v1.5.
  500.  *
  501.  * \return          0 on success, or a specific error code.
  502.  */
  503. int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
  504.                 const unsigned char *input, size_t ilen,
  505.                 unsigned char *output, size_t *olen, size_t osize,
  506.                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  507.  
  508. /**
  509.  * \brief           Check if a public-private pair of keys matches.
  510.  *
  511.  * \param pub       Context holding a public key.
  512.  * \param prv       Context holding a private (and public) key.
  513.  *
  514.  * \return          0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
  515.  */
  516. int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
  517.  
  518. /**
  519.  * \brief           Export debug information
  520.  *
  521.  * \param ctx       The PK context to use. It must have been initialized.
  522.  * \param items     Place to write debug items
  523.  *
  524.  * \return          0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
  525.  */
  526. int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items );
  527.  
  528. /**
  529.  * \brief           Access the type name
  530.  *
  531.  * \param ctx       The PK context to use. It must have been initialized.
  532.  *
  533.  * \return          Type name on success, or "invalid PK"
  534.  */
  535. const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
  536.  
  537. /**
  538.  * \brief           Get the key type
  539.  *
  540.  * \param ctx       The PK context to use. It must have been initialized.
  541.  *
  542.  * \return          Type on success.
  543.  * \return          #MBEDTLS_PK_NONE for a context that has not been set up.
  544.  */
  545. mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx );
  546.  
  547. #if defined(MBEDTLS_PK_PARSE_C)
  548. /** \ingroup pk_module */
  549. /**
  550.  * \brief           Parse a private key in PEM or DER format
  551.  *
  552.  * \param ctx       The PK context to fill. It must have been initialized
  553.  *                  but not set up.
  554.  * \param key       Input buffer to parse.
  555.  *                  The buffer must contain the input exactly, with no
  556.  *                  extra trailing material. For PEM, the buffer must
  557.  *                  contain a null-terminated string.
  558.  * \param keylen    Size of \b key in bytes.
  559.  *                  For PEM data, this includes the terminating null byte,
  560.  *                  so \p keylen must be equal to `strlen(key) + 1`.
  561.  * \param pwd       Optional password for decryption.
  562.  *                  Pass \c NULL if expecting a non-encrypted key.
  563.  *                  Pass a string of \p pwdlen bytes if expecting an encrypted
  564.  *                  key; a non-encrypted key will also be accepted.
  565.  *                  The empty password is not supported.
  566.  * \param pwdlen    Size of the password in bytes.
  567.  *                  Ignored if \p pwd is \c NULL.
  568.  *
  569.  * \note            On entry, ctx must be empty, either freshly initialised
  570.  *                  with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
  571.  *                  specific key type, check the result with mbedtls_pk_can_do().
  572.  *
  573.  * \note            The key is also checked for correctness.
  574.  *
  575.  * \return          0 if successful, or a specific PK or PEM error code
  576.  */
  577. int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
  578.                   const unsigned char *key, size_t keylen,
  579.                   const unsigned char *pwd, size_t pwdlen );
  580.  
  581. /** \ingroup pk_module */
  582. /**
  583.  * \brief           Parse a public key in PEM or DER format
  584.  *
  585.  * \param ctx       The PK context to fill. It must have been initialized
  586.  *                  but not set up.
  587.  * \param key       Input buffer to parse.
  588.  *                  The buffer must contain the input exactly, with no
  589.  *                  extra trailing material. For PEM, the buffer must
  590.  *                  contain a null-terminated string.
  591.  * \param keylen    Size of \b key in bytes.
  592.  *                  For PEM data, this includes the terminating null byte,
  593.  *                  so \p keylen must be equal to `strlen(key) + 1`.
  594.  *
  595.  * \note            On entry, ctx must be empty, either freshly initialised
  596.  *                  with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
  597.  *                  specific key type, check the result with mbedtls_pk_can_do().
  598.  *
  599.  * \note            The key is also checked for correctness.
  600.  *
  601.  * \return          0 if successful, or a specific PK or PEM error code
  602.  */
  603. int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
  604.                          const unsigned char *key, size_t keylen );
  605.  
  606. #if defined(MBEDTLS_FS_IO)
  607. /** \ingroup pk_module */
  608. /**
  609.  * \brief           Load and parse a private key
  610.  *
  611.  * \param ctx       The PK context to fill. It must have been initialized
  612.  *                  but not set up.
  613.  * \param path      filename to read the private key from
  614.  * \param password  Optional password to decrypt the file.
  615.  *                  Pass \c NULL if expecting a non-encrypted key.
  616.  *                  Pass a null-terminated string if expecting an encrypted
  617.  *                  key; a non-encrypted key will also be accepted.
  618.  *                  The empty password is not supported.
  619.  *
  620.  * \note            On entry, ctx must be empty, either freshly initialised
  621.  *                  with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
  622.  *                  specific key type, check the result with mbedtls_pk_can_do().
  623.  *
  624.  * \note            The key is also checked for correctness.
  625.  *
  626.  * \return          0 if successful, or a specific PK or PEM error code
  627.  */
  628. int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
  629.                       const char *path, const char *password );
  630.  
  631. /** \ingroup pk_module */
  632. /**
  633.  * \brief           Load and parse a public key
  634.  *
  635.  * \param ctx       The PK context to fill. It must have been initialized
  636.  *                  but not set up.
  637.  * \param path      filename to read the public key from
  638.  *
  639.  * \note            On entry, ctx must be empty, either freshly initialised
  640.  *                  with mbedtls_pk_init() or reset with mbedtls_pk_free(). If
  641.  *                  you need a specific key type, check the result with
  642.  *                  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_keyfile( mbedtls_pk_context *ctx, const char *path );
  649. #endif /* MBEDTLS_FS_IO */
  650. #endif /* MBEDTLS_PK_PARSE_C */
  651.  
  652. #if defined(MBEDTLS_PK_WRITE_C)
  653. /**
  654.  * \brief           Write a private key to a PKCS#1 or SEC1 DER structure
  655.  *                  Note: data is written at the end of the buffer! Use the
  656.  *                        return value to determine where you should start
  657.  *                        using the buffer
  658.  *
  659.  * \param ctx       PK context which must contain a valid private key.
  660.  * \param buf       buffer to write to
  661.  * \param size      size of the buffer
  662.  *
  663.  * \return          length of data written if successful, or a specific
  664.  *                  error code
  665.  */
  666. int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
  667.  
  668. /**
  669.  * \brief           Write a public key to a SubjectPublicKeyInfo DER structure
  670.  *                  Note: data is written at the end of the buffer! Use the
  671.  *                        return value to determine where you should start
  672.  *                        using the buffer
  673.  *
  674.  * \param ctx       PK context which must contain a valid public or private key.
  675.  * \param buf       buffer to write to
  676.  * \param size      size of the buffer
  677.  *
  678.  * \return          length of data written if successful, or a specific
  679.  *                  error code
  680.  */
  681. int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
  682.  
  683. #if defined(MBEDTLS_PEM_WRITE_C)
  684. /**
  685.  * \brief           Write a public key to a PEM string
  686.  *
  687.  * \param ctx       PK context which must contain a valid public or private key.
  688.  * \param buf       Buffer to write to. The output includes a
  689.  *                  terminating null byte.
  690.  * \param size      Size of the buffer in bytes.
  691.  *
  692.  * \return          0 if successful, or a specific error code
  693.  */
  694. int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
  695.  
  696. /**
  697.  * \brief           Write a private key to a PKCS#1 or SEC1 PEM string
  698.  *
  699.  * \param ctx       PK context which must contain a valid private key.
  700.  * \param buf       Buffer to write to. The output includes a
  701.  *                  terminating null byte.
  702.  * \param size      Size of the buffer in bytes.
  703.  *
  704.  * \return          0 if successful, or a specific error code
  705.  */
  706. int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
  707. #endif /* MBEDTLS_PEM_WRITE_C */
  708. #endif /* MBEDTLS_PK_WRITE_C */
  709.  
  710. /*
  711.  * WARNING: Low-level functions. You probably do not want to use these unless
  712.  *          you are certain you do ;)
  713.  */
  714.  
  715. #if defined(MBEDTLS_PK_PARSE_C)
  716. /**
  717.  * \brief           Parse a SubjectPublicKeyInfo DER structure
  718.  *
  719.  * \param p         the position in the ASN.1 data
  720.  * \param end       end of the buffer
  721.  * \param pk        The PK context to fill. It must have been initialized
  722.  *                  but not set up.
  723.  *
  724.  * \return          0 if successful, or a specific PK error code
  725.  */
  726. int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
  727.                         mbedtls_pk_context *pk );
  728. #endif /* MBEDTLS_PK_PARSE_C */
  729.  
  730. #if defined(MBEDTLS_PK_WRITE_C)
  731. /**
  732.  * \brief           Write a subjectPublicKey to ASN.1 data
  733.  *                  Note: function works backwards in data buffer
  734.  *
  735.  * \param p         reference to current position pointer
  736.  * \param start     start of the buffer (for bounds-checking)
  737.  * \param key       PK context which must contain a valid public or private key.
  738.  *
  739.  * \return          the length written or a negative error code
  740.  */
  741. int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
  742.                      const mbedtls_pk_context *key );
  743. #endif /* MBEDTLS_PK_WRITE_C */
  744.  
  745. /*
  746.  * Internal module functions. You probably do not want to use these unless you
  747.  * know you do.
  748.  */
  749. #if defined(MBEDTLS_FS_IO)
  750. int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
  751. #endif
  752.  
  753. #ifdef __cplusplus
  754. }
  755. #endif
  756.  
  757. #endif /* MBEDTLS_PK_H */
  758.