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