1 /*===----------------- keylockerintrin.h - KL Intrinsics -------------------===
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 *===-----------------------------------------------------------------------===
25 #error "Never use <keylockerintrin.h> directly; include <immintrin.h> instead."
28 #ifndef _KEYLOCKERINTRIN_H
29 #define _KEYLOCKERINTRIN_H
31 #if !defined(__SCE__) || __has_feature(modules) || defined(__KL__)
33 /* Define the default attributes for the functions in this file. */
34 #define __DEFAULT_FN_ATTRS \
35 __attribute__((__always_inline__, __nodebug__, __target__("kl"),\
36 __min_vector_width__(128)))
38 /// Load internal wrapping key from __intkey, __enkey_lo and __enkey_hi. __ctl
39 /// will assigned to EAX, whch specifies the KeySource and whether backing up
40 /// the key is permitted. The 256-bit encryption key is loaded from the two
41 /// explicit operands (__enkey_lo and __enkey_hi). The 128-bit integrity key is
42 /// loaded from the implicit operand XMM0 which assigned by __intkey.
44 /// \headerfile <x86intrin.h>
46 /// This intrinsic corresponds to the <c> LOADIWKEY </c> instructions.
49 /// IF CPL > 0 // LOADKWKEY only allowed at ring 0 (supervisor mode)
52 /// IF “LOADIWKEY exiting” VM execution control set
55 /// IF __ctl[4:1] > 1 // Reserved KeySource encoding used
58 /// IF __ctl[31:5] != 0 // Reserved bit in __ctl is set
61 /// IF __ctl[0] AND (CPUID.19H.ECX[0] == 0) // NoBackup is not supported on this part
64 /// IF (__ctl[4:1] == 1) AND (CPUID.19H.ECX[1] == 0) // KeySource of 1 is not supported on this part
67 /// IF (__ctl[4:1] == 0) // KeySource of 0.
68 /// IWKey.Encryption Key[127:0] := __enkey_hi[127:0]:
69 /// IWKey.Encryption Key[255:128] := __enkey_lo[127:0]
70 /// IWKey.IntegrityKey[127:0] := __intkey[127:0]
71 /// IWKey.NoBackup := __ctl[0]
72 /// IWKey.KeySource := __ctl[4:1]
74 /// ELSE // KeySource of 1. See RDSEED definition for details of randomness
75 /// IF HW_NRND_GEN.ready == 1 // Full-entropy random data from RDSEED was received
76 /// IWKey.Encryption Key[127:0] := __enkey_hi[127:0] XOR HW_NRND_GEN.data[127:0]
77 /// IWKey.Encryption Key[255:128] := __enkey_lo[127:0] XOR HW_NRND_GEN.data[255:128]
78 /// IWKey.Encryption Key[255:0] := __enkey_hi[127:0]:__enkey_lo[127:0] XOR HW_NRND_GEN.data[255:0]
79 /// IWKey.IntegrityKey[127:0] := __intkey[127:0] XOR HW_NRND_GEN.data[383:256]
80 /// IWKey.NoBackup := __ctl[0]
81 /// IWKey.KeySource := __ctl[4:1]
83 /// ELSE // Random data was not returned from RDSEED. IWKey was not loaded
94 static __inline__
void __DEFAULT_FN_ATTRS
95 _mm_loadiwkey (unsigned int __ctl
, __m128i __intkey
,
96 __m128i __enkey_lo
, __m128i __enkey_hi
) {
97 __builtin_ia32_loadiwkey (__intkey
, __enkey_lo
, __enkey_hi
, __ctl
);
100 /// Wrap a 128-bit AES key from __key into a key handle and output in
101 /// ((__m128i*)__h) to ((__m128i*)__h) + 2 and a 32-bit value as return.
102 /// The explicit source operand __htype specifies handle restrictions.
104 /// \headerfile <x86intrin.h>
106 /// This intrinsic corresponds to the <c> ENCODEKEY128 </c> instructions.
108 /// \code{.operation}
109 /// InputKey[127:0] := __key[127:0]
110 /// KeyMetadata[2:0] := __htype[2:0]
111 /// KeyMetadata[23:3] := 0 // Reserved for future usage
112 /// KeyMetadata[27:24] := 0 // KeyType is AES-128 (value of 0)
113 /// KeyMetadata[127:28] := 0 // Reserved for future usage
114 /// Handle[383:0] := WrapKey128(InputKey[127:0], KeyMetadata[127:0],
115 /// IWKey.Integrity Key[127:0], IWKey.Encryption Key[255:0])
116 /// dst[0] := IWKey.NoBackup
117 /// dst[4:1] := IWKey.KeySource[3:0]
119 /// MEM[__h+127:__h] := Handle[127:0] // AAD
120 /// MEM[__h+255:__h+128] := Handle[255:128] // Integrity Tag
121 /// MEM[__h+383:__h+256] := Handle[383:256] // CipherText
129 static __inline__
unsigned int __DEFAULT_FN_ATTRS
130 _mm_encodekey128_u32(unsigned int __htype
, __m128i __key
, void *__h
) {
131 return __builtin_ia32_encodekey128_u32(__htype
, (__v2di
)__key
, __h
);
134 /// Wrap a 256-bit AES key from __key_hi:__key_lo into a key handle, then
135 /// output handle in ((__m128i*)__h) to ((__m128i*)__h) + 3 and
136 /// a 32-bit value as return.
137 /// The explicit source operand __htype specifies handle restrictions.
139 /// \headerfile <x86intrin.h>
141 /// This intrinsic corresponds to the <c> ENCODEKEY256 </c> instructions.
143 /// \code{.operation}
144 /// InputKey[127:0] := __key_lo[127:0]
145 /// InputKey[255:128] := __key_hi[255:128]
146 /// KeyMetadata[2:0] := __htype[2:0]
147 /// KeyMetadata[23:3] := 0 // Reserved for future usage
148 /// KeyMetadata[27:24] := 1 // KeyType is AES-256 (value of 1)
149 /// KeyMetadata[127:28] := 0 // Reserved for future usage
150 /// Handle[511:0] := WrapKey256(InputKey[255:0], KeyMetadata[127:0],
151 /// IWKey.Integrity Key[127:0], IWKey.Encryption Key[255:0])
152 /// dst[0] := IWKey.NoBackup
153 /// dst[4:1] := IWKey.KeySource[3:0]
155 /// MEM[__h+127:__h] := Handle[127:0] // AAD
156 /// MEM[__h+255:__h+128] := Handle[255:128] // Tag
157 /// MEM[__h+383:__h+256] := Handle[383:256] // CipherText[127:0]
158 /// MEM[__h+511:__h+384] := Handle[511:384] // CipherText[255:128]
166 static __inline__
unsigned int __DEFAULT_FN_ATTRS
167 _mm_encodekey256_u32(unsigned int __htype
, __m128i __key_lo
, __m128i __key_hi
,
169 return __builtin_ia32_encodekey256_u32(__htype
, (__v2di
)__key_lo
,
170 (__v2di
)__key_hi
, __h
);
173 /// The AESENC128KL performs 10 rounds of AES to encrypt the __idata using
174 /// the 128-bit key in the handle from the __h. It stores the result in the
175 /// __odata. And return the affected ZF flag status.
177 /// \headerfile <x86intrin.h>
179 /// This intrinsic corresponds to the <c> AESENC128KL </c> instructions.
181 /// \code{.operation}
182 /// Handle[383:0] := MEM[__h+383:__h] // Load is not guaranteed to be atomic.
183 /// IllegalHandle := ( HandleReservedBitSet (Handle[383:0]) ||
184 /// (Handle[127:0] AND (CPL > 0)) ||
185 /// Handle[383:256] ||
186 /// HandleKeyType (Handle[383:0]) != HANDLE_KEY_TYPE_AES128 )
187 /// IF (IllegalHandle)
190 /// (UnwrappedKey, Authentic) := UnwrapKeyAndAuthenticate384 (Handle[383:0], IWKey)
191 /// IF (Authentic == 0)
194 /// MEM[__odata+127:__odata] := AES128Encrypt (__idata[127:0], UnwrappedKey)
205 static __inline__
unsigned char __DEFAULT_FN_ATTRS
206 _mm_aesenc128kl_u8(__m128i
* __odata
, __m128i __idata
, const void *__h
) {
207 return __builtin_ia32_aesenc128kl_u8((__v2di
*)__odata
, (__v2di
)__idata
, __h
);
210 /// The AESENC256KL performs 14 rounds of AES to encrypt the __idata using
211 /// the 256-bit key in the handle from the __h. It stores the result in the
212 /// __odata. And return the affected ZF flag status.
214 /// \headerfile <x86intrin.h>
216 /// This intrinsic corresponds to the <c> AESENC256KL </c> instructions.
218 /// \code{.operation}
219 /// Handle[511:0] := MEM[__h+511:__h] // Load is not guaranteed to be atomic.
220 /// IllegalHandle := ( HandleReservedBitSet (Handle[511:0]) ||
221 /// (Handle[127:0] AND (CPL > 0)) ||
222 /// Handle[255:128] ||
223 /// HandleKeyType (Handle[511:0]) != HANDLE_KEY_TYPE_AES256 )
224 /// IF (IllegalHandle)
226 /// MEM[__odata+127:__odata] := 0
228 /// (UnwrappedKey, Authentic) := UnwrapKeyAndAuthenticate512 (Handle[511:0], IWKey)
229 /// IF (Authentic == 0)
231 /// MEM[__odata+127:__odata] := 0
233 /// MEM[__odata+127:__odata] := AES256Encrypt (__idata[127:0], UnwrappedKey)
244 static __inline__
unsigned char __DEFAULT_FN_ATTRS
245 _mm_aesenc256kl_u8(__m128i
* __odata
, __m128i __idata
, const void *__h
) {
246 return __builtin_ia32_aesenc256kl_u8((__v2di
*)__odata
, (__v2di
)__idata
, __h
);
249 /// The AESDEC128KL performs 10 rounds of AES to decrypt the __idata using
250 /// the 128-bit key in the handle from the __h. It stores the result in the
251 /// __odata. And return the affected ZF flag status.
253 /// \headerfile <x86intrin.h>
255 /// This intrinsic corresponds to the <c> AESDEC128KL </c> instructions.
257 /// \code{.operation}
258 /// Handle[383:0] := MEM[__h+383:__h] // Load is not guaranteed to be atomic.
259 /// IllegalHandle := (HandleReservedBitSet (Handle[383:0]) ||
260 /// (Handle[127:0] AND (CPL > 0)) ||
261 /// Handle[383:256] ||
262 /// HandleKeyType (Handle[383:0]) != HANDLE_KEY_TYPE_AES128)
263 /// IF (IllegalHandle)
265 /// MEM[__odata+127:__odata] := 0
267 /// (UnwrappedKey, Authentic) := UnwrapKeyAndAuthenticate384 (Handle[383:0], IWKey)
268 /// IF (Authentic == 0)
270 /// MEM[__odata+127:__odata] := 0
272 /// MEM[__odata+127:__odata] := AES128Decrypt (__idata[127:0], UnwrappedKey)
283 static __inline__
unsigned char __DEFAULT_FN_ATTRS
284 _mm_aesdec128kl_u8(__m128i
* __odata
, __m128i __idata
, const void *__h
) {
285 return __builtin_ia32_aesdec128kl_u8((__v2di
*)__odata
, (__v2di
)__idata
, __h
);
288 /// The AESDEC256KL performs 10 rounds of AES to decrypt the __idata using
289 /// the 256-bit key in the handle from the __h. It stores the result in the
290 /// __odata. And return the affected ZF flag status.
292 /// \headerfile <x86intrin.h>
294 /// This intrinsic corresponds to the <c> AESDEC256KL </c> instructions.
296 /// \code{.operation}
297 /// Handle[511:0] := MEM[__h+511:__h]
298 /// IllegalHandle := (HandleReservedBitSet (Handle[511:0]) ||
299 /// (Handle[127:0] AND (CPL > 0)) ||
300 /// Handle[383:256] ||
301 /// HandleKeyType (Handle[511:0]) != HANDLE_KEY_TYPE_AES256)
302 /// IF (IllegalHandle)
304 /// MEM[__odata+127:__odata] := 0
306 /// (UnwrappedKey, Authentic) := UnwrapKeyAndAuthenticate512 (Handle[511:0], IWKey)
307 /// IF (Authentic == 0)
309 /// MEM[__odata+127:__odata] := 0
311 /// MEM[__odata+127:__odata] := AES256Decrypt (__idata[127:0], UnwrappedKey)
322 static __inline__
unsigned char __DEFAULT_FN_ATTRS
323 _mm_aesdec256kl_u8(__m128i
* __odata
, __m128i __idata
, const void *__h
) {
324 return __builtin_ia32_aesdec256kl_u8((__v2di
*)__odata
, (__v2di
)__idata
, __h
);
327 #undef __DEFAULT_FN_ATTRS
329 #endif /* !defined(__SCE__ || __has_feature(modules) || defined(__KL__) */
331 #if !defined(__SCE__) || __has_feature(modules) || defined(__WIDEKL__)
333 /* Define the default attributes for the functions in this file. */
334 #define __DEFAULT_FN_ATTRS \
335 __attribute__((__always_inline__, __nodebug__, __target__("kl,widekl"),\
336 __min_vector_width__(128)))
338 /// Encrypt __idata[0] to __idata[7] using 128-bit AES key indicated by handle
339 /// at __h and store each resultant block back from __odata to __odata+7. And
340 /// return the affected ZF flag status.
342 /// \headerfile <x86intrin.h>
344 /// This intrinsic corresponds to the <c> AESENCWIDE128KL </c> instructions.
346 /// \code{.operation}
347 /// Handle := MEM[__h+383:__h]
348 /// IllegalHandle := ( HandleReservedBitSet (Handle[383:0]) ||
349 /// (Handle[127:0] AND (CPL > 0)) ||
350 /// Handle[255:128] ||
351 /// HandleKeyType (Handle[383:0]) != HANDLE_KEY_TYPE_AES128 )
352 /// IF (IllegalHandle)
358 /// (UnwrappedKey, Authentic) := UnwrapKeyAndAuthenticate384 (Handle[383:0], IWKey)
359 /// IF Authentic == 0
366 /// __odata[i] := AES128Encrypt (__idata[i], UnwrappedKey)
378 static __inline__
unsigned char __DEFAULT_FN_ATTRS
379 _mm_aesencwide128kl_u8(__m128i __odata
[8], const __m128i __idata
[8], const void* __h
) {
380 return __builtin_ia32_aesencwide128kl_u8((__v2di
*)__odata
,
381 (const __v2di
*)__idata
, __h
);
384 /// Encrypt __idata[0] to __idata[7] using 256-bit AES key indicated by handle
385 /// at __h and store each resultant block back from __odata to __odata+7. And
386 /// return the affected ZF flag status.
388 /// \headerfile <x86intrin.h>
390 /// This intrinsic corresponds to the <c> AESENCWIDE256KL </c> instructions.
392 /// \code{.operation}
393 /// Handle[511:0] := MEM[__h+511:__h]
394 /// IllegalHandle := ( HandleReservedBitSet (Handle[511:0]) ||
395 /// (Handle[127:0] AND (CPL > 0)) ||
396 /// Handle[255:128] ||
397 /// HandleKeyType (Handle[511:0]) != HANDLE_KEY_TYPE_AES512 )
398 /// IF (IllegalHandle)
404 /// (UnwrappedKey, Authentic) := UnwrapKeyAndAuthenticate512 (Handle[511:0], IWKey)
405 /// IF Authentic == 0
412 /// __odata[i] := AES256Encrypt (__idata[i], UnwrappedKey)
424 static __inline__
unsigned char __DEFAULT_FN_ATTRS
425 _mm_aesencwide256kl_u8(__m128i __odata
[8], const __m128i __idata
[8], const void* __h
) {
426 return __builtin_ia32_aesencwide256kl_u8((__v2di
*)__odata
,
427 (const __v2di
*)__idata
, __h
);
430 /// Decrypt __idata[0] to __idata[7] using 128-bit AES key indicated by handle
431 /// at __h and store each resultant block back from __odata to __odata+7. And
432 /// return the affected ZF flag status.
434 /// \headerfile <x86intrin.h>
436 /// This intrinsic corresponds to the <c> AESDECWIDE128KL </c> instructions.
438 /// \code{.operation}
439 /// Handle[383:0] := MEM[__h+383:__h]
440 /// IllegalHandle := ( HandleReservedBitSet (Handle[383:0]) ||
441 /// (Handle[127:0] AND (CPL > 0)) ||
442 /// Handle[255:128] ||
443 /// HandleKeyType (Handle) != HANDLE_KEY_TYPE_AES128 )
444 /// IF (IllegalHandle)
450 /// (UnwrappedKey, Authentic) := UnwrapKeyAndAuthenticate384 (Handle[383:0], IWKey)
451 /// IF Authentic == 0
458 /// __odata[i] := AES128Decrypt (__idata[i], UnwrappedKey)
470 static __inline__
unsigned char __DEFAULT_FN_ATTRS
471 _mm_aesdecwide128kl_u8(__m128i __odata
[8], const __m128i __idata
[8], const void* __h
) {
472 return __builtin_ia32_aesdecwide128kl_u8((__v2di
*)__odata
,
473 (const __v2di
*)__idata
, __h
);
476 /// Decrypt __idata[0] to __idata[7] using 256-bit AES key indicated by handle
477 /// at __h and store each resultant block back from __odata to __odata+7. And
478 /// return the affected ZF flag status.
480 /// \headerfile <x86intrin.h>
482 /// This intrinsic corresponds to the <c> AESDECWIDE256KL </c> instructions.
484 /// \code{.operation}
485 /// Handle[511:0] := MEM[__h+511:__h]
486 /// IllegalHandle = ( HandleReservedBitSet (Handle[511:0]) ||
487 /// (Handle[127:0] AND (CPL > 0)) ||
488 /// Handle[255:128] ||
489 /// HandleKeyType (Handle) != HANDLE_KEY_TYPE_AES512 )
490 /// If (IllegalHandle)
496 /// (UnwrappedKey, Authentic) := UnwrapKeyAndAuthenticate512 (Handle[511:0], IWKey)
497 /// IF Authentic == 0
504 /// __odata[i] := AES256Decrypt (__idata[i], UnwrappedKey)
516 static __inline__
unsigned char __DEFAULT_FN_ATTRS
517 _mm_aesdecwide256kl_u8(__m128i __odata
[8], const __m128i __idata
[8], const void* __h
) {
518 return __builtin_ia32_aesdecwide256kl_u8((__v2di
*)__odata
,
519 (const __v2di
*)__idata
, __h
);
522 #undef __DEFAULT_FN_ATTRS
524 #endif /* !defined(__SCE__) || __has_feature(modules) || defined(__WIDEKL__) \
527 #endif /* _KEYLOCKERINTRIN_H */