Debian Codegraph @

sources/ippcp/pcpaes_gcm_vaes512.h

raw symbols SCIP
1 /*************************************************************************
2 * Copyright (C) 2019 Intel Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *************************************************************************/
16
17 /*
18 //
19 // Purpose:
20 // Cryptography Primitive.
21 // AES-GCM auxiliary
22 //
23 // Contents:
24 //
25 */
26
27 #if 0 // Not used
28
29 #if !defined(_CP_AES_GCM_VAES512_H)
30 #define _CP_AES_GCM_VAES512_H
31
32 #if (_IPP32E >= _IPP32E_K1)
33 #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
34 #pragma warning(disable : 4310) // cast truncates constant value in MSVC
35 #endif
36
37 #define M128(mem) (*((__m128i*)((Ipp8u*)(mem))))
38 #define M256(mem) (*((__m256i*)((Ipp8u*)(mem))))
39 #define M512(mem) (*((__m512i*)((Ipp8u*)(mem))))
40
41 static const __ALIGN64 Ipp64u POLY2[] = { 0x1, 0xC200000000000000, 0x1, 0xC200000000000000,
42 0x1, 0xC200000000000000, 0x1, 0xC200000000000000 };
43
44 static __ALIGN64 Ipp8u swapBytes[] = {
45 15,14,13,12, 11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
46 31,30,29,28, 27,26,25,24, 23,22,21,20, 19,18,17,16,
47 47,46,45,44, 43,42,41,40, 39,38,37,36, 35,34,33,32,
48 63,62,61,60, 59,58,57,56, 55,54,53,52, 51,50,49,48,
49 };
50
51 /* The function performs careless Karatsuba multiplication of 4x128-bit blocks stored
52 * in 512-bit inputs:
53 * A = [A3 A2 A1 A0], B = [B3 B2 B1 B0]
54 * Ai=[a1i:a0i] and Bi=[b1i:b0i], i=0..3
55 * and returns parts of the multiplication Hi, Mi and Li, i=0..3, where
56 * Hi = a1i*b1i
57 * Mi = (a1i^a0i)*(b1i^b0i)
58 * Li = a0i*b0i
59 *
60 * NB: make sure unused parts of input registers are zeroed to avoid issues with further horizontal XOR.
61 */
62 IPPCP_INLINE void AesGcmKaratsubaMul4(const __m512i * const pA, /* A3 A2 A1 A0 */
63 const __m512i * const pHKeys, /* B3 B2 B1 B0 */
64 const __m512i * const pHKeysKaratsuba, /* precomputed (b1i^b0i) */
65 __m512i * const pH,
66 __m512i * const pM,
67 __m512i * const pL)
68 {
69 *pL = _mm512_clmulepi64_epi128(*pA, *pHKeys, 0x00); // L = [a0i*b0i]
70 *pH = _mm512_clmulepi64_epi128(*pA, *pHKeys, 0x11); // H = [a1i*b1i]
71
72 *pM = _mm512_shuffle_epi32(*pA, 78); // M = [a0i:a1i]
73 *pM = _mm512_xor_epi32(*pM, *pA); // M = [a1i^a0i:a1i^a0i]
74 *pM = _mm512_clmulepi64_epi128(*pM, *pHKeysKaratsuba, 0x00); // M = (a1i^a0i)*(b1i^b0i)
75 }
76
77 /* The function performs horizontal XOR for 4 128-bit values in 512-bit register
78 128-bit result value saved in the low part of the 512-bit register
79 */
80 IPPCP_INLINE void HXor4x128(const __m512i * const zmm,
81 __m128i * const xmm)
82 {
83 __m256i ymm;
84
85 ymm = _mm512_extracti64x4_epi64(*zmm, 0x1); // zmm = [3 2 1 0]; ymm = [3 2]
86 ymm = _mm256_xor_si256(_mm512_castsi512_si256(*zmm), ymm); // ymm = [1^3 0^2]
87
88 *xmm = _mm256_extracti32x4_epi32(ymm, 0x1); // xmm = [1^3]
89 *xmm = _mm_maskz_xor_epi64(0xFF, _mm256_castsi256_si128(ymm), *xmm);
90 }
91
92 /* The function performs Montgomery reduction of 256-bit polynomial to 128-bit one
93 with irreducible polynomial
94 */
95 IPPCP_INLINE void ReducePoly2x128(const __m128i * const pHI,
96 const __m128i * const pLO,
97 __m128i * const result)
98 {
99 __m256i tmp1, tmp2, HI, LO;
100
101 HI = _mm256_set_m128i(_mm_setzero_si128(), *pHI);
102 LO = _mm256_set_m128i(_mm_setzero_si128(), *pLO);
103
104 tmp1 = _mm256_clmulepi64_epi128(LO, M256(POLY2), 0x10);
105 tmp2 = _mm256_shuffle_epi32(LO, 78); // 78 = 01001110b
106 LO = _mm256_xor_si256(tmp1, tmp2);
107
108 tmp1 = _mm256_clmulepi64_epi128(LO, M256(POLY2), 0x10);
109 tmp2 = _mm256_shuffle_epi32(LO, 78); // 78 = 01001110b
110 LO = _mm256_xor_si256(tmp1, tmp2);
111
112 tmp1 = _mm256_xor_si256(HI, LO);
113 *result = _mm256_castsi256_si128(tmp1);
114 }
115
116 /* The function aggregates partial products of Karatsuba multiplication into final ghash value */
117 IPPCP_INLINE void AggregateKaratsubaPartialProducts(const __m512i * const pH,
118 const __m512i * const pM,
119 const __m512i * const pL,
120 __m128i * const result)
121 {
122 __m512i H, M, L;
123 __m128i H128, L128;
124
125 /* Aggregation step1 - combine multiplication results H,M,L into Hi and Lo 128-bit parts */
126 M = _mm512_xor_si512(*pM, *pH);
127 M = _mm512_xor_si512(M, *pL);
128 H = _mm512_bsrli_epi128(M, 8);
129 H = _mm512_xor_si512(*pH, H);
130 L = _mm512_bslli_epi128(M, 8);
131 L = _mm512_xor_si512(*pL, L);
132
133 /* Aggregation step2 - horizontal XOR for H, L*/
134 HXor4x128(&H, &H128);
135 HXor4x128(&L, &L128);
136
137 /* Reduction of 256-bit poly to 128-bit poly using irreducible polynomial */
138 ReducePoly2x128(&H128, &L128, result);
139 }
140
141 #endif /* #if (_IPP32E>=_IPP32E_K1) */
142
143 #endif /* _CP_AES_GCM_VAES512_H*/
144
145 #endif