blob: 47315a32a4d5d1f956c7bef5342282abb62ecd66 [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 Bakker407a0da2013-06-27 14:29:21 +02006 * Copyright (C) 2006-2013, 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
Paul Bakkered27a042013-04-18 22:46:23 +020030#include "config.h"
31
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
Paul Bakkerc9965dc2013-09-29 14:58:17 +020035#if defined(POLARSSL_THREADING_C)
36#include "threading.h"
37#endif
38
Paul Bakker13e2dfe2009-07-28 07:18:38 +000039/*
40 * RSA Error codes
41 */
Paul Bakker9d781402011-05-09 16:17:09 +000042#define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
43#define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
44#define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
45#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the libraries validity check. */
46#define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
47#define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
48#define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
49#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
50#define POLARSSL_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
Paul Bakker5121ce52009-01-03 21:22:43 +000051
52/*
Paul Bakkerc70b9822013-04-07 22:00:46 +020053 * RSA constants
Paul Bakker5121ce52009-01-03 21:22:43 +000054 */
Paul Bakker5121ce52009-01-03 21:22:43 +000055#define RSA_PUBLIC 0
56#define RSA_PRIVATE 1
57
58#define RSA_PKCS_V15 0
59#define RSA_PKCS_V21 1
60
61#define RSA_SIGN 1
62#define RSA_CRYPT 2
63
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020064/*
65 * The above constants may be used even if the RSA module is compile out,
66 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
67 */
68#if defined(POLARSSL_RSA_C)
69
Paul Bakker407a0da2013-06-27 14:29:21 +020070#ifdef __cplusplus
71extern "C" {
72#endif
73
Paul Bakker5121ce52009-01-03 21:22:43 +000074/**
75 * \brief RSA context structure
76 */
77typedef struct
78{
79 int ver; /*!< always 0 */
Paul Bakker23986e52011-04-24 08:57:21 +000080 size_t len; /*!< size(N) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +000081
82 mpi N; /*!< public modulus */
83 mpi E; /*!< public exponent */
84
85 mpi D; /*!< private exponent */
86 mpi P; /*!< 1st prime factor */
87 mpi Q; /*!< 2nd prime factor */
88 mpi DP; /*!< D % (P - 1) */
89 mpi DQ; /*!< D % (Q - 1) */
90 mpi QP; /*!< 1 / (Q % P) */
91
92 mpi RN; /*!< cached R^2 mod N */
93 mpi RP; /*!< cached R^2 mod P */
94 mpi RQ; /*!< cached R^2 mod Q */
95
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +020096#if !defined(POLARSSL_RSA_NO_CRT)
97 mpi Vi; /*!< cached blinding value */
98 mpi Vf; /*!< cached un-blinding value */
99#endif
100
Paul Bakker9dcc3222011-03-08 14:16:06 +0000101 int padding; /*!< RSA_PKCS_V15 for 1.5 padding and
102 RSA_PKCS_v21 for OAEP/PSS */
103 int hash_id; /*!< Hash identifier of md_type_t as
104 specified in the md.h header file
105 for the EME-OAEP and EMSA-PSS
106 encoding */
Paul Bakkerc9965dc2013-09-29 14:58:17 +0200107#if defined(POLARSSL_THREADING_C)
108 threading_mutex_t mutex; /*!< Thread-safety mutex */
109#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000110}
111rsa_context;
112
Paul Bakker5121ce52009-01-03 21:22:43 +0000113/**
114 * \brief Initialize an RSA context
115 *
Paul Bakker9a736322012-11-14 12:39:52 +0000116 * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP
117 * encryption scheme and the RSASSA-PSS signature scheme.
118 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 * \param ctx RSA context to be initialized
120 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
121 * \param hash_id RSA_PKCS_V21 hash identifier
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 *
123 * \note The hash_id parameter is actually ignored
124 * when using RSA_PKCS_V15 padding.
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 */
126void rsa_init( rsa_context *ctx,
127 int padding,
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100128 int hash_id );
129
130/**
131 * \brief Set the padding method on an initilized RSA context.
132 *
133 * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP
134 * encryption scheme and the RSASSA-PSS signature scheme.
135 *
136 * \param ctx RSA context to be set
137 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
138 * \param hash_id RSA_PKCS_V21 hash identifier
139 *
140 * \note The hash_id parameter is actually ignored
141 * when using RSA_PKCS_V15 padding.
142 */
143void rsa_set_padding( rsa_context *ctx,
144 int padding,
145 int hash_id );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147/**
148 * \brief Generate an RSA keypair
149 *
150 * \param ctx RSA context that will hold the key
Paul Bakker21eb2802010-08-16 11:10:02 +0000151 * \param f_rng RNG function
152 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 * \param nbits size of the public key in bits
154 * \param exponent public exponent (e.g., 65537)
155 *
156 * \note rsa_init() must be called beforehand to setup
Paul Bakker21eb2802010-08-16 11:10:02 +0000157 * the RSA context.
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 *
Paul Bakker40e46942009-01-03 21:51:57 +0000159 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 */
Paul Bakker21eb2802010-08-16 11:10:02 +0000161int rsa_gen_key( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000162 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000163 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000164 unsigned int nbits, int exponent );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
166/**
167 * \brief Check a public RSA key
168 *
169 * \param ctx RSA context to be checked
170 *
Paul Bakker40e46942009-01-03 21:51:57 +0000171 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000173int rsa_check_pubkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
175/**
176 * \brief Check a private RSA key
177 *
178 * \param ctx RSA context to be checked
179 *
Paul Bakker40e46942009-01-03 21:51:57 +0000180 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000182int rsa_check_privkey( const rsa_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
184/**
185 * \brief Do an RSA public key operation
186 *
187 * \param ctx RSA context
188 * \param input input buffer
189 * \param output output buffer
190 *
Paul Bakker40e46942009-01-03 21:51:57 +0000191 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 *
193 * \note This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000194 * padding. Also, be sure to set input[0] = 0 or assure that
195 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 *
197 * \note The input and output buffers must be large
198 * enough (eg. 128 bytes if RSA-1024 is used).
199 */
200int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000201 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 unsigned char *output );
203
204/**
205 * \brief Do an RSA private key operation
206 *
207 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200208 * \param f_rng RNG function (Needed for blinding)
209 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 * \param input input buffer
211 * \param output output buffer
212 *
Paul Bakker40e46942009-01-03 21:51:57 +0000213 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 *
215 * \note The input and output buffers must be large
216 * enough (eg. 128 bytes if RSA-1024 is used).
217 */
218int rsa_private( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200219 int (*f_rng)(void *, unsigned char *, size_t),
220 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000221 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 unsigned char *output );
223
224/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100225 * \brief Generic wrapper to perform a PKCS#1 encryption using the
226 * mode from the context. Add the message padding, then do an
227 * RSA operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 *
229 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200230 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
231 * and RSA_PRIVATE)
Paul Bakker21eb2802010-08-16 11:10:02 +0000232 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000234 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 * \param input buffer holding the data to be encrypted
236 * \param output buffer that will hold the ciphertext
237 *
Paul Bakker40e46942009-01-03 21:51:57 +0000238 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 *
240 * \note The output buffer must be as large as the size
241 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
242 */
243int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000244 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000245 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000246 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000247 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 unsigned char *output );
249
250/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100251 * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
252 *
253 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200254 * \param f_rng RNG function (Needed for padding and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100255 * \param p_rng RNG parameter
256 * \param mode RSA_PUBLIC or RSA_PRIVATE
257 * \param ilen contains the plaintext length
258 * \param input buffer holding the data to be encrypted
259 * \param output buffer that will hold the ciphertext
260 *
261 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
262 *
263 * \note The output buffer must be as large as the size
264 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
265 */
266int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx,
267 int (*f_rng)(void *, unsigned char *, size_t),
268 void *p_rng,
269 int mode, size_t ilen,
270 const unsigned char *input,
271 unsigned char *output );
272
273/**
274 * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
275 *
276 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200277 * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
278 * and RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100279 * \param p_rng RNG parameter
280 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100281 * \param label buffer holding the custom label to use
282 * \param label_len contains the label length
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 *
287 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
288 *
289 * \note The output buffer must be as large as the size
290 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
291 */
292int rsa_rsaes_oaep_encrypt( rsa_context *ctx,
293 int (*f_rng)(void *, unsigned char *, size_t),
294 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100295 int mode,
296 const unsigned char *label, size_t label_len,
297 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100298 const unsigned char *input,
299 unsigned char *output );
300
301/**
302 * \brief Generic wrapper to perform a PKCS#1 decryption using the
303 * mode from the context. Do an RSA operation, then remove
304 * the message padding
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 *
306 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200307 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200308 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker4d8ca702011-08-09 10:31:05 +0000310 * \param olen will contain the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 * \param input buffer holding the encrypted data
312 * \param output buffer that will hold the plaintext
Paul Bakker23986e52011-04-24 08:57:21 +0000313 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 *
Paul Bakker40e46942009-01-03 21:51:57 +0000315 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 *
317 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000318 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
319 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 */
321int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200322 int (*f_rng)(void *, unsigned char *, size_t),
323 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000324 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000325 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000326 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000327 size_t output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
329/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100330 * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
331 *
332 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200333 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200334 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100335 * \param mode RSA_PUBLIC or RSA_PRIVATE
336 * \param olen will contain the plaintext length
337 * \param input buffer holding the encrypted data
338 * \param output buffer that will hold the plaintext
339 * \param output_max_len maximum length of the output buffer
340 *
341 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
342 *
343 * \note The output buffer must be as large as the size
344 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
345 * an error is thrown.
346 */
347int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200348 int (*f_rng)(void *, unsigned char *, size_t),
349 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100350 int mode, size_t *olen,
351 const unsigned char *input,
352 unsigned char *output,
353 size_t output_max_len );
354
355/**
356 * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
357 *
358 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200359 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200360 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100361 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkera43231c2013-02-28 17:33:49 +0100362 * \param label buffer holding the custom label to use
363 * \param label_len contains the label length
Paul Bakkerb3869132013-02-28 17:21:01 +0100364 * \param olen will contain the plaintext length
365 * \param input buffer holding the encrypted data
366 * \param output buffer that will hold the plaintext
367 * \param output_max_len maximum length of the output buffer
368 *
369 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
370 *
371 * \note The output buffer must be as large as the size
372 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
373 * an error is thrown.
374 */
375int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200376 int (*f_rng)(void *, unsigned char *, size_t),
377 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100378 int mode,
379 const unsigned char *label, size_t label_len,
380 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100381 const unsigned char *input,
382 unsigned char *output,
383 size_t output_max_len );
384
385/**
386 * \brief Generic wrapper to perform a PKCS#1 signature using the
387 * mode from the context. Do a private RSA operation to sign
388 * a message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 *
390 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200391 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
392 * RSA_PRIVATE)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000393 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200395 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
396 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 * \param hash buffer holding the message digest
398 * \param sig buffer that will hold the ciphertext
399 *
400 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000401 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 *
403 * \note The "sig" buffer must be as large as the size
404 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000405 *
406 * \note In case of PKCS#1 v2.1 encoding keep in mind that
407 * the hash_id in the RSA context is the one used for the
408 * encoding. hash_id in the function call is the type of hash
409 * that is encoded. According to RFC 3447 it is advised to
410 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 */
412int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000413 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000414 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200416 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000417 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000418 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 unsigned char *sig );
420
421/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100422 * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
423 *
424 * \param ctx RSA context
Paul Bakkerf451bac2013-08-30 15:37:02 +0200425 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200426 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100427 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200428 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
429 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100430 * \param hash buffer holding the message digest
431 * \param sig buffer that will hold the ciphertext
432 *
433 * \return 0 if the signing operation was successful,
434 * or an POLARSSL_ERR_RSA_XXX error code
435 *
436 * \note The "sig" buffer must be as large as the size
437 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
438 */
439int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200440 int (*f_rng)(void *, unsigned char *, size_t),
441 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100442 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200443 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100444 unsigned int hashlen,
445 const unsigned char *hash,
446 unsigned char *sig );
447
448/**
449 * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
450 *
451 * \param ctx RSA context
Paul Bakker548957d2013-08-30 10:30:02 +0200452 * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
453 * RSA_PRIVATE)
Paul Bakkerb3869132013-02-28 17:21:01 +0100454 * \param p_rng RNG parameter
455 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200456 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
457 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100458 * \param hash buffer holding the message digest
459 * \param sig buffer that will hold the ciphertext
460 *
461 * \return 0 if the signing operation was successful,
462 * or an POLARSSL_ERR_RSA_XXX error code
463 *
464 * \note The "sig" buffer must be as large as the size
465 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
466 *
467 * \note In case of PKCS#1 v2.1 encoding keep in mind that
468 * the hash_id in the RSA context is the one used for the
469 * encoding. hash_id in the function call is the type of hash
470 * that is encoded. According to RFC 3447 it is advised to
471 * keep both hashes the same.
472 */
473int rsa_rsassa_pss_sign( rsa_context *ctx,
474 int (*f_rng)(void *, unsigned char *, size_t),
475 void *p_rng,
476 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200477 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100478 unsigned int hashlen,
479 const unsigned char *hash,
480 unsigned char *sig );
481
482/**
483 * \brief Generic wrapper to perform a PKCS#1 verification using the
484 * mode from the context. Do a public RSA operation and check
485 * the message digest
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 *
487 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200488 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200489 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200491 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
492 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 * \param hash buffer holding the message digest
494 * \param sig buffer holding the ciphertext
495 *
496 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000497 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 *
499 * \note The "sig" buffer must be as large as the size
500 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
Paul Bakker9dcc3222011-03-08 14:16:06 +0000501 *
502 * \note In case of PKCS#1 v2.1 encoding keep in mind that
503 * the hash_id in the RSA context is the one used for the
504 * verification. hash_id in the function call is the type of hash
505 * that is verified. According to RFC 3447 it is advised to
506 * keep both hashes the same.
Paul Bakker5121ce52009-01-03 21:22:43 +0000507 */
508int rsa_pkcs1_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200509 int (*f_rng)(void *, unsigned char *, size_t),
510 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200512 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000513 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000514 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200515 const unsigned char *sig );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
517/**
Paul Bakkerb3869132013-02-28 17:21:01 +0100518 * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
519 *
520 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200521 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200522 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100523 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200524 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
525 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100526 * \param hash buffer holding the message digest
527 * \param sig buffer holding the ciphertext
528 *
529 * \return 0 if the verify operation was successful,
530 * or an POLARSSL_ERR_RSA_XXX error code
531 *
532 * \note The "sig" buffer must be as large as the size
533 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
534 */
535int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200536 int (*f_rng)(void *, unsigned char *, size_t),
537 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100538 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200539 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100540 unsigned int hashlen,
541 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200542 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100543
544/**
545 * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
Paul Bakkerb3869132013-02-28 17:21:01 +0100546 *
547 * \param ctx points to an RSA public key
Paul Bakkerf451bac2013-08-30 15:37:02 +0200548 * \param f_rng RNG function (Only needed for RSA_PRIVATE)
Paul Bakker548957d2013-08-30 10:30:02 +0200549 * \param p_rng RNG parameter
Paul Bakkerb3869132013-02-28 17:21:01 +0100550 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerc70b9822013-04-07 22:00:46 +0200551 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
552 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakkerb3869132013-02-28 17:21:01 +0100553 * \param hash buffer holding the message digest
554 * \param sig buffer holding the ciphertext
555 *
556 * \return 0 if the verify operation was successful,
557 * or an POLARSSL_ERR_RSA_XXX error code
558 *
559 * \note The "sig" buffer must be as large as the size
560 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
561 *
562 * \note In case of PKCS#1 v2.1 encoding keep in mind that
563 * the hash_id in the RSA context is the one used for the
564 * verification. hash_id in the function call is the type of hash
565 * that is verified. According to RFC 3447 it is advised to
566 * keep both hashes the same.
567 */
568int rsa_rsassa_pss_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200569 int (*f_rng)(void *, unsigned char *, size_t),
570 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100571 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200572 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100573 unsigned int hashlen,
574 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +0200575 const unsigned char *sig );
Paul Bakkerb3869132013-02-28 17:21:01 +0100576
577/**
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +0200578 * \brief Copy the components of an RSA context
579 *
580 * \param dst Destination context
581 * \param src Source context
582 *
583 * \return O on success,
584 * POLARSSL_ERR_MPI_MALLOC_FAILED on memory allocation failure
585 */
586int rsa_copy( rsa_context *dst, const rsa_context *src );
587
588/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000590 *
591 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 */
593void rsa_free( rsa_context *ctx );
594
595/**
596 * \brief Checkup routine
597 *
598 * \return 0 if successful, or 1 if the test failed
599 */
600int rsa_self_test( int verbose );
601
602#ifdef __cplusplus
603}
604#endif
605
Paul Bakkered27a042013-04-18 22:46:23 +0200606#endif /* POLARSSL_RSA_C */
607
Paul Bakker5121ce52009-01-03 21:22:43 +0000608#endif /* rsa.h */