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_BN
18
19 #include <stdbool.h>
20 #include "securec.h"
21 #include "bsl_sal.h"
22 #include "bsl_err_internal.h"
23 #include "crypt_errno.h"
24 #include "bn_basic.h"
25 #include "bn_bincal.h"
26 #include "bn_optimizer.h"
27
BN_Lcm(BN_BigNum * r,const BN_BigNum * a,const BN_BigNum * b,BN_Optimizer * opt)28 int32_t BN_Lcm(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b, BN_Optimizer *opt)
29 {
30 if (r == NULL || a == NULL || b == NULL || opt == NULL) {
31 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
32 return CRYPT_NULL_INPUT;
33 }
34 BN_BigNum *gcd = BN_Create(BN_Bits(r));
35 if (gcd == NULL) {
36 BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL);
37 return CRYPT_MEM_ALLOC_FAIL;
38 }
39 int32_t ret = BN_Gcd(gcd, a, b, opt);
40 if (ret != CRYPT_SUCCESS) {
41 BSL_ERR_PUSH_ERROR(ret);
42 BN_Destroy(gcd);
43 return ret;
44 }
45 if(BN_IsOne(gcd) == false) {
46 ret = BN_Div(r, NULL, a, gcd, opt);
47 if (ret != CRYPT_SUCCESS) {
48 BSL_ERR_PUSH_ERROR(ret);
49 BN_Destroy(gcd);
50 return ret;
51 }
52 }
53 ret = BN_Mul(r, r, b, opt);
54 BN_Destroy(gcd);
55 if (ret != CRYPT_SUCCESS) {
56 BSL_ERR_PUSH_ERROR(ret);
57 }
58 return ret;
59 }
60 #endif /* HITLS_CRYPTO_BN */