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(_MSC_VER) || defined(__SCE__)) || __has_feature(modules) || \
34 /* Define the default attributes for the functions in this file. */
35 #define __DEFAULT_FN_ATTRS \
36 __attribute__((__always_inline__, __nodebug__, __target__("kl"),\
37 __min_vector_width__(128)))
39 /// Load internal wrapping key from __intkey, __enkey_lo and __enkey_hi. __ctl
40 /// will assigned to EAX, whch specifies the KeySource and whether backing up
41 /// the key is permitted. The 256-bit encryption key is loaded from the two
42 /// explicit operands (__enkey_lo and __enkey_hi). The 128-bit integrity key is
43 /// loaded from the implicit operand XMM0 which assigned by __intkey.
45 /// \headerfile <x86intrin.h>
47 /// This intrinsic corresponds to the <c> LOADIWKEY </c> instructions.
50 /// IF CPL > 0 // LOADKWKEY only allowed at ring 0 (supervisor mode)
53 /// IF “LOADIWKEY exiting” VM execution control set
56 /// IF __ctl[4:1] > 1 // Reserved KeySource encoding used
59 /// IF __ctl[31:5] != 0 // Reserved bit in __ctl is set
62 /// IF __ctl[0] AND (CPUID.19H.ECX[0] == 0) // NoBackup is not supported on this part
65 /// IF (__ctl[4:1] == 1) AND (CPUID.19H.ECX[1] == 0) // KeySource of 1 is not supported on this part
68 /// IF (__ctl[4:1] == 0) // KeySource of 0.
69 /// IWKey.Encryption Key[127:0] := __enkey_hi[127:0]:
70 /// IWKey.Encryption Key[255:128] := __enkey_lo[127:0]
71 /// IWKey.IntegrityKey[127:0] := __intkey[127:0]
72 /// IWKey.NoBackup := __ctl[0]
73 /// IWKey.KeySource := __ctl[4:1]
75 /// ELSE // KeySource of 1. See RDSEED definition for details of randomness
76 /// IF HW_NRND_GEN.ready == 1 // Full-entropy random data from RDSEED was received
77 /// IWKey.Encryption Key[127:0] := __enkey_hi[127:0] XOR HW_NRND_GEN.data[127:0]
78 /// IWKey.Encryption Key[255:128] := __enkey_lo[127:0] XOR HW_NRND_GEN.data[255:128]
79 /// IWKey.Encryption Key[255:0] := __enkey_hi[127:0]:__enkey_lo[127:0] XOR HW_NRND_GEN.data[255:0]
80 /// IWKey.IntegrityKey[127:0] := __intkey[127:0] XOR HW_NRND_GEN.data[383:256]
81 /// IWKey.NoBackup := __ctl[0]
82 /// IWKey.KeySource := __ctl[4:1]
84 /// ELSE // Random data was not returned from RDSEED. IWKey was not loaded
95 static __inline__
void __DEFAULT_FN_ATTRS
96 _mm_loadiwkey (unsigned int __ctl
, __m128i __intkey
,
97 __m128i __enkey_lo
, __m128i __enkey_hi
) {
98 __builtin_ia32_loadiwkey (__intkey
, __enkey_lo
, __enkey_hi
, __ctl
);
101 /// Wrap a 128-bit AES key from __key into a key handle and output in
102 /// ((__m128i*)__h) to ((__m128i*)__h) + 2 and a 32-bit value as return.
103 /// The explicit source operand __htype specifies handle restrictions.
105 /// \headerfile <x86intrin.h>
107 /// This intrinsic corresponds to the <c> ENCODEKEY128 </c> instructions.
109 /// \code{.operation}
110 /// InputKey[127:0] := __key[127:0]
111 /// KeyMetadata[2:0] := __htype[2:0]
112 /// KeyMetadata[23:3] := 0 // Reserved for future usage
113 /// KeyMetadata[27:24] := 0 // KeyType is AES-128 (value of 0)
114 /// KeyMetadata[127:28] := 0 // Reserved for future usage
115 /// Handle[383:0] := WrapKey128(InputKey[127:0], KeyMetadata[127:0],
116 /// IWKey.Integrity Key[127:0], IWKey.Encryption Key[255:0])
117 /// dst[0] := IWKey.NoBackup
118 /// dst[4:1] := IWKey.KeySource[3:0]
120 /// MEM[__h+127:__h] := Handle[127:0] // AAD
121 /// MEM[__h+255:__h+128] := Handle[255:128] // Integrity Tag
122 /// MEM[__h+383:__h+256] := Handle[383:256] // CipherText
130 static __inline__
unsigned int __DEFAULT_FN_ATTRS
131 _mm_encodekey128_u32(unsigned int __htype
, __m128i __key
, void *__h
) {
132 return __builtin_ia32_encodekey128_u32(__htype
, (__v2di
)__key
, __h
);
135 /// Wrap a 256-bit AES key from __key_hi:__key_lo into a key handle, then
136 /// output handle in ((__m128i*)__h) to ((__m128i*)__h) + 3 and
137 /// a 32-bit value as return.
138 /// The explicit source operand __htype specifies handle restrictions.
140 /// \headerfile <x86intrin.h>
142 /// This intrinsic corresponds to the <c> ENCODEKEY256 </c> instructions.
144 /// \code{.operation}
145 /// InputKey[127:0] := __key_lo[127:0]
146 /// InputKey[255:128] := __key_hi[255:128]
147 /// KeyMetadata[2:0] := __htype[2:0]
148 /// KeyMetadata[23:3] := 0 // Reserved for future usage
149 /// KeyMetadata[27:24] := 1 // KeyType is AES-256 (value of 1)
150 /// KeyMetadata[127:28] := 0 // Reserved for future usage
151 /// Handle[511:0] := WrapKey256(InputKey[255:0], KeyMetadata[127:0],
152 /// IWKey.Integrity Key[127:0], IWKey.Encryption Key[255:0])
153 /// dst[0] := IWKey.NoBackup
154 /// dst[4:1] := IWKey.KeySource[3:0]
156 /// MEM[__h+127:__h] := Handle[127:0] // AAD
157 /// MEM[__h+255:__h+128] := Handle[255:128] // Tag
158 /// MEM[__h+383:__h+256] := Handle[383:256] // CipherText[127:0]
159 /// MEM[__h+511:__h+384] := Handle[511:384] // CipherText[255:128]
167 static __inline__
unsigned int __DEFAULT_FN_ATTRS
168 _mm_encodekey256_u32(unsigned int __htype
, __m128i __key_lo
, __m128i __key_hi
,
170 return __builtin_ia32_encodekey256_u32(__htype
, (__v2di
)__key_lo
,
171 (__v2di
)__key_hi
, __h
);
174 /// The AESENC128KL performs 10 rounds of AES to encrypt the __idata using
175 /// the 128-bit key in the handle from the __h. It stores the result in the
176 /// __odata. And return the affected ZF flag status.
178 /// \headerfile <x86intrin.h>
180 /// This intrinsic corresponds to the <c> AESENC128KL </c> instructions.
182 /// \code{.operation}
183 /// Handle[383:0] := MEM[__h+383:__h] // Load is not guaranteed to be atomic.
184 /// IllegalHandle := ( HandleReservedBitSet (Handle[383:0]) ||
185 /// (Handle[127:0] AND (CPL > 0)) ||
186 /// Handle[383:256] ||
187 /// HandleKeyType (Handle[383:0]) != HANDLE_KEY_TYPE_AES128 )
188 /// IF (IllegalHandle)
191 /// (UnwrappedKey, Authentic) := UnwrapKeyAndAuthenticate384 (Handle[383:0], IWKey)
192 /// IF (Authentic == 0)
195 /// MEM[__odata+127:__odata] := AES128Encrypt (__idata[127:0], UnwrappedKey)
206 static __inline__
unsigned char __DEFAULT_FN_ATTRS
207 _mm_aesenc128kl_u8(__m128i
* __odata
, __m128i __idata
, const void *__h
) {
208 return __builtin_ia32_aesenc128kl_u8((__v2di
*)__odata
, (__v2di
)__idata
, __h
);
211 /// The AESENC256KL performs 14 rounds of AES to encrypt the __idata using
212 /// the 256-bit key in the handle from the __h. It stores the result in the
213 /// __odata. And return the affected ZF flag status.
215 /// \headerfile <x86intrin.h>
217 /// This intrinsic corresponds to the <c> AESENC256KL </c> instructions.
219 /// \code{.operation}
220 /// Handle[511:0] := MEM[__h+511:__h] // Load is not guaranteed to be atomic.
221 /// IllegalHandle := ( HandleReservedBitSet (Handle[511:0]) ||
222 /// (Handle[127:0] AND (CPL > 0)) ||
223 /// Handle[255:128] ||
224 /// HandleKeyType (Handle[511:0]) != HANDLE_KEY_TYPE_AES256 )
225 /// IF (IllegalHandle)
227 /// MEM[__odata+127:__odata] := 0
229 /// (UnwrappedKey, Authentic) := UnwrapKeyAndAuthenticate512 (Handle[511:0], IWKey)
230 /// IF (Authentic == 0)
232 /// MEM[__odata+127:__odata] := 0
234 /// MEM[__odata+127:__odata] := AES256Encrypt (__idata[127:0], UnwrappedKey)
245 static __inline__
unsigned char __DEFAULT_FN_ATTRS
246 _mm_aesenc256kl_u8(__m128i
* __odata
, __m128i __idata
, const void *__h
) {
247 return __builtin_ia32_aesenc256kl_u8((__v2di
*)__odata
, (__v2di
)__idata
, __h
);
250 /// The AESDEC128KL performs 10 rounds of AES to decrypt the __idata using
251 /// the 128-bit key in the handle from the __h. It stores the result in the
252 /// __odata. And return the affected ZF flag status.
254 /// \headerfile <x86intrin.h>
256 /// This intrinsic corresponds to the <c> AESDEC128KL </c> instructions.
258 /// \code{.operation}
259 /// Handle[383:0] := MEM[__h+383:__h] // Load is not guaranteed to be atomic.
260 /// IllegalHandle := (HandleReservedBitSet (Handle[383:0]) ||
261 /// (Handle[127:0] AND (CPL > 0)) ||
262 /// Handle[383:256] ||
263 /// HandleKeyType (Handle[383:0]) != HANDLE_KEY_TYPE_AES128)
264 /// IF (IllegalHandle)
266 /// MEM[__odata+127:__odata] := 0
268 /// (UnwrappedKey, Authentic) := UnwrapKeyAndAuthenticate384 (Handle[383:0], IWKey)
269 /// IF (Authentic == 0)
271 /// MEM[__odata+127:__odata] := 0
273 /// MEM[__odata+127:__odata] := AES128Decrypt (__idata[127:0], UnwrappedKey)
284 static __inline__
unsigned char __DEFAULT_FN_ATTRS
285 _mm_aesdec128kl_u8(__m128i
* __odata
, __m128i __idata
, const void *__h
) {
286 return __builtin_ia32_aesdec128kl_u8((__v2di
*)__odata
, (__v2di
)__idata
, __h
);
289 /// The AESDEC256KL performs 10 rounds of AES to decrypt the __idata using
290 /// the 256-bit key in the handle from the __h. It stores the result in the
291 /// __odata. And return the affected ZF flag status.
293 /// \headerfile <x86intrin.h>
295 /// This intrinsic corresponds to the <c> AESDEC256KL </c> instructions.
297 /// \code{.operation}
298 /// Handle[511:0] := MEM[__h+511:__h]
299 /// IllegalHandle := (HandleReservedBitSet (Handle[511:0]) ||
300 /// (Handle[127:0] AND (CPL > 0)) ||
301 /// Handle[383:256] ||
302 /// HandleKeyType (Handle[511:0]) != HANDLE_KEY_TYPE_AES256)
303 /// IF (IllegalHandle)
305 /// MEM[__odata+127:__odata] := 0
307 /// (UnwrappedKey, Authentic) := UnwrapKeyAndAuthenticate512 (Handle[511:0], IWKey)
308 /// IF (Authentic == 0)
310 /// MEM[__odata+127:__odata] := 0
312 /// MEM[__odata+127:__odata] := AES256Decrypt (__idata[127:0], UnwrappedKey)
323 static __inline__
unsigned char __DEFAULT_FN_ATTRS
324 _mm_aesdec256kl_u8(__m128i
* __odata
, __m128i __idata
, const void *__h
) {
325 return __builtin_ia32_aesdec256kl_u8((__v2di
*)__odata
, (__v2di
)__idata
, __h
);
328 #undef __DEFAULT_FN_ATTRS
330 #endif /* !(defined(_MSC_VER) || defined(__SCE__)) || __has_feature(modules) \
331 || defined(__KL__) */
333 #if !(defined(_MSC_VER) || defined(__SCE__)) || __has_feature(modules) || \
336 /* Define the default attributes for the functions in this file. */
337 #define __DEFAULT_FN_ATTRS \
338 __attribute__((__always_inline__, __nodebug__, __target__("kl,widekl"),\
339 __min_vector_width__(128)))
341 /// Encrypt __idata[0] to __idata[7] using 128-bit AES key indicated by handle
342 /// at __h and store each resultant block back from __odata to __odata+7. And
343 /// return the affected ZF flag status.
345 /// \headerfile <x86intrin.h>
347 /// This intrinsic corresponds to the <c> AESENCWIDE128KL </c> instructions.
349 /// \code{.operation}
350 /// Handle := MEM[__h+383:__h]
351 /// IllegalHandle := ( HandleReservedBitSet (Handle[383:0]) ||
352 /// (Handle[127:0] AND (CPL > 0)) ||
353 /// Handle[255:128] ||
354 /// HandleKeyType (Handle[383:0]) != HANDLE_KEY_TYPE_AES128 )
355 /// IF (IllegalHandle)
361 /// (UnwrappedKey, Authentic) := UnwrapKeyAndAuthenticate384 (Handle[383:0], IWKey)
362 /// IF Authentic == 0
369 /// __odata[i] := AES128Encrypt (__idata[i], UnwrappedKey)
381 static __inline__
unsigned char __DEFAULT_FN_ATTRS
382 _mm_aesencwide128kl_u8(__m128i __odata
[8], const __m128i __idata
[8], const void* __h
) {
383 return __builtin_ia32_aesencwide128kl_u8((__v2di
*)__odata
,
384 (const __v2di
*)__idata
, __h
);
387 /// Encrypt __idata[0] to __idata[7] using 256-bit AES key indicated by handle
388 /// at __h and store each resultant block back from __odata to __odata+7. And
389 /// return the affected ZF flag status.
391 /// \headerfile <x86intrin.h>
393 /// This intrinsic corresponds to the <c> AESENCWIDE256KL </c> instructions.
395 /// \code{.operation}
396 /// Handle[511:0] := MEM[__h+511:__h]
397 /// IllegalHandle := ( HandleReservedBitSet (Handle[511:0]) ||
398 /// (Handle[127:0] AND (CPL > 0)) ||
399 /// Handle[255:128] ||
400 /// HandleKeyType (Handle[511:0]) != HANDLE_KEY_TYPE_AES512 )
401 /// IF (IllegalHandle)
407 /// (UnwrappedKey, Authentic) := UnwrapKeyAndAuthenticate512 (Handle[511:0], IWKey)
408 /// IF Authentic == 0
415 /// __odata[i] := AES256Encrypt (__idata[i], UnwrappedKey)
427 static __inline__
unsigned char __DEFAULT_FN_ATTRS
428 _mm_aesencwide256kl_u8(__m128i __odata
[8], const __m128i __idata
[8], const void* __h
) {
429 return __builtin_ia32_aesencwide256kl_u8((__v2di
*)__odata
,
430 (const __v2di
*)__idata
, __h
);
433 /// Decrypt __idata[0] to __idata[7] using 128-bit AES key indicated by handle
434 /// at __h and store each resultant block back from __odata to __odata+7. And
435 /// return the affected ZF flag status.
437 /// \headerfile <x86intrin.h>
439 /// This intrinsic corresponds to the <c> AESDECWIDE128KL </c> instructions.
441 /// \code{.operation}
442 /// Handle[383:0] := MEM[__h+383:__h]
443 /// IllegalHandle := ( HandleReservedBitSet (Handle[383:0]) ||
444 /// (Handle[127:0] AND (CPL > 0)) ||
445 /// Handle[255:128] ||
446 /// HandleKeyType (Handle) != HANDLE_KEY_TYPE_AES128 )
447 /// IF (IllegalHandle)
453 /// (UnwrappedKey, Authentic) := UnwrapKeyAndAuthenticate384 (Handle[383:0], IWKey)
454 /// IF Authentic == 0
461 /// __odata[i] := AES128Decrypt (__idata[i], UnwrappedKey)
473 static __inline__
unsigned char __DEFAULT_FN_ATTRS
474 _mm_aesdecwide128kl_u8(__m128i __odata
[8], const __m128i __idata
[8], const void* __h
) {
475 return __builtin_ia32_aesdecwide128kl_u8((__v2di
*)__odata
,
476 (const __v2di
*)__idata
, __h
);
479 /// Decrypt __idata[0] to __idata[7] using 256-bit AES key indicated by handle
480 /// at __h and store each resultant block back from __odata to __odata+7. And
481 /// return the affected ZF flag status.
483 /// \headerfile <x86intrin.h>
485 /// This intrinsic corresponds to the <c> AESDECWIDE256KL </c> instructions.
487 /// \code{.operation}
488 /// Handle[511:0] := MEM[__h+511:__h]
489 /// IllegalHandle = ( HandleReservedBitSet (Handle[511:0]) ||
490 /// (Handle[127:0] AND (CPL > 0)) ||
491 /// Handle[255:128] ||
492 /// HandleKeyType (Handle) != HANDLE_KEY_TYPE_AES512 )
493 /// If (IllegalHandle)
499 /// (UnwrappedKey, Authentic) := UnwrapKeyAndAuthenticate512 (Handle[511:0], IWKey)
500 /// IF Authentic == 0
507 /// __odata[i] := AES256Decrypt (__idata[i], UnwrappedKey)
519 static __inline__
unsigned char __DEFAULT_FN_ATTRS
520 _mm_aesdecwide256kl_u8(__m128i __odata
[8], const __m128i __idata
[8], const void* __h
) {
521 return __builtin_ia32_aesdecwide256kl_u8((__v2di
*)__odata
,
522 (const __v2di
*)__idata
, __h
);
525 #undef __DEFAULT_FN_ATTRS
527 #endif /* !(defined(_MSC_VER) || defined(__SCE__)) || __has_feature(modules) \
528 || defined(__WIDEKL__) */
530 #endif /* _KEYLOCKERINTRIN_H */