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 // Intel(R) Integrated Performance Primitives 43 // Cryptographic Primitives (ippcp) 44 // 45 // 46 // 47 */ 48 49 #if !defined(_CP_BN_H) 50 #define _CP_BN_H 51 52 #include "pcpbnuimpl.h" 53 #include "pcpbnuarith.h" 54 #include "pcpbnumisc.h" 55 #include "pcpbnu32arith.h" 56 #include "pcpbnu32misc.h" 57 58 /* 59 // Big Number context 60 */ 61 struct _cpBigNum 62 { 63 IppCtxId idCtx; /* BigNum ctx id */ 64 IppsBigNumSGN sgn; /* sign */ 65 cpSize size; /* BigNum size (BNU_CHUNK_T) */ 66 cpSize room; /* BigNum max size (BNU_CHUNK_T) */ 67 BNU_CHUNK_T* number; /* BigNum value */ 68 BNU_CHUNK_T* buffer; /* temporary buffer */ 69 }; 70 71 /* BN accessory macros */ 72 #define BN_ID(pBN) ((pBN)->idCtx) 73 #define BN_SIGN(pBN) ((pBN)->sgn) 74 #define BN_POSITIVE(pBN) (BN_SIGN(pBN)==ippBigNumPOS) 75 #define BN_NEGATIVE(pBN) (BN_SIGN(pBN)==ippBigNumNEG) 76 #define BN_NUMBER(pBN) ((pBN)->number) 77 #define BN_BUFFER(pBN) ((pBN)->buffer) 78 #define BN_ROOM(pBN) ((pBN)->room) 79 #define BN_SIZE(pBN) ((pBN)->size) 80 #define BN_SIZE32(pBN) ((pBN)->size*(sizeof(BNU_CHUNK_T)/sizeof(Ipp32u))) 81 //#define BN_SIZE32(pBN) (BITS2WORD32_SIZE( BITSIZE_BNU(BN_NUMBER((pBN)),BN_SIZE((pBN))))) 82 83 #define BN_VALID_ID(pBN) (BN_ID((pBN))==idCtxBigNum) 84 85 #define INVERSE_SIGN(s) (((s)==ippBigNumPOS)? ippBigNumNEG : ippBigNumPOS) 86 87 #define BN_ALIGNMENT ((int)sizeof(void*)) 88 89 90 /* pack-unpack context */ 91 #define cpPackBigNumCtx OWNAPI(cpPackBigNumCtx) 92 void cpPackBigNumCtx(const IppsBigNumState* pBN, Ipp8u* pBuffer); 93 #define cpUnpackBigNumCtx OWNAPI(cpUnpackBigNumCtx) 94 void cpUnpackBigNumCtx(const Ipp8u* pBuffer, IppsBigNumState* pBN); 95 96 /* copy BN */ 97 __INLINE IppsBigNumState* cpBN_copy(IppsBigNumState* pDst, const IppsBigNumState* pSrc) 98 { 99 BN_SIGN(pDst) = BN_SIGN(pSrc); 100 BN_SIZE(pDst) = BN_SIZE(pSrc); 101 ZEXPAND_COPY_BNU(BN_NUMBER(pDst), BN_ROOM(pDst), BN_NUMBER(pSrc), BN_SIZE(pSrc)); 102 return pDst; 103 } 104 /* set BN to zero */ 105 __INLINE IppsBigNumState* cpBN_zero(IppsBigNumState* pBN) 106 { 107 BN_SIGN(pBN) = ippBigNumPOS; 108 BN_SIZE(pBN) = 1; 109 ZEXPAND_BNU(BN_NUMBER(pBN),0, (int)BN_ROOM(pBN)); 110 return pBN; 111 } 112 /* fixup BN */ 113 __INLINE IppsBigNumState* cpBN_fix(IppsBigNumState* pBN) 114 { 115 cpSize len = BN_SIZE(pBN); 116 FIX_BNU(BN_NUMBER(pBN), len); 117 BN_SIZE(pBN) = len; 118 return pBN; 119 } 120 /* set BN to chunk */ 121 __INLINE IppsBigNumState* cpBN_chunk(IppsBigNumState* pBN, BNU_CHUNK_T a) 122 { 123 BN_SIGN(pBN) = ippBigNumPOS; 124 BN_SIZE(pBN) = 1; 125 ZEXPAND_BNU(BN_NUMBER(pBN),0, (int)BN_ROOM(pBN)); 126 BN_NUMBER(pBN)[0] = a; 127 return pBN; 128 } 129 /* set BN to 2^m */ 130 __INLINE IppsBigNumState* cpBN_power2(IppsBigNumState* pBN, int power) 131 { 132 cpSize size = BITS_BNU_CHUNK(power+1); 133 if(BN_ROOM(pBN) >= size) { 134 BN_SIGN(pBN) = ippBigNumPOS; 135 BN_SIZE(pBN) = size; 136 ZEXPAND_BNU(BN_NUMBER(pBN),0, BN_ROOM(pBN)); 137 SET_BIT(BN_NUMBER(pBN), power); 138 return pBN; 139 } 140 else return NULL; 141 } 142 143 /* bitsize of BN */ 144 __INLINE int cpBN_bitsize(const IppsBigNumState* pA) 145 { 146 int bitsize = BITSIZE_BNU(BN_NUMBER(pA), BN_SIZE(pA)); 147 return bitsize; 148 } 149 150 /* returns -1/0/+1 depemding on A~B comparison */ 151 __INLINE int cpBN_cmp(const IppsBigNumState* pA, const IppsBigNumState* pB) 152 { 153 IppsBigNumSGN signA = BN_SIGN(pA); 154 IppsBigNumSGN signB = BN_SIGN(pB); 155 156 if(signA==signB) { 157 int result = cpCmp_BNU(BN_NUMBER(pA), BN_SIZE(pA), BN_NUMBER(pB), BN_SIZE(pB)); 158 return (ippBigNumPOS==signA)? result : -result; 159 } 160 return (ippBigNumPOS==signA)? 1 : -1; 161 } 162 163 /* returns -1/0/+1 depemding on A comparison 0</==0/>0 */ 164 __INLINE int cpBN_tst(const IppsBigNumState* pA) 165 { 166 if(1==BN_SIZE(pA) && 0==BN_NUMBER(pA)[0]) 167 return 0; 168 else 169 return BN_POSITIVE(pA)? 1 : -1; 170 } 171 172 173 // some addtition functions 174 __INLINE int IsZero_BN(const IppsBigNumState* pA) 175 { 176 return ( BN_SIZE(pA)==1 ) && ( BN_NUMBER(pA)[0]==0 ); 177 } 178 __INLINE int IsOdd_BN(const IppsBigNumState* pA) 179 { 180 return BN_NUMBER(pA)[0] & 1; 181 } 182 183 __INLINE IppsBigNumState* BN_Word(IppsBigNumState* pBN, BNU_CHUNK_T w) 184 { 185 BN_SIGN(pBN) = ippBigNumPOS; 186 BN_SIZE(pBN) = 1; 187 ZEXPAND_BNU(BN_NUMBER(pBN),0, BN_ROOM(pBN)); 188 BN_NUMBER(pBN)[0] = w; 189 return pBN; 190 } 191 __INLINE IppsBigNumState* BN_Set(const BNU_CHUNK_T* pData, cpSize len, IppsBigNumState* pBN) 192 { 193 BN_SIGN(pBN) = ippBigNumPOS; 194 BN_SIZE(pBN) = len; 195 ZEXPAND_COPY_BNU(BN_NUMBER(pBN), BN_ROOM(pBN), pData, len); 196 return pBN; 197 } 198 __INLINE IppsBigNumState* BN_Make(BNU_CHUNK_T* pData, BNU_CHUNK_T* pBuffer, cpSize len, IppsBigNumState* pBN) 199 { 200 BN_ID(pBN) = idCtxBigNum; 201 BN_SIGN(pBN) = ippBigNumPOS; 202 BN_SIZE(pBN) = 1; 203 BN_ROOM(pBN) = len; 204 BN_NUMBER(pBN) = pData; 205 BN_BUFFER(pBN) = pBuffer; 206 return pBN; 207 } 208 209 210 211 /* 212 // fixed single chunk BN 213 */ 214 typedef struct _ippcpBigNumChunk { 215 IppsBigNumState bn; 216 BNU_CHUNK_T value; 217 BNU_CHUNK_T temporary; 218 } IppsBigNumStateChunk; 219 220 /* reference to BN(1) and BN(2) */ 221 #define cpBN_OneRef OWNAPI(cpBN_OneRef) 222 IppsBigNumState* cpBN_OneRef(void); 223 #define cpBN_TwoRef OWNAPI(cpBN_TwoRef) 224 IppsBigNumState* cpBN_TwoRef(void); 225 #define cpBN_ThreeRef OWNAPI(cpBN_ThreeRef) 226 IppsBigNumState* cpBN_ThreeRef(void); 227 228 #define BN_ONE_REF() cpBN_OneRef() 229 #define BN_TWO_REF() cpBN_TwoRef() 230 #define BN_THREE_REF() cpBN_ThreeRef() 231 232 #endif /* _CP_BN_H */ 233