Skip to content

Commit

Permalink
Remove unsafe function call from GetRandom
Browse files Browse the repository at this point in the history
  • Loading branch information
richardfan1126 committed Nov 14, 2023
1 parent ed59afe commit 2c39ae9
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,18 @@ fn attest(public_key: Option<ByteBuf>, user_data: Option<ByteBuf>, nonce: Option
nsm_exit(nsm_fd);
}

unsafe fn get_random(byte_length:&u16) {
fn get_random(byte_length:&u16) {
if byte_length < &0 {
return;
}

let buf_len: &mut usize = &mut 0;

let mut buf = vec![0; *byte_length as usize];
let buf_ptr = buf.as_mut_ptr();
*buf_len = buf.len();

let nsm_fd = nsm_init();
let request = Request::GetRandom {};
let response = nsm_process_request(nsm_fd, request);

match response {
Response::GetRandom { random } => {
*buf_len = std::cmp::min(*buf_len, random.len());
std::ptr::copy_nonoverlapping(random.as_ptr(), buf_ptr, *buf_len);
print!("{}", base64::encode(buf));
print!("{}", base64::encode(random));
},
Response::Error(err) => {
eprintln!("{:?}", err);
Expand Down Expand Up @@ -139,9 +131,7 @@ fn main() {
std::process::exit(1)
}

unsafe {
get_random(length);
}
get_random(length);
}
}
}

0 comments on commit 2c39ae9

Please sign in to comment.