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 //        ippsGFpECSignDSA()
46 //
47 */
48 
49 #include "owndefs.h"
50 #include "owncp.h"
51 #include "pcpeccp.h"
52 
53 /*F*
54 //    Name: ippsGFpECSignDSA
55 //
56 // Purpose: DSA Signature Generation.
57 //
58 // Returns:                   Reason:
59 //    ippStsNullPtrErr           NULL == pEC
60 //                               NULL == pMsgDigest
61 //                               NULL == pRegPrivate
62 //                               NULL == pEphPrivate
63 //                               NULL == pSignR
64 //                               NULL == pSignS
65 //                               NULL == pScratchBuffer
66 //
67 //    ippStsContextMatchErr      illegal pEC->idCtx
68 //                               pEC->subgroup == NULL
69 //                               illegal pMsgDigest->idCtx
70 //                               illegal pRegPrivate->idCtx
71 //                               illegal pEphPrivate->idCtx
72 //                               illegal pSignR->idCtx
73 //                               illegal pSignS->idCtx
74 //
75 //    ippStsIvalidPrivateKey     0 >= RegPrivate
76 //                               RegPrivate >= order
77 //
78 //                               0 >= EphPrivate
79 //                               EphPrivate >= order
80 //
81 //    ippStsMessageErr           MsgDigest >= order
82 //                               MsgDigest <  0
83 //
84 //    ippStsRangeErr             not enough room for:
85 //                               signR
86 //                               signS
87 //
88 //    ippStsEphemeralKeyErr      (0==signR) || (0==signS)
89 //
90 //    ippStsNotSupportedModeErr  pGFE->extdegree > 1
91 //
92 //
93 //    ippStsNoErr                no errors
94 //
95 // Parameters:
96 //    pMsgDigest     pointer to the message representative to be signed
97 //    pRegPrivate    pointer to the regular private key
98 //    pEphPrivate    pointer to the ephemeral private key
99 //    pSignR,pSignS  pointer to the signature
100 //    pEC            pointer to the EC context
101 //    pScratchBuffer pointer to buffer (1 mul_point operation)
102 //
103 *F*/
104 IPPFUN(IppStatus, ippsGFpECSignDSA,(const IppsBigNumState* pMsgDigest,
105                                     const IppsBigNumState* pRegPrivate,
106                                     const IppsBigNumState* pEphPrivate,
107                                     IppsBigNumState* pSignR, IppsBigNumState* pSignS,
108                                     IppsGFpECState* pEC,
109                                     Ipp8u* pScratchBuffer))
110 {
111    IppsGFpState*  pGF;
112    gsModEngine* pMontP;
113 
114    /* EC context and buffer */
115    IPP_BAD_PTR2_RET(pEC, pScratchBuffer);
116    pEC = (IppsGFpECState*)( IPP_ALIGNED_PTR(pEC, ECGFP_ALIGNMENT) );
117    IPP_BADARG_RET(!ECP_TEST_ID(pEC), ippStsContextMatchErr);
118    IPP_BADARG_RET(!ECP_SUBGROUP(pEC), ippStsContextMatchErr);
119 
120    pGF = ECP_GFP(pEC);
121    pMontP = GFP_PMA(pGF);
122    IPP_BADARG_RET(1<GFP_EXTDEGREE(pMontP), ippStsNotSupportedModeErr);
123 
124    /* test message representative */
125    IPP_BAD_PTR1_RET(pMsgDigest);
126    pMsgDigest = (IppsBigNumState*)( IPP_ALIGNED_PTR(pMsgDigest, ALIGN_VAL) );
127    IPP_BADARG_RET(!BN_VALID_ID(pMsgDigest), ippStsContextMatchErr);
128    IPP_BADARG_RET(BN_NEGATIVE(pMsgDigest), ippStsMessageErr);
129 
130    /* test signature */
131    IPP_BAD_PTR2_RET(pSignR, pSignS);
132    pSignR = (IppsBigNumState*)( IPP_ALIGNED_PTR(pSignR, BN_ALIGNMENT) );
133    pSignS = (IppsBigNumState*)( IPP_ALIGNED_PTR(pSignS, BN_ALIGNMENT) );
134    IPP_BADARG_RET(!BN_VALID_ID(pSignR), ippStsContextMatchErr);
135    IPP_BADARG_RET(!BN_VALID_ID(pSignS), ippStsContextMatchErr);
136    IPP_BADARG_RET((BN_ROOM(pSignR)*BITSIZE(BNU_CHUNK_T)<ECP_ORDBITSIZE(pEC)), ippStsRangeErr);
137    IPP_BADARG_RET((BN_ROOM(pSignS)*BITSIZE(BNU_CHUNK_T)<ECP_ORDBITSIZE(pEC)), ippStsRangeErr);
138 
139    /* test private keys */
140    IPP_BAD_PTR2_RET(pRegPrivate, pEphPrivate);
141 
142    pRegPrivate = (IppsBigNumState*)( IPP_ALIGNED_PTR(pRegPrivate, ALIGN_VAL) );
143    IPP_BADARG_RET(!BN_VALID_ID(pRegPrivate), ippStsContextMatchErr);
144    IPP_BADARG_RET(BN_NEGATIVE(pRegPrivate), ippStsIvalidPrivateKey);
145 
146    pEphPrivate = (IppsBigNumState*)( IPP_ALIGNED_PTR(pEphPrivate, ALIGN_VAL) );
147    IPP_BADARG_RET(!BN_VALID_ID(pEphPrivate), ippStsContextMatchErr);
148    IPP_BADARG_RET(BN_NEGATIVE(pEphPrivate), ippStsEphemeralKeyErr);
149 
150    {
151       gsModEngine* pMontR = ECP_MONT_R(pEC);
152       BNU_CHUNK_T* pOrder = MOD_MODULUS(pMontR);
153       int ordLen = MOD_LEN(pMontR);
154 
155       BNU_CHUNK_T* dataC = BN_NUMBER(pSignR);
156       BNU_CHUNK_T* dataD = BN_NUMBER(pSignS);
157       BNU_CHUNK_T* buffF = BN_BUFFER(pSignR);
158       BNU_CHUNK_T* buffT = BN_BUFFER(pSignS);
159 
160       BNU_CHUNK_T* pPriData = BN_NUMBER(pRegPrivate);
161       int priLen = BN_SIZE(pRegPrivate);
162 
163       BNU_CHUNK_T* pEphData = BN_NUMBER(pEphPrivate);
164       int ephLen = BN_SIZE(pEphPrivate);
165 
166       BNU_CHUNK_T* pMsgData = BN_NUMBER(pMsgDigest);
167       int msgLen = BN_SIZE(pMsgDigest);
168 
169       /* test value of private keys: 0 < regPrivate < order, 0 < ephPrivate < order */
170       IPP_BADARG_RET(cpEqu_BNU_CHUNK(pPriData, priLen, 0) ||
171                   0<=cpCmp_BNU(pPriData, priLen, pOrder, ordLen), ippStsIvalidPrivateKey);
172       IPP_BADARG_RET(cpEqu_BNU_CHUNK(pEphData, ephLen, 0) ||
173                   0<=cpCmp_BNU(pEphData, ephLen, pOrder, ordLen), ippStsEphemeralKeyErr);
174 
175       /* make sure msg <order */
176       IPP_BADARG_RET(0<=cpCmp_BNU(pMsgData, msgLen, pOrder, ordLen), ippStsMessageErr);
177 
178       {
179          int elmLen = GFP_FELEN(pMontP);
180          int ns;
181 
182          /* compute ephemeral public key */
183          IppsGFpECPoint ephPublic;
184          cpEcGFpInitPoint(&ephPublic, cpEcGFpGetPool(1, pEC), 0, pEC);
185          gfec_MulBasePoint(&ephPublic,
186                            BN_NUMBER(pEphPrivate), BN_SIZE(pEphPrivate),
187                            pEC, pScratchBuffer);
188 
189          /*
190          // signR = int(ephPublic.x) (mod order)
191          */
192          {
193             BNU_CHUNK_T* buffer = gsModPoolAlloc(pMontP, 1);
194             gfec_GetPoint(buffer, NULL, &ephPublic, pEC);
195             GFP_METHOD(pMontP)->decode(buffer, buffer, pMontP);
196             ns = cpMod_BNU(buffer, elmLen, pOrder, ordLen);
197             cpGFpElementCopyPadd(dataC, ordLen, buffer, ns);
198             gsModPoolFree(pMontP, 1);
199          }
200          cpEcGFpReleasePool(1, pEC);
201 
202          if(!GFP_IS_ZERO(dataC, ordLen)) {
203             /*
204             // signS = (1/ephPrivate)*(pMsgDigest + private*signR) (mod order)
205             */
206 
207             /* reduce message: msg = msg mod ordfer */
208             BNU_CHUNK_T* buffMsg= BN_BUFFER(pMsgDigest);
209             COPY_BNU(buffMsg, pMsgData, msgLen);
210             ns = cpMod_BNU(buffMsg, msgLen, pOrder, ordLen);
211             /* copy and expand message is being signed */
212             ZEXPAND_COPY_BNU(buffF, ordLen, buffMsg, ns);
213 
214             /* private representation in Montgomery domain */
215             ZEXPAND_COPY_BNU(dataD, ordLen, pPriData, priLen);
216             GFP_METHOD(pMontR)->encode(dataD, dataD, pMontR);
217 
218             /* (private*signX) in regular domain */
219             GFP_METHOD(pMontR)->mul(dataD, dataD, dataC, pMontR);
220 
221             /* pMsgDigest + private*signX */
222             cpModAdd_BNU(dataD, dataD, buffF, pOrder, ordLen, buffT);
223 
224             if(!GFP_IS_ZERO(dataD, ordLen)) {
225                /* (1/ephPrivate) in Montgomery domain */
226                ZEXPAND_COPY_BNU(buffT, ordLen, pEphData, ephLen);
227                gs_mont_inv(buffT, buffT, pMontR, alm_mont_inv_ct);
228 
229                /* (1/ephPrivate)*(pMsgDigest + private*signS) */
230                GFP_METHOD(pMontR)->mul(dataD, dataD, buffT, pMontR);
231 
232                /* signR */
233                ns = ordLen;
234                FIX_BNU(dataC, ns);
235                BN_SIGN(pSignR) = ippBigNumPOS;
236                BN_SIZE(pSignR) = ns;
237                /* signS */
238                ns = ordLen;
239                FIX_BNU(dataD, ns);
240                BN_SIGN(pSignS) = ippBigNumPOS;
241                BN_SIZE(pSignS) = ns;
242 
243                return ippStsNoErr;
244             }
245          }
246 
247          return ippStsEphemeralKeyErr;
248       }
249    }
250 }
251