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,209 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief SHA1 class
5
+
6
+ @author MITSUNARI Shigeo(@herumi)
7
+ @note this is naive implementation so is not fast.
8
+ */
9
+
10
+ #include <cybozu/inttype.hpp>
11
+ #include <cybozu/endian.hpp>
12
+ #include <string>
13
+ #include <algorithm>
14
+ #include <memory.h>
15
+ #include <assert.h>
16
+
17
+ namespace cybozu {
18
+
19
+ class Sha1 {
20
+ private:
21
+ uint64_t totalSize_;
22
+ size_t roundBufSize_;
23
+ char roundBuf_[64];
24
+ uint32_t H_[5];
25
+ uint32_t K_[80];
26
+ uint32_t digest_[5];
27
+ bool done_;
28
+
29
+ uint32_t S(uint32_t x, int s) const
30
+ {
31
+ #ifdef _MSC_VER
32
+ return _rotl(x, s);
33
+ #else
34
+ return (x << s) | (x >> (32 - s));
35
+ #endif
36
+ }
37
+
38
+ uint32_t f0(uint32_t b, uint32_t c, uint32_t d) const { return (b & c) | (~b & d); }
39
+ uint32_t f1(uint32_t b, uint32_t c, uint32_t d) const { return b ^ c ^ d; }
40
+ uint32_t f2(uint32_t b, uint32_t c, uint32_t d) const { return (b & c) | (b & d) | (c & d); }
41
+ uint32_t f(int t, uint32_t b, uint32_t c, uint32_t d) const
42
+ {
43
+ if (t < 20) {
44
+ return f0(b, c, d);
45
+ } else
46
+ if (t < 40) {
47
+ return f1(b, c, d);
48
+ } else
49
+ if (t < 60) {
50
+ return f2(b, c, d);
51
+ } else {
52
+ return f1(b, c, d);
53
+ }
54
+ }
55
+
56
+ void reset()
57
+ {
58
+ static const uint32_t tbl[] = {
59
+ 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6
60
+ };
61
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
62
+ for (int j = 0; j < 20; j++) {
63
+ K_[i * 20 + j] = tbl[i];
64
+ }
65
+ }
66
+ totalSize_ = 0;
67
+ roundBufSize_ = 0;
68
+ H_[0] = 0x67452301;
69
+ H_[1] = 0xefcdab89;
70
+ H_[2] = 0x98badcfe;
71
+ H_[3] = 0x10325476;
72
+ H_[4] = 0xc3d2e1f0;
73
+ done_ = false;
74
+ }
75
+ /**
76
+ @param buf [in] buffer(64byte)
77
+ */
78
+ void round(const char *buf)
79
+ {
80
+ uint32_t W[80];
81
+ for (int i = 0; i < 16; i++) {
82
+ W[i] = cybozu::Get32bitAsBE(&buf[i * 4]);
83
+ }
84
+ for (int i = 16 ; i < 80; i++) {
85
+ W[i] = S(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
86
+ }
87
+ uint32_t a = H_[0];
88
+ uint32_t b = H_[1];
89
+ uint32_t c = H_[2];
90
+ uint32_t d = H_[3];
91
+ uint32_t e = H_[4];
92
+ for (int i = 0; i < 80; i++) {
93
+ uint32_t tmp = S(a, 5) + f(i, b, c, d) + e + W[i] + K_[i];
94
+ e = d;
95
+ d = c;
96
+ c = S(b, 30);
97
+ b = a;
98
+ a = tmp;
99
+ }
100
+ H_[0] += a;
101
+ H_[1] += b;
102
+ H_[2] += c;
103
+ H_[3] += d;
104
+ H_[4] += e;
105
+ totalSize_ += 64;
106
+ }
107
+ /*
108
+ final phase
109
+ @note bufSize < 64
110
+ */
111
+ void term(const char *buf, size_t bufSize)
112
+ {
113
+ assert(bufSize < 64);
114
+ const uint64_t totalSize = totalSize_ + bufSize;
115
+
116
+ uint8_t last[64];
117
+ memcpy(last, buf, bufSize);
118
+ memset(&last[bufSize], 0, 64 - bufSize);
119
+ last[bufSize] = uint8_t(0x80); /* top bit = 1 */
120
+ if (bufSize >= 56) {
121
+ round(cybozu::cast<const char*>(last));
122
+ memset(last, 0, sizeof(last)); // clear stack
123
+ }
124
+ cybozu::Set32bitAsBE(&last[56], uint32_t(totalSize >> 29));
125
+ cybozu::Set32bitAsBE(&last[60], uint32_t(totalSize * 8));
126
+ round(cybozu::cast<const char*>(last));
127
+
128
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(H_); i++) {
129
+ cybozu::Set32bitAsBE(&digest_[i], H_[i]);
130
+ }
131
+ done_ = true;
132
+ }
133
+ public:
134
+ Sha1()
135
+ {
136
+ reset();
137
+ }
138
+ void update(const char *buf, size_t bufSize)
139
+ {
140
+ if (bufSize == 0) return;
141
+ assert(!done_);
142
+ if (roundBufSize_ > 0) {
143
+ size_t size = std::min(64 - roundBufSize_, bufSize);
144
+ memcpy(roundBuf_ + roundBufSize_, buf, size);
145
+ roundBufSize_ += size;
146
+ buf += size;
147
+ bufSize -= size;
148
+ }
149
+ if (roundBufSize_ == 64) {
150
+ round(roundBuf_);
151
+ roundBufSize_ = 0;
152
+ }
153
+ while (bufSize >= 64) {
154
+ assert(roundBufSize_ == 0);
155
+ round(buf);
156
+ buf += 64;
157
+ bufSize -= 64;
158
+ }
159
+ if (bufSize > 0) {
160
+ assert(bufSize < 64);
161
+ assert(roundBufSize_ == 0);
162
+ memcpy(roundBuf_, buf, bufSize);
163
+ roundBufSize_ = bufSize;
164
+ }
165
+ assert(roundBufSize_ < 64);
166
+ }
167
+ void update(const std::string& buf)
168
+ {
169
+ update(buf.c_str(), buf.size());
170
+ }
171
+ std::string digest(const char *buf, size_t bufSize)
172
+ {
173
+ assert(!done_);
174
+ update(buf, bufSize);
175
+ term(roundBuf_, roundBufSize_);
176
+ std::string ret = get();
177
+ reset();
178
+ return ret;
179
+ }
180
+ std::string digest(const std::string& str = "")
181
+ {
182
+ return digest(str.c_str(), str.size());
183
+ }
184
+ /**
185
+ convert to printable string
186
+ */
187
+ std::string toString() const
188
+ {
189
+ std::string str;
190
+ char buf[32];
191
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(digest_); i++) {
192
+ uint32_t v = cybozu::Get32bitAsBE(&digest_[i]);
193
+ CYBOZU_SNPRINTF(buf, sizeof(buf), "%08x", v);
194
+ str += buf;
195
+ }
196
+ return str;
197
+ }
198
+ void get(char out[20]) const
199
+ {
200
+ memcpy(out, digest_, sizeof(digest_));
201
+ }
202
+ std::string get() const
203
+ {
204
+ return std::string(cybozu::cast<const char*>(&digest_[0]), sizeof(digest_));
205
+ }
206
+ };
207
+
208
+ } // cybozu
209
+
@@ -0,0 +1,506 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief SHA-256, SHA-512 class
5
+ @author MITSUNARI Shigeo(@herumi)
6
+ @license modified new BSD license
7
+ http://opensource.org/licenses/BSD-3-Clause
8
+ */
9
+ #if !defined(CYBOZU_DONT_USE_OPENSSL) && !defined(MCL_DONT_USE_OPENSSL)
10
+ #define CYBOZU_USE_OPENSSL_SHA
11
+ #endif
12
+
13
+ #ifndef CYBOZU_DONT_USE_STRING
14
+ #include <string>
15
+ #endif
16
+ #include <memory.h>
17
+
18
+ #ifdef CYBOZU_USE_OPENSSL_SHA
19
+ #ifdef __APPLE__
20
+ #pragma GCC diagnostic push
21
+ #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
22
+ #endif
23
+ #include <openssl/sha.h>
24
+ #ifdef _MSC_VER
25
+ #include <cybozu/link_libeay32.hpp>
26
+ #endif
27
+
28
+ #ifdef __APPLE__
29
+ #pragma GCC diagnostic pop
30
+ #endif
31
+
32
+ namespace cybozu {
33
+
34
+ class Sha256 {
35
+ SHA256_CTX ctx_;
36
+ public:
37
+ Sha256()
38
+ {
39
+ clear();
40
+ }
41
+ void clear()
42
+ {
43
+ SHA256_Init(&ctx_);
44
+ }
45
+ void update(const void *buf, size_t bufSize)
46
+ {
47
+ SHA256_Update(&ctx_, buf, bufSize);
48
+ }
49
+ size_t digest(void *md, size_t mdSize, const void *buf, size_t bufSize)
50
+ {
51
+ if (mdSize < SHA256_DIGEST_LENGTH) return 0;
52
+ update(buf, bufSize);
53
+ SHA256_Final(reinterpret_cast<uint8_t*>(md), &ctx_);
54
+ return SHA256_DIGEST_LENGTH;
55
+ }
56
+ #ifndef CYBOZU_DONT_USE_STRING
57
+ void update(const std::string& buf)
58
+ {
59
+ update(buf.c_str(), buf.size());
60
+ }
61
+ std::string digest(const std::string& buf)
62
+ {
63
+ return digest(buf.c_str(), buf.size());
64
+ }
65
+ std::string digest(const void *buf, size_t bufSize)
66
+ {
67
+ std::string md(SHA256_DIGEST_LENGTH, 0);
68
+ digest(&md[0], md.size(), buf, bufSize);
69
+ return md;
70
+ }
71
+ #endif
72
+ };
73
+
74
+ class Sha512 {
75
+ SHA512_CTX ctx_;
76
+ public:
77
+ Sha512()
78
+ {
79
+ clear();
80
+ }
81
+ void clear()
82
+ {
83
+ SHA512_Init(&ctx_);
84
+ }
85
+ void update(const void *buf, size_t bufSize)
86
+ {
87
+ SHA512_Update(&ctx_, buf, bufSize);
88
+ }
89
+ size_t digest(void *md, size_t mdSize, const void *buf, size_t bufSize)
90
+ {
91
+ if (mdSize < SHA512_DIGEST_LENGTH) return 0;
92
+ update(buf, bufSize);
93
+ SHA512_Final(reinterpret_cast<uint8_t*>(md), &ctx_);
94
+ return SHA512_DIGEST_LENGTH;
95
+ }
96
+ #ifndef CYBOZU_DONT_USE_STRING
97
+ void update(const std::string& buf)
98
+ {
99
+ update(buf.c_str(), buf.size());
100
+ }
101
+ std::string digest(const std::string& buf)
102
+ {
103
+ return digest(buf.c_str(), buf.size());
104
+ }
105
+ std::string digest(const void *buf, size_t bufSize)
106
+ {
107
+ std::string md(SHA512_DIGEST_LENGTH, 0);
108
+ digest(&md[0], md.size(), buf, bufSize);
109
+ return md;
110
+ }
111
+ #endif
112
+ };
113
+
114
+ } // cybozu
115
+
116
+ #else
117
+
118
+ #include <cybozu/endian.hpp>
119
+ #include <memory.h>
120
+ #include <assert.h>
121
+
122
+ namespace cybozu {
123
+
124
+ namespace sha2_local {
125
+
126
+ template<class T>
127
+ T min_(T x, T y) { return x < y ? x : y;; }
128
+
129
+ inline uint32_t rot32(uint32_t x, int s)
130
+ {
131
+ #ifdef _MSC_VER
132
+ return _rotr(x, s);
133
+ #else
134
+ return (x >> s) | (x << (32 - s));
135
+ #endif
136
+ }
137
+
138
+ inline uint64_t rot64(uint64_t x, int s)
139
+ {
140
+ #ifdef _MSC_VER
141
+ return _rotr64(x, s);
142
+ #else
143
+ return (x >> s) | (x << (64 - s));
144
+ #endif
145
+ }
146
+
147
+ template<class T>
148
+ struct Common {
149
+ void term(uint8_t *buf, size_t bufSize)
150
+ {
151
+ assert(bufSize < T::blockSize_);
152
+ T& self = static_cast<T&>(*this);
153
+ const uint64_t totalSize = self.totalSize_ + bufSize;
154
+
155
+ buf[bufSize] = uint8_t(0x80); /* top bit = 1 */
156
+ memset(&buf[bufSize + 1], 0, T::blockSize_ - bufSize - 1);
157
+ if (bufSize >= T::blockSize_ - T::msgLenByte_) {
158
+ self.round(buf);
159
+ memset(buf, 0, T::blockSize_ - 8); // clear stack
160
+ }
161
+ cybozu::Set64bitAsBE(&buf[T::blockSize_ - 8], totalSize * 8);
162
+ self.round(buf);
163
+ }
164
+ void inner_update(const uint8_t *buf, size_t bufSize)
165
+ {
166
+ T& self = static_cast<T&>(*this);
167
+ if (bufSize == 0) return;
168
+ if (self.roundBufSize_ > 0) {
169
+ size_t size = sha2_local::min_(T::blockSize_ - self.roundBufSize_, bufSize);
170
+ memcpy(self.roundBuf_ + self.roundBufSize_, buf, size);
171
+ self.roundBufSize_ += size;
172
+ buf += size;
173
+ bufSize -= size;
174
+ }
175
+ if (self.roundBufSize_ == T::blockSize_) {
176
+ self.round(self.roundBuf_);
177
+ self.roundBufSize_ = 0;
178
+ }
179
+ while (bufSize >= T::blockSize_) {
180
+ assert(self.roundBufSize_ == 0);
181
+ self.round(buf);
182
+ buf += T::blockSize_;
183
+ bufSize -= T::blockSize_;
184
+ }
185
+ if (bufSize > 0) {
186
+ assert(bufSize < T::blockSize_);
187
+ assert(self.roundBufSize_ == 0);
188
+ memcpy(self.roundBuf_, buf, bufSize);
189
+ self.roundBufSize_ = bufSize;
190
+ }
191
+ assert(self.roundBufSize_ < T::blockSize_);
192
+ }
193
+ };
194
+
195
+ } // cybozu::sha2_local
196
+
197
+ class Sha256 : public sha2_local::Common<Sha256> {
198
+ friend struct sha2_local::Common<Sha256>;
199
+ private:
200
+ static const size_t blockSize_ = 64;
201
+ static const size_t hSize_ = 8;
202
+ static const size_t msgLenByte_ = 8;
203
+ uint64_t totalSize_;
204
+ size_t roundBufSize_;
205
+ uint8_t roundBuf_[blockSize_];
206
+ uint32_t h_[hSize_];
207
+ static const size_t outByteSize_ = hSize_ * sizeof(uint32_t);
208
+ const uint32_t *k_;
209
+
210
+ template<size_t i0, size_t i1, size_t i2, size_t i3, size_t i4, size_t i5, size_t i6, size_t i7>
211
+ void round1(uint32_t *s, uint32_t *w, int i)
212
+ {
213
+ using namespace sha2_local;
214
+ uint32_t e = s[i4];
215
+ uint32_t h = s[i7];
216
+ h += rot32(e, 6) ^ rot32(e, 11) ^ rot32(e, 25);
217
+ uint32_t f = s[i5];
218
+ uint32_t g = s[i6];
219
+ h += g ^ (e & (f ^ g));
220
+ h += k_[i];
221
+ h += w[i];
222
+ s[i3] += h;
223
+ uint32_t a = s[i0];
224
+ uint32_t b = s[i1];
225
+ uint32_t c = s[i2];
226
+ h += rot32(a, 2) ^ rot32(a, 13) ^ rot32(a, 22);
227
+ h += ((a | b) & c) | (a & b);
228
+ s[i7] = h;
229
+ }
230
+ /**
231
+ @param buf [in] buffer(64byte)
232
+ */
233
+ void round(const uint8_t *buf)
234
+ {
235
+ using namespace sha2_local;
236
+ uint32_t w[64];
237
+ for (int i = 0; i < 16; i++) {
238
+ w[i] = cybozu::Get32bitAsBE(&buf[i * 4]);
239
+ }
240
+ for (int i = 16 ; i < 64; i++) {
241
+ uint32_t t = w[i - 15];
242
+ uint32_t s0 = rot32(t, 7) ^ rot32(t, 18) ^ (t >> 3);
243
+ t = w[i - 2];
244
+ uint32_t s1 = rot32(t, 17) ^ rot32(t, 19) ^ (t >> 10);
245
+ w[i] = w[i - 16] + s0 + w[i - 7] + s1;
246
+ }
247
+ uint32_t s[8];
248
+ for (int i = 0; i < 8; i++) {
249
+ s[i] = h_[i];
250
+ }
251
+ for (int i = 0; i < 64; i += 8) {
252
+ round1<0, 1, 2, 3, 4, 5, 6, 7>(s, w, i + 0);
253
+ round1<7, 0, 1, 2, 3, 4, 5, 6>(s, w, i + 1);
254
+ round1<6, 7, 0, 1, 2, 3, 4, 5>(s, w, i + 2);
255
+ round1<5, 6, 7, 0, 1, 2, 3, 4>(s, w, i + 3);
256
+ round1<4, 5, 6, 7, 0, 1, 2, 3>(s, w, i + 4);
257
+ round1<3, 4, 5, 6, 7, 0, 1, 2>(s, w, i + 5);
258
+ round1<2, 3, 4, 5, 6, 7, 0, 1>(s, w, i + 6);
259
+ round1<1, 2, 3, 4, 5, 6, 7, 0>(s, w, i + 7);
260
+ }
261
+ for (int i = 0; i < 8; i++) {
262
+ h_[i] += s[i];
263
+ }
264
+ totalSize_ += blockSize_;
265
+ }
266
+ public:
267
+ Sha256()
268
+ {
269
+ clear();
270
+ }
271
+ void clear()
272
+ {
273
+ static const uint32_t kTbl[] = {
274
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
275
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
276
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
277
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
278
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
279
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
280
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
281
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
282
+ };
283
+ k_ = kTbl;
284
+ totalSize_ = 0;
285
+ roundBufSize_ = 0;
286
+ h_[0] = 0x6a09e667;
287
+ h_[1] = 0xbb67ae85;
288
+ h_[2] = 0x3c6ef372;
289
+ h_[3] = 0xa54ff53a;
290
+ h_[4] = 0x510e527f;
291
+ h_[5] = 0x9b05688c;
292
+ h_[6] = 0x1f83d9ab;
293
+ h_[7] = 0x5be0cd19;
294
+ }
295
+ void update(const void *buf, size_t bufSize)
296
+ {
297
+ inner_update(reinterpret_cast<const uint8_t*>(buf), bufSize);
298
+ }
299
+ size_t digest(void *md, size_t mdSize, const void *buf, size_t bufSize)
300
+ {
301
+ if (mdSize < outByteSize_) return 0;
302
+ update(buf, bufSize);
303
+ term(roundBuf_, roundBufSize_);
304
+ char *p = reinterpret_cast<char*>(md);
305
+ for (size_t i = 0; i < hSize_; i++) {
306
+ cybozu::Set32bitAsBE(&p[i * sizeof(h_[0])], h_[i]);
307
+ }
308
+ return outByteSize_;
309
+ }
310
+ #ifndef CYBOZU_DONT_USE_STRING
311
+ void update(const std::string& buf)
312
+ {
313
+ update(buf.c_str(), buf.size());
314
+ }
315
+ std::string digest(const std::string& buf)
316
+ {
317
+ return digest(buf.c_str(), buf.size());
318
+ }
319
+ std::string digest(const void *buf, size_t bufSize)
320
+ {
321
+ std::string md(outByteSize_, 0);
322
+ digest(&md[0], md.size(), buf, bufSize);
323
+ return md;
324
+ }
325
+ #endif
326
+ };
327
+
328
+ class Sha512 : public sha2_local::Common<Sha512> {
329
+ friend struct sha2_local::Common<Sha512>;
330
+ private:
331
+ static const size_t blockSize_ = 128;
332
+ static const size_t hSize_ = 8;
333
+ static const size_t msgLenByte_ = 16;
334
+ uint64_t totalSize_;
335
+ size_t roundBufSize_;
336
+ uint8_t roundBuf_[blockSize_];
337
+ uint64_t h_[hSize_];
338
+ static const size_t outByteSize_ = hSize_ * sizeof(uint64_t);
339
+ const uint64_t *k_;
340
+
341
+ template<size_t i0, size_t i1, size_t i2, size_t i3, size_t i4, size_t i5, size_t i6, size_t i7>
342
+ void round1(uint64_t *S, const uint64_t *w, size_t i)
343
+ {
344
+ using namespace sha2_local;
345
+ uint64_t& a = S[i0];
346
+ uint64_t& b = S[i1];
347
+ uint64_t& c = S[i2];
348
+ uint64_t& d = S[i3];
349
+ uint64_t& e = S[i4];
350
+ uint64_t& f = S[i5];
351
+ uint64_t& g = S[i6];
352
+ uint64_t& h = S[i7];
353
+
354
+ uint64_t s1 = rot64(e, 14) ^ rot64(e, 18) ^ rot64(e, 41);
355
+ uint64_t ch = g ^ (e & (f ^ g));
356
+ uint64_t t0 = h + s1 + ch + k_[i] + w[i];
357
+ uint64_t s0 = rot64(a, 28) ^ rot64(a, 34) ^ rot64(a, 39);
358
+ uint64_t maj = ((a | b) & c) | (a & b);
359
+ uint64_t t1 = s0 + maj;
360
+ d += t0;
361
+ h = t0 + t1;
362
+ }
363
+ /**
364
+ @param buf [in] buffer(64byte)
365
+ */
366
+ void round(const uint8_t *buf)
367
+ {
368
+ using namespace sha2_local;
369
+ uint64_t w[80];
370
+ for (int i = 0; i < 16; i++) {
371
+ w[i] = cybozu::Get64bitAsBE(&buf[i * 8]);
372
+ }
373
+ for (int i = 16 ; i < 80; i++) {
374
+ uint64_t t = w[i - 15];
375
+ uint64_t s0 = rot64(t, 1) ^ rot64(t, 8) ^ (t >> 7);
376
+ t = w[i - 2];
377
+ uint64_t s1 = rot64(t, 19) ^ rot64(t, 61) ^ (t >> 6);
378
+ w[i] = w[i - 16] + s0 + w[i - 7] + s1;
379
+ }
380
+ uint64_t s[8];
381
+ for (int i = 0; i < 8; i++) {
382
+ s[i] = h_[i];
383
+ }
384
+ for (int i = 0; i < 80; i += 8) {
385
+ round1<0, 1, 2, 3, 4, 5, 6, 7>(s, w, i + 0);
386
+ round1<7, 0, 1, 2, 3, 4, 5, 6>(s, w, i + 1);
387
+ round1<6, 7, 0, 1, 2, 3, 4, 5>(s, w, i + 2);
388
+ round1<5, 6, 7, 0, 1, 2, 3, 4>(s, w, i + 3);
389
+ round1<4, 5, 6, 7, 0, 1, 2, 3>(s, w, i + 4);
390
+ round1<3, 4, 5, 6, 7, 0, 1, 2>(s, w, i + 5);
391
+ round1<2, 3, 4, 5, 6, 7, 0, 1>(s, w, i + 6);
392
+ round1<1, 2, 3, 4, 5, 6, 7, 0>(s, w, i + 7);
393
+ }
394
+ for (int i = 0; i < 8; i++) {
395
+ h_[i] += s[i];
396
+ }
397
+ totalSize_ += blockSize_;
398
+ }
399
+ public:
400
+ Sha512()
401
+ {
402
+ clear();
403
+ }
404
+ void clear()
405
+ {
406
+ static const uint64_t kTbl[] = {
407
+ 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL,
408
+ 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
409
+ 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL,
410
+ 0xc19bf174cf692694ULL, 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
411
+ 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, 0x983e5152ee66dfabULL,
412
+ 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
413
+ 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL,
414
+ 0x53380d139d95b3dfULL, 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
415
+ 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL,
416
+ 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
417
+ 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL,
418
+ 0x682e6ff3d6b2b8a3ULL, 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
419
+ 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, 0xca273eceea26619cULL,
420
+ 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
421
+ 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL,
422
+ 0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
423
+ };
424
+ k_ = kTbl;
425
+ totalSize_ = 0;
426
+ roundBufSize_ = 0;
427
+ h_[0] = 0x6a09e667f3bcc908ull;
428
+ h_[1] = 0xbb67ae8584caa73bull;
429
+ h_[2] = 0x3c6ef372fe94f82bull;
430
+ h_[3] = 0xa54ff53a5f1d36f1ull;
431
+ h_[4] = 0x510e527fade682d1ull;
432
+ h_[5] = 0x9b05688c2b3e6c1full;
433
+ h_[6] = 0x1f83d9abfb41bd6bull;
434
+ h_[7] = 0x5be0cd19137e2179ull;
435
+ }
436
+ void update(const void *buf, size_t bufSize)
437
+ {
438
+ inner_update(reinterpret_cast<const uint8_t*>(buf), bufSize);
439
+ }
440
+ size_t digest(void *md, size_t mdSize, const void *buf, size_t bufSize)
441
+ {
442
+ if (mdSize < outByteSize_) return 0;
443
+ update(buf, bufSize);
444
+ term(roundBuf_, roundBufSize_);
445
+ char *p = reinterpret_cast<char*>(md);
446
+ for (size_t i = 0; i < hSize_; i++) {
447
+ cybozu::Set64bitAsBE(&p[i * sizeof(h_[0])], h_[i]);
448
+ }
449
+ return outByteSize_;
450
+ }
451
+ #ifndef CYBOZU_DONT_USE_STRING
452
+ void update(const std::string& buf)
453
+ {
454
+ update(buf.c_str(), buf.size());
455
+ }
456
+ std::string digest(const std::string& buf)
457
+ {
458
+ return digest(buf.c_str(), buf.size());
459
+ }
460
+ std::string digest(const void *buf, size_t bufSize)
461
+ {
462
+ std::string md(outByteSize_, 0);
463
+ digest(&md[0], md.size(), buf, bufSize);
464
+ return md;
465
+ }
466
+ #endif
467
+ };
468
+
469
+ } // cybozu
470
+
471
+ #endif
472
+
473
+ namespace cybozu {
474
+
475
+ /*
476
+ HMAC-SHA-256
477
+ hmac must have 32 bytes buffer
478
+ */
479
+ inline void hmac256(void *hmac, const void *key, size_t keySize, const void *msg, size_t msgSize)
480
+ {
481
+ const uint8_t ipad = 0x36;
482
+ const uint8_t opad = 0x5c;
483
+ uint8_t k[64];
484
+ Sha256 hash;
485
+ if (keySize > 64) {
486
+ hash.digest(k, 32, key, keySize);
487
+ hash.clear();
488
+ keySize = 32;
489
+ } else {
490
+ memcpy(k, key, keySize);
491
+ }
492
+ for (size_t i = 0; i < keySize; i++) {
493
+ k[i] = k[i] ^ ipad;
494
+ }
495
+ memset(k + keySize, ipad, 64 - keySize);
496
+ hash.update(k, 64);
497
+ hash.digest(hmac, 32, msg, msgSize);
498
+ hash.clear();
499
+ for (size_t i = 0; i < 64; i++) {
500
+ k[i] = k[i] ^ (ipad ^ opad);
501
+ }
502
+ hash.update(k, 64);
503
+ hash.digest(hmac, 32, hmac, 32);
504
+ }
505
+
506
+ } // cybozu