source: azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfssl/wolfcrypt/rsa.h@ 464

Last change on this file since 464 was 464, checked in by coas-nagasima, 3 years ago

WolfSSLとAzure IoT SDKを更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr;charset=UTF-8
File size: 13.8 KB
Line 
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/*!
23 \file wolfssl/wolfcrypt/rsa.h
24*/
25
26/*
27
28DESCRIPTION
29This library provides the interface to the RSA.
30RSA keys can be used to encrypt, decrypt, sign and verify data.
31
32*/
33#ifndef WOLF_CRYPT_RSA_H
34#define WOLF_CRYPT_RSA_H
35
36#include <wolfssl/wolfcrypt/types.h>
37
38#ifndef NO_RSA
39
40
41/* RSA default exponent */
42#ifndef WC_RSA_EXPONENT
43 #define WC_RSA_EXPONENT 65537L
44#endif
45
46#if defined(WC_RSA_NONBLOCK)
47 /* enable support for fast math based non-blocking exptmod */
48 /* this splits the RSA function into many smaller operations */
49 #ifndef USE_FAST_MATH
50 #error RSA non-blocking mode only supported using fast math
51 #endif
52 #ifndef TFM_TIMING_RESISTANT
53 #error RSA non-blocking mode only supported with timing resistance enabled
54 #endif
55
56 /* RSA bounds check is not supported with RSA non-blocking mode */
57 #undef NO_RSA_BOUNDS_CHECK
58 #define NO_RSA_BOUNDS_CHECK
59#endif
60
61/* allow for user to plug in own crypto */
62#if !defined(HAVE_FIPS) && (defined(HAVE_USER_RSA) || defined(HAVE_FAST_RSA))
63 #include "user_rsa.h"
64#else
65
66#if defined(HAVE_FIPS) && \
67 (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
68/* for fips @wc_fips */
69#include <cyassl/ctaocrypt/rsa.h>
70#if defined(CYASSL_KEY_GEN) && !defined(WOLFSSL_KEY_GEN)
71 #define WOLFSSL_KEY_GEN
72#endif
73#else
74 #include <wolfssl/wolfcrypt/integer.h>
75 #include <wolfssl/wolfcrypt/random.h>
76#endif /* HAVE_FIPS && HAVE_FIPS_VERION 1 */
77#if defined(HAVE_FIPS) && \
78 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
79#include <wolfssl/wolfcrypt/fips.h>
80#endif
81
82/* header file needed for OAEP padding */
83#include <wolfssl/wolfcrypt/hash.h>
84
85#ifdef WOLFSSL_XILINX_CRYPT
86#include "xsecure_rsa.h"
87#endif
88
89#if defined(WOLFSSL_CRYPTOCELL)
90 #include <wolfssl/wolfcrypt/port/arm/cryptoCell.h>
91#endif
92
93#ifdef __cplusplus
94 extern "C" {
95#endif
96
97enum {
98 RSA_MIN_SIZE = 512,
99 RSA_MAX_SIZE = 4096,
100};
101
102/* avoid redefinition of structs */
103#if !defined(HAVE_FIPS) || \
104 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
105
106#ifdef WOLFSSL_ASYNC_CRYPT
107 #include <wolfssl/wolfcrypt/async.h>
108 #ifdef WOLFSSL_CERT_GEN
109 #include <wolfssl/wolfcrypt/asn.h>
110 #endif
111#endif
112
113enum {
114 RSA_PUBLIC = 0,
115 RSA_PRIVATE = 1,
116
117 RSA_TYPE_UNKNOWN = -1,
118 RSA_PUBLIC_ENCRYPT = 0,
119 RSA_PUBLIC_DECRYPT = 1,
120 RSA_PRIVATE_ENCRYPT = 2,
121 RSA_PRIVATE_DECRYPT = 3,
122
123 RSA_BLOCK_TYPE_1 = 1,
124 RSA_BLOCK_TYPE_2 = 2,
125
126 RSA_MIN_PAD_SZ = 11, /* separator + 0 + pad value + 8 pads */
127
128 RSA_PSS_PAD_SZ = 8,
129 RSA_PSS_SALT_MAX_SZ = 62,
130
131#ifdef OPENSSL_EXTRA
132 RSA_PKCS1_PADDING_SIZE = 11,
133 RSA_PKCS1_OAEP_PADDING_SIZE = 42, /* (2 * hashlen(SHA-1)) + 2 */
134#endif
135#ifdef WC_RSA_PSS
136 RSA_PSS_PAD_TERM = 0xBC,
137#endif
138
139 RSA_PSS_SALT_LEN_DEFAULT = -1,
140#ifdef WOLFSSL_PSS_SALT_LEN_DISCOVER
141 RSA_PSS_SALT_LEN_DISCOVER = -2,
142#endif
143
144#ifdef WOLF_CRYPTO_CB
145 RSA_MAX_ID_LEN = 32,
146 RSA_MAX_LABEL_LEN = 32,
147#endif
148};
149
150#ifdef WC_RSA_NONBLOCK
151typedef struct RsaNb {
152 exptModNb_t exptmod; /* non-block expt_mod */
153 mp_int tmp;
154} RsaNb;
155#endif
156
157/* RSA */
158struct RsaKey {
159 mp_int n, e;
160#ifndef WOLFSSL_RSA_PUBLIC_ONLY
161 mp_int d, p, q;
162#if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM)
163 mp_int dP, dQ, u;
164#endif
165#endif
166 void* heap; /* for user memory overrides */
167 byte* data; /* temp buffer for async RSA */
168 int type; /* public or private */
169 int state;
170 word32 dataLen;
171#ifdef WC_RSA_BLINDING
172 WC_RNG* rng; /* for PrivateDecrypt blinding */
173#endif
174#ifdef WOLF_CRYPTO_CB
175 int devId;
176#endif
177#ifdef WOLFSSL_ASYNC_CRYPT
178 WC_ASYNC_DEV asyncDev;
179 #ifdef WOLFSSL_CERT_GEN
180 CertSignCtx certSignCtx; /* context info for cert sign (MakeSignature) */
181 #endif
182#endif /* WOLFSSL_ASYNC_CRYPT */
183#ifdef WOLFSSL_XILINX_CRYPT
184 word32 pubExp; /* to keep values in scope they are here in struct */
185 byte* mod;
186 XSecure_Rsa xRsa;
187#endif
188#ifdef WOLF_CRYPTO_CB
189 byte id[RSA_MAX_ID_LEN];
190 int idLen;
191 char label[RSA_MAX_LABEL_LEN];
192 int labelLen;
193#endif
194#if defined(WOLFSSL_ASYNC_CRYPT) || !defined(WOLFSSL_RSA_VERIFY_INLINE)
195 byte dataIsAlloc;
196#endif
197#ifdef WC_RSA_NONBLOCK
198 RsaNb* nb;
199#endif
200#ifdef WOLFSSL_AFALG_XILINX_RSA
201 int alFd;
202 int rdFd;
203#endif
204#if defined(WOLFSSL_CRYPTOCELL)
205 rsa_context_t ctx;
206#endif
207};
208
209#ifndef WC_RSAKEY_TYPE_DEFINED
210 typedef struct RsaKey RsaKey;
211 #define WC_RSAKEY_TYPE_DEFINED
212#endif
213
214#endif /*HAVE_FIPS */
215
216WOLFSSL_API int wc_InitRsaKey(RsaKey* key, void* heap);
217WOLFSSL_API int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId);
218WOLFSSL_API int wc_FreeRsaKey(RsaKey* key);
219#ifdef WOLF_CRYPTO_CB
220WOLFSSL_API int wc_InitRsaKey_Id(RsaKey* key, unsigned char* id, int len,
221 void* heap, int devId);
222WOLFSSL_API int wc_InitRsaKey_Label(RsaKey* key, const char* label, void* heap,
223 int devId);
224#endif
225WOLFSSL_API int wc_CheckRsaKey(RsaKey* key);
226#ifdef WOLFSSL_XILINX_CRYPT
227WOLFSSL_LOCAL int wc_InitRsaHw(RsaKey* key);
228#endif /* WOLFSSL_XILINX_CRYPT */
229
230WOLFSSL_API int wc_RsaFunction(const byte* in, word32 inLen, byte* out,
231 word32* outLen, int type, RsaKey* key, WC_RNG* rng);
232
233WOLFSSL_API int wc_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out,
234 word32 outLen, RsaKey* key, WC_RNG* rng);
235WOLFSSL_API int wc_RsaPrivateDecryptInline(byte* in, word32 inLen, byte** out,
236 RsaKey* key);
237WOLFSSL_API int wc_RsaPrivateDecrypt(const byte* in, word32 inLen, byte* out,
238 word32 outLen, RsaKey* key);
239WOLFSSL_API int wc_RsaSSL_Sign(const byte* in, word32 inLen, byte* out,
240 word32 outLen, RsaKey* key, WC_RNG* rng);
241WOLFSSL_API int wc_RsaPSS_Sign(const byte* in, word32 inLen, byte* out,
242 word32 outLen, enum wc_HashType hash, int mgf,
243 RsaKey* key, WC_RNG* rng);
244WOLFSSL_API int wc_RsaPSS_Sign_ex(const byte* in, word32 inLen, byte* out,
245 word32 outLen, enum wc_HashType hash,
246 int mgf, int saltLen, RsaKey* key,
247 WC_RNG* rng);
248WOLFSSL_API int wc_RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out,
249 RsaKey* key);
250WOLFSSL_API int wc_RsaSSL_Verify(const byte* in, word32 inLen, byte* out,
251 word32 outLen, RsaKey* key);
252WOLFSSL_API int wc_RsaSSL_Verify_ex(const byte* in, word32 inLen, byte* out,
253 word32 outLen, RsaKey* key, int pad_type);
254WOLFSSL_API int wc_RsaSSL_Verify_ex2(const byte* in, word32 inLen, byte* out,
255 word32 outLen, RsaKey* key, int pad_type,
256 enum wc_HashType hash);
257WOLFSSL_API int wc_RsaPSS_VerifyInline(byte* in, word32 inLen, byte** out,
258 enum wc_HashType hash, int mgf,
259 RsaKey* key);
260WOLFSSL_API int wc_RsaPSS_VerifyInline_ex(byte* in, word32 inLen, byte** out,
261 enum wc_HashType hash, int mgf,
262 int saltLen, RsaKey* key);
263WOLFSSL_API int wc_RsaPSS_Verify(byte* in, word32 inLen, byte* out,
264 word32 outLen, enum wc_HashType hash, int mgf,
265 RsaKey* key);
266WOLFSSL_API int wc_RsaPSS_Verify_ex(byte* in, word32 inLen, byte* out,
267 word32 outLen, enum wc_HashType hash,
268 int mgf, int saltLen, RsaKey* key);
269WOLFSSL_API int wc_RsaPSS_CheckPadding(const byte* in, word32 inLen, byte* sig,
270 word32 sigSz,
271 enum wc_HashType hashType);
272WOLFSSL_API int wc_RsaPSS_CheckPadding_ex(const byte* in, word32 inLen,
273 byte* sig, word32 sigSz,
274 enum wc_HashType hashType,
275 int saltLen, int bits);
276WOLFSSL_API int wc_RsaPSS_VerifyCheckInline(byte* in, word32 inLen, byte** out,
277 const byte* digest, word32 digentLen,
278 enum wc_HashType hash, int mgf,
279 RsaKey* key);
280WOLFSSL_API int wc_RsaPSS_VerifyCheck(byte* in, word32 inLen,
281 byte* out, word32 outLen,
282 const byte* digest, word32 digestLen,
283 enum wc_HashType hash, int mgf,
284 RsaKey* key);
285
286WOLFSSL_API int wc_RsaEncryptSize(RsaKey* key);
287
288#if !defined(HAVE_FIPS) || \
289 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
290/* to avoid asn duplicate symbols @wc_fips */
291WOLFSSL_API int wc_RsaPrivateKeyDecode(const byte* input, word32* inOutIdx,
292 RsaKey*, word32);
293WOLFSSL_API int wc_RsaPublicKeyDecode(const byte* input, word32* inOutIdx,
294 RsaKey*, word32);
295WOLFSSL_API int wc_RsaPublicKeyDecodeRaw(const byte* n, word32 nSz,
296 const byte* e, word32 eSz, RsaKey* key);
297#ifdef WOLFSSL_KEY_GEN
298 WOLFSSL_API int wc_RsaKeyToDer(RsaKey*, byte* output, word32 inLen);
299#endif
300
301#ifdef WC_RSA_BLINDING
302 WOLFSSL_API int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng);
303#endif
304#ifdef WC_RSA_NONBLOCK
305 WOLFSSL_API int wc_RsaSetNonBlock(RsaKey* key, RsaNb* nb);
306 #ifdef WC_RSA_NONBLOCK_TIME
307 WOLFSSL_API int wc_RsaSetNonBlockTime(RsaKey* key, word32 maxBlockUs,
308 word32 cpuMHz);
309 #endif
310#endif
311
312/*
313 choice of padding added after fips, so not available when using fips RSA
314 */
315
316/* Mask Generation Function Identifiers */
317#define WC_MGF1NONE 0
318#define WC_MGF1SHA1 26
319#define WC_MGF1SHA224 4
320#define WC_MGF1SHA256 1
321#define WC_MGF1SHA384 2
322#define WC_MGF1SHA512 3
323
324/* Padding types */
325#define WC_RSA_PKCSV15_PAD 0
326#define WC_RSA_OAEP_PAD 1
327#define WC_RSA_PSS_PAD 2
328#define WC_RSA_NO_PAD 3
329
330WOLFSSL_API int wc_RsaPublicEncrypt_ex(const byte* in, word32 inLen, byte* out,
331 word32 outLen, RsaKey* key, WC_RNG* rng, int type,
332 enum wc_HashType hash, int mgf, byte* label, word32 lableSz);
333WOLFSSL_API int wc_RsaPrivateDecrypt_ex(const byte* in, word32 inLen,
334 byte* out, word32 outLen, RsaKey* key, int type,
335 enum wc_HashType hash, int mgf, byte* label, word32 lableSz);
336WOLFSSL_API int wc_RsaPrivateDecryptInline_ex(byte* in, word32 inLen,
337 byte** out, RsaKey* key, int type, enum wc_HashType hash,
338 int mgf, byte* label, word32 lableSz);
339#if defined(WC_RSA_DIRECT) || defined(WC_RSA_NO_PADDING)
340WOLFSSL_API int wc_RsaDirect(byte* in, word32 inLen, byte* out, word32* outSz,
341 RsaKey* key, int type, WC_RNG* rng);
342#endif
343
344#endif /* HAVE_FIPS */
345
346WOLFSSL_API int wc_RsaFlattenPublicKey(RsaKey*, byte*, word32*, byte*,
347 word32*);
348WOLFSSL_API int wc_RsaExportKey(RsaKey* key,
349 byte* e, word32* eSz,
350 byte* n, word32* nSz,
351 byte* d, word32* dSz,
352 byte* p, word32* pSz,
353 byte* q, word32* qSz);
354
355WOLFSSL_API int wc_RsaKeyToPublicDer(RsaKey*, byte* output, word32 inLen);
356
357#ifdef WOLFSSL_KEY_GEN
358 WOLFSSL_API int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng);
359 WOLFSSL_API int wc_CheckProbablePrime_ex(const byte* p, word32 pSz,
360 const byte* q, word32 qSz,
361 const byte* e, word32 eSz,
362 int nlen, int* isPrime, WC_RNG* rng);
363 WOLFSSL_API int wc_CheckProbablePrime(const byte* p, word32 pSz,
364 const byte* q, word32 qSz,
365 const byte* e, word32 eSz,
366 int nlen, int* isPrime);
367#endif
368
369WOLFSSL_LOCAL int wc_RsaPad_ex(const byte* input, word32 inputLen, byte* pkcsBlock,
370 word32 pkcsBlockLen, byte padValue, WC_RNG* rng, int padType,
371 enum wc_HashType hType, int mgf, byte* optLabel, word32 labelLen,
372 int saltLen, int bits, void* heap);
373WOLFSSL_LOCAL int wc_RsaUnPad_ex(byte* pkcsBlock, word32 pkcsBlockLen, byte** out,
374 byte padValue, int padType, enum wc_HashType hType,
375 int mgf, byte* optLabel, word32 labelLen, int saltLen,
376 int bits, void* heap);
377
378WOLFSSL_LOCAL int wc_hash2mgf(enum wc_HashType hType);
379
380#endif /* HAVE_USER_RSA */
381
382#ifdef __cplusplus
383 } /* extern "C" */
384#endif
385
386#endif /* NO_RSA */
387#endif /* WOLF_CRYPT_RSA_H */
388
Note: See TracBrowser for help on using the repository browser.