2023-06-16 12:38:10 -07:00
|
|
|
/**
|
2023-06-16 14:58:22 -07:00
|
|
|
* This file is a part of the UMSKT Project
|
2023-06-16 12:38:10 -07:00
|
|
|
*
|
2024-01-04 16:32:18 -08:00
|
|
|
* Copyleft (C) 2019-2024 UMSKT Contributors (et.al.)
|
2023-06-16 12:38:10 -07:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @FileCreated by Andrew on 01/06/2023
|
|
|
|
* @Maintainer Andrew
|
|
|
|
*
|
|
|
|
* @History {
|
|
|
|
* Algorithm was initially written and open sourced by z22
|
|
|
|
* and uploaded to GitHub by TheMCHK in August of 2019
|
|
|
|
*
|
|
|
|
* Endermanch (Andrew) rewrote the algorithm in May of 2023
|
|
|
|
* }
|
|
|
|
*/
|
2023-06-04 22:01:09 +03:00
|
|
|
|
2023-06-07 12:23:59 -07:00
|
|
|
#include "BINK2002.h"
|
2019-08-21 15:26:59 -03:00
|
|
|
|
2024-01-04 16:32:18 -08:00
|
|
|
/**
|
|
|
|
* Unpacks a Windows Server 2003-like Product Key.
|
|
|
|
*
|
|
|
|
* @param pRaw *QWORD[2] raw product key input
|
|
|
|
**/
|
|
|
|
BOOL BINK2002::Unpack(QWORD (&pRaw)[2])
|
2023-12-10 04:06:42 -08:00
|
|
|
{
|
2023-06-02 17:13:57 +03:00
|
|
|
// We're assuming that the quantity of information within the product key is at most 114 bits.
|
|
|
|
// log2(24^25) = 114.
|
|
|
|
|
2023-06-10 20:17:18 +03:00
|
|
|
// Upgrade = Bit 0
|
2024-01-04 16:32:18 -08:00
|
|
|
isUpgrade = FIRSTNBITS(pRaw[0], 1);
|
2023-06-10 20:17:18 +03:00
|
|
|
|
|
|
|
// Channel ID = Bits [1..10] -> 10 bits
|
2024-01-04 16:32:18 -08:00
|
|
|
ChannelID = NEXTSNBITS(pRaw[0], 10, 1);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
|
|
|
// Hash = Bits [11..41] -> 31 bits
|
2024-01-04 16:32:18 -08:00
|
|
|
Hash = NEXTSNBITS(pRaw[0], 31, 11);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
|
|
|
// Signature = Bits [42..103] -> 62 bits
|
2023-06-05 18:25:46 +03:00
|
|
|
// The quad-word signature overlaps AuthInfo in bits 104 and 105,
|
|
|
|
// hence Microsoft employs a secret technique called: Signature = HIDWORD(Signature) >> 2 | LODWORD(Signature)
|
2024-01-04 16:32:18 -08:00
|
|
|
Signature = NEXTSNBITS(pRaw[1], 30, 10) << 32 | FIRSTNBITS(pRaw[1], 10) << 22 | NEXTSNBITS(pRaw[0], 22, 42);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-05 18:25:46 +03:00
|
|
|
// AuthInfo = Bits [104..113] -> 10 bits
|
2024-01-04 16:32:18 -08:00
|
|
|
AuthInfo = NEXTSNBITS(pRaw[1], 10, 40);
|
|
|
|
|
|
|
|
return true;
|
2019-08-21 15:26:59 -03:00
|
|
|
}
|
|
|
|
|
2024-01-04 16:32:18 -08:00
|
|
|
/**
|
|
|
|
* Packs a Windows Server 2003-like Product Key.
|
|
|
|
*
|
|
|
|
* @param pRaw *QWORD[2] raw product key output
|
|
|
|
**/
|
|
|
|
BOOL BINK2002::Pack(QWORD (&pRaw)[2])
|
2023-12-10 04:06:42 -08:00
|
|
|
{
|
2023-06-05 18:25:46 +03:00
|
|
|
// AuthInfo [113..104] <- Signature [103..42] <- Hash [41..11] <- Channel ID [10..1] <- Upgrade [0]
|
2024-01-04 16:32:18 -08:00
|
|
|
pRaw[0] = FIRSTNBITS(Signature, 22) << 42 | (QWORD)Hash << 11 | ChannelID << 1 | isUpgrade;
|
|
|
|
pRaw[1] = FIRSTNBITS(AuthInfo, 10) << 40 | NEXTSNBITS(Signature, 40, 22);
|
|
|
|
|
|
|
|
return true;
|
2019-08-21 15:26:59 -03:00
|
|
|
}
|
|
|
|
|
2024-01-04 16:32:18 -08:00
|
|
|
/**
|
|
|
|
* Verifies a Windows Server 2003-like Product Key.
|
|
|
|
*
|
|
|
|
* @param pKey
|
|
|
|
**/
|
|
|
|
BOOL BINK2002::Verify(std::string &pKey)
|
2023-12-10 04:06:42 -08:00
|
|
|
{
|
2023-06-02 17:13:57 +03:00
|
|
|
BN_CTX *context = BN_CTX_new();
|
2023-06-01 21:25:43 -07:00
|
|
|
|
2024-01-04 16:32:18 -08:00
|
|
|
QWORD bKey[2]{};
|
2023-06-10 20:17:18 +03:00
|
|
|
|
2023-06-07 01:37:30 +03:00
|
|
|
// Convert Base24 CD-key to bytecode.
|
2024-01-04 16:32:18 -08:00
|
|
|
unbase24((BYTE *)bKey, pKey.c_str());
|
2023-06-01 16:09:22 +03:00
|
|
|
|
2023-06-07 01:37:30 +03:00
|
|
|
// Extract product key segments from bytecode.
|
2024-01-04 16:32:18 -08:00
|
|
|
Unpack(bKey);
|
2023-06-10 20:17:18 +03:00
|
|
|
|
2024-01-04 16:32:18 -08:00
|
|
|
DWORD pData = ChannelID << 1 | isUpgrade;
|
2023-06-01 21:25:43 -07:00
|
|
|
|
2023-07-09 00:08:43 -03:00
|
|
|
fmt::print(UMSKT::debug, "Validation results:\n");
|
2024-01-04 16:32:18 -08:00
|
|
|
fmt::print(UMSKT::debug, " Upgrade: 0x{:08x}\n", isUpgrade);
|
|
|
|
fmt::print(UMSKT::debug, "Channel ID: 0x{:08x}\n", ChannelID);
|
|
|
|
fmt::print(UMSKT::debug, " Hash: 0x{:08x}\n", Hash);
|
|
|
|
fmt::print(UMSKT::debug, " Signature: 0x{:08x}\n", Signature);
|
|
|
|
fmt::print(UMSKT::debug, " AuthInfo: 0x{:08x}\n", AuthInfo);
|
2023-07-09 00:08:43 -03:00
|
|
|
fmt::print(UMSKT::debug, "\n");
|
2023-06-05 18:25:46 +03:00
|
|
|
|
2023-12-10 04:06:42 -08:00
|
|
|
BYTE msgDigest[SHA_DIGEST_LENGTH]{}, msgBuffer[SHA_MSG_LENGTH_2003]{}, xBin[FIELD_BYTES_2003]{},
|
|
|
|
yBin[FIELD_BYTES_2003]{};
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 01:37:30 +03:00
|
|
|
// Assemble the first SHA message.
|
2023-06-05 00:52:10 +03:00
|
|
|
msgBuffer[0x00] = 0x5D;
|
2023-06-10 20:17:18 +03:00
|
|
|
msgBuffer[0x01] = (pData & 0x00FF);
|
|
|
|
msgBuffer[0x02] = (pData & 0xFF00) >> 8;
|
2024-01-04 16:32:18 -08:00
|
|
|
msgBuffer[0x03] = (Hash & 0x000000FF);
|
|
|
|
msgBuffer[0x04] = (Hash & 0x0000FF00) >> 8;
|
|
|
|
msgBuffer[0x05] = (Hash & 0x00FF0000) >> 16;
|
|
|
|
msgBuffer[0x06] = (Hash & 0xFF000000) >> 24;
|
|
|
|
msgBuffer[0x07] = (AuthInfo & 0x00FF);
|
|
|
|
msgBuffer[0x08] = (AuthInfo & 0xFF00) >> 8;
|
2023-06-05 00:52:10 +03:00
|
|
|
msgBuffer[0x09] = 0x00;
|
|
|
|
msgBuffer[0x0A] = 0x00;
|
|
|
|
|
2023-06-07 01:37:30 +03:00
|
|
|
// newSignature = SHA1(5D || Channel ID || Hash || AuthInfo || 00 00)
|
2023-06-05 00:52:10 +03:00
|
|
|
SHA1(msgBuffer, 11, msgDigest);
|
|
|
|
|
2023-06-07 01:37:30 +03:00
|
|
|
// Translate the byte digest into a 64-bit integer - this is our computed intermediate signature.
|
2023-12-10 04:06:42 -08:00
|
|
|
// As the signature is only 62 bits long at most, we have to truncate it by shifting the high DWORD right 2 bits
|
|
|
|
// (per spec).
|
2023-06-07 01:37:30 +03:00
|
|
|
QWORD iSignature = NEXTSNBITS(BYDWORD(&msgDigest[4]), 30, 2) << 32 | BYDWORD(msgDigest);
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Scalars:
|
|
|
|
* e = Hash
|
|
|
|
* s = Schnorr Signature
|
|
|
|
*
|
|
|
|
* Points:
|
|
|
|
* G(x, y) = Generator (Base Point)
|
|
|
|
* K(x, y) = Public Key
|
|
|
|
*
|
|
|
|
* Equation:
|
|
|
|
* P = s(sG + eK)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
BIGNUM *e = BN_lebin2bn((BYTE *)&iSignature, sizeof(iSignature), nullptr),
|
2024-01-04 16:32:18 -08:00
|
|
|
*s = BN_lebin2bn((BYTE *)&Signature, sizeof(Signature), nullptr), *x = BN_new(), *y = BN_new();
|
2023-06-07 01:37:30 +03:00
|
|
|
|
|
|
|
// Create 2 points on the elliptic curve.
|
2024-01-04 16:32:18 -08:00
|
|
|
EC_POINT *p = EC_POINT_new(eCurve), *t = EC_POINT_new(eCurve);
|
2023-06-07 01:37:30 +03:00
|
|
|
|
|
|
|
// t = sG
|
|
|
|
EC_POINT_mul(eCurve, t, nullptr, basePoint, s, context);
|
|
|
|
|
|
|
|
// p = eK
|
|
|
|
EC_POINT_mul(eCurve, p, nullptr, publicKey, e, context);
|
|
|
|
|
|
|
|
// p += t
|
|
|
|
EC_POINT_add(eCurve, p, t, p, context);
|
|
|
|
|
|
|
|
// p *= s
|
|
|
|
EC_POINT_mul(eCurve, p, nullptr, p, s, context);
|
|
|
|
|
|
|
|
// x = p.x; y = p.y;
|
|
|
|
EC_POINT_get_affine_coordinates(eCurve, p, x, y, context);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-05 00:27:50 +03:00
|
|
|
// Convert resulting point coordinates to bytes.
|
|
|
|
BN_bn2lebin(x, xBin, FIELD_BYTES_2003);
|
|
|
|
BN_bn2lebin(y, yBin, FIELD_BYTES_2003);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 01:37:30 +03:00
|
|
|
// Assemble the second SHA message.
|
2023-06-05 00:52:10 +03:00
|
|
|
msgBuffer[0x00] = 0x79;
|
2023-06-10 20:17:18 +03:00
|
|
|
msgBuffer[0x01] = (pData & 0x00FF);
|
|
|
|
msgBuffer[0x02] = (pData & 0xFF00) >> 8;
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-05 00:27:50 +03:00
|
|
|
memcpy((void *)&msgBuffer[3], (void *)xBin, FIELD_BYTES_2003);
|
|
|
|
memcpy((void *)&msgBuffer[3 + FIELD_BYTES_2003], (void *)yBin, FIELD_BYTES_2003);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// compHash = SHA1(79 || Channel ID || p.x || p.y)
|
2023-06-05 00:27:50 +03:00
|
|
|
SHA1(msgBuffer, SHA_MSG_LENGTH_2003, msgDigest);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 01:37:30 +03:00
|
|
|
// Translate the byte digest into a 32-bit integer - this is our computed hash.
|
|
|
|
// Truncate the hash to 31 bits.
|
2023-06-05 00:27:50 +03:00
|
|
|
DWORD compHash = BYDWORD(msgDigest) & BITMASK(31);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
|
|
|
BN_free(s);
|
|
|
|
BN_free(e);
|
|
|
|
BN_free(x);
|
|
|
|
BN_free(y);
|
|
|
|
|
|
|
|
BN_CTX_free(context);
|
|
|
|
|
2023-06-07 01:37:30 +03:00
|
|
|
EC_POINT_free(p);
|
|
|
|
EC_POINT_free(t);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 01:37:30 +03:00
|
|
|
// If the computed hash checks out, the key is valid.
|
2024-01-04 16:32:18 -08:00
|
|
|
return compHash == Hash;
|
2019-08-21 15:26:59 -03:00
|
|
|
}
|
|
|
|
|
2023-06-14 15:01:56 +03:00
|
|
|
/* Generates a Windows Server 2003-like Product Key. */
|
2024-01-04 16:32:18 -08:00
|
|
|
BOOL BINK2002::Generate(std::string &pKey)
|
2023-12-10 04:06:42 -08:00
|
|
|
{
|
2023-06-05 16:07:37 +03:00
|
|
|
BN_CTX *numContext = BN_CTX_new();
|
2019-08-21 15:26:59 -03:00
|
|
|
|
2023-12-10 04:06:42 -08:00
|
|
|
BIGNUM *c = BN_new(), *e = BN_new(), *s = BN_new(), *x = BN_new(), *y = BN_new();
|
2023-06-07 02:22:36 +03:00
|
|
|
|
2024-01-04 16:32:18 -08:00
|
|
|
QWORD pRaw[2]{};
|
2023-06-05 16:07:37 +03:00
|
|
|
|
2023-06-10 20:17:18 +03:00
|
|
|
// Data segment of the RPK.
|
2024-01-04 16:32:18 -08:00
|
|
|
DWORD pData = ChannelID << 1 | isUpgrade;
|
2023-06-10 20:17:18 +03:00
|
|
|
|
|
|
|
BOOL noSquare;
|
2023-06-05 15:26:51 +03:00
|
|
|
|
2023-12-10 04:06:42 -08:00
|
|
|
do
|
|
|
|
{
|
2023-06-05 16:07:37 +03:00
|
|
|
EC_POINT *r = EC_POINT_new(eCurve);
|
|
|
|
|
2023-06-02 17:13:57 +03:00
|
|
|
// Generate a random number c consisting of 512 bits without any constraints.
|
|
|
|
BN_rand(c, FIELD_BITS_2003, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY);
|
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// R = cG
|
2023-06-05 16:07:37 +03:00
|
|
|
EC_POINT_mul(eCurve, r, nullptr, basePoint, c, numContext);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// Acquire its coordinates.
|
|
|
|
// x = R.x; y = R.y;
|
2023-06-05 16:07:37 +03:00
|
|
|
EC_POINT_get_affine_coordinates(eCurve, r, x, y, numContext);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-12-10 04:06:42 -08:00
|
|
|
BYTE msgDigest[SHA_DIGEST_LENGTH]{}, msgBuffer[SHA_MSG_LENGTH_2003]{}, xBin[FIELD_BYTES_2003]{},
|
|
|
|
yBin[FIELD_BYTES_2003]{};
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-05 15:26:51 +03:00
|
|
|
// Convert resulting point coordinates to bytes.
|
|
|
|
BN_bn2lebin(x, xBin, FIELD_BYTES_2003);
|
|
|
|
BN_bn2lebin(y, yBin, FIELD_BYTES_2003);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// Assemble the first SHA message.
|
2023-06-05 15:26:51 +03:00
|
|
|
msgBuffer[0x00] = 0x79;
|
2023-06-10 20:17:18 +03:00
|
|
|
msgBuffer[0x01] = (pData & 0x00FF);
|
|
|
|
msgBuffer[0x02] = (pData & 0xFF00) >> 8;
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-05 15:26:51 +03:00
|
|
|
memcpy((void *)&msgBuffer[3], (void *)xBin, FIELD_BYTES_2003);
|
|
|
|
memcpy((void *)&msgBuffer[3 + FIELD_BYTES_2003], (void *)yBin, FIELD_BYTES_2003);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// pHash = SHA1(79 || Channel ID || R.x || R.y)
|
2023-06-05 15:26:51 +03:00
|
|
|
SHA1(msgBuffer, SHA_MSG_LENGTH_2003, msgDigest);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// Translate the byte digest into a 32-bit integer - this is our computed hash.
|
|
|
|
// Truncate the hash to 31 bits.
|
|
|
|
DWORD pHash = BYDWORD(msgDigest) & BITMASK(31);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// Assemble the second SHA message.
|
2023-06-05 15:26:51 +03:00
|
|
|
msgBuffer[0x00] = 0x5D;
|
2023-06-10 20:17:18 +03:00
|
|
|
msgBuffer[0x01] = (pData & 0x00FF);
|
|
|
|
msgBuffer[0x02] = (pData & 0xFF00) >> 8;
|
2023-06-07 02:22:36 +03:00
|
|
|
msgBuffer[0x03] = (pHash & 0x000000FF);
|
|
|
|
msgBuffer[0x04] = (pHash & 0x0000FF00) >> 8;
|
|
|
|
msgBuffer[0x05] = (pHash & 0x00FF0000) >> 16;
|
|
|
|
msgBuffer[0x06] = (pHash & 0xFF000000) >> 24;
|
2024-01-04 16:32:18 -08:00
|
|
|
msgBuffer[0x07] = (AuthInfo & 0x00FF);
|
|
|
|
msgBuffer[0x08] = (AuthInfo & 0xFF00) >> 8;
|
2023-06-05 15:26:51 +03:00
|
|
|
msgBuffer[0x09] = 0x00;
|
|
|
|
msgBuffer[0x0A] = 0x00;
|
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// newSignature = SHA1(5D || Channel ID || Hash || AuthInfo || 00 00)
|
2023-06-05 15:26:51 +03:00
|
|
|
SHA1(msgBuffer, 11, msgDigest);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// Translate the byte digest into a 64-bit integer - this is our computed intermediate signature.
|
2023-12-10 04:06:42 -08:00
|
|
|
// As the signature is only 62 bits long at most, we have to truncate it by shifting the high DWORD right 2
|
|
|
|
// bits (per spec).
|
2023-06-07 02:22:36 +03:00
|
|
|
QWORD iSignature = NEXTSNBITS(BYDWORD(&msgDigest[4]), 30, 2) << 32 | BYDWORD(msgDigest);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
BN_lebin2bn((BYTE *)&iSignature, sizeof(iSignature), e);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
2023-06-07 12:27:45 +03:00
|
|
|
* Scalars:
|
|
|
|
* c = Random multiplier
|
|
|
|
* e = Intermediate Signature
|
|
|
|
* s = Signature
|
|
|
|
* n = Order of G
|
|
|
|
* k = Private Key
|
2023-06-02 17:13:57 +03:00
|
|
|
*
|
2023-06-07 12:27:45 +03:00
|
|
|
* Points:
|
|
|
|
* G(x, y) = Generator (Base Point)
|
|
|
|
* R(x, y) = Random derivative of the generator
|
|
|
|
* K(x, y) = Public Key
|
2023-06-02 17:13:57 +03:00
|
|
|
*
|
2023-06-07 12:27:45 +03:00
|
|
|
* Equation:
|
|
|
|
* s(sG + eK) = R (mod p)
|
|
|
|
* ↓ K = kG; R = cG ↓
|
2023-06-02 17:13:57 +03:00
|
|
|
*
|
2023-06-07 12:27:45 +03:00
|
|
|
* s(sG + ekG) = cG (mod p)
|
|
|
|
* s(s + ek)G = cG (mod p)
|
|
|
|
* ↓ G cancels out, the scalar arithmetic shrinks to order n ↓
|
2023-06-02 17:13:57 +03:00
|
|
|
*
|
2023-06-07 12:27:45 +03:00
|
|
|
* s(s + ek) = c (mod n)
|
|
|
|
* s² + (ek)s - c = 0 (mod n)
|
|
|
|
* ↓ This is a quadratic equation in respect to the signature ↓
|
|
|
|
*
|
2023-06-14 15:00:56 +03:00
|
|
|
* s = (-ek ± √((ek)² + 4c)) / 2 (mod n)
|
2023-06-02 17:13:57 +03:00
|
|
|
*/
|
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// e = ek (mod n)
|
2023-06-05 16:07:37 +03:00
|
|
|
BN_mod_mul(e, e, privateKey, genOrder, numContext);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-05 16:07:37 +03:00
|
|
|
// s = e
|
|
|
|
BN_copy(s, e);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 12:27:45 +03:00
|
|
|
// s = (ek (mod n))²
|
2023-06-05 16:07:37 +03:00
|
|
|
BN_mod_sqr(s, s, genOrder, numContext);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 12:27:45 +03:00
|
|
|
// c *= 4 (c <<= 2)
|
2023-06-02 17:13:57 +03:00
|
|
|
BN_lshift(c, c, 2);
|
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// s += c
|
2023-06-02 17:13:57 +03:00
|
|
|
BN_add(s, s, c);
|
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// Around half of numbers modulo a prime are not squares -> BN_sqrt_mod fails about half of the times,
|
|
|
|
// hence if BN_sqrt_mod returns NULL, we need to restart with a different seed.
|
2023-06-07 12:27:45 +03:00
|
|
|
// s = √((ek)² + 4c (mod n))
|
2023-06-07 02:34:01 +03:00
|
|
|
noSquare = BN_mod_sqrt(s, s, genOrder, numContext) == nullptr;
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 12:27:45 +03:00
|
|
|
// s = -ek + √((ek)² + 4c) (mod n)
|
2023-06-05 16:07:37 +03:00
|
|
|
BN_mod_sub(s, s, e, genOrder, numContext);
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// If s is odd, add order to it.
|
2023-06-07 12:27:45 +03:00
|
|
|
// The order is a prime, so it can't be even.
|
2023-06-07 02:22:36 +03:00
|
|
|
if (BN_is_odd(s))
|
2023-12-10 04:06:42 -08:00
|
|
|
{
|
2023-06-07 12:27:45 +03:00
|
|
|
|
|
|
|
// s = -ek + √((ek)² + 4c) + n
|
2023-06-05 12:13:15 +03:00
|
|
|
BN_add(s, s, genOrder);
|
2023-12-10 04:06:42 -08:00
|
|
|
}
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 12:27:45 +03:00
|
|
|
// s /= 2 (s >>= 1)
|
2023-06-02 17:13:57 +03:00
|
|
|
BN_rshift1(s, s);
|
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// Translate resulting scalar into a 64-bit integer (the byte order is little-endian).
|
2024-01-04 16:32:18 -08:00
|
|
|
BN_bn2lebinpad(s, (BYTE *)&Signature, BN_num_bytes(s));
|
2023-06-02 17:13:57 +03:00
|
|
|
|
|
|
|
// Pack product key.
|
2024-01-04 16:32:18 -08:00
|
|
|
Pack(pRaw);
|
2023-06-05 16:07:37 +03:00
|
|
|
|
2023-07-09 00:08:43 -03:00
|
|
|
fmt::print(UMSKT::debug, "Generation results:\n");
|
2024-01-04 16:32:18 -08:00
|
|
|
fmt::print(UMSKT::debug, " Upgrade: 0x{:08x}\n", isUpgrade);
|
|
|
|
fmt::print(UMSKT::debug, "Channel ID: 0x{:08x}\n", ChannelID);
|
|
|
|
fmt::print(UMSKT::debug, " Hash: 0x{:08x}\n", Hash);
|
|
|
|
fmt::print(UMSKT::debug, " Signature: 0x{:08x}\n", Signature);
|
|
|
|
fmt::print(UMSKT::debug, " AuthInfo: 0x{:08x}\n", AuthInfo);
|
2023-07-09 00:08:43 -03:00
|
|
|
fmt::print(UMSKT::debug, "\n");
|
2023-06-05 18:25:46 +03:00
|
|
|
|
2023-06-05 16:07:37 +03:00
|
|
|
EC_POINT_free(r);
|
2024-01-04 16:32:18 -08:00
|
|
|
} while (Signature > BITMASK(62) || noSquare);
|
2023-06-07 02:22:36 +03:00
|
|
|
// ↑ ↑ ↑
|
|
|
|
// The signature can't be longer than 62 bits, else it will
|
|
|
|
// overlap with the AuthInfo segment next to it.
|
2023-06-02 17:13:57 +03:00
|
|
|
|
2023-06-07 02:22:36 +03:00
|
|
|
// Convert bytecode to Base24 CD-key.
|
2023-06-06 18:04:39 -04:00
|
|
|
base24(pKey, (BYTE *)pRaw);
|
2023-06-05 12:29:54 +03:00
|
|
|
|
2023-06-05 15:26:51 +03:00
|
|
|
BN_free(c);
|
|
|
|
BN_free(s);
|
|
|
|
BN_free(x);
|
|
|
|
BN_free(y);
|
2023-06-05 16:07:37 +03:00
|
|
|
BN_free(e);
|
2023-06-05 15:26:51 +03:00
|
|
|
|
2023-06-05 16:07:37 +03:00
|
|
|
BN_CTX_free(numContext);
|
2024-01-04 16:32:18 -08:00
|
|
|
|
|
|
|
return true;
|
2023-07-09 00:08:43 -03:00
|
|
|
}
|