-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Use crypto safe random numbers #2738
Conversation
Thanks! A few comments:
|
Look like the problem is |
Thanks for the effort @jmfotokite
|
OK. We could also name it janus_random_uint52() or something. I just though it was very wrong to have a janus_random_uint64() method which returns only 52 bite. In the end, it is your call of course. I can understand if you do not want to change the signature name because 3rd party plugins depend on it or similar. What should we call the "real" rand64 method then?
Those are two orthogonal things. The crypto-safe RNG has nothing to do with the signature change. |
No need to be overly creative, I guess something like
I agree, it would just change their implementation: we do the same capping to 2^53-1 in the current version as well. In case this configure-level thing is added, though, it would mean not add a dependency to the crypto stuff where it's not needed in some cases though (e.g., mjr2pcap when not using the crypto-safe version). |
My idea was to use a macro defined in configure. |
The signatures are reverted now.
So, should I add a |
@jmfotokite I was just trying to collect feedback. |
@jmfotokite the linking with |
Thanks! Let's hope it works now 🙏 |
For sake of completeness, here is a small benchmark that compares the two approaches #include <stdio.h>
#include <inttypes.h>
#include <time.h>
#include <glib.h>
#include <openssl/rand.h>
static guint64 janus_random_uint64(void);
static guint64 janus_random_uint64_crypto(void);
guint64 janus_random_uint64(void) {
guint64 num = g_random_int() & 0x1FFFFF;
num = (num << 32) | g_random_int();
return num;
}
guint64 janus_random_uint64_crypto(void) {
guint64 ret = 0;
if (RAND_bytes((void *)&ret, sizeof(ret)) != 1) {
printf("\tOpenSSL RAND_bytes() failed\n");
exit(1);
}
ret = ret & 0x1FFFFFFFFFFFFF;
return ret;
}
int main(void)
{
const int ITERS=1000000;
clock_t tic = clock();
for(int i=0; i<ITERS; i++) {
janus_random_uint64();
//printf("random1 = %" PRIu64 "\n", val);
}
clock_t toc = clock();
printf("[\tg_random_int\t] : %d iterations took %f seconds\n", ITERS, (double)(toc - tic) / CLOCKS_PER_SEC);
tic = clock();
for(int j=0; j<ITERS; j++) {
janus_random_uint64_crypto();
//printf("random2 = %" PRIu64 "\n", val);
}
toc = clock();
printf("[\tRAND_bytes\t] : %d iterations took %f seconds\n", ITERS, (double)(toc - tic) / CLOCKS_PER_SEC);
} Iterating the RNG functions 100,000 times:
So yeah, the crypto-safe functions have more impact on the CPU, but the time needed to sample a value is still very low (0,0125 ms on average). |
Wow, I did not expect a 30x slowdown... but yes, we are actually not using janus_random_uint64() a lot, so that's fine. |
I've made some tests with echotest, videoroom and streaming. |
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.
Just added some notes inline, thanks again!
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.
Looks good, thanks! I just added a note on logging, and then it's good to go for me.
(obviously this dirty commit history should be squash-merged) |
Thanks, this looks good to me now! Merging then, thanks for the huge work 👍 |
This is a continuation of the discussion in https://groups.google.com/g/meetecho-janus/c/dIVe_v1xaxM.
It is not yet a ready-to-merge PR, more of a suggestion.
Some considerations:
cat /dev/urandom | pv > /dev/null
-->0:00:05 [ 197MiB/s] [ <=> ]