gnunet-android

GNUnet for Android
Log | Files | Refs | README

rsa.h (12927B)


      1 /* rsa.h
      2  *
      3  * Copyright (C) 2006-2025 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 3 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/openssl/compat_types.h>
     31 #include <wolfssl/wolfcrypt/types.h>
     32 #include <wolfssl/wolfcrypt/rsa.h>
     33 
     34 #ifdef __cplusplus
     35     extern "C" {
     36 #endif
     37 
     38 #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
     39 
     40 /* Padding types */
     41 #define WC_RSA_PKCS1_PADDING      0
     42 #define WC_RSA_PKCS1_OAEP_PADDING 1
     43 #define WC_RSA_PKCS1_PSS_PADDING  2
     44 
     45 /* RSA PSS Salt special cases */
     46 /* Salt length same as digest length */
     47 #define WC_RSA_PSS_SALTLEN_DIGEST   (-1)
     48 /* Old max salt length */
     49 #define WC_RSA_PSS_SALTLEN_MAX_SIGN (-2)
     50 /* Verification only value to indicate to discover salt length. */
     51 #define WC_RSA_PSS_SALTLEN_AUTO     (-2)
     52 /* Max salt length */
     53 #define WC_RSA_PSS_SALTLEN_MAX      (-3)
     54 
     55 
     56 #ifndef OPENSSL_COEXIST
     57 
     58 /* Padding types */
     59 #define RSA_PKCS1_PADDING       WC_RSA_PKCS1_PADDING
     60 #define RSA_PKCS1_OAEP_PADDING  WC_RSA_PKCS1_OAEP_PADDING
     61 #define RSA_PKCS1_PSS_PADDING   WC_RSA_PKCS1_PSS_PADDING
     62 #define RSA_NO_PADDING          WC_RSA_NO_PAD
     63 
     64 /* Emulate OpenSSL flags */
     65 #define RSA_METHOD_FLAG_NO_CHECK        (1 << 1)
     66 #define RSA_FLAG_CACHE_PUBLIC           (1 << 2)
     67 #define RSA_FLAG_CACHE_PRIVATE          (1 << 3)
     68 #define RSA_FLAG_BLINDING               (1 << 4)
     69 #define RSA_FLAG_THREAD_SAFE            (1 << 5)
     70 #define RSA_FLAG_EXT_PKEY               (1 << 6)
     71 #define RSA_FLAG_NO_BLINDING            (1 << 7)
     72 #define RSA_FLAG_NO_CONSTTIME           (1 << 8)
     73 
     74 #define RSA_PSS_SALTLEN_DIGEST   WC_RSA_PSS_SALTLEN_DIGEST
     75 #define RSA_PSS_SALTLEN_MAX_SIGN WC_RSA_PSS_SALTLEN_MAX_SIGN
     76 #define RSA_PSS_SALTLEN_AUTO     WC_RSA_PSS_SALTLEN_AUTO
     77 #define RSA_PSS_SALTLEN_MAX      WC_RSA_PSS_SALTLEN_MAX
     78 #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
     79 
     80 #endif /* !OPENSSL_COEXIST */
     81 
     82 typedef struct WOLFSSL_RSA_METHOD {
     83     /* Flags of RSA key implementation. */
     84     int flags;
     85     /* Name of RSA key implementation. */
     86     char *name;
     87     /* RSA method dynamically allocated. */
     88     WC_BITFIELD dynamic:1;
     89 } WOLFSSL_RSA_METHOD;
     90 
     91 #ifndef WOLFSSL_RSA_TYPE_DEFINED /* guard on redeclaration */
     92 #define WOLFSSL_RSA_TYPE_DEFINED
     93 /* RSA key compatible with OpenSSL. */
     94 typedef struct WOLFSSL_RSA {
     95     WOLFSSL_BIGNUM* n;              /* Modulus. */
     96     WOLFSSL_BIGNUM* e;              /* Public exponent. */
     97     WOLFSSL_BIGNUM* d;              /* Private exponent. */
     98     WOLFSSL_BIGNUM* p;              /* First prime. */
     99     WOLFSSL_BIGNUM* q;              /* Second prime. */
    100     WOLFSSL_BIGNUM* dmp1;           /* dP = d mod (p - 1) */
    101     WOLFSSL_BIGNUM* dmq1;           /* dQ = d mod (q - 1) */
    102     WOLFSSL_BIGNUM* iqmp;           /* u = (1 / q) mod p */
    103     void* heap;                     /* Heap used for memory allocations. */
    104     void* internal;                 /* wolfCrypt RSA key. */
    105 #if defined(OPENSSL_EXTRA)
    106     const WOLFSSL_RSA_METHOD* meth; /* RSA method. */
    107 #endif
    108 #ifdef HAVE_EX_DATA
    109     WOLFSSL_CRYPTO_EX_DATA ex_data;  /* external data */
    110 #endif
    111     wolfSSL_Ref ref;                 /* Reference count information. */
    112     word16 pkcs8HeaderSz;            /* Size of PKCS#8 header from decode. */
    113     int flags;                       /* Flags of implementation. */
    114 
    115     /* bits */
    116     WC_BITFIELD inSet:1;             /* Internal set from external. */
    117     WC_BITFIELD exSet:1;             /* External set from internal. */
    118     WC_BITFIELD ownRng:1;            /* Rng needs to be free'd. */
    119 } WOLFSSL_RSA;
    120 #endif
    121 
    122 #if !defined(OPENSSL_COEXIST) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
    123 typedef WOLFSSL_RSA                   RSA;
    124 typedef WOLFSSL_RSA_METHOD            RSA_METHOD;
    125 #endif /* !OPENSSL_COEXIST && (OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL) */
    126 
    127 WOLFSSL_API WOLFSSL_RSA* wolfSSL_RSA_new_ex(void* heap, int devId);
    128 WOLFSSL_API WOLFSSL_RSA* wolfSSL_RSA_new(void);
    129 WOLFSSL_API void        wolfSSL_RSA_free(WOLFSSL_RSA* rsa);
    130 
    131 WOLFSSL_API int wolfSSL_RSA_generate_key_ex(WOLFSSL_RSA* rsa, int bits,
    132                                             WOLFSSL_BIGNUM* bn, void* cb);
    133 
    134 WOLFSSL_API int wolfSSL_RSA_blinding_on(WOLFSSL_RSA* rsa, WOLFSSL_BN_CTX* bn);
    135 WOLFSSL_API int wolfSSL_RSA_check_key(const WOLFSSL_RSA* rsa);
    136 WOLFSSL_API int wolfSSL_RSA_public_encrypt(int len, const unsigned char* fr,
    137                                        unsigned char* to, WOLFSSL_RSA* rsa,
    138                                        int padding);
    139 WOLFSSL_API int wolfSSL_RSA_private_decrypt(int len, const unsigned char* fr,
    140                                        unsigned char* to, WOLFSSL_RSA* rsa,
    141                                        int padding);
    142 WOLFSSL_API int wolfSSL_RSA_private_encrypt(int len, const unsigned char* in,
    143                             unsigned char* out, WOLFSSL_RSA* rsa, int padding);
    144 
    145 WOLFSSL_API int wolfSSL_RSA_size(const WOLFSSL_RSA* rsa);
    146 WOLFSSL_API int wolfSSL_RSA_bits(const WOLFSSL_RSA* rsa);
    147 WOLFSSL_API int wolfSSL_RSA_sign(int type, const unsigned char* m,
    148                                unsigned int mLen, unsigned char* sigRet,
    149                                unsigned int* sigLen, WOLFSSL_RSA* rsa);
    150 WOLFSSL_API int wolfSSL_RSA_sign_ex(int hashAlg,
    151                                const unsigned char* hash, unsigned int hLen,
    152                                unsigned char* sigRet, unsigned int* sigLen,
    153                                WOLFSSL_RSA* rsa, int flag);
    154 WOLFSSL_API int wolfSSL_RSA_sign_generic_padding(int hashAlg,
    155                                const unsigned char* hash, unsigned int hLen,
    156                                unsigned char* sigRet, unsigned int* sigLen,
    157                                WOLFSSL_RSA* rsa, int flag, int padding);
    158 
    159 WOLFSSL_LOCAL int wolfSSL_RSA_sign_mgf(int hashAlg,
    160                                 const unsigned char* hash, unsigned int hLen,
    161                                 unsigned char* sigRet, unsigned int* sigLen,
    162                                 WOLFSSL_RSA* rsa, int flag, int padding,
    163                                 int mgf1Hash, int saltLen);
    164 
    165 WOLFSSL_API int wolfSSL_RSA_verify(int hashAlg,
    166                                const unsigned char* hash, unsigned int hLen,
    167                                const unsigned char* sig, unsigned int sigLen,
    168                                WOLFSSL_RSA* rsa);
    169 WOLFSSL_API int wolfSSL_RSA_verify_ex(int hashAlg,
    170                                const unsigned char* hash, unsigned int hLen,
    171                                const unsigned char* sig, unsigned int sigLen,
    172                                WOLFSSL_RSA* rsa, int padding);
    173 WOLFSSL_LOCAL int wolfSSL_RSA_verify_mgf(int hashAlg,
    174                                 const unsigned char* hash, unsigned int hLen,
    175                                 const unsigned char* sig, unsigned int sigLen,
    176                                 WOLFSSL_RSA* rsa, int padding,
    177                                 int mgf1Hash, int saltLen);
    178 WOLFSSL_API int wolfSSL_RSA_public_decrypt(int flen, const unsigned char* from,
    179                                unsigned char* to, WOLFSSL_RSA* rsa, int padding);
    180 WOLFSSL_API int wolfSSL_RSA_GenAdd(WOLFSSL_RSA* rsa);
    181 WOLFSSL_API int wolfSSL_RSA_LoadDer(WOLFSSL_RSA* rsa,
    182                                const unsigned char* derBuf, int derSz);
    183 WOLFSSL_API int wolfSSL_RSA_LoadDer_ex(WOLFSSL_RSA* rsa,
    184                                const unsigned char* derBuf, int derSz, int opt);
    185 
    186 WOLFSSL_API WOLFSSL_RSA_METHOD *wolfSSL_RSA_meth_new(const char *name, int flags);
    187 WOLFSSL_API void wolfSSL_RSA_meth_free(WOLFSSL_RSA_METHOD *meth);
    188 WOLFSSL_API int wolfSSL_RSA_meth_set(WOLFSSL_RSA_METHOD *rsa, void* p);
    189 WOLFSSL_API int wolfSSL_RSA_set_method(WOLFSSL_RSA *rsa, WOLFSSL_RSA_METHOD *meth);
    190 WOLFSSL_API const WOLFSSL_RSA_METHOD* wolfSSL_RSA_get_method(const WOLFSSL_RSA *rsa);
    191 WOLFSSL_API const WOLFSSL_RSA_METHOD* wolfSSL_RSA_get_default_method(void);
    192 
    193 WOLFSSL_API void wolfSSL_RSA_get0_crt_params(const WOLFSSL_RSA *r,
    194                                              const WOLFSSL_BIGNUM **dmp1,
    195                                              const WOLFSSL_BIGNUM **dmq1,
    196                                              const WOLFSSL_BIGNUM **iqmp);
    197 WOLFSSL_API int wolfSSL_RSA_set0_crt_params(WOLFSSL_RSA *r, WOLFSSL_BIGNUM *dmp1,
    198                                             WOLFSSL_BIGNUM *dmq1, WOLFSSL_BIGNUM *iqmp);
    199 WOLFSSL_API void wolfSSL_RSA_get0_factors(const WOLFSSL_RSA *r, const WOLFSSL_BIGNUM **p,
    200                                           const WOLFSSL_BIGNUM **q);
    201 WOLFSSL_API int wolfSSL_RSA_set0_factors(WOLFSSL_RSA *r, WOLFSSL_BIGNUM *p, WOLFSSL_BIGNUM *q);
    202 WOLFSSL_API void wolfSSL_RSA_get0_key(const WOLFSSL_RSA *r, const WOLFSSL_BIGNUM **n,
    203                                       const WOLFSSL_BIGNUM **e, const WOLFSSL_BIGNUM **d);
    204 WOLFSSL_API int wolfSSL_RSA_set0_key(WOLFSSL_RSA *r, WOLFSSL_BIGNUM *n, WOLFSSL_BIGNUM *e,
    205                                      WOLFSSL_BIGNUM *d);
    206 WOLFSSL_API int wolfSSL_RSA_flags(const WOLFSSL_RSA *r);
    207 WOLFSSL_API void wolfSSL_RSA_set_flags(WOLFSSL_RSA *r, int flags);
    208 WOLFSSL_API void wolfSSL_RSA_clear_flags(WOLFSSL_RSA *r, int flags);
    209 WOLFSSL_API int wolfSSL_RSA_test_flags(const WOLFSSL_RSA *r, int flags);
    210 
    211 WOLFSSL_API WOLFSSL_RSA* wolfSSL_RSAPublicKey_dup(WOLFSSL_RSA *rsa);
    212 
    213 WOLFSSL_API void* wolfSSL_RSA_get_ex_data(const WOLFSSL_RSA *rsa, int idx);
    214 WOLFSSL_API int wolfSSL_RSA_set_ex_data(WOLFSSL_RSA *rsa, int idx, void *data);
    215 #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
    216 WOLFSSL_API int wolfSSL_RSA_set_ex_data_with_cleanup(
    217     WOLFSSL_RSA *rsa,
    218     int idx,
    219     void *data,
    220     wolfSSL_ex_data_cleanup_routine_t cleanup_routine);
    221 #endif
    222 
    223 #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
    224 
    225 #define WOLFSSL_RSA_LOAD_PRIVATE 1
    226 #define WOLFSSL_RSA_LOAD_PUBLIC  2
    227 #define WOLFSSL_RSA_F4           0x10001L
    228 
    229 #ifndef OPENSSL_COEXIST
    230 
    231 #define OPENSSL_RSA_MAX_MODULUS_BITS RSA_MAX_SIZE
    232 
    233 #define RSA_new  wolfSSL_RSA_new
    234 #define RSA_free wolfSSL_RSA_free
    235 
    236 #define RSA_generate_key_ex wolfSSL_RSA_generate_key_ex
    237 
    238 #define RSA_blinding_on     wolfSSL_RSA_blinding_on
    239 #define RSA_check_key       wolfSSL_RSA_check_key
    240 #define RSA_public_encrypt  wolfSSL_RSA_public_encrypt
    241 #define RSA_private_decrypt wolfSSL_RSA_private_decrypt
    242 #define RSA_private_encrypt wolfSSL_RSA_private_encrypt
    243 
    244 #define RSA_size           wolfSSL_RSA_size
    245 #define RSA_sign           wolfSSL_RSA_sign
    246 #define RSA_verify         wolfSSL_RSA_verify
    247 #define RSA_public_decrypt wolfSSL_RSA_public_decrypt
    248 
    249 #define RSA_meth_new            wolfSSL_RSA_meth_new
    250 #define RSA_meth_free           wolfSSL_RSA_meth_free
    251 #define RSA_meth_set_pub_enc    wolfSSL_RSA_meth_set
    252 #define RSA_meth_set_pub_dec    wolfSSL_RSA_meth_set
    253 #define RSA_meth_set_priv_enc   wolfSSL_RSA_meth_set
    254 #define RSA_meth_set_priv_dec   wolfSSL_RSA_meth_set
    255 #define RSA_meth_set_init       wolfSSL_RSA_meth_set
    256 #define RSA_meth_set_finish     wolfSSL_RSA_meth_set
    257 #define RSA_meth_set0_app_data  wolfSSL_RSA_meth_set
    258 #define RSA_get_default_method  wolfSSL_RSA_get_default_method
    259 #define RSA_get_method          wolfSSL_RSA_get_method
    260 #define RSA_set_method          wolfSSL_RSA_set_method
    261 #define RSA_get0_crt_params     wolfSSL_RSA_get0_crt_params
    262 #define RSA_set0_crt_params     wolfSSL_RSA_set0_crt_params
    263 #define RSA_get0_factors        wolfSSL_RSA_get0_factors
    264 #define RSA_set0_factors        wolfSSL_RSA_set0_factors
    265 #define RSA_get0_key            wolfSSL_RSA_get0_key
    266 #define RSA_set0_key            wolfSSL_RSA_set0_key
    267 #define RSA_flags               wolfSSL_RSA_flags
    268 #define RSA_set_flags           wolfSSL_RSA_set_flags
    269 #define RSA_clear_flags         wolfSSL_RSA_clear_flags
    270 #define RSA_test_flags          wolfSSL_RSA_test_flags
    271 
    272 #define RSAPublicKey_dup        wolfSSL_RSAPublicKey_dup
    273 #define RSA_get_ex_data        wolfSSL_RSA_get_ex_data
    274 #define RSA_set_ex_data        wolfSSL_RSA_set_ex_data
    275 
    276 #define RSA_get0_key       wolfSSL_RSA_get0_key
    277 
    278 #define RSA_F4             WOLFSSL_RSA_F4
    279 
    280 #define OPENSSL_RSA_MAX_MODULUS_BITS RSA_MAX_SIZE
    281 #define OPENSSL_RSA_MAX_PUBEXP_BITS  RSA_MAX_SIZE
    282 
    283 #endif /* !OPENSSL_COEXIST */
    284 
    285 #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
    286 
    287 #ifdef __cplusplus
    288     }  /* extern "C" */
    289 #endif
    290 
    291 #endif /* header */