1 /* Copyright 2016-2017 Brian Smith.
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 AUTHORS DISCLAIM ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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 "limbs.h"
16 
17 #include "../internal.h"
18 #include "../fipsmodule/bn/internal.h"
19 #include "limbs.inl"
20 
21 
22 /* XXX: We assume that the conversion from |Carry| to |Limb| is constant-time,
23  * but we haven't verified that assumption. TODO: Fix it so we don't need to
24  * make that assumption. */
25 
26 /* Returns 0xfff..f if |a| is all zero limbs, and zero otherwise. |num_limbs|
27  * may be zero. */
LIMBS_are_zero(const Limb a[],size_t num_limbs)28 Limb LIMBS_are_zero(const Limb a[], size_t num_limbs) {
29   Limb is_zero = CONSTTIME_TRUE_W;
30   for (size_t i = 0; i < num_limbs; ++i) {
31     is_zero = constant_time_select_w(is_zero, constant_time_is_zero_w(a[i]),
32                                      is_zero);
33   }
34   return is_zero;
35 }
36 
37 /* Returns 0xffff..f if |a == b|, and zero otherwise. |num_limbs| may be zero. */
LIMBS_equal(const Limb a[],const Limb b[],size_t num_limbs)38 Limb LIMBS_equal(const Limb a[], const Limb b[], size_t num_limbs) {
39   Limb eq = CONSTTIME_TRUE_W;
40   for (size_t i = 0; i < num_limbs; ++i) {
41     eq = constant_time_select_w(eq, constant_time_eq_w(a[i], b[i]), eq);
42   }
43   return eq;
44 }
45 
46 /* Returns 0xffff..f if |a == b|, and zero otherwise. |num_limbs| may be zero. */
LIMBS_equal_limb(const Limb a[],Limb b,size_t num_limbs)47 Limb LIMBS_equal_limb(const Limb a[], Limb b, size_t num_limbs) {
48   if (num_limbs == 0) {
49     return constant_time_is_zero_w(b);
50   }
51   debug_assert_nonsecret(num_limbs >= 1);
52   Limb lo_equal = constant_time_eq_w(a[0], b);
53   Limb hi_zero = LIMBS_are_zero(&a[1], num_limbs - 1);
54   return constant_time_select_w(lo_equal, hi_zero, 0);
55 }
56 
57 /* Returns 0xfff..f if |a| is all zero limbs, and zero otherwise.
58  * |num_limbs| may be zero. */
LIMBS_are_even(const Limb a[],size_t num_limbs)59 Limb LIMBS_are_even(const Limb a[], size_t num_limbs) {
60   Limb lo;
61   if (num_limbs == 0) {
62     lo = 0;
63   } else {
64     lo = a[0];
65   }
66   return constant_time_is_zero_w(lo & 1);
67 }
68 
69 /* Returns 0xffff...f if |a| is less than |b|, and zero otherwise. */
LIMBS_less_than(const Limb a[],const Limb b[],size_t num_limbs)70 Limb LIMBS_less_than(const Limb a[], const Limb b[], size_t num_limbs) {
71   debug_assert_nonsecret(num_limbs >= 1);
72   /* There are lots of ways to implement this. It is implemented this way to
73    * be consistent with |LIMBS_limbs_reduce_once| and other code that makes such
74    * comparisons as part of doing conditional reductions. */
75   Limb dummy;
76   Carry borrow = limb_sub(&dummy, a[0], b[0]);
77   for (size_t i = 1; i < num_limbs; ++i) {
78     borrow = limb_sbb(&dummy, a[i], b[i], borrow);
79   }
80   return constant_time_is_nonzero_w(borrow);
81 }
82 
LIMBS_less_than_limb(const Limb a[],Limb b,size_t num_limbs)83 Limb LIMBS_less_than_limb(const Limb a[], Limb b, size_t num_limbs) {
84   debug_assert_nonsecret(num_limbs >= 1);
85 
86   Limb dummy;
87   Limb lo = constant_time_is_nonzero_w(limb_sub(&dummy, a[0], b));
88   Limb hi = LIMBS_are_zero(&a[1], num_limbs - 1);
89   return constant_time_select_w(lo, hi, lo);
90 }
91 
92 /* if (r >= m) { r -= m; } */
LIMBS_reduce_once(Limb r[],const Limb m[],size_t num_limbs)93 void LIMBS_reduce_once(Limb r[], const Limb m[], size_t num_limbs) {
94   debug_assert_nonsecret(num_limbs >= 1);
95   /* This could be done more efficiently if we had |num_limbs| of extra space
96    * available, by storing |r - m| and then doing a conditional copy of either
97    * |r| or |r - m|. But, in order to operate in constant space, with an eye
98    * towards this function being used in RSA in the future, we do things a
99    * slightly less efficient way. */
100   Limb lt = LIMBS_less_than(r, m, num_limbs);
101   Carry borrow =
102       limb_sub(&r[0], r[0], constant_time_select_w(lt, 0, m[0]));
103   for (size_t i = 1; i < num_limbs; ++i) {
104     /* XXX: This is probably particularly inefficient because the operations in
105      * constant_time_select affect the carry flag, so there will likely be
106      * loads and stores of |borrow|. */
107     borrow =
108         limb_sbb(&r[i], r[i], constant_time_select_w(lt, 0, m[i]), borrow);
109   }
110   dev_assert_secret(borrow == 0);
111 }
112 
LIMBS_add_mod(Limb r[],const Limb a[],const Limb b[],const Limb m[],size_t num_limbs)113 void LIMBS_add_mod(Limb r[], const Limb a[], const Limb b[], const Limb m[],
114                    size_t num_limbs) {
115   Limb overflow1 =
116       constant_time_is_nonzero_w(limbs_add(r, a, b, num_limbs));
117   Limb overflow2 = ~LIMBS_less_than(r, m, num_limbs);
118   Limb overflow = overflow1 | overflow2;
119   Carry borrow = limb_sub(&r[0], r[0], m[0] & overflow);
120   for (size_t i = 1; i < num_limbs; ++i) {
121     borrow = limb_sbb(&r[i], r[i], m[i] & overflow, borrow);
122   }
123 }
124 
LIMBS_sub_mod(Limb r[],const Limb a[],const Limb b[],const Limb m[],size_t num_limbs)125 void LIMBS_sub_mod(Limb r[], const Limb a[], const Limb b[], const Limb m[],
126                    size_t num_limbs) {
127   Limb underflow =
128       constant_time_is_nonzero_w(limbs_sub(r, a, b, num_limbs));
129   Carry carry = limb_add(&r[0], r[0], m[0] & underflow);
130   for (size_t i = 1; i < num_limbs; ++i) {
131     carry = limb_adc(&r[i], r[i], m[i] & underflow, carry);
132   }
133 }
134 
LIMBS_shl_mod(Limb r[],const Limb a[],const Limb m[],size_t num_limbs)135 void LIMBS_shl_mod(Limb r[], const Limb a[], const Limb m[], size_t num_limbs) {
136   Limb overflow1 =
137       constant_time_is_nonzero_w(a[num_limbs - 1] & LIMB_HIGH_BIT);
138   Limb carry = 0;
139   for (size_t i = 0; i < num_limbs; ++i) {
140     Limb limb = a[i];
141     Limb new_carry = limb >> (LIMB_BITS - 1);
142     r[i] = (limb << 1) | carry;
143     carry = new_carry;
144   }
145   Limb overflow2 = ~LIMBS_less_than(r, m, num_limbs);
146   Limb overflow = overflow1 | overflow2;
147   Carry borrow = limb_sub(&r[0], r[0], m[0] & overflow);
148   for (size_t i = 1; i < num_limbs; ++i) {
149     borrow = limb_sbb(&r[i], r[i], m[i] & overflow, borrow);
150   }
151 }
152 
LIMBS_select_512_32(Limb r[],const Limb table[],size_t num_limbs,crypto_word index)153 int LIMBS_select_512_32(Limb r[], const Limb table[], size_t num_limbs,
154                         crypto_word index) {
155   if (num_limbs % (512 / LIMB_BITS) != 0) {
156     return 0;
157   }
158   limbs_select(r, table, num_limbs, 32, index);
159   return 1;
160 }
161 
162 static const Limb FIVE_BITS_MASK = 0x1f;
163 
LIMBS_window5_split_window(Limb lower_limb,Limb higher_limb,size_t index_within_word)164 crypto_word LIMBS_window5_split_window(Limb lower_limb, Limb higher_limb, size_t index_within_word) {
165   Limb high_bits = (higher_limb << (LIMB_BITS - index_within_word))
166     & FIVE_BITS_MASK;
167   // There are no bits outside the window above |index_within_word| (if there
168   // were then this wouldn't be a split window), so we don't need to mask
169   // |low_bits|.
170   Limb low_bits = lower_limb >> index_within_word;
171   return low_bits | high_bits;
172 }
173 
LIMBS_window5_unsplit_window(Limb limb,size_t index_within_word)174 crypto_word LIMBS_window5_unsplit_window(Limb limb, size_t index_within_word) {
175   return (limb >> index_within_word) & FIVE_BITS_MASK;
176 }
177 
LIMB_shr(Limb a,size_t shift)178 Limb LIMB_shr(Limb a, size_t shift) {
179   return a >> shift;
180 }
181 
GFp_limbs_mul_add_limb(Limb r[],const Limb a[],Limb b,size_t num_limbs)182 Limb GFp_limbs_mul_add_limb(Limb r[], const Limb a[], Limb b, size_t num_limbs) {
183   Limb carried = 0;
184   for (size_t i = 0; i < num_limbs; ++i) {
185     Limb lo;
186     Limb hi;
187     bn_umult_lohi(&lo, &hi, a[i], b);
188     Limb tmp;
189     Carry c = limb_add(&tmp, lo, carried);
190     c = limb_adc(&carried, hi, 0, c);
191     dev_assert_secret(c == 0);
192     c = limb_add(&r[i], r[i], tmp);
193     c = limb_adc(&carried, carried, 0, c);
194     // (A * B) + C + D never carries.
195     dev_assert_secret(c == 0);
196   }
197   return carried;
198 }
199