blob: 562395f2b4697135676a8f870cf18a4042ae3f7c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief The RSA public-key cryptosystem
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_RSA_H
24#define MBEDTLS_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakkered27a042013-04-18 22:46:23 +020031
Paul Bakker314052f2011-08-15 09:07:52 +000032#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020033#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc9965dc2013-09-29 14:58:17 +020036#include "threading.h"
37#endif
38
Paul Bakker13e2dfe2009-07-28 07:18:38 +000039/*
40 * RSA Error codes
41 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
43#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
44#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +020045#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the library's validity check. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
47#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
48#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
49#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
50#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Hanno Beckerbc389d12017-10-05 11:49:53 +010051#define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500 /**< The implementation doesn't offer the requested operation,
52 e.g. because of security violations or lack of functionality */
Paul Bakker5121ce52009-01-03 21:22:43 +000053
54/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020055 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define MBEDTLS_RSA_PUBLIC 0
58#define MBEDTLS_RSA_PRIVATE 1
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define MBEDTLS_RSA_PKCS_V15 0
61#define MBEDTLS_RSA_PKCS_V21 1
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#define MBEDTLS_RSA_SIGN 1
64#define MBEDTLS_RSA_CRYPT 2
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020067
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020068/*
69 * The above constants may be used even if the RSA module is compile out,
70 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
71 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020073
Paul Bakker407a0da2013-06-27 14:29:21 +020074#ifdef __cplusplus
75extern "C" {
76#endif
77
Paul Bakker5121ce52009-01-03 21:22:43 +000078/**
79 * \brief RSA context structure
80 */
81typedef struct
82{
83 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +000084 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_mpi N; /*!< public modulus */
87 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +000088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_mpi D; /*!< private exponent */
90 mbedtls_mpi P; /*!< 1st prime factor */
91 mbedtls_mpi Q; /*!< 2nd prime factor */
92 mbedtls_mpi DP; /*!< D % (P - 1) */
93 mbedtls_mpi DQ; /*!< D % (Q - 1) */
94 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Paul Bakker5121ce52009-01-03 21:22:43 +000095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_mpi RN; /*!< cached R^2 mod N */
97 mbedtls_mpi RP; /*!< cached R^2 mod P */
98 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_mpi Vi; /*!< cached blinding value */
101 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 int padding; /*!< MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
Brian J Murray98844ff2016-08-30 01:50:12 -0700104 MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
106 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000107 for the EME-OAEP and EMSA-PSS
108 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#if defined(MBEDTLS_THREADING_C)
110 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200111#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000112}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Paul Bakker5121ce52009-01-03 21:22:43 +0000115/**
116 * \brief Initialize an RSA context
117 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 * Note: Set padding to MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000119 * encryption scheme and the RSASSA-PSS signature scheme.
120 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 * \param ctx RSA context to be initialized
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
123 * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 *
125 * \note The hash_id parameter is actually ignored
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 * when using MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200127 *
128 * \note Choice of padding mode is strictly enforced for private key
129 * operations, since there might be security concerns in
130 * mixing padding modes. For public key operations it's merely
131 * a default value, which can be overriden by calling specific
132 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
133 *
134 * \note The chosen hash is always used for OEAP encryption.
135 * For PSS signatures, it's always used for making signatures,
136 * but can be overriden (and always is, if set to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 * MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 int padding,
Paul Bakker42099c32014-01-27 11:45:49 +0100141 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
143/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100144 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100146 *
147 * \param ctx RSA context to be set
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
149 * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100152
153/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 * \brief Generate an RSA keypair
155 *
156 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000157 * \param f_rng RNG function
158 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 * \param nbits size of the public key in bits
160 * \param exponent public exponent (e.g., 65537)
161 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000163 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000168 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000169 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000170 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
172/**
173 * \brief Check a public RSA key
174 *
175 * \param ctx RSA context to be checked
176 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
181/**
182 * \brief Check a private RSA key
183 *
184 * \param ctx RSA context to be checked
185 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
190/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100191 * \brief Check a public-private RSA key pair.
192 * Check each of the contexts, and make sure they match.
193 *
194 * \param pub RSA context holding the public key
195 * \param prv RSA context holding the private key
196 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100200
201/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 * \brief Do an RSA public key operation
203 *
204 * \param ctx RSA context
205 * \param input input buffer
206 * \param output output buffer
207 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 *
210 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800211 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000212 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 *
214 * \note The input and output buffers must be large
215 * enough (eg. 128 bytes if RSA-1024 is used).
216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000218 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 unsigned char *output );
220
221/**
222 * \brief Do an RSA private key operation
223 *
224 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200225 * \param f_rng RNG function (Needed for blinding)
226 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 * \param input input buffer
228 * \param output output buffer
229 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 *
232 * \note The input and output buffers must be large
233 * enough (eg. 128 bytes if RSA-1024 is used).
234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200236 int (*f_rng)(void *, unsigned char *, size_t),
237 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000238 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 unsigned char *output );
240
241/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100242 * \brief Generic wrapper to perform a PKCS#1 encryption using the
243 * mode from the context. Add the message padding, then do an
244 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 *
246 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200247 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 * and MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000249 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000251 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 * \param input buffer holding the data to be encrypted
253 * \param output buffer that will hold the ciphertext
254 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100255 * \deprecated It is deprecated and discouraged to call this function
256 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
257 * are likely to remove the mode argument and have it implicitly
258 * set to MBEDTLS_RSA_PUBLIC.
259 *
260 * \note Alternative implementations of RSA need not support
261 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
262 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
263 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 *
266 * \note The output buffer must be as large as the size
267 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000270 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000271 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000272 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000273 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 unsigned char *output );
275
276/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100277 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
278 *
279 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 * \param f_rng RNG function (Needed for padding and MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100281 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100283 * \param ilen contains the plaintext length
284 * \param input buffer holding the data to be encrypted
285 * \param output buffer that will hold the ciphertext
286 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100287 * \deprecated It is deprecated and discouraged to call this function
288 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
289 * are likely to remove the mode argument and have it implicitly
290 * set to MBEDTLS_RSA_PUBLIC.
291 *
292 * \note Alternative implementations of RSA need not support
293 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
294 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
295 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100297 *
298 * \note The output buffer must be as large as the size
299 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100302 int (*f_rng)(void *, unsigned char *, size_t),
303 void *p_rng,
304 int mode, size_t ilen,
305 const unsigned char *input,
306 unsigned char *output );
307
308/**
309 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
310 *
311 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200312 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 * and MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100314 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100316 * \param label buffer holding the custom label to use
317 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100318 * \param ilen contains the plaintext length
319 * \param input buffer holding the data to be encrypted
320 * \param output buffer that will hold the ciphertext
321 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100322 * \deprecated It is deprecated and discouraged to call this function
323 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
324 * are likely to remove the mode argument and have it implicitly
325 * set to MBEDTLS_RSA_PUBLIC.
326 *
327 * \note Alternative implementations of RSA need not support
328 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
329 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
330 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100332 *
333 * \note The output buffer must be as large as the size
334 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100337 int (*f_rng)(void *, unsigned char *, size_t),
338 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100339 int mode,
340 const unsigned char *label, size_t label_len,
341 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100342 const unsigned char *input,
343 unsigned char *output );
344
345/**
346 * \brief Generic wrapper to perform a PKCS#1 decryption using the
347 * mode from the context. Do an RSA operation, then remove
348 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 *
350 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200352 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000354 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 * \param input buffer holding the encrypted data
356 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000357 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000358 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100359 * \deprecated It is deprecated and discouraged to call this function
360 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
361 * are likely to remove the mode argument and have it implicitly
362 * set to MBEDTLS_RSA_PRIVATE.
363 *
364 * \note Alternative implementations of RSA need not support
365 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
366 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
367 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 *
Hanno Beckerb6479192017-05-04 11:27:39 +0100370 * \note The output buffer length \c output_max_len should be
371 * as large as the size ctx->len of ctx->N (eg. 128 bytes
372 * if RSA-1024 is used) to be able to hold an arbitrary
373 * decrypted message. If it is not large enough to hold
Hanno Becker3cdc7112017-10-05 10:09:31 +0100374 * the decryption of the particular ciphertext provided,
Hanno Beckerb6479192017-05-04 11:27:39 +0100375 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
376 *
377 * \note The input buffer must be as large as the size
378 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200381 int (*f_rng)(void *, unsigned char *, size_t),
382 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000383 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000384 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000385 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000386 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
388/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100389 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
390 *
391 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200393 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100395 * \param olen will contain the plaintext length
396 * \param input buffer holding the encrypted data
397 * \param output buffer that will hold the plaintext
398 * \param output_max_len maximum length of the output buffer
399 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100400 * \deprecated It is deprecated and discouraged to call this function
401 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
402 * are likely to remove the mode argument and have it implicitly
403 * set to MBEDTLS_RSA_PRIVATE.
404 *
405 * \note Alternative implementations of RSA need not support
406 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
407 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
408 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100410 *
Hanno Beckerb6479192017-05-04 11:27:39 +0100411 * \note The output buffer length \c output_max_len should be
412 * as large as the size ctx->len of ctx->N (eg. 128 bytes
413 * if RSA-1024 is used) to be able to hold an arbitrary
414 * decrypted message. If it is not large enough to hold
Hanno Becker3cdc7112017-10-05 10:09:31 +0100415 * the decryption of the particular ciphertext provided,
Hanno Beckerb6479192017-05-04 11:27:39 +0100416 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
417 *
418 * \note The input buffer must be as large as the size
419 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200422 int (*f_rng)(void *, unsigned char *, size_t),
423 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100424 int mode, size_t *olen,
425 const unsigned char *input,
426 unsigned char *output,
427 size_t output_max_len );
428
429/**
430 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
431 *
432 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200434 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100436 * \param label buffer holding the custom label to use
437 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100438 * \param olen will contain the plaintext length
439 * \param input buffer holding the encrypted data
440 * \param output buffer that will hold the plaintext
441 * \param output_max_len maximum length of the output buffer
442 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100443 * \deprecated It is deprecated and discouraged to call this function
444 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
445 * are likely to remove the mode argument and have it implicitly
446 * set to MBEDTLS_RSA_PRIVATE.
447 *
448 * \note Alternative implementations of RSA need not support
449 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
450 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
451 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100453 *
Hanno Beckerb6479192017-05-04 11:27:39 +0100454 * \note The output buffer length \c output_max_len should be
455 * as large as the size ctx->len of ctx->N (eg. 128 bytes
456 * if RSA-1024 is used) to be able to hold an arbitrary
457 * decrypted message. If it is not large enough to hold
Hanno Becker3cdc7112017-10-05 10:09:31 +0100458 * the decryption of the particular ciphertext provided,
Hanno Beckerb6479192017-05-04 11:27:39 +0100459 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
460 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100461 * \note The input buffer must be as large as the size
Hanno Beckerb6479192017-05-04 11:27:39 +0100462 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200465 int (*f_rng)(void *, unsigned char *, size_t),
466 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100467 int mode,
468 const unsigned char *label, size_t label_len,
469 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100470 const unsigned char *input,
471 unsigned char *output,
472 size_t output_max_len );
473
474/**
475 * \brief Generic wrapper to perform a PKCS#1 signature using the
476 * mode from the context. Do a private RSA operation to sign
477 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 *
479 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200480 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 * MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000482 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
484 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
485 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 * \param hash buffer holding the message digest
487 * \param sig buffer that will hold the ciphertext
488 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100489 * \deprecated It is deprecated and discouraged to call this function
490 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
491 * are likely to remove the mode argument and have it implicitly
492 * set to MBEDTLS_RSA_PRIVATE.
493 *
494 * \note Alternative implementations of RSA need not support
495 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
496 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
497 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 *
501 * \note The "sig" buffer must be as large as the size
502 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000503 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200504 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000508 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000509 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000512 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000513 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 unsigned char *sig );
515
516/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100517 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
518 *
519 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200521 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
523 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
524 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100525 * \param hash buffer holding the message digest
526 * \param sig buffer that will hold the ciphertext
527 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100528 * \deprecated It is deprecated and discouraged to call this function
529 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
530 * are likely to remove the mode argument and have it implicitly
531 * set to MBEDTLS_RSA_PRIVATE.
532 *
533 * \note Alternative implementations of RSA need not support
534 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
535 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
536 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100537 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100539 *
540 * \note The "sig" buffer must be as large as the size
541 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200544 int (*f_rng)(void *, unsigned char *, size_t),
545 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100546 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100548 unsigned int hashlen,
549 const unsigned char *hash,
550 unsigned char *sig );
551
552/**
553 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
554 *
555 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200556 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 * MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100558 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
560 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
561 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100562 * \param hash buffer holding the message digest
563 * \param sig buffer that will hold the ciphertext
564 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100565 * \deprecated It is deprecated and discouraged to call this function
566 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
567 * are likely to remove the mode argument and have it implicitly
568 * set to MBEDTLS_RSA_PRIVATE.
569 *
570 * \note Alternative implementations of RSA need not support
571 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
572 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
573 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100574 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100576 *
577 * \note The "sig" buffer must be as large as the size
578 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
579 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200580 * \note The hash_id in the RSA context is the one used for the
581 * encoding. md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100582 * that is encoded. According to RFC 3447 it is advised to
583 * keep both hashes the same.
584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100586 int (*f_rng)(void *, unsigned char *, size_t),
587 void *p_rng,
588 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100590 unsigned int hashlen,
591 const unsigned char *hash,
592 unsigned char *sig );
593
594/**
595 * \brief Generic wrapper to perform a PKCS#1 verification using the
596 * mode from the context. Do a public RSA operation and check
597 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 *
599 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200601 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
603 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
604 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 * \param hash buffer holding the message digest
606 * \param sig buffer holding the ciphertext
607 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100608 * \deprecated It is deprecated and discouraged to call this function
609 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
610 * are likely to remove the mode argument and have it implicitly
611 * set to MBEDTLS_RSA_PUBLIC.
612 *
613 * \note Alternative implementations of RSA need not support
614 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
615 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
616 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000619 *
620 * \note The "sig" buffer must be as large as the size
621 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000622 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200623 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200627 int (*f_rng)(void *, unsigned char *, size_t),
628 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000631 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000632 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200633 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000634
635/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100636 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
637 *
638 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200640 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
642 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
643 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100644 * \param hash buffer holding the message digest
645 * \param sig buffer holding the ciphertext
646 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100647 * \deprecated It is deprecated and discouraged to call this function
648 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
649 * are likely to remove the mode argument and have it implicitly
650 * set to MBEDTLS_RSA_PUBLIC.
651 *
652 * \note Alternative implementations of RSA need not support
653 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
654 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
655 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100656 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100658 *
659 * \note The "sig" buffer must be as large as the size
660 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
661 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200663 int (*f_rng)(void *, unsigned char *, size_t),
664 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100665 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100667 unsigned int hashlen,
668 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200669 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100670
671/**
672 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200673 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100674 *
675 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200677 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
679 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
680 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100681 * \param hash buffer holding the message digest
682 * \param sig buffer holding the ciphertext
683 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100684 * \deprecated It is deprecated and discouraged to call this function
685 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
686 * are likely to remove the mode argument and have it implicitly
687 * set to MBEDTLS_RSA_PUBLIC.
688 *
689 * \note Alternative implementations of RSA need not support
690 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
691 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
692 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100693 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100695 *
696 * \note The "sig" buffer must be as large as the size
697 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
698 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200699 * \note The hash_id in the RSA context is the one used for the
700 * verification. md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200701 * hash that is verified. According to RFC 3447 it is advised to
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200702 * keep both hashes the same. If hash_id in the RSA context is
703 * unset, the md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200706 int (*f_rng)(void *, unsigned char *, size_t),
707 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100708 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100710 unsigned int hashlen,
711 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200712 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100713
714/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200715 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
716 * (This is the version with "full" options.)
717 *
718 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200720 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
722 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
723 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200724 * \param hash buffer holding the message digest
725 * \param mgf1_hash_id message digest used for mask generation
726 * \param expected_salt_len Length of the salt used in padding, use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 * MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200728 * \param sig buffer holding the ciphertext
729 *
730 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 * or an MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200732 *
733 * \note The "sig" buffer must be as large as the size
734 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
735 *
736 * \note The hash_id in the RSA context is ignored.
737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200739 int (*f_rng)(void *, unsigned char *, size_t),
740 void *p_rng,
741 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200743 unsigned int hashlen,
744 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200746 int expected_salt_len,
747 const unsigned char *sig );
748
749/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200750 * \brief Copy the components of an RSA context
751 *
752 * \param dst Destination context
753 * \param src Source context
754 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200755 * \return 0 on success,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200756 * MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200759
760/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000761 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000762 *
763 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000766
767/**
768 * \brief Checkup routine
769 *
770 * \return 0 if successful, or 1 if the test failed
771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
774#ifdef __cplusplus
775}
776#endif
777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200779
Paul Bakker5121ce52009-01-03 21:22:43 +0000780#endif /* rsa.h */