1use std::sync::atomic::{Ordering, compiler_fence};
9
10use super::{Error, Result};
11
12#[cfg(all(target_arch = "x86_64", not(miri)))]
13#[path = "memory/x86.rs"]
14mod x86;
15
16#[cfg(all(target_arch = "x86_64", not(miri)))]
17const STREAMING_MIN_BYTES: usize = 1024;
18#[cfg(all(target_arch = "x86_64", not(miri)))]
19const STREAMING_MAX_BYTES: usize = 4 * 1024 * 1024;
20
21#[inline(always)]
30pub fn copy(destination: &mut [u8], source: &[u8]) -> Result<()> {
31 if destination.len() != source.len() {
32 return Err(length_mismatch(
33 destination.len(),
34 source.len(),
35 "memory::copy",
36 ));
37 }
38 unsafe {
41 copy_to_ptr(destination.as_mut_ptr(), source.as_ptr(), source.len());
42 }
43 Ok(())
44}
45
46#[inline(always)]
56pub fn copy_streaming(destination: &mut [u8], source: &[u8]) -> Result<()> {
57 if destination.len() != source.len() {
58 return Err(length_mismatch(
59 destination.len(),
60 source.len(),
61 "memory::copy_streaming",
62 ));
63 }
64 unsafe {
67 copy_streaming_to_ptr(destination.as_mut_ptr(), source.as_ptr(), source.len());
68 }
69 Ok(())
70}
71
72#[must_use]
74pub fn equal(left: &[u8], right: &[u8]) -> bool {
75 if left.len() != right.len() {
76 return false;
77 }
78 if left.as_ptr() == right.as_ptr() || left.is_empty() {
79 return true;
80 }
81
82 #[cfg(all(target_arch = "x86_64", not(miri)))]
83 if std::arch::is_x86_feature_detected!("avx512f")
84 && std::arch::is_x86_feature_detected!("avx512bw")
85 {
86 return unsafe { x86::equal_avx512(left.as_ptr(), right.as_ptr(), left.len()) };
89 }
90
91 #[cfg(all(target_arch = "x86_64", not(miri)))]
92 if std::arch::is_x86_feature_detected!("avx2") {
93 return unsafe { x86::equal_avx2(left.as_ptr(), right.as_ptr(), left.len()) };
96 }
97
98 left == right
99}
100
101#[must_use]
107pub fn equal_constant_time(left: &[u8], right: &[u8]) -> bool {
108 if left.len() != right.len() {
109 return false;
110 }
111
112 #[cfg(all(target_arch = "x86_64", not(miri)))]
113 if std::arch::is_x86_feature_detected!("avx512f")
114 && std::arch::is_x86_feature_detected!("avx512bw")
115 {
116 return unsafe { x86::equal_constant_time_avx512(left.as_ptr(), right.as_ptr(), left.len()) };
119 }
120
121 #[cfg(all(target_arch = "x86_64", not(miri)))]
122 if std::arch::is_x86_feature_detected!("avx2") {
123 return unsafe { x86::equal_constant_time_avx2(left.as_ptr(), right.as_ptr(), left.len()) };
126 }
127
128 let mut difference = 0_u8;
129 for (&left_byte, &right_byte) in left.iter().zip(right) {
130 difference |= left_byte ^ right_byte;
131 }
132 compiler_fence(Ordering::SeqCst);
133 difference == 0
134}
135
136pub fn zero_secure(bytes: &mut [u8]) {
142 for byte in bytes {
143 unsafe {
146 std::ptr::write_volatile(byte, 0);
147 }
148 }
149 compiler_fence(Ordering::SeqCst);
150}
151
152#[cold]
153#[inline(never)]
154fn length_mismatch(destination: usize, source: usize, operation: &'static str) -> Error {
155 Error::invalid_argument(format!(
156 "{operation} requires equal lengths; destination is {destination} bytes, source is {source} bytes"
157 ))
158}
159
160#[inline(always)]
168pub(crate) unsafe fn copy_to_ptr(destination: *mut u8, source: *const u8, length: usize) {
169 #[cfg(all(target_arch = "x86_64", not(target_feature = "avx2")))]
170 {
171 unsafe { std::ptr::copy_nonoverlapping(source, destination, length) };
173 }
174 #[cfg(not(all(target_arch = "x86_64", not(target_feature = "avx2"))))]
175 unsafe {
177 copy_native_to_ptr(destination, source, length)
178 };
179}
180
181#[inline(always)]
182#[cfg(not(all(target_arch = "x86_64", not(target_feature = "avx2"))))]
183unsafe fn copy_native_to_ptr(destination: *mut u8, source: *const u8, length: usize) {
184 if length > 256 {
185 #[cfg(all(target_arch = "x86_64", not(miri)))]
186 if length <= 4096
187 && (cfg!(target_feature = "avx512f") || length >= 2048)
188 && std::arch::is_x86_feature_detected!("avx512f")
189 {
190 unsafe { x86::copy_cached_avx512(destination, source, length) };
193 return;
194 }
195 unsafe {
198 std::ptr::copy_nonoverlapping(source, destination, length);
199 }
200 return;
201 }
202 if length == 0 {
203 return;
204 }
205
206 unsafe {
210 if length <= 16 {
211 if length >= 8 {
212 copy_block::<8>(destination, source);
213 copy_block::<8>(destination.add(length - 8), source.add(length - 8));
214 } else if length >= 4 {
215 copy_block::<4>(destination, source);
216 copy_block::<4>(destination.add(length - 4), source.add(length - 4));
217 } else if length >= 2 {
218 copy_block::<2>(destination, source);
219 copy_block::<2>(destination.add(length - 2), source.add(length - 2));
220 } else {
221 copy_block::<1>(destination, source);
222 }
223 return;
224 }
225
226 if length <= 32 {
227 copy_block::<16>(destination, source);
228 copy_block::<16>(destination.add(length - 16), source.add(length - 16));
229 return;
230 }
231
232 if length <= 64 {
233 copy_block::<32>(destination, source);
234 copy_block::<32>(destination.add(length - 32), source.add(length - 32));
235 return;
236 }
237
238 if length <= 128 {
239 copy_block::<32>(destination, source);
240 copy_block::<32>(destination.add(32), source.add(32));
241 copy_block::<32>(destination.add(length - 64), source.add(length - 64));
242 copy_block::<32>(destination.add(length - 32), source.add(length - 32));
243 return;
244 }
245
246 if length <= 256 {
247 copy_block::<32>(destination, source);
248 copy_block::<32>(destination.add(32), source.add(32));
249 copy_block::<32>(destination.add(64), source.add(64));
250 copy_block::<32>(destination.add(96), source.add(96));
251 copy_block::<32>(destination.add(length - 128), source.add(length - 128));
252 copy_block::<32>(destination.add(length - 96), source.add(length - 96));
253 copy_block::<32>(destination.add(length - 64), source.add(length - 64));
254 copy_block::<32>(destination.add(length - 32), source.add(length - 32));
255 }
256 }
257}
258
259#[inline(always)]
268pub(crate) unsafe fn copy_streaming_to_ptr(destination: *mut u8, source: *const u8, length: usize) {
269 #[cfg(all(target_arch = "x86_64", not(miri)))]
270 if (STREAMING_MIN_BYTES..=STREAMING_MAX_BYTES).contains(&length)
271 && std::arch::is_x86_feature_detected!("avx512f")
272 && std::arch::is_x86_feature_detected!("avx512bw")
273 {
274 unsafe {
277 x86::copy_streaming_avx512(destination, source, length);
278 }
279 return;
280 }
281
282 #[cfg(all(target_arch = "x86_64", not(miri)))]
283 if (STREAMING_MIN_BYTES..=STREAMING_MAX_BYTES).contains(&length)
284 && std::arch::is_x86_feature_detected!("avx2")
285 {
286 unsafe {
289 x86::copy_streaming_avx2(destination, source, length);
290 }
291 return;
292 }
293
294 unsafe {
298 if length > 256 {
299 std::ptr::copy_nonoverlapping(source, destination, length);
300 } else {
301 copy_to_ptr(destination, source, length);
302 }
303 }
304}
305
306#[inline(always)]
307#[cfg(not(all(target_arch = "x86_64", not(target_feature = "avx2"))))]
308unsafe fn copy_block<const LENGTH: usize>(destination: *mut u8, source: *const u8) {
309 unsafe {
312 std::ptr::copy_nonoverlapping(source, destination, LENGTH);
313 }
314}