Debian Codegraph @

sources/ippcp/gfpec/ecnist/ifma_ec_dsaverify_p256.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/pcpgfpstuff.h"
22 #include "gfpec/pcpgfpecstuff.h"
23
24 #include "gfpec/ecnist/ifma_arith_method.h"
25 #include "gfpec/ecnist/ifma_ecpoint_p256.h"
26
27 /* clang-format off */
28 IPP_OWN_DEFN(IppECResult, gfec_VerifyDSA_nistp256_avx512, (const IppsBigNumState* pMsgDigest,
29 const IppsGFpECPoint* pRegPublic,
30 const IppsBigNumState* pSignR,
31 const IppsBigNumState* pSignS,
32 IppsGFpECState* pEC,
33 Ipp8u* pScratchBuffer))
34 /* clang-format on */
35 {
36 IPP_UNREFERENCED_PARAMETER(pScratchBuffer);
37
38 IppECResult verifyResult = ippECInvalidSignature;
39
40 gsModEngine* pME = GFP_PMA(ECP_GFP(pEC));
41 gsModEngine* nME = ECP_MONT_R(pEC);
42
43 const int orderBits = ECP_ORDBITSIZE(pEC);
44 const int orderLen = BITS_BNU_CHUNK(orderBits);
45
46 ifmaArithMethod* pmeth = (ifmaArithMethod*)GFP_METHOD_ALT(pME);
47 ifmaArithMethod* nmeth = (ifmaArithMethod*)GFP_METHOD_ALT(nME);
48
49 ifma_import to_radix52 = pmeth->import_to52;
50 ifma_export from_radix52 = pmeth->export_to64;
51
52 /* Mod engine (mod p) */
53 ifma_decode p_from_mont = pmeth->decode;
54
55 /* Mod engine (mod n - subgroup order) */
56 ifma_encode n_to_mont = nmeth->encode;
57 ifma_decode n_from_mont = nmeth->decode;
58 ifma_mul n_mul = nmeth->mul;
59 ifma_inv n_inv = nmeth->inv;
60 ifma_red n_red = nmeth->red;
61
62 /* Convert input parameters to 2^52 radix */
63 m512 msg, signR, signS;
64 msg = signR = signS = setzero_i64();
65
66 const int elemLen = GFP_FELEN(pME);
67
68 BNU_CHUNK_T* pPool = cpGFpGetPool(3, pME);
69 BNU_CHUNK_T* pBufMsg = pPool;
70 BNU_CHUNK_T* pBufSignR = pPool + elemLen;
71 BNU_CHUNK_T* pBufSignS = pPool + 2 * elemLen;
72
73 ZEXPAND_COPY_BNU(pBufMsg, orderLen, BN_NUMBER(pMsgDigest), BN_SIZE(pMsgDigest));
74 ZEXPAND_COPY_BNU(pBufSignR, orderLen, BN_NUMBER(pSignR), BN_SIZE(pSignR));
75 ZEXPAND_COPY_BNU(pBufSignS, orderLen, BN_NUMBER(pSignS), BN_SIZE(pSignS));
76
77 msg = to_radix52((Ipp64u*)pBufMsg);
78 msg = n_red(msg); /* reduce just in case */
79 signR = to_radix52((Ipp64u*)pBufSignR);
80 signS = to_radix52((Ipp64u*)pBufSignS);
81
82 /* Convert public point to proper Montgomery domain and 2^52 radix */
83 __ALIGN64 P256_POINT_IFMA pubKey;
84 recode_point_to_mont52(&pubKey, ECP_POINT_DATA(pRegPublic), pPool /* 3 elem */, pmeth, pME);
85
86 m512 h, h1, h2;
87
88 /* h = (signS)^(-1) */
89 h = n_to_mont(signS);
90 h = n_inv(h);
91
92 /* h1 = msg*h, h2 = signR*h */
93 h1 = n_to_mont(msg);
94 h2 = n_to_mont(signR);
95
96 h1 = n_mul(h1, h);
97 h2 = n_mul(h2, h);
98
99 h1 = n_from_mont(h1);
100 h2 = n_from_mont(h2);
101
102 BNU_CHUNK_T* pExtendedH1 = cpGFpGetPool(2, pME);
103 BNU_CHUNK_T* pExtendedH2 = cpGFpGetPool(2, pME);
104 BNU_CHUNK_T* pH1 = cpGFpGetPool(1, pME);
105 BNU_CHUNK_T* pH2 = cpGFpGetPool(1, pME);
106
107 from_radix52((Ipp64u*)pH1, h1);
108 from_radix52((Ipp64u*)pH2, h2);
109 cpGFpElementCopyPad(pExtendedH1, orderLen + 1, pH1, orderLen);
110 cpGFpElementCopyPad(pExtendedH2, orderLen + 1, pH2, orderLen);
111
112 cpGFpReleasePool(2, pME); /* pH1, pH2 */
113
114 __ALIGN64 P256_POINT_IFMA P;
115
116 /* P = h1*basePoint + h2*pubKey */
117 ifma_ec_nistp256_mul_point(&pubKey, &pubKey, (Ipp8u*)pExtendedH2, orderBits);
118
119 if (ECP_PREMULBP(pEC)) {
120 ifma_ec_nistp256_mul_pointbase(&P, (Ipp8u*)pExtendedH1, orderBits);
121 } else {
122 /* Convert base point to a new Montgomery domain */
123 __ALIGN64 P256_POINT_IFMA G52;
124 recode_point_to_mont52(&G52, ECP_G(pEC), pPool /* 3 elem */, pmeth, pME);
125
126 ifma_ec_nistp256_mul_point(&P, &G52, (Ipp8u*)pExtendedH1, orderBits);
127 }
128
129 ifma_ec_nistp256_add_point(&P, &pubKey);
130
131 /* Get X in affine coordinates */
132 ifma_ec_nistp256_get_affine_coords(&(P.x), NULL, &P);
133
134 P.x = p_from_mont(P.x);
135 P.x = n_red(P.x);
136
137 const mask8 mask_ok = cmp_i64_mask(P.x, signR, _MM_CMPINT_EQ);
138 if ((mask8)0xFF == mask_ok)
139 verifyResult = ippECValid;
140
141 cpGFpReleasePool(7, pME);
142
143 return verifyResult;
144 }
145
146 #endif // (_IPP32E >= _IPP32E_K1)