Refactor, improved readability

This commit is contained in:
Andrew 2023-06-01 16:09:22 +03:00
parent 3999ed0996
commit f5423a8eb0
12 changed files with 548 additions and 464 deletions

8
.idea/misc.xml generated
View File

@ -1,5 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CidrRootsConfiguration">
<sourceRoots>
<file path="$PROJECT_DIR$" />
</sourceRoots>
<libraryRoots>
<file path="$PROJECT_DIR$/lib" />
</libraryRoots>
</component>
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MakefileSettings">
<option name="linkedExternalProjectsSettings">

8
.idea/modules.xml generated
View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/WindowsXPKg.iml" filepath="$PROJECT_DIR$/.idea/WindowsXPKg.iml" />
</modules>
</component>
</project>

View File

@ -2,30 +2,17 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8.11)
PROJECT(WindowsXPKg)
SET(CMAKE_CXX_STANDARD 17)
set(OPENSSL_USE_STATIC_LIBS TRUE)
find_package(PkgConfig REQUIRED)
pkg_search_module(OPENSSL REQUIRED openssl)
FIND_PACKAGE(PkgConfig REQUIRED)
PKG_SEARCH_MODULE(OPENSSL REQUIRED openssl)
if (!OPENSSL_FOUND)
message(FATAL_ERROR "OpenSSL Development Libraries Not Found")
endif()
IF(!OPENSSL_FOUND)
MESSAGE(FATAL_ERROR "OpenSSL 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
)
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_EXE_LINKER_FLAGS "-static")
ADD_EXECUTABLE(xpkey xp_algorithm.cpp shared.cpp bink.h)
ADD_EXECUTABLE(xpkey xp_algorithm.cpp key.cpp)
TARGET_INCLUDE_DIRECTORIES(xpkey PUBLIC crypto)
TARGET_LINK_LIBRARIES(xpkey PUBLIC crypto)
add_dependencies(xpkey bink.h)
ADD_EXECUTABLE(srv2003key server_algorithm.cpp shared.cpp bink.h)
ADD_EXECUTABLE(srv2003key server_algorithm.cpp key.cpp)
TARGET_INCLUDE_DIRECTORIES(srv2003key PUBLIC crypto)
TARGET_LINK_LIBRARIES(srv2003key PUBLIC crypto)
add_dependencies(srv2003key bink.h)

44
cli.cpp Normal file
View File

@ -0,0 +1,44 @@
//
// Created by Andrew on 01/06/2023.
//
#include "header.h"
void print_product_id(ul32 *pid)
{
char raw[12];
char b[6], c[8];
int i, digit = 0;
// Cut a away last bit of pid and convert it to an accii-number (=raw)
sprintf(raw, "%lu", pid[0] >> 1);
// Make b-part {640-....}
strncpy(b, raw, 3);
b[3] = 0;
// Make c-part {...-123456X...}
strcpy(c, raw + 3);
printf("> %s\n", c);
// Make checksum digit-part {...56X-}
assert(strlen(c) == 6);
for (i = 0; i < 6; i++)
digit -= c[i] - '0'; // Sum digits
while (digit < 0)
digit += 7;
c[6] = digit + '0';
c[7] = 0;
printf("Product ID: PPPPP-%s-%s-23xxx\n", b, c);
}
void print_product_key(char *pk) {
int i;
assert(strlen(pk) == 25);
for (i = 0; i < 25; i++) {
putchar(pk[i]);
if (i != 24 && i % 5 == 4) putchar('-');
}
}

76
header.h Normal file
View File

@ -0,0 +1,76 @@
//
// Created by neo on 5/26/2023.
//
#ifndef WINDOWSXPKG_HEADER_H
#define WINDOWSXPKG_HEADER_H
#include <cassert>
#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <string>
#include <vector>
#include <unordered_map>
#include <openssl/bn.h>
#include <openssl/ec.h>
#include <openssl/sha.h>
#include <openssl/rand.h>
#define PK_LENGTH 25
#define NULL_TERMINATOR 1
#define FIELD_BITS 384
#define FIELD_BYTES 48
#define FIELD_BITS_2003 512
#define FIELD_BYTES_2003 64
typedef unsigned char byte;
typedef unsigned long ul32;
extern char charset[];
// util.cpp
void endian(byte *data, int length);
// key.cpp
void unbase24(ul32 *byteSeq, char *cdKey);
void base24(char *cdKey, ul32 *byteSeq);
// cli.cpp
void print_product_key(char *pk);
void print_product_id(ul32 *pid);
// 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, ul32 *pRaw);
// server.cpp
struct ECDLP_Params {
// p, a, b
std::tuple<std::string, std::string, std::string> E;
// x, y
std::tuple<std::string, std::string> K;
// x, y
std::tuple<std::string, std::string> G;
std::string n;
std::string k;
};
struct ProductID {
uint8_t SiteID;
uint16_t Serial;
};
extern std::unordered_map<std::string, std::unordered_map<int, std::string>> Products;
extern std::unordered_map<std::string, ECDLP_Params> BINKData;
void initBink();
#endif //WINDOWSXPKG_HEADER_H

68
key.cpp Normal file
View File

@ -0,0 +1,68 @@
//
// Created by neo on 5/26/2023.
//
#include "header.h"
/* Converts from CD-key to a byte sequence. */
void unbase24(ul32 *byteSeq, const char *cdKey) {
byte pDecodedKey[PK_LENGTH + NULL_TERMINATOR]{};
BIGNUM *y = BN_new();
BN_zero(y);
// Remove dashes from the CD-key and put it into a Base24 byte array.
for (int i = 0, k = 0; i < strlen(cdKey) && k < PK_LENGTH; i++) {
for (int j = 0; j < 24; j++) {
if (cdKey[i] != '-' && cdKey[i] == charset[j]) {
pDecodedKey[k++] = j;
break;
}
}
}
// Empty byte sequence.
memset(byteSeq, 0, 16);
// Calculate the weighed sum of byte array elements.
for (int i = 0; i < PK_LENGTH; i++) {
BN_mul_word(y, PK_LENGTH - 1);
BN_add_word(y, pDecodedKey[i]);
}
// Acquire length.
int n = BN_num_bytes(y);
// Place the generated code into the byte sequence.
BN_bn2bin(y, (byte *)byteSeq);
BN_free(y);
// Reverse the byte sequence.
endian((byte *) byteSeq, n);
}
/* Converts from byte sequence to the CD-key. */
void base24(char *cdKey, ul32 *byteSeq) {
byte rbyteSeq[16];
BIGNUM *z;
// Copy byte sequence to the reversed byte sequence.
memcpy(rbyteSeq, byteSeq, sizeof(rbyteSeq));
// Skip trailing zeroes and reverse y.
int length;
for (length = 15; rbyteSeq[length] == 0; length--);
endian(rbyteSeq, ++length);
// Convert reversed byte sequence to BigNum z.
z = BN_bin2bn(rbyteSeq, length, nullptr);
// Divide z by 24 and convert the remainder to a CD-key char.
cdKey[25] = 0;
for (int i = 24; i >= 0; i--)
cdKey[i] = charset[BN_div_word(z, 24)];
BN_free(z);
}

83
main.cpp Normal file
View File

@ -0,0 +1,83 @@
//
// Created by Andrew on 01/06/2023.
//
#include "header.h"
char charset[] = "BCDFGHJKMPQRTVWXY2346789";
int main()
{
initBink();
rand();
srand(time(nullptr));
rand();
// Init
BIGNUM *a, *b, *p, *gx, *gy, *pubx, *puby, *n, *priv;
BN_CTX *ctx = BN_CTX_new();
// make BigNumbers
a = BN_new();
b = BN_new();
p = BN_new();
gx = BN_new();
gy = BN_new();
pubx = BN_new();
puby = BN_new();
n = BN_new();
priv = BN_new();
char* BINKID = "2E";
// 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());
/* 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());
/* 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());
// Computed data
/* order of G - computed in 18 hours using a P3-450 */
BN_dec2bn(&n, BINKData[BINKID].n.c_str());
/* THE private key - computed in 10 hours using a P3-450 */
BN_dec2bn(&priv, BINKData[BINKID].k.c_str());
// Calculation
EC_GROUP *ec = EC_GROUP_new_curve_GFp(p, a, b, ctx);
EC_POINT *g = EC_POINT_new(ec);
EC_POINT_set_affine_coordinates_GFp(ec, g, gx, gy, ctx);
EC_POINT *pub = EC_POINT_new(ec);
EC_POINT_set_affine_coordinates_GFp(ec, pub, pubx, puby, ctx);
char pkey[26];
ul32 pid[1];
pid[0] = 640 * 1000000 ; /* <- change */
pid[0] += rand() & 999999;
printf("> PID: %lu\n", pid[0]);
// generate a key
BN_sub(priv, n, priv);
generateXPKey(pkey, ec, g, n, priv, pid);
print_product_key(pkey);
printf("\n\n");
// verify the key
verifyXPKey(ec, g, pub, (char*)pkey);
// Cleanup
BN_CTX_free(ctx);
return 0;
}

View File

@ -1,10 +1,6 @@
#include "shared.h"
#include "bink.h"
#include "header.h"
#define FIELD_BITS_2003 512
#define FIELD_BYTES_2003 64
void unpack2003(uint32_t *osfamily, uint32_t *hash, uint32_t *sig, uint32_t *prefix, uint32_t *raw)
void unpack2003(ul32 *osfamily, ul32 *hash, ul32 *sig, ul32 *prefix, ul32 *raw)
{
osfamily[0] = raw[0] & 0x7ff;
hash[0] = ((raw[0] >> 11) | (raw[1] << 21)) & 0x7fffffff;
@ -13,7 +9,7 @@ void unpack2003(uint32_t *osfamily, uint32_t *hash, uint32_t *sig, uint32_t *pre
prefix[0] = (raw[3] >> 8) & 0x3ff;
}
void pack2003(uint32_t *raw, uint32_t *osfamily, uint32_t *hash, uint32_t *sig, uint32_t *prefix)
void pack2003(ul32 *raw, ul32 *osfamily, ul32 *hash, ul32 *sig, ul32 *prefix)
{
raw[0] = osfamily[0] | (hash[0] << 11);
raw[1] = (hash[0] >> 21) | (sig[0] << 10);
@ -23,12 +19,12 @@ void pack2003(uint32_t *raw, uint32_t *osfamily, uint32_t *hash, uint32_t *sig,
int verify2003(EC_GROUP *ec, EC_POINT *generator, EC_POINT *public_key, char *cdkey)
{
uint8_t key[25];
char key[25];
BN_CTX *ctx = BN_CTX_new();
for (int i = 0, k = 0; i < strlen(cdkey); i++) {
for (int j = 0; j < 24; j++) {
if (cdkey[i] != '-' && cdkey[i] == cset[j]) {
if (cdkey[i] != '-' && cdkey[i] == charset[j]) {
key[k++] = j;
break;
}
@ -37,16 +33,18 @@ int verify2003(EC_GROUP *ec, EC_POINT *generator, EC_POINT *public_key, char *cd
if (k >= 25) break;
}
uint32_t bkey[4] = {0};
uint32_t osfamily[1], hash[1], sig[2], prefix[1];
ul32 bkey[4] = {0};
ul32 osfamily[1], hash[1], sig[2], prefix[1];
unbase24(bkey, key);
printf("%.8x %.8x %.8x %.8x\n", bkey[3], bkey[2], bkey[1], bkey[0]);
printf("%.8lx %.8lx %.8lx %.8lx\n", bkey[3], bkey[2], bkey[1], bkey[0]);
unpack2003(osfamily, hash, sig, prefix, bkey);
printf("OS Family: %u\nHash: %.8x\nSig: %.8x %.8x\nPrefix: %.8x\n", osfamily[0], hash[0], sig[1], sig[0], prefix[0]);
printf("OS Family: %lu\nHash: %.8lx\nSig: %.8lx %.8lx\nPrefix: %.8lx\n", osfamily[0], hash[0], sig[1], sig[0], prefix[0]);
uint8_t buf[FIELD_BYTES_2003], md[20];
uint32_t h1[2];
byte buf[FIELD_BYTES_2003], md[20];
ul32 h1[2];
SHA_CTX h_ctx;
/* h1 = SHA-1(5D || OS Family || Hash || Prefix || 00 00) */
@ -66,26 +64,26 @@ int verify2003(EC_GROUP *ec, EC_POINT *generator, EC_POINT *public_key, char *cd
h1[0] = md[0] | (md[1] << 8) | (md[2] << 16) | (md[3] << 24);
h1[1] = (md[4] | (md[5] << 8) | (md[6] << 16) | (md[7] << 24)) >> 2;
h1[1] &= 0x3FFFFFFF;
printf("h1: %.8x %.8x\n", h1[1], h1[0]);
printf("h1: %.8lx %.8lx\n", h1[1], h1[0]);
BIGNUM *s, *h, *x, *y;
x = BN_new();
y = BN_new();
endian((uint8_t *)sig, 8);
endian((uint8_t *)h1, 8);
s = BN_bin2bn((uint8_t *)sig, 8, NULL);
h = BN_bin2bn((uint8_t *)h1, 8, NULL);
endian((byte *)sig, 8);
endian((byte *)h1, 8);
s = BN_bin2bn((byte *)sig, 8, nullptr);
h = BN_bin2bn((byte *)h1, 8, nullptr);
EC_POINT *r = EC_POINT_new(ec);
EC_POINT *t = EC_POINT_new(ec);
/* r = sig*(sig*generator + h1*public_key) */
EC_POINT_mul(ec, t, NULL, generator, s, ctx);
EC_POINT_mul(ec, r, NULL, public_key, h, ctx);
EC_POINT_mul(ec, t, nullptr, generator, s, ctx);
EC_POINT_mul(ec, r, nullptr, public_key, h, ctx);
EC_POINT_add(ec, r, r, t, ctx);
EC_POINT_mul(ec, r, NULL, r, s, ctx);
EC_POINT_get_affine_coordinates_GFp(ec, r, x, y, ctx);
EC_POINT_mul(ec, r, nullptr, r, s, ctx);
EC_POINT_get_affine_coordinates(ec, r, x, y, ctx);
uint32_t h2[1];
ul32 h2[1];
/* h2 = SHA-1(79 || OS Family || r.x || r.y) */
SHA1_Init(&h_ctx);
buf[0] = 0x79;
@ -95,17 +93,17 @@ int verify2003(EC_GROUP *ec, EC_POINT *generator, EC_POINT *public_key, char *cd
memset(buf, 0, FIELD_BYTES_2003);
BN_bn2bin(x, buf);
endian((uint8_t *)buf, FIELD_BYTES_2003);
endian((byte *)buf, FIELD_BYTES_2003);
SHA1_Update(&h_ctx, buf, FIELD_BYTES_2003);
memset(buf, 0, FIELD_BYTES_2003);
BN_bn2bin(y, buf);
endian((uint8_t *)buf, FIELD_BYTES_2003);
endian((byte *)buf, FIELD_BYTES_2003);
SHA1_Update(&h_ctx, buf, FIELD_BYTES_2003);
SHA1_Final(md, &h_ctx);
h2[0] = (md[0] | (md[1] << 8) | (md[2] << 16) | (md[3] << 24)) & 0x7fffffff;
printf("Calculated hash: %.8x\n", h2[0]);
printf("Calculated hash: %.8lx\n", h2[0]);
BN_free(s);
BN_free(h);
@ -125,7 +123,7 @@ int verify2003(EC_GROUP *ec, EC_POINT *generator, EC_POINT *public_key, char *cd
}
}
void generate2003(uint8_t *pkey, EC_GROUP *ec, EC_POINT *generator, BIGNUM *order, BIGNUM *priv, uint32_t *osfamily, uint32_t *prefix)
void generate2003(char *pkey, EC_GROUP *ec, EC_POINT *generator, BIGNUM *order, BIGNUM *priv, ul32 *osfamily, ul32 *prefix)
{
BN_CTX *ctx = BN_CTX_new();
@ -136,17 +134,17 @@ void generate2003(uint8_t *pkey, EC_GROUP *ec, EC_POINT *generator, BIGNUM *orde
BIGNUM *b = BN_new();
EC_POINT *r = EC_POINT_new(ec);
uint32_t bkey[4];
uint8_t buf[FIELD_BYTES_2003], md[20];
uint32_t h1[2];
uint32_t hash[1], sig[2];
ul32 bkey[4];
byte buf[FIELD_BYTES_2003], md[20];
ul32 h1[2];
ul32 hash[1], sig[2];
SHA_CTX h_ctx;
for (;;) {
/* r = k*generator */
BN_pseudo_rand(k, FIELD_BITS_2003, -1, 0);
EC_POINT_mul(ec, r, NULL, generator, k, ctx);
EC_POINT_mul(ec, r, nullptr, generator, k, ctx);
EC_POINT_get_affine_coordinates_GFp(ec, r, x, y, ctx);
/* hash = SHA-1(79 || OS Family || r.x || r.y) */
@ -158,12 +156,12 @@ void generate2003(uint8_t *pkey, EC_GROUP *ec, EC_POINT *generator, BIGNUM *orde
memset(buf, 0, FIELD_BYTES_2003);
BN_bn2bin(x, buf);
endian((uint8_t *)buf, FIELD_BYTES_2003);
endian((byte *)buf, FIELD_BYTES_2003);
SHA1_Update(&h_ctx, buf, FIELD_BYTES_2003);
memset(buf, 0, FIELD_BYTES_2003);
BN_bn2bin(y, buf);
endian((uint8_t *)buf, FIELD_BYTES_2003);
endian((byte *)buf, FIELD_BYTES_2003);
SHA1_Update(&h_ctx, buf, FIELD_BYTES_2003);
SHA1_Final(md, &h_ctx);
@ -186,11 +184,11 @@ void generate2003(uint8_t *pkey, EC_GROUP *ec, EC_POINT *generator, BIGNUM *orde
h1[0] = md[0] | (md[1] << 8) | (md[2] << 16) | (md[3] << 24);
h1[1] = (md[4] | (md[5] << 8) | (md[6] << 16) | (md[7] << 24)) >> 2;
h1[1] &= 0x3FFFFFFF;
printf("h1: %.8x %.8x\n", h1[1], h1[0]);
printf("h1: %.8lx %.8lx\n", h1[1], h1[0]);
/* s = ( -h1*priv + sqrt( (h1*priv)^2 + 4k ) ) / 2 */
endian((uint8_t *)h1, 8);
BN_bin2bn((uint8_t *)h1, 8, b);
endian((byte *)h1, 8);
BN_bin2bn((byte *)h1, 8, b);
BN_mod_mul(b, b, priv, order, ctx);
BN_copy(s, b);
BN_mod_sqr(s, s, order, ctx);
@ -203,14 +201,15 @@ void generate2003(uint8_t *pkey, EC_GROUP *ec, EC_POINT *generator, BIGNUM *orde
}
BN_rshift1(s, s);
sig[0] = sig[1] = 0;
BN_bn2bin(s, (uint8_t *)sig);
endian((uint8_t *)sig, BN_num_bytes(s));
BN_bn2bin(s, (byte *)sig);
endian((byte *)sig, BN_num_bytes(s));
if (sig[1] < 0x40000000) break;
}
pack2003(bkey, osfamily, hash, sig, prefix);
printf("OS family: %u\nHash: %.8x\nSig: %.8x %.8x\nPrefix: %.8x\n", osfamily[0], hash[0], sig[1], sig[0], prefix[0]);
printf("%.8x %.8x %.8x %.8x\n", bkey[3], bkey[2], bkey[1], bkey[0]);
base24(pkey, bkey);
printf("OS family: %lu\nHash: %.8lx\nSig: %.8lx %.8lx\nPrefix: %.8lx\n", osfamily[0], hash[0], sig[1], sig[0], prefix[0]);
printf("%.8lx %.8lx %.8lx %.8lx\n", bkey[3], bkey[2], bkey[1], bkey[0]);
base24(pkey, bkey);
BN_free(k);
BN_free(s);
@ -223,7 +222,7 @@ void generate2003(uint8_t *pkey, EC_GROUP *ec, EC_POINT *generator, BIGNUM *orde
}
int main()
int gen2003()
{
BIGNUM *a, *b, *p, *gx, *gy, *pubx, *puby, *n, *priv;
BN_CTX *ctx = BN_CTX_new();
@ -259,11 +258,11 @@ int main()
assert(EC_POINT_is_on_curve(ec, g, ctx) == 1);
assert(EC_POINT_is_on_curve(ec, pub, ctx) == 1);
uint8_t pkey[25];
uint32_t osfamily[1], prefix[1];
char pkey[25];
ul32 osfamily[1], prefix[1];
osfamily[0] = 1280;
RAND_pseudo_bytes((uint8_t *)prefix, 4);
RAND_pseudo_bytes((byte *)prefix, 4);
prefix[0] &= 0x3ff;
do {

View File

@ -1,102 +0,0 @@
//
// Created by neo on 5/26/2023.
//
#include "shared.h"
uint8_t cset[] = "BCDFGHJKMPQRTVWXY2346789";
// Reverse data
void endian(uint8_t *data, int len)
{
int i;
for (i = 0; i < len/2; i++) {
uint8_t temp;
temp = data[i];
data[i] = data[len-i-1];
data[len-i-1] = temp;
}
}
void unbase24(uint32_t *x, uint8_t *c)
{
memset(x, 0, 16);
int i, n;
BIGNUM *y = BN_new();
BN_zero(y);
for (i = 0; i < 25; i++)
{
BN_mul_word(y, 24);
BN_add_word(y, c[i]);
}
n = BN_num_bytes(y);
BN_bn2bin(y, (uint8_t *)x);
BN_free(y);
endian((uint8_t *)x, n);
}
void base24(uint8_t *c, uint32_t *x)
{
uint8_t y[16];
int i;
BIGNUM *z;
// Convert x to BigNum z
memcpy(y, x, sizeof(y)); // Copy X to Y; Y=X
for (i = 15; y[i] == 0; i--) {} i++; // skip following nulls
endian(y, i); // Reverse y
z = BN_bin2bn(y, i, NULL); // Convert y to BigNum z
// Divide z by 24 and convert remainder with cset to Base24-CDKEY Char
c[25] = 0;
for (i = 24; i >= 0; i--) {
uint8_t t = BN_div_word(z, 24);
c[i] = cset[t];
}
BN_free(z);
}
void print_product_id(uint32_t *pid)
{
char raw[12];
char b[6], c[8];
int i, digit = 0;
// Cut a away last bit of pid and convert it to an accii-number (=raw)
sprintf(raw, "%d", pid[0] >> 1);
// Make b-part {640-....}
strncpy(b, raw, 3);
b[3] = 0;
// Make c-part {...-123456X...}
strcpy(c, raw + 3);
printf("> %s\n", c);
// Make checksum digit-part {...56X-}
assert(strlen(c) == 6);
for (i = 0; i < 6; i++)
digit -= c[i] - '0'; // Sum digits
while (digit < 0)
digit += 7;
c[6] = digit + '0';
c[7] = 0;
printf("Product ID: PPPPP-%s-%s-23xxx\n", b, c);
}
void print_product_key(uint8_t *pk)
{
int i;
assert(strlen((const char *)pk) == 25);
for (i = 0; i < 25; i++) {
putchar(pk[i]);
if (i != 24 && i % 5 == 4) putchar('-');
}
}

View File

@ -1,54 +0,0 @@
//
// Created by neo on 5/26/2023.
//
#ifndef WINDOWSXPKG_SHARED_H
#define WINDOWSXPKG_SHARED_H
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <string>
#include <vector>
#include <unordered_map>
#include <openssl/bn.h>
#include <openssl/ec.h>
#include <openssl/sha.h>
#include <openssl/rand.h>
extern uint8_t cset[];
void endian(uint8_t *data, int len);
void unbase24(uint32_t *x, uint8_t *c);
void base24(uint8_t *c, uint32_t *x);
void print_product_key(uint8_t *pk);
void print_product_id(uint32_t *pid);
struct ECDLP_Params {
// p, a, b
std::tuple<std::string, std::string, std::string> E;
// x, y
std::tuple<std::string, std::string> K;
// x, y
std::tuple<std::string, std::string> G;
std::string n;
std::string k;
};
struct ProductID {
uint8_t SiteID;
uint16_t Serial;
};
extern std::unordered_map<std::string, std::unordered_map<int, std::string>> Products;
extern std::unordered_map<std::string, ECDLP_Params> BINKData;
void initBink();
#endif //WINDOWSXPKG_SHARED_H

14
util.cpp Normal file
View File

@ -0,0 +1,14 @@
//
// Created by Andrew on 01/06/2023.
//
#include "header.h"
/* Convert data between endianness types. */
void endian(byte *data, int length) {
for (int i = 0; i < length / 2; i++) {
byte temp = data[i];
data[i] = data[length - i - 1];
data[length - i - 1] = temp;
}
}

View File

@ -15,265 +15,234 @@
*/
#include "shared.h"
#include "bink.h"
#include "header.h"
#define FIELD_BITS 384
#define FIELD_BYTES 48
/* Unpacks the Windows XP Product Key. */
void unpackXP(ul32 *serial, ul32 *hash, ul32 *sig, ul32 *raw) {
// We're assuming that the quantity of information within the product key is at most 114 bits.
// log2(24^25) = 114.
static void unpack(uint32_t *pid, uint32_t *hash, uint32_t *sig, uint32_t *raw)
{
// pid = Bit 0..30
pid[0] = raw[0] & 0x7fffffff;
// Serial = Bits [0..30] -> 31 bits
if (serial)
serial[0] = raw[0] & 0x7fffffff;
// hash(s) = Bit 31..58
hash[0] = ((raw[0] >> 31) | (raw[1] << 1)) & 0xfffffff;
// Hash (e) = Bits [31..58] -> 28 bits
if (hash)
hash[0] = ((raw[0] >> 31) | (raw[1] << 1)) & 0xfffffff;
// sig(e) = bit 58..113
sig[0] = (raw[1] >> 27) | (raw[2] << 5);
sig[1] = (raw[2] >> 27) | (raw[3] << 5);
// Signature (s) = Bits [59..113] -> 55 bits
if (sig) {
sig[0] = (raw[1] >> 27) | (raw[2] << 5);
sig[1] = (raw[2] >> 27) | (raw[3] << 5);
}
}
static void pack(uint32_t *raw, uint32_t *pid, uint32_t *hash, uint32_t *sig)
{
raw[0] = pid[0] | ((hash[0] & 1) << 31);
raw[1] = (hash[0] >> 1) | ((sig[0] & 0x1f) << 27);
raw[2] = (sig[0] >> 5) | (sig[1] << 27);
raw[3] = sig[1] >> 5;
/* Packs the Windows XP Product Key. */
void packXP(ul32 *raw, const ul32 *serial, const ul32 *hash, const ul32 *sig) {
raw[0] = serial[0] | ((hash[0] & 1) << 31);
raw[1] = (hash[0] >> 1) | ((sig[0] & 0x1f) << 27);
raw[2] = (sig[0] >> 5) | (sig[1] << 27);
raw[3] = sig[1] >> 5;
}
void verify(EC_GROUP *ec, EC_POINT *generator, EC_POINT *public_key, char *cdkey)
{
uint8_t key[25];
int i, j, k;
/* Verify Product Key */
bool verifyXPKey(EC_GROUP *eCurve, EC_POINT *generator, EC_POINT *publicKey, char *cdKey) {
BN_CTX *context = BN_CTX_new();
BN_CTX *ctx = BN_CTX_new();
// remove Dashs from CDKEY
for (i = 0, k = 0; i < strlen(cdkey); i++) {
for (j = 0; j < 24; j++) {
if (cdkey[i] != '-' && cdkey[i] == cset[j]) {
key[k++] = j;
break;
}
assert(j < 24);
}
if (k >= 25) break;
}
// Convert Base24 CD-key to bytecode.
ul32 bKey[4]{};
ul32 pID, checkHash, sig[2];
// Base24_CDKEY -> Bin_CDKEY
uint32_t bkey[4] = {0};
uint32_t pid[1], hash[1], sig[2];
unbase24(bkey, key);
unbase24(bKey, cdKey);
// Output Bin_CDKEY
printf("%.8x %.8x %.8x %.8x\n", bkey[3], bkey[2], bkey[1], bkey[0]);
// Extract data, hash and signature from the bytecode.
unpackXP(&pID, &checkHash, sig, bKey);
// Divide/Extract pid_data, hash, sig from Bin_CDKEY
unpack(pid, hash, sig, bkey);
print_product_id(pid);
// e = Hash
// s = Signature
BIGNUM *e, *s;
printf("PID: %.8x\nHash: %.8x\nSig: %.8x %.8x\n", pid[0], hash[0], sig[1], sig[0]);
// Put hash word into BigNum e.
e = BN_new();
BN_set_word(e, checkHash);
// Reverse signature and create a new BigNum s.
endian((byte *)sig, sizeof(sig));
s = BN_bin2bn((byte *)sig, sizeof(sig), nullptr);
BIGNUM *e, *s;
// Create x and y.
BIGNUM *x = BN_new();
BIGNUM *y = BN_new();
/* e = hash, s = sig */
e = BN_new();
BN_set_word(e, hash[0]);
endian((uint8_t *)sig, sizeof(sig));
s = BN_bin2bn((uint8_t *)sig, sizeof(sig), NULL);
// Create 2 new points on the existing elliptic curve.
EC_POINT *u = EC_POINT_new(eCurve);
EC_POINT *v = EC_POINT_new(eCurve);
BIGNUM *x = BN_new();
BIGNUM *y = BN_new();
EC_POINT *u = EC_POINT_new(ec);
EC_POINT *v = EC_POINT_new(ec);
// EC_POINT_mul calculates r = generator * n + q * m.
// v = s * generator + e * (-publicKey)
/* v = s*generator + e*(-public_key) */
EC_POINT_mul(ec, u, NULL, generator, s, ctx);
EC_POINT_mul(ec, v, NULL, public_key, e, ctx);
EC_POINT_add(ec, v, u, v, ctx);
EC_POINT_get_affine_coordinates_GFp(ec, v, x, y, ctx);
// u = generator * s
EC_POINT_mul(eCurve, u, nullptr, generator, s, context);
uint8_t buf[FIELD_BYTES], md[20];
uint32_t h;
uint8_t t[4];
SHA_CTX h_ctx;
// v = publicKey * e
EC_POINT_mul(eCurve, v, nullptr, publicKey, e, context);
/* h = (fist 32 bits of SHA1(pid || v.x, v.y)) >> 4 */
SHA1_Init(&h_ctx);
t[0] = pid[0] & 0xff;
t[1] = (pid[0] & 0xff00) >> 8;
t[2] = (pid[0] & 0xff0000) >> 16;
t[3] = (pid[0] & 0xff000000) >> 24;
SHA1_Update(&h_ctx, t, sizeof(t));
// v += u
EC_POINT_add(eCurve, v, u, v, context);
memset(buf, 0, sizeof(buf));
BN_bn2bin(x, buf);
endian((uint8_t *)buf, sizeof(buf));
SHA1_Update(&h_ctx, buf, sizeof(buf));
// EC_POINT_get_affine_coordinates() sets x and y, either of which may be nullptr, to the corresponding coordinates of p.
// x = v.x; y = v.y;
EC_POINT_get_affine_coordinates(eCurve, v, x, y, context);
memset(buf, 0, sizeof(buf));
BN_bn2bin(y, buf);
endian((uint8_t *)buf, sizeof(buf));
SHA1_Update(&h_ctx, buf, sizeof(buf));
byte buf[FIELD_BYTES], md[SHA_DIGEST_LENGTH], t[4];
ul32 newHash;
SHA1_Final(md, &h_ctx);
h = (md[0] | (md[1] << 8) | (md[2] << 16) | (md[3] << 24)) >> 4;
h &= 0xfffffff;
SHA_CTX hContext;
printf("Calculated hash: %.8x\n", h);
if (h == hash[0]) printf("Key valid\n");
else printf("Key invalid\n");
putchar('\n');
// h = First32(SHA-1(pID || v.x || v.y)) >> 4
SHA1_Init(&hContext);
BN_free(e);
BN_free(s);
BN_free(x);
BN_free(y);
EC_POINT_free(u);
EC_POINT_free(v);
// Chop Product ID into 4 bytes.
t[0] = (pID & 0xff); // First 8 bits
t[1] = (pID & 0xff00) >> 8; // Second 8 bits
t[2] = (pID & 0xff0000) >> 16; // Third 8 bits
t[3] = (pID & 0xff000000) >> 24; // Fourth 8 bits
BN_CTX_free(ctx);
// Hash chunk of data.
SHA1_Update(&hContext, t, sizeof(t));
// Empty buffer, place v.x in little-endian.
memset(buf, 0, FIELD_BYTES);
BN_bn2bin(x, buf);
endian(buf, FIELD_BYTES);
// Hash chunk of data.
SHA1_Update(&hContext, buf, FIELD_BYTES);
// Empty buffer, place v.y in little-endian.
memset(buf, 0, FIELD_BYTES);
BN_bn2bin(y, buf);
endian(buf, FIELD_BYTES);
// Hash chunk of data.
SHA1_Update(&hContext, buf, FIELD_BYTES);
// Store the final message from hContext in md.
SHA1_Final(md, &hContext);
// h = First32(SHA-1(pID || v.x || v.y)) >> 4
newHash = (md[0] | (md[1] << 8) | (md[2] << 16) | (md[3] << 24)) >> 4;
newHash &= 0xfffffff;
BN_free(e);
BN_free(s);
BN_free(x);
BN_free(y);
BN_CTX_free(context);
EC_POINT_free(u);
EC_POINT_free(v);
// If we managed to generate a key with the same hash, the key is correct.
return newHash == checkHash;
}
void generate(uint8_t *pkey, EC_GROUP *ec, EC_POINT *generator, BIGNUM *order, BIGNUM *priv, uint32_t *pid)
{
BN_CTX *ctx = BN_CTX_new();
/* Generate a valid Product Key. */
void generateXPKey(char *pKey, EC_GROUP *eCurve, EC_POINT *generator, BIGNUM *order, BIGNUM *privateKey, ul32 *pRaw) {
EC_POINT *r = EC_POINT_new(eCurve);
BN_CTX *ctx = BN_CTX_new();
BIGNUM *k = BN_new();
BIGNUM *s = BN_new();
BIGNUM *x = BN_new();
BIGNUM *y = BN_new();
EC_POINT *r = EC_POINT_new(ec);
uint32_t bkey[4];
BIGNUM *c = BN_new();
BIGNUM *s = BN_new();
BIGNUM *x = BN_new();
BIGNUM *y = BN_new();
// Loop in case signaturepart will make cdkey(base-24 "digits") longer than 25
do {
BN_pseudo_rand(k, FIELD_BITS, -1, 0);
EC_POINT_mul(ec, r, NULL, generator, k, ctx);
EC_POINT_get_affine_coordinates_GFp(ec, r, x, y, ctx);
ul32 bKey[4]{};
SHA_CTX h_ctx;
uint8_t t[4], md[20], buf[FIELD_BYTES];
uint32_t hash[1];
/* h = (fist 32 bits of SHA1(pid || r.x, r.y)) >> 4 */
SHA1_Init(&h_ctx);
t[0] = pid[0] & 0xff;
t[1] = (pid[0] & 0xff00) >> 8;
t[2] = (pid[0] & 0xff0000) >> 16;
t[3] = (pid[0] & 0xff000000) >> 24;
SHA1_Update(&h_ctx, t, sizeof(t));
do {
ul32 hash = 0, sig[2]{};
memset(buf, 0, sizeof(buf));
BN_bn2bin(x, buf);
endian((uint8_t *)buf, sizeof(buf));
SHA1_Update(&h_ctx, buf, sizeof(buf));
memset(bKey, 0, 4);
memset(buf, 0, sizeof(buf));
BN_bn2bin(y, buf);
endian((uint8_t *)buf, sizeof(buf));
SHA1_Update(&h_ctx, buf, sizeof(buf));
// 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);
SHA1_Final(md, &h_ctx);
hash[0] = (md[0] | (md[1] << 8) | (md[2] << 16) | (md[3] << 24)) >> 4;
hash[0] &= 0xfffffff;
// r = generator * c;
EC_POINT_mul(eCurve, r, nullptr, generator, c, ctx);
/* s = priv*h + k */
BN_copy(s, priv);
BN_mul_word(s, hash[0]);
BN_mod_add(s, s, k, order, ctx);
// x = r.x; y = r.y;
EC_POINT_get_affine_coordinates(eCurve, r, x, y, ctx);
uint32_t sig[2] = {0};
BN_bn2bin(s, (uint8_t *)sig);
endian((uint8_t *)sig, BN_num_bytes(s));
pack(bkey, pid, hash, sig);
printf("PID: %.8x\nHash: %.8x\nSig: %.8x %.8x\n", pid[0], hash[0], sig[1], sig[0]);
} while (bkey[3] >= 0x40000);
SHA_CTX hContext;
byte md[SHA_DIGEST_LENGTH]{}, buf[FIELD_BYTES]{}, t[4]{};
base24(pkey, bkey);
// h = (First-32(SHA1(pRaw, r.x, r.y)) >> 4
SHA1_Init(&hContext);
BN_free(k);
BN_free(s);
BN_free(x);
BN_free(y);
EC_POINT_free(r);
// 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;
BN_CTX_free(ctx);
}
int main()
{
initBink();
rand();
srand(time(nullptr));
rand();
// Init
BIGNUM *a, *b, *p, *gx, *gy, *pubx, *puby, *n, *priv;
BN_CTX *ctx = BN_CTX_new();
// make BigNumbers
a = BN_new();
b = BN_new();
p = BN_new();
gx = BN_new();
gy = BN_new();
pubx = BN_new();
puby = BN_new();
n = BN_new();
priv = BN_new();
char* BINKID = "2E";
// 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());
/* 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());
/* 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());
// Computed data
/* order of G - computed in 18 hours using a P3-450 */
BN_dec2bn(&n, BINKData[BINKID].n.c_str());
/* THE private key - computed in 10 hours using a P3-450 */
BN_dec2bn(&priv, BINKData[BINKID].k.c_str());
// Calculation
EC_GROUP *ec = EC_GROUP_new_curve_GFp(p, a, b, ctx);
EC_POINT *g = EC_POINT_new(ec);
EC_POINT_set_affine_coordinates_GFp(ec, g, gx, gy, ctx);
EC_POINT *pub = EC_POINT_new(ec);
EC_POINT_set_affine_coordinates_GFp(ec, pub, pubx, puby, ctx);
uint8_t pkey[26];
uint32_t pid[1];
pid[0] = 640 * 1000000 ; /* <- change */
pid[0] += rand() & 999999;
printf("> PID: %d\n", pid[0]);
// generate a key
BN_sub(priv, n, priv);
generate(pkey, ec, g, n, priv, pid);
print_product_key(pkey);
printf("\n\n");
// verify the key
verify(ec, g, pub, (char*)pkey);
// Cleanup
BN_CTX_free(ctx);
return 0;
// Hash chunk of data.
SHA1_Update(&hContext, t, sizeof(t));
// Empty buffer, place r.x in little-endian
memset(buf, 0, FIELD_BYTES);
BN_bn2bin(x, buf);
endian(buf, FIELD_BYTES);
// Hash chunk of data.
SHA1_Update(&hContext, buf, FIELD_BYTES);
// Empty buffer, place r.y in little-endian.
memset(buf, 0, FIELD_BYTES);
BN_bn2bin(y, buf);
endian(buf, FIELD_BYTES);
// Hash chunk of data.
SHA1_Update(&hContext, buf, FIELD_BYTES);
// Store the final message from hContext in md.
SHA1_Final(md, &hContext);
// h = (First-32(SHA1(pRaw, r.x, r.y)) >> 4
hash = (md[0] | (md[1] << 8) | (md[2] << 16) | (md[3] << 24)) >> 4;
hash &= 0xfffffff;
/* s = privateKey * hash + c; */
// s = privateKey;
BN_copy(s, privateKey);
// s *= hash;
BN_mul_word(s, hash);
// BN_mod_add() adds a to b % m and places the non-negative result in r.
// s = |s + c % order|;
BN_mod_add(s, s, c, order, ctx);
// Convert s from BigNum back to bytecode and reverse the endianness.
BN_bn2bin(s, (byte *)sig);
endian((byte *)sig, BN_num_bytes(s));
// Pack product key.
packXP(bKey, pRaw, &hash, sig);
} while (bKey[3] >= 0x40000);
// ↑ ↑ ↑
// bKey[3] can't be longer than 18 bits, else the signature part will make
// the CD-key longer than 25 characters.
// Convert the key to Base24.
base24(pKey, bKey);
BN_free(c);
BN_free(s);
BN_free(x);
BN_free(y);
BN_CTX_free(ctx);
EC_POINT_free(r);
}