• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 #include "hitls_build.h"
17 #ifdef HITLS_CRYPTO_RSA
18 
19 #include "crypt_utils.h"
20 #include "rsa_local.h"
21 #include "crypt_errno.h"
22 #include "securec.h"
23 #include "eal_md_local.h"
24 
25 #ifdef HITLS_CRYPTO_RSA_EMSA_PKCSV15
SetEmsaPkcsV15(CRYPT_RSA_Ctx * ctx,void * val,uint32_t len)26 static int32_t SetEmsaPkcsV15(CRYPT_RSA_Ctx *ctx, void *val, uint32_t len)
27 {
28     if (val == NULL) {
29         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
30         return CRYPT_NULL_INPUT;
31     }
32     if (len != sizeof(int32_t)) {
33         BSL_ERR_PUSH_ERROR(CRYPT_RSA_SET_EMS_PKCSV15_LEN_ERROR);
34         return CRYPT_RSA_SET_EMS_PKCSV15_LEN_ERROR;
35     }
36     static const uint32_t SIGN_MD_ID_LIST[] = { CRYPT_MD_SHA224, CRYPT_MD_SHA256,
37         CRYPT_MD_SHA384, CRYPT_MD_SHA512, CRYPT_MD_SM3, CRYPT_MD_SHA1, CRYPT_MD_MD5
38     };
39 
40     int32_t mdId = *(int32_t *)val;
41     if (ParamIdIsValid(mdId, SIGN_MD_ID_LIST, sizeof(SIGN_MD_ID_LIST) / sizeof(SIGN_MD_ID_LIST[0])) == false) {
42         // This hash algorithm is not supported.
43         BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_MD_ALGID);
44         return CRYPT_RSA_ERR_MD_ALGID;
45     }
46     (void)memset_s(&(ctx->pad), sizeof(RSAPad), 0, sizeof(RSAPad));
47     ctx->pad.type = EMSA_PKCSV15;
48     ctx->pad.para.pkcsv15.mdId = mdId;
49     return CRYPT_SUCCESS;
50 }
51 #endif
52 
53 #ifdef HITLS_CRYPTO_RSA_EMSA_PSS
54 
SetEmsaPss(CRYPT_RSA_Ctx * ctx,RSA_PadingPara * pad)55 static int32_t SetEmsaPss(CRYPT_RSA_Ctx *ctx, RSA_PadingPara *pad)
56 {
57     uint32_t bits = CRYPT_RSA_GetBits(ctx);
58     if (bits == 0) {
59         // The valid key information does not exist.
60         BSL_ERR_PUSH_ERROR(CRYPT_RSA_NO_KEY_INFO);
61         return CRYPT_RSA_NO_KEY_INFO;
62     }
63     if (pad->saltLen < CRYPT_RSA_SALTLEN_TYPE_AUTOLEN) {
64         BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_SALT_LEN);
65         return  CRYPT_RSA_ERR_SALT_LEN;
66     }
67     uint32_t saltLen = (uint32_t)pad->saltLen;
68     if (pad->saltLen == CRYPT_RSA_SALTLEN_TYPE_HASHLEN) {
69         saltLen = pad->mdMeth->mdSize;
70     }
71     uint32_t bytes = BN_BITS_TO_BYTES(bits);
72     // The minimum specification supported by RSA is 1K,
73     // and the maximum hash length supported by the hash algorithm is 64 bytes.
74     // Therefore, specifying the salt length as the maximum available length is satisfied.
75     if (pad->saltLen != CRYPT_RSA_SALTLEN_TYPE_MAXLEN && pad->saltLen != CRYPT_RSA_SALTLEN_TYPE_AUTOLEN &&
76         saltLen > bytes - pad->mdMeth->mdSize - 2) { // maximum length of the salt is padLen-mdMethod->GetDigestSize-2
77         // The configured salt length does not meet the specification.
78         BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_PSS_SALT_LEN);
79         return CRYPT_RSA_ERR_PSS_SALT_LEN;
80     }
81     (void)memset_s(&(ctx->pad), sizeof(RSAPad), 0, sizeof(RSAPad));
82     (void)memcpy_s(&(ctx->pad.para.pss), sizeof(RSA_PadingPara), pad, sizeof(RSA_PadingPara));
83     ctx->pad.type = EMSA_PSS;
84     ctx->pad.para.pss.mdId = pad->mdId;
85     ctx->pad.para.pss.mgfId = pad->mgfId;
86     return CRYPT_SUCCESS;
87 }
88 #endif // HITLS_CRYPTO_RSA_EMSA_PSS
89 
90 #ifdef HITLS_CRYPTO_RSAES_OAEP
91 
SetOaep(CRYPT_RSA_Ctx * ctx,const RSA_PadingPara * val)92 void SetOaep(CRYPT_RSA_Ctx *ctx, const RSA_PadingPara *val)
93 {
94     (void)memset_s(&(ctx->pad), sizeof(RSAPad), 0, sizeof(RSAPad));
95     (void)memcpy_s(&(ctx->pad.para.oaep), sizeof(RSA_PadingPara), val, sizeof(RSA_PadingPara));
96     ctx->pad.type = RSAES_OAEP;
97     return;
98 }
99 
SetOaepLabel(CRYPT_RSA_Ctx * ctx,const void * val,uint32_t len)100 static int32_t SetOaepLabel(CRYPT_RSA_Ctx *ctx, const void *val, uint32_t len)
101 {
102     uint8_t *data = NULL;
103     // val can be NULL
104     if ((val == NULL && len != 0) || (len == 0 && val != NULL)) {
105         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
106         return CRYPT_NULL_INPUT;
107     }
108     if (len == 0 && val == NULL) {
109         BSL_SAL_FREE(ctx->label.data);
110         ctx->label.data = NULL;
111         ctx->label.len = 0;
112         return CRYPT_SUCCESS;
113     }
114     data = (uint8_t *)BSL_SAL_Malloc(len);
115     if (data == NULL) {
116         BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL);
117         return CRYPT_MEM_ALLOC_FAIL;
118     }
119     BSL_SAL_FREE(ctx->label.data);
120     ctx->label.data = data;
121     ctx->label.len = len;
122     (void)memcpy_s(ctx->label.data, ctx->label.len, val, len);
123     return CRYPT_SUCCESS;
124 }
125 #endif
126 
127 #if defined(HITLS_CRYPTO_RSAES_PKCSV15) || defined(HITLS_CRYPTO_RSAES_PKCSV15_TLS)
SetRsaesPkcsV15(CRYPT_RSA_Ctx * ctx,const void * val,uint32_t len)128 static int32_t SetRsaesPkcsV15(CRYPT_RSA_Ctx *ctx, const void *val, uint32_t len)
129 {
130     if (val == NULL) {
131         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
132         return CRYPT_NULL_INPUT;
133     }
134     if (len != sizeof(int32_t)) {
135         BSL_ERR_PUSH_ERROR(CRYPT_RSA_SET_EMS_PKCSV15_LEN_ERROR);
136         return CRYPT_RSA_SET_EMS_PKCSV15_LEN_ERROR;
137     }
138 
139     (void)memset_s(&(ctx->pad), sizeof(RSAPad), 0, sizeof(RSAPad));
140     ctx->pad.para.pkcsv15.mdId = *(const int32_t *)val;
141     ctx->pad.type = RSAES_PKCSV15;
142     return CRYPT_SUCCESS;
143 }
144 #endif
145 
146 #ifdef HITLS_CRYPTO_RSAES_PKCSV15_TLS
SetRsaesPkcsV15Tls(CRYPT_RSA_Ctx * ctx,const void * val,uint32_t len)147 static int32_t SetRsaesPkcsV15Tls(CRYPT_RSA_Ctx *ctx, const void *val, uint32_t len)
148 {
149     int32_t ret = SetRsaesPkcsV15(ctx, val, len);
150     if (ret != CRYPT_SUCCESS) {
151         return ret;
152     }
153     ctx->pad.type = RSAES_PKCSV15_TLS;
154     return CRYPT_SUCCESS;
155 }
156 #endif
157 
158 #ifdef HITLS_CRYPTO_RSA_EMSA_PSS
SetSalt(CRYPT_RSA_Ctx * ctx,void * val,uint32_t len)159 static int32_t SetSalt(CRYPT_RSA_Ctx *ctx, void *val, uint32_t len)
160 {
161     if (val == NULL || len == 0) {
162         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
163         return CRYPT_NULL_INPUT;
164     }
165     if (ctx->prvKey == NULL) {
166         BSL_ERR_PUSH_ERROR(CRYPT_RSA_NO_KEY_INFO);
167         return CRYPT_RSA_NO_KEY_INFO;
168     }
169     if (ctx->pad.type != EMSA_PSS) {
170         // In non-PSS mode, salt information cannot be set.
171         BSL_ERR_PUSH_ERROR(CRYPT_RSA_SET_SALT_NOT_PSS_ERROR);
172         return CRYPT_RSA_SET_SALT_NOT_PSS_ERROR;
173     }
174     RSA_PadingPara *pad = &(ctx->pad.para.pss);
175     if (pad->mdMeth == NULL) {
176         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
177         return CRYPT_NULL_INPUT;
178     }
179     uint32_t bytes = BN_BITS_TO_BYTES(CRYPT_RSA_GetBits(ctx));
180     // The maximum salt length is padLen - mdMethod->GetDigestSize - 2
181     if (len > bytes - pad->mdMeth->mdSize - 2) {
182         // The configured salt length does not meet the specification.
183         BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_SALT_LEN);
184         return CRYPT_RSA_ERR_SALT_LEN;
185     }
186     ctx->pad.salt.data = val;
187     ctx->pad.salt.len = len;
188     return CRYPT_SUCCESS;
189 }
190 
GetSaltLen(CRYPT_RSA_Ctx * ctx,void * val,uint32_t len)191 static int32_t GetSaltLen(CRYPT_RSA_Ctx *ctx, void *val, uint32_t len)
192 {
193     if (val == NULL || len == 0) {
194         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
195         return CRYPT_NULL_INPUT;
196     }
197     if (len != sizeof(int32_t)) {
198         BSL_ERR_PUSH_ERROR(CRYPT_RSA_GET_SALT_LEN_ERROR);
199         return CRYPT_RSA_GET_SALT_LEN_ERROR;
200     }
201     if (ctx->prvKey == NULL && ctx->pubKey == NULL) {
202         BSL_ERR_PUSH_ERROR(CRYPT_RSA_NO_KEY_INFO);
203         return CRYPT_RSA_NO_KEY_INFO;
204     }
205     if (ctx->pad.type != EMSA_PSS) {
206         BSL_ERR_PUSH_ERROR(CRYPT_RSA_GET_SALT_NOT_PSS_ERROR);
207         return CRYPT_RSA_GET_SALT_NOT_PSS_ERROR;
208     }
209     int32_t *ret = val;
210     int32_t valTmp;
211     RSA_PadingPara *pad = &(ctx->pad.para.pss);
212     if (pad->mdMeth == NULL) {
213         BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_PSS_PARAMS);
214         return CRYPT_RSA_ERR_PSS_PARAMS;
215     }
216     uint32_t bytes = BN_BITS_TO_BYTES(CRYPT_RSA_GetBits(ctx));
217     if (pad->saltLen == CRYPT_RSA_SALTLEN_TYPE_HASHLEN) { // saltLen is -1
218         valTmp = (int32_t)pad->mdMeth->mdSize;
219     } else if (pad->saltLen == CRYPT_RSA_SALTLEN_TYPE_MAXLEN ||
220         pad->saltLen == CRYPT_RSA_SALTLEN_TYPE_AUTOLEN) {
221         // RFC 8017: Max(salt length) = ceil(bits/8) - mdSize - 2
222         valTmp = (int32_t)(bytes - pad->mdMeth->mdSize - 2);
223     } else {
224         valTmp = (int32_t)pad->saltLen;
225     }
226     if (valTmp < 0) {
227         BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_SALT_LEN);
228         return CRYPT_RSA_ERR_SALT_LEN;
229     }
230     *ret = valTmp;
231     return CRYPT_SUCCESS;
232 }
233 #endif
234 
RSAGetKeyLen(CRYPT_RSA_Ctx * ctx)235 static uint32_t RSAGetKeyLen(CRYPT_RSA_Ctx *ctx)
236 {
237     return BN_BITS_TO_BYTES(CRYPT_RSA_GetBits(ctx));
238 }
239 
GetPadding(CRYPT_RSA_Ctx * ctx,void * val,uint32_t len)240 static int32_t GetPadding(CRYPT_RSA_Ctx *ctx, void *val, uint32_t len)
241 {
242     RSA_PadType *valTmp = val;
243     if (val == NULL) {
244         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
245         return CRYPT_NULL_INPUT;
246     }
247     if (len != sizeof(int32_t)) {
248         BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG);
249         return CRYPT_INVALID_ARG;
250     }
251     *valTmp = ctx->pad.type;
252     return CRYPT_SUCCESS;
253 }
254 
GetMd(CRYPT_RSA_Ctx * ctx,void * val,uint32_t len)255 static int32_t GetMd(CRYPT_RSA_Ctx *ctx, void *val, uint32_t len)
256 {
257     CRYPT_MD_AlgId *valTmp = val;
258     if (val == NULL) {
259         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
260         return CRYPT_NULL_INPUT;
261     }
262     if (len != sizeof(int32_t)) {
263         BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG);
264         return CRYPT_INVALID_ARG;
265     }
266     if (ctx->pad.type == EMSA_PKCSV15) {
267         *valTmp = ctx->pad.para.pkcsv15.mdId;
268         return CRYPT_SUCCESS;
269     }
270     *valTmp = ctx->pad.para.pss.mdId;
271 
272     return CRYPT_SUCCESS;
273 }
274 
GetMgf(CRYPT_RSA_Ctx * ctx,void * val,uint32_t len)275 static int32_t GetMgf(CRYPT_RSA_Ctx *ctx, void *val, uint32_t len)
276 {
277     CRYPT_MD_AlgId *valTmp = val;
278     if (val == NULL) {
279         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
280         return CRYPT_NULL_INPUT;
281     }
282     if (len != sizeof(int32_t)) {
283         BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG);
284         return CRYPT_INVALID_ARG;
285     }
286     if (ctx->pad.type == EMSA_PKCSV15) {
287         BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_ALGID);
288         return CRYPT_RSA_ERR_ALGID;
289     }
290     *valTmp = ctx->pad.para.pss.mgfId;
291     return CRYPT_SUCCESS;
292 }
293 
SetFlag(CRYPT_RSA_Ctx * ctx,const void * val,uint32_t len)294 static int32_t SetFlag(CRYPT_RSA_Ctx *ctx, const void *val, uint32_t len)
295 {
296     if (val == NULL) {
297         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
298         return CRYPT_NULL_INPUT;
299     }
300     if (len != sizeof(uint32_t)) {
301         BSL_ERR_PUSH_ERROR(CRYPT_RSA_SET_FLAG_LEN_ERROR);
302         return CRYPT_RSA_SET_FLAG_LEN_ERROR;
303     }
304     uint32_t flag = *(const uint32_t *)val;
305     if (flag == 0 || flag >= CRYPT_RSA_MAXFLAG) {
306         BSL_ERR_PUSH_ERROR(CRYPT_RSA_FLAG_NOT_SUPPORT_ERROR);
307         return CRYPT_RSA_FLAG_NOT_SUPPORT_ERROR;
308     }
309     ctx->flags |= flag;
310     return CRYPT_SUCCESS;
311 }
312 
ClearFlag(CRYPT_RSA_Ctx * ctx,const void * val,uint32_t len)313 static int32_t ClearFlag(CRYPT_RSA_Ctx *ctx, const void *val, uint32_t len)
314 {
315     if (val == NULL) {
316         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
317         return CRYPT_NULL_INPUT;
318     }
319     if (len != sizeof(uint32_t)) {
320         BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG);
321         return CRYPT_INVALID_ARG;
322     }
323     uint32_t flag = *(const uint32_t *)val;
324 
325     if (flag == 0 || flag >= CRYPT_RSA_MAXFLAG) {
326         BSL_ERR_PUSH_ERROR(CRYPT_RSA_FLAG_NOT_SUPPORT_ERROR);
327         return CRYPT_RSA_FLAG_NOT_SUPPORT_ERROR;
328     }
329     ctx->flags &= ~flag;
330     return CRYPT_SUCCESS;
331 }
332 
RsaUpReferences(CRYPT_RSA_Ctx * ctx,void * val,uint32_t len)333 static int32_t RsaUpReferences(CRYPT_RSA_Ctx *ctx, void *val, uint32_t len)
334 {
335     if (val != NULL && len == (uint32_t)sizeof(int)) {
336         return BSL_SAL_AtomicUpReferences(&(ctx->references), (int *)val);
337     }
338     BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
339     return CRYPT_NULL_INPUT;
340 }
341 
SetRsaPad(CRYPT_RSA_Ctx * ctx,const void * val,uint32_t len)342 static int32_t SetRsaPad(CRYPT_RSA_Ctx *ctx, const void *val, uint32_t len)
343 {
344     if (val == NULL) {
345         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
346         return CRYPT_NULL_INPUT;
347     }
348     if (len != sizeof(int32_t)) {
349         BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG);
350         return CRYPT_INVALID_ARG;
351     }
352 
353     int32_t pad = *(const int32_t *)val;
354     if (pad < EMSA_PKCSV15 || pad > RSA_NO_PAD) {
355         BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG);
356         return CRYPT_INVALID_ARG;
357     }
358 
359     ctx->pad.type = pad;
360     return CRYPT_SUCCESS;
361 }
362 
363 #if defined(HITLS_CRYPTO_RSAES_OAEP) || defined(HITLS_CRYPTO_RSA_EMSA_PSS)
MdIdCheckSha1Sha2(CRYPT_MD_AlgId id)364 static int32_t MdIdCheckSha1Sha2(CRYPT_MD_AlgId id)
365 {
366     if (id < CRYPT_MD_MD5 || id > CRYPT_MD_SHA512) {
367         BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_ALGID);
368         return CRYPT_EAL_ERR_ALGID;
369     }
370     return CRYPT_SUCCESS;
371 }
372 #endif
373 
374 #ifdef HITLS_CRYPTO_RSAES_OAEP
375 
RsaSetOaep(CRYPT_RSA_Ctx * ctx,BSL_Param * param)376 static int32_t RsaSetOaep(CRYPT_RSA_Ctx *ctx, BSL_Param *param)
377 {
378     int32_t ret;
379     uint32_t len = 0;
380     RSA_PadingPara padPara = {0};
381     const BSL_Param *temp = NULL;
382     if (param == NULL) {
383         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
384         return CRYPT_NULL_INPUT;
385     }
386     if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_RSA_MD_ID)) != NULL) {
387         len = sizeof(padPara.mdId);
388         GOTO_ERR_IF(BSL_PARAM_GetValue(temp, CRYPT_PARAM_RSA_MD_ID,
389             BSL_PARAM_TYPE_INT32, &padPara.mdId, &len), ret);
390     }
391     if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_RSA_MGF1_ID)) != NULL) {
392         len = sizeof(padPara.mgfId);
393         GOTO_ERR_IF(BSL_PARAM_GetValue(temp, CRYPT_PARAM_RSA_MGF1_ID,
394             BSL_PARAM_TYPE_INT32, &padPara.mgfId, &len), ret);
395     }
396     ret = MdIdCheckSha1Sha2(padPara.mdId);
397     if (ret != CRYPT_SUCCESS) {
398         BSL_ERR_PUSH_ERROR(ret);
399         return ret;
400     }
401     ret = MdIdCheckSha1Sha2(padPara.mgfId);
402     if (ret != CRYPT_SUCCESS) {
403         BSL_ERR_PUSH_ERROR(ret);
404         return ret;
405     }
406     padPara.mdMeth = EAL_MdFindMethod(padPara.mdId);
407     padPara.mgfMeth = EAL_MdFindMethod(padPara.mgfId);
408     if (padPara.mdMeth == NULL || padPara.mgfMeth == NULL) {
409         BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_ALGID);
410         return CRYPT_EAL_ERR_ALGID;
411     }
412     SetOaep(ctx, &padPara);
413 ERR:
414     return ret;
415 }
416 #endif
417 
418 #ifdef HITLS_CRYPTO_RSA_EMSA_PSS
419 
RsaSetPss(CRYPT_RSA_Ctx * ctx,BSL_Param * param)420 static int32_t RsaSetPss(CRYPT_RSA_Ctx *ctx, BSL_Param *param)
421 {
422     int32_t ret;
423     uint32_t len = 0;
424     RSA_PadingPara padPara = {0};
425     const BSL_Param *temp = NULL;
426     if (param == NULL) {
427         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
428         return CRYPT_NULL_INPUT;
429     }
430     if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_RSA_MD_ID)) != NULL) {
431         len = sizeof(padPara.mdId);
432         GOTO_ERR_IF(BSL_PARAM_GetValue(temp, CRYPT_PARAM_RSA_MD_ID, BSL_PARAM_TYPE_INT32, &padPara.mdId, &len), ret);
433     }
434     if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_RSA_MGF1_ID)) != NULL) {
435         len = sizeof(padPara.mgfId);
436         GOTO_ERR_IF(BSL_PARAM_GetValue(temp, CRYPT_PARAM_RSA_MGF1_ID, BSL_PARAM_TYPE_INT32, &padPara.mgfId, &len), ret);
437     }
438     if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_RSA_SALTLEN)) != NULL) {
439         len = sizeof(padPara.saltLen);
440         GOTO_ERR_IF(BSL_PARAM_GetValue(temp, CRYPT_PARAM_RSA_SALTLEN,
441             BSL_PARAM_TYPE_INT32, &padPara.saltLen, &len), ret);
442     }
443     ret = MdIdCheckSha1Sha2(padPara.mdId);
444     if (ret != CRYPT_SUCCESS) {
445         BSL_ERR_PUSH_ERROR(ret);
446         return ret;
447     }
448     ret = MdIdCheckSha1Sha2(padPara.mgfId);
449     if (ret != CRYPT_SUCCESS) {
450         BSL_ERR_PUSH_ERROR(ret);
451         return ret;
452     }
453     padPara.mdMeth = EAL_MdFindMethod(padPara.mdId);
454     padPara.mgfMeth = EAL_MdFindMethod(padPara.mgfId);
455     if (padPara.mdMeth == NULL || padPara.mgfMeth == NULL) {
456         BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_ALGID);
457         return CRYPT_EAL_ERR_ALGID;
458     }
459     ret = SetEmsaPss(ctx, &padPara);
460     if (ret != CRYPT_SUCCESS) {
461         BSL_ERR_PUSH_ERROR(ret);
462     }
463 ERR:
464     return ret;
465 }
466 #endif
467 
RsaCommonCtrl(CRYPT_RSA_Ctx * ctx,int32_t opt,void * val,uint32_t len)468 static int32_t RsaCommonCtrl(CRYPT_RSA_Ctx *ctx, int32_t opt, void *val, uint32_t len)
469 {
470     switch (opt) {
471         case CRYPT_CTRL_UP_REFERENCES:
472             return RsaUpReferences(ctx, val, len);
473         case CRYPT_CTRL_GET_BITS:
474             return GetUintCtrl(ctx, val, len, (GetUintCallBack)CRYPT_RSA_GetBits);
475         case CRYPT_CTRL_GET_SECBITS:
476             return GetUintCtrl(ctx, val, len, (GetUintCallBack)CRYPT_RSA_GetSecBits);
477         case CRYPT_CTRL_SET_RSA_FLAG:
478             return SetFlag(ctx, val, len);
479         case CRYPT_CTRL_CLR_RSA_FLAG:
480             return ClearFlag(ctx, val, len);
481         case CRYPT_CTRL_GET_PUBKEY_LEN:
482         case CRYPT_CTRL_GET_PRVKEY_LEN:
483             return GetUintCtrl(ctx, val, len, (GetUintCallBack)RSAGetKeyLen);
484         default:
485             BSL_ERR_PUSH_ERROR(CRYPT_RSA_CTRL_NOT_SUPPORT_ERROR);
486             return CRYPT_RSA_CTRL_NOT_SUPPORT_ERROR;
487     }
488 }
489 
490 #ifdef HITLS_CRYPTO_RSA_BSSA
SetBssaParamCheck(CRYPT_RSA_Ctx * ctx,const void * val,uint32_t len)491 static int32_t SetBssaParamCheck(CRYPT_RSA_Ctx *ctx, const void *val, uint32_t len)
492 {
493     if (val == NULL || len == 0) {
494         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
495         return CRYPT_NULL_INPUT;
496     }
497     if (ctx->pubKey == NULL) {
498         BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_NO_PUBKEY_INFO);
499         return CRYPT_RSA_ERR_NO_PUBKEY_INFO;
500     }
501     return CRYPT_SUCCESS;
502 }
503 
RsaSetBssa(CRYPT_RSA_Ctx * ctx,const void * val,uint32_t len)504 static int32_t RsaSetBssa(CRYPT_RSA_Ctx *ctx, const void *val, uint32_t len)
505 {
506     int32_t ret = SetBssaParamCheck(ctx, val, len);
507     if (ret != CRYPT_SUCCESS) {
508         return ret;
509     }
510     const uint8_t *r = (const uint8_t *)val;
511     BN_Optimizer *opt = BN_OptimizerCreate();
512     if (opt == NULL) {
513         BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL);
514         return CRYPT_MEM_ALLOC_FAIL;
515     }
516     RSA_Blind *blind = NULL;
517     RSA_BlindParam *param = ctx->blindParam;
518     if (param == NULL) {
519         param = BSL_SAL_Calloc(1u, sizeof(RSA_BlindParam));
520         if (param == NULL) {
521             ret = CRYPT_MEM_ALLOC_FAIL;
522             BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL);
523             goto ERR;
524         }
525         param->type = RSABSSA;
526     }
527     if (param->para.bssa != NULL) {
528         RSA_BlindFreeCtx(param->para.bssa);
529         param->para.bssa = NULL;
530     }
531     param->para.bssa = RSA_BlindNewCtx();
532     if (param->para.bssa == NULL) {
533         ret = CRYPT_MEM_ALLOC_FAIL;
534         goto ERR;
535     }
536     blind = param->para.bssa;
537     GOTO_ERR_IF(RSA_CreateBlind(blind, 0), ret);
538     GOTO_ERR_IF(BN_Bin2Bn(blind->r, r, len), ret);
539     if (BN_IsZero(blind->r) || (BN_Cmp(blind->r, ctx->pubKey->n) >= 0)) { // 1 <= r < n
540         ret = CRYPT_RSA_ERR_BSSA_PARAM;
541         BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_BSSA_PARAM);
542         goto ERR;
543     }
544     GOTO_ERR_IF(BN_ModInv(blind->rInv, blind->r, ctx->pubKey->n, opt), ret);
545     GOTO_ERR_IF(BN_ModExp(blind->r, blind->r, ctx->pubKey->e, ctx->pubKey->n, opt), ret);
546     ctx->blindParam = param;
547 ERR:
548     if (ret != CRYPT_SUCCESS && ctx->blindParam == NULL && param != NULL) {
549         RSA_BlindFreeCtx(param->para.bssa);
550         BSL_SAL_FREE(param);
551     }
552     BN_OptimizerDestroy(opt);
553     return ret;
554 }
555 
556 #endif
557 
CRYPT_RSA_Ctrl(CRYPT_RSA_Ctx * ctx,int32_t opt,void * val,uint32_t len)558 int32_t CRYPT_RSA_Ctrl(CRYPT_RSA_Ctx *ctx, int32_t opt, void *val, uint32_t len)
559 {
560     if (ctx == NULL) {
561         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
562         return CRYPT_NULL_INPUT;
563     }
564     switch (opt) {
565 #ifdef HITLS_CRYPTO_RSA_EMSA_PKCSV15
566         case CRYPT_CTRL_SET_RSA_EMSA_PKCSV15:
567             return SetEmsaPkcsV15(ctx, val, len);
568 #endif
569 #ifdef HITLS_CRYPTO_RSA_EMSA_PSS
570         case CRYPT_CTRL_SET_RSA_EMSA_PSS:
571             return RsaSetPss(ctx, val);
572         case CRYPT_CTRL_SET_RSA_SALT:
573             return SetSalt(ctx, val, len);
574         case CRYPT_CTRL_GET_RSA_SALTLEN:
575             return GetSaltLen(ctx, val, len);
576 #endif
577         case CRYPT_CTRL_GET_RSA_PADDING:
578             return GetPadding(ctx, val, len);
579         case CRYPT_CTRL_GET_RSA_MD:
580             return GetMd(ctx, val, len);
581         case CRYPT_CTRL_GET_RSA_MGF:
582             return GetMgf(ctx, val, len);
583 #ifdef HITLS_CRYPTO_RSAES_OAEP
584         case CRYPT_CTRL_SET_RSA_RSAES_OAEP:
585             return RsaSetOaep(ctx, val);
586         case CRYPT_CTRL_SET_RSA_OAEP_LABEL:
587             return SetOaepLabel(ctx, val, len);
588 #endif
589 #ifdef HITLS_CRYPTO_RSAES_PKCSV15
590         case CRYPT_CTRL_SET_RSA_RSAES_PKCSV15:
591             return SetRsaesPkcsV15(ctx, val, len);
592 #endif
593 #ifdef HITLS_CRYPTO_RSAES_PKCSV15_TLS
594         case CRYPT_CTRL_SET_RSA_RSAES_PKCSV15_TLS:
595             return SetRsaesPkcsV15Tls(ctx, val, len);
596 #endif
597 #ifdef HITLS_CRYPTO_RSA_NO_PAD
598         case CRYPT_CTRL_SET_NO_PADDING:
599             ctx->pad.type = RSA_NO_PAD;
600             return CRYPT_SUCCESS;
601 #endif
602         case CRYPT_CTRL_SET_RSA_PADDING:
603             return SetRsaPad(ctx, val, len);
604 #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_VERIFY)
605         case CRYPT_CTRL_GET_SIGNLEN:
606             return GetUintCtrl(ctx, val, len, (GetUintCallBack)CRYPT_RSA_GetSignLen);
607 #endif
608 #ifdef HITLS_CRYPTO_RSA_BSSA
609         case CRYPT_CTRL_SET_RSA_BSSA_FACTOR_R:
610             return RsaSetBssa(ctx, val, len);
611 #endif
612         default:
613             return RsaCommonCtrl(ctx, opt, val, len);
614     }
615 }
616 #endif /* HITLS_CRYPTO_RSA */