/src/wolfssl/wolfcrypt/src/rsa.c
Line | Count | Source |
1 | | /* rsa.c |
2 | | * |
3 | | * Copyright (C) 2006-2026 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 | | /* |
23 | | |
24 | | DESCRIPTION |
25 | | This library provides the interface to the RSA. |
26 | | RSA keys can be used to encrypt, decrypt, sign and verify data. |
27 | | |
28 | | */ |
29 | | |
30 | | #define WC_FIPS_LL_CRYPTO |
31 | | #define _WC_BUILDING_RSA_C |
32 | | |
33 | | #include <wolfssl/wolfcrypt/libwolfssl_sources.h> |
34 | | |
35 | | #ifndef NO_RSA |
36 | | |
37 | | #if FIPS_VERSION3_GE(2,0,0) |
38 | | #ifdef USE_WINDOWS_API |
39 | | #pragma code_seg(".fipsA$j") |
40 | | #pragma const_seg(".fipsB$j") |
41 | | #endif |
42 | | #endif |
43 | | |
44 | | #include <wolfssl/wolfcrypt/rsa.h> |
45 | | #include <wolfssl/wolfcrypt/logging.h> |
46 | | |
47 | | #ifdef WOLFSSL_AFALG_XILINX_RSA |
48 | | #include <wolfssl/wolfcrypt/port/af_alg/wc_afalg.h> |
49 | | #endif |
50 | | #if defined(WOLFSSL_XILINX_CRYPT_VERSAL) |
51 | | #include <xsecure_rsaclient.h> |
52 | | #endif |
53 | | #if defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA) |
54 | | #include <wolfssl/wolfcrypt/port/nxp/se050_port.h> |
55 | | #endif |
56 | | #ifdef WOLFSSL_HAVE_SP_RSA |
57 | | #include <wolfssl/wolfcrypt/sp.h> |
58 | | #endif |
59 | | #if defined(WOLFSSL_NXP_CASPER_RSA_PUB_EXPTMOD) |
60 | | #include <wolfssl/wolfcrypt/port/nxp/casper_port.h> |
61 | | #endif |
62 | | |
63 | | /* |
64 | | * RSA Build Options: |
65 | | * |
66 | | * Core: |
67 | | * NO_RSA: Disable RSA support entirely default: off |
68 | | * WOLFSSL_RSA_PUBLIC_ONLY: Only include RSA public key operations default: off |
69 | | * WOLFSSL_RSA_VERIFY_ONLY: Only include RSA verify operation default: off |
70 | | * WOLFSSL_RSA_VERIFY_INLINE: RSA verify inline (no output copy) default: off |
71 | | * WC_RSA_DIRECT: Enable direct RSA encrypt/decrypt API default: off |
72 | | * WC_RSA_NO_PADDING: Enable no-padding RSA mode default: off |
73 | | * WOLFSSL_RSA_KEY_CHECK: Enable RSA key pair consistency check default: off |
74 | | * WOLFSSL_RSA_CHECK_D_ON_DECRYPT: Validate private exponent d default: off |
75 | | * before each decrypt operation |
76 | | * WOLFSSL_RSA_DECRYPT_TO_0_LEN: Allow RSA decrypt result of 0 default: off |
77 | | * length (empty plaintext) |
78 | | * NO_RSA_BOUNDS_CHECK: Disable RSA bounds checking on input default: off |
79 | | * SHOW_GEN: Show key generation progress dots default: off |
80 | | * |
81 | | * Padding: |
82 | | * WC_RSA_PSS: Enable RSA-PSS signature support default: off |
83 | | * WC_NO_RSA_OAEP: Disable RSA OAEP padding default: off |
84 | | * WOLFSSL_PSS_LONG_SALT: Allow PSS salt longer than hash length default: off |
85 | | * WOLFSSL_PSS_SALT_LEN_DISCOVER: Auto-discover PSS salt length default: off |
86 | | * during verification |
87 | | * |
88 | | * Performance: |
89 | | * WC_RSA_BLINDING: Use blinding with private key ops default: on |
90 | | * Note: ~20% slower, protects against |
91 | | * timing side-channels |
92 | | * RSA_LOW_MEM: Non-CRT private ops, less memory default: off |
93 | | * WC_RSA_NONBLOCK: Non-blocking RSA operations default: off |
94 | | * WC_RSA_NONBLOCK_TIME: Time-based non-blocking RSA default: off |
95 | | * WOLFSSL_MP_INVMOD_CONSTANT_TIME: Constant-time modular inverse default: off |
96 | | * WC_RSA_NO_FERMAT_CHECK: Skip Fermat factorization check on default: off |
97 | | * key generation (p and q closeness) |
98 | | * |
99 | | * Key Generation: |
100 | | * WOLFSSL_KEY_GEN: Enable RSA private key generation default: off |
101 | | * FP_MAX_BITS: Max key bits with USE_FAST_MATH default: 4096 |
102 | | * Value is key size * 2 (e.g. RSA 3072 = 6144) |
103 | | * |
104 | | * SP Math: |
105 | | * WOLFSSL_HAVE_SP_RSA: Use SP math for RSA operations default: off |
106 | | * WOLFSSL_SP_MATH: Use SP math only (no multi-precision) default: off |
107 | | * WOLFSSL_SP_MATH_ALL: SP math for all key sizes default: off |
108 | | * WOLFSSL_SP_NO_2048: Disable SP RSA 2048-bit support default: off |
109 | | * WOLFSSL_SP_NO_3072: Disable SP RSA 3072-bit support default: off |
110 | | * WOLFSSL_SP_4096: Enable SP RSA 4096-bit support default: off |
111 | | * WOLFSSL_SP_ASM: Use SP assembly optimizations default: off |
112 | | * |
113 | | * Hardware Acceleration (RSA-specific): |
114 | | * WC_ASYNC_ENABLE_RSA: Enable async RSA operations default: off |
115 | | * WOLFSSL_KCAPI_RSA: Linux kernel crypto API for RSA default: off |
116 | | * WOLFSSL_AFALG_XILINX_RSA: AF_ALG Xilinx RSA acceleration default: off |
117 | | * WOLFSSL_SE050_NO_RSA: Disable SE050 RSA default: off |
118 | | * WOLFSSL_XILINX_CRYPT: Xilinx crypto RSA acceleration default: off |
119 | | */ |
120 | | |
121 | | |
122 | | #include <wolfssl/wolfcrypt/random.h> |
123 | | #ifdef WOLF_CRYPTO_CB |
124 | | #include <wolfssl/wolfcrypt/cryptocb.h> |
125 | | #endif |
126 | | #ifdef NO_INLINE |
127 | | #include <wolfssl/wolfcrypt/misc.h> |
128 | | #else |
129 | | #define WOLFSSL_MISC_INCLUDED |
130 | | #include <wolfcrypt/src/misc.c> |
131 | | #endif |
132 | | |
133 | | #if FIPS_VERSION3_GE(6,0,0) |
134 | | const unsigned int wolfCrypt_FIPS_rsa_ro_sanity[2] = |
135 | | { 0x1a2b3c4d, 0x00000012 }; |
136 | | int wolfCrypt_FIPS_RSA_sanity(void) |
137 | | { |
138 | | return 0; |
139 | | } |
140 | | #endif |
141 | | |
142 | | enum { |
143 | | RSA_STATE_NONE = 0, |
144 | | |
145 | | RSA_STATE_ENCRYPT_PAD, |
146 | | RSA_STATE_ENCRYPT_EXPTMOD, |
147 | | RSA_STATE_ENCRYPT_RES, |
148 | | |
149 | | RSA_STATE_DECRYPT_EXPTMOD, |
150 | | RSA_STATE_DECRYPT_UNPAD, |
151 | | RSA_STATE_DECRYPT_RES |
152 | | }; |
153 | | |
154 | | static void wc_RsaCleanup(RsaKey* key) |
155 | 0 | { |
156 | 0 | #if !defined(WOLFSSL_NO_MALLOC) && (defined(WOLFSSL_ASYNC_CRYPT) || \ |
157 | 0 | (!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE))) |
158 | 0 | if (key != NULL) { |
159 | 0 | #ifndef WOLFSSL_RSA_PUBLIC_ONLY |
160 | | #if FIPS_VERSION3_GE(7,0,0) |
161 | | /* Erase the recovered plaintext on the way out, success or failure. |
162 | | * SP 800-56B Rev2 sec 7.2.2.4. Only a buffer we allocated: a |
163 | | * caller-supplied one is the answer itself. No key->type test: |
164 | | * it never holds RSA_PRIVATE_DECRYPT/ENCRYPT, which belong to the |
165 | | * operation-type half of that enum (rsa.h:176-183), so the old test |
166 | | * was always false and the buffer was freed unwiped. */ |
167 | | if (key->dataIsAlloc && key->data != NULL && key->dataLen > 0) { |
168 | | ForceZero(key->data, key->dataLen); |
169 | | } |
170 | | #else |
171 | | /* if private operation zero temp buffer */ |
172 | 0 | if ((key->data != NULL && key->dataLen > 0) && |
173 | 0 | (key->type == RSA_PRIVATE_DECRYPT || |
174 | 0 | key->type == RSA_PRIVATE_ENCRYPT)) { |
175 | 0 | ForceZero(key->data, key->dataLen); |
176 | 0 | } |
177 | 0 | #endif |
178 | 0 | #endif |
179 | | /* make sure any allocated memory is free'd */ |
180 | 0 | if (key->dataIsAlloc) { |
181 | 0 | XFREE(key->data, key->heap, DYNAMIC_TYPE_WOLF_BIGINT); |
182 | 0 | key->dataIsAlloc = 0; |
183 | 0 | } |
184 | |
|
185 | 0 | key->data = NULL; |
186 | 0 | key->dataLen = 0; |
187 | 0 | } |
188 | | #else |
189 | | (void)key; |
190 | | #endif |
191 | 0 | } |
192 | | |
193 | | #ifndef WC_NO_CONSTRUCTORS |
194 | | |
195 | 0 | #define RSA_NEW_INIT_PLAIN 0 |
196 | | #ifdef WOLF_PRIVATE_KEY_ID |
197 | | #define RSA_NEW_INIT_ID 1 |
198 | | #define RSA_NEW_INIT_LABEL 2 |
199 | | #endif |
200 | | |
201 | | static RsaKey* _NewRsaKey_common(void* heap, int devId, int *result_code, |
202 | | int rsaInitType, unsigned char* id, |
203 | | int idLen, const char* label) |
204 | 0 | { |
205 | 0 | int ret; |
206 | 0 | RsaKey* key = (RsaKey*)XMALLOC(sizeof(RsaKey), heap, DYNAMIC_TYPE_RSA); |
207 | 0 | if (key == NULL) { |
208 | 0 | ret = MEMORY_E; |
209 | 0 | } |
210 | 0 | else { |
211 | 0 | switch (rsaInitType) { |
212 | | #ifdef WOLF_PRIVATE_KEY_ID |
213 | | case RSA_NEW_INIT_ID: |
214 | | if (id == NULL || idLen == 0 || label != NULL) { |
215 | | ret = BAD_FUNC_ARG; |
216 | | } |
217 | | else { |
218 | | ret = wc_InitRsaKey_Id(key, id, idLen, heap, devId); |
219 | | } |
220 | | break; |
221 | | case RSA_NEW_INIT_LABEL: |
222 | | if (label == NULL || id != NULL || idLen != 0) { |
223 | | ret = BAD_FUNC_ARG; |
224 | | } |
225 | | else { |
226 | | ret = wc_InitRsaKey_Label(key, label, heap, devId); |
227 | | } |
228 | | break; |
229 | | #endif |
230 | 0 | default: |
231 | 0 | if (id != NULL || idLen != 0 || label != NULL) { |
232 | 0 | ret = BAD_FUNC_ARG; |
233 | 0 | } |
234 | 0 | else { |
235 | 0 | ret = wc_InitRsaKey_ex(key, heap, devId); |
236 | 0 | } |
237 | 0 | break; |
238 | 0 | } |
239 | 0 | if (ret != 0) { |
240 | 0 | XFREE(key, heap, DYNAMIC_TYPE_RSA); |
241 | 0 | key = NULL; |
242 | 0 | } |
243 | 0 | } |
244 | 0 | (void)rsaInitType; |
245 | 0 | (void)id; |
246 | 0 | (void)idLen; |
247 | 0 | (void)label; |
248 | |
|
249 | 0 | if (result_code != NULL) { |
250 | 0 | *result_code = ret; |
251 | 0 | } |
252 | |
|
253 | 0 | return key; |
254 | 0 | } |
255 | | |
256 | | RsaKey* wc_NewRsaKey(void* heap, int devId, int *result_code) |
257 | 0 | { |
258 | 0 | return _NewRsaKey_common(heap, devId, result_code, |
259 | 0 | RSA_NEW_INIT_PLAIN, NULL, 0, NULL); |
260 | 0 | } |
261 | | |
262 | | #ifdef WOLF_PRIVATE_KEY_ID |
263 | | RsaKey* wc_NewRsaKey_Id(unsigned char* id, int len, void* heap, int devId, |
264 | | int *result_code) |
265 | | { |
266 | | return _NewRsaKey_common(heap, devId, result_code, |
267 | | RSA_NEW_INIT_ID, id, len, NULL); |
268 | | } |
269 | | |
270 | | RsaKey* wc_NewRsaKey_Label(const char* label, void* heap, int devId, |
271 | | int *result_code) |
272 | | { |
273 | | return _NewRsaKey_common(heap, devId, result_code, |
274 | | RSA_NEW_INIT_LABEL, NULL, 0, label); |
275 | | } |
276 | | #endif /* WOLF_PRIVATE_KEY_ID */ |
277 | | |
278 | | int wc_DeleteRsaKey(RsaKey* key, RsaKey** key_p) |
279 | 0 | { |
280 | 0 | void* heap; |
281 | 0 | if (key == NULL) { |
282 | 0 | return BAD_FUNC_ARG; |
283 | 0 | } |
284 | 0 | heap = key->heap; |
285 | 0 | wc_FreeRsaKey(key); |
286 | 0 | XFREE(key, heap, DYNAMIC_TYPE_RSA); |
287 | 0 | if (key_p != NULL) { |
288 | 0 | *key_p = NULL; |
289 | 0 | } |
290 | 0 | return 0; |
291 | 0 | } |
292 | | #endif /* !WC_NO_CONSTRUCTORS */ |
293 | | |
294 | | int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId) |
295 | 0 | { |
296 | 0 | int ret = 0; |
297 | |
|
298 | 0 | if (key == NULL) { |
299 | 0 | return BAD_FUNC_ARG; |
300 | 0 | } |
301 | | |
302 | 0 | XMEMSET(key, 0, sizeof(RsaKey)); |
303 | |
|
304 | 0 | key->type = RSA_TYPE_UNKNOWN; |
305 | 0 | key->state = RSA_STATE_NONE; |
306 | 0 | key->heap = heap; |
307 | 0 | #if !defined(WOLFSSL_NO_MALLOC) && (defined(WOLFSSL_ASYNC_CRYPT) || \ |
308 | 0 | (!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE))) |
309 | 0 | key->dataIsAlloc = 0; |
310 | 0 | #endif |
311 | |
|
312 | | #ifdef WOLF_CRYPTO_CB |
313 | | key->devId = devId; |
314 | | #else |
315 | 0 | (void)devId; |
316 | 0 | #endif |
317 | |
|
318 | | #ifdef WOLFSSL_ASYNC_CRYPT |
319 | | #ifdef WOLFSSL_CERT_GEN |
320 | | XMEMSET(&key->certSignCtx, 0, sizeof(CertSignCtx)); |
321 | | #endif |
322 | | |
323 | | #ifdef WC_ASYNC_ENABLE_RSA |
324 | | #ifdef WOLF_CRYPTO_CB |
325 | | /* prefer crypto callback */ |
326 | | if (key->devId != INVALID_DEVID) |
327 | | #endif |
328 | | { |
329 | | /* handle as async */ |
330 | | ret = wolfAsync_DevCtxInit(&key->asyncDev, |
331 | | WOLFSSL_ASYNC_MARKER_RSA, key->heap, devId); |
332 | | if (ret != 0) |
333 | | return ret; |
334 | | } |
335 | | #endif /* WC_ASYNC_ENABLE_RSA */ |
336 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
337 | |
|
338 | 0 | #ifndef WOLFSSL_RSA_PUBLIC_ONLY |
339 | 0 | ret = mp_init_multi(&key->n, &key->e, NULL, NULL, NULL, NULL); |
340 | 0 | if (ret != MP_OKAY) |
341 | 0 | return ret; |
342 | | |
343 | | #if !defined(WOLFSSL_KEY_GEN) && !defined(OPENSSL_EXTRA) && defined(RSA_LOW_MEM) |
344 | | ret = mp_init_multi(&key->d, &key->p, &key->q, NULL, NULL, NULL); |
345 | | #else |
346 | 0 | ret = mp_init_multi(&key->d, &key->p, &key->q, &key->dP, &key->dQ, &key->u); |
347 | 0 | #endif |
348 | 0 | if (ret != MP_OKAY) { |
349 | 0 | mp_clear(&key->n); |
350 | 0 | mp_clear(&key->e); |
351 | 0 | return ret; |
352 | 0 | } |
353 | | #else |
354 | | ret = mp_init(&key->n); |
355 | | if (ret != MP_OKAY) |
356 | | return ret; |
357 | | ret = mp_init(&key->e); |
358 | | if (ret != MP_OKAY) { |
359 | | mp_clear(&key->n); |
360 | | return ret; |
361 | | } |
362 | | #endif |
363 | | |
364 | | #ifdef WOLFSSL_XILINX_CRYPT |
365 | | key->pubExp = 0; |
366 | | key->mod = NULL; |
367 | | #endif |
368 | | |
369 | | #ifdef WOLFSSL_AFALG_XILINX_RSA |
370 | | key->alFd = WC_SOCK_NOTSET; |
371 | | key->rdFd = WC_SOCK_NOTSET; |
372 | | #endif |
373 | | |
374 | | #ifdef WOLFSSL_KCAPI_RSA |
375 | | key->handle = NULL; |
376 | | #endif |
377 | | |
378 | | #if defined(WOLFSSL_RENESAS_FSPSM) |
379 | | key->ctx.wrapped_pri1024_key = NULL; |
380 | | key->ctx.wrapped_pub1024_key = NULL; |
381 | | key->ctx.wrapped_pri2048_key = NULL; |
382 | | key->ctx.wrapped_pub2048_key = NULL; |
383 | | key->ctx.keySz = 0; |
384 | | #endif |
385 | | |
386 | 0 | return ret; |
387 | 0 | } |
388 | | |
389 | | int wc_InitRsaKey(RsaKey* key, void* heap) |
390 | 0 | { |
391 | 0 | return wc_InitRsaKey_ex(key, heap, INVALID_DEVID); |
392 | 0 | } |
393 | | |
394 | | #ifdef WOLF_PRIVATE_KEY_ID |
395 | | int wc_InitRsaKey_Id(RsaKey* key, unsigned char* id, int len, void* heap, |
396 | | int devId) |
397 | | { |
398 | | int ret = 0; |
399 | | #if defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA) |
400 | | /* SE050 TLS users store a word32 at id, need to read it back */ |
401 | | word32 keyId = 0; |
402 | | #endif |
403 | | |
404 | | if (key == NULL) |
405 | | ret = BAD_FUNC_ARG; |
406 | | if (ret == 0 && (len < 0 || len > RSA_MAX_ID_LEN)) |
407 | | ret = BUFFER_E; |
408 | | if (ret == 0) |
409 | | ret = wc_InitRsaKey_ex(key, heap, devId); |
410 | | if (ret == 0 && id != NULL && len != 0) { |
411 | | XMEMCPY(key->id, id, (size_t)len); |
412 | | key->idLen = len; |
413 | | #if defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA) |
414 | | /* Set SE050 ID from word32, populate RsaKey with public from SE050 */ |
415 | | if (len == (int)sizeof(word32)) { |
416 | | keyId = readUnalignedWord32(key->id); |
417 | | ret = wc_RsaUseKeyId(key, keyId, 0); |
418 | | } |
419 | | #endif |
420 | | } |
421 | | |
422 | | return ret; |
423 | | } |
424 | | |
425 | | int wc_InitRsaKey_Label(RsaKey* key, const char* label, void* heap, int devId) |
426 | | { |
427 | | int ret = 0; |
428 | | int labelLen = 0; |
429 | | |
430 | | if (key == NULL || label == NULL) |
431 | | ret = BAD_FUNC_ARG; |
432 | | if (ret == 0) { |
433 | | labelLen = (int)XSTRLEN(label); |
434 | | if (labelLen == 0 || labelLen > RSA_MAX_LABEL_LEN) |
435 | | ret = BUFFER_E; |
436 | | } |
437 | | if (ret == 0) |
438 | | ret = wc_InitRsaKey_ex(key, heap, devId); |
439 | | if (ret == 0) { |
440 | | XMEMCPY(key->label, label, (size_t)labelLen); |
441 | | key->labelLen = labelLen; |
442 | | } |
443 | | |
444 | | return ret; |
445 | | } |
446 | | #endif /* WOLF_PRIVATE_KEY_ID */ |
447 | | |
448 | | |
449 | | #ifdef WOLFSSL_XILINX_CRYPT |
450 | | #define MAX_E_SIZE 4 |
451 | | /* Used to setup hardware state |
452 | | * |
453 | | * key the RSA key to setup |
454 | | * |
455 | | * returns 0 on success |
456 | | */ |
457 | | int wc_InitRsaHw(RsaKey* key) |
458 | | { |
459 | | unsigned char* m; /* RSA modulus */ |
460 | | word32 e = 0; /* RSA public exponent */ |
461 | | int mSz; |
462 | | int eSz; |
463 | | int ret; |
464 | | |
465 | | if (key == NULL) { |
466 | | return BAD_FUNC_ARG; |
467 | | } |
468 | | |
469 | | mSz = mp_unsigned_bin_size(&(key->n)); |
470 | | #if defined(WOLFSSL_XILINX_CRYPT_VERSAL) |
471 | | if (mSz > WOLFSSL_XSECURE_RSA_KEY_SIZE) { |
472 | | return BAD_FUNC_ARG; |
473 | | } |
474 | | /* Allocate 4 bytes more for the public exponent. */ |
475 | | m = (unsigned char*) XMALLOC(WOLFSSL_XSECURE_RSA_KEY_SIZE + 4, key->heap, |
476 | | DYNAMIC_TYPE_KEY); |
477 | | #else |
478 | | m = (unsigned char*)XMALLOC(mSz, key->heap, DYNAMIC_TYPE_KEY); |
479 | | #endif |
480 | | if (m == NULL) { |
481 | | return MEMORY_E; |
482 | | } |
483 | | |
484 | | if (mp_to_unsigned_bin(&(key->n), m) != MP_OKAY) { |
485 | | WOLFSSL_MSG("Unable to get RSA key modulus"); |
486 | | XFREE(m, key->heap, DYNAMIC_TYPE_KEY); |
487 | | return MP_READ_E; |
488 | | } |
489 | | #if defined(WOLFSSL_XILINX_CRYPT_VERSAL) |
490 | | XMEMSET(m + mSz, 0, WOLFSSL_XSECURE_RSA_KEY_SIZE + 4 - mSz); |
491 | | #endif |
492 | | |
493 | | eSz = mp_unsigned_bin_size(&(key->e)); |
494 | | if (eSz > MAX_E_SIZE) { |
495 | | WOLFSSL_MSG("Exponent of size 4 bytes expected"); |
496 | | XFREE(m, key->heap, DYNAMIC_TYPE_KEY); |
497 | | return BAD_FUNC_ARG; |
498 | | } |
499 | | |
500 | | if (mp_to_unsigned_bin(&(key->e), (byte*)&e + (MAX_E_SIZE - eSz)) |
501 | | != MP_OKAY) { |
502 | | XFREE(m, key->heap, DYNAMIC_TYPE_KEY); |
503 | | WOLFSSL_MSG("Unable to get RSA key exponent"); |
504 | | return MP_READ_E; |
505 | | } |
506 | | |
507 | | /* check for existing mod buffer to avoid memory leak */ |
508 | | XFREE(key->mod, key->heap, DYNAMIC_TYPE_KEY); |
509 | | |
510 | | key->pubExp = e; |
511 | | key->mod = m; |
512 | | |
513 | | #if defined(WOLFSSL_XILINX_CRYPT_VERSAL) |
514 | | ret = wc_InitXsecure(&(key->xSec)); |
515 | | if (ret != 0) { |
516 | | WOLFSSL_MSG("Unable to initialize xSecure for RSA"); |
517 | | XFREE(m, key->heap, DYNAMIC_TYPE_KEY); |
518 | | return ret; |
519 | | } |
520 | | XMEMCPY(&m[WOLFSSL_XSECURE_RSA_KEY_SIZE], &e, sizeof(e)); |
521 | | key->mSz = mSz; |
522 | | #else |
523 | | if (XSecure_RsaInitialize(&(key->xRsa), key->mod, NULL, |
524 | | (byte*)&(key->pubExp)) != XST_SUCCESS) { |
525 | | WOLFSSL_MSG("Unable to initialize RSA on hardware"); |
526 | | XFREE(m, key->heap, DYNAMIC_TYPE_KEY); |
527 | | return BAD_STATE_E; |
528 | | } |
529 | | |
530 | | #ifdef WOLFSSL_XILINX_PATCH |
531 | | /* currently a patch of xsecure_rsa.c for 2048 bit keys */ |
532 | | if (wc_RsaEncryptSize(key) == 256) { |
533 | | if (XSecure_RsaSetSize(&(key->xRsa), 2048) != XST_SUCCESS) { |
534 | | WOLFSSL_MSG("Unable to set RSA key size on hardware"); |
535 | | XFREE(m, key->heap, DYNAMIC_TYPE_KEY); |
536 | | return BAD_STATE_E; |
537 | | } |
538 | | } |
539 | | #endif |
540 | | #endif |
541 | | return 0; |
542 | | } /* WOLFSSL_XILINX_CRYPT*/ |
543 | | |
544 | | #elif defined(WOLFSSL_CRYPTOCELL) |
545 | | |
546 | | int wc_InitRsaHw(RsaKey* key) |
547 | | { |
548 | | CRYSError_t ret = 0; |
549 | | byte e[3]; |
550 | | word32 eSz = sizeof(e); |
551 | | byte n[256]; |
552 | | word32 nSz = sizeof(n); |
553 | | byte d[256]; |
554 | | word32 dSz = sizeof(d); |
555 | | byte p[128]; |
556 | | word32 pSz = sizeof(p); |
557 | | byte q[128]; |
558 | | word32 qSz = sizeof(q); |
559 | | |
560 | | if (key == NULL) { |
561 | | return BAD_FUNC_ARG; |
562 | | } |
563 | | |
564 | | ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz); |
565 | | if (ret != 0) |
566 | | return MP_READ_E; |
567 | | |
568 | | ret = CRYS_RSA_Build_PubKey(&key->ctx.pubKey, e, eSz, n, nSz); |
569 | | if (ret != SA_SILIB_RET_OK){ |
570 | | WOLFSSL_MSG("CRYS_RSA_Build_PubKey failed"); |
571 | | return ret; |
572 | | } |
573 | | |
574 | | ret = CRYS_RSA_Build_PrivKey(&key->ctx.privKey, d, dSz, e, eSz, n, nSz); |
575 | | |
576 | | if (ret != SA_SILIB_RET_OK){ |
577 | | WOLFSSL_MSG("CRYS_RSA_Build_PrivKey failed"); |
578 | | return ret; |
579 | | } |
580 | | key->type = RSA_PRIVATE; |
581 | | return 0; |
582 | | } |
583 | | |
584 | | static int cc310_RSA_GenerateKeyPair(RsaKey* key, int size, long e) |
585 | | { |
586 | | CRYSError_t ret = 0; |
587 | | CRYS_RSAKGData_t KeyGenData; |
588 | | CRYS_RSAKGFipsContext_t FipsCtx; |
589 | | byte ex[3]; |
590 | | word16 eSz = sizeof(ex); |
591 | | byte n[256]; |
592 | | word16 nSz = sizeof(n); |
593 | | |
594 | | ret = CRYS_RSA_KG_GenerateKeyPair(&wc_rndState, |
595 | | wc_rndGenVectFunc, |
596 | | (byte*)&e, |
597 | | 3*sizeof(byte), |
598 | | size, |
599 | | &key->ctx.privKey, |
600 | | &key->ctx.pubKey, |
601 | | &KeyGenData, |
602 | | &FipsCtx); |
603 | | |
604 | | if (ret != SA_SILIB_RET_OK){ |
605 | | WOLFSSL_MSG("CRYS_RSA_KG_GenerateKeyPair failed"); |
606 | | return ret; |
607 | | } |
608 | | |
609 | | ret = CRYS_RSA_Get_PubKey(&key->ctx.pubKey, ex, &eSz, n, &nSz); |
610 | | if (ret != SA_SILIB_RET_OK){ |
611 | | WOLFSSL_MSG("CRYS_RSA_Get_PubKey failed"); |
612 | | return ret; |
613 | | } |
614 | | ret = wc_RsaPublicKeyDecodeRaw(n, nSz, ex, eSz, key); |
615 | | |
616 | | key->type = RSA_PRIVATE; |
617 | | |
618 | | return ret; |
619 | | } |
620 | | #endif /* WOLFSSL_CRYPTOCELL */ |
621 | | |
622 | | #if defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA) |
623 | | /* Use specified hardware key ID with RsaKey operations. Unlike devId, |
624 | | * keyId is a word32 so can handle key IDs larger than an int. |
625 | | * |
626 | | * key initialized RsaKey struct |
627 | | * keyId hardware key ID which stores RSA key |
628 | | * flags optional flags, currently unused |
629 | | * |
630 | | * Return 0 on success, negative on error */ |
631 | | int wc_RsaUseKeyId(RsaKey* key, word32 keyId, word32 flags) |
632 | | { |
633 | | (void)flags; |
634 | | |
635 | | if (key == NULL) { |
636 | | return BAD_FUNC_ARG; |
637 | | } |
638 | | |
639 | | return se050_rsa_use_key_id(key, keyId); |
640 | | } |
641 | | |
642 | | /* Get hardware key ID associated with this RsaKey structure. |
643 | | * |
644 | | * key initialized RsaKey struct |
645 | | * keyId [OUT] output for key ID associated with this structure |
646 | | * |
647 | | * Returns 0 on success, negative on error. |
648 | | */ |
649 | | int wc_RsaGetKeyId(RsaKey* key, word32* keyId) |
650 | | { |
651 | | if (key == NULL || keyId == NULL) { |
652 | | return BAD_FUNC_ARG; |
653 | | } |
654 | | |
655 | | return se050_rsa_get_key_id(key, keyId); |
656 | | } |
657 | | #endif /* WOLFSSL_SE050 */ |
658 | | |
659 | | int wc_FreeRsaKey(RsaKey* key) |
660 | 0 | { |
661 | 0 | int ret = 0; |
662 | |
|
663 | 0 | if (key == NULL) { |
664 | 0 | return BAD_FUNC_ARG; |
665 | 0 | } |
666 | | |
667 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE) |
668 | | #ifndef WOLF_CRYPTO_CB_FIND |
669 | | if (key->devId != INVALID_DEVID) |
670 | | #endif |
671 | | { |
672 | | ret = wc_CryptoCb_Free(key->devId, WC_ALGO_TYPE_PK, |
673 | | WC_PK_TYPE_RSA, 0, key); |
674 | | /* If callback wants standard free, it returns CRYPTOCB_UNAVAILABLE. |
675 | | * Otherwise assume the callback handled cleanup. */ |
676 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
677 | | return ret; |
678 | | /* fall-through to software cleanup */ |
679 | | ret = 0; |
680 | | } |
681 | | #endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_FREE */ |
682 | | |
683 | | #if defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA) |
684 | | se050_rsa_free_key(key); |
685 | | #endif |
686 | | |
687 | 0 | wc_RsaCleanup(key); |
688 | |
|
689 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA) |
690 | | wolfAsync_DevCtxFree(&key->asyncDev, WOLFSSL_ASYNC_MARKER_RSA); |
691 | | #endif |
692 | |
|
693 | 0 | #ifndef WOLFSSL_RSA_PUBLIC_ONLY |
694 | | /* Forcezero all private key fields that are present in this build |
695 | | * configuration, since they may contain residual sensitive data even when |
696 | | * key->type is not RSA_PRIVATE (e.g., after a partial key decode failure). */ |
697 | 0 | #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM) |
698 | 0 | mp_forcezero(&key->u); |
699 | 0 | mp_forcezero(&key->dQ); |
700 | 0 | mp_forcezero(&key->dP); |
701 | 0 | #endif |
702 | 0 | mp_forcezero(&key->q); |
703 | 0 | mp_forcezero(&key->p); |
704 | 0 | mp_forcezero(&key->d); |
705 | 0 | #endif /* WOLFSSL_RSA_PUBLIC_ONLY */ |
706 | | |
707 | | /* public part */ |
708 | 0 | mp_clear(&key->e); |
709 | 0 | mp_clear(&key->n); |
710 | |
|
711 | | #ifdef WOLFSSL_XILINX_CRYPT |
712 | | XFREE(key->mod, key->heap, DYNAMIC_TYPE_KEY); |
713 | | key->mod = NULL; |
714 | | #endif |
715 | |
|
716 | | #ifdef WOLFSSL_AFALG_XILINX_RSA |
717 | | /* make sure that sockets are closed on cleanup */ |
718 | | if (key->alFd > 0) { |
719 | | close(key->alFd); |
720 | | key->alFd = WC_SOCK_NOTSET; |
721 | | } |
722 | | if (key->rdFd > 0) { |
723 | | close(key->rdFd); |
724 | | key->rdFd = WC_SOCK_NOTSET; |
725 | | } |
726 | | #endif |
727 | |
|
728 | | #ifdef WOLFSSL_KCAPI_RSA |
729 | | KcapiRsa_Free(key); |
730 | | #endif |
731 | |
|
732 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
733 | | wc_MemZero_Check(key, sizeof(RsaKey)); |
734 | | #endif |
735 | |
|
736 | | #if defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) |
737 | | wc_fspsm_RsaKeyFree(key); |
738 | | #endif |
739 | | #ifdef WOLFSSL_MICROCHIP_TA100 |
740 | | wc_Microchip_rsa_free(key); |
741 | | #endif |
742 | 0 | return ret; |
743 | 0 | } |
744 | | |
745 | | #ifdef WOLFSSL_RSA_KEY_CHECK |
746 | | /* Check the pair-wise consistency of the RSA key. */ |
747 | | static int _ifc_pairwise_consistency_test(RsaKey* key, WC_RNG* rng) |
748 | | { |
749 | | static const char* msg = "Everyone gets Friday off."; |
750 | | #ifndef WOLFSSL_NO_MALLOC |
751 | | byte* sig = NULL; |
752 | | #else |
753 | | byte sig[RSA_MAX_SIZE/8]; |
754 | | #endif |
755 | | byte* plain; |
756 | | int ret = 0; |
757 | | word32 msgLen, plainLen, sigLen; |
758 | | |
759 | | msgLen = (word32)XSTRLEN(msg); |
760 | | ret = wc_RsaEncryptSize(key); |
761 | | if (ret < 0) |
762 | | return ret; |
763 | | else if (ret == 0) |
764 | | return BAD_FUNC_ARG; |
765 | | sigLen = (word32)ret; |
766 | | |
767 | | WOLFSSL_MSG("Doing RSA consistency test"); |
768 | | |
769 | | #ifndef WOLFSSL_NO_MALLOC |
770 | | /* Sign and verify. */ |
771 | | sig = (byte*)XMALLOC(sigLen, key->heap, DYNAMIC_TYPE_RSA); |
772 | | if (sig == NULL) { |
773 | | return MEMORY_E; |
774 | | } |
775 | | #endif |
776 | | XMEMSET(sig, 0, sigLen); |
777 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
778 | | wc_MemZero_Add("Pairwise CT sig", sig, sigLen); |
779 | | #endif |
780 | | plain = sig; |
781 | | |
782 | | #ifdef WOLFSSL_ASYNC_CRYPT |
783 | | /* Do blocking async calls here, caller does not support WC_PENDING_E */ |
784 | | do { |
785 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) |
786 | | ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); |
787 | | if (ret >= 0) |
788 | | #endif |
789 | | ret = wc_RsaSSL_Sign((const byte*)msg, msgLen, sig, sigLen, key, rng); |
790 | | #ifdef WOLFSSL_ASYNC_CRYPT |
791 | | } while (ret == WC_NO_ERR_TRACE(WC_PENDING_E)); |
792 | | #endif |
793 | | |
794 | | if (ret > 0) { |
795 | | sigLen = (word32)ret; |
796 | | #ifdef WOLFSSL_ASYNC_CRYPT |
797 | | /* Do blocking async calls here, caller does not support WC_PENDING_E */ |
798 | | do { |
799 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) |
800 | | ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); |
801 | | if (ret >= 0) |
802 | | #endif |
803 | | ret = wc_RsaSSL_VerifyInline(sig, sigLen, &plain, key); |
804 | | #ifdef WOLFSSL_ASYNC_CRYPT |
805 | | } while (ret == WC_NO_ERR_TRACE(WC_PENDING_E)); |
806 | | #endif |
807 | | } |
808 | | |
809 | | if (ret > 0) { |
810 | | plainLen = (word32)ret; |
811 | | ret = (msgLen != plainLen) || (XMEMCMP(plain, msg, msgLen) != 0); |
812 | | } |
813 | | |
814 | | if (ret != 0) |
815 | | ret = RSA_KEY_PAIR_E; |
816 | | |
817 | | ForceZero(sig, sigLen); |
818 | | #ifndef WOLFSSL_NO_MALLOC |
819 | | XFREE(sig, key->heap, DYNAMIC_TYPE_RSA); |
820 | | #endif |
821 | | |
822 | | return ret; |
823 | | } |
824 | | |
825 | | |
826 | | #if FIPS_VERSION3_GE(7,0,0) && defined(WOLFSSL_KEY_GEN) && \ |
827 | | !defined(WOLFSSL_RSA_PUBLIC_ONLY) |
828 | | /* Defined with the key generation code below; reused here so that key pair |
829 | | * validation checks the primes as strictly as generation does. |
830 | | * FIPS 186-5 App. A.1.1. */ |
831 | | static int _CheckProbablePrime(mp_int* p, mp_int* q, mp_int* e, int nlen, |
832 | | int* isPrime, WC_RNG* rng); |
833 | | #endif |
834 | | |
835 | | int wc_CheckRsaKey(RsaKey* key) |
836 | | { |
837 | | WC_RNG *rng = NULL; |
838 | | #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC) |
839 | | WC_RNG rng_buf; |
840 | | #endif |
841 | | int ret = 0; |
842 | | DECL_MP_INT_SIZE_DYN(tmp, (key)? mp_bitsused(&key->n) : 0, RSA_MAX_SIZE); |
843 | | |
844 | | if (key == NULL) { |
845 | | return BAD_FUNC_ARG; |
846 | | } |
847 | | |
848 | | #ifdef WOLFSSL_CAAM |
849 | | /* can not perform these checks on an encrypted key */ |
850 | | if (key->blackKey != 0) { |
851 | | return 0; |
852 | | } |
853 | | #endif |
854 | | |
855 | | if (MP_BITS_OVER_MAX(mp_bitsused(&key->n), RSA_MAX_SIZE)) { |
856 | | return WC_KEY_SIZE_E; |
857 | | } |
858 | | |
859 | | NEW_MP_INT_SIZE(tmp, mp_bitsused(&key->n), NULL, DYNAMIC_TYPE_RSA); |
860 | | #ifdef MP_INT_SIZE_CHECK_NULL |
861 | | if (tmp == NULL) { |
862 | | return MEMORY_E; |
863 | | } |
864 | | #endif |
865 | | |
866 | | if (key->rng) |
867 | | rng = key->rng; |
868 | | else { |
869 | | #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC) |
870 | | rng = &rng_buf; |
871 | | #else |
872 | | rng = (WC_RNG *)XMALLOC(sizeof(*rng), NULL, DYNAMIC_TYPE_RNG); |
873 | | if (rng == NULL) { |
874 | | FREE_MP_INT_SIZE(tmp, NULL, DYNAMIC_TYPE_RSA); |
875 | | return MEMORY_E; |
876 | | } |
877 | | #endif |
878 | | ret = wc_InitRng(rng); |
879 | | if (ret != 0) { |
880 | | #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
881 | | XFREE(rng, NULL, DYNAMIC_TYPE_RNG); |
882 | | FREE_MP_INT_SIZE(tmp, NULL, DYNAMIC_TYPE_RSA); |
883 | | #endif |
884 | | return ret; |
885 | | } |
886 | | } |
887 | | |
888 | | if (ret == 0) { |
889 | | if (INIT_MP_INT_SIZE(tmp, mp_bitsused(&key->n)) != MP_OKAY) |
890 | | ret = MP_INIT_E; |
891 | | } |
892 | | |
893 | | if (ret == 0) |
894 | | ret = _ifc_pairwise_consistency_test(key, rng); |
895 | | |
896 | | /* Check d is less than n. */ |
897 | | if (ret == 0 ) { |
898 | | if (mp_cmp(&key->d, &key->n) != MP_LT) { |
899 | | ret = MP_EXPTMOD_E; |
900 | | } |
901 | | } |
902 | | /* Check p*q = n. */ |
903 | | if (ret == 0 ) { |
904 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
905 | | mp_memzero_add("RSA CheckKey tmp", tmp); |
906 | | #endif |
907 | | if (mp_mul(&key->p, &key->q, tmp) != MP_OKAY) { |
908 | | ret = MP_EXPTMOD_E; |
909 | | } |
910 | | } |
911 | | if (ret == 0 ) { |
912 | | if (mp_cmp(&key->n, tmp) != MP_EQ) { |
913 | | ret = MP_EXPTMOD_E; |
914 | | } |
915 | | } |
916 | | |
917 | | #if FIPS_VERSION3_GE(7,0,0) && defined(WOLFSSL_KEY_GEN) && \ |
918 | | !defined(WOLFSSL_RSA_PUBLIC_ONLY) |
919 | | /* Validate the key the way SP 800-56B Rev2 sec 6.4.1.4.3 (crt_pkv) does. |
920 | | * Step numbers below are that section's. */ |
921 | | if (ret == 0) { |
922 | | int nBits = mp_count_bits(&key->n); |
923 | | int isPrime = 0; |
924 | | |
925 | | /* Modulus: even number of bits (item D, step 3c) and at least 2048 |
926 | | * (FIPS 186-5 sec 5.1). Spelled out rather than using RSA_MIN_SIZE, |
927 | | * which drops to 1024 under HAVE_WOLFENGINE / HAVE_WOLFPROVIDER. */ |
928 | | if ((nBits < 2048) || ((nBits & 1) != 0)) { |
929 | | ret = WC_KEY_SIZE_E; |
930 | | } |
931 | | |
932 | | /* Public exponent: odd, and 65537 <= e < 2^256 (item B). Bounds are |
933 | | * bit counts, not mp_cmp_d: a digit can be 8 or 16 bits wide |
934 | | * (sp_int.h), and 65537 would truncate there. An odd e of at least |
935 | | * 17 bits is >= 65537, because 65536 is the only 17-bit value below |
936 | | * it and that one is even. */ |
937 | | if ((ret == 0) && (mp_iseven(&key->e) || |
938 | | (mp_count_bits(&key->e) < 17) || |
939 | | (mp_count_bits(&key->e) > 256))) { |
940 | | ret = MP_EXPTMOD_E; |
941 | | } |
942 | | |
943 | | /* Primes: right size, coprime to e, far enough apart, and actually |
944 | | * prime (steps 5a to 5g). Two calls because steps 5f/5g want a |
945 | | * primality test on each prime: the first tests p, the second tests |
946 | | * q and the |p - q| separation. */ |
947 | | if (ret == 0) { |
948 | | ret = _CheckProbablePrime(&key->p, NULL, &key->e, nBits, &isPrime, |
949 | | rng); |
950 | | if ((ret == 0) && isPrime) { |
951 | | ret = _CheckProbablePrime(&key->p, &key->q, &key->e, nBits, |
952 | | &isPrime, rng); |
953 | | } |
954 | | if ((ret == 0) && (!isPrime)) { |
955 | | ret = MP_EXPTMOD_E; |
956 | | } |
957 | | } |
958 | | |
959 | | /* Private exponent must exceed 2^(nBits/2) (step 6a). A d of that |
960 | | * many bits or fewer cannot, so counting bits settles it. */ |
961 | | if ((ret == 0) && (mp_count_bits(&key->d) <= (nBits / 2))) { |
962 | | ret = MP_EXPTMOD_E; |
963 | | } |
964 | | } |
965 | | #endif |
966 | | |
967 | | #ifndef WC_RSA_NO_FERMAT_CHECK |
968 | | /* Fermat's Factorization works when difference between p and q |
969 | | * is less than (conservatively): |
970 | | * n^(1/4) + 32 |
971 | | * ~= 2^(bit count of n)^(1/4) + 32) = 2^((bit count of n)/4 + 32) |
972 | | */ |
973 | | if (ret == 0) { |
974 | | ret = mp_sub(&key->p, &key->q, tmp); |
975 | | } |
976 | | if (ret == 0) { |
977 | | if (mp_count_bits(tmp) <= (mp_count_bits(&key->n) / 4 + 32)) { |
978 | | ret = MP_EXPTMOD_E; |
979 | | } |
980 | | } |
981 | | #endif |
982 | | |
983 | | /* Check dP, dQ and u if they exist */ |
984 | | if (ret == 0 && !mp_iszero(&key->dP)) { |
985 | | #if FIPS_VERSION3_GE(7,0,0) |
986 | | /* Each CRT component must be greater than 1; upper bounds are |
987 | | * checked just below. SP 800-56B Rev2 sec 6.4.1.4.3 item F, steps |
988 | | * 7a/7b/7c. No WOLFSSL_KEY_GEN in the guard: unlike the block |
989 | | * above this calls no key-generation helper. */ |
990 | | if ((mp_cmp_d(&key->dP, 1) != MP_GT) || |
991 | | (mp_cmp_d(&key->dQ, 1) != MP_GT) || |
992 | | (mp_cmp_d(&key->u, 1) != MP_GT)) { |
993 | | ret = MP_EXPTMOD_E; |
994 | | } |
995 | | #endif |
996 | | if ((ret == 0) && (mp_sub_d(&key->p, 1, tmp) != MP_OKAY)) { |
997 | | ret = MP_EXPTMOD_E; |
998 | | } |
999 | | /* Check dP <= p-1. */ |
1000 | | if (ret == 0) { |
1001 | | if (mp_cmp(&key->dP, tmp) != MP_LT) { |
1002 | | ret = MP_EXPTMOD_E; |
1003 | | } |
1004 | | } |
1005 | | /* Check e*dP mod p-1 = 1. (dP = 1/e mod p-1) */ |
1006 | | if (ret == 0) { |
1007 | | if (mp_mulmod(&key->dP, &key->e, tmp, tmp) != MP_OKAY) { |
1008 | | ret = MP_EXPTMOD_E; |
1009 | | } |
1010 | | } |
1011 | | if (ret == 0 ) { |
1012 | | if (!mp_isone(tmp)) { |
1013 | | ret = MP_EXPTMOD_E; |
1014 | | } |
1015 | | } |
1016 | | |
1017 | | if (ret == 0) { |
1018 | | if (mp_sub_d(&key->q, 1, tmp) != MP_OKAY) { |
1019 | | ret = MP_EXPTMOD_E; |
1020 | | } |
1021 | | } |
1022 | | /* Check dQ <= q-1. */ |
1023 | | if (ret == 0) { |
1024 | | if (mp_cmp(&key->dQ, tmp) != MP_LT) { |
1025 | | ret = MP_EXPTMOD_E; |
1026 | | } |
1027 | | } |
1028 | | /* Check e*dP mod p-1 = 1. (dQ = 1/e mod q-1) */ |
1029 | | if (ret == 0) { |
1030 | | if (mp_mulmod(&key->dQ, &key->e, tmp, tmp) != MP_OKAY) { |
1031 | | ret = MP_EXPTMOD_E; |
1032 | | } |
1033 | | } |
1034 | | if (ret == 0 ) { |
1035 | | if (!mp_isone(tmp)) { |
1036 | | ret = MP_EXPTMOD_E; |
1037 | | } |
1038 | | } |
1039 | | |
1040 | | /* Check u <= p. */ |
1041 | | if (ret == 0) { |
1042 | | if (mp_cmp(&key->u, &key->p) != MP_LT) { |
1043 | | ret = MP_EXPTMOD_E; |
1044 | | } |
1045 | | } |
1046 | | /* Check u*q mod p = 1. (u = 1/q mod p) */ |
1047 | | if (ret == 0) { |
1048 | | if (mp_mulmod(&key->u, &key->q, &key->p, tmp) != MP_OKAY) { |
1049 | | ret = MP_EXPTMOD_E; |
1050 | | } |
1051 | | } |
1052 | | if (ret == 0 ) { |
1053 | | if (!mp_isone(tmp)) { |
1054 | | ret = MP_EXPTMOD_E; |
1055 | | } |
1056 | | } |
1057 | | } |
1058 | | |
1059 | | mp_forcezero(tmp); |
1060 | | |
1061 | | if ((rng != NULL) && (rng != key->rng)) { |
1062 | | wc_FreeRng(rng); |
1063 | | #ifdef WOLFSSL_SMALL_STACK |
1064 | | XFREE(rng, NULL, DYNAMIC_TYPE_RNG); |
1065 | | #endif |
1066 | | } |
1067 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1068 | | mp_memzero_check(tmp); |
1069 | | #endif |
1070 | | FREE_MP_INT_SIZE(tmp, NULL, DYNAMIC_TYPE_RSA); |
1071 | | |
1072 | | return ret; |
1073 | | } |
1074 | | #endif /* WOLFSSL_RSA_KEY_CHECK */ |
1075 | | |
1076 | | |
1077 | | #if !defined(WC_NO_RSA_OAEP) || defined(WC_RSA_PSS) |
1078 | | /* Uses MGF1 standard as a mask generation function |
1079 | | hType: hash type used |
1080 | | seed: seed to use for generating mask |
1081 | | seedSz: size of seed buffer |
1082 | | out: mask output after generation |
1083 | | outSz: size of output buffer |
1084 | | */ |
1085 | | #if !defined(NO_SHA) || !defined(NO_SHA256) || defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512) |
1086 | | static int RsaMGF1(enum wc_HashType hType, byte* seed, word32 seedSz, |
1087 | | byte* out, word32 outSz, void* heap) |
1088 | 0 | { |
1089 | | #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
1090 | | byte* tmp = NULL; |
1091 | | byte tmpF = 0; /* 1 if dynamic memory needs freed */ |
1092 | | #else |
1093 | 0 | byte tmp[RSA_MAX_SIZE/8]; |
1094 | 0 | #endif |
1095 | | /* needs to be large enough for seed size plus counter(4) */ |
1096 | 0 | byte tmpA[WC_MAX_DIGEST_SIZE + 4]; |
1097 | 0 | word32 tmpSz = 0; |
1098 | 0 | int hLen; |
1099 | 0 | int ret; |
1100 | 0 | word32 counter; |
1101 | 0 | word32 idx; |
1102 | | #ifdef WOLFSSL_SMALL_STACK_CACHE |
1103 | | wc_HashAlg *hash; |
1104 | | #endif |
1105 | 0 | hLen = wc_HashGetDigestSize(hType); |
1106 | 0 | counter = 0; |
1107 | 0 | idx = 0; |
1108 | |
|
1109 | 0 | (void)heap; |
1110 | |
|
1111 | 0 | XMEMSET(tmpA, 0, sizeof(tmpA)); |
1112 | | /* check error return of wc_HashGetDigestSize */ |
1113 | 0 | if (hLen < 0) { |
1114 | 0 | return hLen; |
1115 | 0 | } |
1116 | | |
1117 | | /* if tmp is not large enough than use some dynamic memory */ |
1118 | 0 | if ((seedSz + 4) > sizeof(tmpA) || (word32)hLen > sizeof(tmpA)) { |
1119 | | /* find largest amount of memory needed which will be the max of |
1120 | | * hLen and (seedSz + 4) since tmp is used to store the hash digest */ |
1121 | 0 | tmpSz = ((seedSz + 4) > (word32)hLen)? seedSz + 4: (word32)hLen; |
1122 | | #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
1123 | | tmp = (byte*)XMALLOC(tmpSz, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1124 | | if (tmp == NULL) { |
1125 | | return MEMORY_E; |
1126 | | } |
1127 | | tmpF = 1; /* make sure to free memory when done */ |
1128 | | #else |
1129 | 0 | if (tmpSz > RSA_MAX_SIZE/8) |
1130 | 0 | return BAD_FUNC_ARG; |
1131 | 0 | #endif |
1132 | 0 | } |
1133 | 0 | else { |
1134 | | /* use array on the stack */ |
1135 | 0 | #ifndef WOLFSSL_SMALL_STACK_CACHE |
1136 | 0 | tmpSz = sizeof(tmpA); |
1137 | 0 | #endif |
1138 | | #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
1139 | | tmp = tmpA; |
1140 | | tmpF = 0; /* no need to free memory at end */ |
1141 | | #endif |
1142 | 0 | } |
1143 | | |
1144 | | #ifdef WOLFSSL_SMALL_STACK_CACHE |
1145 | | hash = (wc_HashAlg*)XMALLOC(sizeof(*hash), heap, DYNAMIC_TYPE_DIGEST); |
1146 | | if (hash == NULL) { |
1147 | | #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
1148 | | if (tmpF) { |
1149 | | XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1150 | | } |
1151 | | #endif |
1152 | | return MEMORY_E; |
1153 | | } |
1154 | | ret = wc_HashInit_ex(hash, hType, heap, INVALID_DEVID); |
1155 | | if (ret != 0) { |
1156 | | XFREE(hash, heap, DYNAMIC_TYPE_DIGEST); |
1157 | | #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
1158 | | if (tmpF) { |
1159 | | XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1160 | | } |
1161 | | #endif |
1162 | | return ret; |
1163 | | } |
1164 | | #endif |
1165 | | |
1166 | 0 | do { |
1167 | 0 | int i = 0; |
1168 | 0 | XMEMCPY(tmp, seed, seedSz); |
1169 | | |
1170 | | /* counter to byte array appended to tmp */ |
1171 | 0 | tmp[seedSz] = (byte)((counter >> 24) & 0xFF); |
1172 | 0 | tmp[seedSz + 1] = (byte)((counter >> 16) & 0xFF); |
1173 | 0 | tmp[seedSz + 2] = (byte)((counter >> 8) & 0xFF); |
1174 | 0 | tmp[seedSz + 3] = (byte)((counter) & 0xFF); |
1175 | | |
1176 | | /* hash and append to existing output */ |
1177 | | #ifdef WOLFSSL_SMALL_STACK_CACHE |
1178 | | ret = wc_HashUpdate(hash, hType, tmp, (seedSz + 4)); |
1179 | | if (ret == 0) { |
1180 | | ret = wc_HashFinal(hash, hType, tmp); |
1181 | | } |
1182 | | #else |
1183 | 0 | ret = wc_Hash(hType, tmp, (seedSz + 4), tmp, tmpSz); |
1184 | 0 | #endif |
1185 | 0 | if (ret != 0) { |
1186 | | /* check for if dynamic memory was needed, then free */ |
1187 | | #ifdef WOLFSSL_SMALL_STACK_CACHE |
1188 | | wc_HashFree(hash, hType); |
1189 | | XFREE(hash, heap, DYNAMIC_TYPE_DIGEST); |
1190 | | #endif |
1191 | | #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
1192 | | if (tmpF) { |
1193 | | XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1194 | | } |
1195 | | #endif |
1196 | 0 | return ret; |
1197 | 0 | } |
1198 | | |
1199 | 0 | for (i = 0; i < hLen && idx < outSz; i++) { |
1200 | 0 | out[idx++] = tmp[i]; |
1201 | 0 | } |
1202 | 0 | counter++; |
1203 | 0 | } while (idx < outSz); |
1204 | | #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
1205 | | /* check for if dynamic memory was needed, then free */ |
1206 | | if (tmpF) { |
1207 | | XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1208 | | } |
1209 | | #endif |
1210 | | #ifdef WOLFSSL_SMALL_STACK_CACHE |
1211 | | wc_HashFree(hash, hType); |
1212 | | XFREE(hash, heap, DYNAMIC_TYPE_DIGEST); |
1213 | | #endif |
1214 | | |
1215 | 0 | return 0; |
1216 | 0 | } |
1217 | | #endif /* SHA2 Hashes */ |
1218 | | |
1219 | | #if defined(WOLFSSL_SHA3) && \ |
1220 | | (defined(WOLFSSL_SHAKE128) || defined(WOLFSSL_SHAKE256)) |
1221 | | /* SHAKE XOF used directly as mask generation function (not MGF1). |
1222 | | * Per FIPS 186-5, SHAKE can be used as the MGF for RSA-PSS. */ |
1223 | | static int RsaMGF_SHAKE(enum wc_HashType shakeType, byte* seed, word32 seedSz, |
1224 | | byte* out, word32 outSz, void* heap) |
1225 | 0 | { |
1226 | 0 | WC_DECLARE_VAR(shake, wc_Shake, 1, heap); |
1227 | 0 | int ret; |
1228 | |
|
1229 | 0 | (void)heap; |
1230 | 0 | (void)shakeType; |
1231 | |
|
1232 | 0 | WC_ALLOC_VAR_EX(shake, wc_Shake, 1, heap, DYNAMIC_TYPE_TMP_BUFFER, |
1233 | 0 | return MEMORY_E); |
1234 | |
|
1235 | 0 | #ifdef WOLFSSL_SHAKE128 |
1236 | 0 | if (shakeType == WC_HASH_TYPE_SHAKE128) { |
1237 | 0 | ret = wc_InitShake128(shake, heap, INVALID_DEVID); |
1238 | 0 | if (ret == 0) { |
1239 | 0 | ret = wc_Shake128_Update(shake, seed, seedSz); |
1240 | 0 | if (ret == 0) |
1241 | 0 | ret = wc_Shake128_Final(shake, out, outSz); |
1242 | 0 | wc_Shake128_Free(shake); |
1243 | 0 | } |
1244 | 0 | } |
1245 | 0 | else |
1246 | 0 | #endif |
1247 | 0 | #ifdef WOLFSSL_SHAKE256 |
1248 | 0 | if (shakeType == WC_HASH_TYPE_SHAKE256) { |
1249 | 0 | ret = wc_InitShake256(shake, heap, INVALID_DEVID); |
1250 | 0 | if (ret == 0) { |
1251 | 0 | ret = wc_Shake256_Update(shake, seed, seedSz); |
1252 | 0 | if (ret == 0) |
1253 | 0 | ret = wc_Shake256_Final(shake, out, outSz); |
1254 | 0 | wc_Shake256_Free(shake); |
1255 | 0 | } |
1256 | 0 | } |
1257 | 0 | else |
1258 | 0 | #endif |
1259 | 0 | { |
1260 | 0 | ret = BAD_FUNC_ARG; |
1261 | 0 | } |
1262 | 0 | WC_FREE_VAR_EX(shake, heap, DYNAMIC_TYPE_TMP_BUFFER); |
1263 | 0 | return ret; |
1264 | 0 | } |
1265 | | #endif /* WOLFSSL_SHA3 && (WOLFSSL_SHAKE128 || WOLFSSL_SHAKE256) */ |
1266 | | |
1267 | | /* helper function to direct which mask generation function is used |
1268 | | switched on type input |
1269 | | */ |
1270 | | static int RsaMGF(int type, byte* seed, word32 seedSz, byte* out, |
1271 | | word32 outSz, void* heap) |
1272 | 0 | { |
1273 | 0 | int ret; |
1274 | |
|
1275 | 0 | switch(type) { |
1276 | 0 | #ifndef NO_SHA |
1277 | 0 | case WC_MGF1SHA1: |
1278 | 0 | ret = RsaMGF1(WC_HASH_TYPE_SHA, seed, seedSz, out, outSz, heap); |
1279 | 0 | break; |
1280 | 0 | #endif |
1281 | 0 | #ifndef NO_SHA256 |
1282 | 0 | #ifdef WOLFSSL_SHA224 |
1283 | 0 | case WC_MGF1SHA224: |
1284 | 0 | ret = RsaMGF1(WC_HASH_TYPE_SHA224, seed, seedSz, out, outSz, heap); |
1285 | 0 | break; |
1286 | 0 | #endif |
1287 | 0 | case WC_MGF1SHA256: |
1288 | 0 | ret = RsaMGF1(WC_HASH_TYPE_SHA256, seed, seedSz, out, outSz, heap); |
1289 | 0 | break; |
1290 | 0 | #endif |
1291 | 0 | #ifdef WOLFSSL_SHA384 |
1292 | 0 | case WC_MGF1SHA384: |
1293 | 0 | ret = RsaMGF1(WC_HASH_TYPE_SHA384, seed, seedSz, out, outSz, heap); |
1294 | 0 | break; |
1295 | 0 | #endif |
1296 | 0 | #ifdef WOLFSSL_SHA512 |
1297 | 0 | case WC_MGF1SHA512: |
1298 | 0 | ret = RsaMGF1(WC_HASH_TYPE_SHA512, seed, seedSz, out, outSz, heap); |
1299 | 0 | break; |
1300 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
1301 | 0 | case WC_MGF1SHA512_224: |
1302 | 0 | ret = RsaMGF1(WC_HASH_TYPE_SHA512_224, seed, seedSz, out, outSz, |
1303 | 0 | heap); |
1304 | 0 | break; |
1305 | 0 | #endif |
1306 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
1307 | 0 | case WC_MGF1SHA512_256: |
1308 | 0 | ret = RsaMGF1(WC_HASH_TYPE_SHA512_256, seed, seedSz, out, outSz, |
1309 | 0 | heap); |
1310 | 0 | break; |
1311 | 0 | #endif |
1312 | 0 | #endif |
1313 | 0 | #ifdef WOLFSSL_SHA3 |
1314 | 0 | #ifndef WOLFSSL_NOSHA3_224 |
1315 | 0 | case WC_MGF1SHA3_224: |
1316 | 0 | ret = RsaMGF1(WC_HASH_TYPE_SHA3_224, seed, seedSz, out, outSz, |
1317 | 0 | heap); |
1318 | 0 | break; |
1319 | 0 | #endif |
1320 | 0 | #ifndef WOLFSSL_NOSHA3_256 |
1321 | 0 | case WC_MGF1SHA3_256: |
1322 | 0 | ret = RsaMGF1(WC_HASH_TYPE_SHA3_256, seed, seedSz, out, outSz, |
1323 | 0 | heap); |
1324 | 0 | break; |
1325 | 0 | #endif |
1326 | 0 | #ifndef WOLFSSL_NOSHA3_384 |
1327 | 0 | case WC_MGF1SHA3_384: |
1328 | 0 | ret = RsaMGF1(WC_HASH_TYPE_SHA3_384, seed, seedSz, out, outSz, |
1329 | 0 | heap); |
1330 | 0 | break; |
1331 | 0 | #endif |
1332 | 0 | #ifndef WOLFSSL_NOSHA3_512 |
1333 | 0 | case WC_MGF1SHA3_512: |
1334 | 0 | ret = RsaMGF1(WC_HASH_TYPE_SHA3_512, seed, seedSz, out, outSz, |
1335 | 0 | heap); |
1336 | 0 | break; |
1337 | 0 | #endif |
1338 | 0 | #endif /* WOLFSSL_SHA3 */ |
1339 | 0 | #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128) |
1340 | 0 | case WC_MGF1SHAKE128: |
1341 | 0 | ret = RsaMGF1(WC_HASH_TYPE_SHAKE128, seed, seedSz, out, outSz, |
1342 | 0 | heap); |
1343 | 0 | break; |
1344 | 0 | case WC_MGFSHAKE128: |
1345 | 0 | ret = RsaMGF_SHAKE(WC_HASH_TYPE_SHAKE128, seed, seedSz, out, outSz, |
1346 | 0 | heap); |
1347 | 0 | break; |
1348 | 0 | #endif |
1349 | 0 | #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256) |
1350 | 0 | case WC_MGF1SHAKE256: |
1351 | 0 | ret = RsaMGF1(WC_HASH_TYPE_SHAKE256, seed, seedSz, out, outSz, |
1352 | 0 | heap); |
1353 | 0 | break; |
1354 | 0 | case WC_MGFSHAKE256: |
1355 | 0 | ret = RsaMGF_SHAKE(WC_HASH_TYPE_SHAKE256, seed, seedSz, out, outSz, |
1356 | 0 | heap); |
1357 | 0 | break; |
1358 | 0 | #endif |
1359 | 0 | default: |
1360 | 0 | WOLFSSL_MSG("Unknown MGF type: check build options"); |
1361 | 0 | ret = BAD_FUNC_ARG; |
1362 | 0 | } |
1363 | | |
1364 | | /* in case of default avoid unused warning */ |
1365 | 0 | (void)seed; |
1366 | 0 | (void)seedSz; |
1367 | 0 | (void)out; |
1368 | 0 | (void)outSz; |
1369 | 0 | (void)heap; |
1370 | |
|
1371 | 0 | return ret; |
1372 | 0 | } |
1373 | | #endif /* !WC_NO_RSA_OAEP || WC_RSA_PSS */ |
1374 | | |
1375 | | |
1376 | | /* Padding */ |
1377 | | #ifndef WOLFSSL_RSA_VERIFY_ONLY |
1378 | | #ifndef WC_NO_RNG |
1379 | | #ifndef WC_NO_RSA_OAEP |
1380 | | static int RsaPad_OAEP(const byte* input, word32 inputLen, byte* pkcsBlock, |
1381 | | word32 pkcsBlockLen, byte padValue, WC_RNG* rng, |
1382 | | enum wc_HashType hType, int mgf, byte* optLabel, word32 labelLen, |
1383 | | void* heap) |
1384 | 0 | { |
1385 | 0 | int ret; |
1386 | 0 | word32 hLen; |
1387 | 0 | int psLen; |
1388 | 0 | word32 idx; |
1389 | |
|
1390 | | #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
1391 | | byte* dbMask = NULL; |
1392 | | byte* lHash = NULL; |
1393 | | byte* seed = NULL; |
1394 | | #else |
1395 | 0 | byte dbMask[RSA_MAX_SIZE/8 + RSA_PSS_PAD_SZ]; |
1396 | | /* must be large enough to contain largest hash */ |
1397 | 0 | byte lHash[WC_MAX_DIGEST_SIZE]; |
1398 | 0 | byte seed[WC_MAX_DIGEST_SIZE]; |
1399 | 0 | #endif |
1400 | | |
1401 | | /* no label is allowed, but catch if no label provided and length > 0 */ |
1402 | 0 | if (optLabel == NULL && labelLen > 0) { |
1403 | 0 | return BUFFER_E; |
1404 | 0 | } |
1405 | | |
1406 | | /* limit of label is the same as limit of hash function which is massive */ |
1407 | 0 | ret = wc_HashGetDigestSize(hType); |
1408 | 0 | if (ret < 0) { |
1409 | 0 | return ret; |
1410 | 0 | } |
1411 | 0 | hLen = (word32)ret; |
1412 | |
|
1413 | | #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
1414 | | lHash = (byte*)XMALLOC(hLen, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1415 | | if (lHash == NULL) { |
1416 | | return MEMORY_E; |
1417 | | } |
1418 | | seed = (byte*)XMALLOC(hLen, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1419 | | if (seed == NULL) { |
1420 | | XFREE(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1421 | | return MEMORY_E; |
1422 | | } |
1423 | | #else |
1424 | | /* hLen should never be larger than lHash since size is max digest size, |
1425 | | but check before blindly calling wc_Hash */ |
1426 | 0 | if (hLen > sizeof(lHash)) { |
1427 | 0 | WOLFSSL_MSG("OAEP lHash to small for digest!!"); |
1428 | 0 | return MEMORY_E; |
1429 | 0 | } |
1430 | 0 | #endif |
1431 | | |
1432 | 0 | if ((ret = wc_Hash(hType, optLabel, labelLen, lHash, hLen)) != 0) { |
1433 | 0 | WOLFSSL_MSG("OAEP hash type possibly not supported or lHash to small"); |
1434 | 0 | WC_FREE_VAR_EX(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1435 | 0 | WC_FREE_VAR_EX(seed, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1436 | 0 | return ret; |
1437 | 0 | } |
1438 | | |
1439 | | /* handles check of location for idx as well as psLen, cast to int to check |
1440 | | for pkcsBlockLen(k) - 2 * hLen - 2 being negative |
1441 | | This check is similar to decryption where k > 2 * hLen + 2 as msg |
1442 | | size approaches 0. In decryption if k is less than or equal -- then there |
1443 | | is no possible room for msg. |
1444 | | k = RSA key size |
1445 | | hLen = hash digest size -- will always be >= 0 at this point |
1446 | | */ |
1447 | 0 | if ((2 * hLen + 2) > pkcsBlockLen) { |
1448 | 0 | WOLFSSL_MSG("OAEP pad error hash to big for RSA key size"); |
1449 | 0 | WC_FREE_VAR_EX(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1450 | 0 | WC_FREE_VAR_EX(seed, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1451 | 0 | return BAD_FUNC_ARG; |
1452 | 0 | } |
1453 | | |
1454 | 0 | if (inputLen > (pkcsBlockLen - 2 * hLen - 2)) { |
1455 | 0 | WOLFSSL_MSG("OAEP pad error message too long"); |
1456 | 0 | WC_FREE_VAR_EX(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1457 | 0 | WC_FREE_VAR_EX(seed, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1458 | 0 | return BAD_FUNC_ARG; |
1459 | 0 | } |
1460 | | |
1461 | | /* concatenate lHash || PS || 0x01 || msg */ |
1462 | 0 | idx = pkcsBlockLen - 1 - inputLen; |
1463 | 0 | psLen = (int)pkcsBlockLen - (int)inputLen - 2 * (int)hLen - 2; |
1464 | 0 | if (pkcsBlockLen < inputLen) { /*make sure not writing over end of buffer */ |
1465 | 0 | WC_FREE_VAR_EX(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1466 | 0 | WC_FREE_VAR_EX(seed, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1467 | 0 | return BUFFER_E; |
1468 | 0 | } |
1469 | 0 | XMEMCPY(pkcsBlock + (pkcsBlockLen - inputLen), input, inputLen); |
1470 | 0 | pkcsBlock[idx--] = 0x01; /* PS and M separator */ |
1471 | 0 | XMEMSET(pkcsBlock + idx - psLen + 1, 0, (size_t)psLen); |
1472 | 0 | idx -= (word32)psLen; |
1473 | |
|
1474 | 0 | idx = idx - hLen + 1; |
1475 | 0 | XMEMCPY(pkcsBlock + idx, lHash, hLen); |
1476 | | |
1477 | | /* generate random seed */ |
1478 | 0 | if ((ret = wc_RNG_GenerateBlock(rng, seed, hLen)) != 0) { |
1479 | 0 | WC_FREE_VAR_EX(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1480 | 0 | ForceZero(seed, hLen); |
1481 | 0 | WC_FREE_VAR_EX(seed, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1482 | 0 | return ret; |
1483 | 0 | } |
1484 | | |
1485 | | #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
1486 | | /* create maskedDB from dbMask */ |
1487 | | dbMask = (byte*)XMALLOC(pkcsBlockLen - hLen - 1, heap, DYNAMIC_TYPE_RSA); |
1488 | | if (dbMask == NULL) { |
1489 | | |
1490 | | XFREE(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1491 | | ForceZero(seed, hLen); |
1492 | | XFREE(seed, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1493 | | return MEMORY_E; |
1494 | | } |
1495 | | #else |
1496 | 0 | if (pkcsBlockLen - hLen - 1 > sizeof(dbMask)) { |
1497 | 0 | return MEMORY_E; |
1498 | 0 | } |
1499 | 0 | #endif |
1500 | 0 | XMEMSET(dbMask, 0, pkcsBlockLen - hLen - 1); /* help static analyzer */ |
1501 | 0 | ret = RsaMGF(mgf, seed, hLen, dbMask, pkcsBlockLen - hLen - 1, heap); |
1502 | 0 | if (ret != 0) { |
1503 | 0 | WC_FREE_VAR_EX(dbMask, heap, DYNAMIC_TYPE_RSA); |
1504 | 0 | WC_FREE_VAR_EX(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1505 | 0 | ForceZero(seed, hLen); |
1506 | 0 | WC_FREE_VAR_EX(seed, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1507 | 0 | return ret; |
1508 | 0 | } |
1509 | | |
1510 | 0 | xorbuf(pkcsBlock + hLen + 1, dbMask,pkcsBlockLen - hLen - 1); |
1511 | |
|
1512 | 0 | WC_FREE_VAR_EX(dbMask, heap, DYNAMIC_TYPE_RSA); |
1513 | | |
1514 | | /* create maskedSeed from seedMask */ |
1515 | 0 | pkcsBlock[0] = 0x00; |
1516 | | /* create seedMask inline */ |
1517 | 0 | if ((ret = RsaMGF(mgf, pkcsBlock + hLen + 1, pkcsBlockLen - hLen - 1, |
1518 | 0 | pkcsBlock + 1, hLen, heap)) != 0) { |
1519 | 0 | WC_FREE_VAR_EX(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1520 | 0 | ForceZero(seed, hLen); |
1521 | 0 | WC_FREE_VAR_EX(seed, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1522 | 0 | return ret; |
1523 | 0 | } |
1524 | | |
1525 | | /* xor created seedMask with seed to make maskedSeed */ |
1526 | 0 | xorbuf(pkcsBlock + 1, seed, hLen); |
1527 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1528 | | /* Seed must be zeroized now that it has been used. */ |
1529 | | wc_MemZero_Add("Pad OAEP seed", seed, hLen); |
1530 | | #endif |
1531 | | |
1532 | | /* Zeroize masking bytes so that padding can't be unmasked. */ |
1533 | 0 | ForceZero(seed, hLen); |
1534 | | #ifdef WOLFSSL_SMALL_STACK |
1535 | | XFREE(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1536 | | XFREE(seed, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1537 | | #elif defined(WOLFSSL_CHECK_MEM_ZERO) |
1538 | | wc_MemZero_Check(seed, hLen); |
1539 | | #endif |
1540 | 0 | (void)padValue; |
1541 | |
|
1542 | 0 | return 0; |
1543 | 0 | } |
1544 | | #endif /* !WC_NO_RSA_OAEP */ |
1545 | | |
1546 | | #ifdef WC_RSA_PSS |
1547 | | |
1548 | | /* 0x00 .. 0x00 0x01 | Salt | Gen Hash | 0xbc |
1549 | | * XOR MGF over all bytes down to end of Salt |
1550 | | * Gen Hash = HASH(8 * 0x00 | Message Hash | Salt) |
1551 | | * |
1552 | | * input Digest of the message. |
1553 | | * inputLen Length of digest. |
1554 | | * pkcsBlock Buffer to write to. |
1555 | | * pkcsBlockLen Length of buffer to write to. |
1556 | | * rng Random number generator (for salt). |
1557 | | * htype Hash function to use. |
1558 | | * mgf Mask generation function. |
1559 | | * saltLen Length of salt to put in padding. |
1560 | | * bits Length of key in bits. |
1561 | | * heap Used for dynamic memory allocation. |
1562 | | * returns 0 on success, PSS_SALTLEN_E when the salt length is invalid |
1563 | | * and other negative values on error. |
1564 | | */ |
1565 | | static int RsaPad_PSS(const byte* input, word32 inputLen, byte* pkcsBlock, |
1566 | | word32 pkcsBlockLen, WC_RNG* rng, enum wc_HashType hType, int mgf, |
1567 | | int saltLen, int bits, void* heap) |
1568 | 0 | { |
1569 | 0 | int ret = 0; |
1570 | 0 | int hLen, o, maskLen, hiBits; |
1571 | 0 | byte* m; |
1572 | 0 | byte* s; |
1573 | | #if defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_STATIC_MEMORY) |
1574 | | byte msg[RSA_MAX_SIZE/8 + RSA_PSS_PAD_SZ]; |
1575 | | #else |
1576 | 0 | byte* msg = NULL; |
1577 | 0 | #endif |
1578 | 0 | #if defined(WOLFSSL_PSS_LONG_SALT) || defined(WOLFSSL_PSS_SALT_LEN_DISCOVER) |
1579 | 0 | byte* salt; |
1580 | | #else |
1581 | | byte salt[WC_MAX_DIGEST_SIZE]; |
1582 | | #endif |
1583 | |
|
1584 | 0 | #if defined(WOLFSSL_PSS_LONG_SALT) || defined(WOLFSSL_PSS_SALT_LEN_DISCOVER) |
1585 | 0 | if (pkcsBlockLen > RSA_MAX_SIZE/8) { |
1586 | 0 | return MEMORY_E; |
1587 | 0 | } |
1588 | 0 | #endif |
1589 | | |
1590 | 0 | hLen = wc_HashGetDigestSize(hType); |
1591 | 0 | if (hLen < 0) |
1592 | 0 | return hLen; |
1593 | 0 | if ((int)inputLen != hLen) { |
1594 | 0 | return BAD_FUNC_ARG; |
1595 | 0 | } |
1596 | | |
1597 | 0 | hiBits = (bits - 1) & 0x7; |
1598 | 0 | if (hiBits == 0) { |
1599 | | /* Per RFC8017, set the leftmost 8emLen - emBits bits of the |
1600 | | leftmost octet in DB to zero. |
1601 | | */ |
1602 | 0 | *(pkcsBlock++) = 0; |
1603 | 0 | pkcsBlockLen--; |
1604 | 0 | } |
1605 | |
|
1606 | 0 | if (saltLen == RSA_PSS_SALT_LEN_DEFAULT) { |
1607 | 0 | saltLen = hLen; |
1608 | 0 | #ifdef WOLFSSL_SHA512 |
1609 | | /* See FIPS 186-4 section 5.5 item (e). */ |
1610 | 0 | if (bits == 1024 && hLen == WC_SHA512_DIGEST_SIZE) { |
1611 | 0 | saltLen = RSA_PSS_SALT_MAX_SZ; |
1612 | 0 | } |
1613 | 0 | #endif |
1614 | 0 | } |
1615 | | /* The salt may not be longer than the hash. FIPS 186-5 sec 5.4(g) states |
1616 | | * this with no exception, so it holds even where long salts are compiled in. */ |
1617 | | #if !defined(WOLFSSL_PSS_LONG_SALT) || FIPS_VERSION3_GE(7,0,0) |
1618 | | else if (saltLen > hLen) { |
1619 | | return PSS_SALTLEN_E; |
1620 | | } |
1621 | | #endif |
1622 | 0 | #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER |
1623 | 0 | else if (saltLen < RSA_PSS_SALT_LEN_DEFAULT) { |
1624 | 0 | return PSS_SALTLEN_E; |
1625 | 0 | } |
1626 | | #else |
1627 | | else if (saltLen == RSA_PSS_SALT_LEN_DISCOVER) { |
1628 | | saltLen = (int)pkcsBlockLen - hLen - 2; |
1629 | | if (saltLen < 0) { |
1630 | | return PSS_SALTLEN_E; |
1631 | | } |
1632 | | #if FIPS_VERSION3_GE(7,0,0) |
1633 | | /* The sentinel is negative, so it slips past the cap above; the |
1634 | | * length derived from it is subject to the same limit. */ |
1635 | | if (saltLen > hLen) { |
1636 | | return PSS_SALTLEN_E; |
1637 | | } |
1638 | | #endif |
1639 | | } |
1640 | | else if (saltLen < RSA_PSS_SALT_LEN_DISCOVER) { |
1641 | | return PSS_SALTLEN_E; |
1642 | | } |
1643 | | #endif |
1644 | 0 | if ((int)pkcsBlockLen - hLen < saltLen + 2) { |
1645 | 0 | return PSS_SALTLEN_E; |
1646 | 0 | } |
1647 | 0 | maskLen = (int)pkcsBlockLen - 1 - hLen; |
1648 | |
|
1649 | 0 | #if defined(WOLFSSL_PSS_LONG_SALT) || defined(WOLFSSL_PSS_SALT_LEN_DISCOVER) |
1650 | 0 | #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY) |
1651 | 0 | msg = (byte*)XMALLOC( |
1652 | 0 | (size_t)(RSA_PSS_PAD_SZ + inputLen + (word32)saltLen), |
1653 | 0 | heap, DYNAMIC_TYPE_RSA_BUFFER); |
1654 | 0 | if (msg == NULL) { |
1655 | 0 | return MEMORY_E; |
1656 | 0 | } |
1657 | 0 | #endif |
1658 | 0 | salt = s = m = msg; |
1659 | 0 | XMEMSET(m, 0, RSA_PSS_PAD_SZ); |
1660 | 0 | m += RSA_PSS_PAD_SZ; |
1661 | 0 | XMEMCPY(m, input, inputLen); |
1662 | 0 | m += inputLen; |
1663 | 0 | o = (int)(m - s); |
1664 | 0 | if (saltLen > 0) { |
1665 | 0 | ret = wc_RNG_GenerateBlock(rng, m, (word32)saltLen); |
1666 | 0 | if (ret == 0) { |
1667 | 0 | m += saltLen; |
1668 | 0 | } |
1669 | 0 | } |
1670 | | #else |
1671 | | if ((int)pkcsBlockLen < RSA_PSS_PAD_SZ + (int)inputLen + saltLen) { |
1672 | | #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY) |
1673 | | msg = (byte*)XMALLOC( |
1674 | | (size_t)(RSA_PSS_PAD_SZ + inputLen + (word32)saltLen), |
1675 | | heap, DYNAMIC_TYPE_RSA_BUFFER); |
1676 | | if (msg == NULL) { |
1677 | | return MEMORY_E; |
1678 | | } |
1679 | | #endif |
1680 | | m = msg; |
1681 | | } |
1682 | | else { |
1683 | | m = pkcsBlock; |
1684 | | } |
1685 | | s = m; |
1686 | | XMEMSET(m, 0, RSA_PSS_PAD_SZ); |
1687 | | m += RSA_PSS_PAD_SZ; |
1688 | | XMEMCPY(m, input, inputLen); |
1689 | | m += inputLen; |
1690 | | o = 0; |
1691 | | if (saltLen > 0) { |
1692 | | ret = wc_RNG_GenerateBlock(rng, salt, (word32)saltLen); |
1693 | | if (ret == 0) { |
1694 | | XMEMCPY(m, salt, (size_t)saltLen); |
1695 | | m += saltLen; |
1696 | | } |
1697 | | } |
1698 | | #endif |
1699 | 0 | if (ret == 0) { |
1700 | | /* Put Hash at end of pkcsBlock - 1 */ |
1701 | 0 | ret = wc_Hash(hType, s, (word32)(m - s), pkcsBlock + maskLen, (word32)hLen); |
1702 | 0 | } |
1703 | 0 | if (ret == 0) { |
1704 | | /* Set the last eight bits or trailer field to the octet 0xbc */ |
1705 | 0 | pkcsBlock[pkcsBlockLen - 1] = RSA_PSS_PAD_TERM; |
1706 | |
|
1707 | 0 | ret = RsaMGF(mgf, pkcsBlock + maskLen, (word32)hLen, pkcsBlock, (word32)maskLen, heap); |
1708 | 0 | } |
1709 | 0 | if (ret == 0) { |
1710 | | /* Clear the first high bit when "8emLen - emBits" is non-zero. |
1711 | | where emBits = n modBits - 1 */ |
1712 | 0 | if (hiBits) |
1713 | 0 | pkcsBlock[0] &= (byte)((1 << hiBits) - 1); |
1714 | |
|
1715 | 0 | m = pkcsBlock + maskLen - saltLen - 1; |
1716 | 0 | *(m++) ^= 0x01; |
1717 | 0 | xorbuf(m, salt + o, (word32)saltLen); |
1718 | 0 | } |
1719 | |
|
1720 | 0 | #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY) |
1721 | | /* msg is always not NULL as we bail on allocation failure */ |
1722 | 0 | XFREE(msg, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1723 | 0 | #endif |
1724 | 0 | return ret; |
1725 | 0 | } |
1726 | | #endif /* WC_RSA_PSS */ |
1727 | | #endif /* !WC_NO_RNG */ |
1728 | | |
1729 | | static int RsaPad(const byte* input, word32 inputLen, byte* pkcsBlock, |
1730 | | word32 pkcsBlockLen, byte padValue, WC_RNG* rng) |
1731 | 0 | { |
1732 | 0 | if (input == NULL || inputLen == 0 || pkcsBlock == NULL || |
1733 | 0 | pkcsBlockLen == 0) { |
1734 | 0 | return BAD_FUNC_ARG; |
1735 | 0 | } |
1736 | | |
1737 | 0 | if (pkcsBlockLen - RSA_MIN_PAD_SZ < inputLen) { |
1738 | 0 | WOLFSSL_MSG("RsaPad error, invalid length"); |
1739 | 0 | return RSA_PAD_E; |
1740 | 0 | } |
1741 | 0 | pkcsBlock[0] = 0x0; /* set first byte to zero and advance */ |
1742 | 0 | pkcsBlock++; pkcsBlockLen--; |
1743 | 0 | pkcsBlock[0] = padValue; /* insert padValue */ |
1744 | |
|
1745 | 0 | if (padValue == RSA_BLOCK_TYPE_1) { |
1746 | | |
1747 | | /* pad with 0xff bytes */ |
1748 | 0 | XMEMSET(&pkcsBlock[1], 0xFF, pkcsBlockLen - inputLen - 2); |
1749 | 0 | } |
1750 | 0 | else { |
1751 | 0 | #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WC_NO_RNG) |
1752 | | /* pad with non-zero random bytes */ |
1753 | 0 | word32 padLen, i; |
1754 | 0 | int ret; |
1755 | 0 | padLen = pkcsBlockLen - inputLen - 1; |
1756 | 0 | ret = wc_RNG_GenerateBlock(rng, &pkcsBlock[1], padLen); |
1757 | 0 | if (ret != 0) { |
1758 | 0 | return ret; |
1759 | 0 | } |
1760 | | |
1761 | | /* remove zeros */ |
1762 | 0 | for (i = 1; i < padLen; i++) { |
1763 | 0 | if (pkcsBlock[i] == 0) pkcsBlock[i] = 0x01; |
1764 | 0 | } |
1765 | | #else |
1766 | | (void)rng; |
1767 | | return RSA_WRONG_TYPE_E; |
1768 | | #endif |
1769 | 0 | } |
1770 | | |
1771 | 0 | pkcsBlock[pkcsBlockLen-inputLen-1] = 0; /* separator */ |
1772 | 0 | XMEMCPY(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen); |
1773 | |
|
1774 | 0 | return 0; |
1775 | 0 | } |
1776 | | |
1777 | | /* helper function to direct which padding is used */ |
1778 | | int wc_RsaPad_ex(const byte* input, word32 inputLen, byte* pkcsBlock, |
1779 | | word32 pkcsBlockLen, byte padValue, WC_RNG* rng, int padType, |
1780 | | enum wc_HashType hType, int mgf, byte* optLabel, word32 labelLen, |
1781 | | int saltLen, int bits, void* heap) |
1782 | 0 | { |
1783 | 0 | int ret; |
1784 | |
|
1785 | 0 | switch (padType) |
1786 | 0 | { |
1787 | 0 | case WC_RSA_PKCSV15_PAD: |
1788 | | /*WOLFSSL_MSG("wolfSSL Using RSA PKCSV15 padding");*/ |
1789 | 0 | ret = RsaPad(input, inputLen, pkcsBlock, pkcsBlockLen, |
1790 | 0 | padValue, rng); |
1791 | 0 | break; |
1792 | | |
1793 | 0 | #ifndef WC_NO_RNG |
1794 | 0 | #ifndef WC_NO_RSA_OAEP |
1795 | 0 | case WC_RSA_OAEP_PAD: |
1796 | 0 | WOLFSSL_MSG("wolfSSL Using RSA OAEP padding"); |
1797 | 0 | ret = RsaPad_OAEP(input, inputLen, pkcsBlock, pkcsBlockLen, |
1798 | 0 | padValue, rng, hType, mgf, optLabel, labelLen, heap); |
1799 | 0 | break; |
1800 | 0 | #endif |
1801 | | |
1802 | 0 | #ifdef WC_RSA_PSS |
1803 | 0 | case WC_RSA_PSS_PAD: |
1804 | 0 | WOLFSSL_MSG("wolfSSL Using RSA PSS padding"); |
1805 | 0 | ret = RsaPad_PSS(input, inputLen, pkcsBlock, pkcsBlockLen, rng, |
1806 | 0 | hType, mgf, saltLen, bits, heap); |
1807 | 0 | break; |
1808 | 0 | #endif |
1809 | 0 | #endif /* !WC_NO_RNG */ |
1810 | | |
1811 | | #ifdef WC_RSA_NO_PADDING |
1812 | | case WC_RSA_NO_PAD: |
1813 | | { |
1814 | | int bytes = (bits + WOLFSSL_BIT_SIZE - 1) / WOLFSSL_BIT_SIZE; |
1815 | | |
1816 | | WOLFSSL_MSG("wolfSSL Using NO padding"); |
1817 | | |
1818 | | /* In the case of no padding being used check that input is exactly |
1819 | | * the RSA key length */ |
1820 | | if ((bits <= 0) || (inputLen != (word32)bytes)) { |
1821 | | WOLFSSL_MSG("Bad input size"); |
1822 | | ret = RSA_PAD_E; |
1823 | | } |
1824 | | else { |
1825 | | XMEMCPY(pkcsBlock, input, inputLen); |
1826 | | ret = 0; |
1827 | | } |
1828 | | break; |
1829 | | } |
1830 | | #endif |
1831 | | |
1832 | 0 | default: |
1833 | 0 | WOLFSSL_MSG("Unknown RSA Pad Type"); |
1834 | 0 | ret = RSA_PAD_E; |
1835 | 0 | } |
1836 | | |
1837 | | /* silence warning if not used with padding scheme */ |
1838 | 0 | (void)input; |
1839 | 0 | (void)inputLen; |
1840 | 0 | (void)pkcsBlock; |
1841 | 0 | (void)pkcsBlockLen; |
1842 | 0 | (void)padValue; |
1843 | 0 | (void)rng; |
1844 | 0 | (void)padType; |
1845 | 0 | (void)hType; |
1846 | 0 | (void)mgf; |
1847 | 0 | (void)optLabel; |
1848 | 0 | (void)labelLen; |
1849 | 0 | (void)saltLen; |
1850 | 0 | (void)bits; |
1851 | 0 | (void)heap; |
1852 | |
|
1853 | 0 | return ret; |
1854 | 0 | } |
1855 | | #endif /* WOLFSSL_RSA_VERIFY_ONLY */ |
1856 | | |
1857 | | |
1858 | | /* UnPadding */ |
1859 | | #if !defined(WC_NO_RSA_OAEP) && !defined(NO_HASH_WRAPPER) |
1860 | | /* UnPad plaintext, set start to *output, return length of plaintext, |
1861 | | * < 0 on error */ |
1862 | | static int RsaUnPad_OAEP(byte *pkcsBlock, unsigned int pkcsBlockLen, |
1863 | | byte **output, enum wc_HashType hType, int mgf, |
1864 | | byte* optLabel, word32 labelLen, void* heap) |
1865 | 0 | { |
1866 | 0 | word32 hLen; |
1867 | 0 | int ret; |
1868 | 0 | byte h[WC_MAX_DIGEST_SIZE]; /* max digest size */ |
1869 | 0 | word32 idx; |
1870 | 0 | word32 i; |
1871 | 0 | volatile word32 inc; |
1872 | |
|
1873 | | #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
1874 | | byte* tmp = NULL; |
1875 | | #else |
1876 | 0 | byte tmp[RSA_MAX_SIZE/8 + RSA_PSS_PAD_SZ]; |
1877 | 0 | #endif |
1878 | | |
1879 | | /* no label is allowed, but catch if no label provided and length > 0 */ |
1880 | 0 | if (optLabel == NULL && labelLen > 0) { |
1881 | 0 | return BUFFER_E; |
1882 | 0 | } |
1883 | | |
1884 | 0 | ret = wc_HashGetDigestSize(hType); |
1885 | 0 | if ((ret < 0) || (pkcsBlockLen < (2 * (word32)ret + 2))) { |
1886 | 0 | return BAD_FUNC_ARG; |
1887 | 0 | } |
1888 | 0 | hLen = (word32)ret; |
1889 | |
|
1890 | | #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
1891 | | tmp = (byte*)XMALLOC(pkcsBlockLen, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1892 | | if (tmp == NULL) { |
1893 | | return MEMORY_E; |
1894 | | } |
1895 | | #endif |
1896 | 0 | XMEMSET(tmp, 0, pkcsBlockLen); |
1897 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1898 | | wc_MemZero_Add("OAEP UnPad temp", tmp, pkcsBlockLen); |
1899 | | #endif |
1900 | | |
1901 | | /* find seedMask value */ |
1902 | 0 | ret = RsaMGF(mgf, (byte*)(pkcsBlock + (hLen + 1)), |
1903 | 0 | pkcsBlockLen - hLen - 1, tmp, hLen, heap); |
1904 | 0 | if (ret != 0) { |
1905 | 0 | WC_FREE_VAR_EX(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1906 | 0 | return ret; |
1907 | 0 | } |
1908 | | |
1909 | | /* xor seedMask value with maskedSeed to get seed value */ |
1910 | 0 | xorbuf(tmp, pkcsBlock + 1, hLen); |
1911 | | |
1912 | | /* get dbMask value */ |
1913 | 0 | ret = RsaMGF(mgf, tmp, hLen, tmp + hLen, pkcsBlockLen - hLen - 1, heap); |
1914 | 0 | if (ret != 0) { |
1915 | 0 | ForceZero(tmp, hLen); |
1916 | | #ifdef WOLFSSL_SMALL_STACK |
1917 | | XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1918 | | #elif defined(WOLFSSL_CHECK_MEM_ZERO) |
1919 | | wc_MemZero_Check(tmp, hLen); |
1920 | | #endif |
1921 | 0 | return ret; |
1922 | 0 | } |
1923 | | |
1924 | | /* get DB value by doing maskedDB xor dbMask */ |
1925 | 0 | xorbuf(pkcsBlock + hLen + 1, tmp + hLen, pkcsBlockLen - hLen - 1); |
1926 | |
|
1927 | 0 | ForceZero(tmp, pkcsBlockLen); |
1928 | | #ifdef WOLFSSL_SMALL_STACK |
1929 | | /* done with use of tmp buffer */ |
1930 | | XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER); |
1931 | | #elif defined(WOLFSSL_CHECK_MEM_ZERO) |
1932 | | wc_MemZero_Check(tmp, pkcsBlockLen); |
1933 | | #endif |
1934 | | |
1935 | | /* advance idx to index of PS and msg separator, account for PS size of 0*/ |
1936 | 0 | idx = hLen + 1 + hLen; |
1937 | | /* Don't reveal length of message: look at every byte. */ |
1938 | 0 | inc = 1; |
1939 | 0 | for (i = hLen + 1 + hLen; i < pkcsBlockLen - 1; i++) { |
1940 | | /* Looking for non-zero byte. */ |
1941 | 0 | inc &= 1 - (((word32)0 - pkcsBlock[i]) >> 31); |
1942 | 0 | idx += inc; |
1943 | 0 | } |
1944 | | |
1945 | | /* create hash of label for comparison with hash sent */ |
1946 | 0 | ret = wc_Hash(hType, optLabel, labelLen, h, hLen); |
1947 | 0 | if (ret != 0) { |
1948 | 0 | return ret; |
1949 | 0 | } |
1950 | | |
1951 | | /* say no to chosen ciphertext attack. |
1952 | | Comparison of lHash, Y, and separator value needs to all happen in |
1953 | | constant time. |
1954 | | Attackers should not be able to get error condition from the timing of |
1955 | | these checks. |
1956 | | */ |
1957 | 0 | { |
1958 | 0 | volatile int c = ConstantCompare(pkcsBlock + hLen + 1, h, (int)hLen); |
1959 | 0 | c = c + (pkcsBlock[idx++] ^ 0x01); /* separator value is 0x01 */ |
1960 | 0 | c = c + (pkcsBlock[0] ^ 0x00); /* Y, the first value, should be 0 */ |
1961 | | |
1962 | | /* Return 0 data length on error. */ |
1963 | 0 | idx = ctMaskSelWord32(ctMaskEq(c, 0), idx, pkcsBlockLen); |
1964 | 0 | } |
1965 | | |
1966 | | /* adjust pointer to correct location in array and return size of M */ |
1967 | 0 | *output = (byte*)(pkcsBlock + idx); |
1968 | 0 | return (int)(pkcsBlockLen - idx); |
1969 | 0 | } |
1970 | | #endif /* !WC_NO_RSA_OAEP */ |
1971 | | |
1972 | | #ifdef WC_RSA_PSS |
1973 | | /* 0x00 .. 0x00 0x01 | Salt | Gen Hash | 0xbc |
1974 | | * MGF over all bytes down to end of Salt |
1975 | | * |
1976 | | * pkcsBlock Buffer holding decrypted data. |
1977 | | * pkcsBlockLen Length of buffer. |
1978 | | * htype Hash function to use. |
1979 | | * mgf Mask generation function. |
1980 | | * saltLen Length of salt to put in padding. |
1981 | | * bits Length of key in bits. |
1982 | | * heap Used for dynamic memory allocation. |
1983 | | * returns the sum of salt length and SHA-256 digest size on success. |
1984 | | * Otherwise, PSS_SALTLEN_E for an incorrect salt length, |
1985 | | * WC_KEY_SIZE_E for an incorrect encoded message (EM) size |
1986 | | and other negative values on error. |
1987 | | */ |
1988 | | static int RsaUnPad_PSS(byte *pkcsBlock, unsigned int pkcsBlockLen, |
1989 | | byte **output, enum wc_HashType hType, int mgf, |
1990 | | int saltLen, int bits, void* heap) |
1991 | 0 | { |
1992 | 0 | int ret; |
1993 | 0 | byte* tmp; |
1994 | 0 | int hLen, i, maskLen; |
1995 | 0 | #ifdef WOLFSSL_SHA512 |
1996 | 0 | int orig_bits = bits; |
1997 | 0 | #endif |
1998 | | #if defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_STATIC_MEMORY) |
1999 | | byte tmp_buf[RSA_MAX_SIZE/8]; |
2000 | | tmp = tmp_buf; |
2001 | | |
2002 | | if (pkcsBlockLen > RSA_MAX_SIZE/8) { |
2003 | | return MEMORY_E; |
2004 | | } |
2005 | | #endif |
2006 | |
|
2007 | 0 | hLen = wc_HashGetDigestSize(hType); |
2008 | 0 | if (hLen < 0) |
2009 | 0 | return hLen; |
2010 | 0 | bits = (bits - 1) & 0x7; |
2011 | 0 | if ((pkcsBlock[0] & (0xff << bits)) != 0) { |
2012 | 0 | return BAD_PADDING_E; |
2013 | 0 | } |
2014 | 0 | if (bits == 0) { |
2015 | 0 | pkcsBlock++; |
2016 | 0 | pkcsBlockLen--; |
2017 | 0 | } |
2018 | 0 | maskLen = (int)pkcsBlockLen - 1 - hLen; |
2019 | 0 | if (maskLen < 0) { |
2020 | 0 | WOLFSSL_MSG("RsaUnPad_PSS: Hash too large"); |
2021 | 0 | return WC_KEY_SIZE_E; |
2022 | 0 | } |
2023 | | |
2024 | 0 | if (saltLen == RSA_PSS_SALT_LEN_DEFAULT) { |
2025 | 0 | saltLen = hLen; |
2026 | 0 | #ifdef WOLFSSL_SHA512 |
2027 | | /* See FIPS 186-4 section 5.5 item (e). */ |
2028 | 0 | if (orig_bits == 1024 && hLen == WC_SHA512_DIGEST_SIZE) |
2029 | 0 | saltLen = RSA_PSS_SALT_MAX_SZ; |
2030 | 0 | #endif |
2031 | 0 | } |
2032 | | /* Same salt limit when verifying: FIPS 186-5 sec 5.4(g) says the check |
2033 | | * "shall also be checked during the signature verification process". */ |
2034 | | #if !defined(WOLFSSL_PSS_LONG_SALT) || FIPS_VERSION3_GE(7,0,0) |
2035 | | else if (saltLen > hLen) |
2036 | | return PSS_SALTLEN_E; |
2037 | | #endif |
2038 | 0 | #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER |
2039 | 0 | else if (saltLen < RSA_PSS_SALT_LEN_DEFAULT) |
2040 | 0 | return PSS_SALTLEN_E; |
2041 | 0 | if (maskLen < saltLen + 1) { |
2042 | 0 | return PSS_SALTLEN_E; |
2043 | 0 | } |
2044 | | #else |
2045 | | else if (saltLen < RSA_PSS_SALT_LEN_DISCOVER) |
2046 | | return PSS_SALTLEN_E; |
2047 | | if (saltLen != RSA_PSS_SALT_LEN_DISCOVER && maskLen < saltLen + 1) { |
2048 | | return WC_KEY_SIZE_E; |
2049 | | } |
2050 | | #endif |
2051 | | |
2052 | 0 | if (pkcsBlock[pkcsBlockLen - 1] != RSA_PSS_PAD_TERM) { |
2053 | 0 | WOLFSSL_MSG("RsaUnPad_PSS: Padding Term Error"); |
2054 | 0 | return BAD_PADDING_E; |
2055 | 0 | } |
2056 | | |
2057 | 0 | #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY) |
2058 | 0 | tmp = (byte*)XMALLOC((size_t)maskLen, heap, DYNAMIC_TYPE_RSA_BUFFER); |
2059 | 0 | if (tmp == NULL) { |
2060 | 0 | return MEMORY_E; |
2061 | 0 | } |
2062 | 0 | XMEMSET(tmp, 0, (size_t)maskLen); |
2063 | 0 | #endif |
2064 | |
|
2065 | 0 | if ((ret = RsaMGF(mgf, pkcsBlock + maskLen, (word32)hLen, tmp, (word32)maskLen, |
2066 | 0 | heap)) != 0) { |
2067 | 0 | #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY) |
2068 | 0 | XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER); |
2069 | 0 | #endif |
2070 | 0 | return ret; |
2071 | 0 | } |
2072 | | |
2073 | | /* When bits==0, the modulus bit length is congruent to 1 mod 8, so |
2074 | | * the encoded block includes a leading 0x00 byte and pkcsBlock was |
2075 | | * already advanced past it (see above); no masking is needed. |
2076 | | * (1<<0)-1 == 0 would zero both bytes and corrupt the XOR separator |
2077 | | * check below. RsaPad_PSS guards the same step with "if (hiBits)" |
2078 | | * for the same reason. */ |
2079 | 0 | if (bits) { |
2080 | 0 | tmp[0] &= (byte)((1 << bits) - 1); |
2081 | 0 | pkcsBlock[0] &= (byte)((1 << bits) - 1); |
2082 | 0 | } |
2083 | | #ifdef WOLFSSL_PSS_SALT_LEN_DISCOVER |
2084 | | if (saltLen == RSA_PSS_SALT_LEN_DISCOVER) { |
2085 | | for (i = 0; i < maskLen - 1; i++) { |
2086 | | if (tmp[i] != pkcsBlock[i]) { |
2087 | | break; |
2088 | | } |
2089 | | } |
2090 | | if (tmp[i] != (pkcsBlock[i] ^ 0x01)) { |
2091 | | #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY) |
2092 | | XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER); |
2093 | | #endif |
2094 | | WOLFSSL_MSG("RsaUnPad_PSS: Padding Error Match"); |
2095 | | return PSS_SALTLEN_RECOVER_E; |
2096 | | } |
2097 | | saltLen = maskLen - (i + 1); |
2098 | | #if FIPS_VERSION3_GE(7,0,0) |
2099 | | /* When the length is discovered rather than supplied, it is this |
2100 | | * recovered value FIPS 186-5 sec 5.4(g) caps at the hash length. */ |
2101 | | if (saltLen > hLen) { |
2102 | | #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY) |
2103 | | XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER); |
2104 | | #endif |
2105 | | return PSS_SALTLEN_E; |
2106 | | } |
2107 | | #endif |
2108 | | } |
2109 | | else |
2110 | | #endif |
2111 | 0 | { |
2112 | 0 | for (i = 0; i < maskLen - 1 - saltLen; i++) { |
2113 | 0 | if (tmp[i] != pkcsBlock[i]) { |
2114 | 0 | #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY) |
2115 | 0 | XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER); |
2116 | 0 | #endif |
2117 | 0 | WOLFSSL_MSG("RsaUnPad_PSS: Padding Error Match"); |
2118 | 0 | return PSS_SALTLEN_E; |
2119 | 0 | } |
2120 | 0 | } |
2121 | 0 | if (tmp[i] != (pkcsBlock[i] ^ 0x01)) { |
2122 | 0 | #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY) |
2123 | 0 | XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER); |
2124 | 0 | #endif |
2125 | 0 | WOLFSSL_MSG("RsaUnPad_PSS: Padding Error End"); |
2126 | 0 | return PSS_SALTLEN_E; |
2127 | 0 | } |
2128 | 0 | } |
2129 | 0 | xorbuf(pkcsBlock + i, tmp + i, (word32)(maskLen - i)); |
2130 | |
|
2131 | 0 | #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY) |
2132 | 0 | XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER); |
2133 | 0 | #endif |
2134 | |
|
2135 | 0 | *output = pkcsBlock + maskLen - saltLen; |
2136 | 0 | return saltLen + hLen; |
2137 | 0 | } |
2138 | | #endif |
2139 | | |
2140 | | /* UnPad plaintext, set start to *output, return length of plaintext, |
2141 | | * < 0 on error */ |
2142 | | static int RsaUnPad(const byte *pkcsBlock, unsigned int pkcsBlockLen, |
2143 | | const byte **output, byte padValue) |
2144 | 0 | { |
2145 | 0 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
2146 | 0 | word16 i; |
2147 | |
|
2148 | 0 | if (output == NULL || pkcsBlockLen < 2 || pkcsBlockLen > 0xFFFF) { |
2149 | 0 | return BAD_FUNC_ARG; |
2150 | 0 | } |
2151 | | |
2152 | 0 | if (padValue == RSA_BLOCK_TYPE_1) { |
2153 | | /* First byte must be 0x00 and Second byte, block type, 0x01 */ |
2154 | 0 | if (pkcsBlock[0] != 0 || pkcsBlock[1] != RSA_BLOCK_TYPE_1) { |
2155 | 0 | WOLFSSL_MSG("RsaUnPad error, invalid formatting"); |
2156 | 0 | return RSA_PAD_E; |
2157 | 0 | } |
2158 | | |
2159 | | /* check the padding until we find the separator */ |
2160 | 0 | for (i = 2; i < pkcsBlockLen; ) { |
2161 | 0 | if (pkcsBlock[i++] != 0xFF) { |
2162 | 0 | break; |
2163 | 0 | } |
2164 | 0 | } |
2165 | | |
2166 | | /* Minimum of 11 bytes of pre-message data and must have separator. */ |
2167 | 0 | if (i < RSA_MIN_PAD_SZ || pkcsBlock[i-1] != 0) { |
2168 | 0 | WOLFSSL_MSG("RsaUnPad error, bad formatting"); |
2169 | 0 | return RSA_PAD_E; |
2170 | 0 | } |
2171 | | |
2172 | 0 | *output = (const byte *)(pkcsBlock + i); |
2173 | 0 | ret = (int)pkcsBlockLen - i; |
2174 | 0 | } |
2175 | 0 | #ifndef WOLFSSL_RSA_VERIFY_ONLY |
2176 | 0 | else { |
2177 | 0 | unsigned int j; |
2178 | 0 | volatile word16 pastSep = 0; |
2179 | 0 | volatile byte invalid = 0; |
2180 | 0 | volatile byte minPad; |
2181 | 0 | volatile int invalidMask; |
2182 | 0 | byte inv; |
2183 | 0 | word16 sep; |
2184 | |
|
2185 | 0 | i = 0; |
2186 | | /* Decrypted with private key - unpad must be constant time. */ |
2187 | 0 | for (j = 2; j < pkcsBlockLen; j++) { |
2188 | | /* Update i if not passed the separator and at separator. */ |
2189 | 0 | i |= (word16)(~pastSep) & ctMask16Eq(pkcsBlock[j], 0x00) & |
2190 | 0 | (word16)(j + 1); |
2191 | 0 | pastSep |= ctMask16Eq(pkcsBlock[j], 0x00); |
2192 | 0 | } |
2193 | | |
2194 | | /* Snapshot volatiles to avoid multiple volatile accesses per |
2195 | | * expression. */ |
2196 | 0 | inv = invalid; |
2197 | 0 | sep = pastSep; |
2198 | | |
2199 | | /* Minimum of 11 bytes of pre-message data - including leading 0x00. */ |
2200 | 0 | minPad = ctMaskLT(i, RSA_MIN_PAD_SZ); |
2201 | 0 | inv |= minPad; |
2202 | | /* Must have seen separator. */ |
2203 | 0 | inv |= (byte)~sep; |
2204 | | /* First byte must be 0x00. */ |
2205 | 0 | inv |= ctMaskNotEq(pkcsBlock[0], 0x00); |
2206 | | /* Check against expected block type: padValue */ |
2207 | 0 | inv |= ctMaskNotEq(pkcsBlock[1], padValue); |
2208 | |
|
2209 | 0 | invalid = inv; |
2210 | 0 | *output = (const byte *)(pkcsBlock + i); |
2211 | 0 | invalidMask = (int)-1 + (int)(inv >> 7); |
2212 | 0 | ret = invalidMask & ((int)pkcsBlockLen - i); |
2213 | 0 | } |
2214 | 0 | #endif |
2215 | | |
2216 | 0 | return ret; |
2217 | 0 | } |
2218 | | |
2219 | | /* helper function to direct unpadding |
2220 | | * |
2221 | | * bits is the key modulus size in bits |
2222 | | */ |
2223 | | int wc_RsaUnPad_ex(byte* pkcsBlock, word32 pkcsBlockLen, byte** out, |
2224 | | byte padValue, int padType, enum wc_HashType hType, |
2225 | | int mgf, byte* optLabel, word32 labelLen, int saltLen, |
2226 | | int bits, void* heap) |
2227 | 0 | { |
2228 | 0 | int ret; |
2229 | |
|
2230 | 0 | switch (padType) { |
2231 | 0 | case WC_RSA_PKCSV15_PAD: |
2232 | | /*WOLFSSL_MSG("wolfSSL Using RSA PKCSV15 un-padding");*/ |
2233 | 0 | ret = RsaUnPad(pkcsBlock, pkcsBlockLen, (const byte **)(void *)out, |
2234 | 0 | padValue); |
2235 | 0 | break; |
2236 | | |
2237 | 0 | #ifndef WC_NO_RSA_OAEP |
2238 | 0 | case WC_RSA_OAEP_PAD: |
2239 | 0 | WOLFSSL_MSG("wolfSSL Using RSA OAEP un-padding"); |
2240 | 0 | ret = RsaUnPad_OAEP((byte*)pkcsBlock, pkcsBlockLen, out, |
2241 | 0 | hType, mgf, optLabel, labelLen, heap); |
2242 | 0 | break; |
2243 | 0 | #endif |
2244 | | |
2245 | 0 | #ifdef WC_RSA_PSS |
2246 | 0 | case WC_RSA_PSS_PAD: |
2247 | 0 | WOLFSSL_MSG("wolfSSL Using RSA PSS un-padding"); |
2248 | 0 | ret = RsaUnPad_PSS((byte*)pkcsBlock, pkcsBlockLen, out, hType, mgf, |
2249 | 0 | saltLen, bits, heap); |
2250 | 0 | break; |
2251 | 0 | #endif |
2252 | | |
2253 | | #ifdef WC_RSA_NO_PADDING |
2254 | | case WC_RSA_NO_PAD: |
2255 | | WOLFSSL_MSG("wolfSSL Using NO un-padding"); |
2256 | | |
2257 | | /* In the case of no padding being used check that input is exactly |
2258 | | * the RSA key length */ |
2259 | | if (bits <= 0 || pkcsBlockLen != |
2260 | | ((word32)(bits+WOLFSSL_BIT_SIZE-1)/WOLFSSL_BIT_SIZE)) { |
2261 | | WOLFSSL_MSG("Bad input size"); |
2262 | | ret = RSA_PAD_E; |
2263 | | } |
2264 | | else { |
2265 | | if (out != NULL) { |
2266 | | *out = pkcsBlock; |
2267 | | } |
2268 | | ret = (int)pkcsBlockLen; |
2269 | | } |
2270 | | break; |
2271 | | #endif /* WC_RSA_NO_PADDING */ |
2272 | | |
2273 | 0 | default: |
2274 | 0 | WOLFSSL_MSG("Unknown RSA UnPad Type"); |
2275 | 0 | ret = RSA_PAD_E; |
2276 | 0 | } |
2277 | | |
2278 | | /* silence warning if not used with padding scheme */ |
2279 | 0 | (void)hType; |
2280 | 0 | (void)mgf; |
2281 | 0 | (void)optLabel; |
2282 | 0 | (void)labelLen; |
2283 | 0 | (void)saltLen; |
2284 | 0 | (void)bits; |
2285 | 0 | (void)heap; |
2286 | |
|
2287 | 0 | return ret; |
2288 | 0 | } |
2289 | | |
2290 | | #if defined(HAVE_FIPS) && \ |
2291 | | !defined(WOLFSSL_FIPS_READY) && !defined(WOLFSSL_FIPS_DEV) |
2292 | | PRAGMA_DIAG_PUSH |
2293 | | PRAGMA("GCC diagnostic ignored \"-Wswitch-enum\"") |
2294 | | #endif |
2295 | | |
2296 | | int wc_hash2mgf(enum wc_HashType hType) |
2297 | 0 | { |
2298 | 0 | switch (hType) { |
2299 | 0 | case WC_HASH_TYPE_NONE: |
2300 | 0 | return WC_MGF1NONE; |
2301 | 0 | case WC_HASH_TYPE_SHA: |
2302 | 0 | #ifndef NO_SHA |
2303 | 0 | return WC_MGF1SHA1; |
2304 | | #else |
2305 | | break; |
2306 | | #endif |
2307 | 0 | case WC_HASH_TYPE_SHA224: |
2308 | 0 | #ifdef WOLFSSL_SHA224 |
2309 | 0 | return WC_MGF1SHA224; |
2310 | | #else |
2311 | | break; |
2312 | | #endif |
2313 | 0 | case WC_HASH_TYPE_SHA256: |
2314 | 0 | #ifndef NO_SHA256 |
2315 | 0 | return WC_MGF1SHA256; |
2316 | | #else |
2317 | | break; |
2318 | | #endif |
2319 | 0 | case WC_HASH_TYPE_SHA384: |
2320 | 0 | #ifdef WOLFSSL_SHA384 |
2321 | 0 | return WC_MGF1SHA384; |
2322 | | #else |
2323 | | break; |
2324 | | #endif |
2325 | 0 | case WC_HASH_TYPE_SHA512: |
2326 | 0 | #ifdef WOLFSSL_SHA512 |
2327 | 0 | return WC_MGF1SHA512; |
2328 | | #else |
2329 | | break; |
2330 | | #endif |
2331 | 0 | case WC_HASH_TYPE_SHA512_224: |
2332 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) |
2333 | 0 | return WC_MGF1SHA512_224; |
2334 | | #else |
2335 | | break; |
2336 | | #endif |
2337 | 0 | case WC_HASH_TYPE_SHA512_256: |
2338 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) |
2339 | 0 | return WC_MGF1SHA512_256; |
2340 | | #else |
2341 | | break; |
2342 | | #endif |
2343 | 0 | case WC_HASH_TYPE_MD2: |
2344 | 0 | case WC_HASH_TYPE_MD4: |
2345 | 0 | case WC_HASH_TYPE_MD5: |
2346 | 0 | case WC_HASH_TYPE_MD5_SHA: |
2347 | 0 | case WC_HASH_TYPE_SHA3_224: |
2348 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) |
2349 | 0 | return WC_MGF1SHA3_224; |
2350 | | #else |
2351 | | break; |
2352 | | #endif |
2353 | 0 | case WC_HASH_TYPE_SHA3_256: |
2354 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) |
2355 | 0 | return WC_MGF1SHA3_256; |
2356 | | #else |
2357 | | break; |
2358 | | #endif |
2359 | 0 | case WC_HASH_TYPE_SHA3_384: |
2360 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) |
2361 | 0 | return WC_MGF1SHA3_384; |
2362 | | #else |
2363 | | break; |
2364 | | #endif |
2365 | 0 | case WC_HASH_TYPE_SHA3_512: |
2366 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) |
2367 | 0 | return WC_MGF1SHA3_512; |
2368 | | #else |
2369 | | break; |
2370 | | #endif |
2371 | 0 | case WC_HASH_TYPE_BLAKE2B: |
2372 | 0 | case WC_HASH_TYPE_BLAKE2S: |
2373 | 0 | case WC_HASH_TYPE_SM3: |
2374 | 0 | break; |
2375 | 0 | #ifdef WOLFSSL_SHAKE128 |
2376 | 0 | case WC_HASH_TYPE_SHAKE128: |
2377 | 0 | return WC_MGF1SHAKE128; |
2378 | | #else |
2379 | | case WC_HASH_TYPE_SHAKE128: |
2380 | | break; |
2381 | | #endif |
2382 | 0 | #ifdef WOLFSSL_SHAKE256 |
2383 | 0 | case WC_HASH_TYPE_SHAKE256: |
2384 | 0 | return WC_MGF1SHAKE256; |
2385 | | #else |
2386 | | case WC_HASH_TYPE_SHAKE256: |
2387 | | break; |
2388 | | #endif |
2389 | 0 | default: |
2390 | 0 | break; |
2391 | 0 | } |
2392 | 0 | WOLFSSL_MSG("Unrecognized or unsupported hash function"); |
2393 | 0 | return WC_MGF1NONE; |
2394 | 0 | } |
2395 | | |
2396 | | #if defined(HAVE_FIPS) && \ |
2397 | | !defined(WOLFSSL_FIPS_READY) && !defined(WOLFSSL_FIPS_DEV) |
2398 | | PRAGMA_DIAG_POP |
2399 | | #endif |
2400 | | |
2401 | | #ifdef WC_RSA_NONBLOCK |
2402 | | static int wc_RsaFunctionNonBlock(const byte* in, word32 inLen, byte* out, |
2403 | | word32* outLen, int type, RsaKey* key) |
2404 | | { |
2405 | | int ret = 0; |
2406 | | #ifdef USE_FAST_MATH |
2407 | | word32 keyLen, len; |
2408 | | #endif |
2409 | | /* SP non-blocking RSA wrappers depend on sp_<N>_mod_exp_<W>_nb, |
2410 | | * which the SP generator only emits when (!RSA_PUBLIC_ONLY || |
2411 | | * HAVE_SP_DH). Match that gate here so the dispatch is omitted when |
2412 | | * those symbols are not available. */ |
2413 | | #if defined(WOLFSSL_HAVE_SP_RSA) && defined(WOLFSSL_SP_NONBLOCK) && \ |
2414 | | defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) && \ |
2415 | | (!defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_HAVE_SP_DH)) |
2416 | | int bits; |
2417 | | #endif |
2418 | | |
2419 | | if (key == NULL || key->nb == NULL) { |
2420 | | return BAD_FUNC_ARG; |
2421 | | } |
2422 | | |
2423 | | #if defined(WOLFSSL_HAVE_SP_RSA) && defined(WOLFSSL_SP_NONBLOCK) && \ |
2424 | | defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) && \ |
2425 | | (!defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_HAVE_SP_DH)) |
2426 | | bits = mp_count_bits(&key->n); |
2427 | | #ifndef WOLFSSL_SP_NO_2048 |
2428 | | if (bits == 2048) { |
2429 | | if (type == RSA_PUBLIC_ENCRYPT || type == RSA_PUBLIC_DECRYPT) { |
2430 | | return sp_RsaPublic_2048_nb(&key->nb->sp_ctx, in, inLen, |
2431 | | &key->e, &key->n, out, outLen); |
2432 | | } |
2433 | | #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ |
2434 | | (defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM)) |
2435 | | return sp_RsaPrivate_2048_nb(&key->nb->sp_ctx, in, inLen, |
2436 | | &key->d, &key->n, out, outLen); |
2437 | | #endif |
2438 | | } |
2439 | | #endif |
2440 | | #ifndef WOLFSSL_SP_NO_3072 |
2441 | | if (bits == 3072) { |
2442 | | if (type == RSA_PUBLIC_ENCRYPT || type == RSA_PUBLIC_DECRYPT) { |
2443 | | return sp_RsaPublic_3072_nb(&key->nb->sp_ctx, in, inLen, |
2444 | | &key->e, &key->n, out, outLen); |
2445 | | } |
2446 | | #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ |
2447 | | (defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM)) |
2448 | | return sp_RsaPrivate_3072_nb(&key->nb->sp_ctx, in, inLen, |
2449 | | &key->d, &key->n, out, outLen); |
2450 | | #endif |
2451 | | } |
2452 | | #endif |
2453 | | #ifdef WOLFSSL_SP_4096 |
2454 | | if (bits == 4096) { |
2455 | | if (type == RSA_PUBLIC_ENCRYPT || type == RSA_PUBLIC_DECRYPT) { |
2456 | | return sp_RsaPublic_4096_nb(&key->nb->sp_ctx, in, inLen, |
2457 | | &key->e, &key->n, out, outLen); |
2458 | | } |
2459 | | #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ |
2460 | | (defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM)) |
2461 | | return sp_RsaPrivate_4096_nb(&key->nb->sp_ctx, in, inLen, |
2462 | | &key->d, &key->n, out, outLen); |
2463 | | #endif |
2464 | | } |
2465 | | #endif |
2466 | | #endif /* SP nonblock RSA */ |
2467 | | |
2468 | | #ifdef USE_FAST_MATH |
2469 | | if (key->nb->exptmod.state == TFM_EXPTMOD_NB_INIT) { |
2470 | | if (mp_init(&key->nb->tmp) != MP_OKAY) { |
2471 | | ret = MP_INIT_E; |
2472 | | } |
2473 | | |
2474 | | if (ret == 0) { |
2475 | | if (mp_read_unsigned_bin(&key->nb->tmp, (byte*)in, inLen) != MP_OKAY) { |
2476 | | ret = MP_READ_E; |
2477 | | } |
2478 | | } |
2479 | | } |
2480 | | |
2481 | | if (ret == 0) { |
2482 | | switch(type) { |
2483 | | #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) |
2484 | | case RSA_PRIVATE_DECRYPT: |
2485 | | case RSA_PRIVATE_ENCRYPT: |
2486 | | ret = fp_exptmod_nb(&key->nb->exptmod, &key->nb->tmp, &key->d, |
2487 | | &key->n, &key->nb->tmp); |
2488 | | if (ret == FP_WOULDBLOCK) |
2489 | | return ret; |
2490 | | if (ret != MP_OKAY) |
2491 | | ret = MP_EXPTMOD_E; |
2492 | | break; |
2493 | | #endif |
2494 | | case RSA_PUBLIC_ENCRYPT: |
2495 | | case RSA_PUBLIC_DECRYPT: |
2496 | | ret = fp_exptmod_nb(&key->nb->exptmod, &key->nb->tmp, &key->e, |
2497 | | &key->n, &key->nb->tmp); |
2498 | | if (ret == FP_WOULDBLOCK) |
2499 | | return ret; |
2500 | | if (ret != MP_OKAY) |
2501 | | ret = MP_EXPTMOD_E; |
2502 | | break; |
2503 | | default: |
2504 | | ret = RSA_WRONG_TYPE_E; |
2505 | | break; |
2506 | | } |
2507 | | } |
2508 | | |
2509 | | if (ret == 0) { |
2510 | | keyLen = wc_RsaEncryptSize(key); |
2511 | | if (keyLen > *outLen) |
2512 | | ret = RSA_BUFFER_E; |
2513 | | } |
2514 | | if (ret == 0) { |
2515 | | len = mp_unsigned_bin_size(&key->nb->tmp); |
2516 | | |
2517 | | /* pad front w/ zeros to match key length */ |
2518 | | while (len < keyLen) { |
2519 | | *out++ = 0x00; |
2520 | | len++; |
2521 | | } |
2522 | | |
2523 | | *outLen = keyLen; |
2524 | | |
2525 | | /* convert */ |
2526 | | if (mp_to_unsigned_bin(&key->nb->tmp, out) != MP_OKAY) { |
2527 | | ret = MP_TO_E; |
2528 | | } |
2529 | | } |
2530 | | |
2531 | | mp_clear(&key->nb->tmp); |
2532 | | #else |
2533 | | /* No non-blocking backend available for this build. The SP non-block |
2534 | | * dispatch above only matches enabled key sizes; if we reach this |
2535 | | * point the key is not 2048/3072/4096 (or SP RSA itself isn't built) |
2536 | | * and TFM fastmath isn't compiled in either. */ |
2537 | | (void)in; |
2538 | | (void)inLen; |
2539 | | (void)out; |
2540 | | (void)outLen; |
2541 | | (void)type; |
2542 | | ret = NOT_COMPILED_IN; |
2543 | | #endif /* USE_FAST_MATH */ |
2544 | | |
2545 | | return ret; |
2546 | | } |
2547 | | #endif /* WC_RSA_NONBLOCK */ |
2548 | | |
2549 | | #ifdef WOLFSSL_XILINX_CRYPT |
2550 | | /* |
2551 | | * Xilinx hardened crypto acceleration. |
2552 | | * |
2553 | | * Returns 0 on success and negative values on error. |
2554 | | */ |
2555 | | static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out, |
2556 | | word32* outLen, int type, RsaKey* key, WC_RNG* rng) |
2557 | | { |
2558 | | int ret = 0; |
2559 | | word32 keyLen; |
2560 | | (void)rng; |
2561 | | |
2562 | | keyLen = wc_RsaEncryptSize(key); |
2563 | | if (keyLen > *outLen) { |
2564 | | WOLFSSL_MSG("Output buffer is not big enough"); |
2565 | | return BAD_FUNC_ARG; |
2566 | | } |
2567 | | |
2568 | | if (inLen != keyLen) { |
2569 | | WOLFSSL_MSG("Expected that inLen equals RSA key length"); |
2570 | | return BAD_FUNC_ARG; |
2571 | | } |
2572 | | |
2573 | | switch(type) { |
2574 | | case RSA_PRIVATE_DECRYPT: |
2575 | | case RSA_PRIVATE_ENCRYPT: |
2576 | | #ifdef WOLFSSL_XILINX_CRYPTO_OLD |
2577 | | /* Currently public exponent is loaded by default. |
2578 | | * In SDK 2017.1 RSA exponent values are expected to be of 4 bytes |
2579 | | * leading to private key operations with Xsecure_RsaDecrypt not being |
2580 | | * supported */ |
2581 | | ret = RSA_WRONG_TYPE_E; |
2582 | | #else |
2583 | | { |
2584 | | byte *d; |
2585 | | int dSz; |
2586 | | #if !defined(WOLFSSL_XILINX_CRYPT_VERSAL) |
2587 | | XSecure_Rsa rsa; |
2588 | | #endif |
2589 | | |
2590 | | #if defined(WOLFSSL_XILINX_CRYPT_VERSAL) |
2591 | | dSz = WOLFSSL_XSECURE_RSA_KEY_SIZE * 2; |
2592 | | #else |
2593 | | dSz = mp_unsigned_bin_size(&key->d); |
2594 | | #endif |
2595 | | d = (byte*)XMALLOC(dSz, key->heap, DYNAMIC_TYPE_PRIVATE_KEY); |
2596 | | if (d == NULL) { |
2597 | | ret = MEMORY_E; |
2598 | | } else { |
2599 | | #if defined(WOLFSSL_XILINX_CRYPT_VERSAL) |
2600 | | XMEMSET(d, 0, dSz); |
2601 | | XMEMCPY(d, key->mod, key->mSz); |
2602 | | ret = mp_to_unsigned_bin(&key->d, &d[WOLFSSL_XSECURE_RSA_KEY_SIZE]); |
2603 | | #else |
2604 | | ret = mp_to_unsigned_bin(&key->d, d); |
2605 | | XSecure_RsaInitialize(&rsa, key->mod, NULL, d); |
2606 | | #endif |
2607 | | } |
2608 | | |
2609 | | if (ret == 0) { |
2610 | | #if defined(WOLFSSL_XILINX_CRYPT_VERSAL) |
2611 | | WOLFSSL_XIL_DCACHE_FLUSH_RANGE((UINTPTR)d, dSz); |
2612 | | WOLFSSL_XIL_DCACHE_FLUSH_RANGE((UINTPTR)in, inLen); |
2613 | | if (XSecure_RsaPrivateDecrypt(&(key->xSec.cinst), XIL_CAST_U64(d), |
2614 | | XIL_CAST_U64(in), inLen, |
2615 | | XIL_CAST_U64(out)) != XST_SUCCESS) { |
2616 | | ret = BAD_STATE_E; |
2617 | | } |
2618 | | WOLFSSL_XIL_DCACHE_FLUSH_RANGE((UINTPTR)out, inLen); |
2619 | | #else |
2620 | | if (XSecure_RsaPrivateDecrypt(&rsa, (u8*)in, inLen, out) != |
2621 | | XST_SUCCESS) { |
2622 | | ret = BAD_STATE_E; |
2623 | | } |
2624 | | #endif |
2625 | | } |
2626 | | |
2627 | | if (d != NULL) |
2628 | | ForceZero(d, dSz); |
2629 | | XFREE(d, key->heap, DYNAMIC_TYPE_PRIVATE_KEY); |
2630 | | } |
2631 | | #endif |
2632 | | break; |
2633 | | case RSA_PUBLIC_ENCRYPT: |
2634 | | case RSA_PUBLIC_DECRYPT: |
2635 | | #if defined(WOLFSSL_XILINX_CRYPT_VERSAL) |
2636 | | WOLFSSL_XIL_DCACHE_FLUSH_RANGE((UINTPTR)key->mod, |
2637 | | WOLFSSL_XSECURE_RSA_KEY_SIZE + 4); |
2638 | | WOLFSSL_XIL_DCACHE_FLUSH_RANGE((UINTPTR)in, inLen); |
2639 | | if (XSecure_RsaPublicEncrypt(&(key->xSec.cinst), |
2640 | | XIL_CAST_U64(key->mod), |
2641 | | XIL_CAST_U64(in), inLen, |
2642 | | XIL_CAST_U64(out))) { |
2643 | | WOLFSSL_MSG("RSA public operation failed"); |
2644 | | ret = BAD_STATE_E; |
2645 | | } |
2646 | | WOLFSSL_XIL_DCACHE_FLUSH_RANGE((UINTPTR)out, inLen); |
2647 | | #elif defined(WOLFSSL_XILINX_CRYPTO_OLD) |
2648 | | if (XSecure_RsaDecrypt(&(key->xRsa), in, out) != XST_SUCCESS) { |
2649 | | ret = BAD_STATE_E; |
2650 | | } |
2651 | | #else |
2652 | | /* starting at Xilinx release 2019 the function XSecure_RsaDecrypt was removed */ |
2653 | | if (XSecure_RsaPublicEncrypt(&(key->xRsa), (u8*)in, inLen, out) != XST_SUCCESS) { |
2654 | | WOLFSSL_MSG("Error happened when calling hardware RSA public operation"); |
2655 | | ret = BAD_STATE_E; |
2656 | | } |
2657 | | #endif |
2658 | | break; |
2659 | | default: |
2660 | | ret = RSA_WRONG_TYPE_E; |
2661 | | } |
2662 | | |
2663 | | *outLen = keyLen; |
2664 | | |
2665 | | return ret; |
2666 | | } |
2667 | | |
2668 | | #elif defined(WOLFSSL_AFALG_XILINX_RSA) |
2669 | | #ifndef ERROR_OUT |
2670 | | #define ERROR_OUT(x) ret = (x); goto done |
2671 | | #endif |
2672 | | |
2673 | | static const char WC_TYPE_ASYMKEY[] = "skcipher"; |
2674 | | static const char WC_NAME_RSA[] = "xilinx-zynqmp-rsa"; |
2675 | | #ifndef MAX_XILINX_RSA_KEY |
2676 | | /* max key size of 4096 bits / 512 bytes */ |
2677 | | #define MAX_XILINX_RSA_KEY 512 |
2678 | | #endif |
2679 | | static const byte XILINX_RSA_FLAG[] = {0x1}; |
2680 | | |
2681 | | |
2682 | | /* AF_ALG implementation of RSA */ |
2683 | | static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out, |
2684 | | word32* outLen, int type, RsaKey* key, WC_RNG* rng) |
2685 | | { |
2686 | | struct msghdr msg; |
2687 | | struct cmsghdr* cmsg; |
2688 | | struct iovec iov; |
2689 | | byte* keyBuf = NULL; |
2690 | | word32 keyBufSz = 0; |
2691 | | char cbuf[CMSG_SPACE(4) + CMSG_SPACE(sizeof(struct af_alg_iv) + 1)] = {0}; |
2692 | | int ret = 0; |
2693 | | int op = 0; /* decryption vs encryption flag */ |
2694 | | word32 keyLen; |
2695 | | |
2696 | | /* input and output buffer need to be aligned */ |
2697 | | ALIGN64 byte outBuf[MAX_XILINX_RSA_KEY]; |
2698 | | ALIGN64 byte inBuf[MAX_XILINX_RSA_KEY]; |
2699 | | |
2700 | | XMEMSET(&msg, 0, sizeof(struct msghdr)); |
2701 | | (void)rng; |
2702 | | |
2703 | | keyLen = wc_RsaEncryptSize(key); |
2704 | | if (keyLen > *outLen) { |
2705 | | ERROR_OUT(RSA_BUFFER_E); |
2706 | | } |
2707 | | |
2708 | | if (keyLen > MAX_XILINX_RSA_KEY) { |
2709 | | WOLFSSL_MSG("RSA key size larger than supported"); |
2710 | | ERROR_OUT(BAD_FUNC_ARG); |
2711 | | } |
2712 | | |
2713 | | if (inLen != keyLen) { |
2714 | | WOLFSSL_MSG("Expected that inLen equals RSA key length"); |
2715 | | ERROR_OUT(BAD_FUNC_ARG); |
2716 | | } |
2717 | | |
2718 | | if ((keyBuf = (byte*)XMALLOC(keyLen * 2, key->heap, DYNAMIC_TYPE_KEY)) |
2719 | | == NULL) { |
2720 | | ERROR_OUT(MEMORY_E); |
2721 | | } |
2722 | | |
2723 | | if ((ret = mp_to_unsigned_bin(&(key->n), keyBuf)) != MP_OKAY) { |
2724 | | ERROR_OUT(MP_TO_E); |
2725 | | } |
2726 | | |
2727 | | switch(type) { |
2728 | | case RSA_PRIVATE_DECRYPT: |
2729 | | case RSA_PRIVATE_ENCRYPT: |
2730 | | op = 1; /* set as decrypt */ |
2731 | | { |
2732 | | keyBufSz = mp_unsigned_bin_size(&(key->d)); |
2733 | | if ((mp_to_unsigned_bin(&(key->d), keyBuf + keyLen)) |
2734 | | != MP_OKAY) { |
2735 | | ERROR_OUT(MP_TO_E); |
2736 | | } |
2737 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2738 | | /* Seed must be zeroized now that it has been used. */ |
2739 | | wc_MemZero_Add("RSA Sync Priv Enc/Dec keyBuf", keyBuf + keyLen, |
2740 | | keyBufSz); |
2741 | | #endif |
2742 | | } |
2743 | | break; |
2744 | | |
2745 | | case RSA_PUBLIC_DECRYPT: |
2746 | | case RSA_PUBLIC_ENCRYPT: { |
2747 | | word32 exp = 0; |
2748 | | word32 eSz = mp_unsigned_bin_size(&(key->e)); |
2749 | | if ((mp_to_unsigned_bin(&(key->e), (byte*)&exp + |
2750 | | (sizeof(word32) - eSz))) != MP_OKAY) { |
2751 | | ERROR_OUT(MP_TO_E); |
2752 | | } |
2753 | | keyBufSz = sizeof(word32); |
2754 | | XMEMCPY(keyBuf + keyLen, (byte*)&exp, keyBufSz); |
2755 | | break; |
2756 | | } |
2757 | | |
2758 | | default: |
2759 | | ERROR_OUT(RSA_WRONG_TYPE_E); |
2760 | | } |
2761 | | keyBufSz += keyLen; /* add size of modulus */ |
2762 | | |
2763 | | /* check for existing sockets before creating new ones */ |
2764 | | if (key->alFd > 0) { |
2765 | | close(key->alFd); |
2766 | | key->alFd = WC_SOCK_NOTSET; |
2767 | | } |
2768 | | if (key->rdFd > 0) { |
2769 | | close(key->rdFd); |
2770 | | key->rdFd = WC_SOCK_NOTSET; |
2771 | | } |
2772 | | |
2773 | | /* create new sockets and set the key to use */ |
2774 | | if ((key->alFd = wc_Afalg_Socket()) < 0) { |
2775 | | WOLFSSL_MSG("Unable to create socket"); |
2776 | | ERROR_OUT(key->alFd); |
2777 | | } |
2778 | | if ((key->rdFd = wc_Afalg_CreateRead(key->alFd, WC_TYPE_ASYMKEY, |
2779 | | WC_NAME_RSA)) < 0) { |
2780 | | WOLFSSL_MSG("Unable to bind and create read/send socket"); |
2781 | | ERROR_OUT(key->rdFd); |
2782 | | } |
2783 | | if ((ret = setsockopt(key->alFd, SOL_ALG, ALG_SET_KEY, keyBuf, |
2784 | | keyBufSz)) < 0) { |
2785 | | WOLFSSL_MSG("Error setting RSA key"); |
2786 | | ERROR_OUT(ret); |
2787 | | } |
2788 | | |
2789 | | msg.msg_control = cbuf; |
2790 | | msg.msg_controllen = sizeof(cbuf); |
2791 | | cmsg = CMSG_FIRSTHDR(&msg); |
2792 | | if ((ret = wc_Afalg_SetOp(cmsg, op)) < 0) { |
2793 | | ERROR_OUT(ret); |
2794 | | } |
2795 | | |
2796 | | /* set flag in IV spot, needed for Xilinx hardware acceleration use */ |
2797 | | cmsg = CMSG_NXTHDR(&msg, cmsg); |
2798 | | if ((ret = wc_Afalg_SetIv(cmsg, (byte*)XILINX_RSA_FLAG, |
2799 | | sizeof(XILINX_RSA_FLAG))) != 0) { |
2800 | | ERROR_OUT(ret); |
2801 | | } |
2802 | | |
2803 | | /* compose and send msg */ |
2804 | | XMEMCPY(inBuf, (byte*)in, inLen); /* for alignment */ |
2805 | | iov.iov_base = inBuf; |
2806 | | iov.iov_len = inLen; |
2807 | | msg.msg_iov = &iov; |
2808 | | msg.msg_iovlen = 1; |
2809 | | if ((ret = sendmsg(key->rdFd, &msg, 0)) <= 0) { |
2810 | | ERROR_OUT(WC_AFALG_SOCK_E); |
2811 | | } |
2812 | | |
2813 | | if ((ret = read(key->rdFd, outBuf, inLen)) <= 0) { |
2814 | | ERROR_OUT(WC_AFALG_SOCK_E); |
2815 | | } |
2816 | | XMEMCPY(out, outBuf, ret); |
2817 | | *outLen = keyLen; |
2818 | | |
2819 | | done: |
2820 | | /* clear key data and free buffer */ |
2821 | | if (keyBuf != NULL) { |
2822 | | ForceZero(keyBuf, keyBufSz); |
2823 | | } |
2824 | | XFREE(keyBuf, key->heap, DYNAMIC_TYPE_KEY); |
2825 | | |
2826 | | if (key->alFd > 0) { |
2827 | | close(key->alFd); |
2828 | | key->alFd = WC_SOCK_NOTSET; |
2829 | | } |
2830 | | if (key->rdFd > 0) { |
2831 | | close(key->rdFd); |
2832 | | key->rdFd = WC_SOCK_NOTSET; |
2833 | | } |
2834 | | |
2835 | | return ret; |
2836 | | } |
2837 | | |
2838 | | #elif defined(WOLFSSL_KCAPI_RSA) |
2839 | | static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out, |
2840 | | word32* outLen, int type, RsaKey* key, |
2841 | | WC_RNG* rng) |
2842 | | { |
2843 | | int ret; |
2844 | | |
2845 | | (void)rng; |
2846 | | |
2847 | | switch(type) { |
2848 | | case RSA_PRIVATE_DECRYPT: |
2849 | | case RSA_PRIVATE_ENCRYPT: |
2850 | | ret = KcapiRsa_Decrypt(key, in, inLen, out, outLen); |
2851 | | break; |
2852 | | |
2853 | | case RSA_PUBLIC_DECRYPT: |
2854 | | case RSA_PUBLIC_ENCRYPT: |
2855 | | ret = KcapiRsa_Encrypt(key, in, inLen, out, outLen); |
2856 | | break; |
2857 | | |
2858 | | default: |
2859 | | ret = RSA_WRONG_TYPE_E; |
2860 | | } |
2861 | | |
2862 | | return ret; |
2863 | | } |
2864 | | |
2865 | | #else |
2866 | | #ifndef WOLF_CRYPTO_CB_ONLY_RSA |
2867 | | #ifdef WOLFSSL_HAVE_SP_RSA |
2868 | | static int RsaFunction_SP(const byte* in, word32 inLen, byte* out, |
2869 | | word32* outLen, int type, RsaKey* key, WC_RNG* rng) |
2870 | | { |
2871 | | (void)rng; |
2872 | | |
2873 | | #ifndef WOLFSSL_SP_NO_2048 |
2874 | | if (mp_count_bits(&key->n) == 2048) { |
2875 | | switch(type) { |
2876 | | #ifndef WOLFSSL_RSA_PUBLIC_ONLY |
2877 | | case RSA_PRIVATE_DECRYPT: |
2878 | | case RSA_PRIVATE_ENCRYPT: |
2879 | | #ifdef WC_RSA_BLINDING |
2880 | | if (rng == NULL) |
2881 | | return MISSING_RNG_E; |
2882 | | #endif |
2883 | | #ifndef RSA_LOW_MEM |
2884 | | if ((mp_count_bits(&key->p) == 1024) && |
2885 | | (mp_count_bits(&key->q) == 1024) && |
2886 | | (mp_count_bits(&key->dP) > 0) && |
2887 | | (mp_count_bits(&key->dQ) > 0) && |
2888 | | (mp_count_bits(&key->u) > 0)) { |
2889 | | return sp_RsaPrivate_2048(in, inLen, &key->d, &key->p, &key->q, |
2890 | | &key->dP, &key->dQ, &key->u, &key->n, |
2891 | | out, outLen); |
2892 | | } |
2893 | | break; |
2894 | | #else |
2895 | | return sp_RsaPrivate_2048(in, inLen, &key->d, NULL, NULL, NULL, |
2896 | | NULL, NULL, &key->n, out, outLen); |
2897 | | #endif |
2898 | | #endif |
2899 | | case RSA_PUBLIC_ENCRYPT: |
2900 | | case RSA_PUBLIC_DECRYPT: |
2901 | | return sp_RsaPublic_2048(in, inLen, &key->e, &key->n, out, outLen); |
2902 | | default: |
2903 | | break; |
2904 | | } |
2905 | | } |
2906 | | #endif |
2907 | | #ifndef WOLFSSL_SP_NO_3072 |
2908 | | if (mp_count_bits(&key->n) == 3072) { |
2909 | | switch(type) { |
2910 | | #ifndef WOLFSSL_RSA_PUBLIC_ONLY |
2911 | | case RSA_PRIVATE_DECRYPT: |
2912 | | case RSA_PRIVATE_ENCRYPT: |
2913 | | #ifdef WC_RSA_BLINDING |
2914 | | if (rng == NULL) |
2915 | | return MISSING_RNG_E; |
2916 | | #endif |
2917 | | #ifndef RSA_LOW_MEM |
2918 | | if ((mp_count_bits(&key->p) == 1536) && |
2919 | | (mp_count_bits(&key->q) == 1536) && |
2920 | | (mp_count_bits(&key->dP) > 0) && |
2921 | | (mp_count_bits(&key->dQ) > 0) && |
2922 | | (mp_count_bits(&key->u) > 0)) { |
2923 | | return sp_RsaPrivate_3072(in, inLen, &key->d, &key->p, &key->q, |
2924 | | &key->dP, &key->dQ, &key->u, &key->n, |
2925 | | out, outLen); |
2926 | | } |
2927 | | break; |
2928 | | #else |
2929 | | return sp_RsaPrivate_3072(in, inLen, &key->d, NULL, NULL, NULL, |
2930 | | NULL, NULL, &key->n, out, outLen); |
2931 | | #endif |
2932 | | #endif |
2933 | | case RSA_PUBLIC_ENCRYPT: |
2934 | | case RSA_PUBLIC_DECRYPT: |
2935 | | return sp_RsaPublic_3072(in, inLen, &key->e, &key->n, out, outLen); |
2936 | | default: |
2937 | | break; |
2938 | | } |
2939 | | } |
2940 | | #endif |
2941 | | #ifdef WOLFSSL_SP_4096 |
2942 | | if (mp_count_bits(&key->n) == 4096) { |
2943 | | switch(type) { |
2944 | | #ifndef WOLFSSL_RSA_PUBLIC_ONLY |
2945 | | case RSA_PRIVATE_DECRYPT: |
2946 | | case RSA_PRIVATE_ENCRYPT: |
2947 | | #ifdef WC_RSA_BLINDING |
2948 | | if (rng == NULL) |
2949 | | return MISSING_RNG_E; |
2950 | | #endif |
2951 | | #ifndef RSA_LOW_MEM |
2952 | | if ((mp_count_bits(&key->p) == 2048) && |
2953 | | (mp_count_bits(&key->q) == 2048) && |
2954 | | (mp_count_bits(&key->dP) > 0) && |
2955 | | (mp_count_bits(&key->dQ) > 0) && |
2956 | | (mp_count_bits(&key->u) > 0)) { |
2957 | | return sp_RsaPrivate_4096(in, inLen, &key->d, &key->p, &key->q, |
2958 | | &key->dP, &key->dQ, &key->u, &key->n, |
2959 | | out, outLen); |
2960 | | } |
2961 | | break; |
2962 | | #else |
2963 | | return sp_RsaPrivate_4096(in, inLen, &key->d, NULL, NULL, NULL, |
2964 | | NULL, NULL, &key->n, out, outLen); |
2965 | | #endif |
2966 | | #endif |
2967 | | case RSA_PUBLIC_ENCRYPT: |
2968 | | case RSA_PUBLIC_DECRYPT: |
2969 | | return sp_RsaPublic_4096(in, inLen, &key->e, &key->n, out, outLen); |
2970 | | default: |
2971 | | break; |
2972 | | } |
2973 | | } |
2974 | | #endif |
2975 | | |
2976 | | /* SP not able to do operation. */ |
2977 | | return WC_KEY_SIZE_E; |
2978 | | } |
2979 | | #endif |
2980 | | |
2981 | | #if !defined(WOLFSSL_SP_MATH) |
2982 | | #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY) |
2983 | | static int RsaFunctionPrivate(mp_int* tmp, RsaKey* key, WC_RNG* rng) |
2984 | 0 | { |
2985 | 0 | int ret = 0; |
2986 | 0 | #if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG) |
2987 | 0 | mp_digit mp = 0; |
2988 | 0 | DECL_MP_INT_SIZE_DYN(rnd, mp_bitsused(&key->n), RSA_MAX_SIZE); |
2989 | 0 | DECL_MP_INT_SIZE_DYN(rndi, mp_bitsused(&key->n), RSA_MAX_SIZE); |
2990 | 0 | DECL_MP_INT_SIZE_DYN(mask, mp_bitsused(&key->n), RSA_MAX_SIZE); |
2991 | 0 | #endif /* WC_RSA_BLINDING && !WC_NO_RNG */ |
2992 | |
|
2993 | 0 | if (MP_BITS_OVER_MAX(mp_bitsused(&key->n), RSA_MAX_SIZE)) { |
2994 | 0 | return WC_KEY_SIZE_E; |
2995 | 0 | } |
2996 | | |
2997 | 0 | (void)rng; |
2998 | |
|
2999 | 0 | #if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG) |
3000 | 0 | NEW_MP_INT_SIZE(rnd, mp_bitsused(&key->n), key->heap, DYNAMIC_TYPE_RSA); |
3001 | 0 | NEW_MP_INT_SIZE(rndi, mp_bitsused(&key->n), key->heap, DYNAMIC_TYPE_RSA); |
3002 | 0 | NEW_MP_INT_SIZE(mask, mp_bitsused(&key->n), key->heap, DYNAMIC_TYPE_RSA); |
3003 | | #ifdef MP_INT_SIZE_CHECK_NULL |
3004 | | if ((rnd == NULL) || (rndi == NULL) || (mask == NULL)) { |
3005 | | FREE_MP_INT_SIZE(rnd, key->heap, DYNAMIC_TYPE_RSA); |
3006 | | FREE_MP_INT_SIZE(rndi, key->heap, DYNAMIC_TYPE_RSA); |
3007 | | FREE_MP_INT_SIZE(mask, key->heap, DYNAMIC_TYPE_RSA); |
3008 | | return MEMORY_E; |
3009 | | } |
3010 | | #endif |
3011 | |
|
3012 | 0 | if ((INIT_MP_INT_SIZE(rnd, mp_bitsused(&key->n)) != MP_OKAY) || |
3013 | 0 | (INIT_MP_INT_SIZE(rndi, mp_bitsused(&key->n)) != MP_OKAY) || |
3014 | 0 | (INIT_MP_INT_SIZE(mask, mp_bitsused(&key->n)) != MP_OKAY)) { |
3015 | 0 | ret = MP_INIT_E; |
3016 | 0 | } |
3017 | |
|
3018 | 0 | if (ret == 0) { |
3019 | | /* blind */ |
3020 | 0 | ret = mp_rand(rnd, mp_get_digit_count(&key->n), rng); |
3021 | 0 | } |
3022 | | /* rndi = 1/rnd mod n |
3023 | | * |
3024 | | * mp_invmod() is a binary extended Euclidean variant whose iteration |
3025 | | * count and branches track its input, and rnd is secret. Invert rnd*mask |
3026 | | * for a fresh random mask and divide it back out afterwards: |
3027 | | * (rnd*mask)^-1 * mask == rnd^-1 mod n. The inversion then sees a value |
3028 | | * independent of rnd. */ |
3029 | 0 | if (ret == 0) { |
3030 | 0 | ret = mp_rand(mask, mp_get_digit_count(&key->n), rng); |
3031 | 0 | } |
3032 | 0 | if (ret == 0) { |
3033 | 0 | if (mp_mulmod(rnd, mask, &key->n, rndi) != MP_OKAY) { |
3034 | 0 | ret = MP_MULMOD_E; |
3035 | 0 | } |
3036 | 0 | } |
3037 | 0 | if (ret == 0) { |
3038 | 0 | if (mp_invmod(rndi, &key->n, rndi) != MP_OKAY) { |
3039 | 0 | ret = MP_INVMOD_E; |
3040 | 0 | } |
3041 | 0 | } |
3042 | 0 | if (ret == 0) { |
3043 | 0 | if (mp_mulmod(rndi, mask, &key->n, rndi) != MP_OKAY) { |
3044 | 0 | ret = MP_MULMOD_E; |
3045 | 0 | } |
3046 | 0 | } |
3047 | 0 | if (ret == 0) { |
3048 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
3049 | | mp_memzero_add("RSA Private rnd", rnd); |
3050 | | mp_memzero_add("RSA Private rndi", rndi); |
3051 | | mp_memzero_add("RSA Private mask", mask); |
3052 | | #endif |
3053 | | |
3054 | | /* rnd = rnd^e */ |
3055 | | #ifndef WOLFSSL_SP_MATH_ALL |
3056 | | if (mp_exptmod(rnd, &key->e, &key->n, rnd) != MP_OKAY) { |
3057 | | ret = MP_EXPTMOD_E; |
3058 | | } |
3059 | | #else |
3060 | 0 | if (mp_exptmod_nct(rnd, &key->e, &key->n, rnd) != MP_OKAY) { |
3061 | 0 | ret = MP_EXPTMOD_E; |
3062 | 0 | } |
3063 | 0 | #endif |
3064 | 0 | } |
3065 | |
|
3066 | 0 | if (ret == 0) { |
3067 | | /* tmp = tmp*rnd mod n */ |
3068 | 0 | if (mp_mulmod(tmp, rnd, &key->n, tmp) != MP_OKAY) { |
3069 | 0 | ret = MP_MULMOD_E; |
3070 | 0 | } |
3071 | 0 | } |
3072 | 0 | #endif /* WC_RSA_BLINDING && !WC_NO_RNG */ |
3073 | |
|
3074 | | #ifdef RSA_LOW_MEM /* half as much memory but twice as slow */ |
3075 | | if (ret == 0) { |
3076 | | if (mp_exptmod(tmp, &key->d, &key->n, tmp) != MP_OKAY) { |
3077 | | ret = MP_EXPTMOD_E; |
3078 | | } |
3079 | | } |
3080 | | #else |
3081 | 0 | if (ret == 0 && (mp_iszero(&key->p) || mp_iszero(&key->q) || |
3082 | 0 | mp_iszero(&key->dP) || mp_iszero(&key->dQ) || mp_iszero(&key->u))) { |
3083 | 0 | if (mp_exptmod(tmp, &key->d, &key->n, tmp) != MP_OKAY) { |
3084 | 0 | ret = MP_EXPTMOD_E; |
3085 | 0 | } |
3086 | 0 | } |
3087 | 0 | else if (ret == 0) { |
3088 | 0 | mp_int* tmpa = tmp; |
3089 | 0 | #if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG) |
3090 | 0 | mp_int* tmpb = rnd; |
3091 | | #else |
3092 | | DECL_MP_INT_SIZE_DYN(tmpb, mp_bitsused(&key->n), RSA_MAX_SIZE); |
3093 | | #endif |
3094 | |
|
3095 | | #if !defined(WC_RSA_BLINDING) || defined(WC_NO_RNG) |
3096 | | NEW_MP_INT_SIZE(tmpb, mp_bitsused(&key->n), key->heap, |
3097 | | DYNAMIC_TYPE_RSA); |
3098 | | #ifdef MP_INT_SIZE_CHECK_NULL |
3099 | | if (tmpb == NULL) { |
3100 | | ret = MEMORY_E; |
3101 | | } |
3102 | | #endif |
3103 | | if ((ret == 0) && INIT_MP_INT_SIZE(tmpb, mp_bitsused(&key->n)) != |
3104 | | MP_OKAY) { |
3105 | | ret = MP_INIT_E; |
3106 | | } |
3107 | | #endif |
3108 | |
|
3109 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
3110 | | if (ret == 0) { |
3111 | | mp_memzero_add("RSA Sync tmpb", tmpb); |
3112 | | } |
3113 | | #endif |
3114 | | |
3115 | | /* tmpb = tmp^dQ mod q */ |
3116 | 0 | if (ret == 0 && mp_exptmod(tmp, &key->dQ, &key->q, tmpb) != MP_OKAY) |
3117 | 0 | ret = MP_EXPTMOD_E; |
3118 | | |
3119 | | /* tmpa = tmp^dP mod p */ |
3120 | 0 | if (ret == 0 && mp_exptmod(tmp, &key->dP, &key->p, tmpa) != MP_OKAY) |
3121 | 0 | ret = MP_EXPTMOD_E; |
3122 | | |
3123 | | /* tmp = (tmp - tmpb) * qInv (mod p) */ |
3124 | 0 | #if (defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL)) && \ |
3125 | 0 | !defined(WOLFSSL_SP_INT_NEGATIVE)) |
3126 | 0 | if (ret == 0 && mp_submod(tmpa, tmpb, &key->p, tmp) != MP_OKAY) |
3127 | 0 | ret = MP_SUB_E; |
3128 | | #else |
3129 | | if (ret == 0 && mp_sub(tmpa, tmpb, tmp) != MP_OKAY) |
3130 | | ret = MP_SUB_E; |
3131 | | #endif |
3132 | |
|
3133 | 0 | if (ret == 0 && mp_mulmod(tmp, &key->u, &key->p, tmp) != MP_OKAY) |
3134 | 0 | ret = MP_MULMOD_E; |
3135 | | |
3136 | | /* tmp = tmpb + q * tmp */ |
3137 | 0 | if (ret == 0 && mp_mul(tmp, &key->q, tmp) != MP_OKAY) |
3138 | 0 | ret = MP_MUL_E; |
3139 | |
|
3140 | 0 | if (ret == 0 && mp_add(tmp, tmpb, tmp) != MP_OKAY) |
3141 | 0 | ret = MP_ADD_E; |
3142 | |
|
3143 | | #if !defined(WC_RSA_BLINDING) || defined(WC_NO_RNG) |
3144 | | mp_forcezero(tmpb); |
3145 | | FREE_MP_INT_SIZE(tmpb, key->heap, DYNAMIC_TYPE_RSA); |
3146 | | #if !defined(MP_INT_SIZE_CHECK_NULL) && defined(WOLFSSL_CHECK_MEM_ZERO) |
3147 | | mp_memzero_check(tmpb); |
3148 | | #endif |
3149 | | #endif |
3150 | 0 | } |
3151 | 0 | #endif /* RSA_LOW_MEM */ |
3152 | |
|
3153 | 0 | #if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG) |
3154 | | /* Multiply result (tmp) by blinding invertor (rndi). |
3155 | | * Use Montgomery form to make operation more constant time. |
3156 | | */ |
3157 | 0 | if ((ret == 0) && (mp_montgomery_setup(&key->n, &mp) != MP_OKAY)) { |
3158 | 0 | ret = MP_MULMOD_E; |
3159 | 0 | } |
3160 | 0 | if ((ret == 0) && (mp_montgomery_calc_normalization(rnd, &key->n) != |
3161 | 0 | MP_OKAY)) { |
3162 | 0 | ret = MP_MULMOD_E; |
3163 | 0 | } |
3164 | | /* Convert blinding invert to Montgomery form. */ |
3165 | 0 | if ((ret == 0) && (mp_mul(rndi, rnd, rndi) != MP_OKAY)) { |
3166 | 0 | ret = MP_MULMOD_E; |
3167 | 0 | } |
3168 | 0 | if ((ret == 0) && (mp_mod(rndi, &key->n, rndi) != MP_OKAY)) { |
3169 | 0 | ret = MP_MULMOD_E; |
3170 | 0 | } |
3171 | | /* Multiply result by blinding invert. */ |
3172 | 0 | if ((ret == 0) && (mp_mul(tmp, rndi, tmp) != MP_OKAY)) { |
3173 | 0 | ret = MP_MULMOD_E; |
3174 | 0 | } |
3175 | | /* Reduce result. */ |
3176 | 0 | if ((ret == 0) && (mp_montgomery_reduce_ct(tmp, &key->n, mp) != MP_OKAY)) { |
3177 | 0 | ret = MP_MULMOD_E; |
3178 | 0 | } |
3179 | |
|
3180 | 0 | mp_forcezero(mask); |
3181 | 0 | mp_forcezero(rndi); |
3182 | 0 | mp_forcezero(rnd); |
3183 | 0 | FREE_MP_INT_SIZE(mask, key->heap, DYNAMIC_TYPE_RSA); |
3184 | 0 | FREE_MP_INT_SIZE(rndi, key->heap, DYNAMIC_TYPE_RSA); |
3185 | 0 | FREE_MP_INT_SIZE(rnd, key->heap, DYNAMIC_TYPE_RSA); |
3186 | | #if !defined(MP_INT_SIZE_CHECK_NULL) && defined(WOLFSSL_CHECK_MEM_ZERO) |
3187 | | mp_memzero_check(rnd); |
3188 | | mp_memzero_check(rndi); |
3189 | | mp_memzero_check(mask); |
3190 | | #endif |
3191 | 0 | #endif /* WC_RSA_BLINDING && !WC_NO_RNG */ |
3192 | 0 | return ret; |
3193 | 0 | } |
3194 | | #endif |
3195 | | |
3196 | | static int RsaFunctionSync(const byte* in, word32 inLen, byte* out, |
3197 | | word32* outLen, int type, RsaKey* key, WC_RNG* rng) |
3198 | 0 | { |
3199 | 0 | DECL_MP_INT_SIZE_DYN(tmp, mp_bitsused(&key->n), RSA_MAX_SIZE); |
3200 | 0 | int ret = 0; |
3201 | |
|
3202 | 0 | if (MP_BITS_OVER_MAX(mp_bitsused(&key->n), RSA_MAX_SIZE)) { |
3203 | 0 | return WC_KEY_SIZE_E; |
3204 | 0 | } |
3205 | | |
3206 | 0 | (void)rng; |
3207 | |
|
3208 | 0 | NEW_MP_INT_SIZE(tmp, mp_bitsused(&key->n), key->heap, DYNAMIC_TYPE_RSA); |
3209 | | #ifdef MP_INT_SIZE_CHECK_NULL |
3210 | | if (tmp == NULL) { |
3211 | | WOLFSSL_MSG("NEW_MP_INT_SIZE tmp is NULL, return MEMORY_E"); |
3212 | | return MEMORY_E; |
3213 | | } |
3214 | | #endif |
3215 | |
|
3216 | 0 | if (INIT_MP_INT_SIZE(tmp, mp_bitsused(&key->n)) != MP_OKAY) { |
3217 | 0 | WOLFSSL_MSG("INIT_MP_INT_SIZE failed."); |
3218 | 0 | ret = MP_INIT_E; |
3219 | 0 | } |
3220 | |
|
3221 | 0 | #ifndef TEST_UNPAD_CONSTANT_TIME |
3222 | 0 | if (ret == 0 && mp_read_unsigned_bin(tmp, in, inLen) != MP_OKAY) |
3223 | 0 | ret = MP_READ_E; |
3224 | |
|
3225 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
3226 | | if (ret == 0) { |
3227 | | mp_memzero_add("RSA sync tmp", tmp); |
3228 | | } |
3229 | | #endif |
3230 | |
|
3231 | 0 | if (ret == 0) { |
3232 | 0 | switch(type) { |
3233 | 0 | #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY) |
3234 | 0 | case RSA_PRIVATE_DECRYPT: |
3235 | 0 | case RSA_PRIVATE_ENCRYPT: |
3236 | 0 | { |
3237 | 0 | ret = RsaFunctionPrivate(tmp, key, rng); |
3238 | 0 | break; |
3239 | 0 | } |
3240 | 0 | #endif |
3241 | 0 | case RSA_PUBLIC_ENCRYPT: |
3242 | 0 | case RSA_PUBLIC_DECRYPT: |
3243 | 0 | if (mp_exptmod_nct(tmp, &key->e, &key->n, tmp) != MP_OKAY) { |
3244 | 0 | WOLFSSL_MSG_CERT_LOG("mp_exptmod_nct failed"); |
3245 | 0 | ret = MP_EXPTMOD_E; |
3246 | 0 | } |
3247 | 0 | break; |
3248 | 0 | default: |
3249 | 0 | ret = RSA_WRONG_TYPE_E; |
3250 | 0 | break; |
3251 | 0 | } |
3252 | 0 | } |
3253 | | |
3254 | 0 | if (ret == 0) { |
3255 | 0 | WOLFSSL_MSG("mp_to_unsigned_bin_len_ct..."); |
3256 | 0 | if (mp_to_unsigned_bin_len_ct(tmp, out, (int)*outLen) != MP_OKAY) { |
3257 | 0 | WOLFSSL_MSG("mp_to_unsigned_bin_len_ct failed"); |
3258 | 0 | ret = MP_TO_E; |
3259 | 0 | } |
3260 | 0 | } |
3261 | | #ifdef WOLFSSL_RSA_CHECK_D_ON_DECRYPT |
3262 | | if ((ret == 0) && (type == RSA_PRIVATE_DECRYPT)) { |
3263 | | mp_sub(&key->n, &key->p, tmp); |
3264 | | mp_sub(tmp, &key->q, tmp); |
3265 | | mp_add_d(tmp, 1, tmp); |
3266 | | mp_mulmod(&key->d, &key->e, tmp, tmp); |
3267 | | if (!mp_isone(tmp)) { |
3268 | | ret = MP_EXPTMOD_E; |
3269 | | } |
3270 | | } |
3271 | | #endif |
3272 | | #else |
3273 | | (void)type; |
3274 | | (void)key; |
3275 | | XMEMCPY(out, in, inLen); |
3276 | | #endif |
3277 | |
|
3278 | 0 | mp_forcezero(tmp); |
3279 | 0 | FREE_MP_INT_SIZE(tmp, key->heap, DYNAMIC_TYPE_RSA); |
3280 | | #if !defined(MP_INT_SIZE_CHECK_NULL) && defined(WOLFSSL_CHECK_MEM_ZERO) |
3281 | | mp_memzero_check(tmp); |
3282 | | #endif |
3283 | 0 | return ret; |
3284 | 0 | } |
3285 | | #endif /* !WOLFSSL_SP_MATH */ |
3286 | | |
3287 | | static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out, |
3288 | | word32* outLen, int type, RsaKey* key, WC_RNG* rng) |
3289 | 0 | { |
3290 | 0 | int ret; |
3291 | 0 | word32 keyLen; |
3292 | |
|
3293 | 0 | ret = wc_RsaEncryptSize(key); |
3294 | 0 | if (ret < 0) { |
3295 | | #ifdef DEBUG_WOLFSSL |
3296 | | WOLFSSL_MSG_EX("wc_RsaEncryptSize failed err = %d", ret); |
3297 | | #endif |
3298 | 0 | return ret; |
3299 | 0 | } |
3300 | 0 | keyLen = (word32)ret; |
3301 | |
|
3302 | 0 | if (inLen > keyLen) { |
3303 | 0 | WOLFSSL_MSG("Expected that inLen be no longer RSA key length"); |
3304 | 0 | return BAD_FUNC_ARG; |
3305 | 0 | } |
3306 | 0 | if (keyLen > *outLen) { |
3307 | 0 | WOLFSSL_MSG("Expected that outLen be no shorter RSA key length"); |
3308 | 0 | return RSA_BUFFER_E; |
3309 | 0 | } |
3310 | | |
3311 | 0 | if (mp_iseven(&key->n)) { |
3312 | 0 | WOLFSSL_MSG("MP_VAL is even"); |
3313 | 0 | return MP_VAL; |
3314 | 0 | } |
3315 | | |
3316 | | #if defined(WOLFSSL_NXP_CASPER_RSA_PUB_EXPTMOD) |
3317 | | if (type == RSA_PUBLIC_DECRYPT || type == RSA_PUBLIC_ENCRYPT) { |
3318 | | ret = casper_rsa_public_exptmod(in, inLen, out, outLen, key); |
3319 | | if (ret == 0) |
3320 | | return MP_OKAY; |
3321 | | /* else fall through for software fallback */ |
3322 | | } |
3323 | | #endif |
3324 | | |
3325 | | #ifdef WOLFSSL_HAVE_SP_RSA |
3326 | | ret = RsaFunction_SP(in, inLen, out, outLen, type, key, rng); |
3327 | | if (ret != WC_NO_ERR_TRACE(WC_KEY_SIZE_E)) |
3328 | | return ret; |
3329 | | #endif /* WOLFSSL_HAVE_SP_RSA */ |
3330 | | |
3331 | | #if defined(WOLFSSL_SP_MATH) |
3332 | | (void)rng; |
3333 | | #ifndef WOLFSSL_HAVE_SP_RSA |
3334 | | (void)in; |
3335 | | (void)inLen; |
3336 | | (void)out; |
3337 | | (void)outLen; |
3338 | | (void)type; |
3339 | | (void)key; |
3340 | | #error RSA SP option invalid (enable WOLFSSL_HAVE_SP_RSA or disable WOLFSSL_SP_MATH) |
3341 | | return NOT_COMPILED_IN; |
3342 | | #else |
3343 | | WOLFSSL_MSG("SP Key Size Error"); |
3344 | | return WC_KEY_SIZE_E; |
3345 | | #endif |
3346 | | #else |
3347 | 0 | *outLen = keyLen; |
3348 | 0 | return RsaFunctionSync(in, inLen, out, outLen, type, key, rng); |
3349 | 0 | #endif /* WOLFSSL_SP_MATH */ |
3350 | 0 | } /* wc_RsaFunctionSync */ |
3351 | | #endif /* WOLF_CRYPTO_CB_ONLY_RSA */ |
3352 | | #endif |
3353 | | |
3354 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA) |
3355 | | static int wc_RsaFunctionAsync(const byte* in, word32 inLen, byte* out, |
3356 | | word32* outLen, int type, RsaKey* key, WC_RNG* rng) |
3357 | | { |
3358 | | int ret = 0; |
3359 | | |
3360 | | (void)rng; |
3361 | | |
3362 | | #ifdef WOLFSSL_ASYNC_CRYPT_SW |
3363 | | if (wc_AsyncSwInit(&key->asyncDev, ASYNC_SW_RSA_FUNC)) { |
3364 | | WC_ASYNC_SW* sw = &key->asyncDev.sw; |
3365 | | sw->rsaFunc.in = in; |
3366 | | sw->rsaFunc.inSz = inLen; |
3367 | | sw->rsaFunc.out = out; |
3368 | | sw->rsaFunc.outSz = outLen; |
3369 | | sw->rsaFunc.type = type; |
3370 | | sw->rsaFunc.key = key; |
3371 | | sw->rsaFunc.rng = rng; |
3372 | | return WC_PENDING_E; |
3373 | | } |
3374 | | #endif /* WOLFSSL_ASYNC_CRYPT_SW */ |
3375 | | |
3376 | | #ifdef WC_RSA_NONBLOCK |
3377 | | /* When a non-blocking context is attached and the SP nonblock backend |
3378 | | * is available, drive the chunked state machine here. wolfAsync_DoSw |
3379 | | * (line "if (ret == FP_WOULDBLOCK) ret = WC_PENDING_E;" at the bottom |
3380 | | * of the SW switch in wolfcrypt/src/async.c, FP_WOULDBLOCK aliases |
3381 | | * MP_WOULDBLOCK) translates per-yield MP_WOULDBLOCK into WC_PENDING_E |
3382 | | * so the TLS / async event loop can drive the operation to completion. */ |
3383 | | if (key->nb != NULL) { |
3384 | | return wc_RsaFunctionNonBlock(in, inLen, out, outLen, type, key); |
3385 | | } |
3386 | | #endif |
3387 | | |
3388 | | switch (type) { |
3389 | | #ifndef WOLFSSL_RSA_PUBLIC_ONLY |
3390 | | case RSA_PRIVATE_DECRYPT: |
3391 | | case RSA_PRIVATE_ENCRYPT: |
3392 | | #ifdef HAVE_CAVIUM |
3393 | | key->dataLen = key->n.raw.len; |
3394 | | ret = NitroxRsaExptMod(in, inLen, |
3395 | | key->d.raw.buf, key->d.raw.len, |
3396 | | key->n.raw.buf, key->n.raw.len, |
3397 | | out, outLen, key); |
3398 | | #elif defined(HAVE_INTEL_QA) |
3399 | | #ifdef RSA_LOW_MEM |
3400 | | ret = IntelQaRsaPrivate(&key->asyncDev, in, inLen, |
3401 | | &key->d.raw, &key->n.raw, |
3402 | | out, outLen); |
3403 | | #else |
3404 | | ret = IntelQaRsaCrtPrivate(&key->asyncDev, in, inLen, |
3405 | | &key->p.raw, &key->q.raw, |
3406 | | &key->dP.raw, &key->dQ.raw, |
3407 | | &key->u.raw, |
3408 | | out, outLen); |
3409 | | #endif |
3410 | | #else |
3411 | | ret = wc_RsaFunctionSync(in, inLen, out, outLen, type, key, rng); |
3412 | | #endif |
3413 | | break; |
3414 | | #endif |
3415 | | |
3416 | | case RSA_PUBLIC_ENCRYPT: |
3417 | | case RSA_PUBLIC_DECRYPT: |
3418 | | #ifdef HAVE_CAVIUM |
3419 | | key->dataLen = key->n.raw.len; |
3420 | | ret = NitroxRsaExptMod(in, inLen, |
3421 | | key->e.raw.buf, key->e.raw.len, |
3422 | | key->n.raw.buf, key->n.raw.len, |
3423 | | out, outLen, key); |
3424 | | #elif defined(HAVE_INTEL_QA) |
3425 | | ret = IntelQaRsaPublic(&key->asyncDev, in, inLen, |
3426 | | &key->e.raw, &key->n.raw, |
3427 | | out, outLen); |
3428 | | #else |
3429 | | ret = wc_RsaFunctionSync(in, inLen, out, outLen, type, key, rng); |
3430 | | #endif |
3431 | | break; |
3432 | | |
3433 | | default: |
3434 | | ret = RSA_WRONG_TYPE_E; |
3435 | | } |
3436 | | |
3437 | | return ret; |
3438 | | } |
3439 | | #endif /* WOLFSSL_ASYNC_CRYPT && WC_ASYNC_ENABLE_RSA */ |
3440 | | |
3441 | | #if defined(WC_RSA_DIRECT) || defined(WC_RSA_NO_PADDING) || \ |
3442 | | defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
3443 | | /* Performs direct RSA computation without padding. The input and output must |
3444 | | * match the key size (ex: 2048-bits = 256 bytes). Returns the size of the |
3445 | | * output on success or negative value on failure. */ |
3446 | | int wc_RsaDirect(const byte* in, word32 inLen, byte* out, word32* outSz, |
3447 | | RsaKey* key, int type, WC_RNG* rng) |
3448 | | { |
3449 | | int ret; |
3450 | | |
3451 | | if (in == NULL || outSz == NULL || key == NULL) { |
3452 | | return BAD_FUNC_ARG; |
3453 | | } |
3454 | | |
3455 | | /* sanity check on type of RSA operation */ |
3456 | | switch (type) { |
3457 | | case RSA_PUBLIC_ENCRYPT: |
3458 | | case RSA_PUBLIC_DECRYPT: |
3459 | | case RSA_PRIVATE_ENCRYPT: |
3460 | | case RSA_PRIVATE_DECRYPT: |
3461 | | break; |
3462 | | default: |
3463 | | WOLFSSL_MSG("Bad RSA type"); |
3464 | | return BAD_FUNC_ARG; |
3465 | | } |
3466 | | |
3467 | | if ((ret = wc_RsaEncryptSize(key)) < 0) { |
3468 | | return ret; |
3469 | | } |
3470 | | |
3471 | | if (inLen != (word32)ret) { |
3472 | | WOLFSSL_MSG("Bad input length. Should be RSA key size"); |
3473 | | return BAD_FUNC_ARG; |
3474 | | } |
3475 | | |
3476 | | if (out == NULL) { |
3477 | | *outSz = inLen; |
3478 | | return WC_NO_ERR_TRACE(LENGTH_ONLY_E); |
3479 | | } |
3480 | | |
3481 | | switch (key->state) { |
3482 | | case RSA_STATE_NONE: |
3483 | | case RSA_STATE_ENCRYPT_PAD: |
3484 | | case RSA_STATE_ENCRYPT_EXPTMOD: |
3485 | | case RSA_STATE_DECRYPT_EXPTMOD: |
3486 | | case RSA_STATE_DECRYPT_UNPAD: |
3487 | | key->state = (type == RSA_PRIVATE_ENCRYPT || |
3488 | | type == RSA_PUBLIC_ENCRYPT) ? RSA_STATE_ENCRYPT_EXPTMOD: |
3489 | | RSA_STATE_DECRYPT_EXPTMOD; |
3490 | | |
3491 | | key->dataLen = *outSz; |
3492 | | |
3493 | | ret = wc_RsaFunction(in, inLen, out, &key->dataLen, type, key, rng); |
3494 | | if (ret >= 0 || ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3495 | | key->state = (type == RSA_PRIVATE_ENCRYPT || |
3496 | | type == RSA_PUBLIC_ENCRYPT) ? RSA_STATE_ENCRYPT_RES: |
3497 | | RSA_STATE_DECRYPT_RES; |
3498 | | } |
3499 | | if (ret < 0) { |
3500 | | break; |
3501 | | } |
3502 | | |
3503 | | FALL_THROUGH; |
3504 | | |
3505 | | case RSA_STATE_ENCRYPT_RES: |
3506 | | case RSA_STATE_DECRYPT_RES: |
3507 | | ret = (int)key->dataLen; |
3508 | | break; |
3509 | | |
3510 | | default: |
3511 | | ret = BAD_STATE_E; |
3512 | | } |
3513 | | |
3514 | | /* if async pending then skip cleanup*/ |
3515 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E) |
3516 | | #ifdef WC_RSA_NONBLOCK |
3517 | | || ret == FP_WOULDBLOCK |
3518 | | #endif |
3519 | | ) { |
3520 | | return ret; |
3521 | | } |
3522 | | |
3523 | | key->state = RSA_STATE_NONE; |
3524 | | wc_RsaCleanup(key); |
3525 | | |
3526 | | return ret; |
3527 | | } |
3528 | | #endif /* WC_RSA_DIRECT || WC_RSA_NO_PADDING || OPENSSL_EXTRA || \ |
3529 | | * OPENSSL_EXTRA_X509_SMALL */ |
3530 | | |
3531 | | #if defined(WOLFSSL_CRYPTOCELL) |
3532 | | static int cc310_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out, |
3533 | | word32 outLen, RsaKey* key) |
3534 | | { |
3535 | | CRYSError_t ret = 0; |
3536 | | CRYS_RSAPrimeData_t primeData; |
3537 | | int modulusSize = wc_RsaEncryptSize(key); |
3538 | | |
3539 | | /* The out buffer must be at least modulus size bytes long. */ |
3540 | | if (outLen < modulusSize) |
3541 | | return BAD_FUNC_ARG; |
3542 | | |
3543 | | ret = CRYS_RSA_PKCS1v15_Encrypt(&wc_rndState, |
3544 | | wc_rndGenVectFunc, |
3545 | | &key->ctx.pubKey, |
3546 | | &primeData, |
3547 | | (byte*)in, |
3548 | | inLen, |
3549 | | out); |
3550 | | |
3551 | | if (ret != SA_SILIB_RET_OK){ |
3552 | | WOLFSSL_MSG("CRYS_RSA_PKCS1v15_Encrypt failed"); |
3553 | | return -1; |
3554 | | } |
3555 | | |
3556 | | return modulusSize; |
3557 | | } |
3558 | | static int cc310_RsaPublicDecrypt(const byte* in, word32 inLen, byte* out, |
3559 | | word32 outLen, RsaKey* key) |
3560 | | { |
3561 | | CRYSError_t ret = 0; |
3562 | | CRYS_RSAPrimeData_t primeData; |
3563 | | word16 actualOutLen = outLen; |
3564 | | |
3565 | | ret = CRYS_RSA_PKCS1v15_Decrypt(&key->ctx.privKey, |
3566 | | &primeData, |
3567 | | (byte*)in, |
3568 | | inLen, |
3569 | | out, |
3570 | | &actualOutLen); |
3571 | | |
3572 | | if (ret != SA_SILIB_RET_OK){ |
3573 | | WOLFSSL_MSG("CRYS_RSA_PKCS1v15_Decrypt failed"); |
3574 | | return -1; |
3575 | | } |
3576 | | return actualOutLen; |
3577 | | } |
3578 | | |
3579 | | int cc310_RsaSSL_Sign(const byte* in, word32 inLen, byte* out, |
3580 | | word32 outLen, RsaKey* key, CRYS_RSA_HASH_OpMode_t mode) |
3581 | | { |
3582 | | CRYSError_t ret = 0; |
3583 | | word16 actualOutLen = outLen*sizeof(byte); |
3584 | | CRYS_RSAPrivUserContext_t contextPrivate; |
3585 | | |
3586 | | ret = CRYS_RSA_PKCS1v15_Sign(&wc_rndState, |
3587 | | wc_rndGenVectFunc, |
3588 | | &contextPrivate, |
3589 | | &key->ctx.privKey, |
3590 | | mode, |
3591 | | (byte*)in, |
3592 | | inLen, |
3593 | | out, |
3594 | | &actualOutLen); |
3595 | | |
3596 | | if (ret != SA_SILIB_RET_OK){ |
3597 | | WOLFSSL_MSG("CRYS_RSA_PKCS1v15_Sign failed"); |
3598 | | return -1; |
3599 | | } |
3600 | | return actualOutLen; |
3601 | | } |
3602 | | |
3603 | | int cc310_RsaSSL_Verify(const byte* in, word32 inLen, byte* sig, |
3604 | | RsaKey* key, CRYS_RSA_HASH_OpMode_t mode) |
3605 | | { |
3606 | | CRYSError_t ret = 0; |
3607 | | CRYS_RSAPubUserContext_t contextPub; |
3608 | | |
3609 | | /* verify the signature in the sig pointer */ |
3610 | | ret = CRYS_RSA_PKCS1v15_Verify(&contextPub, |
3611 | | &key->ctx.pubKey, |
3612 | | mode, |
3613 | | (byte*)in, |
3614 | | inLen, |
3615 | | sig); |
3616 | | |
3617 | | if (ret != SA_SILIB_RET_OK){ |
3618 | | WOLFSSL_MSG("CRYS_RSA_PKCS1v15_Verify failed"); |
3619 | | return -1; |
3620 | | } |
3621 | | |
3622 | | return ret; |
3623 | | } |
3624 | | #endif /* WOLFSSL_CRYPTOCELL */ |
3625 | | |
3626 | | #ifndef WOLF_CRYPTO_CB_ONLY_RSA |
3627 | | #if !defined(NO_RSA_BOUNDS_CHECK) |
3628 | | /* Check that 1 < in < n-1. (Requirement of 800-56B.) */ |
3629 | | int RsaFunctionCheckIn(const byte* in, word32 inLen, RsaKey* key, |
3630 | | int checkSmallCt) |
3631 | 0 | { |
3632 | 0 | int ret = 0; |
3633 | |
|
3634 | 0 | DECL_MP_INT_SIZE_DYN(c, mp_bitsused(&key->n), RSA_MAX_SIZE); |
3635 | |
|
3636 | 0 | if (MP_BITS_OVER_MAX(mp_bitsused(&key->n), RSA_MAX_SIZE)) { |
3637 | 0 | return WC_KEY_SIZE_E; |
3638 | 0 | } |
3639 | | |
3640 | 0 | NEW_MP_INT_SIZE(c, mp_bitsused(&key->n), key->heap, DYNAMIC_TYPE_RSA); |
3641 | | #ifdef MP_INT_SIZE_CHECK_NULL |
3642 | | if (c == NULL) |
3643 | | ret = MEMORY_E; |
3644 | | #endif |
3645 | |
|
3646 | 0 | if (ret == 0 && INIT_MP_INT_SIZE(c, mp_bitsused(&key->n)) != MP_OKAY) { |
3647 | 0 | ret = MP_INIT_E; |
3648 | 0 | } |
3649 | 0 | if (ret == 0) { |
3650 | 0 | if (mp_read_unsigned_bin(c, in, inLen) != 0) |
3651 | 0 | ret = MP_READ_E; |
3652 | 0 | } |
3653 | 0 | if (ret == 0) { |
3654 | | /* check c > 1 */ |
3655 | 0 | if (checkSmallCt && (mp_cmp_d(c, 1) != MP_GT)) |
3656 | 0 | ret = RSA_OUT_OF_RANGE_E; |
3657 | 0 | } |
3658 | 0 | if (ret == 0) { |
3659 | | /* add c+1 */ |
3660 | 0 | if (mp_add_d(c, 1, c) != MP_OKAY) |
3661 | 0 | ret = MP_ADD_E; |
3662 | 0 | } |
3663 | 0 | if (ret == 0) { |
3664 | | /* check c+1 < n */ |
3665 | 0 | if (mp_cmp(c, &key->n) != MP_LT) |
3666 | 0 | ret = RSA_OUT_OF_RANGE_E; |
3667 | 0 | } |
3668 | 0 | mp_clear(c); |
3669 | |
|
3670 | 0 | FREE_MP_INT_SIZE(c, key->heap, DYNAMIC_TYPE_RSA); |
3671 | |
|
3672 | 0 | return ret; |
3673 | 0 | } |
3674 | | #endif /* !NO_RSA_BOUNDS_CHECK */ |
3675 | | #endif /* WOLF_CRYPTO_CB_ONLY_RSA */ |
3676 | | |
3677 | | static int wc_RsaFunction_ex(const byte* in, word32 inLen, byte* out, |
3678 | | word32* outLen, int type, RsaKey* key, WC_RNG* rng, |
3679 | | int checkSmallCt) |
3680 | 0 | { |
3681 | 0 | int ret = 0; |
3682 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD) |
3683 | | RsaPadding padding; |
3684 | | #endif |
3685 | |
|
3686 | 0 | (void)rng; |
3687 | 0 | (void)checkSmallCt; |
3688 | |
|
3689 | 0 | if (key == NULL || in == NULL || inLen == 0 || out == NULL || |
3690 | 0 | outLen == NULL || *outLen == 0 || type == RSA_TYPE_UNKNOWN) { |
3691 | 0 | return BAD_FUNC_ARG; |
3692 | 0 | } |
3693 | | |
3694 | | #ifdef WOLF_CRYPTO_CB |
3695 | | #ifndef WOLF_CRYPTO_CB_FIND |
3696 | | if (key->devId != INVALID_DEVID) |
3697 | | #endif |
3698 | | { |
3699 | | #if defined(WOLF_CRYPTO_CB_RSA_PAD) |
3700 | | /* If we are here, either the RSA PAD callback was already called |
3701 | | * and returned that it could not implement for that padding scheme, |
3702 | | * or this is a public verify operation. Either way indicate to the |
3703 | | * callback that this should be a raw RSA operation with no padding.*/ |
3704 | | XMEMSET(&padding, 0, sizeof(RsaPadding)); |
3705 | | padding.pad_type = WC_RSA_NO_PAD; |
3706 | | ret = wc_CryptoCb_RsaPad(in, inLen, out, |
3707 | | outLen, type, key, rng, &padding); |
3708 | | #else |
3709 | | ret = wc_CryptoCb_Rsa(in, inLen, out, outLen, type, key, rng); |
3710 | | #endif |
3711 | | #ifndef WOLF_CRYPTO_CB_ONLY_RSA |
3712 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
3713 | | return ret; |
3714 | | /* fall-through when unavailable and try using software */ |
3715 | | #endif |
3716 | | #ifdef WOLF_CRYPTO_CB_ONLY_RSA |
3717 | | if (ret == WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
3718 | | return NO_VALID_DEVID; |
3719 | | } |
3720 | | return ret; |
3721 | | #endif |
3722 | | } |
3723 | | #endif |
3724 | | |
3725 | | #ifdef WOLF_CRYPTO_CB_ONLY_RSA |
3726 | | return NO_VALID_DEVID; |
3727 | | #else /* !WOLF_CRYPTO_CB_ONLY_RSA */ |
3728 | | |
3729 | 0 | #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(TEST_UNPAD_CONSTANT_TIME) && \ |
3730 | 0 | !defined(NO_RSA_BOUNDS_CHECK) |
3731 | 0 | if (type == RSA_PRIVATE_DECRYPT && |
3732 | 0 | key->state == RSA_STATE_DECRYPT_EXPTMOD) { |
3733 | |
|
3734 | 0 | ret = RsaFunctionCheckIn(in, inLen, key, checkSmallCt); |
3735 | 0 | if (ret != 0) { |
3736 | 0 | return ret; |
3737 | 0 | } |
3738 | 0 | } |
3739 | 0 | #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !TEST_UNPAD_CONSTANT_TIME && \ |
3740 | | * !NO_RSA_BOUNDS_CHECK */ |
3741 | 0 | #if !defined(NO_RSA_BOUNDS_CHECK) |
3742 | 0 | if (type == RSA_PUBLIC_DECRYPT && |
3743 | 0 | key->state == RSA_STATE_DECRYPT_EXPTMOD) { |
3744 | |
|
3745 | 0 | ret = RsaFunctionCheckIn(in, inLen, key, checkSmallCt); |
3746 | 0 | if (ret != 0) { |
3747 | 0 | return ret; |
3748 | 0 | } |
3749 | 0 | } |
3750 | 0 | #endif |
3751 | | |
3752 | | #if !defined(NO_RSA_BOUNDS_CHECK) && FIPS_VERSION3_GE(7,0,0) |
3753 | | /* Reject a message outside 1 < m < n-1 before exponentiating. |
3754 | | * SP 800-56B Rev2 sec 7.1.1 (RSAEP) step 1. Passed 1 rather than the |
3755 | | * caller's checkSmallCt: the standard gives no opt-out on this path. */ |
3756 | | if ((type == RSA_PUBLIC_ENCRYPT || type == RSA_PRIVATE_ENCRYPT) && |
3757 | | key->state == RSA_STATE_ENCRYPT_EXPTMOD) { |
3758 | | |
3759 | | ret = RsaFunctionCheckIn(in, inLen, key, 1); |
3760 | | if (ret != 0) { |
3761 | | return ret; |
3762 | | } |
3763 | | } |
3764 | | #endif |
3765 | | |
3766 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA) |
3767 | | if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_RSA && |
3768 | | key->n.raw.len > 0) { |
3769 | | /* wc_RsaFunctionAsync dispatches to the SP nonblock state machine |
3770 | | * in its compute path when key->nb is attached - wolfAsync_DoSw |
3771 | | * (in wolfcrypt/src/async.c) translates per-yield FP_WOULDBLOCK |
3772 | | * (alias of MP_WOULDBLOCK) into WC_PENDING_E so the TLS / async |
3773 | | * event loop can drive completion. */ |
3774 | | ret = wc_RsaFunctionAsync(in, inLen, out, outLen, type, key, rng); |
3775 | | } |
3776 | | else |
3777 | | #endif |
3778 | | #ifdef WC_RSA_NONBLOCK |
3779 | | if (key->nb) { |
3780 | | /* Direct (non-async) nonblock dispatch - the caller (e.g. wolfcrypt |
3781 | | * test) drives the loop on MP_WOULDBLOCK directly. Reached when no |
3782 | | * async marker is set on the key. */ |
3783 | | ret = wc_RsaFunctionNonBlock(in, inLen, out, outLen, type, key); |
3784 | | } |
3785 | | else |
3786 | | #endif |
3787 | 0 | { |
3788 | 0 | ret = wc_RsaFunctionSync(in, inLen, out, outLen, type, key, rng); |
3789 | 0 | } |
3790 | | |
3791 | | /* handle error */ |
3792 | 0 | if (ret < 0 && ret != WC_NO_ERR_TRACE(WC_PENDING_E) |
3793 | | #ifdef WC_RSA_NONBLOCK |
3794 | | && ret != FP_WOULDBLOCK |
3795 | | #endif |
3796 | 0 | ) { |
3797 | 0 | if (ret == WC_NO_ERR_TRACE(MP_EXPTMOD_E)) { |
3798 | | /* This can happen due to incorrectly set FP_MAX_BITS or missing XREALLOC */ |
3799 | 0 | WOLFSSL_MSG("RSA_FUNCTION MP_EXPTMOD_E: memory/config problem"); |
3800 | 0 | } |
3801 | |
|
3802 | 0 | key->state = RSA_STATE_NONE; |
3803 | 0 | wc_RsaCleanup(key); |
3804 | 0 | } |
3805 | 0 | return ret; |
3806 | 0 | #endif /* !WOLF_CRYPTO_CB_ONLY_RSA */ |
3807 | 0 | } |
3808 | | |
3809 | | int wc_RsaFunction(const byte* in, word32 inLen, byte* out, |
3810 | | word32* outLen, int type, RsaKey* key, WC_RNG* rng) |
3811 | 0 | { |
3812 | | /* Always check for ciphertext of 0 or 1. (Shouldn't for OAEP decrypt.) */ |
3813 | 0 | return wc_RsaFunction_ex(in, inLen, out, outLen, type, key, rng, 1); |
3814 | 0 | } |
3815 | | |
3816 | | #ifndef WOLFSSL_RSA_VERIFY_ONLY |
3817 | | /* Internal Wrappers */ |
3818 | | /* Gives the option of choosing padding type |
3819 | | in : input to be encrypted |
3820 | | inLen: length of input buffer |
3821 | | out: encrypted output |
3822 | | outLen: length of encrypted output buffer |
3823 | | key : wolfSSL initialized RSA key struct |
3824 | | rng : wolfSSL initialized random number struct |
3825 | | rsa_type : type of RSA: RSA_PUBLIC_ENCRYPT, RSA_PUBLIC_DECRYPT, |
3826 | | RSA_PRIVATE_ENCRYPT or RSA_PRIVATE_DECRYPT |
3827 | | pad_value: RSA_BLOCK_TYPE_1 or RSA_BLOCK_TYPE_2 |
3828 | | pad_type : type of padding: WC_RSA_PKCSV15_PAD, WC_RSA_OAEP_PAD, |
3829 | | WC_RSA_NO_PAD or WC_RSA_PSS_PAD |
3830 | | hash : type of hash algorithm to use found in wolfssl/wolfcrypt/hash.h |
3831 | | mgf : type of mask generation function to use |
3832 | | label : optional label |
3833 | | labelSz : size of optional label buffer |
3834 | | saltLen : Length of salt used in PSS |
3835 | | rng : random number generator */ |
3836 | | static int RsaPublicEncryptEx(const byte* in, word32 inLen, byte* out, |
3837 | | word32 outLen, RsaKey* key, int rsa_type, |
3838 | | byte pad_value, int pad_type, |
3839 | | enum wc_HashType hash, int mgf, |
3840 | | byte* label, word32 labelSz, int saltLen, |
3841 | | WC_RNG* rng) |
3842 | 0 | { |
3843 | 0 | int ret = 0; |
3844 | 0 | int sz; |
3845 | 0 | int state; |
3846 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD) |
3847 | | RsaPadding padding; |
3848 | | #endif |
3849 | |
|
3850 | 0 | if (in == NULL || inLen == 0 || out == NULL || key == NULL) { |
3851 | 0 | return BAD_FUNC_ARG; |
3852 | 0 | } |
3853 | | |
3854 | 0 | sz = wc_RsaEncryptSize(key); |
3855 | 0 | if (sz > (int)outLen) { |
3856 | 0 | return RSA_BUFFER_E; |
3857 | 0 | } |
3858 | | |
3859 | 0 | if (sz < RSA_MIN_PAD_SZ || sz > (int)RSA_MAX_SIZE/8) { |
3860 | 0 | return WC_KEY_SIZE_E; |
3861 | 0 | } |
3862 | | |
3863 | 0 | if (inLen > (word32)(sz - RSA_MIN_PAD_SZ)) { |
3864 | | #ifdef WC_RSA_NO_PADDING |
3865 | | /* In the case that no padding is used the input length can and should |
3866 | | * be the same size as the RSA key. */ |
3867 | | if (pad_type != WC_RSA_NO_PAD) |
3868 | | #endif |
3869 | 0 | return RSA_BUFFER_E; |
3870 | 0 | } |
3871 | | |
3872 | 0 | #ifndef WOLFSSL_BIND |
3873 | 0 | state = key->state; |
3874 | | #else |
3875 | | /* Bind9 shares the EVP_PKEY struct across multiple threads so let's just |
3876 | | * force a restart on each RsaPublicEncryptEx call for it. */ |
3877 | | state = RSA_STATE_NONE; |
3878 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3879 | | #error wolfSSL does not handle building bind support with async crypto |
3880 | | #endif |
3881 | | #endif |
3882 | 0 | switch (state) { |
3883 | 0 | case RSA_STATE_NONE: |
3884 | 0 | case RSA_STATE_ENCRYPT_PAD: |
3885 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA) && \ |
3886 | | defined(HAVE_CAVIUM) |
3887 | | if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_RSA && |
3888 | | pad_type != WC_RSA_PSS_PAD && key->n.raw.buf) { |
3889 | | /* Async operations that include padding */ |
3890 | | if (rsa_type == RSA_PUBLIC_ENCRYPT && |
3891 | | pad_value == RSA_BLOCK_TYPE_2) { |
3892 | | key->state = RSA_STATE_ENCRYPT_RES; |
3893 | | key->dataLen = key->n.raw.len; |
3894 | | return NitroxRsaPublicEncrypt(in, inLen, out, outLen, key); |
3895 | | } |
3896 | | else if (rsa_type == RSA_PRIVATE_ENCRYPT && |
3897 | | pad_value == RSA_BLOCK_TYPE_1) { |
3898 | | key->state = RSA_STATE_ENCRYPT_RES; |
3899 | | key->dataLen = key->n.raw.len; |
3900 | | return NitroxRsaSSL_Sign(in, inLen, out, outLen, key); |
3901 | | } |
3902 | | } |
3903 | | #elif defined(WOLFSSL_CRYPTOCELL) |
3904 | | if (rsa_type == RSA_PUBLIC_ENCRYPT && |
3905 | | pad_value == RSA_BLOCK_TYPE_2) { |
3906 | | |
3907 | | return cc310_RsaPublicEncrypt(in, inLen, out, outLen, key); |
3908 | | } |
3909 | | else if (rsa_type == RSA_PRIVATE_ENCRYPT && |
3910 | | pad_value == RSA_BLOCK_TYPE_1) { |
3911 | | return cc310_RsaSSL_Sign(in, inLen, out, outLen, key, |
3912 | | cc310_hashModeRSA(hash, 0)); |
3913 | | } |
3914 | | #elif defined(WOLFSSL_MICROCHIP_TA100) |
3915 | | if (rsa_type == RSA_PUBLIC_ENCRYPT && |
3916 | | pad_value == RSA_BLOCK_TYPE_2) { |
3917 | | if (key->uKeyH != 0) { |
3918 | | return wc_Microchip_rsa_encrypt(in, inLen, out, outLen, key); |
3919 | | } |
3920 | | return WC_HW_E; |
3921 | | } |
3922 | | else if (rsa_type == RSA_PRIVATE_ENCRYPT && |
3923 | | pad_value == RSA_BLOCK_TYPE_1) { |
3924 | | if (key->rKeyH != 0) { |
3925 | | if (pad_type != WC_RSA_PSS_PAD) { |
3926 | | return WC_HW_E; |
3927 | | } |
3928 | | return wc_Microchip_rsa_sign(in, inLen, out, outLen, key); |
3929 | | } |
3930 | | return WC_HW_E; |
3931 | | } |
3932 | | #elif defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA) |
3933 | | #ifdef WOLFSSL_SE050_ONLY_KEY_ID |
3934 | | /* Only offload to the SE050 when the key is resident in hardware; |
3935 | | * software keys (keyIdSet == 0) fall through to the software path. */ |
3936 | | if (key->keyIdSet) |
3937 | | #endif |
3938 | | { |
3939 | | if (rsa_type == RSA_PUBLIC_ENCRYPT && pad_value == RSA_BLOCK_TYPE_2) { |
3940 | | return se050_rsa_public_encrypt(in, inLen, out, outLen, key, |
3941 | | rsa_type, pad_value, pad_type, hash, |
3942 | | mgf, label, labelSz, sz); |
3943 | | } |
3944 | | else if (rsa_type == RSA_PRIVATE_ENCRYPT && |
3945 | | pad_value == RSA_BLOCK_TYPE_1 && |
3946 | | pad_type != WC_RSA_PSS_PAD) { |
3947 | | /* SE050 handles PKCS#1 v1.5 signing directly. PSS signing falls |
3948 | | * through to software path because the SE050 PSS sign API |
3949 | | * (Se05x_API_RSASign) is hash-then-sign and does not support |
3950 | | * signing a pre-computed digest without double-hashing. */ |
3951 | | return se050_rsa_sign(in, inLen, out, outLen, key, rsa_type, |
3952 | | pad_value, pad_type, hash, mgf, label, |
3953 | | labelSz, sz); |
3954 | | } |
3955 | | } |
3956 | | #endif /* RSA CRYPTO HW */ |
3957 | |
|
3958 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD) |
3959 | | if (key->devId != INVALID_DEVID) { |
3960 | | XMEMSET(&padding, 0, sizeof(RsaPadding)); |
3961 | | padding.pad_value = pad_value; |
3962 | | padding.pad_type = pad_type; |
3963 | | padding.hash = hash; |
3964 | | padding.mgf = mgf; |
3965 | | padding.label = label; |
3966 | | padding.labelSz = labelSz; |
3967 | | padding.saltLen = saltLen; |
3968 | | ret = wc_CryptoCb_RsaPad(in, inLen, out, &outLen, rsa_type, key, rng, |
3969 | | &padding); |
3970 | | |
3971 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
3972 | | if (ret < 0) { |
3973 | | break; |
3974 | | } |
3975 | | |
3976 | | ret = outLen; |
3977 | | break; |
3978 | | } |
3979 | | } |
3980 | | #endif |
3981 | 0 | key->state = RSA_STATE_ENCRYPT_PAD; |
3982 | 0 | ret = wc_RsaPad_ex(in, inLen, out, (word32)sz, pad_value, rng, pad_type, |
3983 | 0 | hash, mgf, label, labelSz, saltLen, |
3984 | 0 | mp_count_bits(&key->n), key->heap); |
3985 | 0 | if (ret < 0) { |
3986 | 0 | break; |
3987 | 0 | } |
3988 | | |
3989 | 0 | key->state = RSA_STATE_ENCRYPT_EXPTMOD; |
3990 | 0 | FALL_THROUGH; |
3991 | |
|
3992 | 0 | case RSA_STATE_ENCRYPT_EXPTMOD: |
3993 | |
|
3994 | 0 | key->dataLen = outLen; |
3995 | 0 | ret = wc_RsaFunction(out, (word32)sz, out, &key->dataLen, rsa_type, key, |
3996 | 0 | rng); |
3997 | |
|
3998 | 0 | if (ret >= 0 || ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3999 | 0 | key->state = RSA_STATE_ENCRYPT_RES; |
4000 | 0 | } |
4001 | 0 | if (ret < 0) { |
4002 | 0 | break; |
4003 | 0 | } |
4004 | | |
4005 | 0 | FALL_THROUGH; |
4006 | |
|
4007 | 0 | case RSA_STATE_ENCRYPT_RES: |
4008 | 0 | ret = (int)key->dataLen; |
4009 | 0 | break; |
4010 | | |
4011 | 0 | default: |
4012 | 0 | ret = BAD_STATE_E; |
4013 | 0 | break; |
4014 | 0 | } |
4015 | | |
4016 | | /* if async pending then return and skip done cleanup below */ |
4017 | 0 | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E) |
4018 | | #ifdef WC_RSA_NONBLOCK |
4019 | | || ret == FP_WOULDBLOCK |
4020 | | #endif |
4021 | 0 | ) { |
4022 | 0 | return ret; |
4023 | 0 | } |
4024 | | |
4025 | 0 | key->state = RSA_STATE_NONE; |
4026 | 0 | wc_RsaCleanup(key); |
4027 | |
|
4028 | 0 | return ret; |
4029 | 0 | } |
4030 | | |
4031 | | #endif |
4032 | | |
4033 | | /* Gives the option of choosing padding type |
4034 | | in : input to be decrypted |
4035 | | inLen: length of input buffer |
4036 | | out: decrypted message |
4037 | | outLen: length of decrypted message in bytes |
4038 | | outPtr: optional inline output pointer (if provided doing inline) |
4039 | | key : wolfSSL initialized RSA key struct |
4040 | | rsa_type : type of RSA: RSA_PUBLIC_ENCRYPT, RSA_PUBLIC_DECRYPT, |
4041 | | RSA_PRIVATE_ENCRYPT or RSA_PRIVATE_DECRYPT |
4042 | | pad_value: RSA_BLOCK_TYPE_1 or RSA_BLOCK_TYPE_2 |
4043 | | pad_type : type of padding: WC_RSA_PKCSV15_PAD, WC_RSA_OAEP_PAD, |
4044 | | WC_RSA_NO_PAD, WC_RSA_PSS_PAD |
4045 | | hash : type of hash algorithm to use found in wolfssl/wolfcrypt/hash.h |
4046 | | mgf : type of mask generation function to use |
4047 | | label : optional label |
4048 | | labelSz : size of optional label buffer |
4049 | | saltLen : Length of salt used in PSS |
4050 | | rng : random number generator */ |
4051 | | static int RsaPrivateDecryptEx(const byte* in, word32 inLen, byte* out, |
4052 | | word32 outLen, byte** outPtr, RsaKey* key, |
4053 | | int rsa_type, byte pad_value, int pad_type, |
4054 | | enum wc_HashType hash, int mgf, |
4055 | | byte* label, word32 labelSz, int saltLen, |
4056 | | WC_RNG* rng) |
4057 | 0 | { |
4058 | 0 | int ret = WC_NO_ERR_TRACE(RSA_WRONG_TYPE_E); |
4059 | 0 | byte* pad = NULL; |
4060 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD) |
4061 | | RsaPadding padding; |
4062 | | #endif |
4063 | |
|
4064 | 0 | if (in == NULL || inLen == 0 || out == NULL || key == NULL) { |
4065 | 0 | return BAD_FUNC_ARG; |
4066 | 0 | } |
4067 | | |
4068 | 0 | switch (key->state) { |
4069 | 0 | case RSA_STATE_NONE: |
4070 | 0 | key->dataLen = inLen; |
4071 | |
|
4072 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA) && \ |
4073 | | defined(HAVE_CAVIUM) |
4074 | | /* Async operations that include padding */ |
4075 | | if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_RSA && |
4076 | | pad_type != WC_RSA_PSS_PAD) { |
4077 | | #ifndef WOLFSSL_RSA_PUBLIC_ONLY |
4078 | | if (rsa_type == RSA_PRIVATE_DECRYPT && |
4079 | | pad_value == RSA_BLOCK_TYPE_2) { |
4080 | | key->state = RSA_STATE_DECRYPT_RES; |
4081 | | key->data = NULL; |
4082 | | return NitroxRsaPrivateDecrypt(in, inLen, out, &key->dataLen, |
4083 | | key); |
4084 | | #endif |
4085 | | } |
4086 | | else if (rsa_type == RSA_PUBLIC_DECRYPT && |
4087 | | pad_value == RSA_BLOCK_TYPE_1) { |
4088 | | key->state = RSA_STATE_DECRYPT_RES; |
4089 | | key->data = NULL; |
4090 | | return NitroxRsaSSL_Verify(in, inLen, out, &key->dataLen, key); |
4091 | | } |
4092 | | } |
4093 | | #elif defined(WOLFSSL_CRYPTOCELL) |
4094 | | if (rsa_type == RSA_PRIVATE_DECRYPT && |
4095 | | pad_value == RSA_BLOCK_TYPE_2) { |
4096 | | ret = cc310_RsaPublicDecrypt(in, inLen, out, outLen, key); |
4097 | | if (outPtr != NULL) |
4098 | | *outPtr = out; /* for inline */ |
4099 | | return ret; |
4100 | | } |
4101 | | else if (rsa_type == RSA_PUBLIC_DECRYPT && |
4102 | | pad_value == RSA_BLOCK_TYPE_1) { |
4103 | | return cc310_RsaSSL_Verify(in, inLen, out, key, |
4104 | | cc310_hashModeRSA(hash, 0)); |
4105 | | } |
4106 | | #elif defined(WOLFSSL_MICROCHIP_TA100) |
4107 | | if (rsa_type == RSA_PRIVATE_DECRYPT && |
4108 | | pad_value == RSA_BLOCK_TYPE_2) { |
4109 | | if (key->rKeyH != 0) { |
4110 | | return wc_Microchip_rsa_decrypt(in, inLen, out, outLen, key); |
4111 | | } |
4112 | | return WC_HW_E; |
4113 | | } |
4114 | | /* Note: RSA_PUBLIC_DECRYPT (verify) is intentionally not intercepted |
4115 | | * here. wc_Microchip_rsa_verify takes a digest as input, not a raw |
4116 | | * signature blob; the proper TA100 short-circuit lives in the |
4117 | | * wc_RsaPSS_CheckPadding / wc_RsaPSS_VerifyCheck path which has the |
4118 | | * digest available. */ |
4119 | | #elif defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA) |
4120 | | #ifdef WOLFSSL_SE050_ONLY_KEY_ID |
4121 | | /* Only offload to the SE050 when the key is resident in hardware; |
4122 | | * software keys (keyIdSet == 0) fall through to the software path. */ |
4123 | | if (key->keyIdSet) |
4124 | | #endif |
4125 | | { |
4126 | | if (rsa_type == RSA_PRIVATE_DECRYPT && pad_value == RSA_BLOCK_TYPE_2) { |
4127 | | ret = se050_rsa_private_decrypt(in, inLen, out, outLen, key, |
4128 | | rsa_type, pad_value, pad_type, hash, |
4129 | | mgf, label, labelSz); |
4130 | | if (outPtr != NULL) { |
4131 | | *outPtr = out; |
4132 | | } |
4133 | | return ret; |
4134 | | } |
4135 | | #if !defined(WOLFSSL_SE050_NO_RSA_VERIFY) |
4136 | | else if (rsa_type == RSA_PUBLIC_DECRYPT && |
4137 | | pad_value == RSA_BLOCK_TYPE_1 && |
4138 | | pad_type != WC_RSA_PSS_PAD) { |
4139 | | /* SE050 handles PKCS#1 v1.5 verification directly. PSS |
4140 | | * verification falls through to software path to match the |
4141 | | * software PSS signing path (SE050 PSS sign uses hash-then-sign |
4142 | | * which double-hashes a pre-computed digest). */ |
4143 | | ret = se050_rsa_verify(in, inLen, out, outLen, key, rsa_type, |
4144 | | pad_value, pad_type, hash, mgf, label, |
4145 | | labelSz); |
4146 | | if (outPtr != NULL) { |
4147 | | *outPtr = out; |
4148 | | } |
4149 | | return ret; |
4150 | | } |
4151 | | #endif /* !WOLFSSL_SE050_NO_RSA_VERIFY */ |
4152 | | } |
4153 | | #endif /* RSA CRYPTO HW */ |
4154 | | |
4155 | |
|
4156 | 0 | #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE) && \ |
4157 | 0 | !defined(WOLFSSL_NO_MALLOC) |
4158 | | /* verify the tmp ptr is NULL, otherwise indicates bad state */ |
4159 | 0 | if (key->data != NULL) { |
4160 | 0 | ret = BAD_STATE_E; |
4161 | 0 | break; |
4162 | 0 | } |
4163 | | |
4164 | | /* if not doing this inline then allocate a buffer for it */ |
4165 | 0 | if (outPtr == NULL) { |
4166 | 0 | key->data = (byte*)XMALLOC(inLen, key->heap, |
4167 | 0 | DYNAMIC_TYPE_WOLF_BIGINT); |
4168 | 0 | key->dataIsAlloc = 1; |
4169 | 0 | if (key->data == NULL) { |
4170 | 0 | ret = MEMORY_E; |
4171 | 0 | break; |
4172 | 0 | } |
4173 | 0 | XMEMCPY(key->data, in, inLen); |
4174 | 0 | key->dataLen = inLen; |
4175 | 0 | } |
4176 | 0 | else { |
4177 | 0 | key->dataIsAlloc = 0; |
4178 | 0 | key->data = out; |
4179 | 0 | } |
4180 | 0 | #endif |
4181 | | |
4182 | 0 | key->state = RSA_STATE_DECRYPT_EXPTMOD; |
4183 | 0 | FALL_THROUGH; |
4184 | |
|
4185 | 0 | case RSA_STATE_DECRYPT_EXPTMOD: |
4186 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD) |
4187 | | if ((key->devId != INVALID_DEVID) |
4188 | | #if !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) && \ |
4189 | | !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY) |
4190 | | && (rsa_type != RSA_PUBLIC_DECRYPT) |
4191 | | #endif |
4192 | | ) { |
4193 | | /* Everything except verify goes to crypto cb if |
4194 | | * WOLF_CRYPTO_CB_RSA_PAD defined */ |
4195 | | XMEMSET(&padding, 0, sizeof(RsaPadding)); |
4196 | | padding.pad_value = pad_value; |
4197 | | padding.pad_type = pad_type; |
4198 | | padding.hash = hash; |
4199 | | padding.mgf = mgf; |
4200 | | padding.label = label; |
4201 | | padding.labelSz = labelSz; |
4202 | | padding.saltLen = saltLen; |
4203 | | ret = wc_CryptoCb_RsaPad(in, inLen, out, |
4204 | | &outLen, rsa_type, key, rng, &padding); |
4205 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
4206 | | if (outPtr != NULL) { |
4207 | | *outPtr = out; |
4208 | | } |
4209 | | if (ret == 0) { |
4210 | | ret = (int)outLen; |
4211 | | } |
4212 | | break; |
4213 | | } |
4214 | | } |
4215 | | #endif |
4216 | 0 | #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE) && \ |
4217 | 0 | !defined(WOLFSSL_NO_MALLOC) |
4218 | 0 | ret = wc_RsaFunction_ex(key->data, inLen, key->data, &key->dataLen, |
4219 | 0 | rsa_type, key, rng, |
4220 | 0 | pad_type != WC_RSA_OAEP_PAD); |
4221 | | #else |
4222 | | ret = wc_RsaFunction_ex(in, inLen, out, &key->dataLen, rsa_type, key, |
4223 | | rng, pad_type != WC_RSA_OAEP_PAD); |
4224 | | #endif |
4225 | |
|
4226 | 0 | if (ret >= 0 || ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
4227 | 0 | key->state = RSA_STATE_DECRYPT_UNPAD; |
4228 | 0 | } |
4229 | 0 | if (ret < 0) { |
4230 | 0 | break; |
4231 | 0 | } |
4232 | | |
4233 | 0 | FALL_THROUGH; |
4234 | |
|
4235 | 0 | case RSA_STATE_DECRYPT_UNPAD: |
4236 | 0 | #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE) && \ |
4237 | 0 | !defined(WOLFSSL_NO_MALLOC) |
4238 | 0 | ret = wc_RsaUnPad_ex(key->data, |
4239 | 0 | key->dataLen, &pad, pad_value, pad_type, hash, mgf, |
4240 | 0 | label, labelSz, saltLen, mp_count_bits(&key->n), key->heap); |
4241 | | #else |
4242 | | ret = wc_RsaUnPad_ex(out, |
4243 | | key->dataLen, &pad, pad_value, pad_type, hash, mgf, label, |
4244 | | labelSz, saltLen, mp_count_bits(&key->n), key->heap); |
4245 | | #endif |
4246 | 0 | if (rsa_type == RSA_PUBLIC_DECRYPT && ret > (int)outLen) { |
4247 | 0 | ret = RSA_BUFFER_E; |
4248 | 0 | } |
4249 | 0 | else if (ret >= 0 && pad != NULL) { |
4250 | | /* only copy output if not inline */ |
4251 | 0 | if (outPtr == NULL) { |
4252 | 0 | #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE) && \ |
4253 | 0 | !defined(WOLFSSL_NO_MALLOC) |
4254 | 0 | if (rsa_type == RSA_PRIVATE_DECRYPT) { |
4255 | 0 | word32 i = 0; |
4256 | 0 | word32 j; |
4257 | 0 | byte last = 0; |
4258 | 0 | int start = (int)((size_t)pad - (size_t)key->data); |
4259 | |
|
4260 | 0 | for (j = 0; j < key->dataLen; j++) { |
4261 | 0 | signed char incMask; |
4262 | 0 | signed char maskData; |
4263 | | |
4264 | | /* When j < start + outLen then out[i] = key->data[j] |
4265 | | * else out[i] = last |
4266 | | */ |
4267 | 0 | maskData = (signed char)ctMaskLT((int)j, |
4268 | 0 | start + (int)outLen); |
4269 | 0 | out[i] = (byte)(key->data[j] & maskData ) | |
4270 | 0 | (byte)(last & (~maskData)); |
4271 | 0 | last = out[i]; |
4272 | | |
4273 | | /* Increment i when j is in range: |
4274 | | * [start..(start + outLen - 1)]. */ |
4275 | 0 | incMask = (signed char)ctMaskGTE((int)j, start); |
4276 | 0 | incMask &= (signed char)ctMaskLT((int)j, |
4277 | 0 | start + (int)outLen - 1); |
4278 | 0 | i += (word32)((byte)(-incMask)); |
4279 | 0 | } |
4280 | 0 | } |
4281 | 0 | else |
4282 | 0 | #endif |
4283 | 0 | { |
4284 | 0 | XMEMCPY(out, pad, (size_t)ret); |
4285 | 0 | } |
4286 | 0 | } |
4287 | 0 | else { |
4288 | 0 | *outPtr = pad; |
4289 | 0 | } |
4290 | |
|
4291 | 0 | #if !defined(WOLFSSL_RSA_VERIFY_ONLY) |
4292 | 0 | ret = ctMaskSelInt(ctMaskLTE(ret, (int)outLen), ret, |
4293 | 0 | WC_NO_ERR_TRACE(RSA_BUFFER_E)); |
4294 | 0 | #ifndef WOLFSSL_RSA_DECRYPT_TO_0_LEN |
4295 | 0 | ret = ctMaskSelInt(ctMaskNotEq(ret, 0), ret, |
4296 | 0 | WC_NO_ERR_TRACE(RSA_BUFFER_E)); |
4297 | 0 | #endif |
4298 | | #else |
4299 | | if (outLen < (word32)ret) |
4300 | | ret = RSA_BUFFER_E; |
4301 | | #endif |
4302 | 0 | } |
4303 | |
|
4304 | 0 | key->state = RSA_STATE_DECRYPT_RES; |
4305 | 0 | FALL_THROUGH; |
4306 | |
|
4307 | 0 | case RSA_STATE_DECRYPT_RES: |
4308 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA) && \ |
4309 | | defined(HAVE_CAVIUM) |
4310 | | if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_RSA && |
4311 | | pad_type != WC_RSA_PSS_PAD) { |
4312 | | ret = key->asyncDev.event.ret; |
4313 | | if (ret >= 0) { |
4314 | | /* convert result */ |
4315 | | byte* dataLen = (byte*)&key->dataLen; |
4316 | | ret = (dataLen[0] << 8) | (dataLen[1]); |
4317 | | |
4318 | | if (outPtr) |
4319 | | *outPtr = in; |
4320 | | } |
4321 | | } |
4322 | | #endif |
4323 | 0 | break; |
4324 | | |
4325 | 0 | default: |
4326 | 0 | ret = BAD_STATE_E; |
4327 | 0 | break; |
4328 | 0 | } |
4329 | | |
4330 | | /* if async pending then return and skip done cleanup below */ |
4331 | 0 | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E) |
4332 | | #ifdef WC_RSA_NONBLOCK |
4333 | | || ret == FP_WOULDBLOCK |
4334 | | #endif |
4335 | 0 | ) { |
4336 | 0 | return ret; |
4337 | 0 | } |
4338 | | |
4339 | 0 | key->state = RSA_STATE_NONE; |
4340 | 0 | wc_RsaCleanup(key); |
4341 | |
|
4342 | 0 | return ret; |
4343 | 0 | } |
4344 | | |
4345 | | |
4346 | | #ifndef WOLFSSL_RSA_VERIFY_ONLY |
4347 | | /* Public RSA Functions */ |
4348 | | int wc_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out, word32 outLen, |
4349 | | RsaKey* key, WC_RNG* rng) |
4350 | 0 | { |
4351 | 0 | int ret; |
4352 | 0 | ret = RsaPublicEncryptEx(in, inLen, out, outLen, key, |
4353 | 0 | RSA_PUBLIC_ENCRYPT, RSA_BLOCK_TYPE_2, WC_RSA_PKCSV15_PAD, |
4354 | 0 | WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0, 0, rng); |
4355 | 0 | return ret; |
4356 | 0 | } |
4357 | | |
4358 | | |
4359 | | #if !defined(WC_NO_RSA_OAEP) || defined(WC_RSA_NO_PADDING) |
4360 | | int wc_RsaPublicEncrypt_ex(const byte* in, word32 inLen, byte* out, |
4361 | | word32 outLen, RsaKey* key, WC_RNG* rng, int type, |
4362 | | enum wc_HashType hash, int mgf, byte* label, |
4363 | | word32 labelSz) |
4364 | 0 | { |
4365 | 0 | int ret; |
4366 | 0 | ret = RsaPublicEncryptEx(in, inLen, out, outLen, key, RSA_PUBLIC_ENCRYPT, |
4367 | 0 | RSA_BLOCK_TYPE_2, type, hash, mgf, label, labelSz, 0, rng); |
4368 | 0 | return ret; |
4369 | 0 | } |
4370 | | #endif /* WC_NO_RSA_OAEP */ |
4371 | | #endif |
4372 | | |
4373 | | |
4374 | | #ifndef WOLFSSL_RSA_PUBLIC_ONLY |
4375 | | int wc_RsaPrivateDecryptInline(byte* in, word32 inLen, byte** out, RsaKey* key) |
4376 | 0 | { |
4377 | 0 | WC_RNG* rng; |
4378 | 0 | int ret; |
4379 | 0 | #if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG) |
4380 | 0 | if (key == NULL) { |
4381 | 0 | return BAD_FUNC_ARG; |
4382 | 0 | } |
4383 | 0 | rng = key->rng; |
4384 | | #else |
4385 | | rng = NULL; |
4386 | | #endif |
4387 | 0 | ret = RsaPrivateDecryptEx(in, inLen, in, inLen, out, key, |
4388 | 0 | RSA_PRIVATE_DECRYPT, RSA_BLOCK_TYPE_2, WC_RSA_PKCSV15_PAD, |
4389 | 0 | WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0, 0, rng); |
4390 | 0 | return ret; |
4391 | 0 | } |
4392 | | |
4393 | | |
4394 | | #ifndef WC_NO_RSA_OAEP |
4395 | | int wc_RsaPrivateDecryptInline_ex(byte* in, word32 inLen, byte** out, |
4396 | | RsaKey* key, int type, enum wc_HashType hash, |
4397 | | int mgf, byte* label, word32 labelSz) |
4398 | 0 | { |
4399 | 0 | WC_RNG* rng; |
4400 | 0 | int ret; |
4401 | 0 | #if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG) |
4402 | 0 | if (key == NULL) { |
4403 | 0 | return BAD_FUNC_ARG; |
4404 | 0 | } |
4405 | 0 | rng = key->rng; |
4406 | | #else |
4407 | | rng = NULL; |
4408 | | #endif |
4409 | 0 | ret = RsaPrivateDecryptEx(in, inLen, in, inLen, out, key, |
4410 | 0 | RSA_PRIVATE_DECRYPT, RSA_BLOCK_TYPE_2, type, hash, |
4411 | 0 | mgf, label, labelSz, 0, rng); |
4412 | 0 | return ret; |
4413 | 0 | } |
4414 | | #endif /* WC_NO_RSA_OAEP */ |
4415 | | |
4416 | | |
4417 | | int wc_RsaPrivateDecrypt(const byte* in, word32 inLen, byte* out, |
4418 | | word32 outLen, RsaKey* key) |
4419 | 0 | { |
4420 | 0 | WC_RNG* rng; |
4421 | 0 | int ret; |
4422 | 0 | #if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG) |
4423 | 0 | if (key == NULL) { |
4424 | 0 | return BAD_FUNC_ARG; |
4425 | 0 | } |
4426 | 0 | rng = key->rng; |
4427 | | #else |
4428 | | rng = NULL; |
4429 | | #endif |
4430 | 0 | ret = RsaPrivateDecryptEx(in, inLen, out, outLen, NULL, key, |
4431 | 0 | RSA_PRIVATE_DECRYPT, RSA_BLOCK_TYPE_2, WC_RSA_PKCSV15_PAD, |
4432 | 0 | WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0, 0, rng); |
4433 | 0 | return ret; |
4434 | 0 | } |
4435 | | |
4436 | | #if !defined(WC_NO_RSA_OAEP) || defined(WC_RSA_NO_PADDING) |
4437 | | int wc_RsaPrivateDecrypt_ex(const byte* in, word32 inLen, byte* out, |
4438 | | word32 outLen, RsaKey* key, int type, |
4439 | | enum wc_HashType hash, int mgf, byte* label, |
4440 | | word32 labelSz) |
4441 | 0 | { |
4442 | 0 | WC_RNG* rng; |
4443 | 0 | int ret; |
4444 | 0 | #if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG) |
4445 | 0 | if (key == NULL) { |
4446 | 0 | return BAD_FUNC_ARG; |
4447 | 0 | } |
4448 | 0 | rng = key->rng; |
4449 | | #else |
4450 | | rng = NULL; |
4451 | | #endif |
4452 | 0 | ret = RsaPrivateDecryptEx(in, inLen, out, outLen, NULL, key, |
4453 | 0 | RSA_PRIVATE_DECRYPT, RSA_BLOCK_TYPE_2, type, hash, mgf, label, |
4454 | 0 | labelSz, 0, rng); |
4455 | 0 | return ret; |
4456 | 0 | } |
4457 | | #endif /* WC_NO_RSA_OAEP || WC_RSA_NO_PADDING */ |
4458 | | #endif /* WOLFSSL_RSA_PUBLIC_ONLY */ |
4459 | | |
4460 | | #if !defined(WOLFSSL_CRYPTOCELL) |
4461 | | int wc_RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out, RsaKey* key) |
4462 | 0 | { |
4463 | 0 | WC_RNG* rng; |
4464 | 0 | int ret; |
4465 | 0 | #if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG) |
4466 | 0 | if (key == NULL) { |
4467 | 0 | return BAD_FUNC_ARG; |
4468 | 0 | } |
4469 | 0 | rng = key->rng; |
4470 | | #else |
4471 | | rng = NULL; |
4472 | | #endif |
4473 | 0 | ret = RsaPrivateDecryptEx(in, inLen, in, inLen, out, key, |
4474 | 0 | RSA_PUBLIC_DECRYPT, RSA_BLOCK_TYPE_1, WC_RSA_PKCSV15_PAD, |
4475 | 0 | WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0, 0, rng); |
4476 | 0 | return ret; |
4477 | 0 | } |
4478 | | #endif |
4479 | | |
4480 | | #ifndef WOLFSSL_RSA_VERIFY_INLINE |
4481 | | int wc_RsaSSL_Verify(const byte* in, word32 inLen, byte* out, word32 outLen, |
4482 | | RsaKey* key) |
4483 | 0 | { |
4484 | 0 | return wc_RsaSSL_Verify_ex(in, inLen, out, outLen, key, WC_RSA_PKCSV15_PAD); |
4485 | 0 | } |
4486 | | |
4487 | | int wc_RsaSSL_Verify_ex(const byte* in, word32 inLen, byte* out, word32 outLen, |
4488 | | RsaKey* key, int pad_type) |
4489 | 0 | { |
4490 | 0 | int ret; |
4491 | 0 | ret = wc_RsaSSL_Verify_ex2(in, inLen, out, outLen, key, pad_type, |
4492 | 0 | WC_HASH_TYPE_NONE); |
4493 | 0 | return ret; |
4494 | 0 | } |
4495 | | |
4496 | | int wc_RsaSSL_Verify_ex2(const byte* in, word32 inLen, byte* out, word32 outLen, |
4497 | | RsaKey* key, int pad_type, enum wc_HashType hash) |
4498 | 0 | { |
4499 | 0 | WC_RNG* rng; |
4500 | 0 | int ret; |
4501 | |
|
4502 | 0 | if (key == NULL) { |
4503 | 0 | return BAD_FUNC_ARG; |
4504 | 0 | } |
4505 | | |
4506 | 0 | #if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG) |
4507 | 0 | rng = key->rng; |
4508 | | #else |
4509 | | rng = NULL; |
4510 | | #endif |
4511 | |
|
4512 | 0 | #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER |
4513 | 0 | ret = RsaPrivateDecryptEx(in, inLen, out, outLen, NULL, key, |
4514 | 0 | RSA_PUBLIC_DECRYPT, RSA_BLOCK_TYPE_1, pad_type, |
4515 | 0 | hash, wc_hash2mgf(hash), NULL, 0, RSA_PSS_SALT_LEN_DEFAULT, rng); |
4516 | | #else |
4517 | | ret = RsaPrivateDecryptEx(in, inLen, out, outLen, NULL, key, |
4518 | | RSA_PUBLIC_DECRYPT, RSA_BLOCK_TYPE_1, pad_type, |
4519 | | hash, wc_hash2mgf(hash), NULL, 0, RSA_PSS_SALT_LEN_DISCOVER, rng); |
4520 | | #endif |
4521 | 0 | return ret; |
4522 | 0 | } |
4523 | | #endif |
4524 | | |
4525 | | #ifdef WC_RSA_PSS |
4526 | | /* Verify the message signed with RSA-PSS. |
4527 | | * The input buffer is reused for the output buffer. |
4528 | | * Salt length is equal to hash length. |
4529 | | * |
4530 | | * in Buffer holding encrypted data. |
4531 | | * inLen Length of data in buffer. |
4532 | | * out Pointer to address containing the PSS data. |
4533 | | * hash Hash algorithm. |
4534 | | * mgf Mask generation function. |
4535 | | * key Public RSA key. |
4536 | | * returns the length of the PSS data on success and negative indicates failure. |
4537 | | */ |
4538 | | int wc_RsaPSS_VerifyInline(byte* in, word32 inLen, byte** out, |
4539 | | enum wc_HashType hash, int mgf, RsaKey* key) |
4540 | 0 | { |
4541 | 0 | #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER |
4542 | 0 | return wc_RsaPSS_VerifyInline_ex(in, inLen, out, hash, mgf, |
4543 | 0 | RSA_PSS_SALT_LEN_DEFAULT, key); |
4544 | | #else |
4545 | | return wc_RsaPSS_VerifyInline_ex(in, inLen, out, hash, mgf, |
4546 | | RSA_PSS_SALT_LEN_DISCOVER, key); |
4547 | | #endif |
4548 | 0 | } |
4549 | | |
4550 | | /* Verify the message signed with RSA-PSS. |
4551 | | * The input buffer is reused for the output buffer. |
4552 | | * |
4553 | | * in Buffer holding encrypted data. |
4554 | | * inLen Length of data in buffer. |
4555 | | * out Pointer to address containing the PSS data. |
4556 | | * hash Hash algorithm. |
4557 | | * mgf Mask generation function. |
4558 | | * key Public RSA key. |
4559 | | * saltLen Length of salt used. RSA_PSS_SALT_LEN_DEFAULT (-1) indicates salt |
4560 | | * length is the same as the hash length. RSA_PSS_SALT_LEN_DISCOVER |
4561 | | * indicates salt length is determined from the data. |
4562 | | * returns the length of the PSS data on success and negative indicates failure. |
4563 | | */ |
4564 | | int wc_RsaPSS_VerifyInline_ex(byte* in, word32 inLen, byte** out, |
4565 | | enum wc_HashType hash, int mgf, int saltLen, |
4566 | | RsaKey* key) |
4567 | 0 | { |
4568 | 0 | WC_RNG* rng; |
4569 | 0 | int ret; |
4570 | 0 | #if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG) |
4571 | 0 | if (key == NULL) { |
4572 | 0 | return BAD_FUNC_ARG; |
4573 | 0 | } |
4574 | 0 | rng = key->rng; |
4575 | | #else |
4576 | | rng = NULL; |
4577 | | #endif |
4578 | 0 | ret = RsaPrivateDecryptEx(in, inLen, in, inLen, out, key, |
4579 | 0 | RSA_PUBLIC_DECRYPT, RSA_BLOCK_TYPE_1, WC_RSA_PSS_PAD, |
4580 | 0 | hash, mgf, NULL, 0, saltLen, rng); |
4581 | 0 | return ret; |
4582 | 0 | } |
4583 | | |
4584 | | /* Verify the message signed with RSA-PSS. |
4585 | | * Salt length is equal to hash length. |
4586 | | * |
4587 | | * in Buffer holding encrypted data. |
4588 | | * inLen Length of data in buffer. |
4589 | | * out Pointer to address containing the PSS data. |
4590 | | * hash Hash algorithm. |
4591 | | * mgf Mask generation function. |
4592 | | * key Public RSA key. |
4593 | | * returns the length of the PSS data on success and negative indicates failure. |
4594 | | */ |
4595 | | int wc_RsaPSS_Verify(const byte* in, word32 inLen, byte* out, word32 outLen, |
4596 | | enum wc_HashType hash, int mgf, RsaKey* key) |
4597 | 0 | { |
4598 | 0 | #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER |
4599 | 0 | return wc_RsaPSS_Verify_ex(in, inLen, out, outLen, hash, mgf, |
4600 | 0 | RSA_PSS_SALT_LEN_DEFAULT, key); |
4601 | | #else |
4602 | | return wc_RsaPSS_Verify_ex(in, inLen, out, outLen, hash, mgf, |
4603 | | RSA_PSS_SALT_LEN_DISCOVER, key); |
4604 | | #endif |
4605 | 0 | } |
4606 | | |
4607 | | /* Verify the message signed with RSA-PSS. |
4608 | | * |
4609 | | * in Buffer holding encrypted data. |
4610 | | * inLen Length of data in buffer. |
4611 | | * out Pointer to address containing the PSS data. |
4612 | | * hash Hash algorithm. |
4613 | | * mgf Mask generation function. |
4614 | | * key Public RSA key. |
4615 | | * saltLen Length of salt used. RSA_PSS_SALT_LEN_DEFAULT (-1) indicates salt |
4616 | | * length is the same as the hash length. RSA_PSS_SALT_LEN_DISCOVER |
4617 | | * indicates salt length is determined from the data. |
4618 | | * returns the length of the PSS data on success and negative indicates failure. |
4619 | | */ |
4620 | | int wc_RsaPSS_Verify_ex(const byte* in, word32 inLen, byte* out, word32 outLen, |
4621 | | enum wc_HashType hash, int mgf, int saltLen, |
4622 | | RsaKey* key) |
4623 | 0 | { |
4624 | 0 | WC_RNG* rng; |
4625 | 0 | int ret; |
4626 | 0 | #if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG) |
4627 | 0 | if (key == NULL) { |
4628 | 0 | return BAD_FUNC_ARG; |
4629 | 0 | } |
4630 | 0 | rng = key->rng; |
4631 | | #else |
4632 | | rng = NULL; |
4633 | | #endif |
4634 | 0 | ret = RsaPrivateDecryptEx(in, inLen, out, outLen, NULL, key, |
4635 | 0 | RSA_PUBLIC_DECRYPT, RSA_BLOCK_TYPE_1, WC_RSA_PSS_PAD, |
4636 | 0 | hash, mgf, NULL, 0, saltLen, rng); |
4637 | 0 | return ret; |
4638 | 0 | } |
4639 | | |
4640 | | |
4641 | | /* Checks the PSS data to ensure that the signature matches. |
4642 | | * Salt length is equal to hash length. |
4643 | | * |
4644 | | * in Hash of the data that is being verified. |
4645 | | * inSz Length of hash. |
4646 | | * sig Buffer holding PSS data. |
4647 | | * sigSz Size of PSS data. |
4648 | | * hashType Hash algorithm. |
4649 | | * returns BAD_PADDING_E when the PSS data is invalid, BAD_FUNC_ARG when |
4650 | | * NULL is passed in to in or sig or inSz is not the same as the hash |
4651 | | * algorithm length and 0 on success. |
4652 | | */ |
4653 | | int wc_RsaPSS_CheckPadding(const byte* in, word32 inSz, const byte* sig, |
4654 | | word32 sigSz, enum wc_HashType hashType) |
4655 | 0 | { |
4656 | 0 | #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER |
4657 | 0 | return wc_RsaPSS_CheckPadding_ex(in, inSz, sig, sigSz, hashType, RSA_PSS_SALT_LEN_DEFAULT, 0); |
4658 | | #else |
4659 | | return wc_RsaPSS_CheckPadding_ex(in, inSz, sig, sigSz, hashType, RSA_PSS_SALT_LEN_DISCOVER, 0); |
4660 | | #endif |
4661 | 0 | } |
4662 | | |
4663 | | /* Checks the PSS data to ensure that the signature matches. |
4664 | | * |
4665 | | * in Hash of the data that is being verified. |
4666 | | * inSz Length of hash. |
4667 | | * sig Buffer holding PSS data. |
4668 | | * sigSz Size of PSS data. |
4669 | | * hashType Hash algorithm. |
4670 | | * saltLen Length of salt used. RSA_PSS_SALT_LEN_DEFAULT (-1) indicates salt |
4671 | | * length is the same as the hash length. RSA_PSS_SALT_LEN_DISCOVER |
4672 | | * indicates salt length is determined from the data. |
4673 | | * bits Can be used to calculate salt size in FIPS case |
4674 | | * returns BAD_PADDING_E when the PSS data is invalid, BAD_FUNC_ARG when |
4675 | | * NULL is passed in to in or sig or inSz is not the same as the hash |
4676 | | * algorithm length and 0 on success. |
4677 | | */ |
4678 | | int wc_RsaPSS_CheckPadding_ex2(const byte* in, word32 inSz, const byte* sig, |
4679 | | word32 sigSz, enum wc_HashType hashType, |
4680 | | int saltLen, int bits, void* heap) |
4681 | 0 | { |
4682 | 0 | int ret = 0; |
4683 | 0 | byte sigCheckBuf[WC_MAX_DIGEST_SIZE*2 + RSA_PSS_PAD_SZ]; |
4684 | 0 | byte *sigCheck = sigCheckBuf; |
4685 | 0 | int digSz; |
4686 | 0 | (void)bits; |
4687 | |
|
4688 | 0 | digSz = wc_HashGetDigestSize(hashType); |
4689 | |
|
4690 | 0 | if (in == NULL || sig == NULL || digSz < 0 || inSz != (word32)digSz) { |
4691 | 0 | ret = BAD_FUNC_ARG; |
4692 | 0 | } |
4693 | |
|
4694 | 0 | if (ret == 0) { |
4695 | 0 | if (saltLen == RSA_PSS_SALT_LEN_DEFAULT) { |
4696 | 0 | saltLen = (int)inSz; |
4697 | 0 | #ifdef WOLFSSL_SHA512 |
4698 | | /* See FIPS 186-4 section 5.5 item (e). */ |
4699 | 0 | if (bits == 1024 && inSz == WC_SHA512_DIGEST_SIZE) { |
4700 | 0 | saltLen = RSA_PSS_SALT_MAX_SZ; |
4701 | 0 | } |
4702 | 0 | #endif |
4703 | 0 | } |
4704 | | /* Same salt limit; here inSz is the hash length. FIPS 186-5 sec 5.4(g). */ |
4705 | | #if !defined(WOLFSSL_PSS_LONG_SALT) || FIPS_VERSION3_GE(7,0,0) |
4706 | | else if (saltLen > (int)inSz) { |
4707 | | ret = PSS_SALTLEN_E; |
4708 | | } |
4709 | | #endif |
4710 | 0 | #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER |
4711 | 0 | else if (saltLen < RSA_PSS_SALT_LEN_DEFAULT) { |
4712 | 0 | ret = PSS_SALTLEN_E; |
4713 | 0 | } |
4714 | | #else |
4715 | | else if (saltLen == RSA_PSS_SALT_LEN_DISCOVER) { |
4716 | | saltLen = sigSz - inSz; |
4717 | | /* Same cap on the discovered length; inSz is the hash length. */ |
4718 | | if ((saltLen < 0) |
4719 | | #if FIPS_VERSION3_GE(7,0,0) |
4720 | | || (saltLen > (int)inSz) |
4721 | | #endif |
4722 | | ) { |
4723 | | ret = PSS_SALTLEN_E; |
4724 | | } |
4725 | | } |
4726 | | else if (saltLen < RSA_PSS_SALT_LEN_DISCOVER) { |
4727 | | ret = PSS_SALTLEN_E; |
4728 | | } |
4729 | | #endif |
4730 | 0 | } |
4731 | | |
4732 | | /* Sig = Salt | Exp Hash */ |
4733 | 0 | if (ret == 0) { |
4734 | 0 | word32 totalSz = 0; |
4735 | 0 | if ((WC_SAFE_SUM_WORD32(inSz, (word32)saltLen, totalSz) == 0) || |
4736 | 0 | (sigSz != totalSz)) |
4737 | 0 | { |
4738 | 0 | ret = PSS_SALTLEN_E; |
4739 | 0 | } |
4740 | 0 | } |
4741 | |
|
4742 | 0 | #ifdef WOLFSSL_PSS_LONG_SALT |
4743 | | /* if long salt is larger then default maximum buffer then allocate a buffer */ |
4744 | 0 | if ((ret == 0) && |
4745 | 0 | (sizeof(sigCheckBuf) < (RSA_PSS_PAD_SZ + inSz + (word32)saltLen))) { |
4746 | 0 | sigCheck = (byte*)XMALLOC( |
4747 | 0 | (size_t)(RSA_PSS_PAD_SZ + inSz + (word32)saltLen), |
4748 | 0 | heap, DYNAMIC_TYPE_RSA_BUFFER); |
4749 | 0 | if (sigCheck == NULL) { |
4750 | 0 | ret = MEMORY_E; |
4751 | 0 | } |
4752 | 0 | } |
4753 | | #else |
4754 | | if (ret == 0 && sizeof(sigCheckBuf) < (RSA_PSS_PAD_SZ + inSz + (word32)saltLen)) { |
4755 | | ret = BUFFER_E; |
4756 | | } |
4757 | | #endif |
4758 | | |
4759 | | /* Exp Hash = HASH(8 * 0x00 | Message Hash | Salt) */ |
4760 | 0 | if (ret == 0) { |
4761 | 0 | XMEMSET(sigCheck, 0, RSA_PSS_PAD_SZ); |
4762 | 0 | XMEMCPY(sigCheck + RSA_PSS_PAD_SZ, in, inSz); |
4763 | 0 | XMEMCPY(sigCheck + RSA_PSS_PAD_SZ + inSz, sig, (size_t)saltLen); |
4764 | 0 | ret = wc_Hash(hashType, sigCheck, RSA_PSS_PAD_SZ + inSz + (word32)saltLen, |
4765 | 0 | sigCheck, inSz); |
4766 | 0 | } |
4767 | 0 | if (ret == 0) { |
4768 | 0 | if (XMEMCMP(sigCheck, sig + saltLen, inSz) != 0) { |
4769 | 0 | WOLFSSL_MSG("RsaPSS_CheckPadding: Padding Error"); |
4770 | 0 | ret = BAD_PADDING_E; |
4771 | 0 | } |
4772 | 0 | } |
4773 | |
|
4774 | 0 | #ifdef WOLFSSL_PSS_LONG_SALT |
4775 | 0 | if (sigCheck != NULL && sigCheck != sigCheckBuf) { |
4776 | 0 | XFREE(sigCheck, heap, DYNAMIC_TYPE_RSA_BUFFER); |
4777 | 0 | } |
4778 | 0 | #endif |
4779 | |
|
4780 | 0 | (void)heap; /* unused if memory is disabled */ |
4781 | 0 | return ret; |
4782 | 0 | } |
4783 | | int wc_RsaPSS_CheckPadding_ex(const byte* in, word32 inSz, const byte* sig, |
4784 | | word32 sigSz, enum wc_HashType hashType, |
4785 | | int saltLen, int bits) |
4786 | 0 | { |
4787 | 0 | return wc_RsaPSS_CheckPadding_ex2(in, inSz, sig, sigSz, hashType, saltLen, |
4788 | 0 | bits, NULL); |
4789 | 0 | } |
4790 | | |
4791 | | |
4792 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD) |
4793 | | /* Let a device verify an RSA-PSS signature and its padding in one shot (it gets |
4794 | | * the digest, which the RsaPad path does not). Shared by the two verify and |
4795 | | * check entry points below. |
4796 | | * |
4797 | | * out Buffer the device may write the recovered PSS block into. |
4798 | | * outSz Size of that buffer. |
4799 | | * recovered Set to the number of bytes the device wrote, 0 for a verdict only. |
4800 | | * returns the length the caller should report, a negative error, or |
4801 | | * CRYPTOCB_UNAVAILABLE when no device handled it. |
4802 | | */ |
4803 | | static int RsaPssVerifyDevice(const byte* in, word32 inLen, const byte* digest, |
4804 | | word32 digestLen, enum wc_HashType hash, int mgf, int saltLen, int hLen, |
4805 | | RsaKey* key, byte* out, word32 outSz, word32* recovered) |
4806 | | { |
4807 | | int ret; |
4808 | | int res = 0; |
4809 | | word32 recSz = 0; |
4810 | | |
4811 | | *recovered = 0; |
4812 | | |
4813 | | #ifndef WOLF_CRYPTO_CB_FIND |
4814 | | if (key == NULL || key->devId == INVALID_DEVID) |
4815 | | #else |
4816 | | if (key == NULL) |
4817 | | #endif |
4818 | | { |
4819 | | return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4820 | | } |
4821 | | |
4822 | | ret = wc_CryptoCb_RsaPssVerify(in, inLen, digest, digestLen, hash, mgf, |
4823 | | saltLen, key, &res, out, outSz, &recSz); |
4824 | | if (ret == WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
4825 | | return ret; |
4826 | | } |
4827 | | if (ret > 0) { |
4828 | | /* A handler returns 0 with res set, or a negative error. */ |
4829 | | return SIG_VERIFY_E; |
4830 | | } |
4831 | | if (ret != 0) { |
4832 | | return ret; |
4833 | | } |
4834 | | if (recSz > outSz) { |
4835 | | recSz = 0; |
4836 | | } |
4837 | | if (res == 0) { |
4838 | | return SIG_VERIFY_E; |
4839 | | } |
4840 | | if (recSz > 0) { |
4841 | | *recovered = recSz; |
4842 | | return (int)recSz; |
4843 | | } |
4844 | | if (outSz < (word32)(saltLen + hLen)) { |
4845 | | return RSA_BUFFER_E; |
4846 | | } |
4847 | | return saltLen + hLen; |
4848 | | } |
4849 | | #endif |
4850 | | |
4851 | | |
4852 | | /* Verify the message signed with RSA-PSS. |
4853 | | * The input buffer is reused for the output buffer. |
4854 | | * Salt length is equal to hash length. |
4855 | | * |
4856 | | * in Buffer holding encrypted data. |
4857 | | * inLen Length of data in buffer. |
4858 | | * out Pointer to address containing the PSS data. |
4859 | | * digest Hash of the data that is being verified. |
4860 | | * digestLen Length of hash. |
4861 | | * hash Hash algorithm. |
4862 | | * mgf Mask generation function. |
4863 | | * key Public RSA key. |
4864 | | * returns the length of the PSS data on success and negative indicates failure. |
4865 | | * |
4866 | | * Note: a device that recovers nothing sets *out to NULL, so check *out first. |
4867 | | */ |
4868 | | int wc_RsaPSS_VerifyCheckInline(byte* in, word32 inLen, byte** out, |
4869 | | const byte* digest, word32 digestLen, |
4870 | | enum wc_HashType hash, int mgf, RsaKey* key) |
4871 | 0 | { |
4872 | 0 | int ret = 0, verify, saltLen, hLen, bits = 0; |
4873 | | #ifdef WOLFSSL_MICROCHIP_TA100 |
4874 | | if (key != NULL && key->uKeyH != 0) { |
4875 | | int verified = 0; |
4876 | | ret = wc_Microchip_rsa_verify(digest, digestLen, in, inLen, key, |
4877 | | &verified); |
4878 | | if (ret != 0) { |
4879 | | return ret; |
4880 | | } |
4881 | | return verified ? (int)inLen : SIG_VERIFY_E; |
4882 | | } |
4883 | | #endif |
4884 | |
|
4885 | 0 | hLen = wc_HashGetDigestSize(hash); |
4886 | 0 | if (hLen < 0) |
4887 | 0 | return BAD_FUNC_ARG; |
4888 | 0 | if ((word32)hLen != digestLen) |
4889 | 0 | return BAD_FUNC_ARG; |
4890 | | |
4891 | 0 | saltLen = hLen; |
4892 | 0 | #ifdef WOLFSSL_SHA512 |
4893 | 0 | if (key == NULL) { |
4894 | 0 | return BAD_FUNC_ARG; |
4895 | 0 | } |
4896 | | /* See FIPS 186-4 section 5.5 item (e). */ |
4897 | 0 | bits = mp_count_bits(&key->n); |
4898 | 0 | if (bits == 1024 && hLen == WC_SHA512_DIGEST_SIZE) |
4899 | 0 | saltLen = RSA_PSS_SALT_MAX_SZ; |
4900 | 0 | #endif |
4901 | |
|
4902 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD) |
4903 | | { |
4904 | | word32 recovered = 0; |
4905 | | |
4906 | | ret = RsaPssVerifyDevice(in, inLen, digest, digestLen, hash, mgf, |
4907 | | saltLen, hLen, key, in, inLen, &recovered); |
4908 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
4909 | | if ((ret > 0) && (out != NULL)) { |
4910 | | if (recovered > 0) { |
4911 | | *out = in; |
4912 | | } |
4913 | | else { |
4914 | | /* Device reported a verdict only; nothing to expose. */ |
4915 | | *out = NULL; |
4916 | | } |
4917 | | } |
4918 | | return ret; |
4919 | | } |
4920 | | ret = 0; |
4921 | | } |
4922 | | #endif |
4923 | |
|
4924 | 0 | verify = wc_RsaPSS_VerifyInline_ex(in, inLen, out, hash, mgf, saltLen, key); |
4925 | 0 | if (verify > 0) |
4926 | 0 | ret = wc_RsaPSS_CheckPadding_ex(digest, digestLen, *out, (word32)verify, |
4927 | 0 | hash, saltLen, bits); |
4928 | 0 | if (ret == 0) |
4929 | 0 | ret = verify; |
4930 | |
|
4931 | 0 | return ret; |
4932 | 0 | } |
4933 | | |
4934 | | |
4935 | | /* Verify the message signed with RSA-PSS. |
4936 | | * Salt length is equal to hash length. |
4937 | | * |
4938 | | * in Buffer holding encrypted data. |
4939 | | * inLen Length of data in buffer. |
4940 | | * out Pointer to address containing the PSS data. |
4941 | | * outLen Length of the output. |
4942 | | * digest Hash of the data that is being verified. |
4943 | | * digestLen Length of hash. |
4944 | | * hash Hash algorithm. |
4945 | | * mgf Mask generation function. |
4946 | | * key Public RSA key. |
4947 | | * returns the length of the PSS data on success and negative indicates failure. |
4948 | | */ |
4949 | | int wc_RsaPSS_VerifyCheck(const byte* in, word32 inLen, byte* out, word32 outLen, |
4950 | | const byte* digest, word32 digestLen, |
4951 | | enum wc_HashType hash, int mgf, |
4952 | | RsaKey* key) |
4953 | 0 | { |
4954 | 0 | int ret = 0, verify, saltLen, hLen, bits = 0; |
4955 | | #ifdef WOLFSSL_MICROCHIP_TA100 |
4956 | | if (key != NULL && key->uKeyH != 0) { |
4957 | | int verified = 0; |
4958 | | ret = wc_Microchip_rsa_verify(digest, digestLen, (byte*)in, inLen, |
4959 | | key, &verified); |
4960 | | if (ret != 0) { |
4961 | | return ret; |
4962 | | } |
4963 | | return verified ? (int)inLen : SIG_VERIFY_E; |
4964 | | } |
4965 | | #endif |
4966 | |
|
4967 | 0 | hLen = wc_HashGetDigestSize(hash); |
4968 | 0 | if (hLen < 0) |
4969 | 0 | return hLen; |
4970 | 0 | if ((word32)hLen != digestLen) |
4971 | 0 | return BAD_FUNC_ARG; |
4972 | | |
4973 | 0 | saltLen = hLen; |
4974 | 0 | #ifdef WOLFSSL_SHA512 |
4975 | 0 | if (key == NULL) { |
4976 | 0 | return BAD_FUNC_ARG; |
4977 | 0 | } |
4978 | | /* See FIPS 186-4 section 5.5 item (e). */ |
4979 | 0 | bits = mp_count_bits(&key->n); |
4980 | 0 | if (bits == 1024 && hLen == WC_SHA512_DIGEST_SIZE) |
4981 | 0 | saltLen = RSA_PSS_SALT_MAX_SZ; |
4982 | 0 | #endif |
4983 | |
|
4984 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD) |
4985 | | { |
4986 | | word32 recovered = 0; |
4987 | | |
4988 | | ret = RsaPssVerifyDevice(in, inLen, digest, digestLen, hash, mgf, |
4989 | | saltLen, hLen, key, out, outLen, &recovered); |
4990 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
4991 | | if ((ret > 0) && (recovered == 0) && (out != NULL)) { |
4992 | | /* Device gave a verdict only; leave no stale data behind. */ |
4993 | | XMEMSET(out, 0, (word32)ret); |
4994 | | } |
4995 | | return ret; |
4996 | | } |
4997 | | ret = 0; |
4998 | | } |
4999 | | #endif |
5000 | |
|
5001 | 0 | verify = wc_RsaPSS_Verify_ex(in, inLen, out, outLen, hash, |
5002 | 0 | mgf, saltLen, key); |
5003 | 0 | if (verify > 0) |
5004 | 0 | ret = wc_RsaPSS_CheckPadding_ex(digest, digestLen, out, (word32)verify, |
5005 | 0 | hash, saltLen, bits); |
5006 | 0 | if (ret == 0) |
5007 | 0 | ret = verify; |
5008 | |
|
5009 | 0 | return ret; |
5010 | 0 | } |
5011 | | |
5012 | | #endif |
5013 | | |
5014 | | #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY) |
5015 | | int wc_RsaSSL_Sign(const byte* in, word32 inLen, byte* out, word32 outLen, |
5016 | | RsaKey* key, WC_RNG* rng) |
5017 | 0 | { |
5018 | 0 | int ret; |
5019 | 0 | ret = RsaPublicEncryptEx(in, inLen, out, outLen, key, |
5020 | 0 | RSA_PRIVATE_ENCRYPT, RSA_BLOCK_TYPE_1, WC_RSA_PKCSV15_PAD, |
5021 | 0 | WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0, 0, rng); |
5022 | 0 | return ret; |
5023 | 0 | } |
5024 | | |
5025 | | #ifdef WC_RSA_PSS |
5026 | | /* Sign the hash of a message using RSA-PSS. |
5027 | | * Salt length is equal to hash length. |
5028 | | * |
5029 | | * in Buffer holding hash of message. |
5030 | | * inLen Length of data in buffer (hash length). |
5031 | | * out Buffer to write encrypted signature into. |
5032 | | * outLen Size of buffer to write to. |
5033 | | * hash Hash algorithm. |
5034 | | * mgf Mask generation function. |
5035 | | * key Public RSA key. |
5036 | | * rng Random number generator. |
5037 | | * returns the length of the encrypted signature on success, a negative value |
5038 | | * indicates failure. |
5039 | | */ |
5040 | | int wc_RsaPSS_Sign(const byte* in, word32 inLen, byte* out, word32 outLen, |
5041 | | enum wc_HashType hash, int mgf, RsaKey* key, WC_RNG* rng) |
5042 | 0 | { |
5043 | 0 | return wc_RsaPSS_Sign_ex(in, inLen, out, outLen, hash, mgf, |
5044 | 0 | RSA_PSS_SALT_LEN_DEFAULT, key, rng); |
5045 | 0 | } |
5046 | | |
5047 | | /* Sign the hash of a message using RSA-PSS. |
5048 | | * |
5049 | | * in Buffer holding hash of message. |
5050 | | * inLen Length of data in buffer (hash length). |
5051 | | * out Buffer to write encrypted signature into. |
5052 | | * outLen Size of buffer to write to. |
5053 | | * hash Hash algorithm. |
5054 | | * mgf Mask generation function. |
5055 | | * saltLen Length of salt used. RSA_PSS_SALT_LEN_DEFAULT (-1) indicates salt |
5056 | | * length is the same as the hash length. RSA_PSS_SALT_LEN_DISCOVER |
5057 | | * indicates salt length is determined from the data. |
5058 | | * key Public RSA key. |
5059 | | * rng Random number generator. |
5060 | | * returns the length of the encrypted signature on success, a negative value |
5061 | | * indicates failure. |
5062 | | */ |
5063 | | int wc_RsaPSS_Sign_ex(const byte* in, word32 inLen, byte* out, word32 outLen, |
5064 | | enum wc_HashType hash, int mgf, int saltLen, RsaKey* key, |
5065 | | WC_RNG* rng) |
5066 | 0 | { |
5067 | 0 | int ret; |
5068 | 0 | ret = RsaPublicEncryptEx(in, inLen, out, outLen, key, |
5069 | 0 | RSA_PRIVATE_ENCRYPT, RSA_BLOCK_TYPE_1, WC_RSA_PSS_PAD, |
5070 | 0 | hash, mgf, NULL, 0, saltLen, rng); |
5071 | 0 | return ret; |
5072 | 0 | } |
5073 | | #endif |
5074 | | #endif |
5075 | | |
5076 | | int wc_RsaEncryptSize(const RsaKey* key) |
5077 | 0 | { |
5078 | 0 | int ret; |
5079 | |
|
5080 | 0 | if (key == NULL) { |
5081 | 0 | return BAD_FUNC_ARG; |
5082 | 0 | } |
5083 | | |
5084 | 0 | ret = mp_unsigned_bin_size(&key->n); |
5085 | |
|
5086 | | #if defined(WOLFSSL_MICROCHIP_TA100) |
5087 | | if (ret == 0 && (key->rKeyH != 0 || key->uKeyH != 0)) { |
5088 | | ret = 2048 / 8; |
5089 | | } |
5090 | | #endif |
5091 | |
|
5092 | | #ifdef WOLF_CRYPTO_CB |
5093 | | if (ret == 0 && key->devId != INVALID_DEVID) { |
5094 | | if (wc_CryptoCb_RsaGetSize(key, &ret) == WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
5095 | | ret = 2048/8; /* hardware handles, use 2048-bit as default */ |
5096 | | } |
5097 | | } |
5098 | | #endif |
5099 | |
|
5100 | 0 | return ret; |
5101 | 0 | } |
5102 | | |
5103 | | #ifndef WOLFSSL_RSA_VERIFY_ONLY |
5104 | | /* Software-only export of RSA public key elements from RsaKey. |
5105 | | * This internal helper avoids recursion when called from the EXPORT_KEY path. */ |
5106 | | static int _RsaFlattenPublicKey(const RsaKey* key, byte* e, word32* eSz, |
5107 | | byte* n, word32* nSz) |
5108 | 0 | { |
5109 | 0 | int sz, ret; |
5110 | |
|
5111 | 0 | if (key == NULL || e == NULL || eSz == NULL || n == NULL || nSz == NULL) { |
5112 | 0 | return BAD_FUNC_ARG; |
5113 | 0 | } |
5114 | | |
5115 | 0 | sz = mp_unsigned_bin_size(&key->e); |
5116 | 0 | if ((word32)sz > *eSz) { |
5117 | 0 | return RSA_BUFFER_E; |
5118 | 0 | } |
5119 | 0 | ret = mp_to_unsigned_bin(&key->e, e); |
5120 | 0 | if (ret != MP_OKAY) { |
5121 | 0 | return ret; |
5122 | 0 | } |
5123 | 0 | *eSz = (word32)sz; |
5124 | |
|
5125 | 0 | sz = wc_RsaEncryptSize(key); |
5126 | 0 | if ((word32)sz > *nSz) { |
5127 | 0 | return RSA_BUFFER_E; |
5128 | 0 | } |
5129 | 0 | ret = mp_to_unsigned_bin(&key->n, n); |
5130 | 0 | if (ret != MP_OKAY) { |
5131 | 0 | return ret; |
5132 | 0 | } |
5133 | 0 | *nSz = (word32)sz; |
5134 | |
|
5135 | 0 | return 0; |
5136 | 0 | } |
5137 | | |
5138 | | /* flatten RsaKey structure into individual elements (e, n) */ |
5139 | | int wc_RsaFlattenPublicKey(const RsaKey* key, byte* e, word32* eSz, byte* n, |
5140 | | word32* nSz) |
5141 | 0 | { |
5142 | 0 | if (key == NULL || e == NULL || eSz == NULL || n == NULL || nSz == NULL) { |
5143 | 0 | return BAD_FUNC_ARG; |
5144 | 0 | } |
5145 | | |
5146 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY) |
5147 | | #ifndef WOLF_CRYPTO_CB_FIND |
5148 | | if (key->devId != INVALID_DEVID) |
5149 | | #endif |
5150 | | { |
5151 | | int ret; |
5152 | | WC_DECLARE_VAR(tmpKey, RsaKey, 1, NULL); |
5153 | | |
5154 | | WC_ALLOC_VAR(tmpKey, RsaKey, 1, key->heap); |
5155 | | if (!WC_VAR_OK(tmpKey)) { |
5156 | | return MEMORY_E; |
5157 | | } |
5158 | | XMEMSET(tmpKey, 0, sizeof(RsaKey)); |
5159 | | |
5160 | | ret = wc_InitRsaKey_ex(tmpKey, key->heap, INVALID_DEVID); |
5161 | | if (ret != 0) { |
5162 | | WC_FREE_VAR(tmpKey, key->heap); |
5163 | | return ret; |
5164 | | } |
5165 | | |
5166 | | ret = wc_CryptoCb_ExportKey(key->devId, WC_PK_TYPE_RSA, |
5167 | | key, tmpKey); |
5168 | | if (ret == 0) { |
5169 | | /* Call software helper (no callback recursion) */ |
5170 | | ret = _RsaFlattenPublicKey(tmpKey, e, eSz, n, nSz); |
5171 | | } |
5172 | | /* wc_FreeRsaKey calls mp_forcezero on all private key components, |
5173 | | * so no separate ForceZero of the struct is needed here. Calling |
5174 | | * ForceZero before wc_FreeRsaKey would zero the mp_int metadata |
5175 | | * and cause a crash. */ |
5176 | | wc_FreeRsaKey(tmpKey); |
5177 | | WC_FREE_VAR(tmpKey, key->heap); |
5178 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
5179 | | return ret; |
5180 | | } |
5181 | | /* fall through to software */ |
5182 | | } |
5183 | | #endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_EXPORT_KEY */ |
5184 | | |
5185 | 0 | return _RsaFlattenPublicKey(key, e, eSz, n, nSz); |
5186 | 0 | } |
5187 | | #endif |
5188 | | |
5189 | | #ifndef WOLFSSL_RSA_VERIFY_ONLY |
5190 | | static int RsaGetValue(const mp_int* in, byte* out, word32* outSz) |
5191 | 0 | { |
5192 | 0 | word32 sz; |
5193 | 0 | int ret = 0; |
5194 | | |
5195 | | /* Parameters ensured by calling function. */ |
5196 | |
|
5197 | 0 | sz = (word32)mp_unsigned_bin_size(in); |
5198 | 0 | if (sz > *outSz) |
5199 | 0 | ret = RSA_BUFFER_E; |
5200 | |
|
5201 | 0 | if (ret == 0) |
5202 | 0 | ret = mp_to_unsigned_bin(in, out); |
5203 | |
|
5204 | 0 | if (ret == MP_OKAY) |
5205 | 0 | *outSz = sz; |
5206 | |
|
5207 | 0 | return ret; |
5208 | 0 | } |
5209 | | |
5210 | | |
5211 | | /* Software-only export of RSA key elements from RsaKey. |
5212 | | * This internal helper avoids recursion when called from the EXPORT_KEY path. */ |
5213 | | static int _RsaExportKey(const RsaKey* key, |
5214 | | byte* e, word32* eSz, byte* n, word32* nSz, |
5215 | | byte* d, word32* dSz, byte* p, word32* pSz, |
5216 | | byte* q, word32* qSz) |
5217 | 0 | { |
5218 | 0 | int ret = 0; |
5219 | |
|
5220 | 0 | if (key == NULL || e == NULL || eSz == NULL || n == NULL || nSz == NULL |
5221 | 0 | || d == NULL || dSz == NULL || p == NULL || pSz == NULL |
5222 | 0 | || q == NULL || qSz == NULL) { |
5223 | 0 | return BAD_FUNC_ARG; |
5224 | 0 | } |
5225 | | |
5226 | 0 | if (ret == 0) { |
5227 | 0 | ret = RsaGetValue(&key->e, e, eSz); |
5228 | 0 | } |
5229 | 0 | if (ret == 0) { |
5230 | 0 | ret = RsaGetValue(&key->n, n, nSz); |
5231 | 0 | } |
5232 | 0 | #ifndef WOLFSSL_RSA_PUBLIC_ONLY |
5233 | 0 | if (ret == 0) { |
5234 | 0 | ret = RsaGetValue(&key->d, d, dSz); |
5235 | 0 | } |
5236 | 0 | if (ret == 0) { |
5237 | 0 | ret = RsaGetValue(&key->p, p, pSz); |
5238 | 0 | } |
5239 | 0 | if (ret == 0) { |
5240 | 0 | ret = RsaGetValue(&key->q, q, qSz); |
5241 | 0 | } |
5242 | | #else |
5243 | | /* no private parts to key */ |
5244 | | if (d == NULL || p == NULL || q == NULL || dSz == NULL || pSz == NULL |
5245 | | || qSz == NULL) { |
5246 | | ret = BAD_FUNC_ARG; |
5247 | | } |
5248 | | else { |
5249 | | *dSz = 0; |
5250 | | *pSz = 0; |
5251 | | *qSz = 0; |
5252 | | } |
5253 | | #endif /* WOLFSSL_RSA_PUBLIC_ONLY */ |
5254 | |
|
5255 | 0 | return ret; |
5256 | 0 | } |
5257 | | |
5258 | | int wc_RsaExportKey(const RsaKey* key, |
5259 | | byte* e, word32* eSz, byte* n, word32* nSz, |
5260 | | byte* d, word32* dSz, byte* p, word32* pSz, |
5261 | | byte* q, word32* qSz) |
5262 | 0 | { |
5263 | 0 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
5264 | |
|
5265 | 0 | if (key && e && eSz && n && nSz && d && dSz && p && pSz && q && qSz) { |
5266 | 0 | ret = 0; |
5267 | 0 | } |
5268 | |
|
5269 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY) |
5270 | | if (ret == 0) { |
5271 | | #ifndef WOLF_CRYPTO_CB_FIND |
5272 | | if (key->devId != INVALID_DEVID) |
5273 | | #endif |
5274 | | { |
5275 | | WC_DECLARE_VAR(tmpKey, RsaKey, 1, NULL); |
5276 | | |
5277 | | WC_ALLOC_VAR(tmpKey, RsaKey, 1, key->heap); |
5278 | | if (!WC_VAR_OK(tmpKey)) { |
5279 | | return MEMORY_E; |
5280 | | } |
5281 | | XMEMSET(tmpKey, 0, sizeof(RsaKey)); |
5282 | | |
5283 | | ret = wc_InitRsaKey_ex(tmpKey, key->heap, INVALID_DEVID); |
5284 | | if (ret != 0) { |
5285 | | WC_FREE_VAR(tmpKey, key->heap); |
5286 | | return ret; |
5287 | | } |
5288 | | |
5289 | | ret = wc_CryptoCb_ExportKey(key->devId, WC_PK_TYPE_RSA, |
5290 | | key, tmpKey); |
5291 | | if (ret == 0) { |
5292 | | /* Call software helper (no callback recursion) */ |
5293 | | ret = _RsaExportKey(tmpKey, e, eSz, n, nSz, |
5294 | | d, dSz, p, pSz, q, qSz); |
5295 | | } |
5296 | | /* wc_FreeRsaKey calls mp_forcezero on all private key components, |
5297 | | * so no separate ForceZero of the struct is needed here. */ |
5298 | | wc_FreeRsaKey(tmpKey); |
5299 | | WC_FREE_VAR(tmpKey, key->heap); |
5300 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
5301 | | return ret; |
5302 | | } |
5303 | | ret = 0; /* fall through to software */ |
5304 | | } |
5305 | | } |
5306 | | #endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_EXPORT_KEY */ |
5307 | |
|
5308 | 0 | if (ret == 0) { |
5309 | 0 | ret = _RsaExportKey(key, e, eSz, n, nSz, d, dSz, p, pSz, q, qSz); |
5310 | 0 | } |
5311 | |
|
5312 | 0 | return ret; |
5313 | 0 | } |
5314 | | #endif |
5315 | | |
5316 | | |
5317 | | #if defined(WOLFSSL_KEY_GEN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) |
5318 | | |
5319 | | /* Check that |p-q| > 2^((size/2)-100) */ |
5320 | | static int wc_CompareDiffPQ(mp_int* p, mp_int* q, int size, int* valid) |
5321 | | { |
5322 | | #ifdef WOLFSSL_SMALL_STACK |
5323 | | mp_int *c = NULL, *d = NULL; |
5324 | | #else |
5325 | | mp_int c[1], d[1]; |
5326 | | #endif |
5327 | | int ret; |
5328 | | |
5329 | | if (p == NULL || q == NULL) |
5330 | | return BAD_FUNC_ARG; |
5331 | | |
5332 | | #ifdef WOLFSSL_SMALL_STACK |
5333 | | if (((c = (mp_int *)XMALLOC(sizeof(*c), NULL, DYNAMIC_TYPE_WOLF_BIGINT)) == NULL) || |
5334 | | ((d = (mp_int *)XMALLOC(sizeof(*d), NULL, DYNAMIC_TYPE_WOLF_BIGINT)) == NULL)) { |
5335 | | /* mp_init_multi() below is skipped, so nothing was initialized: free |
5336 | | * what was allocated here and NULL the pointers. The cleanup at the |
5337 | | * end must not see an allocated-but-uninitialized mp_int - clearing |
5338 | | * one reads a garbage used/size and corrupts the heap. */ |
5339 | | XFREE(c, NULL, DYNAMIC_TYPE_WOLF_BIGINT); |
5340 | | XFREE(d, NULL, DYNAMIC_TYPE_WOLF_BIGINT); |
5341 | | c = NULL; |
5342 | | d = NULL; |
5343 | | ret = MEMORY_E; |
5344 | | } |
5345 | | else |
5346 | | ret = 0; |
5347 | | |
5348 | | if (ret == 0) |
5349 | | #endif |
5350 | | ret = mp_init_multi(c, d, NULL, NULL, NULL, NULL); |
5351 | | |
5352 | | /* c = 2^((size/2)-100) */ |
5353 | | if (ret == 0) |
5354 | | ret = mp_2expt(c, (size/2)-100); |
5355 | | |
5356 | | /* d = |p-q| */ |
5357 | | if (ret == 0) |
5358 | | ret = mp_sub(p, q, d); |
5359 | | |
5360 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
5361 | | if (ret == 0) |
5362 | | mp_memzero_add("Compare PQ d", d); |
5363 | | #endif |
5364 | | |
5365 | | #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \ |
5366 | | defined(WOLFSSL_SP_INT_NEGATIVE)) |
5367 | | if (ret == 0) |
5368 | | ret = mp_abs(d, d); |
5369 | | #endif |
5370 | | |
5371 | | /* compare */ |
5372 | | if (ret == 0) |
5373 | | *valid = (mp_cmp(d, c) == MP_GT); |
5374 | | |
5375 | | #ifdef WOLFSSL_SMALL_STACK |
5376 | | if (d != NULL) { |
5377 | | mp_forcezero(d); |
5378 | | XFREE(d, NULL, DYNAMIC_TYPE_WOLF_BIGINT); |
5379 | | } |
5380 | | if (c != NULL) { |
5381 | | mp_clear(c); |
5382 | | XFREE(c, NULL, DYNAMIC_TYPE_WOLF_BIGINT); |
5383 | | } |
5384 | | #else |
5385 | | mp_forcezero(d); |
5386 | | mp_clear(c); |
5387 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
5388 | | mp_memzero_check(d); |
5389 | | #endif |
5390 | | #endif |
5391 | | |
5392 | | return ret; |
5393 | | } |
5394 | | |
5395 | | |
5396 | | /* The lower_bound value is floor(2^(0.5) * 2^((nlen/2)-1)) where nlen is 4096. |
5397 | | * This number was calculated using a small test tool written with a common |
5398 | | * large number math library. Other values of nlen may be checked with a subset |
5399 | | * of lower_bound. */ |
5400 | | static const byte lower_bound[] = { |
5401 | | 0xB5, 0x04, 0xF3, 0x33, 0xF9, 0xDE, 0x64, 0x84, |
5402 | | 0x59, 0x7D, 0x89, 0xB3, 0x75, 0x4A, 0xBE, 0x9F, |
5403 | | 0x1D, 0x6F, 0x60, 0xBA, 0x89, 0x3B, 0xA8, 0x4C, |
5404 | | 0xED, 0x17, 0xAC, 0x85, 0x83, 0x33, 0x99, 0x15, |
5405 | | /* 512 */ |
5406 | | 0x4A, 0xFC, 0x83, 0x04, 0x3A, 0xB8, 0xA2, 0xC3, |
5407 | | 0xA8, 0xB1, 0xFE, 0x6F, 0xDC, 0x83, 0xDB, 0x39, |
5408 | | 0x0F, 0x74, 0xA8, 0x5E, 0x43, 0x9C, 0x7B, 0x4A, |
5409 | | 0x78, 0x04, 0x87, 0x36, 0x3D, 0xFA, 0x27, 0x68, |
5410 | | /* 1024 */ |
5411 | | 0xD2, 0x20, 0x2E, 0x87, 0x42, 0xAF, 0x1F, 0x4E, |
5412 | | 0x53, 0x05, 0x9C, 0x60, 0x11, 0xBC, 0x33, 0x7B, |
5413 | | 0xCA, 0xB1, 0xBC, 0x91, 0x16, 0x88, 0x45, 0x8A, |
5414 | | 0x46, 0x0A, 0xBC, 0x72, 0x2F, 0x7C, 0x4E, 0x33, |
5415 | | 0xC6, 0xD5, 0xA8, 0xA3, 0x8B, 0xB7, 0xE9, 0xDC, |
5416 | | 0xCB, 0x2A, 0x63, 0x43, 0x31, 0xF3, 0xC8, 0x4D, |
5417 | | 0xF5, 0x2F, 0x12, 0x0F, 0x83, 0x6E, 0x58, 0x2E, |
5418 | | 0xEA, 0xA4, 0xA0, 0x89, 0x90, 0x40, 0xCA, 0x4A, |
5419 | | /* 2048 */ |
5420 | | 0x81, 0x39, 0x4A, 0xB6, 0xD8, 0xFD, 0x0E, 0xFD, |
5421 | | 0xF4, 0xD3, 0xA0, 0x2C, 0xEB, 0xC9, 0x3E, 0x0C, |
5422 | | 0x42, 0x64, 0xDA, 0xBC, 0xD5, 0x28, 0xB6, 0x51, |
5423 | | 0xB8, 0xCF, 0x34, 0x1B, 0x6F, 0x82, 0x36, 0xC7, |
5424 | | 0x01, 0x04, 0xDC, 0x01, 0xFE, 0x32, 0x35, 0x2F, |
5425 | | 0x33, 0x2A, 0x5E, 0x9F, 0x7B, 0xDA, 0x1E, 0xBF, |
5426 | | 0xF6, 0xA1, 0xBE, 0x3F, 0xCA, 0x22, 0x13, 0x07, |
5427 | | 0xDE, 0xA0, 0x62, 0x41, 0xF7, 0xAA, 0x81, 0xC2, |
5428 | | /* 3072 */ |
5429 | | 0xC1, 0xFC, 0xBD, 0xDE, 0xA2, 0xF7, 0xDC, 0x33, |
5430 | | 0x18, 0x83, 0x8A, 0x2E, 0xAF, 0xF5, 0xF3, 0xB2, |
5431 | | 0xD2, 0x4F, 0x4A, 0x76, 0x3F, 0xAC, 0xB8, 0x82, |
5432 | | 0xFD, 0xFE, 0x17, 0x0F, 0xD3, 0xB1, 0xF7, 0x80, |
5433 | | 0xF9, 0xAC, 0xCE, 0x41, 0x79, 0x7F, 0x28, 0x05, |
5434 | | 0xC2, 0x46, 0x78, 0x5E, 0x92, 0x95, 0x70, 0x23, |
5435 | | 0x5F, 0xCF, 0x8F, 0x7B, 0xCA, 0x3E, 0xA3, 0x3B, |
5436 | | 0x4D, 0x7C, 0x60, 0xA5, 0xE6, 0x33, 0xE3, 0xE1 |
5437 | | /* 4096 */ |
5438 | | }; |
5439 | | |
5440 | | |
5441 | | /* returns 1 on key size ok and 0 if not ok */ |
5442 | | static WC_INLINE int RsaSizeCheck(int size) |
5443 | | { |
5444 | | if (size < RSA_MIN_SIZE || size > RSA_MAX_SIZE) { |
5445 | | return 0; |
5446 | | } |
5447 | | |
5448 | | #if FIPS_VERSION3_GE(7,0,0) |
5449 | | /* Only the sizes this module is validated for. The standards set a |
5450 | | * floor, not a list: at least 2048 bits and even (FIPS 186-5 sec 5.1), |
5451 | | * with less disallowed for signing (SP 800-131Ar2 Table 2). These three |
5452 | | * are what wolfSSL holds CAVP certificates for, so this is stricter. */ |
5453 | | switch (size) { |
5454 | | case 2048: |
5455 | | case 3072: |
5456 | | case 4096: |
5457 | | return 1; |
5458 | | } |
5459 | | |
5460 | | return 0; |
5461 | | #elif defined(HAVE_FIPS) |
5462 | | /* Key size requirements for CAVP */ |
5463 | | switch (size) { |
5464 | | case 1024: |
5465 | | case 2048: |
5466 | | case 3072: |
5467 | | case 4096: |
5468 | | return 1; |
5469 | | } |
5470 | | |
5471 | | return 0; |
5472 | | #else |
5473 | | return 1; /* allow unusual key sizes in non FIPS mode */ |
5474 | | #endif /* FIPS_VERSION3_GE(7,0,0) */ |
5475 | | } |
5476 | | |
5477 | | |
5478 | | static int _CheckProbablePrime(mp_int* p, mp_int* q, mp_int* e, int nlen, |
5479 | | int* isPrime, WC_RNG* rng) |
5480 | | { |
5481 | | int ret; |
5482 | | #ifdef WOLFSSL_SMALL_STACK |
5483 | | mp_int *tmp1 = NULL, *tmp2 = NULL; |
5484 | | #else |
5485 | | mp_int tmp1[1], tmp2[2]; |
5486 | | #endif |
5487 | | mp_int* prime; |
5488 | | |
5489 | | if (p == NULL || e == NULL || isPrime == NULL) |
5490 | | return BAD_FUNC_ARG; |
5491 | | |
5492 | | if (!RsaSizeCheck(nlen)) |
5493 | | return BAD_FUNC_ARG; |
5494 | | |
5495 | | *isPrime = MP_NO; |
5496 | | |
5497 | | #ifdef WOLFSSL_SMALL_STACK |
5498 | | if (((tmp1 = (mp_int *)XMALLOC(sizeof(*tmp1), NULL, DYNAMIC_TYPE_WOLF_BIGINT)) == NULL) || |
5499 | | ((tmp2 = (mp_int *)XMALLOC(sizeof(*tmp2), NULL, DYNAMIC_TYPE_WOLF_BIGINT)) == NULL)) { |
5500 | | /* mp_init_multi() below is skipped, so nothing was initialized: free |
5501 | | * what was allocated here and NULL the pointers. The notOkay cleanup |
5502 | | * must not see an allocated-but-uninitialized mp_int - clearing one |
5503 | | * reads a garbage used/size and corrupts the heap. */ |
5504 | | XFREE(tmp1, NULL, DYNAMIC_TYPE_WOLF_BIGINT); |
5505 | | XFREE(tmp2, NULL, DYNAMIC_TYPE_WOLF_BIGINT); |
5506 | | tmp1 = NULL; |
5507 | | tmp2 = NULL; |
5508 | | ret = MEMORY_E; |
5509 | | goto notOkay; |
5510 | | } |
5511 | | #endif |
5512 | | |
5513 | | ret = mp_init_multi(tmp1, tmp2, NULL, NULL, NULL, NULL); |
5514 | | if (ret != MP_OKAY) goto notOkay; |
5515 | | |
5516 | | if (q != NULL) { |
5517 | | int valid = 0; |
5518 | | /* 5.4 (186-4) 5.5 (186-5) - |
5519 | | * check that |p-q| <= (2^(1/2))(2^((nlen/2)-1)) */ |
5520 | | ret = wc_CompareDiffPQ(p, q, nlen, &valid); |
5521 | | if ((ret != MP_OKAY) || (!valid)) goto notOkay; |
5522 | | prime = q; |
5523 | | } |
5524 | | else |
5525 | | prime = p; |
5526 | | |
5527 | | /* 4.4,5.5 (186-4) 4.4,5.4 (186-5) - |
5528 | | * Check that prime >= (2^(1/2))(2^((nlen/2)-1)) |
5529 | | * This is a comparison against lowerBound */ |
5530 | | ret = mp_read_unsigned_bin(tmp1, lower_bound, (word32)nlen/16); |
5531 | | if (ret != MP_OKAY) goto notOkay; |
5532 | | ret = mp_cmp(prime, tmp1); |
5533 | | if (ret == MP_LT) goto exit; |
5534 | | |
5535 | | /* 4.5,5.6 (186-4 & 186-5) - Check that GCD(p-1, e) == 1 */ |
5536 | | ret = mp_sub_d(prime, 1, tmp1); /* tmp1 = prime-1 */ |
5537 | | if (ret != MP_OKAY) goto notOkay; |
5538 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
5539 | | mp_memzero_add("Check Probable Prime tmp1", tmp1); |
5540 | | #endif |
5541 | | ret = mp_gcd(tmp1, e, tmp2); /* tmp2 = gcd(prime-1, e) */ |
5542 | | if (ret != MP_OKAY) goto notOkay; |
5543 | | ret = mp_cmp_d(tmp2, 1); |
5544 | | if (ret != MP_EQ) goto exit; /* e divides p-1 */ |
5545 | | |
5546 | | /* 4.5.1,5.6.1 - Check primality of p with 8 rounds of M-R. |
5547 | | * mp_prime_is_prime_ex() performs test divisions against the first 256 |
5548 | | * prime numbers. After that it performs 8 rounds of M-R using random |
5549 | | * bases between 2 and n-2. |
5550 | | * mp_prime_is_prime() performs the same test divisions and then does |
5551 | | * M-R with the first 8 primes. Both functions set isPrime as a |
5552 | | * side-effect. */ |
5553 | | if (rng != NULL) |
5554 | | ret = mp_prime_is_prime_ex(prime, 8, isPrime, rng); |
5555 | | else |
5556 | | ret = mp_prime_is_prime(prime, 8, isPrime); |
5557 | | if (ret != MP_OKAY) goto notOkay; |
5558 | | |
5559 | | exit: |
5560 | | ret = MP_OKAY; |
5561 | | |
5562 | | notOkay: |
5563 | | |
5564 | | #ifdef WOLFSSL_SMALL_STACK |
5565 | | if (tmp1 != NULL) { |
5566 | | mp_forcezero(tmp1); |
5567 | | XFREE(tmp1, NULL, DYNAMIC_TYPE_WOLF_BIGINT); |
5568 | | } |
5569 | | if (tmp2 != NULL) { |
5570 | | mp_clear(tmp2); |
5571 | | XFREE(tmp2, NULL, DYNAMIC_TYPE_WOLF_BIGINT); |
5572 | | } |
5573 | | #else |
5574 | | mp_forcezero(tmp1); |
5575 | | mp_clear(tmp2); |
5576 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
5577 | | mp_memzero_check(tmp1); |
5578 | | #endif |
5579 | | #endif |
5580 | | |
5581 | | return ret; |
5582 | | } |
5583 | | |
5584 | | |
5585 | | int wc_CheckProbablePrime_ex(const byte* pRaw, word32 pRawSz, |
5586 | | const byte* qRaw, word32 qRawSz, |
5587 | | const byte* eRaw, word32 eRawSz, |
5588 | | int nlen, int* isPrime, WC_RNG* rng) |
5589 | | { |
5590 | | #ifdef WOLFSSL_SMALL_STACK |
5591 | | mp_int *p = NULL, *q = NULL, *e = NULL; |
5592 | | #else |
5593 | | mp_int p[1], q[1], e[1]; |
5594 | | #endif |
5595 | | mp_int* Q = NULL; |
5596 | | int ret; |
5597 | | |
5598 | | if (pRaw == NULL || pRawSz == 0 || |
5599 | | eRaw == NULL || eRawSz == 0 || |
5600 | | isPrime == NULL) { |
5601 | | |
5602 | | return BAD_FUNC_ARG; |
5603 | | } |
5604 | | |
5605 | | if ((qRaw != NULL && qRawSz == 0) || (qRaw == NULL && qRawSz != 0)) |
5606 | | return BAD_FUNC_ARG; |
5607 | | |
5608 | | #ifdef WOLFSSL_SMALL_STACK |
5609 | | |
5610 | | if (((p = (mp_int *)XMALLOC(sizeof(*p), NULL, DYNAMIC_TYPE_RSA_BUFFER)) == NULL) || |
5611 | | ((q = (mp_int *)XMALLOC(sizeof(*q), NULL, DYNAMIC_TYPE_RSA_BUFFER)) == NULL) || |
5612 | | ((e = (mp_int *)XMALLOC(sizeof(*e), NULL, DYNAMIC_TYPE_RSA_BUFFER)) == NULL)) { |
5613 | | /* mp_init_multi() below is skipped, so nothing was initialized: free |
5614 | | * what was allocated here and NULL the pointers. The cleanup at the |
5615 | | * end must not see an allocated-but-uninitialized mp_int - clearing |
5616 | | * one reads a garbage used/size and corrupts the heap. */ |
5617 | | XFREE(p, NULL, DYNAMIC_TYPE_RSA_BUFFER); |
5618 | | XFREE(q, NULL, DYNAMIC_TYPE_RSA_BUFFER); |
5619 | | XFREE(e, NULL, DYNAMIC_TYPE_RSA_BUFFER); |
5620 | | p = NULL; |
5621 | | q = NULL; |
5622 | | e = NULL; |
5623 | | ret = MEMORY_E; |
5624 | | } |
5625 | | else |
5626 | | ret = 0; |
5627 | | |
5628 | | if (ret == 0) |
5629 | | #endif |
5630 | | ret = mp_init_multi(p, q, e, NULL, NULL, NULL); |
5631 | | |
5632 | | if (ret == MP_OKAY) |
5633 | | ret = mp_read_unsigned_bin(p, pRaw, pRawSz); |
5634 | | |
5635 | | if (ret == MP_OKAY) { |
5636 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
5637 | | mp_memzero_add("wc_CheckProbablePrime_ex p", p); |
5638 | | #endif |
5639 | | if (qRaw != NULL) { |
5640 | | ret = mp_read_unsigned_bin(q, qRaw, qRawSz); |
5641 | | if (ret == MP_OKAY) { |
5642 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
5643 | | mp_memzero_add("wc_CheckProbablePrime_ex q", q); |
5644 | | #endif |
5645 | | Q = q; |
5646 | | } |
5647 | | } |
5648 | | } |
5649 | | |
5650 | | if (ret == MP_OKAY) |
5651 | | ret = mp_read_unsigned_bin(e, eRaw, eRawSz); |
5652 | | |
5653 | | if (ret == 0) { |
5654 | | ret = _CheckProbablePrime(p, Q, e, nlen, isPrime, rng); |
5655 | | } |
5656 | | |
5657 | | ret = (ret == MP_OKAY) ? 0 : PRIME_GEN_E; |
5658 | | |
5659 | | #ifdef WOLFSSL_SMALL_STACK |
5660 | | if (p != NULL) { |
5661 | | mp_forcezero(p); |
5662 | | XFREE(p, NULL, DYNAMIC_TYPE_RSA_BUFFER); |
5663 | | } |
5664 | | if (q != NULL) { |
5665 | | mp_forcezero(q); |
5666 | | XFREE(q, NULL, DYNAMIC_TYPE_RSA_BUFFER); |
5667 | | } |
5668 | | if (e != NULL) { |
5669 | | mp_clear(e); |
5670 | | XFREE(e, NULL, DYNAMIC_TYPE_RSA_BUFFER); |
5671 | | } |
5672 | | #else |
5673 | | mp_forcezero(p); |
5674 | | mp_forcezero(q); |
5675 | | mp_clear(e); |
5676 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
5677 | | mp_memzero_check(p); |
5678 | | mp_memzero_check(q); |
5679 | | #endif |
5680 | | #endif |
5681 | | |
5682 | | return ret; |
5683 | | } |
5684 | | |
5685 | | |
5686 | | int wc_CheckProbablePrime(const byte* pRaw, word32 pRawSz, |
5687 | | const byte* qRaw, word32 qRawSz, |
5688 | | const byte* eRaw, word32 eRawSz, |
5689 | | int nlen, int* isPrime) |
5690 | | { |
5691 | | return wc_CheckProbablePrime_ex(pRaw, pRawSz, qRaw, qRawSz, |
5692 | | eRaw, eRawSz, nlen, isPrime, NULL); |
5693 | | } |
5694 | | |
5695 | | #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && \ |
5696 | | defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) |
5697 | | /* Make an RSA key for size bits, with e specified, 65537 is a good e */ |
5698 | | int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng) |
5699 | | { |
5700 | | #ifndef WC_NO_RNG |
5701 | | #if !defined(WOLFSSL_CRYPTOCELL) && \ |
5702 | | (!defined(WOLFSSL_SE050) || defined(WOLFSSL_SE050_NO_RSA) || \ |
5703 | | defined(WOLFSSL_SE050_ONLY_KEY_ID)) && \ |
5704 | | !defined(WOLF_CRYPTO_CB_ONLY_RSA) && \ |
5705 | | !defined(WOLFSSL_MICROCHIP_TA100) |
5706 | | #ifdef WOLFSSL_SMALL_STACK |
5707 | | mp_int *p = NULL; |
5708 | | mp_int *q = NULL; |
5709 | | mp_int *tmp1 = NULL; |
5710 | | mp_int *tmp2 = NULL; |
5711 | | mp_int *tmp3 = NULL; |
5712 | | #else |
5713 | | mp_int p_buf, *p = &p_buf; |
5714 | | mp_int q_buf, *q = &q_buf; |
5715 | | mp_int tmp1_buf, *tmp1 = &tmp1_buf; |
5716 | | mp_int tmp2_buf, *tmp2 = &tmp2_buf; |
5717 | | mp_int tmp3_buf, *tmp3 = &tmp3_buf; |
5718 | | #endif /* WOLFSSL_SMALL_STACK */ |
5719 | | int i, failCount, isPrime = 0; |
5720 | | word32 primeSz; |
5721 | | #ifndef WOLFSSL_NO_MALLOC |
5722 | | byte* buf = NULL; |
5723 | | #else |
5724 | | /* RSA_MAX_SIZE is the size of n in bits. */ |
5725 | | byte buf[RSA_MAX_SIZE/16]; |
5726 | | #endif |
5727 | | #endif /* !WOLFSSL_CRYPTOCELL && !WOLFSSL_SE050 */ |
5728 | | int err; |
5729 | | |
5730 | | #if !defined(WOLFSSL_CRYPTOCELL) && \ |
5731 | | (!defined(WOLFSSL_SE050) || defined(WOLFSSL_SE050_NO_RSA) || \ |
5732 | | defined(WOLFSSL_SE050_ONLY_KEY_ID)) && \ |
5733 | | !defined(WOLF_CRYPTO_CB_ONLY_RSA) && \ |
5734 | | !defined(WOLFSSL_MICROCHIP_TA100) && \ |
5735 | | !defined(WOLFSSL_SMALL_STACK) && defined(WOLFSSL_CHECK_MEM_ZERO) |
5736 | | /* Zero the stack temporaries so the mp_memzero_check() in the 'out' |
5737 | | * cleanup is safe even when an early argument/size check leaves via |
5738 | | * 'goto out' before these are mp_init'd - an uninitialized mp_int's size |
5739 | | * field would otherwise make the check scan an arbitrary stack range. |
5740 | | * Done here, after all declarations, to satisfy C89. */ |
5741 | | XMEMSET(&p_buf, 0, sizeof(p_buf)); |
5742 | | XMEMSET(&q_buf, 0, sizeof(q_buf)); |
5743 | | XMEMSET(&tmp1_buf, 0, sizeof(tmp1_buf)); |
5744 | | XMEMSET(&tmp2_buf, 0, sizeof(tmp2_buf)); |
5745 | | XMEMSET(&tmp3_buf, 0, sizeof(tmp3_buf)); |
5746 | | #endif |
5747 | | |
5748 | | if (key == NULL || rng == NULL) { |
5749 | | err = BAD_FUNC_ARG; |
5750 | | goto out; |
5751 | | } |
5752 | | |
5753 | | if (!RsaSizeCheck(size)) { |
5754 | | err = BAD_FUNC_ARG; |
5755 | | goto out; |
5756 | | } |
5757 | | |
5758 | | #if defined(HAVE_FIPS) |
5759 | | /* WC_RSA_EXPONENT is 65537, which is what FIPS 186-5 sec 5.4(e) requires |
5760 | | * as the lower bound. e is a long, so it cannot reach the 2^256 upper |
5761 | | * bound the same clause sets. */ |
5762 | | if (e < WC_RSA_EXPONENT || (e & 1) == 0) { |
5763 | | #else |
5764 | | if (e < 3 || (e & 1) == 0) { |
5765 | | #endif |
5766 | | err = BAD_FUNC_ARG; |
5767 | | goto out; |
5768 | | } |
5769 | | |
5770 | | #if defined(WOLFSSL_CRYPTOCELL) |
5771 | | err = cc310_RSA_GenerateKeyPair(key, size, e); |
5772 | | goto out; |
5773 | | #elif defined(WOLFSSL_MICROCHIP_TA100) |
5774 | | err = wc_Microchip_rsa_create_key(key, size, e); |
5775 | | goto out; |
5776 | | #elif defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA) && \ |
5777 | | !defined(WOLFSSL_SE050_ONLY_KEY_ID) |
5778 | | err = se050_rsa_create_key(key, size, e); |
5779 | | goto out; |
5780 | | #else |
5781 | | /* software crypto */ |
5782 | | |
5783 | | #ifdef WOLFSSL_SMALL_STACK |
5784 | | p = (mp_int *)XMALLOC(sizeof *p, key->heap, DYNAMIC_TYPE_RSA); |
5785 | | q = (mp_int *)XMALLOC(sizeof *q, key->heap, DYNAMIC_TYPE_RSA); |
5786 | | tmp1 = (mp_int *)XMALLOC(sizeof *tmp1, key->heap, DYNAMIC_TYPE_RSA); |
5787 | | tmp2 = (mp_int *)XMALLOC(sizeof *tmp2, key->heap, DYNAMIC_TYPE_RSA); |
5788 | | tmp3 = (mp_int *)XMALLOC(sizeof *tmp3, key->heap, DYNAMIC_TYPE_RSA); |
5789 | | |
5790 | | if ((p == NULL) || |
5791 | | (q == NULL) || |
5792 | | (tmp1 == NULL) || |
5793 | | (tmp2 == NULL) || |
5794 | | (tmp3 == NULL)) { |
5795 | | err = MEMORY_E; |
5796 | | goto out; |
5797 | | } |
5798 | | #endif |
5799 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
5800 | | XMEMSET(p, 0, sizeof(*p)); |
5801 | | XMEMSET(q, 0, sizeof(*q)); |
5802 | | XMEMSET(tmp1, 0, sizeof(*tmp1)); |
5803 | | XMEMSET(tmp2, 0, sizeof(*tmp2)); |
5804 | | XMEMSET(tmp3, 0, sizeof(*tmp3)); |
5805 | | #endif |
5806 | | |
5807 | | #ifdef WOLF_CRYPTO_CB |
5808 | | #ifndef WOLF_CRYPTO_CB_FIND |
5809 | | if (key->devId != INVALID_DEVID) |
5810 | | #endif |
5811 | | { |
5812 | | err = wc_CryptoCb_MakeRsaKey(key, size, e, rng); |
5813 | | #ifdef WOLF_CRYPTO_CB_ONLY_RSA |
5814 | | if (err == WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
5815 | | err = NO_VALID_DEVID; |
5816 | | goto out; |
5817 | | } |
5818 | | #else |
5819 | | if (err != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
5820 | | goto out; |
5821 | | } |
5822 | | /* fall-through when unavailable */ |
5823 | | #endif |
5824 | | } |
5825 | | #if !defined(WOLF_CRYPTO_CB_FIND) && defined(WOLF_CRYPTO_CB_ONLY_RSA) |
5826 | | else { |
5827 | | err = NO_VALID_DEVID; |
5828 | | } |
5829 | | #endif |
5830 | | #endif |
5831 | | |
5832 | | #ifndef WOLF_CRYPTO_CB_ONLY_RSA |
5833 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA) && \ |
5834 | | defined(WC_ASYNC_ENABLE_RSA_KEYGEN) |
5835 | | if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_RSA) { |
5836 | | #ifdef HAVE_CAVIUM |
5837 | | /* TODO: Not implemented */ |
5838 | | #elif defined(HAVE_INTEL_QA) |
5839 | | err = IntelQaRsaKeyGen(&key->asyncDev, key, size, e, rng); |
5840 | | goto out; |
5841 | | #elif defined(WOLFSSL_ASYNC_CRYPT_SW) |
5842 | | if (wc_AsyncSwInit(&key->asyncDev, ASYNC_SW_RSA_MAKE)) { |
5843 | | WC_ASYNC_SW* sw = &key->asyncDev.sw; |
5844 | | sw->rsaMake.rng = rng; |
5845 | | sw->rsaMake.key = key; |
5846 | | sw->rsaMake.size = size; |
5847 | | sw->rsaMake.e = e; |
5848 | | err = WC_PENDING_E; |
5849 | | goto out; |
5850 | | } |
5851 | | #endif |
5852 | | } |
5853 | | #endif |
5854 | | |
5855 | | err = mp_init_multi(p, q, tmp1, tmp2, tmp3, NULL); |
5856 | | |
5857 | | if (err == MP_OKAY) |
5858 | | err = mp_set_int(tmp3, (unsigned long)e); |
5859 | | |
5860 | | /* The failCount value comes from NIST FIPS 186-4, section B.3.3, |
5861 | | * process steps 4.7 and 5.8. */ |
5862 | | failCount = 5 * (size / 2); |
5863 | | primeSz = (word32)size / 16; /* size is the size of n in bits. |
5864 | | primeSz is in bytes. */ |
5865 | | |
5866 | | #ifndef WOLFSSL_NO_MALLOC |
5867 | | /* allocate buffer to work with */ |
5868 | | if (err == MP_OKAY) { |
5869 | | buf = (byte*)XMALLOC(primeSz, key->heap, DYNAMIC_TYPE_RSA); |
5870 | | if (buf == NULL) |
5871 | | err = MEMORY_E; |
5872 | | } |
5873 | | #endif |
5874 | | |
5875 | | /* make p */ |
5876 | | if (err == MP_OKAY) { |
5877 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
5878 | | wc_MemZero_Add("RSA gen buf", buf, primeSz); |
5879 | | mp_memzero_add("RSA gen p", p); |
5880 | | mp_memzero_add("RSA gen q", q); |
5881 | | mp_memzero_add("RSA gen tmp1", tmp1); |
5882 | | mp_memzero_add("RSA gen tmp2", tmp2); |
5883 | | mp_memzero_add("RSA gen tmp3", tmp3); |
5884 | | #endif |
5885 | | isPrime = 0; |
5886 | | i = 0; |
5887 | | for (;;) { |
5888 | | #ifdef SHOW_GEN |
5889 | | printf("."); |
5890 | | fflush(stdout); |
5891 | | #endif |
5892 | | /* generate value */ |
5893 | | err = wc_RNG_GenerateBlock(rng, buf, primeSz); |
5894 | | if (err == 0) { |
5895 | | /* prime lower bound has the MSB set, set it in candidate */ |
5896 | | buf[0] |= 0x80; |
5897 | | /* make candidate odd */ |
5898 | | buf[primeSz-1] |= 0x01; |
5899 | | /* load value */ |
5900 | | err = mp_read_unsigned_bin(p, buf, primeSz); |
5901 | | } |
5902 | | |
5903 | | if (err == MP_OKAY) |
5904 | | err = _CheckProbablePrime(p, NULL, tmp3, size, &isPrime, rng); |
5905 | | |
5906 | | #ifdef HAVE_FIPS |
5907 | | i++; |
5908 | | #else |
5909 | | /* Keep the old retry behavior in non-FIPS build. */ |
5910 | | #endif |
5911 | | |
5912 | | if (err != MP_OKAY || isPrime || i >= failCount) |
5913 | | break; |
5914 | | |
5915 | | err = WC_CHECK_FOR_INTR_SIGNALS(); |
5916 | | if (err != 0) |
5917 | | break; |
5918 | | WC_RELAX_LONG_LOOP(); |
5919 | | }; |
5920 | | } |
5921 | | |
5922 | | if (err == MP_OKAY && !isPrime) |
5923 | | err = PRIME_GEN_E; |
5924 | | |
5925 | | /* make q */ |
5926 | | if (err == MP_OKAY) { |
5927 | | isPrime = 0; |
5928 | | i = 0; |
5929 | | do { |
5930 | | #ifdef SHOW_GEN |
5931 | | printf("."); |
5932 | | fflush(stdout); |
5933 | | #endif |
5934 | | /* generate value */ |
5935 | | err = wc_RNG_GenerateBlock(rng, buf, primeSz); |
5936 | | if (err == 0) { |
5937 | | /* prime lower bound has the MSB set, set it in candidate */ |
5938 | | buf[0] |= 0x80; |
5939 | | /* make candidate odd */ |
5940 | | buf[primeSz-1] |= 0x01; |
5941 | | /* load value */ |
5942 | | err = mp_read_unsigned_bin(q, buf, primeSz); |
5943 | | } |
5944 | | |
5945 | | if (err == MP_OKAY) |
5946 | | err = _CheckProbablePrime(p, q, tmp3, size, &isPrime, rng); |
5947 | | |
5948 | | #ifndef WC_RSA_NO_FERMAT_CHECK |
5949 | | if (err == MP_OKAY && isPrime) { |
5950 | | /* Fermat's Factorization works when difference between p and q |
5951 | | * is less than (conservatively): |
5952 | | * n^(1/4) + 32 |
5953 | | * ~= 2^(bit count of n)^(1/4) + 32) |
5954 | | * = 2^((bit count of n)/4 + 32) |
5955 | | */ |
5956 | | err = mp_sub(p, q, tmp1); |
5957 | | if (err == MP_OKAY && mp_count_bits(tmp1) <= (size / 4) + 32) { |
5958 | | isPrime = 0; |
5959 | | } |
5960 | | } |
5961 | | #endif |
5962 | | |
5963 | | #ifdef HAVE_FIPS |
5964 | | i++; |
5965 | | #else |
5966 | | /* Keep the old retry behavior in non-FIPS build. */ |
5967 | | (void)i; |
5968 | | #endif |
5969 | | |
5970 | | #if FIPS_VERSION3_GE(7,0,0) |
5971 | | /* Check err before WC_CHECK_FOR_INTR_SIGNALS() overwrites it, as |
5972 | | * the p loop above does. Otherwise a DRBG failure is discarded |
5973 | | * and resurfaces as PRIME_GEN_E, hiding what actually went wrong. |
5974 | | * SP 800-90A Rev1 sec 11.4.2 requires the DRBG's own error |
5975 | | * indicator to reach the caller. */ |
5976 | | if (err != MP_OKAY || isPrime || i >= failCount) |
5977 | | break; |
5978 | | #endif |
5979 | | |
5980 | | err = WC_CHECK_FOR_INTR_SIGNALS(); |
5981 | | if (err != 0) |
5982 | | break; |
5983 | | WC_RELAX_LONG_LOOP(); |
5984 | | |
5985 | | } while (err == MP_OKAY && !isPrime && i < failCount); |
5986 | | } |
5987 | | |
5988 | | if (err == MP_OKAY && !isPrime) |
5989 | | err = PRIME_GEN_E; |
5990 | | |
5991 | | #ifndef WOLFSSL_NO_MALLOC |
5992 | | if (buf) { |
5993 | | ForceZero(buf, primeSz); |
5994 | | XFREE(buf, key->heap, DYNAMIC_TYPE_RSA); |
5995 | | } |
5996 | | #else |
5997 | | ForceZero(buf, primeSz); |
5998 | | #endif |
5999 | | |
6000 | | if (err == MP_OKAY && mp_cmp(p, q) < 0) { |
6001 | | err = mp_copy(p, tmp1); |
6002 | | if (err == MP_OKAY) |
6003 | | err = mp_copy(q, p); |
6004 | | if (err == MP_OKAY) |
6005 | | mp_copy(tmp1, q); |
6006 | | } |
6007 | | |
6008 | | /* Setup RsaKey buffers */ |
6009 | | if (err == MP_OKAY) |
6010 | | err = mp_init_multi(&key->n, &key->e, &key->d, &key->p, &key->q, NULL); |
6011 | | if (err == MP_OKAY) |
6012 | | err = mp_init_multi(&key->dP, &key->dQ, &key->u, NULL, NULL, NULL); |
6013 | | |
6014 | | /* Software Key Calculation */ |
6015 | | if (err == MP_OKAY) /* tmp1 = p-1 */ |
6016 | | err = mp_sub_d(p, 1, tmp1); |
6017 | | if (err == MP_OKAY) /* tmp2 = q-1 */ |
6018 | | err = mp_sub_d(q, 1, tmp2); |
6019 | | #ifdef WC_RSA_BLINDING |
6020 | | if (err == MP_OKAY) /* tmp3 = order of n */ |
6021 | | err = mp_mul(tmp1, tmp2, tmp3); |
6022 | | #else |
6023 | | if (err == MP_OKAY) /* tmp3 = lcm(p-1, q-1), last loop */ |
6024 | | err = mp_lcm(tmp1, tmp2, tmp3); |
6025 | | #endif |
6026 | | /* make key */ |
6027 | | if (err == MP_OKAY) /* key->e = e */ |
6028 | | err = mp_set_int(&key->e, (unsigned long)e); |
6029 | | #ifdef WC_RSA_BLINDING |
6030 | | /* Blind the inverse operation with a value that is invertable */ |
6031 | | if (err == MP_OKAY) { |
6032 | | do { |
6033 | | err = mp_rand(&key->p, mp_get_digit_count(tmp3), rng); |
6034 | | if (err == MP_OKAY) |
6035 | | err = mp_set_bit(&key->p, 0); |
6036 | | if (err == MP_OKAY) |
6037 | | err = mp_set_bit(&key->p, size - 1); |
6038 | | if (err == MP_OKAY) |
6039 | | err = mp_gcd(&key->p, tmp3, &key->q); |
6040 | | } |
6041 | | while ((err == MP_OKAY) && !mp_isone(&key->q)); |
6042 | | } |
6043 | | /* 8/16-bit word size requires a full multiply when e=0x10001 */ |
6044 | | if (err == MP_OKAY) |
6045 | | err = mp_mul(&key->p, &key->e, &key->e); |
6046 | | #endif |
6047 | | if (err == MP_OKAY) /* key->d = 1/e mod lcm(p-1, q-1) */ |
6048 | | err = mp_invmod(&key->e, tmp3, &key->d); |
6049 | | #ifdef WC_RSA_BLINDING |
6050 | | /* Take off blinding from d and reset e */ |
6051 | | if (err == MP_OKAY) |
6052 | | err = mp_mulmod(&key->d, &key->p, tmp3, &key->d); |
6053 | | if (err == MP_OKAY) |
6054 | | err = mp_set_int(&key->e, (unsigned long)e); |
6055 | | #endif |
6056 | | if (err == MP_OKAY) /* key->n = pq */ |
6057 | | err = mp_mul(p, q, &key->n); |
6058 | | if (err == MP_OKAY) /* key->dP = d mod(p-1) */ |
6059 | | err = mp_mod(&key->d, tmp1, &key->dP); |
6060 | | if (err == MP_OKAY) /* key->dQ = d mod(q-1) */ |
6061 | | err = mp_mod(&key->d, tmp2, &key->dQ); |
6062 | | #ifdef WOLFSSL_MP_INVMOD_CONSTANT_TIME |
6063 | | if (err == MP_OKAY) /* key->u = 1/q mod p */ |
6064 | | err = mp_invmod(q, p, &key->u); |
6065 | | #else |
6066 | | if (err == MP_OKAY) |
6067 | | err = mp_sub_d(p, 2, tmp3); |
6068 | | if (err == MP_OKAY) /* key->u = 1/q mod p = q^p-2 mod p */ |
6069 | | err = mp_exptmod(q, tmp3, p, &key->u); |
6070 | | #endif |
6071 | | if (err == MP_OKAY) |
6072 | | err = mp_copy(p, &key->p); |
6073 | | if (err == MP_OKAY) |
6074 | | err = mp_copy(q, &key->q); |
6075 | | |
6076 | | #ifdef HAVE_WOLF_BIGINT |
6077 | | /* make sure raw unsigned bin version is available */ |
6078 | | if (err == MP_OKAY) |
6079 | | err = wc_mp_to_bigint(&key->n, &key->n.raw); |
6080 | | if (err == MP_OKAY) |
6081 | | err = wc_mp_to_bigint(&key->e, &key->e.raw); |
6082 | | if (err == MP_OKAY) |
6083 | | err = wc_mp_to_bigint(&key->d, &key->d.raw); |
6084 | | if (err == MP_OKAY) |
6085 | | err = wc_mp_to_bigint(&key->p, &key->p.raw); |
6086 | | if (err == MP_OKAY) |
6087 | | err = wc_mp_to_bigint(&key->q, &key->q.raw); |
6088 | | if (err == MP_OKAY) |
6089 | | err = wc_mp_to_bigint(&key->dP, &key->dP.raw); |
6090 | | if (err == MP_OKAY) |
6091 | | err = wc_mp_to_bigint(&key->dQ, &key->dQ.raw); |
6092 | | if (err == MP_OKAY) |
6093 | | err = wc_mp_to_bigint(&key->u, &key->u.raw); |
6094 | | #endif |
6095 | | |
6096 | | if (err == MP_OKAY) |
6097 | | key->type = RSA_PRIVATE; |
6098 | | |
6099 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
6100 | | if (err == MP_OKAY) { |
6101 | | mp_memzero_add("Make RSA key d", &key->d); |
6102 | | mp_memzero_add("Make RSA key p", &key->p); |
6103 | | mp_memzero_add("Make RSA key q", &key->q); |
6104 | | mp_memzero_add("Make RSA key dP", &key->dP); |
6105 | | mp_memzero_add("Make RSA key dQ", &key->dQ); |
6106 | | mp_memzero_add("Make RSA key u", &key->u); |
6107 | | } |
6108 | | #endif |
6109 | | |
6110 | | /* Last value p - 1. */ |
6111 | | mp_forcezero(tmp1); |
6112 | | /* Last value q - 1. */ |
6113 | | mp_forcezero(tmp2); |
6114 | | /* Last value p - 2. */ |
6115 | | mp_forcezero(tmp3); |
6116 | | mp_forcezero(p); |
6117 | | mp_forcezero(q); |
6118 | | |
6119 | | #ifdef WOLFSSL_RSA_KEY_CHECK |
6120 | | /* Perform the pair-wise consistency test on the new key. */ |
6121 | | if (err == 0) |
6122 | | err = _ifc_pairwise_consistency_test(key, rng); |
6123 | | #endif |
6124 | | |
6125 | | if (err != 0) { |
6126 | | wc_FreeRsaKey(key); |
6127 | | goto out; |
6128 | | } |
6129 | | |
6130 | | #if defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_CRYPTOCELL) |
6131 | | if (wc_InitRsaHw(key) != 0) { |
6132 | | return BAD_STATE_E; |
6133 | | } |
6134 | | #endif |
6135 | | |
6136 | | err = 0; |
6137 | | #endif /* WOLF_CRYPTO_CB_ONLY_RSA */ |
6138 | | #endif /* WOLFSSL_CRYPTOCELL / SW only */ |
6139 | | out: |
6140 | | |
6141 | | #if !defined(WOLFSSL_CRYPTOCELL) && \ |
6142 | | (!defined(WOLFSSL_SE050) || defined(WOLFSSL_SE050_ONLY_KEY_ID)) |
6143 | | #ifdef WOLFSSL_SMALL_STACK |
6144 | | if (key != NULL) { |
6145 | | XFREE(p, key->heap, DYNAMIC_TYPE_RSA); |
6146 | | XFREE(q, key->heap, DYNAMIC_TYPE_RSA); |
6147 | | XFREE(tmp1, key->heap, DYNAMIC_TYPE_RSA); |
6148 | | XFREE(tmp2, key->heap, DYNAMIC_TYPE_RSA); |
6149 | | XFREE(tmp3, key->heap, DYNAMIC_TYPE_RSA); |
6150 | | } |
6151 | | #elif defined(WOLFSSL_CHECK_MEM_ZERO) |
6152 | | mp_memzero_check(p); |
6153 | | mp_memzero_check(q); |
6154 | | mp_memzero_check(tmp1); |
6155 | | mp_memzero_check(tmp2); |
6156 | | mp_memzero_check(tmp3); |
6157 | | #endif /* WOLFSSL_SMALL_STACK */ |
6158 | | #endif /* !WOLFSSL_CRYPTOCELL && !WOLFSSL_SE050 */ |
6159 | | |
6160 | | return err; |
6161 | | |
6162 | | #else |
6163 | | return NOT_COMPILED_IN; |
6164 | | #endif |
6165 | | } |
6166 | | #endif /* !FIPS || FIPS_VER >= 2 */ |
6167 | | #endif /* WOLFSSL_KEY_GEN */ |
6168 | | |
6169 | | #ifndef WC_NO_RNG |
6170 | | int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng) |
6171 | 0 | { |
6172 | 0 | if (key == NULL || rng == NULL) |
6173 | 0 | return BAD_FUNC_ARG; |
6174 | | |
6175 | 0 | key->rng = rng; |
6176 | |
|
6177 | 0 | return 0; |
6178 | 0 | } |
6179 | | #endif /* !WC_NO_RNG */ |
6180 | | |
6181 | | #ifdef WC_RSA_NONBLOCK |
6182 | | int wc_RsaSetNonBlock(RsaKey* key, RsaNb* nb) |
6183 | | { |
6184 | | if (key == NULL) |
6185 | | return BAD_FUNC_ARG; |
6186 | | |
6187 | | if (nb) { |
6188 | | XMEMSET(nb, 0, sizeof(RsaNb)); |
6189 | | } |
6190 | | |
6191 | | /* Allow nb == NULL to clear non-block mode */ |
6192 | | key->nb = nb; |
6193 | | |
6194 | | return 0; |
6195 | | } |
6196 | | #if defined(WC_RSA_NONBLOCK_TIME) && defined(USE_FAST_MATH) |
6197 | | int wc_RsaSetNonBlockTime(RsaKey* key, word32 maxBlockUs, word32 cpuMHz) |
6198 | | { |
6199 | | if (key == NULL || key->nb == NULL) { |
6200 | | return BAD_FUNC_ARG; |
6201 | | } |
6202 | | |
6203 | | /* calculate maximum number of instructions to block */ |
6204 | | key->nb->exptmod.maxBlockInst = cpuMHz * maxBlockUs; |
6205 | | |
6206 | | return 0; |
6207 | | } |
6208 | | #endif /* WC_RSA_NONBLOCK_TIME && USE_FAST_MATH */ |
6209 | | #endif /* WC_RSA_NONBLOCK */ |
6210 | | |
6211 | | #ifndef WOLFSSL_RSA_PUBLIC_ONLY |
6212 | | |
6213 | | #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM) |
6214 | | /* |
6215 | | * Calculate y = d mod(x-1) |
6216 | | */ |
6217 | | static int CalcDX(mp_int* y, mp_int* x, mp_int* d) |
6218 | 0 | { |
6219 | 0 | int err; |
6220 | 0 | #ifndef WOLFSSL_SMALL_STACK |
6221 | 0 | mp_int m[1]; |
6222 | | #else |
6223 | | mp_int* m = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_WOLF_BIGINT); |
6224 | | if (m == NULL) |
6225 | | return MEMORY_E; |
6226 | | #endif |
6227 | |
|
6228 | 0 | err = mp_init(m); |
6229 | 0 | if (err == MP_OKAY) { |
6230 | 0 | err = mp_sub_d(x, 1, m); |
6231 | 0 | if (err == MP_OKAY) |
6232 | 0 | err = mp_mod(d, m, y); |
6233 | 0 | mp_forcezero(m); |
6234 | 0 | } |
6235 | |
|
6236 | 0 | WC_FREE_VAR_EX(m, NULL, DYNAMIC_TYPE_WOLF_BIGINT); |
6237 | |
|
6238 | 0 | return err; |
6239 | 0 | } |
6240 | | #endif |
6241 | | |
6242 | | /* Software-only import of RSA private key elements into RsaKey. |
6243 | | * This internal helper avoids recursion when called from the SETKEY path. */ |
6244 | | static int _RsaPrivateKeyDecodeRaw(const byte* n, word32 nSz, |
6245 | | const byte* e, word32 eSz, const byte* d, word32 dSz, |
6246 | | const byte* u, word32 uSz, const byte* p, word32 pSz, |
6247 | | const byte* q, word32 qSz, const byte* dP, word32 dPSz, |
6248 | | const byte* dQ, word32 dQSz, RsaKey* key) |
6249 | 0 | { |
6250 | 0 | int err = MP_OKAY; |
6251 | |
|
6252 | 0 | if (n == NULL || nSz == 0 || e == NULL || eSz == 0 |
6253 | 0 | || d == NULL || dSz == 0 || p == NULL || pSz == 0 |
6254 | 0 | || q == NULL || qSz == 0 || key == NULL) { |
6255 | 0 | return BAD_FUNC_ARG; |
6256 | 0 | } |
6257 | | |
6258 | 0 | #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM) |
6259 | 0 | if ((u == NULL || uSz == 0) |
6260 | 0 | || (dP != NULL && dPSz == 0) |
6261 | 0 | || (dQ != NULL && dQSz == 0)) { |
6262 | 0 | return BAD_FUNC_ARG; |
6263 | 0 | } |
6264 | | #else |
6265 | | (void)u; |
6266 | | (void)uSz; |
6267 | | (void)dP; |
6268 | | (void)dPSz; |
6269 | | (void)dQ; |
6270 | | (void)dQSz; |
6271 | | #endif |
6272 | | |
6273 | 0 | if (err == MP_OKAY) { |
6274 | 0 | err = mp_read_unsigned_bin(&key->n, n, nSz); |
6275 | 0 | } |
6276 | 0 | if (err == MP_OKAY) { |
6277 | 0 | err = mp_read_unsigned_bin(&key->e, e, eSz); |
6278 | 0 | } |
6279 | 0 | if (err == MP_OKAY) { |
6280 | 0 | err = mp_read_unsigned_bin(&key->d, d, dSz); |
6281 | 0 | } |
6282 | 0 | if (err == MP_OKAY) { |
6283 | 0 | err = mp_read_unsigned_bin(&key->p, p, pSz); |
6284 | 0 | } |
6285 | 0 | if (err == MP_OKAY) { |
6286 | 0 | err = mp_read_unsigned_bin(&key->q, q, qSz); |
6287 | 0 | } |
6288 | 0 | #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM) |
6289 | 0 | if (err == MP_OKAY) { |
6290 | 0 | err = mp_read_unsigned_bin(&key->u, u, uSz); |
6291 | 0 | } |
6292 | 0 | if (err == MP_OKAY) { |
6293 | 0 | if (dP != NULL) { |
6294 | 0 | err = mp_read_unsigned_bin(&key->dP, dP, dPSz); |
6295 | 0 | } |
6296 | 0 | else { |
6297 | 0 | err = CalcDX(&key->dP, &key->p, &key->d); |
6298 | 0 | } |
6299 | 0 | } |
6300 | 0 | if (err == MP_OKAY) { |
6301 | 0 | if (dQ != NULL) { |
6302 | 0 | err = mp_read_unsigned_bin(&key->dQ, dQ, dQSz); |
6303 | 0 | } |
6304 | 0 | else { |
6305 | 0 | err = CalcDX(&key->dQ, &key->q, &key->d); |
6306 | 0 | } |
6307 | 0 | } |
6308 | 0 | #endif |
6309 | |
|
6310 | 0 | if (err == MP_OKAY) { |
6311 | 0 | key->type = RSA_PRIVATE; |
6312 | 0 | } |
6313 | 0 | else { |
6314 | 0 | mp_clear(&key->n); |
6315 | 0 | mp_clear(&key->e); |
6316 | 0 | mp_forcezero(&key->d); |
6317 | 0 | mp_forcezero(&key->p); |
6318 | 0 | mp_forcezero(&key->q); |
6319 | 0 | #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM) |
6320 | 0 | mp_forcezero(&key->u); |
6321 | 0 | mp_forcezero(&key->dP); |
6322 | 0 | mp_forcezero(&key->dQ); |
6323 | 0 | #endif |
6324 | 0 | } |
6325 | |
|
6326 | 0 | return err; |
6327 | 0 | } |
6328 | | |
6329 | | int wc_RsaPrivateKeyDecodeRaw(const byte* n, word32 nSz, |
6330 | | const byte* e, word32 eSz, const byte* d, word32 dSz, |
6331 | | const byte* u, word32 uSz, const byte* p, word32 pSz, |
6332 | | const byte* q, word32 qSz, const byte* dP, word32 dPSz, |
6333 | | const byte* dQ, word32 dQSz, RsaKey* key) |
6334 | 0 | { |
6335 | 0 | int err = MP_OKAY; |
6336 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_SETKEY) |
6337 | | int cbRet = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
6338 | | WC_DECLARE_VAR(tmpKey, RsaKey, 1, NULL); |
6339 | | #endif |
6340 | |
|
6341 | 0 | if (n == NULL || nSz == 0 || e == NULL || eSz == 0 |
6342 | 0 | || d == NULL || dSz == 0 || p == NULL || pSz == 0 |
6343 | 0 | || q == NULL || qSz == 0 || key == NULL) { |
6344 | 0 | err = BAD_FUNC_ARG; |
6345 | 0 | } |
6346 | |
|
6347 | 0 | #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM) |
6348 | 0 | if (err == MP_OKAY) { |
6349 | 0 | if ((u == NULL || uSz == 0) |
6350 | 0 | || (dP != NULL && dPSz == 0) |
6351 | 0 | || (dQ != NULL && dQSz == 0)) { |
6352 | 0 | err = BAD_FUNC_ARG; |
6353 | 0 | } |
6354 | 0 | } |
6355 | | #else |
6356 | | (void)u; |
6357 | | (void)uSz; |
6358 | | (void)dP; |
6359 | | (void)dPSz; |
6360 | | (void)dQ; |
6361 | | (void)dQSz; |
6362 | | #endif |
6363 | |
|
6364 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_SETKEY) |
6365 | | #ifndef WOLF_CRYPTO_CB_FIND |
6366 | | if (err == MP_OKAY && key->devId != INVALID_DEVID) |
6367 | | #else |
6368 | | if (err == MP_OKAY) |
6369 | | #endif |
6370 | | { |
6371 | | /* Allocate temp key for callback to export from */ |
6372 | | WC_ALLOC_VAR(tmpKey, RsaKey, 1, key->heap); |
6373 | | if (!WC_VAR_OK(tmpKey)) { |
6374 | | return MEMORY_E; |
6375 | | } |
6376 | | XMEMSET(tmpKey, 0, sizeof(RsaKey)); |
6377 | | |
6378 | | /* Init temp with INVALID_DEVID to prevent callback recursion */ |
6379 | | err = wc_InitRsaKey_ex(tmpKey, key->heap, INVALID_DEVID); |
6380 | | if (err != MP_OKAY) { |
6381 | | WC_FREE_VAR(tmpKey, key->heap); |
6382 | | return err; |
6383 | | } |
6384 | | |
6385 | | /* Import into temp via software helper (no callback recursion) */ |
6386 | | err = _RsaPrivateKeyDecodeRaw(n, nSz, e, eSz, d, dSz, |
6387 | | u, uSz, p, pSz, q, qSz, dP, dPSz, dQ, dQSz, tmpKey); |
6388 | | if (err == MP_OKAY) { |
6389 | | cbRet = wc_CryptoCb_SetKey(key->devId, |
6390 | | WC_SETKEY_RSA_PRIV, key, tmpKey, |
6391 | | wc_RsaEncryptSize(tmpKey), NULL, 0, 0); |
6392 | | } |
6393 | | |
6394 | | /* wc_FreeRsaKey calls mp_forcezero on all private key components, |
6395 | | * so no separate ForceZero of the struct is needed here. */ |
6396 | | wc_FreeRsaKey(tmpKey); |
6397 | | WC_FREE_VAR(tmpKey, key->heap); |
6398 | | |
6399 | | if (err != MP_OKAY) { |
6400 | | return err; |
6401 | | } |
6402 | | if (cbRet != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
6403 | | return cbRet; |
6404 | | } |
6405 | | /* CRYPTOCB_UNAVAILABLE: fall through to software import */ |
6406 | | err = MP_OKAY; |
6407 | | } |
6408 | | #endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_SETKEY */ |
6409 | |
|
6410 | 0 | if (err == MP_OKAY) { |
6411 | 0 | err = _RsaPrivateKeyDecodeRaw(n, nSz, e, eSz, d, dSz, |
6412 | 0 | u, uSz, p, pSz, q, qSz, dP, dPSz, dQ, dQSz, key); |
6413 | 0 | } |
6414 | |
|
6415 | 0 | return err; |
6416 | 0 | } |
6417 | | #endif /* WOLFSSL_RSA_PUBLIC_ONLY */ |
6418 | | |
6419 | | #endif /* NO_RSA */ |