API参考
载入中...
搜索中...
未找到
sm9_curve.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 __HEADER_SM9_CURVE_H__
17#define __HEADER_SM9_CURVE_H__
18
19#include "hitls_build.h"
20#ifdef HITLS_CRYPTO_SM9
21
22#include <stdint.h>
23#include "sm9_bn.h"
24
25#define SM9_BITLEN 256
26#define SM9_BYTELEN 32
27#define SM9_WORDLEN 8
28
29/*=======================SM3 Adaptation Layer for SM9========================*/
30
31#define CRYPT_SM3_DIGESTSIZE 32
32#define CRYPT_SM3_BLOCKSIZE 64
33#define SM9_Hash_Size 32
34
39typedef struct {
40 uint32_t h[CRYPT_SM3_DIGESTSIZE / sizeof(uint32_t)];
41 uint32_t hNum, lNum;
42 uint8_t block[CRYPT_SM3_BLOCKSIZE];
43 uint32_t num;
44} SM9_CRYPT_SM3_Ctx;
45
51typedef struct {
52 SM9_CRYPT_SM3_Ctx sm3State;
53} SM9_Hash_Ctx;
54
55// Function declarations are in sm9.h
56
57/*============================================================================*/
58
59// Coef_0 + Coef_1 * u and u ^ 2 = -2
60typedef struct _SM9_FP2 {
61 uint32_t Coef_0[BNWordLen];
62 uint32_t Coef_1[BNWordLen];
63}SM9_Fp2;
64
65// Coef_0 + Coef_1 * v, v ^ 2 = u
66typedef struct _SM9_FP4 {
67 SM9_Fp2 Coef_0;
68 SM9_Fp2 Coef_1;
69}SM9_Fp4;
70
71// Coef_0 +Coef_1 * w + Coef_2 * w ^ 2 and w^3 = v
72typedef struct _SM9_FP12 {
73 SM9_Fp4 Coef_0;
74 SM9_Fp4 Coef_1;
75 SM9_Fp4 Coef_2;
76}SM9_Fp12;
77
78// struct of affine coordinate
79typedef struct _SM9_FP_ECP_A {
80 uint32_t X[BNWordLen];
81 uint32_t Y[BNWordLen];
82}SM9_ECP_A;
83
84// struct of projective coordinate
85typedef struct _SM9_FP_ECP_J {
86 uint32_t X[BNWordLen];
87 uint32_t Y[BNWordLen];
88 uint32_t Z[BNWordLen];
89}SM9_ECP_J;
90
91typedef struct _SM9_FP2_ECP_A {
92 SM9_Fp2 X;
93 SM9_Fp2 Y;
94}SM9_ECP2_A; // struct of affine coordinate
95
96typedef struct _SM9_FP2_ECP_J {
97 SM9_Fp2 X;
98 SM9_Fp2 Y;
99 SM9_Fp2 Z;
100}SM9_ECP2_J; // struct of projective coordinate
101
102typedef struct _SM9_SYS_PARA {
103 int32_t wsize;
104
105 uint32_t EC_T[BNWordLen];
106 uint32_t EC_6T2[BNWordLen]; // 6*t+2
107 uint32_t EC_Trace[BNWordLen];
108
109 uint32_t EC_Q[BNWordLen];
110 uint32_t Q_Mc;
111 uint32_t Q_R1[BNWordLen];
112 uint32_t Q_R2[BNWordLen];
113
114 uint32_t EC_N[BNWordLen];
115 uint32_t N_Mc;
116 uint32_t N_R1[BNWordLen]; // R mod n
117 uint32_t N_R2[BNWordLen]; // RR mod n
118
119 uint32_t EC_Fp_A_Mont[BNWordLen]; // y^2 = x^3 + a*x + b mod q
120 uint32_t EC_Fp_B_Mont[BNWordLen]; // y^2 = x^3 + a*x + b mod q
121 SM9_ECP_A EC_Fp_G_Mont;
122
123 SM9_Fp2 EC_Fp2_A_Mont; // y^2 = x^3 + a*x + b mod q^2
124 SM9_Fp2 EC_Fp2_B_Mont; // y^2 = x^3 + a*x + b mod q^2
125 SM9_ECP2_A EC_Fp2_G_Mont;
126
127 uint32_t EC_Vq_Mont[BNWordLen];
128 uint32_t EC_Wq_Mont[BNWordLen];
129 uint32_t EC_W2q_Mont[BNWordLen];
130 uint32_t EC_Root_Mont[BNWordLen];
131
132 uint32_t EC_One[BNWordLen]; // One
133 uint32_t EC_4_Inv_Mont[BNWordLen];
134}SM9_Sys_Para;
135
136typedef struct _sm9_alg_context_st {
137 uint32_t buf[300][BNWordLen];
138 SM9_Hash_Ctx mac_ctx;
139
140 struct {
141 uint8_t k1[2 * BNByteLen];
142 uint8_t *k2;
143 uint8_t cnt[4];
144 SM9_Hash_Ctx xor_ctx;
145 SM9_Hash_Ctx tmp_ctx;
146 uint32_t bytes;
147 } enc;
148} SM9_CTX;
149
150extern SM9_Sys_Para sm9_sys_para;
151
152extern uint8_t g_SM9_G1[64];
153extern uint8_t g_SM9_G2[128];
154
155#ifdef __cplusplus
156extern "C" {
157#endif
158
159#ifdef __cplusplus
160} /* end extern "C" */
161#endif
162
163#endif // HITLS_CRYPTO_SM9
164
165#endif /* __HEADER_SM9_CURVE_H__ */
166