Debian Codegraph @

sources/ippcp/gfpec/ecnist/ifma_ec_dsaverify_p384.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_p384.h"
26
27 /* clang-format off */
28 IPP_OWN_DEFN(IppECResult, gfec_VerifyDSA_nistp384_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 P384_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 h = h1 = h2 = setzero_i64();
88
89 /* h = (signS)^(-1) */
90 h = n_to_mont(signS);
91 h = n_inv(h);
92
93 /* h1 = msg*h, h2 = signR*h */
94 h1 = n_to_mont(msg);
95 h2 = n_to_mont(signR);
96
97 h1 = n_mul(h1, h);
98 h2 = n_mul(h2, h);
99
100 h1 = n_from_mont(h1);
101 h2 = n_from_mont(h2);
102
103 BNU_CHUNK_T* pExtendedH1 = cpGFpGetPool(2, pME);
104 BNU_CHUNK_T* pExtendedH2 = cpGFpGetPool(2, pME);
105 BNU_CHUNK_T* pH1 = cpGFpGetPool(1, pME);
106 BNU_CHUNK_T* pH2 = cpGFpGetPool(1, pME);
107
108 from_radix52((Ipp64u*)pH1, h1);
109 from_radix52((Ipp64u*)pH2, h2);
110 cpGFpElementCopyPad(pExtendedH1, orderLen + 1, pH1, orderLen);
111 cpGFpElementCopyPad(pExtendedH2, orderLen + 1, pH2, orderLen);
112
113 cpGFpReleasePool(2, pME); /* pH1, pH2 */
114
115 __ALIGN64 P384_POINT_IFMA P;
116 P.x = P.y = P.z = setzero_i64();
117
118 /* P = h1*basePoint + h2*pubKey */
119 ifma_ec_nistp384_mul_point(&pubKey, &pubKey, (Ipp8u*)pExtendedH2, orderBits);
120
121 if (ECP_PREMULBP(pEC)) {
122 ifma_ec_nistp384_mul_pointbase(&P, (Ipp8u*)pExtendedH1, orderBits);
123 } else {
124 /* Convert base point to a new Montgomery domain */
125 __ALIGN64 P384_POINT_IFMA G52;
126 recode_point_to_mont52(&G52, ECP_G(pEC), pPool /* 3 elem */, pmeth, pME);
127
128 ifma_ec_nistp384_mul_point(&P, &G52, (Ipp8u*)pExtendedH1, orderBits);
129 }
130
131 ifma_ec_nistp384_add_point(&P, &P, &pubKey);
132
133 /* Get X in affine coordinates */
134 ifma_ec_nistp384_get_affine_coords(&(P.x), NULL, &P);
135
136 P.x = p_from_mont(P.x);
137 P.x = n_red(P.x);
138
139 const mask8 mask_ok = cmp_i64_mask(P.x, signR, _MM_CMPINT_EQ);
140 if ((mask8)0xFF == mask_ok)
141 verifyResult = ippECValid;
142
143 cpGFpReleasePool(7, pME);
144
145 return verifyResult;
146 }
147
148 #endif // (_IPP32E >= _IPP32E_K1)