Home | History | Annotate | Download | only in ippcp
      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 //        ippsGFpECSignNR()
     46 //
     47 */
     48 
     49 #include "owndefs.h"
     50 #include "owncp.h"
     51 #include "pcpeccp.h"
     52 
     53 /*F*
     54 //    Name: ippsGFpECSignNR
     55 //
     56 // Purpose: NR Signature Generation.
     57 //
     58 // Returns:                   Reason:
     59 //    ippStsNullPtrErr           NULL == pEC
     60 //                               NULL == pMsgDigest
     61 //                               NULL == pRegPrivate
     62 //                               NULL == pEphPrivate
     63 //                               NULL == pSignR
     64 //                               NULL == pSignS
     65 //                               NULL == pScratchBuffer
     66 //
     67 //    ippStsContextMatchErr      illegal pEC->idCtx
     68 //                               pEC->subgroup == NULL
     69 //                               illegal pMsgDigest->idCtx
     70 //                               illegal pRegPrivate->idCtx
     71 //                               illegal pEphPrivate->idCtx
     72 //                               illegal pSignR->idCtx
     73 //                               illegal pSignS->idCtx
     74 //
     75 //    ippStsIvalidPrivateKey     0 >= RegPrivate
     76 //                               RegPrivate >= order
     77 //
     78 //                               0 >= EphPrivate
     79 //                               EphPrivate >= order
     80 //
     81 //    ippStsMessageErr           pMsgDigest < 0
     82 //                               pMsgDigest >= order
     83 //
     84 //    ippStsRangeErr             not enough room for:
     85 //                               signC
     86 //                               signD
     87 //
     88 //    ippStsEphemeralKeyErr      (0==signR) || (0==signS)
     89 //
     90 //    ippStsNotSupportedModeErr  1<GFP_EXTDEGREE(pGFE)
     91 //
     92 //    ippStsNoErr                no errors
     93 //
     94 // Parameters:
     95 //    pMsgDigest     pointer to the message representative to be signed
     96 //    pRegPrivate    pointer to the regular private key
     97 //    pEphPrivate    pointer to the ephemeral private key
     98 //    pSignR,pSignS  pointer to the signature
     99 //    pEC            pointer to the EC context
    100 //    pScratchBuffer pointer to buffer (1 mul_point operation)
    101 //
    102 *F*/
    103 IPPFUN(IppStatus, ippsGFpECSignNR,(const IppsBigNumState* pMsgDigest,
    104                                    const IppsBigNumState* pRegPrivate,
    105                                    const IppsBigNumState* pEphPrivate,
    106                                    IppsBigNumState* pSignR, IppsBigNumState* pSignS,
    107                                    IppsGFpECState* pEC,
    108                                    Ipp8u* pScratchBuffer))
    109 {
    110    IppsGFpState*  pGF;
    111    gsModEngine* pMontP;
    112 
    113    /* EC context and buffer */
    114    IPP_BAD_PTR2_RET(pEC, pScratchBuffer);
    115    pEC = (IppsGFpECState*)( IPP_ALIGNED_PTR(pEC, ECGFP_ALIGNMENT) );
    116    IPP_BADARG_RET(!ECP_TEST_ID(pEC), ippStsContextMatchErr);
    117    IPP_BADARG_RET(!ECP_SUBGROUP(pEC), ippStsContextMatchErr);
    118 
    119    pGF = ECP_GFP(pEC);
    120    pMontP = GFP_PMA(pGF);
    121    IPP_BADARG_RET(1<GFP_EXTDEGREE(pMontP), ippStsNotSupportedModeErr);
    122 
    123    /* test message representative */
    124    IPP_BAD_PTR1_RET(pMsgDigest);
    125    pMsgDigest = (IppsBigNumState*)( IPP_ALIGNED_PTR(pMsgDigest, ALIGN_VAL) );
    126    IPP_BADARG_RET(!BN_VALID_ID(pMsgDigest), ippStsContextMatchErr);
    127    IPP_BADARG_RET(BN_NEGATIVE(pMsgDigest), ippStsMessageErr);
    128 
    129    /* test signature */
    130    IPP_BAD_PTR2_RET(pSignR, pSignS);
    131    pSignR = (IppsBigNumState*)( IPP_ALIGNED_PTR(pSignR, BN_ALIGNMENT) );
    132    pSignS = (IppsBigNumState*)( IPP_ALIGNED_PTR(pSignS, BN_ALIGNMENT) );
    133    IPP_BADARG_RET(!BN_VALID_ID(pSignR), ippStsContextMatchErr);
    134    IPP_BADARG_RET(!BN_VALID_ID(pSignS), ippStsContextMatchErr);
    135    IPP_BADARG_RET((BN_ROOM(pSignR)*BITSIZE(BNU_CHUNK_T)<ECP_ORDBITSIZE(pEC)), ippStsRangeErr);
    136    IPP_BADARG_RET((BN_ROOM(pSignS)*BITSIZE(BNU_CHUNK_T)<ECP_ORDBITSIZE(pEC)), ippStsRangeErr);
    137 
    138    /* test private keys */
    139    IPP_BAD_PTR2_RET(pRegPrivate, pEphPrivate);
    140 
    141    pRegPrivate = (IppsBigNumState*)( IPP_ALIGNED_PTR(pRegPrivate, ALIGN_VAL) );
    142    IPP_BADARG_RET(!BN_VALID_ID(pRegPrivate), ippStsContextMatchErr);
    143    IPP_BADARG_RET(BN_NEGATIVE(pRegPrivate), ippStsIvalidPrivateKey);
    144 
    145    pEphPrivate = (IppsBigNumState*)( IPP_ALIGNED_PTR(pEphPrivate, ALIGN_VAL) );
    146    IPP_BADARG_RET(!BN_VALID_ID(pEphPrivate), ippStsContextMatchErr);
    147    IPP_BADARG_RET(BN_NEGATIVE(pEphPrivate), ippStsEphemeralKeyErr);
    148 
    149    {
    150       gsModEngine* pMontR = ECP_MONT_R(pEC);
    151       BNU_CHUNK_T* pOrder = MOD_MODULUS(pMontR);
    152       int ordLen = MOD_LEN(pMontR);
    153 
    154       BNU_CHUNK_T* dataC = BN_NUMBER(pSignR);
    155       BNU_CHUNK_T* dataD = BN_NUMBER(pSignS);
    156       BNU_CHUNK_T* buffF = BN_BUFFER(pSignR);
    157       BNU_CHUNK_T* buffT = BN_BUFFER(pSignS);
    158 
    159       BNU_CHUNK_T* pPriData = BN_NUMBER(pRegPrivate);
    160       int priLen = BN_SIZE(pRegPrivate);
    161 
    162       BNU_CHUNK_T* pEphData = BN_NUMBER(pEphPrivate);
    163       int ephLen = BN_SIZE(pEphPrivate);
    164 
    165       BNU_CHUNK_T* pMsgData = BN_NUMBER(pMsgDigest);
    166       int msgLen = BN_SIZE(pMsgDigest);
    167 
    168       /* test value of private keys: 0 < regPrivate<order, 0 < ephPrivate < order */
    169       IPP_BADARG_RET(cpEqu_BNU_CHUNK(pPriData, priLen, 0) ||
    170                   0<=cpCmp_BNU(pPriData, priLen, pOrder, ordLen), ippStsIvalidPrivateKey);
    171       IPP_BADARG_RET(cpEqu_BNU_CHUNK(pEphData, ephLen, 0) ||
    172                   0<=cpCmp_BNU(pEphData, ephLen, pOrder, ordLen), ippStsEphemeralKeyErr);
    173 
    174       /* test mesage: msg < order */
    175       IPP_BADARG_RET(0<=cpCmp_BNU(pMsgData, msgLen, pOrder, ordLen), ippStsMessageErr);
    176 
    177       {
    178          int elmLen = GFP_FELEN(pMontP);
    179          int ns;
    180 
    181          /* compute ephemeral public key */
    182          IppsGFpECPoint ephPublic;
    183          cpEcGFpInitPoint(&ephPublic, cpEcGFpGetPool(1, pEC), 0, pEC);
    184          gfec_MulBasePoint(&ephPublic,
    185                            BN_NUMBER(pEphPrivate), BN_SIZE(pEphPrivate),
    186                            pEC, pScratchBuffer);
    187 
    188          /* x = (ephPublic.x) mod order */
    189          {
    190             BNU_CHUNK_T* buffer = gsModPoolAlloc(pMontP, 1);
    191             gfec_GetPoint(buffer, NULL, &ephPublic, pEC);
    192             GFP_METHOD(pMontP)->decode(buffer, buffer, pMontP);
    193             ns = cpMod_BNU(buffer, elmLen, pOrder, ordLen);
    194             cpGFpElementCopyPadd(dataC, ordLen, buffer, ns);
    195             gsModPoolFree(pMontP, 1);
    196          }
    197          cpEcGFpReleasePool(1, pEC);
    198 
    199          /* C = (ephPublic.x + msg) mod order */
    200          ZEXPAND_COPY_BNU(buffF, ordLen, pMsgData, msgLen);
    201          cpModAdd_BNU(dataC, dataC, buffF, pOrder, ordLen, dataD);
    202 
    203          if(!GFP_IS_ZERO(dataC, ordLen)) {
    204 
    205             /* signS = (eph_private - private*signR) (mod order) */
    206             ZEXPAND_COPY_BNU(dataD, ordLen, pPriData, priLen);
    207             GFP_METHOD(pMontR)->encode(dataD, dataD, pMontR);
    208             GFP_METHOD(pMontR)->mul(dataD, dataD, dataC, pMontR);
    209             ZEXPAND_COPY_BNU(buffF, ordLen, pEphData, ephLen);
    210             cpModSub_BNU(dataD, buffF, dataD, pOrder, ordLen, buffT);
    211 
    212             /* signR */
    213             ns = ordLen;
    214             FIX_BNU(dataC, ns);
    215             BN_SIGN(pSignR) = ippBigNumPOS;
    216             BN_SIZE(pSignR) = ns;
    217             /* signS */
    218             ns = ordLen;
    219             FIX_BNU(dataD, ns);
    220             BN_SIGN(pSignS) = ippBigNumPOS;
    221             BN_SIZE(pSignS) = ns;
    222 
    223             return ippStsNoErr;
    224          }
    225 
    226          return ippStsEphemeralKeyErr;
    227       }
    228    }
    229 }
    230