ooxml_crypt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (264) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +58 -0
  5. data/Rakefile +12 -0
  6. data/bin/console +15 -0
  7. data/bin/setup +8 -0
  8. data/ext/ooxml_crypt/extconf.rb +18 -0
  9. data/ext/ooxml_crypt/ooxml_crypt.c +27 -0
  10. data/ext/ooxml_crypt/ooxml_crypt.h +7 -0
  11. data/lib/ooxml_crypt/version.rb +5 -0
  12. data/lib/ooxml_crypt.rb +75 -0
  13. data/vendor/cybozulib/.github/workflows/main.yml +12 -0
  14. data/vendor/cybozulib/.gitignore +5 -0
  15. data/vendor/cybozulib/CMakeLists.txt +6 -0
  16. data/vendor/cybozulib/COPYRIGHT +27 -0
  17. data/vendor/cybozulib/Makefile +26 -0
  18. data/vendor/cybozulib/bin/libeay32.dll +0 -0
  19. data/vendor/cybozulib/bin/libmecab.dll +0 -0
  20. data/vendor/cybozulib/bin/ssleay32.dll +0 -0
  21. data/vendor/cybozulib/common.mk +116 -0
  22. data/vendor/cybozulib/common.props +25 -0
  23. data/vendor/cybozulib/cybozulib.sln +286 -0
  24. data/vendor/cybozulib/debug.props +14 -0
  25. data/vendor/cybozulib/include/cybozu/array.hpp +197 -0
  26. data/vendor/cybozulib/include/cybozu/atoi.hpp +238 -0
  27. data/vendor/cybozulib/include/cybozu/atomic.hpp +146 -0
  28. data/vendor/cybozulib/include/cybozu/base64.hpp +210 -0
  29. data/vendor/cybozulib/include/cybozu/benchmark.hpp +212 -0
  30. data/vendor/cybozulib/include/cybozu/bfd.hpp +105 -0
  31. data/vendor/cybozulib/include/cybozu/bit_operation.hpp +139 -0
  32. data/vendor/cybozulib/include/cybozu/bitvector.hpp +358 -0
  33. data/vendor/cybozulib/include/cybozu/condition_variable.hpp +113 -0
  34. data/vendor/cybozulib/include/cybozu/condition_variable_cs.hpp +74 -0
  35. data/vendor/cybozulib/include/cybozu/config.hpp +392 -0
  36. data/vendor/cybozulib/include/cybozu/critical_section.hpp +60 -0
  37. data/vendor/cybozulib/include/cybozu/crypto.hpp +321 -0
  38. data/vendor/cybozulib/include/cybozu/csucvector.hpp +624 -0
  39. data/vendor/cybozulib/include/cybozu/csv.hpp +294 -0
  40. data/vendor/cybozulib/include/cybozu/data_type.hpp +27 -0
  41. data/vendor/cybozulib/include/cybozu/endian.hpp +224 -0
  42. data/vendor/cybozulib/include/cybozu/env.hpp +63 -0
  43. data/vendor/cybozulib/include/cybozu/event.hpp +122 -0
  44. data/vendor/cybozulib/include/cybozu/exception.hpp +253 -0
  45. data/vendor/cybozulib/include/cybozu/file.hpp +626 -0
  46. data/vendor/cybozulib/include/cybozu/fmindex.hpp +291 -0
  47. data/vendor/cybozulib/include/cybozu/format.hpp +93 -0
  48. data/vendor/cybozulib/include/cybozu/frequency.hpp +264 -0
  49. data/vendor/cybozulib/include/cybozu/hash.hpp +67 -0
  50. data/vendor/cybozulib/include/cybozu/inttype.hpp +174 -0
  51. data/vendor/cybozulib/include/cybozu/itoa.hpp +336 -0
  52. data/vendor/cybozulib/include/cybozu/json.hpp +120 -0
  53. data/vendor/cybozulib/include/cybozu/line_stream.hpp +149 -0
  54. data/vendor/cybozulib/include/cybozu/link_libeay32.hpp +21 -0
  55. data/vendor/cybozulib/include/cybozu/link_mpir.hpp +18 -0
  56. data/vendor/cybozulib/include/cybozu/link_ssleay32.hpp +19 -0
  57. data/vendor/cybozulib/include/cybozu/log.hpp +237 -0
  58. data/vendor/cybozulib/include/cybozu/minixml.hpp +452 -0
  59. data/vendor/cybozulib/include/cybozu/mmap.hpp +143 -0
  60. data/vendor/cybozulib/include/cybozu/mutex.hpp +144 -0
  61. data/vendor/cybozulib/include/cybozu/nlp/mecab.hpp +96 -0
  62. data/vendor/cybozulib/include/cybozu/nlp/plsi.hpp +315 -0
  63. data/vendor/cybozulib/include/cybozu/nlp/random.hpp +74 -0
  64. data/vendor/cybozulib/include/cybozu/nlp/sparse.hpp +529 -0
  65. data/vendor/cybozulib/include/cybozu/nlp/svd.hpp +486 -0
  66. data/vendor/cybozulib/include/cybozu/nlp/tfidf.hpp +226 -0
  67. data/vendor/cybozulib/include/cybozu/nlp/top_score.hpp +75 -0
  68. data/vendor/cybozulib/include/cybozu/option.hpp +743 -0
  69. data/vendor/cybozulib/include/cybozu/parallel.hpp +88 -0
  70. data/vendor/cybozulib/include/cybozu/pcg.hpp +72 -0
  71. data/vendor/cybozulib/include/cybozu/process.hpp +324 -0
  72. data/vendor/cybozulib/include/cybozu/quit_signal_handler.hpp +66 -0
  73. data/vendor/cybozulib/include/cybozu/random_generator.hpp +144 -0
  74. data/vendor/cybozulib/include/cybozu/regex.hpp +463 -0
  75. data/vendor/cybozulib/include/cybozu/select8.hpp +279 -0
  76. data/vendor/cybozulib/include/cybozu/serializer.hpp +363 -0
  77. data/vendor/cybozulib/include/cybozu/sha1.hpp +209 -0
  78. data/vendor/cybozulib/include/cybozu/sha2.hpp +506 -0
  79. data/vendor/cybozulib/include/cybozu/siphash.hpp +105 -0
  80. data/vendor/cybozulib/include/cybozu/socket.hpp +785 -0
  81. data/vendor/cybozulib/include/cybozu/ssl.hpp +203 -0
  82. data/vendor/cybozulib/include/cybozu/stacktrace.hpp +291 -0
  83. data/vendor/cybozulib/include/cybozu/stream.hpp +269 -0
  84. data/vendor/cybozulib/include/cybozu/string.hpp +1746 -0
  85. data/vendor/cybozulib/include/cybozu/string_operation.hpp +365 -0
  86. data/vendor/cybozulib/include/cybozu/sucvector.hpp +378 -0
  87. data/vendor/cybozulib/include/cybozu/test.hpp +373 -0
  88. data/vendor/cybozulib/include/cybozu/thread.hpp +229 -0
  89. data/vendor/cybozulib/include/cybozu/time.hpp +281 -0
  90. data/vendor/cybozulib/include/cybozu/tls.hpp +115 -0
  91. data/vendor/cybozulib/include/cybozu/unordered_map.hpp +13 -0
  92. data/vendor/cybozulib/include/cybozu/unordered_set.hpp +13 -0
  93. data/vendor/cybozulib/include/cybozu/v128.hpp +376 -0
  94. data/vendor/cybozulib/include/cybozu/wavelet_matrix.hpp +345 -0
  95. data/vendor/cybozulib/include/cybozu/xorshift.hpp +189 -0
  96. data/vendor/cybozulib/include/cybozu/zlib.hpp +325 -0
  97. data/vendor/cybozulib/include/sais.hxx +364 -0
  98. data/vendor/cybozulib/misc/make_select8tbl.cpp +26 -0
  99. data/vendor/cybozulib/mk.bat +37 -0
  100. data/vendor/cybozulib/readme.md +29 -0
  101. data/vendor/cybozulib/release.props +12 -0
  102. data/vendor/cybozulib/sample/Makefile +30 -0
  103. data/vendor/cybozulib/sample/csucvector_smpl.cpp +42 -0
  104. data/vendor/cybozulib/sample/data/svd/org/test1.S +4 -0
  105. data/vendor/cybozulib/sample/data/svd/org/test1.U +4 -0
  106. data/vendor/cybozulib/sample/data/svd/org/test1.V +6 -0
  107. data/vendor/cybozulib/sample/data/svd/test1 +4 -0
  108. data/vendor/cybozulib/sample/data/svd/test2 +4 -0
  109. data/vendor/cybozulib/sample/desymbol.cpp +127 -0
  110. data/vendor/cybozulib/sample/exception_smpl.cpp +46 -0
  111. data/vendor/cybozulib/sample/fmindex_smpl.cpp +231 -0
  112. data/vendor/cybozulib/sample/log_smpl.cpp +19 -0
  113. data/vendor/cybozulib/sample/mecab_smpl.cpp +37 -0
  114. data/vendor/cybozulib/sample/option2_smpl.cpp +68 -0
  115. data/vendor/cybozulib/sample/option_smpl.cpp +42 -0
  116. data/vendor/cybozulib/sample/plsi_smpl.cpp +207 -0
  117. data/vendor/cybozulib/sample/proj/exception_smpl.vcproj +184 -0
  118. data/vendor/cybozulib/sample/proj/mecab_smpl.vcproj +184 -0
  119. data/vendor/cybozulib/sample/proj/ssl_smpl/ssl_smpl.vcxproj +85 -0
  120. data/vendor/cybozulib/sample/proj/ssl_smpl.vcproj +347 -0
  121. data/vendor/cybozulib/sample/proj/stacktrace_smpl/stacktrace_smpl.vcxproj +85 -0
  122. data/vendor/cybozulib/sample/proj/svd_smpl.vcproj +184 -0
  123. data/vendor/cybozulib/sample/quit_signal_handler.cpp +30 -0
  124. data/vendor/cybozulib/sample/serializer_smpl.cpp +196 -0
  125. data/vendor/cybozulib/sample/socket_smpl.cpp +82 -0
  126. data/vendor/cybozulib/sample/ssl_smpl.cpp +39 -0
  127. data/vendor/cybozulib/sample/stacktrace_smpl.cpp +52 -0
  128. data/vendor/cybozulib/sample/svd_bench_smpl.cpp +143 -0
  129. data/vendor/cybozulib/sample/svd_smpl.cpp +94 -0
  130. data/vendor/cybozulib/sample/wm_bench_smpl.cpp +182 -0
  131. data/vendor/cybozulib/sample/zlib_smpl.cpp +41 -0
  132. data/vendor/cybozulib/src/Makefile +8 -0
  133. data/vendor/cybozulib/src/base/Makefile +19 -0
  134. data/vendor/cybozulib/test/Makefile +12 -0
  135. data/vendor/cybozulib/test/base/Makefile +37 -0
  136. data/vendor/cybozulib/test/base/array_test.cpp +173 -0
  137. data/vendor/cybozulib/test/base/atoi_test.cpp +774 -0
  138. data/vendor/cybozulib/test/base/atomic_test.cpp +49 -0
  139. data/vendor/cybozulib/test/base/base64_test.cpp +113 -0
  140. data/vendor/cybozulib/test/base/bit_operation_test.cpp +134 -0
  141. data/vendor/cybozulib/test/base/bitvector_test.cpp +204 -0
  142. data/vendor/cybozulib/test/base/condition_variable_cs_test.cpp +92 -0
  143. data/vendor/cybozulib/test/base/condition_variable_test.cpp +88 -0
  144. data/vendor/cybozulib/test/base/config_test.cpp +236 -0
  145. data/vendor/cybozulib/test/base/crypto_test.cpp +122 -0
  146. data/vendor/cybozulib/test/base/csucvector_test.cpp +63 -0
  147. data/vendor/cybozulib/test/base/csv_test.cpp +182 -0
  148. data/vendor/cybozulib/test/base/data/a.xml +26 -0
  149. data/vendor/cybozulib/test/base/endian_test.cpp +56 -0
  150. data/vendor/cybozulib/test/base/env_test.cpp +22 -0
  151. data/vendor/cybozulib/test/base/event_test.cpp +41 -0
  152. data/vendor/cybozulib/test/base/file_test.cpp +233 -0
  153. data/vendor/cybozulib/test/base/fmindex_test.cpp +118 -0
  154. data/vendor/cybozulib/test/base/format_test.cpp +12 -0
  155. data/vendor/cybozulib/test/base/frequency_test.cpp +104 -0
  156. data/vendor/cybozulib/test/base/itoa_test.cpp +522 -0
  157. data/vendor/cybozulib/test/base/line_stream_test.cpp +208 -0
  158. data/vendor/cybozulib/test/base/mecab_test.cpp +41 -0
  159. data/vendor/cybozulib/test/base/minixml_test.cpp +103 -0
  160. data/vendor/cybozulib/test/base/mmap_test.cpp +15 -0
  161. data/vendor/cybozulib/test/base/option_test.cpp +487 -0
  162. data/vendor/cybozulib/test/base/parallel_test.cpp +48 -0
  163. data/vendor/cybozulib/test/base/proj/array_test/array_test.vcxproj +86 -0
  164. data/vendor/cybozulib/test/base/proj/atoi_test/atoi_test.vcxproj +86 -0
  165. data/vendor/cybozulib/test/base/proj/atomic_test/atomic_test.vcxproj +86 -0
  166. data/vendor/cybozulib/test/base/proj/base64_test/base64_test.vcxproj +86 -0
  167. data/vendor/cybozulib/test/base/proj/condition_variable_cs_test/condition_variable_cs_test.vcxproj +86 -0
  168. data/vendor/cybozulib/test/base/proj/condition_variable_test/condition_variable_test.vcxproj +86 -0
  169. data/vendor/cybozulib/test/base/proj/config_test/config_test.vcxproj +86 -0
  170. data/vendor/cybozulib/test/base/proj/csv_test/csv_test.vcxproj +86 -0
  171. data/vendor/cybozulib/test/base/proj/endian_test/endian_test.vcxproj +86 -0
  172. data/vendor/cybozulib/test/base/proj/env_test/env_test.vcxproj +86 -0
  173. data/vendor/cybozulib/test/base/proj/event_test/event_test.vcxproj +86 -0
  174. data/vendor/cybozulib/test/base/proj/file_test/file_test.vcxproj +86 -0
  175. data/vendor/cybozulib/test/base/proj/itoa_test/itoa_test.vcxproj +86 -0
  176. data/vendor/cybozulib/test/base/proj/mecab_test/mecab_test.vcxproj +88 -0
  177. data/vendor/cybozulib/test/base/proj/minixml_test/minixml_test.vcxproj +86 -0
  178. data/vendor/cybozulib/test/base/proj/mmap_test/mmap_test.vcxproj +86 -0
  179. data/vendor/cybozulib/test/base/proj/serializer_test/serializer_test.vcxproj +86 -0
  180. data/vendor/cybozulib/test/base/proj/sha1_test/sha1_test.vcxproj +86 -0
  181. data/vendor/cybozulib/test/base/proj/stream_test/stream_test.vcxproj +86 -0
  182. data/vendor/cybozulib/test/base/proj/string_operation_test/string_operation_test.vcxproj +86 -0
  183. data/vendor/cybozulib/test/base/proj/string_test/string_test.vcxproj +86 -0
  184. data/vendor/cybozulib/test/base/proj/thread_test/thread_test.vcxproj +86 -0
  185. data/vendor/cybozulib/test/base/proj/time_test/time_test.vcxproj +86 -0
  186. data/vendor/cybozulib/test/base/proj/tls_test/tls_test.vcxproj +86 -0
  187. data/vendor/cybozulib/test/base/proj/zlib_test/zlib_test.vcxproj +86 -0
  188. data/vendor/cybozulib/test/base/random_generator_test.cpp +28 -0
  189. data/vendor/cybozulib/test/base/regex_test.cpp +74 -0
  190. data/vendor/cybozulib/test/base/serializer_test.cpp +483 -0
  191. data/vendor/cybozulib/test/base/sha1_test.cpp +61 -0
  192. data/vendor/cybozulib/test/base/sha2_test.cpp +191 -0
  193. data/vendor/cybozulib/test/base/siphash_test.cpp +33 -0
  194. data/vendor/cybozulib/test/base/socket_test.cpp +76 -0
  195. data/vendor/cybozulib/test/base/stream_test.cpp +101 -0
  196. data/vendor/cybozulib/test/base/string_operation_test.cpp +340 -0
  197. data/vendor/cybozulib/test/base/string_test.cpp +1705 -0
  198. data/vendor/cybozulib/test/base/sucvector_test.cpp +312 -0
  199. data/vendor/cybozulib/test/base/thread_test.cpp +62 -0
  200. data/vendor/cybozulib/test/base/time_test.cpp +164 -0
  201. data/vendor/cybozulib/test/base/tls_test.cpp +50 -0
  202. data/vendor/cybozulib/test/base/wavelet_matrix_test.cpp +145 -0
  203. data/vendor/cybozulib/test/base/zlib_test.cpp +371 -0
  204. data/vendor/cybozulib/test/nlp/Makefile +27 -0
  205. data/vendor/cybozulib/test/nlp/proj/random_test.vcproj +184 -0
  206. data/vendor/cybozulib/test/nlp/proj/sparse_test.vcproj +184 -0
  207. data/vendor/cybozulib/test/nlp/proj/svd_test.vcproj +184 -0
  208. data/vendor/cybozulib/test/nlp/random_test.cpp +62 -0
  209. data/vendor/cybozulib/test/nlp/sparse_test.cpp +347 -0
  210. data/vendor/cybozulib/test/nlp/svd_test.cpp +234 -0
  211. data/vendor/cybozulib/test/nlp/top_score_test.cpp +40 -0
  212. data/vendor/cybozulib/tool/create_vcproj.py +186 -0
  213. data/vendor/cybozulib/tool/vcproj_tmpl.py +185 -0
  214. data/vendor/msoffice/COPYRIGHT +27 -0
  215. data/vendor/msoffice/Makefile +29 -0
  216. data/vendor/msoffice/bin/64/msoc.dll +0 -0
  217. data/vendor/msoffice/bin/64/msocsample.exe +0 -0
  218. data/vendor/msoffice/bin/64/msoffice-crypt.exe +0 -0
  219. data/vendor/msoffice/bin/msoc.dll +0 -0
  220. data/vendor/msoffice/bin/msocsample.exe +0 -0
  221. data/vendor/msoffice/bin/msoffice-crypt.exe +0 -0
  222. data/vendor/msoffice/common.mk +71 -0
  223. data/vendor/msoffice/common.props +26 -0
  224. data/vendor/msoffice/debug.props +14 -0
  225. data/vendor/msoffice/include/attack.hpp +211 -0
  226. data/vendor/msoffice/include/cfb.hpp +777 -0
  227. data/vendor/msoffice/include/crypto_util.hpp +450 -0
  228. data/vendor/msoffice/include/custom_sha1.hpp +342 -0
  229. data/vendor/msoffice/include/decode.hpp +240 -0
  230. data/vendor/msoffice/include/encode.hpp +221 -0
  231. data/vendor/msoffice/include/make_dataspace.hpp +316 -0
  232. data/vendor/msoffice/include/msoc.h +129 -0
  233. data/vendor/msoffice/include/resource.hpp +7 -0
  234. data/vendor/msoffice/include/standard_encryption.hpp +145 -0
  235. data/vendor/msoffice/include/uint32vec.hpp +179 -0
  236. data/vendor/msoffice/include/util.hpp +212 -0
  237. data/vendor/msoffice/lib/.emptydir +0 -0
  238. data/vendor/msoffice/misc/decrypt-xls.vbs +46 -0
  239. data/vendor/msoffice/mk.bat +1 -0
  240. data/vendor/msoffice/mkdll.bat +3 -0
  241. data/vendor/msoffice/msoc.def +13 -0
  242. data/vendor/msoffice/msocsample.py +178 -0
  243. data/vendor/msoffice/msoffice12.sln +31 -0
  244. data/vendor/msoffice/readme.md +110 -0
  245. data/vendor/msoffice/release.props +28 -0
  246. data/vendor/msoffice/src/Makefile +19 -0
  247. data/vendor/msoffice/src/attack.cpp +124 -0
  248. data/vendor/msoffice/src/cfb_test.cpp +77 -0
  249. data/vendor/msoffice/src/minisample.c +54 -0
  250. data/vendor/msoffice/src/msocdll.cpp +276 -0
  251. data/vendor/msoffice/src/msocsample.c +136 -0
  252. data/vendor/msoffice/src/msoffice-crypt.cpp +219 -0
  253. data/vendor/msoffice/src/proj/attack/attack.vcxproj +88 -0
  254. data/vendor/msoffice/src/proj/main/msoffice-crypt.vcxproj +88 -0
  255. data/vendor/msoffice/src/sha1.cpp +234 -0
  256. data/vendor/msoffice/test/Makefile +20 -0
  257. data/vendor/msoffice/test/cfb_test.cpp +74 -0
  258. data/vendor/msoffice/test/hash_test.cpp +59 -0
  259. data/vendor/msoffice/test/proj/cfb/cfb_test.vcxproj +90 -0
  260. data/vendor/msoffice/test/proj/hash/hash_test.vcxproj +90 -0
  261. data/vendor/msoffice/test/sampl.bat +8 -0
  262. data/vendor/msoffice/test_all.py +46 -0
  263. data/vendor/update +4 -0
  264. metadata +351 -0
@@ -0,0 +1,145 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief MS Office 2007 Standard Encryption decoder
5
+ Copyright (C) 2012 Cybozu Labs, Inc., all rights reserved.
6
+ */
7
+ #include "util.hpp"
8
+ #include <cybozu/crypto.hpp>
9
+ #include <cybozu/string.hpp>
10
+ #include <cybozu/endian.hpp>
11
+
12
+ namespace ms {
13
+
14
+ /*
15
+ Office 2007
16
+ [MS-OFFCRYPTO] 2.3.2
17
+ */
18
+ struct EncryptionHeader {
19
+ uint32_t flags;
20
+ uint32_t sizeExtra;
21
+ uint32_t algId;
22
+ uint32_t algIdHash;
23
+ uint32_t keySize;
24
+ uint32_t providerType;
25
+ uint32_t reserved1;
26
+ uint32_t reserved2;
27
+ cybozu::String16 cspName;
28
+ cybozu::crypto::Cipher::Name cipherName;
29
+ enum AlgoMode {
30
+ AlgoAny,
31
+ AlgoRC4,
32
+ Algo128AES,
33
+ Algo192AES,
34
+ Algo256AES
35
+ };
36
+ void analyze(const char *data, size_t dataSize)
37
+ {
38
+ if (dataSize < 32) {
39
+ throw cybozu::Exception("ms:EncryptionHeader:shot dataSize") << dataSize;
40
+ }
41
+ uint32_t *tbl[] = {
42
+ &flags, &sizeExtra, &algId, &algIdHash, &keySize, &providerType,
43
+ &reserved1, &reserved2
44
+ };
45
+ const size_t paraNum = 8;
46
+ assert(CYBOZU_NUM_OF_ARRAY(tbl) == paraNum);
47
+ for (size_t i = 0; i < paraNum; i++) {
48
+ *(tbl[i]) = cybozu::Get32bitAsLE(data + i * 4);
49
+ }
50
+ cybozu::Char16 c = cybozu::Get16bitAsLE(data + dataSize - 2);
51
+ if (c != 0) {
52
+ throw cybozu::Exception("ms:EncryptionHeader:cspName is not NULL terminated") << c;
53
+ }
54
+ cspName.assign(reinterpret_cast<const cybozu::Char16*>(data + paraNum * 4));
55
+ MS_ASSERT_EQUAL(sizeExtra, 0u);
56
+ switch (algId) {
57
+ // case 0x0000: cipherName = AlgoAny; break;
58
+ // case 0x6801: cipherName = AlgoRC4; break;
59
+ case 0x660e: cipherName = cybozu::crypto::Cipher::N_AES128_ECB; break;
60
+ case 0x660f: cipherName = cybozu::crypto::Cipher::N_AES192_ECB; break;
61
+ case 0x6610: cipherName = cybozu::crypto::Cipher::N_AES256_ECB; break;
62
+ default:
63
+ throw cybozu::Exception("ms:EncryptionHeader:bad algId") << algId;
64
+ }
65
+ // verify keySize
66
+ MS_ASSERT_EQUAL(keySize, cybozu::crypto::Cipher::getSize(cipherName));
67
+ MS_ASSERT(algIdHash == 0u || algIdHash == 0x8004);
68
+ // verify providerType
69
+ MS_ASSERT_EQUAL(providerType, 0x18u);
70
+ MS_ASSERT_EQUAL(reserved2, 0u);
71
+ }
72
+ void put() const
73
+ {
74
+ printf("flags = %08x\n", flags);
75
+ printf("sizeExtra = %u\n", sizeExtra);
76
+ printf("algId = %08x\n", algId);
77
+ printf("algIdHash = %08x\n", algIdHash);
78
+ printf("keySize = %u\n", keySize);
79
+ printf("providerType = %08x\n", providerType);
80
+ printf("cspName = %s\n", cybozu::ToUtf8(cspName).c_str());
81
+ }
82
+ };
83
+
84
+ /*
85
+ [MS-OFFCRYPTO] 2.3.3
86
+ */
87
+ struct EncryptionVerifier {
88
+ static const size_t bufSize = 16;
89
+ uint32_t saltSize;
90
+ std::string salt; // bufSize
91
+ std::string encryptedVerifier; // bufSize
92
+ uint32_t verifierHashSize;
93
+ std::string encryptedVerifierHash;
94
+
95
+
96
+ void analyze(const char *data, size_t dataSize)
97
+ {
98
+ const size_t saltSizePos = 0;
99
+ const size_t saltPos = saltSizePos + sizeof(saltSize);
100
+ const size_t encryptedVerifierPos = saltPos + bufSize;
101
+ const size_t verifierHashSizePos = encryptedVerifierPos + bufSize;
102
+ const size_t encryptedVerifierHashPos = verifierHashSizePos + sizeof(verifierHashSize);
103
+
104
+ if (dataSize < encryptedVerifierHashPos) {
105
+ throw cybozu::Exception("ms:EncryptionVerifier:dataSize is too small") << dataSize << encryptedVerifierHashPos;
106
+ }
107
+ saltSize = cybozu::Get32bitAsLE(data + saltSizePos);
108
+ MS_ASSERT_EQUAL(saltSize, 0x10u);
109
+ salt.assign(data + saltPos, bufSize);
110
+ encryptedVerifier.assign(data + encryptedVerifierPos, bufSize);
111
+ verifierHashSize = cybozu::Get32bitAsLE(data + verifierHashSizePos);
112
+ if (verifierHashSize + sizeof(verifierHashSize) > dataSize) {
113
+ throw cybozu::Exception("ms:EncryptionVerifier:bad verifierHashSize") << verifierHashSize << dataSize;
114
+ }
115
+ encryptedVerifierHash.assign(data + encryptedVerifierHashPos, data + dataSize);
116
+ }
117
+ /*
118
+ 2.3.4.7
119
+ */
120
+ std::string getEncryptionKey(const std::string& pass) const
121
+ {
122
+ cybozu::crypto::Hash::Name hashName = cybozu::crypto::Hash::N_SHA1;
123
+ const size_t hashSize = cybozu::crypto::Hash::getSize(hashName);
124
+ std::string h = hashPassword(hashName, salt, pass, 50000);
125
+ const std::string zero4byte(4, 0); // 0x00000000;
126
+ h = cybozu::crypto::Hash::digest(hashName, h + zero4byte);
127
+
128
+ std::string t(64, 0x36); // '\x36...\x36'(64 times)
129
+ for (size_t i = 0; i < hashSize; i++) {
130
+ t[i] ^= h[i];
131
+ }
132
+ h = cybozu::crypto::Hash::digest(hashName, t);
133
+ return h;
134
+ }
135
+ void put() const
136
+ {
137
+ printf("saltSize = %d\n", saltSize);
138
+ printf("salt = "); ms::dump(salt);
139
+ printf("encryptedVerifier = "); ms::dump(encryptedVerifier);
140
+ printf("verifierHashSize = %d\n", verifierHashSize);
141
+ printf("encryptedVerifierHash = "); ms::dump(encryptedVerifierHash);
142
+ }
143
+ };
144
+
145
+ } // ms
@@ -0,0 +1,179 @@
1
+ #pragma once
2
+ /**
3
+ Copyright (C) 2012 Cybozu Labs, Inc., all rights reserved.
4
+ */
5
+ #include <cybozu/inttype.hpp>
6
+ #include <stdio.h>
7
+ #include <assert.h>
8
+ #ifdef _WIN32
9
+ #include <winsock2.h>
10
+ #include <intrin.h>
11
+ #else
12
+ #ifdef __linux__
13
+ #include <x86intrin.h>
14
+ #else
15
+ #include <emmintrin.h>
16
+ #endif
17
+ #endif
18
+
19
+ template<size_t size>
20
+ struct Uint32VecT {
21
+ };
22
+
23
+ #ifdef __AVX2__
24
+ #define UINT32VEC_USE_AVX2
25
+ template<>
26
+ struct Uint32VecT<8> {
27
+ static const int size = 8;
28
+ __m256i x_;
29
+ Uint32VecT()
30
+ {
31
+ }
32
+ Uint32VecT(__m256i x)
33
+ : x_(x)
34
+ {
35
+ }
36
+ Uint32VecT(uint32_t x)
37
+ : x_(_mm256_set1_epi32(x))
38
+ {
39
+ }
40
+ // m = [x7:x6:x5:x4:x3:x2:x1:x0]
41
+ Uint32VecT(uint32_t x7, uint32_t x6, uint32_t x5, uint32_t x4, uint32_t x3, uint32_t x2, uint32_t x1, uint32_t x0)
42
+ : x_(_mm256_set_epi32(x7, x6, x5, x4, x3, x2, x1, x0))
43
+ {
44
+ }
45
+ // m = [x7:x6:x5:x4:x3:x2:x1:x0]
46
+ void set(uint32_t x7, uint32_t x6, uint32_t x5, uint32_t x4, uint32_t x3, uint32_t x2, uint32_t x1, uint32_t x0)
47
+ {
48
+ x_ = _mm256_set_epi32(x7, x6, x5, x4, x3, x2, x1, x0);
49
+ }
50
+ void clear()
51
+ {
52
+ *this = _mm256_setzero_si256();
53
+ }
54
+ void put(const char *msg = 0) const
55
+ {
56
+ uint32_t v[8];
57
+ memcpy(&v, &x_, sizeof(v));
58
+ if (msg) printf("%s", msg);
59
+ printf("%08x:%08x:%08x:%08x", v[3], v[2], v[1], v[0]);
60
+ if (msg) putchar('\n');
61
+ }
62
+ };
63
+
64
+ inline Uint32VecT<8> operator<<(const Uint32VecT<8>& a, const int n)
65
+ {
66
+ return _mm256_slli_epi32(a.x_, n);
67
+ }
68
+ inline Uint32VecT<8> operator>>(const Uint32VecT<8>& a, const int n)
69
+ {
70
+ return _mm256_srli_epi32(a.x_, n);
71
+ }
72
+
73
+ inline Uint32VecT<8> operator+(const Uint32VecT<8>& a, const Uint32VecT<8>& b)
74
+ {
75
+ return _mm256_add_epi32(a.x_, b.x_);
76
+ }
77
+
78
+ // return (~a) & b
79
+ inline Uint32VecT<8> andn(const Uint32VecT<8>& a, const Uint32VecT<8>& b)
80
+ {
81
+ return _mm256_andnot_si256(a.x_, b.x_);
82
+ }
83
+
84
+ inline Uint32VecT<8> operator|(const Uint32VecT<8>& a, const Uint32VecT<8>& b)
85
+ {
86
+ return _mm256_or_si256(a.x_, b.x_);
87
+ }
88
+
89
+ inline Uint32VecT<8> operator&(const Uint32VecT<8>& a, const Uint32VecT<8>& b)
90
+ {
91
+ return _mm256_and_si256(a.x_, b.x_);
92
+ }
93
+
94
+ inline Uint32VecT<8> operator^(const Uint32VecT<8>& a, const Uint32VecT<8>& b)
95
+ {
96
+ return _mm256_xor_si256(a.x_, b.x_);
97
+ }
98
+
99
+ typedef Uint32VecT<8> Uint32Vec;
100
+
101
+ #else
102
+
103
+ template<>
104
+ struct Uint32VecT<4> {
105
+ static const int size = 4;
106
+ __m128i x_;
107
+ Uint32VecT()
108
+ {
109
+ }
110
+ Uint32VecT(__m128i x)
111
+ : x_(x)
112
+ {
113
+ }
114
+ Uint32VecT(uint32_t x)
115
+ : x_(_mm_set1_epi32(x))
116
+ {
117
+ }
118
+ // m = [x3:x2:x1:x0]
119
+ Uint32VecT(uint32_t x3, uint32_t x2, uint32_t x1, uint32_t x0)
120
+ : x_(_mm_set_epi32(x3, x2, x1, x0))
121
+ {
122
+ }
123
+ // m = [x3:x2:x1:x0]
124
+ void set(uint32_t x3, uint32_t x2, uint32_t x1, uint32_t x0)
125
+ {
126
+ x_ = _mm_set_epi32(x3, x2, x1, x0);
127
+ }
128
+ void clear()
129
+ {
130
+ *this = _mm_setzero_si128();
131
+ }
132
+ void put(const char *msg = 0) const
133
+ {
134
+ uint32_t v[4];
135
+ memcpy(&v, &x_, sizeof(v));
136
+ if (msg) printf("%s", msg);
137
+ printf("%08x:%08x:%08x:%08x", v[3], v[2], v[1], v[0]);
138
+ if (msg) putchar('\n');
139
+ }
140
+ };
141
+
142
+ inline Uint32VecT<4> operator<<(const Uint32VecT<4>& a, const int n)
143
+ {
144
+ return _mm_slli_epi32(a.x_, n);
145
+ }
146
+ inline Uint32VecT<4> operator>>(const Uint32VecT<4>& a, const int n)
147
+ {
148
+ return _mm_srli_epi32(a.x_, n);
149
+ }
150
+
151
+ inline Uint32VecT<4> operator+(const Uint32VecT<4>& a, const Uint32VecT<4>& b)
152
+ {
153
+ return _mm_add_epi32(a.x_, b.x_);
154
+ }
155
+
156
+ // return (~a) & b
157
+ inline Uint32VecT<4> andn(const Uint32VecT<4>& a, const Uint32VecT<4>& b)
158
+ {
159
+ return _mm_andnot_si128(a.x_, b.x_);
160
+ }
161
+
162
+ inline Uint32VecT<4> operator|(const Uint32VecT<4>& a, const Uint32VecT<4>& b)
163
+ {
164
+ return _mm_or_si128(a.x_, b.x_);
165
+ }
166
+
167
+ inline Uint32VecT<4> operator&(const Uint32VecT<4>& a, const Uint32VecT<4>& b)
168
+ {
169
+ return _mm_and_si128(a.x_, b.x_);
170
+ }
171
+
172
+ inline Uint32VecT<4> operator^(const Uint32VecT<4>& a, const Uint32VecT<4>& b)
173
+ {
174
+ return _mm_xor_si128(a.x_, b.x_);
175
+ }
176
+
177
+ typedef Uint32VecT<4> Uint32Vec;
178
+ #endif
179
+
@@ -0,0 +1,212 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief MS Office encryption util
5
+ Copyright (C) 2012 Cybozu Labs, Inc., all rights reserved.
6
+ */
7
+ #include <string>
8
+ #include <cybozu/atoi.hpp>
9
+ #include <cybozu/itoa.hpp>
10
+ #include <cybozu/base64.hpp>
11
+ #include <cybozu/exception.hpp>
12
+ #include <cybozu/string.hpp>
13
+ #include <stdarg.h>
14
+
15
+ namespace ms {
16
+
17
+ enum Format {
18
+ fZip, // not encrypted(maybe)
19
+ fCfb, // encrypted(maybe)
20
+ fUnknown
21
+ };
22
+
23
+ inline int& debugInstance()
24
+ {
25
+ static int debug;
26
+ return debug;
27
+ }
28
+
29
+ inline void setDebug(int level)
30
+ {
31
+ debugInstance() = level;
32
+ }
33
+
34
+ inline bool& putSecretKeyInstance()
35
+ {
36
+ static bool putSecretKey;
37
+ return putSecretKey;
38
+ }
39
+
40
+ inline bool isDebug(int level = 1) { return debugInstance() > level; }
41
+
42
+ inline void dprintf(const char *format, ...)
43
+ {
44
+ if (!isDebug()) return;
45
+ va_list args;
46
+ va_start(args, format);
47
+ #ifdef __GNUC__
48
+ #pragma GCC diagnostic push
49
+ #pragma GCC diagnostic ignored "-Wformat-nonliteral"
50
+ #endif
51
+ vprintf(format, args);
52
+ #ifdef __GNUC__
53
+ #pragma GCC diagnostic pop
54
+ #endif
55
+ va_end(args);
56
+ }
57
+
58
+ template<class T>
59
+ T getBlockNum(T size, T block)
60
+ {
61
+ return (size + block - 1) / block;
62
+ }
63
+ /*
64
+ round up size to multiple of block
65
+ */
66
+ template<class T>
67
+ T RoundUp(T size, T block)
68
+ {
69
+ return getBlockNum(size, block) * block;
70
+ }
71
+
72
+ inline void dump(const void *p, size_t size, bool putColon = true)
73
+ {
74
+ const uint8_t *str = reinterpret_cast<const uint8_t*>(p);
75
+ for (size_t i = 0; i < size; i++) {
76
+ printf("%02X", (unsigned char)str[i]);
77
+ if (putColon) putchar(':');
78
+ }
79
+ printf("\n");
80
+ }
81
+
82
+ inline void dump(const std::string& str, bool putColon = true)
83
+ {
84
+ dump(str.c_str(), str.size(), putColon);
85
+ }
86
+
87
+ inline void dump16(const std::string& str, size_t size = size_t(-1))
88
+ {
89
+ if (size > str.size()) size = str.size();
90
+ for (size_t i = 0; i < size; i++) {
91
+ printf("%02X ", (unsigned char)str[i]);
92
+ if ((i % 16) == 15) putchar('\n');
93
+ }
94
+ printf("\n");
95
+ }
96
+
97
+ inline void saveFile(const std::string& file, const std::string& str)
98
+ {
99
+ cybozu::File f(file, std::ios::out);
100
+ f.write(str.c_str(), str.size());
101
+ }
102
+
103
+ inline std::string fromHex(const std::string& hex, bool skipColon = false)
104
+ {
105
+ const size_t n = hex.size();
106
+ if (!skipColon && n & 1) throw cybozu::Exception("ms:fromhex:odd length") << n;
107
+ std::string ret;
108
+ for (size_t i = 0; i < n; i += 2) {
109
+ if (skipColon && hex[i] == ':') {
110
+ i++;
111
+ if (i == n) break;
112
+ }
113
+ unsigned char c = cybozu::hextoi(&hex[i], 2);
114
+ ret += (char)c;
115
+ }
116
+ return ret;
117
+ }
118
+
119
+ inline std::string hex(const std::string& str)
120
+ {
121
+ const size_t n = str.size();
122
+ std::string ret;
123
+ ret.reserve(n * 2);
124
+ for (size_t i = 0; i < n; i++) {
125
+ ret += cybozu::itohex(static_cast<uint8_t>(str[i]));
126
+ }
127
+ return ret;
128
+ }
129
+
130
+ inline bool dumpDiff(const std::string& x, const std::string& y)
131
+ {
132
+ printf("dumpDiff %d %d\n", (int)x.size(), (int)y.size());
133
+ int ccc = 0;
134
+ for (int i = 0; i < (int)std::min(x.size(), y.size()); i++) {
135
+ char c = x[i];
136
+ char d = y[i];
137
+ if (c != d) {
138
+ printf("%8d(0x%x) %02x %02x\n", i, i, (uint8_t)c, (uint8_t)d);
139
+ ccc++;
140
+ if (ccc == 100) break;
141
+ }
142
+ }
143
+ if (ccc == 0) printf("no diff\n");
144
+ return ccc == 0;
145
+ }
146
+
147
+ inline std::string dec64(const std::string& str)
148
+ {
149
+ std::string ret;
150
+ cybozu::StringInputStream is(str);
151
+ cybozu::StringOutputStream os(ret);
152
+ cybozu::DecodeFromBase64(os, is);
153
+ return ret;
154
+ }
155
+
156
+ inline std::string enc64(const std::string& str)
157
+ {
158
+ std::string ret;
159
+ cybozu::StringInputStream is(str);
160
+ cybozu::StringOutputStream os(ret);
161
+ cybozu::EncodeToBase64(os, is, 128, cybozu::base64::noEndLine);
162
+ return ret;
163
+ }
164
+
165
+ inline void validate(bool isOK, const char *msg, const char *file, int line)
166
+ {
167
+ if (!isOK) {
168
+ throw cybozu::Exception("ms:cfb:validate") << msg << file << line;
169
+ }
170
+ }
171
+
172
+ template<class L, class R>
173
+ void validateEqual(const L& a, const R& b, const char *pa, const char *pb, const char *file, int line)
174
+ {
175
+ if (a != b) {
176
+ throw cybozu::Exception("ms:cfb:validateEqual") << pa << a << pb << b << file << line;
177
+ }
178
+ }
179
+
180
+ inline std::string toHex(const char *p, size_t size)
181
+ {
182
+ std::string ret;
183
+ for (size_t i = 0; i < size; i++) {
184
+ cybozu::itohex(ret, static_cast<uint8_t>(p[i]), false);
185
+ }
186
+ return ret;
187
+ }
188
+
189
+ inline Format DetectFormat(const char *data, size_t dataSize)
190
+ {
191
+ if (dataSize < 8) throw cybozu::Exception("ms:detectFormat:too small") << dataSize;
192
+ if (memcmp(data, "PK\x03\x04", 4) == 0) return fZip;
193
+ if (memcmp(data, "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1", 8) == 0) return fCfb;
194
+ throw cybozu::Exception("ms:DetectFormat:unknown format") << hex(std::string(data, 8));
195
+ }
196
+
197
+ inline std::string Char16toChar8(const cybozu::String16& w)
198
+ {
199
+ std::string s;
200
+ for (size_t i = 0; i < w.size(); i++) {
201
+ cybozu::Char16 c = w[i];
202
+ s += char(c & 0xff);
203
+ s += char(c >> 8);
204
+ }
205
+ return s;
206
+ }
207
+
208
+ #define MS_ASSERT(x) ms::validate((x), #x, __FILE__, __LINE__)
209
+ #define MS_ASSERT_EQUAL(x, y) ms::validateEqual((x), (y), #x, #y, __FILE__, __LINE__)
210
+ #define PUT(x) printf(#x "=%s(%d)\n", ms::hex(x).c_str(), (int)x.size())
211
+
212
+ } // ms
File without changes
@@ -0,0 +1,46 @@
1
+
2
+ ' decrypt Excel file with password
3
+
4
+ Option Explicit
5
+ Dim g_fs
6
+ set g_fs = wscript.createObject("scripting.fileSystemObject")
7
+
8
+ ParseAndRun wscript.arguments
9
+ wscript.quit
10
+
11
+ sub ParseAndRun(args)
12
+ Dim pass, inName, outName
13
+ if args.unnamed.Count <> 3 then
14
+ wscript.echo "decrypt-xls.vbs pass inName outFileName"
15
+ wscript.quit
16
+ end if
17
+ pass = args.unnamed(0)
18
+ inName = g_fs.getAbsolutePathName(args.unnamed(1))
19
+ outName = g_fs.getAbsolutePathName(args.unnamed(2))
20
+
21
+ Dim suf
22
+ suf = lcase(g_fs.getExtensionName(inName))
23
+ if suf <> "xls" and suf <> "xlsx" then
24
+ wscript.echo "bad suffix=" & suf
25
+ wscript.quit
26
+ end if
27
+
28
+ decryptExcel inName, outName, pass
29
+ end sub
30
+
31
+ sub decryptExcel(inName, outName, pass)
32
+ on error resume next
33
+
34
+ Dim app
35
+ set app = createObject("Excel.application")
36
+ app.Application.DisplayAlerts = False
37
+ app.Visible = False
38
+
39
+ Dim wb
40
+ set wb = app.Workbooks.open(inName, , , , pass)
41
+ wb.SaveAs outName, , ""
42
+ app.quit
43
+ if err.number <> 0 then
44
+ wscript.echo "ERR:" & err.number & " desc:" & err.description
45
+ end if
46
+ end sub
@@ -0,0 +1 @@
1
+ cl /MT /Ox /EHsc src\msoffice-crypt.cpp -I .\include -I ..\cybozulib\include -I ..\cybozulib_ext\include -DNOMINMAX -DNDEBUG /Febin\msoffice-crypt.exe /link /LIBPATH:..\cybozulib_ext\lib"
@@ -0,0 +1,3 @@
1
+ set OPT=/Ox /EHsc /W3 -I .\include -I ..\cybozulib\include -I ..\cybozulib_ext\include -DNOMINMAX -DNDEBUG
2
+ cl /LD src\msocdll.cpp msoc.def %OPT% /Febin\msoc.dll /link /LIBPATH:..\cybozulib_ext\lib
3
+ cl /MT src\msocsample.c %OPT% /Febin\msocsample.exe /link /LIBPATH:bin
@@ -0,0 +1,13 @@
1
+ LIBRARY msoc
2
+ EXPORTS
3
+ MSOC_getErrMessage
4
+ MSOC_encrypt
5
+ MSOC_decrypt
6
+ MSOC_encryptA
7
+ MSOC_decryptA
8
+ MSOC_createOpt
9
+ MSOC_destroyOpt
10
+ MSOC_getInt
11
+ MSOC_setInt
12
+ MSOC_getStr
13
+ MSOC_setStr