// Copyright (c) 2014-2017 Thomas Fussell // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE // // @license: http://www.opensource.org/licenses/mit-license.php // @author: see AUTHORS file #include #include #include #include #include #include #include #include #include #include #include namespace xlnt { namespace detail { enum class hash_algorithm { sha1, sha256, sha384, sha512, md5, md4, md2, ripemd128, ripemd160, whirlpool }; struct XLNT_API crypto_helper { static const std::size_t segment_length; enum class cipher_algorithm { aes, rc2, rc4, des, desx, triple_des, triple_des_112 }; enum class cipher_chaining { ecb, // electronic code book cbc, // cipher block chaining cfb // cipher feedback chaining }; enum class cipher_direction { encryption, decryption }; static std::vector aes(const std::vector &key, const std::vector &iv, const std::vector &source, cipher_chaining chaining, cipher_direction direction); static std::vector decode_base64(const std::string &encoded); static std::string encode_base64(const std::vector &decoded); static std::vector hash(hash_algorithm algorithm, const std::vector &input); static std::vector file(POLE::Storage &storage, const std::string &name); struct standard_encryption_info { const std::size_t spin_count = 50000; std::size_t block_size; std::size_t key_bits; std::size_t key_bytes; std::size_t hash_size; cipher_algorithm cipher; cipher_chaining chaining; const hash_algorithm hash = hash_algorithm::sha1; std::vector salt_value; std::vector verifier_hash_input; std::vector verifier_hash_value; std::vector encrypted_key_value; }; static std::vector decrypt_xlsx_standard(const std::vector &encryption_info, const std::string &password, const std::vector &encrypted_package); struct agile_encryption_info { // key data struct { std::size_t salt_size; std::size_t block_size; std::size_t key_bits; std::size_t hash_size; std::string cipher_algorithm; std::string cipher_chaining; std::string hash_algorithm; std::vector salt_value; } key_data; struct { std::vector hmac_key; std::vector hmac_value; } data_integrity; struct { std::size_t spin_count; std::size_t salt_size; std::size_t block_size; std::size_t key_bits; std::size_t hash_size; std::string cipher_algorithm; std::string cipher_chaining; hash_algorithm hash; std::vector salt_value; std::vector verifier_hash_input; std::vector verifier_hash_value; std::vector encrypted_key_value; } key_encryptor; }; static agile_encryption_info generate_agile_encryption_info(const std::string &password); static std::vector write_agile_encryption_info(const std::string &password); static std::vector decrypt_xlsx_agile(const std::vector &encryption_info, const std::string &password, const std::vector &encrypted_package); static std::vector decrypt_xlsx(const std::vector &bytes, const std::string &password); static std::vector encrypt_xlsx(const std::vector &bytes, const std::string &password); }; } // namespace detail } // namespace xlnt