blob: 2ffb7ab3f8d8b5c5f33bc3da716bc4607f676d6e [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 Becker3cdc7112017-10-05 10:09:31 +010051#define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500 /**< The implementation doesn't provide the requested operation. */
Paul Bakker5121ce52009-01-03 21:22:43 +000052
53/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020054 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define MBEDTLS_RSA_PUBLIC 0
57#define MBEDTLS_RSA_PRIVATE 1
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define MBEDTLS_RSA_PKCS_V15 0
60#define MBEDTLS_RSA_PKCS_V21 1
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define MBEDTLS_RSA_SIGN 1
63#define MBEDTLS_RSA_CRYPT 2
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define MBEDTLS_RSA_SALT_LEN_ANY -1
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +020066
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020067/*
68 * The above constants may be used even if the RSA module is compile out,
69 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
70 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020072
Paul Bakker407a0da2013-06-27 14:29:21 +020073#ifdef __cplusplus
74extern "C" {
75#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/**
78 * \brief RSA context structure
79 */
80typedef struct
81{
82 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +000083 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +000084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 mbedtls_mpi N; /*!< public modulus */
86 mbedtls_mpi E; /*!< public exponent */
Paul Bakker5121ce52009-01-03 21:22:43 +000087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_mpi D; /*!< private exponent */
89 mbedtls_mpi P; /*!< 1st prime factor */
90 mbedtls_mpi Q; /*!< 2nd prime factor */
91 mbedtls_mpi DP; /*!< D % (P - 1) */
92 mbedtls_mpi DQ; /*!< D % (Q - 1) */
93 mbedtls_mpi QP; /*!< 1 / (Q % P) */
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_mpi RN; /*!< cached R^2 mod N */
96 mbedtls_mpi RP; /*!< cached R^2 mod P */
97 mbedtls_mpi RQ; /*!< cached R^2 mod Q */
Paul Bakker5121ce52009-01-03 21:22:43 +000098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_mpi Vi; /*!< cached blinding value */
100 mbedtls_mpi Vf; /*!< cached un-blinding value */
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 int padding; /*!< MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
Brian J Murray98844ff2016-08-30 01:50:12 -0700103 MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
105 specified in the mbedtls_md.h header file
Paul Bakker9dcc3222011-03-08 14:16:06 +0000106 for the EME-OAEP and EMSA-PSS
107 encoding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#if defined(MBEDTLS_THREADING_C)
109 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200110#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000111}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112mbedtls_rsa_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
Paul Bakker5121ce52009-01-03 21:22:43 +0000114/**
115 * \brief Initialize an RSA context
116 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 * Note: Set padding to MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
Paul Bakker9a736322012-11-14 12:39:52 +0000118 * encryption scheme and the RSASSA-PSS signature scheme.
119 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 * \param ctx RSA context to be initialized
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
122 * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 *
124 * \note The hash_id parameter is actually ignored
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 * when using MBEDTLS_RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200126 *
127 * \note Choice of padding mode is strictly enforced for private key
128 * operations, since there might be security concerns in
129 * mixing padding modes. For public key operations it's merely
130 * a default value, which can be overriden by calling specific
131 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
132 *
133 * \note The chosen hash is always used for OEAP encryption.
134 * For PSS signatures, it's always used for making signatures,
135 * but can be overriden (and always is, if set to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 * MBEDTLS_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 int padding,
Paul Bakker42099c32014-01-27 11:45:49 +0100140 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
142/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100143 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 * See \c mbedtls_rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100145 *
146 * \param ctx RSA context to be set
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
148 * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id);
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100151
152/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 * \brief Generate an RSA keypair
154 *
155 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000156 * \param f_rng RNG function
157 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 * \param nbits size of the public key in bits
159 * \param exponent public exponent (e.g., 65537)
160 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 * \note mbedtls_rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000162 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000167 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000168 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000169 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171/**
172 * \brief Check a public RSA key
173 *
174 * \param ctx RSA context to be checked
175 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180/**
181 * \brief Check a private RSA key
182 *
183 * \param ctx RSA context to be checked
184 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189/**
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100190 * \brief Check a public-private RSA key pair.
191 * Check each of the contexts, and make sure they match.
192 *
193 * \param pub RSA context holding the public key
194 * \param prv RSA context holding the private key
195 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100199
200/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 * \brief Do an RSA public key operation
202 *
203 * \param ctx RSA context
204 * \param input input buffer
205 * \param output output buffer
206 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 *
209 * \note This function does NOT take care of message
Brian J Murray2adecba2016-11-06 04:45:15 -0800210 * padding. Also, be sure to set input[0] = 0 or ensure that
Paul Bakker619467a2009-03-28 23:26:51 +0000211 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 *
213 * \note The input and output buffers must be large
214 * enough (eg. 128 bytes if RSA-1024 is used).
215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000217 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 unsigned char *output );
219
220/**
221 * \brief Do an RSA private key operation
222 *
223 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200224 * \param f_rng RNG function (Needed for blinding)
225 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 * \param input input buffer
227 * \param output output buffer
228 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 *
231 * \note The input and output buffers must be large
232 * enough (eg. 128 bytes if RSA-1024 is used).
233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200235 int (*f_rng)(void *, unsigned char *, size_t),
236 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000237 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 unsigned char *output );
239
240/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100241 * \brief Generic wrapper to perform a PKCS#1 encryption using the
242 * mode from the context. Add the message padding, then do an
243 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 *
245 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200246 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 * and MBEDTLS_RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000248 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000250 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 * \param input buffer holding the data to be encrypted
252 * \param output buffer that will hold the ciphertext
253 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100254 * \deprecated It is deprecated and discouraged to call this function
255 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
256 * are likely to remove the mode argument and have it implicitly
257 * set to MBEDTLS_RSA_PUBLIC.
258 *
259 * \note Alternative implementations of RSA need not support
260 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
261 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
262 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 *
265 * \note The output buffer must be as large as the size
266 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000269 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000270 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000271 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000272 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 unsigned char *output );
274
275/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100276 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
277 *
278 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 * \param f_rng RNG function (Needed for padding and MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100280 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100282 * \param ilen contains the plaintext length
283 * \param input buffer holding the data to be encrypted
284 * \param output buffer that will hold the ciphertext
285 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100286 * \deprecated It is deprecated and discouraged to call this function
287 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
288 * are likely to remove the mode argument and have it implicitly
289 * set to MBEDTLS_RSA_PUBLIC.
290 *
291 * \note Alternative implementations of RSA need not support
292 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
293 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
294 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100296 *
297 * \note The output buffer must be as large as the size
298 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100301 int (*f_rng)(void *, unsigned char *, size_t),
302 void *p_rng,
303 int mode, size_t ilen,
304 const unsigned char *input,
305 unsigned char *output );
306
307/**
308 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
309 *
310 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200311 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 * and MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100313 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100315 * \param label buffer holding the custom label to use
316 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100317 * \param ilen contains the plaintext length
318 * \param input buffer holding the data to be encrypted
319 * \param output buffer that will hold the ciphertext
320 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100321 * \deprecated It is deprecated and discouraged to call this function
322 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
323 * are likely to remove the mode argument and have it implicitly
324 * set to MBEDTLS_RSA_PUBLIC.
325 *
326 * \note Alternative implementations of RSA need not support
327 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
328 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
329 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100331 *
332 * \note The output buffer must be as large as the size
333 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100336 int (*f_rng)(void *, unsigned char *, size_t),
337 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100338 int mode,
339 const unsigned char *label, size_t label_len,
340 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100341 const unsigned char *input,
342 unsigned char *output );
343
344/**
345 * \brief Generic wrapper to perform a PKCS#1 decryption using the
346 * mode from the context. Do an RSA operation, then remove
347 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 *
349 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200351 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000353 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 * \param input buffer holding the encrypted data
355 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000356 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100358 * \deprecated It is deprecated and discouraged to call this function
359 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
360 * are likely to remove the mode argument and have it implicitly
361 * set to MBEDTLS_RSA_PRIVATE.
362 *
363 * \note Alternative implementations of RSA need not support
364 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
365 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
366 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 *
Hanno Beckerb6479192017-05-04 11:27:39 +0100369 * \note The output buffer length \c output_max_len should be
370 * as large as the size ctx->len of ctx->N (eg. 128 bytes
371 * if RSA-1024 is used) to be able to hold an arbitrary
372 * decrypted message. If it is not large enough to hold
Hanno Becker3cdc7112017-10-05 10:09:31 +0100373 * the decryption of the particular ciphertext provided,
Hanno Beckerb6479192017-05-04 11:27:39 +0100374 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
375 *
376 * \note The input buffer must be as large as the size
377 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200380 int (*f_rng)(void *, unsigned char *, size_t),
381 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000382 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000383 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000384 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000385 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000386
387/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100388 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
389 *
390 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200392 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkerb3869132013-02-28 17:21:01 +0100394 * \param olen will contain the plaintext length
395 * \param input buffer holding the encrypted data
396 * \param output buffer that will hold the plaintext
397 * \param output_max_len maximum length of the output buffer
398 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100399 * \deprecated It is deprecated and discouraged to call this function
400 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
401 * are likely to remove the mode argument and have it implicitly
402 * set to MBEDTLS_RSA_PRIVATE.
403 *
404 * \note Alternative implementations of RSA need not support
405 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
406 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
407 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100409 *
Hanno Beckerb6479192017-05-04 11:27:39 +0100410 * \note The output buffer length \c output_max_len should be
411 * as large as the size ctx->len of ctx->N (eg. 128 bytes
412 * if RSA-1024 is used) to be able to hold an arbitrary
413 * decrypted message. If it is not large enough to hold
Hanno Becker3cdc7112017-10-05 10:09:31 +0100414 * the decryption of the particular ciphertext provided,
Hanno Beckerb6479192017-05-04 11:27:39 +0100415 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
416 *
417 * \note The input buffer must be as large as the size
418 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200421 int (*f_rng)(void *, unsigned char *, size_t),
422 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100423 int mode, size_t *olen,
424 const unsigned char *input,
425 unsigned char *output,
426 size_t output_max_len );
427
428/**
429 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
430 *
431 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200433 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100435 * \param label buffer holding the custom label to use
436 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100437 * \param olen will contain the plaintext length
438 * \param input buffer holding the encrypted data
439 * \param output buffer that will hold the plaintext
440 * \param output_max_len maximum length of the output buffer
441 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100442 * \deprecated It is deprecated and discouraged to call this function
443 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
444 * are likely to remove the mode argument and have it implicitly
445 * set to MBEDTLS_RSA_PRIVATE.
446 *
447 * \note Alternative implementations of RSA need not support
448 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
449 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
450 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100452 *
Hanno Beckerb6479192017-05-04 11:27:39 +0100453 * \note The output buffer length \c output_max_len should be
454 * as large as the size ctx->len of ctx->N (eg. 128 bytes
455 * if RSA-1024 is used) to be able to hold an arbitrary
456 * decrypted message. If it is not large enough to hold
Hanno Becker3cdc7112017-10-05 10:09:31 +0100457 * the decryption of the particular ciphertext provided,
Hanno Beckerb6479192017-05-04 11:27:39 +0100458 * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
459 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100460 * \note The input buffer must be as large as the size
Hanno Beckerb6479192017-05-04 11:27:39 +0100461 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakkerb3869132013-02-28 17:21:01 +0100462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200464 int (*f_rng)(void *, unsigned char *, size_t),
465 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100466 int mode,
467 const unsigned char *label, size_t label_len,
468 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100469 const unsigned char *input,
470 unsigned char *output,
471 size_t output_max_len );
472
473/**
474 * \brief Generic wrapper to perform a PKCS#1 signature using the
475 * mode from the context. Do a private RSA operation to sign
476 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 *
478 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200479 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 * MBEDTLS_RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000481 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
483 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
484 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 * \param hash buffer holding the message digest
486 * \param sig buffer that will hold the ciphertext
487 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100488 * \deprecated It is deprecated and discouraged to call this function
489 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
490 * are likely to remove the mode argument and have it implicitly
491 * set to MBEDTLS_RSA_PRIVATE.
492 *
493 * \note Alternative implementations of RSA need not support
494 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
495 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
496 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 *
500 * \note The "sig" buffer must be as large as the size
501 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000502 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200503 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 * \note \c mbedtls_rsa_rsassa_pss_sign() for details on md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000507 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000508 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000511 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000512 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 unsigned char *sig );
514
515/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100516 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
517 *
518 * \param ctx RSA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200520 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
522 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
523 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100524 * \param hash buffer holding the message digest
525 * \param sig buffer that will hold the ciphertext
526 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100527 * \deprecated It is deprecated and discouraged to call this function
528 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
529 * are likely to remove the mode argument and have it implicitly
530 * set to MBEDTLS_RSA_PRIVATE.
531 *
532 * \note Alternative implementations of RSA need not support
533 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
534 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
535 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100536 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100538 *
539 * \note The "sig" buffer must be as large as the size
540 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200543 int (*f_rng)(void *, unsigned char *, size_t),
544 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100545 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100547 unsigned int hashlen,
548 const unsigned char *hash,
549 unsigned char *sig );
550
551/**
552 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
553 *
554 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200555 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 * MBEDTLS_RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100557 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
559 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
560 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100561 * \param hash buffer holding the message digest
562 * \param sig buffer that will hold the ciphertext
563 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100564 * \deprecated It is deprecated and discouraged to call this function
565 * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary
566 * are likely to remove the mode argument and have it implicitly
567 * set to MBEDTLS_RSA_PRIVATE.
568 *
569 * \note Alternative implementations of RSA need not support
570 * mode being set to MBEDTLS_RSA_PUBLIC and may instead
571 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
572 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100573 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100575 *
576 * \note The "sig" buffer must be as large as the size
577 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
578 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200579 * \note The hash_id in the RSA context is the one used for the
580 * encoding. md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100581 * that is encoded. According to RFC 3447 it is advised to
582 * keep both hashes the same.
583 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100585 int (*f_rng)(void *, unsigned char *, size_t),
586 void *p_rng,
587 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100589 unsigned int hashlen,
590 const unsigned char *hash,
591 unsigned char *sig );
592
593/**
594 * \brief Generic wrapper to perform a PKCS#1 verification using the
595 * mode from the context. Do a public RSA operation and check
596 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 *
598 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200600 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
602 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
603 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 * \param hash buffer holding the message digest
605 * \param sig buffer holding the ciphertext
606 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100607 * \deprecated It is deprecated and discouraged to call this function
608 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
609 * are likely to remove the mode argument and have it implicitly
610 * set to MBEDTLS_RSA_PUBLIC.
611 *
612 * \note Alternative implementations of RSA need not support
613 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
614 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
615 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 *
619 * \note The "sig" buffer must be as large as the size
620 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000621 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200622 * \note In case of PKCS#1 v2.1 encoding, see comments on
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200626 int (*f_rng)(void *, unsigned char *, size_t),
627 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000628 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000630 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000631 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200632 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
634/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100635 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
636 *
637 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200639 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
641 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
642 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100643 * \param hash buffer holding the message digest
644 * \param sig buffer holding the ciphertext
645 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100646 * \deprecated It is deprecated and discouraged to call this function
647 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
648 * are likely to remove the mode argument and have it implicitly
649 * set to MBEDTLS_RSA_PUBLIC.
650 *
651 * \note Alternative implementations of RSA need not support
652 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
653 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
654 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100655 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100657 *
658 * \note The "sig" buffer must be as large as the size
659 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200662 int (*f_rng)(void *, unsigned char *, size_t),
663 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100664 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100666 unsigned int hashlen,
667 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200668 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100669
670/**
671 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200672 * (This is the "simple" version.)
Paul Bakkerb3869132013-02-28 17:21:01 +0100673 *
674 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200676 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
678 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
679 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100680 * \param hash buffer holding the message digest
681 * \param sig buffer holding the ciphertext
682 *
Hanno Becker3cdc7112017-10-05 10:09:31 +0100683 * \deprecated It is deprecated and discouraged to call this function
684 * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary
685 * are likely to remove the mode argument and have it implicitly
686 * set to MBEDTLS_RSA_PUBLIC.
687 *
688 * \note Alternative implementations of RSA need not support
689 * mode being set to MBEDTLS_RSA_PRIVATE and may instead
690 * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
691 *
Paul Bakkerb3869132013-02-28 17:21:01 +0100692 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakkerb3869132013-02-28 17:21:01 +0100694 *
695 * \note The "sig" buffer must be as large as the size
696 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
697 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200698 * \note The hash_id in the RSA context is the one used for the
699 * verification. md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200700 * hash that is verified. According to RFC 3447 it is advised to
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200701 * keep both hashes the same. If hash_id in the RSA context is
702 * unset, the md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200705 int (*f_rng)(void *, unsigned char *, size_t),
706 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100707 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100709 unsigned int hashlen,
710 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200711 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100712
713/**
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200714 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
715 * (This is the version with "full" options.)
716 *
717 * \param ctx points to an RSA public key
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200719 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
721 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
722 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200723 * \param hash buffer holding the message digest
724 * \param mgf1_hash_id message digest used for mask generation
725 * \param expected_salt_len Length of the salt used in padding, use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 * MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200727 * \param sig buffer holding the ciphertext
728 *
729 * \return 0 if the verify operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 * or an MBEDTLS_ERR_RSA_XXX error code
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200731 *
732 * \note The "sig" buffer must be as large as the size
733 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
734 *
735 * \note The hash_id in the RSA context is ignored.
736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200738 int (*f_rng)(void *, unsigned char *, size_t),
739 void *p_rng,
740 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200742 unsigned int hashlen,
743 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +0200745 int expected_salt_len,
746 const unsigned char *sig );
747
748/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200749 * \brief Copy the components of an RSA context
750 *
751 * \param dst Destination context
752 * \param src Source context
753 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200754 * \return 0 on success,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200755 * MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200756 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200758
759/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000761 *
762 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
766/**
767 * \brief Checkup routine
768 *
769 * \return 0 if successful, or 1 if the test failed
770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771int mbedtls_rsa_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
773#ifdef __cplusplus
774}
775#endif
776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#endif /* MBEDTLS_RSA_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200778
Paul Bakker5121ce52009-01-03 21:22:43 +0000779#endif /* rsa.h */