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
ec_felem_one(const EC_GROUP * group)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
ec_bignum_to_felem(const EC_GROUP * group,EC_FELEM * out,const BIGNUM * in)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
ec_felem_to_bignum(const EC_GROUP * group,BIGNUM * out,const EC_FELEM * in)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
ec_felem_to_bytes(const EC_GROUP * group,uint8_t * out,size_t * out_len,const EC_FELEM * in)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
ec_felem_from_bytes(const EC_GROUP * group,EC_FELEM * out,const uint8_t * in,size_t len)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
ec_felem_neg(const EC_GROUP * group,EC_FELEM * out,const EC_FELEM * a)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
ec_felem_add(const EC_GROUP * group,EC_FELEM * out,const EC_FELEM * a,const EC_FELEM * b)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
ec_felem_sub(const EC_GROUP * group,EC_FELEM * out,const EC_FELEM * a,const EC_FELEM * b)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
ec_felem_non_zero_mask(const EC_GROUP * group,const EC_FELEM * a)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
ec_felem_select(const EC_GROUP * group,EC_FELEM * out,BN_ULONG mask,const EC_FELEM * a,const EC_FELEM * b)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
ec_felem_equal(const EC_GROUP * group,const EC_FELEM * a,const EC_FELEM * b)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