1 /*******************************************************************************
2 * Copyright 2013-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 //     Fixed window exponentiation scramble/unscramble
46 //
47 //  Contents:
48 //     cpScramblePut()
49 //     cpScrambleGet()
50 //
51 //
52 */
53 
54 #if !defined(_PC_SCRAMBLE_H)
55 #define _PC_SCRAMBLE_H
56 
57 /*
58 // cpsScramblePut/cpsScrambleGet
59 // stores to/retrieves from pScrambleEntry position
60 // pre-computed data if fixed window method is used
61 */
cpScramblePut(Ipp8u * pArray,cpSize colummSize,const Ipp32u * pData,cpSize dataSize)62 __INLINE void cpScramblePut(Ipp8u* pArray, cpSize colummSize,
63                       const Ipp32u* pData, cpSize dataSize)
64 {
65    int i;
66    switch(colummSize) {
67       case 1: // column - byte
68          dataSize *= sizeof(Ipp32u);
69          for(i=0; i<dataSize; i++)
70             pArray[i*CACHE_LINE_SIZE] = ((Ipp8u*)pData)[i];
71          break;
72       case 2: // column - word (2 bytes)
73          dataSize *= sizeof(Ipp16u);
74          for(i=0; i<dataSize; i++)
75             ((Ipp16u*)pArray)[i*CACHE_LINE_SIZE/sizeof(Ipp16u)] = ((Ipp16u*)pData)[i];
76          break;
77       case 4: // column - dword (4 bytes)
78          for(i=0; i<dataSize; i++)
79             ((Ipp32u*)pArray)[i*CACHE_LINE_SIZE/sizeof(Ipp32u)] = pData[i];
80          break;
81       case 8: // column - qword (8 bytes => 2 dword)
82          for(; dataSize>=2; dataSize-=2, pArray+=CACHE_LINE_SIZE, pData+=2) {
83             ((Ipp32u*)pArray)[0] = pData[0];
84             ((Ipp32u*)pArray)[1] = pData[1];
85          }
86          if(dataSize)
87             ((Ipp32u*)pArray)[0] = pData[0];
88          break;
89       case 16: // column - oword (16 bytes => 4 dword)
90          for(; dataSize>=4; dataSize-=4, pArray+=CACHE_LINE_SIZE, pData+=4) {
91             ((Ipp32u*)pArray)[0] = pData[0];
92             ((Ipp32u*)pArray)[1] = pData[1];
93             ((Ipp32u*)pArray)[2] = pData[2];
94             ((Ipp32u*)pArray)[3] = pData[3];
95          }
96          for(; dataSize>0; dataSize--, pArray+=sizeof(Ipp32u), pData++)
97             ((Ipp32u*)pArray)[0] = pData[0];
98          break;
99       case 32: // column - 2 oword (32 bytes => 8 dword)
100          for(; dataSize>=8; dataSize-=8, pArray+=CACHE_LINE_SIZE, pData+=8) {
101             ((Ipp32u*)pArray)[0] = pData[0];
102             ((Ipp32u*)pArray)[1] = pData[1];
103             ((Ipp32u*)pArray)[2] = pData[2];
104             ((Ipp32u*)pArray)[3] = pData[3];
105             ((Ipp32u*)pArray)[4] = pData[4];
106             ((Ipp32u*)pArray)[5] = pData[5];
107             ((Ipp32u*)pArray)[6] = pData[6];
108             ((Ipp32u*)pArray)[7] = pData[7];
109          }
110          for(; dataSize>0; dataSize--, pArray+=sizeof(Ipp32u), pData++)
111             ((Ipp32u*)pArray)[0] = pData[0];
112          break;
113       default:
114          break;
115    }
116 }
117 
118 
119 /*
120 // Retrieve data from pArray
121 */
122 #define u8_to_u32(b0,b1,b2,b3, x) \
123   ((x) = (b0), \
124    (x)|=((b1)<<8), \
125    (x)|=((b2)<<16), \
126    (x)|=((b3)<<24))
127 #define u16_to_u32(w0,w1, x) \
128   ((x) = (w0), \
129    (x)|=((w1)<<16))
130 #define u32_to_u64(dw0,dw1, x) \
131   ((x) = (Ipp64u)(dw0), \
132    (x)|= (((Ipp64u)(dw1))<<32))
133 
cpScrambleGet(Ipp32u * pData,cpSize dataSize,const Ipp8u * pArray,cpSize colummSize)134 __INLINE void cpScrambleGet(Ipp32u* pData, cpSize dataSize,
135                       const Ipp8u* pArray, cpSize colummSize)
136 {
137    int i;
138    switch(colummSize) {
139       case 1: // column - byte
140          for(i=0; i<dataSize; i++, pArray+=sizeof(Ipp32u)*CACHE_LINE_SIZE)
141             u8_to_u32(pArray[0*CACHE_LINE_SIZE], pArray[1*CACHE_LINE_SIZE], pArray[2*CACHE_LINE_SIZE], pArray[3*CACHE_LINE_SIZE], pData[i]);
142          break;
143       case 2: // column - word (2 bytes)
144          for(i=0; i<dataSize; i++, pArray+=sizeof(Ipp16u)*CACHE_LINE_SIZE) {
145             Ipp16u w0 = *((Ipp16u*)(pArray));
146             Ipp16u w1 = *((Ipp16u*)(pArray+CACHE_LINE_SIZE));
147             u16_to_u32( w0, w1, pData[i]);
148          }
149          break;
150       case 4: // column - dword (4 bytes)
151          for(i=0; i<dataSize; i++, pArray+=CACHE_LINE_SIZE)
152             pData[i] = ((Ipp32u*)pArray)[0];
153          break;
154       case 8: // column - qword (8 bytes => 2 dword)
155          for(; dataSize>=2; dataSize-=2, pArray+=CACHE_LINE_SIZE, pData+=2) {
156             pData[0] = ((Ipp32u*)pArray)[0];
157             pData[1] = ((Ipp32u*)pArray)[1];
158          }
159          if(dataSize)
160             pData[0] = ((Ipp32u*)pArray)[0];
161          break;
162       case 16: // column - oword (16 bytes => 4 dword)
163          for(; dataSize>=4; dataSize-=4, pArray+=CACHE_LINE_SIZE, pData+=4) {
164             pData[0] = ((Ipp32u*)pArray)[0];
165             pData[1] = ((Ipp32u*)pArray)[1];
166             pData[2] = ((Ipp32u*)pArray)[2];
167             pData[3] = ((Ipp32u*)pArray)[3];
168 
169          }
170          for(; dataSize>0; dataSize--, pArray+=sizeof(Ipp32u), pData++)
171             pData[0] = ((Ipp32u*)pArray)[0];
172          break;
173       case 32: // column - 2 oword (32 bytes => 8 dword)
174          for(; dataSize>=8; dataSize-=8, pArray+=CACHE_LINE_SIZE, pData+=8) {
175             pData[0] = ((Ipp32u*)pArray)[0];
176             pData[1] = ((Ipp32u*)pArray)[1];
177             pData[2] = ((Ipp32u*)pArray)[2];
178             pData[3] = ((Ipp32u*)pArray)[3];
179             pData[4] = ((Ipp32u*)pArray)[4];
180             pData[5] = ((Ipp32u*)pArray)[5];
181             pData[6] = ((Ipp32u*)pArray)[6];
182             pData[7] = ((Ipp32u*)pArray)[7];
183          }
184          for(; dataSize>0; dataSize--, pArray+=sizeof(Ipp32u), pData++)
185             pData[0] = ((Ipp32u*)pArray)[0];
186          break;
187       default:
188          break;
189    }
190 }
191 
192 #endif /* _PC_SCRAMBLE_H */
193