Skip to content

Commit

Permalink
Fixed key transformation condition
Browse files Browse the repository at this point in the history
  • Loading branch information
hozuki committed Apr 24, 2019
1 parent c7eb542 commit 4871906
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/lib/kawashima/hca/CHcaCipherConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CGSS_NS_BEGIN
}

CHcaCipherConfig::CHcaCipherConfig(uint64_t key)
: MyClass((uint32_t)(key >> 32), (uint32_t)(key & 0xffffffff)) {
: MyClass((uint32_t)(key >> 32u), (uint32_t)(key & 0xffffffff)) {
}

CHcaCipherConfig::CHcaCipherConfig(uint32_t key1, uint32_t key2, uint16_t keyModifier)
Expand All @@ -31,7 +31,7 @@ CGSS_NS_BEGIN
}

CHcaCipherConfig::CHcaCipherConfig(uint64_t key, uint16_t keyModifier)
: MyClass((uint32_t)(key >> 32), (uint32_t)(key & 0xffffffff), keyModifier) {
: MyClass((uint32_t)(key >> 32u), (uint32_t)(key & 0xffffffff), keyModifier) {
}

void CHcaCipherConfig::Initialize(uint32_t key1, uint32_t key2, uint16_t keyModifier) {
Expand Down
12 changes: 6 additions & 6 deletions src/lib/kawashima/hca/CHcaDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ CGSS_NS_BEGIN
if (hcaInfo.commentLength > 0) {
uint32_t noteSize = 4 + hcaInfo.commentLength + 1;
// Pad by 4
if (noteSize & 3) {
noteSize += 4 - (noteSize & 3);
if (noteSize & 3u) {
noteSize += 4 - (noteSize & 3u);
}
sizeNeeded += 8 + noteSize;
}
Expand Down Expand Up @@ -185,8 +185,8 @@ CGSS_NS_BEGIN
}
if (hcaInfo.commentLength > 0) {
wavNote.noteSize = 4 + hcaInfo.commentLength + 1;
if (wavNote.noteSize & 3) {
wavNote.noteSize += 4 - (wavNote.noteSize & 3);
if (wavNote.noteSize & 3u) {
wavNote.noteSize += 4 - (wavNote.noteSize & 3u);
}
}
wavData.dataSize = wavRiff.fmtSamplingSize * (hcaInfo.blockCount * 0x80 * 8 +
Expand Down Expand Up @@ -260,7 +260,7 @@ CGSS_NS_BEGIN
}

// Actual decoding process.
auto a = (data.GetBit(9) << 8) - data.GetBit(7);
auto a = (data.GetBit(9) << 8u) - data.GetBit(7);
for (auto i = 0; i < hcaInfo.channelCount; ++i) {
CHcaChannel::Decode1(channels[i], &data, hcaInfo.compR09, a, _ath->GetTable());
}
Expand Down Expand Up @@ -361,7 +361,7 @@ CGSS_NS_BEGIN
if (!buffer) {
throw CArgumentException("CHcaDecoder::Read");
}
bufferSize = std::max(0u, std::min(count, static_cast<uint32_t>(bufferSize - offset)));
bufferSize = std::min(count, static_cast<uint32_t>(bufferSize - offset));
if (bufferSize == 0) {
return bufferSize;
}
Expand Down
34 changes: 17 additions & 17 deletions src/lib/kawashima/hca/internal/CHcaCipher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#include "../../../cgss_cdata.h"

static void TransformKey(uint32_t key1, uint32_t key2, uint16_t mod, uint32_t *pk1, uint32_t *pk2) {
auto key = (uint64_t)key2 << 32 | key1;
auto k2 = ((uint64_t)mod << 16 | (uint16_t)(~mod + 2));
auto key = (uint64_t)key2 << 32u | key1;
auto k2 = ((uint64_t)mod << 16u | (uint16_t)(~mod + 2));

auto newKey = key * k2;

if (pk2) {
*pk2 = (uint32_t)(newKey >> 32);
*pk2 = (uint32_t)(newKey >> 32u);
}

if (pk1) {
Expand Down Expand Up @@ -40,13 +40,13 @@ CGSS_NS_BEGIN
bool_t CHcaCipher::Init(const CHcaCipherConfig &config) {
auto type = static_cast<HcaCipherType>(config.cipherType);

if (!(config.keyParts.key1 | config.keyParts.key2) && type == HcaCipherType::WithKey) {
if (config.key == 0 && type == HcaCipherType::WithKey) {
type = HcaCipherType::NoCipher;
}

uint32_t key1 = config.keyParts.key1, key2 = config.keyParts.key2;

if ((key1 && key2) && type == HcaCipherType::WithKey && config.keyModifier) {
if (config.key != 0 && type == HcaCipherType::WithKey && config.keyModifier) {
TransformKey(key1, key2, config.keyModifier, &key1, &key2);
}

Expand Down Expand Up @@ -87,9 +87,9 @@ CGSS_NS_BEGIN

void CHcaCipher::Init1() {
for (uint32_t i = 1, v = 0; i < 0xFF; i++) {
v = (v * 13 + 11) & 0xFF;
v = (v * 13u + 11u) & 0xFFu;
if (v == 0 || v == 0xFF) {
v = (v * 13 + 11) & 0xFF;
v = (v * 13u + 11u) & 0xFFu;
}
_decryptTable[i] = (uint8_t)v;
}
Expand All @@ -106,8 +106,8 @@ CGSS_NS_BEGIN
key1--;
for (uint8_t i = 0; i < 7; i++) {
t1[i] = (uint8_t)key1;
key1 = (key1 >> 8) | (key2 << 24);
key2 >>= 8;
key1 = (key1 >> 8u) | (key2 << 24u);
key2 >>= 8u;
}

// Generate table #2
Expand All @@ -127,18 +127,18 @@ CGSS_NS_BEGIN
Init56_CreateTable(t31, t1[0]);
for (uint8_t i = 0; i < 0x10; i++) {
Init56_CreateTable(t32, t2[i]);
uint8_t v = t31[i] << 4;
for (uint8_t j = 0; j < 0x10; j++) {
uint8_t v = t31[i] << 4u;
for (uint8_t j = 0; j < 0x10u; j++) {
*(t++) = v | t32[j];
}
}

// Generate CIPH table
t = &_decryptTable[1];
for (uint32_t i = 0, v = 0; i < TableSize; i++) {
v = (v + 0x11) & 0xFF;
v = (v + 0x11u) & 0xFFu;
uint8_t a = t3[v];
if (a != 0 && a != 0xFF) {
if (a != 0 && a != 0xFFu) {
*(t++) = a;
}
}
Expand All @@ -147,11 +147,11 @@ CGSS_NS_BEGIN
}

void CHcaCipher::Init56_CreateTable(uint8_t *r, uint8_t key) {
int32_t mul = ((key & 1) << 3) | 5;
int32_t add = (key & 0xE) | 1;
key >>= 4;
uint32_t mul = ((key & 1u) << 3u) | 5u;
uint32_t add = (key & 0xEu) | 1u;
key >>= 4u;
for (uint32_t i = 0; i < 0x10; i++) {
key = (uint8_t)((key * mul + add) & 0xF);
key = (uint8_t)((key * mul + add) & 0xFu);
*(r++) = key;
}
}
Expand Down

0 comments on commit 4871906

Please sign in to comment.