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