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 num_n_bytes = BITS_TO_BYTES(curve->num_n_bits);
70 unsigned num_n_words = BITS_TO_WORDS(curve->num_n_bits);
71 int shift;
72 uECC_word_t carry;
73 uECC_word_t *ptr;
74
75 if (bits_size > num_n_bytes) {
76 bits_size = num_n_bytes;
77 }
78
79 uECC_vli_clear(native, num_n_words);
80 uECC_vli_bytesToNative(native, bits, bits_size);
81 if (bits_size * 8 <= (unsigned)curve->num_n_bits) {
82 return;
83 }
84 shift = bits_size * 8 - curve->num_n_bits;
85 carry = 0;
86 ptr = native + num_n_words;
87 while (ptr-- > native) {
88 uECC_word_t temp = *ptr;
89 *ptr = (temp >> shift) | carry;
90 carry = temp << (uECC_WORD_BITS - shift);
91 }
92
93 /* Reduce mod curve_n */
94 if (uECC_vli_cmp_unsafe(curve->n, native, num_n_words) != 1) {
95 uECC_vli_sub(native, native, curve->n, num_n_words);
96 }
97 }
98
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)99 int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
100 unsigned hash_size, uECC_word_t *k, uint8_t *signature,
101 uECC_Curve curve)
102 {
103
104 uECC_word_t tmp[NUM_ECC_WORDS];
105 uECC_word_t s[NUM_ECC_WORDS];
106 uECC_word_t *k2[2] = {tmp, s};
107 uECC_word_t p[NUM_ECC_WORDS * 2];
108 uECC_word_t carry;
109 wordcount_t num_words = curve->num_words;
110 wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
111 bitcount_t num_n_bits = curve->num_n_bits;
112
113 /* Make sure 0 < k < curve_n */
114 if (uECC_vli_isZero(k, num_words) ||
115 uECC_vli_cmp(curve->n, k, num_n_words) != 1) {
116 return 0;
117 }
118
119 carry = regularize_k(k, tmp, s, curve);
120 EccPoint_mult(p, curve->G, k2[!carry], 0, num_n_bits + 1, curve);
121 if (uECC_vli_isZero(p, num_words)) {
122 return 0;
123 }
124
125 /* If an RNG function was specified, get a random number
126 to prevent side channel analysis of k. */
127 if (!g_rng_function) {
128 uECC_vli_clear(tmp, num_n_words);
129 tmp[0] = 1;
130 } else if (!uECC_generate_random_int(tmp, curve->n, num_n_words)) {
131 return 0;
132 }
133
134 /* Prevent side channel analysis of uECC_vli_modInv() to determine
135 bits of k / the private key by premultiplying by a random number */
136 uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k' = rand * k */
137 uECC_vli_modInv(k, k, curve->n, num_n_words); /* k = 1 / k' */
138 uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k = 1 / k */
139
140 uECC_vli_nativeToBytes(signature, curve->num_bytes, p); /* store r */
141
142 /* tmp = d: */
143 uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(curve->num_n_bits));
144
145 s[num_n_words - 1] = 0;
146 uECC_vli_set(s, p, num_words);
147 uECC_vli_modMult(s, tmp, s, curve->n, num_n_words); /* s = r*d */
148
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 if (uECC_vli_numBits(s, num_n_words) > (bitcount_t)curve->num_bytes * 8) {
153 return 0;
154 }
155
156 uECC_vli_nativeToBytes(signature + curve->num_bytes, curve->num_bytes, s);
157 return 1;
158 }
159
uECC_sign(const uint8_t * private_key,const uint8_t * message_hash,unsigned hash_size,uint8_t * signature,uECC_Curve curve)160 int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash,
161 unsigned hash_size, uint8_t *signature, uECC_Curve curve)
162 {
163 uECC_word_t _random[2 * NUM_ECC_WORDS];
164 uECC_word_t k[NUM_ECC_WORDS];
165 uECC_word_t tries;
166
167 for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
168 /* Generating _random uniformly at random: */
169 uECC_RNG_Function rng_function = uECC_get_rng();
170 if (!rng_function ||
171 !rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS * uECC_WORD_SIZE)) {
172 return 0;
173 }
174
175 // computing k as modular reduction of _random (see FIPS 186.4 B.5.1):
176 uECC_vli_mmod(k, _random, curve->n, BITS_TO_WORDS(curve->num_n_bits));
177
178 if (uECC_sign_with_k(private_key, message_hash, hash_size, k, signature,
179 curve)) {
180 return 1;
181 }
182 }
183 return 0;
184 }
185
smax(bitcount_t a,bitcount_t b)186 static bitcount_t smax(bitcount_t a, bitcount_t b)
187 {
188 return (a > b ? a : b);
189 }
190
uECC_verify(const uint8_t * public_key,const uint8_t * message_hash,unsigned hash_size,const uint8_t * signature,uECC_Curve curve)191 int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
192 unsigned hash_size, const uint8_t *signature,
193 uECC_Curve curve)
194 {
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];
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];
205 const uECC_word_t *point;
206 bitcount_t num_bits;
207 bitcount_t i;
208
209 uECC_word_t _public[NUM_ECC_WORDS * 2];
210 uECC_word_t r[NUM_ECC_WORDS], s[NUM_ECC_WORDS];
211 wordcount_t num_words = curve->num_words;
212 wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
213
214 rx[num_n_words - 1] = 0;
215 r[num_n_words - 1] = 0;
216 s[num_n_words - 1] = 0;
217
218 uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
219 uECC_vli_bytesToNative(_public + num_words, public_key + curve->num_bytes,
220 curve->num_bytes);
221 uECC_vli_bytesToNative(r, signature, curve->num_bytes);
222 uECC_vli_bytesToNative(s, signature + curve->num_bytes, curve->num_bytes);
223
224 /* r, s must not be 0. */
225 if (uECC_vli_isZero(r, num_words) || uECC_vli_isZero(s, num_words)) {
226 return 0;
227 }
228
229 /* r, s must be < n. */
230 if (uECC_vli_cmp_unsafe(curve->n, r, num_n_words) != 1 ||
231 uECC_vli_cmp_unsafe(curve->n, s, num_n_words) != 1) {
232 return 0;
233 }
234
235 /* Calculate u1 and u2. */
236 uECC_vli_modInv(z, s, curve->n, num_n_words); /* z = 1/s */
237 u1[num_n_words - 1] = 0;
238 bits2int(u1, message_hash, hash_size, curve);
239 uECC_vli_modMult(u1, u1, z, curve->n, num_n_words); /* u1 = e/s */
240 uECC_vli_modMult(u2, r, z, curve->n, num_n_words); /* u2 = r/s */
241
242 /* Calculate sum = G + Q. */
243 uECC_vli_set(sum, _public, num_words);
244 uECC_vli_set(sum + num_words, _public + num_words, num_words);
245 uECC_vli_set(tx, curve->G, num_words);
246 uECC_vli_set(ty, curve->G + num_words, num_words);
247 uECC_vli_modSub(z, sum, tx, curve->p, num_words); /* z = x2 - x1 */
248 XYcZ_add(tx, ty, sum, sum + num_words, curve);
249 uECC_vli_modInv(z, z, curve->p, num_words); /* z = 1/z */
250 apply_z(sum, sum + num_words, z, curve);
251
252 /* Use Shamir's trick to calculate u1*G + u2*Q */
253 points[0] = 0;
254 points[1] = curve->G;
255 points[2] = _public;
256 points[3] = sum;
257 num_bits = smax(uECC_vli_numBits(u1, num_n_words),
258 uECC_vli_numBits(u2, num_n_words));
259
260 point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
261 ((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
262 uECC_vli_set(rx, point, num_words);
263 uECC_vli_set(ry, point + num_words, num_words);
264 uECC_vli_clear(z, num_words);
265 z[0] = 1;
266
267 for (i = num_bits - 2; i >= 0; --i) {
268 uECC_word_t index;
269 curve->double_jacobian(rx, ry, z, curve);
270
271 index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1);
272 point = points[index];
273 if (point) {
274 uECC_vli_set(tx, point, num_words);
275 uECC_vli_set(ty, point + num_words, num_words);
276 apply_z(tx, ty, z, curve);
277 uECC_vli_modSub(tz, rx, tx, curve->p, num_words); /* Z = x2 - x1 */
278 XYcZ_add(tx, ty, rx, ry, curve);
279 uECC_vli_modMult_fast(z, z, tz, curve);
280 }
281 }
282
283 uECC_vli_modInv(z, z, curve->p, num_words); /* Z = 1/Z */
284 apply_z(rx, ry, z, curve);
285
286 /* v = x1 (mod n) */
287 if (uECC_vli_cmp_unsafe(curve->n, rx, num_n_words) != 1) {
288 uECC_vli_sub(rx, rx, curve->n, num_n_words);
289 }
290
291 /* Accept only if v == r. */
292 return (int)(uECC_vli_equal(rx, r, num_words) == 0);
293 }
294