2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
63 #include <openssl/asn1.h>
65 #ifndef OPENSSL_NO_BIO
66 #include <openssl/bio.h>
68 #include <openssl/crypto.h>
69 #include <openssl/ossl_typ.h>
70 #ifndef OPENSSL_NO_DEPRECATED
71 #include <openssl/bn.h>
75 #error RSA is disabled.
78 /* If this flag is set the RSA method is FIPS compliant and can be used
79 * in FIPS mode. This is set in the validated module method. If an
80 * application sets this flag in its own methods it is its reposibility
81 * to ensure the result is compliant.
84 #define RSA_FLAG_FIPS_METHOD 0x0400
86 /* If this flag is set the operations normally disabled in FIPS mode are
87 * permitted it is then the applications responsibility to ensure that the
91 #define RSA_FLAG_NON_FIPS_ALLOW 0x0400
94 #define FIPS_RSA_SIZE_T int
101 /* Declared already in ossl_typ.h */
102 /* typedef struct rsa_st RSA; */
103 /* typedef struct rsa_meth_st RSA_METHOD; */
108 int (*rsa_pub_enc)(int flen,const unsigned char *from,
110 RSA *rsa,int padding);
111 int (*rsa_pub_dec)(int flen,const unsigned char *from,
113 RSA *rsa,int padding);
114 int (*rsa_priv_enc)(int flen,const unsigned char *from,
116 RSA *rsa,int padding);
117 int (*rsa_priv_dec)(int flen,const unsigned char *from,
119 RSA *rsa,int padding);
120 int (*rsa_mod_exp)(BIGNUM *r0,const BIGNUM *I,RSA *rsa,BN_CTX *ctx); /* Can be null */
121 int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
122 const BIGNUM *m, BN_CTX *ctx,
123 BN_MONT_CTX *m_ctx); /* Can be null */
124 int (*init)(RSA *rsa); /* called at new */
125 int (*finish)(RSA *rsa); /* called at free */
126 int flags; /* RSA_METHOD_FLAG_* things */
127 char *app_data; /* may be needed! */
128 /* New sign and verify functions: some libraries don't allow arbitrary data
129 * to be signed/verified: this allows them to be used. Note: for this to work
130 * the RSA_public_decrypt() and RSA_private_encrypt() should *NOT* be used
131 * RSA_sign(), RSA_verify() should be used instead. Note: for backwards
132 * compatibility this functionality is only enabled if the RSA_FLAG_SIGN_VER
133 * option is set in 'flags'.
135 int (*rsa_sign)(int type,
136 const unsigned char *m, unsigned int m_length,
137 unsigned char *sigret, unsigned int *siglen, const RSA *rsa);
138 int (*rsa_verify)(int dtype,
139 const unsigned char *m, unsigned int m_length,
140 unsigned char *sigbuf, unsigned int siglen, const RSA *rsa);
141 /* If this callback is NULL, the builtin software RSA key-gen will be used. This
142 * is for behavioural compatibility whilst the code gets rewired, but one day
143 * it would be nice to assume there are no such things as "builtin software"
144 * implementations. */
145 int (*rsa_keygen)(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
150 /* The first parameter is used to pickup errors where
151 * this is passed instead of aEVP_PKEY, it is set to 0 */
154 const RSA_METHOD *meth;
155 /* functional reference if 'meth' is ENGINE-provided */
165 /* be careful using this if the RSA structure is shared */
166 CRYPTO_EX_DATA ex_data;
170 /* Used to cache montgomery values */
171 BN_MONT_CTX *_method_mod_n;
172 BN_MONT_CTX *_method_mod_p;
173 BN_MONT_CTX *_method_mod_q;
175 /* all BIGNUM values are actually in the following data, if it is not
178 BN_BLINDING *blinding;
179 BN_BLINDING *mt_blinding;
182 #ifndef OPENSSL_RSA_MAX_MODULUS_BITS
183 # define OPENSSL_RSA_MAX_MODULUS_BITS 16384
186 #define OPENSSL_RSA_FIPS_MIN_MODULUS_BITS 1024
188 #ifndef OPENSSL_RSA_SMALL_MODULUS_BITS
189 # define OPENSSL_RSA_SMALL_MODULUS_BITS 3072
191 #ifndef OPENSSL_RSA_MAX_PUBEXP_BITS
192 # define OPENSSL_RSA_MAX_PUBEXP_BITS 64 /* exponent limit enforced for "large" modulus only */
196 #define RSA_F4 0x10001L
198 #define RSA_METHOD_FLAG_NO_CHECK 0x0001 /* don't check pub/private match */
200 #define RSA_FLAG_CACHE_PUBLIC 0x0002
201 #define RSA_FLAG_CACHE_PRIVATE 0x0004
202 #define RSA_FLAG_BLINDING 0x0008
203 #define RSA_FLAG_THREAD_SAFE 0x0010
204 /* This flag means the private key operations will be handled by rsa_mod_exp
205 * and that they do not depend on the private key components being present:
206 * for example a key stored in external hardware. Without this flag bn_mod_exp
207 * gets called when private key components are absent.
209 #define RSA_FLAG_EXT_PKEY 0x0020
211 /* This flag in the RSA_METHOD enables the new rsa_sign, rsa_verify functions.
213 #define RSA_FLAG_SIGN_VER 0x0040
215 #define RSA_FLAG_NO_BLINDING 0x0080 /* new with 0.9.6j and 0.9.7b; the built-in
216 * RSA implementation now uses blinding by
217 * default (ignoring RSA_FLAG_BLINDING),
218 * but other engines might not need it
220 #define RSA_FLAG_NO_CONSTTIME 0x0100 /* new with 0.9.8f; the built-in RSA
221 * implementation now uses constant time
222 * operations by default in private key operations,
223 * e.g., constant time modular exponentiation,
224 * modular inverse without leaking branches,
225 * division without leaking branches. This
226 * flag disables these constant time
227 * operations and results in faster RSA
228 * private key operations.
230 #ifndef OPENSSL_NO_DEPRECATED
231 #define RSA_FLAG_NO_EXP_CONSTTIME RSA_FLAG_NO_CONSTTIME /* deprecated name for the flag*/
232 /* new with 0.9.7h; the built-in RSA
233 * implementation now uses constant time
234 * modular exponentiation for secret exponents
235 * by default. This flag causes the
236 * faster variable sliding window method to
237 * be used for all exponents.
242 #define RSA_PKCS1_PADDING 1
243 #define RSA_SSLV23_PADDING 2
244 #define RSA_NO_PADDING 3
245 #define RSA_PKCS1_OAEP_PADDING 4
246 #define RSA_X931_PADDING 5
248 #define RSA_PKCS1_PADDING_SIZE 11
250 #define RSA_set_app_data(s,arg) RSA_set_ex_data(s,0,arg)
251 #define RSA_get_app_data(s) RSA_get_ex_data(s,0)
254 RSA * RSA_new_method(ENGINE *engine);
255 int RSA_size(const RSA *);
257 /* Deprecated version */
258 #ifndef OPENSSL_NO_DEPRECATED
259 RSA * RSA_generate_key(int bits, unsigned long e,void
260 (*callback)(int,int,void *),void *cb_arg);
261 #endif /* !defined(OPENSSL_NO_DEPRECATED) */
264 int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
265 int RSA_X931_derive_ex(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1, BIGNUM *q2,
266 const BIGNUM *Xp1, const BIGNUM *Xp2, const BIGNUM *Xp,
267 const BIGNUM *Xq1, const BIGNUM *Xq2, const BIGNUM *Xq,
268 const BIGNUM *e, BN_GENCB *cb);
269 int RSA_X931_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e, BN_GENCB *cb);
271 int RSA_check_key(const RSA *);
272 /* next 4 return -1 on error */
273 int RSA_public_encrypt(int flen, const unsigned char *from,
274 unsigned char *to, RSA *rsa,int padding);
275 int RSA_private_encrypt(int flen, const unsigned char *from,
276 unsigned char *to, RSA *rsa,int padding);
277 int RSA_public_decrypt(int flen, const unsigned char *from,
278 unsigned char *to, RSA *rsa,int padding);
279 int RSA_private_decrypt(int flen, const unsigned char *from,
280 unsigned char *to, RSA *rsa,int padding);
281 void RSA_free (RSA *r);
282 /* "up" the RSA object's reference count */
283 int RSA_up_ref(RSA *r);
285 int RSA_flags(const RSA *r);
288 RSA *FIPS_rsa_new(void);
289 void FIPS_rsa_free(RSA *r);
292 void RSA_set_default_method(const RSA_METHOD *meth);
293 const RSA_METHOD *RSA_get_default_method(void);
294 const RSA_METHOD *RSA_get_method(const RSA *rsa);
295 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth);
297 /* This function needs the memory locking malloc callbacks to be installed */
298 int RSA_memory_lock(RSA *r);
300 /* these are the actual SSLeay RSA functions */
301 const RSA_METHOD *RSA_PKCS1_SSLeay(void);
303 const RSA_METHOD *RSA_null_method(void);
305 DECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPublicKey)
306 DECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPrivateKey)
308 #ifndef OPENSSL_NO_FP_API
309 int RSA_print_fp(FILE *fp, const RSA *r,int offset);
312 #ifndef OPENSSL_NO_BIO
313 int RSA_print(BIO *bp, const RSA *r,int offset);
316 #ifndef OPENSSL_NO_RC4
317 int i2d_RSA_NET(const RSA *a, unsigned char **pp,
318 int (*cb)(char *buf, int len, const char *prompt, int verify),
320 RSA *d2i_RSA_NET(RSA **a, const unsigned char **pp, long length,
321 int (*cb)(char *buf, int len, const char *prompt, int verify),
324 int i2d_Netscape_RSA(const RSA *a, unsigned char **pp,
325 int (*cb)(char *buf, int len, const char *prompt,
327 RSA *d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length,
328 int (*cb)(char *buf, int len, const char *prompt,
332 /* The following 2 functions sign and verify a X509_SIG ASN1 object
333 * inside PKCS#1 padded RSA encryption */
334 int RSA_sign(int type, const unsigned char *m, unsigned int m_length,
335 unsigned char *sigret, unsigned int *siglen, RSA *rsa);
336 int RSA_verify(int type, const unsigned char *m, unsigned int m_length,
337 unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
339 /* The following 2 function sign and verify a ASN1_OCTET_STRING
340 * object inside PKCS#1 padded RSA encryption */
341 int RSA_sign_ASN1_OCTET_STRING(int type,
342 const unsigned char *m, unsigned int m_length,
343 unsigned char *sigret, unsigned int *siglen, RSA *rsa);
344 int RSA_verify_ASN1_OCTET_STRING(int type,
345 const unsigned char *m, unsigned int m_length,
346 unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
348 int RSA_blinding_on(RSA *rsa, BN_CTX *ctx);
349 void RSA_blinding_off(RSA *rsa);
350 BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *ctx);
352 int RSA_padding_add_PKCS1_type_1(unsigned char *to,int tlen,
353 const unsigned char *f,int fl);
354 int RSA_padding_check_PKCS1_type_1(unsigned char *to,int tlen,
355 const unsigned char *f,int fl,int rsa_len);
356 int RSA_padding_add_PKCS1_type_2(unsigned char *to,int tlen,
357 const unsigned char *f,int fl);
358 int RSA_padding_check_PKCS1_type_2(unsigned char *to,int tlen,
359 const unsigned char *f,int fl,int rsa_len);
360 int PKCS1_MGF1(unsigned char *mask, long len,
361 const unsigned char *seed, long seedlen, const EVP_MD *dgst);
362 int RSA_padding_add_PKCS1_OAEP(unsigned char *to,int tlen,
363 const unsigned char *f,int fl,
364 const unsigned char *p,int pl);
365 int RSA_padding_check_PKCS1_OAEP(unsigned char *to,int tlen,
366 const unsigned char *f,int fl,int rsa_len,
367 const unsigned char *p,int pl);
368 int RSA_padding_add_SSLv23(unsigned char *to,int tlen,
369 const unsigned char *f,int fl);
370 int RSA_padding_check_SSLv23(unsigned char *to,int tlen,
371 const unsigned char *f,int fl,int rsa_len);
372 int RSA_padding_add_none(unsigned char *to,int tlen,
373 const unsigned char *f,int fl);
374 int RSA_padding_check_none(unsigned char *to,int tlen,
375 const unsigned char *f,int fl,int rsa_len);
376 int RSA_padding_add_X931(unsigned char *to,int tlen,
377 const unsigned char *f,int fl);
378 int RSA_padding_check_X931(unsigned char *to,int tlen,
379 const unsigned char *f,int fl,int rsa_len);
380 int RSA_X931_hash_id(int nid);
382 int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
383 const EVP_MD *Hash, const unsigned char *EM, int sLen);
384 int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
385 const unsigned char *mHash,
386 const EVP_MD *Hash, int sLen);
388 int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
389 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
390 int RSA_set_ex_data(RSA *r,int idx,void *arg);
391 void *RSA_get_ex_data(const RSA *r, int idx);
393 RSA *RSAPublicKey_dup(RSA *rsa);
394 RSA *RSAPrivateKey_dup(RSA *rsa);
396 /* BEGIN ERROR CODES */
397 /* The following lines are auto generated by the script mkerr.pl. Any changes
398 * made after this point may be overwritten when the script is next run.
400 void ERR_load_RSA_strings(void);
402 /* Error codes for the RSA functions. */
404 /* Function codes. */
405 #define RSA_F_FIPS_RSA_SIGN 140
406 #define RSA_F_FIPS_RSA_VERIFY 141
407 #define RSA_F_MEMORY_LOCK 100
408 #define RSA_F_RSA_BUILTIN_KEYGEN 129
409 #define RSA_F_RSA_CHECK_KEY 123
410 #define RSA_F_RSA_EAY_PRIVATE_DECRYPT 101
411 #define RSA_F_RSA_EAY_PRIVATE_ENCRYPT 102
412 #define RSA_F_RSA_EAY_PUBLIC_DECRYPT 103
413 #define RSA_F_RSA_EAY_PUBLIC_ENCRYPT 104
414 #define RSA_F_RSA_GENERATE_KEY 105
415 #define RSA_F_RSA_MEMORY_LOCK 130
416 #define RSA_F_RSA_NEW_METHOD 106
417 #define RSA_F_RSA_NULL 124
418 #define RSA_F_RSA_NULL_MOD_EXP 131
419 #define RSA_F_RSA_NULL_PRIVATE_DECRYPT 132
420 #define RSA_F_RSA_NULL_PRIVATE_ENCRYPT 133
421 #define RSA_F_RSA_NULL_PUBLIC_DECRYPT 134
422 #define RSA_F_RSA_NULL_PUBLIC_ENCRYPT 135
423 #define RSA_F_RSA_PADDING_ADD_NONE 107
424 #define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP 121
425 #define RSA_F_RSA_PADDING_ADD_PKCS1_PSS 125
426 #define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1 108
427 #define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2 109
428 #define RSA_F_RSA_PADDING_ADD_SSLV23 110
429 #define RSA_F_RSA_PADDING_ADD_X931 127
430 #define RSA_F_RSA_PADDING_CHECK_NONE 111
431 #define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP 122
432 #define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1 112
433 #define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2 113
434 #define RSA_F_RSA_PADDING_CHECK_SSLV23 114
435 #define RSA_F_RSA_PADDING_CHECK_X931 128
436 #define RSA_F_RSA_PRINT 115
437 #define RSA_F_RSA_PRINT_FP 116
438 #define RSA_F_RSA_PRIVATE_ENCRYPT 137
439 #define RSA_F_RSA_PUBLIC_DECRYPT 138
440 #define RSA_F_RSA_SETUP_BLINDING 136
441 #define RSA_F_RSA_SET_DEFAULT_METHOD 139
442 #define RSA_F_RSA_SET_METHOD 142
443 #define RSA_F_RSA_SIGN 117
444 #define RSA_F_RSA_SIGN_ASN1_OCTET_STRING 118
445 #define RSA_F_RSA_VERIFY 119
446 #define RSA_F_RSA_VERIFY_ASN1_OCTET_STRING 120
447 #define RSA_F_RSA_VERIFY_PKCS1_PSS 126
450 #define RSA_R_ALGORITHM_MISMATCH 100
451 #define RSA_R_BAD_E_VALUE 101
452 #define RSA_R_BAD_FIXED_HEADER_DECRYPT 102
453 #define RSA_R_BAD_PAD_BYTE_COUNT 103
454 #define RSA_R_BAD_SIGNATURE 104
455 #define RSA_R_BLOCK_TYPE_IS_NOT_01 106
456 #define RSA_R_BLOCK_TYPE_IS_NOT_02 107
457 #define RSA_R_DATA_GREATER_THAN_MOD_LEN 108
458 #define RSA_R_DATA_TOO_LARGE 109
459 #define RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 110
460 #define RSA_R_DATA_TOO_LARGE_FOR_MODULUS 132
461 #define RSA_R_DATA_TOO_SMALL 111
462 #define RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE 122
463 #define RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY 112
464 #define RSA_R_DMP1_NOT_CONGRUENT_TO_D 124
465 #define RSA_R_DMQ1_NOT_CONGRUENT_TO_D 125
466 #define RSA_R_D_E_NOT_CONGRUENT_TO_1 123
467 #define RSA_R_FIRST_OCTET_INVALID 133
468 #define RSA_R_INVALID_HEADER 137
469 #define RSA_R_INVALID_MESSAGE_LENGTH 131
470 #define RSA_R_INVALID_PADDING 138
471 #define RSA_R_INVALID_TRAILER 139
472 #define RSA_R_IQMP_NOT_INVERSE_OF_Q 126
473 #define RSA_R_KEY_SIZE_TOO_SMALL 120
474 #define RSA_R_LAST_OCTET_INVALID 134
475 #define RSA_R_MODULUS_TOO_LARGE 105
476 #define RSA_R_NON_FIPS_METHOD 141
477 #define RSA_R_NO_PUBLIC_EXPONENT 140
478 #define RSA_R_NULL_BEFORE_BLOCK_MISSING 113
479 #define RSA_R_N_DOES_NOT_EQUAL_P_Q 127
480 #define RSA_R_OAEP_DECODING_ERROR 121
481 #define RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE 142
482 #define RSA_R_PADDING_CHECK_FAILED 114
483 #define RSA_R_P_NOT_PRIME 128
484 #define RSA_R_Q_NOT_PRIME 129
485 #define RSA_R_RSA_OPERATIONS_NOT_SUPPORTED 130
486 #define RSA_R_SLEN_CHECK_FAILED 136
487 #define RSA_R_SLEN_RECOVERY_FAILED 135
488 #define RSA_R_SSLV3_ROLLBACK_ATTACK 115
489 #define RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 116
490 #define RSA_R_UNKNOWN_ALGORITHM_TYPE 117
491 #define RSA_R_UNKNOWN_PADDING_TYPE 118
492 #define RSA_R_WRONG_SIGNATURE_LENGTH 119