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