From 08ec7cb9bb6dde7ba588134bab8a15bb2bb77b46 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 4 Jun 2023 14:54:22 +0300 Subject: [PATCH] Simplify pointer arithmetics --- src/header.h | 8 ++++---- src/main.cpp | 6 +++--- src/util.cpp | 16 ++++++++-------- src/xp.cpp | 40 ++++++++++++++++++---------------------- 4 files changed, 33 insertions(+), 37 deletions(-) diff --git a/src/header.h b/src/header.h index 8a1d6a2..84c308e 100644 --- a/src/header.h +++ b/src/header.h @@ -67,8 +67,8 @@ EC_GROUP *initializeEllipticCurve( std::string generatorYSel, std::string publicKeyXSel, std::string publicKeyYSel, - EC_POINT **genPoint, - EC_POINT **pubPoint + EC_POINT *&genPoint, + EC_POINT *&pubPoint ); // key.cpp @@ -92,8 +92,8 @@ Options parseCommandLine(int argc, char* argv[]); void showHelp(char *argv[]); // xp.cpp -bool verifyXPKey(EC_GROUP *eCurve, EC_POINT *generator, EC_POINT *publicKey, char *cdKey); -void generateXPKey(char *pKey, EC_GROUP *eCurve, EC_POINT *generator, BIGNUM *order, BIGNUM *privateKey, DWORD *pRaw); +bool verifyXPKey(EC_GROUP *eCurve, EC_POINT *generator, EC_POINT *publicKey, char (&cdKey)[25]); +void generateXPKey(EC_GROUP *eCurve, EC_POINT *generator, BIGNUM *order, BIGNUM *privateKey, DWORD pRaw, char (&cdKey)[25]); // server.cpp diff --git a/src/main.cpp b/src/main.cpp index bf5320a..c734ba1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -99,8 +99,8 @@ int main(int argc, char *argv[]) { keys["BINK"][BINKID]["g"]["y"].get(), keys["BINK"][BINKID]["pub"]["x"].get(), keys["BINK"][BINKID]["pub"]["y"].get(), - &genPoint, - &pubPoint + genPoint, + pubPoint ); // Calculation @@ -125,7 +125,7 @@ int main(int argc, char *argv[]) { BN_sub(privateKey, genOrder, privateKey); nRaw <<= 1; - generateXPKey(pKey, eCurve, genPoint, genOrder, privateKey, &nRaw); + generateXPKey(eCurve, genPoint, genOrder, privateKey, nRaw, pKey); print_product_key(pKey); std::cout << std::endl << std::endl; diff --git a/src/util.cpp b/src/util.cpp index be2c7b9..0575499 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -27,8 +27,8 @@ EC_GROUP *initializeEllipticCurve( const std::string generatorYSel, const std::string publicKeyXSel, const std::string publicKeyYSel, - EC_POINT **genPoint, - EC_POINT **pubPoint + EC_POINT *&genPoint, + EC_POINT *&pubPoint ) { // Initialize BIGNUM and BIGNUMCTX structures. // BIGNUM - Large numbers @@ -69,16 +69,16 @@ EC_GROUP *initializeEllipticCurve( EC_GROUP *eCurve = EC_GROUP_new_curve_GFp(p, a, b, context); // Create new point for the generator on the elliptic curve and set its coordinates to (genX; genY). - *genPoint = EC_POINT_new(eCurve); - EC_POINT_set_affine_coordinates(eCurve, *genPoint, generatorX, generatorY, context); + genPoint = EC_POINT_new(eCurve); + EC_POINT_set_affine_coordinates(eCurve, genPoint, generatorX, generatorY, context); // Create new point for the public key on the elliptic curve and set its coordinates to (pubX; pubY). - *pubPoint = EC_POINT_new(eCurve); - EC_POINT_set_affine_coordinates(eCurve, *pubPoint, publicKeyX, publicKeyY, context); + pubPoint = EC_POINT_new(eCurve); + EC_POINT_set_affine_coordinates(eCurve, pubPoint, publicKeyX, publicKeyY, context); // If generator and public key points are not on the elliptic curve, either the generator or the public key values are incorrect. - assert(EC_POINT_is_on_curve(eCurve, *genPoint, context) == 1); - assert(EC_POINT_is_on_curve(eCurve, *pubPoint, context) == 1); + assert(EC_POINT_is_on_curve(eCurve, genPoint, context) == true); + assert(EC_POINT_is_on_curve(eCurve, pubPoint, context) == true); // Cleanup BN_CTX_free(context); diff --git a/src/xp.cpp b/src/xp.cpp index c743619..cabda5a 100644 --- a/src/xp.cpp +++ b/src/xp.cpp @@ -18,28 +18,24 @@ #include "header.h" /* Unpacks the Windows XP Product Key. */ -void unpackXP(DWORD *pRaw, DWORD *pSerial, DWORD *pHash, DWORD *pSignature) { +void unpackXP(DWORD (&pRaw)[4], DWORD &pSerial, DWORD &pHash, DWORD (&pSignature)[2]) { // We're assuming that the quantity of information within the product key is at most 114 bits. // log2(24^25) = 114. // Serial = Bits [0..30] -> 31 bits - if (pSerial) - pSerial[0] = pRaw[0] & 0x7fffffff; + pSerial = pRaw[0] & 0x7fffffff; // Hash (e) = Bits [31..58] -> 28 bits - if (pHash) - pHash[0] = ((pRaw[0] >> 31) | (pRaw[1] << 1)) & 0xfffffff; + pHash = ((pRaw[0] >> 31) | (pRaw[1] << 1)) & 0xfffffff; // Signature (s) = Bits [59..113] -> 55 bits - if (pSignature) { - pSignature[0] = (pRaw[1] >> 27) | (pRaw[2] << 5); - pSignature[1] = (pRaw[2] >> 27) | (pRaw[3] << 5); - } + pSignature[0] = (pRaw[1] >> 27) | (pRaw[2] << 5); + pSignature[1] = (pRaw[2] >> 27) | (pRaw[3] << 5); } /* Packs the Windows XP Product Key. */ -void packXP(DWORD *pRaw, const DWORD pSerial, const DWORD pHash, const DWORD *pSignature) { +void packXP(DWORD (&pRaw)[4], DWORD pSerial, DWORD pHash, DWORD (&pSignature)[2]) { pRaw[0] = pSerial | ((pHash & 1) << 31); pRaw[1] = (pHash >> 1) | ((pSignature[0] & 0x1f) << 27); pRaw[2] = (pSignature[0] >> 5) | (pSignature[1] << 27); @@ -47,7 +43,7 @@ void packXP(DWORD *pRaw, const DWORD pSerial, const DWORD pHash, const DWORD *pS } /* Verify Product Key */ -bool verifyXPKey(EC_GROUP *eCurve, EC_POINT *generator, EC_POINT *publicKey, char *cdKey) { +bool verifyXPKey(EC_GROUP *eCurve, EC_POINT *generator, EC_POINT *publicKey, char (&cdKey)[25]) { BN_CTX *context = BN_CTX_new(); // Convert Base24 CD-key to bytecode. @@ -57,7 +53,7 @@ bool verifyXPKey(EC_GROUP *eCurve, EC_POINT *generator, EC_POINT *publicKey, cha unbase24(bKey, cdKey); // Extract data, hash and signature from the bytecode. - unpackXP(bKey, &pID, &checkHash, sig); + unpackXP(bKey, pID, checkHash, sig); // e = Hash // s = Signature @@ -150,7 +146,7 @@ bool verifyXPKey(EC_GROUP *eCurve, EC_POINT *generator, EC_POINT *publicKey, cha } /* Generate a valid Product Key. */ -void generateXPKey(char *pKey, EC_GROUP *eCurve, EC_POINT *generator, BIGNUM *order, BIGNUM *privateKey, DWORD *pRaw) { +void generateXPKey(EC_GROUP *eCurve, EC_POINT *generator, BIGNUM *order, BIGNUM *privateKey, DWORD pRaw, char (&pKey)[25]) { EC_POINT *r = EC_POINT_new(eCurve); BN_CTX *ctx = BN_CTX_new(); @@ -182,10 +178,10 @@ void generateXPKey(char *pKey, EC_GROUP *eCurve, EC_POINT *generator, BIGNUM *or SHA1_Init(&hContext); // Chop Raw Product Key into 4 bytes. - t[0] = (*pRaw & 0xff); - t[1] = (*pRaw & 0xff00) >> 8; - t[2] = (*pRaw & 0xff0000) >> 16; - t[3] = (*pRaw & 0xff000000) >> 24; + t[0] = (pRaw & 0xff); + t[1] = (pRaw & 0xff00) >> 8; + t[2] = (pRaw & 0xff0000) >> 16; + t[3] = (pRaw & 0xff000000) >> 24; // Hash chunk of data. SHA1_Update(&hContext, t, sizeof(t)); @@ -229,13 +225,13 @@ void generateXPKey(char *pKey, EC_GROUP *eCurve, EC_POINT *generator, BIGNUM *or endian((BYTE *)sig, BN_num_bytes(s)); // Pack product key. - packXP(bKey, *pRaw, hash, sig); + packXP(bKey, pRaw, hash, sig); //printf("PID: %.8X\nHash: %.8X\nSig: %.8X %.8X\n", pRaw[0], hash, sig[1], sig[0]); - std::cout << " PID: " << std::hex << std::setw(8) << std::setfill('0') << pRaw[0] << std::endl - << "Hash: " << std::hex << std::setw(8) << std::setfill('0') << hash << std::endl - << " Sig: " << std::hex << std::setw(8) << std::setfill('0') << sig[1] << " " - << std::hex << std::setw(8) << std::setfill('0') << sig[2] << std::endl + std::cout << " PID: " << std::hex << std::setw(8) << std::setfill('0') << pRaw << std::endl + << "Hash: " << std::hex << std::setw(8) << std::setfill('0') << hash << std::endl + << " Sig: " << std::hex << std::setw(8) << std::setfill('0') << sig[0] << " " + << std::hex << std::setw(8) << std::setfill('0') << sig[1] << std::endl << std::endl; } while (bKey[3] >= 0x40000);