1 /*******************************************************************************
2 * Copyright 2002-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 // Purpose:
43 // Intel(R) Integrated Performance Primitives. Cryptography Primitives.
44 // Internal Miscellaneous BNU Definitions & Function Prototypes
45 //
46 //
47 */
48
49 #if !defined(_PCP_BNUMISC_H)
50 #define _PCP_BNUMISC_H
51
52 #include "pcpbnuimpl.h"
53
54
55 /* bit operations */
56 #define BITSIZE_BNU(p,ns) ((ns)*BNU_CHUNK_BITS-cpNLZ_BNU((p)[(ns)-1]))
57 #define BIT_BNU(bnu, ns,nbit) ((((nbit)>>BNU_CHUNK_LOG2) < (ns))? ((((bnu))[(nbit)>>BNU_CHUNK_LOG2] >>((nbit)&(BNU_CHUNK_BITS))) &1) : 0)
58
59 #define TST_BIT(bnu, nbit) (((Ipp8u*)(bnu))[(nbit)/8] & ((1<<((nbit)%8)) &0xFF))
60 #define SET_BIT(bnu, nbit) (((Ipp8u*)(bnu))[(nbit)/8] |= ((1<<((nbit)%8)) &0xFF))
61 #define CLR_BIT(bnu, nbit) (((Ipp8u*)(bnu))[(nbit)/8] &=~((1<<((nbit)%8)) &0xFF))
62
63 /* convert bitsize nbits into the number of BNU_CHUNK_T */
64 #define BITS_BNU_CHUNK(nbits) (((nbits)+BNU_CHUNK_BITS-1)/BNU_CHUNK_BITS)
65
66 /* mask for top BNU_CHUNK_T */
67 #define MASK_BNU_CHUNK(nbits) ((BNU_CHUNK_T)(-1) >>((BNU_CHUNK_BITS- ((nbits)&(BNU_CHUNK_BITS-1))) &(BNU_CHUNK_BITS-1)))
68
69 /* copy BNU content */
70 #define COPY_BNU(dst, src, len) \
71 { \
72 cpSize __idx; \
73 for(__idx=0; __idx<(len); __idx++) (dst)[__idx] = (src)[__idx]; \
74 }
75
76 /* expand by zeros */
77 #define ZEXPAND_BNU(srcdst,srcLen, dstLen) \
78 { \
79 cpSize __idx; \
80 for(__idx=(srcLen); __idx<(dstLen); __idx++) (srcdst)[__idx] = 0; \
81 }
82
83 /* copy and expand by zeros */
84 #define ZEXPAND_COPY_BNU(dst,dstLen, src,srcLen) \
85 { \
86 cpSize __idx; \
87 for(__idx=0; __idx<(srcLen); __idx++) (dst)[__idx] = (src)[__idx]; \
88 for(; __idx<(dstLen); __idx++) (dst)[__idx] = 0; \
89 }
90
91 /* fix actual length */
92 #define FIX_BNU(src,srcLen) \
93 for(; ((srcLen)>1) && (0==(src)[(srcLen)-1]); (srcLen)--) {}
94
95
96 /* copy and set */
cpCpy_BNU(BNU_CHUNK_T * pDst,const BNU_CHUNK_T * pSrc,cpSize ns)97 __INLINE void cpCpy_BNU(BNU_CHUNK_T* pDst, const BNU_CHUNK_T* pSrc, cpSize ns)
98 { COPY_BNU(pDst, pSrc, ns); }
99
cpSet_BNU(BNU_CHUNK_T * pDst,cpSize ns,BNU_CHUNK_T val)100 __INLINE void cpSet_BNU(BNU_CHUNK_T* pDst, cpSize ns, BNU_CHUNK_T val)
101 {
102 ZEXPAND_BNU(pDst, 0, ns);
103 pDst[0] = val;
104 }
105
106 /* fix up */
107
108 /* Name: cpFix_BNU
109 //
110 // Purpose: fix up BigNums.
111 //
112 // Returns:
113 // fixed nsA
114 //
115 // Parameters:
116 // pA BigNum ctx
117 // nsA Size of pA
118 //
119 */
120
cpFix_BNU(const BNU_CHUNK_T * pA,int nsA)121 __INLINE int cpFix_BNU(const BNU_CHUNK_T* pA, int nsA)
122 {
123 FIX_BNU(pA, nsA);
124 return nsA;
125 }
126
127 /* Name: cpCmp_BNU
128 //
129 // Purpose: Compare two BigNums.
130 //
131 // Returns:
132 // negative, if A < B
133 // 0, if A = B
134 // positive, if A > B
135 //
136 // Parameters:
137 // pA BigNum ctx
138 // nsA Size of pA
139 // pB BigNum ctx
140 // nsB Size of pB
141 //
142 */
cpCmp_BNU(const BNU_CHUNK_T * pA,cpSize nsA,const BNU_CHUNK_T * pB,cpSize nsB)143 __INLINE int cpCmp_BNU(const BNU_CHUNK_T* pA, cpSize nsA, const BNU_CHUNK_T* pB, cpSize nsB)
144 {
145 if(nsA!=nsB)
146 return nsA>nsB? 1 : -1;
147 else {
148 for(; nsA>0; nsA--) {
149 if(pA[nsA-1] > pB[nsA-1])
150 return 1;
151 else if(pA[nsA-1] < pB[nsA-1])
152 return -1;
153 }
154 return 0;
155 }
156 }
157
158 /* Name: cpEqu_BNU_CHUNK
159 //
160 // Purpose: Compare two BNU_CHUNKs.
161 //
162 // Returns:
163 // positive, if A = b
164 // 0 , if A != b
165 //
166 // Parameters:
167 // pA BigNum ctx
168 // nsA Size of pA
169 // b BNU_CHUNK_T to compare
170 //
171 */
172
cpEqu_BNU_CHUNK(const BNU_CHUNK_T * pA,cpSize nsA,BNU_CHUNK_T b)173 __INLINE int cpEqu_BNU_CHUNK(const BNU_CHUNK_T* pA, cpSize nsA, BNU_CHUNK_T b)
174 {
175 return (pA[0]==b && 1==cpFix_BNU(pA, nsA));
176 }
177
178 /*
179 // test
180 //
181 // returns
182 // 0, if A = 0
183 // >0, if A > 0
184 // <0, looks like impossible (or error) case
185 */
cpTst_BNU(const BNU_CHUNK_T * pA,int nsA)186 __INLINE int cpTst_BNU(const BNU_CHUNK_T* pA, int nsA)
187 {
188 for(; (nsA>0) && (0==pA[nsA-1]); nsA--) ;
189 return nsA;
190 }
191
192 /* number of leading/trailing zeros */
193 #define cpNLZ_BNU OWNAPI(cpNLZ_BNU)
194 cpSize cpNLZ_BNU(BNU_CHUNK_T x);
195
196 #define cpNTZ_BNU OWNAPI(cpNTZ_BNU)
197 cpSize cpNTZ_BNU(BNU_CHUNK_T x);
198
199 /* logical shift left/right */
200 #define cpLSR_BNU OWNAPI(cpLSR_BNU)
201 int cpLSR_BNU(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, cpSize nsA, cpSize nBits);
202
203 /* most significant BNU bit */
204 #define cpMSBit_BNU OWNAPI(cpMSBit_BNU)
205 int cpMSBit_BNU(const BNU_CHUNK_T* pA, cpSize nsA);
206
207 /* BNU <-> hex-string conversion */
208 #define cpToOctStr_BNU OWNAPI(cpToOctStr_BNU)
209 int cpToOctStr_BNU(Ipp8u* pStr, cpSize strLen, const BNU_CHUNK_T* pA, cpSize nsA);
210 #define cpFromOctStr_BNU OWNAPI(cpFromOctStr_BNU)
211 int cpFromOctStr_BNU(BNU_CHUNK_T* pA, const Ipp8u* pStr, cpSize strLen);
212
213 #endif /* _PCP_BNUMISC_H */
214