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