Home | History | Annotate | Download | only in ippcp
      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 //     Internal ECC (prime) basic Definitions & Function Prototypes
     46 //
     47 //
     48 */
     49 
     50 #if !defined(_NEW_PCP_ECCP_H)
     51 #define _NEW_PCP_ECCP_H
     52 
     53 #include "pcpgfpecstuff.h"
     54 
     55 
     56 __INLINE IppsBigNumState* cpConstructBN(IppsBigNumState* pBN, cpSize len, BNU_CHUNK_T* pData, BNU_CHUNK_T* pBuffer)
     57 {
     58    BN_ID(pBN)   = idCtxBigNum;
     59    BN_SIGN(pBN) = ippBigNumPOS;
     60    BN_SIZE(pBN) = len;
     61    BN_ROOM(pBN) = len;
     62    BN_NUMBER(pBN) = pData;
     63    BN_BUFFER(pBN) = pBuffer;
     64    return pBN;
     65 }
     66 
     67 /* set EC parameters */
     68 #define ECCPSetDP OWNAPI(ECCPSetDP)
     69 IppStatus ECCPSetDP(const IppsGFpMethod* method,
     70                         int pLen, const BNU_CHUNK_T* pP,
     71                         int aLen, const BNU_CHUNK_T* pA,
     72                         int bLen, const BNU_CHUNK_T* pB,
     73                         int xLen, const BNU_CHUNK_T* pX,
     74                         int yLen, const BNU_CHUNK_T* pY,
     75                         int rLen, const BNU_CHUNK_T* pR,
     76                         BNU_CHUNK_T h,
     77                         IppsGFpECState* pEC);
     78 
     79 /*
     80 // Recommended (Standard) Domain Parameters
     81 */
     82 extern const BNU_CHUNK_T secp112r1_p[]; // (2^128 -3)/76439
     83 extern const BNU_CHUNK_T secp112r1_a[];
     84 extern const BNU_CHUNK_T secp112r1_b[];
     85 extern const BNU_CHUNK_T secp112r1_gx[];
     86 extern const BNU_CHUNK_T secp112r1_gy[];
     87 extern const BNU_CHUNK_T secp112r1_r[];
     88 extern       BNU_CHUNK_T secp112r1_h;
     89 
     90 extern const BNU_CHUNK_T secp112r2_p[]; // (2^128 -3)/76439
     91 extern const BNU_CHUNK_T secp112r2_a[];
     92 extern const BNU_CHUNK_T secp112r2_b[];
     93 extern const BNU_CHUNK_T secp112r2_gx[];
     94 extern const BNU_CHUNK_T secp112r2_gy[];
     95 extern const BNU_CHUNK_T secp112r2_r[];
     96 extern       BNU_CHUNK_T secp112r2_h;
     97 
     98 extern const BNU_CHUNK_T secp128r1_p[]; // 2^128 -2^97 -1
     99 extern const BNU_CHUNK_T secp128r1_a[];
    100 extern const BNU_CHUNK_T secp128r1_b[];
    101 extern const BNU_CHUNK_T secp128r1_gx[];
    102 extern const BNU_CHUNK_T secp128r1_gy[];
    103 extern const BNU_CHUNK_T secp128r1_r[];
    104 extern       BNU_CHUNK_T secp128r1_h;
    105 
    106 extern const BNU_CHUNK_T* secp128_mx[];
    107 
    108 extern const BNU_CHUNK_T secp128r2_p[]; // 2^128 -2^97 -1
    109 extern const BNU_CHUNK_T secp128r2_a[];
    110 extern const BNU_CHUNK_T secp128r2_b[];
    111 extern const BNU_CHUNK_T secp128r2_gx[];
    112 extern const BNU_CHUNK_T secp128r2_gy[];
    113 extern const BNU_CHUNK_T secp128r2_r[];
    114 extern       BNU_CHUNK_T secp128r2_h;
    115 
    116 extern const BNU_CHUNK_T secp160r1_p[]; // 2^160 -2^31 -1
    117 extern const BNU_CHUNK_T secp160r1_a[];
    118 extern const BNU_CHUNK_T secp160r1_b[];
    119 extern const BNU_CHUNK_T secp160r1_gx[];
    120 extern const BNU_CHUNK_T secp160r1_gy[];
    121 extern const BNU_CHUNK_T secp160r1_r[];
    122 extern       BNU_CHUNK_T secp160r1_h;
    123 
    124 extern const BNU_CHUNK_T secp160r2_p[]; // 2^160 -2^32 -2^14 -2^12 -2^9 -2^8 -2^7 -2^2 -1
    125 extern const BNU_CHUNK_T secp160r2_a[];
    126 extern const BNU_CHUNK_T secp160r2_b[];
    127 extern const BNU_CHUNK_T secp160r2_gx[];
    128 extern const BNU_CHUNK_T secp160r2_gy[];
    129 extern const BNU_CHUNK_T secp160r2_r[];
    130 extern       BNU_CHUNK_T secp160r2_h;
    131 
    132 extern const BNU_CHUNK_T secp192r1_p[]; // 2^192 -2^64 -1
    133 extern const BNU_CHUNK_T secp192r1_a[];
    134 extern const BNU_CHUNK_T secp192r1_b[];
    135 extern const BNU_CHUNK_T secp192r1_gx[];
    136 extern const BNU_CHUNK_T secp192r1_gy[];
    137 extern const BNU_CHUNK_T secp192r1_r[];
    138 extern       BNU_CHUNK_T secp192r1_h;
    139 
    140 extern const BNU_CHUNK_T secp224r1_p[]; // 2^224 -2^96 +1
    141 extern const BNU_CHUNK_T secp224r1_a[];
    142 extern const BNU_CHUNK_T secp224r1_b[];
    143 extern const BNU_CHUNK_T secp224r1_gx[];
    144 extern const BNU_CHUNK_T secp224r1_gy[];
    145 extern const BNU_CHUNK_T secp224r1_r[];
    146 extern       BNU_CHUNK_T secp224r1_h;
    147 
    148 extern const BNU_CHUNK_T secp256r1_p[]; // 2^256 -2^224 +2^192 +2^96 -1
    149 extern const BNU_CHUNK_T secp256r1_a[];
    150 extern const BNU_CHUNK_T secp256r1_b[];
    151 extern const BNU_CHUNK_T secp256r1_gx[];
    152 extern const BNU_CHUNK_T secp256r1_gy[];
    153 extern const BNU_CHUNK_T secp256r1_r[];
    154 extern       BNU_CHUNK_T secp256r1_h;
    155 
    156 extern const BNU_CHUNK_T secp384r1_p[]; // 2^384 -2^128 -2^96 +2^32 -1
    157 extern const BNU_CHUNK_T secp384r1_a[];
    158 extern const BNU_CHUNK_T secp384r1_b[];
    159 extern const BNU_CHUNK_T secp384r1_gx[];
    160 extern const BNU_CHUNK_T secp384r1_gy[];
    161 extern const BNU_CHUNK_T secp384r1_r[];
    162 extern       BNU_CHUNK_T secp384r1_h;
    163 
    164 extern const BNU_CHUNK_T secp521r1_p[]; // 2^521 -1
    165 extern const BNU_CHUNK_T secp521r1_a[];
    166 extern const BNU_CHUNK_T secp521r1_b[];
    167 extern const BNU_CHUNK_T secp521r1_gx[];
    168 extern const BNU_CHUNK_T secp521r1_gy[];
    169 extern const BNU_CHUNK_T secp521r1_r[];
    170 extern       BNU_CHUNK_T secp521r1_h;
    171 
    172 extern const BNU_CHUNK_T tpmBN_p256p_p[]; // TPM BN_P256
    173 extern const BNU_CHUNK_T tpmBN_p256p_a[];
    174 extern const BNU_CHUNK_T tpmBN_p256p_b[];
    175 extern const BNU_CHUNK_T tpmBN_p256p_gx[];
    176 extern const BNU_CHUNK_T tpmBN_p256p_gy[];
    177 extern const BNU_CHUNK_T tpmBN_p256p_r[];
    178 extern       BNU_CHUNK_T tpmBN_p256p_h;
    179 
    180 extern const BNU_CHUNK_T tpmSM2_p256_p[]; // TPM SM2_P256
    181 extern const BNU_CHUNK_T tpmSM2_p256_a[];
    182 extern const BNU_CHUNK_T tpmSM2_p256_b[];
    183 extern const BNU_CHUNK_T tpmSM2_p256_gx[];
    184 extern const BNU_CHUNK_T tpmSM2_p256_gy[];
    185 extern const BNU_CHUNK_T tpmSM2_p256_r[];
    186 extern       BNU_CHUNK_T tpmSM2_p256_h;
    187 
    188 extern const BNU_CHUNK_T* tpmSM2_p256_p_mx[];
    189 
    190 /* half of some std  modulus */
    191 extern const BNU_CHUNK_T h_secp128r1_p[];
    192 extern const BNU_CHUNK_T h_secp192r1_p[];
    193 extern const BNU_CHUNK_T h_secp224r1_p[];
    194 extern const BNU_CHUNK_T h_secp256r1_p[];
    195 extern const BNU_CHUNK_T h_secp384r1_p[];
    196 extern const BNU_CHUNK_T h_secp521r1_p[];
    197 extern const BNU_CHUNK_T h_tpmSM2_p256_p[];
    198 
    199 __INLINE BNU_CHUNK_T* cpModAdd_BNU(BNU_CHUNK_T* pR,
    200                              const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB,
    201                              const BNU_CHUNK_T* pM, int ns,
    202                                    BNU_CHUNK_T* pBuffer)
    203 {
    204    BNU_CHUNK_T e = cpAdd_BNU(pR, pA, pB, ns);
    205    e -= cpSub_BNU(pBuffer, pR, pM, ns);
    206    MASKED_COPY_BNU(pR, e, pR, pBuffer, ns);
    207    return pR;
    208 }
    209 
    210 __INLINE BNU_CHUNK_T* cpModSub_BNU(BNU_CHUNK_T* pR,
    211                              const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB,
    212                              const BNU_CHUNK_T* pM, int ns,
    213                                    BNU_CHUNK_T* pBuffer)
    214 {
    215    BNU_CHUNK_T e = cpSub_BNU(pR, pA, pB, ns);
    216    cpAdd_BNU(pBuffer, pR, pM, ns);
    217    MASKED_COPY_BNU(pR, (0-e), pBuffer, pR, ns);
    218    return pR;
    219 }
    220 
    221 #endif /* _NEW_PCP_ECCP_H */
    222