1
0
mirror of synced 2024-11-24 05:50:11 +01:00

Push nbgi

This commit is contained in:
Bottersnike 2023-08-30 02:08:59 +01:00
parent cae05bde85
commit 05fcc7f4f8
No known key found for this signature in database
5 changed files with 498 additions and 0 deletions

View File

@ -0,0 +1,387 @@
#include "bnaccardall.h"
#include <memory.h>
#include <stdlib.h>
#include "../crypto/crypto.h"
#include "uint64.h"
/**
* Blowfish function omitted, and substituted with blowfish.c
*
* Function name mappings:
* _bf_dec_block -> BlowfishDecrypt
* _bf_f -> _BlowfishF
* _bf_init -> BlowfishSetKey
* _bf_enc_block -> BlowfishEncrypt
*/
UINT16 BNACCardVersion = 0x200;
BLOWFISH_KEY s_DataKeyNBGIC[8] = {
{
.m_Name = {"NBGIC0"},
.m_Key =
{
0x6B, 0xF9, 0xE0, 0x22, 0x4B, 0x14, 0xA9, 0x51, 0xD4, 0x3D, 0xB6, 0x2D, 0x2A, 0x99,
0x47, 0xD8, 0xB2, 0x78, 0x80, 0x35, 0x8E, 0x71, 0x16, 0xEB, 0xA0, 0x00, 0x01, 0x3E,
0x8C, 0xFE, 0xF6, 0x09, 0x02, 0xA9, 0x59, 0x5D, 0x4C, 0x25, 0xFE, 0xFF, 0x74, 0x3F,
0xC8, 0xD8, 0xE0, 0x75, 0x6A, 0xE6, 0x18, 0xEF, 0x8B, 0x72, 0x12, 0xDE, 0x5A, 0x4B,
},
.Unk40 =
{
0x97, 0xBA, 0xA5, 0x1B, 0x46, 0x04, 0x87, 0x43, 0x9F, 0x21, 0x5B, 0xE9,
0xE1, 0xD0, 0x98, 0x72, 0x27, 0x91, 0x31, 0xBA, 0x8A, 0x82, 0x8F, 0xAB,
0x50, 0x7F, 0x44, 0x7F, 0x8B, 0xC5, 0xE3, 0x4A, 0xC3, 0x32, 0xB7, 0xCB,
0xD8, 0x4F, 0xB1, 0x4C, 0x45, 0x21, 0x20, 0xE2, 0xA0, 0x71, 0x38, 0x4D,
0xE6, 0x95, 0x89, 0xC1, 0x5B, 0x04, 0x14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, 0xEC, 0xD2, 0x15, 0xA5, 0xAA, 0xBF, 0x16,
0xBE, 0x44, 0xF7, 0xB4, 0xD5, 0x7A, 0xF6, 0xC2, 0x3B, 0xE3, 0x19, 0x3D,
0xAB, 0x75, 0xC2, 0xF0, 0x0A, 0x18, 0x46, 0x62, 0xBC, 0xEC, 0x5E, 0xF3,
0xF3, 0xB1, 0x84, 0x68, 0x81, 0x8A, 0x8A, 0x1A, 0x7C, 0x91, 0xED, 0x5D,
0xEA, 0xE5, 0x1E, 0xE7, 0x2C, 0x22, 0x60, 0xDF, 0xFB, 0x52, 0x15, 0x0B,
},
},
};
void SetBitRange(LPVOID lpBuffer, int nStart, size_t nBits, int dwData) {
for (size_t i = 0; i < nBits; i++) {
unsigned int newByte =
(1 << (7 - (nStart + i) % 8)) | ((LPBYTE)lpBuffer)[((nStart + i) >> 3) + 1];
if ((dwData & (1 << (nBits - 1 - i))) == 0) newByte = ~newByte;
((LPBYTE)lpBuffer)[((nStart + i) >> 3) + 1] = newByte & 0xff;
}
}
void SetRandNumData(LPVOID lpBuffer, DWORD dwRandNum) { SetBitRange(lpBuffer, 0, 2, dwRandNum); }
void SetProductData(LPVOID lpBuffer, DWORD dwProduct) { SetBitRange(lpBuffer, 2, 10, dwProduct); }
void SetAppData(LPVOID lpBuffer, DWORD dwAppData) { SetBitRange(lpBuffer, 12, 4, dwAppData); }
void SetNamcoIDData(LPVOID lpBuffer, DWORD dwNamcoId) { SetBitRange(lpBuffer, 16, 32, dwNamcoId); }
void SetBCCData(LPVOID lpBuffer, DWORD dwBcc) { SetBitRange(lpBuffer, 48, 8, dwBcc); }
void LShift(LPBYTE lpBuffer) {
for (int i = 0; i < 8; i++) {
lpBuffer[i] <<= 1;
if (i != 7 && lpBuffer[i + 1] & 0x80) lpBuffer[i] |= 1;
}
}
int NbgiGetAccessCode(char *AccessCode, const void *HeaderData, int *NbgiSerial) {
char strAC[32];
struc_1 tCard;
if (BNACCardDecode(&tCard, HeaderData)) return 0;
if (BNACCardEncodeAccessCode(strAC, tCard.m_KeyNum, tCard.m_Serial)) return 0;
strncpy_s(AccessCode, 20, strAC, _TRUNCATE);
AccessCode[20] = 0;
if (NbgiSerial != NULL) *NbgiSerial = tCard.m_Serial;
return 1;
}
int BNACCardDecode(struc_1 *Decoded, const void *HeaderData) {
if (!Decoded || !HeaderData) return -1;
BYTE head[16];
memcpy(head, HeaderData, sizeof head);
PBLOWFISH_KEY Buf2 = s_DataKeyNBGIC;
UINT8 keynum;
for (keynum = 0; keynum < 8; keynum++)
if (memcmp(&head[2], s_DataKeyNBGIC[keynum].m_Name, 6) == 0) break;
if (keynum == _countof(s_DataKeyNBGIC)) return -1;
if (*((LPWORD)&head[0]) != BNACCardVersion) return -2;
BNACCardBFDec(s_DataKeyNBGIC[keynum].m_Key, 8, &head[8]);
BYTE chk = 0;
for (int j = 0; j < 7; ++j) chk ^= head[j + 8];
if (chk != head[15]) return -2;
Decoded->m_Serial = *((LPDWORD)&head[8]);
Decoded->Unk04 = *((LPWORD)&head[12]);
Decoded->Unk06 = head[14];
Decoded->m_KeyNum = keynum;
return 0;
}
void BNACCardBFDec(LPBYTE lpKey, size_t nBytes, LPBYTE lpData) {
BlowfishSetKey(lpKey, 448);
for (size_t i = 0; i < (nBytes / 8); ++i) BlowfishDecrypt((LPDWORD)&lpData[i * 8], (LPDWORD)&lpData[i * 8 + 4]);
}
int BNACCardEncodeAccessCode(char *AccessCode, BYTE KeyNum, UINT32 NamcoID) {
AC_KEY s_IDKey[8] = {
{
.m_dwPrefix = 300,
.m_szPrefix = {"300"},
.m_Key =
{
0x0f, 0x11, 0x33, 0x28, 0x03, 0x14, 0x2f, 0x1d, 0x10, 0x2a, 0x19, 0x01,
0x2b, 0x1f, 0x1e, 0x04, 0x13, 0x29, 0x15, 0x2e, 0x36, 0x0d, 0x31, 0x09,
0x2d, 0x27, 0x1a, 0x06, 0x16, 0x24, 0x17, 0x23, 0x00, 0x0c, 0x26, 0x22,
0x37, 0x32, 0x0a, 0x30, 0x07, 0x1c, 0x25, 0x21, 0x05, 0x34, 0x0e, 0x08,
0x1b, 0x35, 0x2c, 0x0b, 0x02, 0x20, 0x12, 0x18,
},
.m_NumKey = 0xB2456B04BA6B2300,
},
{
.m_dwPrefix = 302,
.m_szPrefix = {"302"},
.m_Key =
{
0x2d, 0x19, 0x0c, 0x02, 0x24, 0x0b, 0x22, 0x29, 0x25, 0x35, 0x12, 0x09,
0x36, 0x01, 0x33, 0x08, 0x2b, 0x0e, 0x0a, 0x2f, 0x26, 0x14, 0x30, 0x00,
0x1f, 0x37, 0x32, 0x27, 0x1a, 0x2c, 0x20, 0x18, 0x28, 0x05, 0x21, 0x13,
0x1b, 0x06, 0x0d, 0x23, 0x11, 0x10, 0x1e, 0x34, 0x0f, 0x1c, 0x04, 0x2e,
0x16, 0x31, 0x1d, 0x03, 0x2a, 0x17, 0x07, 0x15,
},
.m_NumKey = 0x9209138C279A5F00,
},
{
.m_dwPrefix = 303,
.m_szPrefix = {"303"},
.m_Key =
{
0x09, 0x1c, 0x2b, 0x27, 0x04, 0x17, 0x35, 0x0b, 0x11, 0x37, 0x18, 0x13,
0x0d, 0x1d, 0x24, 0x31, 0x0c, 0x07, 0x2d, 0x29, 0x00, 0x22, 0x03, 0x16,
0x28, 0x2e, 0x20, 0x34, 0x0a, 0x10, 0x2f, 0x2a, 0x1b, 0x1f, 0x2c, 0x02,
0x23, 0x30, 0x08, 0x33, 0x36, 0x26, 0x19, 0x21, 0x05, 0x14, 0x1a, 0x06,
0x25, 0x32, 0x1e, 0x12, 0x15, 0x0e, 0x0f, 0x01,
},
.m_NumKey = 0xF35646010DB85900,
},
{
.m_dwPrefix = 304,
.m_szPrefix = {"304"},
.m_Key =
{
0x0a, 0x16, 0x0c, 0x01, 0x17, 0x1a, 0x13, 0x30, 0x05, 0x29, 0x1e, 0x0d,
0x2d, 0x32, 0x0b, 0x2b, 0x02, 0x00, 0x19, 0x23, 0x20, 0x14, 0x1c, 0x27,
0x15, 0x25, 0x0f, 0x2a, 0x35, 0x12, 0x34, 0x26, 0x04, 0x08, 0x28, 0x1f,
0x36, 0x2e, 0x06, 0x07, 0x11, 0x31, 0x21, 0x09, 0x24, 0x1d, 0x37, 0x2f,
0x2c, 0x33, 0x0e, 0x03, 0x1b, 0x18, 0x22, 0x10,
},
.m_NumKey = 0xB5F62682EEE1300,
},
{
.m_dwPrefix = 305,
.m_szPrefix = {"305"},
.m_Key =
{
0x36, 0x0f, 0x06, 0x01, 0x22, 0x2b, 0x35, 0x30, 0x26, 0x14, 0x16, 0x2a,
0x1f, 0x1e, 0x27, 0x07, 0x2f, 0x2d, 0x33, 0x13, 0x0e, 0x18, 0x1a, 0x08,
0x28, 0x20, 0x25, 0x0a, 0x10, 0x1b, 0x1d, 0x17, 0x1c, 0x23, 0x11, 0x29,
0x34, 0x12, 0x2e, 0x09, 0x05, 0x19, 0x0c, 0x03, 0x2c, 0x37, 0x0d, 0x04,
0x15, 0x21, 0x31, 0x0b, 0x02, 0x00, 0x32, 0x24,
},
.m_NumKey = 0x88895A4A4EBD5E00,
},
{
.m_dwPrefix = 306,
.m_szPrefix = {"306"},
.m_Key =
{
0x0f, 0x02, 0x17, 0x21, 0x0c, 0x0d, 0x1f, 0x08, 0x07, 0x00, 0x1a, 0x11,
0x18, 0x26, 0x01, 0x1d, 0x22, 0x13, 0x28, 0x15, 0x34, 0x23, 0x33, 0x35,
0x24, 0x0e, 0x14, 0x30, 0x06, 0x29, 0x04, 0x12, 0x31, 0x1b, 0x16, 0x25,
0x2a, 0x20, 0x2b, 0x27, 0x0b, 0x10, 0x32, 0x2f, 0x1e, 0x36, 0x37, 0x03,
0x09, 0x1c, 0x19, 0x2c, 0x2e, 0x2d, 0x05, 0x0a,
},
.m_NumKey = 0xC864F27239FD5000,
},
{
.m_dwPrefix = 307,
.m_szPrefix = {"307"},
.m_Key =
{
0x32, 0x03, 0x30, 0x37, 0x16, 0x08, 0x29, 0x09, 0x05, 0x19, 0x13, 0x2e,
0x1d, 0x18, 0x23, 0x2b, 0x15, 0x2a, 0x17, 0x0f, 0x0b, 0x06, 0x26, 0x14,
0x1e, 0x21, 0x04, 0x0d, 0x00, 0x02, 0x0c, 0x25, 0x34, 0x28, 0x07, 0x1c,
0x1a, 0x2d, 0x2c, 0x0e, 0x24, 0x31, 0x1b, 0x10, 0x11, 0x2f, 0x27, 0x0a,
0x01, 0x1f, 0x20, 0x33, 0x36, 0x12, 0x35, 0x22,
},
.m_NumKey = 0x5B0D3FEA400D5700,
},
{
.m_dwPrefix = 308,
.m_szPrefix = {"308"},
.m_Key =
{
0x16, 0x1f, 0x2f, 0x17, 0x1c, 0x11, 0x28, 0x31, 0x12, 0x05, 0x02, 0x07,
0x0b, 0x27, 0x35, 0x2e, 0x1e, 0x0e, 0x25, 0x26, 0x15, 0x34, 0x2a, 0x1a,
0x24, 0x30, 0x04, 0x13, 0x2b, 0x33, 0x00, 0x36, 0x2d, 0x1d, 0x37, 0x01,
0x18, 0x0f, 0x0a, 0x0c, 0x21, 0x20, 0x14, 0x23, 0x10, 0x32, 0x09, 0x22,
0x19, 0x0d, 0x2c, 0x06, 0x03, 0x1b, 0x29, 0x08,
},
.m_NumKey = 0xD5797478D1B15800,
},
};
if (!AccessCode) return -1;
if (KeyNum >= 8) return -1;
DWORD prod = s_IDKey[KeyNum].m_dwPrefix;
DWORD appdata = 0;
UINT64 numkey;
memcpy(&numkey, &s_IDKey[KeyNum].m_NumKey, sizeof numkey);
BYTE bcc = 0;
for (size_t i = 0; i < 4; ++i) {
bcc ^= ((LPBYTE)&prod)[i];
bcc ^= ((LPBYTE)&appdata)[i];
bcc ^= ((LPBYTE)&NamcoID)[i];
}
prod = ((prod & 3) << 8) | (BYTE)(prod >> 2);
BYTE data[8];
data[0] = 0;
SetRandNumData(data, (bcc % 11) & 3);
SetProductData(data, prod);
SetAppData(data, appdata);
SetNamcoIDData(data, _byteswap_ulong(NamcoID));
SetBCCData(data, bcc);
BYTE temp[8];
ZeroMemory(temp, sizeof temp);
for (size_t i = 0; i < KEYLEN; ++i) {
if (((1 << (7 - i % 8)) & (BYTE)data[(i >> 3) + 1]) != 0)
temp[s_IDKey[KeyNum].m_Key[i] % KEYLEN / 8 + 1] =
(1 << (7 - s_IDKey[KeyNum].m_Key[i] % KEYLEN % 8)) |
temp[((s_IDKey[KeyNum].m_Key[i] % KEYLEN) >> 3) + 1];
}
UINT64 res = IDDataToUINT64(temp);
res = UINT64Add(&res, &numkey);
UINT64ToA(AccessCode + 3, &res);
memcpy(AccessCode, s_IDKey[KeyNum].m_szPrefix, 3);
return 0;
}

View File

@ -0,0 +1,40 @@
#include <Windows.h>
/* Structs */
#pragma pack(push, 1)
typedef struct struc_1 {
UINT32 m_Serial;
UINT16 Unk04;
UINT8 Unk06;
UINT8 m_KeyNum;
} struc_1;
#define KEYLEN 56
typedef struct BLOWFISH_KEY {
char m_Name[8];
BYTE m_Key[KEYLEN];
BYTE Unk40[72];
} BLOWFISH_KEY, *PBLOWFISH_KEY;
typedef struct AC_KEY {
DWORD m_dwPrefix;
CHAR m_szPrefix[4];
BYTE m_Key[KEYLEN];
UINT64 m_NumKey;
} AC_KEY, *PAC_KEY;
#pragma pack(pop)
/* BNA functions */
int BNACCardDecode(struc_1 *Decoded, const void *HeaderData);
int BNACCardEncodeAccessCode(char *AccessCode, BYTE KeyNum, UINT32 NamcoID);
/* Exports */
int NbgiGetAccessCode(char *AccessCode, const void *HeaderData, int *NbgiSerial);
void BNACCardBFDec(LPBYTE lpKey, size_t nBytes, LPBYTE lpData);
/* TODO: (felica stuff) */
// int NbgiFelicaLiteAuth(LPVOID IDmPMm, LPVOID RC, LPVOID InData, LPVOID Mac, LPVOID MKey);
// void MakeCardKey(LPVOID IDmPMm, LPVOID lpMac);
// void CMAC(LPVOID dataIn, byte *xorKey, LPVOID macOut);
// void MakeSessionKey(byte *mac, byte *param_2, LPVOID param_3);
// void MakeMac(int param_1, byte *param_2, byte *param_3, byte *param_4);

View File

@ -0,0 +1,10 @@
nbgi_lib = static_library(
'nbgi',
sources: [
'bnaccardall.c',
'uint64.c',
],
link_with: [
crypto_lib,
],
)

View File

@ -0,0 +1,52 @@
#include "uint64.h"
bool UINT64IsZero(UINT64 *pU64) {
int sum = 0;
for (int i = 0; i < 8; i++) sum += ((LPBYTE)pU64)[i];
return sum == 0;
}
UINT64 UINT64Add(UINT64 *pU64_1, UINT64 *pU16_2) {
UINT64 res = 0;
unsigned int carry = 0;
for (int i = 7; i >= 0; --i) {
carry += ((LPBYTE)pU64_1)[i] + ((LPBYTE)pU16_2)[i];
((LPBYTE)res)[i] = carry;
carry >>= 8;
}
return res;
}
UINT32 UINT64ModUInt32(UINT64 *pU64, UINT32 u32) {
UINT32 res = 0;
for (int i = 0; i < 8; i++) res = ((res << 8) + ((LPBYTE)pU64)[i]) % u32;
return res;
}
UINT64 UINT64DivideUInt32(UINT64 *pU64, UINT32 u32) {
UINT64 res = 0;
unsigned int carry = 0;
for (int i = 0; i < 8; i++) {
((LPBYTE)&res)[i] = ((carry << 8) + ((LPBYTE)pU64)[i]) / u32;
carry = ((carry << 8) + ((LPBYTE)pU64)[i]) % u32;
}
return res;
}
void UINT64ToA(char *lpStr, UINT64 *pU64) {
char buf[64];
int i = 0;
char *tail = buf;
UINT64 tmp;
memcpy(&tmp, pU64, sizeof tmp);
while (!UINT64IsZero(&tmp)) {
*tail++ = UINT64ModUInt32(&tmp, 10) + '0';
tmp = UINT64DivideUInt32(&tmp, 10);
i++;
}
for (; i < 17; i++) *lpStr++ = '0';
while (tail != buf) *lpStr++ = *--tail;
}
UINT64 IDDataToUINT64(LPBYTE lpIdData) {
UINT64 res;
memcpy(&res, lpIdData, sizeof res);
return res;
}

View File

@ -0,0 +1,9 @@
#include <Windows.h>
#include <stdbool.h>
bool UINT64IsZero(UINT64 *pU64);
UINT64 UINT64Add(UINT64 *pU64_1, UINT64 *pU16_2);
UINT32 UINT64ModUInt32(UINT64 *pU64, UINT32 u32);
UINT64 UINT64DivideUInt32(UINT64 *pU64, UINT32 u32);
void UINT64ToA(char *lpStr, UINT64 *pU64);
UINT64 IDDataToUINT64(LPBYTE lpIdData);