191 lines · c
1/*===---- shaintrin.h - SHA intrinsics -------------------------------------===2 *3 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4 * See https://llvm.org/LICENSE.txt for license information.5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6 *7 *===-----------------------------------------------------------------------===8 */9 10#ifndef __IMMINTRIN_H11#error "Never use <shaintrin.h> directly; include <immintrin.h> instead."12#endif13 14#ifndef __SHAINTRIN_H15#define __SHAINTRIN_H16 17/* Define the default attributes for the functions in this file. */18#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("sha"), __min_vector_width__(128)))19 20/// Performs four iterations of the inner loop of the SHA-1 message digest21/// algorithm using the starting SHA-1 state (A, B, C, D) from the 128-bit22/// vector of [4 x i32] in \a V1 and the next four 32-bit elements of the23/// message from the 128-bit vector of [4 x i32] in \a V2. Note that the24/// SHA-1 state variable E must have already been added to \a V225/// (\c _mm_sha1nexte_epu32() can perform this step). Returns the updated26/// SHA-1 state (A, B, C, D) as a 128-bit vector of [4 x i32].27///28/// The SHA-1 algorithm has an inner loop of 80 iterations, twenty each29/// with a different combining function and rounding constant. This30/// intrinsic performs four iterations using a combining function and31/// rounding constant selected by \a M[1:0].32///33/// \headerfile <immintrin.h>34///35/// \code36/// __m128i _mm_sha1rnds4_epu32(__m128i V1, __m128i V2, const int M);37/// \endcode38///39/// This intrinsic corresponds to the \c SHA1RNDS4 instruction.40///41/// \param V142/// A 128-bit vector of [4 x i32] containing the initial SHA-1 state.43/// \param V244/// A 128-bit vector of [4 x i32] containing the next four elements of45/// the message, plus SHA-1 state variable E.46/// \param M47/// An immediate value where bits [1:0] select among four possible48/// combining functions and rounding constants (not specified here).49/// \returns A 128-bit vector of [4 x i32] containing the updated SHA-1 state.50#define _mm_sha1rnds4_epu32(V1, V2, M) \51 ((__m128i)__builtin_ia32_sha1rnds4((__v4si)(__m128i)(V1), \52 (__v4si)(__m128i)(V2), (M)))53 54/// Calculates the SHA-1 state variable E from the SHA-1 state variables in55/// the 128-bit vector of [4 x i32] in \a __X, adds that to the next set of56/// four message elements in the 128-bit vector of [4 x i32] in \a __Y, and57/// returns the result.58///59/// \headerfile <immintrin.h>60///61/// This intrinsic corresponds to the \c SHA1NEXTE instruction.62///63/// \param __X64/// A 128-bit vector of [4 x i32] containing the current SHA-1 state.65/// \param __Y66/// A 128-bit vector of [4 x i32] containing the next four elements of the67/// message.68/// \returns A 128-bit vector of [4 x i32] containing the updated SHA-169/// values.70static __inline__ __m128i __DEFAULT_FN_ATTRS71_mm_sha1nexte_epu32(__m128i __X, __m128i __Y)72{73 return (__m128i)__builtin_ia32_sha1nexte((__v4si)__X, (__v4si)__Y);74}75 76/// Performs an intermediate calculation for deriving the next four SHA-177/// message elements using previous message elements from the 128-bit78/// vectors of [4 x i32] in \a __X and \a __Y, and returns the result.79///80/// \headerfile <immintrin.h>81///82/// This intrinsic corresponds to the \c SHA1MSG1 instruction.83///84/// \param __X85/// A 128-bit vector of [4 x i32] containing previous message elements.86/// \param __Y87/// A 128-bit vector of [4 x i32] containing previous message elements.88/// \returns A 128-bit vector of [4 x i32] containing the derived SHA-189/// elements.90static __inline__ __m128i __DEFAULT_FN_ATTRS91_mm_sha1msg1_epu32(__m128i __X, __m128i __Y)92{93 return (__m128i)__builtin_ia32_sha1msg1((__v4si)__X, (__v4si)__Y);94}95 96/// Performs the final calculation for deriving the next four SHA-1 message97/// elements using previous message elements from the 128-bit vectors of98/// [4 x i32] in \a __X and \a __Y, and returns the result.99///100/// \headerfile <immintrin.h>101///102/// This intrinsic corresponds to the \c SHA1MSG2 instruction.103///104/// \param __X105/// A 128-bit vector of [4 x i32] containing an intermediate result.106/// \param __Y107/// A 128-bit vector of [4 x i32] containing previous message values.108/// \returns A 128-bit vector of [4 x i32] containing the updated SHA-1109/// values.110static __inline__ __m128i __DEFAULT_FN_ATTRS111_mm_sha1msg2_epu32(__m128i __X, __m128i __Y)112{113 return (__m128i)__builtin_ia32_sha1msg2((__v4si)__X, (__v4si)__Y);114}115 116/// Performs two rounds of SHA-256 operation using the following inputs: a117/// starting SHA-256 state (C, D, G, H) from the 128-bit vector of118/// [4 x i32] in \a __X; a starting SHA-256 state (A, B, E, F) from the119/// 128-bit vector of [4 x i32] in \a __Y; and a pre-computed sum of the120/// next two message elements (unsigned 32-bit integers) and corresponding121/// rounding constants from the 128-bit vector of [4 x i32] in \a __Z.122/// Returns the updated SHA-256 state (A, B, E, F) as a 128-bit vector of123/// [4 x i32].124///125/// The SHA-256 algorithm has a core loop of 64 iterations. This intrinsic126/// performs two of those iterations.127///128/// \headerfile <immintrin.h>129///130/// This intrinsic corresponds to the \c SHA256RNDS2 instruction.131///132/// \param __X133/// A 128-bit vector of [4 x i32] containing part of the initial SHA-256134/// state.135/// \param __Y136/// A 128-bit vector of [4 x i32] containing part of the initial SHA-256137/// state.138/// \param __Z139/// A 128-bit vector of [4 x i32] containing additional input to the140/// SHA-256 operation.141/// \returns A 128-bit vector of [4 x i32] containing the updated SHA-1 state.142static __inline__ __m128i __DEFAULT_FN_ATTRS143_mm_sha256rnds2_epu32(__m128i __X, __m128i __Y, __m128i __Z)144{145 return (__m128i)__builtin_ia32_sha256rnds2((__v4si)__X, (__v4si)__Y, (__v4si)__Z);146}147 148/// Performs an intermediate calculation for deriving the next four SHA-256149/// message elements using previous message elements from the 128-bit150/// vectors of [4 x i32] in \a __X and \a __Y, and returns the result.151///152/// \headerfile <immintrin.h>153///154/// This intrinsic corresponds to the \c SHA256MSG1 instruction.155///156/// \param __X157/// A 128-bit vector of [4 x i32] containing previous message elements.158/// \param __Y159/// A 128-bit vector of [4 x i32] containing previous message elements.160/// \returns A 128-bit vector of [4 x i32] containing the updated SHA-256161/// values.162static __inline__ __m128i __DEFAULT_FN_ATTRS163_mm_sha256msg1_epu32(__m128i __X, __m128i __Y)164{165 return (__m128i)__builtin_ia32_sha256msg1((__v4si)__X, (__v4si)__Y);166}167 168/// Performs the final calculation for deriving the next four SHA-256 message169/// elements using previous message elements from the 128-bit vectors of170/// [4 x i32] in \a __X and \a __Y, and returns the result.171///172/// \headerfile <immintrin.h>173///174/// This intrinsic corresponds to the \c SHA256MSG2 instruction.175///176/// \param __X177/// A 128-bit vector of [4 x i32] containing an intermediate result.178/// \param __Y179/// A 128-bit vector of [4 x i32] containing previous message values.180/// \returns A 128-bit vector of [4 x i32] containing the updated SHA-256181/// values.182static __inline__ __m128i __DEFAULT_FN_ATTRS183_mm_sha256msg2_epu32(__m128i __X, __m128i __Y)184{185 return (__m128i)__builtin_ia32_sha256msg2((__v4si)__X, (__v4si)__Y);186}187 188#undef __DEFAULT_FN_ATTRS189 190#endif /* __SHAINTRIN_H */191