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