1 /*******************************************************************************
2 * Copyright 2003-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 //
43 //  Purpose:
44 //     Cryptography Primitive.
45 //     EC over Prime Finite Field (setup/retrieve domain parameters)
46 //
47 //  Contents:
48 //        ippsECCPGet()
49 //
50 */
51 
52 #include "owndefs.h"
53 #include "owncp.h"
54 #include "pcpeccp.h"
55 
56 /*F*
57 //    Name: ippsECCPGet
58 //
59 // Purpose: Retrieve ECC Domain Parameter.
60 //
61 // Returns:                Reason:
62 //    ippStsNullPtrErr        NULL == pPrime
63 //                            NULL == pA
64 //                            NULL == pB
65 //                            NULL == pGX
66 //                            NULL == pGY
67 //                            NULL == pOrder
68 //                            NULL == cofactor
69 //                            NULL == pEC
70 //
71 //    ippStsContextMatchErr   illegal pPrime->idCtx
72 //                            illegal pA->idCtx
73 //                            illegal pB->idCtx
74 //                            illegal pGX->idCtx
75 //                            illegal pGY->idCtx
76 //                            illegal pOrder->idCtx
77 //                            illegal pEC->idCtx
78 //
79 //    ippStsRangeErr          not enough room for:
80 //                            pPrime
81 //                            pA, pB,
82 //                            pGX,pGY
83 //                            pOrder
84 //
85 //    ippStsNoErr             no errors
86 //
87 // Parameters:
88 //    pPrime   pointer to the retrieval prime (specify FG(p))
89 //    pA       pointer to the retrieval A coefficient of EC equation
90 //    pB       pointer to the retrieval B coefficient of EC equation
91 //    pGX,pGY  pointer to the retrieval Base Point (x and y coordinates) of EC
92 //    pOrder   pointer to the retrieval Base Point order
93 //    cofactor pointer to the retrieval cofactor value
94 //    pEC     pointer to the ECC context
95 //
96 *F*/
97 IPPFUN(IppStatus, ippsECCPGet, (IppsBigNumState* pPrime,
98                                 IppsBigNumState* pA, IppsBigNumState* pB,
99                                 IppsBigNumState* pGX,IppsBigNumState* pGY,
100                                 IppsBigNumState* pOrder, int* cofactor,
101                                 IppsECCPState* pEC))
102 {
103    IppsGFpState*  pGF;
104    gsModEngine* pGFE;
105 
106    /* test pEC */
107    IPP_BAD_PTR1_RET(pEC);
108    /* use aligned EC context */
109    pEC = (IppsGFpECState*)( IPP_ALIGNED_PTR(pEC, ECGFP_ALIGNMENT) );
110    IPP_BADARG_RET(!ECP_TEST_ID(pEC), ippStsContextMatchErr);
111 
112    pGF = ECP_GFP(pEC);
113    pGFE = GFP_PMA(pGF);
114 
115    /* test pPrime */
116    IPP_BAD_PTR1_RET(pPrime);
117    pPrime = (IppsBigNumState*)( IPP_ALIGNED_PTR(pPrime, ALIGN_VAL) );
118    IPP_BADARG_RET(!BN_VALID_ID(pPrime), ippStsContextMatchErr);
119    IPP_BADARG_RET(BN_ROOM(pPrime)<GFP_FELEN(pGFE), ippStsRangeErr);
120 
121    /* test pA and pB */
122    IPP_BAD_PTR2_RET(pA,pB);
123    pA = (IppsBigNumState*)( IPP_ALIGNED_PTR(pA, ALIGN_VAL) );
124    pB = (IppsBigNumState*)( IPP_ALIGNED_PTR(pB, ALIGN_VAL) );
125    IPP_BADARG_RET(!BN_VALID_ID(pA), ippStsContextMatchErr);
126    IPP_BADARG_RET(!BN_VALID_ID(pB), ippStsContextMatchErr);
127    IPP_BADARG_RET(BN_ROOM(pA)<GFP_FELEN(pGFE), ippStsRangeErr);
128    IPP_BADARG_RET(BN_ROOM(pB)<GFP_FELEN(pGFE), ippStsRangeErr);
129 
130    /* test pG and pGorder pointers */
131    IPP_BAD_PTR3_RET(pGX,pGY, pOrder);
132    pGX   = (IppsBigNumState*)( IPP_ALIGNED_PTR(pGX,   ALIGN_VAL) );
133    pGY   = (IppsBigNumState*)( IPP_ALIGNED_PTR(pGY,   ALIGN_VAL) );
134    pOrder= (IppsBigNumState*)( IPP_ALIGNED_PTR(pOrder,ALIGN_VAL) );
135    IPP_BADARG_RET(!BN_VALID_ID(pGX),    ippStsContextMatchErr);
136    IPP_BADARG_RET(!BN_VALID_ID(pGY),    ippStsContextMatchErr);
137    IPP_BADARG_RET(!BN_VALID_ID(pOrder), ippStsContextMatchErr);
138    IPP_BADARG_RET(BN_ROOM(pGX)<GFP_FELEN(pGFE),    ippStsRangeErr);
139    IPP_BADARG_RET(BN_ROOM(pGY)<GFP_FELEN(pGFE),    ippStsRangeErr);
140    IPP_BADARG_RET((BN_ROOM(pOrder)*BITSIZE(BNU_CHUNK_T)<ECP_ORDBITSIZE(pEC)), ippStsRangeErr);
141 
142    /* test cofactor */
143    IPP_BAD_PTR1_RET(cofactor);
144 
145    {
146       mod_decode  decode = GFP_METHOD(pGFE)->decode;  /* gf decode method  */
147       BNU_CHUNK_T* tmp = cpGFpGetPool(1, pGFE);
148 
149       /* retrieve EC parameter */
150       ippsSet_BN(ippBigNumPOS, GFP_FELEN32(pGFE), (Ipp32u*)GFP_MODULUS(pGFE), pPrime);
151 
152       decode(tmp, ECP_A(pEC), pGFE);
153       ippsSet_BN(ippBigNumPOS, GFP_FELEN32(pGFE), (Ipp32u*)tmp, pA);
154       decode(tmp, ECP_B(pEC), pGFE);
155       ippsSet_BN(ippBigNumPOS, GFP_FELEN32(pGFE), (Ipp32u*)tmp, pB);
156 
157       decode(tmp, ECP_G(pEC), pGFE);
158       ippsSet_BN(ippBigNumPOS, GFP_FELEN32(pGFE), (Ipp32u*)tmp, pGX);
159       decode(tmp, ECP_G(pEC)+GFP_FELEN(pGFE), pGFE);
160       ippsSet_BN(ippBigNumPOS, GFP_FELEN32(pGFE), (Ipp32u*)tmp, pGY);
161 
162       {
163          gsModEngine* pR = ECP_MONT_R(pEC);
164          ippsSet_BN(ippBigNumPOS, MOD_LEN(pR)*sizeof(BNU_CHUNK_T)/sizeof(Ipp32u), (Ipp32u*)MOD_MODULUS(pR), pOrder);
165       }
166 
167       *cofactor = (int)ECP_COFACTOR(pEC)[0];
168 
169       cpGFpReleasePool(1, pGFE);
170       return ippStsNoErr;
171    }
172 }
173