Skip to content
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

Move random_int() to util.cpp #5555

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 14 additions & 36 deletions lib/crypt_prog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

#include "crypt.h"
#include "md5_file.h"
#include "util.h"

void die(const char* p) {
fprintf(stderr, "Error: %s\n", p);
Expand Down Expand Up @@ -85,45 +86,11 @@ void usage() {
);
}

unsigned int random_int() {
unsigned int n;
#if defined(_WIN32)
#if defined(__CYGWIN32__)
HMODULE hLib=LoadLibrary((const char *)"ADVAPI32.DLL");
#else
HMODULE hLib=LoadLibrary("ADVAPI32.DLL");
#endif
if (!hLib) {
die("Can't load ADVAPI32.DLL");
}
BOOLEAN (APIENTRY *pfn)(void*, ULONG) =
(BOOLEAN (APIENTRY *)(void*,ULONG))GetProcAddress(hLib,"SystemFunction036");
if (pfn) {
char buff[32];
ULONG ulCbBuff = sizeof(buff);
if(pfn(buff,ulCbBuff)) {
// use buff full of random goop
memcpy(&n,buff,sizeof(n));
}
}
FreeLibrary(hLib);
#else
FILE* f = fopen("/dev/random", "r");
if (!f) {
die("can't open /dev/random\n");
}
if (1 != fread(&n, sizeof(n), 1, f)) {
die("couldn't read from /dev/random\n");
}
fclose(f);
#endif
return n;
}

int main(int argc, char** argv) {
R_RSA_PUBLIC_KEY public_key;
R_RSA_PRIVATE_KEY private_key;
int i, n, retval;
unsigned int srand_seed;
bool is_valid;
DATA_BLOCK signature, in, out;
unsigned char signature_buf[256], buf[256], buf2[256];
Expand Down Expand Up @@ -154,7 +121,18 @@ int main(int argc, char** argv) {
printf("creating keys in %s and %s\n", argv[3], argv[4]);
n = atoi(argv[2]);

srand(random_int());
retval = random_int(srand_seed);
if (retval == 1) {
die("can't load ADVAPI32.DLL");
} else if (retval == 2) {
die("can't open /dev/random");
} else if (retval == 3) {
die("can't read from /dev/random");
} else if (retval) {
die("random_int");
}
srand(srand_seed);

Copy link
Member

Choose a reason for hiding this comment

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

run locally this command to remove trailing spaces:
python3 ci_tools/trailing_whitespaces_check.py . --fix

e = BN_new();
retval = BN_set_word(e, (unsigned long)65537);
if (retval != 1) {
Expand Down
33 changes: 33 additions & 0 deletions lib/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,36 @@ bool process_exists(int pid) {
}

#endif

int random_int(unsigned int &n) {
#if defined(_WIN32)
#if defined(__CYGWIN32__)
HMODULE hLib=LoadLibrary((const char *)"ADVAPI32.DLL");
#else
HMODULE hLib=LoadLibraryA("ADVAPI32.DLL");
#endif
if (!hLib) {
return 1;
}
BOOLEAN (APIENTRY *pfn)(void*, ULONG) =
Copy link
Member

Choose a reason for hiding this comment

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

If the library was not loaded correctly two lines above - this will fail with an exception

(BOOLEAN (APIENTRY *)(void*,ULONG))GetProcAddress(hLib,"SystemFunction036");
if (pfn) {
char buff[32];
ULONG ulCbBuff = sizeof(buff);
if(pfn(buff,ulCbBuff)) {
// use buff full of random goop
memcpy(&n,buff,sizeof(n));
}
}
FreeLibrary(hLib);
#else
FILE* f = fopen("/dev/random", "r");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
FILE* f = fopen("/dev/random", "r");
FILE* f = boinc::fopen("/dev/random", "r");

if (!f) {
return 2;
}
if (1 != fread(&n, sizeof(n), 1, f)) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (1 != fread(&n, sizeof(n), 1, f)) {
if (1 != boinc::fread(&n, sizeof(n), 1, f)) {

return 3;
}
fclose(f);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
fclose(f);
boinc::fclose(f);

#endif
}
1 change: 1 addition & 0 deletions lib/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extern double dtime();
extern double dday();
extern void boinc_sleep(double);
extern void push_unique(std::string, std::vector<std::string>&);
extern int random_int(unsigned int&);

// NOTE: use #include <functional> to get max,min

Expand Down
Loading