From 96639bbaf7f4065595ceb4cd2335eb0e5c9df674 Mon Sep 17 00:00:00 2001 From: Neo <321592+Neo-Desktop@users.noreply.github.com> Date: Thu, 1 Jun 2023 10:55:36 -0700 Subject: [PATCH] remove bink generation script add runtime json parsing add cmake.cpm update cmake to reflect this fix compilation errors ** breaks 2k3 key generation, work in progress --- .gitignore | 1 - CMakeLists.txt | 26 ++++++++--------- cmake/CPM.cmake | 33 ++++++++++++++++++++++ convert_keys_to_cpp.py | 63 ------------------------------------------ header.h | 7 +++-- main.cpp | 23 ++++++++------- server.cpp | 2 ++ 7 files changed, 65 insertions(+), 90 deletions(-) create mode 100644 cmake/CPM.cmake delete mode 100644 convert_keys_to_cpp.py diff --git a/.gitignore b/.gitignore index 27e3db0..1100325 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ build/* -bink.h ### NotepadPP template # Notepad++ backups # diff --git a/CMakeLists.txt b/CMakeLists.txt index 526a9f0..4bb0cfb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,22 +10,22 @@ if (!OPENSSL_FOUND) message(FATAL_ERROR "OpenSSL Development Libraries Not Found") endif() -# generate bink.h -add_custom_command( - OUTPUT bink.h - COMMAND ${PROJECT_SOURCE_DIR}/convert_keys_to_cpp.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - DEPENDS bink.h -) +include(cmake/CPM.cmake) + +# include json runtime library +CPMAddPackage( + NAME nlohmann_json + GITHUB_REPOSITORY nlohmann/json + VERSION 3.11.2) + +configure_file(keys.json keys.json COPYONLY) set(BUILD_SHARED_LIBS OFF) set(CMAKE_EXE_LINKER_FLAGS "-static") -ADD_EXECUTABLE(xpkey main.cpp xp.cpp key.cpp util.cpp cli.cpp bink.h) +ADD_EXECUTABLE(xpkey main.cpp xp.cpp key.cpp util.cpp cli.cpp) TARGET_INCLUDE_DIRECTORIES(xpkey PUBLIC crypto) -TARGET_LINK_LIBRARIES(xpkey PUBLIC crypto) -add_dependencies(xpkey bink.h) +TARGET_LINK_LIBRARIES(xpkey PUBLIC crypto nlohmann_json::nlohmann_json) -ADD_EXECUTABLE(srv2003key server.cpp key.cpp util.cpp cli.cpp bink.h) +ADD_EXECUTABLE(srv2003key server.cpp key.cpp util.cpp cli.cpp) TARGET_INCLUDE_DIRECTORIES(srv2003key PUBLIC crypto) -TARGET_LINK_LIBRARIES(srv2003key PUBLIC crypto) -add_dependencies(srv2003key bink.h) \ No newline at end of file +TARGET_LINK_LIBRARIES(srv2003key PUBLIC crypto nlohmann_json::nlohmann_json) \ No newline at end of file diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake new file mode 100644 index 0000000..a3086b7 --- /dev/null +++ b/cmake/CPM.cmake @@ -0,0 +1,33 @@ +set(CPM_DOWNLOAD_VERSION 0.38.1) + +if(CPM_SOURCE_CACHE) + set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +elseif(DEFINED ENV{CPM_SOURCE_CACHE}) + set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +else() + set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +endif() + +# Expand relative path. This is important if the provided path contains a tilde (~) +get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE) + +function(download_cpm) + message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}") + file(DOWNLOAD + https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake + ${CPM_DOWNLOAD_LOCATION} + ) +endfunction() + +if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION})) + download_cpm() +else() + # resume download if it previously failed + file(READ ${CPM_DOWNLOAD_LOCATION} check) + if("${check}" STREQUAL "") + download_cpm() + endif() + unset(check) +endif() + +include(${CPM_DOWNLOAD_LOCATION}) diff --git a/convert_keys_to_cpp.py b/convert_keys_to_cpp.py deleted file mode 100644 index e7b1449..0000000 --- a/convert_keys_to_cpp.py +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env python -import json - -with open('keys.json') as json_file: - data = json.load(json_file) - -with open('bink.h', 'w') as out: - out.write(''' - /****************************************************************** - * This file was automatically generated by convert_keys_to_cpp.py * - * DO NOT EDIT * - ******************************************************************/ - -#ifndef WINDOWSXPKG_BINK_H -#define WINDOWSXPKG_BINK_H - -struct ECDLP_Params { - // p, a, b - std::tuple E; - - // x, y - std::tuple K; - - // x, y - std::tuple G; - - std::string n; - std::string k; -}; - -std::unordered_map> Products; -std::unordered_map BINKData; - -struct ProductID { - uint8_t SiteID; - uint16_t Serial; -}; - -void initBink() { -''') - - for product in data['Products']: - d = data['Products'][product] - k = 0 - for v in d: - out.write(' Products["' + product + '"][' + str(k) + '] = "' + v + '";' + "\n") - k += 1 - - out.write("\n") - - for bink in data['BINK']: - d = data['BINK'][bink] - out.write(' BINKData["' + bink + '"] = {' + - '{"' + d['p'] + '", "' + d['a'] + '", "' + d['b'] + '"}, ' + - '{"' + d['pub']['x'] + '", "' + d['pub']['y'] + '"}, ' + - '{"' + d['g']['x'] + '", "' + d['g']['y'] + '"}, ' + - '"' + d['n'] + '", "' + d['priv'] + '"};' + "\n") - - out.write(''' -} - -#endif //WINDOWSXPKG_BINK_H -''') diff --git a/header.h b/header.h index 31020c4..5ac47d7 100644 --- a/header.h +++ b/header.h @@ -10,17 +10,18 @@ #include #include #include +#include #include #include #include +#include + #include #include #include #include -#include "bink.h" - #define PK_LENGTH 25 #define NULL_TERMINATOR 1 @@ -38,7 +39,7 @@ extern char charset[]; void endian(byte *data, int length); // key.cpp -void unbase24(ul32 *byteSeq, char *cdKey); +void unbase24(ul32 *byteSeq, const char *cdKey); void base24(char *cdKey, ul32 *byteSeq); // cli.cpp diff --git a/main.cpp b/main.cpp index 0dbe375..6f3f1a1 100644 --- a/main.cpp +++ b/main.cpp @@ -6,9 +6,12 @@ char charset[] = "BCDFGHJKMPQRTVWXY2346789"; +using json = nlohmann::json; + int main() { - initBink(); + std::ifstream f("keys.json"); + json keys = json::parse(f); rand(); srand(time(nullptr)); @@ -33,25 +36,25 @@ int main() // Data from pidgen-Bink-resources /* Elliptic curve parameters: y^2 = x^3 + ax + b mod p */ - BN_dec2bn(&p, std::get<0>(BINKData[BINKID].E).c_str()); - BN_dec2bn(&a, std::get<1>(BINKData[BINKID].E).c_str()); - BN_dec2bn(&b, std::get<2>(BINKData[BINKID].E).c_str()); + BN_dec2bn(&p, keys["BINK"][BINKID]["p"].get().c_str()); + BN_dec2bn(&a, keys["BINK"][BINKID]["a"].get().c_str()); + BN_dec2bn(&b, keys["BINK"][BINKID]["b"].get().c_str()); /* base point (generator) G */ - BN_dec2bn(&gx, std::get<0>(BINKData[BINKID].G).c_str()); - BN_dec2bn(&gy, std::get<1>(BINKData[BINKID].G).c_str()); + BN_dec2bn(&gx, keys["BINK"][BINKID]["g"]["x"].get().c_str()); + BN_dec2bn(&gy, keys["BINK"][BINKID]["g"]["y"].get().c_str()); /* inverse of public key */ - BN_dec2bn(&pubx, std::get<0>(BINKData[BINKID].K).c_str()); - BN_dec2bn(&puby, std::get<1>(BINKData[BINKID].K).c_str()); + BN_dec2bn(&pubx, keys["BINK"][BINKID]["pub"]["x"].get().c_str()); + BN_dec2bn(&puby, keys["BINK"][BINKID]["pub"]["y"].get().c_str()); // Computed data /* order of G - computed in 18 hours using a P3-450 */ - BN_dec2bn(&n, BINKData[BINKID].n.c_str()); + BN_dec2bn(&n, keys["BINK"][BINKID]["n"].get().c_str()); /* THE private key - computed in 10 hours using a P3-450 */ - BN_dec2bn(&priv, BINKData[BINKID].k.c_str()); + BN_dec2bn(&n, keys["BINK"][BINKID]["priv"].get().c_str()); // Calculation EC_GROUP *ec = EC_GROUP_new_curve_GFp(p, a, b, ctx); diff --git a/server.cpp b/server.cpp index 295320a..3ad4b14 100644 --- a/server.cpp +++ b/server.cpp @@ -1,5 +1,7 @@ #include "header.h" +char charset[] = "BCDFGHJKMPQRTVWXY2346789"; + void unpack2003(ul32 *osfamily, ul32 *hash, ul32 *sig, ul32 *prefix, ul32 *raw) { osfamily[0] = raw[0] & 0x7ff;