blob: 26dddbcd5ed61f46f8e7c05891b0d470dfb2e29a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file rsa.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker77b385e2009-07-28 17:23:11 +00007 * Joined copyright on original XySSL code with: Christophe Devine
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Paul Bakker40e46942009-01-03 21:51:57 +000023#ifndef POLARSSL_RSA_H
24#define POLARSSL_RSA_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Paul Bakker8e831ed2009-01-03 21:24:11 +000026#include "polarssl/bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker13e2dfe2009-07-28 07:18:38 +000028/*
29 * RSA Error codes
30 */
Paul Bakkerb5bf1762009-07-19 20:28:35 +000031#define POLARSSL_ERR_RSA_BAD_INPUT_DATA 0x0400
32#define POLARSSL_ERR_RSA_INVALID_PADDING 0x0410
33#define POLARSSL_ERR_RSA_KEY_GEN_FAILED 0x0420
34#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED 0x0430
35#define POLARSSL_ERR_RSA_PUBLIC_FAILED 0x0440
36#define POLARSSL_ERR_RSA_PRIVATE_FAILED 0x0450
37#define POLARSSL_ERR_RSA_VERIFY_FAILED 0x0460
Paul Bakker38e2b482009-07-19 20:41:06 +000038#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE 0x0470
Paul Bakker5121ce52009-01-03 21:22:43 +000039
40/*
41 * PKCS#1 constants
42 */
Paul Bakkerfc22c442009-07-19 20:36:27 +000043#define SIG_RSA_RAW 0
44#define SIG_RSA_MD2 2
45#define SIG_RSA_MD4 3
46#define SIG_RSA_MD5 4
Paul Bakker4593aea2009-02-09 22:32:35 +000047#define SIG_RSA_SHA1 5
48#define SIG_RSA_SHA224 14
49#define SIG_RSA_SHA256 11
50#define SIG_RSA_SHA384 12
51#define SIG_RSA_SHA512 13
Paul Bakker5121ce52009-01-03 21:22:43 +000052
53#define RSA_PUBLIC 0
54#define RSA_PRIVATE 1
55
56#define RSA_PKCS_V15 0
57#define RSA_PKCS_V21 1
58
59#define RSA_SIGN 1
60#define RSA_CRYPT 2
61
Paul Bakker4593aea2009-02-09 22:32:35 +000062#define ASN1_STR_CONSTRUCTED_SEQUENCE "\x30"
Paul Bakker13e2dfe2009-07-28 07:18:38 +000063#define ASN1_STR_NULL "\x05"
64#define ASN1_STR_OID "\x06"
65#define ASN1_STR_OCTET_STRING "\x04"
Paul Bakker4593aea2009-02-09 22:32:35 +000066
Paul Bakker13e2dfe2009-07-28 07:18:38 +000067#define OID_DIGEST_ALG_MDX "\x2A\x86\x48\x86\xF7\x0D\x02\x00"
68#define OID_HASH_ALG_SHA1 "\x2b\x0e\x03\x02\x1a"
69#define OID_HASH_ALG_SHA2X "\x60\x86\x48\x01\x65\x03\x04\x02\x00"
Paul Bakker4593aea2009-02-09 22:32:35 +000070
Paul Bakker13e2dfe2009-07-28 07:18:38 +000071#define OID_ISO_MEMBER_BODIES "\x2a"
72#define OID_ISO_IDENTIFIED_ORG "\x2b"
Paul Bakker4593aea2009-02-09 22:32:35 +000073
74/*
75 * ISO Member bodies OID parts
76 */
Paul Bakker13e2dfe2009-07-28 07:18:38 +000077#define OID_COUNTRY_US "\x86\x48"
78#define OID_RSA_DATA_SECURITY "\x86\xf7\x0d"
Paul Bakker4593aea2009-02-09 22:32:35 +000079
80/*
81 * ISO Identified organization OID parts
82 */
Paul Bakker13e2dfe2009-07-28 07:18:38 +000083#define OID_OIW_SECSIG_SHA1 "\x0e\x03\x02\x1a"
Paul Bakker4593aea2009-02-09 22:32:35 +000084
Paul Bakker5121ce52009-01-03 21:22:43 +000085/*
86 * DigestInfo ::= SEQUENCE {
87 * digestAlgorithm DigestAlgorithmIdentifier,
88 * digest Digest }
89 *
90 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
91 *
92 * Digest ::= OCTET STRING
93 */
Paul Bakker13e2dfe2009-07-28 07:18:38 +000094#define ASN1_HASH_MDX \
95( \
Paul Bakker4593aea2009-02-09 22:32:35 +000096 ASN1_STR_CONSTRUCTED_SEQUENCE "\x20" \
97 ASN1_STR_CONSTRUCTED_SEQUENCE "\x0C" \
Paul Bakker13e2dfe2009-07-28 07:18:38 +000098 ASN1_STR_OID "\x08" \
99 OID_DIGEST_ALG_MDX \
100 ASN1_STR_NULL "\x00" \
101 ASN1_STR_OCTET_STRING "\x10" \
Paul Bakker4593aea2009-02-09 22:32:35 +0000102)
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000104#define ASN1_HASH_SHA1 \
Paul Bakker4593aea2009-02-09 22:32:35 +0000105 ASN1_STR_CONSTRUCTED_SEQUENCE "\x21" \
106 ASN1_STR_CONSTRUCTED_SEQUENCE "\x09" \
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000107 ASN1_STR_OID "\x05" \
108 OID_HASH_ALG_SHA1 \
109 ASN1_STR_NULL "\x00" \
Paul Bakker4593aea2009-02-09 22:32:35 +0000110 ASN1_STR_OCTET_STRING "\x14"
111
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000112#define ASN1_HASH_SHA2X \
Paul Bakker4593aea2009-02-09 22:32:35 +0000113 ASN1_STR_CONSTRUCTED_SEQUENCE "\x11" \
114 ASN1_STR_CONSTRUCTED_SEQUENCE "\x0d" \
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000115 ASN1_STR_OID "\x09" \
116 OID_HASH_ALG_SHA2X \
117 ASN1_STR_NULL "\x00" \
Paul Bakker4593aea2009-02-09 22:32:35 +0000118 ASN1_STR_OCTET_STRING "\x00"
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
120/**
121 * \brief RSA context structure
122 */
123typedef struct
124{
125 int ver; /*!< always 0 */
126 int len; /*!< size(N) in chars */
127
128 mpi N; /*!< public modulus */
129 mpi E; /*!< public exponent */
130
131 mpi D; /*!< private exponent */
132 mpi P; /*!< 1st prime factor */
133 mpi Q; /*!< 2nd prime factor */
134 mpi DP; /*!< D % (P - 1) */
135 mpi DQ; /*!< D % (Q - 1) */
136 mpi QP; /*!< 1 / (Q % P) */
137
138 mpi RN; /*!< cached R^2 mod N */
139 mpi RP; /*!< cached R^2 mod P */
140 mpi RQ; /*!< cached R^2 mod Q */
141
142 int padding; /*!< 1.5 or OAEP/PSS */
143 int hash_id; /*!< hash identifier */
144 int (*f_rng)(void *); /*!< RNG function */
145 void *p_rng; /*!< RNG parameter */
146}
147rsa_context;
148
149#ifdef __cplusplus
150extern "C" {
151#endif
152
153/**
154 * \brief Initialize an RSA context
155 *
156 * \param ctx RSA context to be initialized
157 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
158 * \param hash_id RSA_PKCS_V21 hash identifier
159 * \param f_rng RNG function
160 * \param p_rng RNG parameter
161 *
162 * \note The hash_id parameter is actually ignored
163 * when using RSA_PKCS_V15 padding.
164 *
165 * \note Currently (xyssl-0.8), RSA_PKCS_V21 padding
166 * is not supported.
167 */
168void rsa_init( rsa_context *ctx,
169 int padding,
170 int hash_id,
171 int (*f_rng)(void *),
172 void *p_rng );
173
174/**
175 * \brief Generate an RSA keypair
176 *
177 * \param ctx RSA context that will hold the key
178 * \param nbits size of the public key in bits
179 * \param exponent public exponent (e.g., 65537)
180 *
181 * \note rsa_init() must be called beforehand to setup
182 * the RSA context (especially f_rng and p_rng).
183 *
Paul Bakker40e46942009-01-03 21:51:57 +0000184 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 */
186int rsa_gen_key( rsa_context *ctx, int nbits, int exponent );
187
188/**
189 * \brief Check a public RSA key
190 *
191 * \param ctx RSA context to be checked
192 *
Paul Bakker40e46942009-01-03 21:51:57 +0000193 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 */
195int rsa_check_pubkey( rsa_context *ctx );
196
197/**
198 * \brief Check a private RSA key
199 *
200 * \param ctx RSA context to be checked
201 *
Paul Bakker40e46942009-01-03 21:51:57 +0000202 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 */
204int rsa_check_privkey( rsa_context *ctx );
205
206/**
207 * \brief Do an RSA public key operation
208 *
209 * \param ctx RSA context
210 * \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 This function does NOT take care of message
Paul Bakker619467a2009-03-28 23:26:51 +0000216 * padding. Also, be sure to set input[0] = 0 or assure that
217 * input is smaller than N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 *
219 * \note The input and output buffers must be large
220 * enough (eg. 128 bytes if RSA-1024 is used).
221 */
222int rsa_public( rsa_context *ctx,
223 unsigned char *input,
224 unsigned char *output );
225
226/**
227 * \brief Do an RSA private key operation
228 *
229 * \param ctx RSA context
230 * \param input input buffer
231 * \param output output buffer
232 *
Paul Bakker40e46942009-01-03 21:51:57 +0000233 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 *
235 * \note The input and output buffers must be large
236 * enough (eg. 128 bytes if RSA-1024 is used).
237 */
238int rsa_private( rsa_context *ctx,
239 unsigned char *input,
240 unsigned char *output );
241
242/**
243 * \brief Add the message padding, then do an RSA operation
244 *
245 * \param ctx RSA context
246 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakker592457c2009-04-01 19:01:43 +0000247 * \param ilen contains the plaintext length
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 * \param input buffer holding the data to be encrypted
249 * \param output buffer that will hold the ciphertext
250 *
Paul Bakker40e46942009-01-03 21:51:57 +0000251 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 *
253 * \note The output buffer must be as large as the size
254 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
255 */
256int rsa_pkcs1_encrypt( rsa_context *ctx,
257 int mode, int ilen,
258 unsigned char *input,
259 unsigned char *output );
260
261/**
262 * \brief Do an RSA operation, then remove the message padding
263 *
264 * \param ctx RSA context
265 * \param mode RSA_PUBLIC or RSA_PRIVATE
266 * \param input buffer holding the encrypted data
267 * \param output buffer that will hold the plaintext
268 * \param olen will contain the plaintext length
Paul Bakker060c5682009-01-12 21:48:39 +0000269 * \param output_max_len maximum length of the output buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 *
Paul Bakker40e46942009-01-03 21:51:57 +0000271 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 *
273 * \note The output buffer must be as large as the size
Paul Bakker060c5682009-01-12 21:48:39 +0000274 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
275 * an error is thrown.
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 */
277int rsa_pkcs1_decrypt( rsa_context *ctx,
278 int mode, int *olen,
279 unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000280 unsigned char *output,
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000281 int output_max_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000282
283/**
284 * \brief Do a private RSA to sign a message digest
285 *
286 * \param ctx RSA context
287 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerfc22c442009-07-19 20:36:27 +0000288 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
289 * \param hashlen message digest length (for SIG_RSA_RAW only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 * \param hash buffer holding the message digest
291 * \param sig buffer that will hold the ciphertext
292 *
293 * \return 0 if the signing operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000294 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 *
296 * \note The "sig" buffer must be as large as the size
297 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
298 */
299int rsa_pkcs1_sign( rsa_context *ctx,
300 int mode,
301 int hash_id,
302 int hashlen,
303 unsigned char *hash,
304 unsigned char *sig );
305
306/**
307 * \brief Do a public RSA and check the message digest
308 *
309 * \param ctx points to an RSA public key
310 * \param mode RSA_PUBLIC or RSA_PRIVATE
Paul Bakkerfc22c442009-07-19 20:36:27 +0000311 * \param hash_id SIG_RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256}
312 * \param hashlen message digest length (for SIG_RSA_RAW only)
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 * \param hash buffer holding the message digest
314 * \param sig buffer holding the ciphertext
315 *
316 * \return 0 if the verify operation was successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000317 * or an POLARSSL_ERR_RSA_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 *
319 * \note The "sig" buffer must be as large as the size
320 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
321 */
322int rsa_pkcs1_verify( rsa_context *ctx,
323 int mode,
324 int hash_id,
325 int hashlen,
326 unsigned char *hash,
327 unsigned char *sig );
328
329/**
330 * \brief Free the components of an RSA key
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000331 *
332 * \param ctx RSA Context to free
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 */
334void rsa_free( rsa_context *ctx );
335
336/**
337 * \brief Checkup routine
338 *
339 * \return 0 if successful, or 1 if the test failed
340 */
341int rsa_self_test( int verbose );
342
343#ifdef __cplusplus
344}
345#endif
346
347#endif /* rsa.h */