Skip to content

Commit 8cf2430

Browse files
committed
Merge pull request #77 from Florob/cleanup
Cleanup
2 parents d240fb4 + eda11f0 commit 8cf2430

File tree

3 files changed

+18
-35
lines changed

3 files changed

+18
-35
lines changed

src/rust-crypto/cryptoutil.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
use std;
12-
use std::mem;
1312
use std::num::{One, Zero, CheckedAdd};
1413
use std::slice::bytes::{MutableByteVector, copy_memory};
1514

@@ -126,62 +125,56 @@ pub fn read_u32_be(input: &[u8]) -> u32 {
126125
#[cfg(target_arch = "x86")]
127126
#[cfg(target_arch = "x86_64")]
128127
#[inline(never)]
129-
#[allow(unused_variable)]
130128
#[allow(dead_assignment)]
131129
unsafe fn fixed_time_eq_asm(mut lhsp: *u8, mut rhsp: *u8, mut count: uint) -> bool {
132130
let mut result: u8 = 0;
133-
let mut tmp: u8 = mem::uninitialized();
134131

135132
asm!(
136133
"
137134
fixed_time_eq_loop:
138135
139-
mov ($1), $4
140-
xor ($2), $4
141-
or $4, $0
136+
mov ($1), %cl
137+
xor ($2), %cl
138+
or %cl, $0
142139
143140
inc $1
144141
inc $2
145142
dec $3
146143
jnz fixed_time_eq_loop
147144
"
148-
: "=&r" (result), "=&r" (lhsp), "=&r" (rhsp), "=&r" (count), "=&r" (tmp) // output
149-
: "0" (result), "1" (lhsp), "2" (rhsp), "3" (count) // input
150-
: "cc" // clobbers
151-
: // flags
145+
: "+r" (result), "+r" (lhsp), "+r" (rhsp), "+r" (count) // all input and output
146+
: // input
147+
: "cl", "cc" // clobbers
148+
: "volatile" // flags
152149
);
153150

154151
return result == 0;
155152
}
156153

157154
#[cfg(target_arch = "arm")]
158155
#[inline(never)]
159-
#[allow(unused_variable)]
160156
#[allow(dead_assignment)]
161157
unsafe fn fixed_time_eq_asm(mut lhsp: *u8, mut rhsp: *u8, mut count: uint) -> bool {
162158
let mut result: u8 = 0;
163-
let mut tmp1: u8 = mem::uninitialized();
164-
let mut tmp2: u8 = mem::uninitialized();
165159

166160
asm!(
167161
"
168162
fixed_time_eq_loop:
169163
170-
ldrb $4, [$1]
171-
ldrb $5, [$2]
172-
eor $4, $4, $5
173-
orr $0, $0, $4
164+
ldrb r4, [$1]
165+
ldrb r5, [$2]
166+
eor r4, r4, r5
167+
orr $0, $0, r4
174168
175169
add $1, $1, #1
176170
add $2, $2, #1
177171
subs $3, $3, #1
178172
bne fixed_time_eq_loop
179173
"
180-
// output
181-
: "=&r" (result), "=&r" (lhsp), "=&r" (rhsp), "=&r" (count), "=&r" (tmp1), "=&r" (tmp2)
182-
: "0" (result), "1" (lhsp), "2" (rhsp), "3" (count) // input
183-
: "cc" // clobbers
184-
: // flags
174+
: "+r" (result), "+r" (lhsp), "+r" (rhsp), "+r" (count) // all input and output
175+
: // input
176+
: "r4", "r5", "cc" // clobbers
177+
: "volatile" // flags
185178
);
186179

187180
return result == 0;

src/rust-crypto/hmac.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,11 @@ fn expand_key<D: Digest>(digest: &mut D, key: &[u8]) -> Vec<u8> {
3737
let mut expanded_key = Vec::from_elem(bs, 0u8);
3838
if key.len() <= bs {
3939
slice::bytes::copy_memory(expanded_key.as_mut_slice(), key);
40-
for elem in expanded_key.mut_slice_from(key.len()).mut_iter() {
41-
*elem = 0;
42-
}
4340
} else {
4441
let output_size = digest.output_bytes();
4542
digest.input(key);
4643
digest.result(expanded_key.mut_slice_to(output_size));
4744
digest.reset();
48-
for elem in expanded_key.mut_slice_from(output_size).mut_iter() {
49-
*elem = 0;
50-
}
5145
}
5246
return expanded_key;
5347
}

src/rust-crypto/pbkdf2.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111

1212
use std::io::IoResult;
13-
use std::num::Bounded;
1413
use std::rand::{OsRng, Rng};
1514
use std::slice::MutableCloneableVector;
1615

@@ -94,12 +93,9 @@ pub fn pbkdf2<M: Mac>(mac: &mut M, salt: &[u8], c: u32, output: &mut [u8]) {
9493
let mut idx: u32 = 0;
9594

9695
for chunk in output.mut_chunks(os) {
97-
if idx == Bounded::max_value() {
98-
fail!("PBKDF2 size limit exceeded.");
99-
} else {
100-
// The block index starts at 1. So, this is supposed to run on the first execution.
101-
idx += 1;
102-
}
96+
// The block index starts at 1. So, this is supposed to run on the first execution.
97+
idx = idx.checked_add(&1).expect("PBKDF2 size limit exceeded.");
98+
10399
if chunk.len() == os {
104100
calculate_block(mac, salt, c, idx, scratch.as_mut_slice(), chunk);
105101
} else {

0 commit comments

Comments
 (0)