1 /*******************************************************************************
2 * Copyright 2014-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 // SMS4 ECB encryption/decryption
46 //
47 // Contents:
48 // Internal stuff
49 //
50 //
51 */
52
53 #include "owndefs.h"
54 #include "owncp.h"
55 #include "pcpsms4.h"
56
57 #if !defined _PCP_SMS4_ECB_H
58 #define _PCP_SMS4_ECB_H
59
60 #if (_IPP>=_IPP_P8) || (_IPP32E>=_IPP32E_Y8)
61
62 static __ALIGN16 Ipp8u inpMaskLO[] = {0x65,0x41,0xfd,0xd9,0x0a,0x2e,0x92,0xb6,0x0f,0x2b,0x97,0xb3,0x60,0x44,0xf8,0xdc};
63 static __ALIGN16 Ipp8u inpMaskHI[] = {0x00,0xc9,0x67,0xae,0x80,0x49,0xe7,0x2e,0x4a,0x83,0x2d,0xe4,0xca,0x03,0xad,0x64};
64 static __ALIGN16 Ipp8u outMaskLO[] = {0xd3,0x59,0x38,0xb2,0xcc,0x46,0x27,0xad,0x36,0xbc,0xdd,0x57,0x29,0xa3,0xc2,0x48};
65 static __ALIGN16 Ipp8u outMaskHI[] = {0x00,0x50,0x14,0x44,0x89,0xd9,0x9d,0xcd,0xde,0x8e,0xca,0x9a,0x57,0x07,0x43,0x13};
66
67 static __ALIGN16 Ipp8u encKey[] = {0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63};
68 static __ALIGN16 Ipp8u maskSrows[] = {0x00,0x0d,0x0a,0x07,0x04,0x01,0x0e,0x0b,0x08,0x05,0x02,0x0f,0x0c,0x09,0x06,0x03};
69
70 static __ALIGN16 Ipp8u lowBits4[] = {0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f};
71
72 static __ALIGN16 Ipp8u swapBytes[] = {3,2,1,0, 7,6,5,4, 11,10,9,8, 15,14,13,12};
73
74 #define M128(mem) (*((__m128i*)((Ipp8u*)(mem))))
75
76 /*
77 // stuff
78 */
affine(__m128i x,__m128i maskLO,__m128i maskHI)79 __INLINE __m128i affine(__m128i x, __m128i maskLO, __m128i maskHI)
80 {
81 __m128i T1 = _mm_and_si128(_mm_srli_epi64(x, 4), M128(lowBits4));
82 __m128i T0 = _mm_and_si128(x, M128(lowBits4));
83 T0 = _mm_shuffle_epi8(maskLO, T0);
84 T1 = _mm_shuffle_epi8(maskHI, T1);
85 return _mm_xor_si128(T0, T1);
86 }
87
Ltag(__m128i x)88 __INLINE __m128i Ltag(__m128i x)
89 {
90 __m128i T = _mm_slli_epi32(x, 13);
91 T = _mm_xor_si128(T, _mm_srli_epi32 (x,19));
92 T = _mm_xor_si128(T, _mm_slli_epi32 (x,23));
93 T = _mm_xor_si128(T, _mm_srli_epi32 (x, 9));
94 return T;
95 }
96
97 /*
98 // ECB encryption/decryption
99 */
100 #define TRANSPOSE_INP(K0,K1,K2,K3, T) \
101 T = _mm_unpacklo_epi32(K0, K1); \
102 K1 = _mm_unpackhi_epi32(K0, K1); \
103 K0 = _mm_unpacklo_epi32(K2, K3); \
104 K3 = _mm_unpackhi_epi32(K2, K3); \
105 \
106 K2 = _mm_unpacklo_epi64(K1, K3); \
107 K3 = _mm_unpackhi_epi64(K1, K3); \
108 K1 = _mm_unpackhi_epi64(T, K0); \
109 K0 = _mm_unpacklo_epi64(T, K0)
110
111 #define TRANSPOSE_OUT(K0,K1,K2,K3, T) \
112 T = _mm_unpacklo_epi32(K1, K0); \
113 K0 = _mm_unpackhi_epi32(K1, K0); \
114 K1 = _mm_unpacklo_epi32(K3, K2); \
115 K3 = _mm_unpackhi_epi32(K3, K2); \
116 \
117 K2 = _mm_unpackhi_epi64(K1, T); \
118 T = _mm_unpacklo_epi64(K1, T); \
119 K1 = _mm_unpacklo_epi64(K3, K0); \
120 K0 = _mm_unpackhi_epi64(K3, K0); \
121 K3 = T
122
L(__m128i x)123 __INLINE __m128i L(__m128i x)
124 {
125 __m128i T = _mm_slli_epi32(x, 2);
126 T = _mm_xor_si128(T, _mm_srli_epi32 (x,30));
127
128 T = _mm_xor_si128(T, _mm_slli_epi32 (x,10));
129 T = _mm_xor_si128(T, _mm_srli_epi32 (x,22));
130
131 T = _mm_xor_si128(T, _mm_slli_epi32 (x,18));
132 T = _mm_xor_si128(T, _mm_srli_epi32 (x,14));
133
134 T = _mm_xor_si128(T, _mm_slli_epi32 (x,24));
135 T = _mm_xor_si128(T, _mm_srli_epi32 (x, 8));
136 return T;
137 }
138
139 #endif /* _IPP_P8, _IPP32E_Y8 */
140
141 #endif /* #if !defined _PCP_SMS4_ECB_H */
142