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 // Purpose: 43 // Cryptography Primitive. 44 // EC over Prime Finite Field (Verify Signature, DSA version) 45 // 46 // Contents: 47 // ippsECCPVerifyDSA() 48 // 49 */ 50 51 #include "owndefs.h" 52 #include "owncp.h" 53 #include "pcpeccp.h" 54 55 56 /*F* 57 // Name: ippsECCPVerifyDSA 58 // 59 // Purpose: Verify Signature (DSA version). 60 // 61 // Returns: Reason: 62 // ippStsNullPtrErr NULL == pEC 63 // NULL == pMsgDigest 64 // NULL == pSignX 65 // NULL == pSignY 66 // NULL == pResult 67 // 68 // ippStsContextMatchErr illegal pEC->idCtx 69 // illegal pMsgDigest->idCtx 70 // illegal pSignX->idCtx 71 // illegal pSignY->idCtx 72 // 73 // ippStsMessageErr MsgDigest >= order 74 // MsgDigest < 0 75 // 76 // ippStsRangeErr SignX < 0 or SignY < 0 77 // 78 // ippStsNoErr no errors 79 // 80 // Parameters: 81 // pMsgDigest pointer to the message representative to be signed 82 // pSignX,pSignY pointer to the signature 83 // pResult pointer to the result: ippECValid/ippECInvalidSignature 84 // pEC pointer to the ECCP context 85 // 86 // Note: 87 // - signer's key must be set up in ECCP context 88 // before ippsECCPVerifyDSA() usage 89 // 90 *F*/ 91 IPPFUN(IppStatus, ippsECCPVerifyDSA,(const IppsBigNumState* pMsgDigest, 92 const IppsBigNumState* pSignX, const IppsBigNumState* pSignY, 93 IppECResult* pResult, 94 IppsECCPState* pEC)) 95 { 96 /* use aligned EC context */ 97 IPP_BAD_PTR1_RET(pEC); 98 pEC = (IppsGFpECState*)( IPP_ALIGNED_PTR(pEC, ECGFP_ALIGNMENT) ); 99 IPP_BADARG_RET(!ECP_TEST_ID(pEC), ippStsContextMatchErr); 100 101 /* test message representative */ 102 IPP_BAD_PTR1_RET(pMsgDigest); 103 pMsgDigest = (IppsBigNumState*)( IPP_ALIGNED_PTR(pMsgDigest, BN_ALIGNMENT) ); 104 IPP_BADARG_RET(!BN_VALID_ID(pMsgDigest), ippStsContextMatchErr); 105 IPP_BADARG_RET(BN_NEGATIVE(pMsgDigest), ippStsMessageErr); 106 107 /* test result */ 108 IPP_BAD_PTR1_RET(pResult); 109 110 /* test signature */ 111 IPP_BAD_PTR2_RET(pSignX,pSignY); 112 pSignX = (IppsBigNumState*)( IPP_ALIGNED_PTR(pSignX, BN_ALIGNMENT) ); 113 pSignY = (IppsBigNumState*)( IPP_ALIGNED_PTR(pSignY, BN_ALIGNMENT) ); 114 IPP_BADARG_RET(!BN_VALID_ID(pSignX), ippStsContextMatchErr); 115 IPP_BADARG_RET(!BN_VALID_ID(pSignY), ippStsContextMatchErr); 116 IPP_BADARG_RET(BN_NEGATIVE(pSignX), ippStsRangeErr); 117 IPP_BADARG_RET(BN_NEGATIVE(pSignY), ippStsRangeErr); 118 119 { 120 IppECResult vResult = ippECInvalidSignature; 121 122 gsModEngine* pModEngine = ECP_MONT_R(pEC); 123 BNU_CHUNK_T* pOrder = MOD_MODULUS(pModEngine); 124 int orderLen = MOD_LEN(pModEngine); 125 126 /* test input message value */ 127 IPP_BADARG_RET(0<=cpCmp_BNU(BN_NUMBER(pMsgDigest), BN_SIZE(pMsgDigest), pOrder, orderLen), ippStsMessageErr); 128 129 /* test signature value */ 130 if(!cpEqu_BNU_CHUNK(BN_NUMBER(pSignX), BN_SIZE(pSignX), 0) && 131 !cpEqu_BNU_CHUNK(BN_NUMBER(pSignY), BN_SIZE(pSignY), 0) && 132 0>cpCmp_BNU(BN_NUMBER(pSignX), BN_SIZE(pSignX), pOrder, orderLen) && 133 0>cpCmp_BNU(BN_NUMBER(pSignY), BN_SIZE(pSignY), pOrder, orderLen)) { 134 135 IppsGFpState* pGF = ECP_GFP(pEC); 136 gsModEngine* pGFE = GFP_PMA(pGF); 137 138 int elmLen = GFP_FELEN(pGFE); 139 int pelmLen = GFP_PELEN(pGFE); 140 141 BNU_CHUNK_T* h1 = cpGFpGetPool(2, pGFE); 142 BNU_CHUNK_T* h2 = h1+pelmLen; 143 144 IppsGFpECPoint P, G, Public; 145 146 /* Y = 1/signY mod order */ 147 __ALIGN8 IppsBigNumState Y; 148 __ALIGN8 IppsBigNumState R; 149 BNU_CHUNK_T* buffer = ECP_SBUFFER(pEC); 150 BN_Make(buffer, buffer+orderLen+1, orderLen, &Y); 151 BN_Make(buffer+(orderLen+1)*2, buffer+(orderLen+1)*3, orderLen, &R); 152 /* BN(order) */ 153 BN_Set(pOrder, orderLen, &R); 154 ippsModInv_BN((IppsBigNumState*)pSignY, &R, &Y); 155 /* h1 = 1/signY mod order */ 156 cpGFpElementCopyPadd(h1, orderLen, BN_NUMBER(&Y), BN_SIZE(&Y)); 157 cpMontEnc_BNU_EX(h1, h1, orderLen, pModEngine); 158 159 /* validate signature */ 160 cpEcGFpInitPoint(&P, cpEcGFpGetPool(1, pEC),0, pEC); 161 cpEcGFpInitPoint(&G, ECP_G(pEC), ECP_AFFINE_POINT|ECP_FINITE_POINT, pEC); 162 cpEcGFpInitPoint(&Public, ECP_PUBLIC(pEC), ECP_FINITE_POINT, pEC); 163 164 /* h2 = pSignX * h1 (mod order) */ 165 cpMontMul_BNU_EX(h2, 166 h1,orderLen, BN_NUMBER(pSignX), BN_SIZE(pSignX), 167 pModEngine); 168 /* h1 = pMsgDigest * h1 (mod order) */ 169 cpMontMul_BNU_EX(h1, 170 h1,orderLen, BN_NUMBER(pMsgDigest), BN_SIZE(pMsgDigest), 171 pModEngine); 172 173 /* compute h1*BasePoint + h2*publicKey */ 174 gfec_BasePointProduct(&P, 175 h1, orderLen, &Public, h2, orderLen, 176 pEC, (Ipp8u*)ECP_SBUFFER(pEC)); 177 178 /* get P.X */ 179 if(gfec_GetPoint(h1, NULL, &P, pEC)) { 180 /* C' = int(P.x) mod order */ 181 GFP_METHOD(pGFE)->decode(h1, h1, pGFE); 182 elmLen = cpMod_BNU(h1, elmLen, pOrder, orderLen); 183 cpGFpElementPadd(h1+elmLen, orderLen-elmLen, 0); 184 185 /* and make sure signX==P.X */ 186 cpGFpElementCopyPadd(h2, orderLen, BN_NUMBER(pSignX), BN_SIZE(pSignX)); 187 if(GFP_EQ(h1, h2, orderLen)) 188 vResult = ippECValid; 189 } 190 191 cpEcGFpReleasePool(1, pEC); 192 cpGFpReleasePool(2, pGFE); 193 } 194 195 *pResult = vResult; 196 return ippStsNoErr; 197 } 198 } 199