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,316 @@
1
+ /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
2
+ * All rights reserved.
3
+ *
4
+ * This package is an SSL implementation written
5
+ * by Eric Young (eay@cryptsoft.com).
6
+ * The implementation was written so as to conform with Netscapes SSL.
7
+ *
8
+ * This library is free for commercial and non-commercial use as long as
9
+ * the following conditions are aheared to. The following conditions
10
+ * apply to all code found in this distribution, be it the RC4, RSA,
11
+ * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12
+ * included with this distribution is covered by the same copyright terms
13
+ * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
+ *
15
+ * Copyright remains Eric Young's, and as such any Copyright notices in
16
+ * the code are not to be removed.
17
+ * If this package is used in a product, Eric Young should be given attribution
18
+ * as the author of the parts of the library used.
19
+ * This can be in the form of a textual message at program startup or
20
+ * in documentation (online or textual) provided with the package.
21
+ *
22
+ * Redistribution and use in source and binary forms, with or without
23
+ * modification, are permitted provided that the following conditions
24
+ * are met:
25
+ * 1. Redistributions of source code must retain the copyright
26
+ * notice, this list of conditions and the following disclaimer.
27
+ * 2. Redistributions in binary form must reproduce the above copyright
28
+ * notice, this list of conditions and the following disclaimer in the
29
+ * documentation and/or other materials provided with the distribution.
30
+ * 3. All advertising materials mentioning features or use of this software
31
+ * must display the following acknowledgement:
32
+ * "This product includes cryptographic software written by
33
+ * Eric Young (eay@cryptsoft.com)"
34
+ * The word 'cryptographic' can be left out if the rouines from the library
35
+ * being used are not cryptographic related :-).
36
+ * 4. If you include any Windows specific code (or a derivative thereof) from
37
+ * the apps directory (application code) you must include an acknowledgement:
38
+ * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
+ *
40
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
+ * SUCH DAMAGE.
51
+ *
52
+ * The licence and distribution terms for any publically available version or
53
+ * derivative of this code cannot be changed. i.e. this code cannot simply be
54
+ * copied and put under another distribution licence
55
+ * [including the GNU Public Licence.]
56
+ */
57
+ /* ====================================================================
58
+ * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
59
+ *
60
+ * Redistribution and use in source and binary forms, with or without
61
+ * modification, are permitted provided that the following conditions
62
+ * are met:
63
+ *
64
+ * 1. Redistributions of source code must retain the above copyright
65
+ * notice, this list of conditions and the following disclaimer.
66
+ *
67
+ * 2. Redistributions in binary form must reproduce the above copyright
68
+ * notice, this list of conditions and the following disclaimer in
69
+ * the documentation and/or other materials provided with the
70
+ * distribution.
71
+ *
72
+ * 3. All advertising materials mentioning features or use of this
73
+ * software must display the following acknowledgment:
74
+ * "This product includes software developed by the OpenSSL Project
75
+ * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76
+ *
77
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78
+ * endorse or promote products derived from this software without
79
+ * prior written permission. For written permission, please contact
80
+ * openssl-core@openssl.org.
81
+ *
82
+ * 5. Products derived from this software may not be called "OpenSSL"
83
+ * nor may "OpenSSL" appear in their names without prior written
84
+ * permission of the OpenSSL Project.
85
+ *
86
+ * 6. Redistributions of any form whatsoever must retain the following
87
+ * acknowledgment:
88
+ * "This product includes software developed by the OpenSSL Project
89
+ * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90
+ *
91
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
103
+ * ====================================================================
104
+ *
105
+ * This product includes cryptographic software written by Eric Young
106
+ * (eay@cryptsoft.com). This product includes software written by Tim
107
+ * Hudson (tjh@cryptsoft.com).
108
+ *
109
+ */
110
+ /* ====================================================================
111
+ * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112
+ *
113
+ * Portions of the attached software ("Contribution") are developed by
114
+ * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115
+ *
116
+ * The Contribution is licensed pursuant to the Eric Young open source
117
+ * license provided above.
118
+ *
119
+ * The binary polynomial arithmetic software is originally written by
120
+ * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
121
+ * Laboratories. */
122
+
123
+ #ifndef OPENSSL_HEADER_BN_INTERNAL_H
124
+ #define OPENSSL_HEADER_BN_INTERNAL_H
125
+
126
+ #include <openssl/base.h>
127
+
128
+ #if defined(OPENSSL_X86_64) && defined(_MSC_VER) && _MSC_VER >= 1400
129
+ #pragma warning(push, 3)
130
+ #include <intrin.h>
131
+ #pragma warning(pop)
132
+ #pragma intrinsic(__umulh, _umul128)
133
+ #endif
134
+
135
+ #if defined(__cplusplus)
136
+ extern "C" {
137
+ #endif
138
+
139
+ /* bn_expand acts the same as |bn_wexpand|, but takes a number of bits rather
140
+ * than a number of words. */
141
+ BIGNUM *bn_expand(BIGNUM *bn, size_t bits);
142
+
143
+ #if defined(OPENSSL_64_BIT)
144
+
145
+ #if !defined(_MSC_VER)
146
+ /* MSVC doesn't support two-word integers on 64-bit. */
147
+ #define BN_ULLONG __uint128_t
148
+ #endif
149
+
150
+ #define BN_BITS2 64
151
+ #define BN_BYTES 8
152
+ #define BN_BITS4 32
153
+ #define BN_MASK2 (0xffffffffffffffffUL)
154
+ #define BN_MASK2l (0xffffffffUL)
155
+ #define BN_MASK2h (0xffffffff00000000UL)
156
+ #define BN_MASK2h1 (0xffffffff80000000UL)
157
+ #define BN_TBIT (0x8000000000000000UL)
158
+ #define BN_DEC_CONV (10000000000000000000UL)
159
+ #define BN_DEC_NUM 19
160
+ #define TOBN(hi, lo) ((BN_ULONG)hi << 32 | lo)
161
+
162
+ #elif defined(OPENSSL_32_BIT)
163
+
164
+ #define BN_ULLONG uint64_t
165
+ #define BN_BITS2 32
166
+ #define BN_BYTES 4
167
+ #define BN_BITS4 16
168
+ #define BN_MASK2 (0xffffffffUL)
169
+ #define BN_MASK2l (0xffffUL)
170
+ #define BN_MASK2h1 (0xffff8000UL)
171
+ #define BN_MASK2h (0xffff0000UL)
172
+ #define BN_TBIT (0x80000000UL)
173
+ #define BN_DEC_CONV (1000000000UL)
174
+ #define BN_DEC_NUM 9
175
+ #define TOBN(hi, lo) lo, hi
176
+
177
+ #else
178
+ #error "Must define either OPENSSL_32_BIT or OPENSSL_64_BIT"
179
+ #endif
180
+
181
+
182
+ /* Pentium pro 16,16,16,32,64 */
183
+ /* Alpha 16,16,16,16.64 */
184
+ #define BN_MULL_SIZE_NORMAL (16) /* 32 */
185
+ #define BN_MUL_RECURSIVE_SIZE_NORMAL (16) /* 32 less than */
186
+ #define BN_SQR_RECURSIVE_SIZE_NORMAL (16) /* 32 */
187
+ #define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL (32) /* 32 */
188
+ #define BN_MONT_CTX_SET_SIZE_WORD (64) /* 32 */
189
+
190
+ #define STATIC_BIGNUM(x) \
191
+ { \
192
+ (BN_ULONG *)x, sizeof(x) / sizeof(BN_ULONG), \
193
+ sizeof(x) / sizeof(BN_ULONG), 0, BN_FLG_STATIC_DATA \
194
+ }
195
+
196
+ #if defined(BN_ULLONG)
197
+ #define Lw(t) (((BN_ULONG)(t))&BN_MASK2)
198
+ #define Hw(t) (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2)
199
+ #endif
200
+
201
+ /* bn_set_words sets |bn| to the value encoded in the |num| words in |words|,
202
+ * least significant word first. */
203
+ int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num);
204
+
205
+ BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
206
+ BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
207
+ void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);
208
+ BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
209
+ BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int num);
210
+ BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int num);
211
+
212
+ void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
213
+ void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b);
214
+ void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a);
215
+ void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a);
216
+
217
+ /* bn_cmp_words returns a value less than, equal to or greater than zero if
218
+ * the, length |n|, array |a| is less than, equal to or greater than |b|. */
219
+ int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n);
220
+
221
+ /* bn_cmp_words returns a value less than, equal to or greater than zero if the
222
+ * array |a| is less than, equal to or greater than |b|. The arrays can be of
223
+ * different lengths: |cl| gives the minimum of the two lengths and |dl| gives
224
+ * the length of |a| minus the length of |b|. */
225
+ int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl);
226
+
227
+ int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
228
+ const BN_ULONG *np, const BN_ULONG *n0, int num);
229
+
230
+ #if !defined(OPENSSL_NO_ASM) && \
231
+ (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
232
+ defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
233
+ #define OPENSSL_BN_ASM_MONT
234
+ #endif
235
+
236
+ /* On some 32-bit platforms, Montgomery multiplication is done using 64-bit
237
+ * arithmetic with SIMD instructions. On such platforms, |BN_MONT_CTX::n0|
238
+ * needs to be two words long. Only certain 32-bit platforms actually make use
239
+ * of n0[1] and shorter R value would suffice for the others. However,
240
+ * currently only the assembly files know which is which. */
241
+ #if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2 <= 32)
242
+ #define BN_MONT_CTX_N0_LIMBS 2
243
+ #else
244
+ #define BN_MONT_CTX_N0_LIMBS 1
245
+ #endif
246
+
247
+
248
+ #if !defined(BN_ULLONG)
249
+
250
+ #define LBITS(a) ((a) & BN_MASK2l)
251
+ #define HBITS(a) (((a) >> BN_BITS4) & BN_MASK2l)
252
+ #define L2HBITS(a) (((a) << BN_BITS4) & BN_MASK2)
253
+
254
+ #define LLBITS(a) ((a) & BN_MASKl)
255
+ #define LHBITS(a) (((a) >> BN_BITS2) & BN_MASKl)
256
+ #define LL2HBITS(a) ((BN_ULLONG)((a) & BN_MASKl) << BN_BITS2)
257
+
258
+ #define mul64(l, h, bl, bh) \
259
+ { \
260
+ BN_ULONG m, m1, lt, ht; \
261
+ \
262
+ lt = l; \
263
+ ht = h; \
264
+ m = (bh) * (lt); \
265
+ lt = (bl) * (lt); \
266
+ m1 = (bl) * (ht); \
267
+ ht = (bh) * (ht); \
268
+ m = (m + m1) & BN_MASK2; \
269
+ if (m < m1) \
270
+ ht += L2HBITS((BN_ULONG)1); \
271
+ ht += HBITS(m); \
272
+ m1 = L2HBITS(m); \
273
+ lt = (lt + m1) & BN_MASK2; \
274
+ if (lt < m1) \
275
+ ht++; \
276
+ (l) = lt; \
277
+ (h) = ht; \
278
+ }
279
+
280
+ #endif /* !defined(BN_ULLONG) */
281
+
282
+ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64)
283
+ # if defined(__GNUC__) && __GNUC__ >= 2
284
+ # define BN_UMULT_HIGH(a,b) ({ \
285
+ register BN_ULONG ret,discard; \
286
+ __asm__ ("mulq %3" \
287
+ : "=a"(discard),"=d"(ret) \
288
+ : "a"(a), "g"(b) \
289
+ : "cc"); \
290
+ ret; })
291
+ # define BN_UMULT_LOHI(low,high,a,b) \
292
+ __asm__ ("mulq %3" \
293
+ : "=a"(low),"=d"(high) \
294
+ : "a"(a),"g"(b) \
295
+ : "cc");
296
+ # elif defined(_MSC_VER) && _MSC_VER >= 1400
297
+ # define BN_UMULT_HIGH(a, b) __umulh((a), (b))
298
+ # define BN_UMULT_LOHI(low, high, a, b) ((low) = _umul128((a), (b), &(high)))
299
+ # endif
300
+ #elif !defined(OPENSSL_NO_ASM) && defined(OPENSSL_AARCH64)
301
+ # if defined(__GNUC__) && __GNUC__>=2
302
+ # define BN_UMULT_HIGH(a,b) ({ \
303
+ register BN_ULONG ret; \
304
+ __asm__ ("umulh %0,%1,%2" \
305
+ : "=r"(ret) \
306
+ : "r"(a), "r"(b)); \
307
+ ret; })
308
+ # endif
309
+ #endif
310
+
311
+
312
+ #if defined(__cplusplus)
313
+ } /* extern C */
314
+ #endif
315
+
316
+ #endif /* OPENSSL_HEADER_BN_INTERNAL_H */
@@ -0,0 +1,516 @@
1
+ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2
+ * All rights reserved.
3
+ *
4
+ * This package is an SSL implementation written
5
+ * by Eric Young (eay@cryptsoft.com).
6
+ * The implementation was written so as to conform with Netscapes SSL.
7
+ *
8
+ * This library is free for commercial and non-commercial use as long as
9
+ * the following conditions are aheared to. The following conditions
10
+ * apply to all code found in this distribution, be it the RC4, RSA,
11
+ * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12
+ * included with this distribution is covered by the same copyright terms
13
+ * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
+ *
15
+ * Copyright remains Eric Young's, and as such any Copyright notices in
16
+ * the code are not to be removed.
17
+ * If this package is used in a product, Eric Young should be given attribution
18
+ * as the author of the parts of the library used.
19
+ * This can be in the form of a textual message at program startup or
20
+ * in documentation (online or textual) provided with the package.
21
+ *
22
+ * Redistribution and use in source and binary forms, with or without
23
+ * modification, are permitted provided that the following conditions
24
+ * are met:
25
+ * 1. Redistributions of source code must retain the copyright
26
+ * notice, this list of conditions and the following disclaimer.
27
+ * 2. Redistributions in binary form must reproduce the above copyright
28
+ * notice, this list of conditions and the following disclaimer in the
29
+ * documentation and/or other materials provided with the distribution.
30
+ * 3. All advertising materials mentioning features or use of this software
31
+ * must display the following acknowledgement:
32
+ * "This product includes cryptographic software written by
33
+ * Eric Young (eay@cryptsoft.com)"
34
+ * The word 'cryptographic' can be left out if the rouines from the library
35
+ * being used are not cryptographic related :-).
36
+ * 4. If you include any Windows specific code (or a derivative thereof) from
37
+ * the apps directory (application code) you must include an acknowledgement:
38
+ * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
+ *
40
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
+ * SUCH DAMAGE.
51
+ *
52
+ * The licence and distribution terms for any publically available version or
53
+ * derivative of this code cannot be changed. i.e. this code cannot simply be
54
+ * copied and put under another distribution licence
55
+ * [including the GNU Public Licence.]
56
+ */
57
+ /* ====================================================================
58
+ * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
59
+ *
60
+ * Redistribution and use in source and binary forms, with or without
61
+ * modification, are permitted provided that the following conditions
62
+ * are met:
63
+ *
64
+ * 1. Redistributions of source code must retain the above copyright
65
+ * notice, this list of conditions and the following disclaimer.
66
+ *
67
+ * 2. Redistributions in binary form must reproduce the above copyright
68
+ * notice, this list of conditions and the following disclaimer in
69
+ * the documentation and/or other materials provided with the
70
+ * distribution.
71
+ *
72
+ * 3. All advertising materials mentioning features or use of this
73
+ * software must display the following acknowledgment:
74
+ * "This product includes software developed by the OpenSSL Project
75
+ * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76
+ *
77
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78
+ * endorse or promote products derived from this software without
79
+ * prior written permission. For written permission, please contact
80
+ * openssl-core@openssl.org.
81
+ *
82
+ * 5. Products derived from this software may not be called "OpenSSL"
83
+ * nor may "OpenSSL" appear in their names without prior written
84
+ * permission of the OpenSSL Project.
85
+ *
86
+ * 6. Redistributions of any form whatsoever must retain the following
87
+ * acknowledgment:
88
+ * "This product includes software developed by the OpenSSL Project
89
+ * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90
+ *
91
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
103
+ * ====================================================================
104
+ *
105
+ * This product includes cryptographic software written by Eric Young
106
+ * (eay@cryptsoft.com). This product includes software written by Tim
107
+ * Hudson (tjh@cryptsoft.com). */
108
+
109
+ #include <openssl/bn.h>
110
+
111
+ #include <string.h>
112
+
113
+ #include <openssl/err.h>
114
+ #include <openssl/mem.h>
115
+ #include <openssl/thread.h>
116
+ #include <openssl/type_check.h>
117
+
118
+ #include "internal.h"
119
+ #include "../internal.h"
120
+
121
+
122
+ OPENSSL_COMPILE_ASSERT(BN_MONT_CTX_N0_LIMBS == 1 || BN_MONT_CTX_N0_LIMBS == 2,
123
+ BN_MONT_CTX_N0_LIMBS_VALUE_INVALID);
124
+
125
+ BN_MONT_CTX *BN_MONT_CTX_new(void) {
126
+ BN_MONT_CTX *ret = OPENSSL_malloc(sizeof(BN_MONT_CTX));
127
+
128
+ if (ret == NULL) {
129
+ return NULL;
130
+ }
131
+
132
+ memset(ret, 0, sizeof(BN_MONT_CTX));
133
+ BN_init(&ret->RR);
134
+ BN_init(&ret->N);
135
+
136
+ return ret;
137
+ }
138
+
139
+ void BN_MONT_CTX_free(BN_MONT_CTX *mont) {
140
+ if (mont == NULL) {
141
+ return;
142
+ }
143
+
144
+ BN_free(&mont->RR);
145
+ BN_free(&mont->N);
146
+ OPENSSL_free(mont);
147
+ }
148
+
149
+ int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) {
150
+ int ret = 0;
151
+ BIGNUM *Ri, *R;
152
+ BIGNUM tmod;
153
+ BN_ULONG buf[2];
154
+
155
+ if (BN_is_zero(mod)) {
156
+ OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO);
157
+ return 0;
158
+ }
159
+
160
+ BN_CTX_start(ctx);
161
+ Ri = BN_CTX_get(ctx);
162
+ if (Ri == NULL) {
163
+ goto err;
164
+ }
165
+ R = &mont->RR; /* grab RR as a temp */
166
+ if (!BN_copy(&mont->N, mod)) {
167
+ goto err; /* Set N */
168
+ }
169
+ mont->N.neg = 0;
170
+
171
+ BN_init(&tmod);
172
+ tmod.d = buf;
173
+ tmod.dmax = 2;
174
+ tmod.neg = 0;
175
+
176
+ BN_zero(R);
177
+ if (!BN_set_bit(R, BN_MONT_CTX_N0_LIMBS * BN_BITS2)) {
178
+ goto err;
179
+ }
180
+
181
+ tmod.top = 0;
182
+ buf[0] = mod->d[0];
183
+ if (buf[0] != 0) {
184
+ tmod.top = 1;
185
+ }
186
+
187
+ buf[1] = 0;
188
+ if (BN_MONT_CTX_N0_LIMBS == 2 && mod->top > 1 && mod->d[1] != 0) {
189
+ buf[1] = mod->d[1];
190
+ tmod.top = 2;
191
+ }
192
+
193
+ if (BN_mod_inverse(Ri, R, &tmod, ctx) == NULL) {
194
+ goto err;
195
+ }
196
+ if (!BN_lshift(Ri, Ri, BN_MONT_CTX_N0_LIMBS * BN_BITS2)) {
197
+ goto err; /* R*Ri */
198
+ }
199
+ const BIGNUM *Ri_dividend;
200
+ if (!BN_is_zero(Ri)) {
201
+ if (!BN_sub_word(Ri, 1)) {
202
+ goto err;
203
+ }
204
+ Ri_dividend = Ri;
205
+ } else {
206
+ /* Ri == 0 so Ri - 1 == -1. -1 % tmod == 0xff..ff. */
207
+ static const BN_ULONG kMinusOneLimbs[BN_MONT_CTX_N0_LIMBS] = {
208
+ BN_MASK2,
209
+ #if BN_MONT_CTX_N0_LIMBS == 2
210
+ BN_MASK2
211
+ #endif
212
+ };
213
+ static const BIGNUM kMinusOne = STATIC_BIGNUM(kMinusOneLimbs);
214
+ Ri_dividend = &kMinusOne;
215
+ }
216
+
217
+ if (!BN_div(Ri, NULL, Ri_dividend, &tmod, ctx)) {
218
+ goto err;
219
+ }
220
+
221
+ mont->n0[0] = 0;
222
+ if (Ri->top > 0) {
223
+ mont->n0[0] = Ri->d[0];
224
+ }
225
+ mont->n0[1] = 0;
226
+ if (BN_MONT_CTX_N0_LIMBS == 2 && Ri->top > 1) {
227
+ mont->n0[1] = Ri->d[1];
228
+ }
229
+
230
+ /* RR = (2^ri)^2 == 2^(ri*2) == 1 << (ri*2), which has its (ri*2)th bit set. */
231
+ int ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;
232
+ BN_zero(&(mont->RR));
233
+ if (!BN_set_bit(&(mont->RR), ri * 2)) {
234
+ goto err;
235
+ }
236
+ if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx)) {
237
+ goto err;
238
+ }
239
+
240
+ ret = 1;
241
+
242
+ err:
243
+ BN_CTX_end(ctx);
244
+ return ret;
245
+ }
246
+
247
+ BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_MUTEX *lock,
248
+ const BIGNUM *mod, BN_CTX *bn_ctx) {
249
+ CRYPTO_MUTEX_lock_read(lock);
250
+ BN_MONT_CTX *ctx = *pmont;
251
+ CRYPTO_MUTEX_unlock(lock);
252
+
253
+ if (ctx) {
254
+ return ctx;
255
+ }
256
+
257
+ CRYPTO_MUTEX_lock_write(lock);
258
+ ctx = *pmont;
259
+ if (ctx) {
260
+ goto out;
261
+ }
262
+
263
+ ctx = BN_MONT_CTX_new();
264
+ if (ctx == NULL) {
265
+ goto out;
266
+ }
267
+ if (!BN_MONT_CTX_set(ctx, mod, bn_ctx)) {
268
+ BN_MONT_CTX_free(ctx);
269
+ ctx = NULL;
270
+ goto out;
271
+ }
272
+ *pmont = ctx;
273
+
274
+ out:
275
+ CRYPTO_MUTEX_unlock(lock);
276
+ return ctx;
277
+ }
278
+
279
+ int BN_to_montgomery(BIGNUM *ret, const BIGNUM *a, const BN_MONT_CTX *mont,
280
+ BN_CTX *ctx) {
281
+ return BN_mod_mul_montgomery(ret, a, &mont->RR, mont, ctx);
282
+ }
283
+
284
+ #if 0
285
+ static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r,
286
+ const BN_MONT_CTX *mont) {
287
+ const BIGNUM *n;
288
+ BN_ULONG *ap, *np, *rp, n0, v, carry;
289
+ int nl, max, i;
290
+
291
+ n = &mont->N;
292
+ nl = n->top;
293
+ if (nl == 0) {
294
+ ret->top = 0;
295
+ return 1;
296
+ }
297
+
298
+ max = (2 * nl); /* carry is stored separately */
299
+ if (bn_wexpand(r, max) == NULL) {
300
+ return 0;
301
+ }
302
+
303
+ r->neg ^= n->neg;
304
+ np = n->d;
305
+ rp = r->d;
306
+
307
+ /* clear the top words of T */
308
+ if (max > r->top) {
309
+ memset(&rp[r->top], 0, (max - r->top) * sizeof(BN_ULONG));
310
+ }
311
+
312
+ r->top = max;
313
+ n0 = mont->n0[0];
314
+
315
+ for (carry = 0, i = 0; i < nl; i++, rp++) {
316
+ v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
317
+ v = (v + carry + rp[nl]) & BN_MASK2;
318
+ carry |= (v != rp[nl]);
319
+ carry &= (v <= rp[nl]);
320
+ rp[nl] = v;
321
+ }
322
+
323
+ if (bn_wexpand(ret, nl) == NULL) {
324
+ return 0;
325
+ }
326
+ ret->top = nl;
327
+ ret->neg = r->neg;
328
+
329
+ rp = ret->d;
330
+ ap = &(r->d[nl]);
331
+
332
+ {
333
+ BN_ULONG *nrp;
334
+ size_t m;
335
+
336
+ v = bn_sub_words(rp, ap, np, nl) - carry;
337
+ /* if subtraction result is real, then trick unconditional memcpy below to
338
+ * perform in-place "refresh" instead of actual copy. */
339
+ m = (0 - (size_t)v);
340
+ nrp = (BN_ULONG *)(((intptr_t)rp & ~m) | ((intptr_t)ap & m));
341
+
342
+ for (i = 0, nl -= 4; i < nl; i += 4) {
343
+ BN_ULONG t1, t2, t3, t4;
344
+
345
+ t1 = nrp[i + 0];
346
+ t2 = nrp[i + 1];
347
+ t3 = nrp[i + 2];
348
+ ap[i + 0] = 0;
349
+ t4 = nrp[i + 3];
350
+ ap[i + 1] = 0;
351
+ rp[i + 0] = t1;
352
+ ap[i + 2] = 0;
353
+ rp[i + 1] = t2;
354
+ ap[i + 3] = 0;
355
+ rp[i + 2] = t3;
356
+ rp[i + 3] = t4;
357
+ }
358
+
359
+ for (nl += 4; i < nl; i++) {
360
+ rp[i] = nrp[i], ap[i] = 0;
361
+ }
362
+ }
363
+
364
+ bn_correct_top(r);
365
+ bn_correct_top(ret);
366
+
367
+ return 1;
368
+ }
369
+ #endif
370
+
371
+ #define PTR_SIZE_INT size_t
372
+
373
+ static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, const BN_MONT_CTX *mont)
374
+ {
375
+ BIGNUM *n;
376
+ BN_ULONG *ap,*np,*rp,n0,v,carry;
377
+ int nl,max,i;
378
+
379
+ n= (BIGNUM*) &(mont->N);
380
+ nl=n->top;
381
+ if (nl == 0) { ret->top=0; return(1); }
382
+
383
+ max=(2*nl); /* carry is stored separately */
384
+ if (bn_wexpand(r,max) == NULL) return(0);
385
+
386
+ r->neg^=n->neg;
387
+ np=n->d;
388
+ rp=r->d;
389
+
390
+ /* clear the top words of T */
391
+ #if 1
392
+ for (i=r->top; i<max; i++) /* memset? XXX */
393
+ rp[i]=0;
394
+ #else
395
+ memset(&(rp[r->top]),0,(max-r->top)*sizeof(BN_ULONG));
396
+ #endif
397
+
398
+ r->top=max;
399
+ n0=mont->n0[0];
400
+
401
+ for (carry=0, i=0; i<nl; i++, rp++)
402
+ {
403
+ v=bn_mul_add_words(rp,np,nl,(rp[0]*n0)&BN_MASK2);
404
+ v = (v+carry+rp[nl])&BN_MASK2;
405
+ carry |= (v != rp[nl]);
406
+ carry &= (v <= rp[nl]);
407
+ rp[nl]=v;
408
+ }
409
+
410
+ if (bn_wexpand(ret,nl) == NULL) return(0);
411
+ ret->top=nl;
412
+ ret->neg=r->neg;
413
+
414
+ rp=ret->d;
415
+ ap=&(r->d[nl]);
416
+
417
+ {
418
+ BN_ULONG *nrp;
419
+ size_t m;
420
+
421
+ v=bn_sub_words(rp,ap,np,nl)-carry;
422
+ /* if subtraction result is real, then
423
+ * trick unconditional memcpy below to perform in-place
424
+ * "refresh" instead of actual copy. */
425
+ m=(0-(size_t)v);
426
+ nrp=(BN_ULONG *)(((PTR_SIZE_INT)rp&~m)|((PTR_SIZE_INT)ap&m));
427
+
428
+ for (i=0,nl-=4; i<nl; i+=4)
429
+ {
430
+ BN_ULONG t1,t2,t3,t4;
431
+
432
+ t1=nrp[i+0];
433
+ t2=nrp[i+1];
434
+ t3=nrp[i+2]; ap[i+0]=0;
435
+ t4=nrp[i+3]; ap[i+1]=0;
436
+ rp[i+0]=t1; ap[i+2]=0;
437
+ rp[i+1]=t2; ap[i+3]=0;
438
+ rp[i+2]=t3;
439
+ rp[i+3]=t4;
440
+ }
441
+ for (nl+=4; i<nl; i++)
442
+ rp[i]=nrp[i], ap[i]=0;
443
+ }
444
+ bn_correct_top(r);
445
+ bn_correct_top(ret);
446
+
447
+ return(1);
448
+ }
449
+
450
+ int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, const BN_MONT_CTX *mont,
451
+ BN_CTX *ctx) {
452
+ int retn = 0;
453
+ BIGNUM *t;
454
+
455
+ BN_CTX_start(ctx);
456
+ t = BN_CTX_get(ctx);
457
+ if (t == NULL) {
458
+ return 0;
459
+ }
460
+
461
+ if (BN_copy(t, a)) {
462
+ retn = BN_from_montgomery_word(ret, t, mont);
463
+ }
464
+ BN_CTX_end(ctx);
465
+
466
+ return retn;
467
+ }
468
+
469
+ int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
470
+ const BN_MONT_CTX *mont, BN_CTX *ctx) {
471
+ BIGNUM *tmp;
472
+ int ret = 0;
473
+
474
+ #if defined(OPENSSL_BN_ASM_MONT)
475
+ int num = mont->N.top;
476
+
477
+ if (num > 1 && a->top == num && b->top == num) {
478
+ if (bn_wexpand(r, num) == NULL) {
479
+ return 0;
480
+ }
481
+ if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {
482
+ r->neg = a->neg ^ b->neg;
483
+ r->top = num;
484
+ bn_correct_top(r);
485
+ return 1;
486
+ }
487
+ }
488
+ #endif
489
+
490
+ BN_CTX_start(ctx);
491
+ tmp = BN_CTX_get(ctx);
492
+ if (tmp == NULL) {
493
+ goto err;
494
+ }
495
+
496
+ if (a == b) {
497
+ if (!BN_sqr(tmp, a, ctx)) {
498
+ goto err;
499
+ }
500
+ } else {
501
+ if (!BN_mul(tmp, a, b, ctx)) {
502
+ goto err;
503
+ }
504
+ }
505
+
506
+ /* reduce from aRR to aR */
507
+ if (!BN_from_montgomery_word(r, tmp, mont)) {
508
+ goto err;
509
+ }
510
+
511
+ ret = 1;
512
+
513
+ err:
514
+ BN_CTX_end(ctx);
515
+ return ret;
516
+ }