/************************************************************************* * Copyright (C) 2013 Intel Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *************************************************************************/ /* // // Purpose: // Cryptography Primitive. // Modular Exponentiation (windowed "safe" version) */ #include "owncp.h" #if defined(_USE_WINDOW_EXP_) #include "pcpngmontexpstuff.h" #include "gsscramble.h" #include "pcpmask_ct.h" /* // "safe" fixed-size window montgomery exponentiation // // - input/output are in Montgomery Domain // - possible inplace mode // // scratch buffer structure: // precomputed table of multipliers[(1<mul(dataTT, dataTT, dataRR, pMont); gsScramblePut(pTable, n, dataTT, nsM, winSize); } /* expand exponent*/ ZEXPAND_COPY_BNU(dataEE, nsM + 1, dataExp, nsE); bitsizeE = ((bitsizeE + winSize - 1) / winSize) * winSize; /* exponentiation */ { /* position of the 1-st (left) window */ int eBit = bitsizeE - winSize; /* extract 1-st window value */ Ipp32u eChunk = *((Ipp32u*)((Ipp16u*)dataEE + eBit / BITSIZE(Ipp16u))); int shift = eBit & 0xF; Ipp32u winVal = (eChunk >> shift) & mask; /* initialize result */ gsScrambleGet_sscm(dataY, nsM, pTable, (int)winVal, winSize); for (eBit -= winSize; eBit >= 0; eBit -= winSize) { /* do square window times */ for (n = 0, winVal = 0; n < winSize; n++) { MOD_METHOD(pMont)->sqr(dataY, dataY, pMont); } /* extract next window value */ eChunk = *((Ipp32u*)((Ipp16u*)dataEE + eBit / BITSIZE(Ipp16u))); shift = eBit & 0xF; winVal = (eChunk >> shift) & mask; /* exptact precomputed value and multiply */ gsScrambleGet_sscm(dataTT, nsM, pTable, (int)winVal, winSize); MOD_METHOD(pMont)->mul(dataY, dataY, dataTT, pMont); } } } return nsM; } /* // "safe" fixed-size window exponentiation // - input/output are in Regular Domain // - possible inplace mode */ /* clang-format off */ IPP_OWN_DEFN(cpSize, gsModExpWin_BNU_sscm, (BNU_CHUNK_T* dataY, const BNU_CHUNK_T* dataX, cpSize nsX, const BNU_CHUNK_T* dataExp, cpSize bitsizeE, gsModEngine* pMont, BNU_CHUNK_T* pBuffer)) /* clang-format on */ { cpSize nsM = MOD_LEN(pMont); /* copy and expand base to the modulus length */ ZEXPAND_COPY_BNU(dataY, nsM, dataX, nsX); /* convert base to Montgomery domain */ MOD_METHOD(pMont)->encode(dataY, dataY, pMont); /* exponentiation */ gsMontExpWin_BNU_sscm(dataY, dataY, nsM, dataExp, bitsizeE, pMont, pBuffer); /* convert result back to regular domain */ MOD_METHOD(pMont)->decode(dataY, dataY, pMont); return nsM; } #endif /* _USE_WINDOW_EXP_ */