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 //
43 //  Purpose:
44 //     Intel(R) Integrated Performance Primitives. Cryptography Primitives.
45 //     EC over GF(p) Operations
46 //
47 //     Context:
48 //        ippsGFpECSetPointHash()
49 //
50 */
51 
52 #include "owndefs.h"
53 #include "owncp.h"
54 #include "pcpgfpecstuff.h"
55 #include "pcphash.h"
56 #include "pcphash_rmf.h"
57 
58 /*F*
59 // Name: ippsGFpECSetPointHash
60 //
61 // Purpose: Constructs a point on an elliptic curve based on the hash of the input message
62 //
63 // Returns:                   Reason:
64 //    ippStsNullPtrErr               pPoint == NULL
65 //                                   pEC == NULL
66 //                                   pScratchBuffer == NULL
67 //                                   (msgLen && !pMsg)
68 //
69 //    ippStsContextMatchErr          invalid pEC->idCtx
70 //                                   invalid pPoint->idCtx
71 //
72 //    ippStsBadArgErr                !GFP_IS_BASIC(pGFE)
73 //
74 //    ippStsOutOfRangeErr            ECP_POINT_FELEN(pPoint)!=GFP_FELEN()
75 //
76 //    ippStsQuadraticNonResidueErr   square of the Y-coordinate of
77 //                                   the pPoint is a quadratic non-residue modulo
78 //
79 //    ippStsLengthErr                msgLen<0
80 //
81 //    ippStsNoErr                    no error
82 //
83 // Parameters:
84 //    hdr              Header of the input message
85 //    pMsg             Pointer to the input message
86 //    msgLen           Length of the input message
87 //    pPoint           Pointer to the IppsGFpECPoint context
88 //    pEC              Pointer to the context of the elliptic curve
89 //    hashID           ID of the hash algorithm used
90 //    pScratchBuffer   Pointer to the scratch buffer
91 //
92 // Note:
93 //    Is not a fact that computed point belongs to BP-related subgroup BP
94 //
95 *F*/
96 
97 IPPFUN(IppStatus, ippsGFpECSetPointHash,(Ipp32u hdr, const Ipp8u* pMsg, int msgLen, IppsGFpECPoint* pPoint,
98                                          IppsGFpECState* pEC, IppHashAlgId hashID,
99                                          Ipp8u* pScratchBuffer))
100 {
101    IppsGFpState*  pGF;
102    gsModEngine* pGFE;
103 
104    /* get algorithm id */
105    hashID = cpValidHashAlg(hashID);
106    IPP_BADARG_RET(ippHashAlg_Unknown==hashID, ippStsNotSupportedModeErr);
107 
108    /* test message length */
109    IPP_BADARG_RET((msgLen<0), ippStsLengthErr);
110    /* test message pointer */
111    IPP_BADARG_RET((msgLen && !pMsg), ippStsNullPtrErr);
112 
113    IPP_BAD_PTR3_RET(pPoint, pEC, pScratchBuffer);
114    pEC = (IppsGFpECState*)( IPP_ALIGNED_PTR(pEC, ECGFP_ALIGNMENT) );
115    IPP_BADARG_RET( !ECP_TEST_ID(pEC), ippStsContextMatchErr );
116 
117    pGF = ECP_GFP(pEC);
118    pGFE = GFP_PMA(pGF);
119 
120    IPP_BADARG_RET( !GFP_IS_BASIC(pGFE), ippStsBadArgErr );
121    IPP_BADARG_RET( !ECP_POINT_TEST_ID(pPoint), ippStsContextMatchErr );
122 
123    IPP_BADARG_RET( ECP_POINT_FELEN(pPoint)!=GFP_FELEN(pGFE), ippStsOutOfRangeErr);
124 
125    {
126       int elemLen = GFP_FELEN(pGFE);
127       BNU_CHUNK_T* pModulus = GFP_MODULUS(pGFE);
128 
129       Ipp8u md[IPP_SHA512_DIGEST_BITSIZE/BYTESIZE];
130       int hashLen = cpHashAlgAttr[hashID].hashSize;
131       BNU_CHUNK_T hashVal[BITS_BNU_CHUNK(IPP_SHA512_DIGEST_BITSIZE)+1];
132       int hashValLen;
133 
134       IppsHashState hashCtx;
135       ippsHashInit(&hashCtx, hashID);
136 
137       {
138          BNU_CHUNK_T* pPoolElm = cpGFpGetPool(1, pGFE);
139 
140          /* convert hdr => hdrStr */
141          BNU_CHUNK_T locHdr = (BNU_CHUNK_T)hdr;
142          Ipp8u hdrOctStr[sizeof(hdr/*locHdr*/)];
143          cpToOctStr_BNU(hdrOctStr, sizeof(hdrOctStr), &locHdr, 1);
144 
145          /* compute md = hash(hrd||msg) */
146          ippsHashUpdate(hdrOctStr, sizeof(hdrOctStr), &hashCtx);
147          ippsHashUpdate(pMsg, msgLen, &hashCtx);
148          ippsHashFinal(md, &hashCtx);
149 
150          /* convert hash into the integer */
151          hashValLen = cpFromOctStr_BNU(hashVal, md, hashLen);
152          hashValLen = cpMod_BNU(hashVal, hashValLen, pModulus, elemLen);
153          cpGFpSet(pPoolElm, hashVal, hashValLen, pGFE);
154 
155          if( gfec_MakePoint(pPoint, pPoolElm, pEC)) {
156             /* set y-coordinate of the point (positive or negative) */
157             BNU_CHUNK_T* pY = ECP_POINT_Y(pPoint);
158             if(pY[0] & 1)
159                cpGFpNeg(pY, pY, pGFE);
160 
161             /* R = [cofactor]R */
162             if(ECP_SUBGROUP(pEC)) {
163                BNU_CHUNK_T* pCofactor = ECP_COFACTOR(pEC);
164                int cofactorLen = GFP_FELEN(pGFE);
165                if(!GFP_IS_ONE(pCofactor, cofactorLen))
166                   gfec_MulPoint(pPoint, pPoint, pCofactor, cofactorLen, /*0,*/ pEC, pScratchBuffer);
167             }
168 
169             cpGFpReleasePool(1, pGFE);
170             return ippStsNoErr;
171          }
172       }
173 
174       cpGFpReleasePool(1, pGFE);
175       return ippStsQuadraticNonResidueErr;
176    }
177 }
178