Subversion Repositories Kolibri OS

Rev

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

  1. /**
  2.  * \file rsa.h
  3.  *
  4.  * \brief This file provides an API for the RSA public-key cryptosystem.
  5.  *
  6.  * The RSA public-key cryptosystem is defined in <em>Public-Key
  7.  * Cryptography Standards (PKCS) #1 v1.5: RSA Encryption</em>
  8.  * and <em>Public-Key Cryptography Standards (PKCS) #1 v2.1:
  9.  * RSA Cryptography Specifications</em>.
  10.  *
  11.  */
  12. /*
  13.  *  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  14.  *  SPDX-License-Identifier: GPL-2.0
  15.  *
  16.  *  This program is free software; you can redistribute it and/or modify
  17.  *  it under the terms of the GNU General Public License as published by
  18.  *  the Free Software Foundation; either version 2 of the License, or
  19.  *  (at your option) any later version.
  20.  *
  21.  *  This program is distributed in the hope that it will be useful,
  22.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  *  GNU General Public License for more details.
  25.  *
  26.  *  You should have received a copy of the GNU General Public License along
  27.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  28.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  29.  *
  30.  *  This file is part of Mbed TLS (https://tls.mbed.org)
  31.  */
  32. #ifndef MBEDTLS_RSA_H
  33. #define MBEDTLS_RSA_H
  34.  
  35. #if !defined(MBEDTLS_CONFIG_FILE)
  36. #include "config.h"
  37. #else
  38. #include MBEDTLS_CONFIG_FILE
  39. #endif
  40.  
  41. #include "bignum.h"
  42. #include "md.h"
  43.  
  44. #if defined(MBEDTLS_THREADING_C)
  45. #include "threading.h"
  46. #endif
  47.  
  48. /*
  49.  * RSA Error codes
  50.  */
  51. #define MBEDTLS_ERR_RSA_BAD_INPUT_DATA                    -0x4080  /**< Bad input parameters to function. */
  52. #define MBEDTLS_ERR_RSA_INVALID_PADDING                   -0x4100  /**< Input data contains invalid padding and is rejected. */
  53. #define MBEDTLS_ERR_RSA_KEY_GEN_FAILED                    -0x4180  /**< Something failed during generation of a key. */
  54. #define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED                  -0x4200  /**< Key failed to pass the validity check of the library. */
  55. #define MBEDTLS_ERR_RSA_PUBLIC_FAILED                     -0x4280  /**< The public key operation failed. */
  56. #define MBEDTLS_ERR_RSA_PRIVATE_FAILED                    -0x4300  /**< The private key operation failed. */
  57. #define MBEDTLS_ERR_RSA_VERIFY_FAILED                     -0x4380  /**< The PKCS#1 verification failed. */
  58. #define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE                  -0x4400  /**< The output buffer for decryption is not large enough. */
  59. #define MBEDTLS_ERR_RSA_RNG_FAILED                        -0x4480  /**< The random generator failed to generate non-zeros. */
  60.  
  61. /* MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is deprecated and should not be used.
  62.  */
  63. #define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION             -0x4500  /**< The implementation does not offer the requested operation, for example, because of security violations or lack of functionality. */
  64.  
  65. /* MBEDTLS_ERR_RSA_HW_ACCEL_FAILED is deprecated and should not be used. */
  66. #define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED                   -0x4580  /**< RSA hardware accelerator failed. */
  67.  
  68. /*
  69.  * RSA constants
  70.  */
  71. #define MBEDTLS_RSA_PUBLIC      0 /**< Request private key operation. */
  72. #define MBEDTLS_RSA_PRIVATE     1 /**< Request public key operation. */
  73.  
  74. #define MBEDTLS_RSA_PKCS_V15    0 /**< Use PKCS#1 v1.5 encoding. */
  75. #define MBEDTLS_RSA_PKCS_V21    1 /**< Use PKCS#1 v2.1 encoding. */
  76.  
  77. #define MBEDTLS_RSA_SIGN        1 /**< Identifier for RSA signature operations. */
  78. #define MBEDTLS_RSA_CRYPT       2 /**< Identifier for RSA encryption and decryption operations. */
  79.  
  80. #define MBEDTLS_RSA_SALT_LEN_ANY    -1
  81.  
  82. /*
  83.  * The above constants may be used even if the RSA module is compile out,
  84.  * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
  85.  */
  86.  
  87. #ifdef __cplusplus
  88. extern "C" {
  89. #endif
  90.  
  91. #if !defined(MBEDTLS_RSA_ALT)
  92. // Regular implementation
  93. //
  94.  
  95. /**
  96.  * \brief   The RSA context structure.
  97.  *
  98.  * \note    Direct manipulation of the members of this structure
  99.  *          is deprecated. All manipulation should instead be done through
  100.  *          the public interface functions.
  101.  */
  102. typedef struct mbedtls_rsa_context
  103. {
  104.     int ver;                    /*!<  Always 0.*/
  105.     size_t len;                 /*!<  The size of \p N in Bytes. */
  106.  
  107.     mbedtls_mpi N;              /*!<  The public modulus. */
  108.     mbedtls_mpi E;              /*!<  The public exponent. */
  109.  
  110.     mbedtls_mpi D;              /*!<  The private exponent. */
  111.     mbedtls_mpi P;              /*!<  The first prime factor. */
  112.     mbedtls_mpi Q;              /*!<  The second prime factor. */
  113.  
  114.     mbedtls_mpi DP;             /*!<  <code>D % (P - 1)</code>. */
  115.     mbedtls_mpi DQ;             /*!<  <code>D % (Q - 1)</code>. */
  116.     mbedtls_mpi QP;             /*!<  <code>1 / (Q % P)</code>. */
  117.  
  118.     mbedtls_mpi RN;             /*!<  cached <code>R^2 mod N</code>. */
  119.  
  120.     mbedtls_mpi RP;             /*!<  cached <code>R^2 mod P</code>. */
  121.     mbedtls_mpi RQ;             /*!<  cached <code>R^2 mod Q</code>. */
  122.  
  123.     mbedtls_mpi Vi;             /*!<  The cached blinding value. */
  124.     mbedtls_mpi Vf;             /*!<  The cached un-blinding value. */
  125.  
  126.     int padding;                /*!< Selects padding mode:
  127.                                      #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
  128.                                      #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
  129.     int hash_id;                /*!< Hash identifier of mbedtls_md_type_t type,
  130.                                      as specified in md.h for use in the MGF
  131.                                      mask generating function used in the
  132.                                      EME-OAEP and EMSA-PSS encodings. */
  133. #if defined(MBEDTLS_THREADING_C)
  134.     mbedtls_threading_mutex_t mutex;    /*!<  Thread-safety mutex. */
  135. #endif
  136. }
  137. mbedtls_rsa_context;
  138.  
  139. #else  /* MBEDTLS_RSA_ALT */
  140. #include "rsa_alt.h"
  141. #endif /* MBEDTLS_RSA_ALT */
  142.  
  143. /**
  144.  * \brief          This function initializes an RSA context.
  145.  *
  146.  * \note           Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
  147.  *                 encryption scheme and the RSASSA-PSS signature scheme.
  148.  *
  149.  * \note           The \p hash_id parameter is ignored when using
  150.  *                 #MBEDTLS_RSA_PKCS_V15 padding.
  151.  *
  152.  * \note           The choice of padding mode is strictly enforced for private key
  153.  *                 operations, since there might be security concerns in
  154.  *                 mixing padding modes. For public key operations it is
  155.  *                 a default value, which can be overridden by calling specific
  156.  *                 \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
  157.  *
  158.  * \note           The hash selected in \p hash_id is always used for OEAP
  159.  *                 encryption. For PSS signatures, it is always used for
  160.  *                 making signatures, but can be overridden for verifying them.
  161.  *                 If set to #MBEDTLS_MD_NONE, it is always overridden.
  162.  *
  163.  * \param ctx      The RSA context to initialize. This must not be \c NULL.
  164.  * \param padding  The padding mode to use. This must be either
  165.  *                 #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
  166.  * \param hash_id  The hash identifier of ::mbedtls_md_type_t type, if
  167.  *                 \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
  168.  *                 otherwise.
  169.  */
  170. void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
  171.                        int padding,
  172.                        int hash_id );
  173.  
  174. /**
  175.  * \brief          This function imports a set of core parameters into an
  176.  *                 RSA context.
  177.  *
  178.  * \note           This function can be called multiple times for successive
  179.  *                 imports, if the parameters are not simultaneously present.
  180.  *
  181.  *                 Any sequence of calls to this function should be followed
  182.  *                 by a call to mbedtls_rsa_complete(), which checks and
  183.  *                 completes the provided information to a ready-for-use
  184.  *                 public or private RSA key.
  185.  *
  186.  * \note           See mbedtls_rsa_complete() for more information on which
  187.  *                 parameters are necessary to set up a private or public
  188.  *                 RSA key.
  189.  *
  190.  * \note           The imported parameters are copied and need not be preserved
  191.  *                 for the lifetime of the RSA context being set up.
  192.  *
  193.  * \param ctx      The initialized RSA context to store the parameters in.
  194.  * \param N        The RSA modulus. This may be \c NULL.
  195.  * \param P        The first prime factor of \p N. This may be \c NULL.
  196.  * \param Q        The second prime factor of \p N. This may be \c NULL.
  197.  * \param D        The private exponent. This may be \c NULL.
  198.  * \param E        The public exponent. This may be \c NULL.
  199.  *
  200.  * \return         \c 0 on success.
  201.  * \return         A non-zero error code on failure.
  202.  */
  203. int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
  204.                         const mbedtls_mpi *N,
  205.                         const mbedtls_mpi *P, const mbedtls_mpi *Q,
  206.                         const mbedtls_mpi *D, const mbedtls_mpi *E );
  207.  
  208. /**
  209.  * \brief          This function imports core RSA parameters, in raw big-endian
  210.  *                 binary format, into an RSA context.
  211.  *
  212.  * \note           This function can be called multiple times for successive
  213.  *                 imports, if the parameters are not simultaneously present.
  214.  *
  215.  *                 Any sequence of calls to this function should be followed
  216.  *                 by a call to mbedtls_rsa_complete(), which checks and
  217.  *                 completes the provided information to a ready-for-use
  218.  *                 public or private RSA key.
  219.  *
  220.  * \note           See mbedtls_rsa_complete() for more information on which
  221.  *                 parameters are necessary to set up a private or public
  222.  *                 RSA key.
  223.  *
  224.  * \note           The imported parameters are copied and need not be preserved
  225.  *                 for the lifetime of the RSA context being set up.
  226.  *
  227.  * \param ctx      The initialized RSA context to store the parameters in.
  228.  * \param N        The RSA modulus. This may be \c NULL.
  229.  * \param N_len    The Byte length of \p N; it is ignored if \p N == NULL.
  230.  * \param P        The first prime factor of \p N. This may be \c NULL.
  231.  * \param P_len    The Byte length of \p P; it ns ignored if \p P == NULL.
  232.  * \param Q        The second prime factor of \p N. This may be \c NULL.
  233.  * \param Q_len    The Byte length of \p Q; it is ignored if \p Q == NULL.
  234.  * \param D        The private exponent. This may be \c NULL.
  235.  * \param D_len    The Byte length of \p D; it is ignored if \p D == NULL.
  236.  * \param E        The public exponent. This may be \c NULL.
  237.  * \param E_len    The Byte length of \p E; it is ignored if \p E == NULL.
  238.  *
  239.  * \return         \c 0 on success.
  240.  * \return         A non-zero error code on failure.
  241.  */
  242. int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
  243.                             unsigned char const *N, size_t N_len,
  244.                             unsigned char const *P, size_t P_len,
  245.                             unsigned char const *Q, size_t Q_len,
  246.                             unsigned char const *D, size_t D_len,
  247.                             unsigned char const *E, size_t E_len );
  248.  
  249. /**
  250.  * \brief          This function completes an RSA context from
  251.  *                 a set of imported core parameters.
  252.  *
  253.  *                 To setup an RSA public key, precisely \p N and \p E
  254.  *                 must have been imported.
  255.  *
  256.  *                 To setup an RSA private key, sufficient information must
  257.  *                 be present for the other parameters to be derivable.
  258.  *
  259.  *                 The default implementation supports the following:
  260.  *                 <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
  261.  *                 <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
  262.  *                 Alternative implementations need not support these.
  263.  *
  264.  *                 If this function runs successfully, it guarantees that
  265.  *                 the RSA context can be used for RSA operations without
  266.  *                 the risk of failure or crash.
  267.  *
  268.  * \warning        This function need not perform consistency checks
  269.  *                 for the imported parameters. In particular, parameters that
  270.  *                 are not needed by the implementation might be silently
  271.  *                 discarded and left unchecked. To check the consistency
  272.  *                 of the key material, see mbedtls_rsa_check_privkey().
  273.  *
  274.  * \param ctx      The initialized RSA context holding imported parameters.
  275.  *
  276.  * \return         \c 0 on success.
  277.  * \return         #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
  278.  *                 failed.
  279.  *
  280.  */
  281. int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
  282.  
  283. /**
  284.  * \brief          This function exports the core parameters of an RSA key.
  285.  *
  286.  *                 If this function runs successfully, the non-NULL buffers
  287.  *                 pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
  288.  *                 written, with additional unused space filled leading by
  289.  *                 zero Bytes.
  290.  *
  291.  *                 Possible reasons for returning
  292.  *                 #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
  293.  *                 <li>An alternative RSA implementation is in use, which
  294.  *                 stores the key externally, and either cannot or should
  295.  *                 not export it into RAM.</li>
  296.  *                 <li>A SW or HW implementation might not support a certain
  297.  *                 deduction. For example, \p P, \p Q from \p N, \p D,
  298.  *                 and \p E if the former are not part of the
  299.  *                 implementation.</li></ul>
  300.  *
  301.  *                 If the function fails due to an unsupported operation,
  302.  *                 the RSA context stays intact and remains usable.
  303.  *
  304.  * \param ctx      The initialized RSA context.
  305.  * \param N        The MPI to hold the RSA modulus.
  306.  *                 This may be \c NULL if this field need not be exported.
  307.  * \param P        The MPI to hold the first prime factor of \p N.
  308.  *                 This may be \c NULL if this field need not be exported.
  309.  * \param Q        The MPI to hold the second prime factor of \p N.
  310.  *                 This may be \c NULL if this field need not be exported.
  311.  * \param D        The MPI to hold the private exponent.
  312.  *                 This may be \c NULL if this field need not be exported.
  313.  * \param E        The MPI to hold the public exponent.
  314.  *                 This may be \c NULL if this field need not be exported.
  315.  *
  316.  * \return         \c 0 on success.
  317.  * \return         #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
  318.  *                 requested parameters cannot be done due to missing
  319.  *                 functionality or because of security policies.
  320.  * \return         A non-zero return code on any other failure.
  321.  *
  322.  */
  323. int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
  324.                         mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
  325.                         mbedtls_mpi *D, mbedtls_mpi *E );
  326.  
  327. /**
  328.  * \brief          This function exports core parameters of an RSA key
  329.  *                 in raw big-endian binary format.
  330.  *
  331.  *                 If this function runs successfully, the non-NULL buffers
  332.  *                 pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
  333.  *                 written, with additional unused space filled leading by
  334.  *                 zero Bytes.
  335.  *
  336.  *                 Possible reasons for returning
  337.  *                 #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
  338.  *                 <li>An alternative RSA implementation is in use, which
  339.  *                 stores the key externally, and either cannot or should
  340.  *                 not export it into RAM.</li>
  341.  *                 <li>A SW or HW implementation might not support a certain
  342.  *                 deduction. For example, \p P, \p Q from \p N, \p D,
  343.  *                 and \p E if the former are not part of the
  344.  *                 implementation.</li></ul>
  345.  *                 If the function fails due to an unsupported operation,
  346.  *                 the RSA context stays intact and remains usable.
  347.  *
  348.  * \note           The length parameters are ignored if the corresponding
  349.  *                 buffer pointers are NULL.
  350.  *
  351.  * \param ctx      The initialized RSA context.
  352.  * \param N        The Byte array to store the RSA modulus,
  353.  *                 or \c NULL if this field need not be exported.
  354.  * \param N_len    The size of the buffer for the modulus.
  355.  * \param P        The Byte array to hold the first prime factor of \p N,
  356.  *                 or \c NULL if this field need not be exported.
  357.  * \param P_len    The size of the buffer for the first prime factor.
  358.  * \param Q        The Byte array to hold the second prime factor of \p N,
  359.  *                 or \c NULL if this field need not be exported.
  360.  * \param Q_len    The size of the buffer for the second prime factor.
  361.  * \param D        The Byte array to hold the private exponent,
  362.  *                 or \c NULL if this field need not be exported.
  363.  * \param D_len    The size of the buffer for the private exponent.
  364.  * \param E        The Byte array to hold the public exponent,
  365.  *                 or \c NULL if this field need not be exported.
  366.  * \param E_len    The size of the buffer for the public exponent.
  367.  *
  368.  * \return         \c 0 on success.
  369.  * \return         #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
  370.  *                 requested parameters cannot be done due to missing
  371.  *                 functionality or because of security policies.
  372.  * \return         A non-zero return code on any other failure.
  373.  */
  374. int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
  375.                             unsigned char *N, size_t N_len,
  376.                             unsigned char *P, size_t P_len,
  377.                             unsigned char *Q, size_t Q_len,
  378.                             unsigned char *D, size_t D_len,
  379.                             unsigned char *E, size_t E_len );
  380.  
  381. /**
  382.  * \brief          This function exports CRT parameters of a private RSA key.
  383.  *
  384.  * \note           Alternative RSA implementations not using CRT-parameters
  385.  *                 internally can implement this function based on
  386.  *                 mbedtls_rsa_deduce_opt().
  387.  *
  388.  * \param ctx      The initialized RSA context.
  389.  * \param DP       The MPI to hold \c D modulo `P-1`,
  390.  *                 or \c NULL if it need not be exported.
  391.  * \param DQ       The MPI to hold \c D modulo `Q-1`,
  392.  *                 or \c NULL if it need not be exported.
  393.  * \param QP       The MPI to hold modular inverse of \c Q modulo \c P,
  394.  *                 or \c NULL if it need not be exported.
  395.  *
  396.  * \return         \c 0 on success.
  397.  * \return         A non-zero error code on failure.
  398.  *
  399.  */
  400. int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
  401.                             mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
  402.  
  403. /**
  404.  * \brief          This function sets padding for an already initialized RSA
  405.  *                 context. See mbedtls_rsa_init() for details.
  406.  *
  407.  * \param ctx      The initialized RSA context to be configured.
  408.  * \param padding  The padding mode to use. This must be either
  409.  *                 #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
  410.  * \param hash_id  The #MBEDTLS_RSA_PKCS_V21 hash identifier.
  411.  */
  412. void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
  413.                               int hash_id );
  414.  
  415. /**
  416.  * \brief          This function retrieves the length of RSA modulus in Bytes.
  417.  *
  418.  * \param ctx      The initialized RSA context.
  419.  *
  420.  * \return         The length of the RSA modulus in Bytes.
  421.  *
  422.  */
  423. size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
  424.  
  425. /**
  426.  * \brief          This function generates an RSA keypair.
  427.  *
  428.  * \note           mbedtls_rsa_init() must be called before this function,
  429.  *                 to set up the RSA context.
  430.  *
  431.  * \param ctx      The initialized RSA context used to hold the key.
  432.  * \param f_rng    The RNG function to be used for key generation.
  433.  *                 This must not be \c NULL.
  434.  * \param p_rng    The RNG context to be passed to \p f_rng.
  435.  *                 This may be \c NULL if \p f_rng doesn't need a context.
  436.  * \param nbits    The size of the public key in bits.
  437.  * \param exponent The public exponent to use. For example, \c 65537.
  438.  *                 This must be odd and greater than \c 1.
  439.  *
  440.  * \return         \c 0 on success.
  441.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  442.  */
  443. int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
  444.                          int (*f_rng)(void *, unsigned char *, size_t),
  445.                          void *p_rng,
  446.                          unsigned int nbits, int exponent );
  447.  
  448. /**
  449.  * \brief          This function checks if a context contains at least an RSA
  450.  *                 public key.
  451.  *
  452.  *                 If the function runs successfully, it is guaranteed that
  453.  *                 enough information is present to perform an RSA public key
  454.  *                 operation using mbedtls_rsa_public().
  455.  *
  456.  * \param ctx      The initialized RSA context to check.
  457.  *
  458.  * \return         \c 0 on success.
  459.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  460.  *
  461.  */
  462. int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
  463.  
  464. /**
  465.  * \brief      This function checks if a context contains an RSA private key
  466.  *             and perform basic consistency checks.
  467.  *
  468.  * \note       The consistency checks performed by this function not only
  469.  *             ensure that mbedtls_rsa_private() can be called successfully
  470.  *             on the given context, but that the various parameters are
  471.  *             mutually consistent with high probability, in the sense that
  472.  *             mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
  473.  *
  474.  * \warning    This function should catch accidental misconfigurations
  475.  *             like swapping of parameters, but it cannot establish full
  476.  *             trust in neither the quality nor the consistency of the key
  477.  *             material that was used to setup the given RSA context:
  478.  *             <ul><li>Consistency: Imported parameters that are irrelevant
  479.  *             for the implementation might be silently dropped. If dropped,
  480.  *             the current function does not have access to them,
  481.  *             and therefore cannot check them. See mbedtls_rsa_complete().
  482.  *             If you want to check the consistency of the entire
  483.  *             content of an PKCS1-encoded RSA private key, for example, you
  484.  *             should use mbedtls_rsa_validate_params() before setting
  485.  *             up the RSA context.
  486.  *             Additionally, if the implementation performs empirical checks,
  487.  *             these checks substantiate but do not guarantee consistency.</li>
  488.  *             <li>Quality: This function is not expected to perform
  489.  *             extended quality assessments like checking that the prime
  490.  *             factors are safe. Additionally, it is the responsibility of the
  491.  *             user to ensure the trustworthiness of the source of his RSA
  492.  *             parameters, which goes beyond what is effectively checkable
  493.  *             by the library.</li></ul>
  494.  *
  495.  * \param ctx  The initialized RSA context to check.
  496.  *
  497.  * \return     \c 0 on success.
  498.  * \return     An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  499.  */
  500. int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
  501.  
  502. /**
  503.  * \brief          This function checks a public-private RSA key pair.
  504.  *
  505.  *                 It checks each of the contexts, and makes sure they match.
  506.  *
  507.  * \param pub      The initialized RSA context holding the public key.
  508.  * \param prv      The initialized RSA context holding the private key.
  509.  *
  510.  * \return         \c 0 on success.
  511.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  512.  */
  513. int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
  514.                                 const mbedtls_rsa_context *prv );
  515.  
  516. /**
  517.  * \brief          This function performs an RSA public key operation.
  518.  *
  519.  * \param ctx      The initialized RSA context to use.
  520.  * \param input    The input buffer. This must be a readable buffer
  521.  *                 of length \c ctx->len Bytes. For example, \c 256 Bytes
  522.  *                 for an 2048-bit RSA modulus.
  523.  * \param output   The output buffer. This must be a writable buffer
  524.  *                 of length \c ctx->len Bytes. For example, \c 256 Bytes
  525.  *                 for an 2048-bit RSA modulus.
  526.  *
  527.  * \note           This function does not handle message padding.
  528.  *
  529.  * \note           Make sure to set \p input[0] = 0 or ensure that
  530.  *                 input is smaller than \p N.
  531.  *
  532.  * \return         \c 0 on success.
  533.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  534.  */
  535. int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
  536.                 const unsigned char *input,
  537.                 unsigned char *output );
  538.  
  539. /**
  540.  * \brief          This function performs an RSA private key operation.
  541.  *
  542.  * \note           Blinding is used if and only if a PRNG is provided.
  543.  *
  544.  * \note           If blinding is used, both the base of exponentation
  545.  *                 and the exponent are blinded, providing protection
  546.  *                 against some side-channel attacks.
  547.  *
  548.  * \warning        It is deprecated and a security risk to not provide
  549.  *                 a PRNG here and thereby prevent the use of blinding.
  550.  *                 Future versions of the library may enforce the presence
  551.  *                 of a PRNG.
  552.  *
  553.  * \param ctx      The initialized RSA context to use.
  554.  * \param f_rng    The RNG function, used for blinding. It is discouraged
  555.  *                 and deprecated to pass \c NULL here, in which case
  556.  *                 blinding will be omitted.
  557.  * \param p_rng    The RNG context to pass to \p f_rng. This may be \c NULL
  558.  *                 if \p f_rng is \c NULL or if \p f_rng doesn't need a context.
  559.  * \param input    The input buffer. This must be a readable buffer
  560.  *                 of length \c ctx->len Bytes. For example, \c 256 Bytes
  561.  *                 for an 2048-bit RSA modulus.
  562.  * \param output   The output buffer. This must be a writable buffer
  563.  *                 of length \c ctx->len Bytes. For example, \c 256 Bytes
  564.  *                 for an 2048-bit RSA modulus.
  565.  *
  566.  * \return         \c 0 on success.
  567.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  568.  *
  569.  */
  570. int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
  571.                  int (*f_rng)(void *, unsigned char *, size_t),
  572.                  void *p_rng,
  573.                  const unsigned char *input,
  574.                  unsigned char *output );
  575.  
  576. /**
  577.  * \brief          This function adds the message padding, then performs an RSA
  578.  *                 operation.
  579.  *
  580.  *                 It is the generic wrapper for performing a PKCS#1 encryption
  581.  *                 operation using the \p mode from the context.
  582.  *
  583.  * \deprecated     It is deprecated and discouraged to call this function
  584.  *                 in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
  585.  *                 are likely to remove the \p mode argument and have it
  586.  *                 implicitly set to #MBEDTLS_RSA_PUBLIC.
  587.  *
  588.  * \note           Alternative implementations of RSA need not support
  589.  *                 mode being set to #MBEDTLS_RSA_PRIVATE and might instead
  590.  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
  591.  *
  592.  * \param ctx      The initialized RSA context to use.
  593.  * \param f_rng    The RNG to use. It is mandatory for PKCS#1 v2.1 padding
  594.  *                 encoding, and for PKCS#1 v1.5 padding encoding when used
  595.  *                 with \p mode set to #MBEDTLS_RSA_PUBLIC. For PKCS#1 v1.5
  596.  *                 padding encoding and \p mode set to #MBEDTLS_RSA_PRIVATE,
  597.  *                 it is used for blinding and should be provided in this
  598.  *                 case; see mbedtls_rsa_private() for more.
  599.  * \param p_rng    The RNG context to be passed to \p f_rng. May be
  600.  *                 \c NULL if \p f_rng is \c NULL or if \p f_rng doesn't
  601.  *                 need a context argument.
  602.  * \param mode     The mode of operation. This must be either
  603.  *                 #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
  604.  * \param ilen     The length of the plaintext in Bytes.
  605.  * \param input    The input data to encrypt. This must be a readable
  606.  *                 buffer of size \p ilen Bytes. This must not be \c NULL.
  607.  * \param output   The output buffer. This must be a writable buffer
  608.  *                 of length \c ctx->len Bytes. For example, \c 256 Bytes
  609.  *                 for an 2048-bit RSA modulus.
  610.  *
  611.  * \return         \c 0 on success.
  612.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  613.  */
  614. int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
  615.                        int (*f_rng)(void *, unsigned char *, size_t),
  616.                        void *p_rng,
  617.                        int mode, size_t ilen,
  618.                        const unsigned char *input,
  619.                        unsigned char *output );
  620.  
  621. /**
  622.  * \brief          This function performs a PKCS#1 v1.5 encryption operation
  623.  *                 (RSAES-PKCS1-v1_5-ENCRYPT).
  624.  *
  625.  * \deprecated     It is deprecated and discouraged to call this function
  626.  *                 in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
  627.  *                 are likely to remove the \p mode argument and have it
  628.  *                 implicitly set to #MBEDTLS_RSA_PUBLIC.
  629.  *
  630.  * \note           Alternative implementations of RSA need not support
  631.  *                 mode being set to #MBEDTLS_RSA_PRIVATE and might instead
  632.  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
  633.  *
  634.  * \param ctx      The initialized RSA context to use.
  635.  * \param f_rng    The RNG function to use. It is needed for padding generation
  636.  *                 if \p mode is #MBEDTLS_RSA_PUBLIC. If \p mode is
  637.  *                 #MBEDTLS_RSA_PRIVATE (discouraged), it is used for
  638.  *                 blinding and should be provided; see mbedtls_rsa_private().
  639.  * \param p_rng    The RNG context to be passed to \p f_rng. This may
  640.  *                 be \c NULL if \p f_rng is \c NULL or if \p f_rng
  641.  *                 doesn't need a context argument.
  642.  * \param mode     The mode of operation. This must be either
  643.  *                 #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
  644.  * \param ilen     The length of the plaintext in Bytes.
  645.  * \param input    The input data to encrypt. This must be a readable
  646.  *                 buffer of size \p ilen Bytes. This must not be \c NULL.
  647.  * \param output   The output buffer. This must be a writable buffer
  648.  *                 of length \c ctx->len Bytes. For example, \c 256 Bytes
  649.  *                 for an 2048-bit RSA modulus.
  650.  *
  651.  * \return         \c 0 on success.
  652.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  653.  */
  654. int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
  655.                                  int (*f_rng)(void *, unsigned char *, size_t),
  656.                                  void *p_rng,
  657.                                  int mode, size_t ilen,
  658.                                  const unsigned char *input,
  659.                                  unsigned char *output );
  660.  
  661. /**
  662.  * \brief            This function performs a PKCS#1 v2.1 OAEP encryption
  663.  *                   operation (RSAES-OAEP-ENCRYPT).
  664.  *
  665.  * \note             The output buffer must be as large as the size
  666.  *                   of ctx->N. For example, 128 Bytes if RSA-1024 is used.
  667.  *
  668.  * \deprecated       It is deprecated and discouraged to call this function
  669.  *                   in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
  670.  *                   are likely to remove the \p mode argument and have it
  671.  *                   implicitly set to #MBEDTLS_RSA_PUBLIC.
  672.  *
  673.  * \note             Alternative implementations of RSA need not support
  674.  *                   mode being set to #MBEDTLS_RSA_PRIVATE and might instead
  675.  *                   return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
  676.  *
  677.  * \param ctx        The initnialized RSA context to use.
  678.  * \param f_rng      The RNG function to use. This is needed for padding
  679.  *                   generation and must be provided.
  680.  * \param p_rng      The RNG context to be passed to \p f_rng. This may
  681.  *                   be \c NULL if \p f_rng doesn't need a context argument.
  682.  * \param mode       The mode of operation. This must be either
  683.  *                   #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
  684.  * \param label      The buffer holding the custom label to use.
  685.  *                   This must be a readable buffer of length \p label_len
  686.  *                   Bytes. It may be \c NULL if \p label_len is \c 0.
  687.  * \param label_len  The length of the label in Bytes.
  688.  * \param ilen       The length of the plaintext buffer \p input in Bytes.
  689.  * \param input      The input data to encrypt. This must be a readable
  690.  *                   buffer of size \p ilen Bytes. This must not be \c NULL.
  691.  * \param output     The output buffer. This must be a writable buffer
  692.  *                   of length \c ctx->len Bytes. For example, \c 256 Bytes
  693.  *                   for an 2048-bit RSA modulus.
  694.  *
  695.  * \return           \c 0 on success.
  696.  * \return           An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  697.  */
  698. int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
  699.                             int (*f_rng)(void *, unsigned char *, size_t),
  700.                             void *p_rng,
  701.                             int mode,
  702.                             const unsigned char *label, size_t label_len,
  703.                             size_t ilen,
  704.                             const unsigned char *input,
  705.                             unsigned char *output );
  706.  
  707. /**
  708.  * \brief          This function performs an RSA operation, then removes the
  709.  *                 message padding.
  710.  *
  711.  *                 It is the generic wrapper for performing a PKCS#1 decryption
  712.  *                 operation using the \p mode from the context.
  713.  *
  714.  * \note           The output buffer length \c output_max_len should be
  715.  *                 as large as the size \p ctx->len of \p ctx->N (for example,
  716.  *                 128 Bytes if RSA-1024 is used) to be able to hold an
  717.  *                 arbitrary decrypted message. If it is not large enough to
  718.  *                 hold the decryption of the particular ciphertext provided,
  719.  *                 the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
  720.  *
  721.  * \deprecated     It is deprecated and discouraged to call this function
  722.  *                 in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
  723.  *                 are likely to remove the \p mode argument and have it
  724.  *                 implicitly set to #MBEDTLS_RSA_PRIVATE.
  725.  *
  726.  * \note           Alternative implementations of RSA need not support
  727.  *                 mode being set to #MBEDTLS_RSA_PUBLIC and might instead
  728.  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
  729.  *
  730.  * \param ctx      The initialized RSA context to use.
  731.  * \param f_rng    The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
  732.  *                 this is used for blinding and should be provided; see
  733.  *                 mbedtls_rsa_private() for more. If \p mode is
  734.  *                 #MBEDTLS_RSA_PUBLIC, it is ignored.
  735.  * \param p_rng    The RNG context to be passed to \p f_rng. This may be
  736.  *                 \c NULL if \p f_rng is \c NULL or doesn't need a context.
  737.  * \param mode     The mode of operation. This must be either
  738.  *                 #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
  739.  * \param olen     The address at which to store the length of
  740.  *                 the plaintext. This must not be \c NULL.
  741.  * \param input    The ciphertext buffer. This must be a readable buffer
  742.  *                 of length \c ctx->len Bytes. For example, \c 256 Bytes
  743.  *                 for an 2048-bit RSA modulus.
  744.  * \param output   The buffer used to hold the plaintext. This must
  745.  *                 be a writable buffer of length \p output_max_len Bytes.
  746.  * \param output_max_len The length in Bytes of the output buffer \p output.
  747.  *
  748.  * \return         \c 0 on success.
  749.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  750.  */
  751. int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
  752.                        int (*f_rng)(void *, unsigned char *, size_t),
  753.                        void *p_rng,
  754.                        int mode, size_t *olen,
  755.                        const unsigned char *input,
  756.                        unsigned char *output,
  757.                        size_t output_max_len );
  758.  
  759. /**
  760.  * \brief          This function performs a PKCS#1 v1.5 decryption
  761.  *                 operation (RSAES-PKCS1-v1_5-DECRYPT).
  762.  *
  763.  * \note           The output buffer length \c output_max_len should be
  764.  *                 as large as the size \p ctx->len of \p ctx->N, for example,
  765.  *                 128 Bytes if RSA-1024 is used, to be able to hold an
  766.  *                 arbitrary decrypted message. If it is not large enough to
  767.  *                 hold the decryption of the particular ciphertext provided,
  768.  *                 the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
  769.  *
  770.  * \deprecated     It is deprecated and discouraged to call this function
  771.  *                 in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
  772.  *                 are likely to remove the \p mode argument and have it
  773.  *                 implicitly set to #MBEDTLS_RSA_PRIVATE.
  774.  *
  775.  * \note           Alternative implementations of RSA need not support
  776.  *                 mode being set to #MBEDTLS_RSA_PUBLIC and might instead
  777.  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
  778.  *
  779.  * \param ctx      The initialized RSA context to use.
  780.  * \param f_rng    The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
  781.  *                 this is used for blinding and should be provided; see
  782.  *                 mbedtls_rsa_private() for more. If \p mode is
  783.  *                 #MBEDTLS_RSA_PUBLIC, it is ignored.
  784.  * \param p_rng    The RNG context to be passed to \p f_rng. This may be
  785.  *                 \c NULL if \p f_rng is \c NULL or doesn't need a context.
  786.  * \param mode     The mode of operation. This must be either
  787.  *                 #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
  788.  * \param olen     The address at which to store the length of
  789.  *                 the plaintext. This must not be \c NULL.
  790.  * \param input    The ciphertext buffer. This must be a readable buffer
  791.  *                 of length \c ctx->len Bytes. For example, \c 256 Bytes
  792.  *                 for an 2048-bit RSA modulus.
  793.  * \param output   The buffer used to hold the plaintext. This must
  794.  *                 be a writable buffer of length \p output_max_len Bytes.
  795.  * \param output_max_len The length in Bytes of the output buffer \p output.
  796.  *
  797.  * \return         \c 0 on success.
  798.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  799.  *
  800.  */
  801. int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
  802.                                  int (*f_rng)(void *, unsigned char *, size_t),
  803.                                  void *p_rng,
  804.                                  int mode, size_t *olen,
  805.                                  const unsigned char *input,
  806.                                  unsigned char *output,
  807.                                  size_t output_max_len );
  808.  
  809. /**
  810.  * \brief            This function performs a PKCS#1 v2.1 OAEP decryption
  811.  *                   operation (RSAES-OAEP-DECRYPT).
  812.  *
  813.  * \note             The output buffer length \c output_max_len should be
  814.  *                   as large as the size \p ctx->len of \p ctx->N, for
  815.  *                   example, 128 Bytes if RSA-1024 is used, to be able to
  816.  *                   hold an arbitrary decrypted message. If it is not
  817.  *                   large enough to hold the decryption of the particular
  818.  *                   ciphertext provided, the function returns
  819.  *                   #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
  820.  *
  821.  * \deprecated       It is deprecated and discouraged to call this function
  822.  *                   in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
  823.  *                   are likely to remove the \p mode argument and have it
  824.  *                   implicitly set to #MBEDTLS_RSA_PRIVATE.
  825.  *
  826.  * \note             Alternative implementations of RSA need not support
  827.  *                   mode being set to #MBEDTLS_RSA_PUBLIC and might instead
  828.  *                   return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
  829.  *
  830.  * \param ctx        The initialized RSA context to use.
  831.  * \param f_rng      The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
  832.  *                   this is used for blinding and should be provided; see
  833.  *                   mbedtls_rsa_private() for more. If \p mode is
  834.  *                   #MBEDTLS_RSA_PUBLIC, it is ignored.
  835.  * \param p_rng      The RNG context to be passed to \p f_rng. This may be
  836.  *                   \c NULL if \p f_rng is \c NULL or doesn't need a context.
  837.  * \param mode       The mode of operation. This must be either
  838.  *                   #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
  839.  * \param label      The buffer holding the custom label to use.
  840.  *                   This must be a readable buffer of length \p label_len
  841.  *                   Bytes. It may be \c NULL if \p label_len is \c 0.
  842.  * \param label_len  The length of the label in Bytes.
  843.  * \param olen       The address at which to store the length of
  844.  *                   the plaintext. This must not be \c NULL.
  845.  * \param input      The ciphertext buffer. This must be a readable buffer
  846.  *                   of length \c ctx->len Bytes. For example, \c 256 Bytes
  847.  *                   for an 2048-bit RSA modulus.
  848.  * \param output     The buffer used to hold the plaintext. This must
  849.  *                   be a writable buffer of length \p output_max_len Bytes.
  850.  * \param output_max_len The length in Bytes of the output buffer \p output.
  851.  *
  852.  * \return         \c 0 on success.
  853.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  854.  */
  855. int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
  856.                             int (*f_rng)(void *, unsigned char *, size_t),
  857.                             void *p_rng,
  858.                             int mode,
  859.                             const unsigned char *label, size_t label_len,
  860.                             size_t *olen,
  861.                             const unsigned char *input,
  862.                             unsigned char *output,
  863.                             size_t output_max_len );
  864.  
  865. /**
  866.  * \brief          This function performs a private RSA operation to sign
  867.  *                 a message digest using PKCS#1.
  868.  *
  869.  *                 It is the generic wrapper for performing a PKCS#1
  870.  *                 signature using the \p mode from the context.
  871.  *
  872.  * \note           The \p sig buffer must be as large as the size
  873.  *                 of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
  874.  *
  875.  * \note           For PKCS#1 v2.1 encoding, see comments on
  876.  *                 mbedtls_rsa_rsassa_pss_sign() for details on
  877.  *                 \p md_alg and \p hash_id.
  878.  *
  879.  * \deprecated     It is deprecated and discouraged to call this function
  880.  *                 in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
  881.  *                 are likely to remove the \p mode argument and have it
  882.  *                 implicitly set to #MBEDTLS_RSA_PRIVATE.
  883.  *
  884.  * \note           Alternative implementations of RSA need not support
  885.  *                 mode being set to #MBEDTLS_RSA_PUBLIC and might instead
  886.  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
  887.  *
  888.  * \param ctx      The initialized RSA context to use.
  889.  * \param f_rng    The RNG function to use. If the padding mode is PKCS#1 v2.1,
  890.  *                 this must be provided. If the padding mode is PKCS#1 v1.5 and
  891.  *                 \p mode is #MBEDTLS_RSA_PRIVATE, it is used for blinding
  892.  *                 and should be provided; see mbedtls_rsa_private() for more
  893.  *                 more. It is ignored otherwise.
  894.  * \param p_rng    The RNG context to be passed to \p f_rng. This may be \c NULL
  895.  *                 if \p f_rng is \c NULL or doesn't need a context argument.
  896.  * \param mode     The mode of operation. This must be either
  897.  *                 #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
  898.  * \param md_alg   The message-digest algorithm used to hash the original data.
  899.  *                 Use #MBEDTLS_MD_NONE for signing raw data.
  900.  * \param hashlen  The length of the message digest.
  901.  *                 Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
  902.  * \param hash     The buffer holding the message digest or raw data.
  903.  *                 If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
  904.  *                 buffer of length \p hashlen Bytes. If \p md_alg is not
  905.  *                 #MBEDTLS_MD_NONE, it must be a readable buffer of length
  906.  *                 the size of the hash corresponding to \p md_alg.
  907.  * \param sig      The buffer to hold the signature. This must be a writable
  908.  *                 buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
  909.  *                 for an 2048-bit RSA modulus. A buffer length of
  910.  *                 #MBEDTLS_MPI_MAX_SIZE is always safe.
  911.  *
  912.  * \return         \c 0 if the signing operation was successful.
  913.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  914.  */
  915. int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
  916.                     int (*f_rng)(void *, unsigned char *, size_t),
  917.                     void *p_rng,
  918.                     int mode,
  919.                     mbedtls_md_type_t md_alg,
  920.                     unsigned int hashlen,
  921.                     const unsigned char *hash,
  922.                     unsigned char *sig );
  923.  
  924. /**
  925.  * \brief          This function performs a PKCS#1 v1.5 signature
  926.  *                 operation (RSASSA-PKCS1-v1_5-SIGN).
  927.  *
  928.  * \deprecated     It is deprecated and discouraged to call this function
  929.  *                 in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
  930.  *                 are likely to remove the \p mode argument and have it
  931.  *                 implicitly set to #MBEDTLS_RSA_PRIVATE.
  932.  *
  933.  * \note           Alternative implementations of RSA need not support
  934.  *                 mode being set to #MBEDTLS_RSA_PUBLIC and might instead
  935.  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
  936.  *
  937.  * \param ctx      The initialized RSA context to use.
  938.  * \param f_rng    The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
  939.  *                 this is used for blinding and should be provided; see
  940.  *                 mbedtls_rsa_private() for more. If \p mode is
  941.  *                 #MBEDTLS_RSA_PUBLIC, it is ignored.
  942.  * \param p_rng    The RNG context to be passed to \p f_rng. This may be \c NULL
  943.  *                 if \p f_rng is \c NULL or doesn't need a context argument.
  944.  * \param mode     The mode of operation. This must be either
  945.  *                 #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
  946.  * \param md_alg   The message-digest algorithm used to hash the original data.
  947.  *                 Use #MBEDTLS_MD_NONE for signing raw data.
  948.  * \param hashlen  The length of the message digest.
  949.  *                 Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
  950.  * \param hash     The buffer holding the message digest or raw data.
  951.  *                 If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
  952.  *                 buffer of length \p hashlen Bytes. If \p md_alg is not
  953.  *                 #MBEDTLS_MD_NONE, it must be a readable buffer of length
  954.  *                 the size of the hash corresponding to \p md_alg.
  955.  * \param sig      The buffer to hold the signature. This must be a writable
  956.  *                 buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
  957.  *                 for an 2048-bit RSA modulus. A buffer length of
  958.  *                 #MBEDTLS_MPI_MAX_SIZE is always safe.
  959.  *
  960.  * \return         \c 0 if the signing operation was successful.
  961.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  962.  */
  963. int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
  964.                                int (*f_rng)(void *, unsigned char *, size_t),
  965.                                void *p_rng,
  966.                                int mode,
  967.                                mbedtls_md_type_t md_alg,
  968.                                unsigned int hashlen,
  969.                                const unsigned char *hash,
  970.                                unsigned char *sig );
  971.  
  972. /**
  973.  * \brief          This function performs a PKCS#1 v2.1 PSS signature
  974.  *                 operation (RSASSA-PSS-SIGN).
  975.  *
  976.  * \note           The \p hash_id in the RSA context is the one used for the
  977.  *                 encoding. \p md_alg in the function call is the type of hash
  978.  *                 that is encoded. According to <em>RFC-3447: Public-Key
  979.  *                 Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
  980.  *                 Specifications</em> it is advised to keep both hashes the
  981.  *                 same.
  982.  *
  983.  * \note           This function always uses the maximum possible salt size,
  984.  *                 up to the length of the payload hash. This choice of salt
  985.  *                 size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
  986.  *                 v2.2) §9.1.1 step 3. Furthermore this function enforces a
  987.  *                 minimum salt size which is the hash size minus 2 bytes. If
  988.  *                 this minimum size is too large given the key size (the salt
  989.  *                 size, plus the hash size, plus 2 bytes must be no more than
  990.  *                 the key size in bytes), this function returns
  991.  *                 #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
  992.  *
  993.  * \deprecated     It is deprecated and discouraged to call this function
  994.  *                 in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
  995.  *                 are likely to remove the \p mode argument and have it
  996.  *                 implicitly set to #MBEDTLS_RSA_PRIVATE.
  997.  *
  998.  * \note           Alternative implementations of RSA need not support
  999.  *                 mode being set to #MBEDTLS_RSA_PUBLIC and might instead
  1000.  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
  1001.  *
  1002.  * \param ctx      The initialized RSA context to use.
  1003.  * \param f_rng    The RNG function. It must not be \c NULL.
  1004.  * \param p_rng    The RNG context to be passed to \p f_rng. This may be \c NULL
  1005.  *                 if \p f_rng doesn't need a context argument.
  1006.  * \param mode     The mode of operation. This must be either
  1007.  *                 #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
  1008.  * \param md_alg   The message-digest algorithm used to hash the original data.
  1009.  *                 Use #MBEDTLS_MD_NONE for signing raw data.
  1010.  * \param hashlen  The length of the message digest.
  1011.  *                 Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
  1012.  * \param hash     The buffer holding the message digest or raw data.
  1013.  *                 If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
  1014.  *                 buffer of length \p hashlen Bytes. If \p md_alg is not
  1015.  *                 #MBEDTLS_MD_NONE, it must be a readable buffer of length
  1016.  *                 the size of the hash corresponding to \p md_alg.
  1017.  * \param sig      The buffer to hold the signature. This must be a writable
  1018.  *                 buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
  1019.  *                 for an 2048-bit RSA modulus. A buffer length of
  1020.  *                 #MBEDTLS_MPI_MAX_SIZE is always safe.
  1021.  *
  1022.  * \return         \c 0 if the signing operation was successful.
  1023.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  1024.  */
  1025. int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
  1026.                          int (*f_rng)(void *, unsigned char *, size_t),
  1027.                          void *p_rng,
  1028.                          int mode,
  1029.                          mbedtls_md_type_t md_alg,
  1030.                          unsigned int hashlen,
  1031.                          const unsigned char *hash,
  1032.                          unsigned char *sig );
  1033.  
  1034. /**
  1035.  * \brief          This function performs a public RSA operation and checks
  1036.  *                 the message digest.
  1037.  *
  1038.  *                 This is the generic wrapper for performing a PKCS#1
  1039.  *                 verification using the mode from the context.
  1040.  *
  1041.  * \note           For PKCS#1 v2.1 encoding, see comments on
  1042.  *                 mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
  1043.  *                 \p hash_id.
  1044.  *
  1045.  * \deprecated     It is deprecated and discouraged to call this function
  1046.  *                 in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
  1047.  *                 are likely to remove the \p mode argument and have it
  1048.  *                 set to #MBEDTLS_RSA_PUBLIC.
  1049.  *
  1050.  * \note           Alternative implementations of RSA need not support
  1051.  *                 mode being set to #MBEDTLS_RSA_PRIVATE and might instead
  1052.  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
  1053.  *
  1054.  * \param ctx      The initialized RSA public key context to use.
  1055.  * \param f_rng    The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
  1056.  *                 this is used for blinding and should be provided; see
  1057.  *                 mbedtls_rsa_private() for more. Otherwise, it is ignored.
  1058.  * \param p_rng    The RNG context to be passed to \p f_rng. This may be
  1059.  *                 \c NULL if \p f_rng is \c NULL or doesn't need a context.
  1060.  * \param mode     The mode of operation. This must be either
  1061.  *                 #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
  1062.  * \param md_alg   The message-digest algorithm used to hash the original data.
  1063.  *                 Use #MBEDTLS_MD_NONE for signing raw data.
  1064.  * \param hashlen  The length of the message digest.
  1065.  *                 This is only used if \p md_alg is #MBEDTLS_MD_NONE.
  1066.  * \param hash     The buffer holding the message digest or raw data.
  1067.  *                 If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
  1068.  *                 buffer of length \p hashlen Bytes. If \p md_alg is not
  1069.  *                 #MBEDTLS_MD_NONE, it must be a readable buffer of length
  1070.  *                 the size of the hash corresponding to \p md_alg.
  1071.  * \param sig      The buffer holding the signature. This must be a readable
  1072.  *                 buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
  1073.  *                 for an 2048-bit RSA modulus.
  1074.  *
  1075.  * \return         \c 0 if the verify operation was successful.
  1076.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  1077.  */
  1078. int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
  1079.                       int (*f_rng)(void *, unsigned char *, size_t),
  1080.                       void *p_rng,
  1081.                       int mode,
  1082.                       mbedtls_md_type_t md_alg,
  1083.                       unsigned int hashlen,
  1084.                       const unsigned char *hash,
  1085.                       const unsigned char *sig );
  1086.  
  1087. /**
  1088.  * \brief          This function performs a PKCS#1 v1.5 verification
  1089.  *                 operation (RSASSA-PKCS1-v1_5-VERIFY).
  1090.  *
  1091.  * \deprecated     It is deprecated and discouraged to call this function
  1092.  *                 in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
  1093.  *                 are likely to remove the \p mode argument and have it
  1094.  *                 set to #MBEDTLS_RSA_PUBLIC.
  1095.  *
  1096.  * \note           Alternative implementations of RSA need not support
  1097.  *                 mode being set to #MBEDTLS_RSA_PRIVATE and might instead
  1098.  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
  1099.  *
  1100.  * \param ctx      The initialized RSA public key context to use.
  1101.  * \param f_rng    The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
  1102.  *                 this is used for blinding and should be provided; see
  1103.  *                 mbedtls_rsa_private() for more. Otherwise, it is ignored.
  1104.  * \param p_rng    The RNG context to be passed to \p f_rng. This may be
  1105.  *                 \c NULL if \p f_rng is \c NULL or doesn't need a context.
  1106.  * \param mode     The mode of operation. This must be either
  1107.  *                 #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
  1108.  * \param md_alg   The message-digest algorithm used to hash the original data.
  1109.  *                 Use #MBEDTLS_MD_NONE for signing raw data.
  1110.  * \param hashlen  The length of the message digest.
  1111.  *                 This is only used if \p md_alg is #MBEDTLS_MD_NONE.
  1112.  * \param hash     The buffer holding the message digest or raw data.
  1113.  *                 If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
  1114.  *                 buffer of length \p hashlen Bytes. If \p md_alg is not
  1115.  *                 #MBEDTLS_MD_NONE, it must be a readable buffer of length
  1116.  *                 the size of the hash corresponding to \p md_alg.
  1117.  * \param sig      The buffer holding the signature. This must be a readable
  1118.  *                 buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
  1119.  *                 for an 2048-bit RSA modulus.
  1120.  *
  1121.  * \return         \c 0 if the verify operation was successful.
  1122.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  1123.  */
  1124. int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
  1125.                                  int (*f_rng)(void *, unsigned char *, size_t),
  1126.                                  void *p_rng,
  1127.                                  int mode,
  1128.                                  mbedtls_md_type_t md_alg,
  1129.                                  unsigned int hashlen,
  1130.                                  const unsigned char *hash,
  1131.                                  const unsigned char *sig );
  1132.  
  1133. /**
  1134.  * \brief          This function performs a PKCS#1 v2.1 PSS verification
  1135.  *                 operation (RSASSA-PSS-VERIFY).
  1136.  *
  1137.  *                 The hash function for the MGF mask generating function
  1138.  *                 is that specified in the RSA context.
  1139.  *
  1140.  * \note           The \p hash_id in the RSA context is the one used for the
  1141.  *                 verification. \p md_alg in the function call is the type of
  1142.  *                 hash that is verified. According to <em>RFC-3447: Public-Key
  1143.  *                 Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
  1144.  *                 Specifications</em> it is advised to keep both hashes the
  1145.  *                 same. If \p hash_id in the RSA context is unset,
  1146.  *                 the \p md_alg from the function call is used.
  1147.  *
  1148.  * \deprecated     It is deprecated and discouraged to call this function
  1149.  *                 in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
  1150.  *                 are likely to remove the \p mode argument and have it
  1151.  *                 implicitly set to #MBEDTLS_RSA_PUBLIC.
  1152.  *
  1153.  * \note           Alternative implementations of RSA need not support
  1154.  *                 mode being set to #MBEDTLS_RSA_PRIVATE and might instead
  1155.  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
  1156.  *
  1157.  * \param ctx      The initialized RSA public key context to use.
  1158.  * \param f_rng    The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
  1159.  *                 this is used for blinding and should be provided; see
  1160.  *                 mbedtls_rsa_private() for more. Otherwise, it is ignored.
  1161.  * \param p_rng    The RNG context to be passed to \p f_rng. This may be
  1162.  *                 \c NULL if \p f_rng is \c NULL or doesn't need a context.
  1163.  * \param mode     The mode of operation. This must be either
  1164.  *                 #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
  1165.  * \param md_alg   The message-digest algorithm used to hash the original data.
  1166.  *                 Use #MBEDTLS_MD_NONE for signing raw data.
  1167.  * \param hashlen  The length of the message digest.
  1168.  *                 This is only used if \p md_alg is #MBEDTLS_MD_NONE.
  1169.  * \param hash     The buffer holding the message digest or raw data.
  1170.  *                 If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
  1171.  *                 buffer of length \p hashlen Bytes. If \p md_alg is not
  1172.  *                 #MBEDTLS_MD_NONE, it must be a readable buffer of length
  1173.  *                 the size of the hash corresponding to \p md_alg.
  1174.  * \param sig      The buffer holding the signature. This must be a readable
  1175.  *                 buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
  1176.  *                 for an 2048-bit RSA modulus.
  1177.  *
  1178.  * \return         \c 0 if the verify operation was successful.
  1179.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  1180.  */
  1181. int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
  1182.                            int (*f_rng)(void *, unsigned char *, size_t),
  1183.                            void *p_rng,
  1184.                            int mode,
  1185.                            mbedtls_md_type_t md_alg,
  1186.                            unsigned int hashlen,
  1187.                            const unsigned char *hash,
  1188.                            const unsigned char *sig );
  1189.  
  1190. /**
  1191.  * \brief          This function performs a PKCS#1 v2.1 PSS verification
  1192.  *                 operation (RSASSA-PSS-VERIFY).
  1193.  *
  1194.  *                 The hash function for the MGF mask generating function
  1195.  *                 is that specified in \p mgf1_hash_id.
  1196.  *
  1197.  * \note           The \p sig buffer must be as large as the size
  1198.  *                 of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
  1199.  *
  1200.  * \note           The \p hash_id in the RSA context is ignored.
  1201.  *
  1202.  * \param ctx      The initialized RSA public key context to use.
  1203.  * \param f_rng    The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
  1204.  *                 this is used for blinding and should be provided; see
  1205.  *                 mbedtls_rsa_private() for more. Otherwise, it is ignored.
  1206.  * \param p_rng    The RNG context to be passed to \p f_rng. This may be
  1207.  *                 \c NULL if \p f_rng is \c NULL or doesn't need a context.
  1208.  * \param mode     The mode of operation. This must be either
  1209.  *                 #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
  1210.  * \param md_alg   The message-digest algorithm used to hash the original data.
  1211.  *                 Use #MBEDTLS_MD_NONE for signing raw data.
  1212.  * \param hashlen  The length of the message digest.
  1213.  *                 This is only used if \p md_alg is #MBEDTLS_MD_NONE.
  1214.  * \param hash     The buffer holding the message digest or raw data.
  1215.  *                 If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
  1216.  *                 buffer of length \p hashlen Bytes. If \p md_alg is not
  1217.  *                 #MBEDTLS_MD_NONE, it must be a readable buffer of length
  1218.  *                 the size of the hash corresponding to \p md_alg.
  1219.  * \param mgf1_hash_id      The message digest used for mask generation.
  1220.  * \param expected_salt_len The length of the salt used in padding. Use
  1221.  *                          #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
  1222.  * \param sig      The buffer holding the signature. This must be a readable
  1223.  *                 buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
  1224.  *                 for an 2048-bit RSA modulus.
  1225.  *
  1226.  * \return         \c 0 if the verify operation was successful.
  1227.  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  1228.  */
  1229. int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
  1230.                                int (*f_rng)(void *, unsigned char *, size_t),
  1231.                                void *p_rng,
  1232.                                int mode,
  1233.                                mbedtls_md_type_t md_alg,
  1234.                                unsigned int hashlen,
  1235.                                const unsigned char *hash,
  1236.                                mbedtls_md_type_t mgf1_hash_id,
  1237.                                int expected_salt_len,
  1238.                                const unsigned char *sig );
  1239.  
  1240. /**
  1241.  * \brief          This function copies the components of an RSA context.
  1242.  *
  1243.  * \param dst      The destination context. This must be initialized.
  1244.  * \param src      The source context. This must be initialized.
  1245.  *
  1246.  * \return         \c 0 on success.
  1247.  * \return         #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
  1248.  */
  1249. int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
  1250.  
  1251. /**
  1252.  * \brief          This function frees the components of an RSA key.
  1253.  *
  1254.  * \param ctx      The RSA context to free. May be \c NULL, in which case
  1255.  *                 this function is a no-op. If it is not \c NULL, it must
  1256.  *                 point to an initialized RSA context.
  1257.  */
  1258. void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
  1259.  
  1260. #if defined(MBEDTLS_SELF_TEST)
  1261.  
  1262. /**
  1263.  * \brief          The RSA checkup routine.
  1264.  *
  1265.  * \return         \c 0 on success.
  1266.  * \return         \c 1 on failure.
  1267.  */
  1268. int mbedtls_rsa_self_test( int verbose );
  1269.  
  1270. #endif /* MBEDTLS_SELF_TEST */
  1271.  
  1272. #ifdef __cplusplus
  1273. }
  1274. #endif
  1275.  
  1276. #endif /* rsa.h */
  1277.