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 all 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 @@
);
}

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 @@
printf("creating keys in %s and %s\n", argv[3], argv[4]);
n = atoi(argv[2]);

srand(random_int());
retval = random_int(srand_seed);

Check warning on line 124 in lib/crypt_prog.cpp

View check run for this annotation

Codecov / codecov/patch

lib/crypt_prog.cpp#L124

Added line #L124 was not covered by tests
if (retval == 1) {
die("can't load ADVAPI32.DLL");

Check warning on line 126 in lib/crypt_prog.cpp

View check run for this annotation

Codecov / codecov/patch

lib/crypt_prog.cpp#L126

Added line #L126 was not covered by tests
} else if (retval == 2) {
die("can't open /dev/random");

Check warning on line 128 in lib/crypt_prog.cpp

View check run for this annotation

Codecov / codecov/patch

lib/crypt_prog.cpp#L128

Added line #L128 was not covered by tests
} else if (retval == 3) {
die("can't read from /dev/random");

Check warning on line 130 in lib/crypt_prog.cpp

View check run for this annotation

Codecov / codecov/patch

lib/crypt_prog.cpp#L130

Added line #L130 was not covered by tests
} else if (retval) {
die("random_int");

Check warning on line 132 in lib/crypt_prog.cpp

View check run for this annotation

Codecov / codecov/patch

lib/crypt_prog.cpp#L132

Added line #L132 was not covered by tests
}
srand(srand_seed);

Check warning on line 134 in lib/crypt_prog.cpp

View check run for this annotation

Codecov / codecov/patch

lib/crypt_prog.cpp#L134

Added line #L134 was not covered by tests

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

#endif

int random_int(unsigned int &n) {

Check warning on line 546 in lib/util.cpp

View check run for this annotation

Codecov / codecov/patch

lib/util.cpp#L546

Added line #L546 was not covered by tests
#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 = boinc::fopen("/dev/random", "r");
if (!f) {
return 2;

Check warning on line 570 in lib/util.cpp

View check run for this annotation

Codecov / codecov/patch

lib/util.cpp#L568-L570

Added lines #L568 - L570 were not covered by tests
}
if (1 != boinc::fread(&n, sizeof(n), 1, f)) {
return 3;

Check warning on line 573 in lib/util.cpp

View check run for this annotation

Codecov / codecov/patch

lib/util.cpp#L572-L573

Added lines #L572 - L573 were not covered by tests
}
boinc::fclose(f);

Check warning on line 575 in lib/util.cpp

View check run for this annotation

Codecov / codecov/patch

lib/util.cpp#L575

Added line #L575 was not covered by tests
#endif
return 0;

Check warning on line 577 in lib/util.cpp

View check run for this annotation

Codecov / codecov/patch

lib/util.cpp#L577

Added line #L577 was not covered by tests
}
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