@@ -12,12 +12,10 @@ use core::fmt;
12
12
use core:: mem:: ManuallyDrop ;
13
13
14
14
#[ cfg( target_arch = "aarch64" ) ]
15
- use crate :: armv8 as intrinsics ;
15
+ use crate :: armv8 as arch ;
16
16
17
17
#[ cfg( any( target_arch = "x86_64" , target_arch = "x86" ) ) ]
18
- use crate :: ni as intrinsics;
19
-
20
- cpufeatures:: new!( aes_intrinsics, "aes" ) ;
18
+ use crate :: x86 as arch;
21
19
22
20
macro_rules! define_aes_impl {
23
21
(
@@ -29,21 +27,21 @@ macro_rules! define_aes_impl {
29
27
doc = $doc: expr,
30
28
) => {
31
29
mod $module {
32
- use super :: { intrinsics , soft} ;
30
+ use super :: { arch , soft} ;
33
31
use core:: mem:: ManuallyDrop ;
34
32
35
33
pub ( super ) union Inner {
36
- pub ( super ) intrinsics : ManuallyDrop <intrinsics :: $name>,
34
+ pub ( super ) arch : ManuallyDrop <arch :: $name>,
37
35
pub ( super ) soft: ManuallyDrop <soft:: $name>,
38
36
}
39
37
40
38
pub ( super ) union InnerEnc {
41
- pub ( super ) intrinsics : ManuallyDrop <intrinsics :: $name_enc>,
39
+ pub ( super ) arch : ManuallyDrop <arch :: $name_enc>,
42
40
pub ( super ) soft: ManuallyDrop <soft:: $name_enc>,
43
41
}
44
42
45
43
pub ( super ) union InnerDec {
46
- pub ( super ) intrinsics : ManuallyDrop <intrinsics :: $name_dec>,
44
+ pub ( super ) arch : ManuallyDrop <arch :: $name_dec>,
47
45
pub ( super ) soft: ManuallyDrop <soft:: $name_dec>,
48
46
}
49
47
}
@@ -52,7 +50,7 @@ macro_rules! define_aes_impl {
52
50
#[ doc = "block cipher" ]
53
51
pub struct $name {
54
52
inner: $module:: Inner ,
55
- token: aes_intrinsics :: InitToken ,
53
+ token: arch :: features :: aes :: InitToken ,
56
54
}
57
55
58
56
impl KeySizeUser for $name {
@@ -70,9 +68,7 @@ macro_rules! define_aes_impl {
70
68
use core:: ops:: Deref ;
71
69
let inner = if enc. token. get( ) {
72
70
$module:: Inner {
73
- intrinsics: ManuallyDrop :: new( unsafe {
74
- enc. inner. intrinsics. deref( ) . into( )
75
- } ) ,
71
+ arch: ManuallyDrop :: new( unsafe { enc. inner. arch. deref( ) . into( ) } ) ,
76
72
}
77
73
} else {
78
74
$module:: Inner {
@@ -90,11 +86,11 @@ macro_rules! define_aes_impl {
90
86
impl KeyInit for $name {
91
87
#[ inline]
92
88
fn new( key: & Key <Self >) -> Self {
93
- let ( token, aesni_present ) = aes_intrinsics :: init_get( ) ;
89
+ let ( token, aes_features ) = arch :: features :: aes :: init_get( ) ;
94
90
95
- let inner = if aesni_present {
91
+ let inner = if aes_features {
96
92
$module:: Inner {
97
- intrinsics : ManuallyDrop :: new( intrinsics :: $name:: new( key) ) ,
93
+ arch : ManuallyDrop :: new( arch :: $name:: new( key) ) ,
98
94
}
99
95
} else {
100
96
$module:: Inner {
@@ -115,7 +111,7 @@ macro_rules! define_aes_impl {
115
111
fn clone( & self ) -> Self {
116
112
let inner = if self . token. get( ) {
117
113
$module:: Inner {
118
- intrinsics : unsafe { self . inner. intrinsics . clone( ) } ,
114
+ arch : unsafe { self . inner. arch . clone( ) } ,
119
115
}
120
116
} else {
121
117
$module:: Inner {
@@ -136,38 +132,20 @@ macro_rules! define_aes_impl {
136
132
137
133
impl BlockCipherEncrypt for $name {
138
134
fn encrypt_with_backend( & self , f: impl BlockCipherEncClosure <BlockSize = U16 >) {
139
- unsafe {
140
- if self . token. get( ) {
141
- #[ target_feature( enable = "aes" ) ]
142
- unsafe fn inner(
143
- state: & intrinsics:: $name,
144
- f: impl BlockCipherEncClosure <BlockSize = U16 >,
145
- ) {
146
- f. call( state. get_enc_backend( ) ) ;
147
- }
148
- inner( & self . inner. intrinsics, f) ;
149
- } else {
150
- f. call( & self . inner. soft. get_enc_backend( ) ) ;
151
- }
135
+ if self . token. get( ) {
136
+ unsafe { & self . inner. arch } . encrypt_with_backend( f)
137
+ } else {
138
+ unsafe { & self . inner. soft } . encrypt_with_backend( f)
152
139
}
153
140
}
154
141
}
155
142
156
143
impl BlockCipherDecrypt for $name {
157
144
fn decrypt_with_backend( & self , f: impl BlockCipherDecClosure <BlockSize = U16 >) {
158
- unsafe {
159
- if self . token. get( ) {
160
- #[ target_feature( enable = "aes" ) ]
161
- unsafe fn inner(
162
- state: & intrinsics:: $name,
163
- f: impl BlockCipherDecClosure <BlockSize = U16 >,
164
- ) {
165
- f. call( state. get_dec_backend( ) ) ;
166
- }
167
- inner( & self . inner. intrinsics, f) ;
168
- } else {
169
- f. call( & self . inner. soft. get_dec_backend( ) ) ;
170
- }
145
+ if self . token. get( ) {
146
+ unsafe { & self . inner. arch } . decrypt_with_backend( f)
147
+ } else {
148
+ unsafe { & self . inner. soft } . decrypt_with_backend( f)
171
149
}
172
150
}
173
151
}
@@ -188,7 +166,7 @@ macro_rules! define_aes_impl {
188
166
#[ inline]
189
167
fn drop( & mut self ) {
190
168
if self . token. get( ) {
191
- unsafe { ManuallyDrop :: drop( & mut self . inner. intrinsics ) } ;
169
+ unsafe { ManuallyDrop :: drop( & mut self . inner. arch ) } ;
192
170
} else {
193
171
unsafe { ManuallyDrop :: drop( & mut self . inner. soft) } ;
194
172
} ;
@@ -202,7 +180,7 @@ macro_rules! define_aes_impl {
202
180
#[ doc = "block cipher (encrypt-only)" ]
203
181
pub struct $name_enc {
204
182
inner: $module:: InnerEnc ,
205
- token: aes_intrinsics :: InitToken ,
183
+ token: arch :: features :: aes :: InitToken ,
206
184
}
207
185
208
186
impl KeySizeUser for $name_enc {
@@ -212,11 +190,11 @@ macro_rules! define_aes_impl {
212
190
impl KeyInit for $name_enc {
213
191
#[ inline]
214
192
fn new( key: & Key <Self >) -> Self {
215
- let ( token, aesni_present ) = aes_intrinsics :: init_get( ) ;
193
+ let ( token, aes_features ) = arch :: features :: aes :: init_get( ) ;
216
194
217
- let inner = if aesni_present {
195
+ let inner = if aes_features {
218
196
$module:: InnerEnc {
219
- intrinsics : ManuallyDrop :: new( intrinsics :: $name_enc:: new( key) ) ,
197
+ arch : ManuallyDrop :: new( arch :: $name_enc:: new( key) ) ,
220
198
}
221
199
} else {
222
200
$module:: InnerEnc {
@@ -237,7 +215,7 @@ macro_rules! define_aes_impl {
237
215
fn clone( & self ) -> Self {
238
216
let inner = if self . token. get( ) {
239
217
$module:: InnerEnc {
240
- intrinsics : unsafe { self . inner. intrinsics . clone( ) } ,
218
+ arch : unsafe { self . inner. arch . clone( ) } ,
241
219
}
242
220
} else {
243
221
$module:: InnerEnc {
@@ -258,19 +236,10 @@ macro_rules! define_aes_impl {
258
236
259
237
impl BlockCipherEncrypt for $name_enc {
260
238
fn encrypt_with_backend( & self , f: impl BlockCipherEncClosure <BlockSize = U16 >) {
261
- unsafe {
262
- if self . token. get( ) {
263
- #[ target_feature( enable = "aes" ) ]
264
- unsafe fn inner(
265
- state: & intrinsics:: $name_enc,
266
- f: impl BlockCipherEncClosure <BlockSize = U16 >,
267
- ) {
268
- f. call( state. get_enc_backend( ) ) ;
269
- }
270
- inner( & self . inner. intrinsics, f) ;
271
- } else {
272
- f. call( & self . inner. soft. get_enc_backend( ) ) ;
273
- }
239
+ if self . token. get( ) {
240
+ unsafe { & self . inner. arch } . encrypt_with_backend( f)
241
+ } else {
242
+ unsafe { & self . inner. soft } . encrypt_with_backend( f)
274
243
}
275
244
}
276
245
}
@@ -291,7 +260,7 @@ macro_rules! define_aes_impl {
291
260
#[ inline]
292
261
fn drop( & mut self ) {
293
262
if self . token. get( ) {
294
- unsafe { ManuallyDrop :: drop( & mut self . inner. intrinsics ) } ;
263
+ unsafe { ManuallyDrop :: drop( & mut self . inner. arch ) } ;
295
264
} else {
296
265
unsafe { ManuallyDrop :: drop( & mut self . inner. soft) } ;
297
266
} ;
@@ -305,7 +274,7 @@ macro_rules! define_aes_impl {
305
274
#[ doc = "block cipher (decrypt-only)" ]
306
275
pub struct $name_dec {
307
276
inner: $module:: InnerDec ,
308
- token: aes_intrinsics :: InitToken ,
277
+ token: arch :: features :: aes :: InitToken ,
309
278
}
310
279
311
280
impl KeySizeUser for $name_dec {
@@ -324,9 +293,7 @@ macro_rules! define_aes_impl {
324
293
use core:: ops:: Deref ;
325
294
let inner = if enc. token. get( ) {
326
295
$module:: InnerDec {
327
- intrinsics: ManuallyDrop :: new( unsafe {
328
- enc. inner. intrinsics. deref( ) . into( )
329
- } ) ,
296
+ arch: ManuallyDrop :: new( unsafe { enc. inner. arch. deref( ) . into( ) } ) ,
330
297
}
331
298
} else {
332
299
$module:: InnerDec {
@@ -344,11 +311,11 @@ macro_rules! define_aes_impl {
344
311
impl KeyInit for $name_dec {
345
312
#[ inline]
346
313
fn new( key: & Key <Self >) -> Self {
347
- let ( token, aesni_present ) = aes_intrinsics :: init_get( ) ;
314
+ let ( token, aes_features ) = arch :: features :: aes :: init_get( ) ;
348
315
349
- let inner = if aesni_present {
316
+ let inner = if aes_features {
350
317
$module:: InnerDec {
351
- intrinsics : ManuallyDrop :: new( intrinsics :: $name_dec:: new( key) ) ,
318
+ arch : ManuallyDrop :: new( arch :: $name_dec:: new( key) ) ,
352
319
}
353
320
} else {
354
321
$module:: InnerDec {
@@ -369,7 +336,7 @@ macro_rules! define_aes_impl {
369
336
fn clone( & self ) -> Self {
370
337
let inner = if self . token. get( ) {
371
338
$module:: InnerDec {
372
- intrinsics : unsafe { self . inner. intrinsics . clone( ) } ,
339
+ arch : unsafe { self . inner. arch . clone( ) } ,
373
340
}
374
341
} else {
375
342
$module:: InnerDec {
@@ -390,19 +357,10 @@ macro_rules! define_aes_impl {
390
357
391
358
impl BlockCipherDecrypt for $name_dec {
392
359
fn decrypt_with_backend( & self , f: impl BlockCipherDecClosure <BlockSize = U16 >) {
393
- unsafe {
394
- if self . token. get( ) {
395
- #[ target_feature( enable = "aes" ) ]
396
- unsafe fn inner(
397
- state: & intrinsics:: $name_dec,
398
- f: impl BlockCipherDecClosure <BlockSize = U16 >,
399
- ) {
400
- f. call( state. get_dec_backend( ) ) ;
401
- }
402
- inner( & self . inner. intrinsics, f) ;
403
- } else {
404
- f. call( & self . inner. soft. get_dec_backend( ) ) ;
405
- }
360
+ if self . token. get( ) {
361
+ unsafe { & self . inner. arch } . decrypt_with_backend( f)
362
+ } else {
363
+ unsafe { & self . inner. soft } . decrypt_with_backend( f)
406
364
}
407
365
}
408
366
}
@@ -423,7 +381,7 @@ macro_rules! define_aes_impl {
423
381
#[ inline]
424
382
fn drop( & mut self ) {
425
383
if self . token. get( ) {
426
- unsafe { ManuallyDrop :: drop( & mut self . inner. intrinsics ) } ;
384
+ unsafe { ManuallyDrop :: drop( & mut self . inner. arch ) } ;
427
385
} else {
428
386
unsafe { ManuallyDrop :: drop( & mut self . inner. soft) } ;
429
387
} ;
0 commit comments