1 /*******************************************************************************
2 * Copyright 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 // Constant time Mask operations
46 //
47 //
48 */
49
50 #if !defined(_PCP_MASK_CT_H)
51 #define _PCP_MASK_CT_H
52
53 #include "owncp.h"
54 #include "pcpbnuimpl.h"
55
56 /*
57 // The following functions test particular conditions
58 // and returns either 0 or 0xffffffff.
59 //
60 // The result is suitable for boolean and masked operations.
61 //
62 // Inspite of operation below are using BNU_CHUNK_T operand(s) it can be applied to Ipp32u, Ipp32s, Ipp16u, Ipp16s, Ipp8u and Ipp8s too.
63 // For example, if
64 // Ipp32u uns_int;
65 // Ipp32s sgn_int;
66 // Ipp8u uns_char;
67 // Ipp8s sgn_char;
68 // then
69 // cpIs_msb_ct((Ipp32s)uns_int) tests 31 bit of uns_int
70 // cpIs_msb_ct( sgn_int) tests 31 bit of sgn_int
71 // cpIs_msb_ct((Ipp8u)uns_char) tests 7 bit of uns_char
72 // cpIs_msb_ct( sgn_char) tests 7 bit of sgn_char
73 */
74
75 /* tests if MSB(a)==1 */
cpIsMsb_ct(BNU_CHUNK_T a)76 __INLINE BNU_CHUNK_T cpIsMsb_ct(BNU_CHUNK_T a)
77 {
78 return (BNU_CHUNK_T)0 - (a >> (sizeof(a) * 8 - 1));
79 }
80
81 /* tests if LSB(a)==1 */
cpIsLsb_ct(BNU_CHUNK_T a)82 __INLINE BNU_CHUNK_T cpIsLsb_ct(BNU_CHUNK_T a)
83 {
84 return (BNU_CHUNK_T)0 - (a & 1);
85 }
86
87 /* tests if a is odd */
cpIsOdd_ct(BNU_CHUNK_T a)88 __INLINE BNU_CHUNK_T cpIsOdd_ct(BNU_CHUNK_T a)
89 {
90 return cpIsLsb_ct(a);
91 }
92
93 /* tests if a is even */
cpIsEven_ct(BNU_CHUNK_T a)94 __INLINE BNU_CHUNK_T cpIsEven_ct(BNU_CHUNK_T a)
95 {
96 return ~cpIsLsb_ct(a);
97 }
98
99 /* tests if a==0 */
cpIsZero_ct(BNU_CHUNK_T a)100 __INLINE BNU_CHUNK_T cpIsZero_ct(BNU_CHUNK_T a)
101 {
102 return cpIsMsb_ct(~a & (a - 1));
103 }
104
105 /* tests if a==b */
cpIsEqu_ct(BNU_CHUNK_T a,BNU_CHUNK_T b)106 __INLINE BNU_CHUNK_T cpIsEqu_ct(BNU_CHUNK_T a, BNU_CHUNK_T b)
107 {
108 return cpIsZero_ct(a ^ b);
109 }
110
111 /* replace under mask: dst[] = replaceFlag? src[] : dst[] */
cpMaskedReplace_ct(BNU_CHUNK_T * dst,const BNU_CHUNK_T * src,int len,BNU_CHUNK_T replaceMask)112 __INLINE void cpMaskedReplace_ct(BNU_CHUNK_T* dst, const BNU_CHUNK_T* src, int len, BNU_CHUNK_T replaceMask)
113 {
114 BNU_CHUNK_T dstMask = ~replaceMask;
115 int n;
116 for(n=0; n<len; n++)
117 dst[n] = (src[n] & replaceMask) ^ (dst[n] & dstMask);
118 }
119
120 /* copy under mask: dst[] = src1[] & mask) ^ src2[] & ~mask */
cpMaskedCopyBNU_ct(BNU_CHUNK_T * dst,BNU_CHUNK_T mask,const BNU_CHUNK_T * src1,const BNU_CHUNK_T * src2,int len)121 __INLINE void cpMaskedCopyBNU_ct(BNU_CHUNK_T* dst, BNU_CHUNK_T mask, const BNU_CHUNK_T* src1, const BNU_CHUNK_T* src2, int len)
122 {
123 int i;
124 for(i=0; i<(len); i++)
125 dst[i] = (src1[i] & mask) ^ (src2[i] & ~mask);
126 }
127
128 /* test if GF elmement is equal to x chunk */
cpIsGFpElemEquChunk_ct(const BNU_CHUNK_T * pE,int nsE,BNU_CHUNK_T x)129 __INLINE BNU_CHUNK_T cpIsGFpElemEquChunk_ct(const BNU_CHUNK_T* pE, int nsE, BNU_CHUNK_T x)
130 {
131 int i;
132 BNU_CHUNK_T accum = pE[0] ^ x;
133 for (i = 1; i < nsE; i++) {
134 accum |= pE[i];
135 }
136 return cpIsZero_ct(accum);
137 }
138
139 #define GFPE_IS_ZERO_CT(a,size) cpIsGFpElemEquChunk_ct((a),(size), 0)
140
141 #endif /* _PCP_MASK_CT_H */
142