1 /******************************************************************************* 2 * Copyright 2013-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 (Verify Signature, SM2 version) 46 // 47 // Contents: 48 // ippsECCPVerifySM2() 49 // 50 // 51 */ 52 53 #include "owndefs.h" 54 #include "owncp.h" 55 #include "pcpeccp.h" 56 57 58 /*F* 59 // Name: ippsECCPVerifySM2 60 // 61 // Purpose: Verify Signature (SM2 version). 62 // 63 // Returns: Reason: 64 // ippStsNullPtrErr NULL == pEC 65 // NULL == pMsgDigest 66 // NULL == pRegPublic 67 // NULL == pSignR 68 // NULL == pSignS 69 // NULL == pResult 70 // 71 // ippStsContextMatchErr illegal pEC->idCtx 72 // illegal pMsgDigest->idCtx 73 // illegal pRegPublic->idCtx 74 // illegal pSignR->idCtx 75 // illegal pSignS->idCtx 76 // 77 // ippStsMessageErr 0> MsgDigest 78 // order<= MsgDigest 79 // 80 // ippStsRangeErr SignR < 0 or SignS < 0 81 // 82 // ippStsNoErr no errors 83 // 84 // Parameters: 85 // pMsgDigest pointer to the message representative to being signed 86 // pRegPublic pointer to the regular public key 87 // pSignR,pSignS pointer to the signature 88 // pResult pointer to the result: ippECValid/ippECInvalidSignature 89 // pEC pointer to the ECCP context 90 // 91 *F*/ 92 IPPFUN(IppStatus, ippsECCPVerifySM2,(const IppsBigNumState* pMsgDigest, 93 const IppsECCPPointState* pRegPublic, 94 const IppsBigNumState* pSignR, const IppsBigNumState* pSignS, 95 IppECResult* pResult, 96 IppsECCPState* pEC)) 97 { 98 IppsGFpState* pGF; 99 gsModEngine* pGFE; 100 101 /* use aligned EC context */ 102 IPP_BAD_PTR1_RET(pEC); 103 pEC = (IppsGFpECState*)( IPP_ALIGNED_PTR(pEC, ECGFP_ALIGNMENT) ); 104 IPP_BADARG_RET(!ECP_TEST_ID(pEC), ippStsContextMatchErr); 105 106 pGF = ECP_GFP(pEC); 107 pGFE = GFP_PMA(pGF); 108 109 /* test message representative */ 110 IPP_BAD_PTR1_RET(pMsgDigest); 111 pMsgDigest = (IppsBigNumState*)( IPP_ALIGNED_PTR(pMsgDigest, ALIGN_VAL) ); 112 IPP_BADARG_RET(!BN_VALID_ID(pMsgDigest), ippStsContextMatchErr); 113 IPP_BADARG_RET(BN_NEGATIVE(pMsgDigest), ippStsMessageErr); 114 115 /* test regular public key */ 116 IPP_BAD_PTR1_RET(pRegPublic); 117 IPP_BADARG_RET( !ECP_POINT_TEST_ID(pRegPublic), ippStsContextMatchErr ); 118 IPP_BADARG_RET( ECP_POINT_FELEN(pRegPublic)!=GFP_FELEN(pGFE), ippStsOutOfRangeErr); 119 120 /* test result */ 121 IPP_BAD_PTR1_RET(pResult); 122 123 /* test signature */ 124 IPP_BAD_PTR2_RET(pSignR, pSignS); 125 pSignR = (IppsBigNumState*)( IPP_ALIGNED_PTR(pSignR, ALIGN_VAL) ); 126 pSignS = (IppsBigNumState*)( IPP_ALIGNED_PTR(pSignS, ALIGN_VAL) ); 127 IPP_BADARG_RET(!BN_VALID_ID(pSignR), ippStsContextMatchErr); 128 IPP_BADARG_RET(!BN_VALID_ID(pSignS), ippStsContextMatchErr); 129 IPP_BADARG_RET(BN_NEGATIVE(pSignR), ippStsRangeErr); 130 IPP_BADARG_RET(BN_NEGATIVE(pSignS), ippStsRangeErr); 131 132 { 133 IppECResult vResult = ippECInvalidSignature; 134 135 gsModEngine* pMontR = ECP_MONT_R(pEC); 136 BNU_CHUNK_T* pOrder = MOD_MODULUS(pMontR); 137 int orderLen = MOD_LEN(pMontR); 138 139 BNU_CHUNK_T* pMsgData = BN_NUMBER(pMsgDigest); 140 int msgLen = BN_SIZE(pMsgDigest); 141 142 /* test message value */ 143 IPP_BADARG_RET(0<=cpCmp_BNU(pMsgData, msgLen, pOrder, orderLen), ippStsMessageErr); 144 145 /* test signature value */ 146 if(!cpEqu_BNU_CHUNK(BN_NUMBER(pSignR), BN_SIZE(pSignR), 0) && 147 !cpEqu_BNU_CHUNK(BN_NUMBER(pSignS), BN_SIZE(pSignS), 0) && 148 0>cpCmp_BNU(BN_NUMBER(pSignR), BN_SIZE(pSignR), pOrder, orderLen) && 149 0>cpCmp_BNU(BN_NUMBER(pSignS), BN_SIZE(pSignS), pOrder, orderLen)) { 150 151 int elmLen = GFP_FELEN(pGFE); 152 int ns; 153 154 BNU_CHUNK_T* r = cpGFpGetPool(4, pGFE); 155 BNU_CHUNK_T* s = r+orderLen; 156 BNU_CHUNK_T* t = s+orderLen; 157 BNU_CHUNK_T* f = t+orderLen; 158 159 /* expand signatire's components */ 160 cpGFpElementCopyPadd(r, orderLen, BN_NUMBER(pSignR), BN_SIZE(pSignR)); 161 cpGFpElementCopyPadd(s, orderLen, BN_NUMBER(pSignS), BN_SIZE(pSignS)); 162 163 /* t = (r+s) mod order */ 164 cpModAdd_BNU(t, r, s, pOrder, orderLen, f); 165 166 /* P = [s]G +[t]regPublic, t = P.x */ 167 { 168 IppsGFpECPoint P, G; 169 cpEcGFpInitPoint(&P, cpEcGFpGetPool(1, pEC),0, pEC); 170 cpEcGFpInitPoint(&G, ECP_G(pEC), ECP_AFFINE_POINT|ECP_FINITE_POINT, pEC); 171 172 gfec_BasePointProduct(&P, 173 s, orderLen, pRegPublic, t, orderLen, 174 pEC, (Ipp8u*)ECP_SBUFFER(pEC)); 175 176 gfec_GetPoint(t, NULL, &P, pEC); 177 GFP_METHOD(pGFE)->decode(t, t, pGFE); 178 ns = cpMod_BNU(t, elmLen, pOrder, orderLen); 179 180 cpEcGFpReleasePool(1, pEC); 181 } 182 183 /* t = (msg+t) mod order */ 184 cpGFpElementCopyPadd(f, orderLen, pMsgData, msgLen); 185 cpModAdd_BNU(t, t, f, pOrder, orderLen, f); 186 187 if(GFP_EQ(t, r, orderLen)) 188 vResult = ippECValid; 189 190 cpGFpReleasePool(4, pGFE); 191 } 192 193 *pResult = vResult; 194 return ippStsNoErr; 195 } 196 } 197