Coverage Report

Created: 2025-12-07 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/fipsmodule/ec/felem.cc.inc
Line
Count
Source
1
// Copyright 2018 The BoringSSL Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
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
26
67.4k
const EC_FELEM *ec_felem_one(const EC_GROUP *group) {
27
  // We reuse generator.Z as a cache for 1 in the field.
28
67.4k
  return &group->generator.raw.Z;
29
67.4k
}
30
31
6.04k
int ec_bignum_to_felem(const EC_GROUP *group, EC_FELEM *out, const BIGNUM *in) {
32
6.04k
  uint8_t bytes[EC_MAX_BYTES];
33
6.04k
  size_t len = BN_num_bytes(&group->field.N);
34
6.04k
  assert(sizeof(bytes) >= len);
35
6.04k
  if (BN_is_negative(in) || BN_cmp(in, &group->field.N) >= 0 ||
36
6.04k
      !BN_bn2bin_padded(bytes, len, in)) {
37
5
    OPENSSL_PUT_ERROR(EC, EC_R_COORDINATES_OUT_OF_RANGE);
38
5
    return 0;
39
5
  }
40
41
6.04k
  return ec_felem_from_bytes(group, out, bytes, len);
42
6.04k
}
43
44
37.8k
int ec_felem_to_bignum(const EC_GROUP *group, BIGNUM *out, const EC_FELEM *in) {
45
37.8k
  uint8_t bytes[EC_MAX_BYTES];
46
37.8k
  size_t len;
47
37.8k
  ec_felem_to_bytes(group, bytes, &len, in);
48
37.8k
  return BN_bin2bn(bytes, len, out) != nullptr;
49
37.8k
}
50
51
void ec_felem_to_bytes(const EC_GROUP *group, uint8_t *out, size_t *out_len,
52
106k
                       const EC_FELEM *in) {
53
106k
  group->meth->felem_to_bytes(group, out, out_len, in);
54
106k
}
55
56
int ec_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out, const uint8_t *in,
57
164k
                        size_t len) {
58
164k
  return group->meth->felem_from_bytes(group, out, in, len);
59
164k
}
60
61
76.4k
void ec_felem_neg(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a) {
62
  // -a is zero if a is zero and p-a otherwise.
63
76.4k
  BN_ULONG mask = ec_felem_non_zero_mask(group, a);
64
76.4k
  BN_ULONG borrow = bn_sub_words(out->words, group->field.N.d, a->words,
65
76.4k
                                 group->field.N.width);
66
76.4k
  assert(borrow == 0);
67
76.4k
  (void)borrow;
68
569k
  for (int i = 0; i < group->field.N.width; i++) {
69
492k
    out->words[i] &= mask;
70
492k
  }
71
76.4k
}
72
73
void ec_felem_add(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a,
74
242M
                  const EC_FELEM *b) {
75
242M
  EC_FELEM tmp;
76
242M
  bn_mod_add_words(out->words, a->words, b->words, group->field.N.d, tmp.words,
77
242M
                   group->field.N.width);
78
242M
}
79
80
void ec_felem_sub(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a,
81
166M
                  const EC_FELEM *b) {
82
166M
  EC_FELEM tmp;
83
166M
  bn_mod_sub_words(out->words, a->words, b->words, group->field.N.d, tmp.words,
84
166M
                   group->field.N.width);
85
166M
}
86
87
21.4M
BN_ULONG ec_felem_non_zero_mask(const EC_GROUP *group, const EC_FELEM *a) {
88
21.4M
  BN_ULONG mask = 0;
89
151M
  for (int i = 0; i < group->field.N.width; i++) {
90
130M
    mask |= a->words[i];
91
130M
  }
92
21.4M
  return ~constant_time_is_zero_w(mask);
93
21.4M
}
94
95
void ec_felem_select(const EC_GROUP *group, EC_FELEM *out, BN_ULONG mask,
96
446M
                     const EC_FELEM *a, const EC_FELEM *b) {
97
446M
  bn_select_words(out->words, mask, a->words, b->words, group->field.N.width);
98
446M
}
99
100
int ec_felem_equal(const EC_GROUP *group, const EC_FELEM *a,
101
83.5k
                   const EC_FELEM *b) {
102
83.5k
  return CRYPTO_memcmp(a->words, b->words,
103
83.5k
                       group->field.N.width * sizeof(BN_ULONG)) == 0;
104
83.5k
}