1 /*******************************************************************************
2 * Copyright 2010-2018 Intel Corporation
3 * All Rights Reserved.
4 *
5 * If this  software was obtained  under the  Intel Simplified  Software License,
6 * the following terms apply:
7 *
8 * The source code,  information  and material  ("Material") contained  herein is
9 * owned by Intel Corporation or its  suppliers or licensors,  and  title to such
10 * Material remains with Intel  Corporation or its  suppliers or  licensors.  The
11 * Material  contains  proprietary  information  of  Intel or  its suppliers  and
12 * licensors.  The Material is protected by  worldwide copyright  laws and treaty
13 * provisions.  No part  of  the  Material   may  be  used,  copied,  reproduced,
14 * modified, published,  uploaded, posted, transmitted,  distributed or disclosed
15 * in any way without Intel's prior express written permission.  No license under
16 * any patent,  copyright or other  intellectual property rights  in the Material
17 * is granted to  or  conferred  upon  you,  either   expressly,  by implication,
18 * inducement,  estoppel  or  otherwise.  Any  license   under such  intellectual
19 * property rights must be express and approved by Intel in writing.
20 *
21 * Unless otherwise agreed by Intel in writing,  you may not remove or alter this
22 * notice or  any  other  notice   embedded  in  Materials  by  Intel  or Intel's
23 * suppliers or licensors in any way.
24 *
25 *
26 * If this  software  was obtained  under the  Apache License,  Version  2.0 (the
27 * "License"), the following terms apply:
28 *
29 * You may  not use this  file except  in compliance  with  the License.  You may
30 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
31 *
32 *
33 * Unless  required  by   applicable  law  or  agreed  to  in  writing,  software
34 * distributed under the License  is distributed  on an  "AS IS"  BASIS,  WITHOUT
35 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 *
37 * See the   License  for the   specific  language   governing   permissions  and
38 * limitations under the License.
39 *******************************************************************************/
40 
41 /*
42 //     Intel(R) Integrated Performance Primitives. Cryptography Primitives.
43 //     EC over GF(p^m) definitinons
44 //
45 //     Context:
46 //        ippsGFpECVerify()
47 //
48 */
49 
50 #include "owndefs.h"
51 #include "owncp.h"
52 #include "pcpgfpecstuff.h"
53 #include "pcpeccp.h"
54 
55 //tbcd: temporary excluded: #include <assert.h>
56 /*F*
57 // Name: ippsGFpECVerify
58 //
59 // Purpose: Verifies the parameters of an elliptic curve.
60 //
61 // Returns:                   Reason:
62 //    ippStsNullPtrErr          pEC == NULL
63 //                              pResult == NULL
64 //                              pScratchBuffer == NULL
65 //    ippStsContextMatchErr     invalid pEC->idCtx
66 //    ippStsNoErr               no error
67 //
68 // Parameters:
69 //    pResult         Pointer to the verification result
70 //    pEC             Pointer to the context of the elliptic curve
71 //    pScratchBuffer  Pointer to the scratch buffer
72 //
73 *F*/
74 
75 IPPFUN(IppStatus, ippsGFpECVerify,(IppECResult* pResult, IppsGFpECState* pEC, Ipp8u* pScratchBuffer))
76 {
77    IPP_BAD_PTR3_RET(pEC, pResult, pScratchBuffer);
78    pEC = (IppsGFpECState*)( IPP_ALIGNED_PTR(pEC, ECGFP_ALIGNMENT) );
79    IPP_BADARG_RET( !ECP_TEST_ID(pEC), ippStsContextMatchErr );
80 
81    *pResult = ippECValid;
82 
83    {
84       IppsGFpState* pGF = ECP_GFP(pEC);
85       gsModEngine* pGFE = GFP_PMA(pGF);
86       int elemLen = GFP_FELEN(pGFE);
87 
88       mod_mul mulF = GFP_METHOD(pGFE)->mul;
89       mod_sqr sqrF = GFP_METHOD(pGFE)->sqr;
90       mod_add addF = GFP_METHOD(pGFE)->add;
91 
92       /*
93       // check discriminant ( 4*A^3 + 27*B^2 != 0 mod P)
94       */
95       if(ippECValid == *pResult) {
96          BNU_CHUNK_T* pT = cpGFpGetPool(1, pGFE);
97          BNU_CHUNK_T* pU = cpGFpGetPool(1, pGFE);
98          //tbcd: temporary excluded: assert(NULL!=pT && NULL!=pU);
99 
100          if(ECP_SPECIFIC(pEC)==ECP_EPID2)
101             cpGFpElementPadd(pT, elemLen, 0);         /* T = 4*A^3 = 0 */
102          else {
103             addF(pT, ECP_A(pEC), ECP_A(pEC), pGFE);   /* T = 4*A^3 */
104             sqrF(pT, pT, pGFE);
105             mulF(pT, ECP_A(pEC), pT, pGFE);
106          }
107 
108          addF(pU, ECP_B(pEC), ECP_B(pEC), pGFE);      /* U = 9*B^2 */
109          addF(pU, pU, ECP_B(pEC), pGFE);
110          sqrF(pU, pU, pGFE);
111 
112          addF(pT, pU, pT, pGFE);                      /* T += 3*U */
113          addF(pT, pU, pT, pGFE);
114          addF(pT, pU, pT, pGFE);
115 
116          *pResult = GFP_IS_ZERO(pT, elemLen)? ippECIsZeroDiscriminant: ippECValid;
117 
118          cpGFpReleasePool(2, pGFE);
119       }
120 
121       if(ECP_SUBGROUP(pEC)) {
122          /*
123          // check base point and it order
124          */
125          if(ippECValid == *pResult) {
126             IppsGFpECPoint G;
127             cpEcGFpInitPoint(&G, ECP_G(pEC), ECP_AFFINE_POINT|ECP_FINITE_POINT, pEC);
128 
129             /* check G != infinity */
130             *pResult = gfec_IsPointAtInfinity(&G)? ippECPointIsAtInfinite : ippECValid;
131 
132             /* check G lies on EC */
133             if(ippECValid == *pResult)
134                *pResult = gfec_IsPointOnCurve(&G, pEC)? ippECValid : ippECPointIsNotValid;
135 
136             /* check Gorder*G = infinity */
137             if(ippECValid == *pResult) {
138                IppsGFpECPoint T;
139                cpEcGFpInitPoint(&T, cpEcGFpGetPool(1, pEC),0, pEC);
140 
141                gfec_MulBasePoint(&T, MOD_MODULUS(ECP_MONT_R(pEC)), BITS_BNU_CHUNK(ECP_ORDBITSIZE(pEC)), pEC, pScratchBuffer);
142 
143                *pResult = gfec_IsPointAtInfinity(&T)? ippECValid : ippECInvalidOrder;
144 
145                cpEcGFpReleasePool(1, pEC);
146             }
147          }
148 
149          /*
150          // check order==P
151          */
152          if(ippECValid == *pResult) {
153             BNU_CHUNK_T* pPrime = GFP_MODULUS(pGFE);
154             int primeLen = GFP_FELEN(pGFE);
155 
156             gsModEngine* pR = ECP_MONT_R(pEC);
157             BNU_CHUNK_T* pOrder = MOD_MODULUS(pR);
158             int orderLen = MOD_LEN(pR);
159 
160             *pResult = (primeLen==orderLen && GFP_EQ(pPrime, pOrder, primeLen))? ippECIsWeakSSSA : ippECValid;
161          }
162       }
163 
164       return ippStsNoErr;
165    }
166 }
167