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,56 @@
1
+ /* Copyright (c) 2014, 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 <openssl/rand.h>
16
+
17
+ #if defined(OPENSSL_WINDOWS)
18
+
19
+ #include <limits.h>
20
+ #include <stdlib.h>
21
+
22
+ #pragma warning(push, 3)
23
+
24
+ #include <windows.h>
25
+
26
+ /* #define needed to link in RtlGenRandom(), a.k.a. SystemFunction036. See the
27
+ * "Community Additions" comment on MSDN here:
28
+ * http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx */
29
+ #define SystemFunction036 NTAPI SystemFunction036
30
+ #include <ntsecapi.h>
31
+ #undef SystemFunction036
32
+
33
+ #pragma warning(pop)
34
+
35
+ #include "internal.h"
36
+
37
+
38
+ void RAND_cleanup(void) {
39
+ }
40
+
41
+ void CRYPTO_sysrand(uint8_t *out, size_t requested) {
42
+ while (requested > 0) {
43
+ ULONG output_bytes_this_pass = ULONG_MAX;
44
+ if (requested < output_bytes_this_pass) {
45
+ output_bytes_this_pass = requested;
46
+ }
47
+ if (RtlGenRandom(out, output_bytes_this_pass) == FALSE) {
48
+ abort();
49
+ }
50
+ requested -= output_bytes_this_pass;
51
+ out += output_bytes_this_pass;
52
+ }
53
+ return;
54
+ }
55
+
56
+ #endif /* OPENSSL_WINDOWS */
@@ -0,0 +1,66 @@
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
+
18
+ #if defined(OPENSSL_C11_ATOMIC)
19
+
20
+ #include <assert.h>
21
+ #include <stdatomic.h>
22
+ #include <stdlib.h>
23
+
24
+ #include <openssl/type_check.h>
25
+
26
+
27
+ /* See comment above the typedef of CRYPTO_refcount_t about these tests. */
28
+ static_assert(alignof(CRYPTO_refcount_t) == alignof(_Atomic CRYPTO_refcount_t),
29
+ "_Atomic alters the needed alignment of a reference count");
30
+ static_assert(sizeof(CRYPTO_refcount_t) == sizeof(_Atomic CRYPTO_refcount_t),
31
+ "_Atomic alters the size of a reference count");
32
+
33
+ static_assert((CRYPTO_refcount_t)-1 == CRYPTO_REFCOUNT_MAX,
34
+ "CRYPTO_REFCOUNT_MAX is incorrect");
35
+
36
+ void CRYPTO_refcount_inc(CRYPTO_refcount_t *in_count) {
37
+ _Atomic CRYPTO_refcount_t *count = (_Atomic CRYPTO_refcount_t *) in_count;
38
+ uint32_t expected = atomic_load(count);
39
+
40
+ while (expected != CRYPTO_REFCOUNT_MAX) {
41
+ uint32_t new_value = expected + 1;
42
+ if (atomic_compare_exchange_weak(count, &expected, new_value)) {
43
+ break;
44
+ }
45
+ }
46
+ }
47
+
48
+ int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *in_count) {
49
+ _Atomic CRYPTO_refcount_t *count = (_Atomic CRYPTO_refcount_t *)in_count;
50
+ uint32_t expected = atomic_load(count);
51
+
52
+ for (;;) {
53
+ if (expected == 0) {
54
+ abort();
55
+ } else if (expected == CRYPTO_REFCOUNT_MAX) {
56
+ return 0;
57
+ } else {
58
+ const uint32_t new_value = expected - 1;
59
+ if (atomic_compare_exchange_weak(count, &expected, new_value)) {
60
+ return new_value == 0;
61
+ }
62
+ }
63
+ }
64
+ }
65
+
66
+ #endif /* OPENSSL_C11_ATOMIC */
@@ -0,0 +1,53 @@
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
+ #include <stdlib.h>
18
+
19
+ #include <openssl/type_check.h>
20
+
21
+
22
+ #if !defined(OPENSSL_C11_ATOMIC)
23
+
24
+ OPENSSL_COMPILE_ASSERT((CRYPTO_refcount_t)-1 == CRYPTO_REFCOUNT_MAX,
25
+ CRYPTO_REFCOUNT_MAX_is_incorrect);
26
+
27
+ static struct CRYPTO_STATIC_MUTEX g_refcount_lock = CRYPTO_STATIC_MUTEX_INIT;
28
+
29
+ void CRYPTO_refcount_inc(CRYPTO_refcount_t *count) {
30
+ CRYPTO_STATIC_MUTEX_lock_write(&g_refcount_lock);
31
+ if (*count < CRYPTO_REFCOUNT_MAX) {
32
+ (*count)++;
33
+ }
34
+ CRYPTO_STATIC_MUTEX_unlock(&g_refcount_lock);
35
+ }
36
+
37
+ int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count) {
38
+ int ret;
39
+
40
+ CRYPTO_STATIC_MUTEX_lock_write(&g_refcount_lock);
41
+ if (*count == 0) {
42
+ abort();
43
+ }
44
+ if (*count < CRYPTO_REFCOUNT_MAX) {
45
+ (*count)--;
46
+ }
47
+ ret = (*count == 0);
48
+ CRYPTO_STATIC_MUTEX_unlock(&g_refcount_lock);
49
+
50
+ return ret;
51
+ }
52
+
53
+ #endif /* OPENSSL_C11_ATOMIC */
@@ -0,0 +1,25 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3
+ <PropertyGroup Label="Globals">
4
+ <ProjectGuid>{5C80997F-DB68-4996-BF6D-2B0EAF69D035}</ProjectGuid>
5
+ <TargetName>refcount_test</TargetName>
6
+ </PropertyGroup>
7
+ <ImportGroup Label="PropertySheets">
8
+ <Import Project="..\mk\WindowsTest.props" />
9
+ </ImportGroup>
10
+ <PropertyGroup Label="Configuration">
11
+ <OutDir>$(OutRootDir)test\ring\crypto\</OutDir>
12
+ </PropertyGroup>
13
+ <ItemGroup>
14
+ <ClCompile Include="refcount_test.c" />
15
+ </ItemGroup>
16
+ <ItemGroup>
17
+ <ProjectReference Include="libring.Windows.vcxproj">
18
+ <Project>{f4c0a1b6-5e09-41c8-8242-3e1f6762fb18}</Project>
19
+ </ProjectReference>
20
+ <ProjectReference Include="test\test.Windows.vcxproj">
21
+ <Project>{1dace503-6498-492d-b1ff-f9ee18624443}</Project>
22
+ </ProjectReference>
23
+ </ItemGroup>
24
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
25
+ </Project>
@@ -0,0 +1,58 @@
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
+ #include <stdio.h>
18
+
19
+ #include <openssl/type_check.h>
20
+
21
+
22
+ int main(int argc, char **argv) {
23
+ CRYPTO_refcount_t count = 0;
24
+
25
+ CRYPTO_refcount_inc(&count);
26
+ if (count != 1) {
27
+ fprintf(stderr, "Incrementing reference count did not work.\n");
28
+ return 1;
29
+ }
30
+ if (!CRYPTO_refcount_dec_and_test_zero(&count) || count != 0) {
31
+ fprintf(stderr, "Decrementing reference count to zero did not work.\n");
32
+ return 1;
33
+ }
34
+
35
+ count = CRYPTO_REFCOUNT_MAX;
36
+ CRYPTO_refcount_inc(&count);
37
+ if (count != CRYPTO_REFCOUNT_MAX) {
38
+ fprintf(stderr, "Count did not saturate correctly when incrementing.\n");
39
+ return 1;
40
+ }
41
+ if (CRYPTO_refcount_dec_and_test_zero(&count) ||
42
+ count != CRYPTO_REFCOUNT_MAX) {
43
+ fprintf(stderr, "Count did not saturate correctly when decrementing.\n");
44
+ return 1;
45
+ }
46
+
47
+ count = 2;
48
+ if (CRYPTO_refcount_dec_and_test_zero(&count)) {
49
+ fprintf(stderr, "Decrementing two resulted in zero!\n");
50
+ return 1;
51
+ }
52
+ if (count != 1) {
53
+ fprintf(stderr, "Decrementing two did not produce one!");
54
+ return 1;
55
+ }
56
+
57
+ return 0;
58
+ }
@@ -0,0 +1,462 @@
1
+ /* ====================================================================
2
+ * Copyright (c) 1998-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
+ * This product includes cryptographic software written by Eric Young
50
+ * (eay@cryptsoft.com). This product includes software written by Tim
51
+ * Hudson (tjh@cryptsoft.com).
52
+ *
53
+ * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
54
+ * All rights reserved.
55
+ *
56
+ * This package is an SSL implementation written
57
+ * by Eric Young (eay@cryptsoft.com).
58
+ * The implementation was written so as to conform with Netscapes SSL.
59
+ *
60
+ * This library is free for commercial and non-commercial use as long as
61
+ * the following conditions are aheared to. The following conditions
62
+ * apply to all code found in this distribution, be it the RC4, RSA,
63
+ * lhash, DES, etc., code; not just the SSL code. The SSL documentation
64
+ * included with this distribution is covered by the same copyright terms
65
+ * except that the holder is Tim Hudson (tjh@cryptsoft.com).
66
+ *
67
+ * Copyright remains Eric Young's, and as such any Copyright notices in
68
+ * the code are not to be removed.
69
+ * If this package is used in a product, Eric Young should be given attribution
70
+ * as the author of the parts of the library used.
71
+ * This can be in the form of a textual message at program startup or
72
+ * in documentation (online or textual) provided with the package.
73
+ *
74
+ * Redistribution and use in source and binary forms, with or without
75
+ * modification, are permitted provided that the following conditions
76
+ * are met:
77
+ * 1. Redistributions of source code must retain the copyright
78
+ * notice, this list of conditions and the following disclaimer.
79
+ * 2. Redistributions in binary form must reproduce the above copyright
80
+ * notice, this list of conditions and the following disclaimer in the
81
+ * documentation and/or other materials provided with the distribution.
82
+ * 3. All advertising materials mentioning features or use of this software
83
+ * must display the following acknowledgement:
84
+ * "This product includes cryptographic software written by
85
+ * Eric Young (eay@cryptsoft.com)"
86
+ * The word 'cryptographic' can be left out if the rouines from the library
87
+ * being used are not cryptographic related :-).
88
+ * 4. If you include any Windows specific code (or a derivative thereof) from
89
+ * the apps directory (application code) you must include an acknowledgement:
90
+ * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
91
+ *
92
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
93
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
95
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
96
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
97
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
98
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
100
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
101
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
102
+ * SUCH DAMAGE.
103
+ *
104
+ * The licence and distribution terms for any publically available version or
105
+ * derivative of this code cannot be changed. i.e. this code cannot simply be
106
+ * copied and put under another distribution licence
107
+ * [including the GNU Public Licence.] */
108
+
109
+ #include <openssl/rsa.h>
110
+
111
+ #include <string.h>
112
+
113
+ #include <openssl/bn.h>
114
+ #include <openssl/mem.h>
115
+ #include <openssl/err.h>
116
+ #include <openssl/thread.h>
117
+
118
+ #include "internal.h"
119
+
120
+
121
+ #define BN_BLINDING_COUNTER 32
122
+
123
+ struct bn_blinding_st {
124
+ BIGNUM *A;
125
+ BIGNUM *Ai;
126
+ BIGNUM *e;
127
+ BIGNUM *mod; /* just a reference */
128
+ int counter;
129
+ unsigned long flags;
130
+ /* mont is the Montgomery context used for this |BN_BLINDING|. It is not
131
+ * owned and must outlive this structure. */
132
+ const BN_MONT_CTX *mont;
133
+ int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
134
+ const BIGNUM *m, BN_CTX *ctx, const BN_MONT_CTX *mont);
135
+ };
136
+
137
+ BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod) {
138
+ BN_BLINDING *ret = NULL;
139
+
140
+ ret = (BN_BLINDING*) OPENSSL_malloc(sizeof(BN_BLINDING));
141
+ if (ret == NULL) {
142
+ OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
143
+ return NULL;
144
+ }
145
+ memset(ret, 0, sizeof(BN_BLINDING));
146
+ if (A != NULL) {
147
+ ret->A = BN_dup(A);
148
+ if (ret->A == NULL) {
149
+ goto err;
150
+ }
151
+ }
152
+ if (Ai != NULL) {
153
+ ret->Ai = BN_dup(Ai);
154
+ if (ret->Ai == NULL) {
155
+ goto err;
156
+ }
157
+ }
158
+
159
+ /* save a copy of mod in the BN_BLINDING structure */
160
+ ret->mod = BN_dup(mod);
161
+ if (ret->mod == NULL) {
162
+ goto err;
163
+ }
164
+ if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0) {
165
+ BN_set_flags(ret->mod, BN_FLG_CONSTTIME);
166
+ }
167
+
168
+ /* Set the counter to the special value -1
169
+ * to indicate that this is never-used fresh blinding
170
+ * that does not need updating before first use. */
171
+ ret->counter = -1;
172
+ return ret;
173
+
174
+ err:
175
+ BN_BLINDING_free(ret);
176
+ return NULL;
177
+ }
178
+
179
+ void BN_BLINDING_free(BN_BLINDING *r) {
180
+ if (r == NULL) {
181
+ return;
182
+ }
183
+
184
+ BN_free(r->A);
185
+ BN_free(r->Ai);
186
+ BN_free(r->e);
187
+ BN_free(r->mod);
188
+ OPENSSL_free(r);
189
+ }
190
+
191
+ int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx) {
192
+ int ret = 0;
193
+
194
+ if (b->A == NULL || b->Ai == NULL) {
195
+ OPENSSL_PUT_ERROR(RSA, RSA_R_BN_NOT_INITIALIZED);
196
+ goto err;
197
+ }
198
+
199
+ if (b->counter == -1) {
200
+ b->counter = 0;
201
+ }
202
+
203
+ if (++b->counter == BN_BLINDING_COUNTER && b->e != NULL &&
204
+ !(b->flags & BN_BLINDING_NO_RECREATE)) {
205
+ /* re-create blinding parameters */
206
+ if (!BN_BLINDING_create_param(b, NULL, NULL, ctx, NULL, NULL)) {
207
+ goto err;
208
+ }
209
+ } else if (!(b->flags & BN_BLINDING_NO_UPDATE)) {
210
+ if (!BN_mod_mul(b->A, b->A, b->A, b->mod, ctx)) {
211
+ goto err;
212
+ }
213
+ if (!BN_mod_mul(b->Ai, b->Ai, b->Ai, b->mod, ctx)) {
214
+ goto err;
215
+ }
216
+ }
217
+
218
+ ret = 1;
219
+
220
+ err:
221
+ if (b->counter == BN_BLINDING_COUNTER) {
222
+ b->counter = 0;
223
+ }
224
+ return ret;
225
+ }
226
+
227
+ int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx) {
228
+ return BN_BLINDING_convert_ex(n, NULL, b, ctx);
229
+ }
230
+
231
+ int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx) {
232
+ int ret = 1;
233
+
234
+ if (b->A == NULL || b->Ai == NULL) {
235
+ OPENSSL_PUT_ERROR(RSA, RSA_R_BN_NOT_INITIALIZED);
236
+ return 0;
237
+ }
238
+
239
+ if (b->counter == -1) {
240
+ /* Fresh blinding, doesn't need updating. */
241
+ b->counter = 0;
242
+ } else if (!BN_BLINDING_update(b, ctx)) {
243
+ return 0;
244
+ }
245
+
246
+ if (r != NULL) {
247
+ if (!BN_copy(r, b->Ai)) {
248
+ ret = 0;
249
+ }
250
+ }
251
+
252
+ if (!BN_mod_mul(n, n, b->A, b->mod, ctx)) {
253
+ ret = 0;
254
+ }
255
+
256
+ return ret;
257
+ }
258
+
259
+ int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx) {
260
+ return BN_BLINDING_invert_ex(n, NULL, b, ctx);
261
+ }
262
+
263
+ int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b,
264
+ BN_CTX *ctx) {
265
+ int ret;
266
+
267
+ if (r != NULL) {
268
+ ret = BN_mod_mul(n, n, r, b->mod, ctx);
269
+ } else {
270
+ if (b->Ai == NULL) {
271
+ OPENSSL_PUT_ERROR(RSA, RSA_R_BN_NOT_INITIALIZED);
272
+ return 0;
273
+ }
274
+ ret = BN_mod_mul(n, n, b->Ai, b->mod, ctx);
275
+ }
276
+
277
+ return ret;
278
+ }
279
+
280
+ unsigned long BN_BLINDING_get_flags(const BN_BLINDING *b) { return b->flags; }
281
+
282
+ void BN_BLINDING_set_flags(BN_BLINDING *b, unsigned long flags) {
283
+ b->flags = flags;
284
+ }
285
+
286
+ BN_BLINDING *BN_BLINDING_create_param(
287
+ BN_BLINDING *b, const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
288
+ int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
289
+ const BIGNUM *m, BN_CTX *ctx, const BN_MONT_CTX *mont),
290
+ const BN_MONT_CTX *mont) {
291
+ int retry_counter = 32;
292
+ BN_BLINDING *ret = NULL;
293
+
294
+ if (b == NULL) {
295
+ ret = BN_BLINDING_new(NULL, NULL, m);
296
+ } else {
297
+ ret = b;
298
+ }
299
+
300
+ if (ret == NULL) {
301
+ goto err;
302
+ }
303
+
304
+ if (ret->A == NULL && (ret->A = BN_new()) == NULL) {
305
+ goto err;
306
+ }
307
+ if (ret->Ai == NULL && (ret->Ai = BN_new()) == NULL) {
308
+ goto err;
309
+ }
310
+
311
+ if (e != NULL) {
312
+ BN_free(ret->e);
313
+ ret->e = BN_dup(e);
314
+ }
315
+ if (ret->e == NULL) {
316
+ goto err;
317
+ }
318
+
319
+ if (bn_mod_exp != NULL) {
320
+ ret->bn_mod_exp = bn_mod_exp;
321
+ }
322
+ if (mont != NULL) {
323
+ ret->mont = mont;
324
+ }
325
+
326
+ do {
327
+ if (!BN_rand_range(ret->A, ret->mod)) {
328
+ goto err;
329
+ }
330
+
331
+ int no_inverse;
332
+ if (BN_mod_inverse_ex(ret->Ai, &no_inverse, ret->A, ret->mod, ctx) == NULL) {
333
+ /* this should almost never happen for good RSA keys */
334
+ if (no_inverse) {
335
+ if (retry_counter-- == 0) {
336
+ OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_MANY_ITERATIONS);
337
+ goto err;
338
+ }
339
+ ERR_clear_error();
340
+ } else {
341
+ goto err;
342
+ }
343
+ } else {
344
+ break;
345
+ }
346
+ } while (1);
347
+
348
+ if (ret->bn_mod_exp != NULL && ret->mont != NULL) {
349
+ if (!ret->bn_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx, ret->mont)) {
350
+ goto err;
351
+ }
352
+ } else {
353
+ if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx)) {
354
+ goto err;
355
+ }
356
+ }
357
+
358
+ return ret;
359
+
360
+ err:
361
+ if (b == NULL) {
362
+ BN_BLINDING_free(ret);
363
+ ret = NULL;
364
+ }
365
+
366
+ return ret;
367
+ }
368
+
369
+ static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p,
370
+ const BIGNUM *q, BN_CTX *ctx) {
371
+ BIGNUM *ret = NULL, *r0, *r1, *r2;
372
+
373
+ if (d == NULL || p == NULL || q == NULL) {
374
+ return NULL;
375
+ }
376
+
377
+ BN_CTX_start(ctx);
378
+ r0 = BN_CTX_get(ctx);
379
+ r1 = BN_CTX_get(ctx);
380
+ r2 = BN_CTX_get(ctx);
381
+ if (r2 == NULL) {
382
+ goto err;
383
+ }
384
+
385
+ if (!BN_sub(r1, p, BN_value_one())) {
386
+ goto err;
387
+ }
388
+ if (!BN_sub(r2, q, BN_value_one())) {
389
+ goto err;
390
+ }
391
+ if (!BN_mul(r0, r1, r2, ctx)) {
392
+ goto err;
393
+ }
394
+
395
+ ret = BN_mod_inverse(NULL, d, r0, ctx);
396
+
397
+ err:
398
+ BN_CTX_end(ctx);
399
+ return ret;
400
+ }
401
+
402
+ BN_BLINDING *rsa_setup_blinding(RSA *rsa, BN_CTX *in_ctx) {
403
+ BIGNUM local_n;
404
+ BIGNUM *e, *n;
405
+ BN_CTX *ctx;
406
+ BN_BLINDING *ret = NULL;
407
+ BN_MONT_CTX *mont_ctx = NULL;
408
+
409
+ if (in_ctx == NULL) {
410
+ ctx = BN_CTX_new();
411
+ if (ctx == NULL) {
412
+ return 0;
413
+ }
414
+ } else {
415
+ ctx = in_ctx;
416
+ }
417
+
418
+ BN_CTX_start(ctx);
419
+ e = BN_CTX_get(ctx);
420
+ if (e == NULL) {
421
+ OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
422
+ goto err;
423
+ }
424
+
425
+ if (rsa->e == NULL) {
426
+ e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx);
427
+ if (e == NULL) {
428
+ OPENSSL_PUT_ERROR(RSA, RSA_R_NO_PUBLIC_EXPONENT);
429
+ goto err;
430
+ }
431
+ } else {
432
+ e = rsa->e;
433
+ }
434
+
435
+ n = &local_n;
436
+ BN_with_flags(n, rsa->n, BN_FLG_CONSTTIME);
437
+
438
+ if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
439
+ mont_ctx = BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx);
440
+ if (mont_ctx == NULL) {
441
+ goto err;
442
+ }
443
+ }
444
+
445
+ ret = BN_BLINDING_create_param(NULL, e, n, ctx, BN_mod_exp_mont,
446
+ mont_ctx);
447
+ if (ret == NULL) {
448
+ OPENSSL_PUT_ERROR(RSA, ERR_R_BN_LIB);
449
+ goto err;
450
+ }
451
+
452
+ err:
453
+ BN_CTX_end(ctx);
454
+ if (in_ctx == NULL) {
455
+ BN_CTX_free(ctx);
456
+ }
457
+ if (rsa->e == NULL) {
458
+ BN_free(e);
459
+ }
460
+
461
+ return ret;
462
+ }