2019-08-22 23:33:25 +03:00
|
|
|
/*
|
|
|
|
Windows XP CD Key Verification/Generator v0.03
|
|
|
|
by z22
|
|
|
|
|
|
|
|
Compile with OpenSSL libs, modify to suit your needs.
|
2019-08-22 23:35:39 +03:00
|
|
|
http://gnuwin32.sourceforge.net/packages/openssl.htm
|
2019-08-21 18:29:48 +03:00
|
|
|
|
2019-08-22 23:33:25 +03:00
|
|
|
History:
|
|
|
|
0.03 Stack corruptionerror on exit fixed (now pkey is large enough)
|
|
|
|
More Comments added
|
|
|
|
0.02 Changed name the *.cpp;
|
|
|
|
Fixed minor bugs & Make it compilable on VC++
|
|
|
|
0.01 First version compilable MingW
|
2019-08-21 18:29:48 +03:00
|
|
|
|
|
|
|
|
2019-08-22 23:33:25 +03:00
|
|
|
*/
|
2019-08-21 18:29:48 +03:00
|
|
|
|
2023-05-30 22:37:13 +03:00
|
|
|
#include "shared.h"
|
2023-06-01 04:49:44 +03:00
|
|
|
#include "bink.h"
|
2019-08-21 18:29:48 +03:00
|
|
|
|
2019-08-22 23:33:25 +03:00
|
|
|
#define FIELD_BITS 384
|
2019-08-21 18:29:48 +03:00
|
|
|
#define FIELD_BYTES 48
|
|
|
|
|
|
|
|
|
2023-05-24 06:06:34 +03:00
|
|
|
static void unpack(uint32_t *pid, uint32_t *hash, uint32_t *sig, uint32_t *raw)
|
2019-08-21 18:29:48 +03:00
|
|
|
{
|
2019-08-22 23:33:25 +03:00
|
|
|
// pid = Bit 0..30
|
|
|
|
pid[0] = raw[0] & 0x7fffffff;
|
|
|
|
|
|
|
|
// hash(s) = Bit 31..58
|
|
|
|
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);
|
2019-08-21 18:29:48 +03:00
|
|
|
}
|
|
|
|
|
2023-05-24 06:06:34 +03:00
|
|
|
static void pack(uint32_t *raw, uint32_t *pid, uint32_t *hash, uint32_t *sig)
|
2019-08-21 18:29:48 +03:00
|
|
|
{
|
2019-08-22 23:33:25 +03:00
|
|
|
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;
|
2019-08-21 18:29:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void verify(EC_GROUP *ec, EC_POINT *generator, EC_POINT *public_key, char *cdkey)
|
|
|
|
{
|
2023-05-24 06:06:34 +03:00
|
|
|
uint8_t key[25];
|
2019-08-22 23:33:25 +03:00
|
|
|
int i, j, k;
|
2019-08-21 18:29:48 +03:00
|
|
|
|
2019-08-22 23:33:25 +03:00
|
|
|
BN_CTX *ctx = BN_CTX_new();
|
2019-08-21 18:29:48 +03:00
|
|
|
// remove Dashs from CDKEY
|
2019-08-22 23:33:25 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Base24_CDKEY -> Bin_CDKEY
|
2023-05-24 06:06:34 +03:00
|
|
|
uint32_t bkey[4] = {0};
|
|
|
|
uint32_t pid[1], hash[1], sig[2];
|
2019-08-22 23:33:25 +03:00
|
|
|
unbase24(bkey, key);
|
|
|
|
|
|
|
|
// Output Bin_CDKEY
|
|
|
|
printf("%.8x %.8x %.8x %.8x\n", bkey[3], bkey[2], bkey[1], bkey[0]);
|
|
|
|
|
|
|
|
// Divide/Extract pid_data, hash, sig from Bin_CDKEY
|
|
|
|
unpack(pid, hash, sig, bkey);
|
|
|
|
print_product_id(pid);
|
|
|
|
|
|
|
|
printf("PID: %.8x\nHash: %.8x\nSig: %.8x %.8x\n", pid[0], hash[0], sig[1], sig[0]);
|
|
|
|
|
|
|
|
|
|
|
|
BIGNUM *e, *s;
|
|
|
|
|
|
|
|
/* e = hash, s = sig */
|
|
|
|
e = BN_new();
|
|
|
|
BN_set_word(e, hash[0]);
|
2023-05-24 06:06:34 +03:00
|
|
|
endian((uint8_t *)sig, sizeof(sig));
|
|
|
|
s = BN_bin2bn((uint8_t *)sig, sizeof(sig), NULL);
|
2019-08-22 23:33:25 +03:00
|
|
|
|
|
|
|
BIGNUM *x = BN_new();
|
|
|
|
BIGNUM *y = BN_new();
|
|
|
|
EC_POINT *u = EC_POINT_new(ec);
|
|
|
|
EC_POINT *v = EC_POINT_new(ec);
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2023-05-24 06:06:34 +03:00
|
|
|
uint8_t buf[FIELD_BYTES], md[20];
|
|
|
|
uint32_t h;
|
|
|
|
uint8_t t[4];
|
2019-08-22 23:33:25 +03:00
|
|
|
SHA_CTX h_ctx;
|
|
|
|
|
|
|
|
/* 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));
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
BN_bn2bin(x, buf);
|
2023-05-24 06:06:34 +03:00
|
|
|
endian((uint8_t *)buf, sizeof(buf));
|
2019-08-22 23:33:25 +03:00
|
|
|
SHA1_Update(&h_ctx, buf, sizeof(buf));
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
BN_bn2bin(y, buf);
|
2023-05-24 06:06:34 +03:00
|
|
|
endian((uint8_t *)buf, sizeof(buf));
|
2019-08-22 23:33:25 +03:00
|
|
|
SHA1_Update(&h_ctx, buf, sizeof(buf));
|
|
|
|
|
|
|
|
SHA1_Final(md, &h_ctx);
|
|
|
|
h = (md[0] | (md[1] << 8) | (md[2] << 16) | (md[3] << 24)) >> 4;
|
|
|
|
h &= 0xfffffff;
|
|
|
|
|
|
|
|
printf("Calculated hash: %.8x\n", h);
|
|
|
|
if (h == hash[0]) printf("Key valid\n");
|
|
|
|
else printf("Key invalid\n");
|
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
BN_free(e);
|
|
|
|
BN_free(s);
|
|
|
|
BN_free(x);
|
|
|
|
BN_free(y);
|
|
|
|
EC_POINT_free(u);
|
|
|
|
EC_POINT_free(v);
|
|
|
|
|
|
|
|
BN_CTX_free(ctx);
|
2019-08-21 18:29:48 +03:00
|
|
|
}
|
|
|
|
|
2023-05-24 06:06:34 +03:00
|
|
|
void generate(uint8_t *pkey, EC_GROUP *ec, EC_POINT *generator, BIGNUM *order, BIGNUM *priv, uint32_t *pid)
|
2019-08-21 18:29:48 +03:00
|
|
|
{
|
2019-08-22 23:33:25 +03:00
|
|
|
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);
|
2023-05-24 06:06:34 +03:00
|
|
|
uint32_t bkey[4];
|
2019-08-22 23:33:25 +03:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
SHA_CTX h_ctx;
|
2023-05-24 06:06:34 +03:00
|
|
|
uint8_t t[4], md[20], buf[FIELD_BYTES];
|
|
|
|
uint32_t hash[1];
|
2019-08-22 23:33:25 +03:00
|
|
|
/* 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));
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
BN_bn2bin(x, buf);
|
2023-05-24 06:06:34 +03:00
|
|
|
endian((uint8_t *)buf, sizeof(buf));
|
2019-08-22 23:33:25 +03:00
|
|
|
SHA1_Update(&h_ctx, buf, sizeof(buf));
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
BN_bn2bin(y, buf);
|
2023-05-24 06:06:34 +03:00
|
|
|
endian((uint8_t *)buf, sizeof(buf));
|
2019-08-22 23:33:25 +03:00
|
|
|
SHA1_Update(&h_ctx, buf, sizeof(buf));
|
|
|
|
|
|
|
|
SHA1_Final(md, &h_ctx);
|
|
|
|
hash[0] = (md[0] | (md[1] << 8) | (md[2] << 16) | (md[3] << 24)) >> 4;
|
|
|
|
hash[0] &= 0xfffffff;
|
|
|
|
|
|
|
|
/* s = priv*h + k */
|
|
|
|
BN_copy(s, priv);
|
|
|
|
BN_mul_word(s, hash[0]);
|
|
|
|
BN_mod_add(s, s, k, order, ctx);
|
|
|
|
|
2023-05-24 06:06:34 +03:00
|
|
|
uint32_t sig[2] = {0};
|
|
|
|
BN_bn2bin(s, (uint8_t *)sig);
|
|
|
|
endian((uint8_t *)sig, BN_num_bytes(s));
|
2019-08-22 23:33:25 +03:00
|
|
|
pack(bkey, pid, hash, sig);
|
|
|
|
printf("PID: %.8x\nHash: %.8x\nSig: %.8x %.8x\n", pid[0], hash[0], sig[1], sig[0]);
|
2023-05-28 20:37:46 +03:00
|
|
|
} while (bkey[3] >= 0x40000);
|
2019-08-22 23:33:25 +03:00
|
|
|
|
|
|
|
base24(pkey, bkey);
|
|
|
|
|
|
|
|
BN_free(k);
|
|
|
|
BN_free(s);
|
|
|
|
BN_free(x);
|
|
|
|
BN_free(y);
|
|
|
|
EC_POINT_free(r);
|
|
|
|
|
|
|
|
BN_CTX_free(ctx);
|
2019-08-21 18:29:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2023-06-01 04:49:44 +03:00
|
|
|
initBink();
|
|
|
|
|
|
|
|
rand();
|
|
|
|
srand(time(nullptr));
|
|
|
|
rand();
|
|
|
|
|
2019-08-22 23:33:25 +03:00
|
|
|
// 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();
|
|
|
|
|
2023-06-01 04:49:44 +03:00
|
|
|
char* BINKID = "2E";
|
|
|
|
|
2019-08-22 23:33:25 +03:00
|
|
|
// Data from pidgen-Bink-resources
|
|
|
|
/* Elliptic curve parameters: y^2 = x^3 + ax + b mod p */
|
2023-06-01 04:49:44 +03:00
|
|
|
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());
|
2019-08-22 23:33:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
/* base point (generator) G */
|
2023-06-01 04:49:44 +03:00
|
|
|
BN_dec2bn(&gx, std::get<0>(BINKData[BINKID].G).c_str());
|
|
|
|
BN_dec2bn(&gy, std::get<1>(BINKData[BINKID].G).c_str());
|
2019-08-22 23:33:25 +03:00
|
|
|
|
|
|
|
/* inverse of public key */
|
2023-06-01 04:49:44 +03:00
|
|
|
BN_dec2bn(&pubx, std::get<0>(BINKData[BINKID].K).c_str());
|
|
|
|
BN_dec2bn(&puby, std::get<1>(BINKData[BINKID].K).c_str());
|
2019-08-22 23:33:25 +03:00
|
|
|
|
|
|
|
// Computed data
|
|
|
|
/* order of G - computed in 18 hours using a P3-450 */
|
2023-06-01 04:49:44 +03:00
|
|
|
BN_dec2bn(&n, BINKData[BINKID].n.c_str());
|
2019-08-22 23:33:25 +03:00
|
|
|
|
|
|
|
/* THE private key - computed in 10 hours using a P3-450 */
|
2023-06-01 04:49:44 +03:00
|
|
|
BN_dec2bn(&priv, BINKData[BINKID].k.c_str());
|
2019-08-22 23:33:25 +03:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2023-05-24 06:06:34 +03:00
|
|
|
uint8_t pkey[26];
|
|
|
|
uint32_t pid[1];
|
2023-06-01 04:49:44 +03:00
|
|
|
pid[0] = 640 * 1000000 ; /* <- change */
|
|
|
|
pid[0] += rand() & 999999;
|
|
|
|
|
|
|
|
printf("> PID: %d\n", pid[0]);
|
2019-08-22 23:33:25 +03:00
|
|
|
|
|
|
|
// generate a key
|
2023-06-01 04:49:44 +03:00
|
|
|
BN_sub(priv, n, priv);
|
2019-08-22 23:33:25 +03:00
|
|
|
generate(pkey, ec, g, n, priv, pid);
|
2023-06-01 04:49:44 +03:00
|
|
|
print_product_key(pkey);
|
|
|
|
printf("\n\n");
|
2019-08-22 23:33:25 +03:00
|
|
|
|
|
|
|
// verify the key
|
|
|
|
verify(ec, g, pub, (char*)pkey);
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
|
|
|
|
return 0;
|
2019-08-21 18:29:48 +03:00
|
|
|
}
|