Debian Codegraph @

sources/ippcp/gfpec/sm2/ifma_ec_sign_sm2.c

raw symbols SCIP
1 /*************************************************************************
2 * Copyright (C) 2021 Intel Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *************************************************************************/
16
17 #include "owndefs.h"
18
19 #if (_IPP32E >= _IPP32E_K1)
20
21 #include "gfpec/pcpgfpecstuff.h"
22 #include "gfpec/pcpgfpstuff.h"
23
24 #include "gfpec/sm2/ifma_defs_sm2.h"
25 #include "gfpec/sm2/ifma_ecpoint_sm2.h"
26 #include "gfpec/sm2/ifma_arith_method_sm2.h"
27
28 /* clang-format off */
29 IPP_OWN_DEFN(IppStatus, gfec_Sign_sm2_avx512, (const IppsBigNumState* pMsgDigest,
30 const IppsBigNumState* pRegPrivate,
31 IppsBigNumState* pEphPrivate,
32 IppsBigNumState* pSignR,
33 IppsBigNumState* pSignS,
34 IppsGFpECState* pEC,
35 Ipp8u* pScratchBuffer))
36 /* clang-format on */
37 {
38 IPP_UNREFERENCED_PARAMETER(pScratchBuffer);
39
40 IppStatus sts = ippStsNoErr;
41
42 gsModEngine* pME = GFP_PMA(ECP_GFP(pEC));
43 gsModEngine* nME = ECP_MONT_R(pEC);
44
45 const int orderBits = ECP_ORDBITSIZE(pEC);
46 const int orderLen = BITS_BNU_CHUNK(orderBits);
47
48 ifmaArithMethod* pmeth = (ifmaArithMethod*)GFP_METHOD_ALT(pME);
49 ifmaArithMethod* nmeth = (ifmaArithMethod*)GFP_METHOD_ALT(nME);
50
51 ifma_import to_radix52 = pmeth->import_to52;
52 ifma_export from_radix52 = pmeth->export_to64;
53
54 /* Mod engine (mod p) */
55 ifma_decode p_from_mont = pmeth->decode;
56
57 /* Mod engine (mod n - subgroup order) */
58 ifma_encode n_to_mont = nmeth->encode;
59 ifma_decode n_from_mont = nmeth->decode;
60 ifma_mul n_mul = nmeth->mul;
61 ifma_add n_add = nmeth->add;
62 ifma_add n_sub = nmeth->sub;
63 ifma_inv n_inv = nmeth->inv;
64 ifma_red n_red = nmeth->red;
65
66 fesm2 sign_r, sign_s;
67 sign_r = sign_s = setzero_i64();
68
69 /* compute r-component
70 * 1) (x1,y1) = [s]G
71 * 2) r = (e + x1) mod n
72 */
73 /* 1) (x1,y1) = [s]G */
74 /* copy scalar */
75 BNU_CHUNK_T* pExtendedScalar = cpGFpGetPool(2, pME);
76 cpGFpElementCopyPad(pExtendedScalar,
77 orderLen + 1,
78 BN_NUMBER(pEphPrivate),
79 BN_SIZE(pEphPrivate));
80
81 __ALIGN64 PSM2_POINT_IFMA P;
82
83 if (ECP_PREMULBP(pEC)) {
84 gesm2_mul_base(&P, (Ipp8u*)pExtendedScalar);
85 } else {
86 BNU_CHUNK_T* pPool = cpGFpGetPool(3, pME);
87
88 /* Convert base point to a new Montgomery domain */
89 __ALIGN64 PSM2_POINT_IFMA G52;
90 recode_point_to_mont52(&G52, ECP_G(pEC), pPool, pmeth, pME);
91
92 gesm2_mul(&P, &G52, (Ipp8u*)pExtendedScalar, orderBits);
93
94 cpGFpReleasePool(3, pME);
95 }
96
97 /* extract affine P.x */
98 gesm2_to_affine(/* x = */ &(P.x), /* y = */ NULL, /* a = */ &P);
99 P.x = p_from_mont(P.x);
100 P.x = n_red(P.x);
101
102 /* 2) r = (e + x1) mod n */
103 BNU_CHUNK_T* pTmp = cpGFpGetPool(1, pME);
104 fesm2 msg = setzero_i64();
105 ZEXPAND_COPY_BNU(pTmp, orderLen, BN_NUMBER(pMsgDigest), BN_SIZE(pMsgDigest));
106 msg = to_radix52((Ipp64u*)pTmp);
107 msg = n_red(msg); /* reduce just in case */
108
109 msg = n_to_mont(msg);
110 P.x = n_to_mont(P.x);
111
112 sign_r = n_add(msg, P.x);
113 /* check r == 0 or r + k == n(0) */
114 /* r == 0 */
115 const mask8 sign_r_err_zeros = FESM2_IS_ZERO(sign_r);
116 /* r + k == 0 */
117 /* extract k */
118 fesm2 eph_key = setzero_i64();
119 ZEXPAND_COPY_BNU(pTmp, orderLen, BN_NUMBER(pEphPrivate), BN_SIZE(pEphPrivate));
120 eph_key = to_radix52((Ipp64u*)pTmp);
121 eph_key = n_to_mont(eph_key);
122
123 fesm2 t;
124 t = n_add(eph_key, sign_r);
125
126 const mask8 rpk_err_zeros = FESM2_IS_ZERO(t);
127
128 if (((mask8)0xFF == sign_r_err_zeros) || ((mask8)0xFF == rpk_err_zeros))
129 sts = ippStsEphemeralKeyErr;
130
131 fesm2 reg_key = setzero_i64();
132 ZEXPAND_COPY_BNU(pTmp, orderLen, BN_NUMBER(pRegPrivate), BN_SIZE(pRegPrivate));
133 reg_key = to_radix52((Ipp64u*)pTmp);
134
135 const fesm2 one = FESM2_LOADU(PSM2_ONE52);
136
137 t = n_to_mont(one);
138 reg_key = n_to_mont(reg_key);
139
140 /* sign_s = r * d (result not normalized) */
141 sign_s = n_mul(sign_r, reg_key);
142 /* reg_key = 1 + d (result normalized) */
143 reg_key = n_add(reg_key, t);
144 /* reg_key = (1 + d)^(-1) (result not normalized) */
145 reg_key = n_inv(reg_key);
146 /* sign_s = (k - r * d) (result normalized) */
147 sign_s = n_sub(eph_key, sign_s);
148 /* sign_s = (1 + d)^(-1) * (k - r * d) (result not normalized) */
149 sign_s = n_mul(sign_s, reg_key);
150
151 // result shall be normalized before the final multiplication inside `n_from_mont` function
152 sign_s = ifma_lnorm52(sign_s);
153 sign_s = n_from_mont(sign_s);
154 sign_r = n_from_mont(sign_r);
155
156 const mask8 is_zero = (mask8)(FESM2_IS_ZERO(sign_r) | FESM2_IS_ZERO(sign_s));
157 if ((mask8)0xFF == is_zero)
158 sts = ippStsEphemeralKeyErr;
159
160 // return sign_s
161 from_radix52((Ipp64u*)pTmp, sign_s);
162 BN_SIGN(pSignS) = ippBigNumPOS;
163 BN_SIZE(pSignS) = orderLen;
164 ZEXPAND_COPY_BNU(BN_NUMBER(pSignS), BN_ROOM(pSignS), pTmp, orderLen);
165 // return sign_r
166 from_radix52((Ipp64u*)pTmp, sign_r);
167 BN_SIGN(pSignR) = ippBigNumPOS;
168 BN_SIZE(pSignR) = orderLen;
169 ZEXPAND_COPY_BNU(BN_NUMBER(pSignR), BN_ROOM(pSignR), pTmp, orderLen);
170
171 /* clear secret data */
172 clear_secrets(&eph_key, &(P.x), &t);
173 clear_secrets(&sign_r, &sign_s, &reg_key);
174
175 /* clear buffer */
176 cpGFpReleasePool(3, pME); /* pExtendedScalar(2) + tmp(1) */
177 return sts;
178 }
179
180 #endif // (_IPP32E >= _IPP32E_K1)