• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2 * Copyright 2016-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 //
44 //     Context:
45 //        ippsGFpECSharedSecretDHC()
46 //
47 */
48 
49 #include "owndefs.h"
50 #include "owncp.h"
51 #include "pcpgfpecstuff.h"
52 
53 
54 
55 /*F*
56 //    Name: ippsGFpECSharedSecretDHC
57 //
58 // Purpose: Compute Shared Secret (Diffie-Hellman with cofactor)
59 //
60 // Returns:                   Reason:
61 //    ippStsNullPtrErr           NULL == pEC
62 //                               NULL == pPrivateA
63 //                               NULL == pPublicB
64 //                               NULL == pShare
65 //
66 //    ippStsContextMatchErr      illegal pEC->idCtx
67 //                               pEC->subgroup == NULL
68 //                               illegal pPrivateA->idCtx
69 //                               illegal pPublicB->idCtx
70 //                               illegal pShare->idCtx
71 //
72 //    ippStsRangeErr             not enough room for share key
73 //
74 //    ippStsShareKeyErr          (infinity) => z
75 //
76 //    ippStsNoErr                no errors
77 //
78 // Parameters:
79 //    pPrivateA   pointer to own   private key
80 //    pPublicB    pointer to alien public  key
81 //    pShare      pointer to the shared secret value
82 //    pEC         pointer to the EC context
83 //
84 *F*/
85 IPPFUN(IppStatus, ippsGFpECSharedSecretDHC,(const IppsBigNumState* pPrivateA, const IppsGFpECPoint* pPublicB,
86                                             IppsBigNumState* pShare,
87                                             IppsGFpECState* pEC, Ipp8u* pScratchBuffer))
88 {
89    IppsGFpState*  pGF;
90    gsModEngine* pGFE;
91 
92    /* EC context and buffer */
93    IPP_BAD_PTR2_RET(pEC, pScratchBuffer);
94    pEC = (IppsGFpECState*)( IPP_ALIGNED_PTR(pEC, ECGFP_ALIGNMENT) );
95    IPP_BADARG_RET(!ECP_TEST_ID(pEC), ippStsContextMatchErr);
96    IPP_BADARG_RET(!ECP_SUBGROUP(pEC), ippStsContextMatchErr);
97 
98    pGF = ECP_GFP(pEC);
99    pGFE = GFP_PMA(pGF);
100 
101    /* test private (own) key */
102    IPP_BAD_PTR1_RET(pPrivateA);
103    pPrivateA = (IppsBigNumState*)( IPP_ALIGNED_PTR(pPrivateA, ALIGN_VAL) );
104    IPP_BADARG_RET(!BN_VALID_ID(pPrivateA), ippStsContextMatchErr);
105 
106    /* test public (other party) key */
107    IPP_BAD_PTR1_RET(pPublicB);
108    IPP_BADARG_RET( !ECP_POINT_TEST_ID(pPublicB), ippStsContextMatchErr );
109 
110    /* test share key */
111    IPP_BAD_PTR1_RET(pShare);
112    pShare = (IppsBigNumState*)( IPP_ALIGNED_PTR(pShare, ALIGN_VAL) );
113    IPP_BADARG_RET(!BN_VALID_ID(pShare), ippStsContextMatchErr);
114    IPP_BADARG_RET((BN_ROOM(pShare)<GFP_FELEN(pGFE)), ippStsRangeErr);
115 
116    {
117       int elmLen = GFP_FELEN(pGFE);
118 
119       IppsGFpElement elm;
120       IppsGFpECPoint T;
121       int finite_point;
122 
123       gsModEngine* montR = ECP_MONT_R(pEC);
124       int nsR = MOD_LEN(montR);
125 
126       BNU_CHUNK_T* F = cpGFpGetPool(2, pGFE);
127 
128       /* compute factored secret F = coFactor*privateA */
129       BNU_CHUNK_T* pCofactor = ECP_COFACTOR(pEC);
130       int cofactorLen = GFP_FELEN(pGFE);
131       cofactorLen = cpGFpElementLen(pCofactor, cofactorLen);
132 
133       if(GFP_IS_ONE(pCofactor, cofactorLen))
134          cpGFpElementCopyPadd(F, nsR, BN_NUMBER(pPrivateA), BN_SIZE(pPrivateA));
135       else {
136          cpMontEnc_BNU_EX(F, BN_NUMBER(pPrivateA), BN_SIZE(pPrivateA), montR);
137          cpMontMul_BNU_EX(F, F, nsR, pCofactor, cofactorLen, montR);
138       }
139       /* T = [F]pPublicB */
140       cpEcGFpInitPoint(&T, cpEcGFpGetPool(1, pEC),0, pEC);
141       gfec_MulPoint(&T, pPublicB, F, nsR, /*ECP_ORDBITSIZE(pEC),*/ pEC, pScratchBuffer);
142 
143       /* share = T.x */
144       cpGFpElementConstruct(&elm, F, elmLen);
145       finite_point = gfec_GetPoint(GFPE_DATA(&elm), NULL, &T, pEC);
146       if(finite_point) {
147          BNU_CHUNK_T* pShareData = BN_NUMBER(pShare);
148          int nsShare = BN_ROOM(pShare);
149          /* share = decode(T.x) */
150          GFP_METHOD(pGFE)->decode(pShareData, GFPE_DATA(&elm), pGFE);
151          cpGFpElementPadd(pShareData+elmLen, nsShare-elmLen, 0);
152 
153          BN_SIGN(pShare) = ippBigNumPOS;
154          FIX_BNU(pShareData, nsShare);
155          BN_SIZE(pShare) = nsShare;
156       }
157 
158       cpGFpReleasePool(2, pGFE);
159       cpEcGFpReleasePool(1, pEC);
160 
161       return finite_point? ippStsNoErr : ippStsShareKeyErr;
162    }
163 }
164