felem.c
Go to the documentation of this file.
1 /* Copyright (c) 2018, 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/ec.h>
16 #include <openssl/err.h>
17 #include <openssl/mem.h>
18 
19 #include <assert.h>
20 
21 #include "internal.h"
22 #include "../bn/internal.h"
23 #include "../../internal.h"
24 
25 
28  size_t len = BN_num_bytes(&group->field);
29  assert(sizeof(bytes) >= len);
30  if (BN_is_negative(in) ||
31  BN_cmp(in, &group->field) >= 0 ||
34  return 0;
35  }
36 
38 }
39 
42  size_t len;
44  return BN_bin2bn(bytes, len, out) != NULL;
45 }
46 
47 void ec_felem_to_bytes(const EC_GROUP *group, uint8_t *out, size_t *out_len,
48  const EC_FELEM *in) {
49  group->meth->felem_to_bytes(group, out, out_len, in);
50 }
51 
53  size_t len) {
54  return group->meth->felem_from_bytes(group, out, in, len);
55 }
56 
57 void ec_felem_neg(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a) {
58  // -a is zero if a is zero and p-a otherwise.
59  BN_ULONG mask = ec_felem_non_zero_mask(group, a);
60  BN_ULONG borrow =
61  bn_sub_words(out->words, group->field.d, a->words, group->field.width);
62  assert(borrow == 0);
63  (void)borrow;
64  for (int i = 0; i < group->field.width; i++) {
65  out->words[i] &= mask;
66  }
67 }
68 
70  const EC_FELEM *b) {
71  EC_FELEM tmp;
72  bn_mod_add_words(out->words, a->words, b->words, group->field.d, tmp.words,
73  group->field.width);
74 }
75 
77  const EC_FELEM *b) {
78  EC_FELEM tmp;
79  bn_mod_sub_words(out->words, a->words, b->words, group->field.d, tmp.words,
80  group->field.width);
81 }
82 
83 BN_ULONG ec_felem_non_zero_mask(const EC_GROUP *group, const EC_FELEM *a) {
84  BN_ULONG mask = 0;
85  for (int i = 0; i < group->field.width; i++) {
86  mask |= a->words[i];
87  }
88  return ~constant_time_is_zero_w(mask);
89 }
90 
91 void ec_felem_select(const EC_GROUP *group, EC_FELEM *out, BN_ULONG mask,
92  const EC_FELEM *a, const EC_FELEM *b) {
93  bn_select_words(out->words, mask, a->words, b->words, group->field.width);
94 }
95 
96 int ec_felem_equal(const EC_GROUP *group, const EC_FELEM *a,
97  const EC_FELEM *b) {
98  return CRYPTO_memcmp(a->words, b->words,
99  group->field.width * sizeof(BN_ULONG)) == 0;
100 }
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
bn_select_words
#define bn_select_words
Definition: boringssl_prefix_symbols.h:2904
BN_bin2bn
#define BN_bin2bn
Definition: boringssl_prefix_symbols.h:900
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
ec_felem_non_zero_mask
BN_ULONG ec_felem_non_zero_mask(const EC_GROUP *group, const EC_FELEM *a)
Definition: felem.c:83
bn_sub_words
#define bn_sub_words
Definition: boringssl_prefix_symbols.h:2915
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
BN_num_bytes
#define BN_num_bytes
Definition: boringssl_prefix_symbols.h:976
ec_felem_to_bignum
int ec_felem_to_bignum(const EC_GROUP *group, BIGNUM *out, const EC_FELEM *in)
Definition: felem.c:40
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
constant_time_is_zero_w
static crypto_word_t constant_time_is_zero_w(crypto_word_t a)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:372
bn_mod_sub_words
#define bn_mod_sub_words
Definition: boringssl_prefix_symbols.h:2881
ec_felem_sub
void ec_felem_sub(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, const EC_FELEM *b)
Definition: felem.c:76
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
EC_MAX_BYTES
#define EC_MAX_BYTES
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:91
ec_felem_neg
void ec_felem_neg(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a)
Definition: felem.c:57
ec_felem_to_bytes
void ec_felem_to_bytes(const EC_GROUP *group, uint8_t *out, size_t *out_len, const EC_FELEM *in)
Definition: felem.c:47
err.h
ec_felem_select
void ec_felem_select(const EC_GROUP *group, EC_FELEM *out, BN_ULONG mask, const EC_FELEM *a, const EC_FELEM *b)
Definition: felem.c:91
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_felem_equal
int ec_felem_equal(const EC_GROUP *group, const EC_FELEM *a, const EC_FELEM *b)
Definition: felem.c:96
ec_felem_add
void ec_felem_add(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, const EC_FELEM *b)
Definition: felem.c:69
upload.group
group
Definition: bloaty/third_party/googletest/googlemock/scripts/upload.py:397
bytes
uint8 bytes[10]
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:153
bignum_st
Definition: bn.h:957
internal.h
ec_group_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:573
BN_cmp
#define BN_cmp
Definition: boringssl_prefix_symbols.h:912
BN_is_negative
#define BN_is_negative
Definition: boringssl_prefix_symbols.h:933
BN_bn2bin_padded
#define BN_bn2bin_padded
Definition: boringssl_prefix_symbols.h:902
ec_bignum_to_felem
int ec_bignum_to_felem(const EC_GROUP *group, EC_FELEM *out, const BIGNUM *in)
Definition: felem.c:26
mem.h
EC_R_COORDINATES_OUT_OF_RANGE
#define EC_R_COORDINATES_OUT_OF_RANGE
Definition: ec.h:408
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
CRYPTO_memcmp
#define CRYPTO_memcmp
Definition: boringssl_prefix_symbols.h:1178
bn_mod_add_words
#define bn_mod_add_words
Definition: boringssl_prefix_symbols.h:2870
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
ec_felem_from_bytes
int ec_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out, const uint8_t *in, size_t len)
Definition: felem.c:52


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:22