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 #include "hitls_build.h"
16 #ifdef HITLS_CRYPTO_BN
17
18 #include <stdint.h>
19 #include "crypt_errno.h"
20 #include "bn_bincal.h"
21 #include "bn_asm.h"
22 #if defined(HITLS_CRYPTO_BN_X8664) && defined(__x86_64__)
23 #include "crypt_utils.h"
24 #endif
25
MontSqrBin(BN_UINT * r,BN_Mont * mont,BN_Optimizer * opt,bool consttime)26 int32_t MontSqrBin(BN_UINT *r, BN_Mont *mont, BN_Optimizer *opt, bool consttime)
27 {
28 if (mont->mSize > 1) {
29 #if defined(HITLS_CRYPTO_BN_X8664) && defined(__x86_64__)
30 if (IsSupportBMI2() && IsSupportADX()) {
31 MontMulx_Asm(r, r, r, mont->mod, mont->k0, mont->mSize);
32 return CRYPT_SUCCESS;
33 }
34 #endif
35 MontMul_Asm(r, r, r, mont->mod, mont->k0, mont->mSize);
36 return CRYPT_SUCCESS;
37 }
38 return MontSqrBinCore(r, mont, opt, consttime);
39 }
40
MontMulBin(BN_UINT * r,const BN_UINT * a,const BN_UINT * b,BN_Mont * mont,BN_Optimizer * opt,bool consttime)41 int32_t MontMulBin(BN_UINT *r, const BN_UINT *a, const BN_UINT *b, BN_Mont *mont,
42 BN_Optimizer *opt, bool consttime)
43 {
44 if (mont->mSize > 1) {
45 #if defined(HITLS_CRYPTO_BN_X8664) && defined(__x86_64__)
46 if (IsSupportBMI2() && IsSupportADX()) {
47 MontMulx_Asm(r, a, b, mont->mod, mont->k0, mont->mSize);
48 return CRYPT_SUCCESS;
49 }
50 #endif
51 MontMul_Asm(r, a, b, mont->mod, mont->k0, mont->mSize);
52 return CRYPT_SUCCESS;
53 }
54 return MontMulBinCore(r, a, b, mont, opt, consttime);
55 }
56
MontEncBin(BN_UINT * r,BN_Mont * mont,BN_Optimizer * opt,bool consttime)57 int32_t MontEncBin(BN_UINT *r, BN_Mont *mont, BN_Optimizer *opt, bool consttime)
58 {
59 if (mont->mSize > 1) {
60 #if defined(HITLS_CRYPTO_BN_X8664) && defined(__x86_64__)
61 if (IsSupportBMI2() && IsSupportADX()) {
62 MontMulx_Asm(r, r, mont->montRR, mont->mod, mont->k0, mont->mSize);
63 return CRYPT_SUCCESS;
64 }
65 #endif
66 MontMul_Asm(r, r, mont->montRR, mont->mod, mont->k0, mont->mSize);
67 return CRYPT_SUCCESS;
68 }
69 return MontEncBinCore(r, mont, opt, consttime);
70 }
71
Reduce(BN_UINT * r,BN_UINT * x,const BN_UINT * one,const BN_UINT * m,uint32_t mSize,BN_UINT m0)72 void Reduce(BN_UINT *r, BN_UINT *x, const BN_UINT *one, const BN_UINT *m, uint32_t mSize, BN_UINT m0)
73 {
74 if (mSize <= 1) {
75 ReduceCore(r, x, m, mSize, m0);
76 return;
77 }
78 #if defined(HITLS_CRYPTO_BN_X8664) && defined(__x86_64__)
79 if (IsSupportBMI2() && IsSupportADX()) {
80 MontMulx_Asm(r, x, one, m, m0, mSize);
81 return;
82 }
83 #endif
84 MontMul_Asm(r, x, one, m, m0, mSize);
85 return;
86 }
87 #endif /* HITLS_CRYPTO_BN */
88