yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /* rsa.h |
| 2 | * |
| 3 | * Copyright (C) 2006-2021 wolfSSL Inc. |
| 4 | * |
| 5 | * This file is part of wolfSSL. |
| 6 | * |
| 7 | * wolfSSL is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * wolfSSL is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | */ |
| 21 | |
| 22 | /* rsa.h for openSSL */ |
| 23 | |
| 24 | |
| 25 | #ifndef WOLFSSL_RSA_H_ |
| 26 | #define WOLFSSL_RSA_H_ |
| 27 | |
| 28 | #include <wolfssl/openssl/bn.h> |
| 29 | #include <wolfssl/openssl/err.h> |
| 30 | #include <wolfssl/wolfcrypt/types.h> |
| 31 | |
| 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
| 36 | /* Padding types */ |
| 37 | #define RSA_PKCS1_PADDING 0 |
| 38 | #define RSA_PKCS1_OAEP_PADDING 1 |
| 39 | #define RSA_PKCS1_PSS_PADDING 2 |
| 40 | #define RSA_NO_PADDING 3 |
| 41 | |
| 42 | /* Emulate OpenSSL flags */ |
| 43 | #define RSA_METHOD_FLAG_NO_CHECK (1 << 1) |
| 44 | #define RSA_FLAG_CACHE_PUBLIC (1 << 2) |
| 45 | #define RSA_FLAG_CACHE_PRIVATE (1 << 3) |
| 46 | #define RSA_FLAG_BLINDING (1 << 4) |
| 47 | #define RSA_FLAG_THREAD_SAFE (1 << 5) |
| 48 | #define RSA_FLAG_EXT_PKEY (1 << 6) |
| 49 | #define RSA_FLAG_NO_BLINDING (1 << 7) |
| 50 | #define RSA_FLAG_NO_CONSTTIME (1 << 8) |
| 51 | |
| 52 | /* Salt length same as digest length */ |
| 53 | #define RSA_PSS_SALTLEN_DIGEST -1 |
| 54 | /* Old max salt length */ |
| 55 | #define RSA_PSS_SALTLEN_MAX_SIGN -2 |
| 56 | /* Max salt length */ |
| 57 | #define RSA_PSS_SALTLEN_MAX -3 |
| 58 | |
| 59 | typedef struct WOLFSSL_RSA_METHOD { |
| 60 | int flags; |
| 61 | char *name; |
| 62 | } WOLFSSL_RSA_METHOD; |
| 63 | |
| 64 | #ifndef WOLFSSL_RSA_TYPE_DEFINED /* guard on redeclaration */ |
| 65 | #define WOLFSSL_RSA_TYPE_DEFINED |
| 66 | typedef struct WOLFSSL_RSA { |
| 67 | #ifdef WC_RSA_BLINDING |
| 68 | WC_RNG* rng; /* for PrivateDecrypt blinding */ |
| 69 | #endif |
| 70 | WOLFSSL_BIGNUM* n; |
| 71 | WOLFSSL_BIGNUM* e; |
| 72 | WOLFSSL_BIGNUM* d; |
| 73 | WOLFSSL_BIGNUM* p; |
| 74 | WOLFSSL_BIGNUM* q; |
| 75 | WOLFSSL_BIGNUM* dmp1; /* dP */ |
| 76 | WOLFSSL_BIGNUM* dmq1; /* dQ */ |
| 77 | WOLFSSL_BIGNUM* iqmp; /* u */ |
| 78 | void* heap; |
| 79 | void* internal; /* our RSA */ |
| 80 | char inSet; /* internal set from external ? */ |
| 81 | char exSet; /* external set from internal ? */ |
| 82 | char ownRng; /* flag for if the rng should be free'd */ |
| 83 | #if defined(OPENSSL_EXTRA) |
| 84 | WOLFSSL_RSA_METHOD* meth; |
| 85 | #endif |
| 86 | #if defined(HAVE_EX_DATA) |
| 87 | WOLFSSL_CRYPTO_EX_DATA ex_data; /* external data */ |
| 88 | #endif |
| 89 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) |
| 90 | wolfSSL_Mutex refMutex; /* ref count mutex */ |
| 91 | int refCount; /* reference count */ |
| 92 | #endif |
| 93 | } WOLFSSL_RSA; |
| 94 | #endif |
| 95 | |
| 96 | typedef WOLFSSL_RSA RSA; |
| 97 | typedef WOLFSSL_RSA_METHOD RSA_METHOD; |
| 98 | |
| 99 | WOLFSSL_API WOLFSSL_RSA* wolfSSL_RSA_new(void); |
| 100 | WOLFSSL_API void wolfSSL_RSA_free(WOLFSSL_RSA*); |
| 101 | |
| 102 | WOLFSSL_API int wolfSSL_RSA_generate_key_ex(WOLFSSL_RSA*, int bits, WOLFSSL_BIGNUM*, |
| 103 | void* cb); |
| 104 | |
| 105 | WOLFSSL_API int wolfSSL_RSA_blinding_on(WOLFSSL_RSA*, WOLFSSL_BN_CTX*); |
| 106 | WOLFSSL_API int wolfSSL_RSA_public_encrypt(int len, const unsigned char* fr, |
| 107 | unsigned char* to, WOLFSSL_RSA*, int padding); |
| 108 | WOLFSSL_API int wolfSSL_RSA_private_decrypt(int len, const unsigned char* fr, |
| 109 | unsigned char* to, WOLFSSL_RSA*, int padding); |
| 110 | WOLFSSL_API int wolfSSL_RSA_private_encrypt(int len, const unsigned char* in, |
| 111 | unsigned char* out, WOLFSSL_RSA* rsa, int padding); |
| 112 | |
| 113 | WOLFSSL_API int wolfSSL_RSA_size(const WOLFSSL_RSA*); |
| 114 | WOLFSSL_API int wolfSSL_RSA_bits(const WOLFSSL_RSA*); |
| 115 | WOLFSSL_API int wolfSSL_RSA_sign(int type, const unsigned char* m, |
| 116 | unsigned int mLen, unsigned char* sigRet, |
| 117 | unsigned int* sigLen, WOLFSSL_RSA*); |
| 118 | WOLFSSL_API int wolfSSL_RSA_sign_ex(int type, const unsigned char* m, |
| 119 | unsigned int mLen, unsigned char* sigRet, |
| 120 | unsigned int* sigLen, WOLFSSL_RSA*, int); |
| 121 | WOLFSSL_API int wolfSSL_RSA_sign_generic_padding(int type, const unsigned char* m, |
| 122 | unsigned int mLen, unsigned char* sigRet, |
| 123 | unsigned int* sigLen, WOLFSSL_RSA*, int, int); |
| 124 | WOLFSSL_API int wolfSSL_RSA_verify(int type, const unsigned char* m, |
| 125 | unsigned int mLen, const unsigned char* sig, |
| 126 | unsigned int sigLen, WOLFSSL_RSA*); |
| 127 | WOLFSSL_API int wolfSSL_RSA_verify_ex(int type, const unsigned char* m, |
| 128 | unsigned int mLen, const unsigned char* sig, |
| 129 | unsigned int sigLen, WOLFSSL_RSA* rsa, |
| 130 | int padding); |
| 131 | WOLFSSL_API int wolfSSL_RSA_public_decrypt(int flen, const unsigned char* from, |
| 132 | unsigned char* to, WOLFSSL_RSA*, int padding); |
| 133 | WOLFSSL_API int wolfSSL_RSA_GenAdd(WOLFSSL_RSA*); |
| 134 | WOLFSSL_API int wolfSSL_RSA_LoadDer(WOLFSSL_RSA*, const unsigned char*, int sz); |
| 135 | WOLFSSL_API int wolfSSL_RSA_LoadDer_ex(WOLFSSL_RSA*, const unsigned char*, int sz, int opt); |
| 136 | |
| 137 | WOLFSSL_API WOLFSSL_RSA_METHOD *wolfSSL_RSA_meth_new(const char *name, int flags); |
| 138 | WOLFSSL_API void wolfSSL_RSA_meth_free(WOLFSSL_RSA_METHOD *meth); |
| 139 | WOLFSSL_API int wolfSSL_RSA_meth_set(WOLFSSL_RSA_METHOD *rsa, void* p); |
| 140 | WOLFSSL_API int wolfSSL_RSA_set_method(WOLFSSL_RSA *rsa, WOLFSSL_RSA_METHOD *meth); |
| 141 | WOLFSSL_API const WOLFSSL_RSA_METHOD* wolfSSL_RSA_get_method(const WOLFSSL_RSA *rsa); |
| 142 | WOLFSSL_API const WOLFSSL_RSA_METHOD* wolfSSL_RSA_get_default_method(void); |
| 143 | |
| 144 | WOLFSSL_API void wolfSSL_RSA_get0_key(const WOLFSSL_RSA *r, const WOLFSSL_BIGNUM **n, |
| 145 | const WOLFSSL_BIGNUM **e, const WOLFSSL_BIGNUM **d); |
| 146 | WOLFSSL_API int wolfSSL_RSA_set0_key(WOLFSSL_RSA *r, WOLFSSL_BIGNUM *n, WOLFSSL_BIGNUM *e, |
| 147 | WOLFSSL_BIGNUM *d); |
| 148 | WOLFSSL_API int wolfSSL_RSA_flags(const WOLFSSL_RSA *r); |
| 149 | WOLFSSL_API void wolfSSL_RSA_set_flags(WOLFSSL_RSA *r, int flags); |
| 150 | |
| 151 | WOLFSSL_API WOLFSSL_RSA* wolfSSL_RSAPublicKey_dup(WOLFSSL_RSA *rsa); |
| 152 | |
| 153 | WOLFSSL_API void* wolfSSL_RSA_get_ex_data(const WOLFSSL_RSA *rsa, int idx); |
| 154 | WOLFSSL_API int wolfSSL_RSA_set_ex_data(WOLFSSL_RSA *rsa, int idx, void *data); |
| 155 | #ifdef HAVE_EX_DATA_CLEANUP_HOOKS |
| 156 | WOLFSSL_API int wolfSSL_RSA_set_ex_data_with_cleanup( |
| 157 | WOLFSSL_RSA *rsa, |
| 158 | int idx, |
| 159 | void *data, |
| 160 | wolfSSL_ex_data_cleanup_routine_t cleanup_routine); |
| 161 | #endif |
| 162 | |
| 163 | #define WOLFSSL_RSA_LOAD_PRIVATE 1 |
| 164 | #define WOLFSSL_RSA_LOAD_PUBLIC 2 |
| 165 | #define WOLFSSL_RSA_F4 0x10001L |
| 166 | |
| 167 | #define RSA_new wolfSSL_RSA_new |
| 168 | #define RSA_free wolfSSL_RSA_free |
| 169 | |
| 170 | #define RSA_generate_key_ex wolfSSL_RSA_generate_key_ex |
| 171 | |
| 172 | #define RSA_blinding_on wolfSSL_RSA_blinding_on |
| 173 | #define RSA_public_encrypt wolfSSL_RSA_public_encrypt |
| 174 | #define RSA_private_decrypt wolfSSL_RSA_private_decrypt |
| 175 | #define RSA_private_encrypt wolfSSL_RSA_private_encrypt |
| 176 | |
| 177 | #define RSA_size wolfSSL_RSA_size |
| 178 | #define RSA_sign wolfSSL_RSA_sign |
| 179 | #define RSA_verify wolfSSL_RSA_verify |
| 180 | #define RSA_public_decrypt wolfSSL_RSA_public_decrypt |
| 181 | |
| 182 | #define RSA_meth_new wolfSSL_RSA_meth_new |
| 183 | #define RSA_meth_free wolfSSL_RSA_meth_free |
| 184 | #define RSA_meth_set_pub_enc wolfSSL_RSA_meth_set |
| 185 | #define RSA_meth_set_pub_dec wolfSSL_RSA_meth_set |
| 186 | #define RSA_meth_set_priv_enc wolfSSL_RSA_meth_set |
| 187 | #define RSA_meth_set_priv_dec wolfSSL_RSA_meth_set |
| 188 | #define RSA_meth_set_init wolfSSL_RSA_meth_set |
| 189 | #define RSA_meth_set_finish wolfSSL_RSA_meth_set |
| 190 | #define RSA_meth_set0_app_data wolfSSL_RSA_meth_set |
| 191 | #define RSA_get_default_method wolfSSL_RSA_get_default_method |
| 192 | #define RSA_get_method wolfSSL_RSA_get_method |
| 193 | #define RSA_set_method wolfSSL_RSA_set_method |
| 194 | #define RSA_get0_key wolfSSL_RSA_get0_key |
| 195 | #define RSA_set0_key wolfSSL_RSA_set0_key |
| 196 | #define RSA_flags wolfSSL_RSA_flags |
| 197 | #define RSA_set_flags wolfSSL_RSA_set_flags |
| 198 | |
| 199 | #define RSAPublicKey_dup wolfSSL_RSAPublicKey_dup |
| 200 | #define RSA_get_ex_data wolfSSL_RSA_get_ex_data |
| 201 | #define RSA_set_ex_data wolfSSL_RSA_set_ex_data |
| 202 | |
| 203 | #define RSA_get0_key wolfSSL_RSA_get0_key |
| 204 | |
| 205 | #define RSA_F4 WOLFSSL_RSA_F4 |
| 206 | |
| 207 | #ifdef __cplusplus |
| 208 | } /* extern "C" */ |
| 209 | #endif |
| 210 | |
| 211 | #endif /* header */ |