10b57cec5SDimitry Andric /*===---- shaintrin.h - SHA intrinsics -------------------------------------===
20b57cec5SDimitry Andric *
30b57cec5SDimitry Andric * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric * See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric *
70b57cec5SDimitry Andric *===-----------------------------------------------------------------------===
80b57cec5SDimitry Andric */
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric #ifndef __IMMINTRIN_H
110b57cec5SDimitry Andric #error "Never use <shaintrin.h> directly; include <immintrin.h> instead."
120b57cec5SDimitry Andric #endif
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #ifndef __SHAINTRIN_H
150b57cec5SDimitry Andric #define __SHAINTRIN_H
160b57cec5SDimitry Andric
170b57cec5SDimitry Andric /* Define the default attributes for the functions in this file. */
180b57cec5SDimitry Andric #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("sha"), __min_vector_width__(128)))
190b57cec5SDimitry Andric
20*06c3fb27SDimitry Andric /// Performs four iterations of the inner loop of the SHA-1 message digest
21*06c3fb27SDimitry Andric /// algorithm using the starting SHA-1 state (A, B, C, D) from the 128-bit
22*06c3fb27SDimitry Andric /// vector of [4 x i32] in \a V1 and the next four 32-bit elements of the
23*06c3fb27SDimitry Andric /// message from the 128-bit vector of [4 x i32] in \a V2. Note that the
24*06c3fb27SDimitry Andric /// SHA-1 state variable E must have already been added to \a V2
25*06c3fb27SDimitry Andric /// (\c _mm_sha1nexte_epu32() can perform this step). Returns the updated
26*06c3fb27SDimitry Andric /// SHA-1 state (A, B, C, D) as a 128-bit vector of [4 x i32].
27*06c3fb27SDimitry Andric ///
28*06c3fb27SDimitry Andric /// The SHA-1 algorithm has an inner loop of 80 iterations, twenty each
29*06c3fb27SDimitry Andric /// with a different combining function and rounding constant. This
30*06c3fb27SDimitry Andric /// intrinsic performs four iterations using a combining function and
31*06c3fb27SDimitry Andric /// rounding constant selected by \a M[1:0].
32*06c3fb27SDimitry Andric ///
33*06c3fb27SDimitry Andric /// \headerfile <immintrin.h>
34*06c3fb27SDimitry Andric ///
35*06c3fb27SDimitry Andric /// \code
36*06c3fb27SDimitry Andric /// __m128i _mm_sha1rnds4_epu32(__m128i V1, __m128i V2, const int M);
37*06c3fb27SDimitry Andric /// \endcode
38*06c3fb27SDimitry Andric ///
39*06c3fb27SDimitry Andric /// This intrinsic corresponds to the \c SHA1RNDS4 instruction.
40*06c3fb27SDimitry Andric ///
41*06c3fb27SDimitry Andric /// \param V1
42*06c3fb27SDimitry Andric /// A 128-bit vector of [4 x i32] containing the initial SHA-1 state.
43*06c3fb27SDimitry Andric /// \param V2
44*06c3fb27SDimitry Andric /// A 128-bit vector of [4 x i32] containing the next four elements of
45*06c3fb27SDimitry Andric /// the message, plus SHA-1 state variable E.
46*06c3fb27SDimitry Andric /// \param M
47*06c3fb27SDimitry Andric /// An immediate value where bits [1:0] select among four possible
48*06c3fb27SDimitry Andric /// combining functions and rounding constants (not specified here).
49*06c3fb27SDimitry Andric /// \returns A 128-bit vector of [4 x i32] containing the updated SHA-1 state.
500b57cec5SDimitry Andric #define _mm_sha1rnds4_epu32(V1, V2, M) \
510b57cec5SDimitry Andric __builtin_ia32_sha1rnds4((__v4si)(__m128i)(V1), (__v4si)(__m128i)(V2), (M))
520b57cec5SDimitry Andric
53*06c3fb27SDimitry Andric /// Calculates the SHA-1 state variable E from the SHA-1 state variables in
54*06c3fb27SDimitry Andric /// the 128-bit vector of [4 x i32] in \a __X, adds that to the next set of
55*06c3fb27SDimitry Andric /// four message elements in the 128-bit vector of [4 x i32] in \a __Y, and
56*06c3fb27SDimitry Andric /// returns the result.
57*06c3fb27SDimitry Andric ///
58*06c3fb27SDimitry Andric /// \headerfile <immintrin.h>
59*06c3fb27SDimitry Andric ///
60*06c3fb27SDimitry Andric /// This intrinsic corresponds to the \c SHA1NEXTE instruction.
61*06c3fb27SDimitry Andric ///
62*06c3fb27SDimitry Andric /// \param __X
63*06c3fb27SDimitry Andric /// A 128-bit vector of [4 x i32] containing the current SHA-1 state.
64*06c3fb27SDimitry Andric /// \param __Y
65*06c3fb27SDimitry Andric /// A 128-bit vector of [4 x i32] containing the next four elements of the
66*06c3fb27SDimitry Andric /// message.
67*06c3fb27SDimitry Andric /// \returns A 128-bit vector of [4 x i32] containing the updated SHA-1
68*06c3fb27SDimitry Andric /// values.
690b57cec5SDimitry Andric static __inline__ __m128i __DEFAULT_FN_ATTRS
_mm_sha1nexte_epu32(__m128i __X,__m128i __Y)700b57cec5SDimitry Andric _mm_sha1nexte_epu32(__m128i __X, __m128i __Y)
710b57cec5SDimitry Andric {
720b57cec5SDimitry Andric return (__m128i)__builtin_ia32_sha1nexte((__v4si)__X, (__v4si)__Y);
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric
75*06c3fb27SDimitry Andric /// Performs an intermediate calculation for deriving the next four SHA-1
76*06c3fb27SDimitry Andric /// message elements using previous message elements from the 128-bit
77*06c3fb27SDimitry Andric /// vectors of [4 x i32] in \a __X and \a __Y, and returns the result.
78*06c3fb27SDimitry Andric ///
79*06c3fb27SDimitry Andric /// \headerfile <immintrin.h>
80*06c3fb27SDimitry Andric ///
81*06c3fb27SDimitry Andric /// This intrinsic corresponds to the \c SHA1MSG1 instruction.
82*06c3fb27SDimitry Andric ///
83*06c3fb27SDimitry Andric /// \param __X
84*06c3fb27SDimitry Andric /// A 128-bit vector of [4 x i32] containing previous message elements.
85*06c3fb27SDimitry Andric /// \param __Y
86*06c3fb27SDimitry Andric /// A 128-bit vector of [4 x i32] containing previous message elements.
87*06c3fb27SDimitry Andric /// \returns A 128-bit vector of [4 x i32] containing the derived SHA-1
88*06c3fb27SDimitry Andric /// elements.
890b57cec5SDimitry Andric static __inline__ __m128i __DEFAULT_FN_ATTRS
_mm_sha1msg1_epu32(__m128i __X,__m128i __Y)900b57cec5SDimitry Andric _mm_sha1msg1_epu32(__m128i __X, __m128i __Y)
910b57cec5SDimitry Andric {
920b57cec5SDimitry Andric return (__m128i)__builtin_ia32_sha1msg1((__v4si)__X, (__v4si)__Y);
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric
95*06c3fb27SDimitry Andric /// Performs the final calculation for deriving the next four SHA-1 message
96*06c3fb27SDimitry Andric /// elements using previous message elements from the 128-bit vectors of
97*06c3fb27SDimitry Andric /// [4 x i32] in \a __X and \a __Y, and returns the result.
98*06c3fb27SDimitry Andric ///
99*06c3fb27SDimitry Andric /// \headerfile <immintrin.h>
100*06c3fb27SDimitry Andric ///
101*06c3fb27SDimitry Andric /// This intrinsic corresponds to the \c SHA1MSG2 instruction.
102*06c3fb27SDimitry Andric ///
103*06c3fb27SDimitry Andric /// \param __X
104*06c3fb27SDimitry Andric /// A 128-bit vector of [4 x i32] containing an intermediate result.
105*06c3fb27SDimitry Andric /// \param __Y
106*06c3fb27SDimitry Andric /// A 128-bit vector of [4 x i32] containing previous message values.
107*06c3fb27SDimitry Andric /// \returns A 128-bit vector of [4 x i32] containing the updated SHA-1
108*06c3fb27SDimitry Andric /// values.
1090b57cec5SDimitry Andric static __inline__ __m128i __DEFAULT_FN_ATTRS
_mm_sha1msg2_epu32(__m128i __X,__m128i __Y)1100b57cec5SDimitry Andric _mm_sha1msg2_epu32(__m128i __X, __m128i __Y)
1110b57cec5SDimitry Andric {
1120b57cec5SDimitry Andric return (__m128i)__builtin_ia32_sha1msg2((__v4si)__X, (__v4si)__Y);
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric
115*06c3fb27SDimitry Andric /// Performs two rounds of SHA-256 operation using the following inputs: a
116*06c3fb27SDimitry Andric /// starting SHA-256 state (C, D, G, H) from the 128-bit vector of
117*06c3fb27SDimitry Andric /// [4 x i32] in \a __X; a starting SHA-256 state (A, B, E, F) from the
118*06c3fb27SDimitry Andric /// 128-bit vector of [4 x i32] in \a __Y; and a pre-computed sum of the
119*06c3fb27SDimitry Andric /// next two message elements (unsigned 32-bit integers) and corresponding
120*06c3fb27SDimitry Andric /// rounding constants from the 128-bit vector of [4 x i32] in \a __Z.
121*06c3fb27SDimitry Andric /// Returns the updated SHA-256 state (A, B, E, F) as a 128-bit vector of
122*06c3fb27SDimitry Andric /// [4 x i32].
123*06c3fb27SDimitry Andric ///
124*06c3fb27SDimitry Andric /// The SHA-256 algorithm has a core loop of 64 iterations. This intrinsic
125*06c3fb27SDimitry Andric /// performs two of those iterations.
126*06c3fb27SDimitry Andric ///
127*06c3fb27SDimitry Andric /// \headerfile <immintrin.h>
128*06c3fb27SDimitry Andric ///
129*06c3fb27SDimitry Andric /// This intrinsic corresponds to the \c SHA256RNDS2 instruction.
130*06c3fb27SDimitry Andric ///
131*06c3fb27SDimitry Andric /// \param __X
132*06c3fb27SDimitry Andric /// A 128-bit vector of [4 x i32] containing part of the initial SHA-256
133*06c3fb27SDimitry Andric /// state.
134*06c3fb27SDimitry Andric /// \param __Y
135*06c3fb27SDimitry Andric /// A 128-bit vector of [4 x i32] containing part of the initial SHA-256
136*06c3fb27SDimitry Andric /// state.
137*06c3fb27SDimitry Andric /// \param __Z
138*06c3fb27SDimitry Andric /// A 128-bit vector of [4 x i32] containing additional input to the
139*06c3fb27SDimitry Andric /// SHA-256 operation.
140*06c3fb27SDimitry Andric /// \returns A 128-bit vector of [4 x i32] containing the updated SHA-1 state.
1410b57cec5SDimitry Andric static __inline__ __m128i __DEFAULT_FN_ATTRS
_mm_sha256rnds2_epu32(__m128i __X,__m128i __Y,__m128i __Z)1420b57cec5SDimitry Andric _mm_sha256rnds2_epu32(__m128i __X, __m128i __Y, __m128i __Z)
1430b57cec5SDimitry Andric {
1440b57cec5SDimitry Andric return (__m128i)__builtin_ia32_sha256rnds2((__v4si)__X, (__v4si)__Y, (__v4si)__Z);
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric
147*06c3fb27SDimitry Andric /// Performs an intermediate calculation for deriving the next four SHA-256
148*06c3fb27SDimitry Andric /// message elements using previous message elements from the 128-bit
149*06c3fb27SDimitry Andric /// vectors of [4 x i32] in \a __X and \a __Y, and returns the result.
150*06c3fb27SDimitry Andric ///
151*06c3fb27SDimitry Andric /// \headerfile <immintrin.h>
152*06c3fb27SDimitry Andric ///
153*06c3fb27SDimitry Andric /// This intrinsic corresponds to the \c SHA256MSG1 instruction.
154*06c3fb27SDimitry Andric ///
155*06c3fb27SDimitry Andric /// \param __X
156*06c3fb27SDimitry Andric /// A 128-bit vector of [4 x i32] containing previous message elements.
157*06c3fb27SDimitry Andric /// \param __Y
158*06c3fb27SDimitry Andric /// A 128-bit vector of [4 x i32] containing previous message elements.
159*06c3fb27SDimitry Andric /// \returns A 128-bit vector of [4 x i32] containing the updated SHA-256
160*06c3fb27SDimitry Andric /// values.
1610b57cec5SDimitry Andric static __inline__ __m128i __DEFAULT_FN_ATTRS
_mm_sha256msg1_epu32(__m128i __X,__m128i __Y)1620b57cec5SDimitry Andric _mm_sha256msg1_epu32(__m128i __X, __m128i __Y)
1630b57cec5SDimitry Andric {
1640b57cec5SDimitry Andric return (__m128i)__builtin_ia32_sha256msg1((__v4si)__X, (__v4si)__Y);
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric
167*06c3fb27SDimitry Andric /// Performs the final calculation for deriving the next four SHA-256 message
168*06c3fb27SDimitry Andric /// elements using previous message elements from the 128-bit vectors of
169*06c3fb27SDimitry Andric /// [4 x i32] in \a __X and \a __Y, and returns the result.
170*06c3fb27SDimitry Andric ///
171*06c3fb27SDimitry Andric /// \headerfile <immintrin.h>
172*06c3fb27SDimitry Andric ///
173*06c3fb27SDimitry Andric /// This intrinsic corresponds to the \c SHA256MSG2 instruction.
174*06c3fb27SDimitry Andric ///
175*06c3fb27SDimitry Andric /// \param __X
176*06c3fb27SDimitry Andric /// A 128-bit vector of [4 x i32] containing an intermediate result.
177*06c3fb27SDimitry Andric /// \param __Y
178*06c3fb27SDimitry Andric /// A 128-bit vector of [4 x i32] containing previous message values.
179*06c3fb27SDimitry Andric /// \returns A 128-bit vector of [4 x i32] containing the updated SHA-256
180*06c3fb27SDimitry Andric /// values.
1810b57cec5SDimitry Andric static __inline__ __m128i __DEFAULT_FN_ATTRS
_mm_sha256msg2_epu32(__m128i __X,__m128i __Y)1820b57cec5SDimitry Andric _mm_sha256msg2_epu32(__m128i __X, __m128i __Y)
1830b57cec5SDimitry Andric {
1840b57cec5SDimitry Andric return (__m128i)__builtin_ia32_sha256msg2((__v4si)__X, (__v4si)__Y);
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric
1870b57cec5SDimitry Andric #undef __DEFAULT_FN_ATTRS
1880b57cec5SDimitry Andric
1890b57cec5SDimitry Andric #endif /* __SHAINTRIN_H */
190