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