ring-native 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (267) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/CHANGES.md +7 -0
  4. data/Makefile +5 -0
  5. data/README.md +12 -5
  6. data/Rakefile +4 -0
  7. data/ext/ring/extconf.rb +4 -5
  8. data/lib/ring/native.rb +3 -1
  9. data/lib/ring/native/version.rb +5 -1
  10. data/ring-native.gemspec +6 -6
  11. data/vendor/ring-ffi/Cargo.lock +26 -0
  12. data/vendor/ring-ffi/Cargo.toml +45 -0
  13. data/vendor/ring-ffi/LICENSE +16 -0
  14. data/vendor/ring-ffi/README.md +59 -0
  15. data/vendor/ring-ffi/src/lib.rs +79 -0
  16. metadata +10 -255
  17. data/vendor/ring/BUILDING.md +0 -40
  18. data/vendor/ring/Cargo.toml +0 -43
  19. data/vendor/ring/LICENSE +0 -185
  20. data/vendor/ring/Makefile +0 -35
  21. data/vendor/ring/PORTING.md +0 -163
  22. data/vendor/ring/README.md +0 -113
  23. data/vendor/ring/STYLE.md +0 -197
  24. data/vendor/ring/appveyor.yml +0 -27
  25. data/vendor/ring/build.rs +0 -108
  26. data/vendor/ring/crypto/aes/aes.c +0 -1142
  27. data/vendor/ring/crypto/aes/aes_test.Windows.vcxproj +0 -25
  28. data/vendor/ring/crypto/aes/aes_test.cc +0 -93
  29. data/vendor/ring/crypto/aes/asm/aes-586.pl +0 -2368
  30. data/vendor/ring/crypto/aes/asm/aes-armv4.pl +0 -1249
  31. data/vendor/ring/crypto/aes/asm/aes-x86_64.pl +0 -2246
  32. data/vendor/ring/crypto/aes/asm/aesni-x86.pl +0 -1318
  33. data/vendor/ring/crypto/aes/asm/aesni-x86_64.pl +0 -2084
  34. data/vendor/ring/crypto/aes/asm/aesv8-armx.pl +0 -675
  35. data/vendor/ring/crypto/aes/asm/bsaes-armv7.pl +0 -1364
  36. data/vendor/ring/crypto/aes/asm/bsaes-x86_64.pl +0 -1565
  37. data/vendor/ring/crypto/aes/asm/vpaes-x86.pl +0 -841
  38. data/vendor/ring/crypto/aes/asm/vpaes-x86_64.pl +0 -1116
  39. data/vendor/ring/crypto/aes/internal.h +0 -87
  40. data/vendor/ring/crypto/aes/mode_wrappers.c +0 -61
  41. data/vendor/ring/crypto/bn/add.c +0 -394
  42. data/vendor/ring/crypto/bn/asm/armv4-mont.pl +0 -694
  43. data/vendor/ring/crypto/bn/asm/armv8-mont.pl +0 -1503
  44. data/vendor/ring/crypto/bn/asm/bn-586.pl +0 -774
  45. data/vendor/ring/crypto/bn/asm/co-586.pl +0 -287
  46. data/vendor/ring/crypto/bn/asm/rsaz-avx2.pl +0 -1882
  47. data/vendor/ring/crypto/bn/asm/x86-mont.pl +0 -592
  48. data/vendor/ring/crypto/bn/asm/x86_64-gcc.c +0 -599
  49. data/vendor/ring/crypto/bn/asm/x86_64-mont.pl +0 -1393
  50. data/vendor/ring/crypto/bn/asm/x86_64-mont5.pl +0 -3507
  51. data/vendor/ring/crypto/bn/bn.c +0 -352
  52. data/vendor/ring/crypto/bn/bn_asn1.c +0 -74
  53. data/vendor/ring/crypto/bn/bn_test.Windows.vcxproj +0 -25
  54. data/vendor/ring/crypto/bn/bn_test.cc +0 -1696
  55. data/vendor/ring/crypto/bn/cmp.c +0 -200
  56. data/vendor/ring/crypto/bn/convert.c +0 -433
  57. data/vendor/ring/crypto/bn/ctx.c +0 -311
  58. data/vendor/ring/crypto/bn/div.c +0 -594
  59. data/vendor/ring/crypto/bn/exponentiation.c +0 -1335
  60. data/vendor/ring/crypto/bn/gcd.c +0 -711
  61. data/vendor/ring/crypto/bn/generic.c +0 -1019
  62. data/vendor/ring/crypto/bn/internal.h +0 -316
  63. data/vendor/ring/crypto/bn/montgomery.c +0 -516
  64. data/vendor/ring/crypto/bn/mul.c +0 -888
  65. data/vendor/ring/crypto/bn/prime.c +0 -829
  66. data/vendor/ring/crypto/bn/random.c +0 -334
  67. data/vendor/ring/crypto/bn/rsaz_exp.c +0 -262
  68. data/vendor/ring/crypto/bn/rsaz_exp.h +0 -53
  69. data/vendor/ring/crypto/bn/shift.c +0 -276
  70. data/vendor/ring/crypto/bytestring/bytestring_test.Windows.vcxproj +0 -25
  71. data/vendor/ring/crypto/bytestring/bytestring_test.cc +0 -421
  72. data/vendor/ring/crypto/bytestring/cbb.c +0 -399
  73. data/vendor/ring/crypto/bytestring/cbs.c +0 -227
  74. data/vendor/ring/crypto/bytestring/internal.h +0 -46
  75. data/vendor/ring/crypto/chacha/chacha_generic.c +0 -140
  76. data/vendor/ring/crypto/chacha/chacha_vec.c +0 -323
  77. data/vendor/ring/crypto/chacha/chacha_vec_arm.S +0 -1447
  78. data/vendor/ring/crypto/chacha/chacha_vec_arm_generate.go +0 -153
  79. data/vendor/ring/crypto/cipher/cipher_test.Windows.vcxproj +0 -25
  80. data/vendor/ring/crypto/cipher/e_aes.c +0 -390
  81. data/vendor/ring/crypto/cipher/e_chacha20poly1305.c +0 -208
  82. data/vendor/ring/crypto/cipher/internal.h +0 -173
  83. data/vendor/ring/crypto/cipher/test/aes_128_gcm_tests.txt +0 -543
  84. data/vendor/ring/crypto/cipher/test/aes_128_key_wrap_tests.txt +0 -9
  85. data/vendor/ring/crypto/cipher/test/aes_256_gcm_tests.txt +0 -475
  86. data/vendor/ring/crypto/cipher/test/aes_256_key_wrap_tests.txt +0 -23
  87. data/vendor/ring/crypto/cipher/test/chacha20_poly1305_old_tests.txt +0 -422
  88. data/vendor/ring/crypto/cipher/test/chacha20_poly1305_tests.txt +0 -484
  89. data/vendor/ring/crypto/cipher/test/cipher_test.txt +0 -100
  90. data/vendor/ring/crypto/constant_time_test.Windows.vcxproj +0 -25
  91. data/vendor/ring/crypto/constant_time_test.c +0 -304
  92. data/vendor/ring/crypto/cpu-arm-asm.S +0 -32
  93. data/vendor/ring/crypto/cpu-arm.c +0 -199
  94. data/vendor/ring/crypto/cpu-intel.c +0 -261
  95. data/vendor/ring/crypto/crypto.c +0 -151
  96. data/vendor/ring/crypto/curve25519/asm/x25519-arm.S +0 -2118
  97. data/vendor/ring/crypto/curve25519/curve25519.c +0 -4888
  98. data/vendor/ring/crypto/curve25519/x25519_test.cc +0 -128
  99. data/vendor/ring/crypto/digest/md32_common.h +0 -181
  100. data/vendor/ring/crypto/ec/asm/p256-x86_64-asm.pl +0 -2725
  101. data/vendor/ring/crypto/ec/ec.c +0 -193
  102. data/vendor/ring/crypto/ec/ec_curves.c +0 -61
  103. data/vendor/ring/crypto/ec/ec_key.c +0 -228
  104. data/vendor/ring/crypto/ec/ec_montgomery.c +0 -114
  105. data/vendor/ring/crypto/ec/example_mul.Windows.vcxproj +0 -25
  106. data/vendor/ring/crypto/ec/internal.h +0 -243
  107. data/vendor/ring/crypto/ec/oct.c +0 -253
  108. data/vendor/ring/crypto/ec/p256-64.c +0 -1794
  109. data/vendor/ring/crypto/ec/p256-x86_64-table.h +0 -9548
  110. data/vendor/ring/crypto/ec/p256-x86_64.c +0 -509
  111. data/vendor/ring/crypto/ec/simple.c +0 -1007
  112. data/vendor/ring/crypto/ec/util-64.c +0 -183
  113. data/vendor/ring/crypto/ec/wnaf.c +0 -508
  114. data/vendor/ring/crypto/ecdh/ecdh.c +0 -155
  115. data/vendor/ring/crypto/ecdsa/ecdsa.c +0 -304
  116. data/vendor/ring/crypto/ecdsa/ecdsa_asn1.c +0 -193
  117. data/vendor/ring/crypto/ecdsa/ecdsa_test.Windows.vcxproj +0 -25
  118. data/vendor/ring/crypto/ecdsa/ecdsa_test.cc +0 -327
  119. data/vendor/ring/crypto/header_removed.h +0 -17
  120. data/vendor/ring/crypto/internal.h +0 -495
  121. data/vendor/ring/crypto/libring.Windows.vcxproj +0 -101
  122. data/vendor/ring/crypto/mem.c +0 -98
  123. data/vendor/ring/crypto/modes/asm/aesni-gcm-x86_64.pl +0 -1045
  124. data/vendor/ring/crypto/modes/asm/ghash-armv4.pl +0 -517
  125. data/vendor/ring/crypto/modes/asm/ghash-x86.pl +0 -1393
  126. data/vendor/ring/crypto/modes/asm/ghash-x86_64.pl +0 -1741
  127. data/vendor/ring/crypto/modes/asm/ghashv8-armx.pl +0 -422
  128. data/vendor/ring/crypto/modes/ctr.c +0 -226
  129. data/vendor/ring/crypto/modes/gcm.c +0 -1206
  130. data/vendor/ring/crypto/modes/gcm_test.Windows.vcxproj +0 -25
  131. data/vendor/ring/crypto/modes/gcm_test.c +0 -348
  132. data/vendor/ring/crypto/modes/internal.h +0 -299
  133. data/vendor/ring/crypto/perlasm/arm-xlate.pl +0 -170
  134. data/vendor/ring/crypto/perlasm/readme +0 -100
  135. data/vendor/ring/crypto/perlasm/x86_64-xlate.pl +0 -1164
  136. data/vendor/ring/crypto/perlasm/x86asm.pl +0 -292
  137. data/vendor/ring/crypto/perlasm/x86gas.pl +0 -263
  138. data/vendor/ring/crypto/perlasm/x86masm.pl +0 -200
  139. data/vendor/ring/crypto/perlasm/x86nasm.pl +0 -187
  140. data/vendor/ring/crypto/poly1305/poly1305.c +0 -331
  141. data/vendor/ring/crypto/poly1305/poly1305_arm.c +0 -301
  142. data/vendor/ring/crypto/poly1305/poly1305_arm_asm.S +0 -2015
  143. data/vendor/ring/crypto/poly1305/poly1305_test.Windows.vcxproj +0 -25
  144. data/vendor/ring/crypto/poly1305/poly1305_test.cc +0 -80
  145. data/vendor/ring/crypto/poly1305/poly1305_test.txt +0 -52
  146. data/vendor/ring/crypto/poly1305/poly1305_vec.c +0 -892
  147. data/vendor/ring/crypto/rand/asm/rdrand-x86_64.pl +0 -75
  148. data/vendor/ring/crypto/rand/internal.h +0 -32
  149. data/vendor/ring/crypto/rand/rand.c +0 -189
  150. data/vendor/ring/crypto/rand/urandom.c +0 -219
  151. data/vendor/ring/crypto/rand/windows.c +0 -56
  152. data/vendor/ring/crypto/refcount_c11.c +0 -66
  153. data/vendor/ring/crypto/refcount_lock.c +0 -53
  154. data/vendor/ring/crypto/refcount_test.Windows.vcxproj +0 -25
  155. data/vendor/ring/crypto/refcount_test.c +0 -58
  156. data/vendor/ring/crypto/rsa/blinding.c +0 -462
  157. data/vendor/ring/crypto/rsa/internal.h +0 -108
  158. data/vendor/ring/crypto/rsa/padding.c +0 -300
  159. data/vendor/ring/crypto/rsa/rsa.c +0 -450
  160. data/vendor/ring/crypto/rsa/rsa_asn1.c +0 -261
  161. data/vendor/ring/crypto/rsa/rsa_impl.c +0 -944
  162. data/vendor/ring/crypto/rsa/rsa_test.Windows.vcxproj +0 -25
  163. data/vendor/ring/crypto/rsa/rsa_test.cc +0 -437
  164. data/vendor/ring/crypto/sha/asm/sha-armv8.pl +0 -436
  165. data/vendor/ring/crypto/sha/asm/sha-x86_64.pl +0 -2390
  166. data/vendor/ring/crypto/sha/asm/sha256-586.pl +0 -1275
  167. data/vendor/ring/crypto/sha/asm/sha256-armv4.pl +0 -735
  168. data/vendor/ring/crypto/sha/asm/sha256-armv8.pl +0 -14
  169. data/vendor/ring/crypto/sha/asm/sha256-x86_64.pl +0 -14
  170. data/vendor/ring/crypto/sha/asm/sha512-586.pl +0 -911
  171. data/vendor/ring/crypto/sha/asm/sha512-armv4.pl +0 -666
  172. data/vendor/ring/crypto/sha/asm/sha512-armv8.pl +0 -14
  173. data/vendor/ring/crypto/sha/asm/sha512-x86_64.pl +0 -14
  174. data/vendor/ring/crypto/sha/sha1.c +0 -271
  175. data/vendor/ring/crypto/sha/sha256.c +0 -204
  176. data/vendor/ring/crypto/sha/sha512.c +0 -355
  177. data/vendor/ring/crypto/test/file_test.cc +0 -326
  178. data/vendor/ring/crypto/test/file_test.h +0 -181
  179. data/vendor/ring/crypto/test/malloc.cc +0 -150
  180. data/vendor/ring/crypto/test/scoped_types.h +0 -95
  181. data/vendor/ring/crypto/test/test.Windows.vcxproj +0 -35
  182. data/vendor/ring/crypto/test/test_util.cc +0 -46
  183. data/vendor/ring/crypto/test/test_util.h +0 -41
  184. data/vendor/ring/crypto/thread_none.c +0 -55
  185. data/vendor/ring/crypto/thread_pthread.c +0 -165
  186. data/vendor/ring/crypto/thread_test.Windows.vcxproj +0 -25
  187. data/vendor/ring/crypto/thread_test.c +0 -200
  188. data/vendor/ring/crypto/thread_win.c +0 -282
  189. data/vendor/ring/examples/checkdigest.rs +0 -103
  190. data/vendor/ring/include/openssl/aes.h +0 -121
  191. data/vendor/ring/include/openssl/arm_arch.h +0 -129
  192. data/vendor/ring/include/openssl/base.h +0 -156
  193. data/vendor/ring/include/openssl/bn.h +0 -794
  194. data/vendor/ring/include/openssl/buffer.h +0 -18
  195. data/vendor/ring/include/openssl/bytestring.h +0 -235
  196. data/vendor/ring/include/openssl/chacha.h +0 -37
  197. data/vendor/ring/include/openssl/cmac.h +0 -76
  198. data/vendor/ring/include/openssl/cpu.h +0 -184
  199. data/vendor/ring/include/openssl/crypto.h +0 -43
  200. data/vendor/ring/include/openssl/curve25519.h +0 -88
  201. data/vendor/ring/include/openssl/ec.h +0 -225
  202. data/vendor/ring/include/openssl/ec_key.h +0 -129
  203. data/vendor/ring/include/openssl/ecdh.h +0 -110
  204. data/vendor/ring/include/openssl/ecdsa.h +0 -156
  205. data/vendor/ring/include/openssl/err.h +0 -201
  206. data/vendor/ring/include/openssl/mem.h +0 -101
  207. data/vendor/ring/include/openssl/obj_mac.h +0 -71
  208. data/vendor/ring/include/openssl/opensslfeatures.h +0 -68
  209. data/vendor/ring/include/openssl/opensslv.h +0 -18
  210. data/vendor/ring/include/openssl/ossl_typ.h +0 -18
  211. data/vendor/ring/include/openssl/poly1305.h +0 -51
  212. data/vendor/ring/include/openssl/rand.h +0 -70
  213. data/vendor/ring/include/openssl/rsa.h +0 -399
  214. data/vendor/ring/include/openssl/thread.h +0 -133
  215. data/vendor/ring/include/openssl/type_check.h +0 -71
  216. data/vendor/ring/mk/Common.props +0 -63
  217. data/vendor/ring/mk/Windows.props +0 -42
  218. data/vendor/ring/mk/WindowsTest.props +0 -18
  219. data/vendor/ring/mk/appveyor.bat +0 -62
  220. data/vendor/ring/mk/bottom_of_makefile.mk +0 -54
  221. data/vendor/ring/mk/ring.mk +0 -266
  222. data/vendor/ring/mk/top_of_makefile.mk +0 -214
  223. data/vendor/ring/mk/travis.sh +0 -40
  224. data/vendor/ring/mk/update-travis-yml.py +0 -229
  225. data/vendor/ring/ring.sln +0 -153
  226. data/vendor/ring/src/aead.rs +0 -682
  227. data/vendor/ring/src/agreement.rs +0 -248
  228. data/vendor/ring/src/c.rs +0 -129
  229. data/vendor/ring/src/constant_time.rs +0 -37
  230. data/vendor/ring/src/der.rs +0 -96
  231. data/vendor/ring/src/digest.rs +0 -690
  232. data/vendor/ring/src/digest_tests.txt +0 -57
  233. data/vendor/ring/src/ecc.rs +0 -28
  234. data/vendor/ring/src/ecc_build.rs +0 -279
  235. data/vendor/ring/src/ecc_curves.rs +0 -117
  236. data/vendor/ring/src/ed25519_tests.txt +0 -2579
  237. data/vendor/ring/src/exe_tests.rs +0 -46
  238. data/vendor/ring/src/ffi.rs +0 -29
  239. data/vendor/ring/src/file_test.rs +0 -187
  240. data/vendor/ring/src/hkdf.rs +0 -153
  241. data/vendor/ring/src/hkdf_tests.txt +0 -59
  242. data/vendor/ring/src/hmac.rs +0 -414
  243. data/vendor/ring/src/hmac_tests.txt +0 -97
  244. data/vendor/ring/src/input.rs +0 -312
  245. data/vendor/ring/src/lib.rs +0 -41
  246. data/vendor/ring/src/pbkdf2.rs +0 -265
  247. data/vendor/ring/src/pbkdf2_tests.txt +0 -113
  248. data/vendor/ring/src/polyfill.rs +0 -57
  249. data/vendor/ring/src/rand.rs +0 -28
  250. data/vendor/ring/src/signature.rs +0 -314
  251. data/vendor/ring/third-party/NIST/README.md +0 -9
  252. data/vendor/ring/third-party/NIST/SHAVS/SHA1LongMsg.rsp +0 -263
  253. data/vendor/ring/third-party/NIST/SHAVS/SHA1Monte.rsp +0 -309
  254. data/vendor/ring/third-party/NIST/SHAVS/SHA1ShortMsg.rsp +0 -267
  255. data/vendor/ring/third-party/NIST/SHAVS/SHA224LongMsg.rsp +0 -263
  256. data/vendor/ring/third-party/NIST/SHAVS/SHA224Monte.rsp +0 -309
  257. data/vendor/ring/third-party/NIST/SHAVS/SHA224ShortMsg.rsp +0 -267
  258. data/vendor/ring/third-party/NIST/SHAVS/SHA256LongMsg.rsp +0 -263
  259. data/vendor/ring/third-party/NIST/SHAVS/SHA256Monte.rsp +0 -309
  260. data/vendor/ring/third-party/NIST/SHAVS/SHA256ShortMsg.rsp +0 -267
  261. data/vendor/ring/third-party/NIST/SHAVS/SHA384LongMsg.rsp +0 -519
  262. data/vendor/ring/third-party/NIST/SHAVS/SHA384Monte.rsp +0 -309
  263. data/vendor/ring/third-party/NIST/SHAVS/SHA384ShortMsg.rsp +0 -523
  264. data/vendor/ring/third-party/NIST/SHAVS/SHA512LongMsg.rsp +0 -519
  265. data/vendor/ring/third-party/NIST/SHAVS/SHA512Monte.rsp +0 -309
  266. data/vendor/ring/third-party/NIST/SHAVS/SHA512ShortMsg.rsp +0 -523
  267. data/vendor/ring/third-party/NIST/sha256sums.txt +0 -1
@@ -1,311 +0,0 @@
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
- }
@@ -1,594 +0,0 @@
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
- }