Skip to content

Commit 02ca0bd

Browse files
committed
Major changes
Changed the feature detection and impl selection to be build-time instead of compile-time. Exposed `pre_enc` and its friends Fixed some typos in doc
1 parent 6f92046 commit 02ca0bd

12 files changed

+239
-184
lines changed

build.rs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#![cfg_attr(
2+
all(feature = "nightly", target_arch = "arm", target_feature = "v8"),
3+
feature(stdarch_arm_feature_detection)
4+
)]
5+
#![cfg_attr(
6+
all(
7+
feature = "nightly",
8+
any(target_arch = "riscv64", target_arch = "riscv32")
9+
),
10+
feature(stdarch_riscv_feature_detection)
11+
)]
12+
use std::arch::*;
13+
14+
fn select_impl() -> &'static str {
15+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
16+
if is_x86_feature_detected!("aes") && is_x86_feature_detected!("sse4.1") {
17+
return "x86";
18+
}
19+
#[cfg(any(target_arch = "aarch64", target_arch = "arm64ec"))]
20+
if is_aarch64_feature_detected!("aes") {
21+
return "neon";
22+
}
23+
#[cfg(all(feature = "nightly", target_arch = "arm", target_feature = "v8"))]
24+
if is_arm_feature_detected!("aes") {
25+
return "arm-neon";
26+
}
27+
#[cfg(all(
28+
feature = "nightly",
29+
any(target_arch = "riscv64", target_arch = "riscv32")
30+
))]
31+
if is_riscv_feature_detected!("zkne") && is_riscv_feature_detected!("zknd") {
32+
return "risc-v";
33+
}
34+
"software"
35+
}
36+
37+
fn select_x2_impl() -> &'static str {
38+
#[cfg(all(feature = "nightly", any(target_arch = "x86", target_arch = "x86_64")))]
39+
if is_x86_feature_detected!("vaes") {
40+
return "vaes";
41+
}
42+
"tuple"
43+
}
44+
45+
fn select_x4_impl() -> &'static str {
46+
#[cfg(all(feature = "nightly", any(target_arch = "x86", target_arch = "x86_64")))]
47+
if is_x86_feature_detected!("avx512f") {
48+
return "avx512f";
49+
}
50+
"tuple"
51+
}
52+
53+
fn main() {
54+
println!("cargo:rerun-if-changed=build.rs");
55+
56+
println!(
57+
"cargo:rustc-check-cfg=cfg(aes_impl, values(\"x86\", \"neon\", \"arm-neon\", \"risc-v\", \"software\"))"
58+
);
59+
println!("cargo:rustc-check-cfg=cfg(aes_x2_impl, values(\"vaes\", \"tuple\"))");
60+
println!("cargo:rustc-check-cfg=cfg(aes_x4_impl, values(\"avx512f\", \"tuple\"))");
61+
62+
println!("cargo:rustc-cfg=aes_impl=\"{}\"", select_impl());
63+
println!("cargo:rustc-cfg=aes_x2_impl=\"{}\"", select_x2_impl());
64+
println!("cargo:rustc-cfg=aes_x4_impl=\"{}\"", select_x4_impl());
65+
}

src/aes_arm.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,15 @@ impl AesBlock {
112112
}
113113
}
114114

115+
/// /// Performs the operation `AddRoundKey` -> `ShiftRows` -> `SubBytes`
115116
#[inline(always)]
116-
pub(crate) fn pre_enc_last(self, round_key: Self) -> Self {
117+
pub fn pre_enc_last(self, round_key: Self) -> Self {
117118
Self(unsafe { vaeseq_u8(self.0, round_key.0) })
118119
}
119120

121+
/// Performs the operation `AddRoundKey` -> `ShiftRows` -> `SubBytes` -> `MixColumns`
120122
#[inline(always)]
121-
pub(crate) fn pre_enc(self, round_key: Self) -> Self {
123+
pub fn pre_enc(self, round_key: Self) -> Self {
122124
self.pre_enc_last(round_key).mc()
123125
}
124126

@@ -128,17 +130,19 @@ impl AesBlock {
128130
self.pre_enc(Self::zero()) ^ round_key
129131
}
130132

133+
/// Performs the operation `AddRoundKey` -> `InvShiftRows` -> `InvSubBytes`
131134
#[inline(always)]
132-
pub(crate) fn pre_dec_last(self, round_key: Self) -> Self {
135+
pub fn pre_dec_last(self, round_key: Self) -> Self {
133136
Self(unsafe { vaesdq_u8(self.0, round_key.0) })
134137
}
135138

139+
/// Performs the operation `AddRoundKey` -> `InvShiftRows` -> `InvSubBytes` -> `InvMixColumns`
136140
#[inline(always)]
137-
pub(crate) fn pre_dec(self, round_key: Self) -> Self {
141+
pub fn pre_dec(self, round_key: Self) -> Self {
138142
self.pre_dec_last(round_key).imc()
139143
}
140144

141-
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumn`s->`AddRoundKey`)
145+
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumns`->`AddRoundKey`)
142146
#[inline]
143147
pub fn dec(self, round_key: Self) -> Self {
144148
self.pre_dec(Self::zero()) ^ round_key
@@ -150,7 +154,7 @@ impl AesBlock {
150154
self.pre_enc_last(Self::zero()) ^ round_key
151155
}
152156

153-
/// Performs one round of AES decryption function without `InvMixColumn`s (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
157+
/// Performs one round of AES decryption function without `InvMixColumns` (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
154158
#[inline]
155159
pub fn dec_last(self, round_key: Self) -> Self {
156160
self.pre_dec_last(Self::zero()) ^ round_key
@@ -162,7 +166,7 @@ impl AesBlock {
162166
Self(unsafe { vaesmcq_u8(self.0) })
163167
}
164168

165-
/// Performs the `InvMixColumn`s operation
169+
/// Performs the `InvMixColumns` operation
166170
#[inline]
167171
pub fn imc(self) -> Self {
168172
Self(unsafe { vaesimcq_u8(self.0) })

src/aes_default.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl AesBlock {
149149
)
150150
}
151151

152-
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumn`s->`AddRoundKey`)
152+
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumns`->`AddRoundKey`)
153153
#[inline]
154154
pub fn dec(self, round_key: Self) -> Self {
155155
Self(
@@ -160,7 +160,7 @@ impl AesBlock {
160160
)
161161
}
162162

163-
/// Performs one round of AES decryption function without `InvMixColumn`s (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
163+
/// Performs one round of AES decryption function without `InvMixColumns` (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
164164
#[inline]
165165
pub fn dec_last(self, round_key: Self) -> Self {
166166
Self(
@@ -210,7 +210,7 @@ impl AesBlock {
210210
)
211211
}
212212

213-
/// Performs the `InvMixColumn`s operation
213+
/// Performs the `InvMixColumns` operation
214214
#[inline]
215215
pub fn imc(self) -> Self {
216216
Self(

src/aes_riscv32.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ impl AesBlock {
122122
(self.0 | self.1 | self.2 | self.3) == 0
123123
}
124124

125+
/// Performs the operation `AddRoundKey` -> `ShiftRows` -> `SubBytes` -> `MixColumns`
125126
#[inline(always)]
126-
pub(crate) fn pre_enc(self, round_key: Self) -> Self {
127+
pub fn pre_enc(self, round_key: Self) -> Self {
127128
outer!(aes32esmi, self, round_key)
128129
}
129130

@@ -133,8 +134,9 @@ impl AesBlock {
133134
self.pre_enc(Self::zero()) ^ round_key
134135
}
135136

137+
/// Performs the operation `AddRoundKey` -> `ShiftRows` -> `SubBytes`
136138
#[inline(always)]
137-
pub(crate) fn pre_enc_last(self, round_key: Self) -> Self {
139+
pub fn pre_enc_last(self, round_key: Self) -> Self {
138140
outer!(aes32esi, self, round_key)
139141
}
140142

@@ -144,23 +146,25 @@ impl AesBlock {
144146
self.pre_enc_last(Self::zero()) ^ round_key
145147
}
146148

149+
/// Performs the operation `AddRoundKey` -> `InvShiftRows` -> `InvSubBytes` -> `InvMixColumns`
147150
#[inline(always)]
148-
pub(crate) fn pre_dec(self, round_key: Self) -> Self {
151+
pub fn pre_dec(self, round_key: Self) -> Self {
149152
outer!(aes32dsmi, self, round_key)
150153
}
151154

152-
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumn`s->`AddRoundKey`)
155+
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumns`->`AddRoundKey`)
153156
#[inline]
154157
pub fn dec(self, round_key: Self) -> Self {
155158
self.pre_dec(Self::zero()) ^ round_key
156159
}
157160

161+
/// Performs the operation `AddRoundKey` -> `InvShiftRows` -> `InvSubBytes`
158162
#[inline(always)]
159-
pub(crate) fn pre_dec_last(self, round_key: Self) -> Self {
163+
pub fn pre_dec_last(self, round_key: Self) -> Self {
160164
outer!(aes32dsi, self, round_key)
161165
}
162166

163-
/// Performs one round of AES decryption function without `InvMixColumn`s (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
167+
/// Performs one round of AES decryption function without `InvMixColumns` (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
164168
#[inline]
165169
pub fn dec_last(self, round_key: Self) -> Self {
166170
self.pre_dec_last(Self::zero()) ^ round_key
@@ -172,7 +176,7 @@ impl AesBlock {
172176
self.pre_dec_last(Self::zero()).enc(Self::zero())
173177
}
174178

175-
/// Performs the `InvMixColumn`s operation
179+
/// Performs the `InvMixColumns` operation
176180
#[inline]
177181
pub fn imc(self) -> Self {
178182
self.pre_enc_last(Self::zero()).dec(Self::zero())

src/aes_riscv64.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl AesBlock {
102102
}
103103
}
104104

105-
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumn`s->`AddRoundKey`)
105+
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumns`->`AddRoundKey`)
106106
#[inline]
107107
pub fn dec(self, round_key: Self) -> Self {
108108
unsafe {
@@ -124,7 +124,7 @@ impl AesBlock {
124124
}
125125
}
126126

127-
/// Performs one round of AES decryption function without `InvMixColumn`s (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
127+
/// Performs one round of AES decryption function without `InvMixColumns` (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
128128
#[inline]
129129
pub fn dec_last(self, round_key: Self) -> Self {
130130
unsafe {
@@ -144,7 +144,7 @@ impl AesBlock {
144144
}
145145
}
146146

147-
/// Performs the `InvMixColumn`s operation
147+
/// Performs the `InvMixColumns` operation
148148
#[inline]
149149
pub fn imc(self) -> Self {
150150
unsafe { Self(aes64im(self.0), aes64im(self.1)) }

src/aes_x86.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl AesBlock {
9191
Self(unsafe { _mm_aesenc_si128(self.0, round_key.0) })
9292
}
9393

94-
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumn`s->`AddRoundKey`)
94+
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumns`->`AddRoundKey`)
9595
#[inline]
9696
pub fn dec(self, round_key: Self) -> Self {
9797
Self(unsafe { _mm_aesdec_si128(self.0, round_key.0) })
@@ -103,7 +103,7 @@ impl AesBlock {
103103
Self(unsafe { _mm_aesenclast_si128(self.0, round_key.0) })
104104
}
105105

106-
/// Performs one round of AES decryption function without `InvMixColumn`s (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
106+
/// Performs one round of AES decryption function without `InvMixColumns` (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
107107
#[inline]
108108
pub fn dec_last(self, round_key: Self) -> Self {
109109
Self(unsafe { _mm_aesdeclast_si128(self.0, round_key.0) })
@@ -120,7 +120,7 @@ impl AesBlock {
120120
})
121121
}
122122

123-
/// Performs the `InvMixColumn`s operation
123+
/// Performs the `InvMixColumns` operation
124124
#[inline]
125125
pub fn imc(self) -> Self {
126126
Self(unsafe { _mm_aesimc_si128(self.0) })

src/aesdefault_x2.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,55 @@ impl AesBlockX2 {
9898
self.0.is_zero() & self.1.is_zero()
9999
}
100100

101+
/// Performs the operation `AddRoundKey` -> `SubBytes` -> `ShiftRows` -> `MixColumns`
102+
#[inline]
103+
pub fn pre_enc(self, round_key: Self) -> Self {
104+
Self(self.0.pre_enc(round_key.0), self.1.pre_enc(round_key.1))
105+
}
106+
101107
/// Performs one round of AES encryption function (`ShiftRows`->`SubBytes`->`MixColumns`->`AddRoundKey`)
102108
#[inline]
103109
pub fn enc(self, round_key: Self) -> Self {
104110
Self(self.0.enc(round_key.0), self.1.enc(round_key.1))
105111
}
106112

107-
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumn`s->`AddRoundKey`)
113+
/// Performs the operation `AddRoundKey` -> `InvShiftRows` -> `InvSubBytes` -> `InvMixColumns`
114+
#[inline]
115+
pub fn pre_dec(self, round_key: Self) -> Self {
116+
Self(self.0.pre_dec(round_key.0), self.1.pre_dec(round_key.1))
117+
}
118+
119+
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumns`->`AddRoundKey`)
108120
#[inline]
109121
pub fn dec(self, round_key: Self) -> Self {
110122
Self(self.0.dec(round_key.0), self.1.dec(round_key.1))
111123
}
112124

125+
/// Performs the operation `AddRoundKey` -> `ShiftRows` -> `SubBytes`
126+
#[inline]
127+
pub fn pre_enc_last(self, round_key: Self) -> Self {
128+
Self(
129+
self.0.pre_enc_last(round_key.0),
130+
self.1.pre_enc_last(round_key.1),
131+
)
132+
}
133+
113134
/// Performs one round of AES encryption function without `MixColumns` (`ShiftRows`->`SubBytes`->`AddRoundKey`)
114135
#[inline]
115136
pub fn enc_last(self, round_key: Self) -> Self {
116137
Self(self.0.enc_last(round_key.0), self.1.enc_last(round_key.1))
117138
}
118139

119-
/// Performs one round of AES decryption function without `InvMixColumn`s (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
140+
/// Performs the operation `AddRoundKey` -> `InvShiftRows` -> `InvSubBytes`
141+
#[inline]
142+
pub fn pre_dec_last(self, round_key: Self) -> Self {
143+
Self(
144+
self.0.pre_dec_last(round_key.0),
145+
self.1.pre_dec_last(round_key.1),
146+
)
147+
}
148+
149+
/// Performs one round of AES decryption function without `InvMixColumns` (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
120150
#[inline]
121151
pub fn dec_last(self, round_key: Self) -> Self {
122152
Self(self.0.dec_last(round_key.0), self.1.dec_last(round_key.1))

src/aesdefault_x4.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,55 @@ impl AesBlockX4 {
121121
self.0.is_zero() & self.1.is_zero()
122122
}
123123

124+
/// Performs the operation `AddRoundKey` -> `ShiftRows` -> `SubBytes` -> `MixColumns`
125+
#[inline]
126+
pub fn pre_enc(self, round_key: Self) -> Self {
127+
Self(self.0.pre_enc(round_key.0), self.1.pre_enc(round_key.1))
128+
}
129+
124130
/// Performs one round of AES encryption function (`ShiftRows`->`SubBytes`->`MixColumns`->`AddRoundKey`)
125131
#[inline]
126132
pub fn enc(self, round_key: Self) -> Self {
127133
Self(self.0.enc(round_key.0), self.1.enc(round_key.1))
128134
}
129135

130-
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumn`s->`AddRoundKey`)
136+
/// Performs the operation `AddRoundKey` -> `InvShiftRows` -> `InvSubBytes` -> `InvMixColumns`
137+
#[inline]
138+
pub fn pre_dec(self, round_key: Self) -> Self {
139+
Self(self.0.pre_dec(round_key.0), self.1.pre_dec(round_key.1))
140+
}
141+
142+
/// Performs one round of AES decryption function (`InvShiftRows`->`InvSubBytes`->`InvMixColumns`->`AddRoundKey`)
131143
#[inline]
132144
pub fn dec(self, round_key: Self) -> Self {
133145
Self(self.0.dec(round_key.0), self.1.dec(round_key.1))
134146
}
135147

148+
/// Performs the operation `AddRoundKey` -> `ShiftRows` -> `SubBytes`
149+
#[inline]
150+
pub fn pre_enc_last(self, round_key: Self) -> Self {
151+
Self(
152+
self.0.pre_enc_last(round_key.0),
153+
self.1.pre_enc_last(round_key.1),
154+
)
155+
}
156+
136157
/// Performs one round of AES encryption function without `MixColumns` (`ShiftRows`->`SubBytes`->`AddRoundKey`)
137158
#[inline]
138159
pub fn enc_last(self, round_key: Self) -> Self {
139160
Self(self.0.enc_last(round_key.0), self.1.enc_last(round_key.1))
140161
}
141162

142-
/// Performs one round of AES decryption function without `InvMixColumn`s (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
163+
/// Performs the operation `AddRoundKey` -> `InvShiftRows` -> `InvSubBytes`
164+
#[inline]
165+
pub fn pre_dec_last(self, round_key: Self) -> Self {
166+
Self(
167+
self.0.pre_dec_last(round_key.0),
168+
self.1.pre_dec_last(round_key.1),
169+
)
170+
}
171+
172+
/// Performs one round of AES decryption function without `InvMixColumns` (`InvShiftRows`->`InvSubBytes`->`AddRoundKey`)
143173
#[inline]
144174
pub fn dec_last(self, round_key: Self) -> Self {
145175
Self(self.0.dec_last(round_key.0), self.1.dec_last(round_key.1))

0 commit comments

Comments
 (0)