-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add getrandom impl using ctru_sys bindings #8
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,3 +67,33 @@ unsafe extern "C" fn clock_gettime( | |
|
||
retval | ||
} | ||
|
||
#[no_mangle] | ||
unsafe extern "C" fn getrandom( | ||
buf: *mut libc::c_void, | ||
mut buflen: libc::size_t, | ||
flags: libc::c_uint, | ||
) -> libc::ssize_t { | ||
// TODO: is this needed? Maybe just `buflen = buflen.min(libc::ssize_t::MAX)` ? | ||
buflen = buflen.min(0x1FFFFFF); | ||
|
||
if flags != 0 { | ||
// no flags are supported on 3DS | ||
*__errno() = libc::EINVAL; | ||
return -1; | ||
} | ||
|
||
let ret = ctru_sys::PS_GenerateRandomBytes(buf, buflen as libc::c_uint) as libc::ssize_t; | ||
if ret < 0 { | ||
// this is kind of a hack, but at least gives some visibility to the | ||
// error code returned by PS_GenerateRandomBytes I guess? Another option | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on this header file, these error codes aren't compatible: We would probably want to parse the 3ds result and return the appropriate error code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks nice! 👍 |
||
// might be to panic, which could use a payload of a specific error type | ||
ian-h-chamberlain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// that the ctru panic handler could decode into 3DS-specific human-readable | ||
// errors. | ||
*__errno() = ret as libc::c_int; | ||
-1 | ||
} else { | ||
// safe because above ensures buflen < isize::MAX | ||
buflen as libc::ssize_t | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This number chosen based on https://man7.org/linux/man-pages/man2/getrandom.2.html#NOTES
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is the GRND_RANDOM flag for using
random
instead ofurandom
(under linux). Maybe we could check those to change the maximum buffer length(forurandom
it's 33554431, while forrandom
it's 512), so it would more closely resemble the original.GRND_NONBLOCK
instead looks like it can just be ignored, as it doesn't change how our code behaves.We don't need to return errors when those flags are set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting... as it turns out,
getrandom
,getentropy
,GRND_NONBLOCK
, andGRND_RANDOM
seem to only be defined in newlib for Cygwin, and not on 3DS (or any other platform).So, I guess I will just add them to
horizon/mod.rs
inlibc
, and anyone using it will just have to live with the fact thatGRND_NONBLOCK
doesn't work at all.