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, NR version) 46 // 47 // Contents: 48 // ippsECCPSignNR() 49 // 50 // 51 */ 52 53 #include "owndefs.h" 54 #include "owncp.h" 55 #include "pcpeccp.h" 56 57 58 /*F* 59 // Name: ippsECCPSignNR 60 // 61 // Purpose: Signing of message representative. 62 // (NR 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 ippsECCPNRSign() usage 100 // - ephemeral key pair destroy before exit 101 // 102 *F*/ 103 IPPFUN(IppStatus, ippsECCPSignNR,(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, ALIGN_VAL) ); 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, ALIGN_VAL) ); 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, ALIGN_VAL) ); 128 pSignY = (IppsBigNumState*)( IPP_ALIGNED_PTR(pSignY, ALIGN_VAL) ); 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 int ns; 160 161 /* ephemeral public */ 162 IppsGFpECPoint ephPublic; 163 cpEcGFpInitPoint(&ephPublic, ECP_PUBLIC_E(pEC), ECP_FINITE_POINT|ECP_AFFINE_POINT, pEC); 164 165 /* ephPublic.x (mod order) */ 166 { 167 BNU_CHUNK_T* buffer = gsModPoolAlloc(pMontP, 1); 168 gfec_GetPoint(buffer, NULL, &ephPublic, pEC); 169 GFP_METHOD(pMontP)->decode(buffer, buffer, pMontP); 170 ns = cpMod_BNU(buffer, elmLen, pOrder, ordLen); 171 cpGFpElementCopyPadd(dataC, ordLen, buffer, ns); 172 gsModPoolFree(pMontP, 1); 173 } 174 175 /* signX = (pMsgDigest + C) (mod order) */ 176 ZEXPAND_COPY_BNU(buffF, ordLen, pMsgData, msgLen); 177 cpModAdd_BNU(dataC, dataC, buffF, pOrder, ordLen, dataD); 178 179 if(!GFP_IS_ZERO(dataC, ordLen)) { 180 181 /* signY = (eph_private - private*signX) (mod order) */ 182 ZEXPAND_COPY_BNU(dataD, ordLen, pPriData, priLen); 183 GFP_METHOD(pMontR)->encode(dataD, dataD, pMontR); 184 GFP_METHOD(pMontR)->mul(dataD, dataD, dataC, pMontR); 185 cpModSub_BNU(dataD, ECP_PRIVAT_E(pEC), dataD, pOrder, ordLen, buffF); 186 187 /* signX */ 188 ns = ordLen; 189 FIX_BNU(dataC, ns); 190 BN_SIGN(pSignX) = ippBigNumPOS; 191 BN_SIZE(pSignX) = ns; 192 193 /* signY */ 194 ns = ordLen; 195 FIX_BNU(dataD, ns); 196 BN_SIGN(pSignY) = ippBigNumPOS; 197 BN_SIZE(pSignY) = ns; 198 199 return ippStsNoErr; 200 } 201 202 return ippStsEphemeralKeyErr; 203 } 204 } 205 } 206