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 }
131 else if (!uECC_generate_random_int(tmp, curve->n, num_n_words)) {
132 return 0;
133 }
134
135 /* Prevent side channel analysis of uECC_vli_modInv() to determine
136 bits of k / the private key by premultiplying by a random number */
137 uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k' = rand * k */
138 uECC_vli_modInv(k, k, curve->n, num_n_words); /* k = 1 / k' */
139 uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k = 1 / k */
140
141 uECC_vli_nativeToBytes(signature, curve->num_bytes, p); /* store r */
142
143 /* tmp = d: */
144 uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(curve->num_n_bits));
145
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
150 bits2int(tmp, message_hash, hash_size, curve);
151 uECC_vli_modAdd(s, tmp, s, curve->n, num_n_words); /* s = e + r*d */
152 uECC_vli_modMult(s, s, k, curve->n, num_n_words); /* s = (e + r*d) / k */
153 if (uECC_vli_numBits(s, num_n_words) > (bitcount_t)curve->num_bytes * 8) {
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];
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)) {
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,
180 curve)) {
181 return 1;
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
197 uECC_word_t u1[NUM_ECC_WORDS], u2[NUM_ECC_WORDS];
198 uECC_word_t z[NUM_ECC_WORDS];
199 uECC_word_t sum[NUM_ECC_WORDS * 2];
200 uECC_word_t rx[NUM_ECC_WORDS];
201 uECC_word_t ry[NUM_ECC_WORDS];
202 uECC_word_t tx[NUM_ECC_WORDS];
203 uECC_word_t ty[NUM_ECC_WORDS];
204 uECC_word_t tz[NUM_ECC_WORDS];
205 const uECC_word_t *points[4];
206 const uECC_word_t *point;
207 bitcount_t num_bits;
208 bitcount_t i;
209
210 uECC_word_t _public[NUM_ECC_WORDS * 2];
211 uECC_word_t r[NUM_ECC_WORDS], s[NUM_ECC_WORDS];
212 wordcount_t num_words = curve->num_words;
213 wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
214
215 rx[num_n_words - 1] = 0;
216 r[num_n_words - 1] = 0;
217 s[num_n_words - 1] = 0;
218
219 uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
220 uECC_vli_bytesToNative(_public + num_words, public_key + curve->num_bytes,
221 curve->num_bytes);
222 uECC_vli_bytesToNative(r, signature, curve->num_bytes);
223 uECC_vli_bytesToNative(s, signature + curve->num_bytes, curve->num_bytes);
224
225 /* r, s must not be 0. */
226 if (uECC_vli_isZero(r, num_words) || uECC_vli_isZero(s, num_words)) {
227 return 0;
228 }
229
230 /* r, s must be < n. */
231 if (uECC_vli_cmp_unsafe(curve->n, r, num_n_words) != 1 ||
232 uECC_vli_cmp_unsafe(curve->n, s, num_n_words) != 1) {
233 return 0;
234 }
235
236 /* Calculate u1 and u2. */
237 uECC_vli_modInv(z, s, curve->n, num_n_words); /* z = 1/s */
238 u1[num_n_words - 1] = 0;
239 bits2int(u1, message_hash, hash_size, curve);
240 uECC_vli_modMult(u1, u1, z, curve->n, num_n_words); /* u1 = e/s */
241 uECC_vli_modMult(u2, r, z, curve->n, num_n_words); /* u2 = r/s */
242
243 /* Calculate sum = G + Q. */
244 uECC_vli_set(sum, _public, num_words);
245 uECC_vli_set(sum + num_words, _public + num_words, num_words);
246 uECC_vli_set(tx, curve->G, num_words);
247 uECC_vli_set(ty, curve->G + num_words, num_words);
248 uECC_vli_modSub(z, sum, tx, curve->p, num_words); /* z = x2 - x1 */
249 XYcZ_add(tx, ty, sum, sum + num_words, curve);
250 uECC_vli_modInv(z, z, curve->p, num_words); /* z = 1/z */
251 apply_z(sum, sum + num_words, z, curve);
252
253 /* Use Shamir's trick to calculate u1*G + u2*Q */
254 points[0] = 0;
255 points[1] = curve->G;
256 points[2] = _public;
257 points[3] = sum;
258 num_bits = smax(uECC_vli_numBits(u1, num_n_words),
259 uECC_vli_numBits(u2, num_n_words));
260
261 point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
262 ((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
263 uECC_vli_set(rx, point, num_words);
264 uECC_vli_set(ry, point + num_words, num_words);
265 uECC_vli_clear(z, num_words);
266 z[0] = 1;
267
268 for (i = num_bits - 2; i >= 0; --i) {
269 uECC_word_t index;
270 curve->double_jacobian(rx, ry, z, curve);
271
272 index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1);
273 point = points[index];
274 if (point) {
275 uECC_vli_set(tx, point, num_words);
276 uECC_vli_set(ty, point + num_words, num_words);
277 apply_z(tx, ty, z, curve);
278 uECC_vli_modSub(tz, rx, tx, curve->p, num_words); /* Z = x2 - x1 */
279 XYcZ_add(tx, ty, rx, ry, curve);
280 uECC_vli_modMult_fast(z, z, tz, curve);
281 }
282 }
283
284 uECC_vli_modInv(z, z, curve->p, num_words); /* Z = 1/Z */
285 apply_z(rx, ry, z, curve);
286
287 /* v = x1 (mod n) */
288 if (uECC_vli_cmp_unsafe(curve->n, rx, num_n_words) != 1) {
289 uECC_vli_sub(rx, rx, curve->n, num_n_words);
290 }
291
292 /* Accept only if v == r. */
293 return (int)(uECC_vli_equal(rx, r, num_words) == 0);
294 }
295