Skip to content

Commit

Permalink
Check for RDRAND failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Jethro Beekman committed Jul 9, 2016
1 parent 6ca4943 commit d386b5d
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,31 @@ mod imp {
impl Rng for OsRng {
fn next_u32(&mut self) -> u32 {
let ret;
unsafe{asm!("rdrand $0":"=r"(ret):::"volatile")};
let mut retry=10;
unsafe{asm!("
1:
rdrand $0
jc 2f
dec $1
jnz 1b
2:
":"=r"(ret),"=r"(retry):"1"(retry)::"volatile")};
if retry==0 { panic!("RDRAND failure") }
ret
}
#[cfg(target_arch="x86_64")]
fn next_u64(&mut self) -> u64 {
let ret;
unsafe{asm!("rdrand $0":"=r"(ret):::"volatile")};
let mut retry=10;
unsafe{asm!("
1:
rdrand $0
jc 2f
dec $1
jnz 1b
2:
":"=r"(ret),"=r"(retry):"1"(retry)::"volatile")};
if retry==0 { panic!("RDRAND failure") }
ret
}
}
Expand Down

0 comments on commit d386b5d

Please sign in to comment.