blob: abca193606f8423bbfed89ff04de7dc968048f02 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
6 * Copyright (C) 2009 Paul Bakker
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000021 */
Paul Bakker40e46942009-01-03 21:51:57 +000022#ifndef POLARSSL_RSA_H
23#define POLARSSL_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000024
Paul Bakker8e831ed2009-01-03 21:24:11 +000025#include "polarssl/bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Paul Bakker40e46942009-01-03 21:51:57 +000027#define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x0400
28#define POLARSSL_ERR_RSA_INVALID_PADDING -0x0410
29#define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x0420
30#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x0430
31#define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x0440
32#define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x0450
33#define POLARSSL_ERR_RSA_VERIFY_FAILED -0x0460
Paul Bakker5121ce52009-01-03 21:22:43 +000034
35/*
36 * PKCS#1 constants
37 */
38#define RSA_RAW 0
39#define RSA_MD2 2
40#define RSA_MD4 3
41#define RSA_MD5 4
42#define RSA_SHA1 5
43#define RSA_SHA256 6
44
45#define RSA_PUBLIC 0
46#define RSA_PRIVATE 1
47
48#define RSA_PKCS_V15 0
49#define RSA_PKCS_V21 1
50
51#define RSA_SIGN 1
52#define RSA_CRYPT 2
53
54/*
55 * DigestInfo ::= SEQUENCE {
56 * digestAlgorithm DigestAlgorithmIdentifier,
57 * digest Digest }
58 *
59 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
60 *
61 * Digest ::= OCTET STRING
62 */
63#define ASN1_HASH_MDX \
64 "\x30\x20\x30\x0C\x06\x08\x2A\x86\x48" \
65 "\x86\xF7\x0D\x02\x00\x05\x00\x04\x10"
66
67#define ASN1_HASH_SHA1 \
68 "\x30\x21\x30\x09\x06\x05\x2B\x0E\x03" \
69 "\x02\x1A\x05\x00\x04\x14"
70
71/**
72 * \brief RSA context structure
73 */
74typedef struct
75{
76 int ver; /*!< always 0 */
77 int len; /*!< size(N) in chars */
78
79 mpi N; /*!< public modulus */
80 mpi E; /*!< public exponent */
81
82 mpi D; /*!< private exponent */
83 mpi P; /*!< 1st prime factor */
84 mpi Q; /*!< 2nd prime factor */
85 mpi DP; /*!< D % (P - 1) */
86 mpi DQ; /*!< D % (Q - 1) */
87 mpi QP; /*!< 1 / (Q % P) */
88
89 mpi RN; /*!< cached R^2 mod N */
90 mpi RP; /*!< cached R^2 mod P */
91 mpi RQ; /*!< cached R^2 mod Q */
92
93 int padding; /*!< 1.5 or OAEP/PSS */
94 int hash_id; /*!< hash identifier */
95 int (*f_rng)(void *); /*!< RNG function */
96 void *p_rng; /*!< RNG parameter */
97}
98rsa_context;
99
100#ifdef __cplusplus
101extern "C" {
102#endif
103
104/**
105 * \brief Initialize an RSA context
106 *
107 * \param ctx RSA context to be initialized
108 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
109 * \param hash_id RSA_PKCS_V21 hash identifier
110 * \param f_rng RNG function
111 * \param p_rng RNG parameter
112 *
113 * \note The hash_id parameter is actually ignored
114 * when using RSA_PKCS_V15 padding.
115 *
116 * \note Currently (xyssl-0.8), RSA_PKCS_V21 padding
117 * is not supported.
118 */
119void rsa_init( rsa_context *ctx,
120 int padding,
121 int hash_id,
122 int (*f_rng)(void *),
123 void *p_rng );
124
125/**
126 * \brief Generate an RSA keypair
127 *
128 * \param ctx RSA context that will hold the key
129 * \param nbits size of the public key in bits
130 * \param exponent public exponent (e.g., 65537)
131 *
132 * \note rsa_init() must be called beforehand to setup
133 * the RSA context (especially f_rng and p_rng).
134 *
Paul Bakker40e46942009-01-03 21:51:57 +0000135 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 */
137int rsa_gen_key( rsa_context *ctx, int nbits, int exponent );
138
139/**
140 * \brief Check a public RSA key
141 *
142 * \param ctx RSA context to be checked
143 *
Paul Bakker40e46942009-01-03 21:51:57 +0000144 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 */
146int rsa_check_pubkey( rsa_context *ctx );
147
148/**
149 * \brief Check a private RSA key
150 *
151 * \param ctx RSA context to be checked
152 *
Paul Bakker40e46942009-01-03 21:51:57 +0000153 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 */
155int rsa_check_privkey( rsa_context *ctx );
156
157/**
158 * \brief Do an RSA public key operation
159 *
160 * \param ctx RSA context
161 * \param input input buffer
162 * \param output output buffer
163 *
Paul Bakker40e46942009-01-03 21:51:57 +0000164 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 *
166 * \note This function does NOT take care of message
167 * padding. Also, be sure to set input[0] = 0.
168 *
169 * \note The input and output buffers must be large
170 * enough (eg. 128 bytes if RSA-1024 is used).
171 */
172int rsa_public( rsa_context *ctx,
173 unsigned char *input,
174 unsigned char *output );
175
176/**
177 * \brief Do an RSA private key operation
178 *
179 * \param ctx RSA context
180 * \param input input buffer
181 * \param output output buffer
182 *
Paul Bakker40e46942009-01-03 21:51:57 +0000183 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 *
185 * \note The input and output buffers must be large
186 * enough (eg. 128 bytes if RSA-1024 is used).
187 */
188int rsa_private( rsa_context *ctx,
189 unsigned char *input,
190 unsigned char *output );
191
192/**
193 * \brief Add the message padding, then do an RSA operation
194 *
195 * \param ctx RSA context
196 * \param mode RSA_PUBLIC or RSA_PRIVATE
197 * \param ilen contains the the plaintext length
198 * \param input buffer holding the data to be encrypted
199 * \param output buffer that will hold the ciphertext
200 *
Paul Bakker40e46942009-01-03 21:51:57 +0000201 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 *
203 * \note The output buffer must be as large as the size
204 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
205 */
206int rsa_pkcs1_encrypt( rsa_context *ctx,
207 int mode, int ilen,
208 unsigned char *input,
209 unsigned char *output );
210
211/**
212 * \brief Do an RSA operation, then remove the message padding
213 *
214 * \param ctx RSA context
215 * \param mode RSA_PUBLIC or RSA_PRIVATE
216 * \param input buffer holding the encrypted data
217 * \param output buffer that will hold the plaintext
218 * \param olen will contain the plaintext length
219 *
Paul Bakker40e46942009-01-03 21:51:57 +0000220 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 *
222 * \note The output buffer must be as large as the size
223 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
224 */
225int rsa_pkcs1_decrypt( rsa_context *ctx,
226 int mode, int *olen,
227 unsigned char *input,
228 unsigned char *output );
229
230/**
231 * \brief Do a private RSA to sign a message digest
232 *
233 * \param ctx RSA context
234 * \param mode RSA_PUBLIC or RSA_PRIVATE
235 * \param hash_id RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256}
236 * \param hashlen message digest length (for RSA_RAW only)
237 * \param hash buffer holding the message digest
238 * \param sig buffer that will hold the ciphertext
239 *
240 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000241 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 *
243 * \note The "sig" buffer must be as large as the size
244 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
245 */
246int rsa_pkcs1_sign( rsa_context *ctx,
247 int mode,
248 int hash_id,
249 int hashlen,
250 unsigned char *hash,
251 unsigned char *sig );
252
253/**
254 * \brief Do a public RSA and check the message digest
255 *
256 * \param ctx points to an RSA public key
257 * \param mode RSA_PUBLIC or RSA_PRIVATE
258 * \param hash_id RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256}
259 * \param hashlen message digest length (for RSA_RAW only)
260 * \param hash buffer holding the message digest
261 * \param sig buffer holding the ciphertext
262 *
263 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000264 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 *
266 * \note The "sig" buffer must be as large as the size
267 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
268 */
269int rsa_pkcs1_verify( rsa_context *ctx,
270 int mode,
271 int hash_id,
272 int hashlen,
273 unsigned char *hash,
274 unsigned char *sig );
275
276/**
277 * \brief Free the components of an RSA key
278 */
279void rsa_free( rsa_context *ctx );
280
281/**
282 * \brief Checkup routine
283 *
284 * \return 0 if successful, or 1 if the test failed
285 */
286int rsa_self_test( int verbose );
287
288#ifdef __cplusplus
289}
290#endif
291
292#endif /* rsa.h */