Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/ec/scalar.c

raw
1 // Copyright (c) 2018, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <openssl/ec.h>
5 #include <openssl/err.h>
6 #include <openssl/mem.h>
7
8 #include "internal.h"
9 #include "../bn/internal.h"
10 #include "../../internal.h"
11
12
13 int ec_bignum_to_scalar(const EC_GROUP *group, EC_SCALAR *out,
14 const BIGNUM *in) {
15 // Scalars, which are often secret, must be reduced modulo the order. Those
16 // that are not will be discarded, so leaking the result of the comparison is
17 // safe.
18 if (!bn_copy_words(out->words, group->order.N.width, in) ||
19 !constant_time_declassify_int(bn_less_than_words(
20 out->words, group->order.N.d, group->order.N.width))) {
21 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_SCALAR);
22 return 0;
23 }
24 return 1;
25 }
26
27 int ec_scalar_equal_vartime(const EC_GROUP *group, const EC_SCALAR *a,
28 const EC_SCALAR *b) {
29 return OPENSSL_memcmp(a->words, b->words,
30 group->order.N.width * sizeof(BN_ULONG)) == 0;
31 }
32
33 int ec_scalar_is_zero(const EC_GROUP *group, const EC_SCALAR *a) {
34 BN_ULONG mask = 0;
35 for (int i = 0; i < group->order.N.width; i++) {
36 mask |= a->words[i];
37 }
38 return mask == 0;
39 }
40
41 int ec_random_nonzero_scalar(const EC_GROUP *group, EC_SCALAR *out,
42 const uint8_t additional_data[32]) {
43 return bn_rand_range_words(out->words, 1, group->order.N.d,
44 group->order.N.width, additional_data);
45 }
46
47 void ec_scalar_to_bytes(const EC_GROUP *group, uint8_t *out, size_t *out_len,
48 const EC_SCALAR *in) {
49 size_t len = BN_num_bytes(&group->order.N);
50 bn_words_to_big_endian(out, len, in->words, group->order.N.width);
51 *out_len = len;
52 }
53
54 int ec_scalar_from_bytes(const EC_GROUP *group, EC_SCALAR *out,
55 const uint8_t *in, size_t len) {
56 if (len != BN_num_bytes(&group->order.N)) {
57 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_SCALAR);
58 return 0;
59 }
60
61 bn_big_endian_to_words(out->words, group->order.N.width, in, len);
62
63 if (!bn_less_than_words(out->words, group->order.N.d, group->order.N.width)) {
64 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_SCALAR);
65 return 0;
66 }
67
68 return 1;
69 }
70
71 void ec_scalar_reduce(const EC_GROUP *group, EC_SCALAR *out,
72 const BN_ULONG *words, size_t num) {
73 // Convert "from" Montgomery form so the value is reduced modulo the order.
74 bn_from_montgomery_small(out->words, group->order.N.width, words, num,
75 &group->order);
76 // Convert "to" Montgomery form to remove the R^-1 factor added.
77 ec_scalar_to_montgomery(group, out, out);
78 }
79
80 void ec_scalar_add(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a,
81 const EC_SCALAR *b) {
82 const BIGNUM *order = &group->order.N;
83 BN_ULONG tmp[EC_MAX_WORDS];
84 bn_mod_add_words(r->words, a->words, b->words, order->d, tmp, order->width);
85 OPENSSL_cleanse(tmp, sizeof(tmp));
86 }
87
88 void ec_scalar_sub(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a,
89 const EC_SCALAR *b) {
90 const BIGNUM *order = &group->order.N;
91 BN_ULONG tmp[EC_MAX_WORDS];
92 bn_mod_sub_words(r->words, a->words, b->words, order->d, tmp, order->width);
93 OPENSSL_cleanse(tmp, sizeof(tmp));
94 }
95
96 void ec_scalar_neg(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a) {
97 EC_SCALAR zero;
98 OPENSSL_memset(&zero, 0, sizeof(EC_SCALAR));
99 ec_scalar_sub(group, r, &zero, a);
100 }
101
102 void ec_scalar_select(const EC_GROUP *group, EC_SCALAR *out, BN_ULONG mask,
103 const EC_SCALAR *a, const EC_SCALAR *b) {
104 const BIGNUM *order = &group->order.N;
105 bn_select_words(out->words, mask, a->words, b->words, order->width);
106 }
107
108 void ec_scalar_to_montgomery(const EC_GROUP *group, EC_SCALAR *r,
109 const EC_SCALAR *a) {
110 const BIGNUM *order = &group->order.N;
111 bn_to_montgomery_small(r->words, a->words, order->width, &group->order);
112 }
113
114 void ec_scalar_from_montgomery(const EC_GROUP *group, EC_SCALAR *r,
115 const EC_SCALAR *a) {
116 const BIGNUM *order = &group->order.N;
117 bn_from_montgomery_small(r->words, order->width, a->words, order->width,
118 &group->order);
119 }
120
121 void ec_scalar_mul_montgomery(const EC_GROUP *group, EC_SCALAR *r,
122 const EC_SCALAR *a, const EC_SCALAR *b) {
123 const BIGNUM *order = &group->order.N;
124 bn_mod_mul_montgomery_small(r->words, a->words, b->words, order->width,
125 &group->order);
126 }
127
128 void ec_simple_scalar_inv0_montgomery(const EC_GROUP *group, EC_SCALAR *r,
129 const EC_SCALAR *a) {
130 const BIGNUM *order = &group->order.N;
131 bn_mod_inverse0_prime_mont_small(r->words, a->words, order->width,
132 &group->order);
133 }
134
135 int ec_simple_scalar_to_montgomery_inv_vartime(const EC_GROUP *group,
136 EC_SCALAR *r,
137 const EC_SCALAR *a) {
138 if (ec_scalar_is_zero(group, a)) {
139 return 0;
140 }
141
142 // This implementation (in fact) runs in constant time,
143 // even though for this interface it is not mandatory.
144
145 // r = a^-1 in the Montgomery domain. This is
146 // |ec_scalar_to_montgomery| followed by |ec_scalar_inv0_montgomery|, but
147 // |ec_scalar_inv0_montgomery| followed by |ec_scalar_from_montgomery| is
148 // equivalent and slightly more efficient.
149 ec_scalar_inv0_montgomery(group, r, a);
150 ec_scalar_from_montgomery(group, r, r);
151 return 1;
152 }
153
154 void ec_scalar_inv0_montgomery(const EC_GROUP *group, EC_SCALAR *r,
155 const EC_SCALAR *a) {
156 group->meth->scalar_inv0_montgomery(group, r, a);
157 }
158
159 int ec_scalar_to_montgomery_inv_vartime(const EC_GROUP *group, EC_SCALAR *r,
160 const EC_SCALAR *a) {
161 return group->meth->scalar_to_montgomery_inv_vartime(group, r, a);
162 }