2020
2121use super :: { Block , Counter , EncryptBlock , EncryptCtr32 , Iv , KeyBytes , Overlapping , AES_KEY } ;
2222use crate :: { cpu, error} ;
23- use cfg_if:: cfg_if;
24-
25- cfg_if ! {
26- if #[ cfg( all( target_arch = "aarch64" , target_endian = "little" ) ) ] {
27- pub ( in super :: super ) type RequiredCpuFeatures = cpu:: aarch64:: Aes ;
28- pub ( in super :: super ) type OptionalCpuFeatures = ( ) ;
29- } else if #[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ] {
30- use cpu:: intel:: { Aes , Avx , Ssse3 } ;
31- // Some functions seem to have been written to require only SSE/SSE2
32- // but there seem to be no SSSE3-less CPUs with AES-NI, and we don't
33- // have feature detection for SSE2.
34- pub ( in super :: super ) type RequiredCpuFeatures = ( Aes , Ssse3 ) ;
35- pub ( in super :: super ) type OptionalCpuFeatures = Avx ;
36- }
37- }
3823
3924#[ derive( Clone ) ]
4025pub struct Key {
@@ -45,22 +30,38 @@ impl Key {
4530 #[ cfg( all( target_arch = "aarch64" , target_endian = "little" ) ) ]
4631 pub ( in super :: super ) fn new (
4732 bytes : KeyBytes < ' _ > ,
48- _required_cpu_features : RequiredCpuFeatures ,
49- _optional_cpu_features : Option < OptionalCpuFeatures > ,
33+ _required_cpu_features : cpu :: aarch64 :: Aes ,
34+ _optional_cpu_features : Option < ( ) > ,
5035 ) -> Result < Self , error:: Unspecified > {
5136 let inner = unsafe { set_encrypt_key ! ( aes_hw_set_encrypt_key, bytes) } ?;
5237 Ok ( Self { inner } )
5338 }
5439
55- #[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
40+ #[ cfg( target_arch = "x86" ) ]
41+ pub ( in super :: super ) fn new (
42+ bytes : KeyBytes < ' _ > ,
43+ _required_cpu_features : ( cpu:: intel:: Aes , cpu:: intel:: Ssse3 ) ,
44+ optional_cpu_features : Option < cpu:: intel:: Avx > ,
45+ ) -> Result < Self , error:: Unspecified > {
46+ // Ssse3 is required, but upstream only uses this if there is also Avx;
47+ // presumably the base version is faster on pre-AVX CPUs.
48+ let inner = if let Some ( cpu:: intel:: Avx { .. } ) = optional_cpu_features {
49+ unsafe { set_encrypt_key ! ( aes_hw_set_encrypt_key_alt, bytes) } ?
50+ } else {
51+ unsafe { set_encrypt_key ! ( aes_hw_set_encrypt_key_base, bytes) } ?
52+ } ;
53+ Ok ( Self { inner } )
54+ }
55+
56+ #[ cfg( target_arch = "x86_64" ) ]
5657 pub ( in super :: super ) fn new (
5758 bytes : KeyBytes < ' _ > ,
58- ( Aes { .. } , Ssse3 { .. } ) : RequiredCpuFeatures ,
59- optional_cpu_features : Option < OptionalCpuFeatures > ,
59+ _required_cpu_features : ( cpu :: intel :: Aes , cpu :: intel :: Ssse3 ) ,
60+ optional_cpu_features : Option < cpu :: intel :: Avx > ,
6061 ) -> Result < Self , error:: Unspecified > {
6162 // Ssse3 is required, but upstream only uses this if there is also Avx;
6263 // presumably the base version is faster on pre-AVX CPUs.
63- let inner = if let Some ( Avx { .. } ) = optional_cpu_features {
64+ let inner = if let Some ( cpu :: intel :: Avx { .. } ) = optional_cpu_features {
6465 unsafe { set_encrypt_key ! ( aes_hw_set_encrypt_key_alt, bytes) } ?
6566 } else {
6667 unsafe { set_encrypt_key ! ( aes_hw_set_encrypt_key_base, bytes) } ?
0 commit comments