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,311 @@
1
+ /* Written by Ulf Moeller for the OpenSSL project. */
2
+ /* ====================================================================
3
+ * Copyright (c) 1998-2004 The OpenSSL Project. All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions
7
+ * are met:
8
+ *
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ *
12
+ * 2. Redistributions in binary form must reproduce the above copyright
13
+ * notice, this list of conditions and the following disclaimer in
14
+ * the documentation and/or other materials provided with the
15
+ * distribution.
16
+ *
17
+ * 3. All advertising materials mentioning features or use of this
18
+ * software must display the following acknowledgment:
19
+ * "This product includes software developed by the OpenSSL Project
20
+ * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21
+ *
22
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23
+ * endorse or promote products derived from this software without
24
+ * prior written permission. For written permission, please contact
25
+ * openssl-core@openssl.org.
26
+ *
27
+ * 5. Products derived from this software may not be called "OpenSSL"
28
+ * nor may "OpenSSL" appear in their names without prior written
29
+ * permission of the OpenSSL Project.
30
+ *
31
+ * 6. Redistributions of any form whatsoever must retain the following
32
+ * acknowledgment:
33
+ * "This product includes software developed by the OpenSSL Project
34
+ * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35
+ *
36
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
48
+ * ====================================================================
49
+ *
50
+ * This product includes cryptographic software written by Eric Young
51
+ * (eay@cryptsoft.com). This product includes software written by Tim
52
+ * Hudson (tjh@cryptsoft.com). */
53
+
54
+
55
+ #include <openssl/bn.h>
56
+
57
+ #include <string.h>
58
+
59
+ #include <openssl/err.h>
60
+ #include <openssl/mem.h>
61
+
62
+
63
+ /* How many bignums are in each "pool item"; */
64
+ #define BN_CTX_POOL_SIZE 16
65
+ /* The stack frame info is resizing, set a first-time expansion size; */
66
+ #define BN_CTX_START_FRAMES 32
67
+
68
+ /* A bundle of bignums that can be linked with other bundles */
69
+ typedef struct bignum_pool_item {
70
+ /* The bignum values */
71
+ BIGNUM vals[BN_CTX_POOL_SIZE];
72
+ /* Linked-list admin */
73
+ struct bignum_pool_item *prev, *next;
74
+ } BN_POOL_ITEM;
75
+
76
+
77
+ typedef struct bignum_pool {
78
+ /* Linked-list admin */
79
+ BN_POOL_ITEM *head, *current, *tail;
80
+ /* Stack depth and allocation size */
81
+ unsigned used, size;
82
+ } BN_POOL;
83
+
84
+ static void BN_POOL_init(BN_POOL *);
85
+ static void BN_POOL_finish(BN_POOL *);
86
+ static BIGNUM *BN_POOL_get(BN_POOL *);
87
+ static void BN_POOL_release(BN_POOL *, unsigned int);
88
+
89
+ /************/
90
+ /* BN_STACK */
91
+ /************/
92
+
93
+ /* A wrapper to manage the "stack frames" */
94
+ typedef struct bignum_ctx_stack {
95
+ /* Array of indexes into the bignum stack */
96
+ unsigned int *indexes;
97
+ /* Number of stack frames, and the size of the allocated array */
98
+ unsigned int depth, size;
99
+ } BN_STACK;
100
+
101
+ static void BN_STACK_init(BN_STACK *);
102
+ static void BN_STACK_finish(BN_STACK *);
103
+ static int BN_STACK_push(BN_STACK *, unsigned int);
104
+ static unsigned int BN_STACK_pop(BN_STACK *);
105
+
106
+ /**********/
107
+ /* BN_CTX */
108
+ /**********/
109
+
110
+ /* The opaque BN_CTX type */
111
+ struct bignum_ctx {
112
+ /* The bignum bundles */
113
+ BN_POOL pool;
114
+ /* The "stack frames", if you will */
115
+ BN_STACK stack;
116
+ /* The number of bignums currently assigned */
117
+ unsigned int used;
118
+ /* Depth of stack overflow */
119
+ int err_stack;
120
+ /* Block "gets" until an "end" (compatibility behaviour) */
121
+ int too_many;
122
+ };
123
+
124
+ BN_CTX *BN_CTX_new(void) {
125
+ BN_CTX *ret = OPENSSL_malloc(sizeof(BN_CTX));
126
+ if (!ret) {
127
+ OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
128
+ return NULL;
129
+ }
130
+
131
+ /* Initialise the structure */
132
+ BN_POOL_init(&ret->pool);
133
+ BN_STACK_init(&ret->stack);
134
+ ret->used = 0;
135
+ ret->err_stack = 0;
136
+ ret->too_many = 0;
137
+ return ret;
138
+ }
139
+
140
+ void BN_CTX_free(BN_CTX *ctx) {
141
+ if (ctx == NULL) {
142
+ return;
143
+ }
144
+
145
+ BN_STACK_finish(&ctx->stack);
146
+ BN_POOL_finish(&ctx->pool);
147
+ OPENSSL_free(ctx);
148
+ }
149
+
150
+ void BN_CTX_start(BN_CTX *ctx) {
151
+ /* If we're already overflowing ... */
152
+ if (ctx->err_stack || ctx->too_many) {
153
+ ctx->err_stack++;
154
+ } else if (!BN_STACK_push(&ctx->stack, ctx->used)) {
155
+ /* (Try to) get a new frame pointer */
156
+ OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
157
+ ctx->err_stack++;
158
+ }
159
+ }
160
+
161
+ BIGNUM *BN_CTX_get(BN_CTX *ctx) {
162
+ BIGNUM *ret;
163
+ if (ctx->err_stack || ctx->too_many) {
164
+ return NULL;
165
+ }
166
+
167
+ ret = BN_POOL_get(&ctx->pool);
168
+ if (ret == NULL) {
169
+ /* Setting too_many prevents repeated "get" attempts from
170
+ * cluttering the error stack. */
171
+ ctx->too_many = 1;
172
+ OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
173
+ return NULL;
174
+ }
175
+
176
+ /* OK, make sure the returned bignum is "zero" */
177
+ BN_zero(ret);
178
+ ctx->used++;
179
+ return ret;
180
+ }
181
+
182
+ void BN_CTX_end(BN_CTX *ctx) {
183
+ if (ctx->err_stack) {
184
+ ctx->err_stack--;
185
+ } else {
186
+ unsigned int fp = BN_STACK_pop(&ctx->stack);
187
+ /* Does this stack frame have anything to release? */
188
+ if (fp < ctx->used) {
189
+ BN_POOL_release(&ctx->pool, ctx->used - fp);
190
+ }
191
+
192
+ ctx->used = fp;
193
+ /* Unjam "too_many" in case "get" had failed */
194
+ ctx->too_many = 0;
195
+ }
196
+ }
197
+
198
+ /************/
199
+ /* BN_STACK */
200
+ /************/
201
+
202
+ static void BN_STACK_init(BN_STACK *st) {
203
+ st->indexes = NULL;
204
+ st->depth = st->size = 0;
205
+ }
206
+
207
+ static void BN_STACK_finish(BN_STACK *st) {
208
+ OPENSSL_free(st->indexes);
209
+ }
210
+
211
+ static int BN_STACK_push(BN_STACK *st, unsigned int idx) {
212
+ if (st->depth == st->size) {
213
+ /* Need to expand */
214
+ unsigned int newsize =
215
+ (st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES);
216
+ unsigned int *newitems = OPENSSL_malloc(newsize * sizeof(unsigned int));
217
+ if (!newitems) {
218
+ return 0;
219
+ }
220
+ if (st->depth) {
221
+ memcpy(newitems, st->indexes, st->depth * sizeof(unsigned int));
222
+ }
223
+ OPENSSL_free(st->indexes);
224
+ st->indexes = newitems;
225
+ st->size = newsize;
226
+ }
227
+
228
+ st->indexes[(st->depth)++] = idx;
229
+ return 1;
230
+ }
231
+
232
+ static unsigned int BN_STACK_pop(BN_STACK *st) {
233
+ return st->indexes[--(st->depth)];
234
+ }
235
+
236
+ static void BN_POOL_init(BN_POOL *p) {
237
+ p->head = p->current = p->tail = NULL;
238
+ p->used = p->size = 0;
239
+ }
240
+
241
+ static void BN_POOL_finish(BN_POOL *p) {
242
+ while (p->head) {
243
+ unsigned int loop = 0;
244
+ BIGNUM *bn = p->head->vals;
245
+ while (loop++ < BN_CTX_POOL_SIZE) {
246
+ if (bn->d) {
247
+ BN_clear_free(bn);
248
+ }
249
+ bn++;
250
+ }
251
+
252
+ p->current = p->head->next;
253
+ OPENSSL_free(p->head);
254
+ p->head = p->current;
255
+ }
256
+ }
257
+
258
+ static BIGNUM *BN_POOL_get(BN_POOL *p) {
259
+ if (p->used == p->size) {
260
+ BIGNUM *bn;
261
+ unsigned int loop = 0;
262
+ BN_POOL_ITEM *item = OPENSSL_malloc(sizeof(BN_POOL_ITEM));
263
+ if (!item) {
264
+ return NULL;
265
+ }
266
+
267
+ /* Initialise the structure */
268
+ bn = item->vals;
269
+ while (loop++ < BN_CTX_POOL_SIZE) {
270
+ BN_init(bn++);
271
+ }
272
+
273
+ item->prev = p->tail;
274
+ item->next = NULL;
275
+ /* Link it in */
276
+ if (!p->head) {
277
+ p->head = p->current = p->tail = item;
278
+ } else {
279
+ p->tail->next = item;
280
+ p->tail = item;
281
+ p->current = item;
282
+ }
283
+
284
+ p->size += BN_CTX_POOL_SIZE;
285
+ p->used++;
286
+ /* Return the first bignum from the new pool */
287
+ return item->vals;
288
+ }
289
+
290
+ if (!p->used) {
291
+ p->current = p->head;
292
+ } else if ((p->used % BN_CTX_POOL_SIZE) == 0) {
293
+ p->current = p->current->next;
294
+ }
295
+
296
+ return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE);
297
+ }
298
+
299
+ static void BN_POOL_release(BN_POOL *p, unsigned int num) {
300
+ unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
301
+ p->used -= num;
302
+
303
+ while (num--) {
304
+ if (!offset) {
305
+ offset = BN_CTX_POOL_SIZE - 1;
306
+ p->current = p->current->prev;
307
+ } else {
308
+ offset--;
309
+ }
310
+ }
311
+ }
@@ -0,0 +1,594 @@
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
+ #include <openssl/bn.h>
58
+
59
+ #include <limits.h>
60
+ #include <openssl/err.h>
61
+
62
+ #include "internal.h"
63
+
64
+
65
+ #define asm __asm__
66
+
67
+ #if !defined(OPENSSL_NO_ASM)
68
+ # if defined(__GNUC__) && __GNUC__>=2
69
+ # if defined(OPENSSL_X86)
70
+ /*
71
+ * There were two reasons for implementing this template:
72
+ * - GNU C generates a call to a function (__udivdi3 to be exact)
73
+ * in reply to ((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0 (I fail to
74
+ * understand why...);
75
+ * - divl doesn't only calculate quotient, but also leaves
76
+ * remainder in %edx which we can definitely use here:-)
77
+ *
78
+ * <appro@fy.chalmers.se>
79
+ */
80
+ #undef div_asm
81
+ # define div_asm(n0,n1,d0) \
82
+ ({ asm volatile ( \
83
+ "divl %4" \
84
+ : "=a"(q), "=d"(rem) \
85
+ : "a"(n1), "d"(n0), "g"(d0) \
86
+ : "cc"); \
87
+ q; \
88
+ })
89
+ # define REMAINDER_IS_ALREADY_CALCULATED
90
+ # elif defined(OPENSSL_X86_64)
91
+ /*
92
+ * Same story here, but it's 128-bit by 64-bit division. Wow!
93
+ * <appro@fy.chalmers.se>
94
+ */
95
+ # undef div_asm
96
+ # define div_asm(n0,n1,d0) \
97
+ ({ asm volatile ( \
98
+ "divq %4" \
99
+ : "=a"(q), "=d"(rem) \
100
+ : "a"(n1), "d"(n0), "g"(d0) \
101
+ : "cc"); \
102
+ q; \
103
+ })
104
+ # define REMAINDER_IS_ALREADY_CALCULATED
105
+ # endif /* __<cpu> */
106
+ # endif /* __GNUC__ */
107
+ #endif /* OPENSSL_NO_ASM */
108
+
109
+ /* BN_div computes dv := num / divisor, rounding towards
110
+ * zero, and sets up rm such that dv*divisor + rm = num holds.
111
+ * Thus:
112
+ * dv->neg == num->neg ^ divisor->neg (unless the result is zero)
113
+ * rm->neg == num->neg (unless the remainder is zero)
114
+ * If 'dv' or 'rm' is NULL, the respective value is not returned. */
115
+ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
116
+ BN_CTX *ctx) {
117
+ int norm_shift, i, loop;
118
+ BIGNUM *tmp, wnum, *snum, *sdiv, *res;
119
+ BN_ULONG *resp, *wnump;
120
+ BN_ULONG d0, d1;
121
+ int num_n, div_n;
122
+ int no_branch = 0;
123
+
124
+ /* Invalid zero-padding would have particularly bad consequences
125
+ * so don't just rely on bn_check_top() here */
126
+ if ((num->top > 0 && num->d[num->top - 1] == 0) ||
127
+ (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {
128
+ OPENSSL_PUT_ERROR(BN, BN_R_NOT_INITIALIZED);
129
+ return 0;
130
+ }
131
+
132
+ if ((num->flags & BN_FLG_CONSTTIME) != 0 ||
133
+ (divisor->flags & BN_FLG_CONSTTIME) != 0) {
134
+ no_branch = 1;
135
+ }
136
+
137
+ if (BN_is_zero(divisor)) {
138
+ OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO);
139
+ return 0;
140
+ }
141
+
142
+ if (!no_branch && BN_ucmp(num, divisor) < 0) {
143
+ if (rm != NULL) {
144
+ if (BN_copy(rm, num) == NULL) {
145
+ return 0;
146
+ }
147
+ }
148
+ if (dv != NULL) {
149
+ BN_zero(dv);
150
+ }
151
+ return 1;
152
+ }
153
+
154
+ BN_CTX_start(ctx);
155
+ tmp = BN_CTX_get(ctx);
156
+ snum = BN_CTX_get(ctx);
157
+ sdiv = BN_CTX_get(ctx);
158
+ if (dv == NULL) {
159
+ res = BN_CTX_get(ctx);
160
+ } else {
161
+ res = dv;
162
+ }
163
+ if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL) {
164
+ goto err;
165
+ }
166
+
167
+ /* First we normalise the numbers */
168
+ norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);
169
+ if (!(BN_lshift(sdiv, divisor, norm_shift))) {
170
+ goto err;
171
+ }
172
+ sdiv->neg = 0;
173
+ norm_shift += BN_BITS2;
174
+ if (!(BN_lshift(snum, num, norm_shift))) {
175
+ goto err;
176
+ }
177
+ snum->neg = 0;
178
+
179
+ if (no_branch) {
180
+ /* Since we don't know whether snum is larger than sdiv,
181
+ * we pad snum with enough zeroes without changing its
182
+ * value.
183
+ */
184
+ if (snum->top <= sdiv->top + 1) {
185
+ if (bn_wexpand(snum, sdiv->top + 2) == NULL) {
186
+ goto err;
187
+ }
188
+ for (i = snum->top; i < sdiv->top + 2; i++) {
189
+ snum->d[i] = 0;
190
+ }
191
+ snum->top = sdiv->top + 2;
192
+ } else {
193
+ if (bn_wexpand(snum, snum->top + 1) == NULL) {
194
+ goto err;
195
+ }
196
+ snum->d[snum->top] = 0;
197
+ snum->top++;
198
+ }
199
+ }
200
+
201
+ div_n = sdiv->top;
202
+ num_n = snum->top;
203
+ loop = num_n - div_n;
204
+ /* Lets setup a 'window' into snum
205
+ * This is the part that corresponds to the current
206
+ * 'area' being divided */
207
+ wnum.neg = 0;
208
+ wnum.d = &(snum->d[loop]);
209
+ wnum.top = div_n;
210
+ /* only needed when BN_ucmp messes up the values between top and max */
211
+ wnum.dmax = snum->dmax - loop; /* so we don't step out of bounds */
212
+
213
+ /* Get the top 2 words of sdiv */
214
+ /* div_n=sdiv->top; */
215
+ d0 = sdiv->d[div_n - 1];
216
+ d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];
217
+
218
+ /* pointer to the 'top' of snum */
219
+ wnump = &(snum->d[num_n - 1]);
220
+
221
+ /* Setup to 'res' */
222
+ res->neg = (num->neg ^ divisor->neg);
223
+ if (!bn_wexpand(res, (loop + 1))) {
224
+ goto err;
225
+ }
226
+ res->top = loop - no_branch;
227
+ resp = &(res->d[loop - 1]);
228
+
229
+ /* space for temp */
230
+ if (!bn_wexpand(tmp, (div_n + 1))) {
231
+ goto err;
232
+ }
233
+
234
+ if (!no_branch) {
235
+ if (BN_ucmp(&wnum, sdiv) >= 0) {
236
+ bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);
237
+ *resp = 1;
238
+ } else {
239
+ res->top--;
240
+ }
241
+ }
242
+
243
+ /* if res->top == 0 then clear the neg value otherwise decrease
244
+ * the resp pointer */
245
+ if (res->top == 0) {
246
+ res->neg = 0;
247
+ } else {
248
+ resp--;
249
+ }
250
+
251
+ for (i = 0; i < loop - 1; i++, wnump--, resp--) {
252
+ BN_ULONG q, l0;
253
+ /* the first part of the loop uses the top two words of snum and sdiv to
254
+ * calculate a BN_ULONG q such that | wnum - sdiv * q | < sdiv */
255
+ BN_ULONG n0, n1, rem = 0;
256
+
257
+ n0 = wnump[0];
258
+ n1 = wnump[-1];
259
+ if (n0 == d0) {
260
+ q = BN_MASK2;
261
+ } else {
262
+ /* n0 < d0 */
263
+ #ifdef BN_ULLONG
264
+ BN_ULLONG t2;
265
+
266
+ #if defined(BN_ULLONG) && !defined(div_asm)
267
+ q = (BN_ULONG)(((((BN_ULLONG)n0) << BN_BITS2) | n1) / d0);
268
+ #else
269
+ q = div_asm(n0, n1, d0);
270
+ #endif
271
+
272
+ #ifndef REMAINDER_IS_ALREADY_CALCULATED
273
+ /* rem doesn't have to be BN_ULLONG. The least we know it's less that d0,
274
+ * isn't it? */
275
+ rem = (n1 - q * d0) & BN_MASK2;
276
+ #endif
277
+
278
+ t2 = (BN_ULLONG)d1 * q;
279
+
280
+ for (;;) {
281
+ if (t2 <= ((((BN_ULLONG)rem) << BN_BITS2) | wnump[-2])) {
282
+ break;
283
+ }
284
+ q--;
285
+ rem += d0;
286
+ if (rem < d0) {
287
+ break; /* don't let rem overflow */
288
+ }
289
+ t2 -= d1;
290
+ }
291
+ #else /* !BN_ULLONG */
292
+ BN_ULONG t2l, t2h;
293
+
294
+ #if defined(div_asm)
295
+ q = div_asm(n0, n1, d0);
296
+ #else
297
+ q = bn_div_words(n0, n1, d0);
298
+ #endif
299
+
300
+ #ifndef REMAINDER_IS_ALREADY_CALCULATED
301
+ rem = (n1 - q * d0) & BN_MASK2;
302
+ #endif
303
+
304
+ #if defined(BN_UMULT_LOHI)
305
+ BN_UMULT_LOHI(t2l, t2h, d1, q);
306
+ #elif defined(BN_UMULT_HIGH)
307
+ t2l = d1 * q;
308
+ t2h = BN_UMULT_HIGH(d1, q);
309
+ #else
310
+ {
311
+ BN_ULONG ql, qh;
312
+ t2l = LBITS(d1);
313
+ t2h = HBITS(d1);
314
+ ql = LBITS(q);
315
+ qh = HBITS(q);
316
+ mul64(t2l, t2h, ql, qh); /* t2=(BN_ULLONG)d1*q; */
317
+ }
318
+ #endif
319
+
320
+ for (;;) {
321
+ if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2]))) {
322
+ break;
323
+ }
324
+ q--;
325
+ rem += d0;
326
+ if (rem < d0) {
327
+ break; /* don't let rem overflow */
328
+ }
329
+ if (t2l < d1) {
330
+ t2h--;
331
+ }
332
+ t2l -= d1;
333
+ }
334
+ #endif /* !BN_ULLONG */
335
+ }
336
+
337
+ l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);
338
+ tmp->d[div_n] = l0;
339
+ wnum.d--;
340
+ /* ingore top values of the bignums just sub the two
341
+ * BN_ULONG arrays with bn_sub_words */
342
+ if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {
343
+ /* Note: As we have considered only the leading
344
+ * two BN_ULONGs in the calculation of q, sdiv * q
345
+ * might be greater than wnum (but then (q-1) * sdiv
346
+ * is less or equal than wnum)
347
+ */
348
+ q--;
349
+ if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n)) {
350
+ /* we can't have an overflow here (assuming
351
+ * that q != 0, but if q == 0 then tmp is
352
+ * zero anyway) */
353
+ (*wnump)++;
354
+ }
355
+ }
356
+ /* store part of the result */
357
+ *resp = q;
358
+ }
359
+ bn_correct_top(snum);
360
+ if (rm != NULL) {
361
+ /* Keep a copy of the neg flag in num because if rm==num
362
+ * BN_rshift() will overwrite it.
363
+ */
364
+ int neg = num->neg;
365
+ if (!BN_rshift(rm, snum, norm_shift)) {
366
+ goto err;
367
+ }
368
+ if (!BN_is_zero(rm)) {
369
+ rm->neg = neg;
370
+ }
371
+ }
372
+ if (no_branch) {
373
+ bn_correct_top(res);
374
+ }
375
+ BN_CTX_end(ctx);
376
+ return 1;
377
+
378
+ err:
379
+ BN_CTX_end(ctx);
380
+ return 0;
381
+ }
382
+
383
+ int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx) {
384
+ if (!(BN_mod(r, m, d, ctx))) {
385
+ return 0;
386
+ }
387
+ if (!r->neg) {
388
+ return 1;
389
+ }
390
+
391
+ /* now -|d| < r < 0, so we have to set r := r + |d|. */
392
+ return (d->neg ? BN_sub : BN_add)(r, r, d);
393
+ }
394
+
395
+ int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
396
+ BN_CTX *ctx) {
397
+ if (!BN_add(r, a, b)) {
398
+ return 0;
399
+ }
400
+ return BN_nnmod(r, r, m, ctx);
401
+ }
402
+
403
+ int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
404
+ const BIGNUM *m) {
405
+ if (!BN_uadd(r, a, b)) {
406
+ return 0;
407
+ }
408
+ if (BN_ucmp(r, m) >= 0) {
409
+ return BN_usub(r, r, m);
410
+ }
411
+ return 1;
412
+ }
413
+
414
+ int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
415
+ BN_CTX *ctx) {
416
+ if (!BN_sub(r, a, b)) {
417
+ return 0;
418
+ }
419
+ return BN_nnmod(r, r, m, ctx);
420
+ }
421
+
422
+ /* BN_mod_sub variant that may be used if both a and b are non-negative
423
+ * and less than m */
424
+ int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
425
+ const BIGNUM *m) {
426
+ if (!BN_sub(r, a, b)) {
427
+ return 0;
428
+ }
429
+ if (r->neg) {
430
+ return BN_add(r, r, m);
431
+ }
432
+ return 1;
433
+ }
434
+
435
+ int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
436
+ BN_CTX *ctx) {
437
+ BIGNUM *t;
438
+ int ret = 0;
439
+
440
+ BN_CTX_start(ctx);
441
+ t = BN_CTX_get(ctx);
442
+ if (t == NULL) {
443
+ goto err;
444
+ }
445
+
446
+ if (a == b) {
447
+ if (!BN_sqr(t, a, ctx)) {
448
+ goto err;
449
+ }
450
+ } else {
451
+ if (!BN_mul(t, a, b, ctx)) {
452
+ goto err;
453
+ }
454
+ }
455
+
456
+ if (!BN_nnmod(r, t, m, ctx)) {
457
+ goto err;
458
+ }
459
+
460
+ ret = 1;
461
+
462
+ err:
463
+ BN_CTX_end(ctx);
464
+ return ret;
465
+ }
466
+
467
+ int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) {
468
+ if (!BN_sqr(r, a, ctx)) {
469
+ return 0;
470
+ }
471
+
472
+ /* r->neg == 0, thus we don't need BN_nnmod */
473
+ return BN_mod(r, r, m, ctx);
474
+ }
475
+
476
+ int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m) {
477
+ if (r != a) {
478
+ if (BN_copy(r, a) == NULL) {
479
+ return 0;
480
+ }
481
+ }
482
+
483
+ while (n > 0) {
484
+ int max_shift;
485
+
486
+ /* 0 < r < m */
487
+ max_shift = BN_num_bits(m) - BN_num_bits(r);
488
+ /* max_shift >= 0 */
489
+
490
+ if (max_shift < 0) {
491
+ OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
492
+ return 0;
493
+ }
494
+
495
+ if (max_shift > n) {
496
+ max_shift = n;
497
+ }
498
+
499
+ if (max_shift) {
500
+ if (!BN_lshift(r, r, max_shift)) {
501
+ return 0;
502
+ }
503
+ n -= max_shift;
504
+ } else {
505
+ if (!BN_lshift1(r, r)) {
506
+ return 0;
507
+ }
508
+ --n;
509
+ }
510
+
511
+ /* BN_num_bits(r) <= BN_num_bits(m) */
512
+ if (BN_cmp(r, m) >= 0) {
513
+ if (!BN_sub(r, r, m)) {
514
+ return 0;
515
+ }
516
+ }
517
+ }
518
+
519
+ return 1;
520
+ }
521
+
522
+ int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m) {
523
+ if (!BN_lshift1(r, a)) {
524
+ return 0;
525
+ }
526
+ if (BN_cmp(r, m) >= 0) {
527
+ return BN_sub(r, r, m);
528
+ }
529
+
530
+ return 1;
531
+ }
532
+
533
+ BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) {
534
+ BN_ULONG ret = 0;
535
+ int i, j;
536
+
537
+ w &= BN_MASK2;
538
+
539
+ if (!w) {
540
+ /* actually this an error (division by zero) */
541
+ return (BN_ULONG) - 1;
542
+ }
543
+
544
+ if (a->top == 0) {
545
+ return 0;
546
+ }
547
+
548
+ /* normalize input (so bn_div_words doesn't complain) */
549
+ j = BN_BITS2 - BN_num_bits_word(w);
550
+ w <<= j;
551
+ if (!BN_lshift(a, a, j)) {
552
+ return (BN_ULONG) - 1;
553
+ }
554
+
555
+ for (i = a->top - 1; i >= 0; i--) {
556
+ BN_ULONG l, d;
557
+
558
+ l = a->d[i];
559
+ d = bn_div_words(ret, l, w);
560
+ ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2;
561
+ a->d[i] = d;
562
+ }
563
+
564
+ if ((a->top > 0) && (a->d[a->top - 1] == 0)) {
565
+ a->top--;
566
+ }
567
+
568
+ ret >>= j;
569
+ return ret;
570
+ }
571
+
572
+ BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) {
573
+ #ifndef BN_ULLONG
574
+ BN_ULONG ret = 0;
575
+ #else
576
+ BN_ULLONG ret = 0;
577
+ #endif
578
+ int i;
579
+
580
+ if (w == 0) {
581
+ return (BN_ULONG) -1;
582
+ }
583
+
584
+ w &= BN_MASK2;
585
+ for (i = a->top - 1; i >= 0; i--) {
586
+ #ifndef BN_ULLONG
587
+ ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w;
588
+ ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w;
589
+ #else
590
+ ret = (BN_ULLONG)(((ret << (BN_ULLONG)BN_BITS2) | a->d[i]) % (BN_ULLONG)w);
591
+ #endif
592
+ }
593
+ return (BN_ULONG)ret;
594
+ }