pare down ltc

This commit is contained in:
Thomas Fussell 2017-04-11 14:14:36 -04:00
parent ea7b7074f0
commit 0d65569857
2 changed files with 133 additions and 297 deletions

View File

@ -7,253 +7,6 @@
#include <string>
const char kBase64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
class Base64 {
public:
static bool Encode(const std::string &in, std::string *out) {
int i = 0, j = 0;
size_t enc_len = 0;
unsigned char a3[3];
unsigned char a4[4];
out->resize(EncodedLength(in));
int input_len = in.size();
std::string::const_iterator input = in.begin();
while (input_len--) {
a3[i++] = *(input++);
if (i == 3) {
a3_to_a4(a4, a3);
for (i = 0; i < 4; i++) {
(*out)[enc_len++] = kBase64Alphabet[a4[i]];
}
i = 0;
}
}
if (i) {
for (j = i; j < 3; j++) {
a3[j] = '\0';
}
a3_to_a4(a4, a3);
for (j = 0; j < i + 1; j++) {
(*out)[enc_len++] = kBase64Alphabet[a4[j]];
}
while ((i++ < 3)) {
(*out)[enc_len++] = '=';
}
}
return (enc_len == out->size());
}
static bool Encode(const char *input, size_t input_length, char *out, size_t out_length) {
int i = 0, j = 0;
char *out_begin = out;
unsigned char a3[3];
unsigned char a4[4];
size_t encoded_length = EncodedLength(input_length);
if (out_length < encoded_length) return false;
while (input_length--) {
a3[i++] = *input++;
if (i == 3) {
a3_to_a4(a4, a3);
for (i = 0; i < 4; i++) {
*out++ = kBase64Alphabet[a4[i]];
}
i = 0;
}
}
if (i) {
for (j = i; j < 3; j++) {
a3[j] = '\0';
}
a3_to_a4(a4, a3);
for (j = 0; j < i + 1; j++) {
*out++ = kBase64Alphabet[a4[j]];
}
while ((i++ < 3)) {
*out++ = '=';
}
}
return (out == (out_begin + encoded_length));
}
static bool Decode(const std::string &in, std::string *out) {
int i = 0, j = 0;
size_t dec_len = 0;
unsigned char a3[3];
unsigned char a4[4];
int input_len = in.size();
std::string::const_iterator input = in.begin();
out->resize(DecodedLength(in));
while (input_len--) {
if (*input == '=') {
break;
}
a4[i++] = *(input++);
if (i == 4) {
for (i = 0; i <4; i++) {
a4[i] = b64_lookup(a4[i]);
}
a4_to_a3(a3,a4);
for (i = 0; i < 3; i++) {
(*out)[dec_len++] = a3[i];
}
i = 0;
}
}
if (i) {
for (j = i; j < 4; j++) {
a4[j] = '\0';
}
for (j = 0; j < 4; j++) {
a4[j] = b64_lookup(a4[j]);
}
a4_to_a3(a3,a4);
for (j = 0; j < i - 1; j++) {
(*out)[dec_len++] = a3[j];
}
}
return (dec_len == out->size());
}
static bool Decode(const char *input, size_t input_length, char *out, size_t out_length) {
int i = 0, j = 0;
char *out_begin = out;
unsigned char a3[3];
unsigned char a4[4];
size_t decoded_length = DecodedLength(input, input_length);
if (out_length < decoded_length) return false;
while (input_length--) {
if (*input == '=') {
break;
}
a4[i++] = *(input++);
if (i == 4) {
for (i = 0; i <4; i++) {
a4[i] = b64_lookup(a4[i]);
}
a4_to_a3(a3,a4);
for (i = 0; i < 3; i++) {
*out++ = a3[i];
}
i = 0;
}
}
if (i) {
for (j = i; j < 4; j++) {
a4[j] = '\0';
}
for (j = 0; j < 4; j++) {
a4[j] = b64_lookup(a4[j]);
}
a4_to_a3(a3,a4);
for (j = 0; j < i - 1; j++) {
*out++ = a3[j];
}
}
return (out == (out_begin + decoded_length));
}
static int DecodedLength(const char *in, size_t in_length) {
int numEq = 0;
const char *in_end = in + in_length;
while (*--in_end == '=') ++numEq;
return ((6 * in_length) / 8) - numEq;
}
static int DecodedLength(const std::string &in) {
int numEq = 0;
int n = in.size();
for (std::string::const_reverse_iterator it = in.rbegin(); *it == '='; ++it) {
++numEq;
}
return ((6 * n) / 8) - numEq;
}
inline static int EncodedLength(size_t length) {
return (length + 2 - ((length + 2) % 3)) / 3 * 4;
}
inline static int EncodedLength(const std::string &in) {
return EncodedLength(in.length());
}
inline static void StripPadding(std::string *in) {
while (!in->empty() && *(in->rbegin()) == '=') in->resize(in->size() - 1);
}
private:
static inline void a3_to_a4(unsigned char * a4, unsigned char * a3) {
a4[0] = (a3[0] & 0xfc) >> 2;
a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);
a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);
a4[3] = (a3[2] & 0x3f);
}
static inline void a4_to_a3(unsigned char * a3, unsigned char * a4) {
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
}
static inline unsigned char b64_lookup(unsigned char c) {
if(c >='A' && c <='Z') return c - 'A';
if(c >='a' && c <='z') return c - 71;
if(c >='0' && c <='9') return c + 4;
if(c == '+') return 62;
if(c == '/') return 63;
return 255;
}
};
#pragma clang diagnostic pop

View File

@ -78,9 +78,6 @@ struct XLNT_API crypto_helper
decryption
};
static std::vector<std::uint8_t> aes(const std::vector<std::uint8_t> &key, const std::vector<std::uint8_t> &iv,
const std::vector<std::uint8_t> &source, cipher_chaining chaining, cipher_direction direction);
static std::vector<std::uint8_t> hash(hash_algorithm algorithm, const std::vector<std::uint8_t> &input);
static std::vector<std::uint8_t> file(POLE::Storage &storage, const std::string &name);
@ -163,70 +160,157 @@ auto read_int(std::size_t &index, const std::vector<std::uint8_t> &raw_data)
return result;
}
std::vector<std::uint8_t> hash_sha1(const std::vector<std::uint8_t> &input)
{
return SHA::sha1(input);
}
std::vector<std::uint8_t> hash_sha512(const std::vector<std::uint8_t> &input)
{
return SHA::sha512(input);
}
std::vector<std::uint8_t> decode_base64(const std::string &encoded)
{
std::vector<std::uint8_t> decoded(static_cast<std::size_t>(Base64::DecodedLength(encoded)), 0);
Base64::Decode(encoded.data(), encoded.size(), reinterpret_cast<char *>(&decoded.data()[0]), decoded.size());
return decoded;
}
/*
std::string encode_base64(const std::vector<std::uint8_t> &decoded)
static std::string encode_base64(const std::vector<std::uint8_t> &input)
{
std::string encoded(static_cast<std::size_t>(Base64::EncodedLength(decoded.size())), 0);
Base64::Encode(reinterpret_cast<const char *>(decoded.data()), decoded.size(), &encoded[0], encoded.size());
auto encoded_length = (input.size() + 2 - ((input.size() + 2) % 3)) / 3 * 4;
auto output = std::string(encoded_length, '\0');
auto input_iterator = input.begin();
auto output_iterator = output.begin();
int i = 0, j = 0;
unsigned char a3[3];
unsigned char a4[4];
const char kBase64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
while (input_iterator != input.end())
{
a3[i++] = *input_iterator++;
return encoded;
if (i == 3)
{
a4[0] = (a3[0] & 0xfc) >> 2;
a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);
a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);
a4[3] = (a3[2] & 0x3f);
for (i = 0; i < 4; i++)
{
*output_iterator++ = kBase64Alphabet[a4[i]];
}
i = 0;
}
}
if (i)
{
for (j = i; j < 3; j++)
{
a3[j] = '\0';
}
a4[0] = (a3[0] & 0xfc) >> 2;
a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);
a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);
a4[3] = (a3[2] & 0x3f);
for (j = 0; j < i + 1; j++)
{
*output_iterator++ = kBase64Alphabet[a4[j]];
}
while ((i++ < 3))
{
*output_iterator++ = '=';
}
}
return output;
}
*/
std::vector<std::uint8_t> crypto_helper::aes(
const std::vector<std::uint8_t> &key,
const std::vector<std::uint8_t> &iv,
const std::vector<std::uint8_t> &source,
cipher_chaining chaining, cipher_direction direction)
static std::vector<std::uint8_t> decode_base64(const std::string &input)
{
if (direction == cipher_direction::encryption && chaining == cipher_chaining::cbc)
int numEq = 0;
auto in_end = input.data() + input.size();
while (*--in_end == '=') ++numEq;
auto decoded_length = ((6 * input.size()) / 8) - numEq;
auto output = std::vector<std::uint8_t>(decoded_length);
auto input_iterator = input.begin();
auto output_iterator = output.begin();
int i = 0, j = 0;
unsigned char a3[3];
unsigned char a4[4];
auto b64_lookup = [](unsigned char c)
{
return xlnt::detail::aes_cbc_encrypt(source, key, iv);
}
else if (direction == cipher_direction::decryption && chaining == cipher_chaining::cbc)
if(c >='A' && c <='Z') return c - 'A';
if(c >='a' && c <='z') return c - 71;
if(c >='0' && c <='9') return c + 4;
if(c == '+') return 62;
if(c == '/') return 63;
return 255;
};
while (input_iterator != input.end())
{
return xlnt::detail::aes_cbc_decrypt(source, key, iv);
}
else if (direction == cipher_direction::encryption && chaining == cipher_chaining::ecb)
{
return xlnt::detail::aes_ecb_encrypt(source, key);
}
else if (direction == cipher_direction::decryption && chaining == cipher_chaining::ecb)
{
return xlnt::detail::aes_ecb_decrypt(source, key);
}
if (*input_iterator == '=')
{
break;
}
a4[i++] = *(input_iterator++);
throw xlnt::exception("unsupported encryption algorithm");
if (i == 4)
{
for (i = 0; i <4; i++)
{
a4[i] = b64_lookup(a4[i]);
}
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
for (i = 0; i < 3; i++)
{
*output_iterator++ = a3[i];
}
i = 0;
}
}
if (i)
{
for (j = i; j < 4; j++)
{
a4[j] = '\0';
}
for (j = 0; j < 4; j++)
{
a4[j] = b64_lookup(a4[j]);
}
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
for (j = 0; j < i - 1; j++)
{
*output_iterator++ = a3[j];
}
}
return output;
}
std::vector<std::uint8_t> crypto_helper::hash(hash_algorithm algorithm, const std::vector<std::uint8_t> &input)
{
if (algorithm == hash_algorithm::sha512)
{
return hash_sha512(input);
return SHA::sha512(input);
}
else if (algorithm == hash_algorithm::sha1)
{
return hash_sha1(input);
return SHA::sha1(input);
}
throw xlnt::exception("unsupported hash algorithm");
@ -621,8 +705,7 @@ std::vector<std::uint8_t> crypto_helper::decrypt_xlsx_agile(
auto current_segment_length = std::min(segment_length, encrypted_package.size() - i);
auto segment_end = encrypted_package.begin() + static_cast<std::ptrdiff_t>(i + current_segment_length);
encrypted_segment.assign(segment_begin, segment_end);
auto decrypted_segment =
aes(key, iv, encrypted_segment, cipher_chaining::cbc, cipher_direction::decryption);
auto decrypted_segment = xlnt::detail::aes_cbc_decrypt(encrypted_segment, key, iv);
decrypted_segment.resize(current_segment_length);
decrypted_package.insert(decrypted_package.end(), decrypted_segment.begin(), decrypted_segment.end());