blob: 49a04b3cd64e65204a7373026682c226091a801b [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 *
Paul Bakker785a9ee2009-01-25 14:15:10 +00006 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
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 Bakkerb5bf1762009-07-19 20:28:35 +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 Bakker38e2b482009-07-19 20:41:06 +000034#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE 0x0470
Paul Bakker5121ce52009-01-03 21:22:43 +000035
36/*
37 * PKCS#1 constants
38 */
Paul Bakkerfc22c442009-07-19 20:36:27 +000039#define SIG_RSA_RAW 0
40#define SIG_RSA_MD2 2
41#define SIG_RSA_MD4 3
42#define SIG_RSA_MD5 4
Paul Bakker4593aea2009-02-09 22:32:35 +000043#define SIG_RSA_SHA1 5
44#define SIG_RSA_SHA224 14
45#define SIG_RSA_SHA256 11
46#define SIG_RSA_SHA384 12
47#define SIG_RSA_SHA512 13
Paul Bakker5121ce52009-01-03 21:22:43 +000048
49#define RSA_PUBLIC 0
50#define RSA_PRIVATE 1
51
52#define RSA_PKCS_V15 0
53#define RSA_PKCS_V21 1
54
55#define RSA_SIGN 1
56#define RSA_CRYPT 2
57
Paul Bakker4593aea2009-02-09 22:32:35 +000058#define ASN1_STR_CONSTRUCTED_SEQUENCE "\x30"
59#define ASN1_STR_NULL "\x05"
60#define ASN1_STR_OID "\x06"
61#define ASN1_STR_OCTET_STRING "\x04"
62
63#define OID_DIGEST_ALG_MDX "\x2A\x86\x48\x86\xF7\x0D\x02\x00"
64#define OID_HASH_ALG_SHA1 "\x2b\x0e\x03\x02\x1a"
65#define OID_HASH_ALG_SHA2X "\x60\x86\x48\x01\x65\x03\x04\x02\x00"
66
67#define OID_ISO_MEMBER_BODIES "\x2a"
68#define OID_ISO_IDENTIFIED_ORG "\x2b"
69
70/*
71 * ISO Member bodies OID parts
72 */
73#define OID_COUNTRY_US "\x86\x48"
74#define OID_RSA_DATA_SECURITY "\x86\xf7\x0d"
75
76/*
77 * ISO Identified organization OID parts
78 */
79#define OID_OIW_SECSIG_SHA1 "\x0e\x03\x02\x1a"
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081/*
82 * DigestInfo ::= SEQUENCE {
83 * digestAlgorithm DigestAlgorithmIdentifier,
84 * digest Digest }
85 *
86 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
87 *
88 * Digest ::= OCTET STRING
89 */
Paul Bakker4593aea2009-02-09 22:32:35 +000090#define ASN1_HASH_MDX \
91( \
92 ASN1_STR_CONSTRUCTED_SEQUENCE "\x20" \
93 ASN1_STR_CONSTRUCTED_SEQUENCE "\x0C" \
94 ASN1_STR_OID "\x08" \
95 OID_DIGEST_ALG_MDX \
96 ASN1_STR_NULL "\x00" \
97 ASN1_STR_OCTET_STRING "\x10" \
98)
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Paul Bakker4593aea2009-02-09 22:32:35 +0000100#define ASN1_HASH_SHA1 \
101 ASN1_STR_CONSTRUCTED_SEQUENCE "\x21" \
102 ASN1_STR_CONSTRUCTED_SEQUENCE "\x09" \
103 ASN1_STR_OID "\x05" \
104 OID_HASH_ALG_SHA1 \
105 ASN1_STR_NULL "\x00" \
106 ASN1_STR_OCTET_STRING "\x14"
107
108#define ASN1_HASH_SHA2X \
109 ASN1_STR_CONSTRUCTED_SEQUENCE "\x11" \
110 ASN1_STR_CONSTRUCTED_SEQUENCE "\x0d" \
111 ASN1_STR_OID "\x09" \
112 OID_HASH_ALG_SHA2X \
113 ASN1_STR_NULL "\x00" \
114 ASN1_STR_OCTET_STRING "\x00"
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
116/**
117 * \brief RSA context structure
118 */
119typedef struct
120{
121 int ver; /*!< always 0 */
122 int len; /*!< size(N) in chars */
123
124 mpi N; /*!< public modulus */
125 mpi E; /*!< public exponent */
126
127 mpi D; /*!< private exponent */
128 mpi P; /*!< 1st prime factor */
129 mpi Q; /*!< 2nd prime factor */
130 mpi DP; /*!< D % (P - 1) */
131 mpi DQ; /*!< D % (Q - 1) */
132 mpi QP; /*!< 1 / (Q % P) */
133
134 mpi RN; /*!< cached R^2 mod N */
135 mpi RP; /*!< cached R^2 mod P */
136 mpi RQ; /*!< cached R^2 mod Q */
137
138 int padding; /*!< 1.5 or OAEP/PSS */
139 int hash_id; /*!< hash identifier */
140 int (*f_rng)(void *); /*!< RNG function */
141 void *p_rng; /*!< RNG parameter */
142}
143rsa_context;
144
145#ifdef __cplusplus
146extern "C" {
147#endif
148
149/**
150 * \brief Initialize an RSA context
151 *
152 * \param ctx RSA context to be initialized
153 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
154 * \param hash_id RSA_PKCS_V21 hash identifier
155 * \param f_rng RNG function
156 * \param p_rng RNG parameter
157 *
158 * \note The hash_id parameter is actually ignored
159 * when using RSA_PKCS_V15 padding.
160 *
161 * \note Currently (xyssl-0.8), RSA_PKCS_V21 padding
162 * is not supported.
163 */
164void rsa_init( rsa_context *ctx,
165 int padding,
166 int hash_id,
167 int (*f_rng)(void *),
168 void *p_rng );
169
170/**
171 * \brief Generate an RSA keypair
172 *
173 * \param ctx RSA context that will hold the key
174 * \param nbits size of the public key in bits
175 * \param exponent public exponent (e.g., 65537)
176 *
177 * \note rsa_init() must be called beforehand to setup
178 * the RSA context (especially f_rng and p_rng).
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 */
182int rsa_gen_key( rsa_context *ctx, int nbits, int exponent );
183
184/**
185 * \brief Check a public RSA key
186 *
187 * \param ctx RSA context to be checked
188 *
Paul Bakker40e46942009-01-03 21:51:57 +0000189 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 */
191int rsa_check_pubkey( rsa_context *ctx );
192
193/**
194 * \brief Check a private RSA key
195 *
196 * \param ctx RSA context to be checked
197 *
Paul Bakker40e46942009-01-03 21:51:57 +0000198 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 */
200int rsa_check_privkey( rsa_context *ctx );
201
202/**
203 * \brief Do an RSA public key operation
204 *
205 * \param ctx RSA context
206 * \param input input buffer
207 * \param output output buffer
208 *
Paul Bakker40e46942009-01-03 21:51:57 +0000209 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 *
211 * \note This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000212 * padding. Also, be sure to set input[0] = 0 or assure that
213 * input is smaller than N.
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_public( rsa_context *ctx,
219 unsigned char *input,
220 unsigned char *output );
221
222/**
223 * \brief Do an RSA private key operation
224 *
225 * \param ctx RSA context
226 * \param input input buffer
227 * \param output output buffer
228 *
Paul Bakker40e46942009-01-03 21:51:57 +0000229 * \return 0 if successful, or an POLARSSL_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 */
234int rsa_private( rsa_context *ctx,
235 unsigned char *input,
236 unsigned char *output );
237
238/**
239 * \brief Add the message padding, then do an RSA operation
240 *
241 * \param ctx RSA context
242 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000243 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 * \param input buffer holding the data to be encrypted
245 * \param output buffer that will hold the ciphertext
246 *
Paul Bakker40e46942009-01-03 21:51:57 +0000247 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 *
249 * \note The output buffer must be as large as the size
250 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
251 */
252int rsa_pkcs1_encrypt( rsa_context *ctx,
253 int mode, int ilen,
254 unsigned char *input,
255 unsigned char *output );
256
257/**
258 * \brief Do an RSA operation, then remove the message padding
259 *
260 * \param ctx RSA context
261 * \param mode RSA_PUBLIC or RSA_PRIVATE
262 * \param input buffer holding the encrypted data
263 * \param output buffer that will hold the plaintext
264 * \param olen will contain the plaintext length
Paul Bakker060c5682009-01-12 21:48:39 +0000265 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 *
Paul Bakker40e46942009-01-03 21:51:57 +0000267 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 *
269 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000270 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
271 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 */
273int rsa_pkcs1_decrypt( rsa_context *ctx,
274 int mode, int *olen,
275 unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000276 unsigned char *output,
277 int output_max_len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000278
279/**
280 * \brief Do a private RSA to sign a message digest
281 *
282 * \param ctx RSA context
283 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerfc22c442009-07-19 20:36:27 +0000284 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
285 * \param hashlen message digest length (for SIG_RSA_RAW only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 * \param hash buffer holding the message digest
287 * \param sig buffer that will hold the ciphertext
288 *
289 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000290 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 *
292 * \note The "sig" buffer must be as large as the size
293 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
294 */
295int rsa_pkcs1_sign( rsa_context *ctx,
296 int mode,
297 int hash_id,
298 int hashlen,
299 unsigned char *hash,
300 unsigned char *sig );
301
302/**
303 * \brief Do a public RSA and check the message digest
304 *
305 * \param ctx points to an RSA public key
306 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerfc22c442009-07-19 20:36:27 +0000307 * \param hash_id SIG_RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256}
308 * \param hashlen message digest length (for SIG_RSA_RAW only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 * \param hash buffer holding the message digest
310 * \param sig buffer holding the ciphertext
311 *
312 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000313 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 *
315 * \note The "sig" buffer must be as large as the size
316 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
317 */
318int rsa_pkcs1_verify( rsa_context *ctx,
319 int mode,
320 int hash_id,
321 int hashlen,
322 unsigned char *hash,
323 unsigned char *sig );
324
325/**
326 * \brief Free the components of an RSA key
327 */
328void rsa_free( rsa_context *ctx );
329
330/**
331 * \brief Checkup routine
332 *
333 * \return 0 if successful, or 1 if the test failed
334 */
335int rsa_self_test( int verbose );
336
337#ifdef __cplusplus
338}
339#endif
340
341#endif /* rsa.h */