Home | History | Annotate | Download | only in ippcp
      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 //     Internal Definitions and
     46 //     Internal ng RSA Function Prototypes
     47 //
     48 //
     49 */
     50 
     51 #if !defined(_CP_NG_MONT_EXP_STUFF_H)
     52 #define _CP_NG_MONT_EXP_STUFF_H
     53 
     54 #include "pcpbnuimpl.h"
     55 #include "pcpbn.h"
     56 #include "gsmodstuff.h"
     57 
     58 
     59 /*
     60 // optimal size of fixed window exponentiation
     61 */
     62 __INLINE cpSize gsMontExp_WinSize(cpSize bitsize)
     63 {
     64    #if defined(_USE_WINDOW_EXP_)
     65    // new computations
     66    return
     67       #if (_IPP !=_IPP_M5)     /*limited by 6 or 4 (LOG_CACHE_LINE_SIZE); we use it for windowing-exp imtigation */
     68          bitsize> 4096? 6 :    /* 4096- .. .  */
     69          bitsize> 2666? 5 :    /* 2666 - 4095 */
     70       #endif
     71          bitsize>  717? 4 :    /*  717 - 2665 */
     72          bitsize>  178? 3 :    /*  178 - 716  */
     73          bitsize>   41? 2 : 1; /*   41 - 177  */
     74    #else
     75    UNREFERENCED_PARAMETER(bitsize);
     76    return 1;
     77    #endif
     78 }
     79 
     80 /*
     81 // Montgomery encoding/decoding
     82 */
     83 __INLINE cpSize gsMontEnc_BNU(BNU_CHUNK_T* pR,
     84                         const BNU_CHUNK_T* pXreg, cpSize nsX,
     85                         const gsModEngine* pMont)
     86 {
     87    cpSize nsM = MOD_LEN( pMont );
     88    ZEXPAND_COPY_BNU(pR, nsM, pXreg, nsX);
     89    MOD_METHOD( pMont )->encode(pR, pR, (gsModEngine*)pMont);
     90    return nsM;
     91 }
     92 
     93 __INLINE cpSize gsMontDec_BNU(BNU_CHUNK_T* pR,
     94                         const BNU_CHUNK_T* pXmont,
     95                               gsModEngine* pMont)
     96 {
     97    cpSize nsM = MOD_LEN(pMont);
     98    MOD_METHOD( pMont )->decode(pR, pXmont, (gsModEngine*)pMont);
     99    return nsM;
    100 }
    101 
    102 __INLINE void gsMontEnc_BN(IppsBigNumState* pRbn,
    103                      const IppsBigNumState* pXbn,
    104                            gsModEngine* pMont)
    105 {
    106    BNU_CHUNK_T* pR = BN_NUMBER(pRbn);
    107    cpSize nsM = MOD_LEN(pMont);
    108 
    109    gsMontEnc_BNU(pR, BN_NUMBER(pXbn), BN_SIZE(pXbn), pMont);
    110 
    111    FIX_BNU(pR, nsM);
    112    BN_SIZE(pRbn) = nsM;
    113    BN_SIGN(pRbn) = ippBigNumPOS;
    114 }
    115 
    116 
    117 /* exponentiation buffer size */
    118 #define gsMontExpBinBuffer OWNAPI(gsMontExpBinBuffer)
    119 #define gsMontExpWinBuffer OWNAPI(gsMontExpWinBuffer)
    120 cpSize  gsMontExpBinBuffer(int modulusBits);
    121 cpSize  gsMontExpWinBuffer(int modulusBits);
    122 
    123 /* exponentiation prototype */
    124 typedef cpSize (*ngMontExp)(BNU_CHUNK_T* dataY,
    125                       const BNU_CHUNK_T* dataX, cpSize nsX,
    126                       const BNU_CHUNK_T* dataE, cpSize nbitsE,
    127                             gsModEngine* pMont,
    128                             BNU_CHUNK_T* pBuffer);
    129 
    130 /*
    131 // "fast" and "safe" binary montgomery exponentiation ("fast" version)
    132 */
    133 #define gsMontExpBin_BNU OWNAPI(gsMontExpBin_BNU)
    134 #define gsModExpBin_BNU OWNAPI(gsModExpBin_BNU)
    135 cpSize  gsMontExpBin_BNU(BNU_CHUNK_T* dataY,
    136                    const BNU_CHUNK_T* dataX, cpSize nsX,
    137                    const BNU_CHUNK_T* dataE, cpSize nbitsE,
    138                          gsModEngine* pMont,
    139                          BNU_CHUNK_T* pBuffer);
    140 cpSize  gsModExpBin_BNU(BNU_CHUNK_T* dataY,
    141                    const BNU_CHUNK_T* dataX, cpSize nsX,
    142                    const BNU_CHUNK_T* dataE, cpSize nbitsE,
    143                          gsModEngine* pMont,
    144                          BNU_CHUNK_T* pBuffer);
    145 
    146 #define gsMontExpBin_BNU_sscm OWNAPI(gsMontExpBin_BNU_sscm)
    147 #define gsModExpBin_BNU_sscm OWNAPI(gsModExpBin_BNU_sscm)
    148 cpSize  gsMontExpBin_BNU_sscm(BNU_CHUNK_T* pY,
    149                         const BNU_CHUNK_T* pX, cpSize nsX,
    150                         const BNU_CHUNK_T* pE, cpSize nbitsE,
    151                               gsModEngine* pMont,
    152                               BNU_CHUNK_T* pBuffer);
    153 cpSize  gsModExpBin_BNU_sscm(BNU_CHUNK_T* pY,
    154                         const BNU_CHUNK_T* pX, cpSize nsX,
    155                         const BNU_CHUNK_T* pE, cpSize nbitsE,
    156                               gsModEngine* pMont,
    157                               BNU_CHUNK_T* pBuffer);
    158 
    159 /*
    160 // "fast" and "safe" fixed-size window montgomery exponentiation
    161 */
    162 #define gsMontExpWin_BNU OWNAPI(gsMontExpWin_BNU_mont)
    163 #define gsModExpWin_BNU OWNAPI(gsModExpWin_BNU)
    164 cpSize  gsMontExpWin_BNU(BNU_CHUNK_T* pY,
    165                         const BNU_CHUNK_T* pX, cpSize nsX,
    166                         const BNU_CHUNK_T* dataE, cpSize nbitsE,
    167                               gsModEngine* pMont,
    168                               BNU_CHUNK_T* pBuffer);
    169 cpSize  gsModExpWin_BNU(BNU_CHUNK_T* pY,
    170                   const BNU_CHUNK_T* pX, cpSize nsX,
    171                   const BNU_CHUNK_T* dataE, cpSize nbitsE,
    172                         gsModEngine* pMont,
    173                         BNU_CHUNK_T* pBuffer);
    174 
    175 #define gsMontExpWin_BNU_sscm OWNAPI(gsMontExpWin_BNU_mont_sscm)
    176 #define gsModExpWin_BNU_sscm OWNAPI(gsModExpWin_BNU_sscm)
    177 cpSize  gsMontExpWin_BNU_sscm(BNU_CHUNK_T* dataY,
    178                              const BNU_CHUNK_T* dataX, cpSize nsX,
    179                              const BNU_CHUNK_T* dataE, cpSize nbitsE,
    180                                    gsModEngine* pMont,
    181                                    BNU_CHUNK_T* pBuffer);
    182 cpSize  gsModExpWin_BNU_sscm(BNU_CHUNK_T* dataY,
    183                         const BNU_CHUNK_T* dataX, cpSize nsX,
    184                         const BNU_CHUNK_T* dataE, cpSize nbitsE,
    185                               gsModEngine* pMont,
    186                               BNU_CHUNK_T* pBuffer);
    187 
    188 #endif /* _CP_NG_MONT_EXP_STUFF_H */
    189