• 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 #ifndef ECC_UTILS_H
17 #define ECC_UTILS_H
18 
19 #include "hitls_build.h"
20 #ifdef HITLS_CRYPTO_ECC
21 
22 #include "crypt_ecc.h"
23 #include "ecc_local.h"
24 #include "crypt_errno.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /* the window length of common point multiplication */
31 #define WINDOW_SIZE 5
32 
33 /*
34  * Decoded from a 6-bit signed code to obtain the sign and value. The upper five bits are the complement of the value,
35  * and the least significant bit is the carry (positive) of the next group of numbers.
36  * Output:
37  *      sign = 0 or 1
38  *      0 <= value <= 16
39  */
DecodeScalarCode(uint32_t * sign,uint32_t * value,uint32_t code)40 inline static void DecodeScalarCode(uint32_t *sign, uint32_t *value, uint32_t code)
41 {
42     uint32_t s, v;
43     s = 0 - (code >> WINDOW_SIZE);  // Bit 5 is the sign bit, and the negative number is all 1s.
44     // Take its value and add a carry. Because the symbol is obtained and then the carry is added, v may be + 16 or - 0.
45     v = (code >> 1) + (code & 1);
46     // Find the Take its value and add a carry. If the number is positive, v is the Take its value and add a carry.
47     // If the number is negative, v is inverted + 1.
48     v = (~s & v) | (s & (~v + 1));
49 
50     *sign = s & 1;
51     *value = v & ((1 << WINDOW_SIZE) - 1);  // Five bits are intercepted.
52 }
53 
CheckParaValid(const ECC_Para * para,CRYPT_PKEY_ParaId id)54 inline static int32_t CheckParaValid(const ECC_Para *para, CRYPT_PKEY_ParaId id)
55 {
56     if (para == NULL) {
57         return CRYPT_NULL_INPUT;
58     }
59     if (para->id != id) {
60         return CRYPT_ECC_POINT_ERR_CURVE_ID;
61     }
62     return CRYPT_SUCCESS;
63 }
64 
CheckPointValid(const ECC_Point * pt,CRYPT_PKEY_ParaId id)65 inline static int32_t CheckPointValid(const ECC_Point *pt, CRYPT_PKEY_ParaId id)
66 {
67     if (pt == NULL) {
68         return CRYPT_NULL_INPUT;
69     }
70     if (pt->id != id) {
71         return CRYPT_ECC_POINT_ERR_CURVE_ID;
72     }
73     return CRYPT_SUCCESS;
74 }
75 
CheckBnValid(const BN_BigNum * k,uint32_t maxBits)76 inline static int32_t CheckBnValid(const BN_BigNum *k, uint32_t maxBits)
77 {
78     if (k == NULL) {
79         return CRYPT_NULL_INPUT;
80     }
81     if (BN_Bits(k) > maxBits) {  // If K is greater than maxBits, it is considered too long.
82         return CRYPT_ECC_POINT_MUL_ERR_K_LEN;
83     }
84     return CRYPT_SUCCESS;
85 }
86 
87 #ifdef __cplusplus
88 }
89 #endif
90 
91 #endif // HITLS_CRYPTO_ECC
92 
93 #endif // ECC_UTILS_H
94