ring-native 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +3 -0
- data/README.md +22 -0
- data/Rakefile +1 -0
- data/ext/ring/extconf.rb +29 -0
- data/lib/ring/native.rb +8 -0
- data/lib/ring/native/version.rb +5 -0
- data/ring-native.gemspec +25 -0
- data/vendor/ring/BUILDING.md +40 -0
- data/vendor/ring/Cargo.toml +43 -0
- data/vendor/ring/LICENSE +185 -0
- data/vendor/ring/Makefile +35 -0
- data/vendor/ring/PORTING.md +163 -0
- data/vendor/ring/README.md +113 -0
- data/vendor/ring/STYLE.md +197 -0
- data/vendor/ring/appveyor.yml +27 -0
- data/vendor/ring/build.rs +108 -0
- data/vendor/ring/crypto/aes/aes.c +1142 -0
- data/vendor/ring/crypto/aes/aes_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/aes/aes_test.cc +93 -0
- data/vendor/ring/crypto/aes/asm/aes-586.pl +2368 -0
- data/vendor/ring/crypto/aes/asm/aes-armv4.pl +1249 -0
- data/vendor/ring/crypto/aes/asm/aes-x86_64.pl +2246 -0
- data/vendor/ring/crypto/aes/asm/aesni-x86.pl +1318 -0
- data/vendor/ring/crypto/aes/asm/aesni-x86_64.pl +2084 -0
- data/vendor/ring/crypto/aes/asm/aesv8-armx.pl +675 -0
- data/vendor/ring/crypto/aes/asm/bsaes-armv7.pl +1364 -0
- data/vendor/ring/crypto/aes/asm/bsaes-x86_64.pl +1565 -0
- data/vendor/ring/crypto/aes/asm/vpaes-x86.pl +841 -0
- data/vendor/ring/crypto/aes/asm/vpaes-x86_64.pl +1116 -0
- data/vendor/ring/crypto/aes/internal.h +87 -0
- data/vendor/ring/crypto/aes/mode_wrappers.c +61 -0
- data/vendor/ring/crypto/bn/add.c +394 -0
- data/vendor/ring/crypto/bn/asm/armv4-mont.pl +694 -0
- data/vendor/ring/crypto/bn/asm/armv8-mont.pl +1503 -0
- data/vendor/ring/crypto/bn/asm/bn-586.pl +774 -0
- data/vendor/ring/crypto/bn/asm/co-586.pl +287 -0
- data/vendor/ring/crypto/bn/asm/rsaz-avx2.pl +1882 -0
- data/vendor/ring/crypto/bn/asm/x86-mont.pl +592 -0
- data/vendor/ring/crypto/bn/asm/x86_64-gcc.c +599 -0
- data/vendor/ring/crypto/bn/asm/x86_64-mont.pl +1393 -0
- data/vendor/ring/crypto/bn/asm/x86_64-mont5.pl +3507 -0
- data/vendor/ring/crypto/bn/bn.c +352 -0
- data/vendor/ring/crypto/bn/bn_asn1.c +74 -0
- data/vendor/ring/crypto/bn/bn_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/bn/bn_test.cc +1696 -0
- data/vendor/ring/crypto/bn/cmp.c +200 -0
- data/vendor/ring/crypto/bn/convert.c +433 -0
- data/vendor/ring/crypto/bn/ctx.c +311 -0
- data/vendor/ring/crypto/bn/div.c +594 -0
- data/vendor/ring/crypto/bn/exponentiation.c +1335 -0
- data/vendor/ring/crypto/bn/gcd.c +711 -0
- data/vendor/ring/crypto/bn/generic.c +1019 -0
- data/vendor/ring/crypto/bn/internal.h +316 -0
- data/vendor/ring/crypto/bn/montgomery.c +516 -0
- data/vendor/ring/crypto/bn/mul.c +888 -0
- data/vendor/ring/crypto/bn/prime.c +829 -0
- data/vendor/ring/crypto/bn/random.c +334 -0
- data/vendor/ring/crypto/bn/rsaz_exp.c +262 -0
- data/vendor/ring/crypto/bn/rsaz_exp.h +53 -0
- data/vendor/ring/crypto/bn/shift.c +276 -0
- data/vendor/ring/crypto/bytestring/bytestring_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/bytestring/bytestring_test.cc +421 -0
- data/vendor/ring/crypto/bytestring/cbb.c +399 -0
- data/vendor/ring/crypto/bytestring/cbs.c +227 -0
- data/vendor/ring/crypto/bytestring/internal.h +46 -0
- data/vendor/ring/crypto/chacha/chacha_generic.c +140 -0
- data/vendor/ring/crypto/chacha/chacha_vec.c +323 -0
- data/vendor/ring/crypto/chacha/chacha_vec_arm.S +1447 -0
- data/vendor/ring/crypto/chacha/chacha_vec_arm_generate.go +153 -0
- data/vendor/ring/crypto/cipher/cipher_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/cipher/e_aes.c +390 -0
- data/vendor/ring/crypto/cipher/e_chacha20poly1305.c +208 -0
- data/vendor/ring/crypto/cipher/internal.h +173 -0
- data/vendor/ring/crypto/cipher/test/aes_128_gcm_tests.txt +543 -0
- data/vendor/ring/crypto/cipher/test/aes_128_key_wrap_tests.txt +9 -0
- data/vendor/ring/crypto/cipher/test/aes_256_gcm_tests.txt +475 -0
- data/vendor/ring/crypto/cipher/test/aes_256_key_wrap_tests.txt +23 -0
- data/vendor/ring/crypto/cipher/test/chacha20_poly1305_old_tests.txt +422 -0
- data/vendor/ring/crypto/cipher/test/chacha20_poly1305_tests.txt +484 -0
- data/vendor/ring/crypto/cipher/test/cipher_test.txt +100 -0
- data/vendor/ring/crypto/constant_time_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/constant_time_test.c +304 -0
- data/vendor/ring/crypto/cpu-arm-asm.S +32 -0
- data/vendor/ring/crypto/cpu-arm.c +199 -0
- data/vendor/ring/crypto/cpu-intel.c +261 -0
- data/vendor/ring/crypto/crypto.c +151 -0
- data/vendor/ring/crypto/curve25519/asm/x25519-arm.S +2118 -0
- data/vendor/ring/crypto/curve25519/curve25519.c +4888 -0
- data/vendor/ring/crypto/curve25519/x25519_test.cc +128 -0
- data/vendor/ring/crypto/digest/md32_common.h +181 -0
- data/vendor/ring/crypto/ec/asm/p256-x86_64-asm.pl +2725 -0
- data/vendor/ring/crypto/ec/ec.c +193 -0
- data/vendor/ring/crypto/ec/ec_curves.c +61 -0
- data/vendor/ring/crypto/ec/ec_key.c +228 -0
- data/vendor/ring/crypto/ec/ec_montgomery.c +114 -0
- data/vendor/ring/crypto/ec/example_mul.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/ec/internal.h +243 -0
- data/vendor/ring/crypto/ec/oct.c +253 -0
- data/vendor/ring/crypto/ec/p256-64.c +1794 -0
- data/vendor/ring/crypto/ec/p256-x86_64-table.h +9548 -0
- data/vendor/ring/crypto/ec/p256-x86_64.c +509 -0
- data/vendor/ring/crypto/ec/simple.c +1007 -0
- data/vendor/ring/crypto/ec/util-64.c +183 -0
- data/vendor/ring/crypto/ec/wnaf.c +508 -0
- data/vendor/ring/crypto/ecdh/ecdh.c +155 -0
- data/vendor/ring/crypto/ecdsa/ecdsa.c +304 -0
- data/vendor/ring/crypto/ecdsa/ecdsa_asn1.c +193 -0
- data/vendor/ring/crypto/ecdsa/ecdsa_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/ecdsa/ecdsa_test.cc +327 -0
- data/vendor/ring/crypto/header_removed.h +17 -0
- data/vendor/ring/crypto/internal.h +495 -0
- data/vendor/ring/crypto/libring.Windows.vcxproj +101 -0
- data/vendor/ring/crypto/mem.c +98 -0
- data/vendor/ring/crypto/modes/asm/aesni-gcm-x86_64.pl +1045 -0
- data/vendor/ring/crypto/modes/asm/ghash-armv4.pl +517 -0
- data/vendor/ring/crypto/modes/asm/ghash-x86.pl +1393 -0
- data/vendor/ring/crypto/modes/asm/ghash-x86_64.pl +1741 -0
- data/vendor/ring/crypto/modes/asm/ghashv8-armx.pl +422 -0
- data/vendor/ring/crypto/modes/ctr.c +226 -0
- data/vendor/ring/crypto/modes/gcm.c +1206 -0
- data/vendor/ring/crypto/modes/gcm_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/modes/gcm_test.c +348 -0
- data/vendor/ring/crypto/modes/internal.h +299 -0
- data/vendor/ring/crypto/perlasm/arm-xlate.pl +170 -0
- data/vendor/ring/crypto/perlasm/readme +100 -0
- data/vendor/ring/crypto/perlasm/x86_64-xlate.pl +1164 -0
- data/vendor/ring/crypto/perlasm/x86asm.pl +292 -0
- data/vendor/ring/crypto/perlasm/x86gas.pl +263 -0
- data/vendor/ring/crypto/perlasm/x86masm.pl +200 -0
- data/vendor/ring/crypto/perlasm/x86nasm.pl +187 -0
- data/vendor/ring/crypto/poly1305/poly1305.c +331 -0
- data/vendor/ring/crypto/poly1305/poly1305_arm.c +301 -0
- data/vendor/ring/crypto/poly1305/poly1305_arm_asm.S +2015 -0
- data/vendor/ring/crypto/poly1305/poly1305_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/poly1305/poly1305_test.cc +80 -0
- data/vendor/ring/crypto/poly1305/poly1305_test.txt +52 -0
- data/vendor/ring/crypto/poly1305/poly1305_vec.c +892 -0
- data/vendor/ring/crypto/rand/asm/rdrand-x86_64.pl +75 -0
- data/vendor/ring/crypto/rand/internal.h +32 -0
- data/vendor/ring/crypto/rand/rand.c +189 -0
- data/vendor/ring/crypto/rand/urandom.c +219 -0
- data/vendor/ring/crypto/rand/windows.c +56 -0
- data/vendor/ring/crypto/refcount_c11.c +66 -0
- data/vendor/ring/crypto/refcount_lock.c +53 -0
- data/vendor/ring/crypto/refcount_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/refcount_test.c +58 -0
- data/vendor/ring/crypto/rsa/blinding.c +462 -0
- data/vendor/ring/crypto/rsa/internal.h +108 -0
- data/vendor/ring/crypto/rsa/padding.c +300 -0
- data/vendor/ring/crypto/rsa/rsa.c +450 -0
- data/vendor/ring/crypto/rsa/rsa_asn1.c +261 -0
- data/vendor/ring/crypto/rsa/rsa_impl.c +944 -0
- data/vendor/ring/crypto/rsa/rsa_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/rsa/rsa_test.cc +437 -0
- data/vendor/ring/crypto/sha/asm/sha-armv8.pl +436 -0
- data/vendor/ring/crypto/sha/asm/sha-x86_64.pl +2390 -0
- data/vendor/ring/crypto/sha/asm/sha256-586.pl +1275 -0
- data/vendor/ring/crypto/sha/asm/sha256-armv4.pl +735 -0
- data/vendor/ring/crypto/sha/asm/sha256-armv8.pl +14 -0
- data/vendor/ring/crypto/sha/asm/sha256-x86_64.pl +14 -0
- data/vendor/ring/crypto/sha/asm/sha512-586.pl +911 -0
- data/vendor/ring/crypto/sha/asm/sha512-armv4.pl +666 -0
- data/vendor/ring/crypto/sha/asm/sha512-armv8.pl +14 -0
- data/vendor/ring/crypto/sha/asm/sha512-x86_64.pl +14 -0
- data/vendor/ring/crypto/sha/sha1.c +271 -0
- data/vendor/ring/crypto/sha/sha256.c +204 -0
- data/vendor/ring/crypto/sha/sha512.c +355 -0
- data/vendor/ring/crypto/test/file_test.cc +326 -0
- data/vendor/ring/crypto/test/file_test.h +181 -0
- data/vendor/ring/crypto/test/malloc.cc +150 -0
- data/vendor/ring/crypto/test/scoped_types.h +95 -0
- data/vendor/ring/crypto/test/test.Windows.vcxproj +35 -0
- data/vendor/ring/crypto/test/test_util.cc +46 -0
- data/vendor/ring/crypto/test/test_util.h +41 -0
- data/vendor/ring/crypto/thread_none.c +55 -0
- data/vendor/ring/crypto/thread_pthread.c +165 -0
- data/vendor/ring/crypto/thread_test.Windows.vcxproj +25 -0
- data/vendor/ring/crypto/thread_test.c +200 -0
- data/vendor/ring/crypto/thread_win.c +282 -0
- data/vendor/ring/examples/checkdigest.rs +103 -0
- data/vendor/ring/include/openssl/aes.h +121 -0
- data/vendor/ring/include/openssl/arm_arch.h +129 -0
- data/vendor/ring/include/openssl/base.h +156 -0
- data/vendor/ring/include/openssl/bn.h +794 -0
- data/vendor/ring/include/openssl/buffer.h +18 -0
- data/vendor/ring/include/openssl/bytestring.h +235 -0
- data/vendor/ring/include/openssl/chacha.h +37 -0
- data/vendor/ring/include/openssl/cmac.h +76 -0
- data/vendor/ring/include/openssl/cpu.h +184 -0
- data/vendor/ring/include/openssl/crypto.h +43 -0
- data/vendor/ring/include/openssl/curve25519.h +88 -0
- data/vendor/ring/include/openssl/ec.h +225 -0
- data/vendor/ring/include/openssl/ec_key.h +129 -0
- data/vendor/ring/include/openssl/ecdh.h +110 -0
- data/vendor/ring/include/openssl/ecdsa.h +156 -0
- data/vendor/ring/include/openssl/err.h +201 -0
- data/vendor/ring/include/openssl/mem.h +101 -0
- data/vendor/ring/include/openssl/obj_mac.h +71 -0
- data/vendor/ring/include/openssl/opensslfeatures.h +68 -0
- data/vendor/ring/include/openssl/opensslv.h +18 -0
- data/vendor/ring/include/openssl/ossl_typ.h +18 -0
- data/vendor/ring/include/openssl/poly1305.h +51 -0
- data/vendor/ring/include/openssl/rand.h +70 -0
- data/vendor/ring/include/openssl/rsa.h +399 -0
- data/vendor/ring/include/openssl/thread.h +133 -0
- data/vendor/ring/include/openssl/type_check.h +71 -0
- data/vendor/ring/mk/Common.props +63 -0
- data/vendor/ring/mk/Windows.props +42 -0
- data/vendor/ring/mk/WindowsTest.props +18 -0
- data/vendor/ring/mk/appveyor.bat +62 -0
- data/vendor/ring/mk/bottom_of_makefile.mk +54 -0
- data/vendor/ring/mk/ring.mk +266 -0
- data/vendor/ring/mk/top_of_makefile.mk +214 -0
- data/vendor/ring/mk/travis.sh +40 -0
- data/vendor/ring/mk/update-travis-yml.py +229 -0
- data/vendor/ring/ring.sln +153 -0
- data/vendor/ring/src/aead.rs +682 -0
- data/vendor/ring/src/agreement.rs +248 -0
- data/vendor/ring/src/c.rs +129 -0
- data/vendor/ring/src/constant_time.rs +37 -0
- data/vendor/ring/src/der.rs +96 -0
- data/vendor/ring/src/digest.rs +690 -0
- data/vendor/ring/src/digest_tests.txt +57 -0
- data/vendor/ring/src/ecc.rs +28 -0
- data/vendor/ring/src/ecc_build.rs +279 -0
- data/vendor/ring/src/ecc_curves.rs +117 -0
- data/vendor/ring/src/ed25519_tests.txt +2579 -0
- data/vendor/ring/src/exe_tests.rs +46 -0
- data/vendor/ring/src/ffi.rs +29 -0
- data/vendor/ring/src/file_test.rs +187 -0
- data/vendor/ring/src/hkdf.rs +153 -0
- data/vendor/ring/src/hkdf_tests.txt +59 -0
- data/vendor/ring/src/hmac.rs +414 -0
- data/vendor/ring/src/hmac_tests.txt +97 -0
- data/vendor/ring/src/input.rs +312 -0
- data/vendor/ring/src/lib.rs +41 -0
- data/vendor/ring/src/pbkdf2.rs +265 -0
- data/vendor/ring/src/pbkdf2_tests.txt +113 -0
- data/vendor/ring/src/polyfill.rs +57 -0
- data/vendor/ring/src/rand.rs +28 -0
- data/vendor/ring/src/signature.rs +314 -0
- data/vendor/ring/third-party/NIST/README.md +9 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA1LongMsg.rsp +263 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA1Monte.rsp +309 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA1ShortMsg.rsp +267 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA224LongMsg.rsp +263 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA224Monte.rsp +309 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA224ShortMsg.rsp +267 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA256LongMsg.rsp +263 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA256Monte.rsp +309 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA256ShortMsg.rsp +267 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA384LongMsg.rsp +519 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA384Monte.rsp +309 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA384ShortMsg.rsp +523 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA512LongMsg.rsp +519 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA512Monte.rsp +309 -0
- data/vendor/ring/third-party/NIST/SHAVS/SHA512ShortMsg.rsp +523 -0
- data/vendor/ring/third-party/NIST/sha256sums.txt +1 -0
- metadata +333 -0
@@ -0,0 +1,183 @@
|
|
1
|
+
/* Copyright (c) 2015, Google Inc.
|
2
|
+
*
|
3
|
+
* Permission to use, copy, modify, and/or distribute this software for any
|
4
|
+
* purpose with or without fee is hereby granted, provided that the above
|
5
|
+
* copyright notice and this permission notice appear in all copies.
|
6
|
+
*
|
7
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
8
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
9
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
10
|
+
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
11
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
12
|
+
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
13
|
+
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
14
|
+
|
15
|
+
#include <openssl/base.h>
|
16
|
+
|
17
|
+
|
18
|
+
#if defined(OPENSSL_64_BIT) && !defined(OPENSSL_WINDOWS)
|
19
|
+
|
20
|
+
#include <openssl/ec.h>
|
21
|
+
|
22
|
+
#include "internal.h"
|
23
|
+
|
24
|
+
/* Convert an array of points into affine coordinates. (If the point at
|
25
|
+
* infinity is found (Z = 0), it remains unchanged.) This function is
|
26
|
+
* essentially an equivalent to EC_POINTs_make_affine(), but works with the
|
27
|
+
* internal representation of points as used by ecp_nistp###.c rather than
|
28
|
+
* with (BIGNUM-based) EC_POINT data structures. point_array is the
|
29
|
+
* input/output buffer ('num' points in projective form, i.e. three
|
30
|
+
* coordinates each), based on an internal representation of field elements
|
31
|
+
* of size 'felem_size'. tmp_felems needs to point to a temporary array of
|
32
|
+
* 'num'+1 field elements for storage of intermediate values. */
|
33
|
+
void ec_GFp_nistp_points_make_affine_internal(
|
34
|
+
size_t num, void *point_array, size_t felem_size, void *tmp_felems,
|
35
|
+
void (*felem_one)(void *out), int (*felem_is_zero)(const void *in),
|
36
|
+
void (*felem_assign)(void *out, const void *in),
|
37
|
+
void (*felem_square)(void *out, const void *in),
|
38
|
+
void (*felem_mul)(void *out, const void *in1, const void *in2),
|
39
|
+
void (*felem_inv)(void *out, const void *in),
|
40
|
+
void (*felem_contract)(void *out, const void *in)) {
|
41
|
+
int i = 0;
|
42
|
+
|
43
|
+
#define tmp_felem(I) (&((char *)tmp_felems)[(I)*felem_size])
|
44
|
+
#define X(I) (&((char *)point_array)[3 * (I)*felem_size])
|
45
|
+
#define Y(I) (&((char *)point_array)[(3 * (I) + 1) * felem_size])
|
46
|
+
#define Z(I) (&((char *)point_array)[(3 * (I) + 2) * felem_size])
|
47
|
+
|
48
|
+
if (!felem_is_zero(Z(0))) {
|
49
|
+
felem_assign(tmp_felem(0), Z(0));
|
50
|
+
} else {
|
51
|
+
felem_one(tmp_felem(0));
|
52
|
+
}
|
53
|
+
|
54
|
+
for (i = 1; i < (int)num; i++) {
|
55
|
+
if (!felem_is_zero(Z(i))) {
|
56
|
+
felem_mul(tmp_felem(i), tmp_felem(i - 1), Z(i));
|
57
|
+
} else {
|
58
|
+
felem_assign(tmp_felem(i), tmp_felem(i - 1));
|
59
|
+
}
|
60
|
+
}
|
61
|
+
/* Now each tmp_felem(i) is the product of Z(0) .. Z(i), skipping any
|
62
|
+
* zero-valued factors: if Z(i) = 0, we essentially pretend that Z(i) = 1. */
|
63
|
+
|
64
|
+
felem_inv(tmp_felem(num - 1), tmp_felem(num - 1));
|
65
|
+
for (i = num - 1; i >= 0; i--) {
|
66
|
+
if (i > 0) {
|
67
|
+
/* tmp_felem(i-1) is the product of Z(0) .. Z(i-1), tmp_felem(i)
|
68
|
+
* is the inverse of the product of Z(0) .. Z(i). */
|
69
|
+
/* 1/Z(i) */
|
70
|
+
felem_mul(tmp_felem(num), tmp_felem(i - 1), tmp_felem(i));
|
71
|
+
} else {
|
72
|
+
felem_assign(tmp_felem(num), tmp_felem(0)); /* 1/Z(0) */
|
73
|
+
}
|
74
|
+
|
75
|
+
if (!felem_is_zero(Z(i))) {
|
76
|
+
if (i > 0) {
|
77
|
+
/* For next iteration, replace tmp_felem(i-1) by its inverse. */
|
78
|
+
felem_mul(tmp_felem(i - 1), tmp_felem(i), Z(i));
|
79
|
+
}
|
80
|
+
|
81
|
+
/* Convert point (X, Y, Z) into affine form (X/(Z^2), Y/(Z^3), 1). */
|
82
|
+
felem_square(Z(i), tmp_felem(num)); /* 1/(Z^2) */
|
83
|
+
felem_mul(X(i), X(i), Z(i)); /* X/(Z^2) */
|
84
|
+
felem_mul(Z(i), Z(i), tmp_felem(num)); /* 1/(Z^3) */
|
85
|
+
felem_mul(Y(i), Y(i), Z(i)); /* Y/(Z^3) */
|
86
|
+
felem_contract(X(i), X(i));
|
87
|
+
felem_contract(Y(i), Y(i));
|
88
|
+
felem_one(Z(i));
|
89
|
+
} else {
|
90
|
+
if (i > 0) {
|
91
|
+
/* For next iteration, replace tmp_felem(i-1) by its inverse. */
|
92
|
+
felem_assign(tmp_felem(i - 1), tmp_felem(i));
|
93
|
+
}
|
94
|
+
}
|
95
|
+
}
|
96
|
+
}
|
97
|
+
|
98
|
+
/* This function looks at 5+1 scalar bits (5 current, 1 adjacent less
|
99
|
+
* significant bit), and recodes them into a signed digit for use in fast point
|
100
|
+
* multiplication: the use of signed rather than unsigned digits means that
|
101
|
+
* fewer points need to be precomputed, given that point inversion is easy (a
|
102
|
+
* precomputed point dP makes -dP available as well).
|
103
|
+
*
|
104
|
+
* BACKGROUND:
|
105
|
+
*
|
106
|
+
* Signed digits for multiplication were introduced by Booth ("A signed binary
|
107
|
+
* multiplication technique", Quart. Journ. Mech. and Applied Math., vol. IV,
|
108
|
+
* pt. 2 (1951), pp. 236-240), in that case for multiplication of integers.
|
109
|
+
* Booth's original encoding did not generally improve the density of nonzero
|
110
|
+
* digits over the binary representation, and was merely meant to simplify the
|
111
|
+
* handling of signed factors given in two's complement; but it has since been
|
112
|
+
* shown to be the basis of various signed-digit representations that do have
|
113
|
+
* further advantages, including the wNAF, using the following general
|
114
|
+
* approach:
|
115
|
+
*
|
116
|
+
* (1) Given a binary representation
|
117
|
+
*
|
118
|
+
* b_k ... b_2 b_1 b_0,
|
119
|
+
*
|
120
|
+
* of a nonnegative integer (b_k in {0, 1}), rewrite it in digits 0, 1, -1
|
121
|
+
* by using bit-wise subtraction as follows:
|
122
|
+
*
|
123
|
+
* b_k b_(k-1) ... b_2 b_1 b_0
|
124
|
+
* - b_k ... b_3 b_2 b_1 b_0
|
125
|
+
* -------------------------------------
|
126
|
+
* s_k b_(k-1) ... s_3 s_2 s_1 s_0
|
127
|
+
*
|
128
|
+
* A left-shift followed by subtraction of the original value yields a new
|
129
|
+
* representation of the same value, using signed bits s_i = b_(i+1) - b_i.
|
130
|
+
* This representation from Booth's paper has since appeared in the
|
131
|
+
* literature under a variety of different names including "reversed binary
|
132
|
+
* form", "alternating greedy expansion", "mutual opposite form", and
|
133
|
+
* "sign-alternating {+-1}-representation".
|
134
|
+
*
|
135
|
+
* An interesting property is that among the nonzero bits, values 1 and -1
|
136
|
+
* strictly alternate.
|
137
|
+
*
|
138
|
+
* (2) Various window schemes can be applied to the Booth representation of
|
139
|
+
* integers: for example, right-to-left sliding windows yield the wNAF
|
140
|
+
* (a signed-digit encoding independently discovered by various researchers
|
141
|
+
* in the 1990s), and left-to-right sliding windows yield a left-to-right
|
142
|
+
* equivalent of the wNAF (independently discovered by various researchers
|
143
|
+
* around 2004).
|
144
|
+
*
|
145
|
+
* To prevent leaking information through side channels in point multiplication,
|
146
|
+
* we need to recode the given integer into a regular pattern: sliding windows
|
147
|
+
* as in wNAFs won't do, we need their fixed-window equivalent -- which is a few
|
148
|
+
* decades older: we'll be using the so-called "modified Booth encoding" due to
|
149
|
+
* MacSorley ("High-speed arithmetic in binary computers", Proc. IRE, vol. 49
|
150
|
+
* (1961), pp. 67-91), in a radix-2^5 setting. That is, we always combine five
|
151
|
+
* signed bits into a signed digit:
|
152
|
+
*
|
153
|
+
* s_(4j + 4) s_(4j + 3) s_(4j + 2) s_(4j + 1) s_(4j)
|
154
|
+
*
|
155
|
+
* The sign-alternating property implies that the resulting digit values are
|
156
|
+
* integers from -16 to 16.
|
157
|
+
*
|
158
|
+
* Of course, we don't actually need to compute the signed digits s_i as an
|
159
|
+
* intermediate step (that's just a nice way to see how this scheme relates
|
160
|
+
* to the wNAF): a direct computation obtains the recoded digit from the
|
161
|
+
* six bits b_(4j + 4) ... b_(4j - 1).
|
162
|
+
*
|
163
|
+
* This function takes those five bits as an integer (0 .. 63), writing the
|
164
|
+
* recoded digit to *sign (0 for positive, 1 for negative) and *digit (absolute
|
165
|
+
* value, in the range 0 .. 8). Note that this integer essentially provides the
|
166
|
+
* input bits "shifted to the left" by one position: for example, the input to
|
167
|
+
* compute the least significant recoded digit, given that there's no bit b_-1,
|
168
|
+
* has to be b_4 b_3 b_2 b_1 b_0 0. */
|
169
|
+
void ec_GFp_nistp_recode_scalar_bits(uint8_t *sign, uint8_t *digit,
|
170
|
+
uint8_t in) {
|
171
|
+
uint8_t s, d;
|
172
|
+
|
173
|
+
s = ~((in >> 5) - 1); /* sets all bits to MSB(in), 'in' seen as
|
174
|
+
* 6-bit value */
|
175
|
+
d = (1 << 6) - in - 1;
|
176
|
+
d = (d & s) | (in & ~s);
|
177
|
+
d = (d >> 1) + (d & 1);
|
178
|
+
|
179
|
+
*sign = s & 1;
|
180
|
+
*digit = d;
|
181
|
+
}
|
182
|
+
|
183
|
+
#endif /* 64_BIT && !WINDOWS */
|
@@ -0,0 +1,508 @@
|
|
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 <string.h>
|
71
|
+
|
72
|
+
#include <openssl/bn.h>
|
73
|
+
#include <openssl/err.h>
|
74
|
+
#include <openssl/mem.h>
|
75
|
+
#include <openssl/thread.h>
|
76
|
+
|
77
|
+
#include "internal.h"
|
78
|
+
#include "../internal.h"
|
79
|
+
|
80
|
+
|
81
|
+
/* This file implements the wNAF-based interleaving multi-exponentation method
|
82
|
+
* (<URL:http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#multiexp>);
|
83
|
+
* */
|
84
|
+
|
85
|
+
/* Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
|
86
|
+
* This is an array r[] of values that are either zero or odd with an
|
87
|
+
* absolute value less than 2^w satisfying
|
88
|
+
* scalar = \sum_j r[j]*2^j
|
89
|
+
* where at most one of any w+1 consecutive digits is non-zero
|
90
|
+
* with the exception that the most significant digit may be only
|
91
|
+
* w-1 zeros away from that next non-zero digit.
|
92
|
+
*/
|
93
|
+
static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len) {
|
94
|
+
int window_val;
|
95
|
+
int ok = 0;
|
96
|
+
signed char *r = NULL;
|
97
|
+
int sign = 1;
|
98
|
+
int bit, next_bit, mask;
|
99
|
+
size_t len = 0, j;
|
100
|
+
|
101
|
+
if (BN_is_zero(scalar)) {
|
102
|
+
r = OPENSSL_malloc(1);
|
103
|
+
if (!r) {
|
104
|
+
OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
|
105
|
+
goto err;
|
106
|
+
}
|
107
|
+
r[0] = 0;
|
108
|
+
*ret_len = 1;
|
109
|
+
return r;
|
110
|
+
}
|
111
|
+
|
112
|
+
if (w <= 0 || w > 7) /* 'signed char' can represent integers with absolute
|
113
|
+
values less than 2^7 */
|
114
|
+
{
|
115
|
+
OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
|
116
|
+
goto err;
|
117
|
+
}
|
118
|
+
bit = 1 << w; /* at most 128 */
|
119
|
+
next_bit = bit << 1; /* at most 256 */
|
120
|
+
mask = next_bit - 1; /* at most 255 */
|
121
|
+
|
122
|
+
if (BN_is_negative(scalar)) {
|
123
|
+
sign = -1;
|
124
|
+
}
|
125
|
+
|
126
|
+
if (scalar->d == NULL || scalar->top == 0) {
|
127
|
+
OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
|
128
|
+
goto err;
|
129
|
+
}
|
130
|
+
|
131
|
+
len = BN_num_bits(scalar);
|
132
|
+
r = OPENSSL_malloc(
|
133
|
+
len +
|
134
|
+
1); /* modified wNAF may be one digit longer than binary representation
|
135
|
+
* (*ret_len will be set to the actual length, i.e. at most
|
136
|
+
* BN_num_bits(scalar) + 1) */
|
137
|
+
if (r == NULL) {
|
138
|
+
OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
|
139
|
+
goto err;
|
140
|
+
}
|
141
|
+
window_val = scalar->d[0] & mask;
|
142
|
+
j = 0;
|
143
|
+
while ((window_val != 0) ||
|
144
|
+
(j + w + 1 < len)) /* if j+w+1 >= len, window_val will not increase */
|
145
|
+
{
|
146
|
+
int digit = 0;
|
147
|
+
|
148
|
+
/* 0 <= window_val <= 2^(w+1) */
|
149
|
+
|
150
|
+
if (window_val & 1) {
|
151
|
+
/* 0 < window_val < 2^(w+1) */
|
152
|
+
|
153
|
+
if (window_val & bit) {
|
154
|
+
digit = window_val - next_bit; /* -2^w < digit < 0 */
|
155
|
+
|
156
|
+
#if 1 /* modified wNAF */
|
157
|
+
if (j + w + 1 >= len) {
|
158
|
+
/* special case for generating modified wNAFs:
|
159
|
+
* no new bits will be added into window_val,
|
160
|
+
* so using a positive digit here will decrease
|
161
|
+
* the total length of the representation */
|
162
|
+
|
163
|
+
digit = window_val & (mask >> 1); /* 0 < digit < 2^w */
|
164
|
+
}
|
165
|
+
#endif
|
166
|
+
} else {
|
167
|
+
digit = window_val; /* 0 < digit < 2^w */
|
168
|
+
}
|
169
|
+
|
170
|
+
if (digit <= -bit || digit >= bit || !(digit & 1)) {
|
171
|
+
OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
|
172
|
+
goto err;
|
173
|
+
}
|
174
|
+
|
175
|
+
window_val -= digit;
|
176
|
+
|
177
|
+
/* now window_val is 0 or 2^(w+1) in standard wNAF generation;
|
178
|
+
* for modified window NAFs, it may also be 2^w
|
179
|
+
*/
|
180
|
+
if (window_val != 0 && window_val != next_bit && window_val != bit) {
|
181
|
+
OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
|
182
|
+
goto err;
|
183
|
+
}
|
184
|
+
}
|
185
|
+
|
186
|
+
r[j++] = sign * digit;
|
187
|
+
|
188
|
+
window_val >>= 1;
|
189
|
+
window_val += bit * BN_is_bit_set(scalar, j + w);
|
190
|
+
|
191
|
+
if (window_val > next_bit) {
|
192
|
+
OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
|
193
|
+
goto err;
|
194
|
+
}
|
195
|
+
}
|
196
|
+
|
197
|
+
if (j > len + 1) {
|
198
|
+
OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
|
199
|
+
goto err;
|
200
|
+
}
|
201
|
+
len = j;
|
202
|
+
ok = 1;
|
203
|
+
|
204
|
+
err:
|
205
|
+
if (!ok) {
|
206
|
+
OPENSSL_free(r);
|
207
|
+
r = NULL;
|
208
|
+
}
|
209
|
+
if (ok) {
|
210
|
+
*ret_len = len;
|
211
|
+
}
|
212
|
+
return r;
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
/* TODO: table should be optimised for the wNAF-based implementation,
|
217
|
+
* sometimes smaller windows will give better performance
|
218
|
+
* (thus the boundaries should be increased)
|
219
|
+
*/
|
220
|
+
#define EC_window_bits_for_scalar_size(b) \
|
221
|
+
((size_t)((b) >= 2000 ? 6 : (b) >= 800 ? 5 : (b) >= 300 \
|
222
|
+
? 4 \
|
223
|
+
: (b) >= 70 ? 3 : (b) >= 20 \
|
224
|
+
? 2 \
|
225
|
+
: 1))
|
226
|
+
|
227
|
+
|
228
|
+
int ec_wNAF_mul_private(const EC_GROUP *group, EC_POINT *r,
|
229
|
+
const BIGNUM *g_scalar, const EC_POINT *p,
|
230
|
+
const BIGNUM *p_scalar, BN_CTX *ctx) {
|
231
|
+
BN_CTX *new_ctx = NULL;;
|
232
|
+
int ret = 0;
|
233
|
+
|
234
|
+
/* We do not want timing information to leak the length of the scalars so we
|
235
|
+
* use equivalent scalars of fixed bit-length. */
|
236
|
+
|
237
|
+
if (ctx == NULL) {
|
238
|
+
new_ctx = BN_CTX_new();
|
239
|
+
if (new_ctx == NULL) {
|
240
|
+
return 0;
|
241
|
+
}
|
242
|
+
ctx = new_ctx;
|
243
|
+
}
|
244
|
+
BN_CTX_start(ctx);
|
245
|
+
|
246
|
+
BIGNUM *g_scalar_new = NULL;
|
247
|
+
if (g_scalar) {
|
248
|
+
g_scalar_new = BN_CTX_get(ctx);
|
249
|
+
if (g_scalar_new == NULL ||
|
250
|
+
!BN_add(g_scalar_new, g_scalar, &group->order)) {
|
251
|
+
goto err;
|
252
|
+
}
|
253
|
+
if (BN_num_bits(g_scalar_new) <= BN_num_bits(&group->order)) {
|
254
|
+
if (!BN_add(g_scalar_new, g_scalar_new, &group->order)) {
|
255
|
+
goto err;
|
256
|
+
}
|
257
|
+
}
|
258
|
+
}
|
259
|
+
|
260
|
+
BIGNUM *p_scalar_new = NULL;
|
261
|
+
if (p_scalar) {
|
262
|
+
p_scalar_new = BN_CTX_get(ctx);
|
263
|
+
if (!p_scalar_new ||
|
264
|
+
!BN_add(p_scalar_new, p_scalar, &group->order)) {
|
265
|
+
goto err;
|
266
|
+
}
|
267
|
+
if (BN_num_bits(p_scalar_new) <= BN_num_bits(&group->order)) {
|
268
|
+
if (!BN_add(p_scalar_new, p_scalar_new, &group->order)) {
|
269
|
+
goto err;
|
270
|
+
}
|
271
|
+
}
|
272
|
+
}
|
273
|
+
|
274
|
+
ret = ec_wNAF_mul_public(group, r, g_scalar_new, p, p_scalar_new, ctx);
|
275
|
+
|
276
|
+
err:
|
277
|
+
BN_CTX_end(ctx);
|
278
|
+
BN_CTX_free(new_ctx);
|
279
|
+
return ret;
|
280
|
+
}
|
281
|
+
|
282
|
+
int ec_wNAF_mul_public(const EC_GROUP *group, EC_POINT *r,
|
283
|
+
const BIGNUM *g_scalar, const EC_POINT *p,
|
284
|
+
const BIGNUM *p_scalar, BN_CTX *ctx) {
|
285
|
+
BN_CTX *new_ctx = NULL;
|
286
|
+
const EC_POINT *generator = NULL;
|
287
|
+
EC_POINT *tmp = NULL;
|
288
|
+
size_t total_num;
|
289
|
+
size_t i, j;
|
290
|
+
int k;
|
291
|
+
int r_is_inverted = 0;
|
292
|
+
int r_is_at_infinity = 1;
|
293
|
+
size_t *wsize = NULL; /* individual window sizes */
|
294
|
+
signed char **wNAF = NULL; /* individual wNAFs */
|
295
|
+
size_t *wNAF_len = NULL;
|
296
|
+
size_t max_len = 0;
|
297
|
+
size_t num_val;
|
298
|
+
EC_POINT **val = NULL; /* precomputation */
|
299
|
+
EC_POINT **v;
|
300
|
+
EC_POINT ***val_sub = NULL; /* pointers to sub-arrays of 'val' */
|
301
|
+
int ret = 0;
|
302
|
+
|
303
|
+
if (ctx == NULL) {
|
304
|
+
ctx = new_ctx = BN_CTX_new();
|
305
|
+
if (ctx == NULL) {
|
306
|
+
goto err;
|
307
|
+
}
|
308
|
+
}
|
309
|
+
|
310
|
+
/* TODO: This function used to take |points| and |scalars| as arrays of
|
311
|
+
* |num| elements. The code below should be simplified to work in terms of |p|
|
312
|
+
* and |p_scalar|. */
|
313
|
+
size_t num = p != NULL ? 1 : 0;
|
314
|
+
const EC_POINT **points = p != NULL ? &p : NULL;
|
315
|
+
const BIGNUM **scalars = p != NULL ? &p_scalar : NULL;
|
316
|
+
|
317
|
+
total_num = num;
|
318
|
+
|
319
|
+
if (g_scalar != NULL) {
|
320
|
+
generator = EC_GROUP_get0_generator(group);
|
321
|
+
if (generator == NULL) {
|
322
|
+
OPENSSL_PUT_ERROR(EC, EC_R_UNDEFINED_GENERATOR);
|
323
|
+
goto err;
|
324
|
+
}
|
325
|
+
|
326
|
+
++total_num; /* treat 'g_scalar' like 'num'-th element of 'scalars' */
|
327
|
+
}
|
328
|
+
|
329
|
+
|
330
|
+
wsize = OPENSSL_malloc(total_num * sizeof wsize[0]);
|
331
|
+
wNAF_len = OPENSSL_malloc(total_num * sizeof wNAF_len[0]);
|
332
|
+
wNAF = OPENSSL_malloc((total_num + 1) *
|
333
|
+
sizeof wNAF[0]); /* includes space for pivot */
|
334
|
+
val_sub = OPENSSL_malloc(total_num * sizeof val_sub[0]);
|
335
|
+
|
336
|
+
/* Ensure wNAF is initialised in case we end up going to err. */
|
337
|
+
if (wNAF) {
|
338
|
+
wNAF[0] = NULL; /* preliminary pivot */
|
339
|
+
}
|
340
|
+
|
341
|
+
if (!wsize || !wNAF_len || !wNAF || !val_sub) {
|
342
|
+
OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
|
343
|
+
goto err;
|
344
|
+
}
|
345
|
+
|
346
|
+
/* num_val will be the total number of temporarily precomputed points */
|
347
|
+
num_val = 0;
|
348
|
+
|
349
|
+
for (i = 0; i < total_num; i++) {
|
350
|
+
size_t bits;
|
351
|
+
|
352
|
+
bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(g_scalar);
|
353
|
+
wsize[i] = EC_window_bits_for_scalar_size(bits);
|
354
|
+
num_val += (size_t)1 << (wsize[i] - 1);
|
355
|
+
wNAF[i + 1] = NULL; /* make sure we always have a pivot */
|
356
|
+
wNAF[i] =
|
357
|
+
compute_wNAF((i < num ? scalars[i] : g_scalar), wsize[i], &wNAF_len[i]);
|
358
|
+
if (wNAF[i] == NULL) {
|
359
|
+
goto err;
|
360
|
+
}
|
361
|
+
if (wNAF_len[i] > max_len) {
|
362
|
+
max_len = wNAF_len[i];
|
363
|
+
}
|
364
|
+
}
|
365
|
+
|
366
|
+
/* All points we precompute now go into a single array 'val'. 'val_sub[i]' is
|
367
|
+
* a pointer to the subarray for the i-th point. */
|
368
|
+
val = OPENSSL_malloc((num_val + 1) * sizeof val[0]);
|
369
|
+
if (val == NULL) {
|
370
|
+
OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
|
371
|
+
goto err;
|
372
|
+
}
|
373
|
+
val[num_val] = NULL; /* pivot element */
|
374
|
+
|
375
|
+
/* allocate points for precomputation */
|
376
|
+
v = val;
|
377
|
+
for (i = 0; i < total_num; i++) {
|
378
|
+
val_sub[i] = v;
|
379
|
+
for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {
|
380
|
+
*v = EC_POINT_new(group);
|
381
|
+
if (*v == NULL) {
|
382
|
+
goto err;
|
383
|
+
}
|
384
|
+
v++;
|
385
|
+
}
|
386
|
+
}
|
387
|
+
if (!(v == val + num_val)) {
|
388
|
+
OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
|
389
|
+
goto err;
|
390
|
+
}
|
391
|
+
|
392
|
+
if (!(tmp = EC_POINT_new(group))) {
|
393
|
+
goto err;
|
394
|
+
}
|
395
|
+
|
396
|
+
/* prepare precomputed values:
|
397
|
+
* val_sub[i][0] := points[i]
|
398
|
+
* val_sub[i][1] := 3 * points[i]
|
399
|
+
* val_sub[i][2] := 5 * points[i]
|
400
|
+
* ...
|
401
|
+
*/
|
402
|
+
for (i = 0; i < total_num; i++) {
|
403
|
+
if (i < num) {
|
404
|
+
if (!ec_GFp_simple_point_copy(val_sub[i][0], points[i])) {
|
405
|
+
goto err;
|
406
|
+
}
|
407
|
+
} else if (!ec_GFp_simple_point_copy(val_sub[i][0], generator)) {
|
408
|
+
goto err;
|
409
|
+
}
|
410
|
+
|
411
|
+
if (wsize[i] > 1) {
|
412
|
+
if (!ec_GFp_simple_dbl(group, tmp, val_sub[i][0], ctx)) {
|
413
|
+
goto err;
|
414
|
+
}
|
415
|
+
for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {
|
416
|
+
if (!ec_GFp_simple_add(group, val_sub[i][j], val_sub[i][j - 1], tmp,
|
417
|
+
ctx)) {
|
418
|
+
goto err;
|
419
|
+
}
|
420
|
+
}
|
421
|
+
}
|
422
|
+
}
|
423
|
+
|
424
|
+
#if 1 /* optional; EC_window_bits_for_scalar_size assumes we do this step */
|
425
|
+
if (!ec_GFp_simple_points_make_affine(group, num_val, val, ctx)) {
|
426
|
+
goto err;
|
427
|
+
}
|
428
|
+
#endif
|
429
|
+
|
430
|
+
r_is_at_infinity = 1;
|
431
|
+
|
432
|
+
for (k = max_len - 1; k >= 0; k--) {
|
433
|
+
if (!r_is_at_infinity && !ec_GFp_simple_dbl(group, r, r, ctx)) {
|
434
|
+
goto err;
|
435
|
+
}
|
436
|
+
|
437
|
+
for (i = 0; i < total_num; i++) {
|
438
|
+
if (wNAF_len[i] > (size_t)k) {
|
439
|
+
int digit = wNAF[i][k];
|
440
|
+
int is_neg;
|
441
|
+
|
442
|
+
if (digit) {
|
443
|
+
is_neg = digit < 0;
|
444
|
+
|
445
|
+
if (is_neg) {
|
446
|
+
digit = -digit;
|
447
|
+
}
|
448
|
+
|
449
|
+
if (is_neg != r_is_inverted) {
|
450
|
+
if (!r_is_at_infinity && !ec_GFp_simple_invert(group, r, ctx)) {
|
451
|
+
goto err;
|
452
|
+
}
|
453
|
+
r_is_inverted = !r_is_inverted;
|
454
|
+
}
|
455
|
+
|
456
|
+
/* digit > 0 */
|
457
|
+
|
458
|
+
if (r_is_at_infinity) {
|
459
|
+
if (!ec_GFp_simple_point_copy(r, val_sub[i][digit >> 1])) {
|
460
|
+
goto err;
|
461
|
+
}
|
462
|
+
r_is_at_infinity = 0;
|
463
|
+
} else {
|
464
|
+
if (!ec_GFp_simple_add(group, r, r, val_sub[i][digit >> 1], ctx)) {
|
465
|
+
goto err;
|
466
|
+
}
|
467
|
+
}
|
468
|
+
}
|
469
|
+
}
|
470
|
+
}
|
471
|
+
}
|
472
|
+
|
473
|
+
if (r_is_at_infinity) {
|
474
|
+
if (!EC_POINT_set_to_infinity(group, r)) {
|
475
|
+
goto err;
|
476
|
+
}
|
477
|
+
} else if (r_is_inverted && !ec_GFp_simple_invert(group, r, ctx)) {
|
478
|
+
goto err;
|
479
|
+
}
|
480
|
+
|
481
|
+
ret = 1;
|
482
|
+
|
483
|
+
err:
|
484
|
+
BN_CTX_free(new_ctx);
|
485
|
+
EC_POINT_free(tmp);
|
486
|
+
OPENSSL_free(wsize);
|
487
|
+
OPENSSL_free(wNAF_len);
|
488
|
+
if (wNAF != NULL) {
|
489
|
+
signed char **w;
|
490
|
+
|
491
|
+
for (w = wNAF; *w != NULL; w++) {
|
492
|
+
OPENSSL_free(*w);
|
493
|
+
}
|
494
|
+
|
495
|
+
OPENSSL_free(wNAF);
|
496
|
+
}
|
497
|
+
if (val != NULL) {
|
498
|
+
for (v = val; *v != NULL; v++) {
|
499
|
+
ec_GFp_simple_point_clear_finish(*v);
|
500
|
+
OPENSSL_cleanse(*v, sizeof **v);
|
501
|
+
OPENSSL_free(*v);
|
502
|
+
}
|
503
|
+
|
504
|
+
OPENSSL_free(val);
|
505
|
+
}
|
506
|
+
OPENSSL_free(val_sub);
|
507
|
+
return ret;
|
508
|
+
}
|