• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //     GF(p) methods
44 //
45 */
46 #include "owndefs.h"
47 #include "owncp.h"
48 
49 #include "pcpbnumisc.h"
50 #include "pcpgfpstuff.h"
51 #include "pcpgfpmethod.h"
52 #include "pcpecprime.h"
53 
54 #if !defined(_PCP_GFPMETHOD_256_H_)
55 #define _PCP_GFPMETHOD_256_H_
56 
57 #if(_IPP32E >= _IPP32E_M7)
58 
59 /* arithmetic over arbitrary 256r-bit modulus */
60 #define      gf256_add  OWNAPI(gf256_add)
61 #define      gf256_sub  OWNAPI(gf256_sub)
62 #define      gf256_neg  OWNAPI(gf256_neg)
63 #define      gf256_mulm OWNAPI(gf256_mulm)
64 #define      gf256_sqrm OWNAPI(gf256_sqrm)
65 #define      gf256_div2 OWNAPI(gf256_div2)
66 
67 BNU_CHUNK_T* gf256_add(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, const BNU_CHUNK_T* pModulus);
68 BNU_CHUNK_T* gf256_sub(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, const BNU_CHUNK_T* pModulus);
69 BNU_CHUNK_T* gf256_neg(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pModulus);
70 BNU_CHUNK_T* gf256_mulm(BNU_CHUNK_T* pR,const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, const BNU_CHUNK_T* pModulus, BNU_CHUNK_T  m0);
71 BNU_CHUNK_T* gf256_sqrm(BNU_CHUNK_T* pR,const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pModulus, BNU_CHUNK_T  m0);
72 BNU_CHUNK_T* gf256_div2(BNU_CHUNK_T* pR,const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pModulus);
73 
74 #define OPERAND_BITSIZE (256)
75 #define LEN_P256        (BITS_BNU_CHUNK(OPERAND_BITSIZE))
76 
p256_add(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,const BNU_CHUNK_T * pB,gsEngine * pGFE)77 static BNU_CHUNK_T* p256_add(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsEngine* pGFE)
78 {
79    return gf256_add(pR, pA, pB, GFP_MODULUS(pGFE));
80 }
81 
p256_sub(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,const BNU_CHUNK_T * pB,gsEngine * pGFE)82 static BNU_CHUNK_T* p256_sub(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsEngine* pGFE)
83 {
84    return gf256_sub(pR, pA, pB, GFP_MODULUS(pGFE));
85 }
86 
p256_neg(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)87 static BNU_CHUNK_T* p256_neg(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
88 {
89    return gf256_neg(pR, pA, GFP_MODULUS(pGFE));
90 }
91 
p256_div_by_2(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)92 static BNU_CHUNK_T* p256_div_by_2(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
93 {
94    return gf256_div2(pR, pA, GFP_MODULUS(pGFE));
95 }
96 
p256_mul_by_2(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)97 static BNU_CHUNK_T* p256_mul_by_2(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
98 {
99    return gf256_add(pR, pA, pA, GFP_MODULUS(pGFE));
100 }
101 
p256_mul_by_3(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)102 static BNU_CHUNK_T* p256_mul_by_3(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
103 {
104    BNU_CHUNK_T tmp[LEN_P256];
105    gf256_add(tmp, pA, pA, GFP_MODULUS(pGFE));
106    return gf256_add(pR, tmp, pA, GFP_MODULUS(pGFE));
107 }
108 
p256_mul_montl(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,const BNU_CHUNK_T * pB,gsEngine * pGFE)109 static BNU_CHUNK_T* p256_mul_montl(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsEngine* pGFE)
110 {
111    return gf256_mulm(pR, pA, pB, GFP_MODULUS(pGFE), GFP_MNT_FACTOR(pGFE));
112 }
113 
p256_sqr_montl(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)114 static BNU_CHUNK_T* p256_sqr_montl(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
115 {
116    return gf256_sqrm(pR, pA, GFP_MODULUS(pGFE), GFP_MNT_FACTOR(pGFE));
117 }
118 
119 
p256_to_mont(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)120 static BNU_CHUNK_T* p256_to_mont(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
121 {
122    return gf256_mulm(pR, pA, GFP_MNT_RR(pGFE), GFP_MODULUS(pGFE), GFP_MNT_FACTOR(pGFE));
123 }
124 
125 static BNU_CHUNK_T one[] = {1,0,0,0};
126 
p256_mont_back(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)127 static BNU_CHUNK_T* p256_mont_back(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
128 {
129    return gf256_mulm(pR, pA, one, GFP_MODULUS(pGFE), GFP_MNT_FACTOR(pGFE));
130 }
131 
132 /* return specific gf p256 arith methods */
gsArithGF_p256(void)133 static gsModMethod* gsArithGF_p256(void)
134 {
135    static gsModMethod m = {
136       p256_to_mont,
137       p256_mont_back,
138       p256_mul_montl,
139       p256_sqr_montl,
140       NULL,
141       p256_add,
142       p256_sub,
143       p256_neg,
144       p256_div_by_2,
145       p256_mul_by_2,
146       p256_mul_by_3,
147    };
148    return &m;
149 }
150 #endif /* _IPP32E >= _IPP32E_M7 */
151 
152 #undef LEN_P256
153 #undef OPERAND_BITSIZE
154 
155 #endif /* #if !defined(_PCP_GFPMETHOD_256_H_) */
156