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