4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifndef TROPICSSL_RSA_H
36 #define TROPICSSL_RSA_H
38 #include "tropicssl/bignum.h"
40 #define TROPICSSL_ERR_RSA_BAD_INPUT_DATA -0x0400
41 #define TROPICSSL_ERR_RSA_INVALID_PADDING -0x0410
42 #define TROPICSSL_ERR_RSA_KEY_GEN_FAILED -0x0420
43 #define TROPICSSL_ERR_RSA_KEY_CHECK_FAILED -0x0430
44 #define TROPICSSL_ERR_RSA_PUBLIC_FAILED -0x0440
45 #define TROPICSSL_ERR_RSA_PRIVATE_FAILED -0x0450
46 #define TROPICSSL_ERR_RSA_VERIFY_FAILED -0x0460
47 #define TROPICSSL_ERR_RSA_OUTPUT_TO_LARGE -0x0470
62 #define RSA_PKCS_V15 0
63 #define RSA_PKCS_V21 1
69 * DigestInfo ::= SEQUENCE {
70 * digestAlgorithm DigestAlgorithmIdentifier,
73 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
75 * Digest ::= OCTET STRING
77 #define ASN1_HASH_MDX \
78 "\x30\x20\x30\x0C\x06\x08\x2A\x86\x48" \
79 "\x86\xF7\x0D\x02\x00\x05\x00\x04\x10"
81 #define ASN1_HASH_SHA1 \
82 "\x30\x21\x30\x09\x06\x05\x2B\x0E\x03" \
83 "\x02\x1A\x05\x00\x04\x14"
86 * \brief RSA context structure
89 int ver
; /*!< always 0 */
90 int len
; /*!< size(N) in chars */
92 mpi N
; /*!< public modulus */
93 mpi E
; /*!< public exponent */
95 mpi D
; /*!< private exponent */
96 mpi P
; /*!< 1st prime factor */
97 mpi Q
; /*!< 2nd prime factor */
98 mpi DP
; /*!< D % (P - 1) */
99 mpi DQ
; /*!< D % (Q - 1) */
100 mpi QP
; /*!< 1 / (Q % P) */
102 mpi RN
; /*!< cached R^2 mod N */
103 mpi RP
; /*!< cached R^2 mod P */
104 mpi RQ
; /*!< cached R^2 mod Q */
106 int padding
; /*!< 1.5 or OAEP/PSS */
107 int hash_id
; /*!< hash identifier */
108 int (*f_rng
) (void *); /*!< RNG function */
109 void *p_rng
; /*!< RNG parameter */
117 * \brief Initialize an RSA context
119 * \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
122 * \param f_rng RNG function
123 * \param p_rng RNG parameter
125 * \note The hash_id parameter is actually ignored
126 * when using RSA_PKCS_V15 padding.
128 * \note Currently (xyssl-0.8), RSA_PKCS_V21 padding
131 void rsa_init(rsa_context
* ctx
,
133 int hash_id
, int (*f_rng
) (void *), void *p_rng
);
136 * \brief Generate an RSA keypair
138 * \param ctx RSA context that will hold the key
139 * \param nbits size of the public key in bits
140 * \param exponent public exponent (e.g., 65537)
142 * \note rsa_init() must be called beforehand to setup
143 * the RSA context (especially f_rng and p_rng).
145 * \return 0 if successful, or an TROPICSSL_ERR_RSA_XXX error code
147 int rsa_gen_key(rsa_context
* ctx
, int nbits
, int exponent
);
150 * \brief Check a public RSA key
152 * \param ctx RSA context to be checked
154 * \return 0 if successful, or an TROPICSSL_ERR_RSA_XXX error code
156 int rsa_check_pubkey(const rsa_context
* ctx
);
159 * \brief Check a private RSA key
161 * \param ctx RSA context to be checked
163 * \return 0 if successful, or an TROPICSSL_ERR_RSA_XXX error code
165 int rsa_check_privkey(const rsa_context
* ctx
);
168 * \brief Do an RSA public key operation
170 * \param ctx RSA context
171 * \param input input buffer
172 * \param output output buffer
174 * \return 0 if successful, or an TROPICSSL_ERR_RSA_XXX error code
176 * \note This function does NOT take care of message
177 * padding. Also, be sure to set input[0] = 0.
179 * \note The input and output buffers must be large
180 * enough (eg. 128 bytes if RSA-1024 is used).
182 int rsa_public(rsa_context
* ctx
,
183 const unsigned char *input
,
184 unsigned char *output
);
187 * \brief Do an RSA private key operation
189 * \param ctx RSA context
190 * \param input input buffer
191 * \param output output buffer
193 * \return 0 if successful, or an TROPICSSL_ERR_RSA_XXX error code
195 * \note The input and output buffers must be large
196 * enough (eg. 128 bytes if RSA-1024 is used).
198 int rsa_private(rsa_context
* ctx
,
199 const unsigned char *input
,
200 unsigned char *output
);
203 * \brief Add the message padding, then do an RSA operation
205 * \param ctx RSA context
206 * \param mode RSA_PUBLIC or RSA_PRIVATE
207 * \param ilen contains the the plaintext length
208 * \param input buffer holding the data to be encrypted
209 * \param output buffer that will hold the ciphertext
211 * \return 0 if successful, or an TROPICSSL_ERR_RSA_XXX error code
213 * \note The output buffer must be as large as the size
214 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
216 int rsa_pkcs1_encrypt(rsa_context
* ctx
,
218 const unsigned char *input
,
219 unsigned char *output
);
222 * \brief Do an RSA operation, then remove the message padding
224 * \param ctx RSA context
225 * \param mode RSA_PUBLIC or RSA_PRIVATE
226 * \param input buffer holding the encrypted data
227 * \param output buffer that will hold the plaintext
228 * \param olen will contain the plaintext length
229 * \param output_max_len maximum length of the output buffer
231 * \return 0 if successful, or an TROPICSSL_ERR_RSA_XXX error code
233 * \note The output buffer must be as large as the size
234 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
235 * an error is thrown.
237 int rsa_pkcs1_decrypt(rsa_context
* ctx
,
239 const unsigned char *input
,
240 unsigned char *output
,
244 * \brief Do a private RSA to sign a message digest
246 * \param ctx RSA context
247 * \param mode RSA_PUBLIC or RSA_PRIVATE
248 * \param hash_id RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256}
249 * \param hashlen message digest length (for RSA_RAW only)
250 * \param hash buffer holding the message digest
251 * \param sig buffer that will hold the ciphertext
253 * \return 0 if the signing operation was successful,
254 * or an TROPICSSL_ERR_RSA_XXX error code
256 * \note The "sig" buffer must be as large as the size
257 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
259 int rsa_pkcs1_sign(rsa_context
* ctx
,
263 const unsigned char *hash
,
267 * \brief Do a public RSA and check the message digest
269 * \param ctx points to an RSA public key
270 * \param mode RSA_PUBLIC or RSA_PRIVATE
271 * \param hash_id RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256}
272 * \param hashlen message digest length (for RSA_RAW only)
273 * \param hash buffer holding the message digest
274 * \param sig buffer holding the ciphertext
276 * \return 0 if the verify operation was successful,
277 * or an TROPICSSL_ERR_RSA_XXX error code
279 * \note The "sig" buffer must be as large as the size
280 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
282 int rsa_pkcs1_verify(rsa_context
* ctx
,
286 const unsigned char *hash
,
287 const unsigned char *sig
);
290 * \brief Free the components of an RSA key
292 void rsa_free(rsa_context
* ctx
);
295 * \brief Checkup routine
297 * \return 0 if successful, or 1 if the test failed
299 int rsa_self_test(int verbose
);