1 /*******************************************************************************
2 * Copyright 2007-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 // Internal Safe Rijndael Encrypt, Decrypt
46 //
47 //
48 */
49
50 #if !defined(_PCP_RIJ_SAFE_H)
51 #define _PCP_RIJ_SAFE_H
52
53 #include "owncp.h"
54 #include "pcprijtables.h"
55 #include "pcpbnuimpl.h"
56
57 #if defined _PCP_RIJ_SAFE_OLD
58 /* old version */
59 #define TransformByte OWNAPI(TransformByte)
60 Ipp8u TransformByte(Ipp8u x, const Ipp8u Transformation[]);
61
62 #define TransformNative2Composite OWNAPI(TransformNative2Composite)
63 #define TransformComposite2Native OWNAPI(TransformComposite2Native)
64
65 void TransformNative2Composite(Ipp8u out[16], const Ipp8u inp[16]);
66 void TransformComposite2Native(Ipp8u out[16], const Ipp8u inp[16]);
67
68 #define InverseComposite OWNAPI(InverseComposite)
69 Ipp8u InverseComposite(Ipp8u x);
70
71 #define AddRoundKey OWNAPI(AddRoundKey)
72 void AddRoundKey(Ipp8u out[], const Ipp8u inp[], const Ipp8u pKey[]);
73 #endif
74
75
76 #if !defined _PCP_RIJ_SAFE_OLD
77 /* new version */
78 #define TransformNative2Composite OWNAPI(TransformNative2Composite)
79 #define TransformComposite2Native OWNAPI(TransformComposite2Native)
80
81 void TransformNative2Composite(Ipp8u out[16], const Ipp8u inp[16]);
82 void TransformComposite2Native(Ipp8u out[16], const Ipp8u inp[16]);
83
84 /* add round key operation */
AddRoundKey(Ipp8u out[16],const Ipp8u inp[16],const Ipp8u rkey[16])85 __INLINE void AddRoundKey(Ipp8u out[16], const Ipp8u inp[16], const Ipp8u rkey[16])
86 {
87 ((Ipp64u*)out)[0] = ((Ipp64u*)inp)[0] ^ ((Ipp64u*)rkey)[0];
88 ((Ipp64u*)out)[1] = ((Ipp64u*)inp)[1] ^ ((Ipp64u*)rkey)[1];
89 }
90
91 /* add logs of GF(2^4) elements
92 // the exp table has been build matched for that implementation
93 */
AddLogGF16(Ipp8u loga,Ipp8u logb)94 __INLINE Ipp8u AddLogGF16(Ipp8u loga, Ipp8u logb)
95 {
96 //Ipp8u s = loga+logb;
97 //return (s>2*14)? 15 : (s>14)? s-15 : s;
98 Ipp8u s = loga+logb;
99 Ipp8u delta = ((0xF-1)-s)>>7;
100 s -= delta;
101 s |= 0-(s>>7);
102 return s & (0xF);
103 }
104 #endif
105
106 #define SELECTION_BITS ((sizeof(BNU_CHUNK_T)/sizeof(Ipp8u)) -1)
107
108 #if defined(__INTEL_COMPILER)
getSboxValue(Ipp8u x)109 __INLINE Ipp8u getSboxValue(Ipp8u x)
110 {
111 BNU_CHUNK_T selection = 0;
112 const BNU_CHUNK_T* SboxEntry = (BNU_CHUNK_T*)RijEncSbox;
113
114 BNU_CHUNK_T i_sel = x / sizeof(BNU_CHUNK_T); /* selection index */
115 BNU_CHUNK_T i;
116 for (i = 0; i<sizeof(RijEncSbox) / sizeof(BNU_CHUNK_T); i++) {
117 BNU_CHUNK_T mask = (i == i_sel) ? (BNU_CHUNK_T)(-1) : 0; /* ipp and IPP build specific avoid jump instruction here */
118 selection |= SboxEntry[i] & mask;
119 }
120 selection >>= (x & SELECTION_BITS) * 8;
121 return (Ipp8u)(selection & 0xFF);
122 }
123
124 #else
125 #include "pcpmask_ct.h"
getSboxValue(Ipp8u x)126 __INLINE Ipp8u getSboxValue(Ipp8u x)
127 {
128 BNU_CHUNK_T selection = 0;
129 const BNU_CHUNK_T* SboxEntry = (BNU_CHUNK_T*)RijEncSbox;
130
131 Ipp32u _x = x / sizeof(BNU_CHUNK_T);
132 Ipp32u i;
133 for (i = 0; i<sizeof(RijEncSbox) / sizeof(BNU_CHUNK_T); i++) {
134 BNS_CHUNK_T mask = cpIsEqu_ct(_x, i);
135 selection |= SboxEntry[i] & mask;
136 }
137 selection >>= (x & SELECTION_BITS) * 8;
138 return (Ipp8u)(selection & 0xFF);
139 }
140 #endif
141
142 #endif /* _PCP_RIJ_SAFE_H */
143