1 /*******************************************************************************
2 * Copyright 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 //     Operations over GF(p).
44 //
45 //     Context:
46 //        ippsGFpMultiExp()
47 //
48 */
49 #include "owndefs.h"
50 #include "owncp.h"
51 
52 #include "pcpgfpstuff.h"
53 #include "pcpgfpxstuff.h"
54 #include "pcptool.h"
55 
56 //tbcd: temporary excluded: #include <assert.h>
57 
58 /*F*
59 // Name: ippsGFpMultiExp
60 //
61 // Purpose: Multiplies exponents of GF elements
62 //
63 // Returns:                   Reason:
64 //    ippStsNullPtrErr              NULL == pGFp
65 //                                  NULL == ppElmA
66 //                                  NULL == pR
67 //                                  NULL == ppE
68 //
69 //    ippStsContextMatchErr         invalid pGFp->idCtx
70 //                                  invalid ppElmA[i]->idCtx
71 //                                  invalid pR->idCtx
72 //                                  invalid ppE[i]->idCtx
73 //
74 //    ippStsOutOfRangeErr           GFPE_ROOM() != GFP_FELEN()
75 //
76 //    ippStsBadArgErr               1>nItems
77 //                                  nItems>6
78 //
79 //    ippStsNoErr                   no error
80 //
81 // Parameters:
82 //    ppElmA          Pointer to the array of contexts of the finite field elements representing the base of the exponentiation.
83 //    ppE             Pointer to the array of the Big Number contexts storing the exponents.
84 //    nItems          Number of exponents.
85 //    pR              Pointer to the context of the resulting element of the finite field.
86 //    pGFp            Pointer to the context of the finite field.
87 //    pScratchBuffer  Pointer to the scratch buffer.
88 //
89 *F*/
90 
91 IPPFUN(IppStatus, ippsGFpMultiExp,(const IppsGFpElement* const ppElmA[], const IppsBigNumState* const ppE[], int nItems,
92                                     IppsGFpElement* pR, IppsGFpState* pGFp,
93                                     Ipp8u* pScratchBuffer))
94 {
95    IPP_BAD_PTR2_RET(ppElmA, ppE);
96 
97    if(nItems==1)
98       return ippsGFpExp(ppElmA[0], ppE[0], pR, pGFp, pScratchBuffer);
99 
100    else {
101       /* test number of exponents */
102       IPP_BADARG_RET(1>nItems || nItems>IPP_MAX_EXPONENT_NUM, ippStsBadArgErr);
103 
104       IPP_BAD_PTR2_RET(pR, pGFp);
105 
106       pGFp = (IppsGFpState*)( IPP_ALIGNED_PTR(pGFp, GFP_ALIGNMENT) );
107       IPP_BADARG_RET( !GFP_TEST_ID(pGFp), ippStsContextMatchErr );
108       IPP_BADARG_RET( !GFPE_TEST_ID(pR), ippStsContextMatchErr );
109       {
110          int n;
111 
112          gsModEngine* pGFE = GFP_PMA(pGFp);
113          IPP_BADARG_RET( GFPE_ROOM(pR)!=GFP_FELEN(pGFE), ippStsOutOfRangeErr);
114 
115          /* test all ppElmA[] and ppE[] pairs */
116          for(n=0; n<nItems; n++) {
117             const IppsGFpElement* pElmA = ppElmA[n];
118             const IppsBigNumState* pE = ppE[n];
119             IPP_BAD_PTR2_RET(pElmA, pE);
120 
121             IPP_BADARG_RET( !GFPE_TEST_ID(pElmA), ippStsContextMatchErr );
122             pE = (IppsBigNumState*)( IPP_ALIGNED_PTR(pE, BN_ALIGNMENT) );
123             IPP_BADARG_RET( !BN_VALID_ID(pE), ippStsContextMatchErr );
124 
125             IPP_BADARG_RET( (GFPE_ROOM(pElmA)!=GFP_FELEN(pGFE)) || (GFPE_ROOM(pR)!=GFP_FELEN(pGFE)), ippStsOutOfRangeErr);
126          }
127 
128          if(NULL==pScratchBuffer) {
129             mod_mul mulF = GFP_METHOD(pGFE)->mul;
130 
131             BNU_CHUNK_T* pTmpR = cpGFpGetPool(1, pGFE);
132             //tbcd: temporary excluded: assert(NULL!=pTmpR);
133 
134             cpGFpxExp(GFPE_DATA(pR), GFPE_DATA(ppElmA[0]), BN_NUMBER(ppE[0]), BN_SIZE(ppE[0]), pGFE, 0);
135             for(n=1; n<nItems; n++) {
136                cpGFpxExp(pTmpR, GFPE_DATA(ppElmA[n]), BN_NUMBER(ppE[n]), BN_SIZE(ppE[n]), pGFE, 0);
137                mulF(GFPE_DATA(pR), GFPE_DATA(pR), pTmpR, pGFE);
138             }
139 
140             cpGFpReleasePool(1, pGFE);
141          }
142 
143          else {
144             const BNU_CHUNK_T* ppAdata[IPP_MAX_EXPONENT_NUM];
145             const BNU_CHUNK_T* ppEdata[IPP_MAX_EXPONENT_NUM];
146             int nsEdataLen[IPP_MAX_EXPONENT_NUM];
147             for(n=0; n<nItems; n++) {
148                ppAdata[n] = GFPE_DATA(ppElmA[n]);
149                ppEdata[n] = BN_NUMBER(ppE[n]);
150                nsEdataLen[n] = BN_SIZE(ppE[n]);
151             }
152             cpGFpxMultiExp(GFPE_DATA(pR), ppAdata, ppEdata, nsEdataLen, nItems, pGFE, pScratchBuffer);
153          }
154 
155          return ippStsNoErr;
156       }
157    }
158 }
159