1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 */
25
26 #if defined(__x86_64) && defined(HAVE_AES)
27
28 #include <sys/simd.h>
29 #include <sys/types.h>
30 #include <sys/asm_linkage.h>
31
32 /* These functions are used to execute AES-NI instructions: */
33 extern ASMABI int rijndael_key_setup_enc_intel(uint32_t rk[],
34 const uint32_t cipherKey[], uint64_t keyBits);
35 extern ASMABI int rijndael_key_setup_dec_intel(uint32_t rk[],
36 const uint32_t cipherKey[], uint64_t keyBits);
37 extern ASMABI void aes_encrypt_intel(const uint32_t rk[], int Nr,
38 const uint32_t pt[4], uint32_t ct[4]);
39 extern ASMABI void aes_decrypt_intel(const uint32_t rk[], int Nr,
40 const uint32_t ct[4], uint32_t pt[4]);
41
42
43 #include <aes/aes_impl.h>
44
45 /*
46 * Expand the 32-bit AES cipher key array into the encryption and decryption
47 * key schedules.
48 *
49 * Parameters:
50 * key AES key schedule to be initialized
51 * keyarr32 User key
52 * keyBits AES key size (128, 192, or 256 bits)
53 */
54 static void
aes_aesni_generate(aes_key_t * key,const uint32_t * keyarr32,int keybits)55 aes_aesni_generate(aes_key_t *key, const uint32_t *keyarr32, int keybits)
56 {
57 kfpu_begin();
58 key->nr = rijndael_key_setup_enc_intel(&(key->encr_ks.ks32[0]),
59 keyarr32, keybits);
60 key->nr = rijndael_key_setup_dec_intel(&(key->decr_ks.ks32[0]),
61 keyarr32, keybits);
62 kfpu_end();
63 }
64
65 /*
66 * Encrypt one block of data. The block is assumed to be an array
67 * of four uint32_t values, so copy for alignment (and byte-order
68 * reversal for little endian systems might be necessary on the
69 * input and output byte streams.
70 * The size of the key schedule depends on the number of rounds
71 * (which can be computed from the size of the key), i.e. 4*(Nr + 1).
72 *
73 * Parameters:
74 * rk Key schedule, of aes_ks_t (60 32-bit integers)
75 * Nr Number of rounds
76 * pt Input block (plain text)
77 * ct Output block (crypto text). Can overlap with pt
78 */
79 static void
aes_aesni_encrypt(const uint32_t rk[],int Nr,const uint32_t pt[4],uint32_t ct[4])80 aes_aesni_encrypt(const uint32_t rk[], int Nr, const uint32_t pt[4],
81 uint32_t ct[4])
82 {
83 kfpu_begin();
84 aes_encrypt_intel(rk, Nr, pt, ct);
85 kfpu_end();
86 }
87
88 /*
89 * Decrypt one block of data. The block is assumed to be an array
90 * of four uint32_t values, so copy for alignment (and byte-order
91 * reversal for little endian systems might be necessary on the
92 * input and output byte streams.
93 * The size of the key schedule depends on the number of rounds
94 * (which can be computed from the size of the key), i.e. 4*(Nr + 1).
95 *
96 * Parameters:
97 * rk Key schedule, of aes_ks_t (60 32-bit integers)
98 * Nr Number of rounds
99 * ct Input block (crypto text)
100 * pt Output block (plain text). Can overlap with pt
101 */
102 static void
aes_aesni_decrypt(const uint32_t rk[],int Nr,const uint32_t ct[4],uint32_t pt[4])103 aes_aesni_decrypt(const uint32_t rk[], int Nr, const uint32_t ct[4],
104 uint32_t pt[4])
105 {
106 kfpu_begin();
107 aes_decrypt_intel(rk, Nr, ct, pt);
108 kfpu_end();
109 }
110
111 static boolean_t
aes_aesni_will_work(void)112 aes_aesni_will_work(void)
113 {
114 return (kfpu_allowed() && zfs_aes_available());
115 }
116
117 const aes_impl_ops_t aes_aesni_impl = {
118 .generate = &aes_aesni_generate,
119 .encrypt = &aes_aesni_encrypt,
120 .decrypt = &aes_aesni_decrypt,
121 .is_supported = &aes_aesni_will_work,
122 .needs_byteswap = B_FALSE,
123 .name = "aesni"
124 };
125
126 #endif /* defined(__x86_64) && defined(HAVE_AES) */
127