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