simple.c
Go to the documentation of this file.
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 
76 #include "internal.h"
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 here specifically assume that if a non-trivial representation is
88 // used, it is a Montgomery representation (i.e. 'encoding' means multiplying
89 // by some factor R).
90 
92  BN_init(&group->field);
93  group->a_is_minus3 = 0;
94  return 1;
95 }
96 
98  BN_free(&group->field);
99 }
100 
102  const BIGNUM *a, const BIGNUM *b,
103  BN_CTX *ctx) {
104  // p must be a prime > 3
105  if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {
107  return 0;
108  }
109 
110  int ret = 0;
111  BN_CTX_start(ctx);
112  BIGNUM *tmp = BN_CTX_get(ctx);
113  if (tmp == NULL) {
114  goto err;
115  }
116 
117  // group->field
118  if (!BN_copy(&group->field, p)) {
119  goto err;
120  }
121  BN_set_negative(&group->field, 0);
122  // Store the field in minimal form, so it can be used with |BN_ULONG| arrays.
123  bn_set_minimal_width(&group->field);
124 
125  if (!ec_bignum_to_felem(group, &group->a, a) ||
126  !ec_bignum_to_felem(group, &group->b, b) ||
128  goto err;
129  }
130 
131  // group->a_is_minus3
132  if (!BN_copy(tmp, a) ||
133  !BN_add_word(tmp, 3)) {
134  goto err;
135  }
136  group->a_is_minus3 = (0 == BN_cmp(tmp, &group->field));
137 
138  ret = 1;
139 
140 err:
141  BN_CTX_end(ctx);
142  return ret;
143 }
144 
146  BIGNUM *b) {
147  if ((p != NULL && !BN_copy(p, &group->field)) ||
148  (a != NULL && !ec_felem_to_bignum(group, a, &group->a)) ||
149  (b != NULL && !ec_felem_to_bignum(group, b, &group->b))) {
150  return 0;
151  }
152  return 1;
153 }
154 
156  OPENSSL_memset(&point->X, 0, sizeof(EC_FELEM));
157  OPENSSL_memset(&point->Y, 0, sizeof(EC_FELEM));
158  OPENSSL_memset(&point->Z, 0, sizeof(EC_FELEM));
159 }
160 
162  OPENSSL_memcpy(&dest->X, &src->X, sizeof(EC_FELEM));
163  OPENSSL_memcpy(&dest->Y, &src->Y, sizeof(EC_FELEM));
164  OPENSSL_memcpy(&dest->Z, &src->Z, sizeof(EC_FELEM));
165 }
166 
168  EC_RAW_POINT *point) {
169  // Although it is strictly only necessary to zero Z, we zero the entire point
170  // in case |point| was stack-allocated and yet to be initialized.
172 }
173 
175  ec_felem_neg(group, &point->Y, &point->Y);
176 }
177 
179  const EC_RAW_POINT *point) {
180  return ec_felem_non_zero_mask(group, &point->Z) == 0;
181 }
182 
184  const EC_RAW_POINT *point) {
185  // We have a curve defined by a Weierstrass equation
186  // y^2 = x^3 + a*x + b.
187  // The point to consider is given in Jacobian projective coordinates
188  // where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
189  // Substituting this and multiplying by Z^6 transforms the above equation
190  // into
191  // Y^2 = X^3 + a*X*Z^4 + b*Z^6.
192  // To test this, we add up the right-hand side in 'rh'.
193  //
194  // This function may be used when double-checking the secret result of a point
195  // multiplication, so we proceed in constant-time.
196 
197  void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
198  const EC_FELEM *b) = group->meth->felem_mul;
199  void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
200  group->meth->felem_sqr;
201 
202  // rh := X^2
203  EC_FELEM rh;
204  felem_sqr(group, &rh, &point->X);
205 
206  EC_FELEM tmp, Z4, Z6;
207  felem_sqr(group, &tmp, &point->Z);
208  felem_sqr(group, &Z4, &tmp);
209  felem_mul(group, &Z6, &Z4, &tmp);
210 
211  // rh := rh + a*Z^4
212  if (group->a_is_minus3) {
213  ec_felem_add(group, &tmp, &Z4, &Z4);
214  ec_felem_add(group, &tmp, &tmp, &Z4);
215  ec_felem_sub(group, &rh, &rh, &tmp);
216  } else {
217  felem_mul(group, &tmp, &Z4, &group->a);
218  ec_felem_add(group, &rh, &rh, &tmp);
219  }
220 
221  // rh := (rh + a*Z^4)*X
222  felem_mul(group, &rh, &rh, &point->X);
223 
224  // rh := rh + b*Z^6
225  felem_mul(group, &tmp, &group->b, &Z6);
226  ec_felem_add(group, &rh, &rh, &tmp);
227 
228  // 'lh' := Y^2
229  felem_sqr(group, &tmp, &point->Y);
230 
231  ec_felem_sub(group, &tmp, &tmp, &rh);
232  BN_ULONG not_equal = ec_felem_non_zero_mask(group, &tmp);
233 
234  // If Z = 0, the point is infinity, which is always on the curve.
235  BN_ULONG not_infinity = ec_felem_non_zero_mask(group, &point->Z);
236 
237  return 1 & ~(not_infinity & not_equal);
238 }
239 
241  const EC_RAW_POINT *b) {
242  // This function is implemented in constant-time for two reasons. First,
243  // although EC points are usually public, their Jacobian Z coordinates may be
244  // secret, or at least are not obviously public. Second, more complex
245  // protocols will sometimes manipulate secret points.
246  //
247  // This does mean that we pay a 6M+2S Jacobian comparison when comparing two
248  // publicly affine points costs no field operations at all. If needed, we can
249  // restore this optimization by keeping better track of affine vs. Jacobian
250  // forms. See https://crbug.com/boringssl/326.
251 
252  // If neither |a| or |b| is infinity, we have to decide whether
253  // (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
254  // or equivalently, whether
255  // (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
256 
257  void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
258  const EC_FELEM *b) = group->meth->felem_mul;
259  void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
260  group->meth->felem_sqr;
261 
262  EC_FELEM tmp1, tmp2, Za23, Zb23;
263  felem_sqr(group, &Zb23, &b->Z); // Zb23 = Z_b^2
264  felem_mul(group, &tmp1, &a->X, &Zb23); // tmp1 = X_a * Z_b^2
265  felem_sqr(group, &Za23, &a->Z); // Za23 = Z_a^2
266  felem_mul(group, &tmp2, &b->X, &Za23); // tmp2 = X_b * Z_a^2
267  ec_felem_sub(group, &tmp1, &tmp1, &tmp2);
268  const BN_ULONG x_not_equal = ec_felem_non_zero_mask(group, &tmp1);
269 
270  felem_mul(group, &Zb23, &Zb23, &b->Z); // Zb23 = Z_b^3
271  felem_mul(group, &tmp1, &a->Y, &Zb23); // tmp1 = Y_a * Z_b^3
272  felem_mul(group, &Za23, &Za23, &a->Z); // Za23 = Z_a^3
273  felem_mul(group, &tmp2, &b->Y, &Za23); // tmp2 = Y_b * Z_a^3
274  ec_felem_sub(group, &tmp1, &tmp1, &tmp2);
275  const BN_ULONG y_not_equal = ec_felem_non_zero_mask(group, &tmp1);
276  const BN_ULONG x_and_y_equal = ~(x_not_equal | y_not_equal);
277 
278  const BN_ULONG a_not_infinity = ec_felem_non_zero_mask(group, &a->Z);
279  const BN_ULONG b_not_infinity = ec_felem_non_zero_mask(group, &b->Z);
280  const BN_ULONG a_and_b_infinity = ~(a_not_infinity | b_not_infinity);
281 
282  const BN_ULONG equal =
283  a_and_b_infinity | (a_not_infinity & b_not_infinity & x_and_y_equal);
284  return equal & 1;
285 }
286 
288  const EC_RAW_POINT *b) {
289  // If |b| is not infinity, we have to decide whether
290  // (X_a, Y_a) = (X_b/Z_b^2, Y_b/Z_b^3),
291  // or equivalently, whether
292  // (X_a*Z_b^2, Y_a*Z_b^3) = (X_b, Y_b).
293 
294  void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
295  const EC_FELEM *b) = group->meth->felem_mul;
296  void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
297  group->meth->felem_sqr;
298 
299  EC_FELEM tmp, Zb2;
300  felem_sqr(group, &Zb2, &b->Z); // Zb2 = Z_b^2
301  felem_mul(group, &tmp, &a->X, &Zb2); // tmp = X_a * Z_b^2
302  ec_felem_sub(group, &tmp, &tmp, &b->X);
303  const BN_ULONG x_not_equal = ec_felem_non_zero_mask(group, &tmp);
304 
305  felem_mul(group, &tmp, &a->Y, &Zb2); // tmp = Y_a * Z_b^2
306  felem_mul(group, &tmp, &tmp, &b->Z); // tmp = Y_a * Z_b^3
307  ec_felem_sub(group, &tmp, &tmp, &b->Y);
308  const BN_ULONG y_not_equal = ec_felem_non_zero_mask(group, &tmp);
309  const BN_ULONG x_and_y_equal = ~(x_not_equal | y_not_equal);
310 
311  const BN_ULONG b_not_infinity = ec_felem_non_zero_mask(group, &b->Z);
312 
313  const BN_ULONG equal = b_not_infinity & x_and_y_equal;
314  return equal & 1;
315 }
316 
318  const EC_SCALAR *r) {
320  // |ec_get_x_coordinate_as_scalar| will check this internally, but this way
321  // we do not push to the error queue.
322  return 0;
323  }
324 
325  EC_SCALAR x;
326  return ec_get_x_coordinate_as_scalar(group, &x, p) &&
328 }
329 
331  size_t *out_len, const EC_FELEM *in) {
332  size_t len = BN_num_bytes(&group->field);
333  for (size_t i = 0; i < len; i++) {
334  out[i] = in->bytes[len - 1 - i];
335  }
336  *out_len = len;
337 }
338 
340  const uint8_t *in, size_t len) {
341  if (len != BN_num_bytes(&group->field)) {
343  return 0;
344  }
345 
346  OPENSSL_memset(out, 0, sizeof(EC_FELEM));
347  for (size_t i = 0; i < len; i++) {
348  out->bytes[i] = in[len - 1 - i];
349  }
350 
351  if (!bn_less_than_words(out->words, group->field.d, group->field.width)) {
353  return 0;
354  }
355 
356  return 1;
357 }
ec_get_x_coordinate_as_scalar
#define ec_get_x_coordinate_as_scalar
Definition: boringssl_prefix_symbols.h:3106
EC_R_INVALID_FIELD
#define EC_R_INVALID_FIELD
Definition: ec.h:417
bn.h
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
bn_set_minimal_width
#define bn_set_minimal_width
Definition: boringssl_prefix_symbols.h:2905
ctx
Definition: benchmark-async.c:30
ec_felem_neg
#define ec_felem_neg
Definition: boringssl_prefix_symbols.h:3099
BN_add_word
#define BN_add_word
Definition: boringssl_prefix_symbols.h:898
ec_GFp_simple_group_set_curve
int ec_GFp_simple_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
Definition: simple.c:101
ec_GFp_simple_felem_to_bytes
void ec_GFp_simple_felem_to_bytes(const EC_GROUP *group, uint8_t *out, size_t *out_len, const EC_FELEM *in)
Definition: simple.c:330
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
string.h
equal
static uint8_t equal(signed char b, signed char c)
Definition: curve25519.c:708
ec_GFp_simple_cmp_x_coordinate
int ec_GFp_simple_cmp_x_coordinate(const EC_GROUP *group, const EC_RAW_POINT *p, const EC_SCALAR *r)
Definition: simple.c:317
error_ref_leak.err
err
Definition: error_ref_leak.py:35
ec_GFp_simple_group_init
int ec_GFp_simple_group_init(EC_GROUP *group)
Definition: simple.c:91
ec_GFp_simple_felem_from_bytes
int ec_GFp_simple_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out, const uint8_t *in, size_t len)
Definition: simple.c:339
BN_free
#define BN_free
Definition: boringssl_prefix_symbols.h:923
ec_GFp_simple_group_finish
void ec_GFp_simple_group_finish(EC_GROUP *group)
Definition: simple.c:97
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
bignum_ctx
Definition: ctx.c:91
BN_CTX_get
#define BN_CTX_get
Definition: boringssl_prefix_symbols.h:884
xds_manager.p
p
Definition: xds_manager.py:60
BN_num_bytes
#define BN_num_bytes
Definition: boringssl_prefix_symbols.h:976
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
EC_RAW_POINT
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:260
BN_value_one
const OPENSSL_EXPORT BIGNUM * BN_value_one(void)
ec_felem_sub
#define ec_felem_sub
Definition: boringssl_prefix_symbols.h:3102
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
EC_RAW_POINT::Y
EC_FELEM Y
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:263
ec_scalar_equal_vartime
#define ec_scalar_equal_vartime
Definition: boringssl_prefix_symbols.h:3127
ec_affine_jacobian_equal
int ec_affine_jacobian_equal(const EC_GROUP *group, const EC_AFFINE *a, const EC_RAW_POINT *b)
Definition: simple.c:287
ec_felem_to_bignum
#define ec_felem_to_bignum
Definition: boringssl_prefix_symbols.h:3103
ec_GFp_simple_group_get_curve
int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b)
Definition: simple.c:145
ec_GFp_simple_points_equal
int ec_GFp_simple_points_equal(const EC_GROUP *group, const EC_RAW_POINT *a, const EC_RAW_POINT *b)
Definition: simple.c:240
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
ec_GFp_simple_point_copy
void ec_GFp_simple_point_copy(EC_RAW_POINT *dest, const EC_RAW_POINT *src)
Definition: simple.c:161
ec_GFp_simple_is_on_curve
int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_RAW_POINT *point)
Definition: simple.c:183
err.h
BN_is_odd
#define BN_is_odd
Definition: boringssl_prefix_symbols.h:934
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
EC_FELEM
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:195
ec_GFp_simple_invert
void ec_GFp_simple_invert(const EC_GROUP *group, EC_RAW_POINT *point)
Definition: simple.c:174
EC_RAW_POINT::Z
EC_FELEM Z
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:263
tests.qps.qps_worker.dest
dest
Definition: qps_worker.py:45
BN_copy
#define BN_copy
Definition: boringssl_prefix_symbols.h:914
BN_num_bits
#define BN_num_bits
Definition: boringssl_prefix_symbols.h:974
point
Definition: bloaty/third_party/zlib/examples/zran.c:67
ec_GFp_simple_point_init
void ec_GFp_simple_point_init(EC_RAW_POINT *point)
Definition: simple.c:155
internal.h
EC_AFFINE
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:269
upload.group
group
Definition: bloaty/third_party/googletest/googlemock/scripts/upload.py:397
ec_felem_non_zero_mask
#define ec_felem_non_zero_mask
Definition: boringssl_prefix_symbols.h:3100
BN_CTX_start
#define BN_CTX_start
Definition: boringssl_prefix_symbols.h:886
ec_felem_add
#define ec_felem_add
Definition: boringssl_prefix_symbols.h:3096
bn_less_than_words
#define bn_less_than_words
Definition: boringssl_prefix_symbols.h:2865
bignum_st
Definition: bn.h:957
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
ec_group_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:573
fix_build_deps.r
r
Definition: fix_build_deps.py:491
BN_cmp
#define BN_cmp
Definition: boringssl_prefix_symbols.h:912
BN_init
#define BN_init
Definition: boringssl_prefix_symbols.h:931
EC_R_DECODE_ERROR
#define EC_R_DECODE_ERROR
Definition: ec.h:435
ec_GFp_simple_is_at_infinity
int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_RAW_POINT *point)
Definition: simple.c:178
ec_GFp_simple_point_set_to_infinity
void ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group, EC_RAW_POINT *point)
Definition: simple.c:167
ec_bignum_to_felem
#define ec_bignum_to_felem
Definition: boringssl_prefix_symbols.h:3092
BN_CTX_end
#define BN_CTX_end
Definition: boringssl_prefix_symbols.h:882
EC_SCALAR
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:103
mem.h
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
autogen_x86imm.tmp
tmp
Definition: autogen_x86imm.py:12
ec.h
EC_RAW_POINT::X
EC_FELEM X
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:263
BN_set_negative
#define BN_set_negative
Definition: boringssl_prefix_symbols.h:990
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:18