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,1007 @@
1
+ /* Originally written by Bodo Moeller for the OpenSSL project.
2
+ * ====================================================================
3
+ * Copyright (c) 1998-2005 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
+ /* ====================================================================
56
+ * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
57
+ *
58
+ * Portions of the attached software ("Contribution") are developed by
59
+ * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
60
+ *
61
+ * The Contribution is licensed pursuant to the OpenSSL open source
62
+ * license provided above.
63
+ *
64
+ * The elliptic curve binary polynomial software is originally written by
65
+ * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
66
+ * Laboratories. */
67
+
68
+ #include <openssl/ec.h>
69
+
70
+ #include <assert.h>
71
+ #include <string.h>
72
+
73
+ #include <openssl/bn.h>
74
+ #include <openssl/err.h>
75
+ #include <openssl/mem.h>
76
+
77
+ #include "internal.h"
78
+
79
+
80
+ /* Most method functions in this file are designed to work with non-trivial
81
+ * representations of field elements if necessary (see ecp_mont.c): while
82
+ * standard modular addition and subtraction are used, the field_mul and
83
+ * field_sqr methods will be used for multiplication, and field_encode and
84
+ * field_decode (if defined) will be used for converting between
85
+ * representations.
86
+
87
+ * Functions ec_GFp_simple_points_make_affine() and
88
+ * ec_GFp_simple_point_get_affine_coordinates() specifically assume that if a
89
+ * non-trivial representation is used, it is a Montgomery representation (i.e.
90
+ * 'encoding' means multiplying by some factor R). */
91
+
92
+ unsigned ec_GFp_simple_group_get_degree(const EC_GROUP *group) {
93
+ return BN_num_bits(&group->field);
94
+ }
95
+
96
+ int ec_GFp_simple_point_init(EC_POINT *point) {
97
+ BN_init(&point->X);
98
+ BN_init(&point->Y);
99
+ BN_init(&point->Z);
100
+
101
+ return 1;
102
+ }
103
+
104
+ void ec_GFp_simple_point_finish(EC_POINT *point) {
105
+ BN_free(&point->X);
106
+ BN_free(&point->Y);
107
+ BN_free(&point->Z);
108
+ }
109
+
110
+ void ec_GFp_simple_point_clear_finish(EC_POINT *point) {
111
+ BN_clear_free(&point->X);
112
+ BN_clear_free(&point->Y);
113
+ BN_clear_free(&point->Z);
114
+ }
115
+
116
+ int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src) {
117
+ if (!BN_copy(&dest->X, &src->X) ||
118
+ !BN_copy(&dest->Y, &src->Y) ||
119
+ !BN_copy(&dest->Z, &src->Z)) {
120
+ return 0;
121
+ }
122
+
123
+ return 1;
124
+ }
125
+
126
+ int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group,
127
+ EC_POINT *point) {
128
+ BN_zero(&point->Z);
129
+ return 1;
130
+ }
131
+
132
+ int ec_GFp_simple_set_Jprojective_coordinates_GFp(
133
+ const EC_GROUP *group, EC_POINT *point, const BIGNUM *x, const BIGNUM *y,
134
+ const BIGNUM *z, BN_CTX *ctx) {
135
+ BN_CTX *new_ctx = NULL;
136
+ int ret = 0;
137
+
138
+ if (ctx == NULL) {
139
+ ctx = new_ctx = BN_CTX_new();
140
+ if (ctx == NULL) {
141
+ return 0;
142
+ }
143
+ }
144
+
145
+ if (x != NULL) {
146
+ if (!BN_nnmod(&point->X, x, &group->field, ctx)) {
147
+ goto err;
148
+ }
149
+ if (group->meth->field_encode &&
150
+ !group->meth->field_encode(group, &point->X, &point->X, ctx)) {
151
+ goto err;
152
+ }
153
+ }
154
+
155
+ if (y != NULL) {
156
+ if (!BN_nnmod(&point->Y, y, &group->field, ctx)) {
157
+ goto err;
158
+ }
159
+ if (group->meth->field_encode &&
160
+ !group->meth->field_encode(group, &point->Y, &point->Y, ctx)) {
161
+ goto err;
162
+ }
163
+ }
164
+
165
+ if (z != NULL) {
166
+ if (!BN_nnmod(&point->Z, z, &group->field, ctx)) {
167
+ goto err;
168
+ }
169
+ int Z_is_one = BN_is_one(&point->Z);
170
+ if (group->meth->field_encode) {
171
+ if (Z_is_one && (group->meth->field_set_to_one != 0)) {
172
+ if (!group->meth->field_set_to_one(group, &point->Z, ctx)) {
173
+ goto err;
174
+ }
175
+ } else if (!group->meth->field_encode(group, &point->Z, &point->Z, ctx)) {
176
+ goto err;
177
+ }
178
+ }
179
+ }
180
+
181
+ ret = 1;
182
+
183
+ err:
184
+ BN_CTX_free(new_ctx);
185
+ return ret;
186
+ }
187
+
188
+ int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group,
189
+ EC_POINT *point, const BIGNUM *x,
190
+ const BIGNUM *y, BN_CTX *ctx) {
191
+ if (x == NULL || y == NULL) {
192
+ /* unlike for projective coordinates, we do not tolerate this */
193
+ OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
194
+ return 0;
195
+ }
196
+
197
+ if (!ec_point_set_Jprojective_coordinates_GFp(group, point, x, y,
198
+ BN_value_one(), ctx)) {
199
+ return 0;
200
+ }
201
+
202
+ if (!ec_GFp_simple_is_on_curve(group, point, ctx)) {
203
+ OPENSSL_PUT_ERROR(EC, EC_R_POINT_IS_NOT_ON_CURVE);
204
+ return 0;
205
+ }
206
+
207
+ return 1;
208
+ }
209
+
210
+ int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group,
211
+ const EC_POINT *point, BIGNUM *x,
212
+ BIGNUM *y, BN_CTX *ctx) {
213
+ BN_CTX *new_ctx = NULL;
214
+ BIGNUM *Z, *Z_1, *Z_2, *Z_3;
215
+ const BIGNUM *Z_;
216
+ int ret = 0;
217
+
218
+ if (EC_POINT_is_at_infinity(group, point)) {
219
+ OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
220
+ return 0;
221
+ }
222
+
223
+ if (ctx == NULL) {
224
+ ctx = new_ctx = BN_CTX_new();
225
+ if (ctx == NULL) {
226
+ return 0;
227
+ }
228
+ }
229
+
230
+ BN_CTX_start(ctx);
231
+ Z = BN_CTX_get(ctx);
232
+ Z_1 = BN_CTX_get(ctx);
233
+ Z_2 = BN_CTX_get(ctx);
234
+ Z_3 = BN_CTX_get(ctx);
235
+ if (Z == NULL || Z_1 == NULL || Z_2 == NULL || Z_3 == NULL) {
236
+ goto err;
237
+ }
238
+
239
+ /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
240
+
241
+ if (group->meth->field_decode) {
242
+ if (!group->meth->field_decode(group, Z, &point->Z, ctx)) {
243
+ goto err;
244
+ }
245
+ Z_ = Z;
246
+ } else {
247
+ Z_ = &point->Z;
248
+ }
249
+
250
+ if (BN_is_one(Z_)) {
251
+ if (group->meth->field_decode) {
252
+ if (x != NULL && !group->meth->field_decode(group, x, &point->X, ctx)) {
253
+ goto err;
254
+ }
255
+ if (y != NULL && !group->meth->field_decode(group, y, &point->Y, ctx)) {
256
+ goto err;
257
+ }
258
+ } else {
259
+ if (x != NULL && !BN_copy(x, &point->X)) {
260
+ goto err;
261
+ }
262
+ if (y != NULL && !BN_copy(y, &point->Y)) {
263
+ goto err;
264
+ }
265
+ }
266
+ } else {
267
+ if (!BN_mod_inverse(Z_1, Z_, &group->field, ctx)) {
268
+ OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
269
+ goto err;
270
+ }
271
+
272
+ if (group->meth->field_encode == 0) {
273
+ /* field_sqr works on standard representation */
274
+ if (!group->meth->field_sqr(group, Z_2, Z_1, ctx)) {
275
+ goto err;
276
+ }
277
+ } else if (!BN_mod_sqr(Z_2, Z_1, &group->field, ctx)) {
278
+ goto err;
279
+ }
280
+
281
+ /* in the Montgomery case, field_mul will cancel out Montgomery factor in
282
+ * X: */
283
+ if (x != NULL && !group->meth->field_mul(group, x, &point->X, Z_2, ctx)) {
284
+ goto err;
285
+ }
286
+
287
+ if (y != NULL) {
288
+ if (group->meth->field_encode == 0) {
289
+ /* field_mul works on standard representation */
290
+ if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx)) {
291
+ goto err;
292
+ }
293
+ } else if (!BN_mod_mul(Z_3, Z_2, Z_1, &group->field, ctx)) {
294
+ goto err;
295
+ }
296
+
297
+ /* in the Montgomery case, field_mul will cancel out Montgomery factor in
298
+ * Y: */
299
+ if (!group->meth->field_mul(group, y, &point->Y, Z_3, ctx)) {
300
+ goto err;
301
+ }
302
+ }
303
+ }
304
+
305
+ ret = 1;
306
+
307
+ err:
308
+ BN_CTX_end(ctx);
309
+ BN_CTX_free(new_ctx);
310
+ return ret;
311
+ }
312
+
313
+ int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
314
+ const EC_POINT *b, BN_CTX *ctx) {
315
+ int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
316
+ BN_CTX *);
317
+ int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
318
+ const BIGNUM *p;
319
+ BN_CTX *new_ctx = NULL;
320
+ BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
321
+ int ret = 0;
322
+
323
+ if (a == b) {
324
+ return ec_GFp_simple_dbl(group, r, a, ctx);
325
+ }
326
+ if (EC_POINT_is_at_infinity(group, a)) {
327
+ return ec_GFp_simple_point_copy(r, b);
328
+ }
329
+ if (EC_POINT_is_at_infinity(group, b)) {
330
+ return ec_GFp_simple_point_copy(r, a);
331
+ }
332
+
333
+ field_mul = group->meth->field_mul;
334
+ field_sqr = group->meth->field_sqr;
335
+ p = &group->field;
336
+
337
+ if (ctx == NULL) {
338
+ ctx = new_ctx = BN_CTX_new();
339
+ if (ctx == NULL) {
340
+ return 0;
341
+ }
342
+ }
343
+
344
+ BN_CTX_start(ctx);
345
+ n0 = BN_CTX_get(ctx);
346
+ n1 = BN_CTX_get(ctx);
347
+ n2 = BN_CTX_get(ctx);
348
+ n3 = BN_CTX_get(ctx);
349
+ n4 = BN_CTX_get(ctx);
350
+ n5 = BN_CTX_get(ctx);
351
+ n6 = BN_CTX_get(ctx);
352
+ if (n6 == NULL) {
353
+ goto end;
354
+ }
355
+
356
+ /* Note that in this function we must not read components of 'a' or 'b'
357
+ * once we have written the corresponding components of 'r'.
358
+ * ('r' might be one of 'a' or 'b'.)
359
+ */
360
+
361
+ /* n1, n2 */
362
+ int b_Z_is_one = BN_cmp(&b->Z, &group->one) == 0;
363
+
364
+ if (b_Z_is_one) {
365
+ if (!BN_copy(n1, &a->X) || !BN_copy(n2, &a->Y)) {
366
+ goto end;
367
+ }
368
+ /* n1 = X_a */
369
+ /* n2 = Y_a */
370
+ } else {
371
+ if (!field_sqr(group, n0, &b->Z, ctx) ||
372
+ !field_mul(group, n1, &a->X, n0, ctx)) {
373
+ goto end;
374
+ }
375
+ /* n1 = X_a * Z_b^2 */
376
+
377
+ if (!field_mul(group, n0, n0, &b->Z, ctx) ||
378
+ !field_mul(group, n2, &a->Y, n0, ctx)) {
379
+ goto end;
380
+ }
381
+ /* n2 = Y_a * Z_b^3 */
382
+ }
383
+
384
+ /* n3, n4 */
385
+ int a_Z_is_one = BN_cmp(&a->Z, &group->one) == 0;
386
+ if (a_Z_is_one) {
387
+ if (!BN_copy(n3, &b->X) || !BN_copy(n4, &b->Y)) {
388
+ goto end;
389
+ }
390
+ /* n3 = X_b */
391
+ /* n4 = Y_b */
392
+ } else {
393
+ if (!field_sqr(group, n0, &a->Z, ctx) ||
394
+ !field_mul(group, n3, &b->X, n0, ctx)) {
395
+ goto end;
396
+ }
397
+ /* n3 = X_b * Z_a^2 */
398
+
399
+ if (!field_mul(group, n0, n0, &a->Z, ctx) ||
400
+ !field_mul(group, n4, &b->Y, n0, ctx)) {
401
+ goto end;
402
+ }
403
+ /* n4 = Y_b * Z_a^3 */
404
+ }
405
+
406
+ /* n5, n6 */
407
+ if (!BN_mod_sub_quick(n5, n1, n3, p) ||
408
+ !BN_mod_sub_quick(n6, n2, n4, p)) {
409
+ goto end;
410
+ }
411
+ /* n5 = n1 - n3 */
412
+ /* n6 = n2 - n4 */
413
+
414
+ if (BN_is_zero(n5)) {
415
+ if (BN_is_zero(n6)) {
416
+ /* a is the same point as b */
417
+ BN_CTX_end(ctx);
418
+ ret = ec_GFp_simple_dbl(group, r, a, ctx);
419
+ ctx = NULL;
420
+ goto end;
421
+ } else {
422
+ /* a is the inverse of b */
423
+ BN_zero(&r->Z);
424
+ ret = 1;
425
+ goto end;
426
+ }
427
+ }
428
+
429
+ /* 'n7', 'n8' */
430
+ if (!BN_mod_add_quick(n1, n1, n3, p) ||
431
+ !BN_mod_add_quick(n2, n2, n4, p)) {
432
+ goto end;
433
+ }
434
+ /* 'n7' = n1 + n3 */
435
+ /* 'n8' = n2 + n4 */
436
+
437
+ /* Z_r */
438
+ if (a_Z_is_one && b_Z_is_one) {
439
+ if (!BN_copy(&r->Z, n5)) {
440
+ goto end;
441
+ }
442
+ } else {
443
+ if (a_Z_is_one) {
444
+ if (!BN_copy(n0, &b->Z)) {
445
+ goto end;
446
+ }
447
+ } else if (b_Z_is_one) {
448
+ if (!BN_copy(n0, &a->Z)) {
449
+ goto end;
450
+ }
451
+ } else if (!field_mul(group, n0, &a->Z, &b->Z, ctx)) {
452
+ goto end;
453
+ }
454
+ if (!field_mul(group, &r->Z, n0, n5, ctx)) {
455
+ goto end;
456
+ }
457
+ }
458
+
459
+ /* Z_r = Z_a * Z_b * n5 */
460
+
461
+ /* X_r */
462
+ if (!field_sqr(group, n0, n6, ctx) ||
463
+ !field_sqr(group, n4, n5, ctx) ||
464
+ !field_mul(group, n3, n1, n4, ctx) ||
465
+ !BN_mod_sub_quick(&r->X, n0, n3, p)) {
466
+ goto end;
467
+ }
468
+ /* X_r = n6^2 - n5^2 * 'n7' */
469
+
470
+ /* 'n9' */
471
+ if (!BN_mod_lshift1_quick(n0, &r->X, p) ||
472
+ !BN_mod_sub_quick(n0, n3, n0, p)) {
473
+ goto end;
474
+ }
475
+ /* n9 = n5^2 * 'n7' - 2 * X_r */
476
+
477
+ /* Y_r */
478
+ if (!field_mul(group, n0, n0, n6, ctx) ||
479
+ !field_mul(group, n5, n4, n5, ctx)) {
480
+ goto end; /* now n5 is n5^3 */
481
+ }
482
+ if (!field_mul(group, n1, n2, n5, ctx) ||
483
+ !BN_mod_sub_quick(n0, n0, n1, p)) {
484
+ goto end;
485
+ }
486
+ if (BN_is_odd(n0) && !BN_add(n0, n0, p)) {
487
+ goto end;
488
+ }
489
+ /* now 0 <= n0 < 2*p, and n0 is even */
490
+ if (!BN_rshift1(&r->Y, n0)) {
491
+ goto end;
492
+ }
493
+ /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
494
+
495
+ ret = 1;
496
+
497
+ end:
498
+ if (ctx) {
499
+ /* otherwise we already called BN_CTX_end */
500
+ BN_CTX_end(ctx);
501
+ }
502
+ BN_CTX_free(new_ctx);
503
+ return ret;
504
+ }
505
+
506
+ int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
507
+ BN_CTX *ctx) {
508
+ int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
509
+ BN_CTX *);
510
+ int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
511
+ const BIGNUM *p;
512
+ BN_CTX *new_ctx = NULL;
513
+ BIGNUM *n0, *n1, *n2, *n3;
514
+ int ret = 0;
515
+
516
+ if (EC_POINT_is_at_infinity(group, a)) {
517
+ BN_zero(&r->Z);
518
+ return 1;
519
+ }
520
+
521
+ field_mul = group->meth->field_mul;
522
+ field_sqr = group->meth->field_sqr;
523
+ p = &group->field;
524
+
525
+ if (ctx == NULL) {
526
+ ctx = new_ctx = BN_CTX_new();
527
+ if (ctx == NULL) {
528
+ return 0;
529
+ }
530
+ }
531
+
532
+ BN_CTX_start(ctx);
533
+ n0 = BN_CTX_get(ctx);
534
+ n1 = BN_CTX_get(ctx);
535
+ n2 = BN_CTX_get(ctx);
536
+ n3 = BN_CTX_get(ctx);
537
+ if (n3 == NULL) {
538
+ goto err;
539
+ }
540
+
541
+ /* Note that in this function we must not read components of 'a'
542
+ * once we have written the corresponding components of 'r'.
543
+ * ('r' might the same as 'a'.)
544
+ */
545
+
546
+ /* n1 */
547
+ if (BN_cmp(&a->Z, &group->one) == 0) {
548
+ if (!field_sqr(group, n0, &a->X, ctx) ||
549
+ !BN_mod_lshift1_quick(n1, n0, p) ||
550
+ !BN_mod_add_quick(n0, n0, n1, p) ||
551
+ !BN_mod_add_quick(n1, n0, &group->a, p)) {
552
+ goto err;
553
+ }
554
+ /* n1 = 3 * X_a^2 + a_curve */
555
+ } else {
556
+ /* ring: This assumes a == -3. */
557
+ if (!field_sqr(group, n1, &a->Z, ctx) ||
558
+ !BN_mod_add_quick(n0, &a->X, n1, p) ||
559
+ !BN_mod_sub_quick(n2, &a->X, n1, p) ||
560
+ !field_mul(group, n1, n0, n2, ctx) ||
561
+ !BN_mod_lshift1_quick(n0, n1, p) ||
562
+ !BN_mod_add_quick(n1, n0, n1, p)) {
563
+ goto err;
564
+ }
565
+ /* n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
566
+ * = 3 * X_a^2 - 3 * Z_a^4 */
567
+ }
568
+
569
+ /* Z_r */
570
+ if (BN_cmp(&a->Z, &group->one) == 0) {
571
+ if (!BN_copy(n0, &a->Y)) {
572
+ goto err;
573
+ }
574
+ } else if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) {
575
+ goto err;
576
+ }
577
+ if (!BN_mod_lshift1_quick(&r->Z, n0, p)) {
578
+ goto err;
579
+ }
580
+ /* Z_r = 2 * Y_a * Z_a */
581
+
582
+ /* n2 */
583
+ if (!field_sqr(group, n3, &a->Y, ctx) ||
584
+ !field_mul(group, n2, &a->X, n3, ctx) ||
585
+ !BN_mod_lshift_quick(n2, n2, 2, p)) {
586
+ goto err;
587
+ }
588
+ /* n2 = 4 * X_a * Y_a^2 */
589
+
590
+ /* X_r */
591
+ if (!BN_mod_lshift1_quick(n0, n2, p) ||
592
+ !field_sqr(group, &r->X, n1, ctx) ||
593
+ !BN_mod_sub_quick(&r->X, &r->X, n0, p)) {
594
+ goto err;
595
+ }
596
+ /* X_r = n1^2 - 2 * n2 */
597
+
598
+ /* n3 */
599
+ if (!field_sqr(group, n0, n3, ctx) ||
600
+ !BN_mod_lshift_quick(n3, n0, 3, p)) {
601
+ goto err;
602
+ }
603
+ /* n3 = 8 * Y_a^4 */
604
+
605
+ /* Y_r */
606
+ if (!BN_mod_sub_quick(n0, n2, &r->X, p) ||
607
+ !field_mul(group, n0, n1, n0, ctx) ||
608
+ !BN_mod_sub_quick(&r->Y, n0, n3, p)) {
609
+ goto err;
610
+ }
611
+ /* Y_r = n1 * (n2 - X_r) - n3 */
612
+
613
+ ret = 1;
614
+
615
+ err:
616
+ BN_CTX_end(ctx);
617
+ BN_CTX_free(new_ctx);
618
+ return ret;
619
+ }
620
+
621
+ int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) {
622
+ if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y)) {
623
+ /* point is its own inverse */
624
+ return 1;
625
+ }
626
+
627
+ return BN_usub(&point->Y, &group->field, &point->Y);
628
+ }
629
+
630
+ int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) {
631
+ return BN_is_zero(&point->Z);
632
+ }
633
+
634
+ int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
635
+ BN_CTX *ctx) {
636
+ int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
637
+ BN_CTX *);
638
+ int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
639
+ const BIGNUM *p;
640
+ BN_CTX *new_ctx = NULL;
641
+ BIGNUM *rh, *tmp, *Z4, *Z6;
642
+ int ret = -1;
643
+
644
+ if (EC_POINT_is_at_infinity(group, point)) {
645
+ return 1;
646
+ }
647
+
648
+ field_mul = group->meth->field_mul;
649
+ field_sqr = group->meth->field_sqr;
650
+ p = &group->field;
651
+
652
+ if (ctx == NULL) {
653
+ ctx = new_ctx = BN_CTX_new();
654
+ if (ctx == NULL) {
655
+ return -1;
656
+ }
657
+ }
658
+
659
+ BN_CTX_start(ctx);
660
+ rh = BN_CTX_get(ctx);
661
+ tmp = BN_CTX_get(ctx);
662
+ Z4 = BN_CTX_get(ctx);
663
+ Z6 = BN_CTX_get(ctx);
664
+ if (Z6 == NULL) {
665
+ goto err;
666
+ }
667
+
668
+ /* We have a curve defined by a Weierstrass equation
669
+ * y^2 = x^3 + a*x + b.
670
+ * The point to consider is given in Jacobian projective coordinates
671
+ * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
672
+ * Substituting this and multiplying by Z^6 transforms the above equation
673
+ * into
674
+ * Y^2 = X^3 + a*X*Z^4 + b*Z^6.
675
+ * To test this, we add up the right-hand side in 'rh'.
676
+ */
677
+
678
+ /* rh := X^2 */
679
+ if (!field_sqr(group, rh, &point->X, ctx)) {
680
+ goto err;
681
+ }
682
+
683
+ if (BN_cmp(&point->Z, &group->one) != 0) {
684
+ if (!field_sqr(group, tmp, &point->Z, ctx) ||
685
+ !field_sqr(group, Z4, tmp, ctx) ||
686
+ !field_mul(group, Z6, Z4, tmp, ctx)) {
687
+ goto err;
688
+ }
689
+
690
+ /* rh := (rh + a*Z^4)*X */
691
+ /* ring: This assumes a == -3. */
692
+ if (!BN_mod_lshift1_quick(tmp, Z4, p) ||
693
+ !BN_mod_add_quick(tmp, tmp, Z4, p) ||
694
+ !BN_mod_sub_quick(rh, rh, tmp, p) ||
695
+ !field_mul(group, rh, rh, &point->X, ctx)) {
696
+ goto err;
697
+ }
698
+
699
+ /* rh := rh + b*Z^6 */
700
+ if (!field_mul(group, tmp, &group->b, Z6, ctx) ||
701
+ !BN_mod_add_quick(rh, rh, tmp, p)) {
702
+ goto err;
703
+ }
704
+ } else {
705
+ /* rh := (rh + a)*X */
706
+ if (!BN_mod_add_quick(rh, rh, &group->a, p) ||
707
+ !field_mul(group, rh, rh, &point->X, ctx)) {
708
+ goto err;
709
+ }
710
+ /* rh := rh + b */
711
+ if (!BN_mod_add_quick(rh, rh, &group->b, p)) {
712
+ goto err;
713
+ }
714
+ }
715
+
716
+ /* 'lh' := Y^2 */
717
+ if (!field_sqr(group, tmp, &point->Y, ctx)) {
718
+ goto err;
719
+ }
720
+
721
+ ret = (0 == BN_ucmp(tmp, rh));
722
+
723
+ err:
724
+ BN_CTX_end(ctx);
725
+ BN_CTX_free(new_ctx);
726
+ return ret;
727
+ }
728
+
729
+ int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
730
+ const EC_POINT *b, BN_CTX *ctx) {
731
+ /* return values:
732
+ * -1 error
733
+ * 0 equal (in affine coordinates)
734
+ * 1 not equal
735
+ */
736
+
737
+ int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *,
738
+ BN_CTX *);
739
+ int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
740
+ BN_CTX *new_ctx = NULL;
741
+ BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
742
+ const BIGNUM *tmp1_, *tmp2_;
743
+ int ret = -1;
744
+
745
+ if (EC_POINT_is_at_infinity(group, a)) {
746
+ return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
747
+ }
748
+
749
+ if (EC_POINT_is_at_infinity(group, b)) {
750
+ return 1;
751
+ }
752
+
753
+ int a_Z_is_one = BN_cmp(&a->Z, &group->one) == 0;
754
+ int b_Z_is_one = BN_cmp(&b->Z, &group->one) == 0;
755
+
756
+ if (a_Z_is_one && b_Z_is_one) {
757
+ return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
758
+ }
759
+
760
+ field_mul = group->meth->field_mul;
761
+ field_sqr = group->meth->field_sqr;
762
+
763
+ if (ctx == NULL) {
764
+ ctx = new_ctx = BN_CTX_new();
765
+ if (ctx == NULL) {
766
+ return -1;
767
+ }
768
+ }
769
+
770
+ BN_CTX_start(ctx);
771
+ tmp1 = BN_CTX_get(ctx);
772
+ tmp2 = BN_CTX_get(ctx);
773
+ Za23 = BN_CTX_get(ctx);
774
+ Zb23 = BN_CTX_get(ctx);
775
+ if (Zb23 == NULL) {
776
+ goto end;
777
+ }
778
+
779
+ /* We have to decide whether
780
+ * (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
781
+ * or equivalently, whether
782
+ * (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
783
+ */
784
+
785
+ if (!b_Z_is_one) {
786
+ if (!field_sqr(group, Zb23, &b->Z, ctx) ||
787
+ !field_mul(group, tmp1, &a->X, Zb23, ctx)) {
788
+ goto end;
789
+ }
790
+ tmp1_ = tmp1;
791
+ } else {
792
+ tmp1_ = &a->X;
793
+ }
794
+ if (!a_Z_is_one) {
795
+ if (!field_sqr(group, Za23, &a->Z, ctx) ||
796
+ !field_mul(group, tmp2, &b->X, Za23, ctx)) {
797
+ goto end;
798
+ }
799
+ tmp2_ = tmp2;
800
+ } else {
801
+ tmp2_ = &b->X;
802
+ }
803
+
804
+ /* compare X_a*Z_b^2 with X_b*Z_a^2 */
805
+ if (BN_cmp(tmp1_, tmp2_) != 0) {
806
+ ret = 1; /* points differ */
807
+ goto end;
808
+ }
809
+
810
+
811
+ if (!b_Z_is_one) {
812
+ if (!field_mul(group, Zb23, Zb23, &b->Z, ctx) ||
813
+ !field_mul(group, tmp1, &a->Y, Zb23, ctx)) {
814
+ goto end;
815
+ }
816
+ /* tmp1_ = tmp1 */
817
+ } else {
818
+ tmp1_ = &a->Y;
819
+ }
820
+ if (!a_Z_is_one) {
821
+ if (!field_mul(group, Za23, Za23, &a->Z, ctx) ||
822
+ !field_mul(group, tmp2, &b->Y, Za23, ctx)) {
823
+ goto end;
824
+ }
825
+ /* tmp2_ = tmp2 */
826
+ } else {
827
+ tmp2_ = &b->Y;
828
+ }
829
+
830
+ /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */
831
+ if (BN_cmp(tmp1_, tmp2_) != 0) {
832
+ ret = 1; /* points differ */
833
+ goto end;
834
+ }
835
+
836
+ /* points are equal */
837
+ ret = 0;
838
+
839
+ end:
840
+ BN_CTX_end(ctx);
841
+ BN_CTX_free(new_ctx);
842
+ return ret;
843
+ }
844
+
845
+ int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,
846
+ EC_POINT *points[], BN_CTX *ctx) {
847
+ BN_CTX *new_ctx = NULL;
848
+ BIGNUM *tmp, *tmp_Z;
849
+ BIGNUM **prod_Z = NULL;
850
+ size_t i;
851
+ int ret = 0;
852
+
853
+ if (num == 0) {
854
+ return 1;
855
+ }
856
+
857
+ if (ctx == NULL) {
858
+ ctx = new_ctx = BN_CTX_new();
859
+ if (ctx == NULL) {
860
+ return 0;
861
+ }
862
+ }
863
+
864
+ BN_CTX_start(ctx);
865
+ tmp = BN_CTX_get(ctx);
866
+ tmp_Z = BN_CTX_get(ctx);
867
+ if (tmp == NULL || tmp_Z == NULL) {
868
+ goto err;
869
+ }
870
+
871
+ prod_Z = OPENSSL_malloc(num * sizeof(prod_Z[0]));
872
+ if (prod_Z == NULL) {
873
+ goto err;
874
+ }
875
+ memset(prod_Z, 0, num * sizeof(prod_Z[0]));
876
+ for (i = 0; i < num; i++) {
877
+ prod_Z[i] = BN_new();
878
+ if (prod_Z[i] == NULL) {
879
+ goto err;
880
+ }
881
+ }
882
+
883
+ /* Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z,
884
+ * skipping any zero-valued inputs (pretend that they're 1). */
885
+
886
+ if (!BN_is_zero(&points[0]->Z)) {
887
+ if (!BN_copy(prod_Z[0], &points[0]->Z)) {
888
+ goto err;
889
+ }
890
+ } else {
891
+ if (group->meth->field_set_to_one != 0) {
892
+ if (!group->meth->field_set_to_one(group, prod_Z[0], ctx)) {
893
+ goto err;
894
+ }
895
+ } else {
896
+ if (!BN_one(prod_Z[0])) {
897
+ goto err;
898
+ }
899
+ }
900
+ }
901
+
902
+ for (i = 1; i < num; i++) {
903
+ if (!BN_is_zero(&points[i]->Z)) {
904
+ if (!group->meth->field_mul(group, prod_Z[i], prod_Z[i - 1],
905
+ &points[i]->Z, ctx)) {
906
+ goto err;
907
+ }
908
+ } else {
909
+ if (!BN_copy(prod_Z[i], prod_Z[i - 1])) {
910
+ goto err;
911
+ }
912
+ }
913
+ }
914
+
915
+ /* Now use a single explicit inversion to replace every
916
+ * non-zero points[i]->Z by its inverse. */
917
+
918
+ if (!BN_mod_inverse(tmp, prod_Z[num - 1], &group->field, ctx)) {
919
+ OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
920
+ goto err;
921
+ }
922
+
923
+ if (group->meth->field_encode != NULL) {
924
+ /* In the Montgomery case, we just turned R*H (representing H)
925
+ * into 1/(R*H), but we need R*(1/H) (representing 1/H);
926
+ * i.e. we need to multiply by the Montgomery factor twice. */
927
+ if (!group->meth->field_encode(group, tmp, tmp, ctx) ||
928
+ !group->meth->field_encode(group, tmp, tmp, ctx)) {
929
+ goto err;
930
+ }
931
+ }
932
+
933
+ for (i = num - 1; i > 0; --i) {
934
+ /* Loop invariant: tmp is the product of the inverses of
935
+ * points[0]->Z .. points[i]->Z (zero-valued inputs skipped). */
936
+ if (BN_is_zero(&points[i]->Z)) {
937
+ continue;
938
+ }
939
+
940
+ /* Set tmp_Z to the inverse of points[i]->Z (as product
941
+ * of Z inverses 0 .. i, Z values 0 .. i - 1). */
942
+ if (!group->meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx) ||
943
+ /* Update tmp to satisfy the loop invariant for i - 1. */
944
+ !group->meth->field_mul(group, tmp, tmp, &points[i]->Z, ctx) ||
945
+ /* Replace points[i]->Z by its inverse. */
946
+ !BN_copy(&points[i]->Z, tmp_Z)) {
947
+ goto err;
948
+ }
949
+ }
950
+
951
+ /* Replace points[0]->Z by its inverse. */
952
+ if (!BN_is_zero(&points[0]->Z) && !BN_copy(&points[0]->Z, tmp)) {
953
+ goto err;
954
+ }
955
+
956
+ /* Finally, fix up the X and Y coordinates for all points. */
957
+ for (i = 0; i < num; i++) {
958
+ EC_POINT *p = points[i];
959
+
960
+ if (!BN_is_zero(&p->Z)) {
961
+ /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1). */
962
+ if (!group->meth->field_sqr(group, tmp, &p->Z, ctx) ||
963
+ !group->meth->field_mul(group, &p->X, &p->X, tmp, ctx) ||
964
+ !group->meth->field_mul(group, tmp, tmp, &p->Z, ctx) ||
965
+ !group->meth->field_mul(group, &p->Y, &p->Y, tmp, ctx)) {
966
+ goto err;
967
+ }
968
+
969
+ if (group->meth->field_set_to_one != NULL) {
970
+ if (!group->meth->field_set_to_one(group, &p->Z, ctx)) {
971
+ goto err;
972
+ }
973
+ } else {
974
+ if (!BN_one(&p->Z)) {
975
+ goto err;
976
+ }
977
+ }
978
+ }
979
+ }
980
+
981
+ ret = 1;
982
+
983
+ err:
984
+ BN_CTX_end(ctx);
985
+ BN_CTX_free(new_ctx);
986
+ if (prod_Z != NULL) {
987
+ for (i = 0; i < num; i++) {
988
+ if (prod_Z[i] == NULL) {
989
+ break;
990
+ }
991
+ BN_clear_free(prod_Z[i]);
992
+ }
993
+ OPENSSL_free(prod_Z);
994
+ }
995
+
996
+ return ret;
997
+ }
998
+
999
+ int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
1000
+ const BIGNUM *b, BN_CTX *ctx) {
1001
+ return BN_mod_mul(r, a, b, &group->field, ctx);
1002
+ }
1003
+
1004
+ int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
1005
+ BN_CTX *ctx) {
1006
+ return BN_mod_sqr(r, a, &group->field, ctx);
1007
+ }