Skip to content

Commit 8b90988

Browse files
committed
Reorder operands to copy_memory and copy_nonoverlapping_memory
1 parent a0cef12 commit 8b90988

File tree

13 files changed

+54
-54
lines changed

13 files changed

+54
-54
lines changed

src/aes_gcm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<'a> AesGcm<'a> {
3333
// earlier preventing the use of generic CTR mode.
3434

3535
let mut iv = [0u8; 16];
36-
copy_memory(&mut iv, nonce);
36+
copy_memory(nonce, &mut iv);
3737
iv[15] = 1u8;
3838
let mut cipher = ctr(key_size,key,&iv);
3939
let temp_block = [0u8; 16];

src/blake2b.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl Blake2b {
122122
key: [0; BLAKE2B_KEYBYTES],
123123
key_length: key.len() as u8
124124
};
125-
copy_memory(&mut b.key, key);
125+
copy_memory(key, &mut b.key);
126126
b
127127
}
128128

@@ -184,7 +184,7 @@ impl Blake2b {
184184

185185
fn apply_key(&mut self) {
186186
let mut block : [u8; BLAKE2B_BLOCKBYTES] = [0; BLAKE2B_BLOCKBYTES];
187-
copy_memory(&mut block, &self.key[..self.key_length as usize]);
187+
copy_memory(&self.key[..self.key_length as usize], &mut block);
188188
self.update(&block);
189189
unsafe {
190190
volatile_set_memory(block.as_mut_ptr(), 0, block.len());
@@ -256,20 +256,20 @@ impl Blake2b {
256256
let fill = 2 * BLAKE2B_BLOCKBYTES - left;
257257

258258
if input.len() > fill {
259-
copy_memory( &mut self.buf[left..], &input[0..fill] ); // Fill buffer
259+
copy_memory(&input[0..fill], &mut self.buf[left..]); // Fill buffer
260260
self.buflen += fill;
261261
self.increment_counter( BLAKE2B_BLOCKBYTES as u64);
262262
self.compress();
263263

264264
let mut halves = self.buf.chunks_mut(BLAKE2B_BLOCKBYTES);
265265
let first_half = halves.next().unwrap();
266266
let second_half = halves.next().unwrap();
267-
copy_memory(first_half, second_half);
267+
copy_memory(second_half, first_half);
268268

269269
self.buflen -= BLAKE2B_BLOCKBYTES;
270270
input = &input[fill..input.len()];
271271
} else { // inlen <= fill
272-
copy_memory(&mut self.buf[left..], input);
272+
copy_memory(input, &mut self.buf[left..]);
273273
self.buflen += input.len();
274274
break;
275275
}
@@ -287,7 +287,7 @@ impl Blake2b {
287287
let mut halves = self.buf.chunks_mut(BLAKE2B_BLOCKBYTES);
288288
let first_half = halves.next().unwrap();
289289
let second_half = halves.next().unwrap();
290-
copy_memory(first_half, second_half);
290+
copy_memory(second_half, first_half);
291291
}
292292

293293
let incby = self.buflen as u64;
@@ -304,7 +304,7 @@ impl Blake2b {
304304
self.computed = true;
305305
}
306306
let outlen = out.len();
307-
copy_memory(out, &self.buf[0..outlen]);
307+
copy_memory(&self.buf[0..outlen], out);
308308
}
309309

310310
pub fn blake2b(out: &mut[u8], input: &[u8], key: &[u8]) {

src/blockmodes.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ fn update_history(in_hist: &mut [u8], out_hist: &mut [u8], last_in: &[u8], last_
9494
let in_hist_len = in_hist.len();
9595
if in_hist_len > 0 {
9696
slice::bytes::copy_memory(
97-
in_hist,
98-
&last_in[last_in.len() - in_hist_len..]);
97+
&last_in[last_in.len() - in_hist_len..],
98+
in_hist);
9999
}
100100
let out_hist_len = out_hist.len();
101101
if out_hist_len > 0 {
102102
slice::bytes::copy_memory(
103-
out_hist,
104-
&last_out[last_out.len() - out_hist_len..]);
103+
&last_out[last_out.len() - out_hist_len..],
104+
out_hist);
105105
}
106106
}
107107

@@ -412,8 +412,8 @@ impl <P: BlockProcessor, X: PaddingProcessor> BlockEngine<P, X> {
412412
}
413413
fn reset_with_history(&mut self, in_hist: &[u8], out_hist: &[u8]) {
414414
self.reset();
415-
slice::bytes::copy_memory(&mut self.in_hist, in_hist);
416-
slice::bytes::copy_memory(&mut self.out_hist, out_hist);
415+
slice::bytes::copy_memory(in_hist, &mut self.in_hist);
416+
slice::bytes::copy_memory(out_hist, &mut self.out_hist);
417417
}
418418
}
419419

@@ -690,7 +690,7 @@ impl <A: BlockEncryptor> CtrMode<A> {
690690
}
691691
}
692692
pub fn reset(&mut self, ctr: &[u8]) {
693-
slice::bytes::copy_memory(&mut self.ctr, ctr);
693+
slice::bytes::copy_memory(ctr, &mut self.ctr);
694694
self.bytes.reset();
695695
}
696696
fn process(&mut self, input: &[u8], output: &mut [u8]) {
@@ -744,7 +744,7 @@ pub struct CtrModeX8<A> {
744744

745745
fn construct_ctr_x8(in_ctr: &[u8], out_ctr_x8: &mut [u8]) {
746746
for (i, ctr_i) in out_ctr_x8.chunks_mut(in_ctr.len()).enumerate() {
747-
slice::bytes::copy_memory(ctr_i, in_ctr);
747+
slice::bytes::copy_memory(in_ctr, ctr_i);
748748
add_ctr(ctr_i, i as u8);
749749
}
750750
}

src/buffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub trait ReadBuffer {
3737

3838
fn push_to<W: WriteBuffer>(&mut self, output: &mut W) {
3939
let count = cmp::min(output.remaining(), self.remaining());
40-
slice::bytes::copy_memory(output.take_next(count), self.take_next(count));
40+
slice::bytes::copy_memory(self.take_next(count), output.take_next(count));
4141
}
4242
}
4343

src/cryptoutil.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn write_u64_be(dst: &mut[u8], mut input: u64) {
2525
input = input.to_be();
2626
unsafe {
2727
let tmp = &input as *const _ as *const u8;
28-
ptr::copy_nonoverlapping(dst.get_unchecked_mut(0), tmp, 8);
28+
ptr::copy_nonoverlapping(tmp, dst.get_unchecked_mut(0), 8);
2929
}
3030
}
3131

@@ -36,7 +36,7 @@ pub fn write_u64_le(dst: &mut[u8], mut input: u64) {
3636
input = input.to_le();
3737
unsafe {
3838
let tmp = &input as *const _ as *const u8;
39-
ptr::copy_nonoverlapping(dst.get_unchecked_mut(0), tmp, 8);
39+
ptr::copy_nonoverlapping(tmp, dst.get_unchecked_mut(0), 8);
4040
}
4141
}
4242

@@ -48,7 +48,7 @@ pub fn write_u64v_le(dst: &mut[u8], input: &[u64]) {
4848
let mut y: *const u64 = input.get_unchecked(0);
4949
for _ in (0..input.len()) {
5050
let tmp = (*y).to_le();
51-
ptr::copy_nonoverlapping(x, &tmp as *const _ as *const u8, 8);
51+
ptr::copy_nonoverlapping(&tmp as *const _ as *const u8, x, 8);
5252
x = x.offset(8);
5353
y = y.offset(1);
5454
}
@@ -62,7 +62,7 @@ pub fn write_u32_be(dst: &mut [u8], mut input: u32) {
6262
input = input.to_be();
6363
unsafe {
6464
let tmp = &input as *const _ as *const u8;
65-
ptr::copy_nonoverlapping(dst.get_unchecked_mut(0), tmp, 4);
65+
ptr::copy_nonoverlapping(tmp, dst.get_unchecked_mut(0), 4);
6666
}
6767
}
6868

@@ -73,7 +73,7 @@ pub fn write_u32_le(dst: &mut[u8], mut input: u32) {
7373
input = input.to_le();
7474
unsafe {
7575
let tmp = &input as *const _ as *const u8;
76-
ptr::copy_nonoverlapping(dst.get_unchecked_mut(0), tmp, 4);
76+
ptr::copy_nonoverlapping(tmp, dst.get_unchecked_mut(0), 4);
7777
}
7878
}
7979

@@ -85,7 +85,7 @@ pub fn write_u32v_le (dst: &mut[u8], input: &[u32]) {
8585
let mut y: *const u32 = input.get_unchecked(0);
8686
for _ in 0..input.len() {
8787
let tmp = (*y).to_le();
88-
ptr::copy_nonoverlapping(x, &tmp as *const _ as *const u8, 4);
88+
ptr::copy_nonoverlapping(&tmp as *const _ as *const u8, x, 4);
8989
x = x.offset(4);
9090
y = y.offset(1);
9191
}
@@ -100,7 +100,7 @@ pub fn read_u64v_be(dst: &mut[u64], input: &[u8]) {
100100
let mut y: *const u8 = input.get_unchecked(0);
101101
for _ in (0..dst.len()) {
102102
let mut tmp: u64 = mem::uninitialized();
103-
ptr::copy_nonoverlapping(&mut tmp as *mut _ as *mut u8, y, 8);
103+
ptr::copy_nonoverlapping(y, &mut tmp as *mut _ as *mut u8, 8);
104104
*x = Int::from_be(tmp);
105105
x = x.offset(1);
106106
y = y.offset(8);
@@ -116,7 +116,7 @@ pub fn read_u64v_le(dst: &mut[u64], input: &[u8]) {
116116
let mut y: *const u8 = input.get_unchecked(0);
117117
for _ in (0..dst.len()) {
118118
let mut tmp: u64 = mem::uninitialized();
119-
ptr::copy_nonoverlapping(&mut tmp as *mut _ as *mut u8, y, 8);
119+
ptr::copy_nonoverlapping(y, &mut tmp as *mut _ as *mut u8, 8);
120120
*x = Int::from_le(tmp);
121121
x = x.offset(1);
122122
y = y.offset(8);
@@ -132,7 +132,7 @@ pub fn read_u32v_be(dst: &mut[u32], input: &[u8]) {
132132
let mut y: *const u8 = input.get_unchecked(0);
133133
for _ in (0..dst.len()) {
134134
let mut tmp: u32 = mem::uninitialized();
135-
ptr::copy_nonoverlapping(&mut tmp as *mut _ as *mut u8, y, 4);
135+
ptr::copy_nonoverlapping(y, &mut tmp as *mut _ as *mut u8, 4);
136136
*x = Int::from_be(tmp);
137137
x = x.offset(1);
138138
y = y.offset(4);
@@ -148,7 +148,7 @@ pub fn read_u32v_le(dst: &mut[u32], input: &[u8]) {
148148
let mut y: *const u8 = input.get_unchecked(0);
149149
for _ in (0..dst.len()) {
150150
let mut tmp: u32 = mem::uninitialized();
151-
ptr::copy_nonoverlapping(&mut tmp as *mut _ as *mut u8, y, 4);
151+
ptr::copy_nonoverlapping(y, &mut tmp as *mut _ as *mut u8, 4);
152152
*x = Int::from_le(tmp);
153153
x = x.offset(1);
154154
y = y.offset(4);
@@ -161,7 +161,7 @@ pub fn read_u32_le(input: &[u8]) -> u32 {
161161
assert!(input.len() == 4);
162162
unsafe {
163163
let mut tmp: u32 = mem::uninitialized();
164-
ptr::copy_nonoverlapping(&mut tmp as *mut _ as *mut u8, input.get_unchecked(0), 4);
164+
ptr::copy_nonoverlapping(input.get_unchecked(0), &mut tmp as *mut _ as *mut u8, 4);
165165
Int::from_le(tmp)
166166
}
167167
}
@@ -171,7 +171,7 @@ pub fn read_u32_be(input: &[u8]) -> u32 {
171171
assert!(input.len() == 4);
172172
unsafe {
173173
let mut tmp: u32 = mem::uninitialized();
174-
ptr::copy_nonoverlapping(&mut tmp as *mut _ as *mut u8, input.get_unchecked(0), 4);
174+
ptr::copy_nonoverlapping(input.get_unchecked(0), &mut tmp as *mut _ as *mut u8, 4);
175175
Int::from_be(tmp)
176176
}
177177
}
@@ -364,15 +364,15 @@ macro_rules! impl_fixed_buffer( ($name:ident, $size:expr) => (
364364
let buffer_remaining = size - self.buffer_idx;
365365
if input.len() >= buffer_remaining {
366366
copy_memory(
367-
&mut self.buffer[self.buffer_idx..size],
368-
&input[..buffer_remaining]);
367+
&input[..buffer_remaining],
368+
&mut self.buffer[self.buffer_idx..size]);
369369
self.buffer_idx = 0;
370370
func(&self.buffer);
371371
i += buffer_remaining;
372372
} else {
373373
copy_memory(
374-
&mut self.buffer[self.buffer_idx..self.buffer_idx + input.len()],
375-
input);
374+
input,
375+
&mut self.buffer[self.buffer_idx..self.buffer_idx + input.len()]);
376376
self.buffer_idx += input.len();
377377
return;
378378
}
@@ -390,8 +390,8 @@ macro_rules! impl_fixed_buffer( ($name:ident, $size:expr) => (
390390
// be empty.
391391
let input_remaining = input.len() - i;
392392
copy_memory(
393-
&mut self.buffer[0..input_remaining],
394-
&input[i..]);
393+
&input[i..],
394+
&mut self.buffer[0..input_remaining]);
395395
self.buffer_idx += input_remaining;
396396
}
397397

src/fortuna.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl FortunaGenerator {
135135
if rem > 0 {
136136
let mut buf = [0; AES_BLOCK_SIZE];
137137
self.generate_blocks(1, &mut buf);
138-
copy_memory(&mut out[(n * AES_BLOCK_SIZE)..], &buf[..rem]);
138+
copy_memory(&buf[..rem], &mut out[(n * AES_BLOCK_SIZE)..]);
139139
}
140140

141141
// Rekey

src/ghash.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ fn update(state: &mut Gf128, len: &mut usize, data: &[u8], srest: &mut Option<[u
161161
None => data,
162162
Some(mut rest) => {
163163
if 16 - rest_len > data_len {
164-
copy_memory(&mut rest[rest_len..], data);
164+
copy_memory(data, &mut rest[rest_len..]);
165165
*srest = Some(rest);
166166
return;
167167
}
168168

169169
let (fill, data) = data.split_at(16 - rest_len);
170-
copy_memory(&mut rest[rest_len..], fill);
170+
copy_memory(fill, &mut rest[rest_len..]);
171171
state.add_and_mul(Gf128::from_bytes(&rest), hs);
172172
data
173173
}
@@ -182,7 +182,7 @@ fn update(state: &mut Gf128, len: &mut usize, data: &[u8], srest: &mut Option<[u
182182

183183
if rest.len() != 0 {
184184
let mut tmp = [0; 16];
185-
copy_memory(&mut tmp, rest);
185+
copy_memory(rest, &mut tmp);
186186
*srest = Some(tmp);
187187
}
188188
}
@@ -316,7 +316,7 @@ impl Mac for Ghash {
316316
self.finished = true;
317317
}
318318

319-
copy_memory(output, &self.state.to_bytes());
319+
copy_memory(&self.state.to_bytes(), output);
320320
}
321321

322322
fn output_bytes(&self) -> usize { 16 }

src/hc128.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ impl Hc128 {
4141
w[i >> 2] |= (key[i] as u32) << (8 * (i & 0x3));
4242
}
4343
unsafe {
44-
ptr::copy_nonoverlapping(w.as_mut_ptr().offset(4), w.as_ptr(), 4);
44+
ptr::copy_nonoverlapping(w.as_ptr(), w.as_mut_ptr().offset(4), 4);
4545
}
4646

4747
for i in (0..nonce.len() & 16) {
4848
w[(i >> 2) + 8] |= (nonce[i] as u32) << (8 * (i & 0x3));
4949
}
5050
unsafe {
51-
ptr::copy_nonoverlapping(w.as_mut_ptr().offset(12), w.as_ptr().offset(8), 4);
51+
ptr::copy_nonoverlapping(w.as_ptr().offset(8), w.as_mut_ptr().offset(12), 4);
5252
}
5353

5454
for i in 16..1280 {
@@ -57,8 +57,8 @@ impl Hc128 {
5757

5858
// Copy contents of w into p and q
5959
unsafe {
60-
ptr::copy_nonoverlapping(self.p.as_mut_ptr(), w.as_ptr().offset(256), 512);
61-
ptr::copy_nonoverlapping(self.q.as_mut_ptr(), w.as_ptr().offset(768), 512);
60+
ptr::copy_nonoverlapping(w.as_ptr().offset(256), self.p.as_mut_ptr(), 512);
61+
ptr::copy_nonoverlapping(w.as_ptr().offset(768), self.q.as_mut_ptr(), 512);
6262
}
6363

6464
for i in 0..512 {

src/hkdf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn hkdf_expand<D: Digest>(mut digest: D, prk: &[u8], info: &[u8], okm: &mut
6363
mac.raw_result(&mut t);
6464
mac.reset();
6565
let chunk_len = chunk.len();
66-
copy_memory(chunk, &t[..chunk_len]);
66+
copy_memory(&t[..chunk_len], chunk);
6767
}
6868
}
6969

src/hmac.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn expand_key<D: Digest>(digest: &mut D, key: &[u8]) -> Vec<u8> {
3838
let mut expanded_key: Vec<u8> = repeat(0).take(bs).collect();
3939

4040
if key.len() <= bs {
41-
slice::bytes::copy_memory(&mut expanded_key, key);
41+
slice::bytes::copy_memory(key, &mut expanded_key);
4242
} else {
4343
let output_size = digest.output_bytes();
4444
digest.input(key);

src/pbkdf2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub fn pbkdf2<M: Mac>(mac: &mut M, salt: &[u8], c: u32, output: &mut [u8]) {
105105
let mut tmp: Vec<u8> = repeat(0).take(os).collect();
106106
calculate_block(mac, salt, c, idx, &mut scratch[..], &mut tmp[..]);
107107
let chunk_len = chunk.len();
108-
copy_memory(chunk, &tmp[..chunk_len]);
108+
copy_memory(&tmp[..chunk_len], chunk);
109109
}
110110
}
111111
}

src/scrypt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ fn xor(x: &[u8], y: &[u8], output: &mut [u8]) {
9797
// output - the output vector. Must be the same length as input.
9898
fn scrypt_block_mix(input: &[u8], output: &mut [u8]) {
9999
let mut x = [0u8; 64];
100-
copy_memory(&mut x, &input[input.len() - 64..]);
100+
copy_memory(&input[input.len() - 64..], &mut x);
101101

102102
let mut t = [0u8; 64];
103103

104104
for (i, chunk) in input.chunks(64).enumerate() {
105105
xor(&x, chunk, &mut t);
106106
salsa20_8(&t, &mut x);
107107
let pos = if i % 2 == 0 { (i / 2) * 64 } else { (i / 2) * 64 + input.len() / 2 };
108-
copy_memory(&mut output[pos..pos + 64], &x);
108+
copy_memory(&x, &mut output[pos..pos + 64]);
109109
}
110110
}
111111

@@ -128,7 +128,7 @@ fn scrypt_ro_mix(b: &mut [u8], v: &mut [u8], t: &mut [u8], n: usize) {
128128
let len = b.len();
129129

130130
for chunk in v.chunks_mut(len) {
131-
copy_memory(chunk, b);
131+
copy_memory(b, chunk);
132132
scrypt_block_mix(chunk, b);
133133
}
134134

0 commit comments

Comments
 (0)