ooxml_crypt 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +58 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/ext/ooxml_crypt/extconf.rb +18 -0
- data/ext/ooxml_crypt/ooxml_crypt.c +27 -0
- data/ext/ooxml_crypt/ooxml_crypt.h +7 -0
- data/lib/ooxml_crypt/version.rb +5 -0
- data/lib/ooxml_crypt.rb +75 -0
- data/vendor/cybozulib/.github/workflows/main.yml +12 -0
- data/vendor/cybozulib/.gitignore +5 -0
- data/vendor/cybozulib/CMakeLists.txt +6 -0
- data/vendor/cybozulib/COPYRIGHT +27 -0
- data/vendor/cybozulib/Makefile +26 -0
- data/vendor/cybozulib/bin/libeay32.dll +0 -0
- data/vendor/cybozulib/bin/libmecab.dll +0 -0
- data/vendor/cybozulib/bin/ssleay32.dll +0 -0
- data/vendor/cybozulib/common.mk +116 -0
- data/vendor/cybozulib/common.props +25 -0
- data/vendor/cybozulib/cybozulib.sln +286 -0
- data/vendor/cybozulib/debug.props +14 -0
- data/vendor/cybozulib/include/cybozu/array.hpp +197 -0
- data/vendor/cybozulib/include/cybozu/atoi.hpp +238 -0
- data/vendor/cybozulib/include/cybozu/atomic.hpp +146 -0
- data/vendor/cybozulib/include/cybozu/base64.hpp +210 -0
- data/vendor/cybozulib/include/cybozu/benchmark.hpp +212 -0
- data/vendor/cybozulib/include/cybozu/bfd.hpp +105 -0
- data/vendor/cybozulib/include/cybozu/bit_operation.hpp +139 -0
- data/vendor/cybozulib/include/cybozu/bitvector.hpp +358 -0
- data/vendor/cybozulib/include/cybozu/condition_variable.hpp +113 -0
- data/vendor/cybozulib/include/cybozu/condition_variable_cs.hpp +74 -0
- data/vendor/cybozulib/include/cybozu/config.hpp +392 -0
- data/vendor/cybozulib/include/cybozu/critical_section.hpp +60 -0
- data/vendor/cybozulib/include/cybozu/crypto.hpp +321 -0
- data/vendor/cybozulib/include/cybozu/csucvector.hpp +624 -0
- data/vendor/cybozulib/include/cybozu/csv.hpp +294 -0
- data/vendor/cybozulib/include/cybozu/data_type.hpp +27 -0
- data/vendor/cybozulib/include/cybozu/endian.hpp +224 -0
- data/vendor/cybozulib/include/cybozu/env.hpp +63 -0
- data/vendor/cybozulib/include/cybozu/event.hpp +122 -0
- data/vendor/cybozulib/include/cybozu/exception.hpp +253 -0
- data/vendor/cybozulib/include/cybozu/file.hpp +626 -0
- data/vendor/cybozulib/include/cybozu/fmindex.hpp +291 -0
- data/vendor/cybozulib/include/cybozu/format.hpp +93 -0
- data/vendor/cybozulib/include/cybozu/frequency.hpp +264 -0
- data/vendor/cybozulib/include/cybozu/hash.hpp +67 -0
- data/vendor/cybozulib/include/cybozu/inttype.hpp +174 -0
- data/vendor/cybozulib/include/cybozu/itoa.hpp +336 -0
- data/vendor/cybozulib/include/cybozu/json.hpp +120 -0
- data/vendor/cybozulib/include/cybozu/line_stream.hpp +149 -0
- data/vendor/cybozulib/include/cybozu/link_libeay32.hpp +21 -0
- data/vendor/cybozulib/include/cybozu/link_mpir.hpp +18 -0
- data/vendor/cybozulib/include/cybozu/link_ssleay32.hpp +19 -0
- data/vendor/cybozulib/include/cybozu/log.hpp +237 -0
- data/vendor/cybozulib/include/cybozu/minixml.hpp +452 -0
- data/vendor/cybozulib/include/cybozu/mmap.hpp +143 -0
- data/vendor/cybozulib/include/cybozu/mutex.hpp +144 -0
- data/vendor/cybozulib/include/cybozu/nlp/mecab.hpp +96 -0
- data/vendor/cybozulib/include/cybozu/nlp/plsi.hpp +315 -0
- data/vendor/cybozulib/include/cybozu/nlp/random.hpp +74 -0
- data/vendor/cybozulib/include/cybozu/nlp/sparse.hpp +529 -0
- data/vendor/cybozulib/include/cybozu/nlp/svd.hpp +486 -0
- data/vendor/cybozulib/include/cybozu/nlp/tfidf.hpp +226 -0
- data/vendor/cybozulib/include/cybozu/nlp/top_score.hpp +75 -0
- data/vendor/cybozulib/include/cybozu/option.hpp +743 -0
- data/vendor/cybozulib/include/cybozu/parallel.hpp +88 -0
- data/vendor/cybozulib/include/cybozu/pcg.hpp +72 -0
- data/vendor/cybozulib/include/cybozu/process.hpp +324 -0
- data/vendor/cybozulib/include/cybozu/quit_signal_handler.hpp +66 -0
- data/vendor/cybozulib/include/cybozu/random_generator.hpp +144 -0
- data/vendor/cybozulib/include/cybozu/regex.hpp +463 -0
- data/vendor/cybozulib/include/cybozu/select8.hpp +279 -0
- data/vendor/cybozulib/include/cybozu/serializer.hpp +363 -0
- data/vendor/cybozulib/include/cybozu/sha1.hpp +209 -0
- data/vendor/cybozulib/include/cybozu/sha2.hpp +506 -0
- data/vendor/cybozulib/include/cybozu/siphash.hpp +105 -0
- data/vendor/cybozulib/include/cybozu/socket.hpp +785 -0
- data/vendor/cybozulib/include/cybozu/ssl.hpp +203 -0
- data/vendor/cybozulib/include/cybozu/stacktrace.hpp +291 -0
- data/vendor/cybozulib/include/cybozu/stream.hpp +269 -0
- data/vendor/cybozulib/include/cybozu/string.hpp +1746 -0
- data/vendor/cybozulib/include/cybozu/string_operation.hpp +365 -0
- data/vendor/cybozulib/include/cybozu/sucvector.hpp +378 -0
- data/vendor/cybozulib/include/cybozu/test.hpp +373 -0
- data/vendor/cybozulib/include/cybozu/thread.hpp +229 -0
- data/vendor/cybozulib/include/cybozu/time.hpp +281 -0
- data/vendor/cybozulib/include/cybozu/tls.hpp +115 -0
- data/vendor/cybozulib/include/cybozu/unordered_map.hpp +13 -0
- data/vendor/cybozulib/include/cybozu/unordered_set.hpp +13 -0
- data/vendor/cybozulib/include/cybozu/v128.hpp +376 -0
- data/vendor/cybozulib/include/cybozu/wavelet_matrix.hpp +345 -0
- data/vendor/cybozulib/include/cybozu/xorshift.hpp +189 -0
- data/vendor/cybozulib/include/cybozu/zlib.hpp +325 -0
- data/vendor/cybozulib/include/sais.hxx +364 -0
- data/vendor/cybozulib/misc/make_select8tbl.cpp +26 -0
- data/vendor/cybozulib/mk.bat +37 -0
- data/vendor/cybozulib/readme.md +29 -0
- data/vendor/cybozulib/release.props +12 -0
- data/vendor/cybozulib/sample/Makefile +30 -0
- data/vendor/cybozulib/sample/csucvector_smpl.cpp +42 -0
- data/vendor/cybozulib/sample/data/svd/org/test1.S +4 -0
- data/vendor/cybozulib/sample/data/svd/org/test1.U +4 -0
- data/vendor/cybozulib/sample/data/svd/org/test1.V +6 -0
- data/vendor/cybozulib/sample/data/svd/test1 +4 -0
- data/vendor/cybozulib/sample/data/svd/test2 +4 -0
- data/vendor/cybozulib/sample/desymbol.cpp +127 -0
- data/vendor/cybozulib/sample/exception_smpl.cpp +46 -0
- data/vendor/cybozulib/sample/fmindex_smpl.cpp +231 -0
- data/vendor/cybozulib/sample/log_smpl.cpp +19 -0
- data/vendor/cybozulib/sample/mecab_smpl.cpp +37 -0
- data/vendor/cybozulib/sample/option2_smpl.cpp +68 -0
- data/vendor/cybozulib/sample/option_smpl.cpp +42 -0
- data/vendor/cybozulib/sample/plsi_smpl.cpp +207 -0
- data/vendor/cybozulib/sample/proj/exception_smpl.vcproj +184 -0
- data/vendor/cybozulib/sample/proj/mecab_smpl.vcproj +184 -0
- data/vendor/cybozulib/sample/proj/ssl_smpl/ssl_smpl.vcxproj +85 -0
- data/vendor/cybozulib/sample/proj/ssl_smpl.vcproj +347 -0
- data/vendor/cybozulib/sample/proj/stacktrace_smpl/stacktrace_smpl.vcxproj +85 -0
- data/vendor/cybozulib/sample/proj/svd_smpl.vcproj +184 -0
- data/vendor/cybozulib/sample/quit_signal_handler.cpp +30 -0
- data/vendor/cybozulib/sample/serializer_smpl.cpp +196 -0
- data/vendor/cybozulib/sample/socket_smpl.cpp +82 -0
- data/vendor/cybozulib/sample/ssl_smpl.cpp +39 -0
- data/vendor/cybozulib/sample/stacktrace_smpl.cpp +52 -0
- data/vendor/cybozulib/sample/svd_bench_smpl.cpp +143 -0
- data/vendor/cybozulib/sample/svd_smpl.cpp +94 -0
- data/vendor/cybozulib/sample/wm_bench_smpl.cpp +182 -0
- data/vendor/cybozulib/sample/zlib_smpl.cpp +41 -0
- data/vendor/cybozulib/src/Makefile +8 -0
- data/vendor/cybozulib/src/base/Makefile +19 -0
- data/vendor/cybozulib/test/Makefile +12 -0
- data/vendor/cybozulib/test/base/Makefile +37 -0
- data/vendor/cybozulib/test/base/array_test.cpp +173 -0
- data/vendor/cybozulib/test/base/atoi_test.cpp +774 -0
- data/vendor/cybozulib/test/base/atomic_test.cpp +49 -0
- data/vendor/cybozulib/test/base/base64_test.cpp +113 -0
- data/vendor/cybozulib/test/base/bit_operation_test.cpp +134 -0
- data/vendor/cybozulib/test/base/bitvector_test.cpp +204 -0
- data/vendor/cybozulib/test/base/condition_variable_cs_test.cpp +92 -0
- data/vendor/cybozulib/test/base/condition_variable_test.cpp +88 -0
- data/vendor/cybozulib/test/base/config_test.cpp +236 -0
- data/vendor/cybozulib/test/base/crypto_test.cpp +122 -0
- data/vendor/cybozulib/test/base/csucvector_test.cpp +63 -0
- data/vendor/cybozulib/test/base/csv_test.cpp +182 -0
- data/vendor/cybozulib/test/base/data/a.xml +26 -0
- data/vendor/cybozulib/test/base/endian_test.cpp +56 -0
- data/vendor/cybozulib/test/base/env_test.cpp +22 -0
- data/vendor/cybozulib/test/base/event_test.cpp +41 -0
- data/vendor/cybozulib/test/base/file_test.cpp +233 -0
- data/vendor/cybozulib/test/base/fmindex_test.cpp +118 -0
- data/vendor/cybozulib/test/base/format_test.cpp +12 -0
- data/vendor/cybozulib/test/base/frequency_test.cpp +104 -0
- data/vendor/cybozulib/test/base/itoa_test.cpp +522 -0
- data/vendor/cybozulib/test/base/line_stream_test.cpp +208 -0
- data/vendor/cybozulib/test/base/mecab_test.cpp +41 -0
- data/vendor/cybozulib/test/base/minixml_test.cpp +103 -0
- data/vendor/cybozulib/test/base/mmap_test.cpp +15 -0
- data/vendor/cybozulib/test/base/option_test.cpp +487 -0
- data/vendor/cybozulib/test/base/parallel_test.cpp +48 -0
- data/vendor/cybozulib/test/base/proj/array_test/array_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/atoi_test/atoi_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/atomic_test/atomic_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/base64_test/base64_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/condition_variable_cs_test/condition_variable_cs_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/condition_variable_test/condition_variable_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/config_test/config_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/csv_test/csv_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/endian_test/endian_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/env_test/env_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/event_test/event_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/file_test/file_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/itoa_test/itoa_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/mecab_test/mecab_test.vcxproj +88 -0
- data/vendor/cybozulib/test/base/proj/minixml_test/minixml_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/mmap_test/mmap_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/serializer_test/serializer_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/sha1_test/sha1_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/stream_test/stream_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/string_operation_test/string_operation_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/string_test/string_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/thread_test/thread_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/time_test/time_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/tls_test/tls_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/zlib_test/zlib_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/random_generator_test.cpp +28 -0
- data/vendor/cybozulib/test/base/regex_test.cpp +74 -0
- data/vendor/cybozulib/test/base/serializer_test.cpp +483 -0
- data/vendor/cybozulib/test/base/sha1_test.cpp +61 -0
- data/vendor/cybozulib/test/base/sha2_test.cpp +191 -0
- data/vendor/cybozulib/test/base/siphash_test.cpp +33 -0
- data/vendor/cybozulib/test/base/socket_test.cpp +76 -0
- data/vendor/cybozulib/test/base/stream_test.cpp +101 -0
- data/vendor/cybozulib/test/base/string_operation_test.cpp +340 -0
- data/vendor/cybozulib/test/base/string_test.cpp +1705 -0
- data/vendor/cybozulib/test/base/sucvector_test.cpp +312 -0
- data/vendor/cybozulib/test/base/thread_test.cpp +62 -0
- data/vendor/cybozulib/test/base/time_test.cpp +164 -0
- data/vendor/cybozulib/test/base/tls_test.cpp +50 -0
- data/vendor/cybozulib/test/base/wavelet_matrix_test.cpp +145 -0
- data/vendor/cybozulib/test/base/zlib_test.cpp +371 -0
- data/vendor/cybozulib/test/nlp/Makefile +27 -0
- data/vendor/cybozulib/test/nlp/proj/random_test.vcproj +184 -0
- data/vendor/cybozulib/test/nlp/proj/sparse_test.vcproj +184 -0
- data/vendor/cybozulib/test/nlp/proj/svd_test.vcproj +184 -0
- data/vendor/cybozulib/test/nlp/random_test.cpp +62 -0
- data/vendor/cybozulib/test/nlp/sparse_test.cpp +347 -0
- data/vendor/cybozulib/test/nlp/svd_test.cpp +234 -0
- data/vendor/cybozulib/test/nlp/top_score_test.cpp +40 -0
- data/vendor/cybozulib/tool/create_vcproj.py +186 -0
- data/vendor/cybozulib/tool/vcproj_tmpl.py +185 -0
- data/vendor/msoffice/COPYRIGHT +27 -0
- data/vendor/msoffice/Makefile +29 -0
- data/vendor/msoffice/bin/64/msoc.dll +0 -0
- data/vendor/msoffice/bin/64/msocsample.exe +0 -0
- data/vendor/msoffice/bin/64/msoffice-crypt.exe +0 -0
- data/vendor/msoffice/bin/msoc.dll +0 -0
- data/vendor/msoffice/bin/msocsample.exe +0 -0
- data/vendor/msoffice/bin/msoffice-crypt.exe +0 -0
- data/vendor/msoffice/common.mk +71 -0
- data/vendor/msoffice/common.props +26 -0
- data/vendor/msoffice/debug.props +14 -0
- data/vendor/msoffice/include/attack.hpp +211 -0
- data/vendor/msoffice/include/cfb.hpp +777 -0
- data/vendor/msoffice/include/crypto_util.hpp +450 -0
- data/vendor/msoffice/include/custom_sha1.hpp +342 -0
- data/vendor/msoffice/include/decode.hpp +240 -0
- data/vendor/msoffice/include/encode.hpp +221 -0
- data/vendor/msoffice/include/make_dataspace.hpp +316 -0
- data/vendor/msoffice/include/msoc.h +129 -0
- data/vendor/msoffice/include/resource.hpp +7 -0
- data/vendor/msoffice/include/standard_encryption.hpp +145 -0
- data/vendor/msoffice/include/uint32vec.hpp +179 -0
- data/vendor/msoffice/include/util.hpp +212 -0
- data/vendor/msoffice/lib/.emptydir +0 -0
- data/vendor/msoffice/misc/decrypt-xls.vbs +46 -0
- data/vendor/msoffice/mk.bat +1 -0
- data/vendor/msoffice/mkdll.bat +3 -0
- data/vendor/msoffice/msoc.def +13 -0
- data/vendor/msoffice/msocsample.py +178 -0
- data/vendor/msoffice/msoffice12.sln +31 -0
- data/vendor/msoffice/readme.md +110 -0
- data/vendor/msoffice/release.props +28 -0
- data/vendor/msoffice/src/Makefile +19 -0
- data/vendor/msoffice/src/attack.cpp +124 -0
- data/vendor/msoffice/src/cfb_test.cpp +77 -0
- data/vendor/msoffice/src/minisample.c +54 -0
- data/vendor/msoffice/src/msocdll.cpp +276 -0
- data/vendor/msoffice/src/msocsample.c +136 -0
- data/vendor/msoffice/src/msoffice-crypt.cpp +219 -0
- data/vendor/msoffice/src/proj/attack/attack.vcxproj +88 -0
- data/vendor/msoffice/src/proj/main/msoffice-crypt.vcxproj +88 -0
- data/vendor/msoffice/src/sha1.cpp +234 -0
- data/vendor/msoffice/test/Makefile +20 -0
- data/vendor/msoffice/test/cfb_test.cpp +74 -0
- data/vendor/msoffice/test/hash_test.cpp +59 -0
- data/vendor/msoffice/test/proj/cfb/cfb_test.vcxproj +90 -0
- data/vendor/msoffice/test/proj/hash/hash_test.vcxproj +90 -0
- data/vendor/msoffice/test/sampl.bat +8 -0
- data/vendor/msoffice/test_all.py +46 -0
- data/vendor/update +4 -0
- metadata +351 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
/**
|
|
3
|
+
@file
|
|
4
|
+
@brief MS Office encryption encoder
|
|
5
|
+
Copyright (C) 2012 Cybozu Labs, Inc., all rights reserved.
|
|
6
|
+
*/
|
|
7
|
+
#include <cybozu/crypto.hpp>
|
|
8
|
+
#include <cybozu/mmap.hpp>
|
|
9
|
+
#include <cybozu/random_generator.hpp>
|
|
10
|
+
#include "crypto_util.hpp"
|
|
11
|
+
#include "cfb.hpp"
|
|
12
|
+
#include "make_dataspace.hpp"
|
|
13
|
+
#include "resource.hpp"
|
|
14
|
+
|
|
15
|
+
//#define SAME_KEY
|
|
16
|
+
|
|
17
|
+
namespace ms {
|
|
18
|
+
|
|
19
|
+
inline cybozu::RandomGenerator& GetRandGen()
|
|
20
|
+
{
|
|
21
|
+
static cybozu::RandomGenerator rg;
|
|
22
|
+
return rg;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
inline void FillRand(std::string& str, size_t n)
|
|
26
|
+
{
|
|
27
|
+
str.resize(n);
|
|
28
|
+
GetRandGen().read(&str[0], static_cast<int>(n));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
#if 0
|
|
32
|
+
inline void VerifyFormat(const char *data, uint32_t dataSize)
|
|
33
|
+
{
|
|
34
|
+
if (dataSize < 2) throw cybozu::Exception("ms:VerifyFormat:too small") << dataSize;
|
|
35
|
+
if (memcmp(data, "PK", 2) != 0) throw cybozu::Exception("ms:VerifyFormat:bad format");
|
|
36
|
+
}
|
|
37
|
+
#endif
|
|
38
|
+
|
|
39
|
+
/*
|
|
40
|
+
encryptedPackage = [uint64_t:encData]
|
|
41
|
+
*/
|
|
42
|
+
inline void MakeEncryptedPackage(std::string& encryptedPackage, const std::string& encData)
|
|
43
|
+
{
|
|
44
|
+
encryptedPackage.reserve(encData.size() + 8);
|
|
45
|
+
encryptedPackage.resize(8);
|
|
46
|
+
cybozu::Set64bitAsLE(&encryptedPackage[0], encData.size());
|
|
47
|
+
encryptedPackage += encData;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/*
|
|
51
|
+
[MS-OFFCRYPTO] 2.3.4.14
|
|
52
|
+
*/
|
|
53
|
+
inline void GenerateIntegrityParameter(
|
|
54
|
+
std::string& encryptedHmacKey,
|
|
55
|
+
std::string& encryptedHmacValue,
|
|
56
|
+
const std::string& encryptedPackage,
|
|
57
|
+
const CipherParam& keyData,
|
|
58
|
+
const std::string& secretKey,
|
|
59
|
+
const std::string& saltValue)
|
|
60
|
+
{
|
|
61
|
+
std::string salt;
|
|
62
|
+
FillRand(salt, keyData.hashSize);
|
|
63
|
+
#ifdef SAME_KEY
|
|
64
|
+
salt = fromHex("C9FACA5436849906B600DE95E155B47A01ABEDD0");
|
|
65
|
+
#endif
|
|
66
|
+
const std::string iv1 = generateIv(keyData, ms::blkKey_dataIntegrity1, saltValue);
|
|
67
|
+
const std::string iv2 = generateIv(keyData, ms::blkKey_dataIntegrity2, saltValue);
|
|
68
|
+
encryptedHmacKey = cipher(keyData.cipherName, salt, secretKey, iv1, cybozu::crypto::Cipher::Encoding);
|
|
69
|
+
cybozu::crypto::Hmac hmac(keyData.hashName);
|
|
70
|
+
std::string ret = hmac.eval(salt, encryptedPackage);
|
|
71
|
+
encryptedHmacValue = cipher(keyData.cipherName, ret, secretKey, iv2, cybozu::crypto::Cipher::Encoding);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
inline void EncContent(std::string& encryptedPackage, const std::string& org, const CipherParam& param, const std::string& key, const std::string& salt)
|
|
75
|
+
{
|
|
76
|
+
uint64_t orgSize = org.size();
|
|
77
|
+
const size_t blockSize = 4096;
|
|
78
|
+
std::string data = org;
|
|
79
|
+
data.resize(RoundUp(data.size(), size_t(16)));
|
|
80
|
+
#ifdef SAME_KEY
|
|
81
|
+
data[data.size() - 2] = 0x4b; // QQQ remove this
|
|
82
|
+
data[data.size() - 1] = 0x6a;
|
|
83
|
+
#endif
|
|
84
|
+
encryptedPackage.reserve(data.size() + 8);
|
|
85
|
+
encryptedPackage.resize(8);
|
|
86
|
+
cybozu::Set64bitAsLE(&encryptedPackage[0], orgSize);
|
|
87
|
+
|
|
88
|
+
const size_t n = (data.size() + blockSize - 1) / blockSize;
|
|
89
|
+
for (size_t i = 0; i < n; i++) {
|
|
90
|
+
const size_t len = (i < n - 1) ? blockSize : (data.size() - blockSize * i);
|
|
91
|
+
std::string blockKey(4, 0);
|
|
92
|
+
cybozu::Set32bitAsLE(&blockKey[0], static_cast<uint32_t>(i));
|
|
93
|
+
const std::string iv = generateKey(param, salt, blockKey);
|
|
94
|
+
encryptedPackage.append(cipher(param.cipherName, data.c_str() + i * blockSize, len, key, iv, cybozu::crypto::Cipher::Encoding));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/*
|
|
99
|
+
����̋t���ňÍ���
|
|
100
|
+
fix parameter : c1(blkKey_VerifierHashInput)
|
|
101
|
+
c2(blkKey_encryptedVerifierHashValue)
|
|
102
|
+
c3(blkKey_encryptedKeyValue)
|
|
103
|
+
|
|
104
|
+
input : pass, spinCount
|
|
105
|
+
output: iv,
|
|
106
|
+
verifierHashInput,
|
|
107
|
+
encryptedVerifierHashValue,
|
|
108
|
+
encryptedKeyValue
|
|
109
|
+
|
|
110
|
+
iv(encryptedKey.saltValue)�������_������
|
|
111
|
+
|
|
112
|
+
pwHash = hashPassword(iv, pass, spinCount)
|
|
113
|
+
skey1 = generateKey(pwHash, c1)
|
|
114
|
+
skey2 = generateKey(pwHash, c2)
|
|
115
|
+
|
|
116
|
+
verifierHashInput�������_������
|
|
117
|
+
encryptedVerifierHashInput = Enc(verifierHashInput, skey1, iv)
|
|
118
|
+
hashedVerifier = H(verifierHashInput)
|
|
119
|
+
|
|
120
|
+
encryptedVerifierHashValue = Enc(verifierHash, skey2, iv)
|
|
121
|
+
|
|
122
|
+
skey3 = generateKey(pwHash, c3)
|
|
123
|
+
secretKey�������_������
|
|
124
|
+
encryptedKeyValue = Enc(secretKey, skey3, iv)
|
|
125
|
+
*/
|
|
126
|
+
|
|
127
|
+
/*
|
|
128
|
+
encode data by pass with cipherName, hashName, spinCount
|
|
129
|
+
output encData and info
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
inline bool encode_in(
|
|
133
|
+
std::string& encryptedPackage,
|
|
134
|
+
EncryptionInfo& info,
|
|
135
|
+
const std::string& data,
|
|
136
|
+
cybozu::crypto::Cipher::Name cipherName,
|
|
137
|
+
cybozu::crypto::Hash::Name hashName,
|
|
138
|
+
int spinCount,
|
|
139
|
+
const std::string& pass,
|
|
140
|
+
const std::string& masterKey)
|
|
141
|
+
{
|
|
142
|
+
if (spinCount > 10000000) throw cybozu::Exception("ms:encode_in:too large spinCount") << spinCount;
|
|
143
|
+
CipherParam& keyData = info.keyData;
|
|
144
|
+
CipherParam& encryptedKey = info.encryptedKey;
|
|
145
|
+
|
|
146
|
+
keyData.setByName(cipherName, hashName);
|
|
147
|
+
encryptedKey.setByName(cipherName, hashName);
|
|
148
|
+
info.spinCount = spinCount;
|
|
149
|
+
|
|
150
|
+
std::string& iv = encryptedKey.saltValue;
|
|
151
|
+
FillRand(iv, encryptedKey.saltSize);
|
|
152
|
+
#ifdef SAME_KEY
|
|
153
|
+
puts("QQQ defined SAME_KEY QQQ");
|
|
154
|
+
iv = fromHex("F4994F9B2DCD5E0E84BC6386D4523D2C");
|
|
155
|
+
#endif
|
|
156
|
+
const std::string pwHash = hashPassword(encryptedKey.hashName, iv, pass, spinCount);
|
|
157
|
+
|
|
158
|
+
const std::string skey1 = generateKey(encryptedKey, pwHash, blkKey_VerifierHashInput);
|
|
159
|
+
const std::string skey2 = generateKey(encryptedKey, pwHash, blkKey_encryptedVerifierHashValue);
|
|
160
|
+
const std::string skey3 = generateKey(encryptedKey, pwHash, blkKey_encryptedKeyValue);
|
|
161
|
+
|
|
162
|
+
std::string verifierHashInput;
|
|
163
|
+
FillRand(verifierHashInput, encryptedKey.saltSize);
|
|
164
|
+
#ifdef SAME_KEY
|
|
165
|
+
verifierHashInput = fromHex("FEDAECD950F9E82C47CADA29B7837C6D");
|
|
166
|
+
#endif
|
|
167
|
+
|
|
168
|
+
verifierHashInput.resize(RoundUp(verifierHashInput.size(), encryptedKey.blockSize));
|
|
169
|
+
|
|
170
|
+
info.encryptedVerifierHashInput = cipher(encryptedKey.cipherName, verifierHashInput, skey1, iv, cybozu::crypto::Cipher::Encoding);
|
|
171
|
+
std::string hashedVerifier = cybozu::crypto::Hash::digest(encryptedKey.hashName, verifierHashInput);
|
|
172
|
+
hashedVerifier.resize(RoundUp(hashedVerifier.size(), encryptedKey.blockSize));
|
|
173
|
+
|
|
174
|
+
info.encryptedVerifierHashValue = cipher(encryptedKey.cipherName, hashedVerifier, skey2, iv, cybozu::crypto::Cipher::Encoding);
|
|
175
|
+
|
|
176
|
+
std::string secretKey;
|
|
177
|
+
FillRand(secretKey, encryptedKey.saltSize);
|
|
178
|
+
#ifdef SAME_KEY
|
|
179
|
+
secretKey = fromHex("BF44FBB51BE1E88BF130156E117E7900");
|
|
180
|
+
#endif
|
|
181
|
+
if (!masterKey.empty()) {
|
|
182
|
+
secretKey = masterKey;
|
|
183
|
+
}
|
|
184
|
+
normalizeKey(secretKey, encryptedKey.keyBits / 8);
|
|
185
|
+
|
|
186
|
+
info.encryptedKeyValue = cipher(encryptedKey.cipherName, secretKey, skey3, iv, cybozu::crypto::Cipher::Encoding);
|
|
187
|
+
|
|
188
|
+
FillRand(keyData.saltValue, keyData.saltSize);
|
|
189
|
+
#ifdef SAME_KEY
|
|
190
|
+
keyData.saltValue = fromHex("C49AAAEE99004C6B017EE5CD11B86729");
|
|
191
|
+
#endif
|
|
192
|
+
|
|
193
|
+
EncContent(encryptedPackage, data, encryptedKey, secretKey, keyData.saltValue);
|
|
194
|
+
|
|
195
|
+
GenerateIntegrityParameter(info.encryptedHmacKey, info.encryptedHmacValue, encryptedPackage, keyData, secretKey, keyData.saltValue);
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
template<class String>
|
|
200
|
+
bool encode(const char *data, uint32_t dataSize, const String& outFile, const std::string& pass, bool isOffice2013, const std::string& masterKey, int spinCount)
|
|
201
|
+
{
|
|
202
|
+
std::string encryptedPackage;
|
|
203
|
+
ms::EncryptionInfo info;
|
|
204
|
+
const cybozu::crypto::Cipher::Name cipherName = isOffice2013 ? cybozu::crypto::Cipher::N_AES256_CBC : cybozu::crypto::Cipher::N_AES128_CBC;
|
|
205
|
+
const cybozu::crypto::Hash::Name hashName = isOffice2013 ? cybozu::crypto::Hash::N_SHA512 : cybozu::crypto::Hash::N_SHA1;
|
|
206
|
+
encode_in(encryptedPackage, info, std::string(data, dataSize), cipherName, hashName, spinCount, pass, masterKey);
|
|
207
|
+
const std::string encryptionInfoStr = info.addHeader(info.toXml(isOffice2013));
|
|
208
|
+
dprintf("encryptionInfoStr size=%d\n", (int)encryptionInfoStr.size());
|
|
209
|
+
ms::cfb::CompoundFile cfb;
|
|
210
|
+
ms::makeDataSpace(cfb.dirs, encryptedPackage, encryptionInfoStr);
|
|
211
|
+
std::string outData;
|
|
212
|
+
makeLayout(outData, cfb);
|
|
213
|
+
{
|
|
214
|
+
cybozu::File out;
|
|
215
|
+
out.openW(outFile);
|
|
216
|
+
out.write(outData.c_str(), outData.size());
|
|
217
|
+
}
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
} // ms
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
/**
|
|
3
|
+
Copyright (C) 2012 Cybozu Labs, Inc., all rights reserved.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#include <cybozu/time.hpp>
|
|
7
|
+
#include "cfb.hpp"
|
|
8
|
+
#include "resource.hpp"
|
|
9
|
+
|
|
10
|
+
namespace ms {
|
|
11
|
+
|
|
12
|
+
enum DSposition {
|
|
13
|
+
iRoot = 0,
|
|
14
|
+
iEncryptionPackage, // 1
|
|
15
|
+
iDataSpaces, // 2
|
|
16
|
+
iVersion, // 3
|
|
17
|
+
iDataSpaceMap, // 4
|
|
18
|
+
iDataSpaceInfo, // 5
|
|
19
|
+
iStongEncryptionDataSpace, // 6
|
|
20
|
+
iTransformInfo, // 7
|
|
21
|
+
iStrongEncryptionTransform, // 8
|
|
22
|
+
iPrimary, // 9
|
|
23
|
+
iEncryptionInfo, // 10
|
|
24
|
+
dirNum
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/*
|
|
28
|
+
make Data Spaces
|
|
29
|
+
*/
|
|
30
|
+
inline void makeDataSpace(
|
|
31
|
+
cfb::DirectoryEntryVec& dirs,
|
|
32
|
+
const std::string& encryptedPackage,
|
|
33
|
+
const std::string& encryptionInfoXml)
|
|
34
|
+
{
|
|
35
|
+
using namespace cfb;
|
|
36
|
+
cybozu::Time curTime;
|
|
37
|
+
curTime.setCurrentTime();
|
|
38
|
+
DirectoryEntry::FileTime fileTime;
|
|
39
|
+
curTime.getFILETIME(fileTime.dwLowDateTime, fileTime.dwHighDateTime);
|
|
40
|
+
|
|
41
|
+
/*
|
|
42
|
+
|
|
43
|
+
0:Root/ 1:EncryptionPackage(F)
|
|
44
|
+
/10:EncryptionInfo(F)
|
|
45
|
+
/ 2:DataSpaces/3:Version(F)
|
|
46
|
+
/4:DataSpaceMap(F)
|
|
47
|
+
/5:DataSpaceInfo/6:StrongEncryptionDataSpace(F)
|
|
48
|
+
/7:Transformation/8:StrongEncryptionTransform/9:Primary(F)
|
|
49
|
+
|
|
50
|
+
<10:B>
|
|
51
|
+
�^�_
|
|
52
|
+
<2:R> <1:R>
|
|
53
|
+
---------------
|
|
54
|
+
<4:B>
|
|
55
|
+
�^�_
|
|
56
|
+
<3:B> <5:B>
|
|
57
|
+
�_
|
|
58
|
+
<7:R>
|
|
59
|
+
---------------
|
|
60
|
+
<6:B>
|
|
61
|
+
---------------
|
|
62
|
+
<8:B>
|
|
63
|
+
---------------
|
|
64
|
+
<9:B>
|
|
65
|
+
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
const struct Info {
|
|
69
|
+
const char *name;
|
|
70
|
+
ObjectType objectType;
|
|
71
|
+
ColorFlag colorFlag;
|
|
72
|
+
uint32_t leftSiblingId;
|
|
73
|
+
uint32_t rightSiblingId;
|
|
74
|
+
uint32_t childId;
|
|
75
|
+
std::string content;
|
|
76
|
+
} infoTbl[] = {
|
|
77
|
+
{ "Root Entry", RootStorageObject, Red, NOSTREAM, NOSTREAM, iEncryptionInfo, "" },
|
|
78
|
+
{ "EncryptedPackage", StreamObject, Red, NOSTREAM, NOSTREAM, NOSTREAM, encryptedPackage },
|
|
79
|
+
{ "\x06""DataSpaces", StorageObject, Red, NOSTREAM, NOSTREAM, iDataSpaceMap, "" },
|
|
80
|
+
{ "Version", StreamObject, Black, NOSTREAM, NOSTREAM, NOSTREAM, resource::Version },
|
|
81
|
+
{ "DataSpaceMap", StreamObject, Black, iVersion, iDataSpaceInfo, NOSTREAM, resource::DataSpaceMap },
|
|
82
|
+
{ "DataSpaceInfo", StorageObject, Black, NOSTREAM, iTransformInfo, iStongEncryptionDataSpace, "" },
|
|
83
|
+
{ "StrongEncryptionDataSpace", StreamObject, Black, NOSTREAM, NOSTREAM, NOSTREAM, resource::StrongEncryptionDataSpace },
|
|
84
|
+
{ "TransformInfo", StorageObject, Red, NOSTREAM, NOSTREAM, iStrongEncryptionTransform, "" },
|
|
85
|
+
{ "StrongEncryptionTransform", StorageObject, Black, NOSTREAM, NOSTREAM, iPrimary, "" },
|
|
86
|
+
{ "\x06""Primary", StreamObject, Black, NOSTREAM, NOSTREAM, NOSTREAM, resource::Primary },
|
|
87
|
+
{ "EncryptionInfo", StreamObject, Black, iDataSpaces, iEncryptionPackage, NOSTREAM, encryptionInfoXml },
|
|
88
|
+
};
|
|
89
|
+
dirs.resize(dirNum);
|
|
90
|
+
const std::string clsid = "";
|
|
91
|
+
const uint32_t stateBits = 0;
|
|
92
|
+
for (size_t i = 0; i < dirs.size(); i++) {
|
|
93
|
+
uint32_t startingSectorLocation = 0;
|
|
94
|
+
const Info& info = infoTbl[i];
|
|
95
|
+
dirs[i].set(
|
|
96
|
+
cybozu::ToUtf16(info.name),
|
|
97
|
+
info.objectType,
|
|
98
|
+
info.colorFlag,
|
|
99
|
+
info.leftSiblingId,
|
|
100
|
+
info.rightSiblingId,
|
|
101
|
+
info.childId,
|
|
102
|
+
clsid,
|
|
103
|
+
stateBits,
|
|
104
|
+
fileTime,
|
|
105
|
+
fileTime,
|
|
106
|
+
startingSectorLocation,
|
|
107
|
+
info.content);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
inline void makeDifat(char *data, uint32_t difatPos, uint32_t difatSectorNum, uint32_t fatSectorNum, uint32_t sectorSize)
|
|
112
|
+
{
|
|
113
|
+
uint32_t v = ms::cfb::Header::firstNumDIFAT + difatSectorNum;
|
|
114
|
+
for (uint32_t i = 0; i < difatSectorNum; i++) {
|
|
115
|
+
dprintf("\ndifatSectorNum i=%d\n", i);
|
|
116
|
+
char *p = data + 512 + (difatPos + i) * sectorSize;
|
|
117
|
+
for (uint32_t j = 0; j < sectorSize / 4 - 1; j++) {
|
|
118
|
+
cybozu::Set32bitAsLE(p + j * 4, v);
|
|
119
|
+
v++;
|
|
120
|
+
dprintf("%d ", v);
|
|
121
|
+
if (v > difatSectorNum + fatSectorNum) {
|
|
122
|
+
for (; j < sectorSize / 4 - 1; j++) {
|
|
123
|
+
cybozu::Set32bitAsLE(p + j * 4, ms::cfb::FREESECT);
|
|
124
|
+
}
|
|
125
|
+
dprintf("endof\n");
|
|
126
|
+
cybozu::Set32bitAsLE(p + sectorSize - 4, ms::cfb::ENDOFCHAIN);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
dprintf("next %d\n", difatPos + i + 1);
|
|
131
|
+
cybozu::Set32bitAsLE(p + sectorSize - 4, difatPos + i + 1);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
inline void makeFat(char *data, const char *dataEnd, const cfb::UintVec& entry)
|
|
136
|
+
{
|
|
137
|
+
uint32_t v = 0;
|
|
138
|
+
for (size_t i = 0; i < entry.size(); i++) {
|
|
139
|
+
const uint32_t n = entry[i];
|
|
140
|
+
if (n <= cfb::MAXREGSECT) {
|
|
141
|
+
for (uint32_t j = 1; j < n; j++) {
|
|
142
|
+
v++;
|
|
143
|
+
if (data == dataEnd) throw cybozu::Exception("ms:makeFat:err1") << v << i << j;
|
|
144
|
+
cybozu::Set32bitAsLE(data, v);
|
|
145
|
+
data += 4;
|
|
146
|
+
}
|
|
147
|
+
if (data == dataEnd) throw cybozu::Exception("ms:makeFat:err2") << v << i;
|
|
148
|
+
cybozu::Set32bitAsLE(data, cfb::ENDOFCHAIN);
|
|
149
|
+
} else {
|
|
150
|
+
cybozu::Set32bitAsLE(data, n);
|
|
151
|
+
}
|
|
152
|
+
data += 4;
|
|
153
|
+
v++;
|
|
154
|
+
}
|
|
155
|
+
while (data != dataEnd) {
|
|
156
|
+
cybozu::Set32bitAsLE(data, cfb::FREESECT);
|
|
157
|
+
data += 4;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
inline void detectSectorNum(uint32_t& difatSectorNum, uint32_t& fatSectorNum, uint32_t n, uint32_t sectorSize)
|
|
162
|
+
{
|
|
163
|
+
const uint32_t numInFat = sectorSize / 4;
|
|
164
|
+
difatSectorNum = 0;
|
|
165
|
+
fatSectorNum = 0;
|
|
166
|
+
int count = 0;
|
|
167
|
+
for (;;) {
|
|
168
|
+
uint32_t a = getBlockNum(difatSectorNum + fatSectorNum + n + 0, numInFat);
|
|
169
|
+
uint32_t b = (a <= cfb::Header::firstNumDIFAT) ? 0 : getBlockNum(a - cfb::Header::firstNumDIFAT, numInFat - 1);
|
|
170
|
+
if (b == difatSectorNum && a == fatSectorNum) return;
|
|
171
|
+
difatSectorNum = b;
|
|
172
|
+
fatSectorNum = a;
|
|
173
|
+
count++;
|
|
174
|
+
if (count == 10) cybozu::Exception("ms:detectSectorNum:max loop") << difatSectorNum << fatSectorNum << n;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
template<class T>
|
|
179
|
+
uint32_t getMiniFatSectorNum(T size)
|
|
180
|
+
{
|
|
181
|
+
return getBlockNum(static_cast<uint32_t>(size), 64u);
|
|
182
|
+
}
|
|
183
|
+
/*
|
|
184
|
+
@note DIFAT is empty for current version
|
|
185
|
+
|
|
186
|
+
|Header|DIFAT0|DIFAT1|...|FAT0|FAT1|...|miniFAT|dir0|dir1|dir2|min0|min1|min2|min3|enc0|enc1|...
|
|
187
|
+
|
|
188
|
+
FAT
|
|
189
|
+
1 ; miniFAT
|
|
190
|
+
2 3 4 ; directoryEntry([d0:d1:d2])
|
|
191
|
+
5 6 7 8 ; miniFATdata([m0:m1:m2:m3])
|
|
192
|
+
9 10... ; encryptedPackage([c0:c1:...])
|
|
193
|
+
|
|
194
|
+
miniFAT(64byte x 8 x 4)
|
|
195
|
+
0 1 ; Version
|
|
196
|
+
2 3 ; DataSpaceMap
|
|
197
|
+
4
|
|
198
|
+
5 6 7 8
|
|
199
|
+
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
|
200
|
+
*/
|
|
201
|
+
inline void makeLayout(std::string& data, cfb::CompoundFile& cfb)
|
|
202
|
+
{
|
|
203
|
+
cfb::Header& header = cfb.header;
|
|
204
|
+
cfb::DirectoryEntryVec& dirs = cfb.dirs;
|
|
205
|
+
const uint32_t sectorSize = header.sectorSize;
|
|
206
|
+
|
|
207
|
+
// miniFAT
|
|
208
|
+
const uint32_t numMiniFatSectors = 1;
|
|
209
|
+
cfb::UintVec miniFatSectorNumVec;
|
|
210
|
+
uint32_t miniFatNum; // num of mini fat sector
|
|
211
|
+
uint32_t miniFatDataSectorNum; // sector num for mini fat data
|
|
212
|
+
{
|
|
213
|
+
// streamObject
|
|
214
|
+
const int tbl[] = {
|
|
215
|
+
iVersion, iDataSpaceMap, iStongEncryptionDataSpace, iPrimary, iEncryptionInfo
|
|
216
|
+
};
|
|
217
|
+
uint32_t pos = 0;
|
|
218
|
+
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
|
|
219
|
+
uint32_t n = getMiniFatSectorNum(dirs[tbl[i]].streamSize);
|
|
220
|
+
miniFatSectorNumVec.push_back(n);
|
|
221
|
+
dirs[tbl[i]].startingSectorLocation = pos;
|
|
222
|
+
pos += n;
|
|
223
|
+
}
|
|
224
|
+
miniFatNum = pos;
|
|
225
|
+
dprintf("miniFatNum=%d\n", miniFatNum);
|
|
226
|
+
miniFatDataSectorNum = getBlockNum(miniFatNum, (sectorSize / 64));
|
|
227
|
+
dprintf("miniFatDataSectorNum=%d\n", miniFatDataSectorNum);
|
|
228
|
+
if (getBlockNum(miniFatDataSectorNum, 128u) > numMiniFatSectors) throw cybozu::Exception("ms:makeLayout:large miniFatDataSectorNum") << miniFatDataSectorNum;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const uint32_t directoryEntrySectorNum = getBlockNum(static_cast<uint32_t>(cfb.dirs.size()), 4u);
|
|
232
|
+
const uint32_t encryptionPackageSectorNum = getBlockNum(static_cast<uint32_t>(dirs[iEncryptionPackage].streamSize), sectorSize);
|
|
233
|
+
const uint32_t contentSectorNum = numMiniFatSectors + directoryEntrySectorNum + miniFatDataSectorNum + encryptionPackageSectorNum;
|
|
234
|
+
dprintf("encryptionPackageSectorNum=%u\n", encryptionPackageSectorNum);
|
|
235
|
+
dprintf("contentSectorNum=%u\n", contentSectorNum);
|
|
236
|
+
|
|
237
|
+
uint32_t fatSectorNum;
|
|
238
|
+
uint32_t difatSectorNum;
|
|
239
|
+
|
|
240
|
+
detectSectorNum(difatSectorNum, fatSectorNum, contentSectorNum, sectorSize);
|
|
241
|
+
dprintf("fatSectorNum=%u\n", fatSectorNum);
|
|
242
|
+
dprintf("difatSectorNum=%u\n", difatSectorNum);
|
|
243
|
+
|
|
244
|
+
const uint32_t difatPos = 0;
|
|
245
|
+
const uint32_t fatPos = difatPos + difatSectorNum;
|
|
246
|
+
const uint32_t miniFatPos = fatPos + fatSectorNum;
|
|
247
|
+
const uint32_t directoryEntryPos = miniFatPos + numMiniFatSectors;
|
|
248
|
+
const uint32_t miniFatDataPos = directoryEntryPos + directoryEntrySectorNum;
|
|
249
|
+
const uint32_t encryptionPackagePos = miniFatDataPos + miniFatDataSectorNum;
|
|
250
|
+
|
|
251
|
+
header.firstDirectorySectorLocation = directoryEntryPos;
|
|
252
|
+
header.firstMiniFatSectorLocation = miniFatPos;
|
|
253
|
+
header.numMiniFatSectors = numMiniFatSectors;
|
|
254
|
+
|
|
255
|
+
// FAT
|
|
256
|
+
dirs[iRoot].startingSectorLocation = miniFatDataPos;
|
|
257
|
+
dirs[iRoot].streamSize = 64 * miniFatNum;
|
|
258
|
+
dirs[iEncryptionPackage].startingSectorLocation = encryptionPackagePos;
|
|
259
|
+
|
|
260
|
+
const uint32_t allSectorNum = difatSectorNum + fatSectorNum + contentSectorNum;
|
|
261
|
+
dprintf("allSectorNum=%u\n", allSectorNum);
|
|
262
|
+
data.resize(512 + allSectorNum * sectorSize);
|
|
263
|
+
|
|
264
|
+
for (uint32_t i = 0; i < std::min<uint32_t>(fatSectorNum, cfb::Header::firstNumDIFAT); i++) {
|
|
265
|
+
header.difat.push_back(fatPos + i);
|
|
266
|
+
}
|
|
267
|
+
header.numFatSectors = fatSectorNum;
|
|
268
|
+
header.numDifatSectors = difatSectorNum;
|
|
269
|
+
if (difatSectorNum > 0) {
|
|
270
|
+
header.firstDifatSectorLocation = difatPos;
|
|
271
|
+
}
|
|
272
|
+
header.write(&data[0]);
|
|
273
|
+
|
|
274
|
+
// make DIFAT
|
|
275
|
+
if (difatSectorNum > 0) {
|
|
276
|
+
makeDifat(&data[0], difatPos, difatSectorNum, fatSectorNum, sectorSize);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// make FAT
|
|
280
|
+
{
|
|
281
|
+
cfb::UintVec v;
|
|
282
|
+
for (uint32_t i = 0; i < difatSectorNum; i++) {
|
|
283
|
+
v.push_back(cfb::DIFSECT);
|
|
284
|
+
}
|
|
285
|
+
for (uint32_t i = 0; i < fatSectorNum; i++) {
|
|
286
|
+
v.push_back(cfb::FATSECT);
|
|
287
|
+
}
|
|
288
|
+
v.push_back(numMiniFatSectors);
|
|
289
|
+
v.push_back(directoryEntrySectorNum);
|
|
290
|
+
v.push_back(miniFatDataSectorNum);
|
|
291
|
+
v.push_back(encryptionPackageSectorNum);
|
|
292
|
+
char *p = &data[512 + fatPos * sectorSize];
|
|
293
|
+
makeFat(p, p + fatSectorNum * sectorSize, v);
|
|
294
|
+
}
|
|
295
|
+
// make mini FAT
|
|
296
|
+
{
|
|
297
|
+
char *p = &data[512 + miniFatPos * sectorSize];
|
|
298
|
+
makeFat(p, p + numMiniFatSectors * sectorSize, miniFatSectorNumVec);
|
|
299
|
+
}
|
|
300
|
+
for (size_t i = 0; i < dirs.size(); i++) {
|
|
301
|
+
dirs[i].write(&data[512 + directoryEntryPos * sectorSize + i * 128]);
|
|
302
|
+
}
|
|
303
|
+
for (size_t i = 0; i < dirs.size(); i++) {
|
|
304
|
+
const cfb::DirectoryEntry& dir = dirs[i];
|
|
305
|
+
const size_t size = dir.content.size();
|
|
306
|
+
if (size == 0) continue;
|
|
307
|
+
if (size <= 4096) {
|
|
308
|
+
memcpy(&data[512 + miniFatDataPos * sectorSize + dir.startingSectorLocation * 64], dir.content.c_str(), size);
|
|
309
|
+
} else {
|
|
310
|
+
memcpy(&data[512 + dir.startingSectorLocation * sectorSize], dir.content.c_str(), size);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
dprintf("data.size=%d\n", (int)data.size());
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
} // ms
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#include <stdlib.h>
|
|
3
|
+
/**
|
|
4
|
+
@file
|
|
5
|
+
@brief MS Office Crypt tool dll
|
|
6
|
+
@author herumi
|
|
7
|
+
Copyright (C) 2016 Cybozu Labs, Inc., all rights reserved.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
#ifdef _MSC_VER
|
|
11
|
+
#ifndef MSOC_DONT_AUTO_LINK
|
|
12
|
+
#pragma comment(lib, "msoc.lib")
|
|
13
|
+
#endif
|
|
14
|
+
#ifdef _WIN64
|
|
15
|
+
#define MSOC_DLL_EXPORT
|
|
16
|
+
#else
|
|
17
|
+
#define MSOC_DLL_EXPORT __stdcall
|
|
18
|
+
#endif
|
|
19
|
+
#else
|
|
20
|
+
#define MSOC_DLL_EXPORT
|
|
21
|
+
#endif
|
|
22
|
+
|
|
23
|
+
#ifdef __cplusplus
|
|
24
|
+
extern "C" {
|
|
25
|
+
#endif
|
|
26
|
+
|
|
27
|
+
#define MSOC_NOERR 0
|
|
28
|
+
#define MSOC_ERR_NOT_SUPPORT (-1)
|
|
29
|
+
#define MSOC_ERR_ALREADY_ENCRYPTED (-2)
|
|
30
|
+
#define MSOC_ERR_ALREADY_DECRYPTED (-3)
|
|
31
|
+
#define MSOC_ERR_BAD_PASSWORD (-4)
|
|
32
|
+
#define MSOC_ERR_BAD_PARAMETER (-5)
|
|
33
|
+
#define MSOC_ERR_SMALL_MAX_SIZE (-6)
|
|
34
|
+
#define MSOC_ERR_NO_MEMORY (-7)
|
|
35
|
+
#define MSOC_ERR_EXCEPTION (-8)
|
|
36
|
+
#define MSOC_ERR_TOO_LARGE_FILE (-9)
|
|
37
|
+
#define MSOC_ERR_INFILE_IS_EMPTY (-10)
|
|
38
|
+
#define MSOC_ERR_OUTFILE_IS_EMPTY (-11)
|
|
39
|
+
#define MSOC_ERR_PASS_IS_EMPTY (-12)
|
|
40
|
+
|
|
41
|
+
const char * MSOC_DLL_EXPORT MSOC_getErrMessage(int err);
|
|
42
|
+
|
|
43
|
+
typedef struct msoc_opt msoc_opt;
|
|
44
|
+
|
|
45
|
+
#ifdef _MSC_VER
|
|
46
|
+
/*
|
|
47
|
+
encrypt inFile and make outFile with pass(UTF-16 version)
|
|
48
|
+
@param outFile [in] encrypted MS Office file
|
|
49
|
+
@param inFile [in] plain MS Office file
|
|
50
|
+
@param pass [in] password
|
|
51
|
+
@param opt [inout] option (NULL is permitted)
|
|
52
|
+
use spinCount, secretKey of opt if set
|
|
53
|
+
*/
|
|
54
|
+
int MSOC_DLL_EXPORT MSOC_encrypt(const wchar_t *outFile, const wchar_t *inFile, const wchar_t *pass, const msoc_opt *opt);
|
|
55
|
+
/*
|
|
56
|
+
decrypt inFile and make outFile with pass(UTF-16 version)
|
|
57
|
+
@param outFile [in] plain MS Office file (NULL is permitted)
|
|
58
|
+
@param inFile [in] encrypted MS Office file
|
|
59
|
+
@param pass [in] password (NULL is permitted)
|
|
60
|
+
@param opt [inout] option (NULL is permitted)
|
|
61
|
+
|
|
62
|
+
secretKey of opt is used if pass is NULL
|
|
63
|
+
opt is set by the value of inFile if opt is not NULL
|
|
64
|
+
*/
|
|
65
|
+
int MSOC_DLL_EXPORT MSOC_decrypt(const wchar_t *outFile, const wchar_t *inFile, const wchar_t *pass, msoc_opt *opt);
|
|
66
|
+
#endif
|
|
67
|
+
/*
|
|
68
|
+
encrypt inFile and make outFile with pass(ASCII version : not UTF-8)
|
|
69
|
+
@param outFile [in] encrypted MS Office file
|
|
70
|
+
@param inFile [in] plain MS Office file
|
|
71
|
+
@param pass [in] password
|
|
72
|
+
@param opt [inout] option (NULL is permitted)
|
|
73
|
+
use spinCount, secretKey of opt if set
|
|
74
|
+
*/
|
|
75
|
+
int MSOC_DLL_EXPORT MSOC_encryptA(const char *outFile, const char *inFile, const char *pass, const msoc_opt *opt);
|
|
76
|
+
/*
|
|
77
|
+
decrypt inFile and make outFile with pass(ASCII version : not UTF-8)
|
|
78
|
+
@param outFile [in] plain MS Office file (NULL is permitted)
|
|
79
|
+
@param inFile [in] encrypted MS Office file
|
|
80
|
+
@param pass [in] password (NULL is permitted)
|
|
81
|
+
@param opt [inout] option (NULL is permitted)
|
|
82
|
+
|
|
83
|
+
secretKey of opt is used if pass is NULL
|
|
84
|
+
opt is set by the value of inFile if opt is not NULL
|
|
85
|
+
*/
|
|
86
|
+
int MSOC_DLL_EXPORT MSOC_decryptA(const char *outFile, const char *inFile, const char *pass, msoc_opt *opt);
|
|
87
|
+
|
|
88
|
+
/*
|
|
89
|
+
optType of msoc_opt
|
|
90
|
+
*/
|
|
91
|
+
#define MSOC_OPT_TYPE_SPIN_COUNT 1 // spinCount(int)
|
|
92
|
+
#define MSOC_OPT_TYPE_SECRET_KEY 2 // secretKey(hex ascii string) (eg. 0123ab)
|
|
93
|
+
|
|
94
|
+
/*
|
|
95
|
+
create default msoc_opt
|
|
96
|
+
spinCount = 100000
|
|
97
|
+
secretKey = "" (auto generated)
|
|
98
|
+
return pointer to msoc_opt
|
|
99
|
+
err if NULL
|
|
100
|
+
*/
|
|
101
|
+
msoc_opt * MSOC_DLL_EXPORT MSOC_createOpt(void);
|
|
102
|
+
/*
|
|
103
|
+
destroy msoc_opt
|
|
104
|
+
*/
|
|
105
|
+
void MSOC_DLL_EXPORT MSOC_destroyOpt(msoc_opt *msoc);
|
|
106
|
+
|
|
107
|
+
/*
|
|
108
|
+
get int value of optType of opt);
|
|
109
|
+
@param value [out] return value
|
|
110
|
+
*/
|
|
111
|
+
int MSOC_DLL_EXPORT MSOC_getInt(int *value, const msoc_opt *opt, int optType);
|
|
112
|
+
/*
|
|
113
|
+
set int value of optType of opt
|
|
114
|
+
*/
|
|
115
|
+
int MSOC_DLL_EXPORT MSOC_setInt(msoc_opt *opt, int optType, int value);
|
|
116
|
+
/*
|
|
117
|
+
get string value of optType of opt
|
|
118
|
+
@param str [out] return value
|
|
119
|
+
@param maxSize [in] buffer size of str including '\0'
|
|
120
|
+
*/
|
|
121
|
+
int MSOC_DLL_EXPORT MSOC_getStr(char *str, size_t maxSize, const msoc_opt *opt, int optType);
|
|
122
|
+
/*
|
|
123
|
+
set string value of optType of opt
|
|
124
|
+
*/
|
|
125
|
+
int MSOC_DLL_EXPORT MSOC_setStr(msoc_opt *opt, int optType, const char *str);
|
|
126
|
+
|
|
127
|
+
#ifdef __cplusplus
|
|
128
|
+
}
|
|
129
|
+
#endif
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
namespace ms { namespace resource {
|
|
3
|
+
const std::string Version("\x3c\x00\x00\x00\x4d\x00\x69\x00\x63\x00\x72\x00\x6f\x00\x73\x00\x6f\x00\x66\x00\x74\x00\x2e\x00\x43\x00\x6f\x00\x6e\x00\x74\x00\x61\x00\x69\x00\x6e\x00\x65\x00\x72\x00\x2e\x00\x44\x00\x61\x00\x74\x00\x61\x00\x53\x00\x70\x00\x61\x00\x63\x00\x65\x00\x73\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00", 76);
|
|
4
|
+
const std::string Primary("\x58\x00\x00\x00\x01\x00\x00\x00\x4c\x00\x00\x00\x7b\x00\x46\x00\x46\x00\x39\x00\x41\x00\x33\x00\x46\x00\x30\x00\x33\x00\x2d\x00\x35\x00\x36\x00\x45\x00\x46\x00\x2d\x00\x34\x00\x36\x00\x31\x00\x33\x00\x2d\x00\x42\x00\x44\x00\x44\x00\x35\x00\x2d\x00\x35\x00\x41\x00\x34\x00\x31\x00\x43\x00\x31\x00\x44\x00\x30\x00\x37\x00\x32\x00\x34\x00\x36\x00\x7d\x00\x4e\x00\x00\x00\x4d\x00\x69\x00\x63\x00\x72\x00\x6f\x00\x73\x00\x6f\x00\x66\x00\x74\x00\x2e\x00\x43\x00\x6f\x00\x6e\x00\x74\x00\x61\x00\x69\x00\x6e\x00\x65\x00\x72\x00\x2e\x00\x45\x00\x6e\x00\x63\x00\x72\x00\x79\x00\x70\x00\x74\x00\x69\x00\x6f\x00\x6e\x00\x54\x00\x72\x00\x61\x00\x6e\x00\x73\x00\x66\x00\x6f\x00\x72\x00\x6d\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00", 200);
|
|
5
|
+
const std::string DataSpaceMap("\x08\x00\x00\x00\x01\x00\x00\x00\x68\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x20\x00\x00\x00\x45\x00\x6e\x00\x63\x00\x72\x00\x79\x00\x70\x00\x74\x00\x65\x00\x64\x00\x50\x00\x61\x00\x63\x00\x6b\x00\x61\x00\x67\x00\x65\x00\x32\x00\x00\x00\x53\x00\x74\x00\x72\x00\x6f\x00\x6e\x00\x67\x00\x45\x00\x6e\x00\x63\x00\x72\x00\x79\x00\x70\x00\x74\x00\x69\x00\x6f\x00\x6e\x00\x44\x00\x61\x00\x74\x00\x61\x00\x53\x00\x70\x00\x61\x00\x63\x00\x65\x00\x00\x00", 112);
|
|
6
|
+
const std::string StrongEncryptionDataSpace("\x08\x00\x00\x00\x01\x00\x00\x00\x32\x00\x00\x00\x53\x00\x74\x00\x72\x00\x6f\x00\x6e\x00\x67\x00\x45\x00\x6e\x00\x63\x00\x72\x00\x79\x00\x70\x00\x74\x00\x69\x00\x6f\x00\x6e\x00\x54\x00\x72\x00\x61\x00\x6e\x00\x73\x00\x66\x00\x6f\x00\x72\x00\x6d\x00\x00\x00", 64);
|
|
7
|
+
} } // ms::resource
|