From 39a8e8e440d0fb1ee01b1c89afe85314d05ddc3a Mon Sep 17 00:00:00 2001 From: computezrmle <57127745+computezrmle@users.noreply.github.com> Date: Tue, 26 Mar 2024 22:13:27 +0100 Subject: [PATCH 1/5] Move random_int() to util.cpp --- lib/crypt_prog.cpp | 36 +----------------------------------- lib/util.cpp | 38 ++++++++++++++++++++++++++++++++++++++ lib/util.h | 1 + 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/lib/crypt_prog.cpp b/lib/crypt_prog.cpp index 5e4d5878b08..8d88c6ae611 100644 --- a/lib/crypt_prog.cpp +++ b/lib/crypt_prog.cpp @@ -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); @@ -85,41 +86,6 @@ 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; diff --git a/lib/util.cpp b/lib/util.cpp index 2af4b0fb945..b069c0ed1a5 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -542,3 +542,41 @@ bool process_exists(int pid) { } #endif + +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) { + fprintf(stderr, "Error: can't load ADVAPI32.DLL\n"); + exit(2); + } + 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) { + fprintf(stderr, "Error: can't open /dev/random\n"); + exit(2); + } + if (1 != fread(&n, sizeof(n), 1, f)) { + fprintf(stderr, "Error: can't read from /dev/random\n"); + exit(2); + } + fclose(f); +#endif + return n; +} diff --git a/lib/util.h b/lib/util.h index fd7f6bedf60..f95361cf3d0 100644 --- a/lib/util.h +++ b/lib/util.h @@ -30,6 +30,7 @@ extern double dtime(); extern double dday(); extern void boinc_sleep(double); extern void push_unique(std::string, std::vector&); +extern unsigned int random_int(); // NOTE: use #include to get max,min From f2908927a6e652c98e3c4b2f443874584ab80f71 Mon Sep 17 00:00:00 2001 From: computezrmle <57127745+computezrmle@users.noreply.github.com> Date: Wed, 27 Mar 2024 12:06:20 +0100 Subject: [PATCH 2/5] Fixes applied --- lib/util.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/util.cpp b/lib/util.cpp index b069c0ed1a5..111033af511 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -549,11 +549,10 @@ unsigned int random_int() { #if defined(__CYGWIN32__) HMODULE hLib=LoadLibrary((const char *)"ADVAPI32.DLL"); #else - HMODULE hLib=LoadLibrary("ADVAPI32.DLL"); + HMODULE hLib=LoadLibraryA("ADVAPI32.DLL"); #endif if (!hLib) { fprintf(stderr, "Error: can't load ADVAPI32.DLL\n"); - exit(2); } BOOLEAN (APIENTRY *pfn)(void*, ULONG) = (BOOLEAN (APIENTRY *)(void*,ULONG))GetProcAddress(hLib,"SystemFunction036"); @@ -570,11 +569,9 @@ unsigned int random_int() { FILE* f = fopen("/dev/random", "r"); if (!f) { fprintf(stderr, "Error: can't open /dev/random\n"); - exit(2); } if (1 != fread(&n, sizeof(n), 1, f)) { fprintf(stderr, "Error: can't read from /dev/random\n"); - exit(2); } fclose(f); #endif From ce7ab834902421ebf2d7823a6fb2150e2e76c505 Mon Sep 17 00:00:00 2001 From: computezrmle <57127745+computezrmle@users.noreply.github.com> Date: Wed, 27 Mar 2024 21:31:57 +0100 Subject: [PATCH 3/5] Add changes to solve build issues --- lib/crypt_prog.cpp | 14 +++++++++++++- lib/util.cpp | 10 ++++------ lib/util.h | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/crypt_prog.cpp b/lib/crypt_prog.cpp index 8d88c6ae611..150bfd7a934 100644 --- a/lib/crypt_prog.cpp +++ b/lib/crypt_prog.cpp @@ -90,6 +90,7 @@ 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]; @@ -120,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); + e = BN_new(); retval = BN_set_word(e, (unsigned long)65537); if (retval != 1) { diff --git a/lib/util.cpp b/lib/util.cpp index 111033af511..2d94dcce953 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -543,8 +543,7 @@ bool process_exists(int pid) { #endif -unsigned int random_int() { - unsigned int n; +int random_int(unsigned int &n) { #if defined(_WIN32) #if defined(__CYGWIN32__) HMODULE hLib=LoadLibrary((const char *)"ADVAPI32.DLL"); @@ -552,7 +551,7 @@ unsigned int random_int() { HMODULE hLib=LoadLibraryA("ADVAPI32.DLL"); #endif if (!hLib) { - fprintf(stderr, "Error: can't load ADVAPI32.DLL\n"); + return 1; } BOOLEAN (APIENTRY *pfn)(void*, ULONG) = (BOOLEAN (APIENTRY *)(void*,ULONG))GetProcAddress(hLib,"SystemFunction036"); @@ -568,12 +567,11 @@ unsigned int random_int() { #else FILE* f = fopen("/dev/random", "r"); if (!f) { - fprintf(stderr, "Error: can't open /dev/random\n"); + return 2; } if (1 != fread(&n, sizeof(n), 1, f)) { - fprintf(stderr, "Error: can't read from /dev/random\n"); + return 3; } fclose(f); #endif - return n; } diff --git a/lib/util.h b/lib/util.h index f95361cf3d0..ce446c8bf5b 100644 --- a/lib/util.h +++ b/lib/util.h @@ -30,7 +30,7 @@ extern double dtime(); extern double dday(); extern void boinc_sleep(double); extern void push_unique(std::string, std::vector&); -extern unsigned int random_int(); +extern int random_int(unsigned int&); // NOTE: use #include to get max,min From ff5e878af7966d883bc353c1deb6eebfe64b4e88 Mon Sep 17 00:00:00 2001 From: computezrmle <57127745+computezrmle@users.noreply.github.com> Date: Thu, 28 Mar 2024 15:54:33 +0100 Subject: [PATCH 4/5] Add changes to address build errors In addition: Added 'return 0' at the end of 'random_int()' to mitigate a runtime error. --- lib/crypt_prog.cpp | 2 +- lib/util.cpp | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/crypt_prog.cpp b/lib/crypt_prog.cpp index 150bfd7a934..99ffb3b445a 100644 --- a/lib/crypt_prog.cpp +++ b/lib/crypt_prog.cpp @@ -132,7 +132,7 @@ int main(int argc, char** argv) { die("random_int"); } srand(srand_seed); - + e = BN_new(); retval = BN_set_word(e, (unsigned long)65537); if (retval != 1) { diff --git a/lib/util.cpp b/lib/util.cpp index 2d94dcce953..02b6c9431a1 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -565,13 +565,14 @@ int random_int(unsigned int &n) { } FreeLibrary(hLib); #else - 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)) { + if (1 != boinc::fread(&n, sizeof(n), 1, f)) { return 3; } - fclose(f); + boinc::fclose(f); #endif + return 0 } From d60cab97a609b6b369950cea993740ef2b8337cf Mon Sep 17 00:00:00 2001 From: computezrmle <57127745+computezrmle@users.noreply.github.com> Date: Thu, 28 Mar 2024 16:06:39 +0100 Subject: [PATCH 5/5] Added missing ";" --- lib/util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/util.cpp b/lib/util.cpp index 02b6c9431a1..fc3e56d9edd 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -574,5 +574,5 @@ int random_int(unsigned int &n) { } boinc::fclose(f); #endif - return 0 + return 0; }