Skip to content

Allow calling getrandom with 257-byte buffer - #355

Closed
kimhyunkang wants to merge 3 commits into
briansmith:masterfrom
kimhyunkang:getrandom_refactor
Closed

kimhyunkang wants to merge 3 commits into
briansmith:masterfrom
kimhyunkang:getrandom_refactor

Conversation

@kimhyunkang

Copy link
Copy Markdown
Contributor

Fixes issue #353.

Now rand::sysrand::fill does not loop with fixed 256-byte chunks,
but calls getrandom to fill the whole buffer.

I agree to license my contributions to each file under the terms given
at the top of each file I changed.

Fixes issue briansmith#353.

Now `rand::sysrand::fill` does not loop with fixed 256-byte chunks,
but calls `getrandom` to fill the whole buffer.

I agree to license my contributions to each file under the terms given
at the top of each file I changed.
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.009%) to 91.365% when pulling 624b6ed on kimhyunkang:getrandom_refactor into ba3de7c on briansmith:master.

1 similar comment
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.009%) to 91.365% when pulling 624b6ed on kimhyunkang:getrandom_refactor into ba3de7c on briansmith:master.

@briansmith briansmith left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, re-reviewing this I noticed that some changes are needed.

Comment thread crypto/rand/sysrand.c Outdated

const size_t GFp_sysrand_chunk_max_len = ULONG_MAX;

int GFp_sysrand_chunk(void *out, size_t requested) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See below. the return type needs to be long and we need to use LONG_MAX and long everywhere here.

Comment thread crypto/rand/sysrand.c Outdated
if (syscall(SYS_getrandom, out, requested, 0u) < 0) {
if (errno == ENOSYS) {
return -1;
int r = syscall(SYS_getrandom, out, requested, 0u);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed in the similar BoringSSL patch they are using long. And, according to http://man7.org/linux/man-pages/man2/syscall.2.html, long is the right type to use. That means that the function return type needs to be long and the FFI declaration in the Rust code needs to use c::long. I just added c::long in 5c4627a.

@briansmith briansmith Nov 23, 2016 •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, according to http://man7.org/linux/man-pages/man2/getrandom.2.html, the flags argument type is unsigned int. I suggest that we write this:

    unsigned int NO_FLAGS = 0;
    syscall(..., NO_FLAGS);

This way, we are sure there's no funny business going on.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot that C's long is different on Windows than on other platforms. I pushed a fix for it: dcba8a8

Comment thread src/rand.rs Outdated
#[cfg(any(target_os = "linux", windows))]
extern {
static GFp_sysrand_chunk_max_len: c::size_t;
fn GFp_sysrand_chunk(buf: *mut u8, len: c::size_t) -> c::int;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> c::long.

@kimhyunkang

Copy link
Copy Markdown
Contributor Author

@briansmith : I just updated the return types from int to long

@briansmith briansmith added this to the 0.6 milestone Dec 10, 2016
@briansmith

Copy link
Copy Markdown
Owner

Thanks for doing this! I merged this as 828e20f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants