00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #ifndef HEADER_RSA_H
00060 #define HEADER_RSA_H
00061
00062 #include <openssl/asn1.h>
00063
00064 #ifndef OPENSSL_NO_BIO
00065 #include <openssl/bio.h>
00066 #endif
00067 #include <openssl/crypto.h>
00068 #include <openssl/ossl_typ.h>
00069 #ifndef OPENSSL_NO_DEPRECATED
00070 #include <openssl/bn.h>
00071 #endif
00072
00073 #ifdef OPENSSL_NO_RSA
00074 #error RSA is disabled.
00075 #endif
00076
00077 #ifdef __cplusplus
00078 extern "C" {
00079 #endif
00080
00081
00082
00083
00084
00085 struct rsa_meth_st
00086 {
00087 const char *name;
00088 int (*rsa_pub_enc)(int flen,const unsigned char *from,
00089 unsigned char *to,
00090 RSA *rsa,int padding);
00091 int (*rsa_pub_dec)(int flen,const unsigned char *from,
00092 unsigned char *to,
00093 RSA *rsa,int padding);
00094 int (*rsa_priv_enc)(int flen,const unsigned char *from,
00095 unsigned char *to,
00096 RSA *rsa,int padding);
00097 int (*rsa_priv_dec)(int flen,const unsigned char *from,
00098 unsigned char *to,
00099 RSA *rsa,int padding);
00100 int (*rsa_mod_exp)(BIGNUM *r0,const BIGNUM *I,RSA *rsa,BN_CTX *ctx);
00101 int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
00102 const BIGNUM *m, BN_CTX *ctx,
00103 BN_MONT_CTX *m_ctx);
00104 int (*init)(RSA *rsa);
00105 int (*finish)(RSA *rsa);
00106 int flags;
00107 char *app_data;
00108
00109
00110
00111
00112
00113
00114
00115 int (*rsa_sign)(int type,
00116 const unsigned char *m, unsigned int m_length,
00117 unsigned char *sigret, unsigned int *siglen, const RSA *rsa);
00118 int (*rsa_verify)(int dtype,
00119 const unsigned char *m, unsigned int m_length,
00120 unsigned char *sigbuf, unsigned int siglen, const RSA *rsa);
00121
00122
00123
00124
00125 int (*rsa_keygen)(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
00126 };
00127
00128 struct rsa_st
00129 {
00130
00131
00132 int pad;
00133 long version;
00134 const RSA_METHOD *meth;
00135
00136 ENGINE *engine;
00137 BIGNUM *n;
00138 BIGNUM *e;
00139 BIGNUM *d;
00140 BIGNUM *p;
00141 BIGNUM *q;
00142 BIGNUM *dmp1;
00143 BIGNUM *dmq1;
00144 BIGNUM *iqmp;
00145
00146 CRYPTO_EX_DATA ex_data;
00147 int references;
00148 int flags;
00149
00150
00151 BN_MONT_CTX *_method_mod_n;
00152 BN_MONT_CTX *_method_mod_p;
00153 BN_MONT_CTX *_method_mod_q;
00154
00155
00156
00157 char *bignum_data;
00158 BN_BLINDING *blinding;
00159 BN_BLINDING *mt_blinding;
00160 };
00161
00162 #ifndef OPENSSL_RSA_MAX_MODULUS_BITS
00163 # define OPENSSL_RSA_MAX_MODULUS_BITS 16384
00164 #endif
00165
00166 #ifndef OPENSSL_RSA_SMALL_MODULUS_BITS
00167 # define OPENSSL_RSA_SMALL_MODULUS_BITS 3072
00168 #endif
00169 #ifndef OPENSSL_RSA_MAX_PUBEXP_BITS
00170 # define OPENSSL_RSA_MAX_PUBEXP_BITS 64
00171 #endif
00172
00173 #define RSA_3 0x3L
00174 #define RSA_F4 0x10001L
00175
00176 #define RSA_METHOD_FLAG_NO_CHECK 0x0001
00177
00178 #define RSA_FLAG_CACHE_PUBLIC 0x0002
00179 #define RSA_FLAG_CACHE_PRIVATE 0x0004
00180 #define RSA_FLAG_BLINDING 0x0008
00181 #define RSA_FLAG_THREAD_SAFE 0x0010
00182
00183
00184
00185
00186
00187 #define RSA_FLAG_EXT_PKEY 0x0020
00188
00189
00190
00191 #define RSA_FLAG_SIGN_VER 0x0040
00192
00193 #define RSA_FLAG_NO_BLINDING 0x0080
00194
00195
00196
00197
00198 #define RSA_FLAG_NO_EXP_CONSTTIME 0x0100
00199
00200
00201
00202
00203
00204
00205
00206 #define RSA_PKCS1_PADDING 1
00207 #define RSA_SSLV23_PADDING 2
00208 #define RSA_NO_PADDING 3
00209 #define RSA_PKCS1_OAEP_PADDING 4
00210 #define RSA_X931_PADDING 5
00211
00212 #define RSA_PKCS1_PADDING_SIZE 11
00213
00214 #define RSA_set_app_data(s,arg) RSA_set_ex_data(s,0,arg)
00215 #define RSA_get_app_data(s) RSA_get_ex_data(s,0)
00216
00217 RSA * RSA_new(void);
00218 RSA * RSA_new_method(ENGINE *engine);
00219 int RSA_size(const RSA *);
00220
00221
00222 #ifndef OPENSSL_NO_DEPRECATED
00223 RSA * RSA_generate_key(int bits, unsigned long e,void
00224 (*callback)(int,int,void *),void *cb_arg);
00225 #endif
00226
00227
00228 int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
00229
00230 int RSA_check_key(const RSA *);
00231
00232 int RSA_public_encrypt(int flen, const unsigned char *from,
00233 unsigned char *to, RSA *rsa,int padding);
00234 int RSA_private_encrypt(int flen, const unsigned char *from,
00235 unsigned char *to, RSA *rsa,int padding);
00236 int RSA_public_decrypt(int flen, const unsigned char *from,
00237 unsigned char *to, RSA *rsa,int padding);
00238 int RSA_private_decrypt(int flen, const unsigned char *from,
00239 unsigned char *to, RSA *rsa,int padding);
00240 void RSA_free (RSA *r);
00241
00242 int RSA_up_ref(RSA *r);
00243
00244 int RSA_flags(const RSA *r);
00245
00246 void RSA_set_default_method(const RSA_METHOD *meth);
00247 const RSA_METHOD *RSA_get_default_method(void);
00248 const RSA_METHOD *RSA_get_method(const RSA *rsa);
00249 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth);
00250
00251
00252 int RSA_memory_lock(RSA *r);
00253
00254
00255 const RSA_METHOD *RSA_PKCS1_SSLeay(void);
00256
00257 const RSA_METHOD *RSA_null_method(void);
00258
00259 DECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPublicKey)
00260 DECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPrivateKey)
00261
00262 #ifndef OPENSSL_NO_FP_API
00263 int RSA_print_fp(FILE *fp, const RSA *r,int offset);
00264 #endif
00265
00266 #ifndef OPENSSL_NO_BIO
00267 int RSA_print(BIO *bp, const RSA *r,int offset);
00268 #endif
00269
00270 int i2d_RSA_NET(const RSA *a, unsigned char **pp,
00271 int (*cb)(char *buf, int len, const char *prompt, int verify),
00272 int sgckey);
00273 RSA *d2i_RSA_NET(RSA **a, const unsigned char **pp, long length,
00274 int (*cb)(char *buf, int len, const char *prompt, int verify),
00275 int sgckey);
00276
00277 int i2d_Netscape_RSA(const RSA *a, unsigned char **pp,
00278 int (*cb)(char *buf, int len, const char *prompt,
00279 int verify));
00280 RSA *d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length,
00281 int (*cb)(char *buf, int len, const char *prompt,
00282 int verify));
00283
00284
00285
00286 int RSA_sign(int type, const unsigned char *m, unsigned int m_length,
00287 unsigned char *sigret, unsigned int *siglen, RSA *rsa);
00288 int RSA_verify(int type, const unsigned char *m, unsigned int m_length,
00289 unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
00290
00291
00292
00293 int RSA_sign_ASN1_OCTET_STRING(int type,
00294 const unsigned char *m, unsigned int m_length,
00295 unsigned char *sigret, unsigned int *siglen, RSA *rsa);
00296 int RSA_verify_ASN1_OCTET_STRING(int type,
00297 const unsigned char *m, unsigned int m_length,
00298 unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
00299
00300 int RSA_blinding_on(RSA *rsa, BN_CTX *ctx);
00301 void RSA_blinding_off(RSA *rsa);
00302 BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *ctx);
00303
00304 int RSA_padding_add_PKCS1_type_1(unsigned char *to,int tlen,
00305 const unsigned char *f,int fl);
00306 int RSA_padding_check_PKCS1_type_1(unsigned char *to,int tlen,
00307 const unsigned char *f,int fl,int rsa_len);
00308 int RSA_padding_add_PKCS1_type_2(unsigned char *to,int tlen,
00309 const unsigned char *f,int fl);
00310 int RSA_padding_check_PKCS1_type_2(unsigned char *to,int tlen,
00311 const unsigned char *f,int fl,int rsa_len);
00312 int PKCS1_MGF1(unsigned char *mask, long len,
00313 const unsigned char *seed, long seedlen, const EVP_MD *dgst);
00314 int RSA_padding_add_PKCS1_OAEP(unsigned char *to,int tlen,
00315 const unsigned char *f,int fl,
00316 const unsigned char *p,int pl);
00317 int RSA_padding_check_PKCS1_OAEP(unsigned char *to,int tlen,
00318 const unsigned char *f,int fl,int rsa_len,
00319 const unsigned char *p,int pl);
00320 int RSA_padding_add_SSLv23(unsigned char *to,int tlen,
00321 const unsigned char *f,int fl);
00322 int RSA_padding_check_SSLv23(unsigned char *to,int tlen,
00323 const unsigned char *f,int fl,int rsa_len);
00324 int RSA_padding_add_none(unsigned char *to,int tlen,
00325 const unsigned char *f,int fl);
00326 int RSA_padding_check_none(unsigned char *to,int tlen,
00327 const unsigned char *f,int fl,int rsa_len);
00328 int RSA_padding_add_X931(unsigned char *to,int tlen,
00329 const unsigned char *f,int fl);
00330 int RSA_padding_check_X931(unsigned char *to,int tlen,
00331 const unsigned char *f,int fl,int rsa_len);
00332 int RSA_X931_hash_id(int nid);
00333
00334 int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
00335 const EVP_MD *Hash, const unsigned char *EM, int sLen);
00336 int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
00337 const unsigned char *mHash,
00338 const EVP_MD *Hash, int sLen);
00339
00340 int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
00341 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
00342 int RSA_set_ex_data(RSA *r,int idx,void *arg);
00343 void *RSA_get_ex_data(const RSA *r, int idx);
00344
00345 RSA *RSAPublicKey_dup(RSA *rsa);
00346 RSA *RSAPrivateKey_dup(RSA *rsa);
00347
00348
00349
00350
00351
00352 void ERR_load_RSA_strings(void);
00353
00354
00355
00356
00357 #define RSA_F_MEMORY_LOCK 100
00358 #define RSA_F_RSA_BUILTIN_KEYGEN 129
00359 #define RSA_F_RSA_CHECK_KEY 123
00360 #define RSA_F_RSA_EAY_PRIVATE_DECRYPT 101
00361 #define RSA_F_RSA_EAY_PRIVATE_ENCRYPT 102
00362 #define RSA_F_RSA_EAY_PUBLIC_DECRYPT 103
00363 #define RSA_F_RSA_EAY_PUBLIC_ENCRYPT 104
00364 #define RSA_F_RSA_GENERATE_KEY 105
00365 #define RSA_F_RSA_MEMORY_LOCK 130
00366 #define RSA_F_RSA_NEW_METHOD 106
00367 #define RSA_F_RSA_NULL 124
00368 #define RSA_F_RSA_NULL_MOD_EXP 131
00369 #define RSA_F_RSA_NULL_PRIVATE_DECRYPT 132
00370 #define RSA_F_RSA_NULL_PRIVATE_ENCRYPT 133
00371 #define RSA_F_RSA_NULL_PUBLIC_DECRYPT 134
00372 #define RSA_F_RSA_NULL_PUBLIC_ENCRYPT 135
00373 #define RSA_F_RSA_PADDING_ADD_NONE 107
00374 #define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP 121
00375 #define RSA_F_RSA_PADDING_ADD_PKCS1_PSS 125
00376 #define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1 108
00377 #define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2 109
00378 #define RSA_F_RSA_PADDING_ADD_SSLV23 110
00379 #define RSA_F_RSA_PADDING_ADD_X931 127
00380 #define RSA_F_RSA_PADDING_CHECK_NONE 111
00381 #define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP 122
00382 #define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1 112
00383 #define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2 113
00384 #define RSA_F_RSA_PADDING_CHECK_SSLV23 114
00385 #define RSA_F_RSA_PADDING_CHECK_X931 128
00386 #define RSA_F_RSA_PRINT 115
00387 #define RSA_F_RSA_PRINT_FP 116
00388 #define RSA_F_RSA_SETUP_BLINDING 136
00389 #define RSA_F_RSA_SIGN 117
00390 #define RSA_F_RSA_SIGN_ASN1_OCTET_STRING 118
00391 #define RSA_F_RSA_VERIFY 119
00392 #define RSA_F_RSA_VERIFY_ASN1_OCTET_STRING 120
00393 #define RSA_F_RSA_VERIFY_PKCS1_PSS 126
00394
00395
00396 #define RSA_R_ALGORITHM_MISMATCH 100
00397 #define RSA_R_BAD_E_VALUE 101
00398 #define RSA_R_BAD_FIXED_HEADER_DECRYPT 102
00399 #define RSA_R_BAD_PAD_BYTE_COUNT 103
00400 #define RSA_R_BAD_SIGNATURE 104
00401 #define RSA_R_BLOCK_TYPE_IS_NOT_01 106
00402 #define RSA_R_BLOCK_TYPE_IS_NOT_02 107
00403 #define RSA_R_DATA_GREATER_THAN_MOD_LEN 108
00404 #define RSA_R_DATA_TOO_LARGE 109
00405 #define RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 110
00406 #define RSA_R_DATA_TOO_LARGE_FOR_MODULUS 132
00407 #define RSA_R_DATA_TOO_SMALL 111
00408 #define RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE 122
00409 #define RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY 112
00410 #define RSA_R_DMP1_NOT_CONGRUENT_TO_D 124
00411 #define RSA_R_DMQ1_NOT_CONGRUENT_TO_D 125
00412 #define RSA_R_D_E_NOT_CONGRUENT_TO_1 123
00413 #define RSA_R_FIRST_OCTET_INVALID 133
00414 #define RSA_R_INVALID_HEADER 137
00415 #define RSA_R_INVALID_MESSAGE_LENGTH 131
00416 #define RSA_R_INVALID_PADDING 138
00417 #define RSA_R_INVALID_TRAILER 139
00418 #define RSA_R_IQMP_NOT_INVERSE_OF_Q 126
00419 #define RSA_R_KEY_SIZE_TOO_SMALL 120
00420 #define RSA_R_LAST_OCTET_INVALID 134
00421 #define RSA_R_MODULUS_TOO_LARGE 105
00422 #define RSA_R_NO_PUBLIC_EXPONENT 140
00423 #define RSA_R_NULL_BEFORE_BLOCK_MISSING 113
00424 #define RSA_R_N_DOES_NOT_EQUAL_P_Q 127
00425 #define RSA_R_OAEP_DECODING_ERROR 121
00426 #define RSA_R_PADDING_CHECK_FAILED 114
00427 #define RSA_R_P_NOT_PRIME 128
00428 #define RSA_R_Q_NOT_PRIME 129
00429 #define RSA_R_RSA_OPERATIONS_NOT_SUPPORTED 130
00430 #define RSA_R_SLEN_CHECK_FAILED 136
00431 #define RSA_R_SLEN_RECOVERY_FAILED 135
00432 #define RSA_R_SSLV3_ROLLBACK_ATTACK 115
00433 #define RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 116
00434 #define RSA_R_UNKNOWN_ALGORITHM_TYPE 117
00435 #define RSA_R_UNKNOWN_PADDING_TYPE 118
00436 #define RSA_R_WRONG_SIGNATURE_LENGTH 119
00437
00438 #ifdef __cplusplus
00439 }
00440 #endif
00441 #endif