encryption_info.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright (c) 2017-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <array>
  24. #include <detail/binary.hpp>
  25. #include <detail/cryptography/aes.hpp>
  26. #include <detail/cryptography/encryption_info.hpp>
  27. namespace {
  28. using xlnt::detail::encryption_info;
  29. std::vector<std::uint8_t> calculate_standard_key(
  30. encryption_info::standard_encryption_info info,
  31. const std::u16string &password)
  32. {
  33. // H_0 = H(salt + password)
  34. auto salt_plus_password = info.salt;
  35. auto password_bytes = xlnt::detail::string_to_bytes(password);
  36. std::copy(password_bytes.begin(),
  37. password_bytes.end(),
  38. std::back_inserter(salt_plus_password));
  39. auto h_0 = hash(info.hash, salt_plus_password);
  40. // H_n = H(iterator + H_n-1)
  41. std::vector<std::uint8_t> iterator_plus_h_n(4, 0);
  42. iterator_plus_h_n.insert(iterator_plus_h_n.end(), h_0.begin(), h_0.end());
  43. std::uint32_t &iterator = *reinterpret_cast<std::uint32_t *>(iterator_plus_h_n.data());
  44. std::vector<std::uint8_t> h_n;
  45. for (iterator = 0; iterator < info.spin_count; ++iterator)
  46. {
  47. hash(info.hash, iterator_plus_h_n, h_n);
  48. std::copy(h_n.begin(), h_n.end(), iterator_plus_h_n.begin() + 4);
  49. }
  50. // H_final = H(H_n + block)
  51. auto h_n_plus_block = h_n;
  52. const std::uint32_t block_number = 0;
  53. h_n_plus_block.insert(
  54. h_n_plus_block.end(),
  55. reinterpret_cast<const std::uint8_t *>(&block_number),
  56. reinterpret_cast<const std::uint8_t *>(&block_number) + sizeof(std::uint32_t));
  57. auto h_final = hash(info.hash, h_n_plus_block);
  58. // X1 = H(h_final ^ 0x36)
  59. std::vector<std::uint8_t> buffer(64, 0x36);
  60. for (std::size_t i = 0; i < h_final.size(); ++i)
  61. {
  62. buffer[i] = static_cast<std::uint8_t>(0x36 ^ h_final[i]);
  63. }
  64. auto X1 = hash(info.hash, buffer);
  65. // X2 = H(h_final ^ 0x5C)
  66. buffer.assign(64, 0x5c);
  67. for (std::size_t i = 0; i < h_final.size(); ++i)
  68. {
  69. buffer[i] = static_cast<std::uint8_t>(0x5c ^ h_final[i]);
  70. }
  71. auto X2 = hash(info.hash, buffer);
  72. auto X3 = X1;
  73. X3.insert(X3.end(), X2.begin(), X2.end());
  74. auto key = std::vector<std::uint8_t>(X3.begin(),
  75. X3.begin() + static_cast<std::ptrdiff_t>(info.key_bytes));
  76. using xlnt::detail::aes_ecb_decrypt;
  77. auto calculated_verifier_hash = hash(info.hash,
  78. aes_ecb_decrypt(info.encrypted_verifier, key));
  79. auto decrypted_verifier_hash = aes_ecb_decrypt(
  80. info.encrypted_verifier_hash, key);
  81. decrypted_verifier_hash.resize(calculated_verifier_hash.size());
  82. if (calculated_verifier_hash != decrypted_verifier_hash)
  83. {
  84. throw xlnt::exception("bad password");
  85. }
  86. return key;
  87. }
  88. std::vector<std::uint8_t> calculate_agile_key(
  89. encryption_info::agile_encryption_info info,
  90. const std::u16string &password)
  91. {
  92. // H_0 = H(salt + password)
  93. auto salt_plus_password = info.key_encryptor.salt_value;
  94. auto password_bytes = xlnt::detail::string_to_bytes(password);
  95. std::copy(password_bytes.begin(),
  96. password_bytes.end(),
  97. std::back_inserter(salt_plus_password));
  98. auto h_0 = hash(info.key_encryptor.hash, salt_plus_password);
  99. // H_n = H(iterator + H_n-1)
  100. std::vector<std::uint8_t> iterator_plus_h_n(4, 0);
  101. iterator_plus_h_n.insert(iterator_plus_h_n.end(), h_0.begin(), h_0.end());
  102. std::uint32_t &iterator = *reinterpret_cast<std::uint32_t *>(iterator_plus_h_n.data());
  103. std::vector<std::uint8_t> h_n;
  104. for (iterator = 0; iterator < info.key_encryptor.spin_count; ++iterator)
  105. {
  106. hash(info.key_encryptor.hash, iterator_plus_h_n, h_n);
  107. std::copy(h_n.begin(), h_n.end(), iterator_plus_h_n.begin() + 4);
  108. }
  109. static const std::size_t block_size = 8;
  110. auto calculate_block = [&info](
  111. const std::vector<std::uint8_t> &raw_key,
  112. const std::array<std::uint8_t, block_size> &block,
  113. const std::vector<std::uint8_t> &encrypted) {
  114. auto combined = raw_key;
  115. combined.insert(combined.end(), block.begin(), block.end());
  116. auto key = hash(info.key_encryptor.hash, combined);
  117. key.resize(info.key_encryptor.key_bits / 8);
  118. using xlnt::detail::aes_cbc_decrypt;
  119. return aes_cbc_decrypt(encrypted, key, info.key_encryptor.salt_value);
  120. };
  121. const std::array<std::uint8_t, block_size> input_block_key = {{0xfe, 0xa7, 0xd2, 0x76, 0x3b, 0x4b, 0x9e, 0x79}};
  122. auto hash_input = calculate_block(h_n, input_block_key, info.key_encryptor.verifier_hash_input);
  123. auto calculated_verifier = hash(info.key_encryptor.hash, hash_input);
  124. const std::array<std::uint8_t, block_size> verifier_block_key = {
  125. {0xd7, 0xaa, 0x0f, 0x6d, 0x30, 0x61, 0x34, 0x4e}};
  126. auto expected_verifier = calculate_block(h_n, verifier_block_key, info.key_encryptor.verifier_hash_value);
  127. expected_verifier.resize(calculated_verifier.size());
  128. if (calculated_verifier != expected_verifier)
  129. {
  130. throw xlnt::exception("bad password");
  131. }
  132. const std::array<std::uint8_t, block_size> key_value_block_key =
  133. {
  134. {0x14, 0x6e, 0x0b, 0xe7, 0xab, 0xac, 0xd0, 0xd6}};
  135. return calculate_block(h_n, key_value_block_key, info.key_encryptor.encrypted_key_value);
  136. }
  137. } // namespace
  138. namespace xlnt {
  139. namespace detail {
  140. std::vector<std::uint8_t> encryption_info::calculate_key() const
  141. {
  142. return is_agile
  143. ? calculate_agile_key(agile, password)
  144. : calculate_standard_key(standard, password);
  145. }
  146. } // namespace detail
  147. } // namespace xlnt