Skip to content

Commit

Permalink
Fixed LTO problems (sgan81#164), at the expense of encryption speed ...
Browse files Browse the repository at this point in the history
  • Loading branch information
sgan81 committed Jan 4, 2023
1 parent 8a9bad3 commit 1f041d7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 34 deletions.
40 changes: 18 additions & 22 deletions ApfsLib/Aes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,17 +428,15 @@ void AES::SetKey(const uint8_t *key, Mode mode)
SetIV(0);
}

void AES::Encrypt(const void *src, void *dst)
void AES::Encrypt(const uint8_t *src, uint8_t *dst)
{
const uint32_t * const s = reinterpret_cast<const uint32_t *>(src);
uint32_t * const d = reinterpret_cast<uint32_t *>(dst);
uint32_t s0, s1, s2, s3, t0, t1, t2, t3;
int r, ki;

s0 = be32toh(s[0]) ^ _erk[0];
s1 = be32toh(s[1]) ^ _erk[1];
s2 = be32toh(s[2]) ^ _erk[2];
s3 = be32toh(s[3]) ^ _erk[3];
s0 = (src[0] << 24 | src[1] << 16 | src[2] << 8 | src[3]) ^ _erk[0];
s1 = (src[4] << 24 | src[5] << 16 | src[6] << 8 | src[7]) ^ _erk[1];
s2 = (src[8] << 24 | src[9] << 16 | src[10] << 8 | src[11]) ^ _erk[2];
s3 = (src[12] << 24 | src[13] << 16 | src[14] << 8 | src[15]) ^ _erk[3];

ki = 4;
r = Nr >> 1;
Expand All @@ -460,23 +458,21 @@ void AES::Encrypt(const void *src, void *dst)
s2 = (Te4[t2 >> 24] & 0xFF000000) ^ (Te4[(t3 >> 16) & 0xFF] & 0xFF0000) ^ (Te4[(t0 >> 8) & 0xFF] & 0xFF00) ^ (Te4[t1 & 0xFF] & 0xFF) ^ _erk[ki++];
s3 = (Te4[t3 >> 24] & 0xFF000000) ^ (Te4[(t0 >> 16) & 0xFF] & 0xFF0000) ^ (Te4[(t1 >> 8) & 0xFF] & 0xFF00) ^ (Te4[t2 & 0xFF] & 0xFF) ^ _erk[ki++];

d[0] = htobe32(s0);
d[1] = htobe32(s1);
d[2] = htobe32(s2);
d[3] = htobe32(s3);
dst[0] = s0 >> 24; dst[1] = s0 >> 16; dst[2] = s0 >> 8; dst[3] = s0;
dst[4] = s1 >> 24; dst[5] = s1 >> 16; dst[6] = s1 >> 8; dst[7] = s1;
dst[8] = s2 >> 24; dst[9] = s2 >> 16; dst[10] = s2 >> 8; dst[11] = s2;
dst[12] = s3 >> 24; dst[13] = s3 >> 16; dst[14] = s3 >> 8; dst[15] = s3;
}

void AES::Decrypt(const void *src, void *dst)
void AES::Decrypt(const uint8_t *src, uint8_t *dst)
{
const uint32_t * const s = reinterpret_cast<const uint32_t *>(src);
uint32_t * const d = reinterpret_cast<uint32_t *>(dst);
uint32_t s0, s1, s2, s3, t0, t1, t2, t3;
int r, ki;

s0 = be32toh(s[0]) ^ _drk[0];
s1 = be32toh(s[1]) ^ _drk[1];
s2 = be32toh(s[2]) ^ _drk[2];
s3 = be32toh(s[3]) ^ _drk[3];
s0 = (src[0] << 24 | src[1] << 16 | src[2] << 8 | src[3]) ^ _drk[0];
s1 = (src[4] << 24 | src[5] << 16 | src[6] << 8 | src[7]) ^ _drk[1];
s2 = (src[8] << 24 | src[9] << 16 | src[10] << 8 | src[11]) ^ _drk[2];
s3 = (src[12] << 24 | src[13] << 16 | src[14] << 8 | src[15]) ^ _drk[3];

ki = 4;
r = Nr >> 1;
Expand All @@ -498,10 +494,10 @@ void AES::Decrypt(const void *src, void *dst)
s2 = (Td4[t2 >> 24] & 0xFF000000) ^ (Td4[(t1 >> 16) & 0xFF] & 0xFF0000) ^ (Td4[(t0 >> 8) & 0xFF] & 0xFF00) ^ (Td4[t3 & 0xFF] & 0xFF) ^ _drk[ki++];
s3 = (Td4[t3 >> 24] & 0xFF000000) ^ (Td4[(t2 >> 16) & 0xFF] & 0xFF0000) ^ (Td4[(t1 >> 8) & 0xFF] & 0xFF00) ^ (Td4[t0 & 0xFF] & 0xFF) ^ _drk[ki++];

d[0] = htobe32(s0);
d[1] = htobe32(s1);
d[2] = htobe32(s2);
d[3] = htobe32(s3);
dst[0] = s0 >> 24; dst[1] = s0 >> 16; dst[2] = s0 >> 8; dst[3] = s0;
dst[4] = s1 >> 24; dst[5] = s1 >> 16; dst[6] = s1 >> 8; dst[7] = s1;
dst[8] = s2 >> 24; dst[9] = s2 >> 16; dst[10] = s2 >> 8; dst[11] = s2;
dst[12] = s3 >> 24; dst[13] = s3 >> 16; dst[14] = s3 >> 8; dst[15] = s3;
}

void AES::SetIV(const uint8_t *iv)
Expand Down
4 changes: 2 additions & 2 deletions ApfsLib/Aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class AES
* @param src Plaintext block (128 bits, 16 bytes)
* @param dst Encrypted block (128 bits, 16 bytes)
*/
void Encrypt(const void *src, void *dst);
void Encrypt(const uint8_t *src, uint8_t *dst);

/**
* @brief Decrypt Block
Expand All @@ -82,7 +82,7 @@ class AES
* @param src Encrypted block (128 bits, 16 bytes)
* @param dst Decrypted block (128 bits, 16 bytes)
*/
void Decrypt(const void *src, void *dst);
void Decrypt(const uint8_t *src, uint8_t *dst);

/**
* @brief Encrypt CBC
Expand Down
12 changes: 4 additions & 8 deletions ApfsLib/AesXts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void AesXts::Encrypt(uint8_t* cipher, const uint8_t* plain, std::size_t size, ui
tweak[0] = htole64(unit_no);
tweak[1] = 0;

m_aes_2.Encrypt(tweak, tweak);
m_aes_2.Encrypt(reinterpret_cast<const uint8_t *>(tweak), reinterpret_cast<uint8_t *>(tweak));

for (k = 0; k < size; k += 0x10)
{
Expand All @@ -56,7 +56,7 @@ void AesXts::Decrypt(uint8_t* plain, const uint8_t* cipher, std::size_t size, ui
tweak[0] = htole64(unit_no);
tweak[1] = 0;

m_aes_2.Encrypt(tweak, tweak);
m_aes_2.Encrypt(reinterpret_cast<const uint8_t *>(tweak), reinterpret_cast<uint8_t *>(tweak));

for (k = 0; k < size; k += 0x10)
{
Expand All @@ -69,12 +69,8 @@ void AesXts::Decrypt(uint8_t* plain, const uint8_t* cipher, std::size_t size, ui

void AesXts::Xor128(void *out, const void *op1, const void *op2)
{
uint64_t *val64 = reinterpret_cast<uint64_t *>(out);
const uint64_t *op1_64 = reinterpret_cast<const uint64_t *>(op1);
const uint64_t *op2_64 = reinterpret_cast<const uint64_t *>(op2);

val64[0] = op1_64[0] ^ op2_64[0];
val64[1] = op1_64[1] ^ op2_64[1];
reinterpret_cast<uint64_t*>(out)[0] = reinterpret_cast<const uint64_t*>(op1)[0] ^ reinterpret_cast<const uint64_t*>(op2)[0];
reinterpret_cast<uint64_t*>(out)[1] = reinterpret_cast<const uint64_t*>(op1)[1] ^ reinterpret_cast<const uint64_t*>(op2)[1];
}

void AesXts::MultiplyTweak(uint64_t* tweak)
Expand Down
4 changes: 2 additions & 2 deletions ApfsLib/DiskStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -538,13 +538,13 @@ constexpr int J_DREC_HASH_SHIFT = 10;
struct j_drec_key_t {
j_key_t hdr;
le_uint16_t name_len;
uint8_t name[0];
uint8_t name[];
};

struct j_drec_hashed_key_t {
j_key_t hdr;
le_uint32_t name_len_and_hash;
uint8_t name[0];
uint8_t name[];
};

struct j_drec_val_t {
Expand Down

0 comments on commit 1f041d7

Please sign in to comment.