API参考
载入中...
搜索中...
未找到
ecc_local.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_LOCAL_H
17#define ECC_LOCAL_H
18
19#include "hitls_build.h"
20#ifdef HITLS_CRYPTO_ECC
21
22#include "crypt_ecc.h"
23#include "crypt_bn.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29#define ECC_MAX_BIT_LEN 521
30
31#define PRE_COMPUTE_WINDOW 5 // Default Window Size
32#define PRE_COMPUTE_MAX_TABLELEN (1 << 5) // Maximum specifications of the pre-calculation table
33// The default ECP window length is 5 bits and only odd points are calculated.
34#define WINDOW_TABLE_SIZE (PRE_COMPUTE_MAX_TABLELEN >> 1)
35
36// Layout format of the pre-computation table.
37// This macro is used to convert values into corresponding offsets.
38// layout rules (1, 3, 5, 7... 15, -1, -3, ... -15)
39#define NUMTOOFFSET(num) (((num) < 0) ? (WINDOW_TABLE_SIZE / 2 - 1 - (((num) - 1) / 2)) : (((num) - 1) / 2))
40
44typedef struct {
45 // Calculate r = k1 * G + k2 * pt
46 int32_t (*pointMulAdd)(ECC_Para *para, ECC_Point *r, const BN_BigNum *k1, const BN_BigNum *k2, const ECC_Point *pt);
47 // Calculate r = k * pt. If pt is null, calculate r = k * G. This is the ConstTime processing function.
48 int32_t (*pointMul)(ECC_Para *para, ECC_Point *r, const BN_BigNum *k, const ECC_Point *pt);
49 // Calculate r = k * pt. If pt is null, calculate r = k * G
50 int32_t (*pointMulFast)(ECC_Para *para, ECC_Point *r, const BN_BigNum *k, const ECC_Point *pt);
51 // point addition r = a + b, a all can be the jacobi coordinate, b must be an affine point
52 int32_t (*pointAddAffine)(const ECC_Para *para, ECC_Point *r, const ECC_Point *a, const ECC_Point *b);
53 // point addition r = a + b, a, b all can be the jacobi coordinate.
54 int32_t (*pointAdd)(const ECC_Para *para, ECC_Point *r, const ECC_Point *a, const ECC_Point *b);
55 // point double r = a + a, a can be the jacobi coordinate.
56 int32_t (*pointDouble)(const ECC_Para *para, ECC_Point *r, const ECC_Point *a);
57 // point Multi-double Calculate r = (2^m)*a, a can be the jacobi coordinate.
58 int32_t (*pointMultDouble)(const ECC_Para *para, ECC_Point *r, const ECC_Point *a, uint32_t m);
59 // Module inverse
60 int32_t (*modInv)(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *p, BN_Optimizer *opt);
61 // Convert points to affine coordinates based on the given module inverse information.
62 int32_t (*point2AffineWithInv)(const ECC_Para *para, ECC_Point *r, const ECC_Point *a, const BN_BigNum *inv);
63 // Convert the point information to affine coordinates.
64 int32_t (*point2Affine)(const ECC_Para *para, ECC_Point *r, const ECC_Point *a);
65 // Calculate r = (a*b) % mod
66 int32_t (*bnModNistEccMul)(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b,
67 void *mod, BN_Optimizer *opt);
68 // Calculate r = (a^2) % mod
69 int32_t (*bnModNistEccSqr)(BN_BigNum *r, const BN_BigNum *a, void *mod, BN_Optimizer *opt);
70 // Inverse mode order.
71 int32_t (*modOrdInv)(const ECC_Para *para, BN_BigNum *r, const BN_BigNum *a);
72 // convert date to Montgomery form
73 int32_t (*bnMontEnc)(BN_BigNum *r, BN_Mont *mont, BN_Optimizer *opt, bool consttime);
74 // convert Montgomery form to common form
75 void (*bnMontDec)(BN_BigNum *r, BN_Mont *mont);
76} ECC_Method;
77
81struct EccPointInfo {
82 BN_BigNum x;
83 BN_BigNum y;
84 BN_BigNum z;
86};
87
91struct EccPara {
92 BN_BigNum *p;
93 BN_BigNum *a;
94 BN_BigNum *b;
95 BN_BigNum *n;
96 BN_BigNum *h;
97 BN_BigNum *x;
98 BN_BigNum *y;
99 // Currently, the 5-bit window is used. Only odd multiple points are calculated.
100 // The total number of pre-calculated data is (2 ^ 5)/2, that is 16 points.
101 ECC_Point *tableG[16];
102 const ECC_Method *method;
104 BN_Mont *montP;
105 void *libCtx;
106};
107
118int32_t ECP_PointAtInfinity(const ECC_Para *para, const ECC_Point *pt);
119
131int32_t ECP_PointOnCurve(const ECC_Para *para, const ECC_Point *pt);
132
133
145int32_t ECP_Point2Affine(const ECC_Para *para, ECC_Point *r, const ECC_Point *pt);
146
160int32_t ECP_PointInvertAtAffine(const ECC_Para *para, ECC_Point *r, const ECC_Point *a);
161
177int32_t ECP_Point2AffineWithInv(const ECC_Para *para, ECC_Point *r, const ECC_Point *pt, const BN_BigNum *inv);
178
192int32_t ECP_PointMulAdd(ECC_Para *para, ECC_Point *r, const BN_BigNum *k1, const BN_BigNum *k2, const ECC_Point *pt);
193
205int32_t ECP_PointCopy(const ECC_Para *para, ECC_Point *a, const ECC_Point *b);
206
220int32_t ECP_PointMul(ECC_Para *para, ECC_Point *r, const BN_BigNum *k, const ECC_Point *pt);
221
236int32_t ECP_PointMulFast(ECC_Para *para, ECC_Point *r, const BN_BigNum *k, const ECC_Point *pt);
237
247BN_BigNum *ECP_HalfPGet(const BN_BigNum *p);
248
258const ECC_Method *ECC_FindMethod(CRYPT_PKEY_ParaId id);
259
271int32_t ECP_NistPointDouble(const ECC_Para *para, ECC_Point *r, const ECC_Point *a);
272
285int32_t ECP_NistPointMultDouble(const ECC_Para *para, ECC_Point *r, const ECC_Point *a, uint32_t m);
286
299int32_t ECP_NistPointAddAffine(const ECC_Para *para, ECC_Point *r, const ECC_Point *a, const ECC_Point *b);
300
313int32_t ECP_NistPointAdd(const ECC_Para *para, ECC_Point *r, const ECC_Point *a, const ECC_Point *b);
314
327int32_t ECP_GetEncodeDataLen(const ECC_Para *para, ECC_Point *pt, CRYPT_PKEY_PointFormat format, uint32_t *dataLen);
328
339int32_t ECP_ModOrderInv(const ECC_Para *para, BN_BigNum *r, const BN_BigNum *a);
340
341#ifdef HITLS_CRYPTO_CURVE_MONT
342
347int32_t ECP_NistPointDoubleMont(const ECC_Para *para, ECC_Point *r, const ECC_Point *a);
348
353int32_t ECP_NistPointMultDoubleMont(const ECC_Para *para, ECC_Point *r, const ECC_Point *a, uint32_t m);
354
359int32_t ECP_NistPointAddAffineMont(const ECC_Para *para, ECC_Point *r, const ECC_Point *a, const ECC_Point *b);
360
365int32_t ECP_NistPointAddMont(const ECC_Para *para, ECC_Point *r, const ECC_Point *a, const ECC_Point *b);
366
371int32_t ECP_Point2AffineMont(const ECC_Para *para, ECC_Point *r, const ECC_Point *pt);
372
377int32_t ECP_PrimePointDoubleMont(const ECC_Para *para, ECC_Point *r, const ECC_Point *a);
378
383int32_t ECP_PrimePointMultDoubleMont(const ECC_Para *para, ECC_Point *r, const ECC_Point *a, uint32_t m);
384
389int32_t ECP_PrimePointAddAffineMont(const ECC_Para *para, ECC_Point *r, const ECC_Point *a, const ECC_Point *b);
390
395int32_t ECP_PrimePointAddMont(const ECC_Para *para, ECC_Point *r, const ECC_Point *a, const ECC_Point *b);
396
401int32_t ECP_PointMulMont(ECC_Para *para, ECC_Point *r, const BN_BigNum *k, const ECC_Point *pt);
402
403#endif // HITLS_CRYPTO_CURVE_MONT
404
405#ifdef __cplusplus
406}
407#endif
408
409#endif // HITLS_CRYPTO_ECC
410
411#endif // ECC_LOCAL_H
CRYPT_PKEY_ParaId
定义 crypt_algid.h:208
CRYPT_PKEY_PointFormat
定义 crypt_algid.h:370