Coverage Report

Created: 2024-11-21 06:47

/src/boringssl/crypto/fipsmodule/ec/felem.c.inc
Line
Count
Source (jump to first uncovered line)
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
26
0
const EC_FELEM *ec_felem_one(const EC_GROUP *group) {
27
  // We reuse generator.Z as a cache for 1 in the field.
28
0
  return &group->generator.raw.Z;
29
0
}
30
31
0
int ec_bignum_to_felem(const EC_GROUP *group, EC_FELEM *out, const BIGNUM *in) {
32
0
  uint8_t bytes[EC_MAX_BYTES];
33
0
  size_t len = BN_num_bytes(&group->field.N);
34
0
  assert(sizeof(bytes) >= len);
35
0
  if (BN_is_negative(in) || BN_cmp(in, &group->field.N) >= 0 ||
36
0
      !BN_bn2bin_padded(bytes, len, in)) {
37
0
    OPENSSL_PUT_ERROR(EC, EC_R_COORDINATES_OUT_OF_RANGE);
38
0
    return 0;
39
0
  }
40
41
0
  return ec_felem_from_bytes(group, out, bytes, len);
42
0
}
43
44
0
int ec_felem_to_bignum(const EC_GROUP *group, BIGNUM *out, const EC_FELEM *in) {
45
0
  uint8_t bytes[EC_MAX_BYTES];
46
0
  size_t len;
47
0
  ec_felem_to_bytes(group, bytes, &len, in);
48
0
  return BN_bin2bn(bytes, len, out) != NULL;
49
0
}
50
51
void ec_felem_to_bytes(const EC_GROUP *group, uint8_t *out, size_t *out_len,
52
0
                       const EC_FELEM *in) {
53
0
  group->meth->felem_to_bytes(group, out, out_len, in);
54
0
}
55
56
int ec_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out, const uint8_t *in,
57
0
                        size_t len) {
58
0
  return group->meth->felem_from_bytes(group, out, in, len);
59
0
}
60
61
0
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
0
  BN_ULONG mask = ec_felem_non_zero_mask(group, a);
64
0
  BN_ULONG borrow = bn_sub_words(out->words, group->field.N.d, a->words,
65
0
                                 group->field.N.width);
66
0
  assert(borrow == 0);
67
0
  (void)borrow;
68
0
  for (int i = 0; i < group->field.N.width; i++) {
69
0
    out->words[i] &= mask;
70
0
  }
71
0
}
72
73
void ec_felem_add(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a,
74
0
                  const EC_FELEM *b) {
75
0
  EC_FELEM tmp;
76
0
  bn_mod_add_words(out->words, a->words, b->words, group->field.N.d, tmp.words,
77
0
                   group->field.N.width);
78
0
}
79
80
void ec_felem_sub(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a,
81
0
                  const EC_FELEM *b) {
82
0
  EC_FELEM tmp;
83
0
  bn_mod_sub_words(out->words, a->words, b->words, group->field.N.d, tmp.words,
84
0
                   group->field.N.width);
85
0
}
86
87
0
BN_ULONG ec_felem_non_zero_mask(const EC_GROUP *group, const EC_FELEM *a) {
88
0
  BN_ULONG mask = 0;
89
0
  for (int i = 0; i < group->field.N.width; i++) {
90
0
    mask |= a->words[i];
91
0
  }
92
0
  return ~constant_time_is_zero_w(mask);
93
0
}
94
95
void ec_felem_select(const EC_GROUP *group, EC_FELEM *out, BN_ULONG mask,
96
0
                     const EC_FELEM *a, const EC_FELEM *b) {
97
0
  bn_select_words(out->words, mask, a->words, b->words, group->field.N.width);
98
0
}
99
100
int ec_felem_equal(const EC_GROUP *group, const EC_FELEM *a,
101
0
                   const EC_FELEM *b) {
102
0
  return CRYPTO_memcmp(a->words, b->words,
103
0
                       group->field.N.width * sizeof(BN_ULONG)) == 0;
104
0
}