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 #ifndef ASM_ECP_SM2_H 17 #define ASM_ECP_SM2_H 18 19 #include "hitls_build.h" 20 #ifdef HITLS_CRYPTO_CURVE_SM2 21 22 #include <stdint.h> 23 #include "crypt_bn.h" 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 29 #define SM2_BITS 256 30 #define SM2_BITSOFBYTES 8 31 #define SM2_BYTES_NUM 32 32 #define SM2_LIMBS (SM2_BYTES_NUM / sizeof(BN_UINT)) /* = 4 or 8 */ 33 34 typedef struct SM2_point { 35 BN_UINT x[SM2_LIMBS]; 36 BN_UINT y[SM2_LIMBS]; 37 BN_UINT z[SM2_LIMBS]; 38 } SM2_point; 39 40 typedef struct SM2_pointaffine { 41 BN_UINT x[SM2_LIMBS]; 42 BN_UINT y[SM2_LIMBS]; 43 } SM2_AffinePoint; 44 45 /* Right shift: a >> 1 */ 46 void ECP_Sm2BnRshift1(BN_UINT *a); 47 /* Finite field operations */ 48 /* Modular div by 2: r = a/2 mod p */ 49 void ECP_Sm2DivBy2(BN_UINT *r, const BN_UINT *a); 50 /* Modular add: r = a+b mod p */ 51 void ECP_Sm2AddModP(BN_UINT *r, const BN_UINT *a, const BN_UINT *b); 52 /* Modular add: r = a+b mod n, where n = ord(p) */ 53 void ECP_Sm2AddModOrd(BN_UINT *r, const BN_UINT *a, const BN_UINT *b); 54 /* Modular sub: r = a-b mod p */ 55 void ECP_Sm2SubModP(BN_UINT *r, const BN_UINT *a, const BN_UINT *b); 56 /* Modular sub: r = a-b mod n, where n = ord(p) */ 57 void ECP_Sm2SubModOrd(BN_UINT *r, const BN_UINT *a, const BN_UINT *b); 58 /* Modular mul by 3: r = 3*a mod p */ 59 void ECP_Sm2MulBy3(BN_UINT *r, const BN_UINT *a); 60 /* Modular mul: r = a*b mod p */ 61 void ECP_Sm2Mul(BN_UINT *r, const BN_UINT *a, const BN_UINT *b); 62 /* Modular sqr: r = a^2 mod p */ 63 void ECP_Sm2Sqr(BN_UINT *r, const BN_UINT *a); 64 /* sub: r = p - b */ 65 void ECP_Sm2Neg(BN_UINT *r, const BN_UINT *b); 66 67 const BN_UINT *ECP_Sm2Precomputed(void); 68 69 /* Right shift 1: r = a >> 1 */ 70 void ECP_Sm2Div2(BN_UINT *r, BN_UINT *a); 71 /* Right shift 2: r = a >> 2 */ 72 void ECP_Sm2Div4(BN_UINT *r, BN_UINT *a); 73 /* Sub: r = a - b */ 74 void ECP_Sm2BnSub(BN_UINT *r, const BN_UINT *a, const BN_UINT *b); 75 /* Add: r = a + b */ 76 void ECP_Sm2BnAdd(BN_UINT *r, const BN_UINT *a, const BN_UINT *b); 77 78 /* Finite field operations */ 79 80 /* Modular div by 2: r = a/2 mod p */ 81 void ECP_Sm2Div2ModP(BN_UINT *r, const BN_UINT *a); 82 /* Modular div by 2: r = a/2 mod n, where n = ord(p) */ 83 void ECP_Sm2Div2ModOrd(BN_UINT *r, const BN_UINT *a); 84 /* Modular div by 4: r = a/4 mod p */ 85 void ECP_Sm2Div4ModP(BN_UINT *r, BN_UINT *a); 86 /* Modular div by 4: r = a/4 mod n, where n = ord(p) */ 87 void ECP_Sm2Div4ModOrd(BN_UINT *r, const BN_UINT *a); 88 89 /* Convert to Montgomery domain */ 90 void ECP_Sm2ToMont(BN_UINT *r, const BN_UINT *a); 91 /* Convert from Montgomery domain */ 92 void ECP_Sm2FromMont(BN_UINT *r, const BN_UINT *a); 93 94 /* Point double in Montgomery domain: r <- a + a */ 95 void ECP_Sm2PointDoubleMont(SM2_point *r, const SM2_point *a); 96 /* Point add affine in Montgomery domain: R <- a + b */ 97 void ECP_Sm2PointAddAffineMont(SM2_point *r, const SM2_point *a, const SM2_AffinePoint *b); 98 /* Point add in Montgomery domain: r <- a + b */ 99 void ECP_Sm2PointAddMont(SM2_point *r, const SM2_point *a, const SM2_point *b); 100 101 #ifdef __cplusplus 102 } 103 #endif 104 105 #endif 106 #endif