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 (Sign, DSA version) 46 // 47 // Contents: 48 // ippsECCPSignDSA() 49 // 50 // 51 */ 52 53 #include "owndefs.h" 54 #include "owncp.h" 55 #include "pcpeccp.h" 56 57 58 /*F* 59 // Name: ippsECCPSignDSA 60 // 61 // Purpose: Signing of message representative. 62 // (DSA version). 63 // 64 // Returns: Reason: 65 // ippStsNullPtrErr NULL == pEC 66 // NULL == pMsgDigest 67 // NULL == pPrivate 68 // NULL == pSignX 69 // NULL == pSignY 70 // 71 // ippStsContextMatchErr illegal pEC->idCtx 72 // illegal pMsgDigest->idCtx 73 // illegal pPrivate->idCtx 74 // illegal pSignX->idCtx 75 // illegal pSignY->idCtx 76 // 77 // ippStsIvalidPrivateKey 0 >= Private 78 // Private >= order 79 // 80 // ippStsMessageErr MsgDigest >= order 81 // MsgDigest < 0 82 // 83 // ippStsRangeErr not enough room for: 84 // signX 85 // signY 86 // 87 // ippStsEphemeralKeyErr (0==signX) || (0==signY) 88 // 89 // ippStsNoErr no errors 90 // 91 // Parameters: 92 // pMsgDigest pointer to the message representative to be signed 93 // pPrivate pointer to the regular private key 94 // pSignX,pSignY pointer to the signature 95 // pEC pointer to the ECCP context 96 // 97 // Note: 98 // - ephemeral key pair extracted from pEC and 99 // must be generated and before ippsECCPDSASign() usage 100 // - ephemeral key pair destroy before exit 101 // 102 *F*/ 103 IPPFUN(IppStatus, ippsECCPSignDSA,(const IppsBigNumState* pMsgDigest, 104 const IppsBigNumState* pPrivate, 105 IppsBigNumState* pSignX, IppsBigNumState* pSignY, 106 IppsECCPState* pEC)) 107 { 108 /* use aligned EC context */ 109 IPP_BAD_PTR1_RET(pEC); 110 pEC = (IppsGFpECState*)( IPP_ALIGNED_PTR(pEC, ECGFP_ALIGNMENT) ); 111 IPP_BADARG_RET(!ECP_TEST_ID(pEC), ippStsContextMatchErr); 112 113 /* test private key*/ 114 IPP_BAD_PTR1_RET(pPrivate); 115 pPrivate = (IppsBigNumState*)( IPP_ALIGNED_PTR(pPrivate, BN_ALIGNMENT) ); 116 IPP_BADARG_RET(!BN_VALID_ID(pPrivate), ippStsContextMatchErr); 117 IPP_BADARG_RET(BN_NEGATIVE(pPrivate), ippStsIvalidPrivateKey); 118 119 /* test message representative */ 120 IPP_BAD_PTR1_RET(pMsgDigest); 121 pMsgDigest = (IppsBigNumState*)( IPP_ALIGNED_PTR(pMsgDigest, BN_ALIGNMENT) ); 122 IPP_BADARG_RET(!BN_VALID_ID(pMsgDigest), ippStsContextMatchErr); 123 IPP_BADARG_RET(BN_NEGATIVE(pMsgDigest), ippStsMessageErr); 124 125 /* test signature */ 126 IPP_BAD_PTR2_RET(pSignX,pSignY); 127 pSignX = (IppsBigNumState*)( IPP_ALIGNED_PTR(pSignX, BN_ALIGNMENT) ); 128 pSignY = (IppsBigNumState*)( IPP_ALIGNED_PTR(pSignY, BN_ALIGNMENT) ); 129 IPP_BADARG_RET(!BN_VALID_ID(pSignX), ippStsContextMatchErr); 130 IPP_BADARG_RET(!BN_VALID_ID(pSignY), ippStsContextMatchErr); 131 IPP_BADARG_RET((BN_ROOM(pSignX)*BITSIZE(BNU_CHUNK_T)<ECP_ORDBITSIZE(pEC)), ippStsRangeErr); 132 IPP_BADARG_RET((BN_ROOM(pSignY)*BITSIZE(BNU_CHUNK_T)<ECP_ORDBITSIZE(pEC)), ippStsRangeErr); 133 134 { 135 gsModEngine* pMontR = ECP_MONT_R(pEC); 136 BNU_CHUNK_T* pOrder = MOD_MODULUS(pMontR); 137 int ordLen = MOD_LEN(pMontR); 138 139 BNU_CHUNK_T* pPriData = BN_NUMBER(pPrivate); 140 int priLen = BN_SIZE(pPrivate); 141 142 BNU_CHUNK_T* pMsgData = BN_NUMBER(pMsgDigest); 143 int msgLen = BN_SIZE(pMsgDigest); 144 145 /* make sure regular 0 < private < order */ 146 IPP_BADARG_RET(cpEqu_BNU_CHUNK(pPriData, priLen, 0) || 147 0<=cpCmp_BNU(pPriData, priLen, pOrder, ordLen), ippStsIvalidPrivateKey); 148 /* make sure msg <order */ 149 IPP_BADARG_RET(0<=cpCmp_BNU(pMsgData, msgLen, pOrder, ordLen), ippStsMessageErr); 150 151 { 152 IppsGFpState* pGF = ECP_GFP(pEC); 153 gsModEngine* pMontP = GFP_PMA(pGF); 154 int elmLen = GFP_FELEN(pMontP); 155 156 BNU_CHUNK_T* dataC = BN_NUMBER(pSignX); 157 BNU_CHUNK_T* dataD = BN_NUMBER(pSignY); 158 BNU_CHUNK_T* buffF = BN_BUFFER(pSignX); 159 BNU_CHUNK_T* buffT = BN_BUFFER(pSignY); 160 int ns; 161 162 /* ephemeral public */ 163 IppsGFpECPoint ephPublic; 164 cpEcGFpInitPoint(&ephPublic, ECP_PUBLIC_E(pEC), ECP_FINITE_POINT|ECP_AFFINE_POINT, pEC); 165 166 /* 167 // signX = int(ephPublic.x) (mod order) 168 */ 169 { 170 BNU_CHUNK_T* buffer = gsModPoolAlloc(pMontP, 1); 171 gfec_GetPoint(buffer, NULL, &ephPublic, pEC); 172 GFP_METHOD(pMontP)->decode(buffer, buffer, pMontP); 173 ns = cpMod_BNU(buffer, elmLen, pOrder, ordLen); 174 cpGFpElementCopyPadd(dataC, ordLen, buffer, ns); 175 gsModPoolFree(pMontP, 1); 176 } 177 178 if(!GFP_IS_ZERO(dataC, ordLen)) { 179 /* 180 // signY = (1/ephPrivate)*(pMsgDigest + private*signX) (mod order) 181 */ 182 183 /* copy and expand message is being signed */ 184 ZEXPAND_COPY_BNU(buffF, ordLen, pMsgData, msgLen); 185 186 /* private representation in Montgomery domain */ 187 ZEXPAND_COPY_BNU(dataD, ordLen, pPriData, priLen); 188 GFP_METHOD(pMontR)->encode(dataD, dataD, pMontR); 189 190 /* (private*signX) in regular domain */ 191 GFP_METHOD(pMontR)->mul(dataD, dataD, dataC, pMontR); 192 193 /* pMsgDigest + private*signX */ 194 cpModAdd_BNU(dataD, dataD, buffF, pOrder, ordLen, buffT); 195 196 if(!GFP_IS_ZERO(dataD, ordLen)) { 197 /* (1/ephPrivate) in Montgomery domain */ 198 gs_mont_inv(buffT, ECP_PRIVAT_E(pEC), pMontR, alm_mont_inv_ct); 199 200 /* (1/ephPrivate)*(pMsgDigest + private*signX) */ 201 GFP_METHOD(pMontR)->mul(dataD, dataD, buffT, pMontR); 202 203 /* signX */ 204 ns = ordLen; 205 FIX_BNU(dataC, ns); 206 BN_SIGN(pSignX) = ippBigNumPOS; 207 BN_SIZE(pSignX) = ns; 208 /* signY */ 209 ns = ordLen; 210 FIX_BNU(dataD, ns); 211 BN_SIGN(pSignY) = ippBigNumPOS; 212 BN_SIZE(pSignY) = ns; 213 214 return ippStsNoErr; 215 } 216 } 217 218 return ippStsEphemeralKeyErr; 219 } 220 } 221 } 222