2023-06-04 22:01:09 +03:00
|
|
|
//
|
|
|
|
// Created by Andrew on 01/06/2023.
|
|
|
|
//
|
2019-08-21 18:29:48 +03:00
|
|
|
|
2023-06-01 19:24:07 +03:00
|
|
|
#include "header.h"
|
2023-06-01 16:09:22 +03:00
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
/* Unpacks the Windows XP-like Product Key. */
|
2023-06-04 16:44:22 +03:00
|
|
|
void unpackXP(
|
|
|
|
QWORD (&pRaw)[2],
|
|
|
|
DWORD &pSerial,
|
|
|
|
DWORD &pHash,
|
|
|
|
QWORD &pSignature
|
|
|
|
) {
|
2023-06-01 16:09:22 +03:00
|
|
|
// 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
|
2023-06-04 15:06:51 +03:00
|
|
|
pSerial = FIRSTNBITS(pRaw[0], 31);
|
2023-06-01 16:09:22 +03:00
|
|
|
|
2023-06-04 16:44:22 +03:00
|
|
|
// Hash = Bits [31..58] -> 28 bits
|
|
|
|
pHash = NEXTSNBITS(pRaw[0], 28, 31);
|
2023-06-01 16:09:22 +03:00
|
|
|
|
2023-06-04 16:44:22 +03:00
|
|
|
// Signature = Bits [59..113] -> 56 bits
|
|
|
|
pSignature = FIRSTNBITS(pRaw[1], 51) << 5 | NEXTSNBITS(pRaw[0], 5, 59);
|
2019-08-21 18:29:48 +03:00
|
|
|
}
|
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
/* Packs the Windows XP-like Product Key. */
|
2023-06-04 16:44:22 +03:00
|
|
|
void packXP(
|
|
|
|
QWORD (&pRaw)[2],
|
|
|
|
DWORD pSerial,
|
|
|
|
DWORD pHash,
|
|
|
|
QWORD pSignature
|
|
|
|
) {
|
|
|
|
// The quantity of information the key provides is 114 bits.
|
|
|
|
// We're storing it in 2 64-bit quad-words with 14 trailing bits.
|
|
|
|
// 64 * 2 = 128
|
|
|
|
|
|
|
|
// Signature [114..59] <- Hash [58..31] <- Serial [30..1] <- Upgrade [0]
|
2023-06-04 16:32:05 +03:00
|
|
|
pRaw[0] = FIRSTNBITS(pSignature, 5) << 59 | FIRSTNBITS(pHash, 28) << 31 | pSerial;
|
2023-06-04 16:44:22 +03:00
|
|
|
pRaw[1] = NEXTSNBITS(pSignature, 51, 5);
|
2019-08-21 18:29:48 +03:00
|
|
|
}
|
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
/* Verifies the Windows XP-like Product Key. */
|
|
|
|
bool verifyXPKey(
|
|
|
|
EC_GROUP *eCurve,
|
|
|
|
EC_POINT *basePoint,
|
|
|
|
EC_POINT *publicKey,
|
2023-06-05 12:13:15 +03:00
|
|
|
char (&pKey)[25]
|
2023-06-04 22:01:09 +03:00
|
|
|
) {
|
|
|
|
BN_CTX *numContext = BN_CTX_new();
|
2023-06-01 16:09:22 +03:00
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
QWORD pRaw[2]{},
|
|
|
|
pSignature = 0;
|
2023-06-01 16:09:22 +03:00
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
DWORD pSerial = 0,
|
|
|
|
pHash = 0;
|
2023-06-01 16:09:22 +03:00
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
// Convert Base24 CD-key to bytecode.
|
2023-06-05 12:13:15 +03:00
|
|
|
unbase24((BYTE *)pRaw, pKey);
|
2023-06-04 22:01:09 +03:00
|
|
|
|
|
|
|
// Extract RPK, hash and signature from bytecode.
|
|
|
|
unpackXP(pRaw, pSerial, pHash, pSignature);
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Scalars:
|
|
|
|
* e = Hash
|
|
|
|
* s = Schnorr Signature
|
|
|
|
*
|
|
|
|
* Points:
|
|
|
|
* G(x, y) = Generator (Base Point)
|
|
|
|
* K(x, y) = Public Key
|
|
|
|
*
|
|
|
|
* Equation:
|
|
|
|
* P = sG + eK
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
BIGNUM *e = BN_lebin2bn((BYTE *)&pHash, sizeof(pHash), nullptr),
|
|
|
|
*s = BN_lebin2bn((BYTE *)&pSignature, sizeof(pSignature), nullptr),
|
|
|
|
*x = BN_new(),
|
|
|
|
*y = BN_new();
|
|
|
|
|
|
|
|
// Create 2 points on the elliptic curve.
|
|
|
|
EC_POINT *t = EC_POINT_new(eCurve);
|
|
|
|
EC_POINT *p = EC_POINT_new(eCurve);
|
|
|
|
|
|
|
|
// t = sG
|
|
|
|
EC_POINT_mul(eCurve, t, nullptr, basePoint, s, numContext);
|
|
|
|
|
2023-06-07 01:37:30 +03:00
|
|
|
// P = eK
|
2023-06-04 22:01:09 +03:00
|
|
|
EC_POINT_mul(eCurve, p, nullptr, publicKey, e, numContext);
|
|
|
|
|
2023-06-07 01:37:30 +03:00
|
|
|
// P += t
|
2023-06-04 22:01:09 +03:00
|
|
|
EC_POINT_add(eCurve, p, t, p, numContext);
|
|
|
|
|
2023-06-07 01:37:30 +03:00
|
|
|
// x = P.x; y = P.y;
|
2023-06-04 22:01:09 +03:00
|
|
|
EC_POINT_get_affine_coordinates(eCurve, p, x, y, numContext);
|
|
|
|
|
|
|
|
BYTE msgDigest[SHA_DIGEST_LENGTH]{},
|
|
|
|
msgBuffer[SHA_MSG_LENGTH_XP]{},
|
|
|
|
xBin[FIELD_BYTES]{},
|
|
|
|
yBin[FIELD_BYTES]{};
|
|
|
|
|
|
|
|
// Convert resulting point coordinates to bytes.
|
|
|
|
BN_bn2lebin(x, xBin, FIELD_BYTES);
|
|
|
|
BN_bn2lebin(y, yBin, FIELD_BYTES);
|
|
|
|
|
|
|
|
// Assemble the SHA message.
|
|
|
|
memcpy((void *)&msgBuffer[0], (void *)&pSerial, 4);
|
|
|
|
memcpy((void *)&msgBuffer[4], (void *)xBin, FIELD_BYTES);
|
|
|
|
memcpy((void *)&msgBuffer[4 + FIELD_BYTES], (void *)yBin, FIELD_BYTES);
|
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// compHash = SHA1(pSerial || P.x || P.y)
|
2023-06-04 22:01:09 +03:00
|
|
|
SHA1(msgBuffer, SHA_MSG_LENGTH_XP, msgDigest);
|
|
|
|
|
|
|
|
// Translate the byte digest into a 32-bit integer - this is our computed hash.
|
|
|
|
// Truncate the hash to 28 bits.
|
2023-06-05 00:52:10 +03:00
|
|
|
DWORD compHash = BYDWORD(msgDigest) >> 4 & BITMASK(28);
|
2023-06-01 16:09:22 +03:00
|
|
|
|
|
|
|
BN_free(e);
|
|
|
|
BN_free(s);
|
|
|
|
BN_free(x);
|
|
|
|
BN_free(y);
|
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
BN_CTX_free(numContext);
|
2023-06-01 16:09:22 +03:00
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
EC_POINT_free(t);
|
|
|
|
EC_POINT_free(p);
|
2023-06-01 16:09:22 +03:00
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
// If the computed hash checks out, the key is valid.
|
|
|
|
return compHash == pHash;
|
2019-08-21 18:29:48 +03:00
|
|
|
}
|
2023-06-01 16:09:22 +03:00
|
|
|
|
|
|
|
/* Generate a valid Product Key. */
|
2023-06-04 22:01:09 +03:00
|
|
|
void generateXPKey(
|
|
|
|
EC_GROUP *eCurve,
|
|
|
|
EC_POINT *basePoint,
|
|
|
|
BIGNUM *genOrder,
|
|
|
|
BIGNUM *privateKey,
|
|
|
|
DWORD pSerial,
|
|
|
|
char (&pKey)[25]
|
|
|
|
) {
|
|
|
|
BN_CTX *numContext = BN_CTX_new();
|
2023-06-01 16:09:22 +03:00
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
BIGNUM *c = BN_new(),
|
|
|
|
*s = BN_new(),
|
|
|
|
*x = BN_new(),
|
|
|
|
*y = BN_new();
|
2023-06-01 16:09:22 +03:00
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
QWORD pRaw[2]{};
|
2023-06-01 16:09:22 +03:00
|
|
|
|
|
|
|
do {
|
2023-06-04 22:01:09 +03:00
|
|
|
EC_POINT *r = EC_POINT_new(eCurve);
|
2023-06-01 16:09:22 +03:00
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
QWORD pSignature = 0;
|
|
|
|
DWORD pHash;
|
2023-06-01 16:09:22 +03:00
|
|
|
|
|
|
|
// Generate a random number c consisting of 384 bits without any constraints.
|
|
|
|
BN_rand(c, FIELD_BITS, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY);
|
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
// Pick a random derivative of the base point on the elliptic curve.
|
|
|
|
// R = cG;
|
|
|
|
EC_POINT_mul(eCurve, r, nullptr, basePoint, c, numContext);
|
|
|
|
|
|
|
|
// Acquire its coordinates.
|
|
|
|
// x = R.x; y = R.y;
|
|
|
|
EC_POINT_get_affine_coordinates(eCurve, r, x, y, numContext);
|
|
|
|
|
|
|
|
BYTE msgDigest[SHA_DIGEST_LENGTH]{},
|
|
|
|
msgBuffer[SHA_MSG_LENGTH_XP]{},
|
|
|
|
xBin[FIELD_BYTES]{},
|
|
|
|
yBin[FIELD_BYTES]{};
|
|
|
|
|
|
|
|
// Convert coordinates to bytes.
|
|
|
|
BN_bn2lebin(x, xBin, FIELD_BYTES);
|
|
|
|
BN_bn2lebin(y, yBin, FIELD_BYTES);
|
|
|
|
|
|
|
|
// Assemble the SHA message.
|
|
|
|
memcpy((void *)&msgBuffer[0], (void *)&pSerial, 4);
|
|
|
|
memcpy((void *)&msgBuffer[4], (void *)xBin, FIELD_BYTES);
|
|
|
|
memcpy((void *)&msgBuffer[4 + FIELD_BYTES], (void *)yBin, FIELD_BYTES);
|
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// pHash = SHA1(pSerial || R.x || R.y)
|
2023-06-04 22:01:09 +03:00
|
|
|
SHA1(msgBuffer, SHA_MSG_LENGTH_XP, msgDigest);
|
|
|
|
|
|
|
|
// Translate the byte digest into a 32-bit integer - this is our computed pHash.
|
|
|
|
// Truncate the pHash to 28 bits.
|
2023-06-05 15:07:31 +03:00
|
|
|
pHash = BYDWORD(msgDigest) >> 4 & BITMASK(28);
|
2023-06-04 22:01:09 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Scalars:
|
|
|
|
* c = Random multiplier
|
|
|
|
* e = Hash
|
|
|
|
* s = Signature
|
|
|
|
* n = Order of G
|
|
|
|
* k = Private Key
|
|
|
|
* K = Public Key
|
|
|
|
*
|
|
|
|
* Points:
|
|
|
|
* G(x, y) = Generator (Base Point)
|
|
|
|
*
|
|
|
|
* We need to find the signature s that satisfies the equation with a given hash:
|
|
|
|
* P = sG + eK
|
|
|
|
* s = ek + c (mod n) <- computation optimization
|
|
|
|
*/
|
|
|
|
|
|
|
|
// s = ek;
|
2023-06-01 16:09:22 +03:00
|
|
|
BN_copy(s, privateKey);
|
2023-06-04 22:01:09 +03:00
|
|
|
BN_mul_word(s, pHash);
|
2023-06-01 16:09:22 +03:00
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
// s += c (mod n)
|
|
|
|
BN_mod_add(s, s, c, genOrder, numContext);
|
2023-06-01 16:09:22 +03:00
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
// Translate resulting scalar into a 64-bit integer (the byte order is little-endian).
|
|
|
|
BN_bn2lebinpad(s, (BYTE *)&pSignature, BN_num_bytes(s));
|
2023-06-01 16:09:22 +03:00
|
|
|
|
|
|
|
// Pack product key.
|
2023-06-04 22:01:09 +03:00
|
|
|
packXP(pRaw, pSerial, pHash, pSignature);
|
2023-06-01 22:19:35 +03:00
|
|
|
|
2023-06-07 01:04:39 +03:00
|
|
|
if (options.verbose) {
|
|
|
|
fmt::print("Generation results:\n");
|
|
|
|
fmt::print(" Serial: 0x{:08x}\n", pSerial);
|
|
|
|
fmt::print(" Hash: 0x{:08x}\n", pHash);
|
|
|
|
fmt::print(" Signature: 0x{:08x}\n", pSignature);
|
|
|
|
fmt::print("\n");
|
|
|
|
}
|
2023-06-02 07:25:43 +03:00
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
EC_POINT_free(r);
|
|
|
|
} while (pRaw[1] > BITMASK(50));
|
2023-06-01 16:09:22 +03:00
|
|
|
// ↑ ↑ ↑
|
2023-06-04 22:01:09 +03:00
|
|
|
// pRaw[1] can't be longer than 50 bits, else the signature part
|
|
|
|
// will make the CD-key longer than 25 characters.
|
2023-06-01 16:09:22 +03:00
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
// Convert bytecode to Base24 CD-key.
|
|
|
|
base24(pKey, (BYTE *)pRaw);
|
2023-06-01 16:09:22 +03:00
|
|
|
|
|
|
|
BN_free(c);
|
|
|
|
BN_free(s);
|
|
|
|
BN_free(x);
|
|
|
|
BN_free(y);
|
|
|
|
|
2023-06-04 22:01:09 +03:00
|
|
|
BN_CTX_free(numContext);
|
2023-06-01 16:09:22 +03:00
|
|
|
}
|