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