Fix datatypes to use platform independent values, Add command line switch scaffolding

Allow users to chose which bink/channelid they'd like to generate with
Add rudimentary help system, Sanitize user input
This commit is contained in:
Neo
2023-06-01 21:25:43 -07:00
parent e437cc548a
commit 72d441b539
7 changed files with 250 additions and 144 deletions

View File

@@ -3,21 +3,64 @@
//
#include "header.h"
#include <iostream>
char charset[] = "BCDFGHJKMPQRTVWXY2346789";
const std::string filename = "keys.json";
using json = nlohmann::json;
int main() {
char* BINKID = "2E";
int main(int argc, char *argv[]) {
Options options = parseCommandLine(argc, argv);
std::ifstream f("keys.json");
if (options.help || options.error) {
if (options.error) {
std::cout << "error parsing command line options" << std::endl;
}
showHelp(argv);
return 0;
}
if (options.verbose) {
std::cout << "loading " << filename << std::endl;
}
std::ifstream f(filename);
json keys = json::parse(f);
rand();
srand(time(nullptr));
rand();
if (options.verbose) {
std::cout << "loaded " << filename << " successfully" << std::endl;
}
if (options.list) {
for (auto el : keys["Products"].items()) {
int id;
sscanf((el.value()[0]).get<std::string>().c_str(), "%x", &id);
if (id >= 0x50) {
continue;
}
std::cout << el.key() << ": " << el.value() << std::endl;
}
std::cout << std::endl << std::endl
<< "** Please note: any BINK ID other than 2E is considered experimental at this time **"
<< std::endl;
return 0;
}
int intBinkID;
sscanf(options.binkid.c_str(), "%x", &intBinkID);
if (intBinkID >= 0x50) {
std::cout << "ERROR: BINK2002 and beyond is not supported in this application at this time" << std::endl;
return 1;
}
if (options.channelID > 999) {
std::cout << "ERROR: refusing to create a key with a siteID greater than 999" << std::endl;
return 1;
}
const char* BINKID = options.binkid.c_str();
// We cannot produce a valid key without knowing the private key k. The reason for this is that
// we need the result of the function K(x; y) = kG(x; y).
@@ -31,56 +74,52 @@ int main() {
BN_dec2bn(&genOrder, keys["BINK"][BINKID]["n"].get<std::string>().c_str());
BN_dec2bn(&privateKey, keys["BINK"][BINKID]["priv"].get<std::string>().c_str());
std::cout << keys["BINK"][BINKID]["p"].get<std::string>().c_str() << std::endl;
std::cout << keys["BINK"][BINKID]["a"].get<std::string>().c_str() << std::endl;
std::cout << keys["BINK"][BINKID]["b"].get<std::string>().c_str() << std::endl;
std::cout << keys["BINK"][BINKID]["g"]["x"].get<std::string>().c_str() << std::endl;
std::cout << keys["BINK"][BINKID]["g"]["y"].get<std::string>().c_str() << std::endl;
std::cout << keys["BINK"][BINKID]["pub"]["x"].get<std::string>().c_str() << std::endl;
std::cout << keys["BINK"][BINKID]["pub"]["y"].get<std::string>().c_str() << std::endl;
std::cout << keys["BINK"][BINKID]["n"].get<std::string>().c_str() << std::endl;
std::cout << keys["BINK"][BINKID]["priv"].get<std::string>().c_str() << std::endl;
if (options.verbose) {
std::cout << "-----------------------------------------------------------" << std::endl
<< "Loaded the following curve constraints: BINK[" << BINKID << "]" << std::endl
<< "-----------------------------------------------------------" << std::endl
<< " P: " << keys["BINK"][BINKID]["p"].get<std::string>() << std::endl
<< " a: " << keys["BINK"][BINKID]["a"].get<std::string>() << std::endl
<< " b: " << keys["BINK"][BINKID]["b"].get<std::string>() << std::endl
<< "Gx: " << keys["BINK"][BINKID]["g"]["x"].get<std::string>() << std::endl
<< "Gy: " << keys["BINK"][BINKID]["g"]["y"].get<std::string>() << std::endl
<< "Kx: " << keys["BINK"][BINKID]["pub"]["x"].get<std::string>() << std::endl
<< "Ky: " << keys["BINK"][BINKID]["pub"]["y"].get<std::string>() << std::endl
<< " n: " << keys["BINK"][BINKID]["n"].get<std::string>() << std::endl
<< " k: " << keys["BINK"][BINKID]["priv"].get<std::string>() << std::endl
<< std::endl << std::endl;
}
EC_POINT *genPoint, *pubPoint;
EC_GROUP *eCurve = initializeEllipticCurve(
keys["BINK"][BINKID]["p"].get<std::string>().c_str(),
keys["BINK"][BINKID]["a"].get<std::string>().c_str(),
keys["BINK"][BINKID]["b"].get<std::string>().c_str(),
keys["BINK"][BINKID]["g"]["x"].get<std::string>().c_str(),
keys["BINK"][BINKID]["g"]["y"].get<std::string>().c_str(),
keys["BINK"][BINKID]["pub"]["x"].get<std::string>().c_str(),
keys["BINK"][BINKID]["pub"]["y"].get<std::string>().c_str(),
keys["BINK"][BINKID]["p"].get<std::string>(),
keys["BINK"][BINKID]["a"].get<std::string>(),
keys["BINK"][BINKID]["b"].get<std::string>(),
keys["BINK"][BINKID]["g"]["x"].get<std::string>(),
keys["BINK"][BINKID]["g"]["y"].get<std::string>(),
keys["BINK"][BINKID]["pub"]["x"].get<std::string>(),
keys["BINK"][BINKID]["pub"]["y"].get<std::string>(),
&genPoint,
&pubPoint
);
/*BN_print_fp(stdout, p);
std::cout << std::endl;
BN_print_fp(stdout, a);
std::cout << std::endl;
BN_print_fp(stdout, b);
std::cout << std::endl;
BN_print_fp(stdout, gx);
std::cout << std::endl;
BN_print_fp(stdout, gy);
std::cout << std::endl;
BN_print_fp(stdout, pubx);
std::cout << std::endl;
BN_print_fp(stdout, puby);
std::cout << std::endl;
BN_print_fp(stdout, n);
std::cout << std::endl;
BN_print_fp(stdout, priv);
std::cout << std::endl;*/
// Calculation
char pKey[25];
ul32 nRaw = 640 * 1000000 ; /* <- change */
//nRaw += rand() & 999999;
uint32_t nRaw = options.channelID * 1000000 ; /* <- change */
printf("> PID: %u\n", nRaw);
BIGNUM *bnrand = BN_new();
BN_rand(bnrand, 19, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY);
int oRaw;
char *cRaw = BN_bn2dec(bnrand);
sscanf(cRaw, "%d", &oRaw);
nRaw += (oRaw &= 0xF423F); // ensure our serial is less than 999999
if (options.verbose) {
std::cout << "> PID: " << std::setw(9) << std::setfill('0') << nRaw << std::endl;
}
// generate a key
BN_sub(privateKey, genOrder, privateKey);
@@ -88,10 +127,12 @@ int main() {
generateXPKey(pKey, eCurve, genPoint, genOrder, privateKey, &nRaw);
print_product_key(pKey);
printf("\n\n");
std::cout << std::endl << std::endl;
// verify the key
if (!verifyXPKey(eCurve, genPoint, pubPoint, pKey)) printf("Fail! Key is invalid.\n");
if (!verifyXPKey(eCurve, genPoint, pubPoint, pKey)) {
std::cout << "Fail! Key is invalid." << std::endl;
}
return 0;
}