ring-native 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (261) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/Gemfile +3 -0
  4. data/README.md +22 -0
  5. data/Rakefile +1 -0
  6. data/ext/ring/extconf.rb +29 -0
  7. data/lib/ring/native.rb +8 -0
  8. data/lib/ring/native/version.rb +5 -0
  9. data/ring-native.gemspec +25 -0
  10. data/vendor/ring/BUILDING.md +40 -0
  11. data/vendor/ring/Cargo.toml +43 -0
  12. data/vendor/ring/LICENSE +185 -0
  13. data/vendor/ring/Makefile +35 -0
  14. data/vendor/ring/PORTING.md +163 -0
  15. data/vendor/ring/README.md +113 -0
  16. data/vendor/ring/STYLE.md +197 -0
  17. data/vendor/ring/appveyor.yml +27 -0
  18. data/vendor/ring/build.rs +108 -0
  19. data/vendor/ring/crypto/aes/aes.c +1142 -0
  20. data/vendor/ring/crypto/aes/aes_test.Windows.vcxproj +25 -0
  21. data/vendor/ring/crypto/aes/aes_test.cc +93 -0
  22. data/vendor/ring/crypto/aes/asm/aes-586.pl +2368 -0
  23. data/vendor/ring/crypto/aes/asm/aes-armv4.pl +1249 -0
  24. data/vendor/ring/crypto/aes/asm/aes-x86_64.pl +2246 -0
  25. data/vendor/ring/crypto/aes/asm/aesni-x86.pl +1318 -0
  26. data/vendor/ring/crypto/aes/asm/aesni-x86_64.pl +2084 -0
  27. data/vendor/ring/crypto/aes/asm/aesv8-armx.pl +675 -0
  28. data/vendor/ring/crypto/aes/asm/bsaes-armv7.pl +1364 -0
  29. data/vendor/ring/crypto/aes/asm/bsaes-x86_64.pl +1565 -0
  30. data/vendor/ring/crypto/aes/asm/vpaes-x86.pl +841 -0
  31. data/vendor/ring/crypto/aes/asm/vpaes-x86_64.pl +1116 -0
  32. data/vendor/ring/crypto/aes/internal.h +87 -0
  33. data/vendor/ring/crypto/aes/mode_wrappers.c +61 -0
  34. data/vendor/ring/crypto/bn/add.c +394 -0
  35. data/vendor/ring/crypto/bn/asm/armv4-mont.pl +694 -0
  36. data/vendor/ring/crypto/bn/asm/armv8-mont.pl +1503 -0
  37. data/vendor/ring/crypto/bn/asm/bn-586.pl +774 -0
  38. data/vendor/ring/crypto/bn/asm/co-586.pl +287 -0
  39. data/vendor/ring/crypto/bn/asm/rsaz-avx2.pl +1882 -0
  40. data/vendor/ring/crypto/bn/asm/x86-mont.pl +592 -0
  41. data/vendor/ring/crypto/bn/asm/x86_64-gcc.c +599 -0
  42. data/vendor/ring/crypto/bn/asm/x86_64-mont.pl +1393 -0
  43. data/vendor/ring/crypto/bn/asm/x86_64-mont5.pl +3507 -0
  44. data/vendor/ring/crypto/bn/bn.c +352 -0
  45. data/vendor/ring/crypto/bn/bn_asn1.c +74 -0
  46. data/vendor/ring/crypto/bn/bn_test.Windows.vcxproj +25 -0
  47. data/vendor/ring/crypto/bn/bn_test.cc +1696 -0
  48. data/vendor/ring/crypto/bn/cmp.c +200 -0
  49. data/vendor/ring/crypto/bn/convert.c +433 -0
  50. data/vendor/ring/crypto/bn/ctx.c +311 -0
  51. data/vendor/ring/crypto/bn/div.c +594 -0
  52. data/vendor/ring/crypto/bn/exponentiation.c +1335 -0
  53. data/vendor/ring/crypto/bn/gcd.c +711 -0
  54. data/vendor/ring/crypto/bn/generic.c +1019 -0
  55. data/vendor/ring/crypto/bn/internal.h +316 -0
  56. data/vendor/ring/crypto/bn/montgomery.c +516 -0
  57. data/vendor/ring/crypto/bn/mul.c +888 -0
  58. data/vendor/ring/crypto/bn/prime.c +829 -0
  59. data/vendor/ring/crypto/bn/random.c +334 -0
  60. data/vendor/ring/crypto/bn/rsaz_exp.c +262 -0
  61. data/vendor/ring/crypto/bn/rsaz_exp.h +53 -0
  62. data/vendor/ring/crypto/bn/shift.c +276 -0
  63. data/vendor/ring/crypto/bytestring/bytestring_test.Windows.vcxproj +25 -0
  64. data/vendor/ring/crypto/bytestring/bytestring_test.cc +421 -0
  65. data/vendor/ring/crypto/bytestring/cbb.c +399 -0
  66. data/vendor/ring/crypto/bytestring/cbs.c +227 -0
  67. data/vendor/ring/crypto/bytestring/internal.h +46 -0
  68. data/vendor/ring/crypto/chacha/chacha_generic.c +140 -0
  69. data/vendor/ring/crypto/chacha/chacha_vec.c +323 -0
  70. data/vendor/ring/crypto/chacha/chacha_vec_arm.S +1447 -0
  71. data/vendor/ring/crypto/chacha/chacha_vec_arm_generate.go +153 -0
  72. data/vendor/ring/crypto/cipher/cipher_test.Windows.vcxproj +25 -0
  73. data/vendor/ring/crypto/cipher/e_aes.c +390 -0
  74. data/vendor/ring/crypto/cipher/e_chacha20poly1305.c +208 -0
  75. data/vendor/ring/crypto/cipher/internal.h +173 -0
  76. data/vendor/ring/crypto/cipher/test/aes_128_gcm_tests.txt +543 -0
  77. data/vendor/ring/crypto/cipher/test/aes_128_key_wrap_tests.txt +9 -0
  78. data/vendor/ring/crypto/cipher/test/aes_256_gcm_tests.txt +475 -0
  79. data/vendor/ring/crypto/cipher/test/aes_256_key_wrap_tests.txt +23 -0
  80. data/vendor/ring/crypto/cipher/test/chacha20_poly1305_old_tests.txt +422 -0
  81. data/vendor/ring/crypto/cipher/test/chacha20_poly1305_tests.txt +484 -0
  82. data/vendor/ring/crypto/cipher/test/cipher_test.txt +100 -0
  83. data/vendor/ring/crypto/constant_time_test.Windows.vcxproj +25 -0
  84. data/vendor/ring/crypto/constant_time_test.c +304 -0
  85. data/vendor/ring/crypto/cpu-arm-asm.S +32 -0
  86. data/vendor/ring/crypto/cpu-arm.c +199 -0
  87. data/vendor/ring/crypto/cpu-intel.c +261 -0
  88. data/vendor/ring/crypto/crypto.c +151 -0
  89. data/vendor/ring/crypto/curve25519/asm/x25519-arm.S +2118 -0
  90. data/vendor/ring/crypto/curve25519/curve25519.c +4888 -0
  91. data/vendor/ring/crypto/curve25519/x25519_test.cc +128 -0
  92. data/vendor/ring/crypto/digest/md32_common.h +181 -0
  93. data/vendor/ring/crypto/ec/asm/p256-x86_64-asm.pl +2725 -0
  94. data/vendor/ring/crypto/ec/ec.c +193 -0
  95. data/vendor/ring/crypto/ec/ec_curves.c +61 -0
  96. data/vendor/ring/crypto/ec/ec_key.c +228 -0
  97. data/vendor/ring/crypto/ec/ec_montgomery.c +114 -0
  98. data/vendor/ring/crypto/ec/example_mul.Windows.vcxproj +25 -0
  99. data/vendor/ring/crypto/ec/internal.h +243 -0
  100. data/vendor/ring/crypto/ec/oct.c +253 -0
  101. data/vendor/ring/crypto/ec/p256-64.c +1794 -0
  102. data/vendor/ring/crypto/ec/p256-x86_64-table.h +9548 -0
  103. data/vendor/ring/crypto/ec/p256-x86_64.c +509 -0
  104. data/vendor/ring/crypto/ec/simple.c +1007 -0
  105. data/vendor/ring/crypto/ec/util-64.c +183 -0
  106. data/vendor/ring/crypto/ec/wnaf.c +508 -0
  107. data/vendor/ring/crypto/ecdh/ecdh.c +155 -0
  108. data/vendor/ring/crypto/ecdsa/ecdsa.c +304 -0
  109. data/vendor/ring/crypto/ecdsa/ecdsa_asn1.c +193 -0
  110. data/vendor/ring/crypto/ecdsa/ecdsa_test.Windows.vcxproj +25 -0
  111. data/vendor/ring/crypto/ecdsa/ecdsa_test.cc +327 -0
  112. data/vendor/ring/crypto/header_removed.h +17 -0
  113. data/vendor/ring/crypto/internal.h +495 -0
  114. data/vendor/ring/crypto/libring.Windows.vcxproj +101 -0
  115. data/vendor/ring/crypto/mem.c +98 -0
  116. data/vendor/ring/crypto/modes/asm/aesni-gcm-x86_64.pl +1045 -0
  117. data/vendor/ring/crypto/modes/asm/ghash-armv4.pl +517 -0
  118. data/vendor/ring/crypto/modes/asm/ghash-x86.pl +1393 -0
  119. data/vendor/ring/crypto/modes/asm/ghash-x86_64.pl +1741 -0
  120. data/vendor/ring/crypto/modes/asm/ghashv8-armx.pl +422 -0
  121. data/vendor/ring/crypto/modes/ctr.c +226 -0
  122. data/vendor/ring/crypto/modes/gcm.c +1206 -0
  123. data/vendor/ring/crypto/modes/gcm_test.Windows.vcxproj +25 -0
  124. data/vendor/ring/crypto/modes/gcm_test.c +348 -0
  125. data/vendor/ring/crypto/modes/internal.h +299 -0
  126. data/vendor/ring/crypto/perlasm/arm-xlate.pl +170 -0
  127. data/vendor/ring/crypto/perlasm/readme +100 -0
  128. data/vendor/ring/crypto/perlasm/x86_64-xlate.pl +1164 -0
  129. data/vendor/ring/crypto/perlasm/x86asm.pl +292 -0
  130. data/vendor/ring/crypto/perlasm/x86gas.pl +263 -0
  131. data/vendor/ring/crypto/perlasm/x86masm.pl +200 -0
  132. data/vendor/ring/crypto/perlasm/x86nasm.pl +187 -0
  133. data/vendor/ring/crypto/poly1305/poly1305.c +331 -0
  134. data/vendor/ring/crypto/poly1305/poly1305_arm.c +301 -0
  135. data/vendor/ring/crypto/poly1305/poly1305_arm_asm.S +2015 -0
  136. data/vendor/ring/crypto/poly1305/poly1305_test.Windows.vcxproj +25 -0
  137. data/vendor/ring/crypto/poly1305/poly1305_test.cc +80 -0
  138. data/vendor/ring/crypto/poly1305/poly1305_test.txt +52 -0
  139. data/vendor/ring/crypto/poly1305/poly1305_vec.c +892 -0
  140. data/vendor/ring/crypto/rand/asm/rdrand-x86_64.pl +75 -0
  141. data/vendor/ring/crypto/rand/internal.h +32 -0
  142. data/vendor/ring/crypto/rand/rand.c +189 -0
  143. data/vendor/ring/crypto/rand/urandom.c +219 -0
  144. data/vendor/ring/crypto/rand/windows.c +56 -0
  145. data/vendor/ring/crypto/refcount_c11.c +66 -0
  146. data/vendor/ring/crypto/refcount_lock.c +53 -0
  147. data/vendor/ring/crypto/refcount_test.Windows.vcxproj +25 -0
  148. data/vendor/ring/crypto/refcount_test.c +58 -0
  149. data/vendor/ring/crypto/rsa/blinding.c +462 -0
  150. data/vendor/ring/crypto/rsa/internal.h +108 -0
  151. data/vendor/ring/crypto/rsa/padding.c +300 -0
  152. data/vendor/ring/crypto/rsa/rsa.c +450 -0
  153. data/vendor/ring/crypto/rsa/rsa_asn1.c +261 -0
  154. data/vendor/ring/crypto/rsa/rsa_impl.c +944 -0
  155. data/vendor/ring/crypto/rsa/rsa_test.Windows.vcxproj +25 -0
  156. data/vendor/ring/crypto/rsa/rsa_test.cc +437 -0
  157. data/vendor/ring/crypto/sha/asm/sha-armv8.pl +436 -0
  158. data/vendor/ring/crypto/sha/asm/sha-x86_64.pl +2390 -0
  159. data/vendor/ring/crypto/sha/asm/sha256-586.pl +1275 -0
  160. data/vendor/ring/crypto/sha/asm/sha256-armv4.pl +735 -0
  161. data/vendor/ring/crypto/sha/asm/sha256-armv8.pl +14 -0
  162. data/vendor/ring/crypto/sha/asm/sha256-x86_64.pl +14 -0
  163. data/vendor/ring/crypto/sha/asm/sha512-586.pl +911 -0
  164. data/vendor/ring/crypto/sha/asm/sha512-armv4.pl +666 -0
  165. data/vendor/ring/crypto/sha/asm/sha512-armv8.pl +14 -0
  166. data/vendor/ring/crypto/sha/asm/sha512-x86_64.pl +14 -0
  167. data/vendor/ring/crypto/sha/sha1.c +271 -0
  168. data/vendor/ring/crypto/sha/sha256.c +204 -0
  169. data/vendor/ring/crypto/sha/sha512.c +355 -0
  170. data/vendor/ring/crypto/test/file_test.cc +326 -0
  171. data/vendor/ring/crypto/test/file_test.h +181 -0
  172. data/vendor/ring/crypto/test/malloc.cc +150 -0
  173. data/vendor/ring/crypto/test/scoped_types.h +95 -0
  174. data/vendor/ring/crypto/test/test.Windows.vcxproj +35 -0
  175. data/vendor/ring/crypto/test/test_util.cc +46 -0
  176. data/vendor/ring/crypto/test/test_util.h +41 -0
  177. data/vendor/ring/crypto/thread_none.c +55 -0
  178. data/vendor/ring/crypto/thread_pthread.c +165 -0
  179. data/vendor/ring/crypto/thread_test.Windows.vcxproj +25 -0
  180. data/vendor/ring/crypto/thread_test.c +200 -0
  181. data/vendor/ring/crypto/thread_win.c +282 -0
  182. data/vendor/ring/examples/checkdigest.rs +103 -0
  183. data/vendor/ring/include/openssl/aes.h +121 -0
  184. data/vendor/ring/include/openssl/arm_arch.h +129 -0
  185. data/vendor/ring/include/openssl/base.h +156 -0
  186. data/vendor/ring/include/openssl/bn.h +794 -0
  187. data/vendor/ring/include/openssl/buffer.h +18 -0
  188. data/vendor/ring/include/openssl/bytestring.h +235 -0
  189. data/vendor/ring/include/openssl/chacha.h +37 -0
  190. data/vendor/ring/include/openssl/cmac.h +76 -0
  191. data/vendor/ring/include/openssl/cpu.h +184 -0
  192. data/vendor/ring/include/openssl/crypto.h +43 -0
  193. data/vendor/ring/include/openssl/curve25519.h +88 -0
  194. data/vendor/ring/include/openssl/ec.h +225 -0
  195. data/vendor/ring/include/openssl/ec_key.h +129 -0
  196. data/vendor/ring/include/openssl/ecdh.h +110 -0
  197. data/vendor/ring/include/openssl/ecdsa.h +156 -0
  198. data/vendor/ring/include/openssl/err.h +201 -0
  199. data/vendor/ring/include/openssl/mem.h +101 -0
  200. data/vendor/ring/include/openssl/obj_mac.h +71 -0
  201. data/vendor/ring/include/openssl/opensslfeatures.h +68 -0
  202. data/vendor/ring/include/openssl/opensslv.h +18 -0
  203. data/vendor/ring/include/openssl/ossl_typ.h +18 -0
  204. data/vendor/ring/include/openssl/poly1305.h +51 -0
  205. data/vendor/ring/include/openssl/rand.h +70 -0
  206. data/vendor/ring/include/openssl/rsa.h +399 -0
  207. data/vendor/ring/include/openssl/thread.h +133 -0
  208. data/vendor/ring/include/openssl/type_check.h +71 -0
  209. data/vendor/ring/mk/Common.props +63 -0
  210. data/vendor/ring/mk/Windows.props +42 -0
  211. data/vendor/ring/mk/WindowsTest.props +18 -0
  212. data/vendor/ring/mk/appveyor.bat +62 -0
  213. data/vendor/ring/mk/bottom_of_makefile.mk +54 -0
  214. data/vendor/ring/mk/ring.mk +266 -0
  215. data/vendor/ring/mk/top_of_makefile.mk +214 -0
  216. data/vendor/ring/mk/travis.sh +40 -0
  217. data/vendor/ring/mk/update-travis-yml.py +229 -0
  218. data/vendor/ring/ring.sln +153 -0
  219. data/vendor/ring/src/aead.rs +682 -0
  220. data/vendor/ring/src/agreement.rs +248 -0
  221. data/vendor/ring/src/c.rs +129 -0
  222. data/vendor/ring/src/constant_time.rs +37 -0
  223. data/vendor/ring/src/der.rs +96 -0
  224. data/vendor/ring/src/digest.rs +690 -0
  225. data/vendor/ring/src/digest_tests.txt +57 -0
  226. data/vendor/ring/src/ecc.rs +28 -0
  227. data/vendor/ring/src/ecc_build.rs +279 -0
  228. data/vendor/ring/src/ecc_curves.rs +117 -0
  229. data/vendor/ring/src/ed25519_tests.txt +2579 -0
  230. data/vendor/ring/src/exe_tests.rs +46 -0
  231. data/vendor/ring/src/ffi.rs +29 -0
  232. data/vendor/ring/src/file_test.rs +187 -0
  233. data/vendor/ring/src/hkdf.rs +153 -0
  234. data/vendor/ring/src/hkdf_tests.txt +59 -0
  235. data/vendor/ring/src/hmac.rs +414 -0
  236. data/vendor/ring/src/hmac_tests.txt +97 -0
  237. data/vendor/ring/src/input.rs +312 -0
  238. data/vendor/ring/src/lib.rs +41 -0
  239. data/vendor/ring/src/pbkdf2.rs +265 -0
  240. data/vendor/ring/src/pbkdf2_tests.txt +113 -0
  241. data/vendor/ring/src/polyfill.rs +57 -0
  242. data/vendor/ring/src/rand.rs +28 -0
  243. data/vendor/ring/src/signature.rs +314 -0
  244. data/vendor/ring/third-party/NIST/README.md +9 -0
  245. data/vendor/ring/third-party/NIST/SHAVS/SHA1LongMsg.rsp +263 -0
  246. data/vendor/ring/third-party/NIST/SHAVS/SHA1Monte.rsp +309 -0
  247. data/vendor/ring/third-party/NIST/SHAVS/SHA1ShortMsg.rsp +267 -0
  248. data/vendor/ring/third-party/NIST/SHAVS/SHA224LongMsg.rsp +263 -0
  249. data/vendor/ring/third-party/NIST/SHAVS/SHA224Monte.rsp +309 -0
  250. data/vendor/ring/third-party/NIST/SHAVS/SHA224ShortMsg.rsp +267 -0
  251. data/vendor/ring/third-party/NIST/SHAVS/SHA256LongMsg.rsp +263 -0
  252. data/vendor/ring/third-party/NIST/SHAVS/SHA256Monte.rsp +309 -0
  253. data/vendor/ring/third-party/NIST/SHAVS/SHA256ShortMsg.rsp +267 -0
  254. data/vendor/ring/third-party/NIST/SHAVS/SHA384LongMsg.rsp +519 -0
  255. data/vendor/ring/third-party/NIST/SHAVS/SHA384Monte.rsp +309 -0
  256. data/vendor/ring/third-party/NIST/SHAVS/SHA384ShortMsg.rsp +523 -0
  257. data/vendor/ring/third-party/NIST/SHAVS/SHA512LongMsg.rsp +519 -0
  258. data/vendor/ring/third-party/NIST/SHAVS/SHA512Monte.rsp +309 -0
  259. data/vendor/ring/third-party/NIST/SHAVS/SHA512ShortMsg.rsp +523 -0
  260. data/vendor/ring/third-party/NIST/sha256sums.txt +1 -0
  261. metadata +333 -0
@@ -0,0 +1,282 @@
1
+ /* Copyright (c) 2015, Google Inc.
2
+ *
3
+ * Permission to use, copy, modify, and/or distribute this software for any
4
+ * purpose with or without fee is hereby granted, provided that the above
5
+ * copyright notice and this permission notice appear in all copies.
6
+ *
7
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
+
15
+ #include "internal.h"
16
+
17
+ #if defined(OPENSSL_WINDOWS) && !defined(OPENSSL_NO_THREADS)
18
+
19
+ #pragma warning(push, 3)
20
+ #include <windows.h>
21
+ #pragma warning(pop)
22
+
23
+ #include <assert.h>
24
+ #include <stdlib.h>
25
+ #include <string.h>
26
+
27
+ #include <openssl/mem.h>
28
+ #include <openssl/type_check.h>
29
+
30
+
31
+ OPENSSL_COMPILE_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(CRITICAL_SECTION),
32
+ CRYPTO_MUTEX_too_small);
33
+
34
+ static void run_once(CRYPTO_once_t *in_once, void (*init)(void *), void *arg) {
35
+ volatile LONG *once = in_once;
36
+
37
+ /* Values must be aligned. */
38
+ assert((((uintptr_t) once) & 3) == 0);
39
+
40
+ /* This assumes that reading *once has acquire semantics. This should be true
41
+ * on x86 and x86-64, where we expect Windows to run. */
42
+ #if !defined(OPENSSL_X86) && !defined(OPENSSL_X86_64)
43
+ #error "Windows once code may not work on other platforms." \
44
+ "You can use InitOnceBeginInitialize on >=Vista"
45
+ #endif
46
+ if (*once == 1) {
47
+ return;
48
+ }
49
+
50
+ for (;;) {
51
+ switch (InterlockedCompareExchange(once, 2, 0)) {
52
+ case 0:
53
+ /* The value was zero so we are the first thread to call |CRYPTO_once|
54
+ * on it. */
55
+ init(arg);
56
+ /* Write one to indicate that initialisation is complete. */
57
+ InterlockedExchange(once, 1);
58
+ return;
59
+
60
+ case 1:
61
+ /* Another thread completed initialisation between our fast-path check
62
+ * and |InterlockedCompareExchange|. */
63
+ return;
64
+
65
+ case 2:
66
+ /* Another thread is running the initialisation. Switch to it then try
67
+ * again. */
68
+ SwitchToThread();
69
+ break;
70
+
71
+ default:
72
+ abort();
73
+ }
74
+ }
75
+ }
76
+
77
+ static void call_once_init(void *arg) {
78
+ void (*init_func)(void);
79
+ /* MSVC does not like casting between data and function pointers. */
80
+ memcpy(&init_func, &arg, sizeof(void *));
81
+ init_func();
82
+ }
83
+
84
+ void CRYPTO_once(CRYPTO_once_t *in_once, void (*init)(void)) {
85
+ void *arg;
86
+ /* MSVC does not like casting between data and function pointers. */
87
+ memcpy(&arg, &init, sizeof(void *));
88
+ run_once(in_once, call_once_init, arg);
89
+ }
90
+
91
+ void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) {
92
+ if (!InitializeCriticalSectionAndSpinCount((CRITICAL_SECTION *) lock, 0x400)) {
93
+ abort();
94
+ }
95
+ }
96
+
97
+ void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) {
98
+ /* Since we have to support Windows XP, read locks are actually exclusive. */
99
+ EnterCriticalSection((CRITICAL_SECTION *) lock);
100
+ }
101
+
102
+ void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) {
103
+ EnterCriticalSection((CRITICAL_SECTION *) lock);
104
+ }
105
+
106
+ void CRYPTO_MUTEX_unlock(CRYPTO_MUTEX *lock) {
107
+ LeaveCriticalSection((CRITICAL_SECTION *) lock);
108
+ }
109
+
110
+ void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) {
111
+ DeleteCriticalSection((CRITICAL_SECTION *) lock);
112
+ }
113
+
114
+ static void static_lock_init(void *arg) {
115
+ struct CRYPTO_STATIC_MUTEX *lock = arg;
116
+ if (!InitializeCriticalSectionAndSpinCount(&lock->lock, 0x400)) {
117
+ abort();
118
+ }
119
+ }
120
+
121
+ void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) {
122
+ /* Since we have to support Windows XP, read locks are actually exclusive. */
123
+ run_once(&lock->once, static_lock_init, lock);
124
+ EnterCriticalSection(&lock->lock);
125
+ }
126
+
127
+ void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) {
128
+ CRYPTO_STATIC_MUTEX_lock_read(lock);
129
+ }
130
+
131
+ void CRYPTO_STATIC_MUTEX_unlock(struct CRYPTO_STATIC_MUTEX *lock) {
132
+ LeaveCriticalSection(&lock->lock);
133
+ }
134
+
135
+ static CRITICAL_SECTION g_destructors_lock;
136
+ static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS];
137
+
138
+ static CRYPTO_once_t g_thread_local_init_once = CRYPTO_ONCE_INIT;
139
+ static DWORD g_thread_local_key;
140
+ static int g_thread_local_failed;
141
+
142
+ static void thread_local_init(void) {
143
+ if (!InitializeCriticalSectionAndSpinCount(&g_destructors_lock, 0x400)) {
144
+ g_thread_local_failed = 1;
145
+ return;
146
+ }
147
+ g_thread_local_key = TlsAlloc();
148
+ g_thread_local_failed = (g_thread_local_key == TLS_OUT_OF_INDEXES);
149
+ }
150
+
151
+ static void NTAPI thread_local_destructor(PVOID module,
152
+ DWORD reason, PVOID reserved) {
153
+ if (DLL_THREAD_DETACH != reason && DLL_PROCESS_DETACH != reason) {
154
+ return;
155
+ }
156
+
157
+ CRYPTO_once(&g_thread_local_init_once, thread_local_init);
158
+ if (g_thread_local_failed) {
159
+ return;
160
+ }
161
+
162
+ void **pointers = (void**) TlsGetValue(g_thread_local_key);
163
+ if (pointers == NULL) {
164
+ return;
165
+ }
166
+
167
+ thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS];
168
+
169
+ EnterCriticalSection(&g_destructors_lock);
170
+ memcpy(destructors, g_destructors, sizeof(destructors));
171
+ LeaveCriticalSection(&g_destructors_lock);
172
+
173
+ unsigned i;
174
+ for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
175
+ if (destructors[i] != NULL) {
176
+ destructors[i](pointers[i]);
177
+ }
178
+ }
179
+
180
+ OPENSSL_free(pointers);
181
+ }
182
+
183
+ /* Thread Termination Callbacks.
184
+ *
185
+ * Windows doesn't support a per-thread destructor with its TLS primitives.
186
+ * So, we build it manually by inserting a function to be called on each
187
+ * thread's exit. This magic is from http://www.codeproject.com/threads/tls.asp
188
+ * and it works for VC++ 7.0 and later.
189
+ *
190
+ * Force a reference to _tls_used to make the linker create the TLS directory
191
+ * if it's not already there. (E.g. if __declspec(thread) is not used). Force
192
+ * a reference to p_thread_callback_boringssl to prevent whole program
193
+ * optimization from discarding the variable. */
194
+ #ifdef _WIN64
195
+ #pragma comment(linker, "/INCLUDE:_tls_used")
196
+ #pragma comment(linker, "/INCLUDE:p_thread_callback_boringssl")
197
+ #else
198
+ #pragma comment(linker, "/INCLUDE:__tls_used")
199
+ #pragma comment(linker, "/INCLUDE:_p_thread_callback_boringssl")
200
+ #endif
201
+
202
+ /* .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are
203
+ * called automatically by the OS loader code (not the CRT) when the module is
204
+ * loaded and on thread creation. They are NOT called if the module has been
205
+ * loaded by a LoadLibrary() call. It must have implicitly been loaded at
206
+ * process startup.
207
+ *
208
+ * By implicitly loaded, I mean that it is directly referenced by the main EXE
209
+ * or by one of its dependent DLLs. Delay-loaded DLL doesn't count as being
210
+ * implicitly loaded.
211
+ *
212
+ * See VC\crt\src\tlssup.c for reference. */
213
+
214
+ /* The linker must not discard p_thread_callback_boringssl. (We force a reference
215
+ * to this variable with a linker /INCLUDE:symbol pragma to ensure that.) If
216
+ * this variable is discarded, the OnThreadExit function will never be
217
+ * called. */
218
+ #ifdef _WIN64
219
+
220
+ /* .CRT section is merged with .rdata on x64 so it must be constant data. */
221
+ #pragma const_seg(".CRT$XLC")
222
+ /* When defining a const variable, it must have external linkage to be sure the
223
+ * linker doesn't discard it. */
224
+ extern const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl;
225
+ const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor;
226
+ /* Reset the default section. */
227
+ #pragma const_seg()
228
+
229
+ #else
230
+
231
+ #pragma data_seg(".CRT$XLC")
232
+ PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor;
233
+ /* Reset the default section. */
234
+ #pragma data_seg()
235
+
236
+ #endif /* _WIN64 */
237
+
238
+ void *CRYPTO_get_thread_local(thread_local_data_t index) {
239
+ CRYPTO_once(&g_thread_local_init_once, thread_local_init);
240
+ if (g_thread_local_failed) {
241
+ return NULL;
242
+ }
243
+
244
+ void **pointers = TlsGetValue(g_thread_local_key);
245
+ if (pointers == NULL) {
246
+ return NULL;
247
+ }
248
+ return pointers[index];
249
+ }
250
+
251
+ int CRYPTO_set_thread_local(thread_local_data_t index, void *value,
252
+ thread_local_destructor_t destructor) {
253
+ CRYPTO_once(&g_thread_local_init_once, thread_local_init);
254
+ if (g_thread_local_failed) {
255
+ destructor(value);
256
+ return 0;
257
+ }
258
+
259
+ void **pointers = TlsGetValue(g_thread_local_key);
260
+ if (pointers == NULL) {
261
+ pointers = OPENSSL_malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
262
+ if (pointers == NULL) {
263
+ destructor(value);
264
+ return 0;
265
+ }
266
+ memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
267
+ if (TlsSetValue(g_thread_local_key, pointers) == 0) {
268
+ OPENSSL_free(pointers);
269
+ destructor(value);
270
+ return 0;
271
+ }
272
+ }
273
+
274
+ EnterCriticalSection(&g_destructors_lock);
275
+ g_destructors[index] = destructor;
276
+ LeaveCriticalSection(&g_destructors_lock);
277
+
278
+ pointers[index] = value;
279
+ return 1;
280
+ }
281
+
282
+ #endif /* OPENSSL_WINDOWS && !OPENSSL_NO_THREADS */
@@ -0,0 +1,103 @@
1
+ // Copyright 2015 Brian Smith.
2
+ //
3
+ // Permission to use, copy, modify, and/or distribute this software for any
4
+ // purpose with or without fee is hereby granted, provided that the above
5
+ // copyright notice and this permission notice appear in all copies.
6
+ //
7
+ // THE SOFTWARE IS PROVIDED "AS IS" AND AND THE AUTHORS DISCLAIM ALL WARRANTIES
8
+ // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
10
+ // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12
+ // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13
+ // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
+
15
+ extern crate ring;
16
+ extern crate rustc_serialize;
17
+
18
+ use ring::*;
19
+ use rustc_serialize::hex::FromHex;
20
+ use std::error::Error;
21
+ use std::io::{Read, Write};
22
+
23
+
24
+ fn print_usage(program_name: &str) {
25
+ let program_file_name = std::path::Path::new(program_name)
26
+ .file_name().unwrap().to_str().unwrap();
27
+
28
+ println!(
29
+ "Usage: {} sha256|sha384|sha512 <digest value in hex> <filename>\n\
30
+ \n\
31
+ On success nothing is output, and 0 is returned.\n\
32
+ On failure, an error message is printed, and a non-zero value is returned\n\
33
+ \n\
34
+ Example:\n\
35
+ {} sha256 \
36
+ def7352915ac84bea5e2ed16f6fff712d35de519799777bf927e2a567ab53b7e \
37
+ LICENSE",
38
+ program_file_name, program_file_name);
39
+ }
40
+
41
+ fn run(digest_name: &str, expected_digest_hex: &str,
42
+ file_path: &std::path::Path) -> Result<(), &'static str> {
43
+ let digest_alg = match digest_name {
44
+ "sha256" => &digest::SHA256,
45
+ "sha384" => &digest::SHA384,
46
+ "sha512" => &digest::SHA512,
47
+ _ => { return Err("unsupported digest algorithm"); }
48
+ };
49
+
50
+ let mut ctx = digest::Context::new(digest_alg);
51
+
52
+ {
53
+ let mut file = match std::fs::File::open(file_path) {
54
+ Ok(file) => file,
55
+ // TODO: don't use panic here.
56
+ Err(why) => panic!("couldn't open {}: {}", file_path.display(),
57
+ why.description())
58
+ };
59
+
60
+ let mut chunk = vec![0u8; 128 * 1024];
61
+ loop {
62
+ match file.read(&mut chunk[..]) {
63
+ Ok(0) => break,
64
+ Ok(bytes_read) => ctx.update(&chunk[0..bytes_read]),
65
+ // TODO: don't use panic here
66
+ Err(why) => panic!("couldn't open {}: {}", file_path.display(),
67
+ why.description())
68
+ }
69
+ }
70
+ }
71
+
72
+ let actual_digest = ctx.finish();
73
+
74
+ let matched = match expected_digest_hex.from_hex() {
75
+ Ok(expected) => actual_digest.as_ref() == &expected[..],
76
+ Err(_) => panic!("syntactically invalid digest")
77
+ };
78
+
79
+ match matched {
80
+ true => Ok(()),
81
+ false => Err("digest mismatch") // TODO: calculated digest.
82
+ }
83
+ }
84
+
85
+ fn main() {
86
+ let args: Vec<String> = std::env::args().collect();
87
+
88
+ if args.iter().any(|arg| arg == "-h") {
89
+ print_usage(&args[0]);
90
+ return
91
+ } else if args.len() < 4 {
92
+ print_usage(&args[0]);
93
+ std::process::exit(1);
94
+ }
95
+
96
+ match run(&args[1], &args[2], std::path::Path::new(&args[3])) {
97
+ Ok(x) => x,
98
+ Err(s) => {
99
+ let _ = writeln!(&mut std::io::stderr(), "{}", s);
100
+ std::process::exit(1)
101
+ }
102
+ }
103
+ }
@@ -0,0 +1,121 @@
1
+ /* ====================================================================
2
+ * Copyright (c) 2002-2006 The OpenSSL Project. All rights reserved.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions
6
+ * are met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in
13
+ * the documentation and/or other materials provided with the
14
+ * distribution.
15
+ *
16
+ * 3. All advertising materials mentioning features or use of this
17
+ * software must display the following acknowledgment:
18
+ * "This product includes software developed by the OpenSSL Project
19
+ * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20
+ *
21
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22
+ * endorse or promote products derived from this software without
23
+ * prior written permission. For written permission, please contact
24
+ * openssl-core@openssl.org.
25
+ *
26
+ * 5. Products derived from this software may not be called "OpenSSL"
27
+ * nor may "OpenSSL" appear in their names without prior written
28
+ * permission of the OpenSSL Project.
29
+ *
30
+ * 6. Redistributions of any form whatsoever must retain the following
31
+ * acknowledgment:
32
+ * "This product includes software developed by the OpenSSL Project
33
+ * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34
+ *
35
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
47
+ * ==================================================================== */
48
+
49
+ #ifndef OPENSSL_HEADER_AES_H
50
+ #define OPENSSL_HEADER_AES_H
51
+
52
+ #include <openssl/base.h>
53
+
54
+ #if defined(__cplusplus)
55
+ extern "C" {
56
+ #endif
57
+
58
+
59
+ /* Raw AES functions. */
60
+
61
+
62
+ #define AES_ENCRYPT 1
63
+ #define AES_DECRYPT 0
64
+
65
+ /* AES_MAXNR is the maximum number of AES rounds. */
66
+ #define AES_MAXNR 14
67
+
68
+ #define AES_BLOCK_SIZE 16
69
+
70
+ /* aes_key_st should be an opaque type, but EVP requires that the size be
71
+ * known. */
72
+ struct aes_key_st {
73
+ uint32_t rd_key[4 * (AES_MAXNR + 1)];
74
+ unsigned rounds;
75
+ };
76
+ typedef struct aes_key_st AES_KEY;
77
+
78
+ /* AES_set_encrypt_key configures |aeskey| to encrypt with the |bits|-bit key,
79
+ * |key|.
80
+ *
81
+ * WARNING: unlike other OpenSSL functions, this returns zero on success and a
82
+ * negative number on error. */
83
+ OPENSSL_EXPORT int AES_set_encrypt_key(const uint8_t *key, unsigned bits,
84
+ AES_KEY *aeskey);
85
+
86
+ /* AES_set_decrypt_key configures |aeskey| to decrypt with the |bits|-bit key,
87
+ * |key|.
88
+ *
89
+ * WARNING: unlike other OpenSSL functions, this returns zero on success and a
90
+ * negative number on error. */
91
+ OPENSSL_EXPORT int AES_set_decrypt_key(const uint8_t *key, unsigned bits,
92
+ AES_KEY *aeskey);
93
+
94
+ /* AES_encrypt encrypts a single block from |in| to |out| with |key|. The |in|
95
+ * and |out| pointers may overlap. */
96
+ OPENSSL_EXPORT void AES_encrypt(const uint8_t *in, uint8_t *out,
97
+ const AES_KEY *key);
98
+
99
+ /* AES_decrypt decrypts a single block from |in| to |out| with |key|. The |in|
100
+ * and |out| pointers may overlap. */
101
+ OPENSSL_EXPORT void AES_decrypt(const uint8_t *in, uint8_t *out,
102
+ const AES_KEY *key);
103
+
104
+
105
+ /* Block cipher modes. */
106
+
107
+ /* AES_ctr128_encrypt encrypts (or decrypts, it's the same in CTR mode) |len|
108
+ * bytes from |in| to |out|. The |num| parameter must be set to zero on the
109
+ * first call and |ivec| will be incremented. */
110
+ OPENSSL_EXPORT void AES_ctr128_encrypt(const uint8_t *in, uint8_t *out,
111
+ size_t len, const AES_KEY *key,
112
+ uint8_t ivec[AES_BLOCK_SIZE],
113
+ uint8_t ecount_buf[AES_BLOCK_SIZE],
114
+ unsigned int *num);
115
+
116
+
117
+ #if defined(__cplusplus)
118
+ } /* extern C */
119
+ #endif
120
+
121
+ #endif /* OPENSSL_HEADER_AES_H */