rsa.h
1 /* rsa.h
2  *
3  * Copyright (C) 2006-2020 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>
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, 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_sign(int type, const unsigned char* m,
115  unsigned int mLen, unsigned char* sigRet,
116  unsigned int* sigLen, WOLFSSL_RSA*);
117 WOLFSSL_API int wolfSSL_RSA_sign_ex(int type, const unsigned char* m,
118  unsigned int mLen, unsigned char* sigRet,
119  unsigned int* sigLen, WOLFSSL_RSA*, int);
120 WOLFSSL_API int wolfSSL_RSA_verify(int type, const unsigned char* m,
121  unsigned int mLen, const unsigned char* sig,
122  unsigned int sigLen, WOLFSSL_RSA*);
123 WOLFSSL_API int wolfSSL_RSA_public_decrypt(int flen, const unsigned char* from,
124  unsigned char* to, WOLFSSL_RSA*, int padding);
125 WOLFSSL_API int wolfSSL_RSA_GenAdd(WOLFSSL_RSA*);
126 WOLFSSL_API int wolfSSL_RSA_LoadDer(WOLFSSL_RSA*, const unsigned char*, int sz);
127 WOLFSSL_API int wolfSSL_RSA_LoadDer_ex(WOLFSSL_RSA*, const unsigned char*, int sz, int opt);
128 
129 WOLFSSL_API WOLFSSL_RSA_METHOD *wolfSSL_RSA_meth_new(const char *name, int flags);
130 WOLFSSL_API void wolfSSL_RSA_meth_free(WOLFSSL_RSA_METHOD *meth);
131 WOLFSSL_API int wolfSSL_RSA_meth_set(WOLFSSL_RSA_METHOD *rsa, void* p);
132 WOLFSSL_API int wolfSSL_RSA_set_method(WOLFSSL_RSA *rsa, WOLFSSL_RSA_METHOD *meth);
133 WOLFSSL_API const WOLFSSL_RSA_METHOD* wolfSSL_RSA_get_method(const WOLFSSL_RSA *rsa);
134 WOLFSSL_API const WOLFSSL_RSA_METHOD* wolfSSL_RSA_get_default_method(void);
135 
136 WOLFSSL_API void wolfSSL_RSA_get0_key(const WOLFSSL_RSA *r, const WOLFSSL_BIGNUM **n,
137  const WOLFSSL_BIGNUM **e, const WOLFSSL_BIGNUM **d);
138 WOLFSSL_API int wolfSSL_RSA_set0_key(WOLFSSL_RSA *r, WOLFSSL_BIGNUM *n, WOLFSSL_BIGNUM *e,
139  WOLFSSL_BIGNUM *d);
140 WOLFSSL_API int wolfSSL_RSA_flags(const WOLFSSL_RSA *r);
141 WOLFSSL_API void wolfSSL_RSA_set_flags(WOLFSSL_RSA *r, int flags);
142 
143 WOLFSSL_API WOLFSSL_RSA* wolfSSL_RSAPublicKey_dup(WOLFSSL_RSA *rsa);
144 
145 WOLFSSL_API void* wolfSSL_RSA_get_ex_data(const WOLFSSL_RSA *rsa, int idx);
146 WOLFSSL_API int wolfSSL_RSA_set_ex_data(WOLFSSL_RSA *rsa, int idx, void *data);
147 
148 
149 #define WOLFSSL_RSA_LOAD_PRIVATE 1
150 #define WOLFSSL_RSA_LOAD_PUBLIC 2
151 #define WOLFSSL_RSA_F4 0x10001L
152 
153 #define RSA_new wolfSSL_RSA_new
154 #define RSA_free wolfSSL_RSA_free
155 
156 #define RSA_generate_key_ex wolfSSL_RSA_generate_key_ex
157 
158 #define RSA_blinding_on wolfSSL_RSA_blinding_on
159 #define RSA_public_encrypt wolfSSL_RSA_public_encrypt
160 #define RSA_private_decrypt wolfSSL_RSA_private_decrypt
161 #define RSA_private_encrypt wolfSSL_RSA_private_encrypt
162 
163 #define RSA_size wolfSSL_RSA_size
164 #define RSA_sign wolfSSL_RSA_sign
165 #define RSA_verify wolfSSL_RSA_verify
166 #define RSA_public_decrypt wolfSSL_RSA_public_decrypt
167 
168 #define RSA_meth_new wolfSSL_RSA_meth_new
169 #define RSA_meth_free wolfSSL_RSA_meth_free
170 #define RSA_meth_set_pub_enc wolfSSL_RSA_meth_set
171 #define RSA_meth_set_pub_dec wolfSSL_RSA_meth_set
172 #define RSA_meth_set_priv_enc wolfSSL_RSA_meth_set
173 #define RSA_meth_set_priv_dec wolfSSL_RSA_meth_set
174 #define RSA_meth_set_init wolfSSL_RSA_meth_set
175 #define RSA_meth_set_finish wolfSSL_RSA_meth_set
176 #define RSA_meth_set0_app_data wolfSSL_RSA_meth_set
177 #define RSA_get_default_method wolfSSL_RSA_get_default_method
178 #define RSA_get_method wolfSSL_RSA_get_method
179 #define RSA_set_method wolfSSL_RSA_set_method
180 #define RSA_get0_key wolfSSL_RSA_get0_key
181 #define RSA_set0_key wolfSSL_RSA_set0_key
182 #define RSA_flags wolfSSL_RSA_flags
183 #define RSA_set_flags wolfSSL_RSA_set_flags
184 
185 #define RSAPublicKey_dup wolfSSL_RSAPublicKey_dup
186 #define RSA_get_ex_data wolfSSL_RSA_get_ex_data
187 #define RSA_set_ex_data wolfSSL_RSA_set_ex_data
188 
189 #define RSA_get0_key wolfSSL_RSA_get0_key
190 
191 #define RSA_F4 WOLFSSL_RSA_F4
192 
193 #ifdef __cplusplus
194  } /* extern "C" */
195 #endif
196 
197 #endif /* header */
Definition: rsa.h:66
bn.h for openssl
Definition: random.h:153