1 /* ec_dsa.c - TinyCrypt implementation of EC-DSA */
2
3 /* Copyright (c) 2014, Kenneth MacKay
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE. */
25
26 /*
27 * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions are met:
31 *
32 * - Redistributions of source code must retain the above copyright notice,
33 * this list of conditions and the following disclaimer.
34 *
35 * - Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 *
39 * - Neither the name of Intel Corporation nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
44 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
47 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
51 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
53 * POSSIBILITY OF SUCH DAMAGE.
54 */
55
56 #include <tinycrypt/constants.h>
57 #include <tinycrypt/ecc.h>
58 #include <tinycrypt/ecc_dsa.h>
59
60 #if default_RNG_defined
61 static uECC_RNG_Function g_rng_function = &default_CSPRNG;
62 #else
63 static uECC_RNG_Function g_rng_function = 0;
64 #endif
65
bits2int(uECC_word_t * native,const uint8_t * bits,unsigned bits_size,uECC_Curve curve)66 static void bits2int(uECC_word_t *native, const uint8_t *bits,
67 unsigned bits_size, uECC_Curve curve)
68 {
69 unsigned Bits_size = bits_size;
70 unsigned num_n_bytes = BITS_TO_BYTES(curve->num_n_bits);
71 unsigned num_n_words = BITS_TO_WORDS(curve->num_n_bits);
72 int shift;
73 uECC_word_t carry;
74 uECC_word_t *ptr;
75
76 if (Bits_size > num_n_bytes) {
77 Bits_size = num_n_bytes;
78 }
79
80 uECC_vli_clear(native, num_n_words);
81 uECC_vli_bytesToNative(native, bits, Bits_size);
82
83 if (Bits_size * 8 <= (unsigned)curve->num_n_bits) { // 8:byte alignment
84 return;
85 }
86
87 shift = Bits_size * 8 - curve->num_n_bits; // 8:byte alignment
88 carry = 0;
89 ptr = native + num_n_words;
90
91 while (ptr-- > native) {
92 uECC_word_t temp = *ptr;
93 *ptr = (temp >> shift) | carry;
94 carry = temp << (uECC_WORD_BITS - shift);
95 }
96
97 /* Reduce mod curve_n */
98 if (uECC_vli_cmp_unsafe(curve->n, native, num_n_words) != 1) {
99 uECC_vli_sub(native, native, curve->n, num_n_words);
100 }
101 }
102
uECC_sign_with_k(const uint8_t * private_key,const uint8_t * message_hash,unsigned hash_size,uECC_word_t * k,uint8_t * signature,uECC_Curve curve)103 int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
104 unsigned hash_size, uECC_word_t *k, uint8_t *signature,
105 uECC_Curve curve)
106 {
107 uECC_word_t tmp[NUM_ECC_WORDS];
108 uECC_word_t s[NUM_ECC_WORDS];
109 uECC_word_t *k2[2] = {tmp, s};
110 uECC_word_t p[NUM_ECC_WORDS * 2];
111 uECC_word_t carry;
112 wordcount_t num_words = curve->num_words;
113 wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
114 bitcount_t num_n_bits = curve->num_n_bits;
115
116 /* Make sure 0 < k < curve_n */
117 if (uECC_vli_isZero(k, num_words) ||
118 uECC_vli_cmp(curve->n, k, num_n_words) != 1) {
119 return 0;
120 }
121
122 carry = regularize_k(k, tmp, s, curve);
123 EccPoint_mult(p, curve->G, k2[!carry], 0, num_n_bits + 1, curve);
124
125 if (uECC_vli_isZero(p, num_words)) {
126 return 0;
127 }
128
129 /* If an RNG function was specified, get a random number
130 to prevent side channel analysis of k. */
131 if (!g_rng_function) {
132 uECC_vli_clear(tmp, num_n_words);
133 tmp[0] = 1;
134 } else if (!uECC_generate_random_int(tmp, curve->n, num_n_words)) {
135 return 0;
136 }
137
138 /* Prevent side channel analysis of uECC_vli_modInv() to determine
139 bits of k / the private key by premultiplying by a random number */
140 uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k' = rand * k */
141 uECC_vli_modInv(k, k, curve->n, num_n_words); /* k = 1 / k' */
142 uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k = 1 / k */
143 uECC_vli_nativeToBytes(signature, curve->num_bytes, p); /* store r */
144 /* tmp = d: */
145 uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(curve->num_n_bits));
146 s[num_n_words - 1] = 0;
147 uECC_vli_set(s, p, num_words);
148 uECC_vli_modMult(s, tmp, s, curve->n, num_n_words); /* s = r*d */
149 bits2int(tmp, message_hash, hash_size, curve);
150 uECC_vli_modAdd(s, tmp, s, curve->n, num_n_words); /* s = e + r*d */
151 uECC_vli_modMult(s, s, k, curve->n, num_n_words); /* s = (e + r*d) / k */
152
153 if (uECC_vli_numBits(s, num_n_words) > (bitcount_t)curve->num_bytes * 8) { // 8:byte alignment
154 return 0;
155 }
156
157 uECC_vli_nativeToBytes(signature + curve->num_bytes, curve->num_bytes, s);
158 return 1;
159 }
160
uECC_sign(const uint8_t * private_key,const uint8_t * message_hash,unsigned hash_size,uint8_t * signature,uECC_Curve curve)161 int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash,
162 unsigned hash_size, uint8_t *signature, uECC_Curve curve)
163 {
164 uECC_word_t _random[2 * NUM_ECC_WORDS]; // 2:byte alignment
165 uECC_word_t k[NUM_ECC_WORDS];
166 uECC_word_t tries;
167
168 for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
169 /* Generating _random uniformly at random: */
170 uECC_RNG_Function rng_function = uECC_get_rng();
171 if (!rng_function ||
172 !rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS * uECC_WORD_SIZE)) { // 2:byte alignment
173 return 0;
174 }
175
176 // computing k as modular reduction of _random (see FIPS 186.4 B.5.1):
177 uECC_vli_mmod(k, _random, curve->n, BITS_TO_WORDS(curve->num_n_bits));
178
179 if (uECC_sign_with_k(private_key, message_hash, hash_size, k, signature, curve)) {
180 return 1;
181 }
182 }
183
184 return 0;
185 }
186
smax(bitcount_t a,bitcount_t b)187 static bitcount_t smax(bitcount_t a, bitcount_t b)
188 {
189 return (a > b ? a : b);
190 }
191
uECC_verify(const uint8_t * public_key,const uint8_t * message_hash,unsigned hash_size,const uint8_t * signature,uECC_Curve curve)192 int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
193 unsigned hash_size, const uint8_t *signature,
194 uECC_Curve curve)
195 {
196 uECC_word_t u1[NUM_ECC_WORDS], u2[NUM_ECC_WORDS];
197 uECC_word_t z[NUM_ECC_WORDS];
198 uECC_word_t sum[NUM_ECC_WORDS * 2]; // 2:byte alignment
199 uECC_word_t rx[NUM_ECC_WORDS];
200 uECC_word_t ry[NUM_ECC_WORDS];
201 uECC_word_t tx[NUM_ECC_WORDS];
202 uECC_word_t ty[NUM_ECC_WORDS];
203 uECC_word_t tz[NUM_ECC_WORDS];
204 const uECC_word_t *points[4]; // 4:array element
205 const uECC_word_t *point;
206 bitcount_t num_bits;
207 bitcount_t i;
208 uECC_word_t _public[NUM_ECC_WORDS * 2]; // 2:byte alignment
209 uECC_word_t r[NUM_ECC_WORDS], s[NUM_ECC_WORDS];
210 wordcount_t num_words = curve->num_words;
211 wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
212 rx[num_n_words - 1] = 0;
213 r[num_n_words - 1] = 0;
214 s[num_n_words - 1] = 0;
215 uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
216 uECC_vli_bytesToNative(_public + num_words, public_key + curve->num_bytes,
217 curve->num_bytes);
218 uECC_vli_bytesToNative(r, signature, curve->num_bytes);
219 uECC_vli_bytesToNative(s, signature + curve->num_bytes, curve->num_bytes);
220
221 /* r, s must not be 0. */
222 if (uECC_vli_isZero(r, num_words) || uECC_vli_isZero(s, num_words)) {
223 return 0;
224 }
225
226 /* r, s must be < n. */
227 if (uECC_vli_cmp_unsafe(curve->n, r, num_n_words) != 1 ||
228 uECC_vli_cmp_unsafe(curve->n, s, num_n_words) != 1) {
229 return 0;
230 }
231
232 /* Calculate u1 and u2. */
233 uECC_vli_modInv(z, s, curve->n, num_n_words); /* z = 1/s */
234 u1[num_n_words - 1] = 0;
235 bits2int(u1, message_hash, hash_size, curve);
236 uECC_vli_modMult(u1, u1, z, curve->n, num_n_words); /* u1 = e/s */
237 uECC_vli_modMult(u2, r, z, curve->n, num_n_words); /* u2 = r/s */
238 /* Calculate sum = G + Q. */
239 uECC_vli_set(sum, _public, num_words);
240 uECC_vli_set(sum + num_words, _public + num_words, num_words);
241 uECC_vli_set(tx, curve->G, num_words);
242 uECC_vli_set(ty, curve->G + num_words, num_words);
243 uECC_vli_modSub(z, sum, tx, curve->p, num_words); /* z = x2 - x1 */
244 XYcZ_add(tx, ty, sum, sum + num_words, curve);
245 uECC_vli_modInv(z, z, curve->p, num_words); /* z = 1/z */
246 apply_z(sum, sum + num_words, z, curve);
247 /* Use Shamir's trick to calculate u1*G + u2*Q */
248 points[0] = 0;
249 points[1] = curve->G;
250 points[2] = _public; // 2:array element
251 points[3] = sum; // 3:array element
252 num_bits = smax(uECC_vli_numBits(u1, num_n_words),
253 uECC_vli_numBits(u2, num_n_words));
254 point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
255 ((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
256 uECC_vli_set(rx, point, num_words);
257 uECC_vli_set(ry, point + num_words, num_words);
258 uECC_vli_clear(z, num_words);
259 z[0] = 1;
260
261 for (i = num_bits - 2; i >= 0; --i) { // 2:byte alignment
262 uECC_word_t index;
263 curve->double_jacobian(rx, ry, z, curve);
264 index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1);
265 point = points[index];
266
267 if (point) {
268 uECC_vli_set(tx, point, num_words);
269 uECC_vli_set(ty, point + num_words, num_words);
270 apply_z(tx, ty, z, curve);
271 uECC_vli_modSub(tz, rx, tx, curve->p, num_words); /* Z = x2 - x1 */
272 XYcZ_add(tx, ty, rx, ry, curve);
273 uECC_vli_modMult_fast(z, z, tz, curve);
274 }
275 }
276
277 uECC_vli_modInv(z, z, curve->p, num_words); /* Z = 1/Z */
278 apply_z(rx, ry, z, curve);
279
280 /* v = x1 (mod n) */
281 if (uECC_vli_cmp_unsafe(curve->n, rx, num_n_words) != 1) {
282 uECC_vli_sub(rx, rx, curve->n, num_n_words);
283 }
284
285 /* Accept only if v == r. */
286 return (int)(uECC_vli_equal(rx, r, num_words) == 0);
287 }