blob: c57ff979eba9babe1d2e0d15bddd26d397a3b1c2 [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 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_RSA_H
28#define POLARSSL_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkered27a042013-04-18 22:46:23 +020031#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakkered27a042013-04-18 22:46:23 +020035
Paul Bakker314052f2011-08-15 09:07:52 +000036#include "bignum.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020037#include "md.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakkerc9965dc2013-09-29 14:58:17 +020039#if defined(POLARSSL_THREADING_C)
40#include "threading.h"
41#endif
42
Paul Bakker13e2dfe2009-07-28 07:18:38 +000043/*
44 * RSA Error codes
45 */
Paul Bakker9d781402011-05-09 16:17:09 +000046#define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
47#define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
48#define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
49#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the libraries validity check. */
50#define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
51#define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
52#define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
53#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
54#define POLARSSL_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Paul Bakker5121ce52009-01-03 21:22:43 +000055
56/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020057 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000058 */
Paul Bakker5121ce52009-01-03 21:22:43 +000059#define RSA_PUBLIC 0
60#define RSA_PRIVATE 1
61
62#define RSA_PKCS_V15 0
63#define RSA_PKCS_V21 1
64
65#define RSA_SIGN 1
66#define RSA_CRYPT 2
67
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 */
72#if defined(POLARSSL_RSA_C)
73
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
86 mpi N; /*!< public modulus */
87 mpi E; /*!< public exponent */
88
89 mpi D; /*!< private exponent */
90 mpi P; /*!< 1st prime factor */
91 mpi Q; /*!< 2nd prime factor */
92 mpi DP; /*!< D % (P - 1) */
93 mpi DQ; /*!< D % (Q - 1) */
94 mpi QP; /*!< 1 / (Q % P) */
95
96 mpi RN; /*!< cached R^2 mod N */
97 mpi RP; /*!< cached R^2 mod P */
98 mpi RQ; /*!< cached R^2 mod Q */
99
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200100#if !defined(POLARSSL_RSA_NO_CRT)
101 mpi Vi; /*!< cached blinding value */
102 mpi Vf; /*!< cached un-blinding value */
103#endif
104
Paul Bakker9dcc3222011-03-08 14:16:06 +0000105 int padding; /*!< RSA_PKCS_V15 for 1.5 padding and
106 RSA_PKCS_v21 for OAEP/PSS */
107 int hash_id; /*!< Hash identifier of md_type_t as
108 specified in the md.h header file
109 for the EME-OAEP and EMSA-PSS
110 encoding */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200111#if defined(POLARSSL_THREADING_C)
112 threading_mutex_t mutex; /*!< Thread-safety mutex */
113#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000114}
115rsa_context;
116
Paul Bakker5121ce52009-01-03 21:22:43 +0000117/**
118 * \brief Initialize an RSA context
119 *
Paul Bakker9a736322012-11-14 12:39:52 +0000120 * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP
121 * encryption scheme and the RSASSA-PSS signature scheme.
122 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 * \param ctx RSA context to be initialized
124 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
125 * \param hash_id RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 *
127 * \note The hash_id parameter is actually ignored
128 * when using RSA_PKCS_V15 padding.
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200129 *
130 * \note Choice of padding mode is strictly enforced for private key
131 * operations, since there might be security concerns in
132 * mixing padding modes. For public key operations it's merely
133 * a default value, which can be overriden by calling specific
134 * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
135 *
136 * \note The chosen hash is always used for OEAP encryption.
137 * For PSS signatures, it's always used for making signatures,
138 * but can be overriden (and always is, if set to
139 * POLARSSL_MD_NONE) for verifying them.
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 */
141void rsa_init( rsa_context *ctx,
142 int padding,
Paul Bakker42099c32014-01-27 11:45:49 +0100143 int hash_id);
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145/**
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100146 * \brief Set padding for an already initialized RSA context
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200147 * See \c rsa_init() for details.
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100148 *
149 * \param ctx RSA context to be set
150 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
151 * \param hash_id RSA_PKCS_V21 hash identifier
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100152 */
153void rsa_set_padding( rsa_context *ctx, int padding, int hash_id);
154
155/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 * \brief Generate an RSA keypair
157 *
158 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000159 * \param f_rng RNG function
160 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 * \param nbits size of the public key in bits
162 * \param exponent public exponent (e.g., 65537)
163 *
164 * \note rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000165 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 *
Paul Bakker40e46942009-01-03 21:51:57 +0000167 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 */
Paul Bakker21eb2802010-08-16 11:10:02 +0000169int rsa_gen_key( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000170 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000171 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000172 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000173
174/**
175 * \brief Check a public RSA key
176 *
177 * \param ctx RSA context to be checked
178 *
Paul Bakker40e46942009-01-03 21:51:57 +0000179 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000181int rsa_check_pubkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
183/**
184 * \brief Check a private RSA key
185 *
186 * \param ctx RSA context to be checked
187 *
Paul Bakker40e46942009-01-03 21:51:57 +0000188 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000190int rsa_check_privkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
192/**
193 * \brief Do an RSA public key operation
194 *
195 * \param ctx RSA context
196 * \param input input buffer
197 * \param output output buffer
198 *
Paul Bakker40e46942009-01-03 21:51:57 +0000199 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 *
201 * \note This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000202 * padding. Also, be sure to set input[0] = 0 or assure that
203 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 *
205 * \note The input and output buffers must be large
206 * enough (eg. 128 bytes if RSA-1024 is used).
207 */
208int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000209 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 unsigned char *output );
211
212/**
213 * \brief Do an RSA private key operation
214 *
215 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200216 * \param f_rng RNG function (Needed for blinding)
217 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 * \param input input buffer
219 * \param output output buffer
220 *
Paul Bakker40e46942009-01-03 21:51:57 +0000221 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 *
223 * \note The input and output buffers must be large
224 * enough (eg. 128 bytes if RSA-1024 is used).
225 */
226int rsa_private( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200227 int (*f_rng)(void *, unsigned char *, size_t),
228 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000229 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 unsigned char *output );
231
232/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100233 * \brief Generic wrapper to perform a PKCS#1 encryption using the
234 * mode from the context. Add the message padding, then do an
235 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 *
237 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200238 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
239 * and RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000240 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000242 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 * \param input buffer holding the data to be encrypted
244 * \param output buffer that will hold the ciphertext
245 *
Paul Bakker40e46942009-01-03 21:51:57 +0000246 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 *
248 * \note The output buffer must be as large as the size
249 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
250 */
251int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000252 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000253 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000254 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000255 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 unsigned char *output );
257
258/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100259 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
260 *
261 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200262 * \param f_rng RNG function (Needed for padding and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100263 * \param p_rng RNG parameter
264 * \param mode RSA_PUBLIC or RSA_PRIVATE
265 * \param ilen contains the plaintext length
266 * \param input buffer holding the data to be encrypted
267 * \param output buffer that will hold the ciphertext
268 *
269 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
270 *
271 * \note The output buffer must be as large as the size
272 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
273 */
274int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx,
275 int (*f_rng)(void *, unsigned char *, size_t),
276 void *p_rng,
277 int mode, size_t ilen,
278 const unsigned char *input,
279 unsigned char *output );
280
281/**
282 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
283 *
284 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200285 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
286 * and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100287 * \param p_rng RNG parameter
288 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100289 * \param label buffer holding the custom label to use
290 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100291 * \param ilen contains the plaintext length
292 * \param input buffer holding the data to be encrypted
293 * \param output buffer that will hold the ciphertext
294 *
295 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
296 *
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 */
300int rsa_rsaes_oaep_encrypt( rsa_context *ctx,
301 int (*f_rng)(void *, unsigned char *, size_t),
302 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100303 int mode,
304 const unsigned char *label, size_t label_len,
305 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100306 const unsigned char *input,
307 unsigned char *output );
308
309/**
310 * \brief Generic wrapper to perform a PKCS#1 decryption using the
311 * mode from the context. Do an RSA operation, then remove
312 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 *
314 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200315 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200316 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000318 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 * \param input buffer holding the encrypted data
320 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000321 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 *
Paul Bakker40e46942009-01-03 21:51:57 +0000323 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 *
325 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000326 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
327 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 */
329int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200330 int (*f_rng)(void *, unsigned char *, size_t),
331 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000332 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000333 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000334 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000335 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100338 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
339 *
340 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200341 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200342 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100343 * \param mode RSA_PUBLIC or RSA_PRIVATE
344 * \param olen will contain the plaintext length
345 * \param input buffer holding the encrypted data
346 * \param output buffer that will hold the plaintext
347 * \param output_max_len maximum length of the output buffer
348 *
349 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
350 *
351 * \note The output buffer must be as large as the size
352 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
353 * an error is thrown.
354 */
355int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200356 int (*f_rng)(void *, unsigned char *, size_t),
357 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100358 int mode, size_t *olen,
359 const unsigned char *input,
360 unsigned char *output,
361 size_t output_max_len );
362
363/**
364 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
365 *
366 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200367 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200368 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100369 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100370 * \param label buffer holding the custom label to use
371 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100372 * \param olen will contain the plaintext length
373 * \param input buffer holding the encrypted data
374 * \param output buffer that will hold the plaintext
375 * \param output_max_len maximum length of the output buffer
376 *
377 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
378 *
379 * \note The output buffer must be as large as the size
380 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
381 * an error is thrown.
382 */
383int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200384 int (*f_rng)(void *, unsigned char *, size_t),
385 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100386 int mode,
387 const unsigned char *label, size_t label_len,
388 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100389 const unsigned char *input,
390 unsigned char *output,
391 size_t output_max_len );
392
393/**
394 * \brief Generic wrapper to perform a PKCS#1 signature using the
395 * mode from the context. Do a private RSA operation to sign
396 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 *
398 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200399 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
400 * RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000401 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200403 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
404 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 * \param hash buffer holding the message digest
406 * \param sig buffer that will hold the ciphertext
407 *
408 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000409 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000410 *
411 * \note The "sig" buffer must be as large as the size
412 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000413 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200414 * \note In case of PKCS#1 v2.1 encoding, see comments on
415 * \note \c rsa_rsassa_pss_sign() for details on md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000416 */
417int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000418 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000419 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200421 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000422 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000423 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 unsigned char *sig );
425
426/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100427 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
428 *
429 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200430 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200431 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100432 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200433 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
434 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100435 * \param hash buffer holding the message digest
436 * \param sig buffer that will hold the ciphertext
437 *
438 * \return 0 if the signing operation was successful,
439 * or an POLARSSL_ERR_RSA_XXX error code
440 *
441 * \note The "sig" buffer must be as large as the size
442 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
443 */
444int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200445 int (*f_rng)(void *, unsigned char *, size_t),
446 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100447 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200448 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100449 unsigned int hashlen,
450 const unsigned char *hash,
451 unsigned char *sig );
452
453/**
454 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
455 *
456 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200457 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
458 * RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100459 * \param p_rng RNG parameter
460 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200461 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
462 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100463 * \param hash buffer holding the message digest
464 * \param sig buffer that will hold the ciphertext
465 *
466 * \return 0 if the signing operation was successful,
467 * or an POLARSSL_ERR_RSA_XXX error code
468 *
469 * \note The "sig" buffer must be as large as the size
470 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
471 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200472 * \note The hash_id in the RSA context is the one used for the
473 * encoding. md_alg in the function call is the type of hash
Paul Bakkerb3869132013-02-28 17:21:01 +0100474 * that is encoded. According to RFC 3447 it is advised to
475 * keep both hashes the same.
476 */
477int rsa_rsassa_pss_sign( rsa_context *ctx,
478 int (*f_rng)(void *, unsigned char *, size_t),
479 void *p_rng,
480 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200481 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100482 unsigned int hashlen,
483 const unsigned char *hash,
484 unsigned char *sig );
485
486/**
487 * \brief Generic wrapper to perform a PKCS#1 verification using the
488 * mode from the context. Do a public RSA operation and check
489 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 *
491 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200492 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200493 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200495 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
496 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 * \param hash buffer holding the message digest
498 * \param sig buffer holding the ciphertext
499 *
500 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000501 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000502 *
503 * \note The "sig" buffer must be as large as the size
504 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000505 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200506 * \note In case of PKCS#1 v2.1 encoding, see comments on
507 * \c rsa_rsassa_pss_verify() about md_alg and hash_id.
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 */
509int rsa_pkcs1_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200510 int (*f_rng)(void *, unsigned char *, size_t),
511 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200513 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000514 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000515 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200516 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
518/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100519 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
520 *
521 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200522 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200523 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100524 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200525 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
526 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100527 * \param hash buffer holding the message digest
528 * \param sig buffer holding the ciphertext
529 *
530 * \return 0 if the verify operation was successful,
531 * or an POLARSSL_ERR_RSA_XXX error code
532 *
533 * \note The "sig" buffer must be as large as the size
534 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
535 */
536int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200537 int (*f_rng)(void *, unsigned char *, size_t),
538 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100539 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200540 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100541 unsigned int hashlen,
542 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200543 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100544
545/**
546 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Paul Bakkerb3869132013-02-28 17:21:01 +0100547 *
548 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200549 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200550 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100551 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200552 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
553 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100554 * \param hash buffer holding the message digest
555 * \param sig buffer holding the ciphertext
556 *
557 * \return 0 if the verify operation was successful,
558 * or an POLARSSL_ERR_RSA_XXX error code
559 *
560 * \note The "sig" buffer must be as large as the size
561 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
562 *
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200563 * \note The hash_id in the RSA context is the one used for the
564 * verification. md_alg in the function call is the type of
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200565 * hash that is verified. According to RFC 3447 it is advised to
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200566 * keep both hashes the same. If hash_id in the RSA context is
567 * unset, the md_alg from the function call is used.
Paul Bakkerb3869132013-02-28 17:21:01 +0100568 */
569int rsa_rsassa_pss_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200570 int (*f_rng)(void *, unsigned char *, size_t),
571 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100572 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200573 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100574 unsigned int hashlen,
575 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200576 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100577
578/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200579 * \brief Copy the components of an RSA context
580 *
581 * \param dst Destination context
582 * \param src Source context
583 *
584 * \return O on success,
585 * POLARSSL_ERR_MPI_MALLOC_FAILED on memory allocation failure
586 */
587int rsa_copy( rsa_context *dst, const rsa_context *src );
588
589/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000591 *
592 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 */
594void rsa_free( rsa_context *ctx );
595
596/**
597 * \brief Checkup routine
598 *
599 * \return 0 if successful, or 1 if the test failed
600 */
601int rsa_self_test( int verbose );
602
603#ifdef __cplusplus
604}
605#endif
606
Paul Bakkered27a042013-04-18 22:46:23 +0200607#endif /* POLARSSL_RSA_C */
608
Paul Bakker5121ce52009-01-03 21:22:43 +0000609#endif /* rsa.h */