Skip to content

Instantly share code, notes, and snippets.

@hshrzd
Created January 22, 2021 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hshrzd/4fe5dfc4d5ccedd71906886ab82e245c to your computer and use it in GitHub Desktop.
Save hshrzd/4fe5dfc4d5ccedd71906886ab82e245c to your computer and use it in GitHub Desktop.
FlareOn Task10: solution to the part 2
#include <iostream>
#ifdef _MSC_VER
#include <stdint.h>
#else
#include <inttypes.h>
#include <climits>
#endif
static inline uint32_t rotr32(uint32_t n, unsigned int c)
{
const unsigned int mask = (CHAR_BIT * sizeof(n) - 1);
c &= mask;
return (n >> c) | (n << ((-c)&mask));
}
uint32_t encode_chunk(uint32_t pass_chunk, uint32_t t1, uint32_t t2, uint32_t t3)
{
uint32_t v2 = pass_chunk + t1;
uint32_t v3 = rotr32(v2, t2);
uint32_t res = v3 ^ t3;
return res;
}
void decode(uint32_t val[2])
{
uint32_t t1[] = {
0x4b695809, 0xe35b9b24, 0x71adcd92, 0x38d6e6c9,
0x5a844444, 0x2d422222, 0x16a11111, 0xcdbfbfa8,
0xe6dfdfd4, 0xf36fefea, 0x79b7f7f5, 0xfa34ccda,
0x7d1a666d, 0xf8620416, 0x7c31020b, 0x78f7b625
};
uint32_t t2[] = {
0xf, 0x11, 0x11, 0x11,
0xc, 0xc, 0xc, 0x15,
0x15, 0x15, 0x15, 0xf,
0xf, 0xf, 0xf, 0x12
};
uint32_t t3[] = {
0x674a1dea, 0xad92774c, 0x56c93ba6, 0x2b649dd3,
0x8b853750, 0x45c29ba8, 0x22e14dd4, 0x8f47df53,
0x47a3efa9, 0x23d1f7d4, 0x11e8fbea, 0x96c3044c,
0x4b618226, 0xbb87b8aa, 0x5dc3dc55, 0xb0d69793
};
uint32_t part1 = val[1];
uint32_t part2 = val[0];
uint32_t prev = 0;
for (int i = 15; i >= 0; i--) {
uint32_t res = part2 ^ encode_chunk(part1, t1[i], t2[i], t3[i]);
part2 = part1;
part1 = res;
}
val[0] = part1;
val[1] = part2;
}
void print_chunk(uint32_t chunk)
{
char* ptr = (char*)&chunk;
for (size_t i = 0; i < 4; i++)
{
std::cout << ptr[i];
}
}
int main()
{
/* Copied buffer:
StartOffset(h): 0015C100, EndOffset(h): 0015C11F, Length(h): 00000020 */
unsigned char rawData[32] = {
0x64, 0xA0, 0x60, 0x02, //0
0xEA, 0x8A, 0x87, 0x7D, //1
0x6C, 0xE9, 0x7C, 0xE4,
0x82, 0x3F, 0x2D, 0x0C,
0x8C, 0xB7, 0xB5, 0xEB,
0xCF, 0x35, 0x4F, 0x42,
0x4F, 0xAD, 0x2B, 0x49,
0x20, 0x28, 0x7C, 0xE0
};
uint32_t *enc = (uint32_t*)rawData;
for (int i = 0; i < 8; i += 2) {
uint32_t val[2] = { enc[i], enc[i+1] };
decode(val);
print_chunk(val[0]);
print_chunk(val[1]);
}
std::cout << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment