API参考
载入中...
搜索中...
未找到
ecc_utils.h
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
27extern "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 */
40inline 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
54inline static int32_t CheckBnValid(const BN_BigNum *k, uint32_t maxBits)
55{
56 if (BN_Bits(k) > maxBits) { // If K is greater than maxBits, it is considered too long.
57 return CRYPT_ECC_POINT_MUL_ERR_K_LEN;
58 }
59 return CRYPT_SUCCESS;
60}
61
62#ifdef __cplusplus
63}
64#endif
65
66#endif // HITLS_CRYPTO_ECC
67
68#endif // ECC_UTILS_H
#define CRYPT_SUCCESS
Return success
定义 crypt_errno.h:33